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 "comedidev.h"
42 #include <linux/highmem.h> /* for SuSE brokenness */
43 #include <linux/vmalloc.h>
44 #include <linux/cdev.h>
45 #include <linux/dma-mapping.h>
48 #include <asm/system.h>
50 static int postconfig(struct comedi_device *dev);
51 static int insn_rw_emulate_bits(struct comedi_device *dev,
52 struct comedi_subdevice *s,
53 struct comedi_insn *insn, unsigned int *data);
54 static void *comedi_recognize(struct comedi_driver *driv, const char *name);
55 static void comedi_report_boards(struct comedi_driver *driv);
56 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s);
57 int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
58 unsigned long new_size);
60 struct comedi_driver *comedi_drivers;
62 int comedi_modprobe(int minor)
67 static void cleanup_device(struct comedi_device *dev)
70 struct comedi_subdevice *s;
72 if (dev->subdevices) {
73 for (i = 0; i < dev->n_subdevices; i++) {
74 s = dev->subdevices + i;
75 comedi_free_subdevice_minor(s);
77 comedi_buf_alloc(dev, s, 0);
81 kfree(dev->subdevices);
82 dev->subdevices = NULL;
83 dev->n_subdevices = 0;
88 dev->board_name = NULL;
89 dev->board_ptr = NULL;
92 dev->read_subdev = NULL;
93 dev->write_subdev = NULL;
96 comedi_set_hw_dev(dev, NULL);
99 static void __comedi_device_detach(struct comedi_device *dev)
103 dev->driver->detach(dev);
105 printk("BUG: dev->driver=NULL in comedi_device_detach()\n");
110 void comedi_device_detach(struct comedi_device *dev)
114 __comedi_device_detach(dev);
117 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
119 struct comedi_driver *driv;
125 for (driv = comedi_drivers; driv; driv = driv->next) {
126 if (!try_module_get(driv->module)) {
128 ("comedi: failed to increment module count, skipping\n");
131 if (driv->num_names) {
132 dev->board_ptr = comedi_recognize(driv, it->board_name);
133 if (dev->board_ptr == NULL) {
134 module_put(driv->module);
138 if (strcmp(driv->driver_name, it->board_name)) {
139 module_put(driv->module);
143 /* initialize dev->driver here so comedi_error() can be called from attach */
145 ret = driv->attach(dev, it);
147 module_put(dev->driver->module);
148 __comedi_device_detach(dev);
154 /* recognize has failed if we get here */
155 /* report valid board names before returning error */
156 for (driv = comedi_drivers; driv; driv = driv->next) {
157 if (!try_module_get(driv->module)) {
158 printk("comedi: failed to increment module count\n");
161 comedi_report_boards(driv);
162 module_put(driv->module);
167 /* do a little post-config cleanup */
168 ret = postconfig(dev);
169 module_put(dev->driver->module);
171 __comedi_device_detach(dev);
175 if (!dev->board_name) {
176 printk("BUG: dev->board_name=<%p>\n", dev->board_name);
177 dev->board_name = "BUG";
185 int comedi_driver_register(struct comedi_driver *driver)
187 driver->next = comedi_drivers;
188 comedi_drivers = driver;
193 int comedi_driver_unregister(struct comedi_driver *driver)
195 struct comedi_driver *prev;
198 /* check for devices using this driver */
199 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
200 struct comedi_device_file_info *dev_file_info =
201 comedi_get_device_file_info(i);
202 struct comedi_device *dev;
204 if (dev_file_info == NULL)
206 dev = dev_file_info->device;
208 mutex_lock(&dev->mutex);
209 if (dev->attached && dev->driver == driver) {
212 ("BUG! detaching device with use_count=%d\n",
214 comedi_device_detach(dev);
216 mutex_unlock(&dev->mutex);
219 if (comedi_drivers == driver) {
220 comedi_drivers = driver->next;
224 for (prev = comedi_drivers; prev->next; prev = prev->next) {
225 if (prev->next == driver) {
226 prev->next = driver->next;
233 static int postconfig(struct comedi_device *dev)
236 struct comedi_subdevice *s;
237 struct comedi_async *async = NULL;
240 for (i = 0; i < dev->n_subdevices; i++) {
241 s = dev->subdevices + i;
243 if (s->type == COMEDI_SUBD_UNUSED)
246 if (s->len_chanlist == 0)
250 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
251 SDF_CMD_WRITE)) == 0);
252 BUG_ON(!s->do_cmdtest);
255 kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
257 printk("failed to allocate async struct\n");
260 init_waitqueue_head(&async->wait_head);
261 async->subdevice = s;
264 #define DEFAULT_BUF_MAXSIZE (64*1024)
265 #define DEFAULT_BUF_SIZE (64*1024)
267 async->max_bufsize = DEFAULT_BUF_MAXSIZE;
269 async->prealloc_buf = NULL;
270 async->prealloc_bufsz = 0;
271 if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
272 printk("Buffer allocation failed\n");
276 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
280 comedi_alloc_subdevice_minor(dev, s);
283 if (!s->range_table && !s->range_table_list)
284 s->range_table = &range_unknown;
286 if (!s->insn_read && s->insn_bits)
287 s->insn_read = insn_rw_emulate_bits;
288 if (!s->insn_write && s->insn_bits)
289 s->insn_write = insn_rw_emulate_bits;
292 s->insn_read = insn_inval;
294 s->insn_write = insn_inval;
296 s->insn_bits = insn_inval;
298 s->insn_config = insn_inval;
301 s->poll = poll_invalid;
307 /* generic recognize function for drivers that register their supported board names */
308 void *comedi_recognize(struct comedi_driver *driv, const char *name)
311 const char *const *name_ptr = driv->board_name;
312 for (i = 0; i < driv->num_names; i++) {
313 if (strcmp(*name_ptr, name) == 0)
314 return (void *)name_ptr;
316 (const char *const *)((const char *)name_ptr +
323 void comedi_report_boards(struct comedi_driver *driv)
326 const char *const *name_ptr;
328 printk("comedi: valid board names for %s driver are:\n",
331 name_ptr = driv->board_name;
332 for (i = 0; i < driv->num_names; i++) {
333 printk(" %s\n", *name_ptr);
334 name_ptr = (const char **)((char *)name_ptr + driv->offset);
337 if (driv->num_names == 0)
338 printk(" %s\n", driv->driver_name);
341 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
346 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
347 struct comedi_insn *insn, unsigned int *data)
352 static int insn_rw_emulate_bits(struct comedi_device *dev,
353 struct comedi_subdevice *s,
354 struct comedi_insn *insn, unsigned int *data)
356 struct comedi_insn new_insn;
358 static const unsigned channels_per_bitfield = 32;
360 unsigned chan = CR_CHAN(insn->chanspec);
361 const unsigned base_bitfield_channel =
362 (chan < channels_per_bitfield) ? 0 : chan;
363 unsigned int new_data[2];
364 memset(new_data, 0, sizeof(new_data));
365 memset(&new_insn, 0, sizeof(new_insn));
366 new_insn.insn = INSN_BITS;
367 new_insn.chanspec = base_bitfield_channel;
369 new_insn.data = new_data;
370 new_insn.subdev = insn->subdev;
372 if (insn->insn == INSN_WRITE) {
373 if (!(s->subdev_flags & SDF_WRITABLE))
375 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
376 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0; /* bits */
379 ret = s->insn_bits(dev, s, &new_insn, new_data);
383 if (insn->insn == INSN_READ) {
384 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
390 static inline unsigned long uvirt_to_kva(pgd_t * pgd, unsigned long adr)
392 unsigned long ret = 0UL;
397 if (!pgd_none(*pgd)) {
398 pud = pud_offset(pgd, adr);
399 pmd = pmd_offset(pud, adr);
400 if (!pmd_none(*pmd)) {
401 ptep = pte_offset_kernel(pmd, adr);
403 if (pte_present(pte)) {
404 ret = (unsigned long)
405 page_address(pte_page(pte));
406 ret |= (adr & (PAGE_SIZE - 1));
413 static inline unsigned long kvirt_to_kva(unsigned long adr)
415 unsigned long va, kva;
418 kva = uvirt_to_kva(pgd_offset_k(va), va);
423 int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
424 unsigned long new_size)
426 struct comedi_async *async = s->async;
428 /* Round up new_size to multiple of PAGE_SIZE */
429 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
431 /* if no change is required, do nothing */
432 if (async->prealloc_buf && async->prealloc_bufsz == new_size) {
435 /* deallocate old buffer */
436 if (async->prealloc_buf) {
437 vunmap(async->prealloc_buf);
438 async->prealloc_buf = NULL;
439 async->prealloc_bufsz = 0;
441 if (async->buf_page_list) {
443 for (i = 0; i < async->n_buf_pages; ++i) {
444 if (async->buf_page_list[i].virt_addr) {
445 mem_map_unreserve(virt_to_page
446 (async->buf_page_list[i].
448 if (s->async_dma_dir != DMA_NONE) {
449 dma_free_coherent(dev->hw_dev,
458 free_page((unsigned long)
459 async->buf_page_list[i].
464 vfree(async->buf_page_list);
465 async->buf_page_list = NULL;
466 async->n_buf_pages = 0;
468 /* allocate new buffer */
471 unsigned n_pages = new_size >> PAGE_SHIFT;
472 struct page **pages = NULL;
474 async->buf_page_list =
475 vmalloc(sizeof(struct comedi_buf_page) * n_pages);
476 if (async->buf_page_list) {
477 memset(async->buf_page_list, 0,
478 sizeof(struct comedi_buf_page) * n_pages);
479 pages = vmalloc(sizeof(struct page *) * n_pages);
482 for (i = 0; i < n_pages; i++) {
483 if (s->async_dma_dir != DMA_NONE) {
484 async->buf_page_list[i].virt_addr =
485 dma_alloc_coherent(dev->hw_dev,
493 async->buf_page_list[i].virt_addr =
495 get_zeroed_page(GFP_KERNEL);
497 if (async->buf_page_list[i].virt_addr == NULL) {
500 mem_map_reserve(virt_to_page
501 (async->buf_page_list[i].
505 buf_page_list[i].virt_addr);
509 async->prealloc_buf =
510 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE);
515 if (async->prealloc_buf == NULL) {
516 /* Some allocation failed above. */
517 if (async->buf_page_list) {
518 for (i = 0; i < n_pages; i++) {
519 if (async->buf_page_list[i].virt_addr ==
523 mem_map_unreserve(virt_to_page
524 (async->buf_page_list
526 if (s->async_dma_dir != DMA_NONE) {
527 dma_free_coherent(dev->hw_dev,
536 free_page((unsigned long)
541 vfree(async->buf_page_list);
542 async->buf_page_list = NULL;
546 async->n_buf_pages = n_pages;
548 async->prealloc_bufsz = new_size;
553 /* munging is applied to data by core as it passes between user
554 * and kernel space */
555 unsigned int comedi_buf_munge(struct comedi_async *async,
556 unsigned int num_bytes)
558 struct comedi_subdevice *s = async->subdevice;
559 unsigned int count = 0;
560 const unsigned num_sample_bytes = bytes_per_sample(s);
562 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
563 async->munge_count += num_bytes;
564 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
567 /* don't munge partial samples */
568 num_bytes -= num_bytes % num_sample_bytes;
569 while (count < num_bytes) {
572 block_size = num_bytes - count;
573 if (block_size < 0) {
574 printk("%s: %s: bug! block_size is negative\n",
578 if ((int)(async->munge_ptr + block_size -
579 async->prealloc_bufsz) > 0)
580 block_size = async->prealloc_bufsz - async->munge_ptr;
582 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
583 block_size, async->munge_chan);
585 smp_wmb(); /* barrier insures data is munged in buffer 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 */
633 /* allocates nothing unless it can completely fulfill the request */
634 unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
637 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
639 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
642 async->buf_write_alloc_count += nbytes;
643 /* barrier insures the read of buf_read_count above occurs before
644 we write data to the write-alloc'ed buffer space */
649 /* transfers a chunk from writer to filled buffer space */
650 unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
652 if ((int)(async->buf_write_count + nbytes -
653 async->buf_write_alloc_count) > 0) {
655 ("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;
667 /* allocates a chunk for the reader from filled (and munged) buffer space */
668 unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
670 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
672 nbytes = async->munge_count - async->buf_read_alloc_count;
674 async->buf_read_alloc_count += nbytes;
675 /* barrier insures read of munge_count occurs before we actually read
676 data out of buffer */
681 /* transfers control of a chunk from reader to free buffer space */
682 unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes)
684 /* barrier insures data has been read out of buffer before read count is incremented */
686 if ((int)(async->buf_read_count + nbytes -
687 async->buf_read_alloc_count) > 0) {
689 ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
690 nbytes = async->buf_read_alloc_count - async->buf_read_count;
692 async->buf_read_count += nbytes;
693 async->buf_read_ptr += nbytes;
694 async->buf_read_ptr %= async->prealloc_bufsz;
698 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
699 const void *data, unsigned int num_bytes)
701 unsigned int write_ptr = async->buf_write_ptr + offset;
703 if (write_ptr >= async->prealloc_bufsz)
704 write_ptr %= async->prealloc_bufsz;
707 unsigned int block_size;
709 if (write_ptr + num_bytes > async->prealloc_bufsz)
710 block_size = async->prealloc_bufsz - write_ptr;
712 block_size = num_bytes;
714 memcpy(async->prealloc_buf + write_ptr, data, block_size);
717 num_bytes -= block_size;
723 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
724 void *dest, unsigned int nbytes)
727 unsigned int read_ptr = async->buf_read_ptr + offset;
729 if (read_ptr >= async->prealloc_bufsz)
730 read_ptr %= async->prealloc_bufsz;
733 unsigned int block_size;
735 src = async->prealloc_buf + read_ptr;
737 if (nbytes >= async->prealloc_bufsz - read_ptr)
738 block_size = async->prealloc_bufsz - read_ptr;
742 memcpy(dest, src, block_size);
743 nbytes -= block_size;
749 unsigned int comedi_buf_read_n_available(struct comedi_async *async)
755 num_bytes = async->munge_count - async->buf_read_count;
756 /* barrier insures the read of munge_count in this
757 query occurs before any following reads of the buffer which
758 might be based on the return value from this query.
764 int comedi_buf_get(struct comedi_async *async, short *x)
766 unsigned int n = comedi_buf_read_n_available(async);
768 if (n < sizeof(short))
770 comedi_buf_read_alloc(async, sizeof(short));
771 *x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
772 comedi_buf_read_free(async, sizeof(short));
776 int comedi_buf_put(struct comedi_async *async, short x)
778 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
780 if (n < sizeof(short)) {
781 async->events |= COMEDI_CB_ERROR;
784 *(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
785 comedi_buf_write_free(async, sizeof(short));
789 void comedi_reset_async_buf(struct comedi_async *async)
791 async->buf_write_alloc_count = 0;
792 async->buf_write_count = 0;
793 async->buf_read_alloc_count = 0;
794 async->buf_read_count = 0;
796 async->buf_write_ptr = 0;
797 async->buf_read_ptr = 0;
800 async->scan_progress = 0;
801 async->munge_chan = 0;
802 async->munge_count = 0;
803 async->munge_ptr = 0;
808 int comedi_auto_config(struct device *hardware_device, const char *board_name,
809 const int *options, unsigned num_options)
811 struct comedi_devconfig it;
813 struct comedi_device_file_info *dev_file_info;
815 unsigned *private_data = NULL;
817 if (!comedi_autoconfig) {
818 dev_set_drvdata(hardware_device, NULL);
822 minor = comedi_alloc_board_minor(hardware_device);
826 private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
827 if (private_data == NULL) {
831 *private_data = minor;
832 dev_set_drvdata(hardware_device, private_data);
834 dev_file_info = comedi_get_device_file_info(minor);
836 memset(&it, 0, sizeof(it));
837 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
838 it.board_name[COMEDI_NAMELEN - 1] = '\0';
839 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
840 memcpy(it.options, options, num_options * sizeof(int));
842 mutex_lock(&dev_file_info->device->mutex);
843 retval = comedi_device_attach(dev_file_info->device, &it);
844 mutex_unlock(&dev_file_info->device->mutex);
849 comedi_free_board_minor(minor);
854 void comedi_auto_unconfig(struct device *hardware_device)
856 unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
860 BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
862 comedi_free_board_minor(*minor);
863 dev_set_drvdata(hardware_device, NULL);
867 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
872 options[0] = pcidev->bus->number;
874 options[1] = PCI_SLOT(pcidev->devfn);
876 return comedi_auto_config(&pcidev->dev, board_name,
877 options, ARRAY_SIZE(options));
880 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
882 comedi_auto_unconfig(&pcidev->dev);
885 int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name)
887 BUG_ON(usbdev == NULL);
888 return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
891 void comedi_usb_auto_unconfig(struct usb_device *usbdev)
893 BUG_ON(usbdev == NULL);
894 comedi_auto_unconfig(&usbdev->dev);