]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/skel.c
Merge branch 'trivial' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[karo-tx-linux.git] / drivers / staging / comedi / drivers / skel.c
1 /*
2     comedi/drivers/skel.c
3     Skeleton code for a Comedi driver
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7
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.
12
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.
17
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.
21
22 */
23 /*
24 Driver: skel
25 Description: Skeleton driver, an example for driver writers
26 Devices:
27 Author: ds
28 Updated: Mon, 18 Mar 2002 15:34:01 -0800
29 Status: works
30
31 This driver is a documented example on how Comedi drivers are
32 written.
33
34 Configuration Options:
35   none
36 */
37
38 /*
39  * The previous block comment is used to automatically generate
40  * documentation in Comedi and Comedilib.  The fields:
41  *
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
49  *    value is used.
50  *  Author: you
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
55  *      passes comedi_test.
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.
64  *
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
69  * subdevices, etc.
70  *
71  * Somewhere in the comment should be information about configuration
72  * options that are used with comedi_config.
73  */
74
75 #include "../comedidev.h"
76
77 #include <linux/pci.h>          /* for PCI devices */
78
79 /* Imaginary registers for the imaginary board */
80
81 #define SKEL_SIZE 0
82
83 #define SKEL_START_AI_CONV      0
84 #define SKEL_AI_READ            0
85
86 /*
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.
90  */
91 struct skel_board {
92         const char *name;
93         int ai_chans;
94         int ai_bits;
95         int have_dio;
96 };
97
98 static const struct skel_board skel_boards[] = {
99         {
100          .name = "skel-100",
101          .ai_chans = 16,
102          .ai_bits = 12,
103          .have_dio = 1,
104          },
105         {
106          .name = "skel-200",
107          .ai_chans = 8,
108          .ai_bits = 16,
109          .have_dio = 0,
110          },
111 };
112
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
116  * upstream. */
117 #define PCI_VENDOR_ID_SKEL 0xdafe
118 static DEFINE_PCI_DEVICE_TABLE(skel_pci_table) = {
119         { PCI_DEVICE(PCI_VENDOR_ID_SKEL, 0x0100) },
120         { PCI_DEVICE(PCI_VENDOR_ID_SKEL, 0x0200) },
121         { 0 }
122 };
123
124 MODULE_DEVICE_TABLE(pci, skel_pci_table);
125
126 /*
127  * Useful for shorthand access to the particular board structure
128  */
129 #define thisboard ((const struct skel_board *)dev->board_ptr)
130
131 /* this structure is for data unique to this hardware driver.  If
132    several hardware drivers keep similar information in this structure,
133    feel free to suggest moving the variable to the struct comedi_device struct.
134  */
135 struct skel_private {
136
137         int data;
138
139         /* would be useful for a PCI device */
140         struct pci_dev *pci_dev;
141
142         /* Used for AO readback */
143         unsigned int ao_readback[2];
144 };
145
146 /*
147  * most drivers define the following macro to make it easy to
148  * access the private structure.
149  */
150 #define devpriv ((struct skel_private *)dev->private)
151
152 /*
153  * The struct comedi_driver structure tells the Comedi core module
154  * which functions to call to configure/deconfigure (attach/detach)
155  * the board, and also about the kernel module that contains
156  * the device code.
157  */
158 static int skel_attach(struct comedi_device *dev, struct comedi_devconfig *it);
159 static void skel_detach(struct comedi_device *dev);
160 static struct comedi_driver driver_skel = {
161         .driver_name = "dummy",
162         .module = THIS_MODULE,
163         .attach = skel_attach,
164         .detach = skel_detach,
165 /* It is not necessary to implement the following members if you are
166  * writing a driver for a ISA PnP or PCI card */
167         /* Most drivers will support multiple types of boards by
168          * having an array of board structures.  These were defined
169          * in skel_boards[] above.  Note that the element 'name'
170          * was first in the structure -- Comedi uses this fact to
171          * extract the name of the board without knowing any details
172          * about the structure except for its length.
173          * When a device is attached (by comedi_config), the name
174          * of the device is given to Comedi, and Comedi tries to
175          * match it by going through the list of board names.  If
176          * there is a match, the address of the pointer is put
177          * into dev->board_ptr and driver->attach() is called.
178          *
179          * Note that these are not necessary if you can determine
180          * the type of board in software.  ISA PnP, PCI, and PCMCIA
181          * devices are such boards.
182          */
183         .board_name = &skel_boards[0].name,
184         .offset = sizeof(struct skel_board),
185         .num_names = ARRAY_SIZE(skel_boards),
186 };
187
188 static int skel_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
189                          struct comedi_insn *insn, unsigned int *data);
190 static int skel_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
191                          struct comedi_insn *insn, unsigned int *data);
192 static int skel_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
193                          struct comedi_insn *insn, unsigned int *data);
194 static int skel_dio_insn_bits(struct comedi_device *dev,
195                               struct comedi_subdevice *s,
196                               struct comedi_insn *insn, unsigned int *data);
197 static int skel_dio_insn_config(struct comedi_device *dev,
198                                 struct comedi_subdevice *s,
199                                 struct comedi_insn *insn, unsigned int *data);
200 static int skel_ai_cmdtest(struct comedi_device *dev,
201                            struct comedi_subdevice *s, struct comedi_cmd *cmd);
202 static int skel_ns_to_timer(unsigned int *ns, int round);
203
204 /*
205  * Attach is called by the Comedi core to configure the driver
206  * for a particular board.  If you specified a board_name array
207  * in the driver structure, dev->board_ptr contains that
208  * address.
209  */
210 static int skel_attach(struct comedi_device *dev, struct comedi_devconfig *it)
211 {
212         struct comedi_subdevice *s;
213         int ret;
214
215         pr_info("comedi%d: skel: ", dev->minor);
216
217 /*
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.
221  */
222         /* dev->board_ptr = skel_probe(dev, it); */
223
224 /*
225  * Initialize dev->board_name.  Note that we can use the "thisboard"
226  * macro now, since we just initialized it in the last line.
227  */
228         dev->board_name = thisboard->name;
229
230 /*
231  * Allocate the private structure area.  alloc_private() is a
232  * convenient macro defined in comedidev.h.
233  */
234         if (alloc_private(dev, sizeof(struct skel_private)) < 0)
235                 return -ENOMEM;
236
237         ret = comedi_alloc_subdevices(dev, 3);
238         if (ret)
239                 return ret;
240
241         s = dev->subdevices + 0;
242         /* dev->read_subdev=s; */
243         /* analog input subdevice */
244         s->type = COMEDI_SUBD_AI;
245         /* we support single-ended (ground) and differential */
246         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
247         s->n_chan = thisboard->ai_chans;
248         s->maxdata = (1 << thisboard->ai_bits) - 1;
249         s->range_table = &range_bipolar10;
250         s->len_chanlist = 16;   /* This is the maximum chanlist length that
251                                    the board can handle */
252         s->insn_read = skel_ai_rinsn;
253 /*
254 *       s->subdev_flags |= SDF_CMD_READ;
255 *       s->do_cmd = skel_ai_cmd;
256 */
257         s->do_cmdtest = skel_ai_cmdtest;
258
259         s = dev->subdevices + 1;
260         /* analog output subdevice */
261         s->type = COMEDI_SUBD_AO;
262         s->subdev_flags = SDF_WRITABLE;
263         s->n_chan = 1;
264         s->maxdata = 0xffff;
265         s->range_table = &range_bipolar5;
266         s->insn_write = skel_ao_winsn;
267         s->insn_read = skel_ao_rinsn;
268
269         s = dev->subdevices + 2;
270         /* digital i/o subdevice */
271         if (thisboard->have_dio) {
272                 s->type = COMEDI_SUBD_DIO;
273                 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
274                 s->n_chan = 16;
275                 s->maxdata = 1;
276                 s->range_table = &range_digital;
277                 s->insn_bits = skel_dio_insn_bits;
278                 s->insn_config = skel_dio_insn_config;
279         } else {
280                 s->type = COMEDI_SUBD_UNUSED;
281         }
282
283         pr_info("attached\n");
284
285         return 0;
286 }
287
288 /*
289  * _detach is called to deconfigure a device.  It should deallocate
290  * resources.
291  * This function is also called when _attach() fails, so it should be
292  * careful not to release resources that were not necessarily
293  * allocated by _attach().  dev->private and dev->subdevices are
294  * deallocated automatically by the core.
295  */
296 static void skel_detach(struct comedi_device *dev)
297 {
298 }
299
300 /*
301  * "instructions" read/write data in "one-shot" or "software-triggered"
302  * mode.
303  */
304 static int skel_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
305                          struct comedi_insn *insn, unsigned int *data)
306 {
307         int n, i;
308         unsigned int d;
309         unsigned int status;
310
311         /* a typical programming sequence */
312
313         /* write channel to multiplexer */
314         /* outw(chan,dev->iobase + SKEL_MUX); */
315
316         /* don't wait for mux to settle */
317
318         /* convert n samples */
319         for (n = 0; n < insn->n; n++) {
320                 /* trigger conversion */
321                 /* outw(0,dev->iobase + SKEL_CONVERT); */
322
323 #define TIMEOUT 100
324                 /* wait for conversion to end */
325                 for (i = 0; i < TIMEOUT; i++) {
326                         status = 1;
327                         /* status = inb(dev->iobase + SKEL_STATUS); */
328                         if (status)
329                                 break;
330                 }
331                 if (i == TIMEOUT) {
332                         /* printk() should be used instead of printk()
333                          * whenever the code can be called from real-time. */
334                         pr_info("timeout\n");
335                         return -ETIMEDOUT;
336                 }
337
338                 /* read data */
339                 /* d = inw(dev->iobase + SKEL_AI_DATA); */
340                 d = 0;
341
342                 /* mangle the data as necessary */
343                 d ^= 1 << (thisboard->ai_bits - 1);
344
345                 data[n] = d;
346         }
347
348         /* return the number of samples read/written */
349         return n;
350 }
351
352 static int skel_ai_cmdtest(struct comedi_device *dev,
353                            struct comedi_subdevice *s, struct comedi_cmd *cmd)
354 {
355         int err = 0;
356         int tmp;
357
358         /* cmdtest tests a particular command to see if it is valid.
359          * Using the cmdtest ioctl, a user can create a valid cmd
360          * and then have it executes by the cmd ioctl.
361          *
362          * cmdtest returns 1,2,3,4 or 0, depending on which tests
363          * the command passes. */
364
365         /* step 1: make sure trigger sources are trivially valid */
366
367         tmp = cmd->start_src;
368         cmd->start_src &= TRIG_NOW;
369         if (!cmd->start_src || tmp != cmd->start_src)
370                 err++;
371
372         tmp = cmd->scan_begin_src;
373         cmd->scan_begin_src &= TRIG_TIMER | TRIG_EXT;
374         if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
375                 err++;
376
377         tmp = cmd->convert_src;
378         cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
379         if (!cmd->convert_src || tmp != cmd->convert_src)
380                 err++;
381
382         tmp = cmd->scan_end_src;
383         cmd->scan_end_src &= TRIG_COUNT;
384         if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
385                 err++;
386
387         tmp = cmd->stop_src;
388         cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
389         if (!cmd->stop_src || tmp != cmd->stop_src)
390                 err++;
391
392         if (err)
393                 return 1;
394
395         /* step 2: make sure trigger sources are unique and mutually compatible
396      */
397
398         /* note that mutual compatibility is not an issue here */
399         if (cmd->scan_begin_src != TRIG_TIMER &&
400             cmd->scan_begin_src != TRIG_EXT)
401                 err++;
402         if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
403                 err++;
404         if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
405                 err++;
406
407         if (err)
408                 return 2;
409
410         /* step 3: make sure arguments are trivially compatible */
411
412         if (cmd->start_arg != 0) {
413                 cmd->start_arg = 0;
414                 err++;
415         }
416 #define MAX_SPEED       10000   /* in nanoseconds */
417 #define MIN_SPEED       1000000000      /* in nanoseconds */
418
419         if (cmd->scan_begin_src == TRIG_TIMER) {
420                 if (cmd->scan_begin_arg < MAX_SPEED) {
421                         cmd->scan_begin_arg = MAX_SPEED;
422                         err++;
423                 }
424                 if (cmd->scan_begin_arg > MIN_SPEED) {
425                         cmd->scan_begin_arg = MIN_SPEED;
426                         err++;
427                 }
428         } else {
429                 /* external trigger */
430                 /* should be level/edge, hi/lo specification here */
431                 /* should specify multiple external triggers */
432                 if (cmd->scan_begin_arg > 9) {
433                         cmd->scan_begin_arg = 9;
434                         err++;
435                 }
436         }
437         if (cmd->convert_src == TRIG_TIMER) {
438                 if (cmd->convert_arg < MAX_SPEED) {
439                         cmd->convert_arg = MAX_SPEED;
440                         err++;
441                 }
442                 if (cmd->convert_arg > MIN_SPEED) {
443                         cmd->convert_arg = MIN_SPEED;
444                         err++;
445                 }
446         } else {
447                 /* external trigger */
448                 /* see above */
449                 if (cmd->convert_arg > 9) {
450                         cmd->convert_arg = 9;
451                         err++;
452                 }
453         }
454
455         if (cmd->scan_end_arg != cmd->chanlist_len) {
456                 cmd->scan_end_arg = cmd->chanlist_len;
457                 err++;
458         }
459         if (cmd->stop_src == TRIG_COUNT) {
460                 if (cmd->stop_arg > 0x00ffffff) {
461                         cmd->stop_arg = 0x00ffffff;
462                         err++;
463                 }
464         } else {
465                 /* TRIG_NONE */
466                 if (cmd->stop_arg != 0) {
467                         cmd->stop_arg = 0;
468                         err++;
469                 }
470         }
471
472         if (err)
473                 return 3;
474
475         /* step 4: fix up any arguments */
476
477         if (cmd->scan_begin_src == TRIG_TIMER) {
478                 tmp = cmd->scan_begin_arg;
479                 skel_ns_to_timer(&cmd->scan_begin_arg,
480                                  cmd->flags & TRIG_ROUND_MASK);
481                 if (tmp != cmd->scan_begin_arg)
482                         err++;
483         }
484         if (cmd->convert_src == TRIG_TIMER) {
485                 tmp = cmd->convert_arg;
486                 skel_ns_to_timer(&cmd->convert_arg,
487                                  cmd->flags & TRIG_ROUND_MASK);
488                 if (tmp != cmd->convert_arg)
489                         err++;
490                 if (cmd->scan_begin_src == TRIG_TIMER &&
491                     cmd->scan_begin_arg <
492                     cmd->convert_arg * cmd->scan_end_arg) {
493                         cmd->scan_begin_arg =
494                             cmd->convert_arg * cmd->scan_end_arg;
495                         err++;
496                 }
497         }
498
499         if (err)
500                 return 4;
501
502         return 0;
503 }
504
505 /* This function doesn't require a particular form, this is just
506  * what happens to be used in some of the drivers.  It should
507  * convert ns nanoseconds to a counter value suitable for programming
508  * the device.  Also, it should adjust ns so that it cooresponds to
509  * the actual time that the device will use. */
510 static int skel_ns_to_timer(unsigned int *ns, int round)
511 {
512         /* trivial timer */
513         /* if your timing is done through two cascaded timers, the
514          * i8253_cascade_ns_to_timer() function in 8253.h can be
515          * very helpful.  There are also i8254_load() and i8254_mm_load()
516          * which can be used to load values into the ubiquitous 8254 counters
517          */
518
519         return *ns;
520 }
521
522 static int skel_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
523                          struct comedi_insn *insn, unsigned int *data)
524 {
525         int i;
526         int chan = CR_CHAN(insn->chanspec);
527
528         pr_info("skel_ao_winsn\n");
529         /* Writing a list of values to an AO channel is probably not
530          * very useful, but that's how the interface is defined. */
531         for (i = 0; i < insn->n; i++) {
532                 /* a typical programming sequence */
533                 /* outw(data[i],dev->iobase + SKEL_DA0 + chan); */
534                 devpriv->ao_readback[chan] = data[i];
535         }
536
537         /* return the number of samples read/written */
538         return i;
539 }
540
541 /* AO subdevices should have a read insn as well as a write insn.
542  * Usually this means copying a value stored in devpriv. */
543 static int skel_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
544                          struct comedi_insn *insn, unsigned int *data)
545 {
546         int i;
547         int chan = CR_CHAN(insn->chanspec);
548
549         for (i = 0; i < insn->n; i++)
550                 data[i] = devpriv->ao_readback[chan];
551
552         return i;
553 }
554
555 /* DIO devices are slightly special.  Although it is possible to
556  * implement the insn_read/insn_write interface, it is much more
557  * useful to applications if you implement the insn_bits interface.
558  * This allows packed reading/writing of the DIO channels.  The
559  * comedi core can convert between insn_bits and insn_read/write */
560 static int skel_dio_insn_bits(struct comedi_device *dev,
561                               struct comedi_subdevice *s,
562                               struct comedi_insn *insn, unsigned int *data)
563 {
564         /* The insn data is a mask in data[0] and the new data
565          * in data[1], each channel cooresponding to a bit. */
566         if (data[0]) {
567                 s->state &= ~data[0];
568                 s->state |= data[0] & data[1];
569                 /* Write out the new digital output lines */
570                 /* outw(s->state,dev->iobase + SKEL_DIO); */
571         }
572
573         /* on return, data[1] contains the value of the digital
574          * input and output lines. */
575         /* data[1]=inw(dev->iobase + SKEL_DIO); */
576         /* or we could just return the software copy of the output values if
577          * it was a purely digital output subdevice */
578         /* data[1]=s->state; */
579
580         return insn->n;
581 }
582
583 static int skel_dio_insn_config(struct comedi_device *dev,
584                                 struct comedi_subdevice *s,
585                                 struct comedi_insn *insn, unsigned int *data)
586 {
587         int chan = CR_CHAN(insn->chanspec);
588
589         /* The input or output configuration of each digital line is
590          * configured by a special insn_config instruction.  chanspec
591          * contains the channel to be changed, and data[0] contains the
592          * value COMEDI_INPUT or COMEDI_OUTPUT. */
593         switch (data[0]) {
594         case INSN_CONFIG_DIO_OUTPUT:
595                 s->io_bits |= 1 << chan;
596                 break;
597         case INSN_CONFIG_DIO_INPUT:
598                 s->io_bits &= ~(1 << chan);
599                 break;
600         case INSN_CONFIG_DIO_QUERY:
601                 data[1] =
602                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
603                 return insn->n;
604                 break;
605         default:
606                 return -EINVAL;
607                 break;
608         }
609         /* outw(s->io_bits,dev->iobase + SKEL_DIO_CONFIG); */
610
611         return insn->n;
612 }
613
614 #ifdef CONFIG_COMEDI_PCI_DRIVERS
615 static int __devinit driver_skel_pci_probe(struct pci_dev *dev,
616                                            const struct pci_device_id *ent)
617 {
618         return comedi_pci_auto_config(dev, &driver_skel);
619 }
620
621 static void __devexit driver_skel_pci_remove(struct pci_dev *dev)
622 {
623         comedi_pci_auto_unconfig(dev);
624 }
625
626 static struct pci_driver driver_skel_pci_driver = {
627         .id_table = skel_pci_table,
628         .probe = &driver_skel_pci_probe,
629         .remove = __devexit_p(&driver_skel_pci_remove)
630 };
631
632 static int __init driver_skel_init_module(void)
633 {
634         int retval;
635
636         retval = comedi_driver_register(&driver_skel);
637         if (retval < 0)
638                 return retval;
639
640         driver_skel_pci_driver.name = (char *)driver_skel.driver_name;
641         return pci_register_driver(&driver_skel_pci_driver);
642 }
643
644 static void __exit driver_skel_cleanup_module(void)
645 {
646         pci_unregister_driver(&driver_skel_pci_driver);
647         comedi_driver_unregister(&driver_skel);
648 }
649
650 module_init(driver_skel_init_module);
651 module_exit(driver_skel_cleanup_module);
652 #else
653 static int __init driver_skel_init_module(void)
654 {
655         return comedi_driver_register(&driver_skel);
656 }
657
658 static void __exit driver_skel_cleanup_module(void)
659 {
660         comedi_driver_unregister(&driver_skel);
661 }
662
663 module_init(driver_skel_init_module);
664 module_exit(driver_skel_cleanup_module);
665 #endif
666
667 MODULE_AUTHOR("Comedi http://www.comedi.org");
668 MODULE_DESCRIPTION("Comedi low-level driver");
669 MODULE_LICENSE("GPL");