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