1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
50
51static int cnt_attach(struct comedi_device *dev, struct comedi_devconfig *it);
52static int cnt_detach(struct comedi_device *dev);
53
54static DEFINE_PCI_DEVICE_TABLE(cnt_pci_table) = {
55 {
56 PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID, PCI_ANY_ID,
57 PCI_ANY_ID, 0, 0, 0}, {
58 0}
59};
60
61MODULE_DEVICE_TABLE(pci, cnt_pci_table);
62
63
64
65struct cnt_board_struct {
66
67 const char *name;
68 int device_id;
69 int cnt_channel_nbr;
70 int cnt_bits;
71};
72
73static const struct cnt_board_struct cnt_boards[] = {
74 {
75 .name = CNT_DRIVER_NAME,
76 .device_id = CNT_CARD_DEVICE_ID,
77 .cnt_channel_nbr = 3,
78 .cnt_bits = 24}
79};
80
81#define cnt_board_nbr (sizeof(cnt_boards)/sizeof(struct cnt_board_struct))
82
83
84
85struct cnt_device_private {
86
87 struct pci_dev *pcidev;
88};
89
90#define devpriv ((struct cnt_device_private *)dev->private)
91
92static struct comedi_driver cnt_driver = {
93 .driver_name = CNT_DRIVER_NAME,
94 .module = THIS_MODULE,
95 .attach = cnt_attach,
96 .detach = cnt_detach,
97};
98
99COMEDI_PCI_INITCLEANUP(cnt_driver, cnt_pci_table);
100
101
102
103
104
105static int cnt_winsn(struct comedi_device *dev,
106 struct comedi_subdevice *s, struct comedi_insn *insn,
107 unsigned int *data)
108{
109 int chan = CR_CHAN(insn->chanspec);
110
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);
119
120
121 return 1;
122}
123
124
125
126static int cnt_rinsn(struct comedi_device *dev,
127 struct comedi_subdevice *s, struct comedi_insn *insn,
128 unsigned int *data)
129{
130 unsigned char a0, a1, a2, a3, a4;
131 int chan = CR_CHAN(insn->chanspec);
132 int result;
133
134 a0 = inb(dev->iobase + chan * 0x20);
135 a1 = inb(dev->iobase + chan * 0x20 + 0x04);
136 a2 = inb(dev->iobase + chan * 0x20 + 0x08);
137 a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
138 a4 = inb(dev->iobase + chan * 0x20 + 0x10);
139
140 result = (a1 + (a2 * 256) + (a3 * 65536));
141 if (a4 > 0)
142 result = result - s->maxdata;
143
144 *data = (unsigned int)result;
145
146
147 return 1;
148}
149
150
151
152static int cnt_attach(struct comedi_device *dev, struct comedi_devconfig *it)
153{
154 struct comedi_subdevice *subdevice;
155 struct pci_dev *pci_device;
156 struct cnt_board_struct *board;
157 unsigned long io_base;
158 int error, i;
159
160
161 error = alloc_private(dev, sizeof(struct cnt_device_private));
162 if (error < 0)
163 return error;
164
165
166 for (pci_device = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
167 pci_device != NULL;
168 pci_device = 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
174 if ((it->options[0] != 0)
175 || (it->options[1] != 0)) {
176
177 if (pci_device->bus->number !=
178 it->options[0]
179 ||
180 PCI_SLOT(pci_device->devfn)
181 != it->options[1]) {
182 continue;
183 }
184 }
185
186 dev->board_ptr = cnt_boards + i;
187 board =
188 (struct cnt_board_struct *)
189 dev->board_ptr;
190 goto found;
191 }
192 }
193 }
194 }
195 printk("comedi%d: no supported board found! (req. bus/slot: %d/%d)\n",
196 dev->minor, it->options[0], it->options[1]);
197 return -EIO;
198
199found:
200 printk("comedi%d: found %s at PCI bus %d, slot %d\n", dev->minor,
201 board->name, pci_device->bus->number,
202 PCI_SLOT(pci_device->devfn));
203 devpriv->pcidev = pci_device;
204 dev->board_name = board->name;
205
206
207 error = comedi_pci_enable(pci_device, CNT_DRIVER_NAME);
208 if (error < 0) {
209 printk
210 ("comedi%d: failed to enable PCI device and request regions!\n",
211 dev->minor);
212 return error;
213 }
214
215
216 io_base = pci_resource_start(pci_device, 0);
217 dev->iobase = io_base;
218
219
220 error = alloc_subdevices(dev, 1);
221 if (error < 0)
222 return error;
223
224 subdevice = dev->subdevices + 0;
225 dev->read_subdev = subdevice;
226
227 subdevice->type = COMEDI_SUBD_COUNTER;
228 subdevice->subdev_flags = SDF_READABLE ;
229 subdevice->n_chan = board->cnt_channel_nbr;
230 subdevice->maxdata = (1 << board->cnt_bits) - 1;
231 subdevice->insn_read = cnt_rinsn;
232 subdevice->insn_write = cnt_winsn;
233
234
235 outb(3, dev->iobase + 248);
236
237
238 outb(0, dev->iobase);
239 outb(0, dev->iobase + 0x20);
240 outb(0, dev->iobase + 0x40);
241
242 printk("comedi%d: " CNT_DRIVER_NAME " attached.\n", dev->minor);
243 return 0;
244}
245
246
247
248static int cnt_detach(struct comedi_device *dev)
249{
250 if (devpriv && devpriv->pcidev) {
251 if (dev->iobase) {
252 comedi_pci_disable(devpriv->pcidev);
253 }
254 pci_dev_put(devpriv->pcidev);
255 }
256 printk("comedi%d: " CNT_DRIVER_NAME " remove\n", dev->minor);
257 return 0;
258}
259