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