3 Skeleton code for a Comedi driver
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 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.
20 Description: Skeleton driver, an example for driver writers
23 Updated: Mon, 18 Mar 2002 15:34:01 -0800
26 This driver is a documented example on how Comedi drivers are
29 Configuration Options:
34 * The previous block comment is used to automatically generate
35 * documentation in Comedi and Comedilib. The fields:
37 * Driver: the name of the driver
38 * Description: a short phrase describing the driver. Don't list boards.
39 * Devices: a full list of the boards that attempt to be supported by
40 * the driver. Format is "(manufacturer) board name [comedi name]",
41 * where comedi_name is the name that is used to configure the board.
42 * See the comment near board_name: in the struct comedi_driver structure
43 * below. If (manufacturer) or [comedi name] is missing, the previous
46 * Updated: date when the _documentation_ was last updated. Use 'date -R'
47 * to get a value for this.
48 * Status: a one-word description of the status. Valid values are:
49 * works - driver works correctly on most boards supported, and
51 * unknown - unknown. Usually put there by ds.
52 * experimental - may not work in any particular release. Author
53 * probably wants assistance testing it.
54 * bitrotten - driver has not been update in a long time, probably
55 * doesn't work, and probably is missing support for significant
56 * Comedi interface features.
57 * untested - author probably wrote it "blind", and is believed to
58 * work, but no confirmation.
60 * These headers should be followed by a blank line, and any comments
61 * you wish to say about the driver. The comment area is the place
62 * to put any known bugs, limitations, unsupported features, supported
63 * command triggers, whether or not commands are supported on particular
66 * Somewhere in the comment should be information about configuration
67 * options that are used with comedi_config.
70 #include <linux/module.h>
71 #include <linux/pci.h>
73 #include "../comedidev.h"
75 #include "comedi_fc.h"
77 /* Imaginary registers for the imaginary board */
78 #define SKEL_START_AI_CONV 0
79 #define SKEL_AI_READ 0
82 * Board descriptions for two imaginary boards. Describing the
83 * boards in this way is optional, and completely driver-dependent.
84 * Some drivers use arrays such as this, other do not.
98 static const struct skel_board skel_boards
[] = {
112 /* this structure is for data unique to this hardware driver. If
113 several hardware drivers keep similar information in this structure,
114 feel free to suggest moving the variable to the struct comedi_device struct.
116 struct skel_private
{
120 /* Used for AO readback */
121 unsigned int ao_readback
[2];
124 /* This function doesn't require a particular form, this is just
125 * what happens to be used in some of the drivers. It should
126 * convert ns nanoseconds to a counter value suitable for programming
127 * the device. Also, it should adjust ns so that it cooresponds to
128 * the actual time that the device will use. */
129 static int skel_ns_to_timer(unsigned int *ns
, unsigned int flags
)
132 /* if your timing is done through two cascaded timers, the
133 * i8253_cascade_ns_to_timer() function in 8253.h can be
134 * very helpful. There are also i8254_load() and i8254_mm_load()
135 * which can be used to load values into the ubiquitous 8254 counters
142 * This function doesn't require a particular form, this is just
143 * what happens to be used in some of the drivers. The comedi_timeout()
144 * helper uses this callback to check for the end-of-conversion while
145 * waiting for up to 1 second. This function should return 0 when the
146 * conversion is finished and -EBUSY to keep waiting. Any other errno
147 * will terminate comedi_timeout() and return that errno to the caller.
148 * If the timeout occurs, comedi_timeout() will return -ETIMEDOUT.
150 static int skel_ai_eoc(struct comedi_device
*dev
,
151 struct comedi_subdevice
*s
,
152 struct comedi_insn
*insn
,
153 unsigned long context
)
157 /* status = inb(dev->iobase + SKEL_STATUS); */
165 * "instructions" read/write data in "one-shot" or "software-triggered"
168 static int skel_ai_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
169 struct comedi_insn
*insn
, unsigned int *data
)
171 const struct skel_board
*thisboard
= comedi_board(dev
);
176 /* a typical programming sequence */
178 /* write channel to multiplexer */
179 /* outw(chan,dev->iobase + SKEL_MUX); */
181 /* don't wait for mux to settle */
183 /* convert n samples */
184 for (n
= 0; n
< insn
->n
; n
++) {
185 /* trigger conversion */
186 /* outw(0,dev->iobase + SKEL_CONVERT); */
188 /* wait for conversion to end */
189 ret
= comedi_timeout(dev
, s
, insn
, skel_ai_eoc
, 0);
194 /* d = inw(dev->iobase + SKEL_AI_DATA); */
197 /* mangle the data as necessary */
198 d
^= 1 << (thisboard
->ai_bits
- 1);
203 /* return the number of samples read/written */
208 * cmdtest tests a particular command to see if it is valid.
209 * Using the cmdtest ioctl, a user can create a valid cmd
210 * and then have it executes by the cmd ioctl.
212 * cmdtest returns 1,2,3,4 or 0, depending on which tests
213 * the command passes.
215 static int skel_ai_cmdtest(struct comedi_device
*dev
,
216 struct comedi_subdevice
*s
,
217 struct comedi_cmd
*cmd
)
222 /* Step 1 : check if triggers are trivially valid */
224 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
);
225 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
226 TRIG_TIMER
| TRIG_EXT
);
227 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_TIMER
| TRIG_EXT
);
228 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
229 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
234 /* Step 2a : make sure trigger sources are unique */
236 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
237 err
|= cfc_check_trigger_is_unique(cmd
->convert_src
);
238 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
240 /* Step 2b : and mutually compatible */
245 /* Step 3: check if arguments are trivially valid */
247 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
249 #define MAX_SPEED 10000 /* in nanoseconds */
250 #define MIN_SPEED 1000000000 /* in nanoseconds */
252 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
253 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
255 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
258 /* external trigger */
259 /* should be level/edge, hi/lo specification here */
260 /* should specify multiple external triggers */
261 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
, 9);
264 if (cmd
->convert_src
== TRIG_TIMER
) {
265 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
, MAX_SPEED
);
266 err
|= cfc_check_trigger_arg_max(&cmd
->convert_arg
, MIN_SPEED
);
268 /* external trigger */
270 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
, 9);
273 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
275 if (cmd
->stop_src
== TRIG_COUNT
)
276 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, 0x00ffffff);
278 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
283 /* step 4: fix up any arguments */
285 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
286 arg
= cmd
->scan_begin_arg
;
287 skel_ns_to_timer(&arg
, cmd
->flags
);
288 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, arg
);
290 if (cmd
->convert_src
== TRIG_TIMER
) {
291 arg
= cmd
->convert_arg
;
292 skel_ns_to_timer(&arg
, cmd
->flags
);
293 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, arg
);
295 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
296 arg
= cmd
->convert_arg
* cmd
->scan_end_arg
;
297 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
308 static int skel_ao_winsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
309 struct comedi_insn
*insn
, unsigned int *data
)
311 struct skel_private
*devpriv
= dev
->private;
313 int chan
= CR_CHAN(insn
->chanspec
);
315 /* Writing a list of values to an AO channel is probably not
316 * very useful, but that's how the interface is defined. */
317 for (i
= 0; i
< insn
->n
; i
++) {
318 /* a typical programming sequence */
319 /* outw(data[i],dev->iobase + SKEL_DA0 + chan); */
320 devpriv
->ao_readback
[chan
] = data
[i
];
323 /* return the number of samples read/written */
327 /* AO subdevices should have a read insn as well as a write insn.
328 * Usually this means copying a value stored in devpriv. */
329 static int skel_ao_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
330 struct comedi_insn
*insn
, unsigned int *data
)
332 struct skel_private
*devpriv
= dev
->private;
334 int chan
= CR_CHAN(insn
->chanspec
);
336 for (i
= 0; i
< insn
->n
; i
++)
337 data
[i
] = devpriv
->ao_readback
[chan
];
343 * DIO devices are slightly special. Although it is possible to
344 * implement the insn_read/insn_write interface, it is much more
345 * useful to applications if you implement the insn_bits interface.
346 * This allows packed reading/writing of the DIO channels. The
347 * comedi core can convert between insn_bits and insn_read/write.
349 static int skel_dio_insn_bits(struct comedi_device
*dev
,
350 struct comedi_subdevice
*s
,
351 struct comedi_insn
*insn
,
355 * The insn data is a mask in data[0] and the new data
356 * in data[1], each channel cooresponding to a bit.
358 * The core provided comedi_dio_update_state() function can
359 * be used to handle the internal state update to DIO subdevices
360 * with <= 32 channels. This function will return '0' if the
361 * state does not change or the mask of the channels that need
364 if (comedi_dio_update_state(s
, data
)) {
365 /* Write out the new digital output lines */
366 /* outw(s->state, dev->iobase + SKEL_DIO); */
370 * On return, data[1] contains the value of the digital
371 * input and output lines.
373 /* data[1] = inw(dev->iobase + SKEL_DIO); */
376 * Or we could just return the software copy of the output
377 * values if it was a purely digital output subdevice.
379 /* data[1] = s->state; */
384 static int skel_dio_insn_config(struct comedi_device
*dev
,
385 struct comedi_subdevice
*s
,
386 struct comedi_insn
*insn
,
392 * The input or output configuration of each digital line is
393 * configured by special insn_config instructions.
395 * chanspec contains the channel to be changed
396 * data[0] contains the instruction to perform on the channel
398 * Normally the core provided comedi_dio_insn_config() function
399 * can be used to handle the boilerplpate.
401 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, 0);
405 /* Update the hardware to the new configuration */
406 /* outw(s->io_bits, dev->iobase + SKEL_DIO_CONFIG); */
412 * Handle common part of skel_attach() and skel_auto_attach().
414 static int skel_common_attach(struct comedi_device
*dev
)
416 const struct skel_board
*thisboard
= comedi_board(dev
);
417 struct comedi_subdevice
*s
;
420 ret
= comedi_alloc_subdevices(dev
, 3);
424 s
= &dev
->subdevices
[0];
425 /* dev->read_subdev=s; */
426 /* analog input subdevice */
427 s
->type
= COMEDI_SUBD_AI
;
428 /* we support single-ended (ground) and differential */
429 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
| SDF_DIFF
;
430 s
->n_chan
= thisboard
->ai_chans
;
431 s
->maxdata
= (1 << thisboard
->ai_bits
) - 1;
432 s
->range_table
= &range_bipolar10
;
433 s
->len_chanlist
= 16; /* This is the maximum chanlist length that
434 the board can handle */
435 s
->insn_read
= skel_ai_rinsn
;
437 * s->subdev_flags |= SDF_CMD_READ;
438 * s->do_cmd = skel_ai_cmd;
440 s
->do_cmdtest
= skel_ai_cmdtest
;
442 s
= &dev
->subdevices
[1];
443 /* analog output subdevice */
444 s
->type
= COMEDI_SUBD_AO
;
445 s
->subdev_flags
= SDF_WRITABLE
;
448 s
->range_table
= &range_bipolar5
;
449 s
->insn_write
= skel_ao_winsn
;
450 s
->insn_read
= skel_ao_rinsn
;
452 s
= &dev
->subdevices
[2];
453 /* digital i/o subdevice */
454 if (thisboard
->have_dio
) {
455 s
->type
= COMEDI_SUBD_DIO
;
456 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
459 s
->range_table
= &range_digital
;
460 s
->insn_bits
= skel_dio_insn_bits
;
461 s
->insn_config
= skel_dio_insn_config
;
463 s
->type
= COMEDI_SUBD_UNUSED
;
470 * _attach is called by the Comedi core to configure the driver
471 * for a particular board in response to the COMEDI_DEVCONFIG ioctl for
472 * a matching board or driver name. If you specified a board_name array
473 * in the driver structure, dev->board_ptr contains that address.
475 * Drivers that handle only PCI or USB devices do not usually support
476 * manual attachment of those devices via the COMEDI_DEVCONFIG ioctl, so
477 * those drivers do not have an _attach function; they just have an
478 * _auto_attach function instead. (See skel_auto_attach() for an example
479 * of such a function.)
481 static int skel_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
483 const struct skel_board
*thisboard
;
484 struct skel_private
*devpriv
;
487 * If you can probe the device to determine what device in a series
488 * it is, this is the place to do it. Otherwise, dev->board_ptr
489 * should already be initialized.
491 /* dev->board_ptr = skel_probe(dev, it); */
493 thisboard
= comedi_board(dev
);
496 * The dev->board_name is initialized by the comedi core before
497 * calling the (*attach) function. It can be optionally set by
498 * the driver if additional probing has been done.
500 /* dev->board_name = thisboard->name; */
502 /* Allocate the private data */
503 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
508 * Supported boards are usually either auto-attached via the
509 * Comedi driver's _auto_attach routine, or manually attached via the
510 * Comedi driver's _attach routine. In most cases, attempts to
511 * manual attach boards that are usually auto-attached should be
512 * rejected by this function.
515 * if (thisboard->bustype == pci_bustype) {
516 * dev_err(dev->class_dev,
517 * "Manual attachment of PCI board '%s' not supported\n",
523 * For ISA boards, get the i/o base address from it->options[],
524 * request the i/o region and set dev->iobase * from it->options[].
525 * If using interrupts, get the IRQ number from it->options[].
529 * Call a common function to handle the remaining things to do for
530 * attaching ISA or PCI boards. (Extra parameters could be added
531 * to pass additional information such as IRQ number.)
533 return skel_common_attach(dev
);
537 * _auto_attach is called via comedi_pci_auto_config() (or
538 * comedi_usb_auto_config(), etc.) to handle devices that can be attached
539 * to the Comedi core automatically without the COMEDI_DEVCONFIG ioctl.
541 * The context parameter is driver dependent.
543 static int skel_auto_attach(struct comedi_device
*dev
,
544 unsigned long context
)
546 struct pci_dev
*pcidev
= comedi_to_pci_dev(dev
);
547 const struct skel_board
*thisboard
= NULL
;
548 struct skel_private
*devpriv
;
551 /* Hack to allow unused code to be optimized out. */
552 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS
))
556 * In this example, the _auto_attach is for a PCI device.
558 * The 'context' passed to this function is the id->driver_data
559 * associated with the PCI device found in the id_table during
560 * the modprobe. This 'context' is the index of the entry in
561 * skel_boards[i] that contains the boardinfo for the PCI device.
563 if (context
< ARRAY_SIZE(skel_boards
))
564 thisboard
= &skel_boards
[context
];
569 * Point the struct comedi_device to the matching board info
570 * and set the board name.
572 dev
->board_ptr
= thisboard
;
573 dev
->board_name
= thisboard
->name
;
575 /* Allocate the private data */
576 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
580 /* Enable the PCI device. */
581 ret
= comedi_pci_enable(dev
);
586 * Record the fact that the PCI device is enabled so that it can
587 * be disabled during _detach().
589 * For this example driver, we assume PCI BAR 0 is the main I/O
590 * region for the board registers and use dev->iobase to hold the
591 * I/O base address and to indicate that the PCI device has been
594 * (For boards with memory-mapped registers, dev->iobase is not
595 * usually needed for register access, so can just be set to 1
596 * to indicate that the PCI device has been enabled.)
598 dev
->iobase
= pci_resource_start(pcidev
, 0);
601 * Call a common function to handle the remaining things to do for
602 * attaching ISA or PCI boards. (Extra parameters could be added
603 * to pass additional information such as IRQ number.)
605 return skel_common_attach(dev
);
609 * _detach is called to deconfigure a device. It should deallocate
611 * This function is also called when _attach() fails, so it should be
612 * careful not to release resources that were not necessarily
613 * allocated by _attach(). dev->private and dev->subdevices are
614 * deallocated automatically by the core.
616 static void skel_detach(struct comedi_device
*dev
)
618 const struct skel_board
*thisboard
= comedi_board(dev
);
619 struct skel_private
*devpriv
= dev
->private;
621 if (!thisboard
|| !devpriv
)
625 * Do common stuff such as freeing IRQ, unmapping remapped memory
626 * regions, etc., being careful to check that the stuff is valid given
627 * that _detach() is called even when _attach() or _auto_attach() return
631 if (IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS
) /* &&
632 thisboard->bustype == pci_bustype */) {
636 * If PCI device enabled by _auto_attach() (or _attach()),
639 comedi_pci_disable(dev
);
644 * Release the first I/O region requested during the
645 * _attach(). This is safe to call even if the request
646 * failed. If any additional I/O regions are requested
647 * they need to be released by the driver.
649 comedi_legacy_detach(dev
);
654 * The struct comedi_driver structure tells the Comedi core module
655 * which functions to call to configure/deconfigure (attach/detach)
656 * the board, and also about the kernel module that contains
659 static struct comedi_driver skel_driver
= {
660 .driver_name
= "dummy",
661 .module
= THIS_MODULE
,
662 .attach
= skel_attach
,
663 .auto_attach
= skel_auto_attach
,
664 .detach
= skel_detach
,
665 /* It is not necessary to implement the following members if you are
666 * writing a driver for a ISA PnP or PCI card */
667 /* Most drivers will support multiple types of boards by
668 * having an array of board structures. These were defined
669 * in skel_boards[] above. Note that the element 'name'
670 * was first in the structure -- Comedi uses this fact to
671 * extract the name of the board without knowing any details
672 * about the structure except for its length.
673 * When a device is attached (by comedi_config), the name
674 * of the device is given to Comedi, and Comedi tries to
675 * match it by going through the list of board names. If
676 * there is a match, the address of the pointer is put
677 * into dev->board_ptr and driver->attach() is called.
679 * Note that these are not necessary if you can determine
680 * the type of board in software. ISA PnP, PCI, and PCMCIA
681 * devices are such boards.
683 .board_name
= &skel_boards
[0].name
,
684 .offset
= sizeof(struct skel_board
),
685 .num_names
= ARRAY_SIZE(skel_boards
),
688 #ifdef CONFIG_COMEDI_PCI_DRIVERS
690 static int skel_pci_probe(struct pci_dev
*dev
,
691 const struct pci_device_id
*id
)
693 return comedi_pci_auto_config(dev
, &skel_driver
, id
->driver_data
);
697 * Please add your PCI vendor ID to comedidev.h, and it will
698 * be forwarded upstream.
700 #define PCI_VENDOR_ID_SKEL 0xdafe
703 * This is used by modprobe to translate PCI IDs to drivers.
704 * Should only be used for PCI and ISA-PnP devices
706 static const struct pci_device_id skel_pci_table
[] = {
707 { PCI_VDEVICE(SKEL
, 0x0100), BOARD_SKEL100
},
708 { PCI_VDEVICE(SKEL
, 0x0200), BOARD_SKEL200
},
711 MODULE_DEVICE_TABLE(pci
, skel_pci_table
);
713 static struct pci_driver skel_pci_driver
= {
715 .id_table
= skel_pci_table
,
716 .probe
= skel_pci_probe
,
717 .remove
= comedi_pci_auto_unconfig
,
719 module_comedi_pci_driver(skel_driver
, skel_pci_driver
);
721 module_comedi_driver(skel_driver
);
724 MODULE_AUTHOR("Comedi http://www.comedi.org");
725 MODULE_DESCRIPTION("Comedi low-level driver");
726 MODULE_LICENSE("GPL");