Commit | Line | Data |
---|---|---|
ed9eccbe DS |
1 | /* |
2 | module/drivers.c | |
3 | functions for manipulating drivers | |
4 | ||
5 | COMEDI - Linux Control and Measurement Device Interface | |
6 | Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org> | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
ed9eccbe DS |
17 | */ |
18 | ||
ed9eccbe DS |
19 | #include <linux/device.h> |
20 | #include <linux/module.h> | |
ed9eccbe | 21 | #include <linux/errno.h> |
78b10615 | 22 | #include <linux/kconfig.h> |
ed9eccbe DS |
23 | #include <linux/kernel.h> |
24 | #include <linux/sched.h> | |
25 | #include <linux/fcntl.h> | |
ed9eccbe DS |
26 | #include <linux/ioport.h> |
27 | #include <linux/mm.h> | |
28 | #include <linux/slab.h> | |
ed9eccbe DS |
29 | #include <linux/highmem.h> /* for SuSE brokenness */ |
30 | #include <linux/vmalloc.h> | |
31 | #include <linux/cdev.h> | |
32 | #include <linux/dma-mapping.h> | |
5617f9da | 33 | #include <linux/io.h> |
3d1fe3f7 | 34 | #include <linux/interrupt.h> |
9ff8b151 | 35 | #include <linux/firmware.h> |
ed9eccbe | 36 | |
242e7ad9 | 37 | #include "comedidev.h" |
3a5fa275 | 38 | #include "comedi_internal.h" |
242e7ad9 | 39 | |
139dfbdf | 40 | struct comedi_driver *comedi_drivers; |
3df9f21a | 41 | /* protects access to comedi_drivers */ |
c383e2d6 | 42 | DEFINE_MUTEX(comedi_drivers_list_lock); |
ed9eccbe | 43 | |
da717511 IA |
44 | int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev) |
45 | { | |
de06d7c6 IA |
46 | if (hw_dev == dev->hw_dev) |
47 | return 0; | |
48 | if (dev->hw_dev != NULL) | |
49 | return -EEXIST; | |
da717511 | 50 | dev->hw_dev = get_device(hw_dev); |
da717511 IA |
51 | return 0; |
52 | } | |
53 | EXPORT_SYMBOL_GPL(comedi_set_hw_dev); | |
54 | ||
de06d7c6 IA |
55 | static void comedi_clear_hw_dev(struct comedi_device *dev) |
56 | { | |
57 | put_device(dev->hw_dev); | |
58 | dev->hw_dev = NULL; | |
59 | } | |
60 | ||
54db996e HS |
61 | /** |
62 | * comedi_alloc_devpriv() - Allocate memory for the device private data. | |
63 | * @dev: comedi_device struct | |
64 | * @size: size of the memory to allocate | |
65 | */ | |
66 | void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size) | |
67 | { | |
68 | dev->private = kzalloc(size, GFP_KERNEL); | |
69 | return dev->private; | |
70 | } | |
71 | EXPORT_SYMBOL_GPL(comedi_alloc_devpriv); | |
72 | ||
8b9ba6e5 | 73 | int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices) |
2f0b9d08 | 74 | { |
03afcf47 | 75 | struct comedi_subdevice *s; |
8b9ba6e5 | 76 | int i; |
2f0b9d08 | 77 | |
7f801c41 HS |
78 | if (num_subdevices < 1) |
79 | return -EINVAL; | |
03afcf47 HS |
80 | |
81 | s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL); | |
82 | if (!s) | |
2f0b9d08 | 83 | return -ENOMEM; |
03afcf47 | 84 | dev->subdevices = s; |
fba1d0fa | 85 | dev->n_subdevices = num_subdevices; |
03afcf47 | 86 | |
2f0b9d08 | 87 | for (i = 0; i < num_subdevices; ++i) { |
5e4c58ce | 88 | s = &dev->subdevices[i]; |
03afcf47 | 89 | s->device = dev; |
90a35c15 | 90 | s->index = i; |
03afcf47 HS |
91 | s->async_dma_dir = DMA_NONE; |
92 | spin_lock_init(&s->spin_lock); | |
93 | s->minor = -1; | |
2f0b9d08 HS |
94 | } |
95 | return 0; | |
96 | } | |
97 | EXPORT_SYMBOL_GPL(comedi_alloc_subdevices); | |
98 | ||
d2762066 HS |
99 | /** |
100 | * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback. | |
101 | * @s: comedi_subdevice struct | |
102 | */ | |
103 | int comedi_alloc_subdev_readback(struct comedi_subdevice *s) | |
104 | { | |
105 | if (!s->n_chan) | |
106 | return -EINVAL; | |
107 | ||
108 | s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL); | |
109 | if (!s->readback) | |
110 | return -ENOMEM; | |
111 | return 0; | |
112 | } | |
113 | EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback); | |
114 | ||
3867e20d | 115 | static void comedi_device_detach_cleanup(struct comedi_device *dev) |
ed9eccbe DS |
116 | { |
117 | int i; | |
34c43922 | 118 | struct comedi_subdevice *s; |
ed9eccbe DS |
119 | |
120 | if (dev->subdevices) { | |
121 | for (i = 0; i < dev->n_subdevices; i++) { | |
5e4c58ce | 122 | s = &dev->subdevices[i]; |
588ba6dc HS |
123 | if (s->runflags & SRF_FREE_SPRIV) |
124 | kfree(s->private); | |
ed9eccbe DS |
125 | comedi_free_subdevice_minor(s); |
126 | if (s->async) { | |
127 | comedi_buf_alloc(dev, s, 0); | |
128 | kfree(s->async); | |
129 | } | |
d2762066 | 130 | kfree(s->readback); |
ed9eccbe DS |
131 | } |
132 | kfree(dev->subdevices); | |
133 | dev->subdevices = NULL; | |
134 | dev->n_subdevices = 0; | |
135 | } | |
dedd1325 BP |
136 | kfree(dev->private); |
137 | dev->private = NULL; | |
7029a874 | 138 | dev->driver = NULL; |
ed9eccbe DS |
139 | dev->board_name = NULL; |
140 | dev->board_ptr = NULL; | |
d7e6dc13 | 141 | dev->mmio = NULL; |
ed9eccbe | 142 | dev->iobase = 0; |
316f97f1 | 143 | dev->iolen = 0; |
00ca6884 | 144 | dev->ioenabled = false; |
ed9eccbe DS |
145 | dev->irq = 0; |
146 | dev->read_subdev = NULL; | |
147 | dev->write_subdev = NULL; | |
148 | dev->open = NULL; | |
149 | dev->close = NULL; | |
de06d7c6 | 150 | comedi_clear_hw_dev(dev); |
ed9eccbe DS |
151 | } |
152 | ||
016599f5 | 153 | void comedi_device_detach(struct comedi_device *dev) |
ed9eccbe | 154 | { |
d19db51a | 155 | comedi_device_cancel_all(dev); |
bf11c134 | 156 | down_write(&dev->attach_lock); |
a7401cdd | 157 | dev->attached = false; |
ef77c0b2 | 158 | dev->detach_count++; |
5617f9da | 159 | if (dev->driver) |
ed9eccbe | 160 | dev->driver->detach(dev); |
3867e20d | 161 | comedi_device_detach_cleanup(dev); |
bf11c134 | 162 | up_write(&dev->attach_lock); |
ed9eccbe DS |
163 | } |
164 | ||
01fca378 | 165 | static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s) |
ed9eccbe | 166 | { |
01fca378 | 167 | return -EINVAL; |
ed9eccbe DS |
168 | } |
169 | ||
01fca378 HS |
170 | int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s, |
171 | struct comedi_insn *insn, unsigned int *data) | |
ed9eccbe | 172 | { |
01fca378 | 173 | return -EINVAL; |
ed9eccbe DS |
174 | } |
175 | ||
d2762066 HS |
176 | /** |
177 | * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback. | |
178 | * @dev: comedi_device struct | |
179 | * @s: comedi_subdevice struct | |
180 | * @insn: comedi_insn struct | |
181 | * @data: pointer to return the readback data | |
182 | */ | |
183 | int comedi_readback_insn_read(struct comedi_device *dev, | |
184 | struct comedi_subdevice *s, | |
185 | struct comedi_insn *insn, | |
186 | unsigned int *data) | |
187 | { | |
188 | unsigned int chan = CR_CHAN(insn->chanspec); | |
189 | int i; | |
190 | ||
191 | if (!s->readback) | |
192 | return -EINVAL; | |
193 | ||
194 | for (i = 0; i < insn->n; i++) | |
195 | data[i] = s->readback[chan]; | |
196 | ||
197 | return insn->n; | |
198 | } | |
199 | EXPORT_SYMBOL_GPL(comedi_readback_insn_read); | |
200 | ||
91506408 HS |
201 | /** |
202 | * comedi_timeout() - busy-wait for a driver condition to occur. | |
203 | * @dev: comedi_device struct | |
204 | * @s: comedi_subdevice struct | |
205 | * @insn: comedi_insn struct | |
206 | * @cb: callback to check for the condition | |
207 | * @context: private context from the driver | |
208 | */ | |
209 | int comedi_timeout(struct comedi_device *dev, | |
210 | struct comedi_subdevice *s, | |
211 | struct comedi_insn *insn, | |
212 | int (*cb)(struct comedi_device *dev, | |
213 | struct comedi_subdevice *s, | |
214 | struct comedi_insn *insn, | |
215 | unsigned long context), | |
216 | unsigned long context) | |
217 | { | |
218 | unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS); | |
219 | int ret; | |
220 | ||
221 | while (time_before(jiffies, timeout)) { | |
222 | ret = cb(dev, s, insn, context); | |
223 | if (ret != -EBUSY) | |
224 | return ret; /* success (0) or non EBUSY errno */ | |
225 | cpu_relax(); | |
226 | } | |
227 | return -ETIMEDOUT; | |
228 | } | |
229 | EXPORT_SYMBOL_GPL(comedi_timeout); | |
230 | ||
e523c6c8 HS |
231 | /** |
232 | * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices. | |
233 | * @dev: comedi_device struct | |
234 | * @s: comedi_subdevice struct | |
235 | * @insn: comedi_insn struct | |
236 | * @data: parameters for the @insn | |
237 | * @mask: io_bits mask for grouped channels | |
238 | */ | |
239 | int comedi_dio_insn_config(struct comedi_device *dev, | |
240 | struct comedi_subdevice *s, | |
241 | struct comedi_insn *insn, | |
242 | unsigned int *data, | |
243 | unsigned int mask) | |
244 | { | |
245 | unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec); | |
246 | ||
247 | if (!mask) | |
248 | mask = chan_mask; | |
249 | ||
250 | switch (data[0]) { | |
251 | case INSN_CONFIG_DIO_INPUT: | |
252 | s->io_bits &= ~mask; | |
253 | break; | |
254 | ||
255 | case INSN_CONFIG_DIO_OUTPUT: | |
256 | s->io_bits |= mask; | |
257 | break; | |
258 | ||
259 | case INSN_CONFIG_DIO_QUERY: | |
260 | data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT; | |
261 | return insn->n; | |
262 | ||
263 | default: | |
264 | return -EINVAL; | |
265 | } | |
266 | ||
267 | return 0; | |
268 | } | |
269 | EXPORT_SYMBOL_GPL(comedi_dio_insn_config); | |
270 | ||
05e60b13 HS |
271 | /** |
272 | * comedi_dio_update_state() - update the internal state of DIO subdevices. | |
273 | * @s: comedi_subdevice struct | |
274 | * @data: the channel mask and bits to update | |
275 | */ | |
276 | unsigned int comedi_dio_update_state(struct comedi_subdevice *s, | |
277 | unsigned int *data) | |
278 | { | |
279 | unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1) | |
280 | : 0xffffffff; | |
281 | unsigned int mask = data[0] & chanmask; | |
282 | unsigned int bits = data[1]; | |
283 | ||
284 | if (mask) { | |
285 | s->state &= ~mask; | |
286 | s->state |= (bits & mask); | |
287 | } | |
288 | ||
289 | return mask; | |
290 | } | |
291 | EXPORT_SYMBOL_GPL(comedi_dio_update_state); | |
292 | ||
01fca378 HS |
293 | static int insn_rw_emulate_bits(struct comedi_device *dev, |
294 | struct comedi_subdevice *s, | |
295 | struct comedi_insn *insn, unsigned int *data) | |
ed9eccbe | 296 | { |
01fca378 HS |
297 | struct comedi_insn new_insn; |
298 | int ret; | |
299 | static const unsigned channels_per_bitfield = 32; | |
ed9eccbe | 300 | |
01fca378 HS |
301 | unsigned chan = CR_CHAN(insn->chanspec); |
302 | const unsigned base_bitfield_channel = | |
303 | (chan < channels_per_bitfield) ? 0 : chan; | |
304 | unsigned int new_data[2]; | |
1e4742df | 305 | |
01fca378 HS |
306 | memset(new_data, 0, sizeof(new_data)); |
307 | memset(&new_insn, 0, sizeof(new_insn)); | |
308 | new_insn.insn = INSN_BITS; | |
309 | new_insn.chanspec = base_bitfield_channel; | |
310 | new_insn.n = 2; | |
311 | new_insn.subdev = insn->subdev; | |
ed9eccbe | 312 | |
01fca378 HS |
313 | if (insn->insn == INSN_WRITE) { |
314 | if (!(s->subdev_flags & SDF_WRITABLE)) | |
315 | return -EINVAL; | |
316 | new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */ | |
317 | new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) | |
318 | : 0; /* bits */ | |
ed9eccbe DS |
319 | } |
320 | ||
01fca378 HS |
321 | ret = s->insn_bits(dev, s, &new_insn, new_data); |
322 | if (ret < 0) | |
323 | return ret; | |
ed9eccbe | 324 | |
01fca378 HS |
325 | if (insn->insn == INSN_READ) |
326 | data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1; | |
327 | ||
328 | return 1; | |
ed9eccbe DS |
329 | } |
330 | ||
40f58a65 HS |
331 | static int __comedi_device_postconfig_async(struct comedi_device *dev, |
332 | struct comedi_subdevice *s) | |
333 | { | |
334 | struct comedi_async *async; | |
335 | unsigned int buf_size; | |
336 | int ret; | |
337 | ||
57b71c3e HS |
338 | if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) { |
339 | dev_warn(dev->class_dev, | |
340 | "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n"); | |
341 | return -EINVAL; | |
342 | } | |
343 | if (!s->do_cmdtest) { | |
344 | dev_warn(dev->class_dev, | |
345 | "async subdevices must have a do_cmdtest() function\n"); | |
346 | return -EINVAL; | |
347 | } | |
40f58a65 HS |
348 | |
349 | async = kzalloc(sizeof(*async), GFP_KERNEL); | |
78110bb8 | 350 | if (!async) |
40f58a65 | 351 | return -ENOMEM; |
78110bb8 | 352 | |
40f58a65 | 353 | init_waitqueue_head(&async->wait_head); |
40f58a65 HS |
354 | s->async = async; |
355 | ||
356 | async->max_bufsize = comedi_default_buf_maxsize_kb * 1024; | |
357 | buf_size = comedi_default_buf_size_kb * 1024; | |
358 | if (buf_size > async->max_bufsize) | |
359 | buf_size = async->max_bufsize; | |
360 | ||
361 | if (comedi_buf_alloc(dev, s, buf_size) < 0) { | |
362 | dev_warn(dev->class_dev, "Buffer allocation failed\n"); | |
363 | return -ENOMEM; | |
364 | } | |
365 | if (s->buf_change) { | |
d546b896 | 366 | ret = s->buf_change(dev, s); |
40f58a65 HS |
367 | if (ret < 0) |
368 | return ret; | |
369 | } | |
370 | ||
f65cc544 | 371 | comedi_alloc_subdevice_minor(s); |
40f58a65 HS |
372 | |
373 | return 0; | |
374 | } | |
375 | ||
376 | static int __comedi_device_postconfig(struct comedi_device *dev) | |
ed9eccbe | 377 | { |
34c43922 | 378 | struct comedi_subdevice *s; |
ed9eccbe | 379 | int ret; |
40f58a65 | 380 | int i; |
ed9eccbe DS |
381 | |
382 | for (i = 0; i < dev->n_subdevices; i++) { | |
5e4c58ce | 383 | s = &dev->subdevices[i]; |
ed9eccbe DS |
384 | |
385 | if (s->type == COMEDI_SUBD_UNUSED) | |
386 | continue; | |
387 | ||
09567cb4 HS |
388 | if (s->type == COMEDI_SUBD_DO) { |
389 | if (s->n_chan < 32) | |
390 | s->io_bits = (1 << s->n_chan) - 1; | |
391 | else | |
392 | s->io_bits = 0xffffffff; | |
393 | } | |
394 | ||
ed9eccbe DS |
395 | if (s->len_chanlist == 0) |
396 | s->len_chanlist = 1; | |
397 | ||
398 | if (s->do_cmd) { | |
40f58a65 HS |
399 | ret = __comedi_device_postconfig_async(dev, s); |
400 | if (ret) | |
401 | return ret; | |
ed9eccbe DS |
402 | } |
403 | ||
404 | if (!s->range_table && !s->range_table_list) | |
405 | s->range_table = &range_unknown; | |
406 | ||
407 | if (!s->insn_read && s->insn_bits) | |
408 | s->insn_read = insn_rw_emulate_bits; | |
409 | if (!s->insn_write && s->insn_bits) | |
410 | s->insn_write = insn_rw_emulate_bits; | |
411 | ||
412 | if (!s->insn_read) | |
413 | s->insn_read = insn_inval; | |
414 | if (!s->insn_write) | |
415 | s->insn_write = insn_inval; | |
416 | if (!s->insn_bits) | |
417 | s->insn_bits = insn_inval; | |
418 | if (!s->insn_config) | |
419 | s->insn_config = insn_inval; | |
420 | ||
421 | if (!s->poll) | |
422 | s->poll = poll_invalid; | |
423 | } | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
01fca378 | 428 | /* do a little post-config cleanup */ |
01fca378 HS |
429 | static int comedi_device_postconfig(struct comedi_device *dev) |
430 | { | |
b2a644b4 IA |
431 | int ret; |
432 | ||
433 | ret = __comedi_device_postconfig(dev); | |
ae5dd5fc | 434 | if (ret < 0) |
01fca378 | 435 | return ret; |
bf11c134 | 436 | down_write(&dev->attach_lock); |
a7401cdd | 437 | dev->attached = true; |
bf11c134 | 438 | up_write(&dev->attach_lock); |
01fca378 HS |
439 | return 0; |
440 | } | |
441 | ||
4e2f002f IA |
442 | /* |
443 | * Generic recognize function for drivers that register their supported | |
444 | * board names. | |
445 | * | |
446 | * 'driv->board_name' points to a 'const char *' member within the | |
447 | * zeroth element of an array of some private board information | |
448 | * structure, say 'struct foo_board' containing a member 'const char | |
449 | * *board_name' that is initialized to point to a board name string that | |
450 | * is one of the candidates matched against this function's 'name' | |
451 | * parameter. | |
452 | * | |
453 | * 'driv->offset' is the size of the private board information | |
454 | * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is | |
455 | * the length of the array of private board information structures. | |
456 | * | |
457 | * If one of the board names in the array of private board information | |
458 | * structures matches the name supplied to this function, the function | |
459 | * returns a pointer to the pointer to the board name, otherwise it | |
460 | * returns NULL. The return value ends up in the 'board_ptr' member of | |
461 | * a 'struct comedi_device' that the low-level comedi driver's | |
462 | * 'attach()' hook can convert to a point to a particular element of its | |
463 | * array of private board information structures by subtracting the | |
464 | * offset of the member that points to the board name. (No subtraction | |
465 | * is required if the board name pointer is the first member of the | |
466 | * private board information structure, which is generally the case.) | |
467 | */ | |
7029a874 | 468 | static void *comedi_recognize(struct comedi_driver *driv, const char *name) |
ed9eccbe | 469 | { |
1c9de58a DC |
470 | char **name_ptr = (char **)driv->board_name; |
471 | int i; | |
472 | ||
ed9eccbe DS |
473 | for (i = 0; i < driv->num_names; i++) { |
474 | if (strcmp(*name_ptr, name) == 0) | |
1c9de58a DC |
475 | return name_ptr; |
476 | name_ptr = (void *)name_ptr + driv->offset; | |
ed9eccbe DS |
477 | } |
478 | ||
479 | return NULL; | |
480 | } | |
481 | ||
7029a874 | 482 | static void comedi_report_boards(struct comedi_driver *driv) |
ed9eccbe DS |
483 | { |
484 | unsigned int i; | |
485 | const char *const *name_ptr; | |
486 | ||
4f870fe6 IA |
487 | pr_info("comedi: valid board names for %s driver are:\n", |
488 | driv->driver_name); | |
ed9eccbe DS |
489 | |
490 | name_ptr = driv->board_name; | |
491 | for (i = 0; i < driv->num_names; i++) { | |
4f870fe6 | 492 | pr_info(" %s\n", *name_ptr); |
ed9eccbe DS |
493 | name_ptr = (const char **)((char *)name_ptr + driv->offset); |
494 | } | |
495 | ||
496 | if (driv->num_names == 0) | |
4f870fe6 | 497 | pr_info(" %s\n", driv->driver_name); |
ed9eccbe DS |
498 | } |
499 | ||
9ff8b151 HS |
500 | /** |
501 | * comedi_load_firmware() - Request and load firmware for a device. | |
502 | * @dev: comedi_device struct | |
503 | * @hw_device: device struct for the comedi_device | |
504 | * @name: the name of the firmware image | |
505 | * @cb: callback to the upload the firmware image | |
d569541e | 506 | * @context: private context from the driver |
9ff8b151 HS |
507 | */ |
508 | int comedi_load_firmware(struct comedi_device *dev, | |
509 | struct device *device, | |
510 | const char *name, | |
511 | int (*cb)(struct comedi_device *dev, | |
d569541e HS |
512 | const u8 *data, size_t size, |
513 | unsigned long context), | |
514 | unsigned long context) | |
9ff8b151 HS |
515 | { |
516 | const struct firmware *fw; | |
517 | int ret; | |
518 | ||
519 | if (!cb) | |
520 | return -EINVAL; | |
521 | ||
522 | ret = request_firmware(&fw, name, device); | |
523 | if (ret == 0) { | |
d569541e | 524 | ret = cb(dev, fw->data, fw->size, context); |
9ff8b151 HS |
525 | release_firmware(fw); |
526 | } | |
527 | ||
c6236c0c | 528 | return ret < 0 ? ret : 0; |
9ff8b151 HS |
529 | } |
530 | EXPORT_SYMBOL_GPL(comedi_load_firmware); | |
531 | ||
f375ac5f | 532 | /** |
ca8b2964 | 533 | * __comedi_request_region() - Request an I/O reqion for a legacy driver. |
f375ac5f HS |
534 | * @dev: comedi_device struct |
535 | * @start: base address of the I/O reqion | |
536 | * @len: length of the I/O region | |
537 | */ | |
ca8b2964 HS |
538 | int __comedi_request_region(struct comedi_device *dev, |
539 | unsigned long start, unsigned long len) | |
f375ac5f HS |
540 | { |
541 | if (!start) { | |
542 | dev_warn(dev->class_dev, | |
543 | "%s: a I/O base address must be specified\n", | |
544 | dev->board_name); | |
545 | return -EINVAL; | |
546 | } | |
547 | ||
548 | if (!request_region(start, len, dev->board_name)) { | |
549 | dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n", | |
550 | dev->board_name, start, len); | |
551 | return -EIO; | |
552 | } | |
f375ac5f HS |
553 | |
554 | return 0; | |
555 | } | |
ca8b2964 HS |
556 | EXPORT_SYMBOL_GPL(__comedi_request_region); |
557 | ||
558 | /** | |
559 | * comedi_request_region() - Request an I/O reqion for a legacy driver. | |
560 | * @dev: comedi_device struct | |
561 | * @start: base address of the I/O reqion | |
562 | * @len: length of the I/O region | |
563 | */ | |
564 | int comedi_request_region(struct comedi_device *dev, | |
565 | unsigned long start, unsigned long len) | |
566 | { | |
567 | int ret; | |
568 | ||
569 | ret = __comedi_request_region(dev, start, len); | |
316f97f1 | 570 | if (ret == 0) { |
ca8b2964 | 571 | dev->iobase = start; |
316f97f1 HS |
572 | dev->iolen = len; |
573 | } | |
ca8b2964 HS |
574 | |
575 | return ret; | |
576 | } | |
f375ac5f HS |
577 | EXPORT_SYMBOL_GPL(comedi_request_region); |
578 | ||
316f97f1 HS |
579 | /** |
580 | * comedi_legacy_detach() - A generic (*detach) function for legacy drivers. | |
581 | * @dev: comedi_device struct | |
582 | */ | |
583 | void comedi_legacy_detach(struct comedi_device *dev) | |
584 | { | |
3d1fe3f7 HS |
585 | if (dev->irq) { |
586 | free_irq(dev->irq, dev); | |
587 | dev->irq = 0; | |
588 | } | |
316f97f1 HS |
589 | if (dev->iobase && dev->iolen) { |
590 | release_region(dev->iobase, dev->iolen); | |
591 | dev->iobase = 0; | |
592 | dev->iolen = 0; | |
593 | } | |
594 | } | |
595 | EXPORT_SYMBOL_GPL(comedi_legacy_detach); | |
596 | ||
01fca378 | 597 | int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
ed9eccbe | 598 | { |
01fca378 HS |
599 | struct comedi_driver *driv; |
600 | int ret; | |
601 | ||
602 | if (dev->attached) | |
603 | return -EBUSY; | |
604 | ||
c383e2d6 | 605 | mutex_lock(&comedi_drivers_list_lock); |
01fca378 HS |
606 | for (driv = comedi_drivers; driv; driv = driv->next) { |
607 | if (!try_module_get(driv->module)) | |
608 | continue; | |
609 | if (driv->num_names) { | |
610 | dev->board_ptr = comedi_recognize(driv, it->board_name); | |
611 | if (dev->board_ptr) | |
612 | break; | |
3df9f21a | 613 | } else if (strcmp(driv->driver_name, it->board_name) == 0) { |
01fca378 | 614 | break; |
3df9f21a | 615 | } |
01fca378 HS |
616 | module_put(driv->module); |
617 | } | |
618 | if (driv == NULL) { | |
619 | /* recognize has failed if we get here */ | |
620 | /* report valid board names before returning error */ | |
621 | for (driv = comedi_drivers; driv; driv = driv->next) { | |
622 | if (!try_module_get(driv->module)) | |
623 | continue; | |
624 | comedi_report_boards(driv); | |
625 | module_put(driv->module); | |
626 | } | |
c383e2d6 IA |
627 | ret = -EIO; |
628 | goto out; | |
01fca378 HS |
629 | } |
630 | if (driv->attach == NULL) { | |
631 | /* driver does not support manual configuration */ | |
632 | dev_warn(dev->class_dev, | |
633 | "driver '%s' does not support attach using comedi_config\n", | |
634 | driv->driver_name); | |
635 | module_put(driv->module); | |
c383e2d6 IA |
636 | ret = -ENOSYS; |
637 | goto out; | |
01fca378 | 638 | } |
01fca378 | 639 | dev->driver = driv; |
34b68400 HS |
640 | dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr |
641 | : dev->driver->driver_name; | |
01fca378 | 642 | ret = driv->attach(dev, it); |
74ece108 IA |
643 | if (ret >= 0) |
644 | ret = comedi_device_postconfig(dev); | |
01fca378 | 645 | if (ret < 0) { |
016599f5 | 646 | comedi_device_detach(dev); |
3955dfa8 | 647 | module_put(driv->module); |
01fca378 | 648 | } |
b2a644b4 | 649 | /* On success, the driver module count has been incremented. */ |
c383e2d6 IA |
650 | out: |
651 | mutex_unlock(&comedi_drivers_list_lock); | |
b2a644b4 | 652 | return ret; |
ed9eccbe DS |
653 | } |
654 | ||
a588da1d IA |
655 | int comedi_auto_config(struct device *hardware_device, |
656 | struct comedi_driver *driver, unsigned long context) | |
f4011670 | 657 | { |
6013a9a5 | 658 | struct comedi_device *dev; |
f4011670 IA |
659 | int ret; |
660 | ||
f08e0ac5 IA |
661 | if (!hardware_device) { |
662 | pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n"); | |
663 | return -EINVAL; | |
664 | } | |
665 | if (!driver) { | |
666 | dev_warn(hardware_device, | |
667 | "BUG! comedi_auto_config called with NULL comedi driver\n"); | |
668 | return -EINVAL; | |
669 | } | |
670 | ||
a588da1d IA |
671 | if (!driver->auto_attach) { |
672 | dev_warn(hardware_device, | |
673 | "BUG! comedi driver '%s' has no auto_attach handler\n", | |
674 | driver->driver_name); | |
675 | return -EINVAL; | |
676 | } | |
677 | ||
6013a9a5 | 678 | dev = comedi_alloc_board_minor(hardware_device); |
bcb6232d BP |
679 | if (IS_ERR(dev)) { |
680 | dev_warn(hardware_device, | |
681 | "driver '%s' could not create device.\n", | |
682 | driver->driver_name); | |
6013a9a5 | 683 | return PTR_ERR(dev); |
bcb6232d | 684 | } |
6013a9a5 | 685 | /* Note: comedi_alloc_board_minor() locked dev->mutex. */ |
f4011670 | 686 | |
6013a9a5 | 687 | dev->driver = driver; |
34b68400 | 688 | dev->board_name = dev->driver->driver_name; |
6013a9a5 | 689 | ret = driver->auto_attach(dev, context); |
74ece108 | 690 | if (ret >= 0) |
6013a9a5 | 691 | ret = comedi_device_postconfig(dev); |
6013a9a5 | 692 | mutex_unlock(&dev->mutex); |
f4011670 | 693 | |
bcb6232d BP |
694 | if (ret < 0) { |
695 | dev_warn(hardware_device, | |
696 | "driver '%s' failed to auto-configure device.\n", | |
697 | driver->driver_name); | |
f5b31e15 | 698 | comedi_release_hardware_device(hardware_device); |
bcb6232d BP |
699 | } else { |
700 | /* | |
701 | * class_dev should be set properly here | |
702 | * after a successful auto config | |
703 | */ | |
704 | dev_info(dev->class_dev, | |
705 | "driver '%s' has successfully auto-configured '%s'.\n", | |
706 | driver->driver_name, dev->board_name); | |
707 | } | |
f4011670 IA |
708 | return ret; |
709 | } | |
8ed705af IA |
710 | EXPORT_SYMBOL_GPL(comedi_auto_config); |
711 | ||
712 | void comedi_auto_unconfig(struct device *hardware_device) | |
ed9eccbe | 713 | { |
c43435d7 IA |
714 | if (hardware_device == NULL) |
715 | return; | |
3346b798 | 716 | comedi_release_hardware_device(hardware_device); |
ed9eccbe | 717 | } |
8ed705af | 718 | EXPORT_SYMBOL_GPL(comedi_auto_unconfig); |
1ae6b20b HS |
719 | |
720 | int comedi_driver_register(struct comedi_driver *driver) | |
721 | { | |
c383e2d6 | 722 | mutex_lock(&comedi_drivers_list_lock); |
1ae6b20b HS |
723 | driver->next = comedi_drivers; |
724 | comedi_drivers = driver; | |
c383e2d6 | 725 | mutex_unlock(&comedi_drivers_list_lock); |
1ae6b20b HS |
726 | |
727 | return 0; | |
728 | } | |
5660e742 | 729 | EXPORT_SYMBOL_GPL(comedi_driver_register); |
1ae6b20b | 730 | |
99c0e269 | 731 | void comedi_driver_unregister(struct comedi_driver *driver) |
1ae6b20b HS |
732 | { |
733 | struct comedi_driver *prev; | |
734 | int i; | |
735 | ||
c383e2d6 IA |
736 | /* unlink the driver */ |
737 | mutex_lock(&comedi_drivers_list_lock); | |
738 | if (comedi_drivers == driver) { | |
739 | comedi_drivers = driver->next; | |
740 | } else { | |
741 | for (prev = comedi_drivers; prev->next; prev = prev->next) { | |
742 | if (prev->next == driver) { | |
743 | prev->next = driver->next; | |
744 | break; | |
745 | } | |
746 | } | |
747 | } | |
748 | mutex_unlock(&comedi_drivers_list_lock); | |
749 | ||
1ae6b20b HS |
750 | /* check for devices using this driver */ |
751 | for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) { | |
a200fadc | 752 | struct comedi_device *dev = comedi_dev_get_from_minor(i); |
1ae6b20b HS |
753 | |
754 | if (!dev) | |
755 | continue; | |
756 | ||
757 | mutex_lock(&dev->mutex); | |
758 | if (dev->attached && dev->driver == driver) { | |
759 | if (dev->use_count) | |
760 | dev_warn(dev->class_dev, | |
761 | "BUG! detaching device with use_count=%d\n", | |
762 | dev->use_count); | |
763 | comedi_device_detach(dev); | |
764 | } | |
765 | mutex_unlock(&dev->mutex); | |
a200fadc | 766 | comedi_dev_put(dev); |
1ae6b20b | 767 | } |
1ae6b20b | 768 | } |
5660e742 | 769 | EXPORT_SYMBOL_GPL(comedi_driver_unregister); |