]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/comedi/drivers.c
staging: comedi: comedi_fc: use comedi_bytes_per_scan()
[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
d2762066
HS
99/**
100 * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback.
101 * @s: comedi_subdevice struct
102 */
103int 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}
113EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
114
3867e20d 115static 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 153void 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 165static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
ed9eccbe 166{
01fca378 167 return -EINVAL;
ed9eccbe
DS
168}
169
01fca378
HS
170int 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 */
183int 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}
199EXPORT_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 */
209int 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}
229EXPORT_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 */
239int 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}
269EXPORT_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 */
276unsigned 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}
291EXPORT_SYMBOL_GPL(comedi_dio_update_state);
292
f146fe63
IA
293/**
294 * comedi_bytes_per_scan - get length of asynchronous command "scan" in bytes
295 * @s: comedi_subdevice struct
296 *
297 * Determines the overall scan length according to the subdevice type and the
298 * number of channels in the scan.
299 *
300 * For digital input, output or input/output subdevices, samples for multiple
301 * channels are assumed to be packed into one or more unsigned short or
302 * unsigned int values according to the subdevice's SDF_LSAMPL flag. For other
303 * types of subdevice, samples are assumed to occupy a whole unsigned short or
304 * unsigned int according to the SDF_LSAMPL flag.
305 *
306 * Returns the overall scan length in bytes.
307 */
308unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
309{
310 struct comedi_cmd *cmd = &s->async->cmd;
311 unsigned int num_samples;
312 unsigned int bits_per_sample;
313
314 switch (s->type) {
315 case COMEDI_SUBD_DI:
316 case COMEDI_SUBD_DO:
317 case COMEDI_SUBD_DIO:
318 bits_per_sample = 8 * bytes_per_sample(s);
319 num_samples = (cmd->chanlist_len + bits_per_sample - 1) /
320 bits_per_sample;
321 break;
322 default:
323 num_samples = cmd->chanlist_len;
324 break;
325 }
326 return num_samples * bytes_per_sample(s);
327}
328EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
329
01fca378
HS
330static int insn_rw_emulate_bits(struct comedi_device *dev,
331 struct comedi_subdevice *s,
332 struct comedi_insn *insn, unsigned int *data)
ed9eccbe 333{
01fca378
HS
334 struct comedi_insn new_insn;
335 int ret;
336 static const unsigned channels_per_bitfield = 32;
ed9eccbe 337
01fca378
HS
338 unsigned chan = CR_CHAN(insn->chanspec);
339 const unsigned base_bitfield_channel =
340 (chan < channels_per_bitfield) ? 0 : chan;
341 unsigned int new_data[2];
1e4742df 342
01fca378
HS
343 memset(new_data, 0, sizeof(new_data));
344 memset(&new_insn, 0, sizeof(new_insn));
345 new_insn.insn = INSN_BITS;
346 new_insn.chanspec = base_bitfield_channel;
347 new_insn.n = 2;
348 new_insn.subdev = insn->subdev;
ed9eccbe 349
01fca378
HS
350 if (insn->insn == INSN_WRITE) {
351 if (!(s->subdev_flags & SDF_WRITABLE))
352 return -EINVAL;
353 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
354 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
355 : 0; /* bits */
ed9eccbe
DS
356 }
357
01fca378
HS
358 ret = s->insn_bits(dev, s, &new_insn, new_data);
359 if (ret < 0)
360 return ret;
ed9eccbe 361
01fca378
HS
362 if (insn->insn == INSN_READ)
363 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
364
365 return 1;
ed9eccbe
DS
366}
367
40f58a65
HS
368static int __comedi_device_postconfig_async(struct comedi_device *dev,
369 struct comedi_subdevice *s)
370{
371 struct comedi_async *async;
372 unsigned int buf_size;
373 int ret;
374
57b71c3e
HS
375 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
376 dev_warn(dev->class_dev,
377 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
378 return -EINVAL;
379 }
380 if (!s->do_cmdtest) {
381 dev_warn(dev->class_dev,
382 "async subdevices must have a do_cmdtest() function\n");
383 return -EINVAL;
384 }
40f58a65
HS
385
386 async = kzalloc(sizeof(*async), GFP_KERNEL);
78110bb8 387 if (!async)
40f58a65 388 return -ENOMEM;
78110bb8 389
40f58a65 390 init_waitqueue_head(&async->wait_head);
40f58a65
HS
391 s->async = async;
392
393 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
394 buf_size = comedi_default_buf_size_kb * 1024;
395 if (buf_size > async->max_bufsize)
396 buf_size = async->max_bufsize;
397
398 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
399 dev_warn(dev->class_dev, "Buffer allocation failed\n");
400 return -ENOMEM;
401 }
402 if (s->buf_change) {
d546b896 403 ret = s->buf_change(dev, s);
40f58a65
HS
404 if (ret < 0)
405 return ret;
406 }
407
f65cc544 408 comedi_alloc_subdevice_minor(s);
40f58a65
HS
409
410 return 0;
411}
412
413static int __comedi_device_postconfig(struct comedi_device *dev)
ed9eccbe 414{
34c43922 415 struct comedi_subdevice *s;
ed9eccbe 416 int ret;
40f58a65 417 int i;
ed9eccbe
DS
418
419 for (i = 0; i < dev->n_subdevices; i++) {
5e4c58ce 420 s = &dev->subdevices[i];
ed9eccbe
DS
421
422 if (s->type == COMEDI_SUBD_UNUSED)
423 continue;
424
09567cb4
HS
425 if (s->type == COMEDI_SUBD_DO) {
426 if (s->n_chan < 32)
427 s->io_bits = (1 << s->n_chan) - 1;
428 else
429 s->io_bits = 0xffffffff;
430 }
431
ed9eccbe
DS
432 if (s->len_chanlist == 0)
433 s->len_chanlist = 1;
434
435 if (s->do_cmd) {
40f58a65
HS
436 ret = __comedi_device_postconfig_async(dev, s);
437 if (ret)
438 return ret;
ed9eccbe
DS
439 }
440
441 if (!s->range_table && !s->range_table_list)
442 s->range_table = &range_unknown;
443
444 if (!s->insn_read && s->insn_bits)
445 s->insn_read = insn_rw_emulate_bits;
446 if (!s->insn_write && s->insn_bits)
447 s->insn_write = insn_rw_emulate_bits;
448
449 if (!s->insn_read)
450 s->insn_read = insn_inval;
451 if (!s->insn_write)
452 s->insn_write = insn_inval;
453 if (!s->insn_bits)
454 s->insn_bits = insn_inval;
455 if (!s->insn_config)
456 s->insn_config = insn_inval;
457
458 if (!s->poll)
459 s->poll = poll_invalid;
460 }
461
462 return 0;
463}
464
01fca378 465/* do a little post-config cleanup */
01fca378
HS
466static int comedi_device_postconfig(struct comedi_device *dev)
467{
b2a644b4
IA
468 int ret;
469
470 ret = __comedi_device_postconfig(dev);
ae5dd5fc 471 if (ret < 0)
01fca378 472 return ret;
bf11c134 473 down_write(&dev->attach_lock);
a7401cdd 474 dev->attached = true;
bf11c134 475 up_write(&dev->attach_lock);
01fca378
HS
476 return 0;
477}
478
4e2f002f
IA
479/*
480 * Generic recognize function for drivers that register their supported
481 * board names.
482 *
483 * 'driv->board_name' points to a 'const char *' member within the
484 * zeroth element of an array of some private board information
485 * structure, say 'struct foo_board' containing a member 'const char
486 * *board_name' that is initialized to point to a board name string that
487 * is one of the candidates matched against this function's 'name'
488 * parameter.
489 *
490 * 'driv->offset' is the size of the private board information
491 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
492 * the length of the array of private board information structures.
493 *
494 * If one of the board names in the array of private board information
495 * structures matches the name supplied to this function, the function
496 * returns a pointer to the pointer to the board name, otherwise it
497 * returns NULL. The return value ends up in the 'board_ptr' member of
498 * a 'struct comedi_device' that the low-level comedi driver's
499 * 'attach()' hook can convert to a point to a particular element of its
500 * array of private board information structures by subtracting the
501 * offset of the member that points to the board name. (No subtraction
502 * is required if the board name pointer is the first member of the
503 * private board information structure, which is generally the case.)
504 */
7029a874 505static void *comedi_recognize(struct comedi_driver *driv, const char *name)
ed9eccbe 506{
1c9de58a
DC
507 char **name_ptr = (char **)driv->board_name;
508 int i;
509
ed9eccbe
DS
510 for (i = 0; i < driv->num_names; i++) {
511 if (strcmp(*name_ptr, name) == 0)
1c9de58a
DC
512 return name_ptr;
513 name_ptr = (void *)name_ptr + driv->offset;
ed9eccbe
DS
514 }
515
516 return NULL;
517}
518
7029a874 519static void comedi_report_boards(struct comedi_driver *driv)
ed9eccbe
DS
520{
521 unsigned int i;
522 const char *const *name_ptr;
523
4f870fe6
IA
524 pr_info("comedi: valid board names for %s driver are:\n",
525 driv->driver_name);
ed9eccbe
DS
526
527 name_ptr = driv->board_name;
528 for (i = 0; i < driv->num_names; i++) {
4f870fe6 529 pr_info(" %s\n", *name_ptr);
ed9eccbe
DS
530 name_ptr = (const char **)((char *)name_ptr + driv->offset);
531 }
532
533 if (driv->num_names == 0)
4f870fe6 534 pr_info(" %s\n", driv->driver_name);
ed9eccbe
DS
535}
536
9ff8b151
HS
537/**
538 * comedi_load_firmware() - Request and load firmware for a device.
539 * @dev: comedi_device struct
540 * @hw_device: device struct for the comedi_device
541 * @name: the name of the firmware image
542 * @cb: callback to the upload the firmware image
d569541e 543 * @context: private context from the driver
9ff8b151
HS
544 */
545int comedi_load_firmware(struct comedi_device *dev,
546 struct device *device,
547 const char *name,
548 int (*cb)(struct comedi_device *dev,
d569541e
HS
549 const u8 *data, size_t size,
550 unsigned long context),
551 unsigned long context)
9ff8b151
HS
552{
553 const struct firmware *fw;
554 int ret;
555
556 if (!cb)
557 return -EINVAL;
558
559 ret = request_firmware(&fw, name, device);
560 if (ret == 0) {
d569541e 561 ret = cb(dev, fw->data, fw->size, context);
9ff8b151
HS
562 release_firmware(fw);
563 }
564
c6236c0c 565 return ret < 0 ? ret : 0;
9ff8b151
HS
566}
567EXPORT_SYMBOL_GPL(comedi_load_firmware);
568
f375ac5f 569/**
ca8b2964 570 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
f375ac5f
HS
571 * @dev: comedi_device struct
572 * @start: base address of the I/O reqion
573 * @len: length of the I/O region
574 */
ca8b2964
HS
575int __comedi_request_region(struct comedi_device *dev,
576 unsigned long start, unsigned long len)
f375ac5f
HS
577{
578 if (!start) {
579 dev_warn(dev->class_dev,
580 "%s: a I/O base address must be specified\n",
581 dev->board_name);
582 return -EINVAL;
583 }
584
585 if (!request_region(start, len, dev->board_name)) {
586 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
587 dev->board_name, start, len);
588 return -EIO;
589 }
f375ac5f
HS
590
591 return 0;
592}
ca8b2964
HS
593EXPORT_SYMBOL_GPL(__comedi_request_region);
594
595/**
596 * comedi_request_region() - Request an I/O reqion for a legacy driver.
597 * @dev: comedi_device struct
598 * @start: base address of the I/O reqion
599 * @len: length of the I/O region
600 */
601int comedi_request_region(struct comedi_device *dev,
602 unsigned long start, unsigned long len)
603{
604 int ret;
605
606 ret = __comedi_request_region(dev, start, len);
316f97f1 607 if (ret == 0) {
ca8b2964 608 dev->iobase = start;
316f97f1
HS
609 dev->iolen = len;
610 }
ca8b2964
HS
611
612 return ret;
613}
f375ac5f
HS
614EXPORT_SYMBOL_GPL(comedi_request_region);
615
316f97f1
HS
616/**
617 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
618 * @dev: comedi_device struct
619 */
620void comedi_legacy_detach(struct comedi_device *dev)
621{
3d1fe3f7
HS
622 if (dev->irq) {
623 free_irq(dev->irq, dev);
624 dev->irq = 0;
625 }
316f97f1
HS
626 if (dev->iobase && dev->iolen) {
627 release_region(dev->iobase, dev->iolen);
628 dev->iobase = 0;
629 dev->iolen = 0;
630 }
631}
632EXPORT_SYMBOL_GPL(comedi_legacy_detach);
633
01fca378 634int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
ed9eccbe 635{
01fca378
HS
636 struct comedi_driver *driv;
637 int ret;
638
639 if (dev->attached)
640 return -EBUSY;
641
c383e2d6 642 mutex_lock(&comedi_drivers_list_lock);
01fca378
HS
643 for (driv = comedi_drivers; driv; driv = driv->next) {
644 if (!try_module_get(driv->module))
645 continue;
646 if (driv->num_names) {
647 dev->board_ptr = comedi_recognize(driv, it->board_name);
648 if (dev->board_ptr)
649 break;
3df9f21a 650 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
01fca378 651 break;
3df9f21a 652 }
01fca378
HS
653 module_put(driv->module);
654 }
655 if (driv == NULL) {
656 /* recognize has failed if we get here */
657 /* report valid board names before returning error */
658 for (driv = comedi_drivers; driv; driv = driv->next) {
659 if (!try_module_get(driv->module))
660 continue;
661 comedi_report_boards(driv);
662 module_put(driv->module);
663 }
c383e2d6
IA
664 ret = -EIO;
665 goto out;
01fca378
HS
666 }
667 if (driv->attach == NULL) {
668 /* driver does not support manual configuration */
669 dev_warn(dev->class_dev,
670 "driver '%s' does not support attach using comedi_config\n",
671 driv->driver_name);
672 module_put(driv->module);
c383e2d6
IA
673 ret = -ENOSYS;
674 goto out;
01fca378 675 }
01fca378 676 dev->driver = driv;
34b68400
HS
677 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
678 : dev->driver->driver_name;
01fca378 679 ret = driv->attach(dev, it);
74ece108
IA
680 if (ret >= 0)
681 ret = comedi_device_postconfig(dev);
01fca378 682 if (ret < 0) {
016599f5 683 comedi_device_detach(dev);
3955dfa8 684 module_put(driv->module);
01fca378 685 }
b2a644b4 686 /* On success, the driver module count has been incremented. */
c383e2d6
IA
687out:
688 mutex_unlock(&comedi_drivers_list_lock);
b2a644b4 689 return ret;
ed9eccbe
DS
690}
691
a588da1d
IA
692int comedi_auto_config(struct device *hardware_device,
693 struct comedi_driver *driver, unsigned long context)
f4011670 694{
6013a9a5 695 struct comedi_device *dev;
f4011670
IA
696 int ret;
697
f08e0ac5
IA
698 if (!hardware_device) {
699 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
700 return -EINVAL;
701 }
702 if (!driver) {
703 dev_warn(hardware_device,
704 "BUG! comedi_auto_config called with NULL comedi driver\n");
705 return -EINVAL;
706 }
707
a588da1d
IA
708 if (!driver->auto_attach) {
709 dev_warn(hardware_device,
710 "BUG! comedi driver '%s' has no auto_attach handler\n",
711 driver->driver_name);
712 return -EINVAL;
713 }
714
6013a9a5 715 dev = comedi_alloc_board_minor(hardware_device);
bcb6232d
BP
716 if (IS_ERR(dev)) {
717 dev_warn(hardware_device,
718 "driver '%s' could not create device.\n",
719 driver->driver_name);
6013a9a5 720 return PTR_ERR(dev);
bcb6232d 721 }
6013a9a5 722 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
f4011670 723
6013a9a5 724 dev->driver = driver;
34b68400 725 dev->board_name = dev->driver->driver_name;
6013a9a5 726 ret = driver->auto_attach(dev, context);
74ece108 727 if (ret >= 0)
6013a9a5 728 ret = comedi_device_postconfig(dev);
6013a9a5 729 mutex_unlock(&dev->mutex);
f4011670 730
bcb6232d
BP
731 if (ret < 0) {
732 dev_warn(hardware_device,
733 "driver '%s' failed to auto-configure device.\n",
734 driver->driver_name);
f5b31e15 735 comedi_release_hardware_device(hardware_device);
bcb6232d
BP
736 } else {
737 /*
738 * class_dev should be set properly here
739 * after a successful auto config
740 */
741 dev_info(dev->class_dev,
742 "driver '%s' has successfully auto-configured '%s'.\n",
743 driver->driver_name, dev->board_name);
744 }
f4011670
IA
745 return ret;
746}
8ed705af
IA
747EXPORT_SYMBOL_GPL(comedi_auto_config);
748
749void comedi_auto_unconfig(struct device *hardware_device)
ed9eccbe 750{
c43435d7
IA
751 if (hardware_device == NULL)
752 return;
3346b798 753 comedi_release_hardware_device(hardware_device);
ed9eccbe 754}
8ed705af 755EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
1ae6b20b
HS
756
757int comedi_driver_register(struct comedi_driver *driver)
758{
c383e2d6 759 mutex_lock(&comedi_drivers_list_lock);
1ae6b20b
HS
760 driver->next = comedi_drivers;
761 comedi_drivers = driver;
c383e2d6 762 mutex_unlock(&comedi_drivers_list_lock);
1ae6b20b
HS
763
764 return 0;
765}
5660e742 766EXPORT_SYMBOL_GPL(comedi_driver_register);
1ae6b20b 767
99c0e269 768void comedi_driver_unregister(struct comedi_driver *driver)
1ae6b20b
HS
769{
770 struct comedi_driver *prev;
771 int i;
772
c383e2d6
IA
773 /* unlink the driver */
774 mutex_lock(&comedi_drivers_list_lock);
775 if (comedi_drivers == driver) {
776 comedi_drivers = driver->next;
777 } else {
778 for (prev = comedi_drivers; prev->next; prev = prev->next) {
779 if (prev->next == driver) {
780 prev->next = driver->next;
781 break;
782 }
783 }
784 }
785 mutex_unlock(&comedi_drivers_list_lock);
786
1ae6b20b
HS
787 /* check for devices using this driver */
788 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
a200fadc 789 struct comedi_device *dev = comedi_dev_get_from_minor(i);
1ae6b20b
HS
790
791 if (!dev)
792 continue;
793
794 mutex_lock(&dev->mutex);
795 if (dev->attached && dev->driver == driver) {
796 if (dev->use_count)
797 dev_warn(dev->class_dev,
798 "BUG! detaching device with use_count=%d\n",
799 dev->use_count);
800 comedi_device_detach(dev);
801 }
802 mutex_unlock(&dev->mutex);
a200fadc 803 comedi_dev_put(dev);
1ae6b20b 804 }
1ae6b20b 805}
5660e742 806EXPORT_SYMBOL_GPL(comedi_driver_unregister);