2 comedi/drivers/ni_labpc.c
3 Driver for National Instruments Lab-PC series boards and compatibles
4 Copyright (C) 2001, 2002, 2003 Frank Mori Hess <fmhess@users.sourceforge.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 ************************************************************************
24 Description: National Instruments Lab-PC (& compatibles)
25 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
26 Devices: [National Instruments] Lab-PC-1200 (labpc-1200),
27 Lab-PC-1200AI (labpc-1200ai), Lab-PC+ (lab-pc+), PCI-1200 (ni_labpc)
30 Tested with lab-pc-1200. For the older Lab-PC+, not all input ranges
31 and analog references will work, the available ranges/arefs will
32 depend on how you have configured the jumpers on your board
33 (see your owner's manual).
35 Kernel-level ISA plug-and-play support for the lab-pc-1200
37 yet been added to the driver, mainly due to the fact that
38 I don't know the device id numbers. If you have one
40 please file a bug report at http://comedi.org/
41 so I can get the necessary information from you.
43 The 1200 series boards have onboard calibration dacs for correcting
44 analog input/output offsets and gains. The proper settings for these
45 caldacs are stored on the board's eeprom. To read the caldac values
46 from the eeprom and store them into a file that can be then be used by
47 comedilib, use the comedi_calibrate program.
49 Configuration options - ISA boards:
50 [0] - I/O port base address
51 [1] - IRQ (optional, required for timed or externally triggered conversions)
52 [2] - DMA channel (optional)
54 Configuration options - PCI boards:
58 The Lab-pc+ has quirky chanlist requirements
59 when scanning multiple channels. Multiple channel scan
60 sequence must start at highest channel, then decrement down to
61 channel 0. The rest of the cards can scan down like lab-pc+ or scan
62 up from channel zero. Chanlists consisting of all one channel
63 are also legal, and allow you to pace conversions in bursts.
70 341309a (labpc-1200 register manual)
76 #include <linux/interrupt.h>
77 #include <linux/slab.h>
79 #include "../comedidev.h"
81 #include <linux/delay.h>
87 #include "comedi_fc.h"
90 #define DRV_NAME "ni_labpc"
92 /* size of io region used by board */
94 /* 2 MHz master clock */
95 #define LABPC_TIMER_BASE 500
97 /* Registers for the lab-pc+ */
99 /* write-only registers */
100 #define COMMAND1_REG 0x0
101 #define ADC_GAIN_MASK (0x7 << 4)
102 #define ADC_CHAN_BITS(x) ((x) & 0x7)
103 /* enables multi channel scans */
104 #define ADC_SCAN_EN_BIT 0x80
105 #define COMMAND2_REG 0x1
106 /* enable pretriggering (used in conjunction with SWTRIG) */
107 #define PRETRIG_BIT 0x1
108 /* enable paced conversions on external trigger */
109 #define HWTRIG_BIT 0x2
110 /* enable paced conversions */
111 #define SWTRIG_BIT 0x4
112 /* use two cascaded counters for pacing */
113 #define CASCADE_BIT 0x8
114 #define DAC_PACED_BIT(channel) (0x40 << ((channel) & 0x1))
115 #define COMMAND3_REG 0x2
116 /* enable dma transfers */
117 #define DMA_EN_BIT 0x1
118 /* enable interrupts for 8255 */
119 #define DIO_INTR_EN_BIT 0x2
120 /* enable dma terminal count interrupt */
121 #define DMATC_INTR_EN_BIT 0x4
122 /* enable timer interrupt */
123 #define TIMER_INTR_EN_BIT 0x8
124 /* enable error interrupt */
125 #define ERR_INTR_EN_BIT 0x10
126 /* enable fifo not empty interrupt */
127 #define ADC_FNE_INTR_EN_BIT 0x20
128 #define ADC_CONVERT_REG 0x3
129 #define DAC_LSB_REG(channel) (0x4 + 2 * ((channel) & 0x1))
130 #define DAC_MSB_REG(channel) (0x5 + 2 * ((channel) & 0x1))
131 #define ADC_CLEAR_REG 0x8
132 #define DMATC_CLEAR_REG 0xa
133 #define TIMER_CLEAR_REG 0xc
134 /* 1200 boards only */
135 #define COMMAND6_REG 0xe
136 /* select ground or common-mode reference */
137 #define ADC_COMMON_BIT 0x1
139 #define ADC_UNIP_BIT 0x2
141 #define DAC_UNIP_BIT(channel) (0x4 << ((channel) & 0x1))
142 /* enable fifo half full interrupt */
143 #define ADC_FHF_INTR_EN_BIT 0x20
144 /* enable interrupt on end of hardware count */
145 #define A1_INTR_EN_BIT 0x40
146 /* scan up from channel zero instead of down to zero */
147 #define ADC_SCAN_UP_BIT 0x80
148 #define COMMAND4_REG 0xf
149 /* enables 'interval' scanning */
150 #define INTERVAL_SCAN_EN_BIT 0x1
151 /* enables external signal on counter b1 output to trigger scan */
152 #define EXT_SCAN_EN_BIT 0x2
153 /* chooses direction (output or input) for EXTCONV* line */
154 #define EXT_CONVERT_OUT_BIT 0x4
155 /* chooses differential inputs for adc (in conjunction with board jumper) */
156 #define ADC_DIFF_BIT 0x8
157 #define EXT_CONVERT_DISABLE_BIT 0x10
158 /* 1200 boards only, calibration stuff */
159 #define COMMAND5_REG 0x1c
160 /* enable eeprom for write */
161 #define EEPROM_WRITE_UNPROTECT_BIT 0x4
162 /* enable dithering */
163 #define DITHER_EN_BIT 0x8
164 /* load calibration dac */
165 #define CALDAC_LOAD_BIT 0x10
166 /* serial clock - rising edge writes, falling edge reads */
167 #define SCLOCK_BIT 0x20
168 /* serial data bit for writing to eeprom or calibration dacs */
169 #define SDATA_BIT 0x40
170 /* enable eeprom for read/write */
171 #define EEPROM_EN_BIT 0x80
172 #define INTERVAL_COUNT_REG 0x1e
173 #define INTERVAL_LOAD_REG 0x1f
174 #define INTERVAL_LOAD_BITS 0x1
176 /* read-only registers */
177 #define STATUS1_REG 0x0
178 /* data is available in fifo */
179 #define DATA_AVAIL_BIT 0x1
180 /* overrun has occurred */
181 #define OVERRUN_BIT 0x2
183 #define OVERFLOW_BIT 0x4
184 /* timer interrupt has occurred */
185 #define TIMER_BIT 0x8
186 /* dma terminal count has occurred */
187 #define DMATC_BIT 0x10
188 /* external trigger has occurred */
189 #define EXT_TRIG_BIT 0x40
190 /* 1200 boards only */
191 #define STATUS2_REG 0x1d
192 /* programmable eeprom serial output */
193 #define EEPROM_OUT_BIT 0x1
194 /* counter A1 terminal count */
195 #define A1_TC_BIT 0x2
196 /* fifo not half full */
198 #define ADC_FIFO_REG 0xa
200 #define DIO_BASE_REG 0x10
201 #define COUNTER_A_BASE_REG 0x14
202 #define COUNTER_A_CONTROL_REG (COUNTER_A_BASE_REG + 0x3)
203 /* check modes put conversion pacer output in harmless state (a0 mode 2) */
204 #define INIT_A0_BITS 0x14
205 /* put hardware conversion counter output in harmless state (a1 mode 0) */
206 #define INIT_A1_BITS 0x70
207 #define COUNTER_B_BASE_REG 0x18
211 MODE_SINGLE_CHAN_INTERVAL,
216 static int labpc_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
217 static irqreturn_t labpc_interrupt(int irq, void *d);
218 static int labpc_drain_fifo(struct comedi_device *dev);
219 #ifdef CONFIG_ISA_DMA_API
220 static void labpc_drain_dma(struct comedi_device *dev);
221 static void handle_isa_dma(struct comedi_device *dev);
223 static void labpc_drain_dregs(struct comedi_device *dev);
224 static int labpc_ai_cmdtest(struct comedi_device *dev,
225 struct comedi_subdevice *s, struct comedi_cmd *cmd);
226 static int labpc_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
227 static int labpc_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
228 struct comedi_insn *insn, unsigned int *data);
229 static int labpc_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
230 struct comedi_insn *insn, unsigned int *data);
231 static int labpc_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
232 struct comedi_insn *insn, unsigned int *data);
233 static int labpc_calib_read_insn(struct comedi_device *dev,
234 struct comedi_subdevice *s,
235 struct comedi_insn *insn, unsigned int *data);
236 static int labpc_calib_write_insn(struct comedi_device *dev,
237 struct comedi_subdevice *s,
238 struct comedi_insn *insn, unsigned int *data);
239 static int labpc_eeprom_read_insn(struct comedi_device *dev,
240 struct comedi_subdevice *s,
241 struct comedi_insn *insn, unsigned int *data);
242 static int labpc_eeprom_write_insn(struct comedi_device *dev,
243 struct comedi_subdevice *s,
244 struct comedi_insn *insn,
246 static void labpc_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd,
247 enum scan_mode scan_mode);
248 #ifdef CONFIG_ISA_DMA_API
249 static unsigned int labpc_suggest_transfer_size(const struct comedi_cmd *cmd);
251 static int labpc_dio_mem_callback(int dir, int port, int data,
253 static void labpc_serial_out(struct comedi_device *dev, unsigned int value,
254 unsigned int num_bits);
255 static unsigned int labpc_serial_in(struct comedi_device *dev);
256 static unsigned int labpc_eeprom_read(struct comedi_device *dev,
257 unsigned int address);
258 static unsigned int labpc_eeprom_read_status(struct comedi_device *dev);
259 static int labpc_eeprom_write(struct comedi_device *dev,
260 unsigned int address,
262 static void write_caldac(struct comedi_device *dev, unsigned int channel,
265 /* analog input ranges */
266 #define NUM_LABPC_PLUS_AI_RANGES 16
267 /* indicates unipolar ranges */
268 static const int labpc_plus_is_unipolar[NUM_LABPC_PLUS_AI_RANGES] = {
287 /* map range index to gain bits */
288 static const int labpc_plus_ai_gain_bits[NUM_LABPC_PLUS_AI_RANGES] = {
307 static const struct comedi_lrange range_labpc_plus_ai = {
308 NUM_LABPC_PLUS_AI_RANGES,
329 #define NUM_LABPC_1200_AI_RANGES 14
330 /* indicates unipolar ranges */
331 const int labpc_1200_is_unipolar[NUM_LABPC_1200_AI_RANGES] = {
347 EXPORT_SYMBOL_GPL(labpc_1200_is_unipolar);
349 /* map range index to gain bits */
350 const int labpc_1200_ai_gain_bits[NUM_LABPC_1200_AI_RANGES] = {
366 EXPORT_SYMBOL_GPL(labpc_1200_ai_gain_bits);
368 const struct comedi_lrange range_labpc_1200_ai = {
369 NUM_LABPC_1200_AI_RANGES,
387 EXPORT_SYMBOL_GPL(range_labpc_1200_ai);
389 /* analog output ranges */
390 #define AO_RANGE_IS_UNIPOLAR 0x1
391 static const struct comedi_lrange range_labpc_ao = {
399 /* functions that do inb/outb and readb/writeb so we can use
400 * function pointers to decide which to use */
401 static inline unsigned int labpc_inb(unsigned long address)
406 static inline void labpc_outb(unsigned int byte, unsigned long address)
411 static inline unsigned int labpc_readb(unsigned long address)
413 return readb((void __iomem *)address);
416 static inline void labpc_writeb(unsigned int byte, unsigned long address)
418 writeb(byte, (void __iomem *)address);
421 static const struct labpc_board_struct labpc_boards[] = {
423 .name = "lab-pc-1200",
425 .bustype = isa_bustype,
426 .register_layout = labpc_1200_layout,
428 .ai_range_table = &range_labpc_1200_ai,
429 .ai_range_code = labpc_1200_ai_gain_bits,
430 .ai_range_is_unipolar = labpc_1200_is_unipolar,
432 .memory_mapped_io = 0,
435 .name = "lab-pc-1200ai",
437 .bustype = isa_bustype,
438 .register_layout = labpc_1200_layout,
440 .ai_range_table = &range_labpc_1200_ai,
441 .ai_range_code = labpc_1200_ai_gain_bits,
442 .ai_range_is_unipolar = labpc_1200_is_unipolar,
444 .memory_mapped_io = 0,
449 .bustype = isa_bustype,
450 .register_layout = labpc_plus_layout,
452 .ai_range_table = &range_labpc_plus_ai,
453 .ai_range_code = labpc_plus_ai_gain_bits,
454 .ai_range_is_unipolar = labpc_plus_is_unipolar,
456 .memory_mapped_io = 0,
458 #ifdef CONFIG_COMEDI_PCI_DRIVERS
463 .bustype = pci_bustype,
464 .register_layout = labpc_1200_layout,
466 .ai_range_table = &range_labpc_1200_ai,
467 .ai_range_code = labpc_1200_ai_gain_bits,
468 .ai_range_is_unipolar = labpc_1200_is_unipolar,
470 .memory_mapped_io = 1,
472 /* dummy entry so pci board works when comedi_config is passed driver name */
475 .bustype = pci_bustype,
481 * Useful for shorthand access to the particular board structure
483 #define thisboard ((struct labpc_board_struct *)dev->board_ptr)
485 /* size in bytes of dma buffer */
486 static const int dma_buffer_size = 0xff00;
487 /* 2 bytes per sample */
488 static const int sample_size = 2;
490 #define devpriv ((struct labpc_private *)dev->private)
492 static inline int labpc_counter_load(struct comedi_device *dev,
493 unsigned long base_address,
494 unsigned int counter_number,
495 unsigned int count, unsigned int mode)
497 if (thisboard->memory_mapped_io)
498 return i8254_mm_load((void __iomem *)base_address, 0,
499 counter_number, count, mode);
501 return i8254_load(base_address, 0, counter_number, count, mode);
504 int labpc_common_attach(struct comedi_device *dev, unsigned long iobase,
505 unsigned int irq, unsigned int dma_chan)
507 struct comedi_subdevice *s;
509 unsigned long isr_flags;
510 #ifdef CONFIG_ISA_DMA_API
511 unsigned long dma_flags;
516 dev_info(dev->class_dev, "ni_labpc: %s\n", thisboard->name);
518 dev_err(dev->class_dev, "io base address is zero!\n");
521 /* request io regions for isa boards */
522 if (thisboard->bustype == isa_bustype) {
523 /* check if io addresses are available */
524 if (!request_region(iobase, LABPC_SIZE, DRV_NAME)) {
525 dev_err(dev->class_dev, "I/O port conflict\n");
529 dev->iobase = iobase;
531 if (thisboard->memory_mapped_io) {
532 devpriv->read_byte = labpc_readb;
533 devpriv->write_byte = labpc_writeb;
535 devpriv->read_byte = labpc_inb;
536 devpriv->write_byte = labpc_outb;
538 /* initialize board's command registers */
539 devpriv->write_byte(devpriv->command1_bits, dev->iobase + COMMAND1_REG);
540 devpriv->write_byte(devpriv->command2_bits, dev->iobase + COMMAND2_REG);
541 devpriv->write_byte(devpriv->command3_bits, dev->iobase + COMMAND3_REG);
542 devpriv->write_byte(devpriv->command4_bits, dev->iobase + COMMAND4_REG);
543 if (thisboard->register_layout == labpc_1200_layout) {
544 devpriv->write_byte(devpriv->command5_bits,
545 dev->iobase + COMMAND5_REG);
546 devpriv->write_byte(devpriv->command6_bits,
547 dev->iobase + COMMAND6_REG);
553 if (thisboard->bustype == pci_bustype
554 || thisboard->bustype == pcmcia_bustype)
555 isr_flags |= IRQF_SHARED;
556 if (request_irq(irq, labpc_interrupt, isr_flags,
558 dev_err(dev->class_dev, "unable to allocate irq %u\n",
565 #ifdef CONFIG_ISA_DMA_API
566 /* grab dma channel */
568 dev_err(dev->class_dev, "invalid dma channel %u\n", dma_chan);
570 } else if (dma_chan) {
571 /* allocate dma buffer */
572 devpriv->dma_buffer =
573 kmalloc(dma_buffer_size, GFP_KERNEL | GFP_DMA);
574 if (devpriv->dma_buffer == NULL) {
575 dev_err(dev->class_dev,
576 "failed to allocate dma buffer\n");
579 if (request_dma(dma_chan, DRV_NAME)) {
580 dev_err(dev->class_dev,
581 "failed to allocate dma channel %u\n",
585 devpriv->dma_chan = dma_chan;
586 dma_flags = claim_dma_lock();
587 disable_dma(devpriv->dma_chan);
588 set_dma_mode(devpriv->dma_chan, DMA_MODE_READ);
589 release_dma_lock(dma_flags);
593 dev->board_name = thisboard->name;
595 ret = comedi_alloc_subdevices(dev, 5);
599 /* analog input subdevice */
600 s = &dev->subdevices[0];
601 dev->read_subdev = s;
602 s->type = COMEDI_SUBD_AI;
604 SDF_READABLE | SDF_GROUND | SDF_COMMON | SDF_DIFF | SDF_CMD_READ;
607 s->maxdata = (1 << 12) - 1; /* 12 bit resolution */
608 s->range_table = thisboard->ai_range_table;
609 s->do_cmd = labpc_ai_cmd;
610 s->do_cmdtest = labpc_ai_cmdtest;
611 s->insn_read = labpc_ai_rinsn;
612 s->cancel = labpc_cancel;
615 s = &dev->subdevices[1];
616 if (thisboard->has_ao) {
618 * Could provide command support, except it only has a
619 * one sample hardware buffer for analog output and no
622 s->type = COMEDI_SUBD_AO;
623 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
624 s->n_chan = NUM_AO_CHAN;
625 s->maxdata = (1 << 12) - 1; /* 12 bit resolution */
626 s->range_table = &range_labpc_ao;
627 s->insn_read = labpc_ao_rinsn;
628 s->insn_write = labpc_ao_winsn;
629 /* initialize analog outputs to a known value */
630 for (i = 0; i < s->n_chan; i++) {
631 devpriv->ao_value[i] = s->maxdata / 2;
632 lsb = devpriv->ao_value[i] & 0xff;
633 msb = (devpriv->ao_value[i] >> 8) & 0xff;
634 devpriv->write_byte(lsb, dev->iobase + DAC_LSB_REG(i));
635 devpriv->write_byte(msb, dev->iobase + DAC_MSB_REG(i));
638 s->type = COMEDI_SUBD_UNUSED;
642 s = &dev->subdevices[2];
643 /* if board uses io memory we have to give a custom callback
644 * function to the 8255 driver */
645 if (thisboard->memory_mapped_io)
646 subdev_8255_init(dev, s, labpc_dio_mem_callback,
647 (unsigned long)(dev->iobase + DIO_BASE_REG));
649 subdev_8255_init(dev, s, NULL, dev->iobase + DIO_BASE_REG);
651 /* calibration subdevices for boards that have one */
652 s = &dev->subdevices[3];
653 if (thisboard->register_layout == labpc_1200_layout) {
654 s->type = COMEDI_SUBD_CALIB;
655 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
658 s->insn_read = labpc_calib_read_insn;
659 s->insn_write = labpc_calib_write_insn;
661 for (i = 0; i < s->n_chan; i++)
662 write_caldac(dev, i, s->maxdata / 2);
664 s->type = COMEDI_SUBD_UNUSED;
667 s = &dev->subdevices[4];
668 if (thisboard->register_layout == labpc_1200_layout) {
669 s->type = COMEDI_SUBD_MEMORY;
670 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
671 s->n_chan = EEPROM_SIZE;
673 s->insn_read = labpc_eeprom_read_insn;
674 s->insn_write = labpc_eeprom_write_insn;
676 for (i = 0; i < EEPROM_SIZE; i++)
677 devpriv->eeprom_data[i] = labpc_eeprom_read(dev, i);
679 s->type = COMEDI_SUBD_UNUSED;
683 EXPORT_SYMBOL_GPL(labpc_common_attach);
685 static const struct labpc_board_struct *
686 labpc_pci_find_boardinfo(struct pci_dev *pcidev)
688 unsigned int device_id = pcidev->device;
691 for (n = 0; n < ARRAY_SIZE(labpc_boards); n++) {
692 const struct labpc_board_struct *board = &labpc_boards[n];
693 if (board->bustype == pci_bustype &&
694 board->device_id == device_id)
700 static int __devinit labpc_attach_pci(struct comedi_device *dev,
701 struct pci_dev *pcidev)
703 unsigned long iobase;
707 if (!IS_ENABLED(CONFIG_COMEDI_PCI_DRIVERS))
709 ret = alloc_private(dev, sizeof(struct labpc_private));
712 dev->board_ptr = labpc_pci_find_boardinfo(pcidev);
715 devpriv->mite = mite_alloc(pcidev);
718 ret = mite_setup(devpriv->mite);
721 iobase = (unsigned long)devpriv->mite->daq_io_addr;
722 irq = mite_irq(devpriv->mite);
723 return labpc_common_attach(dev, iobase, irq, 0);
726 static int labpc_attach(struct comedi_device *dev, struct comedi_devconfig *it)
728 unsigned long iobase = 0;
729 unsigned int irq = 0;
730 unsigned int dma_chan = 0;
732 /* allocate and initialize dev->private */
733 if (alloc_private(dev, sizeof(struct labpc_private)) < 0)
736 /* get base address, irq etc. based on bustype */
737 switch (thisboard->bustype) {
739 #ifdef CONFIG_ISA_DMA_API
740 iobase = it->options[0];
741 irq = it->options[1];
742 dma_chan = it->options[2];
744 dev_err(dev->class_dev,
745 "ni_labpc driver has not been built with ISA DMA support.\n");
750 #ifdef CONFIG_COMEDI_PCI_DRIVERS
751 dev_err(dev->class_dev,
752 "manual configuration of PCI board '%s' is not supported\n",
756 dev_err(dev->class_dev,
757 "ni_labpc driver has not been built with PCI support.\n");
762 dev_err(dev->class_dev,
763 "ni_labpc: bug! couldn't determine board type\n");
768 return labpc_common_attach(dev, iobase, irq, dma_chan);
771 void labpc_common_detach(struct comedi_device *dev)
773 struct comedi_subdevice *s;
777 if (dev->subdevices) {
778 s = &dev->subdevices[2];
779 subdev_8255_cleanup(dev, s);
781 #ifdef CONFIG_ISA_DMA_API
782 /* only free stuff if it has been allocated by _attach */
783 kfree(devpriv->dma_buffer);
784 if (devpriv->dma_chan)
785 free_dma(devpriv->dma_chan);
788 free_irq(dev->irq, dev);
789 if (thisboard->bustype == isa_bustype && dev->iobase)
790 release_region(dev->iobase, LABPC_SIZE);
791 #ifdef CONFIG_COMEDI_PCI_DRIVERS
793 mite_unsetup(devpriv->mite);
794 mite_free(devpriv->mite);
798 EXPORT_SYMBOL_GPL(labpc_common_detach);
800 static void labpc_clear_adc_fifo(const struct comedi_device *dev)
802 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
803 devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
804 devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
807 static int labpc_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
811 spin_lock_irqsave(&dev->spinlock, flags);
812 devpriv->command2_bits &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
813 devpriv->write_byte(devpriv->command2_bits, dev->iobase + COMMAND2_REG);
814 spin_unlock_irqrestore(&dev->spinlock, flags);
816 devpriv->command3_bits = 0;
817 devpriv->write_byte(devpriv->command3_bits, dev->iobase + COMMAND3_REG);
822 static enum scan_mode labpc_ai_scan_mode(const struct comedi_cmd *cmd)
824 if (cmd->chanlist_len == 1)
825 return MODE_SINGLE_CHAN;
827 /* chanlist may be NULL during cmdtest. */
828 if (cmd->chanlist == NULL)
829 return MODE_MULT_CHAN_UP;
831 if (CR_CHAN(cmd->chanlist[0]) == CR_CHAN(cmd->chanlist[1]))
832 return MODE_SINGLE_CHAN_INTERVAL;
834 if (CR_CHAN(cmd->chanlist[0]) < CR_CHAN(cmd->chanlist[1]))
835 return MODE_MULT_CHAN_UP;
837 if (CR_CHAN(cmd->chanlist[0]) > CR_CHAN(cmd->chanlist[1]))
838 return MODE_MULT_CHAN_DOWN;
840 pr_err("ni_labpc: bug! cannot determine AI scan mode\n");
844 static int labpc_ai_chanlist_invalid(const struct comedi_device *dev,
845 const struct comedi_cmd *cmd,
848 int channel, range, aref, i;
850 if (cmd->chanlist == NULL)
853 if (mode == MODE_SINGLE_CHAN)
856 if (mode == MODE_SINGLE_CHAN_INTERVAL) {
857 if (cmd->chanlist_len > 0xff) {
859 "ni_labpc: chanlist too long for single channel interval mode\n");
864 channel = CR_CHAN(cmd->chanlist[0]);
865 range = CR_RANGE(cmd->chanlist[0]);
866 aref = CR_AREF(cmd->chanlist[0]);
868 for (i = 0; i < cmd->chanlist_len; i++) {
871 case MODE_SINGLE_CHAN_INTERVAL:
872 if (CR_CHAN(cmd->chanlist[i]) != channel) {
874 "channel scanning order specified in chanlist is not supported by hardware.\n");
878 case MODE_MULT_CHAN_UP:
879 if (CR_CHAN(cmd->chanlist[i]) != i) {
881 "channel scanning order specified in chanlist is not supported by hardware.\n");
885 case MODE_MULT_CHAN_DOWN:
886 if (CR_CHAN(cmd->chanlist[i]) !=
887 cmd->chanlist_len - i - 1) {
889 "channel scanning order specified in chanlist is not supported by hardware.\n");
894 dev_err(dev->class_dev,
895 "ni_labpc: bug! in chanlist check\n");
900 if (CR_RANGE(cmd->chanlist[i]) != range) {
902 "entries in chanlist must all have the same range\n");
906 if (CR_AREF(cmd->chanlist[i]) != aref) {
908 "entries in chanlist must all have the same reference\n");
916 static int labpc_use_continuous_mode(const struct comedi_cmd *cmd,
919 if (mode == MODE_SINGLE_CHAN)
922 if (cmd->scan_begin_src == TRIG_FOLLOW)
928 static unsigned int labpc_ai_convert_period(const struct comedi_cmd *cmd,
931 if (cmd->convert_src != TRIG_TIMER)
934 if (mode == MODE_SINGLE_CHAN && cmd->scan_begin_src == TRIG_TIMER)
935 return cmd->scan_begin_arg;
937 return cmd->convert_arg;
940 static void labpc_set_ai_convert_period(struct comedi_cmd *cmd,
941 enum scan_mode mode, unsigned int ns)
943 if (cmd->convert_src != TRIG_TIMER)
946 if (mode == MODE_SINGLE_CHAN &&
947 cmd->scan_begin_src == TRIG_TIMER) {
948 cmd->scan_begin_arg = ns;
949 if (cmd->convert_arg > cmd->scan_begin_arg)
950 cmd->convert_arg = cmd->scan_begin_arg;
952 cmd->convert_arg = ns;
955 static unsigned int labpc_ai_scan_period(const struct comedi_cmd *cmd,
958 if (cmd->scan_begin_src != TRIG_TIMER)
961 if (mode == MODE_SINGLE_CHAN && cmd->convert_src == TRIG_TIMER)
964 return cmd->scan_begin_arg;
967 static void labpc_set_ai_scan_period(struct comedi_cmd *cmd,
968 enum scan_mode mode, unsigned int ns)
970 if (cmd->scan_begin_src != TRIG_TIMER)
973 if (mode == MODE_SINGLE_CHAN && cmd->convert_src == TRIG_TIMER)
976 cmd->scan_begin_arg = ns;
979 static int labpc_ai_cmdtest(struct comedi_device *dev,
980 struct comedi_subdevice *s, struct comedi_cmd *cmd)
984 unsigned int stop_mask;
987 /* Step 1 : check if triggers are trivially valid */
989 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
990 err |= cfc_check_trigger_src(&cmd->scan_begin_src,
991 TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT);
992 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
993 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
995 stop_mask = TRIG_COUNT | TRIG_NONE;
996 if (thisboard->register_layout == labpc_1200_layout)
997 stop_mask |= TRIG_EXT;
998 err |= cfc_check_trigger_src(&cmd->stop_src, stop_mask);
1003 /* Step 2a : make sure trigger sources are unique */
1005 err |= cfc_check_trigger_is_unique(cmd->start_src);
1006 err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
1007 err |= cfc_check_trigger_is_unique(cmd->convert_src);
1008 err |= cfc_check_trigger_is_unique(cmd->stop_src);
1010 /* Step 2b : and mutually compatible */
1012 /* can't have external stop and start triggers at once */
1013 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
1019 /* step 3: make sure arguments are trivially compatible */
1021 if (cmd->start_arg == TRIG_NOW && cmd->start_arg != 0) {
1026 if (!cmd->chanlist_len)
1029 if (cmd->scan_end_arg != cmd->chanlist_len) {
1030 cmd->scan_end_arg = cmd->chanlist_len;
1034 if (cmd->convert_src == TRIG_TIMER) {
1035 if (cmd->convert_arg < thisboard->ai_speed) {
1036 cmd->convert_arg = thisboard->ai_speed;
1040 /* make sure scan timing is not too fast */
1041 if (cmd->scan_begin_src == TRIG_TIMER) {
1042 if (cmd->convert_src == TRIG_TIMER &&
1043 cmd->scan_begin_arg <
1044 cmd->convert_arg * cmd->chanlist_len) {
1045 cmd->scan_begin_arg =
1046 cmd->convert_arg * cmd->chanlist_len;
1049 if (cmd->scan_begin_arg <
1050 thisboard->ai_speed * cmd->chanlist_len) {
1051 cmd->scan_begin_arg =
1052 thisboard->ai_speed * cmd->chanlist_len;
1057 switch (cmd->stop_src) {
1059 if (!cmd->stop_arg) {
1065 if (cmd->stop_arg != 0) {
1071 * TRIG_EXT doesn't care since it doesn't
1072 * trigger off a numbered channel
1081 /* step 4: fix up any arguments */
1083 tmp = cmd->convert_arg;
1084 tmp2 = cmd->scan_begin_arg;
1085 mode = labpc_ai_scan_mode(cmd);
1086 labpc_adc_timing(dev, cmd, mode);
1087 if (tmp != cmd->convert_arg || tmp2 != cmd->scan_begin_arg)
1093 if (labpc_ai_chanlist_invalid(dev, cmd, mode))
1099 static int labpc_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1101 int channel, range, aref;
1102 #ifdef CONFIG_ISA_DMA_API
1103 unsigned long irq_flags;
1106 struct comedi_async *async = s->async;
1107 struct comedi_cmd *cmd = &async->cmd;
1108 enum transfer_type xfer;
1109 enum scan_mode mode;
1110 unsigned long flags;
1113 comedi_error(dev, "no irq assigned, cannot perform command");
1117 range = CR_RANGE(cmd->chanlist[0]);
1118 aref = CR_AREF(cmd->chanlist[0]);
1120 /* make sure board is disabled before setting up acquisition */
1121 spin_lock_irqsave(&dev->spinlock, flags);
1122 devpriv->command2_bits &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
1123 devpriv->write_byte(devpriv->command2_bits, dev->iobase + COMMAND2_REG);
1124 spin_unlock_irqrestore(&dev->spinlock, flags);
1126 devpriv->command3_bits = 0;
1127 devpriv->write_byte(devpriv->command3_bits, dev->iobase + COMMAND3_REG);
1129 /* initialize software conversion count */
1130 if (cmd->stop_src == TRIG_COUNT)
1131 devpriv->count = cmd->stop_arg * cmd->chanlist_len;
1133 /* setup hardware conversion counter */
1134 if (cmd->stop_src == TRIG_EXT) {
1136 * load counter a1 with count of 3
1137 * (pc+ manual says this is minimum allowed) using mode 0
1139 ret = labpc_counter_load(dev, dev->iobase + COUNTER_A_BASE_REG,
1142 comedi_error(dev, "error loading counter a1");
1146 * otherwise, just put a1 in mode 0
1147 * with no count to set its output low
1149 devpriv->write_byte(INIT_A1_BITS,
1150 dev->iobase + COUNTER_A_CONTROL_REG);
1152 #ifdef CONFIG_ISA_DMA_API
1153 /* figure out what method we will use to transfer data */
1154 if (devpriv->dma_chan && /* need a dma channel allocated */
1156 * dma unsafe at RT priority,
1157 * and too much setup time for TRIG_WAKE_EOS for
1159 (cmd->flags & (TRIG_WAKE_EOS | TRIG_RT)) == 0 &&
1160 /* only available on the isa boards */
1161 thisboard->bustype == isa_bustype) {
1162 xfer = isa_dma_transfer;
1163 /* pc-plus has no fifo-half full interrupt */
1166 if (thisboard->register_layout == labpc_1200_layout &&
1167 /* wake-end-of-scan should interrupt on fifo not empty */
1168 (cmd->flags & TRIG_WAKE_EOS) == 0 &&
1169 /* make sure we are taking more than just a few points */
1170 (cmd->stop_src != TRIG_COUNT || devpriv->count > 256)) {
1171 xfer = fifo_half_full_transfer;
1173 xfer = fifo_not_empty_transfer;
1174 devpriv->current_transfer = xfer;
1175 mode = labpc_ai_scan_mode(cmd);
1177 /* setup command6 register for 1200 boards */
1178 if (thisboard->register_layout == labpc_1200_layout) {
1179 /* reference inputs to ground or common? */
1180 if (aref != AREF_GROUND)
1181 devpriv->command6_bits |= ADC_COMMON_BIT;
1183 devpriv->command6_bits &= ~ADC_COMMON_BIT;
1184 /* bipolar or unipolar range? */
1185 if (thisboard->ai_range_is_unipolar[range])
1186 devpriv->command6_bits |= ADC_UNIP_BIT;
1188 devpriv->command6_bits &= ~ADC_UNIP_BIT;
1189 /* interrupt on fifo half full? */
1190 if (xfer == fifo_half_full_transfer)
1191 devpriv->command6_bits |= ADC_FHF_INTR_EN_BIT;
1193 devpriv->command6_bits &= ~ADC_FHF_INTR_EN_BIT;
1194 /* enable interrupt on counter a1 terminal count? */
1195 if (cmd->stop_src == TRIG_EXT)
1196 devpriv->command6_bits |= A1_INTR_EN_BIT;
1198 devpriv->command6_bits &= ~A1_INTR_EN_BIT;
1199 /* are we scanning up or down through channels? */
1200 if (mode == MODE_MULT_CHAN_UP)
1201 devpriv->command6_bits |= ADC_SCAN_UP_BIT;
1203 devpriv->command6_bits &= ~ADC_SCAN_UP_BIT;
1204 /* write to register */
1205 devpriv->write_byte(devpriv->command6_bits,
1206 dev->iobase + COMMAND6_REG);
1209 /* setup channel list, etc (command1 register) */
1210 devpriv->command1_bits = 0;
1211 if (mode == MODE_MULT_CHAN_UP)
1212 channel = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
1214 channel = CR_CHAN(cmd->chanlist[0]);
1215 /* munge channel bits for differential / scan disabled mode */
1216 if (mode != MODE_SINGLE_CHAN && aref == AREF_DIFF)
1218 devpriv->command1_bits |= ADC_CHAN_BITS(channel);
1219 devpriv->command1_bits |= thisboard->ai_range_code[range];
1220 devpriv->write_byte(devpriv->command1_bits, dev->iobase + COMMAND1_REG);
1221 /* manual says to set scan enable bit on second pass */
1222 if (mode == MODE_MULT_CHAN_UP || mode == MODE_MULT_CHAN_DOWN) {
1223 devpriv->command1_bits |= ADC_SCAN_EN_BIT;
1224 /* need a brief delay before enabling scan, or scan
1225 * list will get screwed when you switch
1226 * between scan up to scan down mode - dunno why */
1228 devpriv->write_byte(devpriv->command1_bits,
1229 dev->iobase + COMMAND1_REG);
1231 /* setup any external triggering/pacing (command4 register) */
1232 devpriv->command4_bits = 0;
1233 if (cmd->convert_src != TRIG_EXT)
1234 devpriv->command4_bits |= EXT_CONVERT_DISABLE_BIT;
1235 /* XXX should discard first scan when using interval scanning
1236 * since manual says it is not synced with scan clock */
1237 if (labpc_use_continuous_mode(cmd, mode) == 0) {
1238 devpriv->command4_bits |= INTERVAL_SCAN_EN_BIT;
1239 if (cmd->scan_begin_src == TRIG_EXT)
1240 devpriv->command4_bits |= EXT_SCAN_EN_BIT;
1242 /* single-ended/differential */
1243 if (aref == AREF_DIFF)
1244 devpriv->command4_bits |= ADC_DIFF_BIT;
1245 devpriv->write_byte(devpriv->command4_bits, dev->iobase + COMMAND4_REG);
1247 devpriv->write_byte(cmd->chanlist_len,
1248 dev->iobase + INTERVAL_COUNT_REG);
1250 devpriv->write_byte(INTERVAL_LOAD_BITS,
1251 dev->iobase + INTERVAL_LOAD_REG);
1253 if (cmd->convert_src == TRIG_TIMER || cmd->scan_begin_src == TRIG_TIMER) {
1255 labpc_adc_timing(dev, cmd, mode);
1256 /* load counter b0 in mode 3 */
1257 ret = labpc_counter_load(dev, dev->iobase + COUNTER_B_BASE_REG,
1258 0, devpriv->divisor_b0, 3);
1260 comedi_error(dev, "error loading counter b0");
1264 /* set up conversion pacing */
1265 if (labpc_ai_convert_period(cmd, mode)) {
1266 /* load counter a0 in mode 2 */
1267 ret = labpc_counter_load(dev, dev->iobase + COUNTER_A_BASE_REG,
1268 0, devpriv->divisor_a0, 2);
1270 comedi_error(dev, "error loading counter a0");
1274 devpriv->write_byte(INIT_A0_BITS,
1275 dev->iobase + COUNTER_A_CONTROL_REG);
1277 /* set up scan pacing */
1278 if (labpc_ai_scan_period(cmd, mode)) {
1279 /* load counter b1 in mode 2 */
1280 ret = labpc_counter_load(dev, dev->iobase + COUNTER_B_BASE_REG,
1281 1, devpriv->divisor_b1, 2);
1283 comedi_error(dev, "error loading counter b1");
1288 labpc_clear_adc_fifo(dev);
1290 #ifdef CONFIG_ISA_DMA_API
1291 /* set up dma transfer */
1292 if (xfer == isa_dma_transfer) {
1293 irq_flags = claim_dma_lock();
1294 disable_dma(devpriv->dma_chan);
1295 /* clear flip-flop to make sure 2-byte registers for
1296 * count and address get set correctly */
1297 clear_dma_ff(devpriv->dma_chan);
1298 set_dma_addr(devpriv->dma_chan,
1299 virt_to_bus(devpriv->dma_buffer));
1300 /* set appropriate size of transfer */
1301 devpriv->dma_transfer_size = labpc_suggest_transfer_size(cmd);
1302 if (cmd->stop_src == TRIG_COUNT &&
1303 devpriv->count * sample_size < devpriv->dma_transfer_size) {
1304 devpriv->dma_transfer_size =
1305 devpriv->count * sample_size;
1307 set_dma_count(devpriv->dma_chan, devpriv->dma_transfer_size);
1308 enable_dma(devpriv->dma_chan);
1309 release_dma_lock(irq_flags);
1310 /* enable board's dma */
1311 devpriv->command3_bits |= DMA_EN_BIT | DMATC_INTR_EN_BIT;
1313 devpriv->command3_bits &= ~DMA_EN_BIT & ~DMATC_INTR_EN_BIT;
1316 /* enable error interrupts */
1317 devpriv->command3_bits |= ERR_INTR_EN_BIT;
1318 /* enable fifo not empty interrupt? */
1319 if (xfer == fifo_not_empty_transfer)
1320 devpriv->command3_bits |= ADC_FNE_INTR_EN_BIT;
1322 devpriv->command3_bits &= ~ADC_FNE_INTR_EN_BIT;
1323 devpriv->write_byte(devpriv->command3_bits, dev->iobase + COMMAND3_REG);
1325 /* startup acquisition */
1328 /* use 2 cascaded counters for pacing */
1329 spin_lock_irqsave(&dev->spinlock, flags);
1330 devpriv->command2_bits |= CASCADE_BIT;
1331 switch (cmd->start_src) {
1333 devpriv->command2_bits |= HWTRIG_BIT;
1334 devpriv->command2_bits &= ~PRETRIG_BIT & ~SWTRIG_BIT;
1337 devpriv->command2_bits |= SWTRIG_BIT;
1338 devpriv->command2_bits &= ~PRETRIG_BIT & ~HWTRIG_BIT;
1341 comedi_error(dev, "bug with start_src");
1342 spin_unlock_irqrestore(&dev->spinlock, flags);
1346 switch (cmd->stop_src) {
1348 devpriv->command2_bits |= HWTRIG_BIT | PRETRIG_BIT;
1354 comedi_error(dev, "bug with stop_src");
1355 spin_unlock_irqrestore(&dev->spinlock, flags);
1358 devpriv->write_byte(devpriv->command2_bits, dev->iobase + COMMAND2_REG);
1359 spin_unlock_irqrestore(&dev->spinlock, flags);
1364 /* interrupt service routine */
1365 static irqreturn_t labpc_interrupt(int irq, void *d)
1367 struct comedi_device *dev = d;
1368 struct comedi_subdevice *s = dev->read_subdev;
1369 struct comedi_async *async;
1370 struct comedi_cmd *cmd;
1372 if (dev->attached == 0) {
1373 comedi_error(dev, "premature interrupt");
1381 /* read board status */
1382 devpriv->status1_bits = devpriv->read_byte(dev->iobase + STATUS1_REG);
1383 if (thisboard->register_layout == labpc_1200_layout)
1384 devpriv->status2_bits =
1385 devpriv->read_byte(dev->iobase + STATUS2_REG);
1387 if ((devpriv->status1_bits & (DMATC_BIT | TIMER_BIT | OVERFLOW_BIT |
1388 OVERRUN_BIT | DATA_AVAIL_BIT)) == 0
1389 && (devpriv->status2_bits & A1_TC_BIT) == 0
1390 && (devpriv->status2_bits & FNHF_BIT)) {
1394 if (devpriv->status1_bits & OVERRUN_BIT) {
1395 /* clear error interrupt */
1396 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
1397 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1398 comedi_event(dev, s);
1399 comedi_error(dev, "overrun");
1403 #ifdef CONFIG_ISA_DMA_API
1404 if (devpriv->current_transfer == isa_dma_transfer) {
1406 * if a dma terminal count of external stop trigger
1409 if (devpriv->status1_bits & DMATC_BIT ||
1410 (thisboard->register_layout == labpc_1200_layout
1411 && devpriv->status2_bits & A1_TC_BIT)) {
1412 handle_isa_dma(dev);
1416 labpc_drain_fifo(dev);
1418 if (devpriv->status1_bits & TIMER_BIT) {
1419 comedi_error(dev, "handled timer interrupt?");
1421 devpriv->write_byte(0x1, dev->iobase + TIMER_CLEAR_REG);
1424 if (devpriv->status1_bits & OVERFLOW_BIT) {
1425 /* clear error interrupt */
1426 devpriv->write_byte(0x1, dev->iobase + ADC_CLEAR_REG);
1427 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1428 comedi_event(dev, s);
1429 comedi_error(dev, "overflow");
1432 /* handle external stop trigger */
1433 if (cmd->stop_src == TRIG_EXT) {
1434 if (devpriv->status2_bits & A1_TC_BIT) {
1435 labpc_drain_dregs(dev);
1436 labpc_cancel(dev, s);
1437 async->events |= COMEDI_CB_EOA;
1441 /* TRIG_COUNT end of acquisition */
1442 if (cmd->stop_src == TRIG_COUNT) {
1443 if (devpriv->count == 0) {
1444 labpc_cancel(dev, s);
1445 async->events |= COMEDI_CB_EOA;
1449 comedi_event(dev, s);
1453 /* read all available samples from ai fifo */
1454 static int labpc_drain_fifo(struct comedi_device *dev)
1456 unsigned int lsb, msb;
1458 struct comedi_async *async = dev->read_subdev->async;
1459 const int timeout = 10000;
1462 devpriv->status1_bits = devpriv->read_byte(dev->iobase + STATUS1_REG);
1464 for (i = 0; (devpriv->status1_bits & DATA_AVAIL_BIT) && i < timeout;
1466 /* quit if we have all the data we want */
1467 if (async->cmd.stop_src == TRIG_COUNT) {
1468 if (devpriv->count == 0)
1472 lsb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1473 msb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1474 data = (msb << 8) | lsb;
1475 cfc_write_to_buffer(dev->read_subdev, data);
1476 devpriv->status1_bits =
1477 devpriv->read_byte(dev->iobase + STATUS1_REG);
1480 comedi_error(dev, "ai timeout, fifo never empties");
1481 async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1488 #ifdef CONFIG_ISA_DMA_API
1489 static void labpc_drain_dma(struct comedi_device *dev)
1491 struct comedi_subdevice *s = dev->read_subdev;
1492 struct comedi_async *async = s->async;
1494 unsigned long flags;
1495 unsigned int max_points, num_points, residue, leftover;
1498 status = devpriv->status1_bits;
1500 flags = claim_dma_lock();
1501 disable_dma(devpriv->dma_chan);
1502 /* clear flip-flop to make sure 2-byte registers for
1503 * count and address get set correctly */
1504 clear_dma_ff(devpriv->dma_chan);
1506 /* figure out how many points to read */
1507 max_points = devpriv->dma_transfer_size / sample_size;
1508 /* residue is the number of points left to be done on the dma
1509 * transfer. It should always be zero at this point unless
1510 * the stop_src is set to external triggering.
1512 residue = get_dma_residue(devpriv->dma_chan) / sample_size;
1513 num_points = max_points - residue;
1514 if (devpriv->count < num_points && async->cmd.stop_src == TRIG_COUNT)
1515 num_points = devpriv->count;
1517 /* figure out how many points will be stored next time */
1519 if (async->cmd.stop_src != TRIG_COUNT) {
1520 leftover = devpriv->dma_transfer_size / sample_size;
1521 } else if (devpriv->count > num_points) {
1522 leftover = devpriv->count - num_points;
1523 if (leftover > max_points)
1524 leftover = max_points;
1527 /* write data to comedi buffer */
1528 for (i = 0; i < num_points; i++)
1529 cfc_write_to_buffer(s, devpriv->dma_buffer[i]);
1531 if (async->cmd.stop_src == TRIG_COUNT)
1532 devpriv->count -= num_points;
1534 /* set address and count for next transfer */
1535 set_dma_addr(devpriv->dma_chan, virt_to_bus(devpriv->dma_buffer));
1536 set_dma_count(devpriv->dma_chan, leftover * sample_size);
1537 release_dma_lock(flags);
1539 async->events |= COMEDI_CB_BLOCK;
1542 static void handle_isa_dma(struct comedi_device *dev)
1544 labpc_drain_dma(dev);
1546 enable_dma(devpriv->dma_chan);
1548 /* clear dma tc interrupt */
1549 devpriv->write_byte(0x1, dev->iobase + DMATC_CLEAR_REG);
1553 /* makes sure all data acquired by board is transferred to comedi (used
1554 * when acquisition is terminated by stop_src == TRIG_EXT). */
1555 static void labpc_drain_dregs(struct comedi_device *dev)
1557 #ifdef CONFIG_ISA_DMA_API
1558 if (devpriv->current_transfer == isa_dma_transfer)
1559 labpc_drain_dma(dev);
1562 labpc_drain_fifo(dev);
1565 static int labpc_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
1566 struct comedi_insn *insn, unsigned int *data)
1572 unsigned long flags;
1574 /* disable timed conversions */
1575 spin_lock_irqsave(&dev->spinlock, flags);
1576 devpriv->command2_bits &= ~SWTRIG_BIT & ~HWTRIG_BIT & ~PRETRIG_BIT;
1577 devpriv->write_byte(devpriv->command2_bits, dev->iobase + COMMAND2_REG);
1578 spin_unlock_irqrestore(&dev->spinlock, flags);
1580 /* disable interrupt generation and dma */
1581 devpriv->command3_bits = 0;
1582 devpriv->write_byte(devpriv->command3_bits, dev->iobase + COMMAND3_REG);
1584 /* set gain and channel */
1585 devpriv->command1_bits = 0;
1586 chan = CR_CHAN(insn->chanspec);
1587 range = CR_RANGE(insn->chanspec);
1588 devpriv->command1_bits |= thisboard->ai_range_code[range];
1589 /* munge channel bits for differential/scan disabled mode */
1590 if (CR_AREF(insn->chanspec) == AREF_DIFF)
1592 devpriv->command1_bits |= ADC_CHAN_BITS(chan);
1593 devpriv->write_byte(devpriv->command1_bits, dev->iobase + COMMAND1_REG);
1595 /* setup command6 register for 1200 boards */
1596 if (thisboard->register_layout == labpc_1200_layout) {
1597 /* reference inputs to ground or common? */
1598 if (CR_AREF(insn->chanspec) != AREF_GROUND)
1599 devpriv->command6_bits |= ADC_COMMON_BIT;
1601 devpriv->command6_bits &= ~ADC_COMMON_BIT;
1602 /* bipolar or unipolar range? */
1603 if (thisboard->ai_range_is_unipolar[range])
1604 devpriv->command6_bits |= ADC_UNIP_BIT;
1606 devpriv->command6_bits &= ~ADC_UNIP_BIT;
1607 /* don't interrupt on fifo half full */
1608 devpriv->command6_bits &= ~ADC_FHF_INTR_EN_BIT;
1609 /* don't enable interrupt on counter a1 terminal count? */
1610 devpriv->command6_bits &= ~A1_INTR_EN_BIT;
1611 /* write to register */
1612 devpriv->write_byte(devpriv->command6_bits,
1613 dev->iobase + COMMAND6_REG);
1615 /* setup command4 register */
1616 devpriv->command4_bits = 0;
1617 devpriv->command4_bits |= EXT_CONVERT_DISABLE_BIT;
1618 /* single-ended/differential */
1619 if (CR_AREF(insn->chanspec) == AREF_DIFF)
1620 devpriv->command4_bits |= ADC_DIFF_BIT;
1621 devpriv->write_byte(devpriv->command4_bits, dev->iobase + COMMAND4_REG);
1624 * initialize pacer counter output to make sure it doesn't
1625 * cause any problems
1627 devpriv->write_byte(INIT_A0_BITS, dev->iobase + COUNTER_A_CONTROL_REG);
1629 labpc_clear_adc_fifo(dev);
1631 for (n = 0; n < insn->n; n++) {
1632 /* trigger conversion */
1633 devpriv->write_byte(0x1, dev->iobase + ADC_CONVERT_REG);
1635 for (i = 0; i < timeout; i++) {
1636 if (devpriv->read_byte(dev->iobase +
1637 STATUS1_REG) & DATA_AVAIL_BIT)
1642 comedi_error(dev, "timeout");
1645 lsb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1646 msb = devpriv->read_byte(dev->iobase + ADC_FIFO_REG);
1647 data[n] = (msb << 8) | lsb;
1653 /* analog output insn */
1654 static int labpc_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
1655 struct comedi_insn *insn, unsigned int *data)
1658 unsigned long flags;
1661 channel = CR_CHAN(insn->chanspec);
1663 /* turn off pacing of analog output channel */
1664 /* note: hardware bug in daqcard-1200 means pacing cannot
1665 * be independently enabled/disabled for its the two channels */
1666 spin_lock_irqsave(&dev->spinlock, flags);
1667 devpriv->command2_bits &= ~DAC_PACED_BIT(channel);
1668 devpriv->write_byte(devpriv->command2_bits, dev->iobase + COMMAND2_REG);
1669 spin_unlock_irqrestore(&dev->spinlock, flags);
1672 if (thisboard->register_layout == labpc_1200_layout) {
1673 range = CR_RANGE(insn->chanspec);
1674 if (range & AO_RANGE_IS_UNIPOLAR)
1675 devpriv->command6_bits |= DAC_UNIP_BIT(channel);
1677 devpriv->command6_bits &= ~DAC_UNIP_BIT(channel);
1678 /* write to register */
1679 devpriv->write_byte(devpriv->command6_bits,
1680 dev->iobase + COMMAND6_REG);
1683 lsb = data[0] & 0xff;
1684 msb = (data[0] >> 8) & 0xff;
1685 devpriv->write_byte(lsb, dev->iobase + DAC_LSB_REG(channel));
1686 devpriv->write_byte(msb, dev->iobase + DAC_MSB_REG(channel));
1688 /* remember value for readback */
1689 devpriv->ao_value[channel] = data[0];
1694 /* analog output readback insn */
1695 static int labpc_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
1696 struct comedi_insn *insn, unsigned int *data)
1698 data[0] = devpriv->ao_value[CR_CHAN(insn->chanspec)];
1703 static int labpc_calib_read_insn(struct comedi_device *dev,
1704 struct comedi_subdevice *s,
1705 struct comedi_insn *insn, unsigned int *data)
1707 data[0] = devpriv->caldac[CR_CHAN(insn->chanspec)];
1712 static int labpc_calib_write_insn(struct comedi_device *dev,
1713 struct comedi_subdevice *s,
1714 struct comedi_insn *insn, unsigned int *data)
1716 int channel = CR_CHAN(insn->chanspec);
1718 write_caldac(dev, channel, data[0]);
1722 static int labpc_eeprom_read_insn(struct comedi_device *dev,
1723 struct comedi_subdevice *s,
1724 struct comedi_insn *insn, unsigned int *data)
1726 data[0] = devpriv->eeprom_data[CR_CHAN(insn->chanspec)];
1731 static int labpc_eeprom_write_insn(struct comedi_device *dev,
1732 struct comedi_subdevice *s,
1733 struct comedi_insn *insn, unsigned int *data)
1735 int channel = CR_CHAN(insn->chanspec);
1738 /* only allow writes to user area of eeprom */
1739 if (channel < 16 || channel > 127) {
1740 dev_dbg(dev->class_dev,
1741 "eeprom writes are only allowed to channels 16 through 127 (the pointer and user areas)\n");
1745 ret = labpc_eeprom_write(dev, channel, data[0]);
1752 #ifdef CONFIG_ISA_DMA_API
1753 /* utility function that suggests a dma transfer size in bytes */
1754 static unsigned int labpc_suggest_transfer_size(const struct comedi_cmd *cmd)
1759 if (cmd->convert_src == TRIG_TIMER)
1760 freq = 1000000000 / cmd->convert_arg;
1761 /* return some default value */
1765 /* make buffer fill in no more than 1/3 second */
1766 size = (freq / 3) * sample_size;
1768 /* set a minimum and maximum size allowed */
1769 if (size > dma_buffer_size)
1770 size = dma_buffer_size - dma_buffer_size % sample_size;
1771 else if (size < sample_size)
1778 /* figures out what counter values to use based on command */
1779 static void labpc_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd,
1780 enum scan_mode mode)
1782 /* max value for 16 bit counter in mode 2 */
1783 const int max_counter_value = 0x10000;
1784 /* min value for 16 bit counter in mode 2 */
1785 const int min_counter_value = 2;
1786 unsigned int base_period;
1787 unsigned int scan_period;
1788 unsigned int convert_period;
1791 * if both convert and scan triggers are TRIG_TIMER, then they
1792 * both rely on counter b0
1794 convert_period = labpc_ai_convert_period(cmd, mode);
1795 scan_period = labpc_ai_scan_period(cmd, mode);
1796 if (convert_period && scan_period) {
1798 * pick the lowest b0 divisor value we can (for maximum input
1799 * clock speed on convert and scan counters)
1801 devpriv->divisor_b0 = (scan_period - 1) /
1802 (LABPC_TIMER_BASE * max_counter_value) + 1;
1803 if (devpriv->divisor_b0 < min_counter_value)
1804 devpriv->divisor_b0 = min_counter_value;
1805 if (devpriv->divisor_b0 > max_counter_value)
1806 devpriv->divisor_b0 = max_counter_value;
1808 base_period = LABPC_TIMER_BASE * devpriv->divisor_b0;
1810 /* set a0 for conversion frequency and b1 for scan frequency */
1811 switch (cmd->flags & TRIG_ROUND_MASK) {
1813 case TRIG_ROUND_NEAREST:
1814 devpriv->divisor_a0 =
1815 (convert_period + (base_period / 2)) / base_period;
1816 devpriv->divisor_b1 =
1817 (scan_period + (base_period / 2)) / base_period;
1820 devpriv->divisor_a0 =
1821 (convert_period + (base_period - 1)) / base_period;
1822 devpriv->divisor_b1 =
1823 (scan_period + (base_period - 1)) / base_period;
1825 case TRIG_ROUND_DOWN:
1826 devpriv->divisor_a0 = convert_period / base_period;
1827 devpriv->divisor_b1 = scan_period / base_period;
1830 /* make sure a0 and b1 values are acceptable */
1831 if (devpriv->divisor_a0 < min_counter_value)
1832 devpriv->divisor_a0 = min_counter_value;
1833 if (devpriv->divisor_a0 > max_counter_value)
1834 devpriv->divisor_a0 = max_counter_value;
1835 if (devpriv->divisor_b1 < min_counter_value)
1836 devpriv->divisor_b1 = min_counter_value;
1837 if (devpriv->divisor_b1 > max_counter_value)
1838 devpriv->divisor_b1 = max_counter_value;
1839 /* write corrected timings to command */
1840 labpc_set_ai_convert_period(cmd, mode,
1841 base_period * devpriv->divisor_a0);
1842 labpc_set_ai_scan_period(cmd, mode,
1843 base_period * devpriv->divisor_b1);
1845 * if only one TRIG_TIMER is used, we can employ the generic
1846 * cascaded timing functions
1848 } else if (scan_period) {
1850 * calculate cascaded counter values
1851 * that give desired scan timing
1853 i8253_cascade_ns_to_timer_2div(LABPC_TIMER_BASE,
1854 &(devpriv->divisor_b1),
1855 &(devpriv->divisor_b0),
1857 cmd->flags & TRIG_ROUND_MASK);
1858 labpc_set_ai_scan_period(cmd, mode, scan_period);
1859 } else if (convert_period) {
1861 * calculate cascaded counter values
1862 * that give desired conversion timing
1864 i8253_cascade_ns_to_timer_2div(LABPC_TIMER_BASE,
1865 &(devpriv->divisor_a0),
1866 &(devpriv->divisor_b0),
1868 cmd->flags & TRIG_ROUND_MASK);
1869 labpc_set_ai_convert_period(cmd, mode, convert_period);
1873 static int labpc_dio_mem_callback(int dir, int port, int data,
1874 unsigned long iobase)
1877 writeb(data, (void __iomem *)(iobase + port));
1880 return readb((void __iomem *)(iobase + port));
1884 /* lowlevel write to eeprom/dac */
1885 static void labpc_serial_out(struct comedi_device *dev, unsigned int value,
1886 unsigned int value_width)
1890 for (i = 1; i <= value_width; i++) {
1891 /* clear serial clock */
1892 devpriv->command5_bits &= ~SCLOCK_BIT;
1893 /* send bits most significant bit first */
1894 if (value & (1 << (value_width - i)))
1895 devpriv->command5_bits |= SDATA_BIT;
1897 devpriv->command5_bits &= ~SDATA_BIT;
1899 devpriv->write_byte(devpriv->command5_bits,
1900 dev->iobase + COMMAND5_REG);
1901 /* set clock to load bit */
1902 devpriv->command5_bits |= SCLOCK_BIT;
1904 devpriv->write_byte(devpriv->command5_bits,
1905 dev->iobase + COMMAND5_REG);
1909 /* lowlevel read from eeprom */
1910 static unsigned int labpc_serial_in(struct comedi_device *dev)
1912 unsigned int value = 0;
1914 const int value_width = 8; /* number of bits wide values are */
1916 for (i = 1; i <= value_width; i++) {
1917 /* set serial clock */
1918 devpriv->command5_bits |= SCLOCK_BIT;
1920 devpriv->write_byte(devpriv->command5_bits,
1921 dev->iobase + COMMAND5_REG);
1922 /* clear clock bit */
1923 devpriv->command5_bits &= ~SCLOCK_BIT;
1925 devpriv->write_byte(devpriv->command5_bits,
1926 dev->iobase + COMMAND5_REG);
1927 /* read bits most significant bit first */
1929 devpriv->status2_bits =
1930 devpriv->read_byte(dev->iobase + STATUS2_REG);
1931 if (devpriv->status2_bits & EEPROM_OUT_BIT)
1932 value |= 1 << (value_width - i);
1938 static unsigned int labpc_eeprom_read(struct comedi_device *dev,
1939 unsigned int address)
1942 /* bits to tell eeprom to expect a read */
1943 const int read_instruction = 0x3;
1944 /* 8 bit write lengths to eeprom */
1945 const int write_length = 8;
1947 /* enable read/write to eeprom */
1948 devpriv->command5_bits &= ~EEPROM_EN_BIT;
1950 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
1951 devpriv->command5_bits |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1953 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
1955 /* send read instruction */
1956 labpc_serial_out(dev, read_instruction, write_length);
1957 /* send 8 bit address to read from */
1958 labpc_serial_out(dev, address, write_length);
1960 value = labpc_serial_in(dev);
1962 /* disable read/write to eeprom */
1963 devpriv->command5_bits &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
1965 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
1970 static int labpc_eeprom_write(struct comedi_device *dev,
1971 unsigned int address, unsigned int value)
1973 const int write_enable_instruction = 0x6;
1974 const int write_instruction = 0x2;
1975 const int write_length = 8; /* 8 bit write lengths to eeprom */
1976 const int write_in_progress_bit = 0x1;
1977 const int timeout = 10000;
1980 /* make sure there isn't already a write in progress */
1981 for (i = 0; i < timeout; i++) {
1982 if ((labpc_eeprom_read_status(dev) & write_in_progress_bit) ==
1987 comedi_error(dev, "eeprom write timed out");
1990 /* update software copy of eeprom */
1991 devpriv->eeprom_data[address] = value;
1993 /* enable read/write to eeprom */
1994 devpriv->command5_bits &= ~EEPROM_EN_BIT;
1996 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
1997 devpriv->command5_bits |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
1999 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2001 /* send write_enable instruction */
2002 labpc_serial_out(dev, write_enable_instruction, write_length);
2003 devpriv->command5_bits &= ~EEPROM_EN_BIT;
2005 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2007 /* send write instruction */
2008 devpriv->command5_bits |= EEPROM_EN_BIT;
2010 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2011 labpc_serial_out(dev, write_instruction, write_length);
2012 /* send 8 bit address to write to */
2013 labpc_serial_out(dev, address, write_length);
2015 labpc_serial_out(dev, value, write_length);
2016 devpriv->command5_bits &= ~EEPROM_EN_BIT;
2018 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2020 /* disable read/write to eeprom */
2021 devpriv->command5_bits &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
2023 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2028 static unsigned int labpc_eeprom_read_status(struct comedi_device *dev)
2031 const int read_status_instruction = 0x5;
2032 const int write_length = 8; /* 8 bit write lengths to eeprom */
2034 /* enable read/write to eeprom */
2035 devpriv->command5_bits &= ~EEPROM_EN_BIT;
2037 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2038 devpriv->command5_bits |= EEPROM_EN_BIT | EEPROM_WRITE_UNPROTECT_BIT;
2040 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2042 /* send read status instruction */
2043 labpc_serial_out(dev, read_status_instruction, write_length);
2045 value = labpc_serial_in(dev);
2047 /* disable read/write to eeprom */
2048 devpriv->command5_bits &= ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
2050 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2055 /* writes to 8 bit calibration dacs */
2056 static void write_caldac(struct comedi_device *dev, unsigned int channel,
2059 if (value == devpriv->caldac[channel])
2061 devpriv->caldac[channel] = value;
2063 /* clear caldac load bit and make sure we don't write to eeprom */
2064 devpriv->command5_bits &=
2065 ~CALDAC_LOAD_BIT & ~EEPROM_EN_BIT & ~EEPROM_WRITE_UNPROTECT_BIT;
2067 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2069 /* write 4 bit channel */
2070 labpc_serial_out(dev, channel, 4);
2071 /* write 8 bit caldac value */
2072 labpc_serial_out(dev, value, 8);
2074 /* set and clear caldac bit to load caldac value */
2075 devpriv->command5_bits |= CALDAC_LOAD_BIT;
2077 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2078 devpriv->command5_bits &= ~CALDAC_LOAD_BIT;
2080 devpriv->write_byte(devpriv->command5_bits, dev->iobase + COMMAND5_REG);
2083 static struct comedi_driver labpc_driver = {
2084 .driver_name = DRV_NAME,
2085 .module = THIS_MODULE,
2086 .attach = labpc_attach,
2087 .attach_pci = labpc_attach_pci,
2088 .detach = labpc_common_detach,
2089 .num_names = ARRAY_SIZE(labpc_boards),
2090 .board_name = &labpc_boards[0].name,
2091 .offset = sizeof(struct labpc_board_struct),
2094 #ifdef CONFIG_COMEDI_PCI_DRIVERS
2095 static DEFINE_PCI_DEVICE_TABLE(labpc_pci_table) = {
2096 {PCI_DEVICE(PCI_VENDOR_ID_NI, 0x161)},
2099 MODULE_DEVICE_TABLE(pci, labpc_pci_table);
2101 static int __devinit labpc_pci_probe(struct pci_dev *dev,
2102 const struct pci_device_id *ent)
2104 return comedi_pci_auto_config(dev, &labpc_driver);
2107 static void __devexit labpc_pci_remove(struct pci_dev *dev)
2109 comedi_pci_auto_unconfig(dev);
2112 static struct pci_driver labpc_pci_driver = {
2114 .id_table = labpc_pci_table,
2115 .probe = labpc_pci_probe,
2116 .remove = __devexit_p(labpc_pci_remove)
2118 module_comedi_pci_driver(labpc_driver, labpc_pci_driver);
2120 module_comedi_driver(labpc_driver);
2124 MODULE_AUTHOR("Comedi http://www.comedi.org");
2125 MODULE_DESCRIPTION("Comedi low-level driver");
2126 MODULE_LICENSE("GPL");