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>
46 #include <linux/interrupt.h>
48 #include "comedi_pci.h"
52 /* #define CBPCIMDAS_DEBUG */
53 #undef CBPCIMDAS_DEBUG
55 /* Registers for the PCIM-DAS1602/16 */
57 /* sizes of io regions (bytes) */
58 #define BADR0_SIZE 2 /* ?? */
69 /* AI and Counter Constants */
71 #define MAIN_CONN_DIO 1
73 #define ADC_CONV_STAT 3
78 #define CLK8254_1_DATA 8
79 #define CLK8254_2_DATA 9
80 #define CLK8254_3_DATA 10
81 #define CLK8254_CONTROL 11
82 #define USER_COUNTER 12
83 #define RESID_COUNT_H 13
84 #define RESID_COUNT_L 14
86 /* Board description */
87 struct cb_pcimdas_board
{
89 unsigned short device_id
;
90 int ai_se_chans
; /* Inputs in single-ended mode */
91 int ai_diff_chans
; /* Inputs in differential mode */
92 int ai_bits
; /* analog input resolution */
93 int ai_speed
; /* fastest conversion period in ns */
94 int ao_nchan
; /* number of analog out channels */
95 int ao_bits
; /* analogue output resolution */
96 int has_ao_fifo
; /* analog output has fifo */
97 int ao_scan_speed
; /* analog output speed for 1602 series (for a scan, not conversion) */
98 int fifo_size
; /* number of samples fifo can hold */
99 int dio_bits
; /* number of dio bits */
100 int has_dio
; /* has DIO */
101 const struct comedi_lrange
*ranges
;
104 static const struct cb_pcimdas_board cb_pcimdas_boards
[] = {
106 .name
= "PCIM-DAS1602/16",
111 .ai_speed
= 10000, /* ?? */
114 .has_ao_fifo
= 0, /* ?? */
115 .ao_scan_speed
= 10000,
120 /* .ranges = &cb_pcimdas_ranges, */
124 /* This is used by modprobe to translate PCI IDs to drivers. Should
125 * only be used for PCI and ISA-PnP devices */
126 static DEFINE_PCI_DEVICE_TABLE(cb_pcimdas_pci_table
) = {
127 {PCI_VENDOR_ID_COMPUTERBOARDS
, 0x0056, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
131 MODULE_DEVICE_TABLE(pci
, cb_pcimdas_pci_table
);
133 #define N_BOARDS 1 /* Max number of boards supported */
136 * Useful for shorthand access to the particular board structure
138 #define thisboard ((const struct cb_pcimdas_board *)dev->board_ptr)
140 /* this structure is for data unique to this hardware driver. If
141 several hardware drivers keep similar information in this structure,
142 feel free to suggest moving the variable to the struct comedi_device struct. */
143 struct cb_pcimdas_private
{
146 /* would be useful for a PCI device */
147 struct pci_dev
*pci_dev
;
156 /* Used for AO readback */
157 unsigned int ao_readback
[2];
160 unsigned short int port_a
; /* copy of BADR4+0 */
161 unsigned short int port_b
; /* copy of BADR4+1 */
162 unsigned short int port_c
; /* copy of BADR4+2 */
163 unsigned short int dio_mode
; /* copy of BADR4+3 */
168 * most drivers define the following macro to make it easy to
169 * access the private structure.
171 #define devpriv ((struct cb_pcimdas_private *)dev->private)
174 * The struct comedi_driver structure tells the Comedi core module
175 * which functions to call to configure/deconfigure (attach/detach)
176 * the board, and also about the kernel module that contains
179 static int cb_pcimdas_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
);
180 static int cb_pcimdas_detach(struct comedi_device
*dev
);
181 static struct comedi_driver driver_cb_pcimdas
= {
182 .driver_name
= "cb_pcimdas",
183 .module
= THIS_MODULE
,
184 .attach
= cb_pcimdas_attach
,
185 .detach
= cb_pcimdas_detach
,
188 static int cb_pcimdas_ai_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
189 struct comedi_insn
*insn
, unsigned int *data
);
190 static int cb_pcimdas_ao_winsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
191 struct comedi_insn
*insn
, unsigned int *data
);
192 static int cb_pcimdas_ao_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
193 struct comedi_insn
*insn
, unsigned int *data
);
196 * Attach is called by the Comedi core to configure the driver
197 * for a particular board. If you specified a board_name array
198 * in the driver structure, dev->board_ptr contains that
201 static int cb_pcimdas_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
203 struct comedi_subdevice
*s
;
204 struct pci_dev
*pcidev
;
208 printk("comedi%d: cb_pcimdas: ", dev
->minor
);
211 * Allocate the private structure area.
213 if (alloc_private(dev
, sizeof(struct cb_pcimdas_private
)) < 0)
217 * Probe the device to determine what device in the series it is.
221 for (pcidev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, NULL
);
223 pcidev
= pci_get_device(PCI_ANY_ID
, PCI_ANY_ID
, pcidev
)) {
224 /* is it not a computer boards card? */
225 if (pcidev
->vendor
!= PCI_VENDOR_ID_COMPUTERBOARDS
)
227 /* loop through cards supported by this driver */
228 for (index
= 0; index
< N_BOARDS
; index
++) {
229 if (cb_pcimdas_boards
[index
].device_id
!=
232 /* was a particular bus/slot requested? */
233 if (it
->options
[0] || it
->options
[1]) {
234 /* are we on the wrong bus/slot? */
235 if (pcidev
->bus
->number
!= it
->options
[0] ||
236 PCI_SLOT(pcidev
->devfn
) !=
241 devpriv
->pci_dev
= pcidev
;
242 dev
->board_ptr
= cb_pcimdas_boards
+ index
;
247 printk("No supported ComputerBoards/MeasurementComputing card found on "
248 "requested position\n");
253 printk("Found %s on bus %i, slot %i\n", cb_pcimdas_boards
[index
].name
,
254 pcidev
->bus
->number
, PCI_SLOT(pcidev
->devfn
));
256 /* Warn about non-tested features */
257 switch (thisboard
->device_id
) {
261 printk("THIS CARD IS UNSUPPORTED.\n"
262 "PLEASE REPORT USAGE TO <mocelet@sucs.org>\n");
265 if (comedi_pci_enable(pcidev
, "cb_pcimdas")) {
266 printk(" Failed to enable PCI device and request regions\n");
270 devpriv
->BADR0
= pci_resource_start(devpriv
->pci_dev
, 0);
271 devpriv
->BADR1
= pci_resource_start(devpriv
->pci_dev
, 1);
272 devpriv
->BADR2
= pci_resource_start(devpriv
->pci_dev
, 2);
273 devpriv
->BADR3
= pci_resource_start(devpriv
->pci_dev
, 3);
274 devpriv
->BADR4
= pci_resource_start(devpriv
->pci_dev
, 4);
276 #ifdef CBPCIMDAS_DEBUG
277 printk("devpriv->BADR0 = 0x%lx\n", devpriv
->BADR0
);
278 printk("devpriv->BADR1 = 0x%lx\n", devpriv
->BADR1
);
279 printk("devpriv->BADR2 = 0x%lx\n", devpriv
->BADR2
);
280 printk("devpriv->BADR3 = 0x%lx\n", devpriv
->BADR3
);
281 printk("devpriv->BADR4 = 0x%lx\n", devpriv
->BADR4
);
284 /* Dont support IRQ yet */
286 /* if(request_irq(devpriv->pci_dev->irq, cb_pcimdas_interrupt, IRQF_SHARED, "cb_pcimdas", dev )) */
288 /* printk(" unable to allocate irq %u\n", devpriv->pci_dev->irq); */
289 /* return -EINVAL; */
291 /* dev->irq = devpriv->pci_dev->irq; */
293 /* Initialize dev->board_name */
294 dev
->board_name
= thisboard
->name
;
297 * Allocate the subdevice structures. alloc_subdevice() is a
298 * convenient macro defined in comedidev.h.
300 if (alloc_subdevices(dev
, 3) < 0)
303 s
= dev
->subdevices
+ 0;
304 /* dev->read_subdev=s; */
305 /* analog input subdevice */
306 s
->type
= COMEDI_SUBD_AI
;
307 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
;
308 s
->n_chan
= thisboard
->ai_se_chans
;
309 s
->maxdata
= (1 << thisboard
->ai_bits
) - 1;
310 s
->range_table
= &range_unknown
;
311 s
->len_chanlist
= 1; /* This is the maximum chanlist length that */
312 /* the board can handle */
313 s
->insn_read
= cb_pcimdas_ai_rinsn
;
315 s
= dev
->subdevices
+ 1;
316 /* analog output subdevice */
317 s
->type
= COMEDI_SUBD_AO
;
318 s
->subdev_flags
= SDF_WRITABLE
;
319 s
->n_chan
= thisboard
->ao_nchan
;
320 s
->maxdata
= 1 << thisboard
->ao_bits
;
321 s
->range_table
= &range_unknown
; /* ranges are hardware settable, but not software readable. */
322 s
->insn_write
= &cb_pcimdas_ao_winsn
;
323 s
->insn_read
= &cb_pcimdas_ao_rinsn
;
325 s
= dev
->subdevices
+ 2;
326 /* digital i/o subdevice */
327 if (thisboard
->has_dio
) {
328 subdev_8255_init(dev
, s
, NULL
, devpriv
->BADR4
);
330 s
->type
= COMEDI_SUBD_UNUSED
;
333 printk("attached\n");
339 * _detach is called to deconfigure a device. It should deallocate
341 * This function is also called when _attach() fails, so it should be
342 * careful not to release resources that were not necessarily
343 * allocated by _attach(). dev->private and dev->subdevices are
344 * deallocated automatically by the core.
346 static int cb_pcimdas_detach(struct comedi_device
*dev
)
348 #ifdef CBPCIMDAS_DEBUG
350 printk("devpriv->BADR0 = 0x%lx\n", devpriv
->BADR0
);
351 printk("devpriv->BADR1 = 0x%lx\n", devpriv
->BADR1
);
352 printk("devpriv->BADR2 = 0x%lx\n", devpriv
->BADR2
);
353 printk("devpriv->BADR3 = 0x%lx\n", devpriv
->BADR3
);
354 printk("devpriv->BADR4 = 0x%lx\n", devpriv
->BADR4
);
357 printk("comedi%d: cb_pcimdas: remove\n", dev
->minor
);
359 free_irq(dev
->irq
, dev
);
361 if (devpriv
->pci_dev
) {
362 if (devpriv
->BADR0
) {
363 comedi_pci_disable(devpriv
->pci_dev
);
365 pci_dev_put(devpriv
->pci_dev
);
373 * "instructions" read/write data in "one-shot" or "software-triggered"
376 static int cb_pcimdas_ai_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
377 struct comedi_insn
*insn
, unsigned int *data
)
382 int chan
= CR_CHAN(insn
->chanspec
);
383 unsigned short chanlims
;
386 /* only support sw initiated reads from a single channel */
388 /* check channel number */
389 if ((inb(devpriv
->BADR3
+ 2) & 0x20) == 0) /* differential mode */
390 maxchans
= thisboard
->ai_diff_chans
;
392 maxchans
= thisboard
->ai_se_chans
;
394 if (chan
> (maxchans
- 1))
395 return -ETIMEDOUT
; /* *** Wrong error code. Fixme. */
397 /* configure for sw initiated read */
398 d
= inb(devpriv
->BADR3
+ 5);
399 if ((d
& 0x03) > 0) { /* only reset if needed. */
401 outb(d
, devpriv
->BADR3
+ 5);
403 outb(0x01, devpriv
->BADR3
+ 6); /* set bursting off, conversions on */
404 outb(0x00, devpriv
->BADR3
+ 7); /* set range to 10V. UP/BP is controlled by a switch on the board */
406 /* write channel limits to multiplexer, set Low (bits 0-3) and High (bits 4-7) channels to chan. */
407 chanlims
= chan
| (chan
<< 4);
408 outb(chanlims
, devpriv
->BADR3
+ 0);
410 /* convert n samples */
411 for (n
= 0; n
< insn
->n
; n
++) {
412 /* trigger conversion */
413 outw(0, devpriv
->BADR2
+ 0);
415 #define TIMEOUT 1000 /* typically takes 5 loops on a lightly loaded Pentium 100MHz, */
416 /* this is likely to be 100 loops on a 2GHz machine, so set 1000 as the limit. */
418 /* wait for conversion to end */
419 for (i
= 0; i
< TIMEOUT
; i
++) {
420 busy
= inb(devpriv
->BADR3
+ 2) & 0x80;
429 d
= inw(devpriv
->BADR2
+ 0);
431 /* mangle the data as necessary */
432 /* d ^= 1<<(thisboard->ai_bits-1); // 16 bit data from ADC, so no mangle needed. */
437 /* return the number of samples read/written */
441 static int cb_pcimdas_ao_winsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
442 struct comedi_insn
*insn
, unsigned int *data
)
445 int chan
= CR_CHAN(insn
->chanspec
);
447 /* Writing a list of values to an AO channel is probably not
448 * very useful, but that's how the interface is defined. */
449 for (i
= 0; i
< insn
->n
; i
++) {
452 outw(data
[i
] & 0x0FFF, devpriv
->BADR2
+ DAC0_OFFSET
);
455 outw(data
[i
] & 0x0FFF, devpriv
->BADR2
+ DAC1_OFFSET
);
460 devpriv
->ao_readback
[chan
] = data
[i
];
463 /* return the number of samples read/written */
467 /* AO subdevices should have a read insn as well as a write insn.
468 * Usually this means copying a value stored in devpriv. */
469 static int cb_pcimdas_ao_rinsn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
470 struct comedi_insn
*insn
, unsigned int *data
)
473 int chan
= CR_CHAN(insn
->chanspec
);
475 for (i
= 0; i
< insn
->n
; i
++)
476 data
[i
] = devpriv
->ao_readback
[chan
];
482 * A convenient macro that defines init_module() and cleanup_module(),
485 COMEDI_PCI_INITCLEANUP(driver_cb_pcimdas
, cb_pcimdas_pci_table
);