2 comedi/drivers/ke_counter.c
3 Comedi driver for Kolter-Electronic PCI Counter 1 Card
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: Driver for Kolter Electronic Counter Card
26 Devices: [Kolter Electronic] PCI Counter Card (ke_counter)
27 Author: Michael Hillmann
31 This driver is a simple driver to read the counter values from
32 Kolter Electronic PCI Counter Card.
35 #include <linux/comedidev.h>
37 #include <linux/pci.h>
39 #define CNT_DRIVER_NAME "ke_counter"
40 #define PCI_VENDOR_ID_KOLTER 0x1001
41 #define CNT_CARD_DEVICE_ID 0x0014
43 /*-- function prototypes ----------------------------------------------------*/
45 static int cnt_attach(comedi_device *dev,comedi_devconfig *it);
46 static int cnt_detach(comedi_device *dev);
48 static struct pci_device_id cnt_pci_table[] __devinitdata =
50 { PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
53 MODULE_DEVICE_TABLE(pci, cnt_pci_table);
55 /*-- board specification structure ------------------------------------------*/
65 static cnt_board_struct cnt_boards[] =
68 name: CNT_DRIVER_NAME,
69 device_id: CNT_CARD_DEVICE_ID,
75 #define cnt_board_nbr (sizeof(cnt_boards)/sizeof(cnt_board_struct))
77 /*-- device private structure -----------------------------------------------*/
81 struct pci_dev *pcidev;
84 #define devpriv ((cnt_device_private *)dev->private)
86 static comedi_driver cnt_driver =
88 driver_name: CNT_DRIVER_NAME,
93 COMEDI_INITCLEANUP(cnt_driver);
95 /*-- counter write ----------------------------------------------------------*/
97 /* This should be used only for resetting the counters; maybe it is better
98 to make a special command 'reset'. */
99 static int cnt_winsn(comedi_device *dev,
104 int chan = CR_CHAN(insn->chanspec);
106 outb((unsigned char)((data[0] >> 24) & 0xff), dev->iobase + chan * 0x20 + 0x10);
107 outb((unsigned char)((data[0] >> 16) & 0xff), dev->iobase + chan * 0x20 + 0x0c);
108 outb((unsigned char)((data[0] >> 8) & 0xff), dev->iobase + chan * 0x20 + 0x08);
109 outb((unsigned char)((data[0] >> 0) & 0xff), dev->iobase + chan * 0x20 + 0x04);
111 /* return the number of samples written */
115 /*-- counter read -----------------------------------------------------------*/
117 static int cnt_rinsn(comedi_device *dev,
122 unsigned char a0, a1, a2, a3, a4;
123 int chan = CR_CHAN(insn->chanspec);
126 a0 = inb(dev->iobase + chan * 0x20);
127 a1 = inb(dev->iobase + chan * 0x20 + 0x04);
128 a2 = inb(dev->iobase + chan * 0x20 + 0x08);
129 a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
130 a4 = inb(dev->iobase + chan * 0x20 + 0x10);
132 result = (a1 + (a2 * 256) + (a3 * 65536));
133 if (a4 > 0) result = result - s->maxdata;
135 *data = (lsampl_t)result;
137 /* return the number of samples read */
141 /*-- attach -----------------------------------------------------------------*/
143 static int cnt_attach(comedi_device *dev, comedi_devconfig *it)
145 comedi_subdevice *subdevice;
146 struct pci_dev *pci_device;
147 cnt_board_struct *board;
148 unsigned long io_base;
151 /* allocate device private structure */
152 if((error = alloc_private(dev, sizeof(cnt_device_private))) < 0)
157 /* Probe the device to determine what device in the series it is. */
158 for(pci_device = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL); pci_device != NULL ;
159 pci_device = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_device))
161 if(pci_device->vendor == PCI_VENDOR_ID_KOLTER)
163 for(i=0; i<cnt_board_nbr; i++)
165 if(cnt_boards[i].device_id == pci_device->device)
167 /* was a particular bus/slot requested? */
168 if((it->options[0] != 0) || (it->options[1] != 0))
170 /* are we on the wrong bus/slot? */
171 if(pci_device->bus->number != it->options[0] ||
172 PCI_SLOT(pci_device->devfn) != it->options[1])
178 dev->board_ptr = cnt_boards + i;
179 board = (cnt_board_struct *)dev->board_ptr;
185 printk("comedi%d: no supported board found! (req. bus/slot: %d/%d)\n",
186 dev->minor, it->options[0], it->options[1]);
190 printk("comedi%d: found %s at PCI bus %d, slot %d\n",dev->minor,
191 board->name, pci_device->bus->number, PCI_SLOT(pci_device->devfn));
192 devpriv->pcidev = pci_device;
193 dev->board_name = board->name;
195 /* enable PCI device */
196 if((error = pci_enable_device(pci_device)) < 0)
201 /* request PCI regions */
202 if((error = pci_request_regions(pci_device, CNT_DRIVER_NAME)) < 0)
207 /* read register base address [PCI_BASE_ADDRESS #0] */
208 io_base = pci_resource_start(pci_device, 0);
209 dev->iobase = io_base;
211 /* allocate the subdevice structures */
212 if((error = alloc_subdevices(dev, 1)) < 0)
217 subdevice = dev->subdevices + 0;
218 dev->read_subdev = subdevice;
220 subdevice->type = COMEDI_SUBD_COUNTER;
221 subdevice->subdev_flags = SDF_READABLE /* | SDF_COMMON*/;
222 subdevice->n_chan = board->cnt_channel_nbr;
223 subdevice->maxdata = (1 << board->cnt_bits) - 1;
224 subdevice->insn_read = cnt_rinsn;
225 subdevice->insn_write = cnt_winsn;
227 // select 20MHz clock
228 outb(3, dev->iobase + 248);
230 // reset all counters
231 outb(0, dev->iobase);
232 outb(0, dev->iobase + 0x20);
233 outb(0, dev->iobase + 0x40);
235 printk("comedi%d: " CNT_DRIVER_NAME " attached.\n",dev->minor);
239 /*-- detach -----------------------------------------------------------------*/
241 static int cnt_detach(comedi_device *dev)
243 if (devpriv && devpriv->pcidev)
247 pci_release_regions(devpriv->pcidev);
248 pci_disable_device(devpriv->pcidev);
250 pci_dev_put(devpriv->pcidev);
252 printk("comedi%d: " CNT_DRIVER_NAME " remove\n",dev->minor);