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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/device.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <pcmcia/cistpl.h>
28 #include <pcmcia/ds.h>
29 #include <linux/usb.h>
30 #include <linux/errno.h>
31 #include <linux/kconfig.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/fcntl.h>
35 #include <linux/delay.h>
36 #include <linux/ioport.h>
38 #include <linux/slab.h>
39 #include <linux/highmem.h> /* for SuSE brokenness */
40 #include <linux/vmalloc.h>
41 #include <linux/cdev.h>
42 #include <linux/dma-mapping.h>
45 #include "comedidev.h"
46 #include "comedi_internal.h"
48 struct comedi_driver *comedi_drivers;
50 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
52 struct comedi_subdevice *s;
55 if (num_subdevices < 1)
58 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
62 dev->n_subdevices = num_subdevices;
64 for (i = 0; i < num_subdevices; ++i) {
65 s = &dev->subdevices[i];
68 s->async_dma_dir = DMA_NONE;
69 spin_lock_init(&s->spin_lock);
74 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
76 static void cleanup_device(struct comedi_device *dev)
79 struct comedi_subdevice *s;
81 if (dev->subdevices) {
82 for (i = 0; i < dev->n_subdevices; i++) {
83 s = &dev->subdevices[i];
84 comedi_free_subdevice_minor(s);
86 comedi_buf_alloc(dev, s, 0);
90 kfree(dev->subdevices);
91 dev->subdevices = NULL;
92 dev->n_subdevices = 0;
97 dev->board_name = NULL;
98 dev->board_ptr = NULL;
101 dev->read_subdev = NULL;
102 dev->write_subdev = NULL;
105 comedi_set_hw_dev(dev, NULL);
108 static void __comedi_device_detach(struct comedi_device *dev)
112 dev->driver->detach(dev);
114 dev_warn(dev->class_dev,
115 "BUG: dev->driver=NULL in comedi_device_detach()\n");
119 void comedi_device_detach(struct comedi_device *dev)
123 __comedi_device_detach(dev);
126 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
131 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
132 struct comedi_insn *insn, unsigned int *data)
137 static int insn_rw_emulate_bits(struct comedi_device *dev,
138 struct comedi_subdevice *s,
139 struct comedi_insn *insn, unsigned int *data)
141 struct comedi_insn new_insn;
143 static const unsigned channels_per_bitfield = 32;
145 unsigned chan = CR_CHAN(insn->chanspec);
146 const unsigned base_bitfield_channel =
147 (chan < channels_per_bitfield) ? 0 : chan;
148 unsigned int new_data[2];
149 memset(new_data, 0, sizeof(new_data));
150 memset(&new_insn, 0, sizeof(new_insn));
151 new_insn.insn = INSN_BITS;
152 new_insn.chanspec = base_bitfield_channel;
154 new_insn.subdev = insn->subdev;
156 if (insn->insn == INSN_WRITE) {
157 if (!(s->subdev_flags & SDF_WRITABLE))
159 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
160 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
164 ret = s->insn_bits(dev, s, &new_insn, new_data);
168 if (insn->insn == INSN_READ)
169 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
174 static int __comedi_device_postconfig_async(struct comedi_device *dev,
175 struct comedi_subdevice *s)
177 struct comedi_async *async;
178 unsigned int buf_size;
181 BUG_ON((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0);
182 BUG_ON(!s->do_cmdtest);
184 async = kzalloc(sizeof(*async), GFP_KERNEL);
186 dev_warn(dev->class_dev, "failed to allocate async struct\n");
189 init_waitqueue_head(&async->wait_head);
190 async->subdevice = s;
193 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
194 buf_size = comedi_default_buf_size_kb * 1024;
195 if (buf_size > async->max_bufsize)
196 buf_size = async->max_bufsize;
198 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
199 dev_warn(dev->class_dev, "Buffer allocation failed\n");
203 ret = s->buf_change(dev, s, buf_size);
208 comedi_alloc_subdevice_minor(dev, s);
213 static int __comedi_device_postconfig(struct comedi_device *dev)
215 struct comedi_subdevice *s;
219 for (i = 0; i < dev->n_subdevices; i++) {
220 s = &dev->subdevices[i];
222 if (s->type == COMEDI_SUBD_UNUSED)
225 if (s->len_chanlist == 0)
229 ret = __comedi_device_postconfig_async(dev, s);
234 if (!s->range_table && !s->range_table_list)
235 s->range_table = &range_unknown;
237 if (!s->insn_read && s->insn_bits)
238 s->insn_read = insn_rw_emulate_bits;
239 if (!s->insn_write && s->insn_bits)
240 s->insn_write = insn_rw_emulate_bits;
243 s->insn_read = insn_inval;
245 s->insn_write = insn_inval;
247 s->insn_bits = insn_inval;
249 s->insn_config = insn_inval;
252 s->poll = poll_invalid;
258 /* do a little post-config cleanup */
259 /* called with module refcount incremented, decrements it */
260 static int comedi_device_postconfig(struct comedi_device *dev)
262 int ret = __comedi_device_postconfig(dev);
263 module_put(dev->driver->module);
265 __comedi_device_detach(dev);
268 if (!dev->board_name) {
269 dev_warn(dev->class_dev, "BUG: dev->board_name=NULL\n");
270 dev->board_name = "BUG";
278 * Generic recognize function for drivers that register their supported
281 * 'driv->board_name' points to a 'const char *' member within the
282 * zeroth element of an array of some private board information
283 * structure, say 'struct foo_board' containing a member 'const char
284 * *board_name' that is initialized to point to a board name string that
285 * is one of the candidates matched against this function's 'name'
288 * 'driv->offset' is the size of the private board information
289 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
290 * the length of the array of private board information structures.
292 * If one of the board names in the array of private board information
293 * structures matches the name supplied to this function, the function
294 * returns a pointer to the pointer to the board name, otherwise it
295 * returns NULL. The return value ends up in the 'board_ptr' member of
296 * a 'struct comedi_device' that the low-level comedi driver's
297 * 'attach()' hook can convert to a point to a particular element of its
298 * array of private board information structures by subtracting the
299 * offset of the member that points to the board name. (No subtraction
300 * is required if the board name pointer is the first member of the
301 * private board information structure, which is generally the case.)
303 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
305 char **name_ptr = (char **)driv->board_name;
308 for (i = 0; i < driv->num_names; i++) {
309 if (strcmp(*name_ptr, name) == 0)
311 name_ptr = (void *)name_ptr + driv->offset;
317 static void comedi_report_boards(struct comedi_driver *driv)
320 const char *const *name_ptr;
322 pr_info("comedi: valid board names for %s driver are:\n",
325 name_ptr = driv->board_name;
326 for (i = 0; i < driv->num_names; i++) {
327 pr_info(" %s\n", *name_ptr);
328 name_ptr = (const char **)((char *)name_ptr + driv->offset);
331 if (driv->num_names == 0)
332 pr_info(" %s\n", driv->driver_name);
335 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
337 struct comedi_driver *driv;
343 for (driv = comedi_drivers; driv; driv = driv->next) {
344 if (!try_module_get(driv->module))
346 if (driv->num_names) {
347 dev->board_ptr = comedi_recognize(driv, it->board_name);
350 } else if (strcmp(driv->driver_name, it->board_name) == 0)
352 module_put(driv->module);
355 /* recognize has failed if we get here */
356 /* report valid board names before returning error */
357 for (driv = comedi_drivers; driv; driv = driv->next) {
358 if (!try_module_get(driv->module))
360 comedi_report_boards(driv);
361 module_put(driv->module);
365 if (driv->attach == NULL) {
366 /* driver does not support manual configuration */
367 dev_warn(dev->class_dev,
368 "driver '%s' does not support attach using comedi_config\n",
370 module_put(driv->module);
373 /* initialize dev->driver here so
374 * comedi_error() can be called from attach */
376 ret = driv->attach(dev, it);
378 module_put(dev->driver->module);
379 __comedi_device_detach(dev);
382 return comedi_device_postconfig(dev);
385 int comedi_driver_register(struct comedi_driver *driver)
387 driver->next = comedi_drivers;
388 comedi_drivers = driver;
392 EXPORT_SYMBOL(comedi_driver_register);
394 int comedi_driver_unregister(struct comedi_driver *driver)
396 struct comedi_driver *prev;
399 /* check for devices using this driver */
400 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
401 struct comedi_device *dev = comedi_dev_from_minor(i);
406 mutex_lock(&dev->mutex);
407 if (dev->attached && dev->driver == driver) {
409 dev_warn(dev->class_dev,
410 "BUG! detaching device with use_count=%d\n",
412 comedi_device_detach(dev);
414 mutex_unlock(&dev->mutex);
417 if (comedi_drivers == driver) {
418 comedi_drivers = driver->next;
422 for (prev = comedi_drivers; prev->next; prev = prev->next) {
423 if (prev->next == driver) {
424 prev->next = driver->next;
430 EXPORT_SYMBOL(comedi_driver_unregister);
432 int comedi_auto_config(struct device *hardware_device,
433 struct comedi_driver *driver, unsigned long context)
436 struct comedi_device *comedi_dev;
439 if (!comedi_autoconfig)
442 if (!driver->auto_attach) {
443 dev_warn(hardware_device,
444 "BUG! comedi driver '%s' has no auto_attach handler\n",
445 driver->driver_name);
449 minor = comedi_alloc_board_minor(hardware_device);
453 comedi_dev = comedi_dev_from_minor(minor);
455 mutex_lock(&comedi_dev->mutex);
456 if (comedi_dev->attached)
458 else if (!try_module_get(driver->module))
461 comedi_set_hw_dev(comedi_dev, hardware_device);
462 comedi_dev->driver = driver;
463 ret = driver->auto_attach(comedi_dev, context);
465 module_put(driver->module);
466 __comedi_device_detach(comedi_dev);
468 ret = comedi_device_postconfig(comedi_dev);
471 mutex_unlock(&comedi_dev->mutex);
474 comedi_free_board_minor(minor);
477 EXPORT_SYMBOL_GPL(comedi_auto_config);
479 void comedi_auto_unconfig(struct device *hardware_device)
483 if (hardware_device == NULL)
485 minor = comedi_find_board_minor(hardware_device);
488 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
489 comedi_free_board_minor(minor);
491 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
494 * comedi_pci_enable() - Enable the PCI device and request the regions.
495 * @pdev: pci_dev struct
496 * @res_name: name for the requested reqource
498 int comedi_pci_enable(struct pci_dev *pdev, const char *res_name)
502 rc = pci_enable_device(pdev);
506 rc = pci_request_regions(pdev, res_name);
508 pci_disable_device(pdev);
512 EXPORT_SYMBOL_GPL(comedi_pci_enable);
515 * comedi_pci_disable() - Release the regions and disable the PCI device.
516 * @pdev: pci_dev struct
518 * This must be matched with a previous successful call to comedi_pci_enable().
520 void comedi_pci_disable(struct pci_dev *pdev)
522 pci_release_regions(pdev);
523 pci_disable_device(pdev);
525 EXPORT_SYMBOL_GPL(comedi_pci_disable);
527 int comedi_pci_driver_register(struct comedi_driver *comedi_driver,
528 struct pci_driver *pci_driver)
532 ret = comedi_driver_register(comedi_driver);
536 ret = pci_register_driver(pci_driver);
538 comedi_driver_unregister(comedi_driver);
544 EXPORT_SYMBOL_GPL(comedi_pci_driver_register);
546 void comedi_pci_driver_unregister(struct comedi_driver *comedi_driver,
547 struct pci_driver *pci_driver)
549 pci_unregister_driver(pci_driver);
550 comedi_driver_unregister(comedi_driver);
552 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister);
554 #if IS_ENABLED(CONFIG_PCMCIA)
555 int comedi_pcmcia_driver_register(struct comedi_driver *comedi_driver,
556 struct pcmcia_driver *pcmcia_driver)
560 ret = comedi_driver_register(comedi_driver);
564 ret = pcmcia_register_driver(pcmcia_driver);
566 comedi_driver_unregister(comedi_driver);
572 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_register);
574 void comedi_pcmcia_driver_unregister(struct comedi_driver *comedi_driver,
575 struct pcmcia_driver *pcmcia_driver)
577 pcmcia_unregister_driver(pcmcia_driver);
578 comedi_driver_unregister(comedi_driver);
580 EXPORT_SYMBOL_GPL(comedi_pcmcia_driver_unregister);
584 #if IS_ENABLED(CONFIG_USB)
586 int comedi_usb_driver_register(struct comedi_driver *comedi_driver,
587 struct usb_driver *usb_driver)
591 ret = comedi_driver_register(comedi_driver);
595 ret = usb_register(usb_driver);
597 comedi_driver_unregister(comedi_driver);
603 EXPORT_SYMBOL_GPL(comedi_usb_driver_register);
605 void comedi_usb_driver_unregister(struct comedi_driver *comedi_driver,
606 struct usb_driver *usb_driver)
608 usb_deregister(usb_driver);
609 comedi_driver_unregister(comedi_driver);
611 EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister);