]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/quatech_daqp_cs.c
Merge remote-tracking branch 'sh/sh-latest'
[karo-tx-linux.git] / drivers / staging / comedi / drivers / quatech_daqp_cs.c
1 /*======================================================================
2
3     comedi/drivers/quatech_daqp_cs.c
4
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.
8
9     COMEDI - Linux Control and Measurement Device Interface
10     Copyright (C) 1998 David A. Schleef <ds@schleef.org>
11     http://www.comedi.org/
12
13     quatech_daqp_cs.c 1.10
14
15     Documentation for the DAQP PCMCIA cards can be found on Quatech's site:
16
17                 ftp://ftp.quatech.com/Manuals/daqp-208.pdf
18
19     This manual is for both the DAQP-208 and the DAQP-308.
20
21     What works:
22
23         - A/D conversion
24             - 8 channels
25             - 4 gain ranges
26             - ground ref or differential
27             - single-shot and timed both supported
28         - D/A conversion, single-shot
29         - digital I/O
30
31     What doesn't:
32
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
39
40 ======================================================================*/
41
42 /*
43 Driver: quatech_daqp_cs
44 Description: Quatech DAQP PCMCIA data capture cards
45 Author: Brent Baccala <baccala@freesoft.org>
46 Status: works
47 Devices: [Quatech] DAQP-208 (daqp), DAQP-308
48 */
49
50 #include <linux/module.h>
51 #include "../comedidev.h"
52 #include <linux/semaphore.h>
53
54 #include <pcmcia/cistpl.h>
55 #include <pcmcia/cisreg.h>
56 #include <pcmcia/ds.h>
57
58 #include <linux/completion.h>
59
60 #include "comedi_fc.h"
61
62 struct daqp_private {
63         int stop;
64
65         enum { semaphore, buffer } interrupt_mode;
66
67         struct completion eos;
68
69         int count;
70 };
71
72 /* The DAQP communicates with the system through a 16 byte I/O window. */
73
74 #define DAQP_FIFO_SIZE          4096
75
76 #define DAQP_FIFO               0
77 #define DAQP_SCANLIST           1
78 #define DAQP_CONTROL            2
79 #define DAQP_STATUS             2
80 #define DAQP_DIGITAL_IO         3
81 #define DAQP_PACER_LOW          4
82 #define DAQP_PACER_MID          5
83 #define DAQP_PACER_HIGH         6
84 #define DAQP_COMMAND            7
85 #define DAQP_DA                 8
86 #define DAQP_TIMER              10
87 #define DAQP_AUX                15
88
89 #define DAQP_SCANLIST_DIFFERENTIAL      0x4000
90 #define DAQP_SCANLIST_GAIN(x)           ((x)<<12)
91 #define DAQP_SCANLIST_CHANNEL(x)        ((x)<<8)
92 #define DAQP_SCANLIST_START             0x0080
93 #define DAQP_SCANLIST_EXT_GAIN(x)       ((x)<<4)
94 #define DAQP_SCANLIST_EXT_CHANNEL(x)    (x)
95
96 #define DAQP_CONTROL_PACER_100kHz       0xc0
97 #define DAQP_CONTROL_PACER_1MHz         0x80
98 #define DAQP_CONTROL_PACER_5MHz         0x40
99 #define DAQP_CONTROL_PACER_EXTERNAL     0x00
100 #define DAQP_CONTORL_EXPANSION          0x20
101 #define DAQP_CONTROL_EOS_INT_ENABLE     0x10
102 #define DAQP_CONTROL_FIFO_INT_ENABLE    0x08
103 #define DAQP_CONTROL_TRIGGER_ONESHOT    0x00
104 #define DAQP_CONTROL_TRIGGER_CONTINUOUS 0x04
105 #define DAQP_CONTROL_TRIGGER_INTERNAL   0x00
106 #define DAQP_CONTROL_TRIGGER_EXTERNAL   0x02
107 #define DAQP_CONTROL_TRIGGER_RISING     0x00
108 #define DAQP_CONTROL_TRIGGER_FALLING    0x01
109
110 #define DAQP_STATUS_IDLE                0x80
111 #define DAQP_STATUS_RUNNING             0x40
112 #define DAQP_STATUS_EVENTS              0x38
113 #define DAQP_STATUS_DATA_LOST           0x20
114 #define DAQP_STATUS_END_OF_SCAN         0x10
115 #define DAQP_STATUS_FIFO_THRESHOLD      0x08
116 #define DAQP_STATUS_FIFO_FULL           0x04
117 #define DAQP_STATUS_FIFO_NEARFULL       0x02
118 #define DAQP_STATUS_FIFO_EMPTY          0x01
119
120 #define DAQP_COMMAND_ARM                0x80
121 #define DAQP_COMMAND_RSTF               0x40
122 #define DAQP_COMMAND_RSTQ               0x20
123 #define DAQP_COMMAND_STOP               0x10
124 #define DAQP_COMMAND_LATCH              0x08
125 #define DAQP_COMMAND_100kHz             0x00
126 #define DAQP_COMMAND_50kHz              0x02
127 #define DAQP_COMMAND_25kHz              0x04
128 #define DAQP_COMMAND_FIFO_DATA          0x01
129 #define DAQP_COMMAND_FIFO_PROGRAM       0x00
130
131 #define DAQP_AUX_TRIGGER_TTL            0x00
132 #define DAQP_AUX_TRIGGER_ANALOG         0x80
133 #define DAQP_AUX_TRIGGER_PRETRIGGER     0x40
134 #define DAQP_AUX_TIMER_INT_ENABLE       0x20
135 #define DAQP_AUX_TIMER_RELOAD           0x00
136 #define DAQP_AUX_TIMER_PAUSE            0x08
137 #define DAQP_AUX_TIMER_GO               0x10
138 #define DAQP_AUX_TIMER_GO_EXTERNAL      0x18
139 #define DAQP_AUX_TIMER_EXTERNAL_SRC     0x04
140 #define DAQP_AUX_TIMER_INTERNAL_SRC     0x00
141 #define DAQP_AUX_DA_DIRECT              0x00
142 #define DAQP_AUX_DA_OVERFLOW            0x01
143 #define DAQP_AUX_DA_EXTERNAL            0x02
144 #define DAQP_AUX_DA_PACER               0x03
145
146 #define DAQP_AUX_RUNNING                0x80
147 #define DAQP_AUX_TRIGGERED              0x40
148 #define DAQP_AUX_DA_BUFFER              0x20
149 #define DAQP_AUX_TIMER_OVERFLOW         0x10
150 #define DAQP_AUX_CONVERSION             0x08
151 #define DAQP_AUX_DATA_LOST              0x04
152 #define DAQP_AUX_FIFO_NEARFULL          0x02
153 #define DAQP_AUX_FIFO_EMPTY             0x01
154
155 static const struct comedi_lrange range_daqp_ai = {
156         4, {
157                 BIP_RANGE(10),
158                 BIP_RANGE(5),
159                 BIP_RANGE(2.5),
160                 BIP_RANGE(1.25)
161         }
162 };
163
164 /* Cancel a running acquisition */
165
166 static int daqp_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
167 {
168         struct daqp_private *devpriv = dev->private;
169
170         if (devpriv->stop)
171                 return -EIO;
172
173         outb(DAQP_COMMAND_STOP, dev->iobase + DAQP_COMMAND);
174
175         /* flush any linguring data in FIFO - superfluous here */
176         /* outb(DAQP_COMMAND_RSTF, dev->iobase+DAQP_COMMAND); */
177
178         devpriv->interrupt_mode = semaphore;
179
180         return 0;
181 }
182
183 /* Interrupt handler
184  *
185  * Operates in one of two modes.  If devpriv->interrupt_mode is
186  * 'semaphore', just signal the devpriv->eos completion and return
187  * (one-shot mode).  Otherwise (continuous mode), read data in from
188  * the card, transfer it to the buffer provided by the higher-level
189  * comedi kernel module, and signal various comedi callback routines,
190  * which run pretty quick.
191  */
192 static enum irqreturn daqp_interrupt(int irq, void *dev_id)
193 {
194         struct comedi_device *dev = dev_id;
195         struct daqp_private *devpriv = dev->private;
196         struct comedi_subdevice *s = dev->read_subdev;
197         int loop_limit = 10000;
198         int status;
199
200         if (!dev->attached)
201                 return IRQ_NONE;
202
203         switch (devpriv->interrupt_mode) {
204         case semaphore:
205                 complete(&devpriv->eos);
206                 break;
207
208         case buffer:
209                 while (!((status = inb(dev->iobase + DAQP_STATUS))
210                          & DAQP_STATUS_FIFO_EMPTY)) {
211
212                         short data;
213
214                         if (status & DAQP_STATUS_DATA_LOST) {
215                                 s->async->events |=
216                                     COMEDI_CB_EOA | COMEDI_CB_OVERFLOW;
217                                 dev_warn(dev->class_dev, "data lost\n");
218                                 daqp_ai_cancel(dev, s);
219                                 break;
220                         }
221
222                         data = inb(dev->iobase + DAQP_FIFO);
223                         data |= inb(dev->iobase + DAQP_FIFO) << 8;
224                         data ^= 0x8000;
225
226                         comedi_buf_put(s->async, data);
227
228                         /* If there's a limit, decrement it
229                          * and stop conversion if zero
230                          */
231
232                         if (devpriv->count > 0) {
233                                 devpriv->count--;
234                                 if (devpriv->count == 0) {
235                                         daqp_ai_cancel(dev, s);
236                                         s->async->events |= COMEDI_CB_EOA;
237                                         break;
238                                 }
239                         }
240
241                         if ((loop_limit--) <= 0)
242                                 break;
243                 }
244
245                 if (loop_limit <= 0) {
246                         dev_warn(dev->class_dev,
247                                  "loop_limit reached in daqp_interrupt()\n");
248                         daqp_ai_cancel(dev, s);
249                         s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
250                 }
251
252                 s->async->events |= COMEDI_CB_BLOCK;
253
254                 comedi_event(dev, s);
255         }
256         return IRQ_HANDLED;
257 }
258
259 static void daqp_ai_set_one_scanlist_entry(struct comedi_device *dev,
260                                            unsigned int chanspec,
261                                            int start)
262 {
263         unsigned int chan = CR_CHAN(chanspec);
264         unsigned int range = CR_RANGE(chanspec);
265         unsigned int aref = CR_AREF(chanspec);
266         unsigned int val;
267
268         val = DAQP_SCANLIST_CHANNEL(chan) | DAQP_SCANLIST_GAIN(range);
269
270         if (aref == AREF_DIFF)
271                 val |= DAQP_SCANLIST_DIFFERENTIAL;
272
273         if (start)
274                 val |= DAQP_SCANLIST_START;
275
276         outb(val & 0xff, dev->iobase + DAQP_SCANLIST);
277         outb((val >> 8) & 0xff, dev->iobase + DAQP_SCANLIST);
278 }
279
280 /* One-shot analog data acquisition routine */
281
282 static int daqp_ai_insn_read(struct comedi_device *dev,
283                              struct comedi_subdevice *s,
284                              struct comedi_insn *insn, unsigned int *data)
285 {
286         struct daqp_private *devpriv = dev->private;
287         int i;
288         int v;
289         int counter = 10000;
290
291         if (devpriv->stop)
292                 return -EIO;
293
294         /* Stop any running conversion */
295         daqp_ai_cancel(dev, s);
296
297         outb(0, dev->iobase + DAQP_AUX);
298
299         /* Reset scan list queue */
300         outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND);
301
302         /* Program one scan list entry */
303         daqp_ai_set_one_scanlist_entry(dev, insn->chanspec, 1);
304
305         /* Reset data FIFO (see page 28 of DAQP User's Manual) */
306
307         outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND);
308
309         /* Set trigger */
310
311         v = DAQP_CONTROL_TRIGGER_ONESHOT | DAQP_CONTROL_TRIGGER_INTERNAL
312             | DAQP_CONTROL_PACER_100kHz | DAQP_CONTROL_EOS_INT_ENABLE;
313
314         outb(v, dev->iobase + DAQP_CONTROL);
315
316         /* Reset any pending interrupts (my card has a tendency to require
317          * require multiple reads on the status register to achieve this)
318          */
319
320         while (--counter
321                && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS))
322                 ;
323         if (!counter) {
324                 dev_err(dev->class_dev,
325                         "couldn't clear interrupts in status register\n");
326                 return -1;
327         }
328
329         init_completion(&devpriv->eos);
330         devpriv->interrupt_mode = semaphore;
331
332         for (i = 0; i < insn->n; i++) {
333
334                 /* Start conversion */
335                 outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA,
336                      dev->iobase + DAQP_COMMAND);
337
338                 /* Wait for interrupt service routine to unblock completion */
339                 /* Maybe could use a timeout here, but it's interruptible */
340                 if (wait_for_completion_interruptible(&devpriv->eos))
341                         return -EINTR;
342
343                 data[i] = inb(dev->iobase + DAQP_FIFO);
344                 data[i] |= inb(dev->iobase + DAQP_FIFO) << 8;
345                 data[i] ^= 0x8000;
346         }
347
348         return insn->n;
349 }
350
351 /* This function converts ns nanoseconds to a counter value suitable
352  * for programming the device.  We always use the DAQP's 5 MHz clock,
353  * which with its 24-bit counter, allows values up to 84 seconds.
354  * Also, the function adjusts ns so that it cooresponds to the actual
355  * time that the device will use.
356  */
357
358 static int daqp_ns_to_timer(unsigned int *ns, int round)
359 {
360         int timer;
361
362         timer = *ns / 200;
363         *ns = timer * 200;
364
365         return timer;
366 }
367
368 /* cmdtest tests a particular command to see if it is valid.
369  * Using the cmdtest ioctl, a user can create a valid cmd
370  * and then have it executed by the cmd ioctl.
371  *
372  * cmdtest returns 1,2,3,4 or 0, depending on which tests
373  * the command passes.
374  */
375
376 static int daqp_ai_cmdtest(struct comedi_device *dev,
377                            struct comedi_subdevice *s, struct comedi_cmd *cmd)
378 {
379         int err = 0;
380         int tmp;
381
382         /* Step 1 : check if triggers are trivially valid */
383
384         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
385         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
386                                         TRIG_TIMER | TRIG_FOLLOW);
387         err |= cfc_check_trigger_src(&cmd->convert_src,
388                                         TRIG_TIMER | TRIG_NOW);
389         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
390         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
391
392         if (err)
393                 return 1;
394
395         /* Step 2a : make sure trigger sources are unique */
396
397         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
398         err |= cfc_check_trigger_is_unique(cmd->convert_src);
399         err |= cfc_check_trigger_is_unique(cmd->stop_src);
400
401         /* Step 2b : and mutually compatible */
402
403         if (err)
404                 return 2;
405
406         /* Step 3: check if arguments are trivially valid */
407
408         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
409
410 #define MAX_SPEED       10000   /* 100 kHz - in nanoseconds */
411
412         if (cmd->scan_begin_src == TRIG_TIMER)
413                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
414                                                  MAX_SPEED);
415
416         /* If both scan_begin and convert are both timer values, the only
417          * way that can make sense is if the scan time is the number of
418          * conversions times the convert time
419          */
420
421         if (cmd->scan_begin_src == TRIG_TIMER && cmd->convert_src == TRIG_TIMER
422             && cmd->scan_begin_arg != cmd->convert_arg * cmd->scan_end_arg) {
423                 err |= -EINVAL;
424         }
425
426         if (cmd->convert_src == TRIG_TIMER)
427                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, MAX_SPEED);
428
429         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
430
431         if (cmd->stop_src == TRIG_COUNT)
432                 err |= cfc_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
433         else    /* TRIG_NONE */
434                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
435
436         if (err)
437                 return 3;
438
439         /* step 4: fix up any arguments */
440
441         if (cmd->scan_begin_src == TRIG_TIMER) {
442                 tmp = cmd->scan_begin_arg;
443                 daqp_ns_to_timer(&cmd->scan_begin_arg,
444                                  cmd->flags & TRIG_ROUND_MASK);
445                 if (tmp != cmd->scan_begin_arg)
446                         err++;
447         }
448
449         if (cmd->convert_src == TRIG_TIMER) {
450                 tmp = cmd->convert_arg;
451                 daqp_ns_to_timer(&cmd->convert_arg,
452                                  cmd->flags & TRIG_ROUND_MASK);
453                 if (tmp != cmd->convert_arg)
454                         err++;
455         }
456
457         if (err)
458                 return 4;
459
460         return 0;
461 }
462
463 static int daqp_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
464 {
465         struct daqp_private *devpriv = dev->private;
466         struct comedi_cmd *cmd = &s->async->cmd;
467         int counter;
468         int scanlist_start_on_every_entry;
469         int threshold;
470
471         int i;
472         int v;
473
474         if (devpriv->stop)
475                 return -EIO;
476
477         /* Stop any running conversion */
478         daqp_ai_cancel(dev, s);
479
480         outb(0, dev->iobase + DAQP_AUX);
481
482         /* Reset scan list queue */
483         outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND);
484
485         /* Program pacer clock
486          *
487          * There's two modes we can operate in.  If convert_src is
488          * TRIG_TIMER, then convert_arg specifies the time between
489          * each conversion, so we program the pacer clock to that
490          * frequency and set the SCANLIST_START bit on every scanlist
491          * entry.  Otherwise, convert_src is TRIG_NOW, which means
492          * we want the fastest possible conversions, scan_begin_src
493          * is TRIG_TIMER, and scan_begin_arg specifies the time between
494          * each scan, so we program the pacer clock to this frequency
495          * and only set the SCANLIST_START bit on the first entry.
496          */
497
498         if (cmd->convert_src == TRIG_TIMER) {
499                 counter = daqp_ns_to_timer(&cmd->convert_arg,
500                                                cmd->flags & TRIG_ROUND_MASK);
501                 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW);
502                 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID);
503                 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH);
504                 scanlist_start_on_every_entry = 1;
505         } else {
506                 counter = daqp_ns_to_timer(&cmd->scan_begin_arg,
507                                                cmd->flags & TRIG_ROUND_MASK);
508                 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW);
509                 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID);
510                 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH);
511                 scanlist_start_on_every_entry = 0;
512         }
513
514         /* Program scan list */
515         for (i = 0; i < cmd->chanlist_len; i++) {
516                 int start = (i == 0 || scanlist_start_on_every_entry);
517
518                 daqp_ai_set_one_scanlist_entry(dev, cmd->chanlist[i], start);
519         }
520
521         /* Now it's time to program the FIFO threshold, basically the
522          * number of samples the card will buffer before it interrupts
523          * the CPU.
524          *
525          * If we don't have a stop count, then use half the size of
526          * the FIFO (the manufacturer's recommendation).  Consider
527          * that the FIFO can hold 2K samples (4K bytes).  With the
528          * threshold set at half the FIFO size, we have a margin of
529          * error of 1024 samples.  At the chip's maximum sample rate
530          * of 100,000 Hz, the CPU would have to delay interrupt
531          * service for a full 10 milliseconds in order to lose data
532          * here (as opposed to higher up in the kernel).  I've never
533          * seen it happen.  However, for slow sample rates it may
534          * buffer too much data and introduce too much delay for the
535          * user application.
536          *
537          * If we have a stop count, then things get more interesting.
538          * If the stop count is less than the FIFO size (actually
539          * three-quarters of the FIFO size - see below), we just use
540          * the stop count itself as the threshold, the card interrupts
541          * us when that many samples have been taken, and we kill the
542          * acquisition at that point and are done.  If the stop count
543          * is larger than that, then we divide it by 2 until it's less
544          * than three quarters of the FIFO size (we always leave the
545          * top quarter of the FIFO as protection against sluggish CPU
546          * interrupt response) and use that as the threshold.  So, if
547          * the stop count is 4000 samples, we divide by two twice to
548          * get 1000 samples, use that as the threshold, take four
549          * interrupts to get our 4000 samples and are done.
550          *
551          * The algorithm could be more clever.  For example, if 81000
552          * samples are requested, we could set the threshold to 1500
553          * samples and take 54 interrupts to get 81000.  But 54 isn't
554          * a power of two, so this algorithm won't find that option.
555          * Instead, it'll set the threshold at 1266 and take 64
556          * interrupts to get 81024 samples, of which the last 24 will
557          * be discarded... but we won't get the last interrupt until
558          * they've been collected.  To find the first option, the
559          * computer could look at the prime decomposition of the
560          * sample count (81000 = 3^4 * 5^3 * 2^3) and factor it into a
561          * threshold (1500 = 3 * 5^3 * 2^2) and an interrupt count (54
562          * = 3^3 * 2).  Hmmm... a one-line while loop or prime
563          * decomposition of integers... I'll leave it the way it is.
564          *
565          * I'll also note a mini-race condition before ignoring it in
566          * the code.  Let's say we're taking 4000 samples, as before.
567          * After 1000 samples, we get an interrupt.  But before that
568          * interrupt is completely serviced, another sample is taken
569          * and loaded into the FIFO.  Since the interrupt handler
570          * empties the FIFO before returning, it will read 1001 samples.
571          * If that happens four times, we'll end up taking 4004 samples,
572          * not 4000.  The interrupt handler will discard the extra four
573          * samples (by halting the acquisition with four samples still
574          * in the FIFO), but we will have to wait for them.
575          *
576          * In short, this code works pretty well, but for either of
577          * the two reasons noted, might end up waiting for a few more
578          * samples than actually requested.  Shouldn't make too much
579          * of a difference.
580          */
581
582         /* Save away the number of conversions we should perform, and
583          * compute the FIFO threshold (in bytes, not samples - that's
584          * why we multiple devpriv->count by 2 = sizeof(sample))
585          */
586
587         if (cmd->stop_src == TRIG_COUNT) {
588                 devpriv->count = cmd->stop_arg * cmd->scan_end_arg;
589                 threshold = 2 * devpriv->count;
590                 while (threshold > DAQP_FIFO_SIZE * 3 / 4)
591                         threshold /= 2;
592         } else {
593                 devpriv->count = -1;
594                 threshold = DAQP_FIFO_SIZE / 2;
595         }
596
597         /* Reset data FIFO (see page 28 of DAQP User's Manual) */
598
599         outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND);
600
601         /* Set FIFO threshold.  First two bytes are near-empty
602          * threshold, which is unused; next two bytes are near-full
603          * threshold.  We computed the number of bytes we want in the
604          * FIFO when the interrupt is generated, what the card wants
605          * is actually the number of available bytes left in the FIFO
606          * when the interrupt is to happen.
607          */
608
609         outb(0x00, dev->iobase + DAQP_FIFO);
610         outb(0x00, dev->iobase + DAQP_FIFO);
611
612         outb((DAQP_FIFO_SIZE - threshold) & 0xff, dev->iobase + DAQP_FIFO);
613         outb((DAQP_FIFO_SIZE - threshold) >> 8, dev->iobase + DAQP_FIFO);
614
615         /* Set trigger */
616
617         v = DAQP_CONTROL_TRIGGER_CONTINUOUS | DAQP_CONTROL_TRIGGER_INTERNAL
618             | DAQP_CONTROL_PACER_5MHz | DAQP_CONTROL_FIFO_INT_ENABLE;
619
620         outb(v, dev->iobase + DAQP_CONTROL);
621
622         /* Reset any pending interrupts (my card has a tendency to require
623          * require multiple reads on the status register to achieve this)
624          */
625         counter = 100;
626         while (--counter
627                && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS))
628                 ;
629         if (!counter) {
630                 dev_err(dev->class_dev,
631                         "couldn't clear interrupts in status register\n");
632                 return -1;
633         }
634
635         devpriv->interrupt_mode = buffer;
636
637         /* Start conversion */
638         outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA,
639              dev->iobase + DAQP_COMMAND);
640
641         return 0;
642 }
643
644 static int daqp_ao_insn_write(struct comedi_device *dev,
645                               struct comedi_subdevice *s,
646                               struct comedi_insn *insn,
647                               unsigned int *data)
648 {
649         struct daqp_private *devpriv = dev->private;
650         unsigned int chan = CR_CHAN(insn->chanspec);
651         unsigned int val;
652         int i;
653
654         if (devpriv->stop)
655                 return -EIO;
656
657         /* Make sure D/A update mode is direct update */
658         outb(0, dev->iobase + DAQP_AUX);
659
660         for (i = 0; i > insn->n; i++) {
661                 val = data[0];
662                 val &= 0x0fff;
663                 val ^= 0x0800;          /* Flip the sign */
664                 val |= (chan << 12);
665
666                 outw(val, dev->iobase + DAQP_DA);
667         }
668
669         return insn->n;
670 }
671
672 static int daqp_di_insn_bits(struct comedi_device *dev,
673                              struct comedi_subdevice *s,
674                              struct comedi_insn *insn,
675                              unsigned int *data)
676 {
677         struct daqp_private *devpriv = dev->private;
678
679         if (devpriv->stop)
680                 return -EIO;
681
682         data[0] = inb(dev->iobase + DAQP_DIGITAL_IO);
683
684         return insn->n;
685 }
686
687 static int daqp_do_insn_bits(struct comedi_device *dev,
688                              struct comedi_subdevice *s,
689                              struct comedi_insn *insn,
690                              unsigned int *data)
691 {
692         struct daqp_private *devpriv = dev->private;
693         unsigned int mask = data[0];
694         unsigned int bits = data[1];
695
696         if (devpriv->stop)
697                 return -EIO;
698
699         if (mask) {
700                 s->state &= ~mask;
701                 s->state |= (bits & mask);
702
703                 outb(s->state, dev->iobase + DAQP_DIGITAL_IO);
704         }
705
706         data[1] = s->state;
707
708         return insn->n;
709 }
710
711 static int daqp_auto_attach(struct comedi_device *dev,
712                             unsigned long context)
713 {
714         struct pcmcia_device *link = comedi_to_pcmcia_dev(dev);
715         struct daqp_private *devpriv;
716         struct comedi_subdevice *s;
717         int ret;
718
719         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
720         if (!devpriv)
721                 return -ENOMEM;
722
723         link->config_flags |= CONF_AUTO_SET_IO | CONF_ENABLE_IRQ;
724         ret = comedi_pcmcia_enable(dev, NULL);
725         if (ret)
726                 return ret;
727         dev->iobase = link->resource[0]->start;
728
729         link->priv = dev;
730         ret = pcmcia_request_irq(link, daqp_interrupt);
731         if (ret)
732                 return ret;
733
734         ret = comedi_alloc_subdevices(dev, 4);
735         if (ret)
736                 return ret;
737
738         s = &dev->subdevices[0];
739         dev->read_subdev = s;
740         s->type         = COMEDI_SUBD_AI;
741         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
742         s->n_chan       = 8;
743         s->len_chanlist = 2048;
744         s->maxdata      = 0xffff;
745         s->range_table  = &range_daqp_ai;
746         s->insn_read    = daqp_ai_insn_read;
747         s->do_cmdtest   = daqp_ai_cmdtest;
748         s->do_cmd       = daqp_ai_cmd;
749         s->cancel       = daqp_ai_cancel;
750
751         s = &dev->subdevices[1];
752         s->type         = COMEDI_SUBD_AO;
753         s->subdev_flags = SDF_WRITEABLE;
754         s->n_chan       = 2;
755         s->maxdata      = 0x0fff;
756         s->range_table  = &range_bipolar5;
757         s->insn_write   = daqp_ao_insn_write;
758
759         s = &dev->subdevices[2];
760         s->type         = COMEDI_SUBD_DI;
761         s->subdev_flags = SDF_READABLE;
762         s->n_chan       = 1;
763         s->maxdata      = 1;
764         s->insn_bits    = daqp_di_insn_bits;
765
766         s = &dev->subdevices[3];
767         s->type         = COMEDI_SUBD_DO;
768         s->subdev_flags = SDF_WRITEABLE;
769         s->n_chan       = 1;
770         s->maxdata      = 1;
771         s->insn_bits    = daqp_do_insn_bits;
772
773         return 0;
774 }
775
776 static struct comedi_driver driver_daqp = {
777         .driver_name    = "quatech_daqp_cs",
778         .module         = THIS_MODULE,
779         .auto_attach    = daqp_auto_attach,
780         .detach         = comedi_pcmcia_disable,
781 };
782
783 static int daqp_cs_suspend(struct pcmcia_device *link)
784 {
785         struct comedi_device *dev = link->priv;
786         struct daqp_private *devpriv = dev ? dev->private : NULL;
787
788         /* Mark the device as stopped, to block IO until later */
789         if (devpriv)
790                 devpriv->stop = 1;
791
792         return 0;
793 }
794
795 static int daqp_cs_resume(struct pcmcia_device *link)
796 {
797         struct comedi_device *dev = link->priv;
798         struct daqp_private *devpriv = dev ? dev->private : NULL;
799
800         if (devpriv)
801                 devpriv->stop = 0;
802
803         return 0;
804 }
805
806 static int daqp_cs_attach(struct pcmcia_device *link)
807 {
808         return comedi_pcmcia_auto_config(link, &driver_daqp);
809 }
810
811 static const struct pcmcia_device_id daqp_cs_id_table[] = {
812         PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0027),
813         PCMCIA_DEVICE_NULL
814 };
815 MODULE_DEVICE_TABLE(pcmcia, daqp_cs_id_table);
816
817 static struct pcmcia_driver daqp_cs_driver = {
818         .name           = "quatech_daqp_cs",
819         .owner          = THIS_MODULE,
820         .id_table       = daqp_cs_id_table,
821         .probe          = daqp_cs_attach,
822         .remove         = comedi_pcmcia_auto_unconfig,
823         .suspend        = daqp_cs_suspend,
824         .resume         = daqp_cs_resume,
825 };
826 module_comedi_pcmcia_driver(driver_daqp, daqp_cs_driver);
827
828 MODULE_DESCRIPTION("Comedi driver for Quatech DAQP PCMCIA data capture cards");
829 MODULE_AUTHOR("Brent Baccala <baccala@freesoft.org>");
830 MODULE_LICENSE("GPL");