]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/staging/comedi/drivers/quatech_daqp_cs.c
pcmcia: move driver name to struct pcmcia_driver
[mirror_ubuntu-focal-kernel.git] / drivers / staging / comedi / drivers / quatech_daqp_cs.c
CommitLineData
62ed6662
BB
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
3420f6b4 17 ftp://ftp.quatech.com/Manuals/daqp-208.pdf
62ed6662
BB
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/*
43Driver: quatech_daqp_cs
44Description: Quatech DAQP PCMCIA data capture cards
45Author: Brent Baccala <baccala@freesoft.org>
46Status: works
47Devices: [Quatech] DAQP-208 (daqp), DAQP-308
48*/
49
50#include "../comedidev.h"
3142788b 51#include <linux/semaphore.h>
62ed6662 52
62ed6662
BB
53#include <pcmcia/cistpl.h>
54#include <pcmcia/cisreg.h>
55#include <pcmcia/ds.h>
56
7ec52ed2
AIB
57#include <linux/completion.h>
58
62ed6662
BB
59/* Maximum number of separate DAQP devices we'll allow */
60#define MAX_DEV 4
61
ab64f663 62struct local_info_t {
62ed6662 63 struct pcmcia_device *link;
62ed6662
BB
64 int stop;
65 int table_index;
66 char board_name[32];
67
68 enum { semaphore, buffer } interrupt_mode;
69
7ec52ed2 70 struct completion eos;
62ed6662 71
71b5f4f1 72 struct comedi_device *dev;
34c43922 73 struct comedi_subdevice *s;
62ed6662 74 int count;
ab64f663 75};
62ed6662
BB
76
77/* A list of "instances" of the device. */
78
ab64f663 79static struct local_info_t *dev_table[MAX_DEV] = { NULL, /* ... */ };
62ed6662
BB
80
81/* The DAQP communicates with the system through a 16 byte I/O window. */
82
83#define DAQP_FIFO_SIZE 4096
84
85#define DAQP_FIFO 0
86#define DAQP_SCANLIST 1
87#define DAQP_CONTROL 2
88#define DAQP_STATUS 2
89#define DAQP_DIGITAL_IO 3
90#define DAQP_PACER_LOW 4
91#define DAQP_PACER_MID 5
92#define DAQP_PACER_HIGH 6
93#define DAQP_COMMAND 7
94#define DAQP_DA 8
95#define DAQP_TIMER 10
96#define DAQP_AUX 15
97
98#define DAQP_SCANLIST_DIFFERENTIAL 0x4000
99#define DAQP_SCANLIST_GAIN(x) ((x)<<12)
100#define DAQP_SCANLIST_CHANNEL(x) ((x)<<8)
101#define DAQP_SCANLIST_START 0x0080
102#define DAQP_SCANLIST_EXT_GAIN(x) ((x)<<4)
103#define DAQP_SCANLIST_EXT_CHANNEL(x) (x)
104
105#define DAQP_CONTROL_PACER_100kHz 0xc0
106#define DAQP_CONTROL_PACER_1MHz 0x80
107#define DAQP_CONTROL_PACER_5MHz 0x40
108#define DAQP_CONTROL_PACER_EXTERNAL 0x00
109#define DAQP_CONTORL_EXPANSION 0x20
110#define DAQP_CONTROL_EOS_INT_ENABLE 0x10
111#define DAQP_CONTROL_FIFO_INT_ENABLE 0x08
112#define DAQP_CONTROL_TRIGGER_ONESHOT 0x00
113#define DAQP_CONTROL_TRIGGER_CONTINUOUS 0x04
114#define DAQP_CONTROL_TRIGGER_INTERNAL 0x00
115#define DAQP_CONTROL_TRIGGER_EXTERNAL 0x02
116#define DAQP_CONTROL_TRIGGER_RISING 0x00
117#define DAQP_CONTROL_TRIGGER_FALLING 0x01
118
119#define DAQP_STATUS_IDLE 0x80
120#define DAQP_STATUS_RUNNING 0x40
121#define DAQP_STATUS_EVENTS 0x38
122#define DAQP_STATUS_DATA_LOST 0x20
123#define DAQP_STATUS_END_OF_SCAN 0x10
124#define DAQP_STATUS_FIFO_THRESHOLD 0x08
125#define DAQP_STATUS_FIFO_FULL 0x04
126#define DAQP_STATUS_FIFO_NEARFULL 0x02
127#define DAQP_STATUS_FIFO_EMPTY 0x01
128
129#define DAQP_COMMAND_ARM 0x80
130#define DAQP_COMMAND_RSTF 0x40
131#define DAQP_COMMAND_RSTQ 0x20
132#define DAQP_COMMAND_STOP 0x10
133#define DAQP_COMMAND_LATCH 0x08
134#define DAQP_COMMAND_100kHz 0x00
135#define DAQP_COMMAND_50kHz 0x02
136#define DAQP_COMMAND_25kHz 0x04
137#define DAQP_COMMAND_FIFO_DATA 0x01
138#define DAQP_COMMAND_FIFO_PROGRAM 0x00
139
140#define DAQP_AUX_TRIGGER_TTL 0x00
141#define DAQP_AUX_TRIGGER_ANALOG 0x80
142#define DAQP_AUX_TRIGGER_PRETRIGGER 0x40
143#define DAQP_AUX_TIMER_INT_ENABLE 0x20
144#define DAQP_AUX_TIMER_RELOAD 0x00
145#define DAQP_AUX_TIMER_PAUSE 0x08
146#define DAQP_AUX_TIMER_GO 0x10
147#define DAQP_AUX_TIMER_GO_EXTERNAL 0x18
148#define DAQP_AUX_TIMER_EXTERNAL_SRC 0x04
149#define DAQP_AUX_TIMER_INTERNAL_SRC 0x00
150#define DAQP_AUX_DA_DIRECT 0x00
151#define DAQP_AUX_DA_OVERFLOW 0x01
152#define DAQP_AUX_DA_EXTERNAL 0x02
153#define DAQP_AUX_DA_PACER 0x03
154
155#define DAQP_AUX_RUNNING 0x80
156#define DAQP_AUX_TRIGGERED 0x40
157#define DAQP_AUX_DA_BUFFER 0x20
158#define DAQP_AUX_TIMER_OVERFLOW 0x10
159#define DAQP_AUX_CONVERSION 0x08
160#define DAQP_AUX_DATA_LOST 0x04
161#define DAQP_AUX_FIFO_NEARFULL 0x02
162#define DAQP_AUX_FIFO_EMPTY 0x01
163
164/* These range structures tell COMEDI how the sample values map to
68c3dbff
BP
165 * voltages. The A/D converter has four .ranges = +/- 10V through
166 * +/- 1.25V, and the D/A converter has only .one = +/- 5V.
62ed6662
BB
167 */
168
9ced1de6 169static const struct comedi_lrange range_daqp_ai = { 4, {
0a85b6f0
MT
170 BIP_RANGE(10),
171 BIP_RANGE(5),
172 BIP_RANGE(2.5),
173 BIP_RANGE(1.25)
174 }
62ed6662
BB
175};
176
9ced1de6 177static const struct comedi_lrange range_daqp_ao = { 1, {BIP_RANGE(5)} };
62ed6662
BB
178
179/*====================================================================*/
180
181/* comedi interface code */
182
da91b269
BP
183static int daqp_attach(struct comedi_device *dev, struct comedi_devconfig *it);
184static int daqp_detach(struct comedi_device *dev);
139dfbdf 185static struct comedi_driver driver_daqp = {
68c3dbff
BP
186 .driver_name = "quatech_daqp_cs",
187 .module = THIS_MODULE,
188 .attach = daqp_attach,
189 .detach = daqp_detach,
62ed6662
BB
190};
191
192#ifdef DAQP_DEBUG
193
da91b269 194static void daqp_dump(struct comedi_device *dev)
62ed6662 195{
3420f6b4 196 printk(KERN_INFO "DAQP: status %02x; aux status %02x\n",
0a85b6f0 197 inb(dev->iobase + DAQP_STATUS), inb(dev->iobase + DAQP_AUX));
62ed6662
BB
198}
199
200static void hex_dump(char *str, void *ptr, int len)
201{
202 unsigned char *cptr = ptr;
203 int i;
204
205 printk(str);
206
207 for (i = 0; i < len; i++) {
3420f6b4 208 if (i % 16 == 0)
5fc9e4d5 209 printk("\n%p:", cptr);
3420f6b4 210
62ed6662
BB
211 printk(" %02x", *(cptr++));
212 }
213 printk("\n");
214}
215
216#endif
217
218/* Cancel a running acquisition */
219
da91b269 220static int daqp_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
62ed6662 221{
0a85b6f0 222 struct local_info_t *local = (struct local_info_t *)s->private;
62ed6662 223
3420f6b4 224 if (local->stop)
62ed6662 225 return -EIO;
3420f6b4 226
62ed6662
BB
227
228 outb(DAQP_COMMAND_STOP, dev->iobase + DAQP_COMMAND);
229
230 /* flush any linguring data in FIFO - superfluous here */
231 /* outb(DAQP_COMMAND_RSTF, dev->iobase+DAQP_COMMAND); */
232
233 local->interrupt_mode = semaphore;
234
235 return 0;
236}
237
238/* Interrupt handler
239 *
240 * Operates in one of two modes. If local->interrupt_mode is
7ec52ed2 241 * 'semaphore', just signal the local->eos completion and return
62ed6662
BB
242 * (one-shot mode). Otherwise (continuous mode), read data in from
243 * the card, transfer it to the buffer provided by the higher-level
244 * comedi kernel module, and signal various comedi callback routines,
245 * which run pretty quick.
246 */
e3752a1d 247static enum irqreturn daqp_interrupt(int irq, void *dev_id)
62ed6662 248{
0a85b6f0 249 struct local_info_t *local = (struct local_info_t *)dev_id;
71b5f4f1 250 struct comedi_device *dev;
34c43922 251 struct comedi_subdevice *s;
62ed6662
BB
252 int loop_limit = 10000;
253 int status;
254
255 if (local == NULL) {
256 printk(KERN_WARNING
0a85b6f0 257 "daqp_interrupt(): irq %d for unknown device.\n", irq);
e3752a1d 258 return IRQ_NONE;
62ed6662
BB
259 }
260
261 dev = local->dev;
262 if (dev == NULL) {
263 printk(KERN_WARNING "daqp_interrupt(): NULL comedi_device.\n");
e3752a1d 264 return IRQ_NONE;
62ed6662
BB
265 }
266
267 if (!dev->attached) {
268 printk(KERN_WARNING
0a85b6f0 269 "daqp_interrupt(): struct comedi_device not yet attached.\n");
e3752a1d 270 return IRQ_NONE;
62ed6662
BB
271 }
272
273 s = local->s;
274 if (s == NULL) {
275 printk(KERN_WARNING
0a85b6f0 276 "daqp_interrupt(): NULL comedi_subdevice.\n");
e3752a1d 277 return IRQ_NONE;
62ed6662
BB
278 }
279
0a85b6f0 280 if ((struct local_info_t *)s->private != local) {
62ed6662 281 printk(KERN_WARNING
0a85b6f0 282 "daqp_interrupt(): invalid comedi_subdevice.\n");
e3752a1d 283 return IRQ_NONE;
62ed6662
BB
284 }
285
286 switch (local->interrupt_mode) {
287
288 case semaphore:
289
7ec52ed2 290 complete(&local->eos);
62ed6662
BB
291 break;
292
293 case buffer:
294
295 while (!((status = inb(dev->iobase + DAQP_STATUS))
0a85b6f0 296 & DAQP_STATUS_FIFO_EMPTY)) {
62ed6662 297
790c5541 298 short data;
62ed6662
BB
299
300 if (status & DAQP_STATUS_DATA_LOST) {
301 s->async->events |=
0a85b6f0 302 COMEDI_CB_EOA | COMEDI_CB_OVERFLOW;
62ed6662
BB
303 printk("daqp: data lost\n");
304 daqp_ai_cancel(dev, s);
305 break;
306 }
307
308 data = inb(dev->iobase + DAQP_FIFO);
309 data |= inb(dev->iobase + DAQP_FIFO) << 8;
310 data ^= 0x8000;
311
312 comedi_buf_put(s->async, data);
313
314 /* If there's a limit, decrement it
315 * and stop conversion if zero
316 */
317
318 if (local->count > 0) {
319 local->count--;
320 if (local->count == 0) {
321 daqp_ai_cancel(dev, s);
322 s->async->events |= COMEDI_CB_EOA;
323 break;
324 }
325 }
326
327 if ((loop_limit--) <= 0)
328 break;
329 }
330
331 if (loop_limit <= 0) {
332 printk(KERN_WARNING
0a85b6f0 333 "loop_limit reached in daqp_interrupt()\n");
62ed6662
BB
334 daqp_ai_cancel(dev, s);
335 s->async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
336 }
337
338 s->async->events |= COMEDI_CB_BLOCK;
339
340 comedi_event(dev, s);
341 }
e3752a1d 342 return IRQ_HANDLED;
62ed6662
BB
343}
344
345/* One-shot analog data acquisition routine */
346
0a85b6f0
MT
347static int daqp_ai_insn_read(struct comedi_device *dev,
348 struct comedi_subdevice *s,
349 struct comedi_insn *insn, unsigned int *data)
62ed6662 350{
0a85b6f0 351 struct local_info_t *local = (struct local_info_t *)s->private;
62ed6662
BB
352 int i;
353 int v;
354 int counter = 10000;
355
3420f6b4 356 if (local->stop)
62ed6662 357 return -EIO;
3420f6b4 358
62ed6662
BB
359
360 /* Stop any running conversion */
361 daqp_ai_cancel(dev, s);
362
363 outb(0, dev->iobase + DAQP_AUX);
364
365 /* Reset scan list queue */
366 outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND);
367
368 /* Program one scan list entry */
369
370 v = DAQP_SCANLIST_CHANNEL(CR_CHAN(insn->chanspec))
0a85b6f0 371 | DAQP_SCANLIST_GAIN(CR_RANGE(insn->chanspec));
62ed6662 372
3420f6b4 373 if (CR_AREF(insn->chanspec) == AREF_DIFF)
62ed6662 374 v |= DAQP_SCANLIST_DIFFERENTIAL;
3420f6b4 375
62ed6662
BB
376
377 v |= DAQP_SCANLIST_START;
378
379 outb(v & 0xff, dev->iobase + DAQP_SCANLIST);
380 outb(v >> 8, dev->iobase + DAQP_SCANLIST);
381
382 /* Reset data FIFO (see page 28 of DAQP User's Manual) */
383
384 outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND);
385
386 /* Set trigger */
387
388 v = DAQP_CONTROL_TRIGGER_ONESHOT | DAQP_CONTROL_TRIGGER_INTERNAL
0a85b6f0 389 | DAQP_CONTROL_PACER_100kHz | DAQP_CONTROL_EOS_INT_ENABLE;
62ed6662
BB
390
391 outb(v, dev->iobase + DAQP_CONTROL);
392
393 /* Reset any pending interrupts (my card has a tendancy to require
394 * require multiple reads on the status register to achieve this)
395 */
396
397 while (--counter
0a85b6f0 398 && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ;
62ed6662
BB
399 if (!counter) {
400 printk("daqp: couldn't clear interrupts in status register\n");
401 return -1;
402 }
403
7ec52ed2 404 init_completion(&local->eos);
62ed6662
BB
405 local->interrupt_mode = semaphore;
406 local->dev = dev;
407 local->s = s;
408
409 for (i = 0; i < insn->n; i++) {
410
411 /* Start conversion */
412 outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA,
0a85b6f0 413 dev->iobase + DAQP_COMMAND);
62ed6662 414
7ec52ed2 415 /* Wait for interrupt service routine to unblock completion */
62ed6662 416 /* Maybe could use a timeout here, but it's interruptible */
7ec52ed2 417 if (wait_for_completion_interruptible(&local->eos))
62ed6662
BB
418 return -EINTR;
419
420 data[i] = inb(dev->iobase + DAQP_FIFO);
421 data[i] |= inb(dev->iobase + DAQP_FIFO) << 8;
422 data[i] ^= 0x8000;
423 }
424
425 return insn->n;
426}
427
428/* This function converts ns nanoseconds to a counter value suitable
429 * for programming the device. We always use the DAQP's 5 MHz clock,
430 * which with its 24-bit counter, allows values up to 84 seconds.
431 * Also, the function adjusts ns so that it cooresponds to the actual
432 * time that the device will use.
433 */
434
435static int daqp_ns_to_timer(unsigned int *ns, int round)
436{
437 int timer;
438
439 timer = *ns / 200;
440 *ns = timer * 200;
441
442 return timer;
443}
444
445/* cmdtest tests a particular command to see if it is valid.
446 * Using the cmdtest ioctl, a user can create a valid cmd
447 * and then have it executed by the cmd ioctl.
448 *
449 * cmdtest returns 1,2,3,4 or 0, depending on which tests
450 * the command passes.
451 */
452
0a85b6f0
MT
453static int daqp_ai_cmdtest(struct comedi_device *dev,
454 struct comedi_subdevice *s, struct comedi_cmd *cmd)
62ed6662
BB
455{
456 int err = 0;
457 int tmp;
458
459 /* step 1: make sure trigger sources are trivially valid */
460
461 tmp = cmd->start_src;
462 cmd->start_src &= TRIG_NOW;
463 if (!cmd->start_src || tmp != cmd->start_src)
464 err++;
465
466 tmp = cmd->scan_begin_src;
467 cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW;
468 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
469 err++;
470
471 tmp = cmd->convert_src;
472 cmd->convert_src &= TRIG_TIMER | TRIG_NOW;
473 if (!cmd->convert_src || tmp != cmd->convert_src)
474 err++;
475
476 tmp = cmd->scan_end_src;
477 cmd->scan_end_src &= TRIG_COUNT;
478 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
479 err++;
480
481 tmp = cmd->stop_src;
482 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
483 if (!cmd->stop_src || tmp != cmd->stop_src)
484 err++;
485
486 if (err)
487 return 1;
488
3420f6b4
GS
489 /*
490 * step 2: make sure trigger sources
491 * are unique and mutually compatible
492 */
62ed6662 493
828684f9 494 /* note that mutual compatibility is not an issue here */
62ed6662 495 if (cmd->scan_begin_src != TRIG_TIMER &&
0a85b6f0 496 cmd->scan_begin_src != TRIG_FOLLOW)
62ed6662
BB
497 err++;
498 if (cmd->convert_src != TRIG_NOW && cmd->convert_src != TRIG_TIMER)
499 err++;
500 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
501 err++;
502 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
503 err++;
504
505 if (err)
506 return 2;
507
508 /* step 3: make sure arguments are trivially compatible */
509
510 if (cmd->start_arg != 0) {
511 cmd->start_arg = 0;
512 err++;
513 }
514#define MAX_SPEED 10000 /* 100 kHz - in nanoseconds */
515
516 if (cmd->scan_begin_src == TRIG_TIMER
0a85b6f0 517 && cmd->scan_begin_arg < MAX_SPEED) {
62ed6662
BB
518 cmd->scan_begin_arg = MAX_SPEED;
519 err++;
520 }
521
522 /* If both scan_begin and convert are both timer values, the only
523 * way that can make sense is if the scan time is the number of
524 * conversions times the convert time
525 */
526
527 if (cmd->scan_begin_src == TRIG_TIMER && cmd->convert_src == TRIG_TIMER
0a85b6f0 528 && cmd->scan_begin_arg != cmd->convert_arg * cmd->scan_end_arg) {
62ed6662
BB
529 err++;
530 }
531
532 if (cmd->convert_src == TRIG_TIMER && cmd->convert_arg < MAX_SPEED) {
533 cmd->convert_arg = MAX_SPEED;
534 err++;
535 }
536
537 if (cmd->scan_end_arg != cmd->chanlist_len) {
538 cmd->scan_end_arg = cmd->chanlist_len;
539 err++;
540 }
541 if (cmd->stop_src == TRIG_COUNT) {
542 if (cmd->stop_arg > 0x00ffffff) {
543 cmd->stop_arg = 0x00ffffff;
544 err++;
545 }
546 } else {
547 /* TRIG_NONE */
548 if (cmd->stop_arg != 0) {
549 cmd->stop_arg = 0;
550 err++;
551 }
552 }
553
554 if (err)
555 return 3;
556
557 /* step 4: fix up any arguments */
558
559 if (cmd->scan_begin_src == TRIG_TIMER) {
560 tmp = cmd->scan_begin_arg;
561 daqp_ns_to_timer(&cmd->scan_begin_arg,
0a85b6f0 562 cmd->flags & TRIG_ROUND_MASK);
62ed6662
BB
563 if (tmp != cmd->scan_begin_arg)
564 err++;
565 }
566
567 if (cmd->convert_src == TRIG_TIMER) {
568 tmp = cmd->convert_arg;
569 daqp_ns_to_timer(&cmd->convert_arg,
0a85b6f0 570 cmd->flags & TRIG_ROUND_MASK);
62ed6662
BB
571 if (tmp != cmd->convert_arg)
572 err++;
573 }
574
575 if (err)
576 return 4;
577
578 return 0;
579}
580
da91b269 581static int daqp_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
62ed6662 582{
0a85b6f0 583 struct local_info_t *local = (struct local_info_t *)s->private;
ea6d0d4c 584 struct comedi_cmd *cmd = &s->async->cmd;
e3752a1d 585 int counter;
62ed6662
BB
586 int scanlist_start_on_every_entry;
587 int threshold;
588
589 int i;
590 int v;
591
3420f6b4 592 if (local->stop)
62ed6662 593 return -EIO;
3420f6b4 594
62ed6662
BB
595
596 /* Stop any running conversion */
597 daqp_ai_cancel(dev, s);
598
599 outb(0, dev->iobase + DAQP_AUX);
600
601 /* Reset scan list queue */
602 outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND);
603
604 /* Program pacer clock
605 *
606 * There's two modes we can operate in. If convert_src is
607 * TRIG_TIMER, then convert_arg specifies the time between
608 * each conversion, so we program the pacer clock to that
609 * frequency and set the SCANLIST_START bit on every scanlist
610 * entry. Otherwise, convert_src is TRIG_NOW, which means
611 * we want the fastest possible conversions, scan_begin_src
612 * is TRIG_TIMER, and scan_begin_arg specifies the time between
613 * each scan, so we program the pacer clock to this frequency
614 * and only set the SCANLIST_START bit on the first entry.
615 */
616
617 if (cmd->convert_src == TRIG_TIMER) {
e3752a1d 618 counter = daqp_ns_to_timer(&cmd->convert_arg,
0a85b6f0 619 cmd->flags & TRIG_ROUND_MASK);
62ed6662
BB
620 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW);
621 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID);
622 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH);
623 scanlist_start_on_every_entry = 1;
624 } else {
e3752a1d 625 counter = daqp_ns_to_timer(&cmd->scan_begin_arg,
0a85b6f0 626 cmd->flags & TRIG_ROUND_MASK);
62ed6662
BB
627 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW);
628 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID);
629 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH);
630 scanlist_start_on_every_entry = 0;
631 }
632
633 /* Program scan list */
634
635 for (i = 0; i < cmd->chanlist_len; i++) {
636
637 int chanspec = cmd->chanlist[i];
638
639 /* Program one scan list entry */
640
641 v = DAQP_SCANLIST_CHANNEL(CR_CHAN(chanspec))
0a85b6f0 642 | DAQP_SCANLIST_GAIN(CR_RANGE(chanspec));
62ed6662 643
3420f6b4 644 if (CR_AREF(chanspec) == AREF_DIFF)
62ed6662 645 v |= DAQP_SCANLIST_DIFFERENTIAL;
62ed6662 646
3420f6b4 647 if (i == 0 || scanlist_start_on_every_entry)
62ed6662 648 v |= DAQP_SCANLIST_START;
62ed6662
BB
649
650 outb(v & 0xff, dev->iobase + DAQP_SCANLIST);
651 outb(v >> 8, dev->iobase + DAQP_SCANLIST);
652 }
653
654 /* Now it's time to program the FIFO threshold, basically the
655 * number of samples the card will buffer before it interrupts
656 * the CPU.
657 *
658 * If we don't have a stop count, then use half the size of
659 * the FIFO (the manufacturer's recommendation). Consider
660 * that the FIFO can hold 2K samples (4K bytes). With the
661 * threshold set at half the FIFO size, we have a margin of
662 * error of 1024 samples. At the chip's maximum sample rate
663 * of 100,000 Hz, the CPU would have to delay interrupt
664 * service for a full 10 milliseconds in order to lose data
665 * here (as opposed to higher up in the kernel). I've never
666 * seen it happen. However, for slow sample rates it may
667 * buffer too much data and introduce too much delay for the
668 * user application.
669 *
670 * If we have a stop count, then things get more interesting.
671 * If the stop count is less than the FIFO size (actually
672 * three-quarters of the FIFO size - see below), we just use
673 * the stop count itself as the threshold, the card interrupts
674 * us when that many samples have been taken, and we kill the
675 * acquisition at that point and are done. If the stop count
676 * is larger than that, then we divide it by 2 until it's less
677 * than three quarters of the FIFO size (we always leave the
678 * top quarter of the FIFO as protection against sluggish CPU
679 * interrupt response) and use that as the threshold. So, if
680 * the stop count is 4000 samples, we divide by two twice to
681 * get 1000 samples, use that as the threshold, take four
682 * interrupts to get our 4000 samples and are done.
683 *
684 * The algorithm could be more clever. For example, if 81000
685 * samples are requested, we could set the threshold to 1500
686 * samples and take 54 interrupts to get 81000. But 54 isn't
687 * a power of two, so this algorithm won't find that option.
688 * Instead, it'll set the threshold at 1266 and take 64
689 * interrupts to get 81024 samples, of which the last 24 will
690 * be discarded... but we won't get the last interrupt until
691 * they've been collected. To find the first option, the
692 * computer could look at the prime decomposition of the
693 * sample count (81000 = 3^4 * 5^3 * 2^3) and factor it into a
694 * threshold (1500 = 3 * 5^3 * 2^2) and an interrupt count (54
695 * = 3^3 * 2). Hmmm... a one-line while loop or prime
696 * decomposition of integers... I'll leave it the way it is.
697 *
698 * I'll also note a mini-race condition before ignoring it in
699 * the code. Let's say we're taking 4000 samples, as before.
700 * After 1000 samples, we get an interrupt. But before that
701 * interrupt is completely serviced, another sample is taken
702 * and loaded into the FIFO. Since the interrupt handler
703 * empties the FIFO before returning, it will read 1001 samples.
704 * If that happens four times, we'll end up taking 4004 samples,
705 * not 4000. The interrupt handler will discard the extra four
706 * samples (by halting the acquisition with four samples still
707 * in the FIFO), but we will have to wait for them.
708 *
709 * In short, this code works pretty well, but for either of
710 * the two reasons noted, might end up waiting for a few more
711 * samples than actually requested. Shouldn't make too much
712 * of a difference.
713 */
714
715 /* Save away the number of conversions we should perform, and
716 * compute the FIFO threshold (in bytes, not samples - that's
717 * why we multiple local->count by 2 = sizeof(sample))
718 */
719
720 if (cmd->stop_src == TRIG_COUNT) {
721 local->count = cmd->stop_arg * cmd->scan_end_arg;
722 threshold = 2 * local->count;
723 while (threshold > DAQP_FIFO_SIZE * 3 / 4)
724 threshold /= 2;
725 } else {
726 local->count = -1;
727 threshold = DAQP_FIFO_SIZE / 2;
728 }
729
730 /* Reset data FIFO (see page 28 of DAQP User's Manual) */
731
732 outb(DAQP_COMMAND_RSTF, dev->iobase + DAQP_COMMAND);
733
734 /* Set FIFO threshold. First two bytes are near-empty
735 * threshold, which is unused; next two bytes are near-full
736 * threshold. We computed the number of bytes we want in the
737 * FIFO when the interrupt is generated, what the card wants
738 * is actually the number of available bytes left in the FIFO
739 * when the interrupt is to happen.
740 */
741
742 outb(0x00, dev->iobase + DAQP_FIFO);
743 outb(0x00, dev->iobase + DAQP_FIFO);
744
745 outb((DAQP_FIFO_SIZE - threshold) & 0xff, dev->iobase + DAQP_FIFO);
746 outb((DAQP_FIFO_SIZE - threshold) >> 8, dev->iobase + DAQP_FIFO);
747
748 /* Set trigger */
749
750 v = DAQP_CONTROL_TRIGGER_CONTINUOUS | DAQP_CONTROL_TRIGGER_INTERNAL
0a85b6f0 751 | DAQP_CONTROL_PACER_5MHz | DAQP_CONTROL_FIFO_INT_ENABLE;
62ed6662
BB
752
753 outb(v, dev->iobase + DAQP_CONTROL);
754
755 /* Reset any pending interrupts (my card has a tendancy to require
756 * require multiple reads on the status register to achieve this)
757 */
e3752a1d 758 counter = 100;
62ed6662 759 while (--counter
0a85b6f0 760 && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ;
62ed6662 761 if (!counter) {
3420f6b4
GS
762 printk(KERN_ERR
763 "daqp: couldn't clear interrupts in status register\n");
62ed6662
BB
764 return -1;
765 }
766
767 local->interrupt_mode = buffer;
768 local->dev = dev;
769 local->s = s;
770
771 /* Start conversion */
772 outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA,
0a85b6f0 773 dev->iobase + DAQP_COMMAND);
62ed6662
BB
774
775 return 0;
776}
777
778/* Single-shot analog output routine */
779
0a85b6f0
MT
780static int daqp_ao_insn_write(struct comedi_device *dev,
781 struct comedi_subdevice *s,
782 struct comedi_insn *insn, unsigned int *data)
62ed6662 783{
0a85b6f0 784 struct local_info_t *local = (struct local_info_t *)s->private;
62ed6662
BB
785 int d;
786 unsigned int chan;
787
3420f6b4 788 if (local->stop)
62ed6662 789 return -EIO;
62ed6662
BB
790
791 chan = CR_CHAN(insn->chanspec);
792 d = data[0];
793 d &= 0x0fff;
794 d ^= 0x0800; /* Flip the sign */
795 d |= chan << 12;
796
797 /* Make sure D/A update mode is direct update */
798 outb(0, dev->iobase + DAQP_AUX);
799
800 outw(d, dev->iobase + DAQP_DA);
801
802 return 1;
803}
804
805/* Digital input routine */
806
0a85b6f0
MT
807static int daqp_di_insn_read(struct comedi_device *dev,
808 struct comedi_subdevice *s,
809 struct comedi_insn *insn, unsigned int *data)
62ed6662 810{
0a85b6f0 811 struct local_info_t *local = (struct local_info_t *)s->private;
62ed6662 812
3420f6b4 813 if (local->stop)
62ed6662 814 return -EIO;
62ed6662
BB
815
816 data[0] = inb(dev->iobase + DAQP_DIGITAL_IO);
817
818 return 1;
819}
820
821/* Digital output routine */
822
0a85b6f0
MT
823static int daqp_do_insn_write(struct comedi_device *dev,
824 struct comedi_subdevice *s,
825 struct comedi_insn *insn, unsigned int *data)
62ed6662 826{
0a85b6f0 827 struct local_info_t *local = (struct local_info_t *)s->private;
62ed6662 828
3420f6b4 829 if (local->stop)
62ed6662 830 return -EIO;
62ed6662
BB
831
832 outw(data[0] & 0xf, dev->iobase + DAQP_DIGITAL_IO);
833
834 return 1;
835}
836
837/* daqp_attach is called via comedi_config to attach a comedi device
838 * to a /dev/comedi*. Note that this is different from daqp_cs_attach()
839 * which is called by the pcmcia subsystem to attach the PCMCIA card
840 * when it is inserted.
841 */
842
da91b269 843static int daqp_attach(struct comedi_device *dev, struct comedi_devconfig *it)
62ed6662
BB
844{
845 int ret;
ab64f663 846 struct local_info_t *local = dev_table[it->options[0]];
34c43922 847 struct comedi_subdevice *s;
62ed6662
BB
848
849 if (it->options[0] < 0 || it->options[0] >= MAX_DEV || !local) {
850 printk("comedi%d: No such daqp device %d\n",
0a85b6f0 851 dev->minor, it->options[0]);
62ed6662
BB
852 return -EIO;
853 }
854
855 /* Typically brittle code that I don't completely understand,
856 * but "it works on my card". The intent is to pull the model
857 * number of the card out the PCMCIA CIS and stash it away as
858 * the COMEDI board_name. Looks like the third field in
859 * CISTPL_VERS_1 (offset 2) holds what we're looking for. If
860 * it doesn't work, who cares, just leave it as "DAQP".
861 */
862
863 strcpy(local->board_name, "DAQP");
864 dev->board_name = local->board_name;
55a19b39
DB
865 if (local->link->prod_id[2]) {
866 if (strncmp(local->link->prod_id[2], "DAQP", 4) == 0) {
867 strncpy(local->board_name, local->link->prod_id[2],
868 sizeof(local->board_name));
62ed6662
BB
869 }
870 }
871
9a017a91 872 dev->iobase = local->link->resource[0]->start;
62ed6662 873
c3744138
BP
874 ret = alloc_subdevices(dev, 4);
875 if (ret < 0)
62ed6662
BB
876 return ret;
877
3420f6b4 878 printk(KERN_INFO "comedi%d: attaching daqp%d (io 0x%04lx)\n",
0a85b6f0 879 dev->minor, it->options[0], dev->iobase);
62ed6662
BB
880
881 s = dev->subdevices + 0;
882 dev->read_subdev = s;
883 s->private = local;
884 s->type = COMEDI_SUBD_AI;
885 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
886 s->n_chan = 8;
887 s->len_chanlist = 2048;
888 s->maxdata = 0xffff;
889 s->range_table = &range_daqp_ai;
890 s->insn_read = daqp_ai_insn_read;
891 s->do_cmdtest = daqp_ai_cmdtest;
892 s->do_cmd = daqp_ai_cmd;
893 s->cancel = daqp_ai_cancel;
894
895 s = dev->subdevices + 1;
896 dev->write_subdev = s;
897 s->private = local;
898 s->type = COMEDI_SUBD_AO;
899 s->subdev_flags = SDF_WRITEABLE;
900 s->n_chan = 2;
901 s->len_chanlist = 1;
902 s->maxdata = 0x0fff;
903 s->range_table = &range_daqp_ao;
904 s->insn_write = daqp_ao_insn_write;
905
906 s = dev->subdevices + 2;
907 s->private = local;
908 s->type = COMEDI_SUBD_DI;
909 s->subdev_flags = SDF_READABLE;
910 s->n_chan = 1;
911 s->len_chanlist = 1;
912 s->insn_read = daqp_di_insn_read;
913
914 s = dev->subdevices + 3;
915 s->private = local;
916 s->type = COMEDI_SUBD_DO;
917 s->subdev_flags = SDF_WRITEABLE;
918 s->n_chan = 1;
919 s->len_chanlist = 1;
920 s->insn_write = daqp_do_insn_write;
921
922 return 1;
923}
924
925/* daqp_detach (called from comedi_comdig) does nothing. If the PCMCIA
926 * card is removed, daqp_cs_detach() is called by the pcmcia subsystem.
927 */
928
da91b269 929static int daqp_detach(struct comedi_device *dev)
62ed6662 930{
3420f6b4 931 printk(KERN_INFO "comedi%d: detaching daqp\n", dev->minor);
62ed6662
BB
932
933 return 0;
934}
935
936/*====================================================================
937
938 PCMCIA interface code
939
940 The rest of the code in this file is based on dummy_cs.c v1.24
941 from the Linux pcmcia_cs distribution v3.1.8 and is subject
942 to the following license agreement.
943
944 The remaining contents of this file are subject to the Mozilla Public
945 License Version 1.1 (the "License"); you may not use this file
946 except in compliance with the License. You may obtain a copy of
947 the License at http://www.mozilla.org/MPL/
948
949 Software distributed under the License is distributed on an "AS
950 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
951 implied. See the License for the specific language governing
952 rights and limitations under the License.
953
954 The initial developer of the original code is David A. Hinds
955 <dhinds@pcmcia.sourceforge.org>. Portions created by David A. Hinds
956 are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
957
958 Alternatively, the contents of this file may be used under the
959 terms of the GNU Public License version 2 (the "GPL"), in which
960 case the provisions of the GPL are applicable instead of the
961 above. If you wish to allow the use of your version of this file
962 only under the terms of the GPL and not to allow others to use
963 your version of this file under the MPL, indicate your decision
964 by deleting the provisions above and replace them with the notice
965 and other provisions required by the GPL. If you do not delete
966 the provisions above, a recipient may use your version of this
967 file under either the MPL or the GPL.
968
969======================================================================*/
970
971/*
972 The event() function is this driver's Card Services event handler.
973 It will be called by Card Services when an appropriate card status
974 event is received. The config() and release() entry points are
975 used to configure or release a socket, in response to card
976 insertion and ejection events.
977
978 Kernel version 2.6.16 upwards uses suspend() and resume() functions
979 instead of an event() function.
980*/
981
982static void daqp_cs_config(struct pcmcia_device *link);
983static void daqp_cs_release(struct pcmcia_device *link);
984static int daqp_cs_suspend(struct pcmcia_device *p_dev);
985static int daqp_cs_resume(struct pcmcia_device *p_dev);
986
987/*
988 The attach() and detach() entry points are used to create and destroy
989 "instances" of the driver, where each instance represents everything
990 needed to manage one actual PCMCIA card.
991*/
992
993static int daqp_cs_attach(struct pcmcia_device *);
994static void daqp_cs_detach(struct pcmcia_device *);
995
62ed6662
BB
996/*======================================================================
997
998 daqp_cs_attach() creates an "instance" of the driver, allocating
999 local data structures for one device. The device is registered
1000 with Card Services.
1001
1002 The dev_link structure is initialized, but we don't actually
1003 configure the card at this point -- we wait until we receive a
1004 card insertion event.
1005
1006======================================================================*/
1007
1008static int daqp_cs_attach(struct pcmcia_device *link)
1009{
ab64f663 1010 struct local_info_t *local;
62ed6662
BB
1011 int i;
1012
55a19b39 1013 dev_dbg(&link->dev, "daqp_cs_attach()\n");
62ed6662
BB
1014
1015 for (i = 0; i < MAX_DEV; i++)
1016 if (dev_table[i] == NULL)
1017 break;
1018 if (i == MAX_DEV) {
1019 printk(KERN_NOTICE "daqp_cs: no devices available\n");
1020 return -ENODEV;
1021 }
1022
1023 /* Allocate space for private device-specific data */
ab64f663 1024 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL);
62ed6662
BB
1025 if (!local)
1026 return -ENOMEM;
1027
1028 local->table_index = i;
1029 dev_table[i] = local;
1030 local->link = link;
1031 link->priv = local;
1032
62ed6662
BB
1033 daqp_cs_config(link);
1034
1035 return 0;
1036} /* daqp_cs_attach */
1037
1038/*======================================================================
1039
1040 This deletes a driver "instance". The device is de-registered
1041 with Card Services. If it has been released, all local data
1042 structures are freed. Otherwise, the structures will be freed
1043 when the device is released.
1044
1045======================================================================*/
1046
1047static void daqp_cs_detach(struct pcmcia_device *link)
1048{
ab64f663 1049 struct local_info_t *dev = link->priv;
62ed6662 1050
55a19b39 1051 dev_dbg(&link->dev, "daqp_cs_detach\n");
62ed6662 1052
7622802e
JMC
1053 dev->stop = 1;
1054 daqp_cs_release(link);
62ed6662
BB
1055
1056 /* Unlink device structure, and free it */
1057 dev_table[dev->table_index] = NULL;
f25bd6bf 1058 kfree(dev);
62ed6662
BB
1059
1060} /* daqp_cs_detach */
1061
1062/*======================================================================
1063
1064 daqp_cs_config() is scheduled to run after a CARD_INSERTION event
1065 is received, to configure the PCMCIA socket, and to make the
1066 device available to the system.
1067
1068======================================================================*/
1069
c3744138 1070
00990e7c 1071static int daqp_pcmcia_config_loop(struct pcmcia_device *p_dev, void *priv_data)
55a19b39 1072{
00990e7c
DB
1073 if (p_dev->config_index == 0)
1074 return -EINVAL;
62ed6662 1075
90abdc3b 1076 return pcmcia_request_io(p_dev);
55a19b39 1077}
62ed6662 1078
55a19b39
DB
1079static void daqp_cs_config(struct pcmcia_device *link)
1080{
55a19b39 1081 int ret;
62ed6662 1082
55a19b39 1083 dev_dbg(&link->dev, "daqp_cs_config\n");
62ed6662 1084
00990e7c
DB
1085 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
1086
55a19b39
DB
1087 ret = pcmcia_loop_config(link, daqp_pcmcia_config_loop, NULL);
1088 if (ret) {
1089 dev_warn(&link->dev, "no configuration found\n");
1090 goto failed;
62ed6662
BB
1091 }
1092
eb14120f
DB
1093 ret = pcmcia_request_irq(link, daqp_interrupt);
1094 if (ret)
1095 goto failed;
62ed6662
BB
1096
1097 /*
1098 This actually configures the PCMCIA socket -- setting up
1099 the I/O windows and the interrupt mapping, and putting the
1100 card and host interface into "Memory and IO" mode.
1101 */
1ac71e5a 1102 ret = pcmcia_enable_device(link);
55a19b39
DB
1103 if (ret)
1104 goto failed;
62ed6662 1105
62ed6662
BB
1106 return;
1107
55a19b39 1108failed:
62ed6662
BB
1109 daqp_cs_release(link);
1110
1111} /* daqp_cs_config */
1112
1113static void daqp_cs_release(struct pcmcia_device *link)
1114{
55a19b39 1115 dev_dbg(&link->dev, "daqp_cs_release\n");
62ed6662
BB
1116
1117 pcmcia_disable_device(link);
1118} /* daqp_cs_release */
1119
1120/*======================================================================
1121
1122 The card status event handler. Mostly, this schedules other
1123 stuff to run after an event is received.
1124
1125 When a CARD_REMOVAL event is received, we immediately set a
1126 private flag to block future accesses to this device. All the
1127 functions that actually access the device should check this flag
1128 to make sure the card is still present.
1129
1130======================================================================*/
1131
1132static int daqp_cs_suspend(struct pcmcia_device *link)
1133{
ab64f663 1134 struct local_info_t *local = link->priv;
62ed6662
BB
1135
1136 /* Mark the device as stopped, to block IO until later */
1137 local->stop = 1;
1138 return 0;
1139}
1140
1141static int daqp_cs_resume(struct pcmcia_device *link)
1142{
ab64f663 1143 struct local_info_t *local = link->priv;
62ed6662
BB
1144
1145 local->stop = 0;
1146
1147 return 0;
1148}
1149
1150/*====================================================================*/
1151
1152#ifdef MODULE
1153
1154static struct pcmcia_device_id daqp_cs_id_table[] = {
1155 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0027),
1156 PCMCIA_DEVICE_NULL
1157};
1158
1159MODULE_DEVICE_TABLE(pcmcia, daqp_cs_id_table);
6c7f8196
AK
1160MODULE_AUTHOR("Brent Baccala <baccala@freesoft.org>");
1161MODULE_DESCRIPTION("Comedi driver for Quatech DAQP PCMCIA data capture cards");
1162MODULE_LICENSE("GPL");
62ed6662 1163
e3752a1d 1164static struct pcmcia_driver daqp_cs_driver = {
62ed6662
BB
1165 .probe = daqp_cs_attach,
1166 .remove = daqp_cs_detach,
1167 .suspend = daqp_cs_suspend,
1168 .resume = daqp_cs_resume,
1169 .id_table = daqp_cs_id_table,
1170 .owner = THIS_MODULE,
2e9b981a 1171 .name = "quatech_daqp_cs",
62ed6662
BB
1172};
1173
1174int __init init_module(void)
1175{
62ed6662
BB
1176 pcmcia_register_driver(&daqp_cs_driver);
1177 comedi_driver_register(&driver_daqp);
1178 return 0;
1179}
1180
1181void __exit cleanup_module(void)
1182{
62ed6662
BB
1183 comedi_driver_unregister(&driver_daqp);
1184 pcmcia_unregister_driver(&daqp_cs_driver);
1185}
1186
1187#endif