2 comedi/drivers/cb_pcimdas.c
3 Comedi driver for Computer Boards PCIM-DAS1602/16
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: Measurement Computing PCI Migration series boards
26 Devices: [ComputerBoards] PCIM-DAS1602/16 (cb_pcimdas)
27 Author: Richard Bytheway
28 Updated: Wed, 13 Nov 2002 12:34:56 +0000
31 Written to support the PCIM-DAS1602/16 on a 2.4 series kernel.
33 Configuration Options:
37 Developed from cb_pcidas and skel by Richard Bytheway (mocelet@sucs.org).
38 Only supports DIO, AO and simple AI in it's present form.
39 No interrupts, multi channel or FIFO AI, although the card looks like it could support this.
40 See http://www.measurementcomputing.com/PDFManuals/pcim-das1602_16.pdf for more details.
43 #include "../comedidev.h"
45 #include <linux/delay.h>
47 #include "comedi_pci.h"
51 //#define CBPCIMDAS_DEBUG
52 #undef CBPCIMDAS_DEBUG
54 /* Registers for the PCIM-DAS1602/16 */
56 // sizes of io regions (bytes)
57 #define BADR0_SIZE 2 //??
68 //AI and Counter Constants
70 #define MAIN_CONN_DIO 1
72 #define ADC_CONV_STAT 3
77 #define CLK8254_1_DATA 8
78 #define CLK8254_2_DATA 9
79 #define CLK8254_3_DATA 10
80 #define CLK8254_CONTROL 11
81 #define USER_COUNTER 12
82 #define RESID_COUNT_H 13
83 #define RESID_COUNT_L 14
85 /* Board description */
86 typedef struct cb_pcimdas_board_struct {
88 unsigned short device_id;
89 int ai_se_chans; // Inputs in single-ended mode
90 int ai_diff_chans; // Inputs in differential mode
91 int ai_bits; // analog input resolution
92 int ai_speed; // fastest conversion period in ns
93 int ao_nchan; // number of analog out channels
94 int ao_bits; // analogue output resolution
95 int has_ao_fifo; // analog output has fifo
96 int ao_scan_speed; // analog output speed for 1602 series (for a scan, not conversion)
97 int fifo_size; // number of samples fifo can hold
98 int dio_bits; // number of dio bits
99 int has_dio; // has DIO
100 const struct comedi_lrange *ranges;
103 static const cb_pcimdas_board cb_pcimdas_boards[] = {
105 name: "PCIM-DAS1602/16",
119 // ranges: &cb_pcimdas_ranges,
123 /* This is used by modprobe to translate PCI IDs to drivers. Should
124 * only be used for PCI and ISA-PnP devices */
125 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = {
126 {PCI_VENDOR_ID_COMPUTERBOARDS, 0x0056, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
130 MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
132 #define N_BOARDS 1 // Max number of boards supported
135 * Useful for shorthand access to the particular board structure
137 #define thisboard ((const cb_pcimdas_board *)dev->board_ptr)
139 /* this structure is for data unique to this hardware driver. If
140 several hardware drivers keep similar information in this structure,
141 feel free to suggest moving the variable to the struct comedi_device struct. */
145 // would be useful for a PCI device
146 struct pci_dev *pci_dev;
155 /* Used for AO readback */
156 unsigned int ao_readback[2];
159 unsigned short int port_a; // copy of BADR4+0
160 unsigned short int port_b; // copy of BADR4+1
161 unsigned short int port_c; // copy of BADR4+2
162 unsigned short int dio_mode; // copy of BADR4+3
164 } cb_pcimdas_private;
167 * most drivers define the following macro to make it easy to
168 * access the private structure.
170 #define devpriv ((cb_pcimdas_private *)dev->private)
173 * The struct comedi_driver structure tells the Comedi core module
174 * which functions to call to configure/deconfigure (attach/detach)
175 * the board, and also about the kernel module that contains
178 static int cb_pcimdas_attach(struct comedi_device * dev, comedi_devconfig * it);
179 static int cb_pcimdas_detach(struct comedi_device * dev);
180 static struct comedi_driver driver_cb_pcimdas = {
181 driver_name:"cb_pcimdas",
183 attach:cb_pcimdas_attach,
184 detach:cb_pcimdas_detach,
187 static int cb_pcimdas_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
188 struct comedi_insn * insn, unsigned int * data);
189 static int cb_pcimdas_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
190 struct comedi_insn * insn, unsigned int * data);
191 static int cb_pcimdas_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
192 struct comedi_insn * insn, unsigned int * data);
195 * Attach is called by the Comedi core to configure the driver
196 * for a particular board. If you specified a board_name array
197 * in the driver structure, dev->board_ptr contains that
200 static int cb_pcimdas_attach(struct comedi_device * dev, comedi_devconfig * it)
202 struct comedi_subdevice *s;
203 struct pci_dev *pcidev;
207 printk("comedi%d: cb_pcimdas: ", dev->minor);
210 * Allocate the private structure area.
212 if (alloc_private(dev, sizeof(cb_pcimdas_private)) < 0)
216 * Probe the device to determine what device in the series it is.
220 for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
222 pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
223 // is it not a computer boards card?
224 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS)
226 // loop through cards supported by this driver
227 for (index = 0; index < N_BOARDS; index++) {
228 if (cb_pcimdas_boards[index].device_id !=
231 // was a particular bus/slot requested?
232 if (it->options[0] || it->options[1]) {
233 // are we on the wrong bus/slot?
234 if (pcidev->bus->number != it->options[0] ||
235 PCI_SLOT(pcidev->devfn) !=
240 devpriv->pci_dev = pcidev;
241 dev->board_ptr = cb_pcimdas_boards + index;
246 printk("No supported ComputerBoards/MeasurementComputing card found on "
247 "requested position\n");
252 printk("Found %s on bus %i, slot %i\n", cb_pcimdas_boards[index].name,
253 pcidev->bus->number, PCI_SLOT(pcidev->devfn));
255 // Warn about non-tested features
256 switch (thisboard->device_id) {
260 printk("THIS CARD IS UNSUPPORTED.\n"
261 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n");
264 if (comedi_pci_enable(pcidev, "cb_pcimdas")) {
265 printk(" Failed to enable PCI device and request regions\n");
269 devpriv->BADR0 = pci_resource_start(devpriv->pci_dev, 0);
270 devpriv->BADR1 = pci_resource_start(devpriv->pci_dev, 1);
271 devpriv->BADR2 = pci_resource_start(devpriv->pci_dev, 2);
272 devpriv->BADR3 = pci_resource_start(devpriv->pci_dev, 3);
273 devpriv->BADR4 = pci_resource_start(devpriv->pci_dev, 4);
275 #ifdef CBPCIMDAS_DEBUG
276 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
277 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
278 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
279 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
280 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
283 // Dont support IRQ yet
285 // if(comedi_request_irq(devpriv->pci_dev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev ))
287 // printk(" unable to allocate irq %u\n", devpriv->pci_dev->irq);
290 // dev->irq = devpriv->pci_dev->irq;
292 //Initialize dev->board_name
293 dev->board_name = thisboard->name;
296 * Allocate the subdevice structures. alloc_subdevice() is a
297 * convenient macro defined in comedidev.h.
299 if (alloc_subdevices(dev, 3) < 0)
302 s = dev->subdevices + 0;
303 //dev->read_subdev=s;
304 // analog input subdevice
305 s->type = COMEDI_SUBD_AI;
306 s->subdev_flags = SDF_READABLE | SDF_GROUND;
307 s->n_chan = thisboard->ai_se_chans;
308 s->maxdata = (1 << thisboard->ai_bits) - 1;
309 s->range_table = &range_unknown;
310 s->len_chanlist = 1; // This is the maximum chanlist length that
311 // the board can handle
312 s->insn_read = cb_pcimdas_ai_rinsn;
314 s = dev->subdevices + 1;
315 // analog output subdevice
316 s->type = COMEDI_SUBD_AO;
317 s->subdev_flags = SDF_WRITABLE;
318 s->n_chan = thisboard->ao_nchan;
319 s->maxdata = 1 << thisboard->ao_bits;
320 s->range_table = &range_unknown; //ranges are hardware settable, but not software readable.
321 s->insn_write = &cb_pcimdas_ao_winsn;
322 s->insn_read = &cb_pcimdas_ao_rinsn;
324 s = dev->subdevices + 2;
325 /* digital i/o subdevice */
326 if (thisboard->has_dio) {
327 subdev_8255_init(dev, s, NULL, devpriv->BADR4);
329 s->type = COMEDI_SUBD_UNUSED;
332 printk("attached\n");
338 * _detach is called to deconfigure a device. It should deallocate
340 * This function is also called when _attach() fails, so it should be
341 * careful not to release resources that were not necessarily
342 * allocated by _attach(). dev->private and dev->subdevices are
343 * deallocated automatically by the core.
345 static int cb_pcimdas_detach(struct comedi_device * dev)
347 #ifdef CBPCIMDAS_DEBUG
349 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
350 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
351 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
352 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
353 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
356 printk("comedi%d: cb_pcimdas: remove\n", dev->minor);
358 comedi_free_irq(dev->irq, dev);
360 if (devpriv->pci_dev) {
361 if (devpriv->BADR0) {
362 comedi_pci_disable(devpriv->pci_dev);
364 pci_dev_put(devpriv->pci_dev);
372 * "instructions" read/write data in "one-shot" or "software-triggered"
375 static int cb_pcimdas_ai_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
376 struct comedi_insn * insn, unsigned int * data)
381 int chan = CR_CHAN(insn->chanspec);
382 unsigned short chanlims;
385 // only support sw initiated reads from a single channel
387 //check channel number
388 if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) //differential mode
389 maxchans = thisboard->ai_diff_chans;
391 maxchans = thisboard->ai_se_chans;
393 if (chan > (maxchans - 1))
394 return -ETIMEDOUT; //*** Wrong error code. Fixme.
396 //configure for sw initiated read
397 d = inb(devpriv->BADR3 + 5);
398 if ((d & 0x03) > 0) { //only reset if needed.
400 outb(d, devpriv->BADR3 + 5);
402 outb(0x01, devpriv->BADR3 + 6); //set bursting off, conversions on
403 outb(0x00, devpriv->BADR3 + 7); //set range to 10V. UP/BP is controlled by a switch on the board
405 // write channel limits to multiplexer, set Low (bits 0-3) and High (bits 4-7) channels to chan.
406 chanlims = chan | (chan << 4);
407 outb(chanlims, devpriv->BADR3 + 0);
409 /* convert n samples */
410 for (n = 0; n < insn->n; n++) {
411 /* trigger conversion */
412 outw(0, devpriv->BADR2 + 0);
414 #define TIMEOUT 1000 //typically takes 5 loops on a lightly loaded Pentium 100MHz,
415 //this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit.
417 /* wait for conversion to end */
418 for (i = 0; i < TIMEOUT; i++) {
419 busy = inb(devpriv->BADR3 + 2) & 0x80;
428 d = inw(devpriv->BADR2 + 0);
430 /* mangle the data as necessary */
431 //d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed.
436 /* return the number of samples read/written */
440 static int cb_pcimdas_ao_winsn(struct comedi_device * dev, struct comedi_subdevice * s,
441 struct comedi_insn * insn, unsigned int * data)
444 int chan = CR_CHAN(insn->chanspec);
446 /* Writing a list of values to an AO channel is probably not
447 * very useful, but that's how the interface is defined. */
448 for (i = 0; i < insn->n; i++) {
451 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC0_OFFSET);
454 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC1_OFFSET);
459 devpriv->ao_readback[chan] = data[i];
462 /* return the number of samples read/written */
466 /* AO subdevices should have a read insn as well as a write insn.
467 * Usually this means copying a value stored in devpriv. */
468 static int cb_pcimdas_ao_rinsn(struct comedi_device * dev, struct comedi_subdevice * s,
469 struct comedi_insn * insn, unsigned int * data)
472 int chan = CR_CHAN(insn->chanspec);
474 for (i = 0; i < insn->n; i++)
475 data[i] = devpriv->ao_readback[chan];
481 * A convenient macro that defines init_module() and cleanup_module(),
484 COMEDI_PCI_INITCLEANUP(driver_cb_pcimdas, cb_pcimdas_pci_table);