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");
109 void comedi_device_detach(struct comedi_device *dev)
113 __comedi_device_detach(dev);
116 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
118 struct comedi_driver *driv;
124 for (driv = comedi_drivers; driv; driv = driv->next) {
125 if (!try_module_get(driv->module)) {
127 ("comedi: failed to increment module count, skipping\n");
130 if (driv->num_names) {
131 dev->board_ptr = comedi_recognize(driv, it->board_name);
132 if (dev->board_ptr == NULL) {
133 module_put(driv->module);
137 if (strcmp(driv->driver_name, it->board_name)) {
138 module_put(driv->module);
142 /* initialize dev->driver here so comedi_error() can be called from attach */
144 ret = driv->attach(dev, it);
146 module_put(dev->driver->module);
147 __comedi_device_detach(dev);
153 /* recognize has failed if we get here */
154 /* report valid board names before returning error */
155 for (driv = comedi_drivers; driv; driv = driv->next) {
156 if (!try_module_get(driv->module)) {
157 printk("comedi: failed to increment module count\n");
160 comedi_report_boards(driv);
161 module_put(driv->module);
166 /* do a little post-config cleanup */
167 ret = postconfig(dev);
168 module_put(dev->driver->module);
170 __comedi_device_detach(dev);
174 if (!dev->board_name) {
175 printk("BUG: dev->board_name=<%p>\n", dev->board_name);
176 dev->board_name = "BUG";
184 int comedi_driver_register(struct comedi_driver *driver)
186 driver->next = comedi_drivers;
187 comedi_drivers = driver;
192 int comedi_driver_unregister(struct comedi_driver *driver)
194 struct comedi_driver *prev;
197 /* check for devices using this driver */
198 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
199 struct comedi_device_file_info *dev_file_info =
200 comedi_get_device_file_info(i);
201 struct comedi_device *dev;
203 if (dev_file_info == NULL)
205 dev = dev_file_info->device;
207 mutex_lock(&dev->mutex);
208 if (dev->attached && dev->driver == driver) {
211 ("BUG! detaching device with use_count=%d\n",
213 comedi_device_detach(dev);
215 mutex_unlock(&dev->mutex);
218 if (comedi_drivers == driver) {
219 comedi_drivers = driver->next;
223 for (prev = comedi_drivers; prev->next; prev = prev->next) {
224 if (prev->next == driver) {
225 prev->next = driver->next;
232 static int postconfig(struct comedi_device *dev)
235 struct comedi_subdevice *s;
236 struct comedi_async *async = NULL;
239 for (i = 0; i < dev->n_subdevices; i++) {
240 s = dev->subdevices + i;
242 if (s->type == COMEDI_SUBD_UNUSED)
245 if (s->len_chanlist == 0)
249 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
250 SDF_CMD_WRITE)) == 0);
251 BUG_ON(!s->do_cmdtest);
254 kzalloc(sizeof(struct comedi_async), GFP_KERNEL);
256 printk("failed to allocate async struct\n");
259 init_waitqueue_head(&async->wait_head);
260 async->subdevice = s;
263 #define DEFAULT_BUF_MAXSIZE (64*1024)
264 #define DEFAULT_BUF_SIZE (64*1024)
266 async->max_bufsize = DEFAULT_BUF_MAXSIZE;
268 async->prealloc_buf = NULL;
269 async->prealloc_bufsz = 0;
270 if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
271 printk("Buffer allocation failed\n");
275 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
279 comedi_alloc_subdevice_minor(dev, s);
282 if (!s->range_table && !s->range_table_list)
283 s->range_table = &range_unknown;
285 if (!s->insn_read && s->insn_bits)
286 s->insn_read = insn_rw_emulate_bits;
287 if (!s->insn_write && s->insn_bits)
288 s->insn_write = insn_rw_emulate_bits;
291 s->insn_read = insn_inval;
293 s->insn_write = insn_inval;
295 s->insn_bits = insn_inval;
297 s->insn_config = insn_inval;
300 s->poll = poll_invalid;
306 /* generic recognize function for drivers that register their supported board names */
307 void *comedi_recognize(struct comedi_driver *driv, const char *name)
310 const char *const *name_ptr = driv->board_name;
311 for (i = 0; i < driv->num_names; i++) {
312 if (strcmp(*name_ptr, name) == 0)
313 return (void *)name_ptr;
315 (const char *const *)((const char *)name_ptr +
322 void comedi_report_boards(struct comedi_driver *driv)
325 const char *const *name_ptr;
327 printk("comedi: valid board names for %s driver are:\n",
330 name_ptr = driv->board_name;
331 for (i = 0; i < driv->num_names; i++) {
332 printk(" %s\n", *name_ptr);
333 name_ptr = (const char **)((char *)name_ptr + driv->offset);
336 if (driv->num_names == 0)
337 printk(" %s\n", driv->driver_name);
340 static int poll_invalid(struct comedi_device *dev, struct comedi_subdevice *s)
345 int insn_inval(struct comedi_device *dev, struct comedi_subdevice *s,
346 struct comedi_insn *insn, unsigned int *data)
351 static int insn_rw_emulate_bits(struct comedi_device *dev,
352 struct comedi_subdevice *s,
353 struct comedi_insn *insn, unsigned int *data)
355 struct comedi_insn new_insn;
357 static const unsigned channels_per_bitfield = 32;
359 unsigned chan = CR_CHAN(insn->chanspec);
360 const unsigned base_bitfield_channel =
361 (chan < channels_per_bitfield) ? 0 : chan;
362 unsigned int new_data[2];
363 memset(new_data, 0, sizeof(new_data));
364 memset(&new_insn, 0, sizeof(new_insn));
365 new_insn.insn = INSN_BITS;
366 new_insn.chanspec = base_bitfield_channel;
368 new_insn.data = new_data;
369 new_insn.subdev = insn->subdev;
371 if (insn->insn == INSN_WRITE) {
372 if (!(s->subdev_flags & SDF_WRITABLE))
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;
388 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
390 unsigned long ret = 0UL;
395 if (!pgd_none(*pgd)) {
396 pud = pud_offset(pgd, adr);
397 pmd = pmd_offset(pud, adr);
398 if (!pmd_none(*pmd)) {
399 ptep = pte_offset_kernel(pmd, adr);
401 if (pte_present(pte)) {
402 ret = (unsigned long)
403 page_address(pte_page(pte));
404 ret |= (adr & (PAGE_SIZE - 1));
411 static inline unsigned long kvirt_to_kva(unsigned long adr)
413 unsigned long va, kva;
416 kva = uvirt_to_kva(pgd_offset_k(va), va);
421 int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
422 unsigned long new_size)
424 struct comedi_async *async = s->async;
426 /* Round up new_size to multiple of PAGE_SIZE */
427 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
429 /* if no change is required, do nothing */
430 if (async->prealloc_buf && async->prealloc_bufsz == new_size)
433 /* deallocate old buffer */
434 if (async->prealloc_buf) {
435 vunmap(async->prealloc_buf);
436 async->prealloc_buf = NULL;
437 async->prealloc_bufsz = 0;
439 if (async->buf_page_list) {
441 for (i = 0; i < async->n_buf_pages; ++i) {
442 if (async->buf_page_list[i].virt_addr) {
443 mem_map_unreserve(virt_to_page
444 (async->buf_page_list[i].
446 if (s->async_dma_dir != DMA_NONE) {
447 dma_free_coherent(dev->hw_dev,
456 free_page((unsigned long)
457 async->buf_page_list[i].
462 vfree(async->buf_page_list);
463 async->buf_page_list = NULL;
464 async->n_buf_pages = 0;
466 /* allocate new buffer */
469 unsigned n_pages = new_size >> PAGE_SHIFT;
470 struct page **pages = NULL;
472 async->buf_page_list =
473 vmalloc(sizeof(struct comedi_buf_page) * n_pages);
474 if (async->buf_page_list) {
475 memset(async->buf_page_list, 0,
476 sizeof(struct comedi_buf_page) * n_pages);
477 pages = vmalloc(sizeof(struct page *) * n_pages);
480 for (i = 0; i < n_pages; i++) {
481 if (s->async_dma_dir != DMA_NONE) {
482 async->buf_page_list[i].virt_addr =
483 dma_alloc_coherent(dev->hw_dev,
491 async->buf_page_list[i].virt_addr =
493 get_zeroed_page(GFP_KERNEL);
495 if (async->buf_page_list[i].virt_addr == NULL)
498 mem_map_reserve(virt_to_page
499 (async->buf_page_list[i].
503 buf_page_list[i].virt_addr);
507 async->prealloc_buf =
508 vmap(pages, n_pages, VM_MAP, PAGE_KERNEL_NOCACHE);
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 mem_map_unreserve(virt_to_page
521 (async->buf_page_list
523 if (s->async_dma_dir != DMA_NONE) {
524 dma_free_coherent(dev->hw_dev,
533 free_page((unsigned long)
538 vfree(async->buf_page_list);
539 async->buf_page_list = NULL;
543 async->n_buf_pages = n_pages;
545 async->prealloc_bufsz = new_size;
550 /* munging is applied to data by core as it passes between user
551 * and kernel space */
552 unsigned int comedi_buf_munge(struct comedi_async *async,
553 unsigned int num_bytes)
555 struct comedi_subdevice *s = async->subdevice;
556 unsigned int count = 0;
557 const unsigned num_sample_bytes = bytes_per_sample(s);
559 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
560 async->munge_count += num_bytes;
561 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
564 /* don't munge partial samples */
565 num_bytes -= num_bytes % num_sample_bytes;
566 while (count < num_bytes) {
569 block_size = num_bytes - count;
570 if (block_size < 0) {
571 printk("%s: %s: bug! block_size is negative\n",
575 if ((int)(async->munge_ptr + block_size -
576 async->prealloc_bufsz) > 0)
577 block_size = async->prealloc_bufsz - async->munge_ptr;
579 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
580 block_size, async->munge_chan);
582 smp_wmb(); /* barrier insures data is munged in buffer before munge_count is incremented */
584 async->munge_chan += block_size / num_sample_bytes;
585 async->munge_chan %= async->cmd.chanlist_len;
586 async->munge_count += block_size;
587 async->munge_ptr += block_size;
588 async->munge_ptr %= async->prealloc_bufsz;
591 BUG_ON((int)(async->munge_count - async->buf_write_count) > 0);
595 unsigned int comedi_buf_write_n_available(struct comedi_async *async)
597 unsigned int free_end;
603 free_end = async->buf_read_count + async->prealloc_bufsz;
604 nbytes = free_end - async->buf_write_alloc_count;
605 nbytes -= nbytes % bytes_per_sample(async->subdevice);
606 /* barrier insures the read of buf_read_count in this
607 query occurs before any following writes to the buffer which
608 might be based on the return value from this query.
614 /* allocates chunk for the writer from free buffer space */
615 unsigned int comedi_buf_write_alloc(struct comedi_async *async,
618 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
620 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
621 nbytes = free_end - async->buf_write_alloc_count;
623 async->buf_write_alloc_count += nbytes;
624 /* barrier insures the read of buf_read_count above occurs before
625 we write data to the write-alloc'ed buffer space */
630 /* allocates nothing unless it can completely fulfill the request */
631 unsigned int comedi_buf_write_alloc_strict(struct comedi_async *async,
634 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
636 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0)
639 async->buf_write_alloc_count += nbytes;
640 /* barrier insures the read of buf_read_count above occurs before
641 we write data to the write-alloc'ed buffer space */
646 /* transfers a chunk from writer to filled buffer space */
647 unsigned comedi_buf_write_free(struct comedi_async *async, unsigned int nbytes)
649 if ((int)(async->buf_write_count + nbytes -
650 async->buf_write_alloc_count) > 0) {
652 ("comedi: attempted to write-free more bytes than have been write-allocated.\n");
653 nbytes = async->buf_write_alloc_count - async->buf_write_count;
655 async->buf_write_count += nbytes;
656 async->buf_write_ptr += nbytes;
657 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
658 if (async->buf_write_ptr >= async->prealloc_bufsz)
659 async->buf_write_ptr %= async->prealloc_bufsz;
664 /* allocates a chunk for the reader from filled (and munged) buffer space */
665 unsigned comedi_buf_read_alloc(struct comedi_async *async, unsigned nbytes)
667 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
669 nbytes = async->munge_count - async->buf_read_alloc_count;
671 async->buf_read_alloc_count += nbytes;
672 /* barrier insures read of munge_count occurs before we actually read
673 data out of buffer */
678 /* transfers control of a chunk from reader to free buffer space */
679 unsigned comedi_buf_read_free(struct comedi_async *async, unsigned int nbytes)
681 /* barrier insures data has been read out of buffer before read count is incremented */
683 if ((int)(async->buf_read_count + nbytes -
684 async->buf_read_alloc_count) > 0) {
686 ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
687 nbytes = async->buf_read_alloc_count - async->buf_read_count;
689 async->buf_read_count += nbytes;
690 async->buf_read_ptr += nbytes;
691 async->buf_read_ptr %= async->prealloc_bufsz;
695 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
696 const void *data, unsigned int num_bytes)
698 unsigned int write_ptr = async->buf_write_ptr + offset;
700 if (write_ptr >= async->prealloc_bufsz)
701 write_ptr %= async->prealloc_bufsz;
704 unsigned int block_size;
706 if (write_ptr + num_bytes > async->prealloc_bufsz)
707 block_size = async->prealloc_bufsz - write_ptr;
709 block_size = num_bytes;
711 memcpy(async->prealloc_buf + write_ptr, data, block_size);
714 num_bytes -= block_size;
720 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
721 void *dest, unsigned int nbytes)
724 unsigned int read_ptr = async->buf_read_ptr + offset;
726 if (read_ptr >= async->prealloc_bufsz)
727 read_ptr %= async->prealloc_bufsz;
730 unsigned int block_size;
732 src = async->prealloc_buf + read_ptr;
734 if (nbytes >= async->prealloc_bufsz - read_ptr)
735 block_size = async->prealloc_bufsz - read_ptr;
739 memcpy(dest, src, block_size);
740 nbytes -= block_size;
746 unsigned int comedi_buf_read_n_available(struct comedi_async *async)
752 num_bytes = async->munge_count - async->buf_read_count;
753 /* barrier insures the read of munge_count in this
754 query occurs before any following reads of the buffer which
755 might be based on the return value from this query.
761 int comedi_buf_get(struct comedi_async *async, short *x)
763 unsigned int n = comedi_buf_read_n_available(async);
765 if (n < sizeof(short))
767 comedi_buf_read_alloc(async, sizeof(short));
768 *x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
769 comedi_buf_read_free(async, sizeof(short));
773 int comedi_buf_put(struct comedi_async *async, short x)
775 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(short));
777 if (n < sizeof(short)) {
778 async->events |= COMEDI_CB_ERROR;
781 *(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
782 comedi_buf_write_free(async, sizeof(short));
786 void comedi_reset_async_buf(struct comedi_async *async)
788 async->buf_write_alloc_count = 0;
789 async->buf_write_count = 0;
790 async->buf_read_alloc_count = 0;
791 async->buf_read_count = 0;
793 async->buf_write_ptr = 0;
794 async->buf_read_ptr = 0;
797 async->scan_progress = 0;
798 async->munge_chan = 0;
799 async->munge_count = 0;
800 async->munge_ptr = 0;
805 int comedi_auto_config(struct device *hardware_device, const char *board_name,
806 const int *options, unsigned num_options)
808 struct comedi_devconfig it;
810 struct comedi_device_file_info *dev_file_info;
812 unsigned *private_data = NULL;
814 if (!comedi_autoconfig) {
815 dev_set_drvdata(hardware_device, NULL);
819 minor = comedi_alloc_board_minor(hardware_device);
823 private_data = kmalloc(sizeof(unsigned), GFP_KERNEL);
824 if (private_data == NULL) {
828 *private_data = minor;
829 dev_set_drvdata(hardware_device, private_data);
831 dev_file_info = comedi_get_device_file_info(minor);
833 memset(&it, 0, sizeof(it));
834 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
835 it.board_name[COMEDI_NAMELEN - 1] = '\0';
836 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
837 memcpy(it.options, options, num_options * sizeof(int));
839 mutex_lock(&dev_file_info->device->mutex);
840 retval = comedi_device_attach(dev_file_info->device, &it);
841 mutex_unlock(&dev_file_info->device->mutex);
846 comedi_free_board_minor(minor);
851 void comedi_auto_unconfig(struct device *hardware_device)
853 unsigned *minor = (unsigned *)dev_get_drvdata(hardware_device);
857 BUG_ON(*minor >= COMEDI_NUM_BOARD_MINORS);
859 comedi_free_board_minor(*minor);
860 dev_set_drvdata(hardware_device, NULL);
864 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
869 options[0] = pcidev->bus->number;
871 options[1] = PCI_SLOT(pcidev->devfn);
873 return comedi_auto_config(&pcidev->dev, board_name,
874 options, ARRAY_SIZE(options));
877 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
879 comedi_auto_unconfig(&pcidev->dev);
882 int comedi_usb_auto_config(struct usb_device *usbdev, const char *board_name)
884 BUG_ON(usbdev == NULL);
885 return comedi_auto_config(&usbdev->dev, board_name, NULL, 0);
888 void comedi_usb_auto_unconfig(struct usb_device *usbdev)
890 BUG_ON(usbdev == NULL);
891 comedi_auto_unconfig(&usbdev->dev);