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.
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.
25 Description: Skeleton driver, an example for driver writers
28 Updated: Mon, 18 Mar 2002 15:34:01 -0800
31 This driver is a documented example on how Comedi drivers are
34 Configuration Options:
39 * The previous block comment is used to automatically generate
40 * documentation in Comedi and Comedilib. The fields:
42 * Driver: the name of the driver
43 * Description: a short phrase describing the driver. Don't list boards.
44 * Devices: a full list of the boards that attempt to be supported by
45 * the driver. Format is "(manufacturer) board name [comedi name]",
46 * where comedi_name is the name that is used to configure the board.
47 * See the comment near board_name: in the struct comedi_driver structure
48 * below. If (manufacturer) or [comedi name] is missing, the previous
51 * Updated: date when the _documentation_ was last updated. Use 'date -R'
52 * to get a value for this.
53 * Status: a one-word description of the status. Valid values are:
54 * works - driver works correctly on most boards supported, and
56 * unknown - unknown. Usually put there by ds.
57 * experimental - may not work in any particular release. Author
58 * probably wants assistance testing it.
59 * bitrotten - driver has not been update in a long time, probably
60 * doesn't work, and probably is missing support for significant
61 * Comedi interface features.
62 * untested - author probably wrote it "blind", and is believed to
63 * work, but no confirmation.
65 * These headers should be followed by a blank line, and any comments
66 * you wish to say about the driver. The comment area is the place
67 * to put any known bugs, limitations, unsupported features, supported
68 * command triggers, whether or not commands are supported on particular
71 * Somewhere in the comment should be information about configuration
72 * options that are used with comedi_config.
75 #include <linux/pci.h>
77 #include "../comedidev.h"
79 #include "comedi_fc.h"
81 /* Imaginary registers for the imaginary board */
85 #define SKEL_START_AI_CONV 0
86 #define SKEL_AI_READ 0
89 * Board descriptions for two imaginary boards. Describing the
90 * boards in this way is optional, and completely driver-dependent.
91 * Some drivers use arrays such as this, other do not.
105 static const struct skel_board skel_boards
[] = {
119 /* this structure is for data unique to this hardware driver. If
120 several hardware drivers keep similar information in this structure,
121 feel free to suggest moving the variable to the struct comedi_device struct.
123 struct skel_private
{
127 /* Used for AO readback */
128 unsigned int ao_readback
[2];
131 /* This function doesn't require a particular form, this is just
132 * what happens to be used in some of the drivers. It should
133 * convert ns nanoseconds to a counter value suitable for programming
134 * the device. Also, it should adjust ns so that it cooresponds to
135 * the actual time that the device will use. */
136 static int skel_ns_to_timer(unsigned int *ns
, int round
)
139 /* if your timing is done through two cascaded timers, the
140 * i8253_cascade_ns_to_timer() function in 8253.h can be
141 * very helpful. There are also i8254_load() and i8254_mm_load()
142 * which can be used to load values into the ubiquitous 8254 counters
149 * "instructions" read/write data in "one-shot" or "software-triggered"
152 static int skel_ai_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
153 struct comedi_insn
*insn
, unsigned int *data
)
155 const struct skel_board
*thisboard
= comedi_board(dev
);
160 /* a typical programming sequence */
162 /* write channel to multiplexer */
163 /* outw(chan,dev->iobase + SKEL_MUX); */
165 /* don't wait for mux to settle */
167 /* convert n samples */
168 for (n
= 0; n
< insn
->n
; n
++) {
169 /* trigger conversion */
170 /* outw(0,dev->iobase + SKEL_CONVERT); */
173 /* wait for conversion to end */
174 for (i
= 0; i
< TIMEOUT
; i
++) {
176 /* status = inb(dev->iobase + SKEL_STATUS); */
181 dev_warn(dev
->class_dev
, "ai timeout\n");
186 /* d = inw(dev->iobase + SKEL_AI_DATA); */
189 /* mangle the data as necessary */
190 d
^= 1 << (thisboard
->ai_bits
- 1);
195 /* return the number of samples read/written */
200 * cmdtest tests a particular command to see if it is valid.
201 * Using the cmdtest ioctl, a user can create a valid cmd
202 * and then have it executes by the cmd ioctl.
204 * cmdtest returns 1,2,3,4 or 0, depending on which tests
205 * the command passes.
207 static int skel_ai_cmdtest(struct comedi_device
*dev
,
208 struct comedi_subdevice
*s
,
209 struct comedi_cmd
*cmd
)
214 /* Step 1 : check if triggers are trivially valid */
216 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
);
217 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
218 TRIG_TIMER
| TRIG_EXT
);
219 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_TIMER
| TRIG_EXT
);
220 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
221 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
226 /* Step 2a : make sure trigger sources are unique */
228 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
229 err
|= cfc_check_trigger_is_unique(cmd
->convert_src
);
230 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
232 /* Step 2b : and mutually compatible */
237 /* Step 3: check if arguments are trivially valid */
239 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
241 #define MAX_SPEED 10000 /* in nanoseconds */
242 #define MIN_SPEED 1000000000 /* in nanoseconds */
244 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
245 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
247 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
250 /* external trigger */
251 /* should be level/edge, hi/lo specification here */
252 /* should specify multiple external triggers */
253 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
, 9);
256 if (cmd
->convert_src
== TRIG_TIMER
) {
257 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
, MAX_SPEED
);
258 err
|= cfc_check_trigger_arg_max(&cmd
->convert_arg
, MIN_SPEED
);
260 /* external trigger */
262 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
, 9);
265 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
267 if (cmd
->stop_src
== TRIG_COUNT
)
268 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, 0x00ffffff);
270 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
275 /* step 4: fix up any arguments */
277 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
278 tmp
= cmd
->scan_begin_arg
;
279 skel_ns_to_timer(&cmd
->scan_begin_arg
,
280 cmd
->flags
& TRIG_ROUND_MASK
);
281 if (tmp
!= cmd
->scan_begin_arg
)
284 if (cmd
->convert_src
== TRIG_TIMER
) {
285 tmp
= cmd
->convert_arg
;
286 skel_ns_to_timer(&cmd
->convert_arg
,
287 cmd
->flags
& TRIG_ROUND_MASK
);
288 if (tmp
!= cmd
->convert_arg
)
290 if (cmd
->scan_begin_src
== TRIG_TIMER
&&
291 cmd
->scan_begin_arg
<
292 cmd
->convert_arg
* cmd
->scan_end_arg
) {
293 cmd
->scan_begin_arg
=
294 cmd
->convert_arg
* cmd
->scan_end_arg
;
305 static int skel_ao_winsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
306 struct comedi_insn
*insn
, unsigned int *data
)
308 struct skel_private
*devpriv
= dev
->private;
310 int chan
= CR_CHAN(insn
->chanspec
);
312 /* Writing a list of values to an AO channel is probably not
313 * very useful, but that's how the interface is defined. */
314 for (i
= 0; i
< insn
->n
; i
++) {
315 /* a typical programming sequence */
316 /* outw(data[i],dev->iobase + SKEL_DA0 + chan); */
317 devpriv
->ao_readback
[chan
] = data
[i
];
320 /* return the number of samples read/written */
324 /* AO subdevices should have a read insn as well as a write insn.
325 * Usually this means copying a value stored in devpriv. */
326 static int skel_ao_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
327 struct comedi_insn
*insn
, unsigned int *data
)
329 struct skel_private
*devpriv
= dev
->private;
331 int chan
= CR_CHAN(insn
->chanspec
);
333 for (i
= 0; i
< insn
->n
; i
++)
334 data
[i
] = devpriv
->ao_readback
[chan
];
339 /* DIO devices are slightly special. Although it is possible to
340 * implement the insn_read/insn_write interface, it is much more
341 * useful to applications if you implement the insn_bits interface.
342 * This allows packed reading/writing of the DIO channels. The
343 * comedi core can convert between insn_bits and insn_read/write */
344 static int skel_dio_insn_bits(struct comedi_device
*dev
,
345 struct comedi_subdevice
*s
,
346 struct comedi_insn
*insn
, unsigned int *data
)
348 /* The insn data is a mask in data[0] and the new data
349 * in data[1], each channel cooresponding to a bit. */
351 s
->state
&= ~data
[0];
352 s
->state
|= data
[0] & data
[1];
353 /* Write out the new digital output lines */
354 /* outw(s->state,dev->iobase + SKEL_DIO); */
357 /* on return, data[1] contains the value of the digital
358 * input and output lines. */
359 /* data[1]=inw(dev->iobase + SKEL_DIO); */
360 /* or we could just return the software copy of the output values if
361 * it was a purely digital output subdevice */
362 /* data[1]=s->state; */
367 static int skel_dio_insn_config(struct comedi_device
*dev
,
368 struct comedi_subdevice
*s
,
369 struct comedi_insn
*insn
, unsigned int *data
)
371 int chan
= CR_CHAN(insn
->chanspec
);
373 /* The input or output configuration of each digital line is
374 * configured by a special insn_config instruction. chanspec
375 * contains the channel to be changed, and data[0] contains the
376 * value COMEDI_INPUT or COMEDI_OUTPUT. */
378 case INSN_CONFIG_DIO_OUTPUT
:
379 s
->io_bits
|= 1 << chan
;
381 case INSN_CONFIG_DIO_INPUT
:
382 s
->io_bits
&= ~(1 << chan
);
384 case INSN_CONFIG_DIO_QUERY
:
386 (s
->io_bits
& (1 << chan
)) ? COMEDI_OUTPUT
: COMEDI_INPUT
;
393 /* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
399 * Handle common part of skel_attach() and skel_auto_attach().
401 static int skel_common_attach(struct comedi_device
*dev
)
403 const struct skel_board
*thisboard
= comedi_board(dev
);
404 struct comedi_subdevice
*s
;
407 ret
= comedi_alloc_subdevices(dev
, 3);
411 s
= &dev
->subdevices
[0];
412 /* dev->read_subdev=s; */
413 /* analog input subdevice */
414 s
->type
= COMEDI_SUBD_AI
;
415 /* we support single-ended (ground) and differential */
416 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
| SDF_DIFF
;
417 s
->n_chan
= thisboard
->ai_chans
;
418 s
->maxdata
= (1 << thisboard
->ai_bits
) - 1;
419 s
->range_table
= &range_bipolar10
;
420 s
->len_chanlist
= 16; /* This is the maximum chanlist length that
421 the board can handle */
422 s
->insn_read
= skel_ai_rinsn
;
424 * s->subdev_flags |= SDF_CMD_READ;
425 * s->do_cmd = skel_ai_cmd;
427 s
->do_cmdtest
= skel_ai_cmdtest
;
429 s
= &dev
->subdevices
[1];
430 /* analog output subdevice */
431 s
->type
= COMEDI_SUBD_AO
;
432 s
->subdev_flags
= SDF_WRITABLE
;
435 s
->range_table
= &range_bipolar5
;
436 s
->insn_write
= skel_ao_winsn
;
437 s
->insn_read
= skel_ao_rinsn
;
439 s
= &dev
->subdevices
[2];
440 /* digital i/o subdevice */
441 if (thisboard
->have_dio
) {
442 s
->type
= COMEDI_SUBD_DIO
;
443 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
446 s
->range_table
= &range_digital
;
447 s
->insn_bits
= skel_dio_insn_bits
;
448 s
->insn_config
= skel_dio_insn_config
;
450 s
->type
= COMEDI_SUBD_UNUSED
;
453 dev_info(dev
->class_dev
, "skel: attached\n");
459 * _attach is called by the Comedi core to configure the driver
460 * for a particular board in response to the COMEDI_DEVCONFIG ioctl for
461 * a matching board or driver name. If you specified a board_name array
462 * in the driver structure, dev->board_ptr contains that address.
464 * Drivers that handle only PCI or USB devices do not usually support
465 * manual attachment of those devices via the COMEDI_DEVCONFIG ioctl, so
466 * those drivers do not have an _attach function; they just have an
467 * _auto_attach function instead. (See skel_auto_attach() for an example
468 * of such a function.)
470 static int skel_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
472 const struct skel_board
*thisboard
;
473 struct skel_private
*devpriv
;
476 * If you can probe the device to determine what device in a series
477 * it is, this is the place to do it. Otherwise, dev->board_ptr
478 * should already be initialized.
480 /* dev->board_ptr = skel_probe(dev, it); */
482 thisboard
= comedi_board(dev
);
485 * The dev->board_name is initialized by the comedi core before
486 * calling the (*attach) function. It can be optionally set by
487 * the driver if additional probing has been done.
489 /* dev->board_name = thisboard->name; */
491 /* Allocate the private data */
492 devpriv
= kzalloc(sizeof(*devpriv
), GFP_KERNEL
);
495 dev
->private = devpriv
;
498 * Supported boards are usually either auto-attached via the
499 * Comedi driver's _auto_attach routine, or manually attached via the
500 * Comedi driver's _attach routine. In most cases, attempts to
501 * manual attach boards that are usually auto-attached should be
502 * rejected by this function.
505 * if (thisboard->bustype == pci_bustype) {
506 * dev_err(dev->class_dev,
507 * "Manual attachment of PCI board '%s' not supported\n",
513 * For ISA boards, get the i/o base address from it->options[],
514 * request the i/o region and set dev->iobase * from it->options[].
515 * If using interrupts, get the IRQ number from it->options[].
519 * Call a common function to handle the remaining things to do for
520 * attaching ISA or PCI boards. (Extra parameters could be added
521 * to pass additional information such as IRQ number.)
523 return skel_common_attach(dev
);
527 * _auto_attach is called via comedi_pci_auto_config() (or
528 * comedi_usb_auto_config(), etc.) to handle devices that can be attached
529 * to the Comedi core automatically without the COMEDI_DEVCONFIG ioctl.
531 * The context parameter is driver dependent.
533 static int skel_auto_attach(struct comedi_device
*dev
,
534 unsigned long context
)
536 struct pci_dev
*pcidev
= comedi_to_pci_dev(dev
);
537 const struct skel_board
*thisboard
= NULL
;
538 struct skel_private
*devpriv
;
541 /* Hack to allow unused code to be optimized out. */
542 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS
))
546 * In this example, the _auto_attach is for a PCI device.
548 * The 'context' passed to this function is the id->driver_data
549 * associated with the PCI device found in the id_table during
550 * the modprobe. This 'context' is the index of the entry in
551 * skel_boards[i] that contains the boardinfo for the PCI device.
553 if (context
< ARRAY_SIZE(skel_boards
))
554 thisboard
= &skel_boards
[context
];
559 * Point the struct comedi_device to the matching board info
560 * and set the board name.
562 dev
->board_ptr
= thisboard
;
563 dev
->board_name
= thisboard
->name
;
565 /* Allocate the private data */
566 devpriv
= kzalloc(sizeof(*devpriv
), GFP_KERNEL
);
569 dev
->private = devpriv
;
571 /* Enable the PCI device. */
572 ret
= comedi_pci_enable(dev
);
577 * Record the fact that the PCI device is enabled so that it can
578 * be disabled during _detach().
580 * For this example driver, we assume PCI BAR 0 is the main I/O
581 * region for the board registers and use dev->iobase to hold the
582 * I/O base address and to indicate that the PCI device has been
585 * (For boards with memory-mapped registers, dev->iobase is not
586 * usually needed for register access, so can just be set to 1
587 * to indicate that the PCI device has been enabled.)
589 dev
->iobase
= pci_resource_start(pcidev
, 0);
592 * Call a common function to handle the remaining things to do for
593 * attaching ISA or PCI boards. (Extra parameters could be added
594 * to pass additional information such as IRQ number.)
596 return skel_common_attach(dev
);
600 * _detach is called to deconfigure a device. It should deallocate
602 * This function is also called when _attach() fails, so it should be
603 * careful not to release resources that were not necessarily
604 * allocated by _attach(). dev->private and dev->subdevices are
605 * deallocated automatically by the core.
607 static void skel_detach(struct comedi_device
*dev
)
609 const struct skel_board
*thisboard
= comedi_board(dev
);
610 struct skel_private
*devpriv
= dev
->private;
612 if (!thisboard
|| !devpriv
)
616 * Do common stuff such as freeing IRQ, unmapping remapped memory
617 * regions, etc., being careful to check that the stuff is valid given
618 * that _detach() is called even when _attach() or _auto_attach() return
622 if (IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS
) /* &&
623 thisboard->bustype == pci_bustype */) {
627 * If PCI device enabled by _auto_attach() (or _attach()),
630 comedi_pci_disable(dev
);
635 * Release the first I/O region requested during the
636 * _attach(). This is safe to call even if the request
637 * failed. If any additional I/O regions are requested
638 * they need to be released by the driver.
640 comedi_legacy_detach(dev
);
645 * The struct comedi_driver structure tells the Comedi core module
646 * which functions to call to configure/deconfigure (attach/detach)
647 * the board, and also about the kernel module that contains
650 static struct comedi_driver skel_driver
= {
651 .driver_name
= "dummy",
652 .module
= THIS_MODULE
,
653 .attach
= skel_attach
,
654 .auto_attach
= skel_auto_attach
,
655 .detach
= skel_detach
,
656 /* It is not necessary to implement the following members if you are
657 * writing a driver for a ISA PnP or PCI card */
658 /* Most drivers will support multiple types of boards by
659 * having an array of board structures. These were defined
660 * in skel_boards[] above. Note that the element 'name'
661 * was first in the structure -- Comedi uses this fact to
662 * extract the name of the board without knowing any details
663 * about the structure except for its length.
664 * When a device is attached (by comedi_config), the name
665 * of the device is given to Comedi, and Comedi tries to
666 * match it by going through the list of board names. If
667 * there is a match, the address of the pointer is put
668 * into dev->board_ptr and driver->attach() is called.
670 * Note that these are not necessary if you can determine
671 * the type of board in software. ISA PnP, PCI, and PCMCIA
672 * devices are such boards.
674 .board_name
= &skel_boards
[0].name
,
675 .offset
= sizeof(struct skel_board
),
676 .num_names
= ARRAY_SIZE(skel_boards
),
679 #ifdef CONFIG_COMEDI_PCI_DRIVERS
681 static int skel_pci_probe(struct pci_dev
*dev
,
682 const struct pci_device_id
*id
)
684 return comedi_pci_auto_config(dev
, &skel_driver
, id
->driver_data
);
688 * Please add your PCI vendor ID to comedidev.h, and it will
689 * be forwarded upstream.
691 #define PCI_VENDOR_ID_SKEL 0xdafe
694 * This is used by modprobe to translate PCI IDs to drivers.
695 * Should only be used for PCI and ISA-PnP devices
697 static DEFINE_PCI_DEVICE_TABLE(skel_pci_table
) = {
698 { PCI_VDEVICE(SKEL
, 0x0100), BOARD_SKEL100
},
699 { PCI_VDEVICE(SKEL
, 0x0200), BOARD_SKEL200
},
702 MODULE_DEVICE_TABLE(pci
, skel_pci_table
);
704 static struct pci_driver skel_pci_driver
= {
706 .id_table
= skel_pci_table
,
707 .probe
= skel_pci_probe
,
708 .remove
= comedi_pci_auto_unconfig
,
710 module_comedi_pci_driver(skel_driver
, skel_pci_driver
);
712 module_comedi_driver(skel_driver
);
715 MODULE_AUTHOR("Comedi http://www.comedi.org");
716 MODULE_DESCRIPTION("Comedi low-level driver");
717 MODULE_LICENSE("GPL");