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 <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 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 __comedi_device_detach(dev);
152 // recognize has failed if we get here
153 // report valid board names before returning error
154 for (driv = comedi_drivers; driv; driv = driv->next) {
155 if (!try_module_get(driv->module)) {
156 printk("comedi: failed to increment module count\n");
159 comedi_report_boards(driv);
160 module_put(driv->module);
165 /* do a little post-config cleanup */
166 ret = postconfig(dev);
168 __comedi_device_detach(dev);
172 if (!dev->board_name) {
173 printk("BUG: dev->board_name=<%p>\n", dev->board_name);
174 dev->board_name = "BUG";
178 module_put(dev->driver->module);
183 int comedi_driver_register(comedi_driver * driver)
185 driver->next = comedi_drivers;
186 comedi_drivers = driver;
191 int comedi_driver_unregister(comedi_driver * driver)
196 /* check for devices using this driver */
197 for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
198 struct comedi_device_file_info *dev_file_info = comedi_get_device_file_info(i);
201 if(dev_file_info == NULL) continue;
202 dev = dev_file_info->device;
204 mutex_lock(&dev->mutex);
205 if (dev->attached && dev->driver == driver) {
207 printk("BUG! detaching device with use_count=%d\n", dev->use_count);
208 comedi_device_detach(dev);
210 mutex_unlock(&dev->mutex);
213 if (comedi_drivers == driver) {
214 comedi_drivers = driver->next;
218 for (prev = comedi_drivers; prev->next; prev = prev->next) {
219 if (prev->next == driver) {
220 prev->next = driver->next;
227 static int postconfig(comedi_device * dev)
231 comedi_async *async = NULL;
234 for (i = 0; i < dev->n_subdevices; i++) {
235 s = dev->subdevices + i;
237 if (s->type == COMEDI_SUBD_UNUSED)
240 if (s->len_chanlist == 0)
244 BUG_ON((s->subdev_flags & (SDF_CMD_READ |
245 SDF_CMD_WRITE)) == 0);
246 BUG_ON(!s->do_cmdtest);
248 async = kzalloc(sizeof(comedi_async), GFP_KERNEL);
250 printk("failed to allocate async struct\n");
253 init_waitqueue_head(&async->wait_head);
254 async->subdevice = s;
257 #define DEFAULT_BUF_MAXSIZE (64*1024)
258 #define DEFAULT_BUF_SIZE (64*1024)
260 async->max_bufsize = DEFAULT_BUF_MAXSIZE;
262 async->prealloc_buf = NULL;
263 async->prealloc_bufsz = 0;
264 if (comedi_buf_alloc(dev, s, DEFAULT_BUF_SIZE) < 0) {
265 printk("Buffer allocation failed\n");
269 ret = s->buf_change(dev, s, DEFAULT_BUF_SIZE);
273 comedi_alloc_subdevice_minor(dev, s);
276 if (!s->range_table && !s->range_table_list)
277 s->range_table = &range_unknown;
279 if (!s->insn_read && s->insn_bits)
280 s->insn_read = insn_rw_emulate_bits;
281 if (!s->insn_write && s->insn_bits)
282 s->insn_write = insn_rw_emulate_bits;
285 s->insn_read = insn_inval;
287 s->insn_write = insn_inval;
289 s->insn_bits = insn_inval;
291 s->insn_config = insn_inval;
294 s->poll = poll_invalid;
300 // generic recognize function for drivers that register their supported board names
301 void *comedi_recognize(comedi_driver * driv, const char *name)
304 const char *const *name_ptr = driv->board_name;
305 for (i = 0; i < driv->num_names; i++) {
306 if (strcmp(*name_ptr, name) == 0)
307 return (void *)name_ptr;
309 (const char *const *)((const char *)name_ptr +
316 void comedi_report_boards(comedi_driver * driv)
319 const char *const *name_ptr;
321 printk("comedi: valid board names for %s driver are:\n",
324 name_ptr = driv->board_name;
325 for (i = 0; i < driv->num_names; i++) {
326 printk(" %s\n", *name_ptr);
327 name_ptr = (const char **)((char *)name_ptr + driv->offset);
330 if (driv->num_names == 0)
331 printk(" %s\n", driv->driver_name);
334 static int poll_invalid(comedi_device * dev, comedi_subdevice * s)
339 int insn_inval(comedi_device * dev, comedi_subdevice * s,
340 comedi_insn * insn, lsampl_t * data)
345 static int insn_rw_emulate_bits(comedi_device * dev, comedi_subdevice * s,
346 comedi_insn * insn, lsampl_t * data)
348 comedi_insn new_insn;
350 static const unsigned channels_per_bitfield = 32;
352 unsigned chan = CR_CHAN(insn->chanspec);
353 const unsigned base_bitfield_channel =
354 (chan < channels_per_bitfield) ? 0 : chan;
355 lsampl_t new_data[2];
356 memset(new_data, 0, sizeof(new_data));
357 memset(&new_insn, 0, sizeof(new_insn));
358 new_insn.insn = INSN_BITS;
359 new_insn.chanspec = base_bitfield_channel;
361 new_insn.data = new_data;
362 new_insn.subdev = insn->subdev;
364 if (insn->insn == INSN_WRITE) {
365 if (!(s->subdev_flags & SDF_WRITABLE))
367 new_data[0] = 1 << (chan - base_bitfield_channel); /* mask */
368 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel)) : 0; /* bits */
371 ret = s->insn_bits(dev, s, &new_insn, new_data);
375 if (insn->insn == INSN_READ) {
376 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
382 static inline unsigned long uvirt_to_kva(pgd_t * pgd, unsigned long adr)
384 unsigned long ret = 0UL;
389 if (!pgd_none(*pgd)) {
390 pud = pud_offset(pgd, adr);
391 pmd = pmd_offset(pud, adr);
392 if (!pmd_none(*pmd)) {
393 ptep = pte_offset_kernel(pmd, adr);
395 if (pte_present(pte)) {
396 ret = (unsigned long)
397 page_address(pte_page(pte));
398 ret |= (adr & (PAGE_SIZE - 1));
405 static inline unsigned long kvirt_to_kva(unsigned long adr)
407 unsigned long va, kva;
410 kva = uvirt_to_kva(pgd_offset_k(va), va);
415 int comedi_buf_alloc(comedi_device * dev, comedi_subdevice * s,
416 unsigned long new_size)
418 comedi_async *async = s->async;
420 /* Round up new_size to multiple of PAGE_SIZE */
421 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
423 /* if no change is required, do nothing */
424 if (async->prealloc_buf && async->prealloc_bufsz == new_size) {
427 // deallocate old buffer
428 if (async->prealloc_buf) {
429 vunmap(async->prealloc_buf);
430 async->prealloc_buf = NULL;
431 async->prealloc_bufsz = 0;
433 if (async->buf_page_list) {
435 for (i = 0; i < async->n_buf_pages; ++i) {
436 if (async->buf_page_list[i].virt_addr) {
437 mem_map_unreserve(virt_to_page(async->
438 buf_page_list[i].virt_addr));
439 if (s->async_dma_dir != DMA_NONE) {
440 dma_free_coherent(dev->hw_dev,
442 async->buf_page_list[i].
444 async->buf_page_list[i].
447 free_page((unsigned long)async->
448 buf_page_list[i].virt_addr);
452 vfree(async->buf_page_list);
453 async->buf_page_list = NULL;
454 async->n_buf_pages = 0;
456 // allocate new buffer
459 unsigned n_pages = new_size >> PAGE_SHIFT;
460 struct page **pages = NULL;
462 async->buf_page_list =
463 vmalloc(sizeof(struct comedi_buf_page) * n_pages);
464 if (async->buf_page_list) {
465 memset(async->buf_page_list, 0,
466 sizeof(struct comedi_buf_page) * n_pages);
467 pages = vmalloc(sizeof(struct page *) * n_pages);
470 for (i = 0; i < n_pages; i++) {
471 if (s->async_dma_dir != DMA_NONE) {
472 async->buf_page_list[i].virt_addr =
473 dma_alloc_coherent(dev->hw_dev,
475 &async->buf_page_list[i].
477 GFP_KERNEL | __GFP_COMP);
479 async->buf_page_list[i].virt_addr =
481 get_zeroed_page(GFP_KERNEL);
483 if (async->buf_page_list[i].virt_addr == NULL) {
486 mem_map_reserve(virt_to_page(async->
487 buf_page_list[i].virt_addr));
489 virt_to_page(async->buf_page_list[i].
494 async->prealloc_buf =
495 vmap(pages, n_pages, VM_MAP,
496 PAGE_KERNEL_NOCACHE);
501 if (async->prealloc_buf == NULL) {
502 /* Some allocation failed above. */
503 if (async->buf_page_list) {
504 for (i = 0; i < n_pages; i++) {
505 if (async->buf_page_list[i].virt_addr ==
509 mem_map_unreserve(virt_to_page(async->
512 if (s->async_dma_dir != DMA_NONE) {
513 dma_free_coherent(dev->hw_dev,
515 async->buf_page_list[i].
517 async->buf_page_list[i].
520 free_page((unsigned long)async->
525 vfree(async->buf_page_list);
526 async->buf_page_list = NULL;
530 async->n_buf_pages = n_pages;
532 async->prealloc_bufsz = new_size;
537 /* munging is applied to data by core as it passes between user
538 * and kernel space */
539 unsigned int comedi_buf_munge(comedi_async * async, unsigned int num_bytes)
541 comedi_subdevice *s = async->subdevice;
542 unsigned int count = 0;
543 const unsigned num_sample_bytes = bytes_per_sample(s);
545 if (s->munge == NULL || (async->cmd.flags & CMDF_RAWDATA)) {
546 async->munge_count += num_bytes;
547 if ((int)(async->munge_count - async->buf_write_count) > 0)
551 /* don't munge partial samples */
552 num_bytes -= num_bytes % num_sample_bytes;
553 while (count < num_bytes) {
556 block_size = num_bytes - count;
557 if (block_size < 0) {
558 rt_printk("%s: %s: bug! block_size is negative\n",
559 __FILE__, __FUNCTION__);
562 if ((int)(async->munge_ptr + block_size -
563 async->prealloc_bufsz) > 0)
564 block_size = async->prealloc_bufsz - async->munge_ptr;
566 s->munge(s->device, s, async->prealloc_buf + async->munge_ptr,
567 block_size, async->munge_chan);
569 smp_wmb(); //barrier insures data is munged in buffer before munge_count is incremented
571 async->munge_chan += block_size / num_sample_bytes;
572 async->munge_chan %= async->cmd.chanlist_len;
573 async->munge_count += block_size;
574 async->munge_ptr += block_size;
575 async->munge_ptr %= async->prealloc_bufsz;
578 if ((int)(async->munge_count - async->buf_write_count) > 0)
583 unsigned int comedi_buf_write_n_available(comedi_async * async)
585 unsigned int free_end;
591 free_end = async->buf_read_count + async->prealloc_bufsz;
592 nbytes = free_end - async->buf_write_alloc_count;
593 nbytes -= nbytes % bytes_per_sample(async->subdevice);
594 /* barrier insures the read of buf_read_count in this
595 query occurs before any following writes to the buffer which
596 might be based on the return value from this query.
602 /* allocates chunk for the writer from free buffer space */
603 unsigned int comedi_buf_write_alloc(comedi_async * async, unsigned int nbytes)
605 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
607 if ((int)(async->buf_write_alloc_count + nbytes - free_end) > 0) {
608 nbytes = free_end - async->buf_write_alloc_count;
610 async->buf_write_alloc_count += nbytes;
611 /* barrier insures the read of buf_read_count above occurs before
612 we write data to the write-alloc'ed buffer space */
617 /* allocates nothing unless it can completely fulfill the request */
618 unsigned int comedi_buf_write_alloc_strict(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) {
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 /* transfers a chunk from writer to filled buffer space */
634 unsigned comedi_buf_write_free(comedi_async * async, unsigned int nbytes)
636 if ((int)(async->buf_write_count + nbytes -
637 async->buf_write_alloc_count) > 0) {
639 ("comedi: attempted to write-free more bytes than have been write-allocated.\n");
640 nbytes = async->buf_write_alloc_count - async->buf_write_count;
642 async->buf_write_count += nbytes;
643 async->buf_write_ptr += nbytes;
644 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
645 if (async->buf_write_ptr >= async->prealloc_bufsz) {
646 async->buf_write_ptr %= async->prealloc_bufsz;
651 /* allocates a chunk for the reader from filled (and munged) buffer space */
652 unsigned comedi_buf_read_alloc(comedi_async * async, unsigned nbytes)
654 if ((int)(async->buf_read_alloc_count + nbytes - async->munge_count) >
656 nbytes = async->munge_count - async->buf_read_alloc_count;
658 async->buf_read_alloc_count += nbytes;
659 /* barrier insures read of munge_count occurs before we actually read
660 data out of buffer */
665 /* transfers control of a chunk from reader to free buffer space */
666 unsigned comedi_buf_read_free(comedi_async * async, unsigned int nbytes)
668 // barrier insures data has been read out of buffer before read count is incremented
670 if ((int)(async->buf_read_count + nbytes -
671 async->buf_read_alloc_count) > 0) {
673 ("comedi: attempted to read-free more bytes than have been read-allocated.\n");
674 nbytes = async->buf_read_alloc_count - async->buf_read_count;
676 async->buf_read_count += nbytes;
677 async->buf_read_ptr += nbytes;
678 async->buf_read_ptr %= async->prealloc_bufsz;
682 void comedi_buf_memcpy_to(comedi_async * async, unsigned int offset,
683 const void *data, unsigned int num_bytes)
685 unsigned int write_ptr = async->buf_write_ptr + offset;
687 if (write_ptr >= async->prealloc_bufsz)
688 write_ptr %= async->prealloc_bufsz;
691 unsigned int block_size;
693 if (write_ptr + num_bytes > async->prealloc_bufsz)
694 block_size = async->prealloc_bufsz - write_ptr;
696 block_size = num_bytes;
698 memcpy(async->prealloc_buf + write_ptr, data, block_size);
701 num_bytes -= block_size;
707 void comedi_buf_memcpy_from(comedi_async * async, unsigned int offset,
708 void *dest, unsigned int nbytes)
711 unsigned int read_ptr = async->buf_read_ptr + offset;
713 if (read_ptr >= async->prealloc_bufsz)
714 read_ptr %= async->prealloc_bufsz;
717 unsigned int block_size;
719 src = async->prealloc_buf + read_ptr;
721 if (nbytes >= async->prealloc_bufsz - read_ptr)
722 block_size = async->prealloc_bufsz - read_ptr;
726 memcpy(dest, src, block_size);
727 nbytes -= block_size;
733 unsigned int comedi_buf_read_n_available(comedi_async * async)
739 num_bytes = async->munge_count - async->buf_read_count;
740 /* barrier insures the read of munge_count in this
741 query occurs before any following reads of the buffer which
742 might be based on the return value from this query.
748 int comedi_buf_get(comedi_async * async, sampl_t * x)
750 unsigned int n = comedi_buf_read_n_available(async);
752 if (n < sizeof(sampl_t))
754 comedi_buf_read_alloc(async, sizeof(sampl_t));
755 *x = *(sampl_t *) (async->prealloc_buf + async->buf_read_ptr);
756 comedi_buf_read_free(async, sizeof(sampl_t));
760 int comedi_buf_put(comedi_async * async, sampl_t x)
762 unsigned int n = comedi_buf_write_alloc_strict(async, sizeof(sampl_t));
764 if (n < sizeof(sampl_t)) {
765 async->events |= COMEDI_CB_ERROR;
768 *(sampl_t *) (async->prealloc_buf + async->buf_write_ptr) = x;
769 comedi_buf_write_free(async, sizeof(sampl_t));
773 void comedi_reset_async_buf(comedi_async * async)
775 async->buf_write_alloc_count = 0;
776 async->buf_write_count = 0;
777 async->buf_read_alloc_count = 0;
778 async->buf_read_count = 0;
780 async->buf_write_ptr = 0;
781 async->buf_read_ptr = 0;
784 async->scan_progress = 0;
785 async->munge_chan = 0;
786 async->munge_count = 0;
787 async->munge_ptr = 0;
792 int comedi_pci_auto_config(const char *board_name, struct pci_dev *pcidev)
796 struct comedi_device_file_info *dev_file_info;
799 minor = comedi_alloc_board_minor(&pcidev->dev);
800 if(minor < 0) return minor;
801 pci_set_drvdata(pcidev, (void*)minor);
803 dev_file_info = comedi_get_device_file_info(minor);
805 memset(&it, 0, sizeof(it));
806 strncpy(it.board_name, board_name, COMEDI_NAMELEN);
807 it.board_name[COMEDI_NAMELEN - 1] = '\0';
809 it.options[0] = pcidev->bus->number;
811 it.options[1] = PCI_SLOT(pcidev->devfn);
813 mutex_lock(&dev_file_info->device->mutex);
814 retval = comedi_device_attach(dev_file_info->device, &it);
815 mutex_unlock(&dev_file_info->device->mutex);
818 comedi_free_board_minor(minor);
823 void comedi_pci_auto_unconfig(struct pci_dev *pcidev)
825 unsigned long minor = (unsigned long)pci_get_drvdata(pcidev);
826 struct comedi_device_file_info *dev_file_info;
828 BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
830 comedi_free_board_minor(minor);