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.
101 static const struct skel_board skel_boards[] = {
118 /* this structure is for data unique to this hardware driver. If
119 several hardware drivers keep similar information in this structure,
120 feel free to suggest moving the variable to the struct comedi_device struct.
122 struct skel_private {
126 /* Used for AO readback */
127 unsigned int ao_readback[2];
130 /* This function doesn't require a particular form, this is just
131 * what happens to be used in some of the drivers. It should
132 * convert ns nanoseconds to a counter value suitable for programming
133 * the device. Also, it should adjust ns so that it cooresponds to
134 * the actual time that the device will use. */
135 static int skel_ns_to_timer(unsigned int *ns, int round)
138 /* if your timing is done through two cascaded timers, the
139 * i8253_cascade_ns_to_timer() function in 8253.h can be
140 * very helpful. There are also i8254_load() and i8254_mm_load()
141 * which can be used to load values into the ubiquitous 8254 counters
148 * "instructions" read/write data in "one-shot" or "software-triggered"
151 static int skel_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
152 struct comedi_insn *insn, unsigned int *data)
154 const struct skel_board *thisboard = comedi_board(dev);
159 /* a typical programming sequence */
161 /* write channel to multiplexer */
162 /* outw(chan,dev->iobase + SKEL_MUX); */
164 /* don't wait for mux to settle */
166 /* convert n samples */
167 for (n = 0; n < insn->n; n++) {
168 /* trigger conversion */
169 /* outw(0,dev->iobase + SKEL_CONVERT); */
172 /* wait for conversion to end */
173 for (i = 0; i < TIMEOUT; i++) {
175 /* status = inb(dev->iobase + SKEL_STATUS); */
180 dev_warn(dev->class_dev, "ai timeout\n");
185 /* d = inw(dev->iobase + SKEL_AI_DATA); */
188 /* mangle the data as necessary */
189 d ^= 1 << (thisboard->ai_bits - 1);
194 /* return the number of samples read/written */
199 * cmdtest tests a particular command to see if it is valid.
200 * Using the cmdtest ioctl, a user can create a valid cmd
201 * and then have it executes by the cmd ioctl.
203 * cmdtest returns 1,2,3,4 or 0, depending on which tests
204 * the command passes.
206 static int skel_ai_cmdtest(struct comedi_device *dev,
207 struct comedi_subdevice *s,
208 struct comedi_cmd *cmd)
213 /* Step 1 : check if triggers are trivially valid */
215 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
216 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
217 TRIG_TIMER | TRIG_EXT);
218 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
219 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
220 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
225 /* Step 2a : make sure trigger sources are unique */
227 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
228 err |= cfc_check_trigger_is_unique(cmd->convert_src);
229 err |= cfc_check_trigger_is_unique(cmd->stop_src);
231 /* Step 2b : and mutually compatible */
236 /* Step 3: check if arguments are trivially valid */
238 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
240 #define MAX_SPEED 10000 /* in nanoseconds */
241 #define MIN_SPEED 1000000000 /* in nanoseconds */
243 if (cmd->scan_begin_src == TRIG_TIMER) {
244 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
246 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
249 /* external trigger */
250 /* should be level/edge, hi/lo specification here */
251 /* should specify multiple external triggers */
252 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
255 if (cmd->convert_src == TRIG_TIMER) {
256 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, MAX_SPEED);
257 err |= cfc_check_trigger_arg_max(&cmd->convert_arg, MIN_SPEED);
259 /* external trigger */
261 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
264 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
266 if (cmd->stop_src == TRIG_COUNT)
267 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
269 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
274 /* step 4: fix up any arguments */
276 if (cmd->scan_begin_src == TRIG_TIMER) {
277 tmp = cmd->scan_begin_arg;
278 skel_ns_to_timer(&cmd->scan_begin_arg,
279 cmd->flags & TRIG_ROUND_MASK);
280 if (tmp != cmd->scan_begin_arg)
283 if (cmd->convert_src == TRIG_TIMER) {
284 tmp = cmd->convert_arg;
285 skel_ns_to_timer(&cmd->convert_arg,
286 cmd->flags & TRIG_ROUND_MASK);
287 if (tmp != cmd->convert_arg)
289 if (cmd->scan_begin_src == TRIG_TIMER &&
290 cmd->scan_begin_arg <
291 cmd->convert_arg * cmd->scan_end_arg) {
292 cmd->scan_begin_arg =
293 cmd->convert_arg * cmd->scan_end_arg;
304 static int skel_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
305 struct comedi_insn *insn, unsigned int *data)
307 struct skel_private *devpriv = dev->private;
309 int chan = CR_CHAN(insn->chanspec);
311 /* Writing a list of values to an AO channel is probably not
312 * very useful, but that's how the interface is defined. */
313 for (i = 0; i < insn->n; i++) {
314 /* a typical programming sequence */
315 /* outw(data[i],dev->iobase + SKEL_DA0 + chan); */
316 devpriv->ao_readback[chan] = data[i];
319 /* return the number of samples read/written */
323 /* AO subdevices should have a read insn as well as a write insn.
324 * Usually this means copying a value stored in devpriv. */
325 static int skel_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
326 struct comedi_insn *insn, unsigned int *data)
328 struct skel_private *devpriv = dev->private;
330 int chan = CR_CHAN(insn->chanspec);
332 for (i = 0; i < insn->n; i++)
333 data[i] = devpriv->ao_readback[chan];
338 /* DIO devices are slightly special. Although it is possible to
339 * implement the insn_read/insn_write interface, it is much more
340 * useful to applications if you implement the insn_bits interface.
341 * This allows packed reading/writing of the DIO channels. The
342 * comedi core can convert between insn_bits and insn_read/write */
343 static int skel_dio_insn_bits(struct comedi_device *dev,
344 struct comedi_subdevice *s,
345 struct comedi_insn *insn, unsigned int *data)
347 /* The insn data is a mask in data[0] and the new data
348 * in data[1], each channel cooresponding to a bit. */
350 s->state &= ~data[0];
351 s->state |= data[0] & data[1];
352 /* Write out the new digital output lines */
353 /* outw(s->state,dev->iobase + SKEL_DIO); */
356 /* on return, data[1] contains the value of the digital
357 * input and output lines. */
358 /* data[1]=inw(dev->iobase + SKEL_DIO); */
359 /* or we could just return the software copy of the output values if
360 * it was a purely digital output subdevice */
361 /* data[1]=s->state; */
366 static int skel_dio_insn_config(struct comedi_device *dev,
367 struct comedi_subdevice *s,
368 struct comedi_insn *insn, unsigned int *data)
370 int chan = CR_CHAN(insn->chanspec);
372 /* The input or output configuration of each digital line is
373 * configured by a special insn_config instruction. chanspec
374 * contains the channel to be changed, and data[0] contains the
375 * value COMEDI_INPUT or COMEDI_OUTPUT. */
377 case INSN_CONFIG_DIO_OUTPUT:
378 s->io_bits |= 1 << chan;
380 case INSN_CONFIG_DIO_INPUT:
381 s->io_bits &= ~(1 << chan);
383 case INSN_CONFIG_DIO_QUERY:
385 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
392 /* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
397 static const struct skel_board *skel_find_pci_board(struct pci_dev *pcidev)
402 * This example code assumes all the entries in skel_boards[] are PCI boards
403 * and all use the same PCI vendor ID. If skel_boards[] contains a mixture
404 * of PCI and non-PCI boards, this loop should skip over the non-PCI boards.
406 for (i = 0; i < ARRAY_SIZE(skel_boards); i++)
407 if (/* skel_boards[i].bustype == pci_bustype && */
408 pcidev->device == skel_boards[i].devid)
409 return &skel_boards[i];
414 * Handle common part of skel_attach() and skel_auto_attach().
416 static int skel_common_attach(struct comedi_device *dev)
418 const struct skel_board *thisboard = comedi_board(dev);
419 struct comedi_subdevice *s;
422 ret = comedi_alloc_subdevices(dev, 3);
426 s = &dev->subdevices[0];
427 /* dev->read_subdev=s; */
428 /* analog input subdevice */
429 s->type = COMEDI_SUBD_AI;
430 /* we support single-ended (ground) and differential */
431 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
432 s->n_chan = thisboard->ai_chans;
433 s->maxdata = (1 << thisboard->ai_bits) - 1;
434 s->range_table = &range_bipolar10;
435 s->len_chanlist = 16; /* This is the maximum chanlist length that
436 the board can handle */
437 s->insn_read = skel_ai_rinsn;
439 * s->subdev_flags |= SDF_CMD_READ;
440 * s->do_cmd = skel_ai_cmd;
442 s->do_cmdtest = skel_ai_cmdtest;
444 s = &dev->subdevices[1];
445 /* analog output subdevice */
446 s->type = COMEDI_SUBD_AO;
447 s->subdev_flags = SDF_WRITABLE;
450 s->range_table = &range_bipolar5;
451 s->insn_write = skel_ao_winsn;
452 s->insn_read = skel_ao_rinsn;
454 s = &dev->subdevices[2];
455 /* digital i/o subdevice */
456 if (thisboard->have_dio) {
457 s->type = COMEDI_SUBD_DIO;
458 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
461 s->range_table = &range_digital;
462 s->insn_bits = skel_dio_insn_bits;
463 s->insn_config = skel_dio_insn_config;
465 s->type = COMEDI_SUBD_UNUSED;
468 dev_info(dev->class_dev, "skel: attached\n");
474 * _attach is called by the Comedi core to configure the driver
475 * for a particular board in response to the COMEDI_DEVCONFIG ioctl for
476 * a matching board or driver name. If you specified a board_name array
477 * in the driver structure, dev->board_ptr contains that address.
479 * Drivers that handle only PCI or USB devices do not usually support
480 * manual attachment of those devices via the COMEDI_DEVCONFIG ioctl, so
481 * those drivers do not have an _attach function; they just have an
482 * _auto_attach function instead. (See skel_auto_attach() for an example
483 * of such a function.)
485 static int skel_attach(struct comedi_device *dev, struct comedi_devconfig *it)
487 const struct skel_board *thisboard;
488 struct skel_private *devpriv;
491 * If you can probe the device to determine what device in a series
492 * it is, this is the place to do it. Otherwise, dev->board_ptr
493 * should already be initialized.
495 /* dev->board_ptr = skel_probe(dev, it); */
497 thisboard = comedi_board(dev);
500 * Initialize dev->board_name.
502 dev->board_name = thisboard->name;
504 /* Allocate the private data */
505 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
508 dev->private = devpriv;
511 * Supported boards are usually either auto-attached via the
512 * Comedi driver's _auto_attach routine, or manually attached via the
513 * Comedi driver's _attach routine. In most cases, attempts to
514 * manual attach boards that are usually auto-attached should be
515 * rejected by this function.
518 * if (thisboard->bustype == pci_bustype) {
519 * dev_err(dev->class_dev,
520 * "Manual attachment of PCI board '%s' not supported\n",
526 * For ISA boards, get the i/o base address from it->options[],
527 * request the i/o region and set dev->iobase * from it->options[].
528 * If using interrupts, get the IRQ number from it->options[].
532 * Call a common function to handle the remaining things to do for
533 * attaching ISA or PCI boards. (Extra parameters could be added
534 * to pass additional information such as IRQ number.)
536 return skel_common_attach(dev);
540 * _auto_attach is called via comedi_pci_auto_config() (or
541 * comedi_usb_auto_config(), etc.) to handle devices that can be attached
542 * to the Comedi core automatically without the COMEDI_DEVCONFIG ioctl.
544 * The context parameter is usually unused, but if the driver called
545 * comedi_auto_config() directly instead of the comedi_pci_auto_config()
546 * wrapper function, this will be a copy of the context passed to
547 * comedi_auto_config().
549 static int skel_auto_attach(struct comedi_device *dev,
550 unsigned long context)
552 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
553 const struct skel_board *thisboard;
554 struct skel_private *devpriv;
557 /* Hack to allow unused code to be optimized out. */
558 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS))
561 /* Find a matching board in skel_boards[]. */
562 thisboard = skel_find_pci_board(pcidev);
564 dev_err(dev->class_dev, "BUG! cannot determine board type!\n");
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 = kzalloc(sizeof(*devpriv), GFP_KERNEL);
579 dev->private = devpriv;
581 /* Enable the PCI device. */
582 ret = comedi_pci_enable(pcidev, dev->board_name);
587 * Record the fact that the PCI device is enabled so that it can
588 * be disabled during _detach().
590 * For this example driver, we assume PCI BAR 0 is the main I/O
591 * region for the board registers and use dev->iobase to hold the
592 * I/O base address and to indicate that the PCI device has been
595 * (For boards with memory-mapped registers, dev->iobase is not
596 * usually needed for register access, so can just be set to 1
597 * to indicate that the PCI device has been enabled.)
599 dev->iobase = pci_resource_start(pcidev, 0);
602 * Call a common function to handle the remaining things to do for
603 * attaching ISA or PCI boards. (Extra parameters could be added
604 * to pass additional information such as IRQ number.)
606 return skel_common_attach(dev);
610 * _detach is called to deconfigure a device. It should deallocate
612 * This function is also called when _attach() fails, so it should be
613 * careful not to release resources that were not necessarily
614 * allocated by _attach(). dev->private and dev->subdevices are
615 * deallocated automatically by the core.
617 static void skel_detach(struct comedi_device *dev)
619 const struct skel_board *thisboard = comedi_board(dev);
620 struct skel_private *devpriv = dev->private;
621 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
623 if (!thisboard || !devpriv)
627 * Do common stuff such as freeing IRQ, unmapping remapped memory
628 * regions, etc., being careful to check that the stuff is valid given
629 * that _detach() is called even when _attach() or _auto_attach() return
633 if (IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS) /* &&
634 thisboard->bustype == pci_bustype */) {
638 * If PCI device enabled by _auto_attach() (or _attach()),
641 if (pcidev && dev->iobase)
642 comedi_pci_disable(pcidev);
647 * If I/O regions successfully requested by _attach(),
651 release_region(dev->iobase, SKEL_SIZE);
656 * The struct comedi_driver structure tells the Comedi core module
657 * which functions to call to configure/deconfigure (attach/detach)
658 * the board, and also about the kernel module that contains
661 static struct comedi_driver skel_driver = {
662 .driver_name = "dummy",
663 .module = THIS_MODULE,
664 .attach = skel_attach,
665 .auto_attach = skel_auto_attach,
666 .detach = skel_detach,
667 /* It is not necessary to implement the following members if you are
668 * writing a driver for a ISA PnP or PCI card */
669 /* Most drivers will support multiple types of boards by
670 * having an array of board structures. These were defined
671 * in skel_boards[] above. Note that the element 'name'
672 * was first in the structure -- Comedi uses this fact to
673 * extract the name of the board without knowing any details
674 * about the structure except for its length.
675 * When a device is attached (by comedi_config), the name
676 * of the device is given to Comedi, and Comedi tries to
677 * match it by going through the list of board names. If
678 * there is a match, the address of the pointer is put
679 * into dev->board_ptr and driver->attach() is called.
681 * Note that these are not necessary if you can determine
682 * the type of board in software. ISA PnP, PCI, and PCMCIA
683 * devices are such boards.
685 .board_name = &skel_boards[0].name,
686 .offset = sizeof(struct skel_board),
687 .num_names = ARRAY_SIZE(skel_boards),
690 #ifdef CONFIG_COMEDI_PCI_DRIVERS
692 /* This is used by modprobe to translate PCI IDs to drivers. Should
693 * only be used for PCI and ISA-PnP devices */
694 /* Please add your PCI vendor ID to comedidev.h, and it will be forwarded
696 #define PCI_VENDOR_ID_SKEL 0xdafe
697 static DEFINE_PCI_DEVICE_TABLE(skel_pci_table) = {
698 { PCI_DEVICE(PCI_VENDOR_ID_SKEL, 0x0100) },
699 { PCI_DEVICE(PCI_VENDOR_ID_SKEL, 0x0200) },
702 MODULE_DEVICE_TABLE(pci, skel_pci_table);
704 static int skel_pci_probe(struct pci_dev *dev,
705 const struct pci_device_id *ent)
707 return comedi_pci_auto_config(dev, &skel_driver);
710 static struct pci_driver skel_pci_driver = {
712 .id_table = skel_pci_table,
713 .probe = &skel_pci_probe,
714 .remove = comedi_pci_auto_unconfig,
716 module_comedi_pci_driver(skel_driver, skel_pci_driver);
718 module_comedi_driver(skel_driver);
721 MODULE_AUTHOR("Comedi http://www.comedi.org");
722 MODULE_DESCRIPTION("Comedi low-level driver");
723 MODULE_LICENSE("GPL");