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 <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/usb.h>
31 #include <linux/errno.h>
32 #include <linux/kconfig.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 <linux/highmem.h> /* for SuSE brokenness */
41 #include <linux/vmalloc.h>
42 #include <linux/cdev.h>
43 #include <linux/dma-mapping.h>
46 #include "comedidev.h"
47 #include "comedi_internal.h"
49 static int postconfig(struct comedi_device
*dev
);
50 static int insn_rw_emulate_bits(struct comedi_device
*dev
,
51 struct comedi_subdevice
*s
,
52 struct comedi_insn
*insn
, unsigned int *data
);
53 static void *comedi_recognize(struct comedi_driver
*driv
, const char *name
);
54 static void comedi_report_boards(struct comedi_driver
*driv
);
55 static int poll_invalid(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
57 struct comedi_driver
*comedi_drivers
;
59 int comedi_alloc_subdevices(struct comedi_device
*dev
, int num_subdevices
)
61 struct comedi_subdevice
*s
;
64 if (num_subdevices
< 1)
67 s
= kcalloc(num_subdevices
, sizeof(*s
), GFP_KERNEL
);
71 dev
->n_subdevices
= num_subdevices
;
73 for (i
= 0; i
< num_subdevices
; ++i
) {
74 s
= &dev
->subdevices
[i
];
76 s
->async_dma_dir
= DMA_NONE
;
77 spin_lock_init(&s
->spin_lock
);
82 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices
);
84 static void cleanup_device(struct comedi_device
*dev
)
87 struct comedi_subdevice
*s
;
89 if (dev
->subdevices
) {
90 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
91 s
= &dev
->subdevices
[i
];
92 comedi_free_subdevice_minor(s
);
94 comedi_buf_alloc(dev
, s
, 0);
98 kfree(dev
->subdevices
);
99 dev
->subdevices
= NULL
;
100 dev
->n_subdevices
= 0;
105 dev
->board_name
= NULL
;
106 dev
->board_ptr
= NULL
;
109 dev
->read_subdev
= NULL
;
110 dev
->write_subdev
= NULL
;
113 comedi_set_hw_dev(dev
, NULL
);
116 static void __comedi_device_detach(struct comedi_device
*dev
)
120 dev
->driver
->detach(dev
);
122 dev_warn(dev
->class_dev
,
123 "BUG: dev->driver=NULL in comedi_device_detach()\n");
127 void comedi_device_detach(struct comedi_device
*dev
)
131 __comedi_device_detach(dev
);
134 /* do a little post-config cleanup */
135 /* called with module refcount incremented, decrements it */
136 static int comedi_device_postconfig(struct comedi_device
*dev
)
138 int ret
= postconfig(dev
);
139 module_put(dev
->driver
->module
);
141 __comedi_device_detach(dev
);
144 if (!dev
->board_name
) {
145 dev_warn(dev
->class_dev
, "BUG: dev->board_name=NULL\n");
146 dev
->board_name
= "BUG";
153 int comedi_device_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
155 struct comedi_driver
*driv
;
161 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
162 if (!try_module_get(driv
->module
))
164 if (driv
->num_names
) {
165 dev
->board_ptr
= comedi_recognize(driv
, it
->board_name
);
168 } else if (strcmp(driv
->driver_name
, it
->board_name
) == 0)
170 module_put(driv
->module
);
173 /* recognize has failed if we get here */
174 /* report valid board names before returning error */
175 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
176 if (!try_module_get(driv
->module
))
178 comedi_report_boards(driv
);
179 module_put(driv
->module
);
183 if (driv
->attach
== NULL
) {
184 /* driver does not support manual configuration */
185 dev_warn(dev
->class_dev
,
186 "driver '%s' does not support attach using comedi_config\n",
188 module_put(driv
->module
);
191 /* initialize dev->driver here so
192 * comedi_error() can be called from attach */
194 ret
= driv
->attach(dev
, it
);
196 module_put(dev
->driver
->module
);
197 __comedi_device_detach(dev
);
200 return comedi_device_postconfig(dev
);
203 int comedi_driver_register(struct comedi_driver
*driver
)
205 driver
->next
= comedi_drivers
;
206 comedi_drivers
= driver
;
210 EXPORT_SYMBOL(comedi_driver_register
);
212 int comedi_driver_unregister(struct comedi_driver
*driver
)
214 struct comedi_driver
*prev
;
217 /* check for devices using this driver */
218 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++) {
219 struct comedi_device_file_info
*dev_file_info
=
220 comedi_get_device_file_info(i
);
221 struct comedi_device
*dev
;
223 if (dev_file_info
== NULL
)
225 dev
= dev_file_info
->device
;
227 mutex_lock(&dev
->mutex
);
228 if (dev
->attached
&& dev
->driver
== driver
) {
230 dev_warn(dev
->class_dev
,
231 "BUG! detaching device with use_count=%d\n",
233 comedi_device_detach(dev
);
235 mutex_unlock(&dev
->mutex
);
238 if (comedi_drivers
== driver
) {
239 comedi_drivers
= driver
->next
;
243 for (prev
= comedi_drivers
; prev
->next
; prev
= prev
->next
) {
244 if (prev
->next
== driver
) {
245 prev
->next
= driver
->next
;
251 EXPORT_SYMBOL(comedi_driver_unregister
);
253 static int postconfig(struct comedi_device
*dev
)
256 struct comedi_subdevice
*s
;
257 struct comedi_async
*async
= NULL
;
260 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
261 s
= &dev
->subdevices
[i
];
263 if (s
->type
== COMEDI_SUBD_UNUSED
)
266 if (s
->len_chanlist
== 0)
270 unsigned int buf_size
;
272 BUG_ON((s
->subdev_flags
& (SDF_CMD_READ
|
273 SDF_CMD_WRITE
)) == 0);
274 BUG_ON(!s
->do_cmdtest
);
277 kzalloc(sizeof(struct comedi_async
), GFP_KERNEL
);
279 dev_warn(dev
->class_dev
,
280 "failed to allocate async struct\n");
283 init_waitqueue_head(&async
->wait_head
);
284 async
->subdevice
= s
;
288 comedi_default_buf_maxsize_kb
* 1024;
289 buf_size
= comedi_default_buf_size_kb
* 1024;
290 if (buf_size
> async
->max_bufsize
)
291 buf_size
= async
->max_bufsize
;
293 async
->prealloc_buf
= NULL
;
294 async
->prealloc_bufsz
= 0;
295 if (comedi_buf_alloc(dev
, s
, buf_size
) < 0) {
296 dev_warn(dev
->class_dev
,
297 "Buffer allocation failed\n");
301 ret
= s
->buf_change(dev
, s
, buf_size
);
305 comedi_alloc_subdevice_minor(dev
, s
);
308 if (!s
->range_table
&& !s
->range_table_list
)
309 s
->range_table
= &range_unknown
;
311 if (!s
->insn_read
&& s
->insn_bits
)
312 s
->insn_read
= insn_rw_emulate_bits
;
313 if (!s
->insn_write
&& s
->insn_bits
)
314 s
->insn_write
= insn_rw_emulate_bits
;
317 s
->insn_read
= insn_inval
;
319 s
->insn_write
= insn_inval
;
321 s
->insn_bits
= insn_inval
;
323 s
->insn_config
= insn_inval
;
326 s
->poll
= poll_invalid
;
333 * Generic recognize function for drivers that register their supported
336 * 'driv->board_name' points to a 'const char *' member within the
337 * zeroth element of an array of some private board information
338 * structure, say 'struct foo_board' containing a member 'const char
339 * *board_name' that is initialized to point to a board name string that
340 * is one of the candidates matched against this function's 'name'
343 * 'driv->offset' is the size of the private board information
344 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
345 * the length of the array of private board information structures.
347 * If one of the board names in the array of private board information
348 * structures matches the name supplied to this function, the function
349 * returns a pointer to the pointer to the board name, otherwise it
350 * returns NULL. The return value ends up in the 'board_ptr' member of
351 * a 'struct comedi_device' that the low-level comedi driver's
352 * 'attach()' hook can convert to a point to a particular element of its
353 * array of private board information structures by subtracting the
354 * offset of the member that points to the board name. (No subtraction
355 * is required if the board name pointer is the first member of the
356 * private board information structure, which is generally the case.)
358 static void *comedi_recognize(struct comedi_driver
*driv
, const char *name
)
360 char **name_ptr
= (char **)driv
->board_name
;
363 for (i
= 0; i
< driv
->num_names
; i
++) {
364 if (strcmp(*name_ptr
, name
) == 0)
366 name_ptr
= (void *)name_ptr
+ driv
->offset
;
372 static void comedi_report_boards(struct comedi_driver
*driv
)
375 const char *const *name_ptr
;
377 pr_info("comedi: valid board names for %s driver are:\n",
380 name_ptr
= driv
->board_name
;
381 for (i
= 0; i
< driv
->num_names
; i
++) {
382 pr_info(" %s\n", *name_ptr
);
383 name_ptr
= (const char **)((char *)name_ptr
+ driv
->offset
);
386 if (driv
->num_names
== 0)
387 pr_info(" %s\n", driv
->driver_name
);
390 static int poll_invalid(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
395 int insn_inval(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
396 struct comedi_insn
*insn
, unsigned int *data
)
401 static int insn_rw_emulate_bits(struct comedi_device
*dev
,
402 struct comedi_subdevice
*s
,
403 struct comedi_insn
*insn
, unsigned int *data
)
405 struct comedi_insn new_insn
;
407 static const unsigned channels_per_bitfield
= 32;
409 unsigned chan
= CR_CHAN(insn
->chanspec
);
410 const unsigned base_bitfield_channel
=
411 (chan
< channels_per_bitfield
) ? 0 : chan
;
412 unsigned int new_data
[2];
413 memset(new_data
, 0, sizeof(new_data
));
414 memset(&new_insn
, 0, sizeof(new_insn
));
415 new_insn
.insn
= INSN_BITS
;
416 new_insn
.chanspec
= base_bitfield_channel
;
418 new_insn
.subdev
= insn
->subdev
;
420 if (insn
->insn
== INSN_WRITE
) {
421 if (!(s
->subdev_flags
& SDF_WRITABLE
))
423 new_data
[0] = 1 << (chan
- base_bitfield_channel
); /* mask */
424 new_data
[1] = data
[0] ? (1 << (chan
- base_bitfield_channel
))
428 ret
= s
->insn_bits(dev
, s
, &new_insn
, new_data
);
432 if (insn
->insn
== INSN_READ
)
433 data
[0] = (new_data
[1] >> (chan
- base_bitfield_channel
)) & 1;
438 int comedi_buf_alloc(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
439 unsigned long new_size
)
441 struct comedi_async
*async
= s
->async
;
443 /* Round up new_size to multiple of PAGE_SIZE */
444 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
446 /* if no change is required, do nothing */
447 if (async
->prealloc_buf
&& async
->prealloc_bufsz
== new_size
)
450 /* deallocate old buffer */
451 if (async
->prealloc_buf
) {
452 vunmap(async
->prealloc_buf
);
453 async
->prealloc_buf
= NULL
;
454 async
->prealloc_bufsz
= 0;
456 if (async
->buf_page_list
) {
458 for (i
= 0; i
< async
->n_buf_pages
; ++i
) {
459 if (async
->buf_page_list
[i
].virt_addr
) {
460 clear_bit(PG_reserved
,
461 &(virt_to_page(async
->buf_page_list
[i
].
463 if (s
->async_dma_dir
!= DMA_NONE
) {
464 dma_free_coherent(dev
->hw_dev
,
473 free_page((unsigned long)
474 async
->buf_page_list
[i
].
479 vfree(async
->buf_page_list
);
480 async
->buf_page_list
= NULL
;
481 async
->n_buf_pages
= 0;
483 /* allocate new buffer */
486 unsigned n_pages
= new_size
>> PAGE_SHIFT
;
487 struct page
**pages
= NULL
;
489 async
->buf_page_list
=
490 vzalloc(sizeof(struct comedi_buf_page
) * n_pages
);
491 if (async
->buf_page_list
)
492 pages
= vmalloc(sizeof(struct page
*) * n_pages
);
495 for (i
= 0; i
< n_pages
; i
++) {
496 if (s
->async_dma_dir
!= DMA_NONE
) {
497 async
->buf_page_list
[i
].virt_addr
=
498 dma_alloc_coherent(dev
->hw_dev
,
506 async
->buf_page_list
[i
].virt_addr
=
508 get_zeroed_page(GFP_KERNEL
);
510 if (async
->buf_page_list
[i
].virt_addr
== NULL
)
514 &(virt_to_page(async
->buf_page_list
[i
].
516 pages
[i
] = virt_to_page(async
->buf_page_list
[i
].
521 async
->prealloc_buf
=
522 #ifdef PAGE_KERNEL_NOCACHE
523 vmap(pages
, n_pages
, VM_MAP
, PAGE_KERNEL_NOCACHE
);
525 vmap(pages
, n_pages
, VM_MAP
, PAGE_KERNEL
);
530 if (async
->prealloc_buf
== NULL
) {
531 /* Some allocation failed above. */
532 if (async
->buf_page_list
) {
533 for (i
= 0; i
< n_pages
; i
++) {
534 if (async
->buf_page_list
[i
].virt_addr
==
538 clear_bit(PG_reserved
,
539 &(virt_to_page(async
->
542 if (s
->async_dma_dir
!= DMA_NONE
) {
543 dma_free_coherent(dev
->hw_dev
,
552 free_page((unsigned long)
557 vfree(async
->buf_page_list
);
558 async
->buf_page_list
= NULL
;
562 async
->n_buf_pages
= n_pages
;
564 async
->prealloc_bufsz
= new_size
;
569 /* munging is applied to data by core as it passes between user
570 * and kernel space */
571 static unsigned int comedi_buf_munge(struct comedi_async
*async
,
572 unsigned int num_bytes
)
574 struct comedi_subdevice
*s
= async
->subdevice
;
575 unsigned int count
= 0;
576 const unsigned num_sample_bytes
= bytes_per_sample(s
);
578 if (s
->munge
== NULL
|| (async
->cmd
.flags
& CMDF_RAWDATA
)) {
579 async
->munge_count
+= num_bytes
;
580 BUG_ON((int)(async
->munge_count
- async
->buf_write_count
) > 0);
583 /* don't munge partial samples */
584 num_bytes
-= num_bytes
% num_sample_bytes
;
585 while (count
< num_bytes
) {
588 block_size
= num_bytes
- count
;
589 if (block_size
< 0) {
590 dev_warn(s
->device
->class_dev
,
591 "%s: %s: bug! block_size is negative\n",
595 if ((int)(async
->munge_ptr
+ block_size
-
596 async
->prealloc_bufsz
) > 0)
597 block_size
= async
->prealloc_bufsz
- async
->munge_ptr
;
599 s
->munge(s
->device
, s
, async
->prealloc_buf
+ async
->munge_ptr
,
600 block_size
, async
->munge_chan
);
602 smp_wmb(); /* barrier insures data is munged in buffer
603 * before munge_count is incremented */
605 async
->munge_chan
+= block_size
/ num_sample_bytes
;
606 async
->munge_chan
%= async
->cmd
.chanlist_len
;
607 async
->munge_count
+= block_size
;
608 async
->munge_ptr
+= block_size
;
609 async
->munge_ptr
%= async
->prealloc_bufsz
;
612 BUG_ON((int)(async
->munge_count
- async
->buf_write_count
) > 0);
616 unsigned int comedi_buf_write_n_available(struct comedi_async
*async
)
618 unsigned int free_end
;
624 free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
625 nbytes
= free_end
- async
->buf_write_alloc_count
;
626 nbytes
-= nbytes
% bytes_per_sample(async
->subdevice
);
627 /* barrier insures the read of buf_read_count in this
628 query occurs before any following writes to the buffer which
629 might be based on the return value from this query.
635 /* allocates chunk for the writer from free buffer space */
636 unsigned int comedi_buf_write_alloc(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)
642 nbytes
= free_end
- async
->buf_write_alloc_count
;
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 */
650 EXPORT_SYMBOL(comedi_buf_write_alloc
);
652 /* allocates nothing unless it can completely fulfill the request */
653 unsigned int comedi_buf_write_alloc_strict(struct comedi_async
*async
,
656 unsigned int free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
658 if ((int)(async
->buf_write_alloc_count
+ nbytes
- free_end
) > 0)
661 async
->buf_write_alloc_count
+= nbytes
;
662 /* barrier insures the read of buf_read_count above occurs before
663 we write data to the write-alloc'ed buffer space */
668 /* transfers a chunk from writer to filled buffer space */
669 unsigned comedi_buf_write_free(struct comedi_async
*async
, unsigned int nbytes
)
671 if ((int)(async
->buf_write_count
+ nbytes
-
672 async
->buf_write_alloc_count
) > 0) {
673 dev_info(async
->subdevice
->device
->class_dev
,
674 "attempted to write-free more bytes than have been write-allocated.\n");
675 nbytes
= async
->buf_write_alloc_count
- async
->buf_write_count
;
677 async
->buf_write_count
+= nbytes
;
678 async
->buf_write_ptr
+= nbytes
;
679 comedi_buf_munge(async
, async
->buf_write_count
- async
->munge_count
);
680 if (async
->buf_write_ptr
>= async
->prealloc_bufsz
)
681 async
->buf_write_ptr
%= async
->prealloc_bufsz
;
685 EXPORT_SYMBOL(comedi_buf_write_free
);
687 /* allocates a chunk for the reader from filled (and munged) buffer space */
688 unsigned comedi_buf_read_alloc(struct comedi_async
*async
, unsigned nbytes
)
690 if ((int)(async
->buf_read_alloc_count
+ nbytes
- async
->munge_count
) >
692 nbytes
= async
->munge_count
- async
->buf_read_alloc_count
;
694 async
->buf_read_alloc_count
+= nbytes
;
695 /* barrier insures read of munge_count occurs before we actually read
696 data out of buffer */
700 EXPORT_SYMBOL(comedi_buf_read_alloc
);
702 /* transfers control of a chunk from reader to free buffer space */
703 unsigned comedi_buf_read_free(struct comedi_async
*async
, unsigned int nbytes
)
705 /* barrier insures data has been read out of
706 * buffer before read count is incremented */
708 if ((int)(async
->buf_read_count
+ nbytes
-
709 async
->buf_read_alloc_count
) > 0) {
710 dev_info(async
->subdevice
->device
->class_dev
,
711 "attempted to read-free more bytes than have been read-allocated.\n");
712 nbytes
= async
->buf_read_alloc_count
- async
->buf_read_count
;
714 async
->buf_read_count
+= nbytes
;
715 async
->buf_read_ptr
+= nbytes
;
716 async
->buf_read_ptr
%= async
->prealloc_bufsz
;
719 EXPORT_SYMBOL(comedi_buf_read_free
);
721 void comedi_buf_memcpy_to(struct comedi_async
*async
, unsigned int offset
,
722 const void *data
, unsigned int num_bytes
)
724 unsigned int write_ptr
= async
->buf_write_ptr
+ offset
;
726 if (write_ptr
>= async
->prealloc_bufsz
)
727 write_ptr
%= async
->prealloc_bufsz
;
730 unsigned int block_size
;
732 if (write_ptr
+ num_bytes
> async
->prealloc_bufsz
)
733 block_size
= async
->prealloc_bufsz
- write_ptr
;
735 block_size
= num_bytes
;
737 memcpy(async
->prealloc_buf
+ write_ptr
, data
, block_size
);
740 num_bytes
-= block_size
;
745 EXPORT_SYMBOL(comedi_buf_memcpy_to
);
747 void comedi_buf_memcpy_from(struct comedi_async
*async
, unsigned int offset
,
748 void *dest
, unsigned int nbytes
)
751 unsigned int read_ptr
= async
->buf_read_ptr
+ offset
;
753 if (read_ptr
>= async
->prealloc_bufsz
)
754 read_ptr
%= async
->prealloc_bufsz
;
757 unsigned int block_size
;
759 src
= async
->prealloc_buf
+ read_ptr
;
761 if (nbytes
>= async
->prealloc_bufsz
- read_ptr
)
762 block_size
= async
->prealloc_bufsz
- read_ptr
;
766 memcpy(dest
, src
, block_size
);
767 nbytes
-= block_size
;
772 EXPORT_SYMBOL(comedi_buf_memcpy_from
);
774 unsigned int comedi_buf_read_n_available(struct comedi_async
*async
)
780 num_bytes
= async
->munge_count
- async
->buf_read_count
;
781 /* barrier insures the read of munge_count in this
782 query occurs before any following reads of the buffer which
783 might be based on the return value from this query.
788 EXPORT_SYMBOL(comedi_buf_read_n_available
);
790 int comedi_buf_get(struct comedi_async
*async
, short *x
)
792 unsigned int n
= comedi_buf_read_n_available(async
);
794 if (n
< sizeof(short))
796 comedi_buf_read_alloc(async
, sizeof(short));
797 *x
= *(short *)(async
->prealloc_buf
+ async
->buf_read_ptr
);
798 comedi_buf_read_free(async
, sizeof(short));
801 EXPORT_SYMBOL(comedi_buf_get
);
803 int comedi_buf_put(struct comedi_async
*async
, short x
)
805 unsigned int n
= comedi_buf_write_alloc_strict(async
, sizeof(short));
807 if (n
< sizeof(short)) {
808 async
->events
|= COMEDI_CB_ERROR
;
811 *(short *)(async
->prealloc_buf
+ async
->buf_write_ptr
) = x
;
812 comedi_buf_write_free(async
, sizeof(short));
815 EXPORT_SYMBOL(comedi_buf_put
);
817 void comedi_reset_async_buf(struct comedi_async
*async
)
819 async
->buf_write_alloc_count
= 0;
820 async
->buf_write_count
= 0;
821 async
->buf_read_alloc_count
= 0;
822 async
->buf_read_count
= 0;
824 async
->buf_write_ptr
= 0;
825 async
->buf_read_ptr
= 0;
828 async
->scan_progress
= 0;
829 async
->munge_chan
= 0;
830 async
->munge_count
= 0;
831 async
->munge_ptr
= 0;
837 comedi_auto_config_helper(struct device
*hardware_device
,
838 struct comedi_driver
*driver
,
839 int (*attach_wrapper
) (struct comedi_device
*,
840 void *), void *context
)
843 struct comedi_device_file_info
*dev_file_info
;
844 struct comedi_device
*comedi_dev
;
847 if (!comedi_autoconfig
)
850 minor
= comedi_alloc_board_minor(hardware_device
);
854 dev_file_info
= comedi_get_device_file_info(minor
);
855 comedi_dev
= dev_file_info
->device
;
857 mutex_lock(&comedi_dev
->mutex
);
858 if (comedi_dev
->attached
)
860 else if (!try_module_get(driver
->module
))
863 /* set comedi_dev->driver here for attach wrapper */
864 comedi_dev
->driver
= driver
;
865 ret
= (*attach_wrapper
)(comedi_dev
, context
);
867 module_put(driver
->module
);
868 __comedi_device_detach(comedi_dev
);
870 ret
= comedi_device_postconfig(comedi_dev
);
873 mutex_unlock(&comedi_dev
->mutex
);
876 comedi_free_board_minor(minor
);
880 static int comedi_auto_config_wrapper(struct comedi_device
*dev
, void *context
)
882 struct comedi_devconfig
*it
= context
;
883 struct comedi_driver
*driv
= dev
->driver
;
885 if (driv
->num_names
) {
886 /* look for generic board entry matching driver name, which
887 * has already been copied to it->board_name */
888 dev
->board_ptr
= comedi_recognize(driv
, it
->board_name
);
889 if (dev
->board_ptr
== NULL
) {
890 dev_warn(dev
->class_dev
,
891 "auto config failed to find board entry '%s' for driver '%s'\n",
892 it
->board_name
, driv
->driver_name
);
893 comedi_report_boards(driv
);
898 dev_warn(dev
->class_dev
,
899 "BUG! driver '%s' using old-style auto config but has no attach handler\n",
903 return driv
->attach(dev
, it
);
906 static int comedi_auto_config(struct device
*hardware_device
,
907 struct comedi_driver
*driver
, const int *options
,
908 unsigned num_options
)
910 struct comedi_devconfig it
;
912 memset(&it
, 0, sizeof(it
));
913 strncpy(it
.board_name
, driver
->driver_name
, COMEDI_NAMELEN
);
914 it
.board_name
[COMEDI_NAMELEN
- 1] = '\0';
915 BUG_ON(num_options
> COMEDI_NDEVCONFOPTS
);
916 memcpy(it
.options
, options
, num_options
* sizeof(int));
917 return comedi_auto_config_helper(hardware_device
, driver
,
918 comedi_auto_config_wrapper
, &it
);
921 static void comedi_auto_unconfig(struct device
*hardware_device
)
925 if (hardware_device
== NULL
)
927 minor
= comedi_find_board_minor(hardware_device
);
930 BUG_ON(minor
>= COMEDI_NUM_BOARD_MINORS
);
931 comedi_free_board_minor(minor
);
935 * comedi_pci_enable() - Enable the PCI device and request the regions.
936 * @pdev: pci_dev struct
937 * @res_name: name for the requested reqource
939 int comedi_pci_enable(struct pci_dev
*pdev
, const char *res_name
)
943 rc
= pci_enable_device(pdev
);
947 rc
= pci_request_regions(pdev
, res_name
);
949 pci_disable_device(pdev
);
953 EXPORT_SYMBOL_GPL(comedi_pci_enable
);
956 * comedi_pci_disable() - Release the regions and disable the PCI device.
957 * @pdev: pci_dev struct
959 * This must be matched with a previous successful call to comedi_pci_enable().
961 void comedi_pci_disable(struct pci_dev
*pdev
)
963 pci_release_regions(pdev
);
964 pci_disable_device(pdev
);
966 EXPORT_SYMBOL_GPL(comedi_pci_disable
);
968 static int comedi_old_pci_auto_config(struct pci_dev
*pcidev
,
969 struct comedi_driver
*driver
)
974 options
[0] = pcidev
->bus
->number
;
976 options
[1] = PCI_SLOT(pcidev
->devfn
);
978 return comedi_auto_config(&pcidev
->dev
, driver
,
979 options
, ARRAY_SIZE(options
));
982 static int comedi_pci_attach_wrapper(struct comedi_device
*dev
, void *pcidev
)
984 return dev
->driver
->attach_pci(dev
, pcidev
);
987 static int comedi_new_pci_auto_config(struct pci_dev
*pcidev
,
988 struct comedi_driver
*driver
)
990 return comedi_auto_config_helper(&pcidev
->dev
, driver
,
991 comedi_pci_attach_wrapper
, pcidev
);
994 int comedi_pci_auto_config(struct pci_dev
*pcidev
, struct comedi_driver
*driver
)
997 if (driver
->attach_pci
)
998 return comedi_new_pci_auto_config(pcidev
, driver
);
1000 return comedi_old_pci_auto_config(pcidev
, driver
);
1002 EXPORT_SYMBOL_GPL(comedi_pci_auto_config
);
1004 void comedi_pci_auto_unconfig(struct pci_dev
*pcidev
)
1006 comedi_auto_unconfig(&pcidev
->dev
);
1008 EXPORT_SYMBOL_GPL(comedi_pci_auto_unconfig
);
1010 int comedi_pci_driver_register(struct comedi_driver
*comedi_driver
,
1011 struct pci_driver
*pci_driver
)
1015 ret
= comedi_driver_register(comedi_driver
);
1019 /* FIXME: Remove this test after auditing all comedi pci drivers */
1020 if (!pci_driver
->name
)
1021 pci_driver
->name
= comedi_driver
->driver_name
;
1023 ret
= pci_register_driver(pci_driver
);
1025 comedi_driver_unregister(comedi_driver
);
1031 EXPORT_SYMBOL_GPL(comedi_pci_driver_register
);
1033 void comedi_pci_driver_unregister(struct comedi_driver
*comedi_driver
,
1034 struct pci_driver
*pci_driver
)
1036 pci_unregister_driver(pci_driver
);
1037 comedi_driver_unregister(comedi_driver
);
1039 EXPORT_SYMBOL_GPL(comedi_pci_driver_unregister
);
1041 #if IS_ENABLED(CONFIG_USB)
1043 static int comedi_old_usb_auto_config(struct usb_interface
*intf
,
1044 struct comedi_driver
*driver
)
1046 return comedi_auto_config(&intf
->dev
, driver
, NULL
, 0);
1049 static int comedi_usb_attach_wrapper(struct comedi_device
*dev
, void *intf
)
1051 return dev
->driver
->attach_usb(dev
, intf
);
1054 static int comedi_new_usb_auto_config(struct usb_interface
*intf
,
1055 struct comedi_driver
*driver
)
1057 return comedi_auto_config_helper(&intf
->dev
, driver
,
1058 comedi_usb_attach_wrapper
, intf
);
1061 int comedi_usb_auto_config(struct usb_interface
*intf
,
1062 struct comedi_driver
*driver
)
1064 BUG_ON(intf
== NULL
);
1065 if (driver
->attach_usb
)
1066 return comedi_new_usb_auto_config(intf
, driver
);
1068 return comedi_old_usb_auto_config(intf
, driver
);
1070 EXPORT_SYMBOL_GPL(comedi_usb_auto_config
);
1072 void comedi_usb_auto_unconfig(struct usb_interface
*intf
)
1074 BUG_ON(intf
== NULL
);
1075 comedi_auto_unconfig(&intf
->dev
);
1077 EXPORT_SYMBOL_GPL(comedi_usb_auto_unconfig
);
1079 int comedi_usb_driver_register(struct comedi_driver
*comedi_driver
,
1080 struct usb_driver
*usb_driver
)
1084 ret
= comedi_driver_register(comedi_driver
);
1088 ret
= usb_register(usb_driver
);
1090 comedi_driver_unregister(comedi_driver
);
1096 EXPORT_SYMBOL_GPL(comedi_usb_driver_register
);
1098 void comedi_usb_driver_unregister(struct comedi_driver
*comedi_driver
,
1099 struct usb_driver
*usb_driver
)
1101 usb_deregister(usb_driver
);
1102 comedi_driver_unregister(comedi_driver
);
1104 EXPORT_SYMBOL_GPL(comedi_usb_driver_unregister
);