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/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 "comedidev.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 static int postconfig(comedi_device * dev);
50 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
51 comedi_insn * insn, lsampl_t * data);
52 static void *comedi_recognize(comedi_driver * driv, const char *name);
53 static void comedi_report_boards(comedi_driver * driv);
54 static int poll_invalid(comedi_device * dev, comedi_subdevice * s);
55 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
56 unsigned long new_size);
58 comedi_driver *comedi_drivers;
60 int comedi_modprobe(int minor)
65 static void cleanup_device(comedi_device * dev)
70 if (dev->subdevices) {
71 for (i = 0; i < dev->n_subdevices; i++) {
72 s = dev->subdevices + i;
73 comedi_free_subdevice_minor(s);
75 comedi_buf_alloc(dev, s, 0);
79 kfree(dev->subdevices);
80 dev->subdevices = NULL;
81 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(comedi_device * dev)
103 dev->driver->detach(dev);
105 printk("BUG: dev->driver=NULL in comedi_device_detach()\n");
110 void comedi_device_detach(comedi_device * dev)
114 __comedi_device_detach(dev);
117 int comedi_device_attach(comedi_device * dev, comedi_devconfig * it)
125 for (driv = comedi_drivers; driv; driv = driv->next) {
126 if (!try_module_get(driv->module)) {
127 printk("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(comedi_driver * driver)
186 driver->next = comedi_drivers;
187 comedi_drivers = driver;
192 int comedi_driver_unregister(comedi_driver * driver)
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 = comedi_get_device_file_info(i);
202 if(dev_file_info == NULL) continue;
203 dev = dev_file_info->device;
205 mutex_lock(&dev->mutex);
206 if (dev->attached && dev->driver == driver) {
208 printk("BUG! detaching device with use_count=%d\n", dev->use_count);
209 comedi_device_detach(dev);
211 mutex_unlock(&dev->mutex);
214 if (comedi_drivers == driver) {
215 comedi_drivers = driver->next;
219 for (prev = comedi_drivers; prev->next; prev = prev->next) {
220 if (prev->next == driver) {
221 prev->next = driver->next;
228 static int postconfig(comedi_device * dev)
232 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);
249 async = kzalloc(sizeof(comedi_async), GFP_KERNEL);
251 printk("failed to allocate async struct\n");
254 init_waitqueue_head(&async->wait_head);
255 async->subdevice = s;
258 #define DEFAULT_BUF_MAXSIZE (64*1024)
259 #define DEFAULT_BUF_SIZE (64*1024)
261 async->max_bufsize = DEFAULT_BUF_MAXSIZE;
263 async->prealloc_buf = NULL;
264 async->prealloc_bufsz = 0;
265 if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
266 printk("Buffer allocation failed\n");
270 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
274 comedi_alloc_subdevice_minor(dev, s);
277 if (!s->range_table && !s->range_table_list)
278 s->range_table = &range_unknown;
280 if (!s->insn_read && s->insn_bits)
281 s->insn_read = insn_rw_emulate_bits;
282 if (!s->insn_write && s->insn_bits)
283 s->insn_write = insn_rw_emulate_bits;
286 s->insn_read = insn_inval;
288 s->insn_write = insn_inval;
290 s->insn_bits = insn_inval;
292 s->insn_config = insn_inval;
295 s->poll = poll_invalid;
301 // generic recognize function for drivers that register their supported board names
302 void *comedi_recognize(comedi_driver * driv, const char *name)
305 const char *const *name_ptr = driv->board_name;
306 for (i = 0; i < driv->num_names; i++) {
307 if (strcmp(*name_ptr, name) == 0)
308 return (void *)name_ptr;
310 (const char *const *)((const char *)name_ptr +
317 void comedi_report_boards(comedi_driver * driv)
320 const char *const *name_ptr;
322 printk("comedi: valid board names for %s driver are:\n",
325 name_ptr = driv->board_name;
326 for (i = 0; i < driv->num_names; i++) {
327 printk(" %s\n", *name_ptr);
328 name_ptr = (const char **)((char *)name_ptr + driv->offset);
331 if (driv->num_names == 0)
332 printk(" %s\n", driv->driver_name);
335 static int poll_invalid(comedi_device * dev, comedi_subdevice * s)
340 int insn_inval(comedi_device * dev, comedi_subdevice * s,
341 comedi_insn * insn, lsampl_t * data)
346 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
347 comedi_insn * insn, lsampl_t * data)
349 comedi_insn new_insn;
351 static const unsigned channels_per_bitfield = 32;
353 unsigned chan = CR_CHAN(insn->chanspec);
354 const unsigned base_bitfield_channel =
355 (chan < channels_per_bitfield) ? 0 : chan;
356 lsampl_t new_data[2];
357 memset(new_data, 0, sizeof(new_data));
358 memset(&new_insn, 0, sizeof(new_insn));
359 new_insn.insn = INSN_BITS;
360 new_insn.chanspec = base_bitfield_channel;
362 new_insn.data = new_data;
363 new_insn.subdev = insn->subdev;
365 if (insn->insn == INSN_WRITE) {
366 if (!(s->subdev_flags & SDF_WRITABLE))
368 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
369 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0; /* bits */
372 ret = s->insn_bits(dev, s, &new_insn, new_data);
376 if (insn->insn == INSN_READ) {
377 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
383 static inline unsigned long uvirt_to_kva(pgd_t * pgd, unsigned long adr)
385 unsigned long ret = 0UL;
390 if (!pgd_none(*pgd)) {
391 pud = pud_offset(pgd, adr);
392 pmd = pmd_offset(pud, adr);
393 if (!pmd_none(*pmd)) {
394 ptep = pte_offset_kernel(pmd, adr);
396 if (pte_present(pte)) {
397 ret = (unsigned long)
398 page_address(pte_page(pte));
399 ret |= (adr & (PAGE_SIZE - 1));
406 static inline unsigned long kvirt_to_kva(unsigned long adr)
408 unsigned long va, kva;
411 kva = uvirt_to_kva(pgd_offset_k(va), va);
416 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
417 unsigned long new_size)
419 comedi_async *async = s->async;
421 /* Round up new_size to multiple of PAGE_SIZE */
422 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
424 /* if no change is required, do nothing */
425 if (async->prealloc_buf && async->prealloc_bufsz == new_size) {
428 // deallocate old buffer
429 if (async->prealloc_buf) {
430 vunmap(async->prealloc_buf);
431 async->prealloc_buf = NULL;
432 async->prealloc_bufsz = 0;
434 if (async->buf_page_list) {
436 for (i = 0; i < async->n_buf_pages; ++i) {
437 if (async->buf_page_list[i].virt_addr) {
438 mem_map_unreserve(virt_to_page(async->
439 buf_page_list[i].virt_addr));
440 if (s->async_dma_dir != DMA_NONE) {
441 dma_free_coherent(dev->hw_dev,
443 async->buf_page_list[i].
445 async->buf_page_list[i].
448 free_page((unsigned long)async->
449 buf_page_list[i].virt_addr);
453 vfree(async->buf_page_list);
454 async->buf_page_list = NULL;
455 async->n_buf_pages = 0;
457 // allocate new buffer
460 unsigned n_pages = new_size >> PAGE_SHIFT;
461 struct page **pages = NULL;
463 async->buf_page_list =
464 vmalloc(sizeof(struct comedi_buf_page) * n_pages);
465 if (async->buf_page_list) {
466 memset(async->buf_page_list, 0,
467 sizeof(struct comedi_buf_page) * n_pages);
468 pages = vmalloc(sizeof(struct page *) * n_pages);
471 for (i = 0; i < n_pages; i++) {
472 if (s->async_dma_dir != DMA_NONE) {
473 async->buf_page_list[i].virt_addr =
474 dma_alloc_coherent(dev->hw_dev,
476 &async->buf_page_list[i].
478 GFP_KERNEL | __GFP_COMP);
480 async->buf_page_list[i].virt_addr =
482 get_zeroed_page(GFP_KERNEL);
484 if (async->buf_page_list[i].virt_addr == NULL) {
487 mem_map_reserve(virt_to_page(async->
488 buf_page_list[i].virt_addr));
490 virt_to_page(async->buf_page_list[i].
495 async->prealloc_buf =
496 vmap(pages, n_pages, VM_MAP,
497 PAGE_KERNEL_NOCACHE);
502 if (async->prealloc_buf == NULL) {
503 /* Some allocation failed above. */
504 if (async->buf_page_list) {
505 for (i = 0; i < n_pages; i++) {
506 if (async->buf_page_list[i].virt_addr ==
510 mem_map_unreserve(virt_to_page(async->
513 if (s->async_dma_dir != DMA_NONE) {
514 dma_free_coherent(dev->hw_dev,
516 async->buf_page_list[i].
518 async->buf_page_list[i].
521 free_page((unsigned long)async->
526 vfree(async->buf_page_list);
527 async->buf_page_list = NULL;
531 async->n_buf_pages = n_pages;
533 async->prealloc_bufsz = new_size;
538 /* munging is applied to data by core as it passes between user
539 * and kernel space */
540 unsigned int comedi_buf_munge(comedi_async * async, unsigned int num_bytes)
542 comedi_subdevice *s = async->subdevice;
543 unsigned int count = 0;
544 const unsigned num_sample_bytes = bytes_per_sample(s);
546 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
547 async->munge_count += num_bytes;
548 if ((int)(async->munge_count - async->buf_write_count) > 0)
552 /* don't munge partial samples */
553 num_bytes -= num_bytes % num_sample_bytes;
554 while (count < num_bytes) {
557 block_size = num_bytes - count;
558 if (block_size < 0) {
559 rt_printk("%s: %s: bug! block_size is negative\n",
563 if ((int)(async->munge_ptr + block_size -
564 async->prealloc_bufsz) > 0)
565 block_size = async->prealloc_bufsz - async->munge_ptr;
567 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
568 block_size, async->munge_chan);
570 smp_wmb(); //barrier insures data is munged in buffer before munge_count is incremented
572 async->munge_chan += block_size / num_sample_bytes;
573 async->munge_chan %= async->cmd.chanlist_len;
574 async->munge_count += block_size;
575 async->munge_ptr += block_size;
576 async->munge_ptr %= async->prealloc_bufsz;
579 if ((int)(async->munge_count - async->buf_write_count) > 0)
584 unsigned int comedi_buf_write_n_available(comedi_async * async)
586 unsigned int free_end;
592 free_end = async->buf_read_count + async->prealloc_bufsz;
593 nbytes = free_end - async->buf_write_alloc_count;
594 nbytes -= nbytes % bytes_per_sample(async->subdevice);
595 /* barrier insures the read of buf_read_count in this
596 query occurs before any following writes to the buffer which
597 might be based on the return value from this query.
603 /* allocates chunk for the writer from free buffer space */
604 unsigned int comedi_buf_write_alloc(comedi_async * async, unsigned int nbytes)
606 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
608 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
609 nbytes = free_end - async->buf_write_alloc_count;
611 async->buf_write_alloc_count += nbytes;
612 /* barrier insures the read of buf_read_count above occurs before
613 we write data to the write-alloc'ed buffer space */
618 /* allocates nothing unless it can completely fulfill the request */
619 unsigned int comedi_buf_write_alloc_strict(comedi_async * async,
622 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
624 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
627 async->buf_write_alloc_count += nbytes;
628 /* barrier insures the read of buf_read_count above occurs before
629 we write data to the write-alloc'ed buffer space */
634 /* transfers a chunk from writer to filled buffer space */
635 unsigned comedi_buf_write_free(comedi_async * async, unsigned int nbytes)
637 if ((int)(async->buf_write_count + nbytes -
638 async->buf_write_alloc_count) > 0) {
640 ("comedi: attempted to write-free more bytes than have been write-allocated.\n");
641 nbytes = async->buf_write_alloc_count - async->buf_write_count;
643 async->buf_write_count += nbytes;
644 async->buf_write_ptr += nbytes;
645 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
646 if (async->buf_write_ptr >= async->prealloc_bufsz) {
647 async->buf_write_ptr %= async->prealloc_bufsz;
652 /* allocates a chunk for the reader from filled (and munged) buffer space */
653 unsigned comedi_buf_read_alloc(comedi_async * async, unsigned nbytes)
655 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
657 nbytes = async->munge_count - async->buf_read_alloc_count;
659 async->buf_read_alloc_count += nbytes;
660 /* barrier insures read of munge_count occurs before we actually read
661 data out of buffer */
666 /* transfers control of a chunk from reader to free buffer space */
667 unsigned comedi_buf_read_free(comedi_async * async, unsigned int nbytes)
669 // barrier insures data has been read out of buffer before read count is incremented
671 if ((int)(async->buf_read_count + nbytes -
672 async->buf_read_alloc_count) > 0) {
674 ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
675 nbytes = async->buf_read_alloc_count - async->buf_read_count;
677 async->buf_read_count += nbytes;
678 async->buf_read_ptr += nbytes;
679 async->buf_read_ptr %= async->prealloc_bufsz;
683 void comedi_buf_memcpy_to(comedi_async * async, unsigned int offset,
684 const void *data, unsigned int num_bytes)
686 unsigned int write_ptr = async->buf_write_ptr + offset;
688 if (write_ptr >= async->prealloc_bufsz)
689 write_ptr %= async->prealloc_bufsz;
692 unsigned int block_size;
694 if (write_ptr + num_bytes > async->prealloc_bufsz)
695 block_size = async->prealloc_bufsz - write_ptr;
697 block_size = num_bytes;
699 memcpy(async->prealloc_buf + write_ptr, data, block_size);
702 num_bytes -= block_size;
708 void comedi_buf_memcpy_from(comedi_async * async, unsigned int offset,
709 void *dest, unsigned int nbytes)
712 unsigned int read_ptr = async->buf_read_ptr + offset;
714 if (read_ptr >= async->prealloc_bufsz)
715 read_ptr %= async->prealloc_bufsz;
718 unsigned int block_size;
720 src = async->prealloc_buf + read_ptr;
722 if (nbytes >= async->prealloc_bufsz - read_ptr)
723 block_size = async->prealloc_bufsz - read_ptr;
727 memcpy(dest, src, block_size);
728 nbytes -= block_size;
734 unsigned int comedi_buf_read_n_available(comedi_async * async)
740 num_bytes = async->munge_count - async->buf_read_count;
741 /* barrier insures the read of munge_count in this
742 query occurs before any following reads of the buffer which
743 might be based on the return value from this query.
749 int comedi_buf_get(comedi_async * async, sampl_t * x)
751 unsigned int n = comedi_buf_read_n_available(async);
753 if (n < sizeof(sampl_t))
755 comedi_buf_read_alloc(async, sizeof(sampl_t));
756 *x = *(sampl_t *) (async->prealloc_buf + async->buf_read_ptr);
757 comedi_buf_read_free(async, sizeof(sampl_t));
761 int comedi_buf_put(comedi_async * async, sampl_t x)
763 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(sampl_t));
765 if (n < sizeof(sampl_t)) {
766 async->events |= COMEDI_CB_ERROR;
769 *(sampl_t *) (async->prealloc_buf + async->buf_write_ptr) = x;
770 comedi_buf_write_free(async, sizeof(sampl_t));
774 void comedi_reset_async_buf(comedi_async * async)
776 async->buf_write_alloc_count = 0;
777 async->buf_write_count = 0;
778 async->buf_read_alloc_count = 0;
779 async->buf_read_count = 0;
781 async->buf_write_ptr = 0;
782 async->buf_read_ptr = 0;
785 async->scan_progress = 0;
786 async->munge_chan = 0;
787 async->munge_count = 0;
788 async->munge_ptr = 0;
793 int comedi_auto_config(struct device *hardware_device, const char *board_name, const int *options, unsigned num_options)
797 struct comedi_device_file_info *dev_file_info;
800 minor = comedi_alloc_board_minor(hardware_device);
801 if(minor < 0) return minor;
802 dev_set_drvdata(hardware_device, (void*)(unsigned long)minor);
804 dev_file_info = comedi_get_device_file_info(minor);
806 memset(&it, 0, sizeof(it));
807 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
808 it.board_name[COMEDI_NAMELEN - 1] = '\0';
809 BUG_ON(num_options > COMEDI_NDEVCONFOPTS);
810 memcpy(it.options, options, num_options * sizeof(int));
812 mutex_lock(&dev_file_info->device->mutex);
813 retval = comedi_device_attach(dev_file_info->device, &it);
814 mutex_unlock(&dev_file_info->device->mutex);
817 comedi_free_board_minor(minor);
822 void comedi_auto_unconfig(struct device *hardware_device)
824 unsigned long minor = (unsigned long)dev_get_drvdata(hardware_device);
826 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
828 comedi_free_board_minor(minor);
831 int comedi_pci_auto_config(struct pci_dev *pcidev, const char *board_name)
836 options[0] = pcidev->bus->number;
838 options[1] = PCI_SLOT(pcidev->devfn);
840 return comedi_auto_config(&pcidev->dev, board_name, options, sizeof(options) / sizeof(options[0]));
843 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
845 comedi_auto_unconfig(&pcidev->dev);