3 functions for manipulating drivers
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7 Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 #include <linux/device.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/kconfig.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/fcntl.h>
27 #include <linux/ioport.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h> /* for SuSE brokenness */
31 #include <linux/vmalloc.h>
32 #include <linux/cdev.h>
33 #include <linux/dma-mapping.h>
35 #include <linux/interrupt.h>
36 #include <linux/firmware.h>
38 #include "comedidev.h"
39 #include "comedi_internal.h"
41 struct comedi_driver *comedi_drivers;
42 /* protects access to comedi_drivers */
43 DEFINE_MUTEX(comedi_drivers_list_lock);
45 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev)
47 if (hw_dev == dev->hw_dev)
49 if (dev->hw_dev != NULL)
51 dev->hw_dev = get_device(hw_dev);
54 EXPORT_SYMBOL_GPL(comedi_set_hw_dev);
56 static void comedi_clear_hw_dev(struct comedi_device *dev)
58 put_device(dev->hw_dev);
63 * comedi_alloc_devpriv() - Allocate memory for the device private data.
64 * @dev: comedi_device struct
65 * @size: size of the memory to allocate
67 void *comedi_alloc_devpriv(struct comedi_device *dev, size_t size)
69 dev->private = kzalloc(size, GFP_KERNEL);
72 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
74 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
76 struct comedi_subdevice *s;
79 if (num_subdevices < 1)
82 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
86 dev->n_subdevices = num_subdevices;
88 for (i = 0; i < num_subdevices; ++i) {
89 s = &dev->subdevices[i];
92 s->async_dma_dir = DMA_NONE;
93 spin_lock_init(&s->spin_lock);
98 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
101 * comedi_alloc_subdev_readback() - Allocate memory for the subdevice readback.
102 * @s: comedi_subdevice struct
104 int comedi_alloc_subdev_readback(struct comedi_subdevice *s)
109 s->readback = kcalloc(s->n_chan, sizeof(*s->readback), GFP_KERNEL);
114 EXPORT_SYMBOL_GPL(comedi_alloc_subdev_readback);
116 static void comedi_device_detach_cleanup(struct comedi_device *dev)
119 struct comedi_subdevice *s;
121 if (dev->subdevices) {
122 for (i = 0; i < dev->n_subdevices; i++) {
123 s = &dev->subdevices[i];
124 if (s->runflags & SRF_FREE_SPRIV)
126 comedi_free_subdevice_minor(s);
128 comedi_buf_alloc(dev, s, 0);
133 kfree(dev->subdevices);
134 dev->subdevices = NULL;
135 dev->n_subdevices = 0;
140 dev->board_name = NULL;
141 dev->board_ptr = NULL;
145 dev->ioenabled = false;
147 dev->read_subdev = NULL;
148 dev->write_subdev = NULL;
151 comedi_clear_hw_dev(dev);
154 void comedi_device_detach(struct comedi_device *dev)
156 comedi_device_cancel_all(dev);
157 down_write(&dev->attach_lock);
158 dev->attached = false;
161 dev->driver->detach(dev);
162 comedi_device_detach_cleanup(dev);
163 up_write(&dev->attach_lock);
166 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
171 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
172 struct comedi_insn *insn, unsigned int *data)
178 * comedi_readback_insn_read() - A generic (*insn_read) for subdevice readback.
179 * @dev: comedi_device struct
180 * @s: comedi_subdevice struct
181 * @insn: comedi_insn struct
182 * @data: pointer to return the readback data
184 int comedi_readback_insn_read(struct comedi_device *dev,
185 struct comedi_subdevice *s,
186 struct comedi_insn *insn,
189 unsigned int chan = CR_CHAN(insn->chanspec);
195 for (i = 0; i < insn->n; i++)
196 data[i] = s->readback[chan];
200 EXPORT_SYMBOL_GPL(comedi_readback_insn_read);
203 * comedi_timeout() - busy-wait for a driver condition to occur.
204 * @dev: comedi_device struct
205 * @s: comedi_subdevice struct
206 * @insn: comedi_insn struct
207 * @cb: callback to check for the condition
208 * @context: private context from the driver
210 int comedi_timeout(struct comedi_device *dev,
211 struct comedi_subdevice *s,
212 struct comedi_insn *insn,
213 int (*cb)(struct comedi_device *dev,
214 struct comedi_subdevice *s,
215 struct comedi_insn *insn,
216 unsigned long context),
217 unsigned long context)
219 unsigned long timeout = jiffies + msecs_to_jiffies(COMEDI_TIMEOUT_MS);
222 while (time_before(jiffies, timeout)) {
223 ret = cb(dev, s, insn, context);
225 return ret; /* success (0) or non EBUSY errno */
230 EXPORT_SYMBOL_GPL(comedi_timeout);
233 * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
234 * @dev: comedi_device struct
235 * @s: comedi_subdevice struct
236 * @insn: comedi_insn struct
237 * @data: parameters for the @insn
238 * @mask: io_bits mask for grouped channels
240 int comedi_dio_insn_config(struct comedi_device *dev,
241 struct comedi_subdevice *s,
242 struct comedi_insn *insn,
246 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
252 case INSN_CONFIG_DIO_INPUT:
256 case INSN_CONFIG_DIO_OUTPUT:
260 case INSN_CONFIG_DIO_QUERY:
261 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
270 EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
273 * comedi_dio_update_state() - update the internal state of DIO subdevices.
274 * @s: comedi_subdevice struct
275 * @data: the channel mask and bits to update
277 unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
280 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
282 unsigned int mask = data[0] & chanmask;
283 unsigned int bits = data[1];
287 s->state |= (bits & mask);
292 EXPORT_SYMBOL_GPL(comedi_dio_update_state);
295 * comedi_bytes_per_scan - get length of asynchronous command "scan" in bytes
296 * @s: comedi_subdevice struct
298 * Determines the overall scan length according to the subdevice type and the
299 * number of channels in the scan.
301 * For digital input, output or input/output subdevices, samples for multiple
302 * channels are assumed to be packed into one or more unsigned short or
303 * unsigned int values according to the subdevice's SDF_LSAMPL flag. For other
304 * types of subdevice, samples are assumed to occupy a whole unsigned short or
305 * unsigned int according to the SDF_LSAMPL flag.
307 * Returns the overall scan length in bytes.
309 unsigned int comedi_bytes_per_scan(struct comedi_subdevice *s)
311 struct comedi_cmd *cmd = &s->async->cmd;
312 unsigned int num_samples;
313 unsigned int bits_per_sample;
318 case COMEDI_SUBD_DIO:
319 bits_per_sample = 8 * bytes_per_sample(s);
320 num_samples = (cmd->chanlist_len + bits_per_sample - 1) /
324 num_samples = cmd->chanlist_len;
327 return num_samples * bytes_per_sample(s);
329 EXPORT_SYMBOL_GPL(comedi_bytes_per_scan);
332 * comedi_inc_scan_progress - update scan progress in asynchronous command
333 * @s: comedi_subdevice struct
334 * @num_bytes: amount of data in bytes to increment scan progress
336 * Increments the scan progress by the number of bytes specified by num_bytes.
337 * If the scan progress reaches or exceeds the scan length in bytes, reduce
338 * it modulo the scan length in bytes and set the "end of scan" asynchronous
339 * event flag to be processed later.
341 void comedi_inc_scan_progress(struct comedi_subdevice *s,
342 unsigned int num_bytes)
344 struct comedi_async *async = s->async;
345 unsigned int scan_length = comedi_bytes_per_scan(s);
347 async->scan_progress += num_bytes;
348 if (async->scan_progress >= scan_length) {
349 async->scan_progress %= scan_length;
350 async->events |= COMEDI_CB_EOS;
353 EXPORT_SYMBOL_GPL(comedi_inc_scan_progress);
356 * comedi_handle_events - handle events and possibly stop acquisition
357 * @dev: comedi_device struct
358 * @s: comedi_subdevice struct
360 * Handles outstanding asynchronous acquisition event flags associated
361 * with the subdevice. Call the subdevice's "->cancel()" handler if the
362 * "end of acquisition", "error" or "overflow" event flags are set in order
363 * to stop the acquisition at the driver level.
365 * Calls comedi_event() to further process the event flags, which may mark
366 * the asynchronous command as no longer running, possibly terminated with
367 * an error, and may wake up tasks.
369 * Return a bit-mask of the handled events.
371 unsigned int comedi_handle_events(struct comedi_device *dev,
372 struct comedi_subdevice *s)
374 unsigned int events = s->async->events;
379 if (events & (COMEDI_CB_EOA | COMEDI_CB_ERROR | COMEDI_CB_OVERFLOW))
382 comedi_event(dev, s);
386 EXPORT_SYMBOL_GPL(comedi_handle_events);
388 static int insn_rw_emulate_bits(struct comedi_device *dev,
389 struct comedi_subdevice *s,
390 struct comedi_insn *insn, unsigned int *data)
392 struct comedi_insn new_insn;
394 static const unsigned channels_per_bitfield = 32;
396 unsigned chan = CR_CHAN(insn->chanspec);
397 const unsigned base_bitfield_channel =
398 (chan < channels_per_bitfield) ? 0 : chan;
399 unsigned int new_data[2];
401 memset(new_data, 0, sizeof(new_data));
402 memset(&new_insn, 0, sizeof(new_insn));
403 new_insn.insn = INSN_BITS;
404 new_insn.chanspec = base_bitfield_channel;
406 new_insn.subdev = insn->subdev;
408 if (insn->insn == INSN_WRITE) {
409 if (!(s->subdev_flags & SDF_WRITABLE))
411 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
412 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
416 ret = s->insn_bits(dev, s, &new_insn, new_data);
420 if (insn->insn == INSN_READ)
421 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
426 static int __comedi_device_postconfig_async(struct comedi_device *dev,
427 struct comedi_subdevice *s)
429 struct comedi_async *async;
430 unsigned int buf_size;
433 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
434 dev_warn(dev->class_dev,
435 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
438 if (!s->do_cmdtest) {
439 dev_warn(dev->class_dev,
440 "async subdevices must have a do_cmdtest() function\n");
444 async = kzalloc(sizeof(*async), GFP_KERNEL);
448 init_waitqueue_head(&async->wait_head);
451 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
452 buf_size = comedi_default_buf_size_kb * 1024;
453 if (buf_size > async->max_bufsize)
454 buf_size = async->max_bufsize;
456 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
457 dev_warn(dev->class_dev, "Buffer allocation failed\n");
461 ret = s->buf_change(dev, s);
466 comedi_alloc_subdevice_minor(s);
471 static int __comedi_device_postconfig(struct comedi_device *dev)
473 struct comedi_subdevice *s;
477 for (i = 0; i < dev->n_subdevices; i++) {
478 s = &dev->subdevices[i];
480 if (s->type == COMEDI_SUBD_UNUSED)
483 if (s->type == COMEDI_SUBD_DO) {
485 s->io_bits = (1 << s->n_chan) - 1;
487 s->io_bits = 0xffffffff;
490 if (s->len_chanlist == 0)
494 ret = __comedi_device_postconfig_async(dev, s);
499 if (!s->range_table && !s->range_table_list)
500 s->range_table = &range_unknown;
502 if (!s->insn_read && s->insn_bits)
503 s->insn_read = insn_rw_emulate_bits;
504 if (!s->insn_write && s->insn_bits)
505 s->insn_write = insn_rw_emulate_bits;
508 s->insn_read = insn_inval;
510 s->insn_write = insn_inval;
512 s->insn_bits = insn_inval;
514 s->insn_config = insn_inval;
517 s->poll = poll_invalid;
523 /* do a little post-config cleanup */
524 static int comedi_device_postconfig(struct comedi_device *dev)
528 ret = __comedi_device_postconfig(dev);
531 down_write(&dev->attach_lock);
532 dev->attached = true;
533 up_write(&dev->attach_lock);
538 * Generic recognize function for drivers that register their supported
541 * 'driv->board_name' points to a 'const char *' member within the
542 * zeroth element of an array of some private board information
543 * structure, say 'struct foo_board' containing a member 'const char
544 * *board_name' that is initialized to point to a board name string that
545 * is one of the candidates matched against this function's 'name'
548 * 'driv->offset' is the size of the private board information
549 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
550 * the length of the array of private board information structures.
552 * If one of the board names in the array of private board information
553 * structures matches the name supplied to this function, the function
554 * returns a pointer to the pointer to the board name, otherwise it
555 * returns NULL. The return value ends up in the 'board_ptr' member of
556 * a 'struct comedi_device' that the low-level comedi driver's
557 * 'attach()' hook can convert to a point to a particular element of its
558 * array of private board information structures by subtracting the
559 * offset of the member that points to the board name. (No subtraction
560 * is required if the board name pointer is the first member of the
561 * private board information structure, which is generally the case.)
563 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
565 char **name_ptr = (char **)driv->board_name;
568 for (i = 0; i < driv->num_names; i++) {
569 if (strcmp(*name_ptr, name) == 0)
571 name_ptr = (void *)name_ptr + driv->offset;
577 static void comedi_report_boards(struct comedi_driver *driv)
580 const char *const *name_ptr;
582 pr_info("comedi: valid board names for %s driver are:\n",
585 name_ptr = driv->board_name;
586 for (i = 0; i < driv->num_names; i++) {
587 pr_info(" %s\n", *name_ptr);
588 name_ptr = (const char **)((char *)name_ptr + driv->offset);
591 if (driv->num_names == 0)
592 pr_info(" %s\n", driv->driver_name);
596 * comedi_load_firmware() - Request and load firmware for a device.
597 * @dev: comedi_device struct
598 * @hw_device: device struct for the comedi_device
599 * @name: the name of the firmware image
600 * @cb: callback to the upload the firmware image
601 * @context: private context from the driver
603 int comedi_load_firmware(struct comedi_device *dev,
604 struct device *device,
606 int (*cb)(struct comedi_device *dev,
607 const u8 *data, size_t size,
608 unsigned long context),
609 unsigned long context)
611 const struct firmware *fw;
617 ret = request_firmware(&fw, name, device);
619 ret = cb(dev, fw->data, fw->size, context);
620 release_firmware(fw);
623 return ret < 0 ? ret : 0;
625 EXPORT_SYMBOL_GPL(comedi_load_firmware);
628 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
629 * @dev: comedi_device struct
630 * @start: base address of the I/O reqion
631 * @len: length of the I/O region
633 int __comedi_request_region(struct comedi_device *dev,
634 unsigned long start, unsigned long len)
637 dev_warn(dev->class_dev,
638 "%s: a I/O base address must be specified\n",
643 if (!request_region(start, len, dev->board_name)) {
644 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
645 dev->board_name, start, len);
651 EXPORT_SYMBOL_GPL(__comedi_request_region);
654 * comedi_request_region() - Request an I/O reqion for a legacy driver.
655 * @dev: comedi_device struct
656 * @start: base address of the I/O reqion
657 * @len: length of the I/O region
659 int comedi_request_region(struct comedi_device *dev,
660 unsigned long start, unsigned long len)
664 ret = __comedi_request_region(dev, start, len);
672 EXPORT_SYMBOL_GPL(comedi_request_region);
675 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
676 * @dev: comedi_device struct
678 void comedi_legacy_detach(struct comedi_device *dev)
681 free_irq(dev->irq, dev);
684 if (dev->iobase && dev->iolen) {
685 release_region(dev->iobase, dev->iolen);
690 EXPORT_SYMBOL_GPL(comedi_legacy_detach);
692 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
694 struct comedi_driver *driv;
700 mutex_lock(&comedi_drivers_list_lock);
701 for (driv = comedi_drivers; driv; driv = driv->next) {
702 if (!try_module_get(driv->module))
704 if (driv->num_names) {
705 dev->board_ptr = comedi_recognize(driv, it->board_name);
708 } else if (strcmp(driv->driver_name, it->board_name) == 0) {
711 module_put(driv->module);
714 /* recognize has failed if we get here */
715 /* report valid board names before returning error */
716 for (driv = comedi_drivers; driv; driv = driv->next) {
717 if (!try_module_get(driv->module))
719 comedi_report_boards(driv);
720 module_put(driv->module);
725 if (driv->attach == NULL) {
726 /* driver does not support manual configuration */
727 dev_warn(dev->class_dev,
728 "driver '%s' does not support attach using comedi_config\n",
730 module_put(driv->module);
735 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
736 : dev->driver->driver_name;
737 ret = driv->attach(dev, it);
739 ret = comedi_device_postconfig(dev);
741 comedi_device_detach(dev);
742 module_put(driv->module);
744 /* On success, the driver module count has been incremented. */
746 mutex_unlock(&comedi_drivers_list_lock);
750 int comedi_auto_config(struct device *hardware_device,
751 struct comedi_driver *driver, unsigned long context)
753 struct comedi_device *dev;
756 if (!hardware_device) {
757 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
761 dev_warn(hardware_device,
762 "BUG! comedi_auto_config called with NULL comedi driver\n");
766 if (!driver->auto_attach) {
767 dev_warn(hardware_device,
768 "BUG! comedi driver '%s' has no auto_attach handler\n",
769 driver->driver_name);
773 dev = comedi_alloc_board_minor(hardware_device);
775 dev_warn(hardware_device,
776 "driver '%s' could not create device.\n",
777 driver->driver_name);
780 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
782 dev->driver = driver;
783 dev->board_name = dev->driver->driver_name;
784 ret = driver->auto_attach(dev, context);
786 ret = comedi_device_postconfig(dev);
787 mutex_unlock(&dev->mutex);
790 dev_warn(hardware_device,
791 "driver '%s' failed to auto-configure device.\n",
792 driver->driver_name);
793 comedi_release_hardware_device(hardware_device);
796 * class_dev should be set properly here
797 * after a successful auto config
799 dev_info(dev->class_dev,
800 "driver '%s' has successfully auto-configured '%s'.\n",
801 driver->driver_name, dev->board_name);
805 EXPORT_SYMBOL_GPL(comedi_auto_config);
807 void comedi_auto_unconfig(struct device *hardware_device)
809 if (hardware_device == NULL)
811 comedi_release_hardware_device(hardware_device);
813 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
815 int comedi_driver_register(struct comedi_driver *driver)
817 mutex_lock(&comedi_drivers_list_lock);
818 driver->next = comedi_drivers;
819 comedi_drivers = driver;
820 mutex_unlock(&comedi_drivers_list_lock);
824 EXPORT_SYMBOL_GPL(comedi_driver_register);
826 void comedi_driver_unregister(struct comedi_driver *driver)
828 struct comedi_driver *prev;
831 /* unlink the driver */
832 mutex_lock(&comedi_drivers_list_lock);
833 if (comedi_drivers == driver) {
834 comedi_drivers = driver->next;
836 for (prev = comedi_drivers; prev->next; prev = prev->next) {
837 if (prev->next == driver) {
838 prev->next = driver->next;
843 mutex_unlock(&comedi_drivers_list_lock);
845 /* check for devices using this driver */
846 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
847 struct comedi_device *dev = comedi_dev_get_from_minor(i);
852 mutex_lock(&dev->mutex);
853 if (dev->attached && dev->driver == driver) {
855 dev_warn(dev->class_dev,
856 "BUG! detaching device with use_count=%d\n",
858 comedi_device_detach(dev);
860 mutex_unlock(&dev->mutex);
864 EXPORT_SYMBOL_GPL(comedi_driver_unregister);