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(comedi_device * dev);
51 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
52 comedi_insn * insn, lsampl_t * data);
53 static void *comedi_recognize(comedi_driver * driv, const char *name);
54 static void comedi_report_boards(comedi_driver * driv);
55 static int poll_invalid(comedi_device * dev, comedi_subdevice * s);
56 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
57 unsigned long new_size);
59 comedi_driver *comedi_drivers;
61 int comedi_modprobe(int minor)
66 static void cleanup_device(comedi_device * dev)
71 if (dev->subdevices) {
72 for (i = 0; i < dev->n_subdevices; i++) {
73 s = dev->subdevices + i;
74 comedi_free_subdevice_minor(s);
76 comedi_buf_alloc(dev, s, 0);
80 kfree(dev->subdevices);
81 dev->subdevices = NULL;
82 dev->n_subdevices = 0;
89 dev->board_name = NULL;
90 dev->board_ptr = NULL;
93 dev->read_subdev = NULL;
94 dev->write_subdev = NULL;
97 comedi_set_hw_dev(dev, NULL);
100 static void __comedi_device_detach(comedi_device * dev)
104 dev->driver->detach(dev);
106 printk("BUG: dev->driver=NULL in comedi_device_detach()\n");
111 void comedi_device_detach(comedi_device * dev)
115 __comedi_device_detach(dev);
118 int comedi_device_attach(comedi_device * dev, comedi_devconfig * it)
126 for (driv = comedi_drivers; driv; driv = driv->next) {
127 if (!try_module_get(driv->module)) {
128 printk("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(comedi_driver * driver)
187 driver->next = comedi_drivers;
188 comedi_drivers = driver;
193 int comedi_driver_unregister(comedi_driver * driver)
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 = comedi_get_device_file_info(i);
203 if(dev_file_info == NULL) continue;
204 dev = dev_file_info->device;
206 mutex_lock(&dev->mutex);
207 if (dev->attached && dev->driver == driver) {
209 printk("BUG! detaching device with use_count=%d\n", dev->use_count);
210 comedi_device_detach(dev);
212 mutex_unlock(&dev->mutex);
215 if (comedi_drivers == driver) {
216 comedi_drivers = driver->next;
220 for (prev = comedi_drivers; prev->next; prev = prev->next) {
221 if (prev->next == driver) {
222 prev->next = driver->next;
229 static int postconfig(comedi_device * dev)
233 comedi_async *async = NULL;
236 for (i = 0; i < dev->n_subdevices; i++) {
237 s = dev->subdevices + i;
239 if (s->type == COMEDI_SUBD_UNUSED)
242 if (s->len_chanlist == 0)
246 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
247 SDF_CMD_WRITE)) == 0);
248 BUG_ON(!s->do_cmdtest);
250 async = kzalloc(sizeof(comedi_async), GFP_KERNEL);
252 printk("failed to allocate async struct\n");
255 init_waitqueue_head(&async->wait_head);
256 async->subdevice = s;
259 #define DEFAULT_BUF_MAXSIZE (64*1024)
260 #define DEFAULT_BUF_SIZE (64*1024)
262 async->max_bufsize = DEFAULT_BUF_MAXSIZE;
264 async->prealloc_buf = NULL;
265 async->prealloc_bufsz = 0;
266 if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
267 printk("Buffer allocation failed\n");
271 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
275 comedi_alloc_subdevice_minor(dev, s);
278 if (!s->range_table && !s->range_table_list)
279 s->range_table = &range_unknown;
281 if (!s->insn_read && s->insn_bits)
282 s->insn_read = insn_rw_emulate_bits;
283 if (!s->insn_write && s->insn_bits)
284 s->insn_write = insn_rw_emulate_bits;
287 s->insn_read = insn_inval;
289 s->insn_write = insn_inval;
291 s->insn_bits = insn_inval;
293 s->insn_config = insn_inval;
296 s->poll = poll_invalid;
302 // generic recognize function for drivers that register their supported board names
303 void *comedi_recognize(comedi_driver * driv, const char *name)
306 const char *const *name_ptr = driv->board_name;
307 for (i = 0; i < driv->num_names; i++) {
308 if (strcmp(*name_ptr, name) == 0)
309 return (void *)name_ptr;
311 (const char *const *)((const char *)name_ptr +
318 void comedi_report_boards(comedi_driver * driv)
321 const char *const *name_ptr;
323 printk("comedi: valid board names for %s driver are:\n",
326 name_ptr = driv->board_name;
327 for (i = 0; i < driv->num_names; i++) {
328 printk(" %s\n", *name_ptr);
329 name_ptr = (const char **)((char *)name_ptr + driv->offset);
332 if (driv->num_names == 0)
333 printk(" %s\n", driv->driver_name);
336 static int poll_invalid(comedi_device * dev, comedi_subdevice * s)
341 int insn_inval(comedi_device * dev, comedi_subdevice * s,
342 comedi_insn * insn, lsampl_t * data)
347 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
348 comedi_insn * insn, lsampl_t * data)
350 comedi_insn new_insn;
352 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];
358 memset(new_data, 0, sizeof(new_data));
359 memset(&new_insn, 0, sizeof(new_insn));
360 new_insn.insn = INSN_BITS;
361 new_insn.chanspec = base_bitfield_channel;
363 new_insn.data = new_data;
364 new_insn.subdev = insn->subdev;
366 if (insn->insn == INSN_WRITE) {
367 if (!(s->subdev_flags & SDF_WRITABLE))
369 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
370 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0; /* bits */
373 ret = s->insn_bits(dev, s, &new_insn, new_data);
377 if (insn->insn == INSN_READ) {
378 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
384 static inline unsigned long uvirt_to_kva(pgd_t * pgd, unsigned long adr)
386 unsigned long ret = 0UL;
391 if (!pgd_none(*pgd)) {
392 pud = pud_offset(pgd, adr);
393 pmd = pmd_offset(pud, adr);
394 if (!pmd_none(*pmd)) {
395 ptep = pte_offset_kernel(pmd, adr);
397 if (pte_present(pte)) {
398 ret = (unsigned long)
399 page_address(pte_page(pte));
400 ret |= (adr & (PAGE_SIZE - 1));
407 static inline unsigned long kvirt_to_kva(unsigned long adr)
409 unsigned long va, kva;
412 kva = uvirt_to_kva(pgd_offset_k(va), va);
417 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
418 unsigned long new_size)
420 comedi_async *async = s->async;
422 /* Round up new_size to multiple of PAGE_SIZE */
423 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
425 /* if no change is required, do nothing */
426 if (async->prealloc_buf && async->prealloc_bufsz == new_size) {
429 // deallocate old buffer
430 if (async->prealloc_buf) {
431 vunmap(async->prealloc_buf);
432 async->prealloc_buf = NULL;
433 async->prealloc_bufsz = 0;
435 if (async->buf_page_list) {
437 for (i = 0; i < async->n_buf_pages; ++i) {
438 if (async->buf_page_list[i].virt_addr) {
439 mem_map_unreserve(virt_to_page(async->
440 buf_page_list[i].virt_addr));
441 if (s->async_dma_dir != DMA_NONE) {
442 dma_free_coherent(dev->hw_dev,
444 async->buf_page_list[i].
446 async->buf_page_list[i].
449 free_page((unsigned long)async->
450 buf_page_list[i].virt_addr);
454 vfree(async->buf_page_list);
455 async->buf_page_list = NULL;
456 async->n_buf_pages = 0;
458 // allocate new buffer
461 unsigned n_pages = new_size >> PAGE_SHIFT;
462 struct page **pages = NULL;
464 async->buf_page_list =
465 vmalloc(sizeof(struct comedi_buf_page) * n_pages);
466 if (async->buf_page_list) {
467 memset(async->buf_page_list, 0,
468 sizeof(struct comedi_buf_page) * n_pages);
469 pages = vmalloc(sizeof(struct page *) * n_pages);
472 for (i = 0; i < n_pages; i++) {
473 if (s->async_dma_dir != DMA_NONE) {
474 async->buf_page_list[i].virt_addr =
475 dma_alloc_coherent(dev->hw_dev,
477 &async->buf_page_list[i].
479 GFP_KERNEL | __GFP_COMP);
481 async->buf_page_list[i].virt_addr =
483 get_zeroed_page(GFP_KERNEL);
485 if (async->buf_page_list[i].virt_addr == NULL) {
488 mem_map_reserve(virt_to_page(async->
489 buf_page_list[i].virt_addr));
491 virt_to_page(async->buf_page_list[i].
496 async->prealloc_buf =
497 vmap(pages, n_pages, VM_MAP,
498 PAGE_KERNEL_NOCACHE);
503 if (async->prealloc_buf == NULL) {
504 /* Some allocation failed above. */
505 if (async->buf_page_list) {
506 for (i = 0; i < n_pages; i++) {
507 if (async->buf_page_list[i].virt_addr ==
511 mem_map_unreserve(virt_to_page(async->
514 if (s->async_dma_dir != DMA_NONE) {
515 dma_free_coherent(dev->hw_dev,
517 async->buf_page_list[i].
519 async->buf_page_list[i].
522 free_page((unsigned long)async->
527 vfree(async->buf_page_list);
528 async->buf_page_list = NULL;
532 async->n_buf_pages = n_pages;
534 async->prealloc_bufsz = new_size;
539 /* munging is applied to data by core as it passes between user
540 * and kernel space */
541 unsigned int comedi_buf_munge(comedi_async * async, unsigned int num_bytes)
543 comedi_subdevice *s = async->subdevice;
544 unsigned int count = 0;
545 const unsigned num_sample_bytes = bytes_per_sample(s);
547 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
548 async->munge_count += num_bytes;
549 if ((int)(async->munge_count - async->buf_write_count) > 0)
553 /* don't munge partial samples */
554 num_bytes -= num_bytes % num_sample_bytes;
555 while (count < num_bytes) {
558 block_size = num_bytes - count;
559 if (block_size < 0) {
560 rt_printk("%s: %s: bug! block_size is negative\n",
564 if ((int)(async->munge_ptr + block_size -
565 async->prealloc_bufsz) > 0)
566 block_size = async->prealloc_bufsz - async->munge_ptr;
568 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
569 block_size, async->munge_chan);
571 smp_wmb(); //barrier insures data is munged in buffer before munge_count is incremented
573 async->munge_chan += block_size / num_sample_bytes;
574 async->munge_chan %= async->cmd.chanlist_len;
575 async->munge_count += block_size;
576 async->munge_ptr += block_size;
577 async->munge_ptr %= async->prealloc_bufsz;
580 if ((int)(async->munge_count - async->buf_write_count) > 0)
585 unsigned int comedi_buf_write_n_available(comedi_async * async)
587 unsigned int free_end;
593 free_end = async->buf_read_count + async->prealloc_bufsz;
594 nbytes = free_end - async->buf_write_alloc_count;
595 nbytes -= nbytes % bytes_per_sample(async->subdevice);
596 /* barrier insures the read of buf_read_count in this
597 query occurs before any following writes to the buffer which
598 might be based on the return value from this query.
604 /* allocates chunk for the writer from free buffer space */
605 unsigned int comedi_buf_write_alloc(comedi_async * async, unsigned int nbytes)
607 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
609 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
610 nbytes = free_end - async->buf_write_alloc_count;
612 async->buf_write_alloc_count += nbytes;
613 /* barrier insures the read of buf_read_count above occurs before
614 we write data to the write-alloc'ed buffer space */
619 /* allocates nothing unless it can completely fulfill the request */
620 unsigned int comedi_buf_write_alloc_strict(comedi_async * async,
623 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
625 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
628 async->buf_write_alloc_count += nbytes;
629 /* barrier insures the read of buf_read_count above occurs before
630 we write data to the write-alloc'ed buffer space */
635 /* transfers a chunk from writer to filled buffer space */
636 unsigned comedi_buf_write_free(comedi_async * async, unsigned int nbytes)
638 if ((int)(async->buf_write_count + nbytes -
639 async->buf_write_alloc_count) > 0) {
641 ("comedi: attempted to write-free more bytes than have been write-allocated.\n");
642 nbytes = async->buf_write_alloc_count - async->buf_write_count;
644 async->buf_write_count += nbytes;
645 async->buf_write_ptr += nbytes;
646 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
647 if (async->buf_write_ptr >= async->prealloc_bufsz) {
648 async->buf_write_ptr %= async->prealloc_bufsz;
653 /* allocates a chunk for the reader from filled (and munged) buffer space */
654 unsigned comedi_buf_read_alloc(comedi_async * async, unsigned nbytes)
656 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
658 nbytes = async->munge_count - async->buf_read_alloc_count;
660 async->buf_read_alloc_count += nbytes;
661 /* barrier insures read of munge_count occurs before we actually read
662 data out of buffer */
667 /* transfers control of a chunk from reader to free buffer space */
668 unsigned comedi_buf_read_free(comedi_async * async, unsigned int nbytes)
670 // barrier insures data has been read out of buffer before read count is incremented
672 if ((int)(async->buf_read_count + nbytes -
673 async->buf_read_alloc_count) > 0) {
675 ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
676 nbytes = async->buf_read_alloc_count - async->buf_read_count;
678 async->buf_read_count += nbytes;
679 async->buf_read_ptr += nbytes;
680 async->buf_read_ptr %= async->prealloc_bufsz;
684 void comedi_buf_memcpy_to(comedi_async * async, unsigned int offset,
685 const void *data, unsigned int num_bytes)
687 unsigned int write_ptr = async->buf_write_ptr + offset;
689 if (write_ptr >= async->prealloc_bufsz)
690 write_ptr %= async->prealloc_bufsz;
693 unsigned int block_size;
695 if (write_ptr + num_bytes > async->prealloc_bufsz)
696 block_size = async->prealloc_bufsz - write_ptr;
698 block_size = num_bytes;
700 memcpy(async->prealloc_buf + write_ptr, data, block_size);
703 num_bytes -= block_size;
709 void comedi_buf_memcpy_from(comedi_async * async, unsigned int offset,
710 void *dest, unsigned int nbytes)
713 unsigned int read_ptr = async->buf_read_ptr + offset;
715 if (read_ptr >= async->prealloc_bufsz)
716 read_ptr %= async->prealloc_bufsz;
719 unsigned int block_size;
721 src = async->prealloc_buf + read_ptr;
723 if (nbytes >= async->prealloc_bufsz - read_ptr)
724 block_size = async->prealloc_bufsz - read_ptr;
728 memcpy(dest, src, block_size);
729 nbytes -= block_size;
735 unsigned int comedi_buf_read_n_available(comedi_async * async)
741 num_bytes = async->munge_count - async->buf_read_count;
742 /* barrier insures the read of munge_count in this
743 query occurs before any following reads of the buffer which
744 might be based on the return value from this query.
750 int comedi_buf_get(comedi_async * async, sampl_t * x)
752 unsigned int n = comedi_buf_read_n_available(async);
754 if (n < sizeof(sampl_t))
756 comedi_buf_read_alloc(async, sizeof(sampl_t));
757 *x = *(sampl_t *) (async->prealloc_buf + async->buf_read_ptr);
758 comedi_buf_read_free(async, sizeof(sampl_t));
762 int comedi_buf_put(comedi_async * async, sampl_t x)
764 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(sampl_t));
766 if (n < sizeof(sampl_t)) {
767 async->events |= COMEDI_CB_ERROR;
770 *(sampl_t *) (async->prealloc_buf + async->buf_write_ptr) = x;
771 comedi_buf_write_free(async, sizeof(sampl_t));
775 void comedi_reset_async_buf(comedi_async * async)
777 async->buf_write_alloc_count = 0;
778 async->buf_write_count = 0;
779 async->buf_read_alloc_count = 0;
780 async->buf_read_count = 0;
782 async->buf_write_ptr = 0;
783 async->buf_read_ptr = 0;
786 async->scan_progress = 0;
787 async->munge_chan = 0;
788 async->munge_count = 0;
789 async->munge_ptr = 0;
794 int comedi_auto_config(struct device *hardware_device, const char *board_name, const int *options, unsigned num_options)
798 struct comedi_device_file_info *dev_file_info;
800 unsigned *private_data = NULL;
802 if (!comedi_autoconfig) {
803 dev_set_drvdata(hardware_device, NULL);
807 minor = comedi_alloc_board_minor(hardware_device);
808 if(minor < 0) return minor;
810 private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
811 if (private_data == NULL) {
815 *private_data = minor;
816 dev_set_drvdata(hardware_device, private_data);
818 dev_file_info = comedi_get_device_file_info(minor);
820 memset(&it, 0, sizeof(it));
821 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
822 it.board_name[COMEDI_NAMELEN - 1] = '\0';
823 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
824 memcpy(it.options, options, num_options * sizeof(int));
826 mutex_lock(&dev_file_info->device->mutex);
827 retval = comedi_device_attach(dev_file_info->device, &it);
828 mutex_unlock(&dev_file_info->device->mutex);
834 comedi_free_board_minor(minor);
839 void comedi_auto_unconfig(struct device *hardware_device)
841 unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
842 if(minor == NULL) return;
844 BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
846 comedi_free_board_minor(*minor);
847 dev_set_drvdata(hardware_device, NULL);
851 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
856 options[0] = pcidev->bus->number;
858 options[1] = PCI_SLOT(pcidev->devfn);
860 return comedi_auto_config(&pcidev->dev, board_name, options, sizeof(options) / sizeof(options[0]));
863 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
865 comedi_auto_unconfig(&pcidev->dev);
868 int comedi_usb_auto_config(struct usb_device *usbdev,
869 const char *board_name)
871 BUG_ON(usbdev == NULL);
872 return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
875 void comedi_usb_auto_unconfig(struct usb_device *usbdev)
877 BUG_ON(usbdev == NULL);
878 comedi_auto_unconfig(&usbdev->dev);