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