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/errno.h>
27 #include <linux/kconfig.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/fcntl.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
34 #include <linux/slab.h>
35 #include <linux/highmem.h> /* for SuSE brokenness */
36 #include <linux/vmalloc.h>
37 #include <linux/cdev.h>
38 #include <linux/dma-mapping.h>
40 #include <linux/interrupt.h>
42 #include "comedidev.h"
43 #include "comedi_internal.h"
45 struct comedi_driver
*comedi_drivers
;
47 int comedi_set_hw_dev(struct comedi_device
*dev
, struct device
*hw_dev
)
49 if (hw_dev
== dev
->hw_dev
)
51 if (dev
->hw_dev
!= NULL
)
53 dev
->hw_dev
= get_device(hw_dev
);
56 EXPORT_SYMBOL_GPL(comedi_set_hw_dev
);
58 static void comedi_clear_hw_dev(struct comedi_device
*dev
)
60 put_device(dev
->hw_dev
);
64 int comedi_alloc_subdevices(struct comedi_device
*dev
, int num_subdevices
)
66 struct comedi_subdevice
*s
;
69 if (num_subdevices
< 1)
72 s
= kcalloc(num_subdevices
, sizeof(*s
), GFP_KERNEL
);
76 dev
->n_subdevices
= num_subdevices
;
78 for (i
= 0; i
< num_subdevices
; ++i
) {
79 s
= &dev
->subdevices
[i
];
82 s
->async_dma_dir
= DMA_NONE
;
83 spin_lock_init(&s
->spin_lock
);
88 EXPORT_SYMBOL_GPL(comedi_alloc_subdevices
);
90 void comedi_spriv_free(struct comedi_device
*dev
, int subdev_num
)
92 struct comedi_subdevice
*s
;
94 if (dev
->subdevices
&& subdev_num
< dev
->n_subdevices
) {
95 s
= &dev
->subdevices
[subdev_num
];
100 EXPORT_SYMBOL_GPL(comedi_spriv_free
);
102 static void cleanup_device(struct comedi_device
*dev
)
105 struct comedi_subdevice
*s
;
107 if (dev
->subdevices
) {
108 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
109 s
= &dev
->subdevices
[i
];
110 comedi_free_subdevice_minor(s
);
112 comedi_buf_alloc(dev
, s
, 0);
116 kfree(dev
->subdevices
);
117 dev
->subdevices
= NULL
;
118 dev
->n_subdevices
= 0;
123 dev
->board_name
= NULL
;
124 dev
->board_ptr
= NULL
;
127 dev
->ioenabled
= false;
129 dev
->read_subdev
= NULL
;
130 dev
->write_subdev
= NULL
;
133 comedi_clear_hw_dev(dev
);
136 void comedi_device_detach(struct comedi_device
*dev
)
138 dev
->attached
= false;
140 dev
->driver
->detach(dev
);
144 static int poll_invalid(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
149 int insn_inval(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
150 struct comedi_insn
*insn
, unsigned int *data
)
155 static int insn_rw_emulate_bits(struct comedi_device
*dev
,
156 struct comedi_subdevice
*s
,
157 struct comedi_insn
*insn
, unsigned int *data
)
159 struct comedi_insn new_insn
;
161 static const unsigned channels_per_bitfield
= 32;
163 unsigned chan
= CR_CHAN(insn
->chanspec
);
164 const unsigned base_bitfield_channel
=
165 (chan
< channels_per_bitfield
) ? 0 : chan
;
166 unsigned int new_data
[2];
167 memset(new_data
, 0, sizeof(new_data
));
168 memset(&new_insn
, 0, sizeof(new_insn
));
169 new_insn
.insn
= INSN_BITS
;
170 new_insn
.chanspec
= base_bitfield_channel
;
172 new_insn
.subdev
= insn
->subdev
;
174 if (insn
->insn
== INSN_WRITE
) {
175 if (!(s
->subdev_flags
& SDF_WRITABLE
))
177 new_data
[0] = 1 << (chan
- base_bitfield_channel
); /* mask */
178 new_data
[1] = data
[0] ? (1 << (chan
- base_bitfield_channel
))
182 ret
= s
->insn_bits(dev
, s
, &new_insn
, new_data
);
186 if (insn
->insn
== INSN_READ
)
187 data
[0] = (new_data
[1] >> (chan
- base_bitfield_channel
)) & 1;
192 static int __comedi_device_postconfig_async(struct comedi_device
*dev
,
193 struct comedi_subdevice
*s
)
195 struct comedi_async
*async
;
196 unsigned int buf_size
;
199 if ((s
->subdev_flags
& (SDF_CMD_READ
| SDF_CMD_WRITE
)) == 0) {
200 dev_warn(dev
->class_dev
,
201 "async subdevices must support SDF_CMD_READ or SDF_CMD_WRITE\n");
204 if (!s
->do_cmdtest
) {
205 dev_warn(dev
->class_dev
,
206 "async subdevices must have a do_cmdtest() function\n");
210 async
= kzalloc(sizeof(*async
), GFP_KERNEL
);
214 init_waitqueue_head(&async
->wait_head
);
215 async
->subdevice
= s
;
218 async
->max_bufsize
= comedi_default_buf_maxsize_kb
* 1024;
219 buf_size
= comedi_default_buf_size_kb
* 1024;
220 if (buf_size
> async
->max_bufsize
)
221 buf_size
= async
->max_bufsize
;
223 if (comedi_buf_alloc(dev
, s
, buf_size
) < 0) {
224 dev_warn(dev
->class_dev
, "Buffer allocation failed\n");
228 ret
= s
->buf_change(dev
, s
, buf_size
);
233 comedi_alloc_subdevice_minor(s
);
238 static int __comedi_device_postconfig(struct comedi_device
*dev
)
240 struct comedi_subdevice
*s
;
244 for (i
= 0; i
< dev
->n_subdevices
; i
++) {
245 s
= &dev
->subdevices
[i
];
247 if (s
->type
== COMEDI_SUBD_UNUSED
)
250 if (s
->len_chanlist
== 0)
254 ret
= __comedi_device_postconfig_async(dev
, s
);
259 if (!s
->range_table
&& !s
->range_table_list
)
260 s
->range_table
= &range_unknown
;
262 if (!s
->insn_read
&& s
->insn_bits
)
263 s
->insn_read
= insn_rw_emulate_bits
;
264 if (!s
->insn_write
&& s
->insn_bits
)
265 s
->insn_write
= insn_rw_emulate_bits
;
268 s
->insn_read
= insn_inval
;
270 s
->insn_write
= insn_inval
;
272 s
->insn_bits
= insn_inval
;
274 s
->insn_config
= insn_inval
;
277 s
->poll
= poll_invalid
;
283 /* do a little post-config cleanup */
284 static int comedi_device_postconfig(struct comedi_device
*dev
)
288 ret
= __comedi_device_postconfig(dev
);
292 dev
->attached
= true;
297 * Generic recognize function for drivers that register their supported
300 * 'driv->board_name' points to a 'const char *' member within the
301 * zeroth element of an array of some private board information
302 * structure, say 'struct foo_board' containing a member 'const char
303 * *board_name' that is initialized to point to a board name string that
304 * is one of the candidates matched against this function's 'name'
307 * 'driv->offset' is the size of the private board information
308 * structure, say 'sizeof(struct foo_board)', and 'driv->num_names' is
309 * the length of the array of private board information structures.
311 * If one of the board names in the array of private board information
312 * structures matches the name supplied to this function, the function
313 * returns a pointer to the pointer to the board name, otherwise it
314 * returns NULL. The return value ends up in the 'board_ptr' member of
315 * a 'struct comedi_device' that the low-level comedi driver's
316 * 'attach()' hook can convert to a point to a particular element of its
317 * array of private board information structures by subtracting the
318 * offset of the member that points to the board name. (No subtraction
319 * is required if the board name pointer is the first member of the
320 * private board information structure, which is generally the case.)
322 static void *comedi_recognize(struct comedi_driver
*driv
, const char *name
)
324 char **name_ptr
= (char **)driv
->board_name
;
327 for (i
= 0; i
< driv
->num_names
; i
++) {
328 if (strcmp(*name_ptr
, name
) == 0)
330 name_ptr
= (void *)name_ptr
+ driv
->offset
;
336 static void comedi_report_boards(struct comedi_driver
*driv
)
339 const char *const *name_ptr
;
341 pr_info("comedi: valid board names for %s driver are:\n",
344 name_ptr
= driv
->board_name
;
345 for (i
= 0; i
< driv
->num_names
; i
++) {
346 pr_info(" %s\n", *name_ptr
);
347 name_ptr
= (const char **)((char *)name_ptr
+ driv
->offset
);
350 if (driv
->num_names
== 0)
351 pr_info(" %s\n", driv
->driver_name
);
355 * __comedi_request_region() - Request an I/O reqion for a legacy driver.
356 * @dev: comedi_device struct
357 * @start: base address of the I/O reqion
358 * @len: length of the I/O region
360 int __comedi_request_region(struct comedi_device
*dev
,
361 unsigned long start
, unsigned long len
)
364 dev_warn(dev
->class_dev
,
365 "%s: a I/O base address must be specified\n",
370 if (!request_region(start
, len
, dev
->board_name
)) {
371 dev_warn(dev
->class_dev
, "%s: I/O port conflict (%#lx,%lu)\n",
372 dev
->board_name
, start
, len
);
378 EXPORT_SYMBOL_GPL(__comedi_request_region
);
381 * comedi_request_region() - Request an I/O reqion for a legacy driver.
382 * @dev: comedi_device struct
383 * @start: base address of the I/O reqion
384 * @len: length of the I/O region
386 int comedi_request_region(struct comedi_device
*dev
,
387 unsigned long start
, unsigned long len
)
391 ret
= __comedi_request_region(dev
, start
, len
);
399 EXPORT_SYMBOL_GPL(comedi_request_region
);
402 * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
403 * @dev: comedi_device struct
405 void comedi_legacy_detach(struct comedi_device
*dev
)
408 free_irq(dev
->irq
, dev
);
411 if (dev
->iobase
&& dev
->iolen
) {
412 release_region(dev
->iobase
, dev
->iolen
);
417 EXPORT_SYMBOL_GPL(comedi_legacy_detach
);
419 int comedi_device_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
421 struct comedi_driver
*driv
;
427 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
428 if (!try_module_get(driv
->module
))
430 if (driv
->num_names
) {
431 dev
->board_ptr
= comedi_recognize(driv
, it
->board_name
);
434 } else if (strcmp(driv
->driver_name
, it
->board_name
) == 0)
436 module_put(driv
->module
);
439 /* recognize has failed if we get here */
440 /* report valid board names before returning error */
441 for (driv
= comedi_drivers
; driv
; driv
= driv
->next
) {
442 if (!try_module_get(driv
->module
))
444 comedi_report_boards(driv
);
445 module_put(driv
->module
);
449 if (driv
->attach
== NULL
) {
450 /* driver does not support manual configuration */
451 dev_warn(dev
->class_dev
,
452 "driver '%s' does not support attach using comedi_config\n",
454 module_put(driv
->module
);
457 /* initialize dev->driver here so
458 * comedi_error() can be called from attach */
460 dev
->board_name
= dev
->board_ptr
? *(const char **)dev
->board_ptr
461 : dev
->driver
->driver_name
;
462 ret
= driv
->attach(dev
, it
);
464 ret
= comedi_device_postconfig(dev
);
466 comedi_device_detach(dev
);
467 module_put(driv
->module
);
469 /* On success, the driver module count has been incremented. */
473 int comedi_auto_config(struct device
*hardware_device
,
474 struct comedi_driver
*driver
, unsigned long context
)
476 struct comedi_device
*dev
;
479 if (!hardware_device
) {
480 pr_warn("BUG! comedi_auto_config called with NULL hardware_device\n");
484 dev_warn(hardware_device
,
485 "BUG! comedi_auto_config called with NULL comedi driver\n");
489 if (!driver
->auto_attach
) {
490 dev_warn(hardware_device
,
491 "BUG! comedi driver '%s' has no auto_attach handler\n",
492 driver
->driver_name
);
496 dev
= comedi_alloc_board_minor(hardware_device
);
499 /* Note: comedi_alloc_board_minor() locked dev->mutex. */
501 dev
->driver
= driver
;
502 dev
->board_name
= dev
->driver
->driver_name
;
503 ret
= driver
->auto_attach(dev
, context
);
505 ret
= comedi_device_postconfig(dev
);
507 comedi_device_detach(dev
);
508 mutex_unlock(&dev
->mutex
);
511 comedi_release_hardware_device(hardware_device
);
514 EXPORT_SYMBOL_GPL(comedi_auto_config
);
516 void comedi_auto_unconfig(struct device
*hardware_device
)
518 if (hardware_device
== NULL
)
520 comedi_release_hardware_device(hardware_device
);
522 EXPORT_SYMBOL_GPL(comedi_auto_unconfig
);
524 int comedi_driver_register(struct comedi_driver
*driver
)
526 driver
->next
= comedi_drivers
;
527 comedi_drivers
= driver
;
531 EXPORT_SYMBOL_GPL(comedi_driver_register
);
533 int comedi_driver_unregister(struct comedi_driver
*driver
)
535 struct comedi_driver
*prev
;
538 /* check for devices using this driver */
539 for (i
= 0; i
< COMEDI_NUM_BOARD_MINORS
; i
++) {
540 struct comedi_device
*dev
= comedi_dev_from_minor(i
);
545 mutex_lock(&dev
->mutex
);
546 if (dev
->attached
&& dev
->driver
== driver
) {
548 dev_warn(dev
->class_dev
,
549 "BUG! detaching device with use_count=%d\n",
551 comedi_device_detach(dev
);
553 mutex_unlock(&dev
->mutex
);
556 if (comedi_drivers
== driver
) {
557 comedi_drivers
= driver
->next
;
561 for (prev
= comedi_drivers
; prev
->next
; prev
= prev
->next
) {
562 if (prev
->next
== driver
) {
563 prev
->next
= driver
->next
;
569 EXPORT_SYMBOL_GPL(comedi_driver_unregister
);