x86, cpufeature: If we disable CLFLUSH, we should disable CLFLUSHOPT
[linux/fpc-iii.git] / drivers / staging / comedi / drivers.c
blob5b15033a94bf6106cc5b26b9da157ba24be60613
1 /*
2 module/drivers.c
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>
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>
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)
46 return 0;
47 if (dev->hw_dev != NULL)
48 return -EEXIST;
49 dev->hw_dev = get_device(hw_dev);
50 return 0;
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);
57 dev->hw_dev = NULL;
60 /**
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);
68 return dev->private;
70 EXPORT_SYMBOL_GPL(comedi_alloc_devpriv);
72 int comedi_alloc_subdevices(struct comedi_device *dev, int num_subdevices)
74 struct comedi_subdevice *s;
75 int i;
77 if (num_subdevices < 1)
78 return -EINVAL;
80 s = kcalloc(num_subdevices, sizeof(*s), GFP_KERNEL);
81 if (!s)
82 return -ENOMEM;
83 dev->subdevices = s;
84 dev->n_subdevices = num_subdevices;
86 for (i = 0; i < num_subdevices; ++i) {
87 s = &dev->subdevices[i];
88 s->device = dev;
89 s->index = i;
90 s->async_dma_dir = DMA_NONE;
91 spin_lock_init(&s->spin_lock);
92 s->minor = -1;
94 return 0;
96 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices);
98 static void comedi_device_detach_cleanup(struct comedi_device *dev)
100 int i;
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)
107 kfree(s->private);
108 comedi_free_subdevice_minor(s);
109 if (s->async) {
110 comedi_buf_alloc(dev, s, 0);
111 kfree(s->async);
114 kfree(dev->subdevices);
115 dev->subdevices = NULL;
116 dev->n_subdevices = 0;
118 kfree(dev->private);
119 dev->private = NULL;
120 dev->driver = NULL;
121 dev->board_name = NULL;
122 dev->board_ptr = NULL;
123 dev->iobase = 0;
124 dev->iolen = 0;
125 dev->ioenabled = false;
126 dev->irq = 0;
127 dev->read_subdev = NULL;
128 dev->write_subdev = NULL;
129 dev->open = NULL;
130 dev->close = NULL;
131 comedi_clear_hw_dev(dev);
134 void comedi_device_detach(struct comedi_device *dev)
136 comedi_device_cancel_all(dev);
137 down_write(&dev->attach_lock);
138 dev->attached = false;
139 dev->detach_count++;
140 if (dev->driver)
141 dev->driver->detach(dev);
142 comedi_device_detach_cleanup(dev);
143 up_write(&dev->attach_lock);
146 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
148 return -EINVAL;
151 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
152 struct comedi_insn *insn, unsigned int *data)
154 return -EINVAL;
158 * comedi_dio_insn_config() - boilerplate (*insn_config) for DIO subdevices.
159 * @dev: comedi_device struct
160 * @s: comedi_subdevice struct
161 * @insn: comedi_insn struct
162 * @data: parameters for the @insn
163 * @mask: io_bits mask for grouped channels
165 int comedi_dio_insn_config(struct comedi_device *dev,
166 struct comedi_subdevice *s,
167 struct comedi_insn *insn,
168 unsigned int *data,
169 unsigned int mask)
171 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
173 if (!mask)
174 mask = chan_mask;
176 switch (data[0]) {
177 case INSN_CONFIG_DIO_INPUT:
178 s->io_bits &= ~mask;
179 break;
181 case INSN_CONFIG_DIO_OUTPUT:
182 s->io_bits |= mask;
183 break;
185 case INSN_CONFIG_DIO_QUERY:
186 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
187 return insn->n;
189 default:
190 return -EINVAL;
193 return 0;
195 EXPORT_SYMBOL_GPL(comedi_dio_insn_config);
198 * comedi_dio_update_state() - update the internal state of DIO subdevices.
199 * @s: comedi_subdevice struct
200 * @data: the channel mask and bits to update
202 unsigned int comedi_dio_update_state(struct comedi_subdevice *s,
203 unsigned int *data)
205 unsigned int chanmask = (s->n_chan < 32) ? ((1 << s->n_chan) - 1)
206 : 0xffffffff;
207 unsigned int mask = data[0] & chanmask;
208 unsigned int bits = data[1];
210 if (mask) {
211 s->state &= ~mask;
212 s->state |= (bits & mask);
215 return mask;
217 EXPORT_SYMBOL_GPL(comedi_dio_update_state);
219 static int insn_rw_emulate_bits(struct comedi_device *dev,
220 struct comedi_subdevice *s,
221 struct comedi_insn *insn, unsigned int *data)
223 struct comedi_insn new_insn;
224 int ret;
225 static const unsigned channels_per_bitfield = 32;
227 unsigned chan = CR_CHAN(insn->chanspec);
228 const unsigned base_bitfield_channel =
229 (chan < channels_per_bitfield) ? 0 : chan;
230 unsigned int new_data[2];
231 memset(new_data, 0, sizeof(new_data));
232 memset(&new_insn, 0, sizeof(new_insn));
233 new_insn.insn = INSN_BITS;
234 new_insn.chanspec = base_bitfield_channel;
235 new_insn.n = 2;
236 new_insn.subdev = insn->subdev;
238 if (insn->insn == INSN_WRITE) {
239 if (!(s->subdev_flags & SDF_WRITABLE))
240 return -EINVAL;
241 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
242 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
243 : 0; /* bits */
246 ret = s->insn_bits(dev, s, &new_insn, new_data);
247 if (ret < 0)
248 return ret;
250 if (insn->insn == INSN_READ)
251 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
253 return 1;
256 static int __comedi_device_postconfig_async(struct comedi_device *dev,
257 struct comedi_subdevice *s)
259 struct comedi_async *async;
260 unsigned int buf_size;
261 int ret;
263 if ((s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) == 0) {
264 dev_warn(dev->class_dev,
265 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
266 return -EINVAL;
268 if (!s->do_cmdtest) {
269 dev_warn(dev->class_dev,
270 "async subdevices must have a do_cmdtest() function\n");
271 return -EINVAL;
274 async = kzalloc(sizeof(*async), GFP_KERNEL);
275 if (!async)
276 return -ENOMEM;
278 init_waitqueue_head(&async->wait_head);
279 async->subdevice = s;
280 s->async = async;
282 async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
283 buf_size = comedi_default_buf_size_kb * 1024;
284 if (buf_size > async->max_bufsize)
285 buf_size = async->max_bufsize;
287 if (comedi_buf_alloc(dev, s, buf_size) < 0) {
288 dev_warn(dev->class_dev, "Buffer allocation failed\n");
289 return -ENOMEM;
291 if (s->buf_change) {
292 ret = s->buf_change(dev, s, buf_size);
293 if (ret < 0)
294 return ret;
297 comedi_alloc_subdevice_minor(s);
299 return 0;
302 static int __comedi_device_postconfig(struct comedi_device *dev)
304 struct comedi_subdevice *s;
305 int ret;
306 int i;
308 for (i = 0; i < dev->n_subdevices; i++) {
309 s = &dev->subdevices[i];
311 if (s->type == COMEDI_SUBD_UNUSED)
312 continue;
314 if (s->type == COMEDI_SUBD_DO) {
315 if (s->n_chan < 32)
316 s->io_bits = (1 << s->n_chan) - 1;
317 else
318 s->io_bits = 0xffffffff;
321 if (s->len_chanlist == 0)
322 s->len_chanlist = 1;
324 if (s->do_cmd) {
325 ret = __comedi_device_postconfig_async(dev, s);
326 if (ret)
327 return ret;
330 if (!s->range_table && !s->range_table_list)
331 s->range_table = &range_unknown;
333 if (!s->insn_read && s->insn_bits)
334 s->insn_read = insn_rw_emulate_bits;
335 if (!s->insn_write && s->insn_bits)
336 s->insn_write = insn_rw_emulate_bits;
338 if (!s->insn_read)
339 s->insn_read = insn_inval;
340 if (!s->insn_write)
341 s->insn_write = insn_inval;
342 if (!s->insn_bits)
343 s->insn_bits = insn_inval;
344 if (!s->insn_config)
345 s->insn_config = insn_inval;
347 if (!s->poll)
348 s->poll = poll_invalid;
351 return 0;
354 /* do a little post-config cleanup */
355 static int comedi_device_postconfig(struct comedi_device *dev)
357 int ret;
359 ret = __comedi_device_postconfig(dev);
360 if (ret < 0)
361 return ret;
362 down_write(&dev->attach_lock);
363 dev->attached = true;
364 up_write(&dev->attach_lock);
365 return 0;
369 * Generic recognize function for drivers that register their supported
370 * board names.
372 * 'driv->board_name' points to a 'const char *' member within the
373 * zeroth element of an array of some private board information
374 * structure, say 'struct foo_board' containing a member 'const char
375 * *board_name' that is initialized to point to a board name string that
376 * is one of the candidates matched against this function's 'name'
377 * parameter.
379 * 'driv->offset' is the size of the private board information
380 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
381 * the length of the array of private board information structures.
383 * If one of the board names in the array of private board information
384 * structures matches the name supplied to this function, the function
385 * returns a pointer to the pointer to the board name, otherwise it
386 * returns NULL. The return value ends up in the 'board_ptr' member of
387 * a 'struct comedi_device' that the low-level comedi driver's
388 * 'attach()' hook can convert to a point to a particular element of its
389 * array of private board information structures by subtracting the
390 * offset of the member that points to the board name. (No subtraction
391 * is required if the board name pointer is the first member of the
392 * private board information structure, which is generally the case.)
394 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
396 char **name_ptr = (char **)driv->board_name;
397 int i;
399 for (i = 0; i < driv->num_names; i++) {
400 if (strcmp(*name_ptr, name) == 0)
401 return name_ptr;
402 name_ptr = (void *)name_ptr + driv->offset;
405 return NULL;
408 static void comedi_report_boards(struct comedi_driver *driv)
410 unsigned int i;
411 const char *const *name_ptr;
413 pr_info("comedi: valid board names for %s driver are:\n",
414 driv->driver_name);
416 name_ptr = driv->board_name;
417 for (i = 0; i < driv->num_names; i++) {
418 pr_info(" %s\n", *name_ptr);
419 name_ptr = (const char **)((char *)name_ptr + driv->offset);
422 if (driv->num_names == 0)
423 pr_info(" %s\n", driv->driver_name);
427 * comedi_load_firmware() - Request and load firmware for a device.
428 * @dev: comedi_device struct
429 * @hw_device: device struct for the comedi_device
430 * @name: the name of the firmware image
431 * @cb: callback to the upload the firmware image
432 * @context: private context from the driver
434 int comedi_load_firmware(struct comedi_device *dev,
435 struct device *device,
436 const char *name,
437 int (*cb)(struct comedi_device *dev,
438 const u8 *data, size_t size,
439 unsigned long context),
440 unsigned long context)
442 const struct firmware *fw;
443 int ret;
445 if (!cb)
446 return -EINVAL;
448 ret = request_firmware(&fw, name, device);
449 if (ret == 0) {
450 ret = cb(dev, fw->data, fw->size, context);
451 release_firmware(fw);
454 return ret < 0 ? ret : 0;
456 EXPORT_SYMBOL_GPL(comedi_load_firmware);
459 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
460 * @dev: comedi_device struct
461 * @start: base address of the I/O reqion
462 * @len: length of the I/O region
464 int __comedi_request_region(struct comedi_device *dev,
465 unsigned long start, unsigned long len)
467 if (!start) {
468 dev_warn(dev->class_dev,
469 "%s: a I/O base address must be specified\n",
470 dev->board_name);
471 return -EINVAL;
474 if (!request_region(start, len, dev->board_name)) {
475 dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
476 dev->board_name, start, len);
477 return -EIO;
480 return 0;
482 EXPORT_SYMBOL_GPL(__comedi_request_region);
485 * comedi_request_region() - Request an I/O reqion for a legacy driver.
486 * @dev: comedi_device struct
487 * @start: base address of the I/O reqion
488 * @len: length of the I/O region
490 int comedi_request_region(struct comedi_device *dev,
491 unsigned long start, unsigned long len)
493 int ret;
495 ret = __comedi_request_region(dev, start, len);
496 if (ret == 0) {
497 dev->iobase = start;
498 dev->iolen = len;
501 return ret;
503 EXPORT_SYMBOL_GPL(comedi_request_region);
506 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
507 * @dev: comedi_device struct
509 void comedi_legacy_detach(struct comedi_device *dev)
511 if (dev->irq) {
512 free_irq(dev->irq, dev);
513 dev->irq = 0;
515 if (dev->iobase && dev->iolen) {
516 release_region(dev->iobase, dev->iolen);
517 dev->iobase = 0;
518 dev->iolen = 0;
521 EXPORT_SYMBOL_GPL(comedi_legacy_detach);
523 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
525 struct comedi_driver *driv;
526 int ret;
528 if (dev->attached)
529 return -EBUSY;
531 mutex_lock(&comedi_drivers_list_lock);
532 for (driv = comedi_drivers; driv; driv = driv->next) {
533 if (!try_module_get(driv->module))
534 continue;
535 if (driv->num_names) {
536 dev->board_ptr = comedi_recognize(driv, it->board_name);
537 if (dev->board_ptr)
538 break;
539 } else if (strcmp(driv->driver_name, it->board_name) == 0)
540 break;
541 module_put(driv->module);
543 if (driv == NULL) {
544 /* recognize has failed if we get here */
545 /* report valid board names before returning error */
546 for (driv = comedi_drivers; driv; driv = driv->next) {
547 if (!try_module_get(driv->module))
548 continue;
549 comedi_report_boards(driv);
550 module_put(driv->module);
552 ret = -EIO;
553 goto out;
555 if (driv->attach == NULL) {
556 /* driver does not support manual configuration */
557 dev_warn(dev->class_dev,
558 "driver '%s' does not support attach using comedi_config\n",
559 driv->driver_name);
560 module_put(driv->module);
561 ret = -ENOSYS;
562 goto out;
564 /* initialize dev->driver here so
565 * comedi_error() can be called from attach */
566 dev->driver = driv;
567 dev->board_name = dev->board_ptr ? *(const char **)dev->board_ptr
568 : dev->driver->driver_name;
569 ret = driv->attach(dev, it);
570 if (ret >= 0)
571 ret = comedi_device_postconfig(dev);
572 if (ret < 0) {
573 comedi_device_detach(dev);
574 module_put(driv->module);
576 /* On success, the driver module count has been incremented. */
577 out:
578 mutex_unlock(&comedi_drivers_list_lock);
579 return ret;
582 int comedi_auto_config(struct device *hardware_device,
583 struct comedi_driver *driver, unsigned long context)
585 struct comedi_device *dev;
586 int ret;
588 if (!hardware_device) {
589 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
590 return -EINVAL;
592 if (!driver) {
593 dev_warn(hardware_device,
594 "BUG! comedi_auto_config called with NULL comedi driver\n");
595 return -EINVAL;
598 if (!driver->auto_attach) {
599 dev_warn(hardware_device,
600 "BUG! comedi driver '%s' has no auto_attach handler\n",
601 driver->driver_name);
602 return -EINVAL;
605 dev = comedi_alloc_board_minor(hardware_device);
606 if (IS_ERR(dev)) {
607 dev_warn(hardware_device,
608 "driver '%s' could not create device.\n",
609 driver->driver_name);
610 return PTR_ERR(dev);
612 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
614 dev->driver = driver;
615 dev->board_name = dev->driver->driver_name;
616 ret = driver->auto_attach(dev, context);
617 if (ret >= 0)
618 ret = comedi_device_postconfig(dev);
619 mutex_unlock(&dev->mutex);
621 if (ret < 0) {
622 dev_warn(hardware_device,
623 "driver '%s' failed to auto-configure device.\n",
624 driver->driver_name);
625 comedi_release_hardware_device(hardware_device);
626 } else {
628 * class_dev should be set properly here
629 * after a successful auto config
631 dev_info(dev->class_dev,
632 "driver '%s' has successfully auto-configured '%s'.\n",
633 driver->driver_name, dev->board_name);
635 return ret;
637 EXPORT_SYMBOL_GPL(comedi_auto_config);
639 void comedi_auto_unconfig(struct device *hardware_device)
641 if (hardware_device == NULL)
642 return;
643 comedi_release_hardware_device(hardware_device);
645 EXPORT_SYMBOL_GPL(comedi_auto_unconfig);
647 int comedi_driver_register(struct comedi_driver *driver)
649 mutex_lock(&comedi_drivers_list_lock);
650 driver->next = comedi_drivers;
651 comedi_drivers = driver;
652 mutex_unlock(&comedi_drivers_list_lock);
654 return 0;
656 EXPORT_SYMBOL_GPL(comedi_driver_register);
658 void comedi_driver_unregister(struct comedi_driver *driver)
660 struct comedi_driver *prev;
661 int i;
663 /* unlink the driver */
664 mutex_lock(&comedi_drivers_list_lock);
665 if (comedi_drivers == driver) {
666 comedi_drivers = driver->next;
667 } else {
668 for (prev = comedi_drivers; prev->next; prev = prev->next) {
669 if (prev->next == driver) {
670 prev->next = driver->next;
671 break;
675 mutex_unlock(&comedi_drivers_list_lock);
677 /* check for devices using this driver */
678 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
679 struct comedi_device *dev = comedi_dev_get_from_minor(i);
681 if (!dev)
682 continue;
684 mutex_lock(&dev->mutex);
685 if (dev->attached && dev->driver == driver) {
686 if (dev->use_count)
687 dev_warn(dev->class_dev,
688 "BUG! detaching device with use_count=%d\n",
689 dev->use_count);
690 comedi_device_detach(dev);
692 mutex_unlock(&dev->mutex);
693 comedi_dev_put(dev);
696 EXPORT_SYMBOL_GPL(comedi_driver_unregister);