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 <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/usb.h>
31 #include <linux/errno.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/comedidev.h>
40 #include <linux/wrapper.h>
41 #include <linux/highmem.h> /* for SuSE brokenness */
42 #include <linux/vmalloc.h>
43 #include <linux/cdev.h>
44 #include <linux/dma-mapping.h>
47 #include <asm/system.h>
49 #include "comedi_fops.h"
51 static int postconfig(comedi_device * dev);
52 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
53 comedi_insn * insn, lsampl_t * data);
54 static void *comedi_recognize(comedi_driver * driv, const char *name);
55 static void comedi_report_boards(comedi_driver * driv);
56 static int poll_invalid(comedi_device * dev, comedi_subdevice * s);
57 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
58 unsigned long new_size);
60 comedi_driver *comedi_drivers;
62 int comedi_modprobe(int minor)
67 static void cleanup_device(comedi_device * dev)
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;
90 dev->board_name = NULL;
91 dev->board_ptr = NULL;
94 dev->read_subdev = NULL;
95 dev->write_subdev = NULL;
98 comedi_set_hw_dev(dev, NULL);
101 static void __comedi_device_detach(comedi_device * dev)
105 dev->driver->detach(dev);
107 printk("BUG: dev->driver=NULL in comedi_device_detach()\n");
112 void comedi_device_detach(comedi_device * dev)
116 __comedi_device_detach(dev);
119 int comedi_device_attach(comedi_device * dev, comedi_devconfig * it)
127 for (driv = comedi_drivers; driv; driv = driv->next) {
128 if (!try_module_get(driv->module)) {
129 printk("comedi: failed to increment module count, skipping\n");
132 if (driv->num_names) {
133 dev->board_ptr = comedi_recognize(driv, it->board_name);
134 if (dev->board_ptr == NULL) {
135 module_put(driv->module);
139 if (strcmp(driv->driver_name, it->board_name)) {
140 module_put(driv->module);
144 //initialize dev->driver here so comedi_error() can be called from attach
146 ret = driv->attach(dev, it);
148 module_put(dev->driver->module);
149 __comedi_device_detach(dev);
155 // recognize has failed if we get here
156 // report valid board names before returning error
157 for (driv = comedi_drivers; driv; driv = driv->next) {
158 if (!try_module_get(driv->module)) {
159 printk("comedi: failed to increment module count\n");
162 comedi_report_boards(driv);
163 module_put(driv->module);
168 /* do a little post-config cleanup */
169 ret = postconfig(dev);
170 module_put(dev->driver->module);
172 __comedi_device_detach(dev);
176 if (!dev->board_name) {
177 printk("BUG: dev->board_name=<%p>\n", dev->board_name);
178 dev->board_name = "BUG";
186 int comedi_driver_register(comedi_driver * driver)
188 driver->next = comedi_drivers;
189 comedi_drivers = driver;
194 int comedi_driver_unregister(comedi_driver * driver)
199 /* check for devices using this driver */
200 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
201 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(i);
204 if(dev_file_info == NULL) continue;
205 dev = dev_file_info->device;
207 mutex_lock(&dev->mutex);
208 if (dev->attached && dev->driver == driver) {
210 printk("BUG! detaching device with use_count=%d\n", dev->use_count);
211 comedi_device_detach(dev);
213 mutex_unlock(&dev->mutex);
216 if (comedi_drivers == driver) {
217 comedi_drivers = driver->next;
221 for (prev = comedi_drivers; prev->next; prev = prev->next) {
222 if (prev->next == driver) {
223 prev->next = driver->next;
230 static int postconfig(comedi_device * dev)
234 comedi_async *async = NULL;
237 for (i = 0; i < dev->n_subdevices; i++) {
238 s = dev->subdevices + i;
240 if (s->type == COMEDI_SUBD_UNUSED)
243 if (s->len_chanlist == 0)
247 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
248 SDF_CMD_WRITE)) == 0);
249 BUG_ON(!s->do_cmdtest);
251 async = kzalloc(sizeof(comedi_async), GFP_KERNEL);
253 printk("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("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 that register their supported board names
304 void *comedi_recognize(comedi_driver * driv, const char *name)
307 const char *const *name_ptr = driv->board_name;
308 for (i = 0; i < driv->num_names; i++) {
309 if (strcmp(*name_ptr, name) == 0)
310 return (void *)name_ptr;
312 (const char *const *)((const char *)name_ptr +
319 void comedi_report_boards(comedi_driver * driv)
322 const char *const *name_ptr;
324 printk("comedi: valid board names for %s driver are:\n",
327 name_ptr = driv->board_name;
328 for (i = 0; i < driv->num_names; i++) {
329 printk(" %s\n", *name_ptr);
330 name_ptr = (const char **)((char *)name_ptr + driv->offset);
333 if (driv->num_names == 0)
334 printk(" %s\n", driv->driver_name);
337 static int poll_invalid(comedi_device * dev, comedi_subdevice * s)
342 int insn_inval(comedi_device * dev, comedi_subdevice * s,
343 comedi_insn * insn, lsampl_t * data)
348 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
349 comedi_insn * insn, lsampl_t * data)
351 comedi_insn new_insn;
353 static const unsigned channels_per_bitfield = 32;
354 unsigned chan = CR_CHAN(insn->chanspec);
355 const unsigned base_bitfield_channel =
356 (chan < channels_per_bitfield) ? 0 : chan;
357 lsampl_t new_data[2];
359 if ((insn->insn == INSN_WRITE) && !(s->subdev_flags & SDF_WRITABLE))
365 memset(new_data, 0, sizeof(new_data));
366 memset(&new_insn, 0, sizeof(new_insn));
367 new_insn.insn = INSN_BITS;
368 new_insn.chanspec = base_bitfield_channel;
370 new_insn.data = new_data;
371 new_insn.subdev = insn->subdev;
373 if (insn->insn == INSN_WRITE) {
374 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
375 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0; /* bits */
378 ret = s->insn_bits(dev, s, &new_insn, new_data);
382 if (insn->insn == INSN_READ) {
383 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
389 static inline unsigned long uvirt_to_kva(pgd_t * pgd, unsigned long adr)
391 unsigned long ret = 0UL;
396 if (!pgd_none(*pgd)) {
397 pud = pud_offset(pgd, adr);
398 pmd = pmd_offset(pud, adr);
399 if (!pmd_none(*pmd)) {
400 ptep = pte_offset_kernel(pmd, adr);
402 if (pte_present(pte)) {
403 ret = (unsigned long)
404 page_address(pte_page(pte));
405 ret |= (adr & (PAGE_SIZE - 1));
412 static inline unsigned long kvirt_to_kva(unsigned long adr)
414 unsigned long va, kva;
417 kva = uvirt_to_kva(pgd_offset_k(va), va);
422 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
423 unsigned long new_size)
425 comedi_async *async = s->async;
427 /* Round up new_size to multiple of PAGE_SIZE */
428 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
430 /* if no change is required, do nothing */
431 if (async->prealloc_buf && async->prealloc_bufsz == new_size) {
434 // deallocate old buffer
435 if (async->prealloc_buf) {
436 vunmap(async->prealloc_buf);
437 async->prealloc_buf = NULL;
438 async->prealloc_bufsz = 0;
440 if (async->buf_page_list) {
442 for (i = 0; i < async->n_buf_pages; ++i) {
443 if (async->buf_page_list[i].virt_addr) {
444 mem_map_unreserve(virt_to_page(async->
445 buf_page_list[i].virt_addr));
446 if (s->async_dma_dir != DMA_NONE) {
447 dma_free_coherent(dev->hw_dev,
449 async->buf_page_list[i].
451 async->buf_page_list[i].
454 free_page((unsigned long)async->
455 buf_page_list[i].virt_addr);
459 vfree(async->buf_page_list);
460 async->buf_page_list = NULL;
461 async->n_buf_pages = 0;
463 // allocate new buffer
466 unsigned n_pages = new_size >> PAGE_SHIFT;
467 struct page **pages = NULL;
469 async->buf_page_list =
470 vmalloc(sizeof(struct comedi_buf_page) * n_pages);
471 if (async->buf_page_list) {
472 memset(async->buf_page_list, 0,
473 sizeof(struct comedi_buf_page) * n_pages);
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,
482 &async->buf_page_list[i].
484 GFP_KERNEL | __GFP_COMP);
486 async->buf_page_list[i].virt_addr =
488 get_zeroed_page(GFP_KERNEL);
490 if (async->buf_page_list[i].virt_addr == NULL) {
493 mem_map_reserve(virt_to_page(async->
494 buf_page_list[i].virt_addr));
496 virt_to_page(async->buf_page_list[i].
501 async->prealloc_buf =
502 vmap(pages, n_pages, VM_MAP,
503 PAGE_KERNEL_NOCACHE);
508 if (async->prealloc_buf == NULL) {
509 /* Some allocation failed above. */
510 if (async->buf_page_list) {
511 for (i = 0; i < n_pages; i++) {
512 if (async->buf_page_list[i].virt_addr ==
516 mem_map_unreserve(virt_to_page(async->
519 if (s->async_dma_dir != DMA_NONE) {
520 dma_free_coherent(dev->hw_dev,
522 async->buf_page_list[i].
524 async->buf_page_list[i].
527 free_page((unsigned long)async->
532 vfree(async->buf_page_list);
533 async->buf_page_list = NULL;
537 async->n_buf_pages = n_pages;
539 async->prealloc_bufsz = new_size;
544 /* munging is applied to data by core as it passes between user
545 * and kernel space */
546 unsigned int comedi_buf_munge(comedi_async * async, unsigned int num_bytes)
548 comedi_subdevice *s = async->subdevice;
549 unsigned int count = 0;
550 const unsigned num_sample_bytes = bytes_per_sample(s);
552 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
553 async->munge_count += num_bytes;
554 if ((int)(async->munge_count - async->buf_write_count) > 0)
558 /* don't munge partial samples */
559 num_bytes -= num_bytes % num_sample_bytes;
560 while (count < num_bytes) {
563 block_size = num_bytes - count;
564 if (block_size < 0) {
565 rt_printk("%s: %s: bug! block_size is negative\n",
566 __FILE__, __FUNCTION__);
569 if ((int)(async->munge_ptr + block_size -
570 async->prealloc_bufsz) > 0)
571 block_size = async->prealloc_bufsz - async->munge_ptr;
573 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
574 block_size, async->munge_chan);
576 smp_wmb(); //barrier insures data is munged in buffer before munge_count is incremented
578 async->munge_chan += block_size / num_sample_bytes;
579 async->munge_chan %= async->cmd.chanlist_len;
580 async->munge_count += block_size;
581 async->munge_ptr += block_size;
582 async->munge_ptr %= async->prealloc_bufsz;
585 if ((int)(async->munge_count - async->buf_write_count) > 0)
590 unsigned int comedi_buf_write_n_available(comedi_async * async)
592 unsigned int free_end;
598 free_end = async->buf_read_count + async->prealloc_bufsz;
599 nbytes = free_end - async->buf_write_alloc_count;
600 nbytes -= nbytes % bytes_per_sample(async->subdevice);
601 /* barrier insures the read of buf_read_count in this
602 query occurs before any following writes to the buffer which
603 might be based on the return value from this query.
609 /* allocates chunk for the writer from free buffer space */
610 unsigned int comedi_buf_write_alloc(comedi_async * async, unsigned int nbytes)
612 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
614 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
615 nbytes = free_end - async->buf_write_alloc_count;
617 async->buf_write_alloc_count += nbytes;
618 /* barrier insures the read of buf_read_count above occurs before
619 we write data to the write-alloc'ed buffer space */
624 /* allocates nothing unless it can completely fulfill the request */
625 unsigned int comedi_buf_write_alloc_strict(comedi_async * async,
628 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
630 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
633 async->buf_write_alloc_count += nbytes;
634 /* barrier insures the read of buf_read_count above occurs before
635 we write data to the write-alloc'ed buffer space */
640 /* transfers a chunk from writer to filled buffer space */
641 unsigned comedi_buf_write_free(comedi_async * async, unsigned int nbytes)
643 if ((int)(async->buf_write_count + nbytes -
644 async->buf_write_alloc_count) > 0) {
646 ("comedi: attempted to write-free more bytes than have been write-allocated.\n");
647 nbytes = async->buf_write_alloc_count - async->buf_write_count;
649 async->buf_write_count += nbytes;
650 async->buf_write_ptr += nbytes;
651 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
652 if (async->buf_write_ptr >= async->prealloc_bufsz) {
653 async->buf_write_ptr %= async->prealloc_bufsz;
658 /* allocates a chunk for the reader from filled (and munged) buffer space */
659 unsigned comedi_buf_read_alloc(comedi_async * async, unsigned nbytes)
661 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
663 nbytes = async->munge_count - async->buf_read_alloc_count;
665 async->buf_read_alloc_count += nbytes;
666 /* barrier insures read of munge_count occurs before we actually read
667 data out of buffer */
672 /* transfers control of a chunk from reader to free buffer space */
673 unsigned comedi_buf_read_free(comedi_async * async, unsigned int nbytes)
675 // barrier insures data has been read out of buffer before read count is incremented
677 if ((int)(async->buf_read_count + nbytes -
678 async->buf_read_alloc_count) > 0) {
680 ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
681 nbytes = async->buf_read_alloc_count - async->buf_read_count;
683 async->buf_read_count += nbytes;
684 async->buf_read_ptr += nbytes;
685 async->buf_read_ptr %= async->prealloc_bufsz;
689 void comedi_buf_memcpy_to(comedi_async * async, unsigned int offset,
690 const void *data, unsigned int num_bytes)
692 unsigned int write_ptr = async->buf_write_ptr + offset;
694 if (write_ptr >= async->prealloc_bufsz)
695 write_ptr %= async->prealloc_bufsz;
698 unsigned int block_size;
700 if (write_ptr + num_bytes > async->prealloc_bufsz)
701 block_size = async->prealloc_bufsz - write_ptr;
703 block_size = num_bytes;
705 memcpy(async->prealloc_buf + write_ptr, data, block_size);
708 num_bytes -= block_size;
714 void comedi_buf_memcpy_from(comedi_async * async, unsigned int offset,
715 void *dest, unsigned int nbytes)
718 unsigned int read_ptr = async->buf_read_ptr + offset;
720 if (read_ptr >= async->prealloc_bufsz)
721 read_ptr %= async->prealloc_bufsz;
724 unsigned int block_size;
726 src = async->prealloc_buf + read_ptr;
728 if (nbytes >= async->prealloc_bufsz - read_ptr)
729 block_size = async->prealloc_bufsz - read_ptr;
733 memcpy(dest, src, block_size);
734 nbytes -= block_size;
740 unsigned int comedi_buf_read_n_available(comedi_async * async)
746 num_bytes = async->munge_count - async->buf_read_count;
747 /* barrier insures the read of munge_count in this
748 query occurs before any following reads of the buffer which
749 might be based on the return value from this query.
755 int comedi_buf_get(comedi_async * async, sampl_t * x)
757 unsigned int n = comedi_buf_read_n_available(async);
759 if (n < sizeof(sampl_t))
761 comedi_buf_read_alloc(async, sizeof(sampl_t));
762 *x = *(sampl_t *) (async->prealloc_buf + async->buf_read_ptr);
763 comedi_buf_read_free(async, sizeof(sampl_t));
767 int comedi_buf_put(comedi_async * async, sampl_t x)
769 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(sampl_t));
771 if (n < sizeof(sampl_t)) {
772 async->events |= COMEDI_CB_ERROR;
775 *(sampl_t *) (async->prealloc_buf + async->buf_write_ptr) = x;
776 comedi_buf_write_free(async, sizeof(sampl_t));
780 void comedi_reset_async_buf(comedi_async * async)
782 async->buf_write_alloc_count = 0;
783 async->buf_write_count = 0;
784 async->buf_read_alloc_count = 0;
785 async->buf_read_count = 0;
787 async->buf_write_ptr = 0;
788 async->buf_read_ptr = 0;
791 async->scan_progress = 0;
792 async->munge_chan = 0;
793 async->munge_count = 0;
794 async->munge_ptr = 0;
799 int comedi_auto_config(struct device *hardware_device, const char *board_name, const int *options, unsigned num_options)
803 struct comedi_device_file_info *dev_file_info;
805 unsigned *private_data = NULL;
807 if(!comedi_autoconfig) {
808 dev_set_drvdata(hardware_device, NULL);
812 minor = comedi_alloc_board_minor(hardware_device);
813 if(minor < 0) return minor;
815 private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
816 if(private_data == NULL) {
820 *private_data = minor;
821 dev_set_drvdata(hardware_device, private_data);
823 dev_file_info = comedi_get_device_file_info(minor);
825 memset(&it, 0, sizeof(it));
826 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
827 it.board_name[COMEDI_NAMELEN - 1] = '\0';
828 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
829 memcpy(it.options, options, num_options * sizeof(int));
831 mutex_lock(&dev_file_info->device->mutex);
832 retval = comedi_device_attach(dev_file_info->device, &it);
833 mutex_unlock(&dev_file_info->device->mutex);
839 comedi_free_board_minor(minor);
844 void comedi_auto_unconfig(struct device *hardware_device)
846 unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
847 if(minor == NULL) return;
849 BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
851 comedi_free_board_minor(*minor);
852 dev_set_drvdata(hardware_device, NULL);
856 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
861 options[0] = pcidev->bus->number;
863 options[1] = PCI_SLOT(pcidev->devfn);
865 return comedi_auto_config(&pcidev->dev, board_name, options, sizeof(options) / sizeof(options[0]));
868 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
870 comedi_auto_unconfig(&pcidev->dev);
873 int comedi_usb_auto_config(struct usb_device *usbdev,
874 const char *board_name)
876 BUG_ON(usbdev == NULL);
877 return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
880 void comedi_usb_auto_unconfig(struct usb_device *usbdev)
882 BUG_ON(usbdev == NULL);
883 comedi_auto_unconfig(&usbdev->dev);