Fixed an issue with jiffies vs miliseconds for the timeout value
[comedi.git] / comedi / drivers / ke_counter.c
1 /*
2     comedi/drivers/ke_counter.c
3     Comedi driver for Kolter-Electronic PCI Counter 1 Card
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7
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.
12
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.
17
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.
21
22 */
23 /*
24 Driver: ke_counter
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
29 Status: tested
30
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.
36
37 This driver is a simple driver to read the counter values from
38 Kolter Electronic PCI Counter Card.
39 */
40
41 #include <linux/comedidev.h>
42
43 #include "comedi_pci.h"
44
45 #define CNT_DRIVER_NAME         "ke_counter"
46 #define PCI_VENDOR_ID_KOLTER    0x1001
47 #define CNT_CARD_DEVICE_ID      0x0014
48
49 /*-- function prototypes ----------------------------------------------------*/
50
51 static int cnt_attach(comedi_device * dev, comedi_devconfig * it);
52 static int cnt_detach(comedi_device * dev);
53
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,
56                 0},
57         {0}
58 };
59
60 MODULE_DEVICE_TABLE(pci, cnt_pci_table);
61
62 /*-- board specification structure ------------------------------------------*/
63
64 typedef struct {
65         const char *name;
66         int device_id;
67         int cnt_channel_nbr;
68         int cnt_bits;
69 } cnt_board_struct;
70
71 static const cnt_board_struct cnt_boards[] = {
72         {
73               name:     CNT_DRIVER_NAME,
74               device_id:CNT_CARD_DEVICE_ID,
75               cnt_channel_nbr:3,
76       cnt_bits:24}
77 };
78
79 #define cnt_board_nbr (sizeof(cnt_boards)/sizeof(cnt_board_struct))
80
81 /*-- device private structure -----------------------------------------------*/
82
83 typedef struct {
84         struct pci_dev *pcidev;
85 } cnt_device_private;
86
87 #define devpriv ((cnt_device_private *)dev->private)
88
89 static comedi_driver cnt_driver = {
90       driver_name:CNT_DRIVER_NAME,
91       module:THIS_MODULE,
92       attach:cnt_attach,
93       detach:cnt_detach,
94 };
95
96 COMEDI_PCI_INITCLEANUP(cnt_driver, cnt_pci_table);
97
98 /*-- counter write ----------------------------------------------------------*/
99
100 /* This should be used only for resetting the counters; maybe it is better
101    to make a special command 'reset'. */
102 static int cnt_winsn(comedi_device * dev,
103         comedi_subdevice * s, comedi_insn * insn, lsampl_t * data)
104 {
105         int chan = CR_CHAN(insn->chanspec);
106
107         outb((unsigned char)((data[0] >> 24) & 0xff),
108                 dev->iobase + chan * 0x20 + 0x10);
109         outb((unsigned char)((data[0] >> 16) & 0xff),
110                 dev->iobase + chan * 0x20 + 0x0c);
111         outb((unsigned char)((data[0] >> 8) & 0xff),
112                 dev->iobase + chan * 0x20 + 0x08);
113         outb((unsigned char)((data[0] >> 0) & 0xff),
114                 dev->iobase + chan * 0x20 + 0x04);
115
116         /* return the number of samples written */
117         return 1;
118 }
119
120 /*-- counter read -----------------------------------------------------------*/
121
122 static int cnt_rinsn(comedi_device * dev,
123         comedi_subdevice * s, comedi_insn * insn, lsampl_t * data)
124 {
125         unsigned char a0, a1, a2, a3, a4;
126         int chan = CR_CHAN(insn->chanspec);
127         int result;
128
129         a0 = inb(dev->iobase + chan * 0x20);
130         a1 = inb(dev->iobase + chan * 0x20 + 0x04);
131         a2 = inb(dev->iobase + chan * 0x20 + 0x08);
132         a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
133         a4 = inb(dev->iobase + chan * 0x20 + 0x10);
134
135         result = (a1 + (a2 * 256) + (a3 * 65536));
136         if (a4 > 0)
137                 result = result - s->maxdata;
138
139         *data = (lsampl_t) result;
140
141         /* return the number of samples read */
142         return 1;
143 }
144
145 /*-- attach -----------------------------------------------------------------*/
146
147 static int cnt_attach(comedi_device * dev, comedi_devconfig * it)
148 {
149         comedi_subdevice *subdevice;
150         struct pci_dev *pci_device;
151         cnt_board_struct *board;
152         unsigned long io_base;
153         int error, i;
154
155         /* allocate device private structure */
156         if ((error = alloc_private(dev, sizeof(cnt_device_private))) < 0) {
157                 return error;
158         }
159
160         /* Probe the device to determine what device in the series it is. */
161         for (pci_device = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
162                 pci_device != NULL;
163                 pci_device =
164                 pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_device)) {
165                 if (pci_device->vendor == PCI_VENDOR_ID_KOLTER) {
166                         for (i = 0; i < cnt_board_nbr; i++) {
167                                 if (cnt_boards[i].device_id ==
168                                         pci_device->device) {
169                                         /* was a particular bus/slot requested? */
170                                         if ((it->options[0] != 0)
171                                                 || (it->options[1] != 0)) {
172                                                 /* are we on the wrong bus/slot? */
173                                                 if (pci_device->bus->number !=
174                                                         it->options[0]
175                                                         || PCI_SLOT(pci_device->
176                                                                 devfn) !=
177                                                         it->options[1]) {
178                                                         continue;
179                                                 }
180                                         }
181
182                                         dev->board_ptr = cnt_boards + i;
183                                         board = (cnt_board_struct *) dev->
184                                                 board_ptr;
185                                         goto found;
186                                 }
187                         }
188                 }
189         }
190         printk("comedi%d: no supported board found! (req. bus/slot: %d/%d)\n",
191                 dev->minor, it->options[0], it->options[1]);
192         return -EIO;
193
194       found:
195         printk("comedi%d: found %s at PCI bus %d, slot %d\n", dev->minor,
196                 board->name, pci_device->bus->number,
197                 PCI_SLOT(pci_device->devfn));
198         devpriv->pcidev = pci_device;
199         dev->board_name = board->name;
200
201         /* enable PCI device and request regions */
202         if ((error = comedi_pci_enable(pci_device, CNT_DRIVER_NAME)) < 0) {
203                 printk("comedi%d: failed to enable PCI device and request regions!\n", dev->minor);
204                 return error;
205         }
206
207         /* read register base address [PCI_BASE_ADDRESS #0] */
208         io_base = pci_resource_start(pci_device, 0);
209         dev->iobase = io_base;
210
211         /* allocate the subdevice structures */
212         if ((error = alloc_subdevices(dev, 1)) < 0) {
213                 return error;
214         }
215
216         subdevice = dev->subdevices + 0;
217         dev->read_subdev = subdevice;
218
219         subdevice->type = COMEDI_SUBD_COUNTER;
220         subdevice->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
221         subdevice->n_chan = board->cnt_channel_nbr;
222         subdevice->maxdata = (1 << board->cnt_bits) - 1;
223         subdevice->insn_read = cnt_rinsn;
224         subdevice->insn_write = cnt_winsn;
225
226         // select 20MHz clock
227         outb(3, dev->iobase + 248);
228
229         // reset all counters
230         outb(0, dev->iobase);
231         outb(0, dev->iobase + 0x20);
232         outb(0, dev->iobase + 0x40);
233
234         printk("comedi%d: " CNT_DRIVER_NAME " attached.\n", dev->minor);
235         return 0;
236 }
237
238 /*-- detach -----------------------------------------------------------------*/
239
240 static int cnt_detach(comedi_device * dev)
241 {
242         if (devpriv && devpriv->pcidev) {
243                 if (dev->iobase) {
244                         comedi_pci_disable(devpriv->pcidev);
245                 }
246                 pci_dev_put(devpriv->pcidev);
247         }
248         printk("comedi%d: " CNT_DRIVER_NAME " remove\n", dev->minor);
249         return 0;
250 }