dmm32at.c revision 5f74ea14c07fee91d3bdbaad88bff6264c6200e6
1/* 2 comedi/drivers/dmm32at.c 3 Diamond Systems mm32at code for a Comedi driver 4 5 COMEDI - Linux Control and Measurement Device Interface 6 Copyright (C) 2000 David A. Schleef <ds@schleef.org> 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 22*/ 23/* 24Driver: dmm32at 25Description: Diamond Systems mm32at driver. 26Devices: 27Author: Perry J. Piplani <perry.j.piplani@nasa.gov> 28Updated: Fri Jun 4 09:13:24 CDT 2004 29Status: experimental 30 31This driver is for the Diamond Systems MM-32-AT board 32http://www.diamondsystems.com/products/diamondmm32at It is being used 33on serveral projects inside NASA, without problems so far. For analog 34input commands, TRIG_EXT is not yet supported at all.. 35 36Configuration Options: 37 comedi_config /dev/comedi0 dmm32at baseaddr,irq 38*/ 39 40/* 41 * The previous block comment is used to automatically generate 42 * documentation in Comedi and Comedilib. The fields: 43 * 44 * Driver: the name of the driver 45 * Description: a short phrase describing the driver. Don't list boards. 46 * Devices: a full list of the boards that attempt to be supported by 47 * the driver. Format is "(manufacturer) board name [comedi name]", 48 * where comedi_name is the name that is used to configure the board. 49 * See the comment near board_name: in the struct comedi_driver structure 50 * below. If (manufacturer) or [comedi name] is missing, the previous 51 * value is used. 52 * Author: you 53 * Updated: date when the _documentation_ was last updated. Use 'date -R' 54 * to get a value for this. 55 * Status: a one-word description of the status. Valid values are: 56 * works - driver works correctly on most boards supported, and 57 * passes comedi_test. 58 * unknown - unknown. Usually put there by ds. 59 * experimental - may not work in any particular release. Author 60 * probably wants assistance testing it. 61 * bitrotten - driver has not been update in a long time, probably 62 * doesn't work, and probably is missing support for significant 63 * Comedi interface features. 64 * untested - author probably wrote it "blind", and is believed to 65 * work, but no confirmation. 66 * 67 * These headers should be followed by a blank line, and any comments 68 * you wish to say about the driver. The comment area is the place 69 * to put any known bugs, limitations, unsupported features, supported 70 * command triggers, whether or not commands are supported on particular 71 * subdevices, etc. 72 * 73 * Somewhere in the comment should be information about configuration 74 * options that are used with comedi_config. 75 */ 76 77#include "../comedidev.h" 78#include <linux/ioport.h> 79 80/* Board register addresses */ 81 82#define DMM32AT_MEMSIZE 0x10 83 84#define DMM32AT_CONV 0x00 85#define DMM32AT_AILSB 0x00 86#define DMM32AT_AUXDOUT 0x01 87#define DMM32AT_AIMSB 0x01 88#define DMM32AT_AILOW 0x02 89#define DMM32AT_AIHIGH 0x03 90 91#define DMM32AT_DACLSB 0x04 92#define DMM32AT_DACSTAT 0x04 93#define DMM32AT_DACMSB 0x05 94 95#define DMM32AT_FIFOCNTRL 0x07 96#define DMM32AT_FIFOSTAT 0x07 97 98#define DMM32AT_CNTRL 0x08 99#define DMM32AT_AISTAT 0x08 100 101#define DMM32AT_INTCLOCK 0x09 102 103#define DMM32AT_CNTRDIO 0x0a 104 105#define DMM32AT_AICONF 0x0b 106#define DMM32AT_AIRBACK 0x0b 107 108#define DMM32AT_CLK1 0x0d 109#define DMM32AT_CLK2 0x0e 110#define DMM32AT_CLKCT 0x0f 111 112#define DMM32AT_DIOA 0x0c 113#define DMM32AT_DIOB 0x0d 114#define DMM32AT_DIOC 0x0e 115#define DMM32AT_DIOCONF 0x0f 116 117#define dmm_inb(cdev, reg) inb((cdev->iobase)+reg) 118#define dmm_outb(cdev, reg, valu) outb(valu, (cdev->iobase)+reg) 119 120/* Board register values. */ 121 122/* DMM32AT_DACSTAT 0x04 */ 123#define DMM32AT_DACBUSY 0x80 124 125/* DMM32AT_FIFOCNTRL 0x07 */ 126#define DMM32AT_FIFORESET 0x02 127#define DMM32AT_SCANENABLE 0x04 128 129/* DMM32AT_CNTRL 0x08 */ 130#define DMM32AT_RESET 0x20 131#define DMM32AT_INTRESET 0x08 132#define DMM32AT_CLKACC 0x00 133#define DMM32AT_DIOACC 0x01 134 135/* DMM32AT_AISTAT 0x08 */ 136#define DMM32AT_STATUS 0x80 137 138/* DMM32AT_INTCLOCK 0x09 */ 139#define DMM32AT_ADINT 0x80 140#define DMM32AT_CLKSEL 0x03 141 142/* DMM32AT_CNTRDIO 0x0a */ 143#define DMM32AT_FREQ12 0x80 144 145/* DMM32AT_AICONF 0x0b */ 146#define DMM32AT_RANGE_U10 0x0c 147#define DMM32AT_RANGE_U5 0x0d 148#define DMM32AT_RANGE_B10 0x08 149#define DMM32AT_RANGE_B5 0x00 150#define DMM32AT_SCINT_20 0x00 151#define DMM32AT_SCINT_15 0x10 152#define DMM32AT_SCINT_10 0x20 153#define DMM32AT_SCINT_5 0x30 154 155/* DMM32AT_CLKCT 0x0f */ 156#define DMM32AT_CLKCT1 0x56 /* mode3 counter 1 - write low byte only */ 157#define DMM32AT_CLKCT2 0xb6 /* mode3 counter 2 - write high and low byte */ 158 159/* DMM32AT_DIOCONF 0x0f */ 160#define DMM32AT_DIENABLE 0x80 161#define DMM32AT_DIRA 0x10 162#define DMM32AT_DIRB 0x02 163#define DMM32AT_DIRCL 0x01 164#define DMM32AT_DIRCH 0x08 165 166/* board AI ranges in comedi structure */ 167static const struct comedi_lrange dmm32at_airanges = { 168 4, 169 { 170 UNI_RANGE(10), 171 UNI_RANGE(5), 172 BIP_RANGE(10), 173 BIP_RANGE(5), 174 } 175}; 176 177/* register values for above ranges */ 178static const unsigned char dmm32at_rangebits[] = { 179 DMM32AT_RANGE_U10, 180 DMM32AT_RANGE_U5, 181 DMM32AT_RANGE_B10, 182 DMM32AT_RANGE_B5, 183}; 184 185/* only one of these ranges is valid, as set by a jumper on the 186 * board. The application should only use the range set by the jumper 187 */ 188static const struct comedi_lrange dmm32at_aoranges = { 189 4, 190 { 191 UNI_RANGE(10), 192 UNI_RANGE(5), 193 BIP_RANGE(10), 194 BIP_RANGE(5), 195 } 196}; 197 198/* 199 * Board descriptions for two imaginary boards. Describing the 200 * boards in this way is optional, and completely driver-dependent. 201 * Some drivers use arrays such as this, other do not. 202 */ 203struct dmm32at_board { 204 const char *name; 205 int ai_chans; 206 int ai_bits; 207 const struct comedi_lrange *ai_ranges; 208 int ao_chans; 209 int ao_bits; 210 const struct comedi_lrange *ao_ranges; 211 int have_dio; 212 int dio_chans; 213}; 214static const struct dmm32at_board dmm32at_boards[] = { 215 { 216 .name = "dmm32at", 217 .ai_chans = 32, 218 .ai_bits = 16, 219 .ai_ranges = &dmm32at_airanges, 220 .ao_chans = 4, 221 .ao_bits = 12, 222 .ao_ranges = &dmm32at_aoranges, 223 .have_dio = 1, 224 .dio_chans = 24, 225 }, 226}; 227 228/* 229 * Useful for shorthand access to the particular board structure 230 */ 231#define thisboard ((const struct dmm32at_board *)dev->board_ptr) 232 233/* this structure is for data unique to this hardware driver. If 234 * several hardware drivers keep similar information in this structure, 235 * feel free to suggest moving the variable to the struct comedi_device struct. 236 */ 237struct dmm32at_private { 238 239 int data; 240 int ai_inuse; 241 unsigned int ai_scans_left; 242 243 /* Used for AO readback */ 244 unsigned int ao_readback[4]; 245 unsigned char dio_config; 246 247}; 248 249/* 250 * most drivers define the following macro to make it easy to 251 * access the private structure. 252 */ 253#define devpriv ((struct dmm32at_private *)dev->private) 254 255/* 256 * The struct comedi_driver structure tells the Comedi core module 257 * which functions to call to configure/deconfigure (attach/detach) 258 * the board, and also about the kernel module that contains 259 * the device code. 260 */ 261static int dmm32at_attach(struct comedi_device *dev, struct comedi_devconfig *it); 262static int dmm32at_detach(struct comedi_device *dev); 263static struct comedi_driver driver_dmm32at = { 264 .driver_name = "dmm32at", 265 .module = THIS_MODULE, 266 .attach = dmm32at_attach, 267 .detach = dmm32at_detach, 268/* It is not necessary to implement the following members if you are 269 * writing a driver for a ISA PnP or PCI card */ 270/* Most drivers will support multiple types of boards by 271 * having an array of board structures. These were defined 272 * in dmm32at_boards[] above. Note that the element 'name' 273 * was first in the structure -- Comedi uses this fact to 274 * extract the name of the board without knowing any details 275 * about the structure except for its length. 276 * When a device is attached (by comedi_config), the name 277 * of the device is given to Comedi, and Comedi tries to 278 * match it by going through the list of board names. If 279 * there is a match, the address of the pointer is put 280 * into dev->board_ptr and driver->attach() is called. 281 * 282 * Note that these are not necessary if you can determine 283 * the type of board in software. ISA PnP, PCI, and PCMCIA 284 * devices are such boards. 285 */ 286 .board_name = &dmm32at_boards[0].name, 287 .offset = sizeof(struct dmm32at_board), 288 .num_names = ARRAY_SIZE(dmm32at_boards), 289}; 290 291/* prototypes for driver functions below */ 292static int dmm32at_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s, 293 struct comedi_insn *insn, unsigned int *data); 294static int dmm32at_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s, 295 struct comedi_insn *insn, unsigned int *data); 296static int dmm32at_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s, 297 struct comedi_insn *insn, unsigned int *data); 298static int dmm32at_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s, 299 struct comedi_insn *insn, unsigned int *data); 300static int dmm32at_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s, 301 struct comedi_insn *insn, unsigned int *data); 302static int dmm32at_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, 303 struct comedi_cmd *cmd); 304static int dmm32at_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s); 305static int dmm32at_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s); 306static int dmm32at_ns_to_timer(unsigned int *ns, int round); 307static irqreturn_t dmm32at_isr(int irq, void *d); 308void dmm32at_setaitimer(struct comedi_device *dev, unsigned int nansec); 309 310/* 311 * Attach is called by the Comedi core to configure the driver 312 * for a particular board. If you specified a board_name array 313 * in the driver structure, dev->board_ptr contains that 314 * address. 315 */ 316static int dmm32at_attach(struct comedi_device *dev, struct comedi_devconfig *it) 317{ 318 int ret; 319 struct comedi_subdevice *s; 320 unsigned char aihi, ailo, fifostat, aistat, intstat, airback; 321 unsigned long iobase; 322 unsigned int irq; 323 324 iobase = it->options[0]; 325 irq = it->options[1]; 326 327 printk("comedi%d: dmm32at: attaching\n", dev->minor); 328 printk("dmm32at: probing at address 0x%04lx, irq %u\n", iobase, irq); 329 330 /* register address space */ 331 if (!request_region(iobase, DMM32AT_MEMSIZE, thisboard->name)) { 332 printk("I/O port conflict\n"); 333 return -EIO; 334 } 335 dev->iobase = iobase; 336 337 /* the following just makes sure the board is there and gets 338 it to a known state */ 339 340 /* reset the board */ 341 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_RESET); 342 343 /* allow a millisecond to reset */ 344 udelay(1000); 345 346 /* zero scan and fifo control */ 347 dmm_outb(dev, DMM32AT_FIFOCNTRL, 0x0); 348 349 /* zero interrupt and clock control */ 350 dmm_outb(dev, DMM32AT_INTCLOCK, 0x0); 351 352 /* write a test channel range, the high 3 bits should drop */ 353 dmm_outb(dev, DMM32AT_AILOW, 0x80); 354 dmm_outb(dev, DMM32AT_AIHIGH, 0xff); 355 356 /* set the range at 10v unipolar */ 357 dmm_outb(dev, DMM32AT_AICONF, DMM32AT_RANGE_U10); 358 359 /* should take 10 us to settle, here's a hundred */ 360 udelay(100); 361 362 /* read back the values */ 363 ailo = dmm_inb(dev, DMM32AT_AILOW); 364 aihi = dmm_inb(dev, DMM32AT_AIHIGH); 365 fifostat = dmm_inb(dev, DMM32AT_FIFOSTAT); 366 aistat = dmm_inb(dev, DMM32AT_AISTAT); 367 intstat = dmm_inb(dev, DMM32AT_INTCLOCK); 368 airback = dmm_inb(dev, DMM32AT_AIRBACK); 369 370 printk("dmm32at: lo=0x%02x hi=0x%02x fifostat=0x%02x\n", 371 ailo, aihi, fifostat); 372 printk("dmm32at: aistat=0x%02x intstat=0x%02x airback=0x%02x\n", 373 aistat, intstat, airback); 374 375 if ((ailo != 0x00) || (aihi != 0x1f) || (fifostat != 0x80) || 376 (aistat != 0x60 || (intstat != 0x00) || airback != 0x0c)) { 377 printk("dmmat32: board detection failed\n"); 378 return -EIO; 379 } 380 381 /* board is there, register interrupt */ 382 if (irq) { 383 ret = request_irq(irq, dmm32at_isr, 0, thisboard->name, dev); 384 if (ret < 0) { 385 printk("irq conflict\n"); 386 return ret; 387 } 388 dev->irq = irq; 389 } 390 391/* 392 * If you can probe the device to determine what device in a series 393 * it is, this is the place to do it. Otherwise, dev->board_ptr 394 * should already be initialized. 395 */ 396 /* dev->board_ptr = dmm32at_probe(dev); */ 397 398/* 399 * Initialize dev->board_name. Note that we can use the "thisboard" 400 * macro now, since we just initialized it in the last line. 401 */ 402 dev->board_name = thisboard->name; 403 404/* 405 * Allocate the private structure area. alloc_private() is a 406 * convenient macro defined in comedidev.h. 407 */ 408 if (alloc_private(dev, sizeof(struct dmm32at_private)) < 0) 409 return -ENOMEM; 410 411/* 412 * Allocate the subdevice structures. alloc_subdevice() is a 413 * convenient macro defined in comedidev.h. 414 */ 415 if (alloc_subdevices(dev, 3) < 0) 416 return -ENOMEM; 417 418 s = dev->subdevices + 0; 419 dev->read_subdev = s; 420 /* analog input subdevice */ 421 s->type = COMEDI_SUBD_AI; 422 /* we support single-ended (ground) and differential */ 423 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ; 424 s->n_chan = thisboard->ai_chans; 425 s->maxdata = (1 << thisboard->ai_bits) - 1; 426 s->range_table = thisboard->ai_ranges; 427 s->len_chanlist = 32; /* This is the maximum chanlist length that 428 the board can handle */ 429 s->insn_read = dmm32at_ai_rinsn; 430 s->do_cmd = dmm32at_ai_cmd; 431 s->do_cmdtest = dmm32at_ai_cmdtest; 432 s->cancel = dmm32at_ai_cancel; 433 434 s = dev->subdevices + 1; 435 /* analog output subdevice */ 436 s->type = COMEDI_SUBD_AO; 437 s->subdev_flags = SDF_WRITABLE; 438 s->n_chan = thisboard->ao_chans; 439 s->maxdata = (1 << thisboard->ao_bits) - 1; 440 s->range_table = thisboard->ao_ranges; 441 s->insn_write = dmm32at_ao_winsn; 442 s->insn_read = dmm32at_ao_rinsn; 443 444 s = dev->subdevices + 2; 445 /* digital i/o subdevice */ 446 if (thisboard->have_dio) { 447 448 /* get access to the DIO regs */ 449 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_DIOACC); 450 /* set the DIO's to the defualt input setting */ 451 devpriv->dio_config = DMM32AT_DIRA | DMM32AT_DIRB | 452 DMM32AT_DIRCL | DMM32AT_DIRCH | DMM32AT_DIENABLE; 453 dmm_outb(dev, DMM32AT_DIOCONF, devpriv->dio_config); 454 455 /* set up the subdevice */ 456 s->type = COMEDI_SUBD_DIO; 457 s->subdev_flags = SDF_READABLE | SDF_WRITABLE; 458 s->n_chan = thisboard->dio_chans; 459 s->maxdata = 1; 460 s->state = 0; 461 s->range_table = &range_digital; 462 s->insn_bits = dmm32at_dio_insn_bits; 463 s->insn_config = dmm32at_dio_insn_config; 464 } else { 465 s->type = COMEDI_SUBD_UNUSED; 466 } 467 468 /* success */ 469 printk("comedi%d: dmm32at: attached\n", dev->minor); 470 471 return 1; 472 473} 474 475/* 476 * _detach is called to deconfigure a device. It should deallocate 477 * resources. 478 * This function is also called when _attach() fails, so it should be 479 * careful not to release resources that were not necessarily 480 * allocated by _attach(). dev->private and dev->subdevices are 481 * deallocated automatically by the core. 482 */ 483static int dmm32at_detach(struct comedi_device *dev) 484{ 485 printk("comedi%d: dmm32at: remove\n", dev->minor); 486 if (dev->irq) 487 free_irq(dev->irq, dev); 488 if (dev->iobase) 489 release_region(dev->iobase, DMM32AT_MEMSIZE); 490 491 return 0; 492} 493 494/* 495 * "instructions" read/write data in "one-shot" or "software-triggered" 496 * mode. 497 */ 498 499static int dmm32at_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s, 500 struct comedi_insn *insn, unsigned int *data) 501{ 502 int n, i; 503 unsigned int d; 504 unsigned char status; 505 unsigned short msb, lsb; 506 unsigned char chan; 507 int range; 508 509 /* get the channel and range number */ 510 511 chan = CR_CHAN(insn->chanspec) & (s->n_chan - 1); 512 range = CR_RANGE(insn->chanspec); 513 514 /* printk("channel=0x%02x, range=%d\n",chan,range); */ 515 516 /* zero scan and fifo control and reset fifo */ 517 dmm_outb(dev, DMM32AT_FIFOCNTRL, DMM32AT_FIFORESET); 518 519 /* write the ai channel range regs */ 520 dmm_outb(dev, DMM32AT_AILOW, chan); 521 dmm_outb(dev, DMM32AT_AIHIGH, chan); 522 /* set the range bits */ 523 dmm_outb(dev, DMM32AT_AICONF, dmm32at_rangebits[range]); 524 525 /* wait for circuit to settle */ 526 for (i = 0; i < 40000; i++) { 527 status = dmm_inb(dev, DMM32AT_AIRBACK); 528 if ((status & DMM32AT_STATUS) == 0) 529 break; 530 } 531 if (i == 40000) { 532 printk("timeout\n"); 533 return -ETIMEDOUT; 534 } 535 536 /* convert n samples */ 537 for (n = 0; n < insn->n; n++) { 538 /* trigger conversion */ 539 dmm_outb(dev, DMM32AT_CONV, 0xff); 540 /* wait for conversion to end */ 541 for (i = 0; i < 40000; i++) { 542 status = dmm_inb(dev, DMM32AT_AISTAT); 543 if ((status & DMM32AT_STATUS) == 0) 544 break; 545 } 546 if (i == 40000) { 547 printk("timeout\n"); 548 return -ETIMEDOUT; 549 } 550 551 /* read data */ 552 lsb = dmm_inb(dev, DMM32AT_AILSB); 553 msb = dmm_inb(dev, DMM32AT_AIMSB); 554 555 /* invert sign bit to make range unsigned, this is an 556 idiosyncracy of the diamond board, it return 557 conversions as a signed value, i.e. -32768 to 558 32767, flipping the bit and interpreting it as 559 signed gives you a range of 0 to 65535 which is 560 used by comedi */ 561 d = ((msb ^ 0x0080) << 8) + lsb; 562 563 data[n] = d; 564 } 565 566 /* return the number of samples read/written */ 567 return n; 568} 569 570static int dmm32at_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, 571 struct comedi_cmd *cmd) 572{ 573 int err = 0; 574 int tmp; 575 int start_chan, gain, i; 576 577 /* printk("dmmat32 in command test\n"); */ 578 579 /* cmdtest tests a particular command to see if it is valid. 580 * Using the cmdtest ioctl, a user can create a valid cmd 581 * and then have it executes by the cmd ioctl. 582 * 583 * cmdtest returns 1,2,3,4 or 0, depending on which tests 584 * the command passes. */ 585 586 /* step 1: make sure trigger sources are trivially valid */ 587 588 tmp = cmd->start_src; 589 cmd->start_src &= TRIG_NOW; 590 if (!cmd->start_src || tmp != cmd->start_src) 591 err++; 592 593 tmp = cmd->scan_begin_src; 594 cmd->scan_begin_src &= TRIG_TIMER /*| TRIG_EXT */ ; 595 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src) 596 err++; 597 598 tmp = cmd->convert_src; 599 cmd->convert_src &= TRIG_TIMER /*| TRIG_EXT */ ; 600 if (!cmd->convert_src || tmp != cmd->convert_src) 601 err++; 602 603 tmp = cmd->scan_end_src; 604 cmd->scan_end_src &= TRIG_COUNT; 605 if (!cmd->scan_end_src || tmp != cmd->scan_end_src) 606 err++; 607 608 tmp = cmd->stop_src; 609 cmd->stop_src &= TRIG_COUNT | TRIG_NONE; 610 if (!cmd->stop_src || tmp != cmd->stop_src) 611 err++; 612 613 if (err) 614 return 1; 615 616 /* step 2: make sure trigger sources are unique and mutually compatible */ 617 618 /* note that mutual compatiblity is not an issue here */ 619 if (cmd->scan_begin_src != TRIG_TIMER && 620 cmd->scan_begin_src != TRIG_EXT) 621 err++; 622 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT) 623 err++; 624 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE) 625 err++; 626 627 if (err) 628 return 2; 629 630 /* step 3: make sure arguments are trivially compatible */ 631 632 if (cmd->start_arg != 0) { 633 cmd->start_arg = 0; 634 err++; 635 } 636#define MAX_SCAN_SPEED 1000000 /* in nanoseconds */ 637#define MIN_SCAN_SPEED 1000000000 /* in nanoseconds */ 638 639 if (cmd->scan_begin_src == TRIG_TIMER) { 640 if (cmd->scan_begin_arg < MAX_SCAN_SPEED) { 641 cmd->scan_begin_arg = MAX_SCAN_SPEED; 642 err++; 643 } 644 if (cmd->scan_begin_arg > MIN_SCAN_SPEED) { 645 cmd->scan_begin_arg = MIN_SCAN_SPEED; 646 err++; 647 } 648 } else { 649 /* external trigger */ 650 /* should be level/edge, hi/lo specification here */ 651 /* should specify multiple external triggers */ 652 if (cmd->scan_begin_arg > 9) { 653 cmd->scan_begin_arg = 9; 654 err++; 655 } 656 } 657 if (cmd->convert_src == TRIG_TIMER) { 658 if (cmd->convert_arg >= 17500) 659 cmd->convert_arg = 20000; 660 else if (cmd->convert_arg >= 12500) 661 cmd->convert_arg = 15000; 662 else if (cmd->convert_arg >= 7500) 663 cmd->convert_arg = 10000; 664 else 665 cmd->convert_arg = 5000; 666 667 } else { 668 /* external trigger */ 669 /* see above */ 670 if (cmd->convert_arg > 9) { 671 cmd->convert_arg = 9; 672 err++; 673 } 674 } 675 676 if (cmd->scan_end_arg != cmd->chanlist_len) { 677 cmd->scan_end_arg = cmd->chanlist_len; 678 err++; 679 } 680 if (cmd->stop_src == TRIG_COUNT) { 681 if (cmd->stop_arg > 0xfffffff0) { 682 cmd->stop_arg = 0xfffffff0; 683 err++; 684 } 685 if (cmd->stop_arg == 0) { 686 cmd->stop_arg = 1; 687 err++; 688 } 689 } else { 690 /* TRIG_NONE */ 691 if (cmd->stop_arg != 0) { 692 cmd->stop_arg = 0; 693 err++; 694 } 695 } 696 697 if (err) 698 return 3; 699 700 /* step 4: fix up any arguments */ 701 702 if (cmd->scan_begin_src == TRIG_TIMER) { 703 tmp = cmd->scan_begin_arg; 704 dmm32at_ns_to_timer(&cmd->scan_begin_arg, 705 cmd->flags & TRIG_ROUND_MASK); 706 if (tmp != cmd->scan_begin_arg) 707 err++; 708 } 709 if (cmd->convert_src == TRIG_TIMER) { 710 tmp = cmd->convert_arg; 711 dmm32at_ns_to_timer(&cmd->convert_arg, 712 cmd->flags & TRIG_ROUND_MASK); 713 if (tmp != cmd->convert_arg) 714 err++; 715 if (cmd->scan_begin_src == TRIG_TIMER && 716 cmd->scan_begin_arg < 717 cmd->convert_arg * cmd->scan_end_arg) { 718 cmd->scan_begin_arg = 719 cmd->convert_arg * cmd->scan_end_arg; 720 err++; 721 } 722 } 723 724 if (err) 725 return 4; 726 727 /* step 5 check the channel list, the channel list for this 728 board must be consecutive and gains must be the same */ 729 730 if (cmd->chanlist) { 731 gain = CR_RANGE(cmd->chanlist[0]); 732 start_chan = CR_CHAN(cmd->chanlist[0]); 733 for (i = 1; i < cmd->chanlist_len; i++) { 734 if (CR_CHAN(cmd->chanlist[i]) != 735 (start_chan + i) % s->n_chan) { 736 comedi_error(dev, 737 "entries in chanlist must be consecutive channels, counting upwards\n"); 738 err++; 739 } 740 if (CR_RANGE(cmd->chanlist[i]) != gain) { 741 comedi_error(dev, 742 "entries in chanlist must all have the same gain\n"); 743 err++; 744 } 745 } 746 } 747 748 if (err) 749 return 5; 750 751 return 0; 752} 753 754static int dmm32at_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s) 755{ 756 struct comedi_cmd *cmd = &s->async->cmd; 757 int i, range; 758 unsigned char chanlo, chanhi, status; 759 760 if (!cmd->chanlist) 761 return -EINVAL; 762 763 /* get the channel list and range */ 764 chanlo = CR_CHAN(cmd->chanlist[0]) & (s->n_chan - 1); 765 chanhi = chanlo + cmd->chanlist_len - 1; 766 if (chanhi >= s->n_chan) 767 return -EINVAL; 768 range = CR_RANGE(cmd->chanlist[0]); 769 770 /* reset fifo */ 771 dmm_outb(dev, DMM32AT_FIFOCNTRL, DMM32AT_FIFORESET); 772 773 /* set scan enable */ 774 dmm_outb(dev, DMM32AT_FIFOCNTRL, DMM32AT_SCANENABLE); 775 776 /* write the ai channel range regs */ 777 dmm_outb(dev, DMM32AT_AILOW, chanlo); 778 dmm_outb(dev, DMM32AT_AIHIGH, chanhi); 779 780 /* set the range bits */ 781 dmm_outb(dev, DMM32AT_AICONF, dmm32at_rangebits[range]); 782 783 /* reset the interrupt just in case */ 784 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_INTRESET); 785 786 if (cmd->stop_src == TRIG_COUNT) 787 devpriv->ai_scans_left = cmd->stop_arg; 788 else { /* TRIG_NONE */ 789 devpriv->ai_scans_left = 0xffffffff; /* indicates TRIG_NONE to isr */ 790 } 791 792 /* wait for circuit to settle */ 793 for (i = 0; i < 40000; i++) { 794 status = dmm_inb(dev, DMM32AT_AIRBACK); 795 if ((status & DMM32AT_STATUS) == 0) 796 break; 797 } 798 if (i == 40000) { 799 printk("timeout\n"); 800 return -ETIMEDOUT; 801 } 802 803 if (devpriv->ai_scans_left > 1) { 804 /* start the clock and enable the interrupts */ 805 dmm32at_setaitimer(dev, cmd->scan_begin_arg); 806 } else { 807 /* start the interrups and initiate a single scan */ 808 dmm_outb(dev, DMM32AT_INTCLOCK, DMM32AT_ADINT); 809 dmm_outb(dev, DMM32AT_CONV, 0xff); 810 } 811 812/* printk("dmmat32 in command\n"); */ 813 814/* for(i=0;i<cmd->chanlist_len;i++) */ 815/* comedi_buf_put(s->async,i*100); */ 816 817/* s->async->events |= COMEDI_CB_EOA; */ 818/* comedi_event(dev, s); */ 819 820 return 0; 821 822} 823 824static int dmm32at_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s) 825{ 826 devpriv->ai_scans_left = 1; 827 return 0; 828} 829 830static irqreturn_t dmm32at_isr(int irq, void *d) 831{ 832 unsigned char intstat; 833 unsigned int samp; 834 unsigned short msb, lsb; 835 int i; 836 struct comedi_device *dev = d; 837 838 if (!dev->attached) { 839 comedi_error(dev, "spurious interrupt"); 840 return IRQ_HANDLED; 841 } 842 843 intstat = dmm_inb(dev, DMM32AT_INTCLOCK); 844 845 if (intstat & DMM32AT_ADINT) { 846 struct comedi_subdevice *s = dev->read_subdev; 847 struct comedi_cmd *cmd = &s->async->cmd; 848 849 for (i = 0; i < cmd->chanlist_len; i++) { 850 /* read data */ 851 lsb = dmm_inb(dev, DMM32AT_AILSB); 852 msb = dmm_inb(dev, DMM32AT_AIMSB); 853 854 /* invert sign bit to make range unsigned */ 855 samp = ((msb ^ 0x0080) << 8) + lsb; 856 comedi_buf_put(s->async, samp); 857 } 858 859 if (devpriv->ai_scans_left != 0xffffffff) { /* TRIG_COUNT */ 860 devpriv->ai_scans_left--; 861 if (devpriv->ai_scans_left == 0) { 862 /* disable further interrupts and clocks */ 863 dmm_outb(dev, DMM32AT_INTCLOCK, 0x0); 864 /* set the buffer to be flushed with an EOF */ 865 s->async->events |= COMEDI_CB_EOA; 866 } 867 868 } 869 /* flush the buffer */ 870 comedi_event(dev, s); 871 } 872 873 /* reset the interrupt */ 874 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_INTRESET); 875 return IRQ_HANDLED; 876} 877 878/* This function doesn't require a particular form, this is just 879 * what happens to be used in some of the drivers. It should 880 * convert ns nanoseconds to a counter value suitable for programming 881 * the device. Also, it should adjust ns so that it cooresponds to 882 * the actual time that the device will use. */ 883static int dmm32at_ns_to_timer(unsigned int *ns, int round) 884{ 885 /* trivial timer */ 886 /* if your timing is done through two cascaded timers, the 887 * i8253_cascade_ns_to_timer() function in 8253.h can be 888 * very helpful. There are also i8254_load() and i8254_mm_load() 889 * which can be used to load values into the ubiquitous 8254 counters 890 */ 891 892 return *ns; 893} 894 895static int dmm32at_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s, 896 struct comedi_insn *insn, unsigned int *data) 897{ 898 int i; 899 int chan = CR_CHAN(insn->chanspec); 900 unsigned char hi, lo, status; 901 902 /* Writing a list of values to an AO channel is probably not 903 * very useful, but that's how the interface is defined. */ 904 for (i = 0; i < insn->n; i++) { 905 906 devpriv->ao_readback[chan] = data[i]; 907 908 /* get the low byte */ 909 lo = data[i] & 0x00ff; 910 /* high byte also contains channel number */ 911 hi = (data[i] >> 8) + chan * (1 << 6); 912 /* printk("writing 0x%02x 0x%02x\n",hi,lo); */ 913 /* write the low and high values to the board */ 914 dmm_outb(dev, DMM32AT_DACLSB, lo); 915 dmm_outb(dev, DMM32AT_DACMSB, hi); 916 917 /* wait for circuit to settle */ 918 for (i = 0; i < 40000; i++) { 919 status = dmm_inb(dev, DMM32AT_DACSTAT); 920 if ((status & DMM32AT_DACBUSY) == 0) 921 break; 922 } 923 if (i == 40000) { 924 printk("timeout\n"); 925 return -ETIMEDOUT; 926 } 927 /* dummy read to update trigger the output */ 928 status = dmm_inb(dev, DMM32AT_DACMSB); 929 930 } 931 932 /* return the number of samples read/written */ 933 return i; 934} 935 936/* AO subdevices should have a read insn as well as a write insn. 937 * Usually this means copying a value stored in devpriv. */ 938static int dmm32at_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s, 939 struct comedi_insn *insn, unsigned int *data) 940{ 941 int i; 942 int chan = CR_CHAN(insn->chanspec); 943 944 for (i = 0; i < insn->n; i++) 945 data[i] = devpriv->ao_readback[chan]; 946 947 return i; 948} 949 950/* DIO devices are slightly special. Although it is possible to 951 * implement the insn_read/insn_write interface, it is much more 952 * useful to applications if you implement the insn_bits interface. 953 * This allows packed reading/writing of the DIO channels. The 954 * comedi core can convert between insn_bits and insn_read/write */ 955static int dmm32at_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s, 956 struct comedi_insn *insn, unsigned int *data) 957{ 958 unsigned char diobits; 959 960 if (insn->n != 2) 961 return -EINVAL; 962 963 /* The insn data is a mask in data[0] and the new data 964 * in data[1], each channel cooresponding to a bit. */ 965 if (data[0]) { 966 s->state &= ~data[0]; 967 s->state |= data[0] & data[1]; 968 /* Write out the new digital output lines */ 969 /* outw(s->state,dev->iobase + DMM32AT_DIO); */ 970 } 971 972 /* get access to the DIO regs */ 973 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_DIOACC); 974 975 /* if either part of dio is set for output */ 976 if (((devpriv->dio_config & DMM32AT_DIRCL) == 0) || 977 ((devpriv->dio_config & DMM32AT_DIRCH) == 0)) { 978 diobits = (s->state & 0x00ff0000) >> 16; 979 dmm_outb(dev, DMM32AT_DIOC, diobits); 980 } 981 if ((devpriv->dio_config & DMM32AT_DIRB) == 0) { 982 diobits = (s->state & 0x0000ff00) >> 8; 983 dmm_outb(dev, DMM32AT_DIOB, diobits); 984 } 985 if ((devpriv->dio_config & DMM32AT_DIRA) == 0) { 986 diobits = (s->state & 0x000000ff); 987 dmm_outb(dev, DMM32AT_DIOA, diobits); 988 } 989 990 /* now read the state back in */ 991 s->state = dmm_inb(dev, DMM32AT_DIOC); 992 s->state <<= 8; 993 s->state |= dmm_inb(dev, DMM32AT_DIOB); 994 s->state <<= 8; 995 s->state |= dmm_inb(dev, DMM32AT_DIOA); 996 data[1] = s->state; 997 998 /* on return, data[1] contains the value of the digital 999 * input and output lines. */ 1000 /* data[1]=inw(dev->iobase + DMM32AT_DIO); */ 1001 /* or we could just return the software copy of the output values if 1002 * it was a purely digital output subdevice */ 1003 /* data[1]=s->state; */ 1004 1005 return 2; 1006} 1007 1008static int dmm32at_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s, 1009 struct comedi_insn *insn, unsigned int *data) 1010{ 1011 unsigned char chanbit; 1012 int chan = CR_CHAN(insn->chanspec); 1013 1014 if (insn->n != 1) 1015 return -EINVAL; 1016 1017 if (chan < 8) 1018 chanbit = DMM32AT_DIRA; 1019 else if (chan < 16) 1020 chanbit = DMM32AT_DIRB; 1021 else if (chan < 20) 1022 chanbit = DMM32AT_DIRCL; 1023 else 1024 chanbit = DMM32AT_DIRCH; 1025 1026 /* The input or output configuration of each digital line is 1027 * configured by a special insn_config instruction. chanspec 1028 * contains the channel to be changed, and data[0] contains the 1029 * value COMEDI_INPUT or COMEDI_OUTPUT. */ 1030 1031 /* if output clear the bit, otherwise set it */ 1032 if (data[0] == COMEDI_OUTPUT) { 1033 devpriv->dio_config &= ~chanbit; 1034 } else { 1035 devpriv->dio_config |= chanbit; 1036 } 1037 /* get access to the DIO regs */ 1038 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_DIOACC); 1039 /* set the DIO's to the new configuration setting */ 1040 dmm_outb(dev, DMM32AT_DIOCONF, devpriv->dio_config); 1041 1042 return 1; 1043} 1044 1045void dmm32at_setaitimer(struct comedi_device *dev, unsigned int nansec) 1046{ 1047 unsigned char lo1, lo2, hi2; 1048 unsigned short both2; 1049 1050 /* based on 10mhz clock */ 1051 lo1 = 200; 1052 both2 = nansec / 20000; 1053 hi2 = (both2 & 0xff00) >> 8; 1054 lo2 = both2 & 0x00ff; 1055 1056 /* set the counter frequency to 10mhz */ 1057 dmm_outb(dev, DMM32AT_CNTRDIO, 0); 1058 1059 /* get access to the clock regs */ 1060 dmm_outb(dev, DMM32AT_CNTRL, DMM32AT_CLKACC); 1061 1062 /* write the counter 1 control word and low byte to counter */ 1063 dmm_outb(dev, DMM32AT_CLKCT, DMM32AT_CLKCT1); 1064 dmm_outb(dev, DMM32AT_CLK1, lo1); 1065 1066 /* write the counter 2 control word and low byte then to counter */ 1067 dmm_outb(dev, DMM32AT_CLKCT, DMM32AT_CLKCT2); 1068 dmm_outb(dev, DMM32AT_CLK2, lo2); 1069 dmm_outb(dev, DMM32AT_CLK2, hi2); 1070 1071 /* enable the ai conversion interrupt and the clock to start scans */ 1072 dmm_outb(dev, DMM32AT_INTCLOCK, DMM32AT_ADINT | DMM32AT_CLKSEL); 1073 1074} 1075 1076/* 1077 * A convenient macro that defines init_module() and cleanup_module(), 1078 * as necessary. 1079 */ 1080COMEDI_INITCLEANUP(driver_dmm32at); 1081