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.
26 #define __NO_VERSION__
27 #include "comedi_fops.h"
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/usb.h>
32 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/fcntl.h>
36 #include <linux/delay.h>
37 #include <linux/ioport.h>
39 #include <linux/slab.h>
40 #include <linux/highmem.h> /* for SuSE brokenness */
41 #include <linux/vmalloc.h>
42 #include <linux/cdev.h>
43 #include <linux/dma-mapping.h>
46 #include "comedidev.h"
49 static int postconfig(struct comedi_device *dev);
50 static int insn_rw_emulate_bits(struct comedi_device *dev,
51 struct comedi_subdevice *s,
52 struct comedi_insn *insn, unsigned int *data);
53 static void *comedi_recognize(struct comedi_driver *driv, const char *name);
54 static void comedi_report_boards(struct comedi_driver *driv);
55 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s);
57 struct comedi_driver *comedi_drivers;
59 static void cleanup_device(struct comedi_device *dev)
62 struct comedi_subdevice *s;
64 if (dev->subdevices) {
65 for (i = 0; i < dev->n_subdevices; i++) {
66 s = dev->subdevices + i;
67 comedi_free_subdevice_minor(s);
69 comedi_buf_alloc(dev, s, 0);
73 kfree(dev->subdevices);
74 dev->subdevices = NULL;
75 dev->n_subdevices = 0;
80 dev->board_name = NULL;
81 dev->board_ptr = NULL;
84 dev->read_subdev = NULL;
85 dev->write_subdev = NULL;
88 comedi_set_hw_dev(dev, NULL);
91 static void __comedi_device_detach(struct comedi_device *dev)
95 dev->driver->detach(dev);
98 "BUG: dev->driver=NULL in comedi_device_detach()\n");
102 void comedi_device_detach(struct comedi_device *dev)
106 __comedi_device_detach(dev);
109 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
111 struct comedi_driver *driv;
117 for (driv = comedi_drivers; driv; driv = driv->next) {
118 if (!try_module_get(driv->module)) {
119 printk(KERN_INFO "comedi: failed to increment module count, skipping\n");
122 if (driv->num_names) {
123 dev->board_ptr = comedi_recognize(driv, it->board_name);
124 if (dev->board_ptr == NULL) {
125 module_put(driv->module);
129 if (strcmp(driv->driver_name, it->board_name)) {
130 module_put(driv->module);
134 /* initialize dev->driver here so
135 * comedi_error() can be called from attach */
137 ret = driv->attach(dev, it);
139 module_put(dev->driver->module);
140 __comedi_device_detach(dev);
146 /* recognize has failed if we get here */
147 /* report valid board names before returning error */
148 for (driv = comedi_drivers; driv; driv = driv->next) {
149 if (!try_module_get(driv->module)) {
151 "comedi: failed to increment module count\n");
154 comedi_report_boards(driv);
155 module_put(driv->module);
160 /* do a little post-config cleanup */
161 ret = postconfig(dev);
162 module_put(dev->driver->module);
164 __comedi_device_detach(dev);
168 if (!dev->board_name) {
169 printk(KERN_WARNING "BUG: dev->board_name=<%p>\n",
171 dev->board_name = "BUG";
179 int comedi_driver_register(struct comedi_driver *driver)
181 driver->next = comedi_drivers;
182 comedi_drivers = driver;
186 EXPORT_SYMBOL(comedi_driver_register);
188 int comedi_driver_unregister(struct comedi_driver *driver)
190 struct comedi_driver *prev;
193 /* check for devices using this driver */
194 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
195 struct comedi_device_file_info *dev_file_info =
196 comedi_get_device_file_info(i);
197 struct comedi_device *dev;
199 if (dev_file_info == NULL)
201 dev = dev_file_info->device;
203 mutex_lock(&dev->mutex);
204 if (dev->attached && dev->driver == driver) {
206 printk(KERN_WARNING "BUG! detaching device with use_count=%d\n",
208 comedi_device_detach(dev);
210 mutex_unlock(&dev->mutex);
213 if (comedi_drivers == driver) {
214 comedi_drivers = driver->next;
218 for (prev = comedi_drivers; prev->next; prev = prev->next) {
219 if (prev->next == driver) {
220 prev->next = driver->next;
226 EXPORT_SYMBOL(comedi_driver_unregister);
228 static int postconfig(struct comedi_device *dev)
231 struct comedi_subdevice *s;
232 struct comedi_async *async = NULL;
235 for (i = 0; i < dev->n_subdevices; i++) {
236 s = dev->subdevices + i;
238 if (s->type == COMEDI_SUBD_UNUSED)
241 if (s->len_chanlist == 0)
245 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
246 SDF_CMD_WRITE)) == 0);
247 BUG_ON(!s->do_cmdtest);
250 kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
253 "failed to allocate async struct\n");
256 init_waitqueue_head(&async->wait_head);
257 async->subdevice = s;
260 #define DEFAULT_BUF_MAXSIZE (64*1024)
261 #define DEFAULT_BUF_SIZE (64*1024)
263 async->max_bufsize = DEFAULT_BUF_MAXSIZE;
265 async->prealloc_buf = NULL;
266 async->prealloc_bufsz = 0;
267 if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
268 printk(KERN_INFO "Buffer allocation failed\n");
272 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
276 comedi_alloc_subdevice_minor(dev, s);
279 if (!s->range_table && !s->range_table_list)
280 s->range_table = &range_unknown;
282 if (!s->insn_read && s->insn_bits)
283 s->insn_read = insn_rw_emulate_bits;
284 if (!s->insn_write && s->insn_bits)
285 s->insn_write = insn_rw_emulate_bits;
288 s->insn_read = insn_inval;
290 s->insn_write = insn_inval;
292 s->insn_bits = insn_inval;
294 s->insn_config = insn_inval;
297 s->poll = poll_invalid;
303 /* generic recognize function for drivers
304 * that register their supported board names */
305 static void *comedi_recognize(struct comedi_driver *driv, const char *name)
308 const char *const *name_ptr = driv->board_name;
309 for (i = 0; i < driv->num_names; i++) {
310 if (strcmp(*name_ptr, name) == 0)
311 return (void *)name_ptr;
313 (const char *const *)((const char *)name_ptr +
320 static void comedi_report_boards(struct comedi_driver *driv)
323 const char *const *name_ptr;
325 printk(KERN_INFO "comedi: valid board names for %s driver are:\n",
328 name_ptr = driv->board_name;
329 for (i = 0; i < driv->num_names; i++) {
330 printk(KERN_INFO " %s\n", *name_ptr);
331 name_ptr = (const char **)((char *)name_ptr + driv->offset);
334 if (driv->num_names == 0)
335 printk(KERN_INFO " %s\n", driv->driver_name);
338 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
343 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
344 struct comedi_insn *insn, unsigned int *data)
349 static int insn_rw_emulate_bits(struct comedi_device *dev,
350 struct comedi_subdevice *s,
351 struct comedi_insn *insn, unsigned int *data)
353 struct comedi_insn new_insn;
355 static const unsigned channels_per_bitfield = 32;
357 unsigned chan = CR_CHAN(insn->chanspec);
358 const unsigned base_bitfield_channel =
359 (chan < channels_per_bitfield) ? 0 : chan;
360 unsigned int new_data[2];
361 memset(new_data, 0, sizeof(new_data));
362 memset(&new_insn, 0, sizeof(new_insn));
363 new_insn.insn = INSN_BITS;
364 new_insn.chanspec = base_bitfield_channel;
366 new_insn.data = new_data;
367 new_insn.subdev = insn->subdev;
369 if (insn->insn == INSN_WRITE) {
370 if (!(s->subdev_flags & SDF_WRITABLE))
372 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
373 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
377 ret = s->insn_bits(dev, s, &new_insn, new_data);
381 if (insn->insn == INSN_READ)
382 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
387 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
389 unsigned long ret = 0UL;
394 if (!pgd_none(*pgd)) {
395 pud = pud_offset(pgd, adr);
396 pmd = pmd_offset(pud, adr);
397 if (!pmd_none(*pmd)) {
398 ptep = pte_offset_kernel(pmd, adr);
400 if (pte_present(pte)) {
401 ret = (unsigned long)
402 page_address(pte_page(pte));
403 ret |= (adr & (PAGE_SIZE - 1));
410 static inline unsigned long kvirt_to_kva(unsigned long adr)
412 unsigned long va, kva;
415 kva = uvirt_to_kva(pgd_offset_k(va), va);
420 int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
421 unsigned long new_size)
423 struct comedi_async *async = s->async;
425 /* Round up new_size to multiple of PAGE_SIZE */
426 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
428 /* if no change is required, do nothing */
429 if (async->prealloc_buf && async->prealloc_bufsz == new_size)
432 /* deallocate old buffer */
433 if (async->prealloc_buf) {
434 vunmap(async->prealloc_buf);
435 async->prealloc_buf = NULL;
436 async->prealloc_bufsz = 0;
438 if (async->buf_page_list) {
440 for (i = 0; i < async->n_buf_pages; ++i) {
441 if (async->buf_page_list[i].virt_addr) {
442 clear_bit(PG_reserved,
443 &(virt_to_page(async->buf_page_list[i].
445 if (s->async_dma_dir != DMA_NONE) {
446 dma_free_coherent(dev->hw_dev,
455 free_page((unsigned long)
456 async->buf_page_list[i].
461 vfree(async->buf_page_list);
462 async->buf_page_list = NULL;
463 async->n_buf_pages = 0;
465 /* allocate new buffer */
468 unsigned n_pages = new_size >> PAGE_SHIFT;
469 struct page **pages = NULL;
471 async->buf_page_list =
472 vzalloc(sizeof(struct comedi_buf_page) * n_pages);
473 if (async->buf_page_list)
474 pages = vmalloc(sizeof(struct page *) * n_pages);
477 for (i = 0; i < n_pages; i++) {
478 if (s->async_dma_dir != DMA_NONE) {
479 async->buf_page_list[i].virt_addr =
480 dma_alloc_coherent(dev->hw_dev,
488 async->buf_page_list[i].virt_addr =
490 get_zeroed_page(GFP_KERNEL);
492 if (async->buf_page_list[i].virt_addr == NULL)
496 &(virt_to_page(async->buf_page_list[i].
498 pages[i] = virt_to_page(async->buf_page_list[i].
503 async->prealloc_buf =
504 #ifdef PAGE_KERNEL_NOCACHE
505 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE);
507 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL);
512 if (async->prealloc_buf == NULL) {
513 /* Some allocation failed above. */
514 if (async->buf_page_list) {
515 for (i = 0; i < n_pages; i++) {
516 if (async->buf_page_list[i].virt_addr ==
520 clear_bit(PG_reserved,
521 &(virt_to_page(async->
524 if (s->async_dma_dir != DMA_NONE) {
525 dma_free_coherent(dev->hw_dev,
534 free_page((unsigned long)
539 vfree(async->buf_page_list);
540 async->buf_page_list = NULL;
544 async->n_buf_pages = n_pages;
546 async->prealloc_bufsz = new_size;
551 /* munging is applied to data by core as it passes between user
552 * and kernel space */
553 static unsigned int comedi_buf_munge(struct comedi_async *async,
554 unsigned int num_bytes)
556 struct comedi_subdevice *s = async->subdevice;
557 unsigned int count = 0;
558 const unsigned num_sample_bytes = bytes_per_sample(s);
560 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
561 async->munge_count += num_bytes;
562 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
565 /* don't munge partial samples */
566 num_bytes -= num_bytes % num_sample_bytes;
567 while (count < num_bytes) {
570 block_size = num_bytes - count;
571 if (block_size < 0) {
573 "%s: %s: bug! block_size is negative\n",
577 if ((int)(async->munge_ptr + block_size -
578 async->prealloc_bufsz) > 0)
579 block_size = async->prealloc_bufsz - async->munge_ptr;
581 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
582 block_size, async->munge_chan);
584 smp_wmb(); /* barrier insures data is munged in buffer
585 * before munge_count is incremented */
587 async->munge_chan += block_size / num_sample_bytes;
588 async->munge_chan %= async->cmd.chanlist_len;
589 async->munge_count += block_size;
590 async->munge_ptr += block_size;
591 async->munge_ptr %= async->prealloc_bufsz;
594 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
598 unsigned int comedi_buf_write_n_available(struct comedi_async *async)
600 unsigned int free_end;
606 free_end = async->buf_read_count + async->prealloc_bufsz;
607 nbytes = free_end - async->buf_write_alloc_count;
608 nbytes -= nbytes % bytes_per_sample(async->subdevice);
609 /* barrier insures the read of buf_read_count in this
610 query occurs before any following writes to the buffer which
611 might be based on the return value from this query.
617 /* allocates chunk for the writer from free buffer space */
618 unsigned int comedi_buf_write_alloc(struct comedi_async *async,
621 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
623 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
624 nbytes = free_end - async->buf_write_alloc_count;
626 async->buf_write_alloc_count += nbytes;
627 /* barrier insures the read of buf_read_count above occurs before
628 we write data to the write-alloc'ed buffer space */
632 EXPORT_SYMBOL(comedi_buf_write_alloc);
634 /* allocates nothing unless it can completely fulfill the request */
635 unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
638 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
640 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
643 async->buf_write_alloc_count += nbytes;
644 /* barrier insures the read of buf_read_count above occurs before
645 we write data to the write-alloc'ed buffer space */
650 /* transfers a chunk from writer to filled buffer space */
651 unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
653 if ((int)(async->buf_write_count + nbytes -
654 async->buf_write_alloc_count) > 0) {
655 printk(KERN_INFO "comedi: attempted to write-free more bytes than have been write-allocated.\n");
656 nbytes = async->buf_write_alloc_count - async->buf_write_count;
658 async->buf_write_count += nbytes;
659 async->buf_write_ptr += nbytes;
660 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
661 if (async->buf_write_ptr >= async->prealloc_bufsz)
662 async->buf_write_ptr %= async->prealloc_bufsz;
666 EXPORT_SYMBOL(comedi_buf_write_free);
668 /* allocates a chunk for the reader from filled (and munged) buffer space */
669 unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
671 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
673 nbytes = async->munge_count - async->buf_read_alloc_count;
675 async->buf_read_alloc_count += nbytes;
676 /* barrier insures read of munge_count occurs before we actually read
677 data out of buffer */
681 EXPORT_SYMBOL(comedi_buf_read_alloc);
683 /* transfers control of a chunk from reader to free buffer space */
684 unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes)
686 /* barrier insures data has been read out of
687 * buffer before read count is incremented */
689 if ((int)(async->buf_read_count + nbytes -
690 async->buf_read_alloc_count) > 0) {
692 "comedi: attempted to read-free more bytes than have been read-allocated.\n");
693 nbytes = async->buf_read_alloc_count - async->buf_read_count;
695 async->buf_read_count += nbytes;
696 async->buf_read_ptr += nbytes;
697 async->buf_read_ptr %= async->prealloc_bufsz;
700 EXPORT_SYMBOL(comedi_buf_read_free);
702 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
703 const void *data, unsigned int num_bytes)
705 unsigned int write_ptr = async->buf_write_ptr + offset;
707 if (write_ptr >= async->prealloc_bufsz)
708 write_ptr %= async->prealloc_bufsz;
711 unsigned int block_size;
713 if (write_ptr + num_bytes > async->prealloc_bufsz)
714 block_size = async->prealloc_bufsz - write_ptr;
716 block_size = num_bytes;
718 memcpy(async->prealloc_buf + write_ptr, data, block_size);
721 num_bytes -= block_size;
726 EXPORT_SYMBOL(comedi_buf_memcpy_to);
728 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
729 void *dest, unsigned int nbytes)
732 unsigned int read_ptr = async->buf_read_ptr + offset;
734 if (read_ptr >= async->prealloc_bufsz)
735 read_ptr %= async->prealloc_bufsz;
738 unsigned int block_size;
740 src = async->prealloc_buf + read_ptr;
742 if (nbytes >= async->prealloc_bufsz - read_ptr)
743 block_size = async->prealloc_bufsz - read_ptr;
747 memcpy(dest, src, block_size);
748 nbytes -= block_size;
753 EXPORT_SYMBOL(comedi_buf_memcpy_from);
755 unsigned int comedi_buf_read_n_available(struct comedi_async *async)
761 num_bytes = async->munge_count - async->buf_read_count;
762 /* barrier insures the read of munge_count in this
763 query occurs before any following reads of the buffer which
764 might be based on the return value from this query.
769 EXPORT_SYMBOL(comedi_buf_read_n_available);
771 int comedi_buf_get(struct comedi_async *async, short *x)
773 unsigned int n = comedi_buf_read_n_available(async);
775 if (n < sizeof(short))
777 comedi_buf_read_alloc(async, sizeof(short));
778 *x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
779 comedi_buf_read_free(async, sizeof(short));
782 EXPORT_SYMBOL(comedi_buf_get);
784 int comedi_buf_put(struct comedi_async *async, short x)
786 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
788 if (n < sizeof(short)) {
789 async->events |= COMEDI_CB_ERROR;
792 *(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
793 comedi_buf_write_free(async, sizeof(short));
796 EXPORT_SYMBOL(comedi_buf_put);
798 void comedi_reset_async_buf(struct comedi_async *async)
800 async->buf_write_alloc_count = 0;
801 async->buf_write_count = 0;
802 async->buf_read_alloc_count = 0;
803 async->buf_read_count = 0;
805 async->buf_write_ptr = 0;
806 async->buf_read_ptr = 0;
809 async->scan_progress = 0;
810 async->munge_chan = 0;
811 async->munge_count = 0;
812 async->munge_ptr = 0;
817 static int comedi_auto_config(struct device *hardware_device,
818 const char *board_name, const int *options,
819 unsigned num_options)
821 struct comedi_devconfig it;
823 struct comedi_device_file_info *dev_file_info;
825 unsigned *private_data = NULL;
827 if (!comedi_autoconfig) {
828 dev_set_drvdata(hardware_device, NULL);
832 minor = comedi_alloc_board_minor(hardware_device);
836 private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
837 if (private_data == NULL) {
841 *private_data = minor;
842 dev_set_drvdata(hardware_device, private_data);
844 dev_file_info = comedi_get_device_file_info(minor);
846 memset(&it, 0, sizeof(it));
847 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
848 it.board_name[COMEDI_NAMELEN - 1] = '\0';
849 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
850 memcpy(it.options, options, num_options * sizeof(int));
852 mutex_lock(&dev_file_info->device->mutex);
853 retval = comedi_device_attach(dev_file_info->device, &it);
854 mutex_unlock(&dev_file_info->device->mutex);
859 comedi_free_board_minor(minor);
864 static void comedi_auto_unconfig(struct device *hardware_device)
866 unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
870 BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
872 comedi_free_board_minor(*minor);
873 dev_set_drvdata(hardware_device, NULL);
877 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
882 options[0] = pcidev->bus->number;
884 options[1] = PCI_SLOT(pcidev->devfn);
886 return comedi_auto_config(&pcidev->dev, board_name,
887 options, ARRAY_SIZE(options));
889 EXPORT_SYMBOL_GPL(comedi_pci_auto_config);
891 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
893 comedi_auto_unconfig(&pcidev->dev);
895 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig);
897 int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name)
899 BUG_ON(usbdev == NULL);
900 return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
902 EXPORT_SYMBOL_GPL(comedi_usb_auto_config);
904 void comedi_usb_auto_unconfig(struct usb_device *usbdev)
906 BUG_ON(usbdev == NULL);
907 comedi_auto_unconfig(&usbdev->dev);
909 EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig);