Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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.
17*/
18
19#include <linux/device.h>
20#include <linux/module.h>
21#include <linux/errno.h>
22#include <linux/kconfig.h>
23#include <linux/kernel.h>
24#include <linux/sched.h>
25#include <linux/fcntl.h>
26#include <linux/ioport.h>
27#include <linux/mm.h>
28#include <linux/slab.h>
29#include <linux/highmem.h> /* for SuSE brokenness */
30#include <linux/vmalloc.h>
31#include <linux/cdev.h>
32#include <linux/dma-mapping.h>
33#include <linux/io.h>
34#include <linux/interrupt.h>
35#include <linux/firmware.h>
36
37#include "comedidev.h"
38#include "comedi_internal.h"
39
40struct comedi_driver *comedi_drivers;
41/* protects access to comedi_drivers */
42DEFINE_MUTEX(comedi_drivers_list_lock);
43
44int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
45{
46 if (hw_dev == dev->hw_dev)
47 return 0;
48 if (dev->hw_dev != NULL)
49 return -EEXIST;
50 dev->hw_dev = get_device(hw_dev);
51 return 0;
52}
53EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
54
55static void comedi_clear_hw_dev(struct comedi_device *dev)
56{
57 put_device(dev->hw_dev);
58 dev->hw_dev = NULL;
59}
60
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
73int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
74{
75 struct comedi_subdevice *s;
76 int i;
77
78 if (num_subdevices < 1)
79 return -EINVAL;
80
81 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
82 if (!s)
83 return -ENOMEM;
84 dev->subdevices = s;
85 dev->n_subdevices = num_subdevices;
86
87 for (i = 0; i < num_subdevices; ++i) {
88 s = &dev->subdevices[i];
89 s->device = dev;
90 s->index = i;
91 s->async_dma_dir = DMA_NONE;
92 spin_lock_init(&s->spin_lock);
93 s->minor = -1;
94 }
95 return 0;
96}
97EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
98
99static void comedi_device_detach_cleanup(struct comedi_device *dev)
100{
101 int i;
102 struct comedi_subdevice *s;
103
104 if (dev->subdevices) {
105 for (i = 0; i < dev->n_subdevices; i++) {
106 s = &dev->subdevices[i];
107 if (s->runflags & SRF_FREE_SPRIV)
108 kfree(s->private);
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 }
119 kfree(dev->private);
120 dev->private = NULL;
121 dev->driver = NULL;
122 dev->board_name = NULL;
123 dev->board_ptr = NULL;
124 dev->mmio = NULL;
125 dev->iobase = 0;
126 dev->iolen = 0;
127 dev->ioenabled = false;
128 dev->irq = 0;
129 dev->read_subdev = NULL;
130 dev->write_subdev = NULL;
131 dev->open = NULL;
132 dev->close = NULL;
133 comedi_clear_hw_dev(dev);
134}
135
136void comedi_device_detach(struct comedi_device *dev)
137{
138 comedi_device_cancel_all(dev);
139 down_write(&dev->attach_lock);
140 dev->attached = false;
141 dev->detach_count++;
142 if (dev->driver)
143 dev->driver->detach(dev);
144 comedi_device_detach_cleanup(dev);
145 up_write(&dev->attach_lock);
146}
147
148static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
149{
150 return -EINVAL;
151}
152
153int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
154 struct comedi_insn *insn, unsigned int *data)
155{
156 return -EINVAL;
157}
158
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
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
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
251static int insn_rw_emulate_bits(struct comedi_device *dev,
252 struct comedi_subdevice *s,
253 struct comedi_insn *insn, unsigned int *data)
254{
255 struct comedi_insn new_insn;
256 int ret;
257 static const unsigned channels_per_bitfield = 32;
258
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];
263
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;
270
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 */
277 }
278
279 ret = s->insn_bits(dev, s, &new_insn, new_data);
280 if (ret < 0)
281 return ret;
282
283 if (insn->insn == INSN_READ)
284 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
285
286 return 1;
287}
288
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
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 }
306
307 async = kzalloc(sizeof(*async), GFP_KERNEL);
308 if (!async)
309 return -ENOMEM;
310
311 init_waitqueue_head(&async->wait_head);
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) {
324 ret = s->buf_change(dev, s);
325 if (ret < 0)
326 return ret;
327 }
328
329 comedi_alloc_subdevice_minor(s);
330
331 return 0;
332}
333
334static int __comedi_device_postconfig(struct comedi_device *dev)
335{
336 struct comedi_subdevice *s;
337 int ret;
338 int i;
339
340 for (i = 0; i < dev->n_subdevices; i++) {
341 s = &dev->subdevices[i];
342
343 if (s->type == COMEDI_SUBD_UNUSED)
344 continue;
345
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
353 if (s->len_chanlist == 0)
354 s->len_chanlist = 1;
355
356 if (s->do_cmd) {
357 ret = __comedi_device_postconfig_async(dev, s);
358 if (ret)
359 return ret;
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
386/* do a little post-config cleanup */
387static int comedi_device_postconfig(struct comedi_device *dev)
388{
389 int ret;
390
391 ret = __comedi_device_postconfig(dev);
392 if (ret < 0)
393 return ret;
394 down_write(&dev->attach_lock);
395 dev->attached = true;
396 up_write(&dev->attach_lock);
397 return 0;
398}
399
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 */
426static void *comedi_recognize(struct comedi_driver *driv, const char *name)
427{
428 char **name_ptr = (char **)driv->board_name;
429 int i;
430
431 for (i = 0; i < driv->num_names; i++) {
432 if (strcmp(*name_ptr, name) == 0)
433 return name_ptr;
434 name_ptr = (void *)name_ptr + driv->offset;
435 }
436
437 return NULL;
438}
439
440static void comedi_report_boards(struct comedi_driver *driv)
441{
442 unsigned int i;
443 const char *const *name_ptr;
444
445 pr_info("comedi: valid board names for %s driver are:\n",
446 driv->driver_name);
447
448 name_ptr = driv->board_name;
449 for (i = 0; i < driv->num_names; i++) {
450 pr_info(" %s\n", *name_ptr);
451 name_ptr = (const char **)((char *)name_ptr + driv->offset);
452 }
453
454 if (driv->num_names == 0)
455 pr_info(" %s\n", driv->driver_name);
456}
457
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
464 * @context: private context from the driver
465 */
466int comedi_load_firmware(struct comedi_device *dev,
467 struct device *device,
468 const char *name,
469 int (*cb)(struct comedi_device *dev,
470 const u8 *data, size_t size,
471 unsigned long context),
472 unsigned long context)
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) {
482 ret = cb(dev, fw->data, fw->size, context);
483 release_firmware(fw);
484 }
485
486 return ret < 0 ? ret : 0;
487}
488EXPORT_SYMBOL_GPL(comedi_load_firmware);
489
490/**
491 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
492 * @dev: comedi_device struct
493 * @start: base address of the I/O reqion
494 * @len: length of the I/O region
495 */
496int __comedi_request_region(struct comedi_device *dev,
497 unsigned long start, unsigned long len)
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 }
511
512 return 0;
513}
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);
528 if (ret == 0) {
529 dev->iobase = start;
530 dev->iolen = len;
531 }
532
533 return ret;
534}
535EXPORT_SYMBOL_GPL(comedi_request_region);
536
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{
543 if (dev->irq) {
544 free_irq(dev->irq, dev);
545 dev->irq = 0;
546 }
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
555int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
556{
557 struct comedi_driver *driv;
558 int ret;
559
560 if (dev->attached)
561 return -EBUSY;
562
563 mutex_lock(&comedi_drivers_list_lock);
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;
571 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
572 break;
573 }
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 }
585 ret = -EIO;
586 goto out;
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);
594 ret = -ENOSYS;
595 goto out;
596 }
597 dev->driver = driv;
598 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
599 : dev->driver->driver_name;
600 ret = driv->attach(dev, it);
601 if (ret >= 0)
602 ret = comedi_device_postconfig(dev);
603 if (ret < 0) {
604 comedi_device_detach(dev);
605 module_put(driv->module);
606 }
607 /* On success, the driver module count has been incremented. */
608out:
609 mutex_unlock(&comedi_drivers_list_lock);
610 return ret;
611}
612
613int comedi_auto_config(struct device *hardware_device,
614 struct comedi_driver *driver, unsigned long context)
615{
616 struct comedi_device *dev;
617 int ret;
618
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
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
636 dev = comedi_alloc_board_minor(hardware_device);
637 if (IS_ERR(dev)) {
638 dev_warn(hardware_device,
639 "driver '%s' could not create device.\n",
640 driver->driver_name);
641 return PTR_ERR(dev);
642 }
643 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
644
645 dev->driver = driver;
646 dev->board_name = dev->driver->driver_name;
647 ret = driver->auto_attach(dev, context);
648 if (ret >= 0)
649 ret = comedi_device_postconfig(dev);
650 mutex_unlock(&dev->mutex);
651
652 if (ret < 0) {
653 dev_warn(hardware_device,
654 "driver '%s' failed to auto-configure device.\n",
655 driver->driver_name);
656 comedi_release_hardware_device(hardware_device);
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 }
666 return ret;
667}
668EXPORT_SYMBOL_GPL(comedi_auto_config);
669
670void comedi_auto_unconfig(struct device *hardware_device)
671{
672 if (hardware_device == NULL)
673 return;
674 comedi_release_hardware_device(hardware_device);
675}
676EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
677
678int comedi_driver_register(struct comedi_driver *driver)
679{
680 mutex_lock(&comedi_drivers_list_lock);
681 driver->next = comedi_drivers;
682 comedi_drivers = driver;
683 mutex_unlock(&comedi_drivers_list_lock);
684
685 return 0;
686}
687EXPORT_SYMBOL_GPL(comedi_driver_register);
688
689void comedi_driver_unregister(struct comedi_driver *driver)
690{
691 struct comedi_driver *prev;
692 int i;
693
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
708 /* check for devices using this driver */
709 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
710 struct comedi_device *dev = comedi_dev_get_from_minor(i);
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);
724 comedi_dev_put(dev);
725 }
726}
727EXPORT_SYMBOL_GPL(comedi_driver_unregister);