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
28 Updated: Mon, 14 Apr 2008 15:42:42 +0100
31 Configuration Options:
32 [0] - PCI bus of device (optional)
33 [1] - PCI slot of device (optional)
34 If bus/slot is not specified, the first supported
35 PCI device found will be used.
37 This driver is a simple driver to read the counter values from
38 Kolter Electronic PCI Counter Card.
41 #include "../comedidev.h"
43 #include "comedi_pci.h"
45 #define CNT_DRIVER_NAME "ke_counter"
46 #define PCI_VENDOR_ID_KOLTER 0x1001
47 #define CNT_CARD_DEVICE_ID 0x0014
49 /*-- function prototypes ----------------------------------------------------*/
51 static int cnt_attach(struct comedi_device *dev, struct comedi_devconfig *it);
52 static int cnt_detach(struct comedi_device *dev);
54 static DEFINE_PCI_DEVICE_TABLE(cnt_pci_table) = {
55 {PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
60 MODULE_DEVICE_TABLE(pci, cnt_pci_table);
62 /*-- board specification structure ------------------------------------------*/
64 struct cnt_board_struct {
73 static const struct cnt_board_struct cnt_boards[] = {
75 .name = CNT_DRIVER_NAME,
76 .device_id = CNT_CARD_DEVICE_ID,
81 #define cnt_board_nbr (sizeof(cnt_boards)/sizeof(struct cnt_board_struct))
83 /*-- device private structure -----------------------------------------------*/
85 struct cnt_device_private {
87 struct pci_dev *pcidev;
91 #define devpriv ((struct cnt_device_private *)dev->private)
93 static struct comedi_driver cnt_driver = {
94 .driver_name = CNT_DRIVER_NAME,
95 .module = THIS_MODULE,
100 COMEDI_PCI_INITCLEANUP(cnt_driver, cnt_pci_table);
102 /*-- counter write ----------------------------------------------------------*/
104 /* This should be used only for resetting the counters; maybe it is better
105 to make a special command 'reset'. */
106 static int cnt_winsn(struct comedi_device *dev,
107 struct comedi_subdevice *s, struct comedi_insn *insn, unsigned int *data)
109 int chan = CR_CHAN(insn->chanspec);
111 outb((unsigned char)((data[0] >> 24) & 0xff),
112 dev->iobase + chan * 0x20 + 0x10);
113 outb((unsigned char)((data[0] >> 16) & 0xff),
114 dev->iobase + chan * 0x20 + 0x0c);
115 outb((unsigned char)((data[0] >> 8) & 0xff),
116 dev->iobase + chan * 0x20 + 0x08);
117 outb((unsigned char)((data[0] >> 0) & 0xff),
118 dev->iobase + chan * 0x20 + 0x04);
120 /* return the number of samples written */
124 /*-- counter read -----------------------------------------------------------*/
126 static int cnt_rinsn(struct comedi_device *dev,
127 struct comedi_subdevice *s, struct comedi_insn *insn, unsigned int *data)
129 unsigned char a0, a1, a2, a3, a4;
130 int chan = CR_CHAN(insn->chanspec);
133 a0 = inb(dev->iobase + chan * 0x20);
134 a1 = inb(dev->iobase + chan * 0x20 + 0x04);
135 a2 = inb(dev->iobase + chan * 0x20 + 0x08);
136 a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
137 a4 = inb(dev->iobase + chan * 0x20 + 0x10);
139 result = (a1 + (a2 * 256) + (a3 * 65536));
141 result = result - s->maxdata;
143 *data = (unsigned int) result;
145 /* return the number of samples read */
149 /*-- attach -----------------------------------------------------------------*/
151 static int cnt_attach(struct comedi_device *dev, struct comedi_devconfig *it)
153 struct comedi_subdevice *subdevice;
154 struct pci_dev *pci_device;
155 struct cnt_board_struct *board;
156 unsigned long io_base;
159 /* allocate device private structure */
160 error = alloc_private(dev, sizeof(struct cnt_device_private));
164 /* Probe the device to determine what device in the series it is. */
165 for (pci_device = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
168 pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_device)) {
169 if (pci_device->vendor == PCI_VENDOR_ID_KOLTER) {
170 for (i = 0; i < cnt_board_nbr; i++) {
171 if (cnt_boards[i].device_id ==
172 pci_device->device) {
173 /* was a particular bus/slot requested? */
174 if ((it->options[0] != 0)
175 || (it->options[1] != 0)) {
176 /* are we on the wrong bus/slot? */
177 if (pci_device->bus->number !=
179 || PCI_SLOT(pci_device->
186 dev->board_ptr = cnt_boards + i;
187 board = (struct cnt_board_struct *) dev->
194 printk("comedi%d: no supported board found! (req. bus/slot: %d/%d)\n",
195 dev->minor, it->options[0], it->options[1]);
199 printk("comedi%d: found %s at PCI bus %d, slot %d\n", dev->minor,
200 board->name, pci_device->bus->number,
201 PCI_SLOT(pci_device->devfn));
202 devpriv->pcidev = pci_device;
203 dev->board_name = board->name;
205 /* enable PCI device and request regions */
206 error = comedi_pci_enable(pci_device, CNT_DRIVER_NAME);
208 printk("comedi%d: failed to enable PCI device and request regions!\n", dev->minor);
212 /* read register base address [PCI_BASE_ADDRESS #0] */
213 io_base = pci_resource_start(pci_device, 0);
214 dev->iobase = io_base;
216 /* allocate the subdevice structures */
217 error = alloc_subdevices(dev, 1);
221 subdevice = dev->subdevices + 0;
222 dev->read_subdev = subdevice;
224 subdevice->type = COMEDI_SUBD_COUNTER;
225 subdevice->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
226 subdevice->n_chan = board->cnt_channel_nbr;
227 subdevice->maxdata = (1 << board->cnt_bits) - 1;
228 subdevice->insn_read = cnt_rinsn;
229 subdevice->insn_write = cnt_winsn;
231 /* select 20MHz clock */
232 outb(3, dev->iobase + 248);
234 /* reset all counters */
235 outb(0, dev->iobase);
236 outb(0, dev->iobase + 0x20);
237 outb(0, dev->iobase + 0x40);
239 printk("comedi%d: " CNT_DRIVER_NAME " attached.\n", dev->minor);
243 /*-- detach -----------------------------------------------------------------*/
245 static int cnt_detach(struct comedi_device *dev)
247 if (devpriv && devpriv->pcidev) {
249 comedi_pci_disable(devpriv->pcidev);
251 pci_dev_put(devpriv->pcidev);
253 printk("comedi%d: " CNT_DRIVER_NAME " remove\n", dev->minor);