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 */
81 #define SKEL_START_AI_CONV 0
82 #define SKEL_AI_READ 0
85 * Board descriptions for two imaginary boards. Describing the
86 * boards in this way is optional, and completely driver-dependent.
87 * Some drivers use arrays such as this, other do not.
101 static const struct skel_board skel_boards[] = {
115 /* this structure is for data unique to this hardware driver. If
116 several hardware drivers keep similar information in this structure,
117 feel free to suggest moving the variable to the struct comedi_device struct.
119 struct skel_private {
123 /* Used for AO readback */
124 unsigned int ao_readback[2];
127 /* This function doesn't require a particular form, this is just
128 * what happens to be used in some of the drivers. It should
129 * convert ns nanoseconds to a counter value suitable for programming
130 * the device. Also, it should adjust ns so that it cooresponds to
131 * the actual time that the device will use. */
132 static int skel_ns_to_timer(unsigned int *ns, int round)
135 /* if your timing is done through two cascaded timers, the
136 * i8253_cascade_ns_to_timer() function in 8253.h can be
137 * very helpful. There are also i8254_load() and i8254_mm_load()
138 * which can be used to load values into the ubiquitous 8254 counters
145 * "instructions" read/write data in "one-shot" or "software-triggered"
148 static int skel_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
149 struct comedi_insn *insn, unsigned int *data)
151 const struct skel_board *thisboard = comedi_board(dev);
156 /* a typical programming sequence */
158 /* write channel to multiplexer */
159 /* outw(chan,dev->iobase + SKEL_MUX); */
161 /* don't wait for mux to settle */
163 /* convert n samples */
164 for (n = 0; n < insn->n; n++) {
165 /* trigger conversion */
166 /* outw(0,dev->iobase + SKEL_CONVERT); */
169 /* wait for conversion to end */
170 for (i = 0; i < TIMEOUT; i++) {
172 /* status = inb(dev->iobase + SKEL_STATUS); */
177 dev_warn(dev->class_dev, "ai timeout\n");
182 /* d = inw(dev->iobase + SKEL_AI_DATA); */
185 /* mangle the data as necessary */
186 d ^= 1 << (thisboard->ai_bits - 1);
191 /* return the number of samples read/written */
196 * cmdtest tests a particular command to see if it is valid.
197 * Using the cmdtest ioctl, a user can create a valid cmd
198 * and then have it executes by the cmd ioctl.
200 * cmdtest returns 1,2,3,4 or 0, depending on which tests
201 * the command passes.
203 static int skel_ai_cmdtest(struct comedi_device *dev,
204 struct comedi_subdevice *s,
205 struct comedi_cmd *cmd)
210 /* Step 1 : check if triggers are trivially valid */
212 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
213 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
214 TRIG_TIMER | TRIG_EXT);
215 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
216 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
217 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
222 /* Step 2a : make sure trigger sources are unique */
224 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
225 err |= cfc_check_trigger_is_unique(cmd->convert_src);
226 err |= cfc_check_trigger_is_unique(cmd->stop_src);
228 /* Step 2b : and mutually compatible */
233 /* Step 3: check if arguments are trivially valid */
235 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
237 #define MAX_SPEED 10000 /* in nanoseconds */
238 #define MIN_SPEED 1000000000 /* in nanoseconds */
240 if (cmd->scan_begin_src == TRIG_TIMER) {
241 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
243 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg,
246 /* external trigger */
247 /* should be level/edge, hi/lo specification here */
248 /* should specify multiple external triggers */
249 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
252 if (cmd->convert_src == TRIG_TIMER) {
253 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, MAX_SPEED);
254 err |= cfc_check_trigger_arg_max(&cmd->convert_arg, MIN_SPEED);
256 /* external trigger */
258 err |= cfc_check_trigger_arg_max(&cmd->scan_begin_arg, 9);
261 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
263 if (cmd->stop_src == TRIG_COUNT)
264 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
266 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
271 /* step 4: fix up any arguments */
273 if (cmd->scan_begin_src == TRIG_TIMER) {
274 tmp = cmd->scan_begin_arg;
275 skel_ns_to_timer(&cmd->scan_begin_arg,
276 cmd->flags & TRIG_ROUND_MASK);
277 if (tmp != cmd->scan_begin_arg)
280 if (cmd->convert_src == TRIG_TIMER) {
281 tmp = cmd->convert_arg;
282 skel_ns_to_timer(&cmd->convert_arg,
283 cmd->flags & TRIG_ROUND_MASK);
284 if (tmp != cmd->convert_arg)
286 if (cmd->scan_begin_src == TRIG_TIMER &&
287 cmd->scan_begin_arg <
288 cmd->convert_arg * cmd->scan_end_arg) {
289 cmd->scan_begin_arg =
290 cmd->convert_arg * cmd->scan_end_arg;
301 static int skel_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
302 struct comedi_insn *insn, unsigned int *data)
304 struct skel_private *devpriv = dev->private;
306 int chan = CR_CHAN(insn->chanspec);
308 /* Writing a list of values to an AO channel is probably not
309 * very useful, but that's how the interface is defined. */
310 for (i = 0; i < insn->n; i++) {
311 /* a typical programming sequence */
312 /* outw(data[i],dev->iobase + SKEL_DA0 + chan); */
313 devpriv->ao_readback[chan] = data[i];
316 /* return the number of samples read/written */
320 /* AO subdevices should have a read insn as well as a write insn.
321 * Usually this means copying a value stored in devpriv. */
322 static int skel_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
323 struct comedi_insn *insn, unsigned int *data)
325 struct skel_private *devpriv = dev->private;
327 int chan = CR_CHAN(insn->chanspec);
329 for (i = 0; i < insn->n; i++)
330 data[i] = devpriv->ao_readback[chan];
336 * DIO devices are slightly special. Although it is possible to
337 * implement the insn_read/insn_write interface, it is much more
338 * useful to applications if you implement the insn_bits interface.
339 * This allows packed reading/writing of the DIO channels. The
340 * comedi core can convert between insn_bits and insn_read/write.
342 static int skel_dio_insn_bits(struct comedi_device *dev,
343 struct comedi_subdevice *s,
344 struct comedi_insn *insn,
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 * The core provided comedi_dio_update_state() function can
352 * be used to handle the internal state update to DIO subdevices
353 * with <= 32 channels. This function will return '0' if the
354 * state does not change or the mask of the channels that need
357 if (comedi_dio_update_state(s, data)) {
358 /* Write out the new digital output lines */
359 /* outw(s->state, dev->iobase + SKEL_DIO); */
363 * On return, data[1] contains the value of the digital
364 * input and output lines.
366 /* data[1] = inw(dev->iobase + SKEL_DIO); */
369 * Or we could just return the software copy of the output
370 * values if it was a purely digital output subdevice.
372 /* data[1] = s->state; */
377 static int skel_dio_insn_config(struct comedi_device *dev,
378 struct comedi_subdevice *s,
379 struct comedi_insn *insn,
385 * The input or output configuration of each digital line is
386 * configured by special insn_config instructions.
388 * chanspec contains the channel to be changed
389 * data[0] contains the instruction to perform on the channel
391 * Normally the core provided comedi_dio_insn_config() function
392 * can be used to handle the boilerplpate.
394 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
398 /* Update the hardware to the new configuration */
399 /* outw(s->io_bits, dev->iobase + SKEL_DIO_CONFIG); */
405 * Handle common part of skel_attach() and skel_auto_attach().
407 static int skel_common_attach(struct comedi_device *dev)
409 const struct skel_board *thisboard = comedi_board(dev);
410 struct comedi_subdevice *s;
413 ret = comedi_alloc_subdevices(dev, 3);
417 s = &dev->subdevices[0];
418 /* dev->read_subdev=s; */
419 /* analog input subdevice */
420 s->type = COMEDI_SUBD_AI;
421 /* we support single-ended (ground) and differential */
422 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
423 s->n_chan = thisboard->ai_chans;
424 s->maxdata = (1 << thisboard->ai_bits) - 1;
425 s->range_table = &range_bipolar10;
426 s->len_chanlist = 16; /* This is the maximum chanlist length that
427 the board can handle */
428 s->insn_read = skel_ai_rinsn;
430 * s->subdev_flags |= SDF_CMD_READ;
431 * s->do_cmd = skel_ai_cmd;
433 s->do_cmdtest = skel_ai_cmdtest;
435 s = &dev->subdevices[1];
436 /* analog output subdevice */
437 s->type = COMEDI_SUBD_AO;
438 s->subdev_flags = SDF_WRITABLE;
441 s->range_table = &range_bipolar5;
442 s->insn_write = skel_ao_winsn;
443 s->insn_read = skel_ao_rinsn;
445 s = &dev->subdevices[2];
446 /* digital i/o subdevice */
447 if (thisboard->have_dio) {
448 s->type = COMEDI_SUBD_DIO;
449 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
452 s->range_table = &range_digital;
453 s->insn_bits = skel_dio_insn_bits;
454 s->insn_config = skel_dio_insn_config;
456 s->type = COMEDI_SUBD_UNUSED;
459 dev_info(dev->class_dev, "skel: attached\n");
465 * _attach is called by the Comedi core to configure the driver
466 * for a particular board in response to the COMEDI_DEVCONFIG ioctl for
467 * a matching board or driver name. If you specified a board_name array
468 * in the driver structure, dev->board_ptr contains that address.
470 * Drivers that handle only PCI or USB devices do not usually support
471 * manual attachment of those devices via the COMEDI_DEVCONFIG ioctl, so
472 * those drivers do not have an _attach function; they just have an
473 * _auto_attach function instead. (See skel_auto_attach() for an example
474 * of such a function.)
476 static int skel_attach(struct comedi_device *dev, struct comedi_devconfig *it)
478 const struct skel_board *thisboard;
479 struct skel_private *devpriv;
482 * If you can probe the device to determine what device in a series
483 * it is, this is the place to do it. Otherwise, dev->board_ptr
484 * should already be initialized.
486 /* dev->board_ptr = skel_probe(dev, it); */
488 thisboard = comedi_board(dev);
491 * The dev->board_name is initialized by the comedi core before
492 * calling the (*attach) function. It can be optionally set by
493 * the driver if additional probing has been done.
495 /* dev->board_name = thisboard->name; */
497 /* Allocate the private data */
498 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
503 * Supported boards are usually either auto-attached via the
504 * Comedi driver's _auto_attach routine, or manually attached via the
505 * Comedi driver's _attach routine. In most cases, attempts to
506 * manual attach boards that are usually auto-attached should be
507 * rejected by this function.
510 * if (thisboard->bustype == pci_bustype) {
511 * dev_err(dev->class_dev,
512 * "Manual attachment of PCI board '%s' not supported\n",
518 * For ISA boards, get the i/o base address from it->options[],
519 * request the i/o region and set dev->iobase * from it->options[].
520 * If using interrupts, get the IRQ number from it->options[].
524 * Call a common function to handle the remaining things to do for
525 * attaching ISA or PCI boards. (Extra parameters could be added
526 * to pass additional information such as IRQ number.)
528 return skel_common_attach(dev);
532 * _auto_attach is called via comedi_pci_auto_config() (or
533 * comedi_usb_auto_config(), etc.) to handle devices that can be attached
534 * to the Comedi core automatically without the COMEDI_DEVCONFIG ioctl.
536 * The context parameter is driver dependent.
538 static int skel_auto_attach(struct comedi_device *dev,
539 unsigned long context)
541 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
542 const struct skel_board *thisboard = NULL;
543 struct skel_private *devpriv;
546 /* Hack to allow unused code to be optimized out. */
547 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS))
551 * In this example, the _auto_attach is for a PCI device.
553 * The 'context' passed to this function is the id->driver_data
554 * associated with the PCI device found in the id_table during
555 * the modprobe. This 'context' is the index of the entry in
556 * skel_boards[i] that contains the boardinfo for the PCI device.
558 if (context < ARRAY_SIZE(skel_boards))
559 thisboard = &skel_boards[context];
564 * Point the struct comedi_device to the matching board info
565 * and set the board name.
567 dev->board_ptr = thisboard;
568 dev->board_name = thisboard->name;
570 /* Allocate the private data */
571 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
575 /* Enable the PCI device. */
576 ret = comedi_pci_enable(dev);
581 * Record the fact that the PCI device is enabled so that it can
582 * be disabled during _detach().
584 * For this example driver, we assume PCI BAR 0 is the main I/O
585 * region for the board registers and use dev->iobase to hold the
586 * I/O base address and to indicate that the PCI device has been
589 * (For boards with memory-mapped registers, dev->iobase is not
590 * usually needed for register access, so can just be set to 1
591 * to indicate that the PCI device has been enabled.)
593 dev->iobase = pci_resource_start(pcidev, 0);
596 * Call a common function to handle the remaining things to do for
597 * attaching ISA or PCI boards. (Extra parameters could be added
598 * to pass additional information such as IRQ number.)
600 return skel_common_attach(dev);
604 * _detach is called to deconfigure a device. It should deallocate
606 * This function is also called when _attach() fails, so it should be
607 * careful not to release resources that were not necessarily
608 * allocated by _attach(). dev->private and dev->subdevices are
609 * deallocated automatically by the core.
611 static void skel_detach(struct comedi_device *dev)
613 const struct skel_board *thisboard = comedi_board(dev);
614 struct skel_private *devpriv = dev->private;
616 if (!thisboard || !devpriv)
620 * Do common stuff such as freeing IRQ, unmapping remapped memory
621 * regions, etc., being careful to check that the stuff is valid given
622 * that _detach() is called even when _attach() or _auto_attach() return
626 if (IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS) /* &&
627 thisboard->bustype == pci_bustype */) {
631 * If PCI device enabled by _auto_attach() (or _attach()),
634 comedi_pci_disable(dev);
639 * Release the first I/O region requested during the
640 * _attach(). This is safe to call even if the request
641 * failed. If any additional I/O regions are requested
642 * they need to be released by the driver.
644 comedi_legacy_detach(dev);
649 * The struct comedi_driver structure tells the Comedi core module
650 * which functions to call to configure/deconfigure (attach/detach)
651 * the board, and also about the kernel module that contains
654 static struct comedi_driver skel_driver = {
655 .driver_name = "dummy",
656 .module = THIS_MODULE,
657 .attach = skel_attach,
658 .auto_attach = skel_auto_attach,
659 .detach = skel_detach,
660 /* It is not necessary to implement the following members if you are
661 * writing a driver for a ISA PnP or PCI card */
662 /* Most drivers will support multiple types of boards by
663 * having an array of board structures. These were defined
664 * in skel_boards[] above. Note that the element 'name'
665 * was first in the structure -- Comedi uses this fact to
666 * extract the name of the board without knowing any details
667 * about the structure except for its length.
668 * When a device is attached (by comedi_config), the name
669 * of the device is given to Comedi, and Comedi tries to
670 * match it by going through the list of board names. If
671 * there is a match, the address of the pointer is put
672 * into dev->board_ptr and driver->attach() is called.
674 * Note that these are not necessary if you can determine
675 * the type of board in software. ISA PnP, PCI, and PCMCIA
676 * devices are such boards.
678 .board_name = &skel_boards[0].name,
679 .offset = sizeof(struct skel_board),
680 .num_names = ARRAY_SIZE(skel_boards),
683 #ifdef CONFIG_COMEDI_PCI_DRIVERS
685 static int skel_pci_probe(struct pci_dev *dev,
686 const struct pci_device_id *id)
688 return comedi_pci_auto_config(dev, &skel_driver, id->driver_data);
692 * Please add your PCI vendor ID to comedidev.h, and it will
693 * be forwarded upstream.
695 #define PCI_VENDOR_ID_SKEL 0xdafe
698 * This is used by modprobe to translate PCI IDs to drivers.
699 * Should only be used for PCI and ISA-PnP devices
701 static DEFINE_PCI_DEVICE_TABLE(skel_pci_table) = {
702 { PCI_VDEVICE(SKEL, 0x0100), BOARD_SKEL100 },
703 { PCI_VDEVICE(SKEL, 0x0200), BOARD_SKEL200 },
706 MODULE_DEVICE_TABLE(pci, skel_pci_table);
708 static struct pci_driver skel_pci_driver = {
710 .id_table = skel_pci_table,
711 .probe = skel_pci_probe,
712 .remove = comedi_pci_auto_unconfig,
714 module_comedi_pci_driver(skel_driver, skel_pci_driver);
716 module_comedi_driver(skel_driver);
719 MODULE_AUTHOR("Comedi http://www.comedi.org");
720 MODULE_DESCRIPTION("Comedi low-level driver");
721 MODULE_LICENSE("GPL");