1 /***************************************************************************
2 * *
3 * comedi/drivers/unioxx5.c *
4 * Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards. *
5 * *
6 * Copyright (C) 2006 Kruchinin Daniil (asgard) [asgard@etersoft.ru] *
7 * *
8 * COMEDI - Linux Control and Measurement Device Interface *
9 * Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org> *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 ***************************************************************************/
22 /*
23
24 Driver: unioxx5
25 Description: Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.
26 Author: Kruchinin Daniil (asgard) <asgard@etersoft.ru>
27 Status: unknown
28 Updated: 2006-10-09
29 Devices: [Fastwel] UNIOxx-5 (unioxx5),
30
31 This card supports digital and analog I/O. It written for g01
32 subdevices only.
33 channels range: 0 .. 23 dio channels
34 and 0 .. 11 analog modules range
35 During attaching unioxx5 module displays modules identifiers
36 (see dmesg after comedi_config) in format:
37 | [module_number] module_id |
38
39 */
40
41 #include <linux/module.h>
42 #include <linux/delay.h>
43 #include "../comedidev.h"
44
45 #define UNIOXX5_SIZE 0x10
46 #define UNIOXX5_SUBDEV_BASE 0xA000 /* base addr of first subdev */
47 #define UNIOXX5_SUBDEV_ODDS 0x400
48
49 /* modules types */
50 #define MODULE_DIGITAL 0
51 #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */
52
53 /* constants for digital i/o */
54 #define UNIOXX5_NUM_OF_CHANS 24
55
56 /* constants for analog i/o */
57 #define TxBE 0x10 /* transmit buffer enable */
58 #define RxCA 0x20 /* 1 receive character available */
59 #define Rx2CA 0x40 /* 2 receive character available */
60 #define Rx4CA 0x80 /* 4 receive character available */
61
62 /* bytes mask errors */
63 #define Rx2CA_ERR_MASK 0x04 /* 2 bytes receiving error */
64 #define Rx4CA_ERR_MASK 0x08 /* 4 bytes receiving error */
65
66 /* channel modes */
67 #define ALL_2_INPUT 0 /* config all digital channels to input */
68 #define ALL_2_OUTPUT 1 /* config all digital channels to output */
69
70 /* 'private' structure for each subdevice */
71 struct unioxx5_subd_priv {
72 int usp_iobase;
73 /* 12 modules. each can be 70L or 73L */
74 unsigned char usp_module_type[12];
75 /* for saving previous written value for analog modules */
76 unsigned char usp_extra_data[12][4];
77 unsigned char usp_prev_wr_val[3]; /* previous written value */
78 unsigned char usp_prev_cn_val[3]; /* previous channel value */
79 };
80
__unioxx5_define_chan_offset(int chan_num)81 static int __unioxx5_define_chan_offset(int chan_num)
82 {
83 if (chan_num < 0 || chan_num > 23)
84 return -1;
85
86 return (chan_num >> 3) + 1;
87 }
88
89 #if 0 /* not used? */
90 static void __unioxx5_digital_config(struct comedi_subdevice *s, int mode)
91 {
92 struct unioxx5_subd_priv *usp = s->private;
93 struct device *csdev = s->device->class_dev;
94 int i, mask;
95
96 mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
97 dev_dbg(csdev, "mode = %d\n", mask);
98
99 outb(1, usp->usp_iobase + 0);
100
101 for (i = 0; i < 3; i++)
102 outb(mask, usp->usp_iobase + i);
103
104 outb(0, usp->usp_iobase + 0);
105 }
106 #endif
107
108 /* configure channels for analog i/o (even to output, odd to input) */
__unioxx5_analog_config(struct unioxx5_subd_priv * usp,int channel)109 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
110 {
111 int chan_a, chan_b, conf, channel_offset;
112
113 channel_offset = __unioxx5_define_chan_offset(channel);
114 conf = usp->usp_prev_cn_val[channel_offset - 1];
115 chan_a = chan_b = 1;
116
117 /* setting channel A and channel B mask */
118 if (channel % 2 == 0) {
119 chan_a <<= channel & 0x07;
120 chan_b <<= (channel + 1) & 0x07;
121 } else {
122 chan_a <<= (channel - 1) & 0x07;
123 chan_b <<= channel & 0x07;
124 }
125
126 conf |= chan_a; /* even channel ot output */
127 conf &= ~chan_b; /* odd channel to input */
128
129 outb(1, usp->usp_iobase + 0);
130 outb(conf, usp->usp_iobase + channel_offset);
131 outb(0, usp->usp_iobase + 0);
132
133 usp->usp_prev_cn_val[channel_offset - 1] = conf;
134 }
135
__unioxx5_digital_read(struct comedi_subdevice * s,unsigned int * data,int channel,int minor)136 static int __unioxx5_digital_read(struct comedi_subdevice *s,
137 unsigned int *data, int channel, int minor)
138 {
139 struct unioxx5_subd_priv *usp = s->private;
140 struct device *csdev = s->device->class_dev;
141 int channel_offset, mask = 1 << (channel & 0x07);
142
143 channel_offset = __unioxx5_define_chan_offset(channel);
144 if (channel_offset < 0) {
145 dev_err(csdev,
146 "undefined channel %d. channel range is 0 .. 23\n",
147 channel);
148 return 0;
149 }
150
151 *data = inb(usp->usp_iobase + channel_offset);
152 *data &= mask;
153
154 /* correct the read value to 0 or 1 */
155 if (channel_offset > 1)
156 channel -= 2 << channel_offset;
157 *data >>= channel;
158 return 1;
159 }
160
__unioxx5_analog_read(struct comedi_subdevice * s,unsigned int * data,int channel,int minor)161 static int __unioxx5_analog_read(struct comedi_subdevice *s,
162 unsigned int *data, int channel, int minor)
163 {
164 struct unioxx5_subd_priv *usp = s->private;
165 struct device *csdev = s->device->class_dev;
166 int module_no, read_ch;
167 char control;
168
169 module_no = channel / 2;
170 read_ch = channel % 2; /* depend on type of channel (A or B) */
171
172 /* defining if given module can work on input */
173 if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
174 dev_err(csdev,
175 "module in position %d with id 0x%02x is for output only",
176 module_no, usp->usp_module_type[module_no]);
177 return 0;
178 }
179
180 __unioxx5_analog_config(usp, channel);
181 /* sends module number to card(1 .. 12) */
182 outb(module_no + 1, usp->usp_iobase + 5);
183 outb('V', usp->usp_iobase + 6); /* sends to module (V)erify command */
184 control = inb(usp->usp_iobase); /* get control register byte */
185
186 /* waits while reading four bytes will be allowed */
187 while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
188 ;
189
190 /* if four bytes readding error occurs - return 0(false) */
191 if ((control & Rx4CA_ERR_MASK)) {
192 dev_err(csdev, "4 bytes error\n");
193 return 0;
194 }
195
196 if (read_ch)
197 *data = inw(usp->usp_iobase + 6); /* channel B */
198 else
199 *data = inw(usp->usp_iobase + 4); /* channel A */
200
201 return 1;
202 }
203
__unioxx5_digital_write(struct comedi_subdevice * s,unsigned int * data,int channel,int minor)204 static int __unioxx5_digital_write(struct comedi_subdevice *s,
205 unsigned int *data, int channel, int minor)
206 {
207 struct unioxx5_subd_priv *usp = s->private;
208 struct device *csdev = s->device->class_dev;
209 int channel_offset, val;
210 int mask = 1 << (channel & 0x07);
211
212 channel_offset = __unioxx5_define_chan_offset(channel);
213 if (channel_offset < 0) {
214 dev_err(csdev,
215 "undefined channel %d. channel range is 0 .. 23\n",
216 channel);
217 return 0;
218 }
219
220 /* getting previous written value */
221 val = usp->usp_prev_wr_val[channel_offset - 1];
222
223 if (*data)
224 val |= mask;
225 else
226 val &= ~mask;
227
228 outb(val, usp->usp_iobase + channel_offset);
229 /* saving new written value */
230 usp->usp_prev_wr_val[channel_offset - 1] = val;
231
232 return 1;
233 }
234
__unioxx5_analog_write(struct comedi_subdevice * s,unsigned int * data,int channel,int minor)235 static int __unioxx5_analog_write(struct comedi_subdevice *s,
236 unsigned int *data, int channel, int minor)
237 {
238 struct unioxx5_subd_priv *usp = s->private;
239 struct device *csdev = s->device->class_dev;
240 int module, i;
241
242 module = channel / 2; /* definig module number(0 .. 11) */
243 i = (channel % 2) << 1; /* depends on type of channel (A or B) */
244
245 /* defining if given module can work on output */
246 if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
247 dev_err(csdev,
248 "module in position %d with id 0x%0x is for input only!\n",
249 module, usp->usp_module_type[module]);
250 return 0;
251 }
252
253 __unioxx5_analog_config(usp, channel);
254 /* saving minor byte */
255 usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
256 /* saving major byte */
257 usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
258
259 /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
260 /* sending module number to card(1 .. 12) */
261 outb(module + 1, usp->usp_iobase + 5);
262 outb('W', usp->usp_iobase + 6); /* sends (W)rite command to module */
263
264 /* sending for bytes to module(one byte per cycle iteration) */
265 for (i = 0; i < 4; i++) {
266 while (!((inb(usp->usp_iobase + 0)) & TxBE))
267 ; /* waits while writing will be allowed */
268 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
269 }
270
271 return 1;
272 }
273
unioxx5_subdev_read(struct comedi_device * dev,struct comedi_subdevice * subdev,struct comedi_insn * insn,unsigned int * data)274 static int unioxx5_subdev_read(struct comedi_device *dev,
275 struct comedi_subdevice *subdev,
276 struct comedi_insn *insn, unsigned int *data)
277 {
278 struct unioxx5_subd_priv *usp = subdev->private;
279 int channel, type;
280
281 channel = CR_CHAN(insn->chanspec);
282 /* defining module type(analog or digital) */
283 type = usp->usp_module_type[channel / 2];
284
285 if (type == MODULE_DIGITAL) {
286 if (!__unioxx5_digital_read(subdev, data, channel, dev->minor))
287 return -1;
288 } else {
289 if (!__unioxx5_analog_read(subdev, data, channel, dev->minor))
290 return -1;
291 }
292
293 return 1;
294 }
295
unioxx5_subdev_write(struct comedi_device * dev,struct comedi_subdevice * subdev,struct comedi_insn * insn,unsigned int * data)296 static int unioxx5_subdev_write(struct comedi_device *dev,
297 struct comedi_subdevice *subdev,
298 struct comedi_insn *insn, unsigned int *data)
299 {
300 struct unioxx5_subd_priv *usp = subdev->private;
301 int channel, type;
302
303 channel = CR_CHAN(insn->chanspec);
304 /* defining module type(analog or digital) */
305 type = usp->usp_module_type[channel / 2];
306
307 if (type == MODULE_DIGITAL) {
308 if (!__unioxx5_digital_write(subdev, data, channel, dev->minor))
309 return -1;
310 } else {
311 if (!__unioxx5_analog_write(subdev, data, channel, dev->minor))
312 return -1;
313 }
314
315 return 1;
316 }
317
318 /* for digital modules only */
unioxx5_insn_config(struct comedi_device * dev,struct comedi_subdevice * subdev,struct comedi_insn * insn,unsigned int * data)319 static int unioxx5_insn_config(struct comedi_device *dev,
320 struct comedi_subdevice *subdev,
321 struct comedi_insn *insn, unsigned int *data)
322 {
323 int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
324 struct unioxx5_subd_priv *usp = subdev->private;
325 int mask = 1 << (channel & 0x07);
326
327 type = usp->usp_module_type[channel / 2];
328
329 if (type != MODULE_DIGITAL) {
330 dev_err(dev->class_dev,
331 "channel configuration accessible only for digital modules\n");
332 return -1;
333 }
334
335 channel_offset = __unioxx5_define_chan_offset(channel);
336 if (channel_offset < 0) {
337 dev_err(dev->class_dev,
338 "undefined channel %d. channel range is 0 .. 23\n",
339 channel);
340 return -1;
341 }
342
343 /* gets previously written value */
344 flags = usp->usp_prev_cn_val[channel_offset - 1];
345
346 switch (*data) {
347 case COMEDI_INPUT:
348 flags &= ~mask;
349 break;
350 case COMEDI_OUTPUT:
351 flags |= mask;
352 break;
353 default:
354 dev_err(dev->class_dev, "unknown flag\n");
355 return -1;
356 }
357
358 /* *\
359 * sets channels buffer to 1(after this we are allowed to *
360 * change channel type on input or output) *
361 \* */
362 outb(1, usp->usp_iobase + 0);
363 /* changes type of _one_ channel */
364 outb(flags, usp->usp_iobase + channel_offset);
365 /* sets channels bank to 0(allows directly input/output) */
366 outb(0, usp->usp_iobase + 0);
367 /* saves written value */
368 usp->usp_prev_cn_val[channel_offset - 1] = flags;
369
370 return 0;
371 }
372
373 /* initializing subdevice with given address */
__unioxx5_subdev_init(struct comedi_device * dev,struct comedi_subdevice * s,int iobase)374 static int __unioxx5_subdev_init(struct comedi_device *dev,
375 struct comedi_subdevice *s,
376 int iobase)
377 {
378 struct unioxx5_subd_priv *usp;
379 int i, to, ndef_flag = 0;
380 int ret;
381
382 usp = comedi_alloc_spriv(s, sizeof(*usp));
383 if (!usp)
384 return -ENOMEM;
385
386 ret = __comedi_request_region(dev, iobase, UNIOXX5_SIZE);
387 if (ret)
388 return ret;
389 usp->usp_iobase = iobase;
390
391 /* defining modules types */
392 for (i = 0; i < 12; i++) {
393 to = 10000;
394
395 __unioxx5_analog_config(usp, i * 2);
396 /* sends channel number to card */
397 outb(i + 1, iobase + 5);
398 outb('H', iobase + 6); /* requests EEPROM world */
399 while (!(inb(iobase + 0) & TxBE))
400 ; /* waits while writing will be allowed */
401 outb(0, iobase + 6);
402
403 /* waits while reading of two bytes will be allowed */
404 while (!(inb(iobase + 0) & Rx2CA)) {
405 if (--to <= 0) {
406 ndef_flag = 1;
407 break;
408 }
409 }
410
411 if (ndef_flag) {
412 usp->usp_module_type[i] = 0;
413 ndef_flag = 0;
414 } else {
415 usp->usp_module_type[i] = inb(iobase + 6);
416 }
417
418 udelay(1);
419 }
420
421 /* initial subdevice for digital or analog i/o */
422 s->type = COMEDI_SUBD_DIO;
423 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
424 s->n_chan = UNIOXX5_NUM_OF_CHANS;
425 s->maxdata = 0xFFF;
426 s->range_table = &range_digital;
427 s->insn_read = unioxx5_subdev_read;
428 s->insn_write = unioxx5_subdev_write;
429 /* for digital modules only!!! */
430 s->insn_config = unioxx5_insn_config;
431
432 return 0;
433 }
434
unioxx5_attach(struct comedi_device * dev,struct comedi_devconfig * it)435 static int unioxx5_attach(struct comedi_device *dev,
436 struct comedi_devconfig *it)
437 {
438 struct comedi_subdevice *s;
439 int iobase, i, n_subd;
440 int id, num, ba;
441 int ret;
442
443 iobase = it->options[0];
444
445 dev->iobase = iobase;
446 iobase += UNIOXX5_SUBDEV_BASE;
447 n_subd = 0;
448
449 /* getting number of subdevices with types 'g01' */
450 for (i = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
451 id = inb(ba + 0xE);
452 num = inb(ba + 0xF);
453
454 if (id != 'g' || num != 1)
455 continue;
456
457 n_subd++;
458 }
459
460 /* unioxx5 can has from two to four subdevices */
461 if (n_subd < 2) {
462 dev_err(dev->class_dev,
463 "your card must has at least 2 'g01' subdevices\n");
464 return -1;
465 }
466
467 ret = comedi_alloc_subdevices(dev, n_subd);
468 if (ret)
469 return ret;
470
471 /* initializing each of for same subdevices */
472 for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
473 s = &dev->subdevices[i];
474 ret = __unioxx5_subdev_init(dev, s, iobase);
475 if (ret)
476 return ret;
477 }
478
479 return 0;
480 }
481
unioxx5_detach(struct comedi_device * dev)482 static void unioxx5_detach(struct comedi_device *dev)
483 {
484 struct comedi_subdevice *s;
485 struct unioxx5_subd_priv *spriv;
486 int i;
487
488 for (i = 0; i < dev->n_subdevices; i++) {
489 s = &dev->subdevices[i];
490 spriv = s->private;
491 if (spriv && spriv->usp_iobase)
492 release_region(spriv->usp_iobase, UNIOXX5_SIZE);
493 }
494 }
495
496 static struct comedi_driver unioxx5_driver = {
497 .driver_name = "unioxx5",
498 .module = THIS_MODULE,
499 .attach = unioxx5_attach,
500 .detach = unioxx5_detach,
501 };
502 module_comedi_driver(unioxx5_driver);
503
504 MODULE_AUTHOR("Comedi http://www.comedi.org");
505 MODULE_DESCRIPTION("Comedi low-level driver");
506 MODULE_LICENSE("GPL");
507