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