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