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/kconfig.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/fcntl.h>
37 #include <linux/delay.h>
38 #include <linux/ioport.h>
40 #include <linux/slab.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 "comedidev.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
);
58 struct comedi_driver
*comedi_drivers
;
60 static void cleanup_device(struct comedi_device
*dev
)
63 struct comedi_subdevice
*s
;
65 if (dev
->subdevices
) {
66 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
67 s
= dev
->subdevices
+ i
;
68 comedi_free_subdevice_minor(s
);
70 comedi_buf_alloc(dev
, s
, 0);
74 kfree(dev
->subdevices
);
75 dev
->subdevices
= NULL
;
76 dev
->n_subdevices
= 0;
81 dev
->board_name
= NULL
;
82 dev
->board_ptr
= NULL
;
85 dev
->read_subdev
= NULL
;
86 dev
->write_subdev
= NULL
;
89 comedi_set_hw_dev(dev
, NULL
);
92 static void __comedi_device_detach(struct comedi_device
*dev
)
96 dev
->driver
->detach(dev
);
99 "BUG: dev->driver=NULL in comedi_device_detach()\n");
103 void comedi_device_detach(struct comedi_device
*dev
)
107 __comedi_device_detach(dev
);
110 /* do a little post-config cleanup */
111 /* called with module refcount incremented, decrements it */
112 static int comedi_device_postconfig(struct comedi_device
*dev
)
114 int ret
= postconfig(dev
);
115 module_put(dev
->driver
->module
);
117 __comedi_device_detach(dev
);
120 if (!dev
->board_name
) {
121 printk(KERN_WARNING
"BUG: dev->board_name=<%p>\n",
123 dev
->board_name
= "BUG";
130 int comedi_device_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
132 struct comedi_driver
*driv
;
138 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
139 if (!try_module_get(driv
->module
)) {
140 printk(KERN_INFO
"comedi: failed to increment module count, skipping\n");
143 if (driv
->num_names
) {
144 dev
->board_ptr
= comedi_recognize(driv
, it
->board_name
);
147 } else if (strcmp(driv
->driver_name
, it
->board_name
))
149 module_put(driv
->module
);
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
)) {
157 "comedi: failed to increment module count\n");
160 comedi_report_boards(driv
);
161 module_put(driv
->module
);
165 /* initialize dev->driver here so
166 * comedi_error() can be called from attach */
168 ret
= driv
->attach(dev
, it
);
170 module_put(dev
->driver
->module
);
171 __comedi_device_detach(dev
);
174 return comedi_device_postconfig(dev
);
177 int comedi_driver_register(struct comedi_driver
*driver
)
179 driver
->next
= comedi_drivers
;
180 comedi_drivers
= driver
;
184 EXPORT_SYMBOL(comedi_driver_register
);
186 int comedi_driver_unregister(struct comedi_driver
*driver
)
188 struct comedi_driver
*prev
;
191 /* check for devices using this driver */
192 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++) {
193 struct comedi_device_file_info
*dev_file_info
=
194 comedi_get_device_file_info(i
);
195 struct comedi_device
*dev
;
197 if (dev_file_info
== NULL
)
199 dev
= dev_file_info
->device
;
201 mutex_lock(&dev
->mutex
);
202 if (dev
->attached
&& dev
->driver
== driver
) {
204 printk(KERN_WARNING
"BUG! detaching device with use_count=%d\n",
206 comedi_device_detach(dev
);
208 mutex_unlock(&dev
->mutex
);
211 if (comedi_drivers
== driver
) {
212 comedi_drivers
= driver
->next
;
216 for (prev
= comedi_drivers
; prev
->next
; prev
= prev
->next
) {
217 if (prev
->next
== driver
) {
218 prev
->next
= driver
->next
;
224 EXPORT_SYMBOL(comedi_driver_unregister
);
226 static int postconfig(struct comedi_device
*dev
)
229 struct comedi_subdevice
*s
;
230 struct comedi_async
*async
= NULL
;
233 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
234 s
= dev
->subdevices
+ i
;
236 if (s
->type
== COMEDI_SUBD_UNUSED
)
239 if (s
->len_chanlist
== 0)
243 unsigned int buf_size
;
245 BUG_ON((s
->subdev_flags
& (SDF_CMD_READ
|
246 SDF_CMD_WRITE
)) == 0);
247 BUG_ON(!s
->do_cmdtest
);
250 kzalloc(sizeof(struct comedi_async
), GFP_KERNEL
);
253 "failed to allocate async struct\n");
256 init_waitqueue_head(&async
->wait_head
);
257 async
->subdevice
= s
;
261 comedi_default_buf_maxsize_kb
* 1024;
262 buf_size
= comedi_default_buf_size_kb
* 1024;
263 if (buf_size
> async
->max_bufsize
)
264 buf_size
= async
->max_bufsize
;
266 async
->prealloc_buf
= NULL
;
267 async
->prealloc_bufsz
= 0;
268 if (comedi_buf_alloc(dev
, s
, buf_size
) < 0) {
269 printk(KERN_INFO
"Buffer allocation failed\n");
273 ret
= s
->buf_change(dev
, s
, buf_size
);
277 comedi_alloc_subdevice_minor(dev
, s
);
280 if (!s
->range_table
&& !s
->range_table_list
)
281 s
->range_table
= &range_unknown
;
283 if (!s
->insn_read
&& s
->insn_bits
)
284 s
->insn_read
= insn_rw_emulate_bits
;
285 if (!s
->insn_write
&& s
->insn_bits
)
286 s
->insn_write
= insn_rw_emulate_bits
;
289 s
->insn_read
= insn_inval
;
291 s
->insn_write
= insn_inval
;
293 s
->insn_bits
= insn_inval
;
295 s
->insn_config
= insn_inval
;
298 s
->poll
= poll_invalid
;
304 /* generic recognize function for drivers
305 * that register their supported board names */
306 static void *comedi_recognize(struct comedi_driver
*driv
, const char *name
)
309 const char *const *name_ptr
= driv
->board_name
;
310 for (i
= 0; i
< driv
->num_names
; i
++) {
311 if (strcmp(*name_ptr
, name
) == 0)
312 return (void *)name_ptr
;
314 (const char *const *)((const char *)name_ptr
+
321 static void comedi_report_boards(struct comedi_driver
*driv
)
324 const char *const *name_ptr
;
326 printk(KERN_INFO
"comedi: valid board names for %s driver are:\n",
329 name_ptr
= driv
->board_name
;
330 for (i
= 0; i
< driv
->num_names
; i
++) {
331 printk(KERN_INFO
" %s\n", *name_ptr
);
332 name_ptr
= (const char **)((char *)name_ptr
+ driv
->offset
);
335 if (driv
->num_names
== 0)
336 printk(KERN_INFO
" %s\n", driv
->driver_name
);
339 static int poll_invalid(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
344 int insn_inval(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
345 struct comedi_insn
*insn
, unsigned int *data
)
350 static int insn_rw_emulate_bits(struct comedi_device
*dev
,
351 struct comedi_subdevice
*s
,
352 struct comedi_insn
*insn
, unsigned int *data
)
354 struct comedi_insn new_insn
;
356 static const unsigned channels_per_bitfield
= 32;
358 unsigned chan
= CR_CHAN(insn
->chanspec
);
359 const unsigned base_bitfield_channel
=
360 (chan
< channels_per_bitfield
) ? 0 : chan
;
361 unsigned int new_data
[2];
362 memset(new_data
, 0, sizeof(new_data
));
363 memset(&new_insn
, 0, sizeof(new_insn
));
364 new_insn
.insn
= INSN_BITS
;
365 new_insn
.chanspec
= base_bitfield_channel
;
367 new_insn
.data
= new_data
;
368 new_insn
.subdev
= insn
->subdev
;
370 if (insn
->insn
== INSN_WRITE
) {
371 if (!(s
->subdev_flags
& SDF_WRITABLE
))
373 new_data
[0] = 1 << (chan
- base_bitfield_channel
); /* mask */
374 new_data
[1] = data
[0] ? (1 << (chan
- base_bitfield_channel
))
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 clear_bit(PG_reserved
,
444 &(virt_to_page(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 vzalloc(sizeof(struct comedi_buf_page
) * n_pages
);
474 if (async
->buf_page_list
)
475 pages
= vmalloc(sizeof(struct page
*) * n_pages
);
478 for (i
= 0; i
< n_pages
; i
++) {
479 if (s
->async_dma_dir
!= DMA_NONE
) {
480 async
->buf_page_list
[i
].virt_addr
=
481 dma_alloc_coherent(dev
->hw_dev
,
489 async
->buf_page_list
[i
].virt_addr
=
491 get_zeroed_page(GFP_KERNEL
);
493 if (async
->buf_page_list
[i
].virt_addr
== NULL
)
497 &(virt_to_page(async
->buf_page_list
[i
].
499 pages
[i
] = virt_to_page(async
->buf_page_list
[i
].
504 async
->prealloc_buf
=
505 #ifdef PAGE_KERNEL_NOCACHE
506 vmap(pages
, n_pages
, VM_MAP
, PAGE_KERNEL_NOCACHE
);
508 vmap(pages
, n_pages
, VM_MAP
, PAGE_KERNEL
);
513 if (async
->prealloc_buf
== NULL
) {
514 /* Some allocation failed above. */
515 if (async
->buf_page_list
) {
516 for (i
= 0; i
< n_pages
; i
++) {
517 if (async
->buf_page_list
[i
].virt_addr
==
521 clear_bit(PG_reserved
,
522 &(virt_to_page(async
->
525 if (s
->async_dma_dir
!= DMA_NONE
) {
526 dma_free_coherent(dev
->hw_dev
,
535 free_page((unsigned long)
540 vfree(async
->buf_page_list
);
541 async
->buf_page_list
= NULL
;
545 async
->n_buf_pages
= n_pages
;
547 async
->prealloc_bufsz
= new_size
;
552 /* munging is applied to data by core as it passes between user
553 * and kernel space */
554 static unsigned int comedi_buf_munge(struct comedi_async
*async
,
555 unsigned int num_bytes
)
557 struct comedi_subdevice
*s
= async
->subdevice
;
558 unsigned int count
= 0;
559 const unsigned num_sample_bytes
= bytes_per_sample(s
);
561 if (s
->munge
== NULL
|| (async
->cmd
.flags
& CMDF_RAWDATA
)) {
562 async
->munge_count
+= num_bytes
;
563 BUG_ON((int)(async
->munge_count
- async
->buf_write_count
) > 0);
566 /* don't munge partial samples */
567 num_bytes
-= num_bytes
% num_sample_bytes
;
568 while (count
< num_bytes
) {
571 block_size
= num_bytes
- count
;
572 if (block_size
< 0) {
574 "%s: %s: bug! block_size is negative\n",
578 if ((int)(async
->munge_ptr
+ block_size
-
579 async
->prealloc_bufsz
) > 0)
580 block_size
= async
->prealloc_bufsz
- async
->munge_ptr
;
582 s
->munge(s
->device
, s
, async
->prealloc_buf
+ async
->munge_ptr
,
583 block_size
, async
->munge_chan
);
585 smp_wmb(); /* barrier insures data is munged in buffer
586 * before munge_count is incremented */
588 async
->munge_chan
+= block_size
/ num_sample_bytes
;
589 async
->munge_chan
%= async
->cmd
.chanlist_len
;
590 async
->munge_count
+= block_size
;
591 async
->munge_ptr
+= block_size
;
592 async
->munge_ptr
%= async
->prealloc_bufsz
;
595 BUG_ON((int)(async
->munge_count
- async
->buf_write_count
) > 0);
599 unsigned int comedi_buf_write_n_available(struct comedi_async
*async
)
601 unsigned int free_end
;
607 free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
608 nbytes
= free_end
- async
->buf_write_alloc_count
;
609 nbytes
-= nbytes
% bytes_per_sample(async
->subdevice
);
610 /* barrier insures the read of buf_read_count in this
611 query occurs before any following writes to the buffer which
612 might be based on the return value from this query.
618 /* allocates chunk for the writer from free buffer space */
619 unsigned int comedi_buf_write_alloc(struct 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)
625 nbytes
= free_end
- async
->buf_write_alloc_count
;
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 */
633 EXPORT_SYMBOL(comedi_buf_write_alloc
);
635 /* allocates nothing unless it can completely fulfill the request */
636 unsigned int comedi_buf_write_alloc_strict(struct comedi_async
*async
,
639 unsigned int free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
641 if ((int)(async
->buf_write_alloc_count
+ nbytes
- free_end
) > 0)
644 async
->buf_write_alloc_count
+= nbytes
;
645 /* barrier insures the read of buf_read_count above occurs before
646 we write data to the write-alloc'ed buffer space */
651 /* transfers a chunk from writer to filled buffer space */
652 unsigned comedi_buf_write_free(struct comedi_async
*async
, unsigned int nbytes
)
654 if ((int)(async
->buf_write_count
+ nbytes
-
655 async
->buf_write_alloc_count
) > 0) {
656 printk(KERN_INFO
"comedi: attempted to write-free more bytes than have been write-allocated.\n");
657 nbytes
= async
->buf_write_alloc_count
- async
->buf_write_count
;
659 async
->buf_write_count
+= nbytes
;
660 async
->buf_write_ptr
+= nbytes
;
661 comedi_buf_munge(async
, async
->buf_write_count
- async
->munge_count
);
662 if (async
->buf_write_ptr
>= async
->prealloc_bufsz
)
663 async
->buf_write_ptr
%= async
->prealloc_bufsz
;
667 EXPORT_SYMBOL(comedi_buf_write_free
);
669 /* allocates a chunk for the reader from filled (and munged) buffer space */
670 unsigned comedi_buf_read_alloc(struct comedi_async
*async
, unsigned nbytes
)
672 if ((int)(async
->buf_read_alloc_count
+ nbytes
- async
->munge_count
) >
674 nbytes
= async
->munge_count
- async
->buf_read_alloc_count
;
676 async
->buf_read_alloc_count
+= nbytes
;
677 /* barrier insures read of munge_count occurs before we actually read
678 data out of buffer */
682 EXPORT_SYMBOL(comedi_buf_read_alloc
);
684 /* transfers control of a chunk from reader to free buffer space */
685 unsigned comedi_buf_read_free(struct comedi_async
*async
, unsigned int nbytes
)
687 /* barrier insures data has been read out of
688 * buffer before read count is incremented */
690 if ((int)(async
->buf_read_count
+ nbytes
-
691 async
->buf_read_alloc_count
) > 0) {
693 "comedi: attempted to read-free more bytes than have been read-allocated.\n");
694 nbytes
= async
->buf_read_alloc_count
- async
->buf_read_count
;
696 async
->buf_read_count
+= nbytes
;
697 async
->buf_read_ptr
+= nbytes
;
698 async
->buf_read_ptr
%= async
->prealloc_bufsz
;
701 EXPORT_SYMBOL(comedi_buf_read_free
);
703 void comedi_buf_memcpy_to(struct comedi_async
*async
, unsigned int offset
,
704 const void *data
, unsigned int num_bytes
)
706 unsigned int write_ptr
= async
->buf_write_ptr
+ offset
;
708 if (write_ptr
>= async
->prealloc_bufsz
)
709 write_ptr
%= async
->prealloc_bufsz
;
712 unsigned int block_size
;
714 if (write_ptr
+ num_bytes
> async
->prealloc_bufsz
)
715 block_size
= async
->prealloc_bufsz
- write_ptr
;
717 block_size
= num_bytes
;
719 memcpy(async
->prealloc_buf
+ write_ptr
, data
, block_size
);
722 num_bytes
-= block_size
;
727 EXPORT_SYMBOL(comedi_buf_memcpy_to
);
729 void comedi_buf_memcpy_from(struct comedi_async
*async
, unsigned int offset
,
730 void *dest
, unsigned int nbytes
)
733 unsigned int read_ptr
= async
->buf_read_ptr
+ offset
;
735 if (read_ptr
>= async
->prealloc_bufsz
)
736 read_ptr
%= async
->prealloc_bufsz
;
739 unsigned int block_size
;
741 src
= async
->prealloc_buf
+ read_ptr
;
743 if (nbytes
>= async
->prealloc_bufsz
- read_ptr
)
744 block_size
= async
->prealloc_bufsz
- read_ptr
;
748 memcpy(dest
, src
, block_size
);
749 nbytes
-= block_size
;
754 EXPORT_SYMBOL(comedi_buf_memcpy_from
);
756 unsigned int comedi_buf_read_n_available(struct comedi_async
*async
)
762 num_bytes
= async
->munge_count
- async
->buf_read_count
;
763 /* barrier insures the read of munge_count in this
764 query occurs before any following reads of the buffer which
765 might be based on the return value from this query.
770 EXPORT_SYMBOL(comedi_buf_read_n_available
);
772 int comedi_buf_get(struct comedi_async
*async
, short *x
)
774 unsigned int n
= comedi_buf_read_n_available(async
);
776 if (n
< sizeof(short))
778 comedi_buf_read_alloc(async
, sizeof(short));
779 *x
= *(short *)(async
->prealloc_buf
+ async
->buf_read_ptr
);
780 comedi_buf_read_free(async
, sizeof(short));
783 EXPORT_SYMBOL(comedi_buf_get
);
785 int comedi_buf_put(struct comedi_async
*async
, short x
)
787 unsigned int n
= comedi_buf_write_alloc_strict(async
, sizeof(short));
789 if (n
< sizeof(short)) {
790 async
->events
|= COMEDI_CB_ERROR
;
793 *(short *)(async
->prealloc_buf
+ async
->buf_write_ptr
) = x
;
794 comedi_buf_write_free(async
, sizeof(short));
797 EXPORT_SYMBOL(comedi_buf_put
);
799 void comedi_reset_async_buf(struct comedi_async
*async
)
801 async
->buf_write_alloc_count
= 0;
802 async
->buf_write_count
= 0;
803 async
->buf_read_alloc_count
= 0;
804 async
->buf_read_count
= 0;
806 async
->buf_write_ptr
= 0;
807 async
->buf_read_ptr
= 0;
810 async
->scan_progress
= 0;
811 async
->munge_chan
= 0;
812 async
->munge_count
= 0;
813 async
->munge_ptr
= 0;
819 comedi_auto_config_helper(struct device
*hardware_device
,
820 struct comedi_driver
*driver
,
821 int (*attach_wrapper
) (struct comedi_device
*,
822 void *), void *context
)
825 struct comedi_device_file_info
*dev_file_info
;
826 struct comedi_device
*comedi_dev
;
829 if (!comedi_autoconfig
)
832 minor
= comedi_alloc_board_minor(hardware_device
);
836 dev_file_info
= comedi_get_device_file_info(minor
);
837 comedi_dev
= dev_file_info
->device
;
839 mutex_lock(&comedi_dev
->mutex
);
840 if (comedi_dev
->attached
)
842 else if (!try_module_get(driver
->module
)) {
843 printk(KERN_INFO
"comedi: failed to increment module count\n");
846 /* set comedi_dev->driver here for attach wrapper */
847 comedi_dev
->driver
= driver
;
848 ret
= (*attach_wrapper
)(comedi_dev
, context
);
850 module_put(driver
->module
);
851 __comedi_device_detach(comedi_dev
);
853 ret
= comedi_device_postconfig(comedi_dev
);
856 mutex_unlock(&comedi_dev
->mutex
);
859 comedi_free_board_minor(minor
);
863 static int comedi_auto_config_wrapper(struct comedi_device
*dev
, void *context
)
865 struct comedi_devconfig
*it
= context
;
866 struct comedi_driver
*driv
= dev
->driver
;
868 if (driv
->num_names
) {
869 /* look for generic board entry matching driver name, which
870 * has already been copied to it->board_name */
871 dev
->board_ptr
= comedi_recognize(driv
, it
->board_name
);
872 if (dev
->board_ptr
== NULL
) {
874 "comedi: auto config failed to find board entry"
875 " '%s' for driver '%s'\n", it
->board_name
,
877 comedi_report_boards(driv
);
881 return driv
->attach(dev
, it
);
884 static int comedi_auto_config(struct device
*hardware_device
,
885 struct comedi_driver
*driver
, const int *options
,
886 unsigned num_options
)
888 struct comedi_devconfig it
;
890 memset(&it
, 0, sizeof(it
));
891 strncpy(it
.board_name
, driver
->driver_name
, COMEDI_NAMELEN
);
892 it
.board_name
[COMEDI_NAMELEN
- 1] = '\0';
893 BUG_ON(num_options
> COMEDI_NDEVCONFOPTS
);
894 memcpy(it
.options
, options
, num_options
* sizeof(int));
895 return comedi_auto_config_helper(hardware_device
, driver
,
896 comedi_auto_config_wrapper
, &it
);
899 static void comedi_auto_unconfig(struct device
*hardware_device
)
903 if (hardware_device
== NULL
)
905 minor
= comedi_find_board_minor(hardware_device
);
908 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
909 comedi_free_board_minor(minor
);
912 static int comedi_old_pci_auto_config(struct pci_dev
*pcidev
,
913 struct comedi_driver
*driver
)
918 options
[0] = pcidev
->bus
->number
;
920 options
[1] = PCI_SLOT(pcidev
->devfn
);
922 return comedi_auto_config(&pcidev
->dev
, driver
,
923 options
, ARRAY_SIZE(options
));
926 static int comedi_pci_attach_wrapper(struct comedi_device
*dev
, void *pcidev
)
928 return dev
->driver
->attach_pci(dev
, pcidev
);
931 static int comedi_new_pci_auto_config(struct pci_dev
*pcidev
,
932 struct comedi_driver
*driver
)
934 return comedi_auto_config_helper(&pcidev
->dev
, driver
,
935 comedi_pci_attach_wrapper
, pcidev
);
938 int comedi_pci_auto_config(struct pci_dev
*pcidev
, struct comedi_driver
*driver
)
941 if (driver
->attach_pci
)
942 return comedi_new_pci_auto_config(pcidev
, driver
);
944 return comedi_old_pci_auto_config(pcidev
, driver
);
946 EXPORT_SYMBOL_GPL(comedi_pci_auto_config
);
948 void comedi_pci_auto_unconfig(struct pci_dev
*pcidev
)
950 comedi_auto_unconfig(&pcidev
->dev
);
952 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig
);
954 int comedi_pci_driver_register(struct comedi_driver
*comedi_driver
,
955 struct pci_driver
*pci_driver
)
959 ret
= comedi_driver_register(comedi_driver
);
963 /* FIXME: Remove this test after auditing all comedi pci drivers */
964 if (!pci_driver
->name
)
965 pci_driver
->name
= comedi_driver
->driver_name
;
967 ret
= pci_register_driver(pci_driver
);
969 comedi_driver_unregister(comedi_driver
);
975 EXPORT_SYMBOL_GPL(comedi_pci_driver_register
);
977 void comedi_pci_driver_unregister(struct comedi_driver
*comedi_driver
,
978 struct pci_driver
*pci_driver
)
980 pci_unregister_driver(pci_driver
);
981 comedi_driver_unregister(comedi_driver
);
983 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister
);
985 #if IS_ENABLED(CONFIG_USB)
987 static int comedi_old_usb_auto_config(struct usb_interface
*intf
,
988 struct comedi_driver
*driver
)
990 return comedi_auto_config(&intf
->dev
, driver
, NULL
, 0);
993 static int comedi_usb_attach_wrapper(struct comedi_device
*dev
, void *intf
)
995 return dev
->driver
->attach_usb(dev
, intf
);
998 static int comedi_new_usb_auto_config(struct usb_interface
*intf
,
999 struct comedi_driver
*driver
)
1001 return comedi_auto_config_helper(&intf
->dev
, driver
,
1002 comedi_usb_attach_wrapper
, intf
);
1005 int comedi_usb_auto_config(struct usb_interface
*intf
,
1006 struct comedi_driver
*driver
)
1008 BUG_ON(intf
== NULL
);
1009 if (driver
->attach_usb
)
1010 return comedi_new_usb_auto_config(intf
, driver
);
1012 return comedi_old_usb_auto_config(intf
, driver
);
1014 EXPORT_SYMBOL_GPL(comedi_usb_auto_config
);
1016 void comedi_usb_auto_unconfig(struct usb_interface
*intf
)
1018 BUG_ON(intf
== NULL
);
1019 comedi_auto_unconfig(&intf
->dev
);
1021 EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig
);
1023 int comedi_usb_driver_register(struct comedi_driver
*comedi_driver
,
1024 struct usb_driver
*usb_driver
)
1028 ret
= comedi_driver_register(comedi_driver
);
1032 ret
= usb_register(usb_driver
);
1034 comedi_driver_unregister(comedi_driver
);
1040 EXPORT_SYMBOL_GPL(comedi_usb_driver_register
);
1042 void comedi_usb_driver_unregister(struct comedi_driver
*comedi_driver
,
1043 struct usb_driver
*usb_driver
)
1045 usb_deregister(usb_driver
);
1046 comedi_driver_unregister(comedi_driver
);
1048 EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister
);