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.
24 #include <linux/device.h>
25 #include <linux/module.h>
26 #include <linux/pci.h>
27 #include <linux/usb.h>
28 #include <linux/errno.h>
29 #include <linux/kconfig.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/fcntl.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/highmem.h> /* for SuSE brokenness */
38 #include <linux/vmalloc.h>
39 #include <linux/cdev.h>
40 #include <linux/dma-mapping.h>
43 #include "comedidev.h"
44 #include "comedi_internal.h"
46 static int postconfig(struct comedi_device
*dev
);
47 static int insn_rw_emulate_bits(struct comedi_device
*dev
,
48 struct comedi_subdevice
*s
,
49 struct comedi_insn
*insn
, unsigned int *data
);
50 static void *comedi_recognize(struct comedi_driver
*driv
, const char *name
);
51 static void comedi_report_boards(struct comedi_driver
*driv
);
52 static int poll_invalid(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
54 struct comedi_driver
*comedi_drivers
;
56 int comedi_alloc_subdevices(struct comedi_device
*dev
, int num_subdevices
)
58 struct comedi_subdevice
*s
;
61 if (num_subdevices
< 1)
64 s
= kcalloc(num_subdevices
, sizeof(*s
), GFP_KERNEL
);
68 dev
->n_subdevices
= num_subdevices
;
70 for (i
= 0; i
< num_subdevices
; ++i
) {
71 s
= &dev
->subdevices
[i
];
73 s
->async_dma_dir
= DMA_NONE
;
74 spin_lock_init(&s
->spin_lock
);
79 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices
);
81 static void cleanup_device(struct comedi_device
*dev
)
84 struct comedi_subdevice
*s
;
86 if (dev
->subdevices
) {
87 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
88 s
= &dev
->subdevices
[i
];
89 comedi_free_subdevice_minor(s
);
91 comedi_buf_alloc(dev
, s
, 0);
95 kfree(dev
->subdevices
);
96 dev
->subdevices
= NULL
;
97 dev
->n_subdevices
= 0;
102 dev
->board_name
= NULL
;
103 dev
->board_ptr
= NULL
;
106 dev
->read_subdev
= NULL
;
107 dev
->write_subdev
= NULL
;
110 comedi_set_hw_dev(dev
, NULL
);
113 static void __comedi_device_detach(struct comedi_device
*dev
)
117 dev
->driver
->detach(dev
);
119 dev_warn(dev
->class_dev
,
120 "BUG: dev->driver=NULL in comedi_device_detach()\n");
124 void comedi_device_detach(struct comedi_device
*dev
)
128 __comedi_device_detach(dev
);
131 /* do a little post-config cleanup */
132 /* called with module refcount incremented, decrements it */
133 static int comedi_device_postconfig(struct comedi_device
*dev
)
135 int ret
= postconfig(dev
);
136 module_put(dev
->driver
->module
);
138 __comedi_device_detach(dev
);
141 if (!dev
->board_name
) {
142 dev_warn(dev
->class_dev
, "BUG: dev->board_name=NULL\n");
143 dev
->board_name
= "BUG";
150 int comedi_device_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
152 struct comedi_driver
*driv
;
158 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
159 if (!try_module_get(driv
->module
))
161 if (driv
->num_names
) {
162 dev
->board_ptr
= comedi_recognize(driv
, it
->board_name
);
165 } else if (strcmp(driv
->driver_name
, it
->board_name
) == 0)
167 module_put(driv
->module
);
170 /* recognize has failed if we get here */
171 /* report valid board names before returning error */
172 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
173 if (!try_module_get(driv
->module
))
175 comedi_report_boards(driv
);
176 module_put(driv
->module
);
180 if (driv
->attach
== NULL
) {
181 /* driver does not support manual configuration */
182 dev_warn(dev
->class_dev
,
183 "driver '%s' does not support attach using comedi_config\n",
185 module_put(driv
->module
);
188 /* initialize dev->driver here so
189 * comedi_error() can be called from attach */
191 ret
= driv
->attach(dev
, it
);
193 module_put(dev
->driver
->module
);
194 __comedi_device_detach(dev
);
197 return comedi_device_postconfig(dev
);
200 int comedi_driver_register(struct comedi_driver
*driver
)
202 driver
->next
= comedi_drivers
;
203 comedi_drivers
= driver
;
207 EXPORT_SYMBOL(comedi_driver_register
);
209 int comedi_driver_unregister(struct comedi_driver
*driver
)
211 struct comedi_driver
*prev
;
214 /* check for devices using this driver */
215 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++) {
216 struct comedi_device_file_info
*dev_file_info
=
217 comedi_get_device_file_info(i
);
218 struct comedi_device
*dev
;
220 if (dev_file_info
== NULL
)
222 dev
= dev_file_info
->device
;
224 mutex_lock(&dev
->mutex
);
225 if (dev
->attached
&& dev
->driver
== driver
) {
227 dev_warn(dev
->class_dev
,
228 "BUG! detaching device with use_count=%d\n",
230 comedi_device_detach(dev
);
232 mutex_unlock(&dev
->mutex
);
235 if (comedi_drivers
== driver
) {
236 comedi_drivers
= driver
->next
;
240 for (prev
= comedi_drivers
; prev
->next
; prev
= prev
->next
) {
241 if (prev
->next
== driver
) {
242 prev
->next
= driver
->next
;
248 EXPORT_SYMBOL(comedi_driver_unregister
);
250 static int postconfig(struct comedi_device
*dev
)
253 struct comedi_subdevice
*s
;
254 struct comedi_async
*async
= NULL
;
257 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
258 s
= &dev
->subdevices
[i
];
260 if (s
->type
== COMEDI_SUBD_UNUSED
)
263 if (s
->len_chanlist
== 0)
267 unsigned int buf_size
;
269 BUG_ON((s
->subdev_flags
& (SDF_CMD_READ
|
270 SDF_CMD_WRITE
)) == 0);
271 BUG_ON(!s
->do_cmdtest
);
274 kzalloc(sizeof(struct comedi_async
), GFP_KERNEL
);
276 dev_warn(dev
->class_dev
,
277 "failed to allocate async struct\n");
280 init_waitqueue_head(&async
->wait_head
);
281 async
->subdevice
= s
;
285 comedi_default_buf_maxsize_kb
* 1024;
286 buf_size
= comedi_default_buf_size_kb
* 1024;
287 if (buf_size
> async
->max_bufsize
)
288 buf_size
= async
->max_bufsize
;
290 async
->prealloc_buf
= NULL
;
291 async
->prealloc_bufsz
= 0;
292 if (comedi_buf_alloc(dev
, s
, buf_size
) < 0) {
293 dev_warn(dev
->class_dev
,
294 "Buffer allocation failed\n");
298 ret
= s
->buf_change(dev
, s
, buf_size
);
302 comedi_alloc_subdevice_minor(dev
, s
);
305 if (!s
->range_table
&& !s
->range_table_list
)
306 s
->range_table
= &range_unknown
;
308 if (!s
->insn_read
&& s
->insn_bits
)
309 s
->insn_read
= insn_rw_emulate_bits
;
310 if (!s
->insn_write
&& s
->insn_bits
)
311 s
->insn_write
= insn_rw_emulate_bits
;
314 s
->insn_read
= insn_inval
;
316 s
->insn_write
= insn_inval
;
318 s
->insn_bits
= insn_inval
;
320 s
->insn_config
= insn_inval
;
323 s
->poll
= poll_invalid
;
330 * Generic recognize function for drivers that register their supported
333 * 'driv->board_name' points to a 'const char *' member within the
334 * zeroth element of an array of some private board information
335 * structure, say 'struct foo_board' containing a member 'const char
336 * *board_name' that is initialized to point to a board name string that
337 * is one of the candidates matched against this function's 'name'
340 * 'driv->offset' is the size of the private board information
341 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
342 * the length of the array of private board information structures.
344 * If one of the board names in the array of private board information
345 * structures matches the name supplied to this function, the function
346 * returns a pointer to the pointer to the board name, otherwise it
347 * returns NULL. The return value ends up in the 'board_ptr' member of
348 * a 'struct comedi_device' that the low-level comedi driver's
349 * 'attach()' hook can convert to a point to a particular element of its
350 * array of private board information structures by subtracting the
351 * offset of the member that points to the board name. (No subtraction
352 * is required if the board name pointer is the first member of the
353 * private board information structure, which is generally the case.)
355 static void *comedi_recognize(struct comedi_driver
*driv
, const char *name
)
357 char **name_ptr
= (char **)driv
->board_name
;
360 for (i
= 0; i
< driv
->num_names
; i
++) {
361 if (strcmp(*name_ptr
, name
) == 0)
363 name_ptr
= (void *)name_ptr
+ driv
->offset
;
369 static void comedi_report_boards(struct comedi_driver
*driv
)
372 const char *const *name_ptr
;
374 pr_info("comedi: valid board names for %s driver are:\n",
377 name_ptr
= driv
->board_name
;
378 for (i
= 0; i
< driv
->num_names
; i
++) {
379 pr_info(" %s\n", *name_ptr
);
380 name_ptr
= (const char **)((char *)name_ptr
+ driv
->offset
);
383 if (driv
->num_names
== 0)
384 pr_info(" %s\n", driv
->driver_name
);
387 static int poll_invalid(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
392 int insn_inval(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
393 struct comedi_insn
*insn
, unsigned int *data
)
398 static int insn_rw_emulate_bits(struct comedi_device
*dev
,
399 struct comedi_subdevice
*s
,
400 struct comedi_insn
*insn
, unsigned int *data
)
402 struct comedi_insn new_insn
;
404 static const unsigned channels_per_bitfield
= 32;
406 unsigned chan
= CR_CHAN(insn
->chanspec
);
407 const unsigned base_bitfield_channel
=
408 (chan
< channels_per_bitfield
) ? 0 : chan
;
409 unsigned int new_data
[2];
410 memset(new_data
, 0, sizeof(new_data
));
411 memset(&new_insn
, 0, sizeof(new_insn
));
412 new_insn
.insn
= INSN_BITS
;
413 new_insn
.chanspec
= base_bitfield_channel
;
415 new_insn
.subdev
= insn
->subdev
;
417 if (insn
->insn
== INSN_WRITE
) {
418 if (!(s
->subdev_flags
& SDF_WRITABLE
))
420 new_data
[0] = 1 << (chan
- base_bitfield_channel
); /* mask */
421 new_data
[1] = data
[0] ? (1 << (chan
- base_bitfield_channel
))
425 ret
= s
->insn_bits(dev
, s
, &new_insn
, new_data
);
429 if (insn
->insn
== INSN_READ
)
430 data
[0] = (new_data
[1] >> (chan
- base_bitfield_channel
)) & 1;
435 int comedi_buf_alloc(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
436 unsigned long new_size
)
438 struct comedi_async
*async
= s
->async
;
440 /* Round up new_size to multiple of PAGE_SIZE */
441 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
443 /* if no change is required, do nothing */
444 if (async
->prealloc_buf
&& async
->prealloc_bufsz
== new_size
)
447 /* deallocate old buffer */
448 if (async
->prealloc_buf
) {
449 vunmap(async
->prealloc_buf
);
450 async
->prealloc_buf
= NULL
;
451 async
->prealloc_bufsz
= 0;
453 if (async
->buf_page_list
) {
455 for (i
= 0; i
< async
->n_buf_pages
; ++i
) {
456 if (async
->buf_page_list
[i
].virt_addr
) {
457 clear_bit(PG_reserved
,
458 &(virt_to_page(async
->buf_page_list
[i
].
460 if (s
->async_dma_dir
!= DMA_NONE
) {
461 dma_free_coherent(dev
->hw_dev
,
470 free_page((unsigned long)
471 async
->buf_page_list
[i
].
476 vfree(async
->buf_page_list
);
477 async
->buf_page_list
= NULL
;
478 async
->n_buf_pages
= 0;
480 /* allocate new buffer */
483 unsigned n_pages
= new_size
>> PAGE_SHIFT
;
484 struct page
**pages
= NULL
;
486 async
->buf_page_list
=
487 vzalloc(sizeof(struct comedi_buf_page
) * n_pages
);
488 if (async
->buf_page_list
)
489 pages
= vmalloc(sizeof(struct page
*) * n_pages
);
492 for (i
= 0; i
< n_pages
; i
++) {
493 if (s
->async_dma_dir
!= DMA_NONE
) {
494 async
->buf_page_list
[i
].virt_addr
=
495 dma_alloc_coherent(dev
->hw_dev
,
503 async
->buf_page_list
[i
].virt_addr
=
505 get_zeroed_page(GFP_KERNEL
);
507 if (async
->buf_page_list
[i
].virt_addr
== NULL
)
511 &(virt_to_page(async
->buf_page_list
[i
].
513 pages
[i
] = virt_to_page(async
->buf_page_list
[i
].
518 async
->prealloc_buf
=
519 #ifdef PAGE_KERNEL_NOCACHE
520 vmap(pages
, n_pages
, VM_MAP
, PAGE_KERNEL_NOCACHE
);
522 vmap(pages
, n_pages
, VM_MAP
, PAGE_KERNEL
);
527 if (async
->prealloc_buf
== NULL
) {
528 /* Some allocation failed above. */
529 if (async
->buf_page_list
) {
530 for (i
= 0; i
< n_pages
; i
++) {
531 if (async
->buf_page_list
[i
].virt_addr
==
535 clear_bit(PG_reserved
,
536 &(virt_to_page(async
->
539 if (s
->async_dma_dir
!= DMA_NONE
) {
540 dma_free_coherent(dev
->hw_dev
,
549 free_page((unsigned long)
554 vfree(async
->buf_page_list
);
555 async
->buf_page_list
= NULL
;
559 async
->n_buf_pages
= n_pages
;
561 async
->prealloc_bufsz
= new_size
;
566 /* munging is applied to data by core as it passes between user
567 * and kernel space */
568 static unsigned int comedi_buf_munge(struct comedi_async
*async
,
569 unsigned int num_bytes
)
571 struct comedi_subdevice
*s
= async
->subdevice
;
572 unsigned int count
= 0;
573 const unsigned num_sample_bytes
= bytes_per_sample(s
);
575 if (s
->munge
== NULL
|| (async
->cmd
.flags
& CMDF_RAWDATA
)) {
576 async
->munge_count
+= num_bytes
;
577 BUG_ON((int)(async
->munge_count
- async
->buf_write_count
) > 0);
580 /* don't munge partial samples */
581 num_bytes
-= num_bytes
% num_sample_bytes
;
582 while (count
< num_bytes
) {
585 block_size
= num_bytes
- count
;
586 if (block_size
< 0) {
587 dev_warn(s
->device
->class_dev
,
588 "%s: %s: bug! block_size is negative\n",
592 if ((int)(async
->munge_ptr
+ block_size
-
593 async
->prealloc_bufsz
) > 0)
594 block_size
= async
->prealloc_bufsz
- async
->munge_ptr
;
596 s
->munge(s
->device
, s
, async
->prealloc_buf
+ async
->munge_ptr
,
597 block_size
, async
->munge_chan
);
599 smp_wmb(); /* barrier insures data is munged in buffer
600 * before munge_count is incremented */
602 async
->munge_chan
+= block_size
/ num_sample_bytes
;
603 async
->munge_chan
%= async
->cmd
.chanlist_len
;
604 async
->munge_count
+= block_size
;
605 async
->munge_ptr
+= block_size
;
606 async
->munge_ptr
%= async
->prealloc_bufsz
;
609 BUG_ON((int)(async
->munge_count
- async
->buf_write_count
) > 0);
613 unsigned int comedi_buf_write_n_available(struct comedi_async
*async
)
615 unsigned int free_end
;
621 free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
622 nbytes
= free_end
- async
->buf_write_alloc_count
;
623 nbytes
-= nbytes
% bytes_per_sample(async
->subdevice
);
624 /* barrier insures the read of buf_read_count in this
625 query occurs before any following writes to the buffer which
626 might be based on the return value from this query.
632 /* allocates chunk for the writer from free buffer space */
633 unsigned int comedi_buf_write_alloc(struct comedi_async
*async
,
636 unsigned int free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
638 if ((int)(async
->buf_write_alloc_count
+ nbytes
- free_end
) > 0)
639 nbytes
= free_end
- async
->buf_write_alloc_count
;
641 async
->buf_write_alloc_count
+= nbytes
;
642 /* barrier insures the read of buf_read_count above occurs before
643 we write data to the write-alloc'ed buffer space */
647 EXPORT_SYMBOL(comedi_buf_write_alloc
);
649 /* allocates nothing unless it can completely fulfill the request */
650 unsigned int comedi_buf_write_alloc_strict(struct comedi_async
*async
,
653 unsigned int free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
655 if ((int)(async
->buf_write_alloc_count
+ nbytes
- free_end
) > 0)
658 async
->buf_write_alloc_count
+= nbytes
;
659 /* barrier insures the read of buf_read_count above occurs before
660 we write data to the write-alloc'ed buffer space */
665 /* transfers a chunk from writer to filled buffer space */
666 unsigned comedi_buf_write_free(struct comedi_async
*async
, unsigned int nbytes
)
668 if ((int)(async
->buf_write_count
+ nbytes
-
669 async
->buf_write_alloc_count
) > 0) {
670 dev_info(async
->subdevice
->device
->class_dev
,
671 "attempted to write-free more bytes than have been write-allocated.\n");
672 nbytes
= async
->buf_write_alloc_count
- async
->buf_write_count
;
674 async
->buf_write_count
+= nbytes
;
675 async
->buf_write_ptr
+= nbytes
;
676 comedi_buf_munge(async
, async
->buf_write_count
- async
->munge_count
);
677 if (async
->buf_write_ptr
>= async
->prealloc_bufsz
)
678 async
->buf_write_ptr
%= async
->prealloc_bufsz
;
682 EXPORT_SYMBOL(comedi_buf_write_free
);
684 /* allocates a chunk for the reader from filled (and munged) buffer space */
685 unsigned comedi_buf_read_alloc(struct comedi_async
*async
, unsigned nbytes
)
687 if ((int)(async
->buf_read_alloc_count
+ nbytes
- async
->munge_count
) >
689 nbytes
= async
->munge_count
- async
->buf_read_alloc_count
;
691 async
->buf_read_alloc_count
+= nbytes
;
692 /* barrier insures read of munge_count occurs before we actually read
693 data out of buffer */
697 EXPORT_SYMBOL(comedi_buf_read_alloc
);
699 /* transfers control of a chunk from reader to free buffer space */
700 unsigned comedi_buf_read_free(struct comedi_async
*async
, unsigned int nbytes
)
702 /* barrier insures data has been read out of
703 * buffer before read count is incremented */
705 if ((int)(async
->buf_read_count
+ nbytes
-
706 async
->buf_read_alloc_count
) > 0) {
707 dev_info(async
->subdevice
->device
->class_dev
,
708 "attempted to read-free more bytes than have been read-allocated.\n");
709 nbytes
= async
->buf_read_alloc_count
- async
->buf_read_count
;
711 async
->buf_read_count
+= nbytes
;
712 async
->buf_read_ptr
+= nbytes
;
713 async
->buf_read_ptr
%= async
->prealloc_bufsz
;
716 EXPORT_SYMBOL(comedi_buf_read_free
);
718 void comedi_buf_memcpy_to(struct comedi_async
*async
, unsigned int offset
,
719 const void *data
, unsigned int num_bytes
)
721 unsigned int write_ptr
= async
->buf_write_ptr
+ offset
;
723 if (write_ptr
>= async
->prealloc_bufsz
)
724 write_ptr
%= async
->prealloc_bufsz
;
727 unsigned int block_size
;
729 if (write_ptr
+ num_bytes
> async
->prealloc_bufsz
)
730 block_size
= async
->prealloc_bufsz
- write_ptr
;
732 block_size
= num_bytes
;
734 memcpy(async
->prealloc_buf
+ write_ptr
, data
, block_size
);
737 num_bytes
-= block_size
;
742 EXPORT_SYMBOL(comedi_buf_memcpy_to
);
744 void comedi_buf_memcpy_from(struct comedi_async
*async
, unsigned int offset
,
745 void *dest
, unsigned int nbytes
)
748 unsigned int read_ptr
= async
->buf_read_ptr
+ offset
;
750 if (read_ptr
>= async
->prealloc_bufsz
)
751 read_ptr
%= async
->prealloc_bufsz
;
754 unsigned int block_size
;
756 src
= async
->prealloc_buf
+ read_ptr
;
758 if (nbytes
>= async
->prealloc_bufsz
- read_ptr
)
759 block_size
= async
->prealloc_bufsz
- read_ptr
;
763 memcpy(dest
, src
, block_size
);
764 nbytes
-= block_size
;
769 EXPORT_SYMBOL(comedi_buf_memcpy_from
);
771 unsigned int comedi_buf_read_n_available(struct comedi_async
*async
)
777 num_bytes
= async
->munge_count
- async
->buf_read_count
;
778 /* barrier insures the read of munge_count in this
779 query occurs before any following reads of the buffer which
780 might be based on the return value from this query.
785 EXPORT_SYMBOL(comedi_buf_read_n_available
);
787 int comedi_buf_get(struct comedi_async
*async
, short *x
)
789 unsigned int n
= comedi_buf_read_n_available(async
);
791 if (n
< sizeof(short))
793 comedi_buf_read_alloc(async
, sizeof(short));
794 *x
= *(short *)(async
->prealloc_buf
+ async
->buf_read_ptr
);
795 comedi_buf_read_free(async
, sizeof(short));
798 EXPORT_SYMBOL(comedi_buf_get
);
800 int comedi_buf_put(struct comedi_async
*async
, short x
)
802 unsigned int n
= comedi_buf_write_alloc_strict(async
, sizeof(short));
804 if (n
< sizeof(short)) {
805 async
->events
|= COMEDI_CB_ERROR
;
808 *(short *)(async
->prealloc_buf
+ async
->buf_write_ptr
) = x
;
809 comedi_buf_write_free(async
, sizeof(short));
812 EXPORT_SYMBOL(comedi_buf_put
);
814 void comedi_reset_async_buf(struct comedi_async
*async
)
816 async
->buf_write_alloc_count
= 0;
817 async
->buf_write_count
= 0;
818 async
->buf_read_alloc_count
= 0;
819 async
->buf_read_count
= 0;
821 async
->buf_write_ptr
= 0;
822 async
->buf_read_ptr
= 0;
825 async
->scan_progress
= 0;
826 async
->munge_chan
= 0;
827 async
->munge_count
= 0;
828 async
->munge_ptr
= 0;
833 int comedi_auto_config(struct device
*hardware_device
,
834 struct comedi_driver
*driver
, unsigned long context
)
837 struct comedi_device_file_info
*dev_file_info
;
838 struct comedi_device
*comedi_dev
;
841 if (!comedi_autoconfig
)
844 if (!driver
->auto_attach
) {
845 dev_warn(hardware_device
,
846 "BUG! comedi driver '%s' has no auto_attach handler\n",
847 driver
->driver_name
);
851 minor
= comedi_alloc_board_minor(hardware_device
);
855 dev_file_info
= comedi_get_device_file_info(minor
);
856 comedi_dev
= dev_file_info
->device
;
858 mutex_lock(&comedi_dev
->mutex
);
859 if (comedi_dev
->attached
)
861 else if (!try_module_get(driver
->module
))
864 comedi_set_hw_dev(comedi_dev
, hardware_device
);
865 comedi_dev
->driver
= driver
;
866 ret
= driver
->auto_attach(comedi_dev
, context
);
868 module_put(driver
->module
);
869 __comedi_device_detach(comedi_dev
);
871 ret
= comedi_device_postconfig(comedi_dev
);
874 mutex_unlock(&comedi_dev
->mutex
);
877 comedi_free_board_minor(minor
);
880 EXPORT_SYMBOL_GPL(comedi_auto_config
);
882 void comedi_auto_unconfig(struct device
*hardware_device
)
886 if (hardware_device
== NULL
)
888 minor
= comedi_find_board_minor(hardware_device
);
891 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
892 comedi_free_board_minor(minor
);
894 EXPORT_SYMBOL_GPL(comedi_auto_unconfig
);
897 * comedi_pci_enable() - Enable the PCI device and request the regions.
898 * @pdev: pci_dev struct
899 * @res_name: name for the requested reqource
901 int comedi_pci_enable(struct pci_dev
*pdev
, const char *res_name
)
905 rc
= pci_enable_device(pdev
);
909 rc
= pci_request_regions(pdev
, res_name
);
911 pci_disable_device(pdev
);
915 EXPORT_SYMBOL_GPL(comedi_pci_enable
);
918 * comedi_pci_disable() - Release the regions and disable the PCI device.
919 * @pdev: pci_dev struct
921 * This must be matched with a previous successful call to comedi_pci_enable().
923 void comedi_pci_disable(struct pci_dev
*pdev
)
925 pci_release_regions(pdev
);
926 pci_disable_device(pdev
);
928 EXPORT_SYMBOL_GPL(comedi_pci_disable
);
930 int comedi_pci_driver_register(struct comedi_driver
*comedi_driver
,
931 struct pci_driver
*pci_driver
)
935 ret
= comedi_driver_register(comedi_driver
);
939 /* FIXME: Remove this test after auditing all comedi pci drivers */
940 if (!pci_driver
->name
)
941 pci_driver
->name
= comedi_driver
->driver_name
;
943 ret
= pci_register_driver(pci_driver
);
945 comedi_driver_unregister(comedi_driver
);
951 EXPORT_SYMBOL_GPL(comedi_pci_driver_register
);
953 void comedi_pci_driver_unregister(struct comedi_driver
*comedi_driver
,
954 struct pci_driver
*pci_driver
)
956 pci_unregister_driver(pci_driver
);
957 comedi_driver_unregister(comedi_driver
);
959 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister
);
961 #if IS_ENABLED(CONFIG_USB)
963 int comedi_usb_driver_register(struct comedi_driver
*comedi_driver
,
964 struct usb_driver
*usb_driver
)
968 ret
= comedi_driver_register(comedi_driver
);
972 ret
= usb_register(usb_driver
);
974 comedi_driver_unregister(comedi_driver
);
980 EXPORT_SYMBOL_GPL(comedi_usb_driver_register
);
982 void comedi_usb_driver_unregister(struct comedi_driver
*comedi_driver
,
983 struct usb_driver
*usb_driver
)
985 usb_deregister(usb_driver
);
986 comedi_driver_unregister(comedi_driver
);
988 EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister
);