staging: comedi: comedidev.h: remove unused callbacks from comedi_subdevice
[linux-2.6-block.git] / drivers / staging / comedi / drivers.c
CommitLineData
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 40struct comedi_driver *comedi_drivers;
3df9f21a 41/* protects access to comedi_drivers */
c383e2d6 42DEFINE_MUTEX(comedi_drivers_list_lock);
ed9eccbe 43
da717511
IA
44int 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}
53EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
54
de06d7c6
IA
55static 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 */
66void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
67{
68 dev->private = kzalloc(size, GFP_KERNEL);
69 return dev->private;
70}
71EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
72
8b9ba6e5 73int 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}
97EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
98
3867e20d 99static void comedi_device_detach_cleanup(struct comedi_device *dev)
ed9eccbe
DS
100{
101 int i;
34c43922 102 struct comedi_subdevice *s;
ed9eccbe
DS
103
104 if (dev->subdevices) {
105 for (i = 0; i < dev->n_subdevices; i++) {
5e4c58ce 106 s = &dev->subdevices[i];
588ba6dc
HS
107 if (s->runflags & SRF_FREE_SPRIV)
108 kfree(s->private);
ed9eccbe
DS
109 comedi_free_subdevice_minor(s);
110 if (s->async) {
111 comedi_buf_alloc(dev, s, 0);
112 kfree(s->async);
113 }
114 }
115 kfree(dev->subdevices);
116 dev->subdevices = NULL;
117 dev->n_subdevices = 0;
118 }
dedd1325
BP
119 kfree(dev->private);
120 dev->private = NULL;
7029a874 121 dev->driver = NULL;
ed9eccbe
DS
122 dev->board_name = NULL;
123 dev->board_ptr = NULL;
124 dev->iobase = 0;
316f97f1 125 dev->iolen = 0;
00ca6884 126 dev->ioenabled = false;
ed9eccbe
DS
127 dev->irq = 0;
128 dev->read_subdev = NULL;
129 dev->write_subdev = NULL;
130 dev->open = NULL;
131 dev->close = NULL;
de06d7c6 132 comedi_clear_hw_dev(dev);
ed9eccbe
DS
133}
134
016599f5 135void comedi_device_detach(struct comedi_device *dev)
ed9eccbe 136{
d19db51a 137 comedi_device_cancel_all(dev);
bf11c134 138 down_write(&dev->attach_lock);
a7401cdd 139 dev->attached = false;
ef77c0b2 140 dev->detach_count++;
5617f9da 141 if (dev->driver)
ed9eccbe 142 dev->driver->detach(dev);
3867e20d 143 comedi_device_detach_cleanup(dev);
bf11c134 144 up_write(&dev->attach_lock);
ed9eccbe
DS
145}
146
01fca378 147static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
ed9eccbe 148{
01fca378 149 return -EINVAL;
ed9eccbe
DS
150}
151
01fca378
HS
152int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
153 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 154{
01fca378 155 return -EINVAL;
ed9eccbe
DS
156}
157
91506408
HS
158/**
159 * comedi_timeout() - busy-wait for a driver condition to occur.
160 * @dev: comedi_device struct
161 * @s: comedi_subdevice struct
162 * @insn: comedi_insn struct
163 * @cb: callback to check for the condition
164 * @context: private context from the driver
165 */
166int comedi_timeout(struct comedi_device *dev,
167 struct comedi_subdevice *s,
168 struct comedi_insn *insn,
169 int (*cb)(struct comedi_device *dev,
170 struct comedi_subdevice *s,
171 struct comedi_insn *insn,
172 unsigned long context),
173 unsigned long context)
174{
175 unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
176 int ret;
177
178 while (time_before(jiffies, timeout)) {
179 ret = cb(dev, s, insn, context);
180 if (ret != -EBUSY)
181 return ret; /* success (0) or non EBUSY errno */
182 cpu_relax();
183 }
184 return -ETIMEDOUT;
185}
186EXPORT_SYMBOL_GPL(comedi_timeout);
187
e523c6c8
HS
188/**
189 * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
190 * @dev: comedi_device struct
191 * @s: comedi_subdevice struct
192 * @insn: comedi_insn struct
193 * @data: parameters for the @insn
194 * @mask: io_bits mask for grouped channels
195 */
196int comedi_dio_insn_config(struct comedi_device *dev,
197 struct comedi_subdevice *s,
198 struct comedi_insn *insn,
199 unsigned int *data,
200 unsigned int mask)
201{
202 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
203
204 if (!mask)
205 mask = chan_mask;
206
207 switch (data[0]) {
208 case INSN_CONFIG_DIO_INPUT:
209 s->io_bits &= ~mask;
210 break;
211
212 case INSN_CONFIG_DIO_OUTPUT:
213 s->io_bits |= mask;
214 break;
215
216 case INSN_CONFIG_DIO_QUERY:
217 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
218 return insn->n;
219
220 default:
221 return -EINVAL;
222 }
223
224 return 0;
225}
226EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
227
05e60b13
HS
228/**
229 * comedi_dio_update_state() - update the internal state of DIO subdevices.
230 * @s: comedi_subdevice struct
231 * @data: the channel mask and bits to update
232 */
233unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
234 unsigned int *data)
235{
236 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
237 : 0xffffffff;
238 unsigned int mask = data[0] & chanmask;
239 unsigned int bits = data[1];
240
241 if (mask) {
242 s->state &= ~mask;
243 s->state |= (bits & mask);
244 }
245
246 return mask;
247}
248EXPORT_SYMBOL_GPL(comedi_dio_update_state);
249
01fca378
HS
250static int insn_rw_emulate_bits(struct comedi_device *dev,
251 struct comedi_subdevice *s,
252 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 253{
01fca378
HS
254 struct comedi_insn new_insn;
255 int ret;
256 static const unsigned channels_per_bitfield = 32;
ed9eccbe 257
01fca378
HS
258 unsigned chan = CR_CHAN(insn->chanspec);
259 const unsigned base_bitfield_channel =
260 (chan < channels_per_bitfield) ? 0 : chan;
261 unsigned int new_data[2];
1e4742df 262
01fca378
HS
263 memset(new_data, 0, sizeof(new_data));
264 memset(&new_insn, 0, sizeof(new_insn));
265 new_insn.insn = INSN_BITS;
266 new_insn.chanspec = base_bitfield_channel;
267 new_insn.n = 2;
268 new_insn.subdev = insn->subdev;
ed9eccbe 269
01fca378
HS
270 if (insn->insn == INSN_WRITE) {
271 if (!(s->subdev_flags & SDF_WRITABLE))
272 return -EINVAL;
273 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
274 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
275 : 0; /* bits */
ed9eccbe
DS
276 }
277
01fca378
HS
278 ret = s->insn_bits(dev, s, &new_insn, new_data);
279 if (ret < 0)
280 return ret;
ed9eccbe 281
01fca378
HS
282 if (insn->insn == INSN_READ)
283 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
284
285 return 1;
ed9eccbe
DS
286}
287
40f58a65
HS
288static int __comedi_device_postconfig_async(struct comedi_device *dev,
289 struct comedi_subdevice *s)
290{
291 struct comedi_async *async;
292 unsigned int buf_size;
293 int ret;
294
57b71c3e
HS
295 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
296 dev_warn(dev->class_dev,
297 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
298 return -EINVAL;
299 }
300 if (!s->do_cmdtest) {
301 dev_warn(dev->class_dev,
302 "async subdevices must have a do_cmdtest() function\n");
303 return -EINVAL;
304 }
40f58a65
HS
305
306 async = kzalloc(sizeof(*async), GFP_KERNEL);
78110bb8 307 if (!async)
40f58a65 308 return -ENOMEM;
78110bb8 309
40f58a65 310 init_waitqueue_head(&async->wait_head);
40f58a65
HS
311 s->async = async;
312
313 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
314 buf_size = comedi_default_buf_size_kb * 1024;
315 if (buf_size > async->max_bufsize)
316 buf_size = async->max_bufsize;
317
318 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
319 dev_warn(dev->class_dev, "Buffer allocation failed\n");
320 return -ENOMEM;
321 }
322 if (s->buf_change) {
323 ret = s->buf_change(dev, s, buf_size);
324 if (ret < 0)
325 return ret;
326 }
327
f65cc544 328 comedi_alloc_subdevice_minor(s);
40f58a65
HS
329
330 return 0;
331}
332
333static int __comedi_device_postconfig(struct comedi_device *dev)
ed9eccbe 334{
34c43922 335 struct comedi_subdevice *s;
ed9eccbe 336 int ret;
40f58a65 337 int i;
ed9eccbe
DS
338
339 for (i = 0; i < dev->n_subdevices; i++) {
5e4c58ce 340 s = &dev->subdevices[i];
ed9eccbe
DS
341
342 if (s->type == COMEDI_SUBD_UNUSED)
343 continue;
344
09567cb4
HS
345 if (s->type == COMEDI_SUBD_DO) {
346 if (s->n_chan < 32)
347 s->io_bits = (1 << s->n_chan) - 1;
348 else
349 s->io_bits = 0xffffffff;
350 }
351
ed9eccbe
DS
352 if (s->len_chanlist == 0)
353 s->len_chanlist = 1;
354
355 if (s->do_cmd) {
40f58a65
HS
356 ret = __comedi_device_postconfig_async(dev, s);
357 if (ret)
358 return ret;
ed9eccbe
DS
359 }
360
361 if (!s->range_table && !s->range_table_list)
362 s->range_table = &range_unknown;
363
364 if (!s->insn_read && s->insn_bits)
365 s->insn_read = insn_rw_emulate_bits;
366 if (!s->insn_write && s->insn_bits)
367 s->insn_write = insn_rw_emulate_bits;
368
369 if (!s->insn_read)
370 s->insn_read = insn_inval;
371 if (!s->insn_write)
372 s->insn_write = insn_inval;
373 if (!s->insn_bits)
374 s->insn_bits = insn_inval;
375 if (!s->insn_config)
376 s->insn_config = insn_inval;
377
378 if (!s->poll)
379 s->poll = poll_invalid;
380 }
381
382 return 0;
383}
384
01fca378 385/* do a little post-config cleanup */
01fca378
HS
386static int comedi_device_postconfig(struct comedi_device *dev)
387{
b2a644b4
IA
388 int ret;
389
390 ret = __comedi_device_postconfig(dev);
ae5dd5fc 391 if (ret < 0)
01fca378 392 return ret;
bf11c134 393 down_write(&dev->attach_lock);
a7401cdd 394 dev->attached = true;
bf11c134 395 up_write(&dev->attach_lock);
01fca378
HS
396 return 0;
397}
398
4e2f002f
IA
399/*
400 * Generic recognize function for drivers that register their supported
401 * board names.
402 *
403 * 'driv->board_name' points to a 'const char *' member within the
404 * zeroth element of an array of some private board information
405 * structure, say 'struct foo_board' containing a member 'const char
406 * *board_name' that is initialized to point to a board name string that
407 * is one of the candidates matched against this function's 'name'
408 * parameter.
409 *
410 * 'driv->offset' is the size of the private board information
411 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
412 * the length of the array of private board information structures.
413 *
414 * If one of the board names in the array of private board information
415 * structures matches the name supplied to this function, the function
416 * returns a pointer to the pointer to the board name, otherwise it
417 * returns NULL. The return value ends up in the 'board_ptr' member of
418 * a 'struct comedi_device' that the low-level comedi driver's
419 * 'attach()' hook can convert to a point to a particular element of its
420 * array of private board information structures by subtracting the
421 * offset of the member that points to the board name. (No subtraction
422 * is required if the board name pointer is the first member of the
423 * private board information structure, which is generally the case.)
424 */
7029a874 425static void *comedi_recognize(struct comedi_driver *driv, const char *name)
ed9eccbe 426{
1c9de58a
DC
427 char **name_ptr = (char **)driv->board_name;
428 int i;
429
ed9eccbe
DS
430 for (i = 0; i < driv->num_names; i++) {
431 if (strcmp(*name_ptr, name) == 0)
1c9de58a
DC
432 return name_ptr;
433 name_ptr = (void *)name_ptr + driv->offset;
ed9eccbe
DS
434 }
435
436 return NULL;
437}
438
7029a874 439static void comedi_report_boards(struct comedi_driver *driv)
ed9eccbe
DS
440{
441 unsigned int i;
442 const char *const *name_ptr;
443
4f870fe6
IA
444 pr_info("comedi: valid board names for %s driver are:\n",
445 driv->driver_name);
ed9eccbe
DS
446
447 name_ptr = driv->board_name;
448 for (i = 0; i < driv->num_names; i++) {
4f870fe6 449 pr_info(" %s\n", *name_ptr);
ed9eccbe
DS
450 name_ptr = (const char **)((char *)name_ptr + driv->offset);
451 }
452
453 if (driv->num_names == 0)
4f870fe6 454 pr_info(" %s\n", driv->driver_name);
ed9eccbe
DS
455}
456
9ff8b151
HS
457/**
458 * comedi_load_firmware() - Request and load firmware for a device.
459 * @dev: comedi_device struct
460 * @hw_device: device struct for the comedi_device
461 * @name: the name of the firmware image
462 * @cb: callback to the upload the firmware image
d569541e 463 * @context: private context from the driver
9ff8b151
HS
464 */
465int comedi_load_firmware(struct comedi_device *dev,
466 struct device *device,
467 const char *name,
468 int (*cb)(struct comedi_device *dev,
d569541e
HS
469 const u8 *data, size_t size,
470 unsigned long context),
471 unsigned long context)
9ff8b151
HS
472{
473 const struct firmware *fw;
474 int ret;
475
476 if (!cb)
477 return -EINVAL;
478
479 ret = request_firmware(&fw, name, device);
480 if (ret == 0) {
d569541e 481 ret = cb(dev, fw->data, fw->size, context);
9ff8b151
HS
482 release_firmware(fw);
483 }
484
c6236c0c 485 return ret < 0 ? ret : 0;
9ff8b151
HS
486}
487EXPORT_SYMBOL_GPL(comedi_load_firmware);
488
f375ac5f 489/**
ca8b2964 490 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
f375ac5f
HS
491 * @dev: comedi_device struct
492 * @start: base address of the I/O reqion
493 * @len: length of the I/O region
494 */
ca8b2964
HS
495int __comedi_request_region(struct comedi_device *dev,
496 unsigned long start, unsigned long len)
f375ac5f
HS
497{
498 if (!start) {
499 dev_warn(dev->class_dev,
500 "%s: a I/O base address must be specified\n",
501 dev->board_name);
502 return -EINVAL;
503 }
504
505 if (!request_region(start, len, dev->board_name)) {
506 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
507 dev->board_name, start, len);
508 return -EIO;
509 }
f375ac5f
HS
510
511 return 0;
512}
ca8b2964
HS
513EXPORT_SYMBOL_GPL(__comedi_request_region);
514
515/**
516 * comedi_request_region() - Request an I/O reqion for a legacy driver.
517 * @dev: comedi_device struct
518 * @start: base address of the I/O reqion
519 * @len: length of the I/O region
520 */
521int comedi_request_region(struct comedi_device *dev,
522 unsigned long start, unsigned long len)
523{
524 int ret;
525
526 ret = __comedi_request_region(dev, start, len);
316f97f1 527 if (ret == 0) {
ca8b2964 528 dev->iobase = start;
316f97f1
HS
529 dev->iolen = len;
530 }
ca8b2964
HS
531
532 return ret;
533}
f375ac5f
HS
534EXPORT_SYMBOL_GPL(comedi_request_region);
535
316f97f1
HS
536/**
537 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
538 * @dev: comedi_device struct
539 */
540void comedi_legacy_detach(struct comedi_device *dev)
541{
3d1fe3f7
HS
542 if (dev->irq) {
543 free_irq(dev->irq, dev);
544 dev->irq = 0;
545 }
316f97f1
HS
546 if (dev->iobase && dev->iolen) {
547 release_region(dev->iobase, dev->iolen);
548 dev->iobase = 0;
549 dev->iolen = 0;
550 }
551}
552EXPORT_SYMBOL_GPL(comedi_legacy_detach);
553
01fca378 554int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
ed9eccbe 555{
01fca378
HS
556 struct comedi_driver *driv;
557 int ret;
558
559 if (dev->attached)
560 return -EBUSY;
561
c383e2d6 562 mutex_lock(&comedi_drivers_list_lock);
01fca378
HS
563 for (driv = comedi_drivers; driv; driv = driv->next) {
564 if (!try_module_get(driv->module))
565 continue;
566 if (driv->num_names) {
567 dev->board_ptr = comedi_recognize(driv, it->board_name);
568 if (dev->board_ptr)
569 break;
3df9f21a 570 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
01fca378 571 break;
3df9f21a 572 }
01fca378
HS
573 module_put(driv->module);
574 }
575 if (driv == NULL) {
576 /* recognize has failed if we get here */
577 /* report valid board names before returning error */
578 for (driv = comedi_drivers; driv; driv = driv->next) {
579 if (!try_module_get(driv->module))
580 continue;
581 comedi_report_boards(driv);
582 module_put(driv->module);
583 }
c383e2d6
IA
584 ret = -EIO;
585 goto out;
01fca378
HS
586 }
587 if (driv->attach == NULL) {
588 /* driver does not support manual configuration */
589 dev_warn(dev->class_dev,
590 "driver '%s' does not support attach using comedi_config\n",
591 driv->driver_name);
592 module_put(driv->module);
c383e2d6
IA
593 ret = -ENOSYS;
594 goto out;
01fca378 595 }
01fca378 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
607out:
608 mutex_unlock(&comedi_drivers_list_lock);
b2a644b4 609 return ret;
ed9eccbe
DS
610}
611
a588da1d
IA
612int 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
667EXPORT_SYMBOL_GPL(comedi_auto_config);
668
669void 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 675EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
1ae6b20b
HS
676
677int 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 686EXPORT_SYMBOL_GPL(comedi_driver_register);
1ae6b20b 687
99c0e269 688void 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 726EXPORT_SYMBOL_GPL(comedi_driver_unregister);