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
42
43
44
45
46
47
48
49
50
51#include <linux/module.h>
52#include "../comedidev.h"
53
54#include "8255.h"
55
56struct subdev_8255_private {
57 unsigned long regbase;
58 int (*io)(struct comedi_device *, int, int, int, unsigned long);
59};
60
61static int subdev_8255_io(struct comedi_device *dev,
62 int dir, int port, int data, unsigned long regbase)
63{
64 if (dir) {
65 outb(data, dev->iobase + regbase + port);
66 return 0;
67 }
68 return inb(dev->iobase + regbase + port);
69}
70
71static int subdev_8255_mmio(struct comedi_device *dev,
72 int dir, int port, int data, unsigned long regbase)
73{
74 if (dir) {
75 writeb(data, dev->mmio + regbase + port);
76 return 0;
77 }
78 return readb(dev->mmio + regbase + port);
79}
80
81static int subdev_8255_insn(struct comedi_device *dev,
82 struct comedi_subdevice *s,
83 struct comedi_insn *insn,
84 unsigned int *data)
85{
86 struct subdev_8255_private *spriv = s->private;
87 unsigned long regbase = spriv->regbase;
88 unsigned int mask;
89 unsigned int v;
90
91 mask = comedi_dio_update_state(s, data);
92 if (mask) {
93 if (mask & 0xff)
94 spriv->io(dev, 1, I8255_DATA_A_REG,
95 s->state & 0xff, regbase);
96 if (mask & 0xff00)
97 spriv->io(dev, 1, I8255_DATA_B_REG,
98 (s->state >> 8) & 0xff, regbase);
99 if (mask & 0xff0000)
100 spriv->io(dev, 1, I8255_DATA_C_REG,
101 (s->state >> 16) & 0xff, regbase);
102 }
103
104 v = spriv->io(dev, 0, I8255_DATA_A_REG, 0, regbase);
105 v |= (spriv->io(dev, 0, I8255_DATA_B_REG, 0, regbase) << 8);
106 v |= (spriv->io(dev, 0, I8255_DATA_C_REG, 0, regbase) << 16);
107
108 data[1] = v;
109
110 return insn->n;
111}
112
113static void subdev_8255_do_config(struct comedi_device *dev,
114 struct comedi_subdevice *s)
115{
116 struct subdev_8255_private *spriv = s->private;
117 unsigned long regbase = spriv->regbase;
118 int config;
119
120 config = I8255_CTRL_CW;
121
122 if (!(s->io_bits & 0x0000ff))
123 config |= I8255_CTRL_A_IO;
124 if (!(s->io_bits & 0x00ff00))
125 config |= I8255_CTRL_B_IO;
126 if (!(s->io_bits & 0x0f0000))
127 config |= I8255_CTRL_C_LO_IO;
128 if (!(s->io_bits & 0xf00000))
129 config |= I8255_CTRL_C_HI_IO;
130
131 spriv->io(dev, 1, I8255_CTRL_REG, config, regbase);
132}
133
134static int subdev_8255_insn_config(struct comedi_device *dev,
135 struct comedi_subdevice *s,
136 struct comedi_insn *insn,
137 unsigned int *data)
138{
139 unsigned int chan = CR_CHAN(insn->chanspec);
140 unsigned int mask;
141 int ret;
142
143 if (chan < 8)
144 mask = 0x0000ff;
145 else if (chan < 16)
146 mask = 0x00ff00;
147 else if (chan < 20)
148 mask = 0x0f0000;
149 else
150 mask = 0xf00000;
151
152 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
153 if (ret)
154 return ret;
155
156 subdev_8255_do_config(dev, s);
157
158 return insn->n;
159}
160
161static int __subdev_8255_init(struct comedi_device *dev,
162 struct comedi_subdevice *s,
163 int (*io)(struct comedi_device *,
164 int, int, int, unsigned long),
165 unsigned long regbase,
166 bool is_mmio)
167{
168 struct subdev_8255_private *spriv;
169
170 spriv = comedi_alloc_spriv(s, sizeof(*spriv));
171 if (!spriv)
172 return -ENOMEM;
173
174 if (io)
175 spriv->io = io;
176 else if (is_mmio)
177 spriv->io = subdev_8255_mmio;
178 else
179 spriv->io = subdev_8255_io;
180 spriv->regbase = regbase;
181
182 s->type = COMEDI_SUBD_DIO;
183 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
184 s->n_chan = 24;
185 s->range_table = &range_digital;
186 s->maxdata = 1;
187 s->insn_bits = subdev_8255_insn;
188 s->insn_config = subdev_8255_insn_config;
189
190 subdev_8255_do_config(dev, s);
191
192 return 0;
193}
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222int subdev_8255_init(struct comedi_device *dev, struct comedi_subdevice *s,
223 int (*io)(struct comedi_device *,
224 int, int, int, unsigned long),
225 unsigned long regbase)
226{
227 return __subdev_8255_init(dev, s, io, regbase, false);
228}
229EXPORT_SYMBOL_GPL(subdev_8255_init);
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258int subdev_8255_mm_init(struct comedi_device *dev, struct comedi_subdevice *s,
259 int (*io)(struct comedi_device *,
260 int, int, int, unsigned long),
261 unsigned long regbase)
262{
263 return __subdev_8255_init(dev, s, io, regbase, true);
264}
265EXPORT_SYMBOL_GPL(subdev_8255_mm_init);
266
267
268
269
270
271static int dev_8255_attach(struct comedi_device *dev,
272 struct comedi_devconfig *it)
273{
274 struct comedi_subdevice *s;
275 unsigned long iobase;
276 int ret;
277 int i;
278
279 for (i = 0; i < COMEDI_NDEVCONFOPTS; i++) {
280 iobase = it->options[i];
281 if (!iobase)
282 break;
283 }
284 if (i == 0) {
285 dev_warn(dev->class_dev, "no devices specified\n");
286 return -EINVAL;
287 }
288
289 ret = comedi_alloc_subdevices(dev, i);
290 if (ret)
291 return ret;
292
293 for (i = 0; i < dev->n_subdevices; i++) {
294 s = &dev->subdevices[i];
295 iobase = it->options[i];
296
297
298
299
300
301
302
303
304 ret = __comedi_request_region(dev, iobase, I8255_SIZE);
305 if (ret) {
306 s->type = COMEDI_SUBD_UNUSED;
307 } else {
308 ret = subdev_8255_init(dev, s, NULL, iobase);
309 if (ret)
310 return ret;
311 }
312 }
313
314 return 0;
315}
316
317static void dev_8255_detach(struct comedi_device *dev)
318{
319 struct comedi_subdevice *s;
320 struct subdev_8255_private *spriv;
321 int i;
322
323 for (i = 0; i < dev->n_subdevices; i++) {
324 s = &dev->subdevices[i];
325 if (s->type != COMEDI_SUBD_UNUSED) {
326 spriv = s->private;
327 release_region(spriv->regbase, I8255_SIZE);
328 }
329 }
330}
331
332static struct comedi_driver dev_8255_driver = {
333 .driver_name = "8255",
334 .module = THIS_MODULE,
335 .attach = dev_8255_attach,
336 .detach = dev_8255_detach,
337};
338module_comedi_driver(dev_8255_driver);
339
340MODULE_AUTHOR("Comedi http://www.comedi.org");
341MODULE_DESCRIPTION("Comedi low-level driver");
342MODULE_LICENSE("GPL");
343