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#include <linux/module.h>
31#include <linux/pci.h>
32
33#include "../comedidev.h"
34
35
36
37
38#define KE_RESET_REG(x) (0x00 + ((x) * 0x20))
39#define KE_LATCH_REG(x) (0x00 + ((x) * 0x20))
40#define KE_LSB_REG(x) (0x04 + ((x) * 0x20))
41#define KE_MID_REG(x) (0x08 + ((x) * 0x20))
42#define KE_MSB_REG(x) (0x0c + ((x) * 0x20))
43#define KE_SIGN_REG(x) (0x10 + ((x) * 0x20))
44#define KE_OSC_SEL_REG 0xf8
45#define KE_OSC_SEL_EXT (1 << 0)
46#define KE_OSC_SEL_4MHZ (2 << 0)
47#define KE_OSC_SEL_20MHZ (3 << 0)
48#define KE_DO_REG 0xfc
49
50static int ke_counter_insn_write(struct comedi_device *dev,
51 struct comedi_subdevice *s,
52 struct comedi_insn *insn,
53 unsigned int *data)
54{
55 unsigned int chan = CR_CHAN(insn->chanspec);
56 unsigned int val;
57 int i;
58
59 for (i = 0; i < insn->n; i++) {
60 val = data[0];
61
62
63 outb((val >> 24) & 0xff, dev->iobase + KE_SIGN_REG(chan));
64 outb((val >> 16) & 0xff, dev->iobase + KE_MSB_REG(chan));
65 outb((val >> 8) & 0xff, dev->iobase + KE_MID_REG(chan));
66 outb((val >> 0) & 0xff, dev->iobase + KE_LSB_REG(chan));
67 }
68
69 return insn->n;
70}
71
72static int ke_counter_insn_read(struct comedi_device *dev,
73 struct comedi_subdevice *s,
74 struct comedi_insn *insn,
75 unsigned int *data)
76{
77 unsigned int chan = CR_CHAN(insn->chanspec);
78 unsigned int val;
79 int i;
80
81 for (i = 0; i < insn->n; i++) {
82
83 inb(dev->iobase + KE_LATCH_REG(chan));
84
85 val = inb(dev->iobase + KE_LSB_REG(chan));
86 val |= (inb(dev->iobase + KE_MID_REG(chan)) << 8);
87 val |= (inb(dev->iobase + KE_MSB_REG(chan)) << 16);
88 val |= (inb(dev->iobase + KE_SIGN_REG(chan)) << 24);
89
90 data[i] = val;
91 }
92
93 return insn->n;
94}
95
96static int ke_counter_do_insn_bits(struct comedi_device *dev,
97 struct comedi_subdevice *s,
98 struct comedi_insn *insn,
99 unsigned int *data)
100{
101 if (comedi_dio_update_state(s, data))
102 outb(s->state, dev->iobase + KE_DO_REG);
103
104 data[1] = s->state;
105
106 return insn->n;
107}
108
109static int ke_counter_auto_attach(struct comedi_device *dev,
110 unsigned long context_unused)
111{
112 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
113 struct comedi_subdevice *s;
114 int ret;
115
116 ret = comedi_pci_enable(dev);
117 if (ret)
118 return ret;
119 dev->iobase = pci_resource_start(pcidev, 0);
120
121 ret = comedi_alloc_subdevices(dev, 2);
122 if (ret)
123 return ret;
124
125 s = &dev->subdevices[0];
126 s->type = COMEDI_SUBD_COUNTER;
127 s->subdev_flags = SDF_READABLE;
128 s->n_chan = 3;
129 s->maxdata = 0x01ffffff;
130 s->range_table = &range_unknown;
131 s->insn_read = ke_counter_insn_read;
132 s->insn_write = ke_counter_insn_write;
133
134 s = &dev->subdevices[1];
135 s->type = COMEDI_SUBD_DO;
136 s->subdev_flags = SDF_WRITABLE;
137 s->n_chan = 3;
138 s->maxdata = 1;
139 s->range_table = &range_digital;
140 s->insn_bits = ke_counter_do_insn_bits;
141
142 outb(KE_OSC_SEL_20MHZ, dev->iobase + KE_OSC_SEL_REG);
143
144 outb(0, dev->iobase + KE_RESET_REG(0));
145 outb(0, dev->iobase + KE_RESET_REG(1));
146 outb(0, dev->iobase + KE_RESET_REG(2));
147
148 return 0;
149}
150
151static struct comedi_driver ke_counter_driver = {
152 .driver_name = "ke_counter",
153 .module = THIS_MODULE,
154 .auto_attach = ke_counter_auto_attach,
155 .detach = comedi_pci_disable,
156};
157
158static int ke_counter_pci_probe(struct pci_dev *dev,
159 const struct pci_device_id *id)
160{
161 return comedi_pci_auto_config(dev, &ke_counter_driver,
162 id->driver_data);
163}
164
165static const struct pci_device_id ke_counter_pci_table[] = {
166 { PCI_DEVICE(PCI_VENDOR_ID_KOLTER, 0x0014) },
167 { 0 }
168};
169MODULE_DEVICE_TABLE(pci, ke_counter_pci_table);
170
171static struct pci_driver ke_counter_pci_driver = {
172 .name = "ke_counter",
173 .id_table = ke_counter_pci_table,
174 .probe = ke_counter_pci_probe,
175 .remove = comedi_pci_auto_unconfig,
176};
177module_comedi_pci_driver(ke_counter_driver, ke_counter_pci_driver);
178
179MODULE_AUTHOR("Comedi http://www.comedi.org");
180MODULE_DESCRIPTION("Comedi driver for Kolter Electronic Counter Card");
181MODULE_LICENSE("GPL");
182