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: not applicable, uses PCI auto config
33 This driver is a simple driver to read the counter values from
34 Kolter Electronic PCI Counter Card.
37 #include "../comedidev.h"
39 #define CNT_DRIVER_NAME "ke_counter"
40 #define PCI_VENDOR_ID_KOLTER 0x1001
41 #define CNT_CARD_DEVICE_ID 0x0014
43 /*-- board specification structure ------------------------------------------*/
45 struct cnt_board_struct {
53 static const struct cnt_board_struct cnt_boards[] = {
55 .name = CNT_DRIVER_NAME,
56 .device_id = CNT_CARD_DEVICE_ID,
61 /*-- counter write ----------------------------------------------------------*/
63 /* This should be used only for resetting the counters; maybe it is better
64 to make a special command 'reset'. */
65 static int cnt_winsn(struct comedi_device *dev,
66 struct comedi_subdevice *s, struct comedi_insn *insn,
69 int chan = CR_CHAN(insn->chanspec);
71 outb((unsigned char)((data[0] >> 24) & 0xff),
72 dev->iobase + chan * 0x20 + 0x10);
73 outb((unsigned char)((data[0] >> 16) & 0xff),
74 dev->iobase + chan * 0x20 + 0x0c);
75 outb((unsigned char)((data[0] >> 8) & 0xff),
76 dev->iobase + chan * 0x20 + 0x08);
77 outb((unsigned char)((data[0] >> 0) & 0xff),
78 dev->iobase + chan * 0x20 + 0x04);
80 /* return the number of samples written */
84 /*-- counter read -----------------------------------------------------------*/
86 static int cnt_rinsn(struct comedi_device *dev,
87 struct comedi_subdevice *s, struct comedi_insn *insn,
90 unsigned char a0, a1, a2, a3, a4;
91 int chan = CR_CHAN(insn->chanspec);
94 a0 = inb(dev->iobase + chan * 0x20);
95 a1 = inb(dev->iobase + chan * 0x20 + 0x04);
96 a2 = inb(dev->iobase + chan * 0x20 + 0x08);
97 a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
98 a4 = inb(dev->iobase + chan * 0x20 + 0x10);
100 result = (a1 + (a2 * 256) + (a3 * 65536));
102 result = result - s->maxdata;
104 *data = (unsigned int)result;
106 /* return the number of samples read */
110 static const void *cnt_find_boardinfo(struct comedi_device *dev,
111 struct pci_dev *pcidev)
113 const struct cnt_board_struct *board;
116 for (i = 0; i < ARRAY_SIZE(cnt_boards); i++) {
117 board = &cnt_boards[i];
118 if (board->device_id == pcidev->device)
124 static int cnt_attach_pci(struct comedi_device *dev,
125 struct pci_dev *pcidev)
127 const struct cnt_board_struct *board;
128 struct comedi_subdevice *s;
131 comedi_set_hw_dev(dev, &pcidev->dev);
133 board = cnt_find_boardinfo(dev, pcidev);
136 dev->board_ptr = board;
137 dev->board_name = board->name;
139 ret = comedi_pci_enable(pcidev, dev->board_name);
142 dev->iobase = pci_resource_start(pcidev, 0);
144 ret = comedi_alloc_subdevices(dev, 1);
148 s = &dev->subdevices[0];
149 dev->read_subdev = s;
151 s->type = COMEDI_SUBD_COUNTER;
152 s->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
153 s->n_chan = board->cnt_channel_nbr;
154 s->maxdata = (1 << board->cnt_bits) - 1;
155 s->insn_read = cnt_rinsn;
156 s->insn_write = cnt_winsn;
158 /* select 20MHz clock */
159 outb(3, dev->iobase + 248);
161 /* reset all counters */
162 outb(0, dev->iobase);
163 outb(0, dev->iobase + 0x20);
164 outb(0, dev->iobase + 0x40);
166 dev_info(dev->class_dev, "%s: %s attached\n",
167 dev->driver->driver_name, dev->board_name);
172 static void cnt_detach(struct comedi_device *dev)
174 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
178 comedi_pci_disable(pcidev);
182 static struct comedi_driver ke_counter_driver = {
183 .driver_name = "ke_counter",
184 .module = THIS_MODULE,
185 .attach_pci = cnt_attach_pci,
186 .detach = cnt_detach,
189 static int __devinit ke_counter_pci_probe(struct pci_dev *dev,
190 const struct pci_device_id *ent)
192 return comedi_pci_auto_config(dev, &ke_counter_driver);
195 static void __devexit ke_counter_pci_remove(struct pci_dev *dev)
197 comedi_pci_auto_unconfig(dev);
200 static DEFINE_PCI_DEVICE_TABLE(ke_counter_pci_table) = {
201 { PCI_DEVICE(PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID) },
204 MODULE_DEVICE_TABLE(pci, ke_counter_pci_table);
206 static struct pci_driver ke_counter_pci_driver = {
207 .name = "ke_counter",
208 .id_table = ke_counter_pci_table,
209 .probe = ke_counter_pci_probe,
210 .remove = __devexit_p(ke_counter_pci_remove),
212 module_comedi_pci_driver(ke_counter_driver, ke_counter_pci_driver);
214 MODULE_AUTHOR("Comedi http://www.comedi.org");
215 MODULE_DESCRIPTION("Comedi low-level driver");
216 MODULE_LICENSE("GPL");