5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7 Copyright (C) 2000 Chris R. Baugher <baugher@enteract.com>
8 Copyright (C) 2001,2002 Frank Mori Hess <fmhess@users.sourceforge.net>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
22 Description: DAS16 compatible boards
23 Author: Sam Moore, Warren Jasper, ds, Chris Baugher, Frank Hess, Roman Fietze
24 Devices: [Keithley Metrabyte] DAS-16 (das-16), DAS-16G (das-16g),
25 DAS-16F (das-16f), DAS-1201 (das-1201), DAS-1202 (das-1202),
26 DAS-1401 (das-1401), DAS-1402 (das-1402), DAS-1601 (das-1601),
28 [ComputerBoards] PC104-DAS16/JR (pc104-das16jr),
29 PC104-DAS16JR/16 (pc104-das16jr/16),
30 CIO-DAS16JR/16 (cio-das16jr/16),
31 CIO-DAS16/JR (cio-das16/jr), CIO-DAS1401/12 (cio-das1401/12),
32 CIO-DAS1402/12 (cio-das1402/12), CIO-DAS1402/16 (cio-das1402/16),
33 CIO-DAS1601/12 (cio-das1601/12), CIO-DAS1602/12 (cio-das1602/12),
34 CIO-DAS1602/16 (cio-das1602/16), CIO-DAS16/330 (cio-das16/330)
38 A rewrite of the das16 and das1600 drivers.
41 [1] - irq (does nothing, irq is not used anymore)
42 [2] - dma (optional, required for comedi_command support)
43 [3] - master clock speed in MHz (optional, 1 or 10, ignored if
44 board can probe clock, defaults to 1)
45 [4] - analog input range lowest voltage in microvolts (optional,
46 only useful if your board does not have software
48 [5] - analog input range highest voltage in microvolts (optional,
49 only useful if board does not have software programmable
51 [6] - analog output range lowest voltage in microvolts (optional)
52 [7] - analog output range highest voltage in microvolts (optional)
53 [8] - use timer mode for DMA. Timer mode is needed e.g. for
54 buggy DMA controllers in NS CS5530A (Geode Companion), and for
55 'jr' cards that lack a hardware fifo. This option is no
56 longer needed, since timer mode is _always_ used.
58 Passing a zero for an option is the same as leaving it unspecified.
63 Testing and debugging help provided by Daniel Koch.
67 4919.PDF (das1400, 1600)
69 4923.PDF (das1200, 1400, 1600)
71 Computer boards manuals also available from their website
72 www.measurementcomputing.com
76 #include <linux/module.h>
77 #include <linux/delay.h>
78 #include <linux/pci.h>
79 #include <linux/interrupt.h>
83 #include "../comedidev.h"
87 #include "comedi_fc.h"
93 #define DEBUG_PRINT(format, args...) \
94 printk(KERN_DEBUG "das16: " format, ## args)
96 #define DEBUG_PRINT(format, args...)
99 #define DAS16_SIZE 20 /* number of ioports */
100 #define DAS16_DMA_SIZE 0xff00 /* size in bytes of allocated dma buffer */
108 0 a/d bits 0-3 start 12 bit
109 1 a/d bits 4-11 unused
116 8 status eoc uni/bip interrupt reset
117 9 dma, int, trig ctrl set dma, int
118 a pacer control unused
130 0 a/d bits 0-3 start 12 bit
131 1 a/d bits 4-11 unused
135 8 status eoc uni/bip interrupt reset
136 9 dma, int, trig ctrl set dma, int
137 a pacer control unused
138 b gain status gain control
148 0 a/d bits 0-7 start 16 bit
149 1 a/d bits 8-15 unused
153 8 status eoc uni/bip interrupt reset
154 9 dma, int, trig ctrl set dma, int
155 a pacer control unused
156 b gain status gain control
167 0 a/d bits 0-3 start 12 bit
168 1 a/d bits 4-11 unused
175 8 status eoc uni/bip interrupt reset
176 9 dma, int, trig ctrl set dma, int
177 a pacer control unused
178 b gain status gain control
181 404 unused conversion enable
182 405 unused burst enable
183 406 unused das1600 enable
188 /* size in bytes of a sample from board */
189 static const int sample_size = 2;
192 #define DAS16_AI_LSB 0
193 #define DAS16_AI_MSB 1
196 #define DAS16_AO_LSB(x) ((x) ? 6 : 4)
197 #define DAS16_AO_MSB(x) ((x) ? 7 : 5)
198 #define DAS16_STATUS 8
200 #define UNIPOLAR (1<<6)
201 #define DAS16_MUXBIT (1<<5)
202 #define DAS16_INT (1<<4)
203 #define DAS16_CONTROL 9
204 #define DAS16_INTE (1<<7)
205 #define DAS16_IRQ(x) (((x) & 0x7) << 4)
206 #define DMA_ENABLE (1<<2)
207 #define PACING_MASK 0x3
208 #define INT_PACER 0x03
209 #define EXT_PACER 0x02
210 #define DAS16_SOFT 0x00
211 #define DAS16_PACER 0x0A
212 #define DAS16_CTR0 (1<<1)
213 #define DAS16_TRIG0 (1<<0)
214 #define BURST_LEN_BITS(x) (((x) & 0xf) << 4)
215 #define DAS16_GAIN 0x0B
216 #define DAS16_CNTR0_DATA 0x0C
217 #define DAS16_CNTR1_DATA 0x0D
218 #define DAS16_CNTR2_DATA 0x0E
219 #define DAS16_CNTR_CONTROL 0x0F
220 #define DAS16_TERM_CNT 0x00
221 #define DAS16_ONE_SHOT 0x02
222 #define DAS16_RATE_GEN 0x04
223 #define DAS16_CNTR_LSB_MSB 0x30
224 #define DAS16_CNTR0 0x00
225 #define DAS16_CNTR1 0x40
226 #define DAS16_CNTR2 0x80
228 #define DAS1600_CONV 0x404
229 #define DAS1600_CONV_DISABLE 0x40
230 #define DAS1600_BURST 0x405
231 #define DAS1600_BURST_VAL 0x40
232 #define DAS1600_ENABLE 0x406
233 #define DAS1600_ENABLE_VAL 0x40
234 #define DAS1600_STATUS_B 0x407
235 #define DAS1600_BME 0x40
236 #define DAS1600_ME 0x20
237 #define DAS1600_CD 0x10
238 #define DAS1600_WS 0x02
239 #define DAS1600_CLK_10MHZ 0x01
241 static const struct comedi_lrange range_das1x01_bip = {
250 static const struct comedi_lrange range_das1x01_unip = {
259 static const struct comedi_lrange range_das1x02_bip = {
268 static const struct comedi_lrange range_das1x02_unip = {
277 static const struct comedi_lrange range_das16jr = {
291 static const struct comedi_lrange range_das16jr_16 = {
304 static const int das16jr_gainlist[] = { 8, 0, 1, 2, 3, 4, 5, 6, 7 };
305 static const int das16jr_16_gainlist[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
306 static const int das1600_gainlist[] = { 0, 1, 2, 3 };
315 static const int *const das16_gainlists[] = {
323 static const struct comedi_lrange *const das16_ai_uni_lranges[] = {
331 static const struct comedi_lrange *const das16_ai_bip_lranges[] = {
341 unsigned int ai_maxdata;
342 unsigned int ai_speed; /* max conversion speed in nanosec */
344 unsigned int has_ao:1;
346 unsigned int i8255_offset;
347 unsigned int i8254_offset;
353 #define DAS16_TIMEOUT 1000
355 /* Period for timer interrupt in jiffies. It's a function
356 * to deal with possibility of dynamic HZ patches */
357 static inline int timer_period(void)
362 struct das16_private_struct {
363 unsigned int clockbase; /* master clock speed in ns */
364 volatile unsigned int control_state; /* dma, interrupt and trigger control bits */
365 volatile unsigned long adc_byte_count; /* number of bytes remaining */
366 /* divisor dividing master clock to get conversion frequency */
367 unsigned int divisor1;
368 /* divisor dividing master clock to get conversion frequency */
369 unsigned int divisor2;
370 unsigned int dma_chan; /* dma channel */
371 uint16_t *dma_buffer[2];
372 dma_addr_t dma_buffer_addr[2];
373 unsigned int current_buffer;
374 volatile unsigned int dma_transfer_size; /* target number of bytes to transfer per dma shot */
376 * user-defined analog input and output ranges
377 * defined from config options
379 struct comedi_lrange *user_ai_range_table;
380 struct comedi_lrange *user_ao_range_table;
382 struct timer_list timer; /* for timed interrupt */
383 volatile short timer_running;
385 unsigned long extra_iobase;
388 static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
389 struct comedi_cmd *cmd)
391 const struct das16_board *board = comedi_board(dev);
392 struct das16_private_struct *devpriv = dev->private;
394 int gain, start_chan, i;
397 /* Step 1 : check if triggers are trivially valid */
399 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
402 /* if board supports burst mode */
403 if (board->size > 0x400)
404 mask |= TRIG_TIMER | TRIG_EXT;
405 err |= cfc_check_trigger_src(&cmd->scan_begin_src, mask);
407 tmp = cmd->convert_src;
408 mask = TRIG_TIMER | TRIG_EXT;
409 /* if board supports burst mode */
410 if (board->size > 0x400)
412 err |= cfc_check_trigger_src(&cmd->convert_src, mask);
414 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
415 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
420 /* Step 2a : make sure trigger sources are unique */
422 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
423 err |= cfc_check_trigger_is_unique(cmd->convert_src);
424 err |= cfc_check_trigger_is_unique(cmd->stop_src);
426 /* Step 2b : and mutually compatible */
428 /* make sure scan_begin_src and convert_src dont conflict */
429 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
431 if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
437 /* Step 3: check if arguments are trivially valid */
439 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
441 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
442 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
444 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
446 /* check against maximum frequency */
447 if (cmd->scan_begin_src == TRIG_TIMER)
448 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
449 board->ai_speed * cmd->chanlist_len);
451 if (cmd->convert_src == TRIG_TIMER)
452 err |= cfc_check_trigger_arg_min(&cmd->convert_arg,
455 if (cmd->stop_src == TRIG_NONE)
456 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
461 /* step 4: fix up arguments */
462 if (cmd->scan_begin_src == TRIG_TIMER) {
463 unsigned int tmp = cmd->scan_begin_arg;
464 /* set divisors, correct timing arguments */
465 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
466 &(devpriv->divisor1),
467 &(devpriv->divisor2),
468 &(cmd->scan_begin_arg),
469 cmd->flags & TRIG_ROUND_MASK);
470 err += (tmp != cmd->scan_begin_arg);
472 if (cmd->convert_src == TRIG_TIMER) {
473 unsigned int tmp = cmd->convert_arg;
474 /* set divisors, correct timing arguments */
475 i8253_cascade_ns_to_timer_2div(devpriv->clockbase,
476 &(devpriv->divisor1),
477 &(devpriv->divisor2),
479 cmd->flags & TRIG_ROUND_MASK);
480 err += (tmp != cmd->convert_arg);
485 /* check channel/gain list against card's limitations */
487 gain = CR_RANGE(cmd->chanlist[0]);
488 start_chan = CR_CHAN(cmd->chanlist[0]);
489 for (i = 1; i < cmd->chanlist_len; i++) {
490 if (CR_CHAN(cmd->chanlist[i]) !=
491 (start_chan + i) % s->n_chan) {
493 "entries in chanlist must be "
494 "consecutive channels, "
495 "counting upwards\n");
498 if (CR_RANGE(cmd->chanlist[i]) != gain) {
500 "entries in chanlist must all "
501 "have the same gain\n");
512 static unsigned int das16_set_pacer(struct comedi_device *dev, unsigned int ns,
515 struct das16_private_struct *devpriv = dev->private;
517 i8253_cascade_ns_to_timer_2div(devpriv->clockbase, &(devpriv->divisor1),
518 &(devpriv->divisor2), &ns,
519 rounding_flags & TRIG_ROUND_MASK);
521 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
522 i8254_load(dev->iobase + DAS16_CNTR0_DATA, 0, 1, devpriv->divisor1, 2);
523 i8254_load(dev->iobase + DAS16_CNTR0_DATA, 0, 2, devpriv->divisor2, 2);
528 static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
530 const struct das16_board *board = comedi_board(dev);
531 struct das16_private_struct *devpriv = dev->private;
532 struct comedi_async *async = s->async;
533 struct comedi_cmd *cmd = &async->cmd;
538 if (cmd->flags & TRIG_RT) {
539 comedi_error(dev, "isa dma transfers cannot be performed with "
540 "TRIG_RT, aborting");
544 devpriv->adc_byte_count =
545 cmd->stop_arg * cmd->chanlist_len * sizeof(uint16_t);
547 /* disable conversions for das1600 mode */
548 if (board->size > 0x400)
549 outb(DAS1600_CONV_DISABLE, dev->iobase + DAS1600_CONV);
551 /* set scan limits */
552 byte = CR_CHAN(cmd->chanlist[0]);
553 byte |= CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]) << 4;
554 outb(byte, dev->iobase + DAS16_MUX);
556 /* set gain (this is also burst rate register but according to
557 * computer boards manual, burst rate does nothing, even on
559 if (board->ai_pg != das16_pg_none) {
560 range = CR_RANGE(cmd->chanlist[0]);
561 outb((das16_gainlists[board->ai_pg])[range],
562 dev->iobase + DAS16_GAIN);
565 /* set counter mode and counts */
567 das16_set_pacer(dev, cmd->convert_arg,
568 cmd->flags & TRIG_ROUND_MASK);
569 DEBUG_PRINT("pacer period: %d ns\n", cmd->convert_arg);
571 /* enable counters */
573 /* Enable burst mode if appropriate. */
574 if (board->size > 0x400) {
575 if (cmd->convert_src == TRIG_NOW) {
576 outb(DAS1600_BURST_VAL, dev->iobase + DAS1600_BURST);
577 /* set burst length */
578 byte |= BURST_LEN_BITS(cmd->chanlist_len - 1);
580 outb(0, dev->iobase + DAS1600_BURST);
583 outb(byte, dev->iobase + DAS16_PACER);
585 /* set up dma transfer */
586 flags = claim_dma_lock();
587 disable_dma(devpriv->dma_chan);
588 /* clear flip-flop to make sure 2-byte registers for
589 * count and address get set correctly */
590 clear_dma_ff(devpriv->dma_chan);
591 devpriv->current_buffer = 0;
592 set_dma_addr(devpriv->dma_chan,
593 devpriv->dma_buffer_addr[devpriv->current_buffer]);
594 devpriv->dma_transfer_size = DAS16_DMA_SIZE;
595 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
596 enable_dma(devpriv->dma_chan);
597 release_dma_lock(flags);
599 /* set up interrupt */
600 devpriv->timer_running = 1;
601 devpriv->timer.expires = jiffies + timer_period();
602 add_timer(&devpriv->timer);
603 devpriv->control_state &= ~DAS16_INTE;
604 devpriv->control_state |= DMA_ENABLE;
605 devpriv->control_state &= ~PACING_MASK;
606 if (cmd->convert_src == TRIG_EXT)
607 devpriv->control_state |= EXT_PACER;
609 devpriv->control_state |= INT_PACER;
610 outb(devpriv->control_state, dev->iobase + DAS16_CONTROL);
612 /* Enable conversions if using das1600 mode */
613 if (board->size > 0x400)
614 outb(0, dev->iobase + DAS1600_CONV);
620 static int das16_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
622 const struct das16_board *board = comedi_board(dev);
623 struct das16_private_struct *devpriv = dev->private;
626 spin_lock_irqsave(&dev->spinlock, flags);
627 /* disable interrupts, dma and pacer clocked conversions */
628 devpriv->control_state &= ~DAS16_INTE & ~PACING_MASK & ~DMA_ENABLE;
629 outb(devpriv->control_state, dev->iobase + DAS16_CONTROL);
630 disable_dma(devpriv->dma_chan);
632 /* disable SW timer */
633 if (devpriv->timer_running) {
634 devpriv->timer_running = 0;
635 del_timer(&devpriv->timer);
638 /* disable burst mode */
639 if (board->size > 0x400)
640 outb(0, dev->iobase + DAS1600_BURST);
643 spin_unlock_irqrestore(&dev->spinlock, flags);
648 static void das16_reset(struct comedi_device *dev)
650 outb(0, dev->iobase + DAS16_STATUS);
651 outb(0, dev->iobase + DAS16_CONTROL);
652 outb(0, dev->iobase + DAS16_PACER);
653 outb(0, dev->iobase + DAS16_CNTR_CONTROL);
656 static int das16_ai_insn_read(struct comedi_device *dev,
657 struct comedi_subdevice *s,
658 struct comedi_insn *insn,
661 const struct das16_board *board = comedi_board(dev);
662 struct das16_private_struct *devpriv = dev->private;
667 /* disable interrupts and pacing */
668 devpriv->control_state &= ~DAS16_INTE & ~DMA_ENABLE & ~PACING_MASK;
669 outb(devpriv->control_state, dev->iobase + DAS16_CONTROL);
671 /* set multiplexer */
672 chan = CR_CHAN(insn->chanspec);
673 chan |= CR_CHAN(insn->chanspec) << 4;
674 outb(chan, dev->iobase + DAS16_MUX);
677 if (board->ai_pg != das16_pg_none) {
678 range = CR_RANGE(insn->chanspec);
679 outb((das16_gainlists[board->ai_pg])[range],
680 dev->iobase + DAS16_GAIN);
683 for (n = 0; n < insn->n; n++) {
686 /* trigger conversion */
687 outb_p(0, dev->iobase + DAS16_TRIG);
689 for (i = 0; i < DAS16_TIMEOUT; i++) {
690 if (!(inb(dev->iobase + DAS16_STATUS) & BUSY))
693 if (i == DAS16_TIMEOUT) {
694 printk("das16: timeout\n");
698 val = inb(dev->iobase + DAS16_AI_MSB) << 8;
699 val |= inb(dev->iobase + DAS16_AI_LSB);
700 if (s->maxdata == 0x0fff)
710 static int das16_di_insn_bits(struct comedi_device *dev,
711 struct comedi_subdevice *s,
712 struct comedi_insn *insn,
715 data[1] = inb(dev->iobase + DAS16_DIO) & 0xf;
720 static int das16_do_insn_bits(struct comedi_device *dev,
721 struct comedi_subdevice *s,
722 struct comedi_insn *insn,
725 unsigned int mask = data[0];
726 unsigned int bits = data[1];
730 s->state |= (bits & mask);
732 outb(s->state, dev->iobase + DAS16_DIO);
740 static int das16_ao_insn_write(struct comedi_device *dev,
741 struct comedi_subdevice *s,
742 struct comedi_insn *insn,
745 unsigned int chan = CR_CHAN(insn->chanspec);
749 for (i = 0; i < insn->n; i++) {
753 outb(val & 0xff, dev->iobase + DAS16_AO_LSB(chan));
754 outb((val >> 8) & 0xff, dev->iobase + DAS16_AO_MSB(chan));
760 /* the pc104-das16jr (at least) has problems if the dma
761 transfer is interrupted in the middle of transferring
762 a 16 bit sample, so this function takes care to get
763 an even transfer count after disabling dma
766 static int disable_dma_on_even(struct comedi_device *dev)
768 struct das16_private_struct *devpriv = dev->private;
771 static const int disable_limit = 100;
772 static const int enable_timeout = 100;
774 disable_dma(devpriv->dma_chan);
775 residue = get_dma_residue(devpriv->dma_chan);
776 for (i = 0; i < disable_limit && (residue % 2); ++i) {
778 enable_dma(devpriv->dma_chan);
779 for (j = 0; j < enable_timeout; ++j) {
782 new_residue = get_dma_residue(devpriv->dma_chan);
783 if (new_residue != residue)
786 disable_dma(devpriv->dma_chan);
787 residue = get_dma_residue(devpriv->dma_chan);
789 if (i == disable_limit) {
790 comedi_error(dev, "failed to get an even dma transfer, "
791 "could be trouble.");
796 static void das16_interrupt(struct comedi_device *dev)
798 struct das16_private_struct *devpriv = dev->private;
799 unsigned long dma_flags, spin_flags;
800 struct comedi_subdevice *s = dev->read_subdev;
801 struct comedi_async *async;
802 struct comedi_cmd *cmd;
803 int num_bytes, residue;
806 if (!dev->attached) {
807 comedi_error(dev, "premature interrupt");
810 /* initialize async here to make sure it is not NULL */
814 spin_lock_irqsave(&dev->spinlock, spin_flags);
815 if ((devpriv->control_state & DMA_ENABLE) == 0) {
816 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
817 DEBUG_PRINT("interrupt while dma disabled?\n");
821 dma_flags = claim_dma_lock();
822 clear_dma_ff(devpriv->dma_chan);
823 residue = disable_dma_on_even(dev);
825 /* figure out how many points to read */
826 if (residue > devpriv->dma_transfer_size) {
827 comedi_error(dev, "residue > transfer size!\n");
828 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
831 num_bytes = devpriv->dma_transfer_size - residue;
833 if (cmd->stop_src == TRIG_COUNT &&
834 num_bytes >= devpriv->adc_byte_count) {
835 num_bytes = devpriv->adc_byte_count;
836 async->events |= COMEDI_CB_EOA;
839 buffer_index = devpriv->current_buffer;
840 devpriv->current_buffer = (devpriv->current_buffer + 1) % 2;
841 devpriv->adc_byte_count -= num_bytes;
844 if ((async->events & COMEDI_CB_EOA) == 0) {
845 set_dma_addr(devpriv->dma_chan,
846 devpriv->dma_buffer_addr[devpriv->current_buffer]);
847 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
848 enable_dma(devpriv->dma_chan);
850 release_dma_lock(dma_flags);
852 spin_unlock_irqrestore(&dev->spinlock, spin_flags);
854 cfc_write_array_to_buffer(s,
855 devpriv->dma_buffer[buffer_index], num_bytes);
857 cfc_handle_events(dev, s);
860 static void das16_timer_interrupt(unsigned long arg)
862 struct comedi_device *dev = (struct comedi_device *)arg;
863 struct das16_private_struct *devpriv = dev->private;
865 das16_interrupt(dev);
867 if (devpriv->timer_running)
868 mod_timer(&devpriv->timer, jiffies + timer_period());
871 static int das16_probe(struct comedi_device *dev, struct comedi_devconfig *it)
873 const struct das16_board *board = comedi_board(dev);
876 /* diobits indicates boards */
877 diobits = inb(dev->iobase + DAS16_DIO) & 0xf0;
879 printk(KERN_INFO " id bits are 0x%02x\n", diobits);
880 if (board->id != diobits) {
881 printk(KERN_INFO " requested board's id bits are 0x%x (ignore)\n",
888 static int das1600_mode_detect(struct comedi_device *dev)
890 struct das16_private_struct *devpriv = dev->private;
893 status = inb(dev->iobase + DAS1600_STATUS_B);
895 if (status & DAS1600_CLK_10MHZ) {
896 devpriv->clockbase = 100;
897 printk(KERN_INFO " 10MHz pacer clock\n");
899 devpriv->clockbase = 1000;
900 printk(KERN_INFO " 1MHz pacer clock\n");
906 static void das16_ai_munge(struct comedi_device *dev,
907 struct comedi_subdevice *s, void *array,
908 unsigned int num_bytes,
909 unsigned int start_chan_index)
911 unsigned int i, num_samples = num_bytes / sizeof(short);
914 for (i = 0; i < num_samples; i++) {
915 data[i] = le16_to_cpu(data[i]);
916 if (s->maxdata == 0x0fff)
918 data[i] &= s->maxdata;
928 * 3 Clock speed (in MHz)
930 static int das16_attach(struct comedi_device *dev, struct comedi_devconfig *it)
932 const struct das16_board *board = comedi_board(dev);
933 struct das16_private_struct *devpriv;
934 struct comedi_subdevice *s;
935 struct comedi_krange *user_ai_range;
936 struct comedi_krange *user_ao_range;
937 unsigned int dma_chan = it->options[2];
941 /* check that clock setting is valid */
942 if (it->options[3]) {
943 if (it->options[3] != 0 &&
944 it->options[3] != 1 && it->options[3] != 10) {
946 ("\n Invalid option. Master clock must be set "
947 "to 1 or 10 (MHz)\n");
952 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
956 if (board->size < 0x400) {
957 ret = comedi_request_region(dev, it->options[0], board->size);
961 ret = comedi_request_region(dev, it->options[0], 0x10);
964 /* Request an additional region for the 8255 */
965 ret = __comedi_request_region(dev, dev->iobase + 0x400,
966 board->size & 0x3ff);
969 devpriv->extra_iobase = dev->iobase + 0x400;
972 /* probe id bits to make sure they are consistent */
973 if (das16_probe(dev, it)) {
974 printk(KERN_ERR " id bits do not match selected board, aborting\n");
978 /* get master clock speed */
979 if (board->size < 0x400) {
981 devpriv->clockbase = 1000 / it->options[3];
983 devpriv->clockbase = 1000; /* 1 MHz default */
985 das1600_mode_detect(dev);
989 if (dma_chan == 1 || dma_chan == 3) {
993 if (request_dma(dma_chan, dev->board_name)) {
994 dev_err(dev->class_dev,
995 "failed to request dma channel %i\n",
999 devpriv->dma_chan = dma_chan;
1001 /* allocate dma buffers */
1002 for (i = 0; i < 2; i++) {
1005 p = pci_alloc_consistent(NULL, DAS16_DMA_SIZE,
1006 &devpriv->dma_buffer_addr[i]);
1009 devpriv->dma_buffer[i] = p;
1012 flags = claim_dma_lock();
1013 disable_dma(devpriv->dma_chan);
1014 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
1015 release_dma_lock(flags);
1017 init_timer(&(devpriv->timer));
1018 devpriv->timer.function = das16_timer_interrupt;
1019 devpriv->timer.data = (unsigned long)dev;
1022 /* get any user-defined input range */
1023 if (board->ai_pg == das16_pg_none &&
1024 (it->options[4] || it->options[5])) {
1025 /* allocate single-range range table */
1026 devpriv->user_ai_range_table =
1027 kmalloc(sizeof(struct comedi_lrange) +
1028 sizeof(struct comedi_krange), GFP_KERNEL);
1029 /* initialize ai range */
1030 devpriv->user_ai_range_table->length = 1;
1031 user_ai_range = devpriv->user_ai_range_table->range;
1032 user_ai_range->min = it->options[4];
1033 user_ai_range->max = it->options[5];
1034 user_ai_range->flags = UNIT_volt;
1036 /* get any user-defined output range */
1037 if (it->options[6] || it->options[7]) {
1038 /* allocate single-range range table */
1039 devpriv->user_ao_range_table =
1040 kmalloc(sizeof(struct comedi_lrange) +
1041 sizeof(struct comedi_krange), GFP_KERNEL);
1042 /* initialize ao range */
1043 devpriv->user_ao_range_table->length = 1;
1044 user_ao_range = devpriv->user_ao_range_table->range;
1045 user_ao_range->min = it->options[6];
1046 user_ao_range->max = it->options[7];
1047 user_ao_range->flags = UNIT_volt;
1050 ret = comedi_alloc_subdevices(dev, 5);
1054 status = inb(dev->iobase + DAS16_STATUS);
1056 /* Analog Input subdevice */
1057 s = &dev->subdevices[0];
1058 s->type = COMEDI_SUBD_AI;
1059 s->subdev_flags = SDF_READABLE;
1060 if (status & DAS16_MUXBIT) {
1061 s->subdev_flags |= SDF_GROUND;
1064 s->subdev_flags |= SDF_DIFF;
1067 s->len_chanlist = s->n_chan;
1068 s->maxdata = board->ai_maxdata;
1069 if (devpriv->user_ai_range_table) { /* user defined ai range */
1070 s->range_table = devpriv->user_ai_range_table;
1071 } else if (status & UNIPOLAR) {
1072 s->range_table = das16_ai_uni_lranges[board->ai_pg];
1074 s->range_table = das16_ai_bip_lranges[board->ai_pg];
1076 s->insn_read = das16_ai_insn_read;
1077 if (devpriv->dma_chan) {
1078 dev->read_subdev = s;
1079 s->subdev_flags |= SDF_CMD_READ;
1080 s->do_cmdtest = das16_cmd_test;
1081 s->do_cmd = das16_cmd_exec;
1082 s->cancel = das16_cancel;
1083 s->munge = das16_ai_munge;
1086 /* Analog Output subdevice */
1087 s = &dev->subdevices[1];
1088 if (board->has_ao) {
1089 s->type = COMEDI_SUBD_AO;
1090 s->subdev_flags = SDF_WRITABLE;
1092 s->maxdata = 0x0fff;
1093 if (devpriv->user_ao_range_table)
1094 s->range_table = devpriv->user_ao_range_table;
1096 s->range_table = &range_unknown;
1098 s->insn_write = das16_ao_insn_write;
1100 s->type = COMEDI_SUBD_UNUSED;
1103 /* Digital Input subdevice */
1104 s = &dev->subdevices[2];
1105 s->type = COMEDI_SUBD_DI;
1106 s->subdev_flags = SDF_READABLE;
1109 s->range_table = &range_digital;
1110 s->insn_bits = das16_di_insn_bits;
1112 /* Digital Output subdevice */
1113 s = &dev->subdevices[3];
1114 s->type = COMEDI_SUBD_DO;
1115 s->subdev_flags = SDF_WRITABLE;
1118 s->range_table = &range_digital;
1119 s->insn_bits = das16_do_insn_bits;
1121 /* initialize digital output lines */
1122 outb(s->state, dev->iobase + DAS16_DIO);
1124 s = &dev->subdevices[4];
1126 if (board->i8255_offset != 0) {
1127 subdev_8255_init(dev, s, NULL, (dev->iobase +
1128 board->i8255_offset));
1130 s->type = COMEDI_SUBD_UNUSED;
1134 /* set the interrupt level */
1135 devpriv->control_state = DAS16_IRQ(dev->irq);
1136 outb(devpriv->control_state, dev->iobase + DAS16_CONTROL);
1138 /* turn on das1600 mode if available */
1139 if (board->size > 0x400) {
1140 outb(DAS1600_ENABLE_VAL, dev->iobase + DAS1600_ENABLE);
1141 outb(0, dev->iobase + DAS1600_CONV);
1142 outb(0, dev->iobase + DAS1600_BURST);
1148 static void das16_detach(struct comedi_device *dev)
1150 const struct das16_board *board = comedi_board(dev);
1151 struct das16_private_struct *devpriv = dev->private;
1156 for (i = 0; i < 2; i++) {
1157 if (devpriv->dma_buffer[i])
1158 pci_free_consistent(NULL, DAS16_DMA_SIZE,
1159 devpriv->dma_buffer[i],
1161 dma_buffer_addr[i]);
1163 if (devpriv->dma_chan)
1164 free_dma(devpriv->dma_chan);
1165 kfree(devpriv->user_ai_range_table);
1166 kfree(devpriv->user_ao_range_table);
1168 if (devpriv->extra_iobase)
1169 release_region(devpriv->extra_iobase, board->size & 0x3ff);
1170 comedi_legacy_detach(dev);
1173 static const struct das16_board das16_boards[] = {
1176 .ai_maxdata = 0x0fff,
1178 .ai_pg = das16_pg_none,
1180 .i8255_offset = 0x10,
1181 .i8254_offset = 0x0c,
1186 .ai_maxdata = 0x0fff,
1188 .ai_pg = das16_pg_none,
1190 .i8255_offset = 0x10,
1191 .i8254_offset = 0x0c,
1196 .ai_maxdata = 0x0fff,
1198 .ai_pg = das16_pg_none,
1200 .i8255_offset = 0x10,
1201 .i8254_offset = 0x0c,
1205 .name = "cio-das16",
1206 .ai_maxdata = 0x0fff,
1208 .ai_pg = das16_pg_none,
1210 .i8255_offset = 0x10,
1211 .i8254_offset = 0x0c,
1215 .name = "cio-das16/f",
1216 .ai_maxdata = 0x0fff,
1218 .ai_pg = das16_pg_none,
1220 .i8255_offset = 0x10,
1221 .i8254_offset = 0x0c,
1225 .name = "cio-das16/jr",
1226 .ai_maxdata = 0x0fff,
1228 .ai_pg = das16_pg_16jr,
1230 .i8254_offset = 0x0c,
1234 .name = "pc104-das16jr",
1235 .ai_maxdata = 0x0fff,
1237 .ai_pg = das16_pg_16jr,
1239 .i8254_offset = 0x0c,
1243 .name = "cio-das16jr/16",
1244 .ai_maxdata = 0xffff,
1246 .ai_pg = das16_pg_16jr_16,
1248 .i8254_offset = 0x0c,
1252 .name = "pc104-das16jr/16",
1253 .ai_maxdata = 0xffff,
1255 .ai_pg = das16_pg_16jr_16,
1257 .i8254_offset = 0x0c,
1262 .ai_maxdata = 0x0fff,
1264 .ai_pg = das16_pg_none,
1265 .i8255_offset = 0x400,
1266 .i8254_offset = 0x0c,
1271 .ai_maxdata = 0x0fff,
1273 .ai_pg = das16_pg_none,
1274 .i8255_offset = 0x400,
1275 .i8254_offset = 0x0c,
1280 .ai_maxdata = 0x0fff,
1282 .ai_pg = das16_pg_1601,
1283 .i8255_offset = 0x0,
1284 .i8254_offset = 0x0c,
1289 .ai_maxdata = 0x0fff,
1291 .ai_pg = das16_pg_1602,
1292 .i8255_offset = 0x0,
1293 .i8254_offset = 0x0c,
1298 .ai_maxdata = 0x0fff,
1300 .ai_pg = das16_pg_1601,
1302 .i8255_offset = 0x400,
1303 .i8254_offset = 0x0c,
1308 .ai_maxdata = 0x0fff,
1310 .ai_pg = das16_pg_1602,
1312 .i8255_offset = 0x400,
1313 .i8254_offset = 0x0c,
1317 .name = "cio-das1401/12",
1318 .ai_maxdata = 0x0fff,
1320 .ai_pg = das16_pg_1601,
1322 .i8254_offset = 0x0c,
1326 .name = "cio-das1402/12",
1327 .ai_maxdata = 0x0fff,
1329 .ai_pg = das16_pg_1602,
1331 .i8254_offset = 0x0c,
1335 .name = "cio-das1402/16",
1336 .ai_maxdata = 0xffff,
1338 .ai_pg = das16_pg_1602,
1340 .i8254_offset = 0x0c,
1344 .name = "cio-das1601/12",
1345 .ai_maxdata = 0x0fff,
1347 .ai_pg = das16_pg_1601,
1349 .i8255_offset = 0x400,
1350 .i8254_offset = 0x0c,
1354 .name = "cio-das1602/12",
1355 .ai_maxdata = 0x0fff,
1357 .ai_pg = das16_pg_1602,
1359 .i8255_offset = 0x400,
1360 .i8254_offset = 0x0c,
1364 .name = "cio-das1602/16",
1365 .ai_maxdata = 0xffff,
1367 .ai_pg = das16_pg_1602,
1369 .i8255_offset = 0x400,
1370 .i8254_offset = 0x0c,
1374 .name = "cio-das16/330",
1375 .ai_maxdata = 0x0fff,
1377 .ai_pg = das16_pg_16jr,
1379 .i8254_offset = 0x0c,
1385 static struct comedi_driver das16_driver = {
1386 .driver_name = "das16",
1387 .module = THIS_MODULE,
1388 .attach = das16_attach,
1389 .detach = das16_detach,
1390 .board_name = &das16_boards[0].name,
1391 .num_names = ARRAY_SIZE(das16_boards),
1392 .offset = sizeof(das16_boards[0]),
1394 module_comedi_driver(das16_driver);
1396 MODULE_AUTHOR("Comedi http://www.comedi.org");
1397 MODULE_DESCRIPTION("Comedi low-level driver");
1398 MODULE_LICENSE("GPL");