quatech_daqp_cs.c revision e3752a1dfd8a003139dee2c80de3d915534e2b39
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 52#include <pcmcia/cs_types.h> 53#include <pcmcia/cs.h> 54#include <pcmcia/cistpl.h> 55#include <pcmcia/cisreg.h> 56#include <pcmcia/ds.h> 57 58/* Maximum number of separate DAQP devices we'll allow */ 59#define MAX_DEV 4 60 61struct local_info_t { 62 struct pcmcia_device *link; 63 dev_node_t node; 64 int stop; 65 int table_index; 66 char board_name[32]; 67 68 enum { semaphore, buffer } interrupt_mode; 69 70 struct semaphore eos; 71 72 struct comedi_device *dev; 73 struct comedi_subdevice *s; 74 int count; 75}; 76 77/* A list of "instances" of the device. */ 78 79static struct local_info_t *dev_table[MAX_DEV] = { NULL, /* ... */ }; 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 165 * voltages. The A/D converter has four .ranges = +/- 10V through 166 * +/- 1.25V, and the D/A converter has only .one = +/- 5V. 167 */ 168 169static const struct comedi_lrange range_daqp_ai = { 4, { 170 BIP_RANGE(10), 171 BIP_RANGE(5), 172 BIP_RANGE(2.5), 173 BIP_RANGE(1.25) 174 } 175}; 176 177static const struct comedi_lrange range_daqp_ao = { 1, {BIP_RANGE(5)} }; 178 179/*====================================================================*/ 180 181/* comedi interface code */ 182 183static int daqp_attach(struct comedi_device *dev, struct comedi_devconfig *it); 184static int daqp_detach(struct comedi_device *dev); 185static struct comedi_driver driver_daqp = { 186 .driver_name = "quatech_daqp_cs", 187 .module = THIS_MODULE, 188 .attach = daqp_attach, 189 .detach = daqp_detach, 190}; 191 192#ifdef DAQP_DEBUG 193 194static void daqp_dump(struct comedi_device *dev) 195{ 196 printk("DAQP: status %02x; aux status %02x\n", 197 inb(dev->iobase + DAQP_STATUS), inb(dev->iobase + DAQP_AUX)); 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++) { 208 if (i % 16 == 0) { 209 printk("\n0x%08x:", (unsigned int)cptr); 210 } 211 printk(" %02x", *(cptr++)); 212 } 213 printk("\n"); 214} 215 216#endif 217 218/* Cancel a running acquisition */ 219 220static int daqp_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s) 221{ 222 struct local_info_t *local = (struct local_info_t *)s->private; 223 224 if (local->stop) { 225 return -EIO; 226 } 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 241 * 'semaphore', just signal the local->eos semaphore and return 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 */ 247static enum irqreturn daqp_interrupt(int irq, void *dev_id) 248{ 249 struct local_info_t *local = (struct local_info_t *)dev_id; 250 struct comedi_device *dev; 251 struct comedi_subdevice *s; 252 int loop_limit = 10000; 253 int status; 254 255 if (local == NULL) { 256 printk(KERN_WARNING 257 "daqp_interrupt(): irq %d for unknown device.\n", irq); 258 return IRQ_NONE; 259 } 260 261 dev = local->dev; 262 if (dev == NULL) { 263 printk(KERN_WARNING "daqp_interrupt(): NULL comedi_device.\n"); 264 return IRQ_NONE; 265 } 266 267 if (!dev->attached) { 268 printk(KERN_WARNING 269 "daqp_interrupt(): struct comedi_device not yet attached.\n"); 270 return IRQ_NONE; 271 } 272 273 s = local->s; 274 if (s == NULL) { 275 printk(KERN_WARNING 276 "daqp_interrupt(): NULL comedi_subdevice.\n"); 277 return IRQ_NONE; 278 } 279 280 if ((struct local_info_t *)s->private != local) { 281 printk(KERN_WARNING 282 "daqp_interrupt(): invalid comedi_subdevice.\n"); 283 return IRQ_NONE; 284 } 285 286 switch (local->interrupt_mode) { 287 288 case semaphore: 289 290 up(&local->eos); 291 break; 292 293 case buffer: 294 295 while (!((status = inb(dev->iobase + DAQP_STATUS)) 296 & DAQP_STATUS_FIFO_EMPTY)) { 297 298 short data; 299 300 if (status & DAQP_STATUS_DATA_LOST) { 301 s->async->events |= 302 COMEDI_CB_EOA | COMEDI_CB_OVERFLOW; 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 333 "loop_limit reached in daqp_interrupt()\n"); 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 } 342 return IRQ_HANDLED; 343} 344 345/* One-shot analog data acquisition routine */ 346 347static int daqp_ai_insn_read(struct comedi_device *dev, 348 struct comedi_subdevice *s, 349 struct comedi_insn *insn, unsigned int *data) 350{ 351 struct local_info_t *local = (struct local_info_t *)s->private; 352 int i; 353 int v; 354 int counter = 10000; 355 356 if (local->stop) { 357 return -EIO; 358 } 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)) 371 | DAQP_SCANLIST_GAIN(CR_RANGE(insn->chanspec)); 372 373 if (CR_AREF(insn->chanspec) == AREF_DIFF) { 374 v |= DAQP_SCANLIST_DIFFERENTIAL; 375 } 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 389 | DAQP_CONTROL_PACER_100kHz | DAQP_CONTROL_EOS_INT_ENABLE; 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 398 && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ; 399 if (!counter) { 400 printk("daqp: couldn't clear interrupts in status register\n"); 401 return -1; 402 } 403 404 /* Make sure semaphore is blocked */ 405 sema_init(&local->eos, 0); 406 local->interrupt_mode = semaphore; 407 local->dev = dev; 408 local->s = s; 409 410 for (i = 0; i < insn->n; i++) { 411 412 /* Start conversion */ 413 outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA, 414 dev->iobase + DAQP_COMMAND); 415 416 /* Wait for interrupt service routine to unblock semaphore */ 417 /* Maybe could use a timeout here, but it's interruptible */ 418 if (down_interruptible(&local->eos)) 419 return -EINTR; 420 421 data[i] = inb(dev->iobase + DAQP_FIFO); 422 data[i] |= inb(dev->iobase + DAQP_FIFO) << 8; 423 data[i] ^= 0x8000; 424 } 425 426 return insn->n; 427} 428 429/* This function converts ns nanoseconds to a counter value suitable 430 * for programming the device. We always use the DAQP's 5 MHz clock, 431 * which with its 24-bit counter, allows values up to 84 seconds. 432 * Also, the function adjusts ns so that it cooresponds to the actual 433 * time that the device will use. 434 */ 435 436static int daqp_ns_to_timer(unsigned int *ns, int round) 437{ 438 int timer; 439 440 timer = *ns / 200; 441 *ns = timer * 200; 442 443 return timer; 444} 445 446/* cmdtest tests a particular command to see if it is valid. 447 * Using the cmdtest ioctl, a user can create a valid cmd 448 * and then have it executed by the cmd ioctl. 449 * 450 * cmdtest returns 1,2,3,4 or 0, depending on which tests 451 * the command passes. 452 */ 453 454static int daqp_ai_cmdtest(struct comedi_device *dev, 455 struct comedi_subdevice *s, struct comedi_cmd *cmd) 456{ 457 int err = 0; 458 int tmp; 459 460 /* step 1: make sure trigger sources are trivially valid */ 461 462 tmp = cmd->start_src; 463 cmd->start_src &= TRIG_NOW; 464 if (!cmd->start_src || tmp != cmd->start_src) 465 err++; 466 467 tmp = cmd->scan_begin_src; 468 cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW; 469 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src) 470 err++; 471 472 tmp = cmd->convert_src; 473 cmd->convert_src &= TRIG_TIMER | TRIG_NOW; 474 if (!cmd->convert_src || tmp != cmd->convert_src) 475 err++; 476 477 tmp = cmd->scan_end_src; 478 cmd->scan_end_src &= TRIG_COUNT; 479 if (!cmd->scan_end_src || tmp != cmd->scan_end_src) 480 err++; 481 482 tmp = cmd->stop_src; 483 cmd->stop_src &= TRIG_COUNT | TRIG_NONE; 484 if (!cmd->stop_src || tmp != cmd->stop_src) 485 err++; 486 487 if (err) 488 return 1; 489 490 /* step 2: make sure trigger sources are unique and mutually compatible */ 491 492 /* note that mutual compatibility is not an issue here */ 493 if (cmd->scan_begin_src != TRIG_TIMER && 494 cmd->scan_begin_src != TRIG_FOLLOW) 495 err++; 496 if (cmd->convert_src != TRIG_NOW && cmd->convert_src != TRIG_TIMER) 497 err++; 498 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW) 499 err++; 500 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE) 501 err++; 502 503 if (err) 504 return 2; 505 506 /* step 3: make sure arguments are trivially compatible */ 507 508 if (cmd->start_arg != 0) { 509 cmd->start_arg = 0; 510 err++; 511 } 512#define MAX_SPEED 10000 /* 100 kHz - in nanoseconds */ 513 514 if (cmd->scan_begin_src == TRIG_TIMER 515 && cmd->scan_begin_arg < MAX_SPEED) { 516 cmd->scan_begin_arg = MAX_SPEED; 517 err++; 518 } 519 520 /* If both scan_begin and convert are both timer values, the only 521 * way that can make sense is if the scan time is the number of 522 * conversions times the convert time 523 */ 524 525 if (cmd->scan_begin_src == TRIG_TIMER && cmd->convert_src == TRIG_TIMER 526 && cmd->scan_begin_arg != cmd->convert_arg * cmd->scan_end_arg) { 527 err++; 528 } 529 530 if (cmd->convert_src == TRIG_TIMER && cmd->convert_arg < MAX_SPEED) { 531 cmd->convert_arg = MAX_SPEED; 532 err++; 533 } 534 535 if (cmd->scan_end_arg != cmd->chanlist_len) { 536 cmd->scan_end_arg = cmd->chanlist_len; 537 err++; 538 } 539 if (cmd->stop_src == TRIG_COUNT) { 540 if (cmd->stop_arg > 0x00ffffff) { 541 cmd->stop_arg = 0x00ffffff; 542 err++; 543 } 544 } else { 545 /* TRIG_NONE */ 546 if (cmd->stop_arg != 0) { 547 cmd->stop_arg = 0; 548 err++; 549 } 550 } 551 552 if (err) 553 return 3; 554 555 /* step 4: fix up any arguments */ 556 557 if (cmd->scan_begin_src == TRIG_TIMER) { 558 tmp = cmd->scan_begin_arg; 559 daqp_ns_to_timer(&cmd->scan_begin_arg, 560 cmd->flags & TRIG_ROUND_MASK); 561 if (tmp != cmd->scan_begin_arg) 562 err++; 563 } 564 565 if (cmd->convert_src == TRIG_TIMER) { 566 tmp = cmd->convert_arg; 567 daqp_ns_to_timer(&cmd->convert_arg, 568 cmd->flags & TRIG_ROUND_MASK); 569 if (tmp != cmd->convert_arg) 570 err++; 571 } 572 573 if (err) 574 return 4; 575 576 return 0; 577} 578 579static int daqp_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s) 580{ 581 struct local_info_t *local = (struct local_info_t *)s->private; 582 struct comedi_cmd *cmd = &s->async->cmd; 583 int counter; 584 int scanlist_start_on_every_entry; 585 int threshold; 586 587 int i; 588 int v; 589 590 if (local->stop) { 591 return -EIO; 592 } 593 594 /* Stop any running conversion */ 595 daqp_ai_cancel(dev, s); 596 597 outb(0, dev->iobase + DAQP_AUX); 598 599 /* Reset scan list queue */ 600 outb(DAQP_COMMAND_RSTQ, dev->iobase + DAQP_COMMAND); 601 602 /* Program pacer clock 603 * 604 * There's two modes we can operate in. If convert_src is 605 * TRIG_TIMER, then convert_arg specifies the time between 606 * each conversion, so we program the pacer clock to that 607 * frequency and set the SCANLIST_START bit on every scanlist 608 * entry. Otherwise, convert_src is TRIG_NOW, which means 609 * we want the fastest possible conversions, scan_begin_src 610 * is TRIG_TIMER, and scan_begin_arg specifies the time between 611 * each scan, so we program the pacer clock to this frequency 612 * and only set the SCANLIST_START bit on the first entry. 613 */ 614 615 if (cmd->convert_src == TRIG_TIMER) { 616 counter = daqp_ns_to_timer(&cmd->convert_arg, 617 cmd->flags & TRIG_ROUND_MASK); 618 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW); 619 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID); 620 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH); 621 scanlist_start_on_every_entry = 1; 622 } else { 623 counter = daqp_ns_to_timer(&cmd->scan_begin_arg, 624 cmd->flags & TRIG_ROUND_MASK); 625 outb(counter & 0xff, dev->iobase + DAQP_PACER_LOW); 626 outb((counter >> 8) & 0xff, dev->iobase + DAQP_PACER_MID); 627 outb((counter >> 16) & 0xff, dev->iobase + DAQP_PACER_HIGH); 628 scanlist_start_on_every_entry = 0; 629 } 630 631 /* Program scan list */ 632 633 for (i = 0; i < cmd->chanlist_len; i++) { 634 635 int chanspec = cmd->chanlist[i]; 636 637 /* Program one scan list entry */ 638 639 v = DAQP_SCANLIST_CHANNEL(CR_CHAN(chanspec)) 640 | DAQP_SCANLIST_GAIN(CR_RANGE(chanspec)); 641 642 if (CR_AREF(chanspec) == AREF_DIFF) { 643 v |= DAQP_SCANLIST_DIFFERENTIAL; 644 } 645 646 if (i == 0 || scanlist_start_on_every_entry) { 647 v |= DAQP_SCANLIST_START; 648 } 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 751 | DAQP_CONTROL_PACER_5MHz | DAQP_CONTROL_FIFO_INT_ENABLE; 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 */ 758 counter = 100; 759 while (--counter 760 && (inb(dev->iobase + DAQP_STATUS) & DAQP_STATUS_EVENTS)) ; 761 if (!counter) { 762 printk("daqp: couldn't clear interrupts in status register\n"); 763 return -1; 764 } 765 766 local->interrupt_mode = buffer; 767 local->dev = dev; 768 local->s = s; 769 770 /* Start conversion */ 771 outb(DAQP_COMMAND_ARM | DAQP_COMMAND_FIFO_DATA, 772 dev->iobase + DAQP_COMMAND); 773 774 return 0; 775} 776 777/* Single-shot analog output routine */ 778 779static int daqp_ao_insn_write(struct comedi_device *dev, 780 struct comedi_subdevice *s, 781 struct comedi_insn *insn, unsigned int *data) 782{ 783 struct local_info_t *local = (struct local_info_t *)s->private; 784 int d; 785 unsigned int chan; 786 787 if (local->stop) { 788 return -EIO; 789 } 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 807static int daqp_di_insn_read(struct comedi_device *dev, 808 struct comedi_subdevice *s, 809 struct comedi_insn *insn, unsigned int *data) 810{ 811 struct local_info_t *local = (struct local_info_t *)s->private; 812 813 if (local->stop) { 814 return -EIO; 815 } 816 817 data[0] = inb(dev->iobase + DAQP_DIGITAL_IO); 818 819 return 1; 820} 821 822/* Digital output routine */ 823 824static int daqp_do_insn_write(struct comedi_device *dev, 825 struct comedi_subdevice *s, 826 struct comedi_insn *insn, unsigned int *data) 827{ 828 struct local_info_t *local = (struct local_info_t *)s->private; 829 830 if (local->stop) { 831 return -EIO; 832 } 833 834 outw(data[0] & 0xf, dev->iobase + DAQP_DIGITAL_IO); 835 836 return 1; 837} 838 839/* daqp_attach is called via comedi_config to attach a comedi device 840 * to a /dev/comedi*. Note that this is different from daqp_cs_attach() 841 * which is called by the pcmcia subsystem to attach the PCMCIA card 842 * when it is inserted. 843 */ 844 845static int daqp_attach(struct comedi_device *dev, struct comedi_devconfig *it) 846{ 847 int ret; 848 struct local_info_t *local = dev_table[it->options[0]]; 849 struct comedi_subdevice *s; 850 851 if (it->options[0] < 0 || it->options[0] >= MAX_DEV || !local) { 852 printk("comedi%d: No such daqp device %d\n", 853 dev->minor, it->options[0]); 854 return -EIO; 855 } 856 857 /* Typically brittle code that I don't completely understand, 858 * but "it works on my card". The intent is to pull the model 859 * number of the card out the PCMCIA CIS and stash it away as 860 * the COMEDI board_name. Looks like the third field in 861 * CISTPL_VERS_1 (offset 2) holds what we're looking for. If 862 * it doesn't work, who cares, just leave it as "DAQP". 863 */ 864 865 strcpy(local->board_name, "DAQP"); 866 dev->board_name = local->board_name; 867 if (local->link->prod_id[2]) { 868 if (strncmp(local->link->prod_id[2], "DAQP", 4) == 0) { 869 strncpy(local->board_name, local->link->prod_id[2], 870 sizeof(local->board_name)); 871 } 872 } 873 874 dev->iobase = local->link->io.BasePort1; 875 876 ret = alloc_subdevices(dev, 4); 877 if (ret < 0) 878 return ret; 879 880 printk("comedi%d: attaching daqp%d (io 0x%04lx)\n", 881 dev->minor, it->options[0], dev->iobase); 882 883 s = dev->subdevices + 0; 884 dev->read_subdev = s; 885 s->private = local; 886 s->type = COMEDI_SUBD_AI; 887 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ; 888 s->n_chan = 8; 889 s->len_chanlist = 2048; 890 s->maxdata = 0xffff; 891 s->range_table = &range_daqp_ai; 892 s->insn_read = daqp_ai_insn_read; 893 s->do_cmdtest = daqp_ai_cmdtest; 894 s->do_cmd = daqp_ai_cmd; 895 s->cancel = daqp_ai_cancel; 896 897 s = dev->subdevices + 1; 898 dev->write_subdev = s; 899 s->private = local; 900 s->type = COMEDI_SUBD_AO; 901 s->subdev_flags = SDF_WRITEABLE; 902 s->n_chan = 2; 903 s->len_chanlist = 1; 904 s->maxdata = 0x0fff; 905 s->range_table = &range_daqp_ao; 906 s->insn_write = daqp_ao_insn_write; 907 908 s = dev->subdevices + 2; 909 s->private = local; 910 s->type = COMEDI_SUBD_DI; 911 s->subdev_flags = SDF_READABLE; 912 s->n_chan = 1; 913 s->len_chanlist = 1; 914 s->insn_read = daqp_di_insn_read; 915 916 s = dev->subdevices + 3; 917 s->private = local; 918 s->type = COMEDI_SUBD_DO; 919 s->subdev_flags = SDF_WRITEABLE; 920 s->n_chan = 1; 921 s->len_chanlist = 1; 922 s->insn_write = daqp_do_insn_write; 923 924 return 1; 925} 926 927/* daqp_detach (called from comedi_comdig) does nothing. If the PCMCIA 928 * card is removed, daqp_cs_detach() is called by the pcmcia subsystem. 929 */ 930 931static int daqp_detach(struct comedi_device *dev) 932{ 933 printk("comedi%d: detaching daqp\n", dev->minor); 934 935 return 0; 936} 937 938/*==================================================================== 939 940 PCMCIA interface code 941 942 The rest of the code in this file is based on dummy_cs.c v1.24 943 from the Linux pcmcia_cs distribution v3.1.8 and is subject 944 to the following license agreement. 945 946 The remaining contents of this file are subject to the Mozilla Public 947 License Version 1.1 (the "License"); you may not use this file 948 except in compliance with the License. You may obtain a copy of 949 the License at http://www.mozilla.org/MPL/ 950 951 Software distributed under the License is distributed on an "AS 952 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 953 implied. See the License for the specific language governing 954 rights and limitations under the License. 955 956 The initial developer of the original code is David A. Hinds 957 <dhinds@pcmcia.sourceforge.org>. Portions created by David A. Hinds 958 are Copyright (C) 1999 David A. Hinds. All Rights Reserved. 959 960 Alternatively, the contents of this file may be used under the 961 terms of the GNU Public License version 2 (the "GPL"), in which 962 case the provisions of the GPL are applicable instead of the 963 above. If you wish to allow the use of your version of this file 964 only under the terms of the GPL and not to allow others to use 965 your version of this file under the MPL, indicate your decision 966 by deleting the provisions above and replace them with the notice 967 and other provisions required by the GPL. If you do not delete 968 the provisions above, a recipient may use your version of this 969 file under either the MPL or the GPL. 970 971======================================================================*/ 972 973/* 974 The event() function is this driver's Card Services event handler. 975 It will be called by Card Services when an appropriate card status 976 event is received. The config() and release() entry points are 977 used to configure or release a socket, in response to card 978 insertion and ejection events. 979 980 Kernel version 2.6.16 upwards uses suspend() and resume() functions 981 instead of an event() function. 982*/ 983 984static void daqp_cs_config(struct pcmcia_device *link); 985static void daqp_cs_release(struct pcmcia_device *link); 986static int daqp_cs_suspend(struct pcmcia_device *p_dev); 987static int daqp_cs_resume(struct pcmcia_device *p_dev); 988 989/* 990 The attach() and detach() entry points are used to create and destroy 991 "instances" of the driver, where each instance represents everything 992 needed to manage one actual PCMCIA card. 993*/ 994 995static int daqp_cs_attach(struct pcmcia_device *); 996static void daqp_cs_detach(struct pcmcia_device *); 997 998/* 999 The dev_info variable is the "key" that is used to match up this 1000 device driver with appropriate cards, through the card configuration 1001 database. 1002*/ 1003 1004static const dev_info_t dev_info = "quatech_daqp_cs"; 1005 1006/*====================================================================== 1007 1008 daqp_cs_attach() creates an "instance" of the driver, allocating 1009 local data structures for one device. The device is registered 1010 with Card Services. 1011 1012 The dev_link structure is initialized, but we don't actually 1013 configure the card at this point -- we wait until we receive a 1014 card insertion event. 1015 1016======================================================================*/ 1017 1018static int daqp_cs_attach(struct pcmcia_device *link) 1019{ 1020 struct local_info_t *local; 1021 int i; 1022 1023 dev_dbg(&link->dev, "daqp_cs_attach()\n"); 1024 1025 for (i = 0; i < MAX_DEV; i++) 1026 if (dev_table[i] == NULL) 1027 break; 1028 if (i == MAX_DEV) { 1029 printk(KERN_NOTICE "daqp_cs: no devices available\n"); 1030 return -ENODEV; 1031 } 1032 1033 /* Allocate space for private device-specific data */ 1034 local = kzalloc(sizeof(struct local_info_t), GFP_KERNEL); 1035 if (!local) 1036 return -ENOMEM; 1037 1038 local->table_index = i; 1039 dev_table[i] = local; 1040 local->link = link; 1041 link->priv = local; 1042 1043 /* Interrupt setup */ 1044 link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING; 1045 link->irq.Handler = daqp_interrupt; 1046 1047 /* 1048 General socket configuration defaults can go here. In this 1049 client, we assume very little, and rely on the CIS for almost 1050 everything. In most clients, many details (i.e., number, sizes, 1051 and attributes of IO windows) are fixed by the nature of the 1052 device, and can be hard-wired here. 1053 */ 1054 link->conf.Attributes = 0; 1055 link->conf.IntType = INT_MEMORY_AND_IO; 1056 1057 daqp_cs_config(link); 1058 1059 return 0; 1060} /* daqp_cs_attach */ 1061 1062/*====================================================================== 1063 1064 This deletes a driver "instance". The device is de-registered 1065 with Card Services. If it has been released, all local data 1066 structures are freed. Otherwise, the structures will be freed 1067 when the device is released. 1068 1069======================================================================*/ 1070 1071static void daqp_cs_detach(struct pcmcia_device *link) 1072{ 1073 struct local_info_t *dev = link->priv; 1074 1075 dev_dbg(&link->dev, "daqp_cs_detach\n"); 1076 1077 if (link->dev_node) { 1078 dev->stop = 1; 1079 daqp_cs_release(link); 1080 } 1081 1082 /* Unlink device structure, and free it */ 1083 dev_table[dev->table_index] = NULL; 1084 if (dev) 1085 kfree(dev); 1086 1087} /* daqp_cs_detach */ 1088 1089/*====================================================================== 1090 1091 daqp_cs_config() is scheduled to run after a CARD_INSERTION event 1092 is received, to configure the PCMCIA socket, and to make the 1093 device available to the system. 1094 1095======================================================================*/ 1096 1097 1098static int daqp_pcmcia_config_loop(struct pcmcia_device *p_dev, 1099 cistpl_cftable_entry_t *cfg, 1100 cistpl_cftable_entry_t *dflt, 1101 unsigned int vcc, 1102 void *priv_data) 1103{ 1104 if (cfg->index == 0) 1105 return -ENODEV; 1106 1107 /* Do we need to allocate an interrupt? */ 1108 if (cfg->irq.IRQInfo1 || dflt->irq.IRQInfo1) 1109 p_dev->conf.Attributes |= CONF_ENABLE_IRQ; 1110 1111 /* IO window settings */ 1112 p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0; 1113 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) { 1114 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io; 1115 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO; 1116 if (!(io->flags & CISTPL_IO_8BIT)) 1117 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16; 1118 if (!(io->flags & CISTPL_IO_16BIT)) 1119 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8; 1120 p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK; 1121 p_dev->io.BasePort1 = io->win[0].base; 1122 p_dev->io.NumPorts1 = io->win[0].len; 1123 if (io->nwin > 1) { 1124 p_dev->io.Attributes2 = p_dev->io.Attributes1; 1125 p_dev->io.BasePort2 = io->win[1].base; 1126 p_dev->io.NumPorts2 = io->win[1].len; 1127 } 1128 } 1129 1130 /* This reserves IO space but doesn't actually enable it */ 1131 return pcmcia_request_io(p_dev, &p_dev->io); 1132} 1133 1134static void daqp_cs_config(struct pcmcia_device *link) 1135{ 1136 struct local_info_t *dev = link->priv; 1137 int ret; 1138 1139 dev_dbg(&link->dev, "daqp_cs_config\n"); 1140 1141 ret = pcmcia_loop_config(link, daqp_pcmcia_config_loop, NULL); 1142 if (ret) { 1143 dev_warn(&link->dev, "no configuration found\n"); 1144 goto failed; 1145 } 1146 1147 /* 1148 Allocate an interrupt line. Note that this does not assign a 1149 handler to the interrupt, unless the 'Handler' member of the 1150 irq structure is initialized. 1151 */ 1152 if (link->conf.Attributes & CONF_ENABLE_IRQ) { 1153 ret = pcmcia_request_irq(link, &link->irq); 1154 if (ret) 1155 goto failed; 1156 } 1157 1158 /* 1159 This actually configures the PCMCIA socket -- setting up 1160 the I/O windows and the interrupt mapping, and putting the 1161 card and host interface into "Memory and IO" mode. 1162 */ 1163 ret = pcmcia_request_configuration(link, &link->conf); 1164 if (ret) 1165 goto failed; 1166 1167 /* 1168 At this point, the dev_node_t structure(s) need to be 1169 initialized and arranged in a linked list at link->dev. 1170 */ 1171 /* Comedi's PCMCIA script uses this device name (extracted 1172 * from /var/lib/pcmcia/stab) to pass to comedi_config 1173 */ 1174 /* sprintf(dev->node.dev_name, "daqp%d", dev->table_index); */ 1175 sprintf(dev->node.dev_name, "quatech_daqp_cs"); 1176 dev->node.major = dev->node.minor = 0; 1177 link->dev_node = &dev->node; 1178 1179 /* Finally, report what we've done */ 1180 printk(KERN_INFO "%s: index 0x%02x", 1181 dev->node.dev_name, link->conf.ConfigIndex); 1182 if (link->conf.Attributes & CONF_ENABLE_IRQ) 1183 printk(", irq %u", link->irq.AssignedIRQ); 1184 if (link->io.NumPorts1) 1185 printk(", io 0x%04x-0x%04x", link->io.BasePort1, 1186 link->io.BasePort1 + link->io.NumPorts1 - 1); 1187 if (link->io.NumPorts2) 1188 printk(" & 0x%04x-0x%04x", link->io.BasePort2, 1189 link->io.BasePort2 + link->io.NumPorts2 - 1); 1190 printk("\n"); 1191 1192 return; 1193 1194failed: 1195 daqp_cs_release(link); 1196 1197} /* daqp_cs_config */ 1198 1199static void daqp_cs_release(struct pcmcia_device *link) 1200{ 1201 dev_dbg(&link->dev, "daqp_cs_release\n"); 1202 1203 pcmcia_disable_device(link); 1204} /* daqp_cs_release */ 1205 1206/*====================================================================== 1207 1208 The card status event handler. Mostly, this schedules other 1209 stuff to run after an event is received. 1210 1211 When a CARD_REMOVAL event is received, we immediately set a 1212 private flag to block future accesses to this device. All the 1213 functions that actually access the device should check this flag 1214 to make sure the card is still present. 1215 1216======================================================================*/ 1217 1218static int daqp_cs_suspend(struct pcmcia_device *link) 1219{ 1220 struct local_info_t *local = link->priv; 1221 1222 /* Mark the device as stopped, to block IO until later */ 1223 local->stop = 1; 1224 return 0; 1225} 1226 1227static int daqp_cs_resume(struct pcmcia_device *link) 1228{ 1229 struct local_info_t *local = link->priv; 1230 1231 local->stop = 0; 1232 1233 return 0; 1234} 1235 1236/*====================================================================*/ 1237 1238#ifdef MODULE 1239 1240static struct pcmcia_device_id daqp_cs_id_table[] = { 1241 PCMCIA_DEVICE_MANF_CARD(0x0137, 0x0027), 1242 PCMCIA_DEVICE_NULL 1243}; 1244 1245MODULE_DEVICE_TABLE(pcmcia, daqp_cs_id_table); 1246 1247static struct pcmcia_driver daqp_cs_driver = { 1248 .probe = daqp_cs_attach, 1249 .remove = daqp_cs_detach, 1250 .suspend = daqp_cs_suspend, 1251 .resume = daqp_cs_resume, 1252 .id_table = daqp_cs_id_table, 1253 .owner = THIS_MODULE, 1254 .drv = { 1255 .name = dev_info, 1256 }, 1257}; 1258 1259int __init init_module(void) 1260{ 1261 pcmcia_register_driver(&daqp_cs_driver); 1262 comedi_driver_register(&driver_daqp); 1263 return 0; 1264} 1265 1266void __exit cleanup_module(void) 1267{ 1268 comedi_driver_unregister(&driver_daqp); 1269 pcmcia_unregister_driver(&daqp_cs_driver); 1270} 1271 1272#endif 1273