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 "../comedidev.h"
77 #include <linux/pci.h> /* for PCI devices */
79 /* Imaginary registers for the imaginary board */
83 #define SKEL_START_AI_CONV 0
84 #define SKEL_AI_READ 0
87 * Board descriptions for two imaginary boards. Describing the
88 * boards in this way is optional, and completely driver-dependent.
89 * Some drivers use arrays such as this, other do not.
98 static const struct skel_board skel_boards[] = {
113 /* This is used by modprobe to translate PCI IDs to drivers. Should
114 * only be used for PCI and ISA-PnP devices */
115 /* Please add your PCI vendor ID to comedidev.h, and it will be forwarded
117 #define PCI_VENDOR_ID_SKEL 0xdafe
118 static DEFINE_PCI_DEVICE_TABLE(skel_pci_table) = {
120 PCI_VENDOR_ID_SKEL, 0x0100, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
121 PCI_VENDOR_ID_SKEL, 0x0200, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
125 MODULE_DEVICE_TABLE(pci, skel_pci_table);
128 * Useful for shorthand access to the particular board structure
130 #define thisboard ((const struct skel_board *)dev->board_ptr)
132 /* this structure is for data unique to this hardware driver. If
133 several hardware drivers keep similar information in this structure,
134 feel free to suggest moving the variable to the struct comedi_device struct.
136 struct skel_private {
140 /* would be useful for a PCI device */
141 struct pci_dev *pci_dev;
143 /* Used for AO readback */
144 unsigned int ao_readback[2];
148 * most drivers define the following macro to make it easy to
149 * access the private structure.
151 #define devpriv ((struct skel_private *)dev->private)
154 * The struct comedi_driver structure tells the Comedi core module
155 * which functions to call to configure/deconfigure (attach/detach)
156 * the board, and also about the kernel module that contains
159 static int skel_attach(struct comedi_device *dev, struct comedi_devconfig *it);
160 static int skel_detach(struct comedi_device *dev);
161 static struct comedi_driver driver_skel = {
162 .driver_name = "dummy",
163 .module = THIS_MODULE,
164 .attach = skel_attach,
165 .detach = skel_detach,
166 /* It is not necessary to implement the following members if you are
167 * writing a driver for a ISA PnP or PCI card */
168 /* Most drivers will support multiple types of boards by
169 * having an array of board structures. These were defined
170 * in skel_boards[] above. Note that the element 'name'
171 * was first in the structure -- Comedi uses this fact to
172 * extract the name of the board without knowing any details
173 * about the structure except for its length.
174 * When a device is attached (by comedi_config), the name
175 * of the device is given to Comedi, and Comedi tries to
176 * match it by going through the list of board names. If
177 * there is a match, the address of the pointer is put
178 * into dev->board_ptr and driver->attach() is called.
180 * Note that these are not necessary if you can determine
181 * the type of board in software. ISA PnP, PCI, and PCMCIA
182 * devices are such boards.
184 .board_name = &skel_boards[0].name,
185 .offset = sizeof(struct skel_board),
186 .num_names = ARRAY_SIZE(skel_boards),
189 static int skel_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
190 struct comedi_insn *insn, unsigned int *data);
191 static int skel_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
192 struct comedi_insn *insn, unsigned int *data);
193 static int skel_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
194 struct comedi_insn *insn, unsigned int *data);
195 static int skel_dio_insn_bits(struct comedi_device *dev,
196 struct comedi_subdevice *s,
197 struct comedi_insn *insn, unsigned int *data);
198 static int skel_dio_insn_config(struct comedi_device *dev,
199 struct comedi_subdevice *s,
200 struct comedi_insn *insn, unsigned int *data);
201 static int skel_ai_cmdtest(struct comedi_device *dev,
202 struct comedi_subdevice *s, struct comedi_cmd *cmd);
203 static int skel_ns_to_timer(unsigned int *ns, int round);
206 * Attach is called by the Comedi core to configure the driver
207 * for a particular board. If you specified a board_name array
208 * in the driver structure, dev->board_ptr contains that
211 static int skel_attach(struct comedi_device *dev, struct comedi_devconfig *it)
213 struct comedi_subdevice *s;
215 pr_info("comedi%d: skel: ", dev->minor);
218 * If you can probe the device to determine what device in a series
219 * it is, this is the place to do it. Otherwise, dev->board_ptr
220 * should already be initialized.
222 /* dev->board_ptr = skel_probe(dev, it); */
225 * Initialize dev->board_name. Note that we can use the "thisboard"
226 * macro now, since we just initialized it in the last line.
228 dev->board_name = thisboard->name;
231 * Allocate the private structure area. alloc_private() is a
232 * convenient macro defined in comedidev.h.
234 if (alloc_private(dev, sizeof(struct skel_private)) < 0)
238 * Allocate the subdevice structures. alloc_subdevice() is a
239 * convenient macro defined in comedidev.h.
241 if (alloc_subdevices(dev, 3) < 0)
244 s = dev->subdevices + 0;
245 /* dev->read_subdev=s; */
246 /* analog input subdevice */
247 s->type = COMEDI_SUBD_AI;
248 /* we support single-ended (ground) and differential */
249 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
250 s->n_chan = thisboard->ai_chans;
251 s->maxdata = (1 << thisboard->ai_bits) - 1;
252 s->range_table = &range_bipolar10;
253 s->len_chanlist = 16; /* This is the maximum chanlist length that
254 the board can handle */
255 s->insn_read = skel_ai_rinsn;
257 * s->subdev_flags |= SDF_CMD_READ;
258 * s->do_cmd = skel_ai_cmd;
260 s->do_cmdtest = skel_ai_cmdtest;
262 s = dev->subdevices + 1;
263 /* analog output subdevice */
264 s->type = COMEDI_SUBD_AO;
265 s->subdev_flags = SDF_WRITABLE;
268 s->range_table = &range_bipolar5;
269 s->insn_write = skel_ao_winsn;
270 s->insn_read = skel_ao_rinsn;
272 s = dev->subdevices + 2;
273 /* digital i/o subdevice */
274 if (thisboard->have_dio) {
275 s->type = COMEDI_SUBD_DIO;
276 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
279 s->range_table = &range_digital;
280 s->insn_bits = skel_dio_insn_bits;
281 s->insn_config = skel_dio_insn_config;
283 s->type = COMEDI_SUBD_UNUSED;
286 pr_info("attached\n");
292 * _detach is called to deconfigure a device. It should deallocate
294 * This function is also called when _attach() fails, so it should be
295 * careful not to release resources that were not necessarily
296 * allocated by _attach(). dev->private and dev->subdevices are
297 * deallocated automatically by the core.
299 static int skel_detach(struct comedi_device *dev)
301 pr_info("comedi%d: skel: remove\n", dev->minor);
307 * "instructions" read/write data in "one-shot" or "software-triggered"
310 static int skel_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
311 struct comedi_insn *insn, unsigned int *data)
317 /* a typical programming sequence */
319 /* write channel to multiplexer */
320 /* outw(chan,dev->iobase + SKEL_MUX); */
322 /* don't wait for mux to settle */
324 /* convert n samples */
325 for (n = 0; n < insn->n; n++) {
326 /* trigger conversion */
327 /* outw(0,dev->iobase + SKEL_CONVERT); */
330 /* wait for conversion to end */
331 for (i = 0; i < TIMEOUT; i++) {
333 /* status = inb(dev->iobase + SKEL_STATUS); */
338 /* printk() should be used instead of printk()
339 * whenever the code can be called from real-time. */
340 pr_info("timeout\n");
345 /* d = inw(dev->iobase + SKEL_AI_DATA); */
348 /* mangle the data as necessary */
349 d ^= 1 << (thisboard->ai_bits - 1);
354 /* return the number of samples read/written */
358 static int skel_ai_cmdtest(struct comedi_device *dev,
359 struct comedi_subdevice *s, struct comedi_cmd *cmd)
364 /* cmdtest tests a particular command to see if it is valid.
365 * Using the cmdtest ioctl, a user can create a valid cmd
366 * and then have it executes by the cmd ioctl.
368 * cmdtest returns 1,2,3,4 or 0, depending on which tests
369 * the command passes. */
371 /* step 1: make sure trigger sources are trivially valid */
373 tmp = cmd->start_src;
374 cmd->start_src &= TRIG_NOW;
375 if (!cmd->start_src || tmp != cmd->start_src)
378 tmp = cmd->scan_begin_src;
379 cmd->scan_begin_src &= TRIG_TIMER | TRIG_EXT;
380 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
383 tmp = cmd->convert_src;
384 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
385 if (!cmd->convert_src || tmp != cmd->convert_src)
388 tmp = cmd->scan_end_src;
389 cmd->scan_end_src &= TRIG_COUNT;
390 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
394 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
395 if (!cmd->stop_src || tmp != cmd->stop_src)
401 /* step 2: make sure trigger sources are unique and mutually compatible
404 /* note that mutual compatibility is not an issue here */
405 if (cmd->scan_begin_src != TRIG_TIMER &&
406 cmd->scan_begin_src != TRIG_EXT)
408 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
410 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
416 /* step 3: make sure arguments are trivially compatible */
418 if (cmd->start_arg != 0) {
422 #define MAX_SPEED 10000 /* in nanoseconds */
423 #define MIN_SPEED 1000000000 /* in nanoseconds */
425 if (cmd->scan_begin_src == TRIG_TIMER) {
426 if (cmd->scan_begin_arg < MAX_SPEED) {
427 cmd->scan_begin_arg = MAX_SPEED;
430 if (cmd->scan_begin_arg > MIN_SPEED) {
431 cmd->scan_begin_arg = MIN_SPEED;
435 /* external trigger */
436 /* should be level/edge, hi/lo specification here */
437 /* should specify multiple external triggers */
438 if (cmd->scan_begin_arg > 9) {
439 cmd->scan_begin_arg = 9;
443 if (cmd->convert_src == TRIG_TIMER) {
444 if (cmd->convert_arg < MAX_SPEED) {
445 cmd->convert_arg = MAX_SPEED;
448 if (cmd->convert_arg > MIN_SPEED) {
449 cmd->convert_arg = MIN_SPEED;
453 /* external trigger */
455 if (cmd->convert_arg > 9) {
456 cmd->convert_arg = 9;
461 if (cmd->scan_end_arg != cmd->chanlist_len) {
462 cmd->scan_end_arg = cmd->chanlist_len;
465 if (cmd->stop_src == TRIG_COUNT) {
466 if (cmd->stop_arg > 0x00ffffff) {
467 cmd->stop_arg = 0x00ffffff;
472 if (cmd->stop_arg != 0) {
481 /* step 4: fix up any arguments */
483 if (cmd->scan_begin_src == TRIG_TIMER) {
484 tmp = cmd->scan_begin_arg;
485 skel_ns_to_timer(&cmd->scan_begin_arg,
486 cmd->flags & TRIG_ROUND_MASK);
487 if (tmp != cmd->scan_begin_arg)
490 if (cmd->convert_src == TRIG_TIMER) {
491 tmp = cmd->convert_arg;
492 skel_ns_to_timer(&cmd->convert_arg,
493 cmd->flags & TRIG_ROUND_MASK);
494 if (tmp != cmd->convert_arg)
496 if (cmd->scan_begin_src == TRIG_TIMER &&
497 cmd->scan_begin_arg <
498 cmd->convert_arg * cmd->scan_end_arg) {
499 cmd->scan_begin_arg =
500 cmd->convert_arg * cmd->scan_end_arg;
511 /* This function doesn't require a particular form, this is just
512 * what happens to be used in some of the drivers. It should
513 * convert ns nanoseconds to a counter value suitable for programming
514 * the device. Also, it should adjust ns so that it cooresponds to
515 * the actual time that the device will use. */
516 static int skel_ns_to_timer(unsigned int *ns, int round)
519 /* if your timing is done through two cascaded timers, the
520 * i8253_cascade_ns_to_timer() function in 8253.h can be
521 * very helpful. There are also i8254_load() and i8254_mm_load()
522 * which can be used to load values into the ubiquitous 8254 counters
528 static int skel_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
529 struct comedi_insn *insn, unsigned int *data)
532 int chan = CR_CHAN(insn->chanspec);
534 pr_info("skel_ao_winsn\n");
535 /* Writing a list of values to an AO channel is probably not
536 * very useful, but that's how the interface is defined. */
537 for (i = 0; i < insn->n; i++) {
538 /* a typical programming sequence */
539 /* outw(data[i],dev->iobase + SKEL_DA0 + chan); */
540 devpriv->ao_readback[chan] = data[i];
543 /* return the number of samples read/written */
547 /* AO subdevices should have a read insn as well as a write insn.
548 * Usually this means copying a value stored in devpriv. */
549 static int skel_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
550 struct comedi_insn *insn, unsigned int *data)
553 int chan = CR_CHAN(insn->chanspec);
555 for (i = 0; i < insn->n; i++)
556 data[i] = devpriv->ao_readback[chan];
561 /* DIO devices are slightly special. Although it is possible to
562 * implement the insn_read/insn_write interface, it is much more
563 * useful to applications if you implement the insn_bits interface.
564 * This allows packed reading/writing of the DIO channels. The
565 * comedi core can convert between insn_bits and insn_read/write */
566 static int skel_dio_insn_bits(struct comedi_device *dev,
567 struct comedi_subdevice *s,
568 struct comedi_insn *insn, unsigned int *data)
573 /* The insn data is a mask in data[0] and the new data
574 * in data[1], each channel cooresponding to a bit. */
576 s->state &= ~data[0];
577 s->state |= data[0] & data[1];
578 /* Write out the new digital output lines */
579 /* outw(s->state,dev->iobase + SKEL_DIO); */
582 /* on return, data[1] contains the value of the digital
583 * input and output lines. */
584 /* data[1]=inw(dev->iobase + SKEL_DIO); */
585 /* or we could just return the software copy of the output values if
586 * it was a purely digital output subdevice */
587 /* data[1]=s->state; */
592 static int skel_dio_insn_config(struct comedi_device *dev,
593 struct comedi_subdevice *s,
594 struct comedi_insn *insn, unsigned int *data)
596 int chan = CR_CHAN(insn->chanspec);
598 /* The input or output configuration of each digital line is
599 * configured by a special insn_config instruction. chanspec
600 * contains the channel to be changed, and data[0] contains the
601 * value COMEDI_INPUT or COMEDI_OUTPUT. */
603 case INSN_CONFIG_DIO_OUTPUT:
604 s->io_bits |= 1 << chan;
606 case INSN_CONFIG_DIO_INPUT:
607 s->io_bits &= ~(1 << chan);
609 case INSN_CONFIG_DIO_QUERY:
611 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
618 /* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
624 * A convenient macro that defines init_module() and cleanup_module(),
627 COMEDI_INITCLEANUP(driver_skel);
628 /* If you are writing a PCI driver you should use COMEDI_PCI_INITCLEANUP
631 /* COMEDI_PCI_INITCLEANUP(driver_skel, skel_pci_table) */