3 functions for manipulating drivers
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
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.
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.
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>
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>
34 #include <linux/interrupt.h>
35 #include <linux/firmware.h>
37 #include "comedidev.h"
38 #include "comedi_internal.h"
40 struct comedi_driver
*comedi_drivers
;
41 DEFINE_MUTEX(comedi_drivers_list_lock
);
43 int comedi_set_hw_dev(struct comedi_device
*dev
, struct device
*hw_dev
)
45 if (hw_dev
== dev
->hw_dev
)
47 if (dev
->hw_dev
!= NULL
)
49 dev
->hw_dev
= get_device(hw_dev
);
52 EXPORT_SYMBOL_GPL(comedi_set_hw_dev
);
54 static void comedi_clear_hw_dev(struct comedi_device
*dev
)
56 put_device(dev
->hw_dev
);
61 * comedi_alloc_devpriv() - Allocate memory for the device private data.
62 * @dev: comedi_device struct
63 * @size: size of the memory to allocate
65 void *comedi_alloc_devpriv(struct comedi_device
*dev
, size_t size
)
67 dev
->private = kzalloc(size
, GFP_KERNEL
);
70 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv
);
72 int comedi_alloc_subdevices(struct comedi_device
*dev
, int num_subdevices
)
74 struct comedi_subdevice
*s
;
77 if (num_subdevices
< 1)
80 s
= kcalloc(num_subdevices
, sizeof(*s
), GFP_KERNEL
);
84 dev
->n_subdevices
= num_subdevices
;
86 for (i
= 0; i
< num_subdevices
; ++i
) {
87 s
= &dev
->subdevices
[i
];
90 s
->async_dma_dir
= DMA_NONE
;
91 spin_lock_init(&s
->spin_lock
);
96 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices
);
98 static void cleanup_device(struct comedi_device
*dev
)
101 struct comedi_subdevice
*s
;
103 if (dev
->subdevices
) {
104 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
105 s
= &dev
->subdevices
[i
];
106 if (s
->runflags
& SRF_FREE_SPRIV
)
108 comedi_free_subdevice_minor(s
);
110 comedi_buf_alloc(dev
, s
, 0);
114 kfree(dev
->subdevices
);
115 dev
->subdevices
= NULL
;
116 dev
->n_subdevices
= 0;
121 dev
->board_name
= NULL
;
122 dev
->board_ptr
= NULL
;
125 dev
->ioenabled
= false;
127 dev
->read_subdev
= NULL
;
128 dev
->write_subdev
= NULL
;
131 comedi_clear_hw_dev(dev
);
134 void comedi_device_detach(struct comedi_device
*dev
)
136 dev
->attached
= false;
138 dev
->driver
->detach(dev
);
142 static int poll_invalid(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
147 int insn_inval(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
148 struct comedi_insn
*insn
, unsigned int *data
)
154 * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
155 * @dev: comedi_device struct
156 * @s: comedi_subdevice struct
157 * @insn: comedi_insn struct
158 * @data: parameters for the @insn
159 * @mask: io_bits mask for grouped channels
161 int comedi_dio_insn_config(struct comedi_device
*dev
,
162 struct comedi_subdevice
*s
,
163 struct comedi_insn
*insn
,
167 unsigned int chan_mask
= 1 << CR_CHAN(insn
->chanspec
);
173 case INSN_CONFIG_DIO_INPUT
:
177 case INSN_CONFIG_DIO_OUTPUT
:
181 case INSN_CONFIG_DIO_QUERY
:
182 data
[1] = (s
->io_bits
& mask
) ? COMEDI_OUTPUT
: COMEDI_INPUT
;
191 EXPORT_SYMBOL_GPL(comedi_dio_insn_config
);
194 * comedi_dio_update_state() - update the internal state of DIO subdevices.
195 * @s: comedi_subdevice struct
196 * @data: the channel mask and bits to update
198 unsigned int comedi_dio_update_state(struct comedi_subdevice
*s
,
201 unsigned int chanmask
= (s
->n_chan
< 32) ? ((1 << s
->n_chan
) - 1)
203 unsigned int mask
= data
[0] & chanmask
;
204 unsigned int bits
= data
[1];
208 s
->state
|= (bits
& mask
);
213 EXPORT_SYMBOL_GPL(comedi_dio_update_state
);
215 static int insn_rw_emulate_bits(struct comedi_device
*dev
,
216 struct comedi_subdevice
*s
,
217 struct comedi_insn
*insn
, unsigned int *data
)
219 struct comedi_insn new_insn
;
221 static const unsigned channels_per_bitfield
= 32;
223 unsigned chan
= CR_CHAN(insn
->chanspec
);
224 const unsigned base_bitfield_channel
=
225 (chan
< channels_per_bitfield
) ? 0 : chan
;
226 unsigned int new_data
[2];
227 memset(new_data
, 0, sizeof(new_data
));
228 memset(&new_insn
, 0, sizeof(new_insn
));
229 new_insn
.insn
= INSN_BITS
;
230 new_insn
.chanspec
= base_bitfield_channel
;
232 new_insn
.subdev
= insn
->subdev
;
234 if (insn
->insn
== INSN_WRITE
) {
235 if (!(s
->subdev_flags
& SDF_WRITABLE
))
237 new_data
[0] = 1 << (chan
- base_bitfield_channel
); /* mask */
238 new_data
[1] = data
[0] ? (1 << (chan
- base_bitfield_channel
))
242 ret
= s
->insn_bits(dev
, s
, &new_insn
, new_data
);
246 if (insn
->insn
== INSN_READ
)
247 data
[0] = (new_data
[1] >> (chan
- base_bitfield_channel
)) & 1;
252 static int __comedi_device_postconfig_async(struct comedi_device
*dev
,
253 struct comedi_subdevice
*s
)
255 struct comedi_async
*async
;
256 unsigned int buf_size
;
259 if ((s
->subdev_flags
& (SDF_CMD_READ
| SDF_CMD_WRITE
)) == 0) {
260 dev_warn(dev
->class_dev
,
261 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
264 if (!s
->do_cmdtest
) {
265 dev_warn(dev
->class_dev
,
266 "async subdevices must have a do_cmdtest() function\n");
270 async
= kzalloc(sizeof(*async
), GFP_KERNEL
);
274 init_waitqueue_head(&async
->wait_head
);
275 async
->subdevice
= s
;
278 async
->max_bufsize
= comedi_default_buf_maxsize_kb
* 1024;
279 buf_size
= comedi_default_buf_size_kb
* 1024;
280 if (buf_size
> async
->max_bufsize
)
281 buf_size
= async
->max_bufsize
;
283 if (comedi_buf_alloc(dev
, s
, buf_size
) < 0) {
284 dev_warn(dev
->class_dev
, "Buffer allocation failed\n");
288 ret
= s
->buf_change(dev
, s
, buf_size
);
293 comedi_alloc_subdevice_minor(s
);
298 static int __comedi_device_postconfig(struct comedi_device
*dev
)
300 struct comedi_subdevice
*s
;
304 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
305 s
= &dev
->subdevices
[i
];
307 if (s
->type
== COMEDI_SUBD_UNUSED
)
310 if (s
->type
== COMEDI_SUBD_DO
) {
312 s
->io_bits
= (1 << s
->n_chan
) - 1;
314 s
->io_bits
= 0xffffffff;
317 if (s
->len_chanlist
== 0)
321 ret
= __comedi_device_postconfig_async(dev
, s
);
326 if (!s
->range_table
&& !s
->range_table_list
)
327 s
->range_table
= &range_unknown
;
329 if (!s
->insn_read
&& s
->insn_bits
)
330 s
->insn_read
= insn_rw_emulate_bits
;
331 if (!s
->insn_write
&& s
->insn_bits
)
332 s
->insn_write
= insn_rw_emulate_bits
;
335 s
->insn_read
= insn_inval
;
337 s
->insn_write
= insn_inval
;
339 s
->insn_bits
= insn_inval
;
341 s
->insn_config
= insn_inval
;
344 s
->poll
= poll_invalid
;
350 /* do a little post-config cleanup */
351 static int comedi_device_postconfig(struct comedi_device
*dev
)
355 ret
= __comedi_device_postconfig(dev
);
359 dev
->attached
= true;
364 * Generic recognize function for drivers that register their supported
367 * 'driv->board_name' points to a 'const char *' member within the
368 * zeroth element of an array of some private board information
369 * structure, say 'struct foo_board' containing a member 'const char
370 * *board_name' that is initialized to point to a board name string that
371 * is one of the candidates matched against this function's 'name'
374 * 'driv->offset' is the size of the private board information
375 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
376 * the length of the array of private board information structures.
378 * If one of the board names in the array of private board information
379 * structures matches the name supplied to this function, the function
380 * returns a pointer to the pointer to the board name, otherwise it
381 * returns NULL. The return value ends up in the 'board_ptr' member of
382 * a 'struct comedi_device' that the low-level comedi driver's
383 * 'attach()' hook can convert to a point to a particular element of its
384 * array of private board information structures by subtracting the
385 * offset of the member that points to the board name. (No subtraction
386 * is required if the board name pointer is the first member of the
387 * private board information structure, which is generally the case.)
389 static void *comedi_recognize(struct comedi_driver
*driv
, const char *name
)
391 char **name_ptr
= (char **)driv
->board_name
;
394 for (i
= 0; i
< driv
->num_names
; i
++) {
395 if (strcmp(*name_ptr
, name
) == 0)
397 name_ptr
= (void *)name_ptr
+ driv
->offset
;
403 static void comedi_report_boards(struct comedi_driver
*driv
)
406 const char *const *name_ptr
;
408 pr_info("comedi: valid board names for %s driver are:\n",
411 name_ptr
= driv
->board_name
;
412 for (i
= 0; i
< driv
->num_names
; i
++) {
413 pr_info(" %s\n", *name_ptr
);
414 name_ptr
= (const char **)((char *)name_ptr
+ driv
->offset
);
417 if (driv
->num_names
== 0)
418 pr_info(" %s\n", driv
->driver_name
);
422 * comedi_load_firmware() - Request and load firmware for a device.
423 * @dev: comedi_device struct
424 * @hw_device: device struct for the comedi_device
425 * @name: the name of the firmware image
426 * @cb: callback to the upload the firmware image
427 * @context: private context from the driver
429 int comedi_load_firmware(struct comedi_device
*dev
,
430 struct device
*device
,
432 int (*cb
)(struct comedi_device
*dev
,
433 const u8
*data
, size_t size
,
434 unsigned long context
),
435 unsigned long context
)
437 const struct firmware
*fw
;
443 ret
= request_firmware(&fw
, name
, device
);
445 ret
= cb(dev
, fw
->data
, fw
->size
, context
);
446 release_firmware(fw
);
451 EXPORT_SYMBOL_GPL(comedi_load_firmware
);
454 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
455 * @dev: comedi_device struct
456 * @start: base address of the I/O reqion
457 * @len: length of the I/O region
459 int __comedi_request_region(struct comedi_device
*dev
,
460 unsigned long start
, unsigned long len
)
463 dev_warn(dev
->class_dev
,
464 "%s: a I/O base address must be specified\n",
469 if (!request_region(start
, len
, dev
->board_name
)) {
470 dev_warn(dev
->class_dev
, "%s: I/O port conflict (%#lx,%lu)\n",
471 dev
->board_name
, start
, len
);
477 EXPORT_SYMBOL_GPL(__comedi_request_region
);
480 * comedi_request_region() - Request an I/O reqion for a legacy driver.
481 * @dev: comedi_device struct
482 * @start: base address of the I/O reqion
483 * @len: length of the I/O region
485 int comedi_request_region(struct comedi_device
*dev
,
486 unsigned long start
, unsigned long len
)
490 ret
= __comedi_request_region(dev
, start
, len
);
498 EXPORT_SYMBOL_GPL(comedi_request_region
);
501 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
502 * @dev: comedi_device struct
504 void comedi_legacy_detach(struct comedi_device
*dev
)
507 free_irq(dev
->irq
, dev
);
510 if (dev
->iobase
&& dev
->iolen
) {
511 release_region(dev
->iobase
, dev
->iolen
);
516 EXPORT_SYMBOL_GPL(comedi_legacy_detach
);
518 int comedi_device_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
520 struct comedi_driver
*driv
;
526 mutex_lock(&comedi_drivers_list_lock
);
527 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
528 if (!try_module_get(driv
->module
))
530 if (driv
->num_names
) {
531 dev
->board_ptr
= comedi_recognize(driv
, it
->board_name
);
534 } else if (strcmp(driv
->driver_name
, it
->board_name
) == 0)
536 module_put(driv
->module
);
539 /* recognize has failed if we get here */
540 /* report valid board names before returning error */
541 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
542 if (!try_module_get(driv
->module
))
544 comedi_report_boards(driv
);
545 module_put(driv
->module
);
550 if (driv
->attach
== NULL
) {
551 /* driver does not support manual configuration */
552 dev_warn(dev
->class_dev
,
553 "driver '%s' does not support attach using comedi_config\n",
555 module_put(driv
->module
);
559 /* initialize dev->driver here so
560 * comedi_error() can be called from attach */
562 dev
->board_name
= dev
->board_ptr
? *(const char **)dev
->board_ptr
563 : dev
->driver
->driver_name
;
564 ret
= driv
->attach(dev
, it
);
566 ret
= comedi_device_postconfig(dev
);
568 comedi_device_detach(dev
);
569 module_put(driv
->module
);
571 /* On success, the driver module count has been incremented. */
573 mutex_unlock(&comedi_drivers_list_lock
);
577 int comedi_auto_config(struct device
*hardware_device
,
578 struct comedi_driver
*driver
, unsigned long context
)
580 struct comedi_device
*dev
;
583 if (!hardware_device
) {
584 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
588 dev_warn(hardware_device
,
589 "BUG! comedi_auto_config called with NULL comedi driver\n");
593 if (!driver
->auto_attach
) {
594 dev_warn(hardware_device
,
595 "BUG! comedi driver '%s' has no auto_attach handler\n",
596 driver
->driver_name
);
600 dev
= comedi_alloc_board_minor(hardware_device
);
603 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
605 dev
->driver
= driver
;
606 dev
->board_name
= dev
->driver
->driver_name
;
607 ret
= driver
->auto_attach(dev
, context
);
609 ret
= comedi_device_postconfig(dev
);
611 comedi_device_detach(dev
);
612 mutex_unlock(&dev
->mutex
);
615 comedi_release_hardware_device(hardware_device
);
618 EXPORT_SYMBOL_GPL(comedi_auto_config
);
620 void comedi_auto_unconfig(struct device
*hardware_device
)
622 if (hardware_device
== NULL
)
624 comedi_release_hardware_device(hardware_device
);
626 EXPORT_SYMBOL_GPL(comedi_auto_unconfig
);
628 int comedi_driver_register(struct comedi_driver
*driver
)
630 mutex_lock(&comedi_drivers_list_lock
);
631 driver
->next
= comedi_drivers
;
632 comedi_drivers
= driver
;
633 mutex_unlock(&comedi_drivers_list_lock
);
637 EXPORT_SYMBOL_GPL(comedi_driver_register
);
639 void comedi_driver_unregister(struct comedi_driver
*driver
)
641 struct comedi_driver
*prev
;
644 /* unlink the driver */
645 mutex_lock(&comedi_drivers_list_lock
);
646 if (comedi_drivers
== driver
) {
647 comedi_drivers
= driver
->next
;
649 for (prev
= comedi_drivers
; prev
->next
; prev
= prev
->next
) {
650 if (prev
->next
== driver
) {
651 prev
->next
= driver
->next
;
656 mutex_unlock(&comedi_drivers_list_lock
);
658 /* check for devices using this driver */
659 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++) {
660 struct comedi_device
*dev
= comedi_dev_from_minor(i
);
665 mutex_lock(&dev
->mutex
);
666 if (dev
->attached
&& dev
->driver
== driver
) {
668 dev_warn(dev
->class_dev
,
669 "BUG! detaching device with use_count=%d\n",
671 comedi_device_detach(dev
);
673 mutex_unlock(&dev
->mutex
);
676 EXPORT_SYMBOL_GPL(comedi_driver_unregister
);