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/pci.h>
72 #include "../comedidev.h"
74 #include "comedi_fc.h"
76 /* Imaginary registers for the imaginary board */
80 #define SKEL_START_AI_CONV 0
81 #define SKEL_AI_READ 0
84 * Board descriptions for two imaginary boards. Describing the
85 * boards in this way is optional, and completely driver-dependent.
86 * Some drivers use arrays such as this, other do not.
100 static const struct skel_board skel_boards[] = {
114 /* this structure is for data unique to this hardware driver. If
115 several hardware drivers keep similar information in this structure,
116 feel free to suggest moving the variable to the struct comedi_device struct.
118 struct skel_private {
122 /* Used for AO readback */
123 unsigned int ao_readback[2];
126 /* This function doesn't require a particular form, this is just
127 * what happens to be used in some of the drivers. It should
128 * convert ns nanoseconds to a counter value suitable for programming
129 * the device. Also, it should adjust ns so that it cooresponds to
130 * the actual time that the device will use. */
131 static int skel_ns_to_timer(unsigned int *ns, int round)
134 /* if your timing is done through two cascaded timers, the
135 * i8253_cascade_ns_to_timer() function in 8253.h can be
136 * very helpful. There are also i8254_load() and i8254_mm_load()
137 * which can be used to load values into the ubiquitous 8254 counters
144 * "instructions" read/write data in "one-shot" or "software-triggered"
147 static int skel_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
148 struct comedi_insn *insn, unsigned int *data)
150 const struct skel_board *thisboard = comedi_board(dev);
155 /* a typical programming sequence */
157 /* write channel to multiplexer */
158 /* outw(chan,dev->iobase + SKEL_MUX); */
160 /* don't wait for mux to settle */
162 /* convert n samples */
163 for (n = 0; n < insn->n; n++) {
164 /* trigger conversion */
165 /* outw(0,dev->iobase + SKEL_CONVERT); */
168 /* wait for conversion to end */
169 for (i = 0; i < TIMEOUT; i++) {
171 /* status = inb(dev->iobase + SKEL_STATUS); */
176 dev_warn(dev->class_dev, "ai timeout\n");
181 /* d = inw(dev->iobase + SKEL_AI_DATA); */
184 /* mangle the data as necessary */
185 d ^= 1 << (thisboard->ai_bits - 1);
190 /* return the number of samples read/written */
195 * cmdtest tests a particular command to see if it is valid.
196 * Using the cmdtest ioctl, a user can create a valid cmd
197 * and then have it executes by the cmd ioctl.
199 * cmdtest returns 1,2,3,4 or 0, depending on which tests
200 * the command passes.
202 static int skel_ai_cmdtest(struct comedi_device *dev,
203 struct comedi_subdevice *s,
204 struct comedi_cmd *cmd)
209 /* Step 1 : check if triggers are trivially valid */
211 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
212 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
213 TRIG_TIMER | TRIG_EXT);
214 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
215 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
216 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
221 /* Step 2a : make sure trigger sources are unique */
223 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
224 err |= cfc_check_trigger_is_unique(cmd->convert_src);
225 err |= cfc_check_trigger_is_unique(cmd->stop_src);
227 /* Step 2b : and mutually compatible */
232 /* Step 3: check if arguments are trivially valid */
234 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
236 #define MAX_SPEED 10000 /* in nanoseconds */
237 #define MIN_SPEED 1000000000 /* in nanoseconds */
239 if (cmd->scan_begin_src == TRIG_TIMER) {
240 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
242 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
245 /* external trigger */
246 /* should be level/edge, hi/lo specification here */
247 /* should specify multiple external triggers */
248 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
251 if (cmd->convert_src == TRIG_TIMER) {
252 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, MAX_SPEED);
253 err |= cfc_check_trigger_arg_max(&cmd->convert_arg, MIN_SPEED);
255 /* external trigger */
257 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
260 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
262 if (cmd->stop_src == TRIG_COUNT)
263 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
265 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
270 /* step 4: fix up any arguments */
272 if (cmd->scan_begin_src == TRIG_TIMER) {
273 tmp = cmd->scan_begin_arg;
274 skel_ns_to_timer(&cmd->scan_begin_arg,
275 cmd->flags & TRIG_ROUND_MASK);
276 if (tmp != cmd->scan_begin_arg)
279 if (cmd->convert_src == TRIG_TIMER) {
280 tmp = cmd->convert_arg;
281 skel_ns_to_timer(&cmd->convert_arg,
282 cmd->flags & TRIG_ROUND_MASK);
283 if (tmp != cmd->convert_arg)
285 if (cmd->scan_begin_src == TRIG_TIMER &&
286 cmd->scan_begin_arg <
287 cmd->convert_arg * cmd->scan_end_arg) {
288 cmd->scan_begin_arg =
289 cmd->convert_arg * cmd->scan_end_arg;
300 static int skel_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
301 struct comedi_insn *insn, unsigned int *data)
303 struct skel_private *devpriv = dev->private;
305 int chan = CR_CHAN(insn->chanspec);
307 /* Writing a list of values to an AO channel is probably not
308 * very useful, but that's how the interface is defined. */
309 for (i = 0; i < insn->n; i++) {
310 /* a typical programming sequence */
311 /* outw(data[i],dev->iobase + SKEL_DA0 + chan); */
312 devpriv->ao_readback[chan] = data[i];
315 /* return the number of samples read/written */
319 /* AO subdevices should have a read insn as well as a write insn.
320 * Usually this means copying a value stored in devpriv. */
321 static int skel_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
322 struct comedi_insn *insn, unsigned int *data)
324 struct skel_private *devpriv = dev->private;
326 int chan = CR_CHAN(insn->chanspec);
328 for (i = 0; i < insn->n; i++)
329 data[i] = devpriv->ao_readback[chan];
334 /* DIO devices are slightly special. Although it is possible to
335 * implement the insn_read/insn_write interface, it is much more
336 * useful to applications if you implement the insn_bits interface.
337 * This allows packed reading/writing of the DIO channels. The
338 * comedi core can convert between insn_bits and insn_read/write */
339 static int skel_dio_insn_bits(struct comedi_device *dev,
340 struct comedi_subdevice *s,
341 struct comedi_insn *insn, unsigned int *data)
343 /* The insn data is a mask in data[0] and the new data
344 * in data[1], each channel cooresponding to a bit. */
346 s->state &= ~data[0];
347 s->state |= data[0] & data[1];
348 /* Write out the new digital output lines */
349 /* outw(s->state,dev->iobase + SKEL_DIO); */
352 /* on return, data[1] contains the value of the digital
353 * input and output lines. */
354 /* data[1]=inw(dev->iobase + SKEL_DIO); */
355 /* or we could just return the software copy of the output values if
356 * it was a purely digital output subdevice */
357 /* data[1]=s->state; */
362 static int skel_dio_insn_config(struct comedi_device *dev,
363 struct comedi_subdevice *s,
364 struct comedi_insn *insn, unsigned int *data)
366 int chan = CR_CHAN(insn->chanspec);
368 /* The input or output configuration of each digital line is
369 * configured by a special insn_config instruction. chanspec
370 * contains the channel to be changed, and data[0] contains the
371 * value COMEDI_INPUT or COMEDI_OUTPUT. */
373 case INSN_CONFIG_DIO_OUTPUT:
374 s->io_bits |= 1 << chan;
376 case INSN_CONFIG_DIO_INPUT:
377 s->io_bits &= ~(1 << chan);
379 case INSN_CONFIG_DIO_QUERY:
381 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
388 /* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
394 * Handle common part of skel_attach() and skel_auto_attach().
396 static int skel_common_attach(struct comedi_device *dev)
398 const struct skel_board *thisboard = comedi_board(dev);
399 struct comedi_subdevice *s;
402 ret = comedi_alloc_subdevices(dev, 3);
406 s = &dev->subdevices[0];
407 /* dev->read_subdev=s; */
408 /* analog input subdevice */
409 s->type = COMEDI_SUBD_AI;
410 /* we support single-ended (ground) and differential */
411 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
412 s->n_chan = thisboard->ai_chans;
413 s->maxdata = (1 << thisboard->ai_bits) - 1;
414 s->range_table = &range_bipolar10;
415 s->len_chanlist = 16; /* This is the maximum chanlist length that
416 the board can handle */
417 s->insn_read = skel_ai_rinsn;
419 * s->subdev_flags |= SDF_CMD_READ;
420 * s->do_cmd = skel_ai_cmd;
422 s->do_cmdtest = skel_ai_cmdtest;
424 s = &dev->subdevices[1];
425 /* analog output subdevice */
426 s->type = COMEDI_SUBD_AO;
427 s->subdev_flags = SDF_WRITABLE;
430 s->range_table = &range_bipolar5;
431 s->insn_write = skel_ao_winsn;
432 s->insn_read = skel_ao_rinsn;
434 s = &dev->subdevices[2];
435 /* digital i/o subdevice */
436 if (thisboard->have_dio) {
437 s->type = COMEDI_SUBD_DIO;
438 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
441 s->range_table = &range_digital;
442 s->insn_bits = skel_dio_insn_bits;
443 s->insn_config = skel_dio_insn_config;
445 s->type = COMEDI_SUBD_UNUSED;
448 dev_info(dev->class_dev, "skel: attached\n");
454 * _attach is called by the Comedi core to configure the driver
455 * for a particular board in response to the COMEDI_DEVCONFIG ioctl for
456 * a matching board or driver name. If you specified a board_name array
457 * in the driver structure, dev->board_ptr contains that address.
459 * Drivers that handle only PCI or USB devices do not usually support
460 * manual attachment of those devices via the COMEDI_DEVCONFIG ioctl, so
461 * those drivers do not have an _attach function; they just have an
462 * _auto_attach function instead. (See skel_auto_attach() for an example
463 * of such a function.)
465 static int skel_attach(struct comedi_device *dev, struct comedi_devconfig *it)
467 const struct skel_board *thisboard;
468 struct skel_private *devpriv;
471 * If you can probe the device to determine what device in a series
472 * it is, this is the place to do it. Otherwise, dev->board_ptr
473 * should already be initialized.
475 /* dev->board_ptr = skel_probe(dev, it); */
477 thisboard = comedi_board(dev);
480 * The dev->board_name is initialized by the comedi core before
481 * calling the (*attach) function. It can be optionally set by
482 * the driver if additional probing has been done.
484 /* dev->board_name = thisboard->name; */
486 /* Allocate the private data */
487 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
490 dev->private = devpriv;
493 * Supported boards are usually either auto-attached via the
494 * Comedi driver's _auto_attach routine, or manually attached via the
495 * Comedi driver's _attach routine. In most cases, attempts to
496 * manual attach boards that are usually auto-attached should be
497 * rejected by this function.
500 * if (thisboard->bustype == pci_bustype) {
501 * dev_err(dev->class_dev,
502 * "Manual attachment of PCI board '%s' not supported\n",
508 * For ISA boards, get the i/o base address from it->options[],
509 * request the i/o region and set dev->iobase * from it->options[].
510 * If using interrupts, get the IRQ number from it->options[].
514 * Call a common function to handle the remaining things to do for
515 * attaching ISA or PCI boards. (Extra parameters could be added
516 * to pass additional information such as IRQ number.)
518 return skel_common_attach(dev);
522 * _auto_attach is called via comedi_pci_auto_config() (or
523 * comedi_usb_auto_config(), etc.) to handle devices that can be attached
524 * to the Comedi core automatically without the COMEDI_DEVCONFIG ioctl.
526 * The context parameter is driver dependent.
528 static int skel_auto_attach(struct comedi_device *dev,
529 unsigned long context)
531 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
532 const struct skel_board *thisboard = NULL;
533 struct skel_private *devpriv;
536 /* Hack to allow unused code to be optimized out. */
537 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS))
541 * In this example, the _auto_attach is for a PCI device.
543 * The 'context' passed to this function is the id->driver_data
544 * associated with the PCI device found in the id_table during
545 * the modprobe. This 'context' is the index of the entry in
546 * skel_boards[i] that contains the boardinfo for the PCI device.
548 if (context < ARRAY_SIZE(skel_boards))
549 thisboard = &skel_boards[context];
554 * Point the struct comedi_device to the matching board info
555 * and set the board name.
557 dev->board_ptr = thisboard;
558 dev->board_name = thisboard->name;
560 /* Allocate the private data */
561 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
564 dev->private = devpriv;
566 /* Enable the PCI device. */
567 ret = comedi_pci_enable(dev);
572 * Record the fact that the PCI device is enabled so that it can
573 * be disabled during _detach().
575 * For this example driver, we assume PCI BAR 0 is the main I/O
576 * region for the board registers and use dev->iobase to hold the
577 * I/O base address and to indicate that the PCI device has been
580 * (For boards with memory-mapped registers, dev->iobase is not
581 * usually needed for register access, so can just be set to 1
582 * to indicate that the PCI device has been enabled.)
584 dev->iobase = pci_resource_start(pcidev, 0);
587 * Call a common function to handle the remaining things to do for
588 * attaching ISA or PCI boards. (Extra parameters could be added
589 * to pass additional information such as IRQ number.)
591 return skel_common_attach(dev);
595 * _detach is called to deconfigure a device. It should deallocate
597 * This function is also called when _attach() fails, so it should be
598 * careful not to release resources that were not necessarily
599 * allocated by _attach(). dev->private and dev->subdevices are
600 * deallocated automatically by the core.
602 static void skel_detach(struct comedi_device *dev)
604 const struct skel_board *thisboard = comedi_board(dev);
605 struct skel_private *devpriv = dev->private;
607 if (!thisboard || !devpriv)
611 * Do common stuff such as freeing IRQ, unmapping remapped memory
612 * regions, etc., being careful to check that the stuff is valid given
613 * that _detach() is called even when _attach() or _auto_attach() return
617 if (IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS) /* &&
618 thisboard->bustype == pci_bustype */) {
622 * If PCI device enabled by _auto_attach() (or _attach()),
625 comedi_pci_disable(dev);
630 * Release the first I/O region requested during the
631 * _attach(). This is safe to call even if the request
632 * failed. If any additional I/O regions are requested
633 * they need to be released by the driver.
635 comedi_legacy_detach(dev);
640 * The struct comedi_driver structure tells the Comedi core module
641 * which functions to call to configure/deconfigure (attach/detach)
642 * the board, and also about the kernel module that contains
645 static struct comedi_driver skel_driver = {
646 .driver_name = "dummy",
647 .module = THIS_MODULE,
648 .attach = skel_attach,
649 .auto_attach = skel_auto_attach,
650 .detach = skel_detach,
651 /* It is not necessary to implement the following members if you are
652 * writing a driver for a ISA PnP or PCI card */
653 /* Most drivers will support multiple types of boards by
654 * having an array of board structures. These were defined
655 * in skel_boards[] above. Note that the element 'name'
656 * was first in the structure -- Comedi uses this fact to
657 * extract the name of the board without knowing any details
658 * about the structure except for its length.
659 * When a device is attached (by comedi_config), the name
660 * of the device is given to Comedi, and Comedi tries to
661 * match it by going through the list of board names. If
662 * there is a match, the address of the pointer is put
663 * into dev->board_ptr and driver->attach() is called.
665 * Note that these are not necessary if you can determine
666 * the type of board in software. ISA PnP, PCI, and PCMCIA
667 * devices are such boards.
669 .board_name = &skel_boards[0].name,
670 .offset = sizeof(struct skel_board),
671 .num_names = ARRAY_SIZE(skel_boards),
674 #ifdef CONFIG_COMEDI_PCI_DRIVERS
676 static int skel_pci_probe(struct pci_dev *dev,
677 const struct pci_device_id *id)
679 return comedi_pci_auto_config(dev, &skel_driver, id->driver_data);
683 * Please add your PCI vendor ID to comedidev.h, and it will
684 * be forwarded upstream.
686 #define PCI_VENDOR_ID_SKEL 0xdafe
689 * This is used by modprobe to translate PCI IDs to drivers.
690 * Should only be used for PCI and ISA-PnP devices
692 static DEFINE_PCI_DEVICE_TABLE(skel_pci_table) = {
693 { PCI_VDEVICE(SKEL, 0x0100), BOARD_SKEL100 },
694 { PCI_VDEVICE(SKEL, 0x0200), BOARD_SKEL200 },
697 MODULE_DEVICE_TABLE(pci, skel_pci_table);
699 static struct pci_driver skel_pci_driver = {
701 .id_table = skel_pci_table,
702 .probe = skel_pci_probe,
703 .remove = comedi_pci_auto_unconfig,
705 module_comedi_pci_driver(skel_driver, skel_pci_driver);
707 module_comedi_driver(skel_driver);
710 MODULE_AUTHOR("Comedi http://www.comedi.org");
711 MODULE_DESCRIPTION("Comedi low-level driver");
712 MODULE_LICENSE("GPL");