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#include "../comedidev.h"
46#include <linux/ioport.h>
47#include <linux/slab.h>
48
49#define DRIVER_NAME "unioxx5"
50#define UNIOXX5_SIZE 0x10
51#define UNIOXX5_SUBDEV_BASE 0xA000
52#define UNIOXX5_SUBDEV_ODDS 0x400
53
54
55#define MODULE_DIGITAL 0
56#define MODULE_OUTPUT_MASK 0x80
57
58
59#define UNIOXX5_NUM_OF_CHANS 24
60
61
62#define TxBE 0x10
63#define RxCA 0x20
64#define Rx2CA 0x40
65#define Rx4CA 0x80
66
67
68#define Rx2CA_ERR_MASK 0x04
69#define Rx4CA_ERR_MASK 0x08
70
71
72#define ALL_2_INPUT 0
73#define ALL_2_OUTPUT 1
74
75
76struct unioxx5_subd_priv {
77 int usp_iobase;
78 unsigned char usp_module_type[12];
79 unsigned char usp_extra_data[12][4];
80 unsigned char usp_prev_wr_val[3];
81 unsigned char usp_prev_cn_val[3];
82};
83
84static int unioxx5_attach(struct comedi_device *dev,
85 struct comedi_devconfig *it);
86static int unioxx5_subdev_write(struct comedi_device *dev,
87 struct comedi_subdevice *subdev,
88 struct comedi_insn *insn, unsigned int *data);
89static int unioxx5_subdev_read(struct comedi_device *dev,
90 struct comedi_subdevice *subdev,
91 struct comedi_insn *insn, unsigned int *data);
92static int unioxx5_insn_config(struct comedi_device *dev,
93 struct comedi_subdevice *subdev,
94 struct comedi_insn *insn, unsigned int *data);
95static int unioxx5_detach(struct comedi_device *dev);
96static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
97 int subdev_iobase, int minor);
98static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
99 unsigned int *data, int channel, int minor);
100static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
101 unsigned int *data, int channel, int minor);
102
103static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
104 unsigned int *data, int channel, int minor);
105static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
106 unsigned int *data, int channel, int minor);
107static int __unioxx5_define_chan_offset(int chan_num);
108static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel);
109
110static struct comedi_driver unioxx5_driver = {
111 .driver_name = DRIVER_NAME,
112 .module = THIS_MODULE,
113 .attach = unioxx5_attach,
114 .detach = unioxx5_detach
115};
116
117static int __init unioxx5_driver_init_module(void)
118{
119 return comedi_driver_register(&unioxx5_driver);
120}
121
122static void __exit unioxx5_driver_cleanup_module(void)
123{
124 comedi_driver_unregister(&unioxx5_driver);
125}
126
127module_init(unioxx5_driver_init_module);
128module_exit(unioxx5_driver_cleanup_module);
129
130static int unioxx5_attach(struct comedi_device *dev,
131 struct comedi_devconfig *it)
132{
133 int iobase, i, n_subd;
134 int id, num, ba;
135
136 iobase = it->options[0];
137
138 dev->board_name = DRIVER_NAME;
139 dev->iobase = iobase;
140 iobase += UNIOXX5_SUBDEV_BASE;
141
142
143 for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
144 id = inb(ba + 0xE);
145 num = inb(ba + 0xF);
146
147 if (id != 'g' || num != 1)
148 continue;
149
150 n_subd++;
151 }
152
153
154 if (n_subd < 2) {
155 printk(KERN_ERR
156 "your card must has at least 2 'g01' subdevices\n");
157 return -1;
158 }
159
160 if (alloc_subdevices(dev, n_subd) < 0) {
161 printk(KERN_ERR "out of memory\n");
162 return -ENOMEM;
163 }
164
165
166 for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
167 if (__unioxx5_subdev_init(&dev->subdevices[i], iobase,
168 dev->minor) < 0)
169 return -1;
170 }
171
172 printk("attached\n");
173 return 0;
174}
175
176static int unioxx5_subdev_read(struct comedi_device *dev,
177 struct comedi_subdevice *subdev,
178 struct comedi_insn *insn, unsigned int *data)
179{
180 struct unioxx5_subd_priv *usp = subdev->private;
181 int channel, type;
182
183 channel = CR_CHAN(insn->chanspec);
184 type = usp->usp_module_type[channel / 2];
185
186 if (type == MODULE_DIGITAL) {
187 if (!__unioxx5_digital_read(usp, data, channel, dev->minor))
188 return -1;
189 } else {
190 if (!__unioxx5_analog_read(usp, data, channel, dev->minor))
191 return -1;
192 }
193
194 return 1;
195}
196
197static int unioxx5_subdev_write(struct comedi_device *dev,
198 struct comedi_subdevice *subdev,
199 struct comedi_insn *insn, unsigned int *data)
200{
201 struct unioxx5_subd_priv *usp = subdev->private;
202 int channel, type;
203
204 channel = CR_CHAN(insn->chanspec);
205 type = usp->usp_module_type[channel / 2];
206
207 if (type == MODULE_DIGITAL) {
208 if (!__unioxx5_digital_write(usp, data, channel, dev->minor))
209 return -1;
210 } else {
211 if (!__unioxx5_analog_write(usp, data, channel, dev->minor))
212 return -1;
213 }
214
215 return 1;
216}
217
218
219static int unioxx5_insn_config(struct comedi_device *dev,
220 struct comedi_subdevice *subdev,
221 struct comedi_insn *insn, unsigned int *data)
222{
223 int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
224 struct unioxx5_subd_priv *usp = subdev->private;
225 int mask = 1 << (channel & 0x07);
226
227 type = usp->usp_module_type[channel / 2];
228
229 if (type != MODULE_DIGITAL) {
230 printk(KERN_ERR
231 "comedi%d: channel configuration accessible only for digital modules\n",
232 dev->minor);
233 return -1;
234 }
235
236 channel_offset = __unioxx5_define_chan_offset(channel);
237 if (channel_offset < 0) {
238 printk(KERN_ERR
239 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
240 dev->minor, channel);
241 return -1;
242 }
243
244
245 flags = usp->usp_prev_cn_val[channel_offset - 1];
246
247 switch (*data) {
248 case COMEDI_INPUT:
249 flags &= ~mask;
250 break;
251 case COMEDI_OUTPUT:
252 flags |= mask;
253 break;
254 default:
255 printk(KERN_ERR "comedi%d: unknown flag\n", dev->minor);
256 return -1;
257 }
258
259
260
261
262
263 outb(1, usp->usp_iobase + 0);
264 outb(flags, usp->usp_iobase + channel_offset);
265 outb(0, usp->usp_iobase + 0);
266 usp->usp_prev_cn_val[channel_offset - 1] = flags;
267
268 return 0;
269}
270
271static int unioxx5_detach(struct comedi_device *dev)
272{
273 int i;
274 struct comedi_subdevice *subdev;
275 struct unioxx5_subd_priv *usp;
276
277 for (i = 0; i < dev->n_subdevices; i++) {
278 subdev = &dev->subdevices[i];
279 usp = subdev->private;
280 release_region(usp->usp_iobase, UNIOXX5_SIZE);
281 kfree(subdev->private);
282 }
283
284 return 0;
285}
286
287
288static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
289 int subdev_iobase, int minor)
290{
291 struct unioxx5_subd_priv *usp;
292 int i, to, ndef_flag = 0;
293
294 if (!request_region(subdev_iobase, UNIOXX5_SIZE, DRIVER_NAME)) {
295 printk(KERN_ERR "comedi%d: I/O port conflict\n", minor);
296 return -EIO;
297 }
298
299 usp = kzalloc(sizeof(*usp), GFP_KERNEL);
300
301 if (usp == NULL) {
302 printk(KERN_ERR "comedi%d: erorr! --> out of memory!\n", minor);
303 return -1;
304 }
305
306 usp->usp_iobase = subdev_iobase;
307 printk("comedi%d: |", minor);
308
309
310 for (i = 0; i < 12; i++) {
311 to = 10000;
312
313 __unioxx5_analog_config(usp, i * 2);
314 outb(i + 1, subdev_iobase + 5);
315 outb('H', subdev_iobase + 6);
316 while (!(inb(subdev_iobase + 0) & TxBE))
317 ;
318 outb(0, subdev_iobase + 6);
319
320
321 while (!(inb(subdev_iobase + 0) & Rx2CA)) {
322 if (--to <= 0) {
323 ndef_flag = 1;
324 break;
325 }
326 }
327
328 if (ndef_flag) {
329 usp->usp_module_type[i] = 0;
330 ndef_flag = 0;
331 } else
332 usp->usp_module_type[i] = inb(subdev_iobase + 6);
333
334 printk(" [%d] 0x%02x |", i, usp->usp_module_type[i]);
335 udelay(1);
336 }
337
338 printk("\n");
339
340
341 subdev->type = COMEDI_SUBD_DIO;
342 subdev->private = usp;
343 subdev->subdev_flags = SDF_READABLE | SDF_WRITABLE;
344 subdev->n_chan = UNIOXX5_NUM_OF_CHANS;
345 subdev->maxdata = 0xFFF;
346 subdev->range_table = &range_digital;
347 subdev->insn_read = unioxx5_subdev_read;
348 subdev->insn_write = unioxx5_subdev_write;
349 subdev->insn_config = unioxx5_insn_config;
350
351 printk("subdevice configured\n");
352
353 return 0;
354}
355
356static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
357 unsigned int *data, int channel, int minor)
358{
359 int channel_offset, val;
360 int mask = 1 << (channel & 0x07);
361
362 channel_offset = __unioxx5_define_chan_offset(channel);
363 if (channel_offset < 0) {
364 printk(KERN_ERR
365 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
366 minor, channel);
367 return 0;
368 }
369
370 val = usp->usp_prev_wr_val[channel_offset - 1];
371
372 if (*data)
373 val |= mask;
374 else
375 val &= ~mask;
376
377 outb(val, usp->usp_iobase + channel_offset);
378 usp->usp_prev_wr_val[channel_offset - 1] = val;
379
380 return 1;
381}
382
383
384static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
385 unsigned int *data, int channel, int minor)
386{
387 int channel_offset, mask = 1 << (channel & 0x07);
388
389 channel_offset = __unioxx5_define_chan_offset(channel);
390 if (channel_offset < 0) {
391 printk(KERN_ERR
392 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
393 minor, channel);
394 return 0;
395 }
396
397 *data = inb(usp->usp_iobase + channel_offset);
398 *data &= mask;
399
400 if (channel_offset > 1)
401 channel -= 2 << channel_offset;
402
403 *data >>= channel;
404 return 1;
405}
406
407#if 0
408static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
409{
410 int i, mask;
411
412 mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
413 printk("COMEDI: mode = %d\n", mask);
414
415 outb(1, usp->usp_iobase + 0);
416
417 for (i = 0; i < 3; i++)
418 outb(mask, usp->usp_iobase + i);
419
420 outb(0, usp->usp_iobase + 0);
421}
422#endif
423
424static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
425 unsigned int *data, int channel, int minor)
426{
427 int module, i;
428
429 module = channel / 2;
430 i = (channel % 2) << 1;
431
432
433 if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
434 printk(KERN_ERR
435 "comedi%d: module in position %d with id 0x%0x is for input only!\n",
436 minor, module, usp->usp_module_type[module]);
437 return 0;
438 }
439
440 __unioxx5_analog_config(usp, channel);
441
442 usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
443
444 usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
445
446
447 outb(module + 1, usp->usp_iobase + 5);
448 outb('W', usp->usp_iobase + 6);
449
450
451 for (i = 0; i < 4; i++) {
452 while (!((inb(usp->usp_iobase + 0)) & TxBE))
453 ;
454 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
455 }
456
457 return 1;
458}
459
460static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
461 unsigned int *data, int channel, int minor)
462{
463 int module_no, read_ch;
464 char control;
465
466 module_no = channel / 2;
467 read_ch = channel % 2;
468
469
470 if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
471 printk(KERN_ERR
472 "comedi%d: module in position %d with id 0x%02x is for output only",
473 minor, module_no, usp->usp_module_type[module_no]);
474 return 0;
475 }
476
477 __unioxx5_analog_config(usp, channel);
478 outb(module_no + 1, usp->usp_iobase + 5);
479 outb('V', usp->usp_iobase + 6);
480 control = inb(usp->usp_iobase);
481
482
483 while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
484 ;
485
486
487 if ((control & Rx4CA_ERR_MASK)) {
488 printk("COMEDI: 4 bytes error\n");
489 return 0;
490 }
491
492 if (read_ch)
493 *data = inw(usp->usp_iobase + 6);
494 else
495 *data = inw(usp->usp_iobase + 4);
496
497 return 1;
498}
499
500
501static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
502{
503 int chan_a, chan_b, conf, channel_offset;
504
505 channel_offset = __unioxx5_define_chan_offset(channel);
506 conf = usp->usp_prev_cn_val[channel_offset - 1];
507 chan_a = chan_b = 1;
508
509
510 if (channel % 2 == 0) {
511 chan_a <<= channel & 0x07;
512 chan_b <<= (channel + 1) & 0x07;
513 } else {
514 chan_a <<= (channel - 1) & 0x07;
515 chan_b <<= channel & 0x07;
516 }
517
518 conf |= chan_a;
519 conf &= ~chan_b;
520
521 outb(1, usp->usp_iobase + 0);
522 outb(conf, usp->usp_iobase + channel_offset);
523 outb(0, usp->usp_iobase + 0);
524
525 usp->usp_prev_cn_val[channel_offset - 1] = conf;
526}
527
528
529
530
531
532
533
534
535static int __unioxx5_define_chan_offset(int chan_num)
536{
537
538 if (chan_num < 0 || chan_num > 23)
539 return -1;
540
541 return (chan_num >> 3) + 1;
542}
543
544MODULE_AUTHOR("Comedi http://www.comedi.org");
545MODULE_DESCRIPTION("Comedi low-level driver");
546MODULE_LICENSE("GPL");
547