]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/ke_counter.c
fef6ea74ce02560eeff1e7a219b6646c68547731
[karo-tx-linux.git] / drivers / staging / 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 "../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(struct comedi_device *dev, struct comedi_devconfig *it);
52 static int cnt_detach(struct comedi_device *dev);
53
54 static DEFINE_PCI_DEVICE_TABLE(cnt_pci_table) = {
55         { PCI_DEVICE(PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID) },
56         {0}
57 };
58
59 MODULE_DEVICE_TABLE(pci, cnt_pci_table);
60
61 /*-- board specification structure ------------------------------------------*/
62
63 struct cnt_board_struct {
64
65         const char *name;
66         int device_id;
67         int cnt_channel_nbr;
68         int cnt_bits;
69 };
70
71 static const struct 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(struct cnt_board_struct))
80
81 /*-- device private structure -----------------------------------------------*/
82
83 struct cnt_device_private {
84
85         struct pci_dev *pcidev;
86 };
87
88 #define devpriv ((struct cnt_device_private *)dev->private)
89
90 static struct comedi_driver cnt_driver = {
91         .driver_name = CNT_DRIVER_NAME,
92         .module = THIS_MODULE,
93         .attach = cnt_attach,
94         .detach = cnt_detach,
95 };
96
97 static int __devinit cnt_driver_pci_probe(struct pci_dev *dev,
98                                           const struct pci_device_id *ent)
99 {
100         return comedi_pci_auto_config(dev, &cnt_driver);
101 }
102
103 static void __devexit cnt_driver_pci_remove(struct pci_dev *dev)
104 {
105         comedi_pci_auto_unconfig(dev);
106 }
107
108 static struct pci_driver cnt_driver_pci_driver = {
109         .id_table = cnt_pci_table,
110         .probe = &cnt_driver_pci_probe,
111         .remove = __devexit_p(&cnt_driver_pci_remove)
112 };
113
114 static int __init cnt_driver_init_module(void)
115 {
116         int retval;
117
118         retval = comedi_driver_register(&cnt_driver);
119         if (retval < 0)
120                 return retval;
121
122         cnt_driver_pci_driver.name = (char *)cnt_driver.driver_name;
123         return pci_register_driver(&cnt_driver_pci_driver);
124 }
125
126 static void __exit cnt_driver_cleanup_module(void)
127 {
128         pci_unregister_driver(&cnt_driver_pci_driver);
129         comedi_driver_unregister(&cnt_driver);
130 }
131
132 module_init(cnt_driver_init_module);
133 module_exit(cnt_driver_cleanup_module);
134
135 /*-- counter write ----------------------------------------------------------*/
136
137 /* This should be used only for resetting the counters; maybe it is better
138    to make a special command 'reset'. */
139 static int cnt_winsn(struct comedi_device *dev,
140                      struct comedi_subdevice *s, struct comedi_insn *insn,
141                      unsigned int *data)
142 {
143         int chan = CR_CHAN(insn->chanspec);
144
145         outb((unsigned char)((data[0] >> 24) & 0xff),
146              dev->iobase + chan * 0x20 + 0x10);
147         outb((unsigned char)((data[0] >> 16) & 0xff),
148              dev->iobase + chan * 0x20 + 0x0c);
149         outb((unsigned char)((data[0] >> 8) & 0xff),
150              dev->iobase + chan * 0x20 + 0x08);
151         outb((unsigned char)((data[0] >> 0) & 0xff),
152              dev->iobase + chan * 0x20 + 0x04);
153
154         /* return the number of samples written */
155         return 1;
156 }
157
158 /*-- counter read -----------------------------------------------------------*/
159
160 static int cnt_rinsn(struct comedi_device *dev,
161                      struct comedi_subdevice *s, struct comedi_insn *insn,
162                      unsigned int *data)
163 {
164         unsigned char a0, a1, a2, a3, a4;
165         int chan = CR_CHAN(insn->chanspec);
166         int result;
167
168         a0 = inb(dev->iobase + chan * 0x20);
169         a1 = inb(dev->iobase + chan * 0x20 + 0x04);
170         a2 = inb(dev->iobase + chan * 0x20 + 0x08);
171         a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
172         a4 = inb(dev->iobase + chan * 0x20 + 0x10);
173
174         result = (a1 + (a2 * 256) + (a3 * 65536));
175         if (a4 > 0)
176                 result = result - s->maxdata;
177
178         *data = (unsigned int)result;
179
180         /* return the number of samples read */
181         return 1;
182 }
183
184 /*-- attach -----------------------------------------------------------------*/
185
186 static int cnt_attach(struct comedi_device *dev, struct comedi_devconfig *it)
187 {
188         struct comedi_subdevice *subdevice;
189         struct pci_dev *pci_device = NULL;
190         struct cnt_board_struct *board;
191         unsigned long io_base;
192         int error, i;
193
194         /* allocate device private structure */
195         error = alloc_private(dev, sizeof(struct cnt_device_private));
196         if (error < 0)
197                 return error;
198
199         /* Probe the device to determine what device in the series it is. */
200         for_each_pci_dev(pci_device) {
201                 if (pci_device->vendor == PCI_VENDOR_ID_KOLTER) {
202                         for (i = 0; i < cnt_board_nbr; i++) {
203                                 if (cnt_boards[i].device_id ==
204                                     pci_device->device) {
205                                         /* was a particular bus/slot requested? */
206                                         if ((it->options[0] != 0)
207                                             || (it->options[1] != 0)) {
208                                                 /* are we on the wrong bus/slot? */
209                                                 if (pci_device->bus->number !=
210                                                     it->options[0]
211                                                     ||
212                                                     PCI_SLOT(pci_device->devfn)
213                                                     != it->options[1]) {
214                                                         continue;
215                                                 }
216                                         }
217
218                                         dev->board_ptr = cnt_boards + i;
219                                         board =
220                                             (struct cnt_board_struct *)
221                                             dev->board_ptr;
222                                         goto found;
223                                 }
224                         }
225                 }
226         }
227         printk(KERN_WARNING
228                "comedi%d: no supported board found! (req. bus/slot: %d/%d)\n",
229                dev->minor, it->options[0], it->options[1]);
230         return -EIO;
231
232 found:
233         printk(KERN_INFO
234                "comedi%d: found %s at PCI bus %d, slot %d\n", dev->minor,
235                board->name, pci_device->bus->number,
236                PCI_SLOT(pci_device->devfn));
237         devpriv->pcidev = pci_device;
238         dev->board_name = board->name;
239
240         /* enable PCI device and request regions */
241         error = comedi_pci_enable(pci_device, CNT_DRIVER_NAME);
242         if (error < 0) {
243                 printk(KERN_WARNING "comedi%d: "
244                        "failed to enable PCI device and request regions!\n",
245                        dev->minor);
246                 return error;
247         }
248
249         /* read register base address [PCI_BASE_ADDRESS #0] */
250         io_base = pci_resource_start(pci_device, 0);
251         dev->iobase = io_base;
252
253         /* allocate the subdevice structures */
254         error = alloc_subdevices(dev, 1);
255         if (error < 0)
256                 return error;
257
258         subdevice = dev->subdevices + 0;
259         dev->read_subdev = subdevice;
260
261         subdevice->type = COMEDI_SUBD_COUNTER;
262         subdevice->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
263         subdevice->n_chan = board->cnt_channel_nbr;
264         subdevice->maxdata = (1 << board->cnt_bits) - 1;
265         subdevice->insn_read = cnt_rinsn;
266         subdevice->insn_write = cnt_winsn;
267
268         /*  select 20MHz clock */
269         outb(3, dev->iobase + 248);
270
271         /*  reset all counters */
272         outb(0, dev->iobase);
273         outb(0, dev->iobase + 0x20);
274         outb(0, dev->iobase + 0x40);
275
276         printk(KERN_INFO "comedi%d: " CNT_DRIVER_NAME " attached.\n",
277                dev->minor);
278         return 0;
279 }
280
281 /*-- detach -----------------------------------------------------------------*/
282
283 static int cnt_detach(struct comedi_device *dev)
284 {
285         if (devpriv && devpriv->pcidev) {
286                 if (dev->iobase)
287                         comedi_pci_disable(devpriv->pcidev);
288                 pci_dev_put(devpriv->pcidev);
289         }
290         printk(KERN_INFO "comedi%d: " CNT_DRIVER_NAME " remove\n",
291                dev->minor);
292         return 0;
293 }
294
295 MODULE_AUTHOR("Comedi http://www.comedi.org");
296 MODULE_DESCRIPTION("Comedi low-level driver");
297 MODULE_LICENSE("GPL");