1 /*======================================================================
3 comedi/drivers/quatech_daqp_cs.c
5 Quatech DAQP PCMCIA data capture cards COMEDI client driver
6 Copyright (C) 2000, 2003 Brent Baccala <baccala@freesoft.org>
7 The DAQP interface code in this file is released into the public domain.
9 COMEDI - Linux Control and Measurement Device Interface
10 Copyright (C) 1998 David A. Schleef <ds@schleef.org>
11 http://www.comedi.org/
13 quatech_daqp_cs.c 1.10
15 Documentation for the DAQP PCMCIA cards can be found on Quatech's site:
17 ftp://ftp.quatech.com/Manuals/daqp-208.pdf
19 This manual is for both the DAQP-208 and the DAQP-308.
26 - ground ref or differential
27 - single-shot and timed both supported
28 - D/A conversion, single-shot
33 - any kind of triggering - external or D/A channel 1
34 - the card's optional expansion board
35 - the card's timer (for anything other than A/D conversion)
36 - D/A update modes other than immediate (i.e, timed)
37 - fancier timing modes
38 - setting card's FIFO buffer thresholds to anything but default
40 ======================================================================*/
43 Driver: quatech_daqp_cs
44 Description: Quatech DAQP PCMCIA data capture cards
45 Author: Brent Baccala <baccala@freesoft.org>
47 Devices: [Quatech] DAQP-208 (daqp), DAQP-308
50 #include "../comedidev.h"
51 #include <linux/semaphore.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/cisreg.h>
55 #include <pcmcia/ds.h>
57 #include <linux/completion.h>
59 #include "comedi_fc.h"
61 /* Maximum number of separate DAQP devices we'll allow */
65 struct pcmcia_device *link;
70 enum { semaphore, buffer } interrupt_mode;
72 struct completion eos;
74 struct comedi_device *dev;
75 struct comedi_subdevice *s;
79 /* A list of "instances" of the device. */
81 static struct local_info_t *dev_table[MAX_DEV] = { NULL, /* ... */ };
83 /* The DAQP communicates with the system through a 16 byte I/O window. */
85 #define DAQP_FIFO_SIZE 4096
88 #define DAQP_SCANLIST 1
89 #define DAQP_CONTROL 2
91 #define DAQP_DIGITAL_IO 3
92 #define DAQP_PACER_LOW 4
93 #define DAQP_PACER_MID 5
94 #define DAQP_PACER_HIGH 6
95 #define DAQP_COMMAND 7
100 #define DAQP_SCANLIST_DIFFERENTIAL 0x4000
101 #define DAQP_SCANLIST_GAIN(x) ((x)<<12)
102 #define DAQP_SCANLIST_CHANNEL(x) ((x)<<8)
103 #define DAQP_SCANLIST_START 0x0080
104 #define DAQP_SCANLIST_EXT_GAIN(x) ((x)<<4)
105 #define DAQP_SCANLIST_EXT_CHANNEL(x) (x)
107 #define DAQP_CONTROL_PACER_100kHz 0xc0
108 #define DAQP_CONTROL_PACER_1MHz 0x80
109 #define DAQP_CONTROL_PACER_5MHz 0x40
110 #define DAQP_CONTROL_PACER_EXTERNAL 0x00
111 #define DAQP_CONTORL_EXPANSION 0x20
112 #define DAQP_CONTROL_EOS_INT_ENABLE 0x10
113 #define DAQP_CONTROL_FIFO_INT_ENABLE 0x08
114 #define DAQP_CONTROL_TRIGGER_ONESHOT 0x00
115 #define DAQP_CONTROL_TRIGGER_CONTINUOUS 0x04
116 #define DAQP_CONTROL_TRIGGER_INTERNAL 0x00
117 #define DAQP_CONTROL_TRIGGER_EXTERNAL 0x02
118 #define DAQP_CONTROL_TRIGGER_RISING 0x00
119 #define DAQP_CONTROL_TRIGGER_FALLING 0x01
121 #define DAQP_STATUS_IDLE 0x80
122 #define DAQP_STATUS_RUNNING 0x40
123 #define DAQP_STATUS_EVENTS 0x38
124 #define DAQP_STATUS_DATA_LOST 0x20
125 #define DAQP_STATUS_END_OF_SCAN 0x10
126 #define DAQP_STATUS_FIFO_THRESHOLD 0x08
127 #define DAQP_STATUS_FIFO_FULL 0x04
128 #define DAQP_STATUS_FIFO_NEARFULL 0x02
129 #define DAQP_STATUS_FIFO_EMPTY 0x01
131 #define DAQP_COMMAND_ARM 0x80
132 #define DAQP_COMMAND_RSTF 0x40
133 #define DAQP_COMMAND_RSTQ 0x20
134 #define DAQP_COMMAND_STOP 0x10
135 #define DAQP_COMMAND_LATCH 0x08
136 #define DAQP_COMMAND_100kHz 0x00
137 #define DAQP_COMMAND_50kHz 0x02
138 #define DAQP_COMMAND_25kHz 0x04
139 #define DAQP_COMMAND_FIFO_DATA 0x01
140 #define DAQP_COMMAND_FIFO_PROGRAM 0x00
142 #define DAQP_AUX_TRIGGER_TTL 0x00
143 #define DAQP_AUX_TRIGGER_ANALOG 0x80
144 #define DAQP_AUX_TRIGGER_PRETRIGGER 0x40
145 #define DAQP_AUX_TIMER_INT_ENABLE 0x20
146 #define DAQP_AUX_TIMER_RELOAD 0x00
147 #define DAQP_AUX_TIMER_PAUSE 0x08
148 #define DAQP_AUX_TIMER_GO 0x10
149 #define DAQP_AUX_TIMER_GO_EXTERNAL 0x18
150 #define DAQP_AUX_TIMER_EXTERNAL_SRC 0x04
151 #define DAQP_AUX_TIMER_INTERNAL_SRC 0x00
152 #define DAQP_AUX_DA_DIRECT 0x00
153 #define DAQP_AUX_DA_OVERFLOW 0x01
154 #define DAQP_AUX_DA_EXTERNAL 0x02
155 #define DAQP_AUX_DA_PACER 0x03
157 #define DAQP_AUX_RUNNING 0x80
158 #define DAQP_AUX_TRIGGERED 0x40
159 #define DAQP_AUX_DA_BUFFER 0x20
160 #define DAQP_AUX_TIMER_OVERFLOW 0x10
161 #define DAQP_AUX_CONVERSION 0x08
162 #define DAQP_AUX_DATA_LOST 0x04
163 #define DAQP_AUX_FIFO_NEARFULL 0x02
164 #define DAQP_AUX_FIFO_EMPTY 0x01
166 /* These range structures tell COMEDI how the sample values map to
167 * voltages. The A/D converter has four .ranges = +/- 10V through
168 * +/- 1.25V, and the D/A converter has only .one = +/- 5V.
171 static const struct comedi_lrange range_daqp_ai = { 4, {
179 static const struct comedi_lrange range_daqp_ao = { 1, {BIP_RANGE(5)} };
181 /*====================================================================*/
183 /* comedi interface code */
185 static int daqp_attach(struct comedi_device *dev, struct comedi_devconfig *it);
186 static void daqp_detach(struct comedi_device *dev);
187 static struct comedi_driver driver_daqp = {
188 .driver_name = "quatech_daqp_cs",
189 .module = THIS_MODULE,
190 .attach = daqp_attach,
191 .detach = daqp_detach,
196 static void daqp_dump(struct comedi_device *dev)
198 printk(KERN_INFO "DAQP: status %02x; aux status %02x\n",
199 inb(dev->iobase + DAQP_STATUS), inb(dev->iobase + DAQP_AUX));
202 static void hex_dump(char *str, void *ptr, int len)
204 unsigned char *cptr = ptr;
209 for (i = 0; i < len; i++) {
211 printk("\n%p:", cptr);
213 printk(" %02x", *(cptr++));
220 /* Cancel a running acquisition */
222 static int daqp_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
224 struct local_info_t *local = (struct local_info_t *)s->private;
230 outb(DAQP_COMMAND_STOP, dev->iobase + DAQP_COMMAND);
232 /* flush any linguring data in FIFO - superfluous here */
233 /* outb(DAQP_COMMAND_RSTF, dev->iobase+DAQP_COMMAND); */
235 local->interrupt_mode = semaphore;
242 * Operates in one of two modes. If local->interrupt_mode is
243 * 'semaphore', just signal the local->eos completion and return
244 * (one-shot mode). Otherwise (continuous mode), read data in from
245 * the card, transfer it to the buffer provided by the higher-level
246 * comedi kernel module, and signal various comedi callback routines,
247 * which run pretty quick.
249 static enum irqreturn daqp_interrupt(int irq, void *dev_id)
251 struct local_info_t *local = (struct local_info_t *)dev_id;
252 struct comedi_device *dev;
253 struct comedi_subdevice *s;
254 int loop_limit = 10000;
259 "daqp_interrupt(): irq %d for unknown device.\n", irq);
265 printk(KERN_WARNING "daqp_interrupt(): NULL comedi_device.\n");
269 if (!dev->attached) {
271 "daqp_interrupt(): struct comedi_device not yet attached.\n");
278 "daqp_interrupt(): NULL comedi_subdevice.\n");
282 if ((struct local_info_t *)s->private != local) {
284 "daqp_interrupt(): invalid comedi_subdevice.\n");
288 switch (local->interrupt_mode) {
292 complete(&local->eos);
297 while (!((status = inb(dev->iobase + DAQP_STATUS))
298 & DAQP_STATUS_FIFO_EMPTY)) {
302 if (status & DAQP_STATUS_DATA_LOST) {
304 COMEDI_CB_EOA | COMEDI_CB_OVERFLOW;
305 printk("daqp: data lost\n");
306 daqp_ai_cancel(dev, s);
310 data = inb(dev->iobase + DAQP_FIFO);
311 data |= inb(dev->iobase + DAQP_FIFO) << 8;
314 comedi_buf_put(s->async, data);
316 /* If there's a limit, decrement it
317 * and stop conversion if zero
320 if (local->count > 0) {
322 if (local->count == 0) {
323 daqp_ai_cancel(dev, s);
324 s->async->events |= COMEDI_CB_EOA;
329 if ((loop_limit--) <= 0)
333 if (loop_limit <= 0) {
335 "loop_limit reached in daqp_interrupt()\n");
336 daqp_ai_cancel(dev, s);
337 s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
340 s->async->events |= COMEDI_CB_BLOCK;
342 comedi_event(dev, s);
347 /* One-shot analog data acquisition routine */
349 static int daqp_ai_insn_read(struct comedi_device *dev,
350 struct comedi_subdevice *s,
351 struct comedi_insn *insn, unsigned int *data)
353 struct local_info_t *local = (struct local_info_t *)s->private;
362 /* Stop any running conversion */
363 daqp_ai_cancel(dev, s);
365 outb(0, dev->iobase + DAQP_AUX);
367 /* Reset scan list queue */
368 outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND);
370 /* Program one scan list entry */
372 v = DAQP_SCANLIST_CHANNEL(CR_CHAN(insn->chanspec))
373 | DAQP_SCANLIST_GAIN(CR_RANGE(insn->chanspec));
375 if (CR_AREF(insn->chanspec) == AREF_DIFF)
376 v |= DAQP_SCANLIST_DIFFERENTIAL;
379 v |= DAQP_SCANLIST_START;
381 outb(v & 0xff, dev->iobase + DAQP_SCANLIST);
382 outb(v >> 8, dev->iobase + DAQP_SCANLIST);
384 /* Reset data FIFO (see page 28 of DAQP User's Manual) */
386 outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND);
390 v = DAQP_CONTROL_TRIGGER_ONESHOT | DAQP_CONTROL_TRIGGER_INTERNAL
391 | DAQP_CONTROL_PACER_100kHz | DAQP_CONTROL_EOS_INT_ENABLE;
393 outb(v, dev->iobase + DAQP_CONTROL);
395 /* Reset any pending interrupts (my card has a tendency to require
396 * require multiple reads on the status register to achieve this)
400 && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ;
402 printk("daqp: couldn't clear interrupts in status register\n");
406 init_completion(&local->eos);
407 local->interrupt_mode = semaphore;
411 for (i = 0; i < insn->n; i++) {
413 /* Start conversion */
414 outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA,
415 dev->iobase + DAQP_COMMAND);
417 /* Wait for interrupt service routine to unblock completion */
418 /* Maybe could use a timeout here, but it's interruptible */
419 if (wait_for_completion_interruptible(&local->eos))
422 data[i] = inb(dev->iobase + DAQP_FIFO);
423 data[i] |= inb(dev->iobase + DAQP_FIFO) << 8;
430 /* This function converts ns nanoseconds to a counter value suitable
431 * for programming the device. We always use the DAQP's 5 MHz clock,
432 * which with its 24-bit counter, allows values up to 84 seconds.
433 * Also, the function adjusts ns so that it cooresponds to the actual
434 * time that the device will use.
437 static int daqp_ns_to_timer(unsigned int *ns, int round)
447 /* cmdtest tests a particular command to see if it is valid.
448 * Using the cmdtest ioctl, a user can create a valid cmd
449 * and then have it executed by the cmd ioctl.
451 * cmdtest returns 1,2,3,4 or 0, depending on which tests
452 * the command passes.
455 static int daqp_ai_cmdtest(struct comedi_device *dev,
456 struct comedi_subdevice *s, struct comedi_cmd *cmd)
461 /* Step 1 : check if triggers are trivially valid */
463 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
464 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
465 TRIG_TIMER | TRIG_FOLLOW);
466 err |= cfc_check_trigger_src(&cmd->convert_src,
467 TRIG_TIMER | TRIG_NOW);
468 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
469 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
474 /* Step 2a : make sure trigger sources are unique */
476 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
477 err |= cfc_check_trigger_is_unique(cmd->convert_src);
478 err |= cfc_check_trigger_is_unique(cmd->stop_src);
480 /* Step 2b : and mutually compatible */
485 /* step 3: make sure arguments are trivially compatible */
487 if (cmd->start_arg != 0) {
491 #define MAX_SPEED 10000 /* 100 kHz - in nanoseconds */
493 if (cmd->scan_begin_src == TRIG_TIMER
494 && cmd->scan_begin_arg < MAX_SPEED) {
495 cmd->scan_begin_arg = MAX_SPEED;
499 /* If both scan_begin and convert are both timer values, the only
500 * way that can make sense is if the scan time is the number of
501 * conversions times the convert time
504 if (cmd->scan_begin_src == TRIG_TIMER && cmd->convert_src == TRIG_TIMER
505 && cmd->scan_begin_arg != cmd->convert_arg * cmd->scan_end_arg) {
509 if (cmd->convert_src == TRIG_TIMER && cmd->convert_arg < MAX_SPEED) {
510 cmd->convert_arg = MAX_SPEED;
514 if (cmd->scan_end_arg != cmd->chanlist_len) {
515 cmd->scan_end_arg = cmd->chanlist_len;
518 if (cmd->stop_src == TRIG_COUNT) {
519 if (cmd->stop_arg > 0x00ffffff) {
520 cmd->stop_arg = 0x00ffffff;
525 if (cmd->stop_arg != 0) {
534 /* step 4: fix up any arguments */
536 if (cmd->scan_begin_src == TRIG_TIMER) {
537 tmp = cmd->scan_begin_arg;
538 daqp_ns_to_timer(&cmd->scan_begin_arg,
539 cmd->flags & TRIG_ROUND_MASK);
540 if (tmp != cmd->scan_begin_arg)
544 if (cmd->convert_src == TRIG_TIMER) {
545 tmp = cmd->convert_arg;
546 daqp_ns_to_timer(&cmd->convert_arg,
547 cmd->flags & TRIG_ROUND_MASK);
548 if (tmp != cmd->convert_arg)
558 static int daqp_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
560 struct local_info_t *local = (struct local_info_t *)s->private;
561 struct comedi_cmd *cmd = &s->async->cmd;
563 int scanlist_start_on_every_entry;
573 /* Stop any running conversion */
574 daqp_ai_cancel(dev, s);
576 outb(0, dev->iobase + DAQP_AUX);
578 /* Reset scan list queue */
579 outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND);
581 /* Program pacer clock
583 * There's two modes we can operate in. If convert_src is
584 * TRIG_TIMER, then convert_arg specifies the time between
585 * each conversion, so we program the pacer clock to that
586 * frequency and set the SCANLIST_START bit on every scanlist
587 * entry. Otherwise, convert_src is TRIG_NOW, which means
588 * we want the fastest possible conversions, scan_begin_src
589 * is TRIG_TIMER, and scan_begin_arg specifies the time between
590 * each scan, so we program the pacer clock to this frequency
591 * and only set the SCANLIST_START bit on the first entry.
594 if (cmd->convert_src == TRIG_TIMER) {
595 counter = daqp_ns_to_timer(&cmd->convert_arg,
596 cmd->flags & TRIG_ROUND_MASK);
597 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW);
598 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID);
599 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH);
600 scanlist_start_on_every_entry = 1;
602 counter = daqp_ns_to_timer(&cmd->scan_begin_arg,
603 cmd->flags & TRIG_ROUND_MASK);
604 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW);
605 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID);
606 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH);
607 scanlist_start_on_every_entry = 0;
610 /* Program scan list */
612 for (i = 0; i < cmd->chanlist_len; i++) {
614 int chanspec = cmd->chanlist[i];
616 /* Program one scan list entry */
618 v = DAQP_SCANLIST_CHANNEL(CR_CHAN(chanspec))
619 | DAQP_SCANLIST_GAIN(CR_RANGE(chanspec));
621 if (CR_AREF(chanspec) == AREF_DIFF)
622 v |= DAQP_SCANLIST_DIFFERENTIAL;
624 if (i == 0 || scanlist_start_on_every_entry)
625 v |= DAQP_SCANLIST_START;
627 outb(v & 0xff, dev->iobase + DAQP_SCANLIST);
628 outb(v >> 8, dev->iobase + DAQP_SCANLIST);
631 /* Now it's time to program the FIFO threshold, basically the
632 * number of samples the card will buffer before it interrupts
635 * If we don't have a stop count, then use half the size of
636 * the FIFO (the manufacturer's recommendation). Consider
637 * that the FIFO can hold 2K samples (4K bytes). With the
638 * threshold set at half the FIFO size, we have a margin of
639 * error of 1024 samples. At the chip's maximum sample rate
640 * of 100,000 Hz, the CPU would have to delay interrupt
641 * service for a full 10 milliseconds in order to lose data
642 * here (as opposed to higher up in the kernel). I've never
643 * seen it happen. However, for slow sample rates it may
644 * buffer too much data and introduce too much delay for the
647 * If we have a stop count, then things get more interesting.
648 * If the stop count is less than the FIFO size (actually
649 * three-quarters of the FIFO size - see below), we just use
650 * the stop count itself as the threshold, the card interrupts
651 * us when that many samples have been taken, and we kill the
652 * acquisition at that point and are done. If the stop count
653 * is larger than that, then we divide it by 2 until it's less
654 * than three quarters of the FIFO size (we always leave the
655 * top quarter of the FIFO as protection against sluggish CPU
656 * interrupt response) and use that as the threshold. So, if
657 * the stop count is 4000 samples, we divide by two twice to
658 * get 1000 samples, use that as the threshold, take four
659 * interrupts to get our 4000 samples and are done.
661 * The algorithm could be more clever. For example, if 81000
662 * samples are requested, we could set the threshold to 1500
663 * samples and take 54 interrupts to get 81000. But 54 isn't
664 * a power of two, so this algorithm won't find that option.
665 * Instead, it'll set the threshold at 1266 and take 64
666 * interrupts to get 81024 samples, of which the last 24 will
667 * be discarded... but we won't get the last interrupt until
668 * they've been collected. To find the first option, the
669 * computer could look at the prime decomposition of the
670 * sample count (81000 = 3^4 * 5^3 * 2^3) and factor it into a
671 * threshold (1500 = 3 * 5^3 * 2^2) and an interrupt count (54
672 * = 3^3 * 2). Hmmm... a one-line while loop or prime
673 * decomposition of integers... I'll leave it the way it is.
675 * I'll also note a mini-race condition before ignoring it in
676 * the code. Let's say we're taking 4000 samples, as before.
677 * After 1000 samples, we get an interrupt. But before that
678 * interrupt is completely serviced, another sample is taken
679 * and loaded into the FIFO. Since the interrupt handler
680 * empties the FIFO before returning, it will read 1001 samples.
681 * If that happens four times, we'll end up taking 4004 samples,
682 * not 4000. The interrupt handler will discard the extra four
683 * samples (by halting the acquisition with four samples still
684 * in the FIFO), but we will have to wait for them.
686 * In short, this code works pretty well, but for either of
687 * the two reasons noted, might end up waiting for a few more
688 * samples than actually requested. Shouldn't make too much
692 /* Save away the number of conversions we should perform, and
693 * compute the FIFO threshold (in bytes, not samples - that's
694 * why we multiple local->count by 2 = sizeof(sample))
697 if (cmd->stop_src == TRIG_COUNT) {
698 local->count = cmd->stop_arg * cmd->scan_end_arg;
699 threshold = 2 * local->count;
700 while (threshold > DAQP_FIFO_SIZE * 3 / 4)
704 threshold = DAQP_FIFO_SIZE / 2;
707 /* Reset data FIFO (see page 28 of DAQP User's Manual) */
709 outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND);
711 /* Set FIFO threshold. First two bytes are near-empty
712 * threshold, which is unused; next two bytes are near-full
713 * threshold. We computed the number of bytes we want in the
714 * FIFO when the interrupt is generated, what the card wants
715 * is actually the number of available bytes left in the FIFO
716 * when the interrupt is to happen.
719 outb(0x00, dev->iobase + DAQP_FIFO);
720 outb(0x00, dev->iobase + DAQP_FIFO);
722 outb((DAQP_FIFO_SIZE - threshold) & 0xff, dev->iobase + DAQP_FIFO);
723 outb((DAQP_FIFO_SIZE - threshold) >> 8, dev->iobase + DAQP_FIFO);
727 v = DAQP_CONTROL_TRIGGER_CONTINUOUS | DAQP_CONTROL_TRIGGER_INTERNAL
728 | DAQP_CONTROL_PACER_5MHz | DAQP_CONTROL_FIFO_INT_ENABLE;
730 outb(v, dev->iobase + DAQP_CONTROL);
732 /* Reset any pending interrupts (my card has a tendency to require
733 * require multiple reads on the status register to achieve this)
737 && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ;
740 "daqp: couldn't clear interrupts in status register\n");
744 local->interrupt_mode = buffer;
748 /* Start conversion */
749 outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA,
750 dev->iobase + DAQP_COMMAND);
755 /* Single-shot analog output routine */
757 static int daqp_ao_insn_write(struct comedi_device *dev,
758 struct comedi_subdevice *s,
759 struct comedi_insn *insn, unsigned int *data)
761 struct local_info_t *local = (struct local_info_t *)s->private;
768 chan = CR_CHAN(insn->chanspec);
771 d ^= 0x0800; /* Flip the sign */
774 /* Make sure D/A update mode is direct update */
775 outb(0, dev->iobase + DAQP_AUX);
777 outw(d, dev->iobase + DAQP_DA);
782 /* Digital input routine */
784 static int daqp_di_insn_read(struct comedi_device *dev,
785 struct comedi_subdevice *s,
786 struct comedi_insn *insn, unsigned int *data)
788 struct local_info_t *local = (struct local_info_t *)s->private;
793 data[0] = inb(dev->iobase + DAQP_DIGITAL_IO);
798 /* Digital output routine */
800 static int daqp_do_insn_write(struct comedi_device *dev,
801 struct comedi_subdevice *s,
802 struct comedi_insn *insn, unsigned int *data)
804 struct local_info_t *local = (struct local_info_t *)s->private;
809 outw(data[0] & 0xf, dev->iobase + DAQP_DIGITAL_IO);
814 /* daqp_attach is called via comedi_config to attach a comedi device
815 * to a /dev/comedi*. Note that this is different from daqp_cs_attach()
816 * which is called by the pcmcia subsystem to attach the PCMCIA card
817 * when it is inserted.
820 static int daqp_attach(struct comedi_device *dev, struct comedi_devconfig *it)
823 struct local_info_t *local = dev_table[it->options[0]];
824 struct comedi_subdevice *s;
826 if (it->options[0] < 0 || it->options[0] >= MAX_DEV || !local) {
827 printk("comedi%d: No such daqp device %d\n",
828 dev->minor, it->options[0]);
832 /* Typically brittle code that I don't completely understand,
833 * but "it works on my card". The intent is to pull the model
834 * number of the card out the PCMCIA CIS and stash it away as
835 * the COMEDI board_name. Looks like the third field in
836 * CISTPL_VERS_1 (offset 2) holds what we're looking for. If
837 * it doesn't work, who cares, just leave it as "DAQP".
840 strcpy(local->board_name, "DAQP");
841 dev->board_name = local->board_name;
842 if (local->link->prod_id[2]) {
843 if (strncmp(local->link->prod_id[2], "DAQP", 4) == 0) {
844 strncpy(local->board_name, local->link->prod_id[2],
845 sizeof(local->board_name));
849 dev->iobase = local->link->resource[0]->start;
851 ret = comedi_alloc_subdevices(dev, 4);
855 printk(KERN_INFO "comedi%d: attaching daqp%d (io 0x%04lx)\n",
856 dev->minor, it->options[0], dev->iobase);
858 s = &dev->subdevices[0];
859 dev->read_subdev = s;
861 s->type = COMEDI_SUBD_AI;
862 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
864 s->len_chanlist = 2048;
866 s->range_table = &range_daqp_ai;
867 s->insn_read = daqp_ai_insn_read;
868 s->do_cmdtest = daqp_ai_cmdtest;
869 s->do_cmd = daqp_ai_cmd;
870 s->cancel = daqp_ai_cancel;
872 s = &dev->subdevices[1];
873 dev->write_subdev = s;
875 s->type = COMEDI_SUBD_AO;
876 s->subdev_flags = SDF_WRITEABLE;
880 s->range_table = &range_daqp_ao;
881 s->insn_write = daqp_ao_insn_write;
883 s = &dev->subdevices[2];
885 s->type = COMEDI_SUBD_DI;
886 s->subdev_flags = SDF_READABLE;
889 s->insn_read = daqp_di_insn_read;
891 s = &dev->subdevices[3];
893 s->type = COMEDI_SUBD_DO;
894 s->subdev_flags = SDF_WRITEABLE;
897 s->insn_write = daqp_do_insn_write;
902 static void daqp_detach(struct comedi_device *dev)
904 /* Nothing to cleanup */
907 /*====================================================================
909 PCMCIA interface code
911 The rest of the code in this file is based on dummy_cs.c v1.24
912 from the Linux pcmcia_cs distribution v3.1.8 and is subject
913 to the following license agreement.
915 The remaining contents of this file are subject to the Mozilla Public
916 License Version 1.1 (the "License"); you may not use this file
917 except in compliance with the License. You may obtain a copy of
918 the License at http://www.mozilla.org/MPL/
920 Software distributed under the License is distributed on an "AS
921 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
922 implied. See the License for the specific language governing
923 rights and limitations under the License.
925 The initial developer of the original code is David A. Hinds
926 <dhinds@pcmcia.sourceforge.org>. Portions created by David A. Hinds
927 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
929 Alternatively, the contents of this file may be used under the
930 terms of the GNU Public License version 2 (the "GPL"), in which
931 case the provisions of the GPL are applicable instead of the
932 above. If you wish to allow the use of your version of this file
933 only under the terms of the GPL and not to allow others to use
934 your version of this file under the MPL, indicate your decision
935 by deleting the provisions above and replace them with the notice
936 and other provisions required by the GPL. If you do not delete
937 the provisions above, a recipient may use your version of this
938 file under either the MPL or the GPL.
940 ======================================================================*/
942 static void daqp_cs_config(struct pcmcia_device *link);
943 static void daqp_cs_release(struct pcmcia_device *link);
944 static int daqp_cs_suspend(struct pcmcia_device *p_dev);
945 static int daqp_cs_resume(struct pcmcia_device *p_dev);
947 static int daqp_cs_attach(struct pcmcia_device *);
948 static void daqp_cs_detach(struct pcmcia_device *);
950 static int daqp_cs_attach(struct pcmcia_device *link)
952 struct local_info_t *local;
955 dev_dbg(&link->dev, "daqp_cs_attach()\n");
957 for (i = 0; i < MAX_DEV; i++)
958 if (dev_table[i] == NULL)
961 printk(KERN_NOTICE "daqp_cs: no devices available\n");
965 /* Allocate space for private device-specific data */
966 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
970 local->table_index = i;
971 dev_table[i] = local;
975 daqp_cs_config(link);
978 } /* daqp_cs_attach */
980 static void daqp_cs_detach(struct pcmcia_device *link)
982 struct local_info_t *dev = link->priv;
985 daqp_cs_release(link);
987 /* Unlink device structure, and free it */
988 dev_table[dev->table_index] = NULL;
993 static int daqp_pcmcia_config_loop(struct pcmcia_device *p_dev, void *priv_data)
995 if (p_dev->config_index == 0)
998 return pcmcia_request_io(p_dev);
1001 static void daqp_cs_config(struct pcmcia_device *link)
1005 dev_dbg(&link->dev, "daqp_cs_config\n");
1007 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
1009 ret = pcmcia_loop_config(link, daqp_pcmcia_config_loop, NULL);
1011 dev_warn(&link->dev, "no configuration found\n");
1015 ret = pcmcia_request_irq(link, daqp_interrupt);
1019 ret = pcmcia_enable_device(link);
1026 daqp_cs_release(link);
1028 } /* daqp_cs_config */
1030 static void daqp_cs_release(struct pcmcia_device *link)
1032 dev_dbg(&link->dev, "daqp_cs_release\n");
1034 pcmcia_disable_device(link);
1035 } /* daqp_cs_release */
1037 static int daqp_cs_suspend(struct pcmcia_device *link)
1039 struct local_info_t *local = link->priv;
1041 /* Mark the device as stopped, to block IO until later */
1046 static int daqp_cs_resume(struct pcmcia_device *link)
1048 struct local_info_t *local = link->priv;
1055 /*====================================================================*/
1059 static const struct pcmcia_device_id daqp_cs_id_table[] = {
1060 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0027),
1064 MODULE_DEVICE_TABLE(pcmcia, daqp_cs_id_table);
1065 MODULE_AUTHOR("Brent Baccala <baccala@freesoft.org>");
1066 MODULE_DESCRIPTION("Comedi driver for Quatech DAQP PCMCIA data capture cards");
1067 MODULE_LICENSE("GPL");
1069 static struct pcmcia_driver daqp_cs_driver = {
1070 .probe = daqp_cs_attach,
1071 .remove = daqp_cs_detach,
1072 .suspend = daqp_cs_suspend,
1073 .resume = daqp_cs_resume,
1074 .id_table = daqp_cs_id_table,
1075 .owner = THIS_MODULE,
1076 .name = "quatech_daqp_cs",
1079 int __init init_module(void)
1081 pcmcia_register_driver(&daqp_cs_driver);
1082 comedi_driver_register(&driver_daqp);
1086 void __exit cleanup_module(void)
1088 comedi_driver_unregister(&driver_daqp);
1089 pcmcia_unregister_driver(&daqp_cs_driver);