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.mccdaq.com/PDFs/Manuals/pcim-das1602-16.pdf for more details.
43 #include "../comedidev.h"
45 #include <linux/delay.h>
46 #include <linux/interrupt.h>
48 #include "comedi_pci.h"
52 /* #define CBPCIMDAS_DEBUG */
53 #undef CBPCIMDAS_DEBUG
55 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
57 /* Registers for the PCIM-DAS1602/16 */
59 /* sizes of io regions (bytes) */
60 #define BADR0_SIZE 2 /* ?? */
71 /* AI and Counter Constants */
73 #define MAIN_CONN_DIO 1
75 #define ADC_CONV_STAT 3
80 #define CLK8254_1_DATA 8
81 #define CLK8254_2_DATA 9
82 #define CLK8254_3_DATA 10
83 #define CLK8254_CONTROL 11
84 #define USER_COUNTER 12
85 #define RESID_COUNT_H 13
86 #define RESID_COUNT_L 14
88 /* Board description */
89 struct cb_pcimdas_board {
91 unsigned short device_id;
92 int ai_se_chans; /* Inputs in single-ended mode */
93 int ai_diff_chans; /* Inputs in differential mode */
94 int ai_bits; /* analog input resolution */
95 int ai_speed; /* fastest conversion period in ns */
96 int ao_nchan; /* number of analog out channels */
97 int ao_bits; /* analogue output resolution */
98 int has_ao_fifo; /* analog output has fifo */
99 int ao_scan_speed; /* analog output speed for 1602 series (for a scan, not conversion) */
100 int fifo_size; /* number of samples fifo can hold */
101 int dio_bits; /* number of dio bits */
102 int has_dio; /* has DIO */
103 const struct comedi_lrange *ranges;
106 static const struct cb_pcimdas_board cb_pcimdas_boards[] = {
108 .name = "PCIM-DAS1602/16",
113 .ai_speed = 10000, /* ?? */
116 .has_ao_fifo = 0, /* ?? */
117 .ao_scan_speed = 10000,
122 /* .ranges = &cb_pcimdas_ranges, */
126 /* This is used by modprobe to translate PCI IDs to drivers. Should
127 * only be used for PCI and ISA-PnP devices */
128 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table) = {
130 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0056, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
135 MODULE_DEVICE_TABLE(pci, cb_pcimdas_pci_table);
137 #define N_BOARDS 1 /* Max number of boards supported */
140 * Useful for shorthand access to the particular board structure
142 #define thisboard ((const struct cb_pcimdas_board *)dev->board_ptr)
144 /* this structure is for data unique to this hardware driver. If
145 several hardware drivers keep similar information in this structure,
146 feel free to suggest moving the variable to the struct comedi_device struct. */
147 struct cb_pcimdas_private {
150 /* would be useful for a PCI device */
151 struct pci_dev *pci_dev;
160 /* Used for AO readback */
161 unsigned int ao_readback[2];
164 unsigned short int port_a; /* copy of BADR4+0 */
165 unsigned short int port_b; /* copy of BADR4+1 */
166 unsigned short int port_c; /* copy of BADR4+2 */
167 unsigned short int dio_mode; /* copy of BADR4+3 */
172 * most drivers define the following macro to make it easy to
173 * access the private structure.
175 #define devpriv ((struct cb_pcimdas_private *)dev->private)
178 * The struct comedi_driver structure tells the Comedi core module
179 * which functions to call to configure/deconfigure (attach/detach)
180 * the board, and also about the kernel module that contains
183 static int cb_pcimdas_attach(struct comedi_device *dev,
184 struct comedi_devconfig *it);
185 static int cb_pcimdas_detach(struct comedi_device *dev);
186 static struct comedi_driver driver_cb_pcimdas = {
187 .driver_name = "cb_pcimdas",
188 .module = THIS_MODULE,
189 .attach = cb_pcimdas_attach,
190 .detach = cb_pcimdas_detach,
193 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
194 struct comedi_subdevice *s,
195 struct comedi_insn *insn, unsigned int *data);
196 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
197 struct comedi_subdevice *s,
198 struct comedi_insn *insn, unsigned int *data);
199 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
200 struct comedi_subdevice *s,
201 struct comedi_insn *insn, unsigned int *data);
204 * Attach is called by the Comedi core to configure the driver
205 * for a particular board. If you specified a board_name array
206 * in the driver structure, dev->board_ptr contains that
209 static int cb_pcimdas_attach(struct comedi_device *dev,
210 struct comedi_devconfig *it)
212 struct comedi_subdevice *s;
213 struct pci_dev *pcidev = NULL;
217 printk("comedi%d: cb_pcimdas: ", dev->minor);
220 * Allocate the private structure area.
222 if (alloc_private(dev, sizeof(struct cb_pcimdas_private)) < 0)
226 * Probe the device to determine what device in the series it is.
230 for_each_pci_dev(pcidev) {
231 /* is it not a computer boards card? */
232 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS)
234 /* loop through cards supported by this driver */
235 for (index = 0; index < N_BOARDS; index++) {
236 if (cb_pcimdas_boards[index].device_id !=
239 /* was a particular bus/slot requested? */
240 if (it->options[0] || it->options[1]) {
241 /* are we on the wrong bus/slot? */
242 if (pcidev->bus->number != it->options[0] ||
243 PCI_SLOT(pcidev->devfn) != it->options[1]) {
247 devpriv->pci_dev = pcidev;
248 dev->board_ptr = cb_pcimdas_boards + index;
253 printk("No supported ComputerBoards/MeasurementComputing card found on "
254 "requested position\n");
259 printk("Found %s on bus %i, slot %i\n", cb_pcimdas_boards[index].name,
260 pcidev->bus->number, PCI_SLOT(pcidev->devfn));
262 /* Warn about non-tested features */
263 switch (thisboard->device_id) {
267 printk("THIS CARD IS UNSUPPORTED.\n"
268 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n");
271 if (comedi_pci_enable(pcidev, "cb_pcimdas")) {
272 printk(" Failed to enable PCI device and request regions\n");
276 devpriv->BADR0 = pci_resource_start(devpriv->pci_dev, 0);
277 devpriv->BADR1 = pci_resource_start(devpriv->pci_dev, 1);
278 devpriv->BADR2 = pci_resource_start(devpriv->pci_dev, 2);
279 devpriv->BADR3 = pci_resource_start(devpriv->pci_dev, 3);
280 devpriv->BADR4 = pci_resource_start(devpriv->pci_dev, 4);
282 #ifdef CBPCIMDAS_DEBUG
283 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
284 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
285 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
286 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
287 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
290 /* Dont support IRQ yet */
292 /* if(request_irq(devpriv->pci_dev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
294 /* printk(" unable to allocate irq %u\n", devpriv->pci_dev->irq); */
295 /* return -EINVAL; */
297 /* dev->irq = devpriv->pci_dev->irq; */
299 /* Initialize dev->board_name */
300 dev->board_name = thisboard->name;
303 * Allocate the subdevice structures. alloc_subdevice() is a
304 * convenient macro defined in comedidev.h.
306 if (alloc_subdevices(dev, 3) < 0)
309 s = dev->subdevices + 0;
310 /* dev->read_subdev=s; */
311 /* analog input subdevice */
312 s->type = COMEDI_SUBD_AI;
313 s->subdev_flags = SDF_READABLE | SDF_GROUND;
314 s->n_chan = thisboard->ai_se_chans;
315 s->maxdata = (1 << thisboard->ai_bits) - 1;
316 s->range_table = &range_unknown;
317 s->len_chanlist = 1; /* This is the maximum chanlist length that */
318 /* the board can handle */
319 s->insn_read = cb_pcimdas_ai_rinsn;
321 s = dev->subdevices + 1;
322 /* analog output subdevice */
323 s->type = COMEDI_SUBD_AO;
324 s->subdev_flags = SDF_WRITABLE;
325 s->n_chan = thisboard->ao_nchan;
326 s->maxdata = 1 << thisboard->ao_bits;
327 s->range_table = &range_unknown; /* ranges are hardware settable, but not software readable. */
328 s->insn_write = &cb_pcimdas_ao_winsn;
329 s->insn_read = &cb_pcimdas_ao_rinsn;
331 s = dev->subdevices + 2;
332 /* digital i/o subdevice */
333 if (thisboard->has_dio)
334 subdev_8255_init(dev, s, NULL, devpriv->BADR4);
336 s->type = COMEDI_SUBD_UNUSED;
338 printk("attached\n");
344 * _detach is called to deconfigure a device. It should deallocate
346 * This function is also called when _attach() fails, so it should be
347 * careful not to release resources that were not necessarily
348 * allocated by _attach(). dev->private and dev->subdevices are
349 * deallocated automatically by the core.
351 static int cb_pcimdas_detach(struct comedi_device *dev)
353 #ifdef CBPCIMDAS_DEBUG
355 printk("devpriv->BADR0 = 0x%lx\n", devpriv->BADR0);
356 printk("devpriv->BADR1 = 0x%lx\n", devpriv->BADR1);
357 printk("devpriv->BADR2 = 0x%lx\n", devpriv->BADR2);
358 printk("devpriv->BADR3 = 0x%lx\n", devpriv->BADR3);
359 printk("devpriv->BADR4 = 0x%lx\n", devpriv->BADR4);
362 printk("comedi%d: cb_pcimdas: remove\n", dev->minor);
364 free_irq(dev->irq, dev);
366 if (devpriv->pci_dev) {
368 comedi_pci_disable(devpriv->pci_dev);
369 pci_dev_put(devpriv->pci_dev);
377 * "instructions" read/write data in "one-shot" or "software-triggered"
380 static int cb_pcimdas_ai_rinsn(struct comedi_device *dev,
381 struct comedi_subdevice *s,
382 struct comedi_insn *insn, unsigned int *data)
387 int chan = CR_CHAN(insn->chanspec);
388 unsigned short chanlims;
391 /* only support sw initiated reads from a single channel */
393 /* check channel number */
394 if ((inb(devpriv->BADR3 + 2) & 0x20) == 0) /* differential mode */
395 maxchans = thisboard->ai_diff_chans;
397 maxchans = thisboard->ai_se_chans;
399 if (chan > (maxchans - 1))
400 return -ETIMEDOUT; /* *** Wrong error code. Fixme. */
402 /* configure for sw initiated read */
403 d = inb(devpriv->BADR3 + 5);
404 if ((d & 0x03) > 0) { /* only reset if needed. */
406 outb(d, devpriv->BADR3 + 5);
408 outb(0x01, devpriv->BADR3 + 6); /* set bursting off, conversions on */
409 outb(0x00, devpriv->BADR3 + 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
411 /* write channel limits to multiplexer, set Low (bits 0-3) and High (bits 4-7) channels to chan. */
412 chanlims = chan | (chan << 4);
413 outb(chanlims, devpriv->BADR3 + 0);
415 /* convert n samples */
416 for (n = 0; n < insn->n; n++) {
417 /* trigger conversion */
418 outw(0, devpriv->BADR2 + 0);
420 #define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
421 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
423 /* wait for conversion to end */
424 for (i = 0; i < TIMEOUT; i++) {
425 busy = inb(devpriv->BADR3 + 2) & 0x80;
434 d = inw(devpriv->BADR2 + 0);
436 /* mangle the data as necessary */
437 /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */
442 /* return the number of samples read/written */
446 static int cb_pcimdas_ao_winsn(struct comedi_device *dev,
447 struct comedi_subdevice *s,
448 struct comedi_insn *insn, unsigned int *data)
451 int chan = CR_CHAN(insn->chanspec);
453 /* Writing a list of values to an AO channel is probably not
454 * very useful, but that's how the interface is defined. */
455 for (i = 0; i < insn->n; i++) {
458 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC0_OFFSET);
461 outw(data[i] & 0x0FFF, devpriv->BADR2 + DAC1_OFFSET);
466 devpriv->ao_readback[chan] = data[i];
469 /* return the number of samples read/written */
473 /* AO subdevices should have a read insn as well as a write insn.
474 * Usually this means copying a value stored in devpriv. */
475 static int cb_pcimdas_ao_rinsn(struct comedi_device *dev,
476 struct comedi_subdevice *s,
477 struct comedi_insn *insn, unsigned int *data)
480 int chan = CR_CHAN(insn->chanspec);
482 for (i = 0; i < insn->n; i++)
483 data[i] = devpriv->ao_readback[chan];
489 * A convenient macro that defines init_module() and cleanup_module(),
492 static int __devinit driver_cb_pcimdas_pci_probe(struct pci_dev *dev,
493 const struct pci_device_id
496 return comedi_pci_auto_config(dev, driver_cb_pcimdas.driver_name);
499 static void __devexit driver_cb_pcimdas_pci_remove(struct pci_dev *dev)
501 comedi_pci_auto_unconfig(dev);
504 static struct pci_driver driver_cb_pcimdas_pci_driver = {
505 .id_table = cb_pcimdas_pci_table,
506 .probe = &driver_cb_pcimdas_pci_probe,
507 .remove = __devexit_p(&driver_cb_pcimdas_pci_remove)
510 static int __init driver_cb_pcimdas_init_module(void)
514 retval = comedi_driver_register(&driver_cb_pcimdas);
518 driver_cb_pcimdas_pci_driver.name =
519 (char *)driver_cb_pcimdas.driver_name;
520 return pci_register_driver(&driver_cb_pcimdas_pci_driver);
523 static void __exit driver_cb_pcimdas_cleanup_module(void)
525 pci_unregister_driver(&driver_cb_pcimdas_pci_driver);
526 comedi_driver_unregister(&driver_cb_pcimdas);
529 module_init(driver_cb_pcimdas_init_module);
530 module_exit(driver_cb_pcimdas_cleanup_module);
532 MODULE_AUTHOR("Comedi http://www.comedi.org");
533 MODULE_DESCRIPTION("Comedi low-level driver");
534 MODULE_LICENSE("GPL");