3 comedi/drivers/adl_pci9111.c
5 Hardware driver for PCI9111 ADLink cards:
9 Copyright (C) 2002-2005 Emmanuel Pacaud <emmanuel.pacaud@univ-poitiers.fr>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 Description: Adlink PCI-9111HR
29 Author: Emmanuel Pacaud <emmanuel.pacaud@univ-poitiers.fr>
30 Devices: [ADLink] PCI-9111HR (adl_pci9111)
39 - ai_do_cmd mode with the following sources:
42 - scan_begin_src TRIG_FOLLOW TRIG_TIMER TRIG_EXT
43 - convert_src TRIG_TIMER TRIG_EXT
44 - scan_end_src TRIG_COUNT
45 - stop_src TRIG_COUNT TRIG_NONE
47 The scanned channels must be consecutive and start from 0. They must
48 all have the same range and aref.
50 Configuration options: not applicable, uses PCI auto config
56 2005/02/17 Extend AI streaming capabilities. Now, scan_begin_arg can be
57 a multiple of chanlist_len*convert_arg.
58 2002/02/19 Fixed the two's complement conversion in pci9111_(hr_)ai_get_data.
59 2002/02/18 Added external trigger support for analog input.
63 - Really test implemented functionality.
64 - Add support for the PCI-9111DG with a probe routine to identify
65 the card type (perhaps with the help of the channel number readback
66 of the A/D Data register).
67 - Add external multiplexer support.
71 #include "../comedidev.h"
73 #include <linux/delay.h>
74 #include <linux/interrupt.h>
77 #include "comedi_fc.h"
79 #define PCI9111_DRIVER_NAME "adl_pci9111"
80 #define PCI9111_HR_DEVICE_ID 0x9111
82 #define PCI9111_FIFO_HALF_SIZE 512
84 #define PCI9111_AI_ACQUISITION_PERIOD_MIN_NS 10000
86 #define PCI9111_RANGE_SETTING_DELAY 10
87 #define PCI9111_AI_INSTANT_READ_UDELAY_US 2
88 #define PCI9111_AI_INSTANT_READ_TIMEOUT 100
90 #define PCI9111_8254_CLOCK_PERIOD_NS 500
93 * IO address map and bit defines
95 #define PCI9111_AI_FIFO_REG 0x00
96 #define PCI9111_AO_REG 0x00
97 #define PCI9111_DIO_REG 0x02
98 #define PCI9111_EDIO_REG 0x04
99 #define PCI9111_AI_CHANNEL_REG 0x06
100 #define PCI9111_AI_RANGE_STAT_REG 0x08
101 #define PCI9111_AI_STAT_AD_BUSY (1 << 7)
102 #define PCI9111_AI_STAT_FF_FF (1 << 6)
103 #define PCI9111_AI_STAT_FF_HF (1 << 5)
104 #define PCI9111_AI_STAT_FF_EF (1 << 4)
105 #define PCI9111_AI_RANGE_MASK (7 << 0)
106 #define PCI9111_AI_TRIG_CTRL_REG 0x0a
107 #define PCI9111_AI_TRIG_CTRL_TRGEVENT (1 << 5)
108 #define PCI9111_AI_TRIG_CTRL_POTRG (1 << 4)
109 #define PCI9111_AI_TRIG_CTRL_PTRG (1 << 3)
110 #define PCI9111_AI_TRIG_CTRL_ETIS (1 << 2)
111 #define PCI9111_AI_TRIG_CTRL_TPST (1 << 1)
112 #define PCI9111_AI_TRIG_CTRL_ASCAN (1 << 0)
113 #define PCI9111_INT_CTRL_REG 0x0c
114 #define PCI9111_INT_CTRL_ISC2 (1 << 3)
115 #define PCI9111_INT_CTRL_FFEN (1 << 2)
116 #define PCI9111_INT_CTRL_ISC1 (1 << 1)
117 #define PCI9111_INT_CTRL_ISC0 (1 << 0)
118 #define PCI9111_SOFT_TRIG_REG 0x0e
119 #define PCI9111_8254_BASE_REG 0x40
120 #define PCI9111_INT_CLR_REG 0x48
122 static const struct comedi_lrange pci9111_ai_range = {
133 /* Private data structure */
135 struct pci9111_private_data {
136 unsigned long lcr_io_base; /* Local configuration register base
142 unsigned int scan_delay;
143 unsigned int chanlist_len;
144 unsigned int chunk_counter;
145 unsigned int chunk_num_samples;
147 int ao_readback; /* Last written analog output data */
152 short ai_bounce_buffer[2 * PCI9111_FIFO_HALF_SIZE];
155 /* ------------------------------------------------------------------ */
156 /* PLX9050 SECTION */
157 /* ------------------------------------------------------------------ */
159 #define PLX9050_REGISTER_INTERRUPT_CONTROL 0x4c
161 #define PLX9050_LINTI1_ENABLE (1 << 0)
162 #define PLX9050_LINTI1_ACTIVE_HIGH (1 << 1)
163 #define PLX9050_LINTI1_STATUS (1 << 2)
164 #define PLX9050_LINTI2_ENABLE (1 << 3)
165 #define PLX9050_LINTI2_ACTIVE_HIGH (1 << 4)
166 #define PLX9050_LINTI2_STATUS (1 << 5)
167 #define PLX9050_PCI_INTERRUPT_ENABLE (1 << 6)
168 #define PLX9050_SOFTWARE_INTERRUPT (1 << 7)
170 static void plx9050_interrupt_control(unsigned long io_base,
172 bool LINTi1_active_high,
174 bool LINTi2_active_high,
175 bool interrupt_enable)
180 flags |= PLX9050_LINTI1_ENABLE;
181 if (LINTi1_active_high)
182 flags |= PLX9050_LINTI1_ACTIVE_HIGH;
184 flags |= PLX9050_LINTI2_ENABLE;
185 if (LINTi2_active_high)
186 flags |= PLX9050_LINTI2_ACTIVE_HIGH;
188 if (interrupt_enable)
189 flags |= PLX9050_PCI_INTERRUPT_ENABLE;
191 outb(flags, io_base + PLX9050_REGISTER_INTERRUPT_CONTROL);
194 /* ------------------------------------------------------------------ */
195 /* MISCELLANEOUS SECTION */
196 /* ------------------------------------------------------------------ */
200 static void pci9111_timer_set(struct comedi_device *dev)
202 struct pci9111_private_data *dev_private = dev->private;
203 unsigned long timer_base = dev->iobase + PCI9111_8254_BASE_REG;
205 i8254_set_mode(timer_base, 1, 0, I8254_MODE0 | I8254_BINARY);
206 i8254_set_mode(timer_base, 1, 1, I8254_MODE2 | I8254_BINARY);
207 i8254_set_mode(timer_base, 1, 2, I8254_MODE2 | I8254_BINARY);
211 i8254_write(timer_base, 1, 2, dev_private->div2);
212 i8254_write(timer_base, 1, 1, dev_private->div1);
215 enum pci9111_trigger_sources {
221 static void pci9111_trigger_source_set(struct comedi_device *dev,
222 enum pci9111_trigger_sources source)
226 /* Read the current trigger mode control bits */
227 flags = inb(dev->iobase + PCI9111_AI_TRIG_CTRL_REG);
228 /* Mask off the EITS and TPST bits */
236 flags |= PCI9111_AI_TRIG_CTRL_TPST;
240 flags |= PCI9111_AI_TRIG_CTRL_ETIS;
244 outb(flags, dev->iobase + PCI9111_AI_TRIG_CTRL_REG);
247 static void pci9111_pretrigger_set(struct comedi_device *dev, bool pretrigger)
251 /* Read the current trigger mode control bits */
252 flags = inb(dev->iobase + PCI9111_AI_TRIG_CTRL_REG);
253 /* Mask off the PTRG bit */
257 flags |= PCI9111_AI_TRIG_CTRL_PTRG;
259 outb(flags, dev->iobase + PCI9111_AI_TRIG_CTRL_REG);
262 static void pci9111_autoscan_set(struct comedi_device *dev, bool autoscan)
266 /* Read the current trigger mode control bits */
267 flags = inb(dev->iobase + PCI9111_AI_TRIG_CTRL_REG);
268 /* Mask off the ASCAN bit */
272 flags |= PCI9111_AI_TRIG_CTRL_ASCAN;
274 outb(flags, dev->iobase + PCI9111_AI_TRIG_CTRL_REG);
277 enum pci9111_ISC0_sources {
279 irq_on_fifo_half_full
282 enum pci9111_ISC1_sources {
284 irq_on_external_trigger
287 static void pci9111_interrupt_source_set(struct comedi_device *dev,
288 enum pci9111_ISC0_sources irq_0_source,
289 enum pci9111_ISC1_sources irq_1_source)
293 /* Read the current interrupt control bits */
294 flags = inb(dev->iobase + PCI9111_AI_TRIG_CTRL_REG);
295 /* Shift the bits so they are compatible with the write register */
297 /* Mask off the ISCx bits */
300 /* Now set the new ISCx bits */
301 if (irq_0_source == irq_on_fifo_half_full)
302 flags |= PCI9111_INT_CTRL_ISC0;
304 if (irq_1_source == irq_on_external_trigger)
305 flags |= PCI9111_INT_CTRL_ISC1;
307 outb(flags, dev->iobase + PCI9111_INT_CTRL_REG);
310 static void pci9111_fifo_reset(struct comedi_device *dev)
312 unsigned long int_ctrl_reg = dev->iobase + PCI9111_INT_CTRL_REG;
314 /* To reset the FIFO, set FFEN sequence as 0 -> 1 -> 0 */
315 outb(0, int_ctrl_reg);
316 outb(PCI9111_INT_CTRL_FFEN, int_ctrl_reg);
317 outb(0, int_ctrl_reg);
320 /* ------------------------------------------------------------------ */
321 /* HARDWARE TRIGGERED ANALOG INPUT SECTION */
322 /* ------------------------------------------------------------------ */
324 /* Cancel analog input autoscan */
326 static int pci9111_ai_cancel(struct comedi_device *dev,
327 struct comedi_subdevice *s)
329 struct pci9111_private_data *dev_private = dev->private;
331 /* Disable interrupts */
333 plx9050_interrupt_control(dev_private->lcr_io_base, true, true, true,
336 pci9111_trigger_source_set(dev, software);
338 pci9111_autoscan_set(dev, false);
340 pci9111_fifo_reset(dev);
345 static int pci9111_ai_do_cmd_test(struct comedi_device *dev,
346 struct comedi_subdevice *s,
347 struct comedi_cmd *cmd)
349 struct pci9111_private_data *dev_private = dev->private;
352 int range, reference;
355 /* Step 1 : check if trigger are trivialy valid */
357 error |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
358 error |= cfc_check_trigger_src(&cmd->scan_begin_src,
359 TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT);
360 error |= cfc_check_trigger_src(&cmd->convert_src,
361 TRIG_TIMER | TRIG_EXT);
362 error |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
363 error |= cfc_check_trigger_src(&cmd->stop_src,
364 TRIG_COUNT | TRIG_NONE);
369 /* Step 2a : make sure trigger sources are unique */
371 error |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
372 error |= cfc_check_trigger_is_unique(cmd->convert_src);
373 error |= cfc_check_trigger_is_unique(cmd->stop_src);
375 /* Step 2b : and mutually compatible */
377 if ((cmd->convert_src == TRIG_TIMER) &&
378 !((cmd->scan_begin_src == TRIG_TIMER) ||
379 (cmd->scan_begin_src == TRIG_FOLLOW)))
381 if ((cmd->convert_src == TRIG_EXT) &&
382 !((cmd->scan_begin_src == TRIG_EXT) ||
383 (cmd->scan_begin_src == TRIG_FOLLOW)))
389 /* Step 3 : make sure arguments are trivialy compatible */
391 if ((cmd->start_src == TRIG_NOW) && (cmd->start_arg != 0)) {
396 if ((cmd->convert_src == TRIG_TIMER) &&
397 (cmd->convert_arg < PCI9111_AI_ACQUISITION_PERIOD_MIN_NS)) {
398 cmd->convert_arg = PCI9111_AI_ACQUISITION_PERIOD_MIN_NS;
401 if ((cmd->convert_src == TRIG_EXT) && (cmd->convert_arg != 0)) {
402 cmd->convert_arg = 0;
406 if ((cmd->scan_begin_src == TRIG_TIMER) &&
407 (cmd->scan_begin_arg < PCI9111_AI_ACQUISITION_PERIOD_MIN_NS)) {
408 cmd->scan_begin_arg = PCI9111_AI_ACQUISITION_PERIOD_MIN_NS;
411 if ((cmd->scan_begin_src == TRIG_FOLLOW)
412 && (cmd->scan_begin_arg != 0)) {
413 cmd->scan_begin_arg = 0;
416 if ((cmd->scan_begin_src == TRIG_EXT) && (cmd->scan_begin_arg != 0)) {
417 cmd->scan_begin_arg = 0;
421 if ((cmd->scan_end_src == TRIG_COUNT) &&
422 (cmd->scan_end_arg != cmd->chanlist_len)) {
423 cmd->scan_end_arg = cmd->chanlist_len;
427 if ((cmd->stop_src == TRIG_COUNT) && (cmd->stop_arg < 1)) {
431 if ((cmd->stop_src == TRIG_NONE) && (cmd->stop_arg != 0)) {
439 /* Step 4 : fix up any arguments */
441 if (cmd->convert_src == TRIG_TIMER) {
442 tmp = cmd->convert_arg;
443 i8253_cascade_ns_to_timer_2div(PCI9111_8254_CLOCK_PERIOD_NS,
447 cmd->flags & TRIG_ROUND_MASK);
448 if (tmp != cmd->convert_arg)
451 /* There's only one timer on this card, so the scan_begin timer must */
452 /* be a multiple of chanlist_len*convert_arg */
454 if (cmd->scan_begin_src == TRIG_TIMER) {
456 unsigned int scan_begin_min;
457 unsigned int scan_begin_arg;
458 unsigned int scan_factor;
460 scan_begin_min = cmd->chanlist_len * cmd->convert_arg;
462 if (cmd->scan_begin_arg != scan_begin_min) {
463 if (scan_begin_min < cmd->scan_begin_arg) {
465 cmd->scan_begin_arg / scan_begin_min;
466 scan_begin_arg = scan_factor * scan_begin_min;
467 if (cmd->scan_begin_arg != scan_begin_arg) {
468 cmd->scan_begin_arg = scan_begin_arg;
472 cmd->scan_begin_arg = scan_begin_min;
481 /* Step 5 : check channel list */
485 range = CR_RANGE(cmd->chanlist[0]);
486 reference = CR_AREF(cmd->chanlist[0]);
488 if (cmd->chanlist_len > 1) {
489 for (i = 0; i < cmd->chanlist_len; i++) {
490 if (CR_CHAN(cmd->chanlist[i]) != i) {
492 "entries in chanlist must be consecutive "
493 "channels,counting upwards from 0\n");
496 if (CR_RANGE(cmd->chanlist[i]) != range) {
498 "entries in chanlist must all have the same gain\n");
501 if (CR_AREF(cmd->chanlist[i]) != reference) {
503 "entries in chanlist must all have the same reference\n");
517 /* Analog input command */
519 static int pci9111_ai_do_cmd(struct comedi_device *dev,
520 struct comedi_subdevice *s)
522 struct pci9111_private_data *dev_private = dev->private;
523 struct comedi_cmd *async_cmd = &s->async->cmd;
527 "no irq assigned for PCI9111, cannot do hardware conversion");
530 /* Set channel scan limit */
531 /* PCI9111 allows only scanning from channel 0 to channel n */
532 /* TODO: handle the case of an external multiplexer */
534 if (async_cmd->chanlist_len > 1) {
535 outb(async_cmd->chanlist_len - 1,
536 dev->iobase + PCI9111_AI_CHANNEL_REG);
537 pci9111_autoscan_set(dev, true);
539 outb(CR_CHAN(async_cmd->chanlist[0]),
540 dev->iobase + PCI9111_AI_CHANNEL_REG);
541 pci9111_autoscan_set(dev, false);
545 /* This is the same gain on every channel */
547 outb(CR_RANGE(async_cmd->chanlist[0]) & PCI9111_AI_RANGE_MASK,
548 dev->iobase + PCI9111_AI_RANGE_STAT_REG);
552 switch (async_cmd->stop_src) {
554 dev_private->stop_counter =
555 async_cmd->stop_arg * async_cmd->chanlist_len;
556 dev_private->stop_is_none = 0;
560 dev_private->stop_counter = 0;
561 dev_private->stop_is_none = 1;
565 comedi_error(dev, "Invalid stop trigger");
569 /* Set timer pacer */
571 dev_private->scan_delay = 0;
572 switch (async_cmd->convert_src) {
574 pci9111_trigger_source_set(dev, software);
575 pci9111_timer_set(dev);
576 pci9111_fifo_reset(dev);
577 pci9111_interrupt_source_set(dev, irq_on_fifo_half_full,
579 pci9111_trigger_source_set(dev, timer_pacer);
580 plx9050_interrupt_control(dev_private->lcr_io_base, true, true,
583 if (async_cmd->scan_begin_src == TRIG_TIMER) {
584 dev_private->scan_delay =
585 (async_cmd->scan_begin_arg /
586 (async_cmd->convert_arg *
587 async_cmd->chanlist_len)) - 1;
594 pci9111_trigger_source_set(dev, external);
595 pci9111_fifo_reset(dev);
596 pci9111_interrupt_source_set(dev, irq_on_fifo_half_full,
598 plx9050_interrupt_control(dev_private->lcr_io_base, true, true,
604 comedi_error(dev, "Invalid convert trigger");
608 dev_private->stop_counter *= (1 + dev_private->scan_delay);
609 dev_private->chanlist_len = async_cmd->chanlist_len;
610 dev_private->chunk_counter = 0;
611 dev_private->chunk_num_samples =
612 dev_private->chanlist_len * (1 + dev_private->scan_delay);
617 static void pci9111_ai_munge(struct comedi_device *dev,
618 struct comedi_subdevice *s, void *data,
619 unsigned int num_bytes,
620 unsigned int start_chan_index)
623 unsigned int maxdata = s->maxdata;
624 unsigned int invert = (maxdata + 1) >> 1;
625 unsigned int shift = (maxdata == 0xffff) ? 0 : 4;
626 unsigned int num_samples = num_bytes / sizeof(short);
629 for (i = 0; i < num_samples; i++)
630 array[i] = ((array[i] >> shift) & maxdata) ^ invert;
633 /* ------------------------------------------------------------------ */
634 /* INTERRUPT SECTION */
635 /* ------------------------------------------------------------------ */
637 static irqreturn_t pci9111_interrupt(int irq, void *p_device)
639 struct comedi_device *dev = p_device;
640 struct pci9111_private_data *dev_private = dev->private;
641 struct comedi_subdevice *s = dev->read_subdev;
642 struct comedi_async *async;
644 unsigned long irq_flags;
645 unsigned char intcsr;
647 if (!dev->attached) {
648 /* Ignore interrupt before device fully attached. */
649 /* Might not even have allocated subdevices yet! */
655 spin_lock_irqsave(&dev->spinlock, irq_flags);
657 /* Check if we are source of interrupt */
658 intcsr = inb(dev_private->lcr_io_base +
659 PLX9050_REGISTER_INTERRUPT_CONTROL);
660 if (!(((intcsr & PLX9050_PCI_INTERRUPT_ENABLE) != 0)
661 && (((intcsr & (PLX9050_LINTI1_ENABLE | PLX9050_LINTI1_STATUS))
662 == (PLX9050_LINTI1_ENABLE | PLX9050_LINTI1_STATUS))
663 || ((intcsr & (PLX9050_LINTI2_ENABLE | PLX9050_LINTI2_STATUS))
664 == (PLX9050_LINTI2_ENABLE | PLX9050_LINTI2_STATUS))))) {
665 /* Not the source of the interrupt. */
666 /* (N.B. not using PLX9050_SOFTWARE_INTERRUPT) */
667 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
671 if ((intcsr & (PLX9050_LINTI1_ENABLE | PLX9050_LINTI1_STATUS)) ==
672 (PLX9050_LINTI1_ENABLE | PLX9050_LINTI1_STATUS)) {
673 /* Interrupt comes from fifo_half-full signal */
675 status = inb(dev->iobase + PCI9111_AI_RANGE_STAT_REG);
677 /* '0' means FIFO is full, data may have been lost */
678 if (!(status & PCI9111_AI_STAT_FF_FF)) {
679 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
680 comedi_error(dev, PCI9111_DRIVER_NAME " fifo overflow");
681 outb(0, dev->iobase + PCI9111_INT_CLR_REG);
682 pci9111_ai_cancel(dev, s);
683 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
684 comedi_event(dev, s);
689 /* '0' means FIFO is half-full */
690 if (!(status & PCI9111_AI_STAT_FF_HF)) {
691 unsigned int num_samples;
692 unsigned int bytes_written = 0;
695 PCI9111_FIFO_HALF_SIZE >
696 dev_private->stop_counter
698 stop_is_none ? dev_private->stop_counter :
699 PCI9111_FIFO_HALF_SIZE;
700 insw(dev->iobase + PCI9111_AI_FIFO_REG,
701 dev_private->ai_bounce_buffer, num_samples);
703 if (dev_private->scan_delay < 1) {
705 cfc_write_array_to_buffer(s,
714 while (position < num_samples) {
715 if (dev_private->chunk_counter <
716 dev_private->chanlist_len) {
718 dev_private->chanlist_len -
719 dev_private->chunk_counter;
722 num_samples - position)
728 cfc_write_array_to_buffer
730 dev_private->ai_bounce_buffer
732 to_read * sizeof(short));
735 dev_private->chunk_num_samples
737 dev_private->chunk_counter;
739 num_samples - position)
745 sizeof(short) * to_read;
749 dev_private->chunk_counter += to_read;
751 if (dev_private->chunk_counter >=
752 dev_private->chunk_num_samples)
753 dev_private->chunk_counter = 0;
757 dev_private->stop_counter -=
758 bytes_written / sizeof(short);
762 if ((dev_private->stop_counter == 0) && (!dev_private->stop_is_none)) {
763 async->events |= COMEDI_CB_EOA;
764 pci9111_ai_cancel(dev, s);
767 outb(0, dev->iobase + PCI9111_INT_CLR_REG);
769 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
771 comedi_event(dev, s);
776 /* ------------------------------------------------------------------ */
777 /* INSTANT ANALOG INPUT OUTPUT SECTION */
778 /* ------------------------------------------------------------------ */
780 /* analog instant input */
782 static int pci9111_ai_insn_read(struct comedi_device *dev,
783 struct comedi_subdevice *s,
784 struct comedi_insn *insn, unsigned int *data)
786 unsigned int chan = CR_CHAN(insn->chanspec);
787 unsigned int range = CR_RANGE(insn->chanspec);
788 unsigned int maxdata = s->maxdata;
789 unsigned int invert = (maxdata + 1) >> 1;
790 unsigned int shift = (maxdata == 0xffff) ? 0 : 4;
795 outb(chan, dev->iobase + PCI9111_AI_CHANNEL_REG);
797 status = inb(dev->iobase + PCI9111_AI_RANGE_STAT_REG);
798 if ((status & PCI9111_AI_RANGE_MASK) != range) {
799 outb(range & PCI9111_AI_RANGE_MASK,
800 dev->iobase + PCI9111_AI_RANGE_STAT_REG);
803 pci9111_fifo_reset(dev);
805 for (i = 0; i < insn->n; i++) {
806 /* Generate a software trigger */
807 outb(0, dev->iobase + PCI9111_SOFT_TRIG_REG);
809 timeout = PCI9111_AI_INSTANT_READ_TIMEOUT;
812 status = inb(dev->iobase + PCI9111_AI_RANGE_STAT_REG);
813 /* '1' means FIFO is not empty */
814 if (status & PCI9111_AI_STAT_FF_EF)
815 goto conversion_done;
818 comedi_error(dev, "A/D read timeout");
820 pci9111_fifo_reset(dev);
825 data[i] = inw(dev->iobase + PCI9111_AI_FIFO_REG);
826 data[i] = ((data[i] >> shift) & maxdata) ^ invert;
832 static int pci9111_ao_insn_write(struct comedi_device *dev,
833 struct comedi_subdevice *s,
834 struct comedi_insn *insn,
837 struct pci9111_private_data *dev_private = dev->private;
838 unsigned int val = 0;
841 for (i = 0; i < insn->n; i++) {
843 outw(val, dev->iobase + PCI9111_AO_REG);
845 dev_private->ao_readback = val;
850 static int pci9111_ao_insn_read(struct comedi_device *dev,
851 struct comedi_subdevice *s,
852 struct comedi_insn *insn,
855 struct pci9111_private_data *dev_private = dev->private;
858 for (i = 0; i < insn->n; i++)
859 data[i] = dev_private->ao_readback;
864 static int pci9111_di_insn_bits(struct comedi_device *dev,
865 struct comedi_subdevice *s,
866 struct comedi_insn *insn,
869 data[1] = inw(dev->iobase + PCI9111_DIO_REG);
874 static int pci9111_do_insn_bits(struct comedi_device *dev,
875 struct comedi_subdevice *s,
876 struct comedi_insn *insn,
879 unsigned int mask = data[0];
880 unsigned int bits = data[1];
884 s->state |= (bits & mask);
886 outw(s->state, dev->iobase + PCI9111_DIO_REG);
894 /* ------------------------------------------------------------------ */
895 /* INITIALISATION SECTION */
896 /* ------------------------------------------------------------------ */
900 static int pci9111_reset(struct comedi_device *dev)
902 struct pci9111_private_data *dev_private = dev->private;
904 /* Set trigger source to software */
906 plx9050_interrupt_control(dev_private->lcr_io_base, true, true, true,
909 pci9111_trigger_source_set(dev, software);
910 pci9111_pretrigger_set(dev, false);
911 pci9111_autoscan_set(dev, false);
913 /* Reset 8254 chip */
914 dev_private->div1 = 0;
915 dev_private->div2 = 0;
916 pci9111_timer_set(dev);
921 static int pci9111_attach_pci(struct comedi_device *dev,
922 struct pci_dev *pcidev)
924 struct pci9111_private_data *dev_private;
925 struct comedi_subdevice *s;
928 comedi_set_hw_dev(dev, &pcidev->dev);
929 dev->board_name = dev->driver->driver_name;
931 ret = alloc_private(dev, sizeof(*dev_private));
934 dev_private = dev->private;
936 ret = comedi_pci_enable(pcidev, dev->board_name);
939 dev_private->lcr_io_base = pci_resource_start(pcidev, 1);
940 dev->iobase = pci_resource_start(pcidev, 2);
944 if (pcidev->irq > 0) {
945 ret = request_irq(dev->irq, pci9111_interrupt,
946 IRQF_SHARED, dev->board_name, dev);
949 dev->irq = pcidev->irq;
952 ret = comedi_alloc_subdevices(dev, 4);
956 s = &dev->subdevices[0];
957 dev->read_subdev = s;
958 s->type = COMEDI_SUBD_AI;
959 s->subdev_flags = SDF_READABLE | SDF_COMMON | SDF_CMD_READ;
962 s->len_chanlist = 16;
963 s->range_table = &pci9111_ai_range;
964 s->cancel = pci9111_ai_cancel;
965 s->insn_read = pci9111_ai_insn_read;
966 s->do_cmdtest = pci9111_ai_do_cmd_test;
967 s->do_cmd = pci9111_ai_do_cmd;
968 s->munge = pci9111_ai_munge;
970 s = &dev->subdevices[1];
971 s->type = COMEDI_SUBD_AO;
972 s->subdev_flags = SDF_WRITABLE | SDF_COMMON;
976 s->range_table = &range_bipolar10;
977 s->insn_write = pci9111_ao_insn_write;
978 s->insn_read = pci9111_ao_insn_read;
980 s = &dev->subdevices[2];
981 s->type = COMEDI_SUBD_DI;
982 s->subdev_flags = SDF_READABLE;
985 s->range_table = &range_digital;
986 s->insn_bits = pci9111_di_insn_bits;
988 s = &dev->subdevices[3];
989 s->type = COMEDI_SUBD_DO;
990 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
993 s->range_table = &range_digital;
994 s->insn_bits = pci9111_do_insn_bits;
996 dev_info(dev->class_dev, "%s attached\n", dev->board_name);
1001 static void pci9111_detach(struct comedi_device *dev)
1003 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1008 free_irq(dev->irq, dev);
1011 comedi_pci_disable(pcidev);
1012 pci_dev_put(pcidev);
1016 static struct comedi_driver adl_pci9111_driver = {
1017 .driver_name = "adl_pci9111",
1018 .module = THIS_MODULE,
1019 .attach_pci = pci9111_attach_pci,
1020 .detach = pci9111_detach,
1023 static int __devinit pci9111_pci_probe(struct pci_dev *dev,
1024 const struct pci_device_id *ent)
1026 return comedi_pci_auto_config(dev, &adl_pci9111_driver);
1029 static void __devexit pci9111_pci_remove(struct pci_dev *dev)
1031 comedi_pci_auto_unconfig(dev);
1034 static DEFINE_PCI_DEVICE_TABLE(pci9111_pci_table) = {
1035 { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI9111_HR_DEVICE_ID) },
1036 /* { PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI9111_HG_DEVICE_ID) }, */
1039 MODULE_DEVICE_TABLE(pci, pci9111_pci_table);
1041 static struct pci_driver adl_pci9111_pci_driver = {
1042 .name = "adl_pci9111",
1043 .id_table = pci9111_pci_table,
1044 .probe = pci9111_pci_probe,
1045 .remove = __devexit_p(pci9111_pci_remove),
1047 module_comedi_pci_driver(adl_pci9111_driver, adl_pci9111_pci_driver);
1049 MODULE_AUTHOR("Comedi http://www.comedi.org");
1050 MODULE_DESCRIPTION("Comedi low-level driver");
1051 MODULE_LICENSE("GPL");