1 /***************************************************************************
3 * comedi/drivers/unioxx5.c *
4 * Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards. *
6 * Copyright (C) 2006 Kruchinin Daniil (asgard) [asgard@etersoft.ru] *
8 * COMEDI - Linux Control and Measurement Device Interface *
9 * Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org> *
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. *
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. *
21 ***************************************************************************/
25 Description: Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.
26 Author: Kruchinin Daniil (asgard) <asgard@etersoft.ru>
29 Devices: [Fastwel] UNIOxx-5 (unioxx5),
31 This card supports digital and analog I/O. It written for g01
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 |
42 #include <linux/module.h>
43 #include <linux/delay.h>
44 #include "../comedidev.h"
46 #define DRIVER_NAME "unioxx5"
47 #define UNIOXX5_SIZE 0x10
48 #define UNIOXX5_SUBDEV_BASE 0xA000 /* base addr of first subdev */
49 #define UNIOXX5_SUBDEV_ODDS 0x400
52 #define MODULE_DIGITAL 0
53 #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */
55 /* constants for digital i/o */
56 #define UNIOXX5_NUM_OF_CHANS 24
58 /* constants for analog i/o */
59 #define TxBE 0x10 /* transmit buffer enable */
60 #define RxCA 0x20 /* 1 receive character available */
61 #define Rx2CA 0x40 /* 2 receive character available */
62 #define Rx4CA 0x80 /* 4 receive character available */
64 /* bytes mask errors */
65 #define Rx2CA_ERR_MASK 0x04 /* 2 bytes receiving error */
66 #define Rx4CA_ERR_MASK 0x08 /* 4 bytes receiving error */
69 #define ALL_2_INPUT 0 /* config all digital channels to input */
70 #define ALL_2_OUTPUT 1 /* config all digital channels to output */
72 /* 'private' structure for each subdevice */
73 struct unioxx5_subd_priv {
75 /* 12 modules. each can be 70L or 73L */
76 unsigned char usp_module_type[12];
77 /* for saving previous written value for analog modules */
78 unsigned char usp_extra_data[12][4];
79 unsigned char usp_prev_wr_val[3]; /* previous written value */
80 unsigned char usp_prev_cn_val[3]; /* previous channel value */
83 static int __unioxx5_define_chan_offset(int chan_num)
86 if (chan_num < 0 || chan_num > 23)
89 return (chan_num >> 3) + 1;
93 static void __unioxx5_digital_config(struct comedi_subdevice *s, int mode)
95 struct unioxx5_subd_priv *usp = s->private;
96 struct device *csdev = s->device->class_dev;
99 mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
100 dev_dbg(csdev, "mode = %d\n", mask);
102 outb(1, usp->usp_iobase + 0);
104 for (i = 0; i < 3; i++)
105 outb(mask, usp->usp_iobase + i);
107 outb(0, usp->usp_iobase + 0);
111 /* configure channels for analog i/o (even to output, odd to input) */
112 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
114 int chan_a, chan_b, conf, channel_offset;
116 channel_offset = __unioxx5_define_chan_offset(channel);
117 conf = usp->usp_prev_cn_val[channel_offset - 1];
120 /* setting channel A and channel B mask */
121 if (channel % 2 == 0) {
122 chan_a <<= channel & 0x07;
123 chan_b <<= (channel + 1) & 0x07;
125 chan_a <<= (channel - 1) & 0x07;
126 chan_b <<= channel & 0x07;
129 conf |= chan_a; /* even channel ot output */
130 conf &= ~chan_b; /* odd channel to input */
132 outb(1, usp->usp_iobase + 0);
133 outb(conf, usp->usp_iobase + channel_offset);
134 outb(0, usp->usp_iobase + 0);
136 usp->usp_prev_cn_val[channel_offset - 1] = conf;
139 static int __unioxx5_digital_read(struct comedi_subdevice *s,
140 unsigned int *data, int channel, int minor)
142 struct unioxx5_subd_priv *usp = s->private;
143 struct device *csdev = s->device->class_dev;
144 int channel_offset, mask = 1 << (channel & 0x07);
146 channel_offset = __unioxx5_define_chan_offset(channel);
147 if (channel_offset < 0) {
149 "undefined channel %d. channel range is 0 .. 23\n",
154 *data = inb(usp->usp_iobase + channel_offset);
157 /* correct the read value to 0 or 1 */
158 if (channel_offset > 1)
159 channel -= 2 << channel_offset;
164 static int __unioxx5_analog_read(struct comedi_subdevice *s,
165 unsigned int *data, int channel, int minor)
167 struct unioxx5_subd_priv *usp = s->private;
168 struct device *csdev = s->device->class_dev;
169 int module_no, read_ch;
172 module_no = channel / 2;
173 read_ch = channel % 2; /* depend on type of channel (A or B) */
175 /* defining if given module can work on input */
176 if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
178 "module in position %d with id 0x%02x is for output only",
179 module_no, usp->usp_module_type[module_no]);
183 __unioxx5_analog_config(usp, channel);
184 /* sends module number to card(1 .. 12) */
185 outb(module_no + 1, usp->usp_iobase + 5);
186 outb('V', usp->usp_iobase + 6); /* sends to module (V)erify command */
187 control = inb(usp->usp_iobase); /* get control register byte */
189 /* waits while reading four bytes will be allowed */
190 while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
193 /* if four bytes readding error occurs - return 0(false) */
194 if ((control & Rx4CA_ERR_MASK)) {
195 dev_err(csdev, "4 bytes error\n");
200 *data = inw(usp->usp_iobase + 6); /* channel B */
202 *data = inw(usp->usp_iobase + 4); /* channel A */
207 static int __unioxx5_digital_write(struct comedi_subdevice *s,
208 unsigned int *data, int channel, int minor)
210 struct unioxx5_subd_priv *usp = s->private;
211 struct device *csdev = s->device->class_dev;
212 int channel_offset, val;
213 int mask = 1 << (channel & 0x07);
215 channel_offset = __unioxx5_define_chan_offset(channel);
216 if (channel_offset < 0) {
218 "undefined channel %d. channel range is 0 .. 23\n",
223 /* getting previous written value */
224 val = usp->usp_prev_wr_val[channel_offset - 1];
231 outb(val, usp->usp_iobase + channel_offset);
232 /* saving new written value */
233 usp->usp_prev_wr_val[channel_offset - 1] = val;
238 static int __unioxx5_analog_write(struct comedi_subdevice *s,
239 unsigned int *data, int channel, int minor)
241 struct unioxx5_subd_priv *usp = s->private;
242 struct device *csdev = s->device->class_dev;
245 module = channel / 2; /* definig module number(0 .. 11) */
246 i = (channel % 2) << 1; /* depends on type of channel (A or B) */
248 /* defining if given module can work on output */
249 if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
251 "module in position %d with id 0x%0x is for input only!\n",
252 module, usp->usp_module_type[module]);
256 __unioxx5_analog_config(usp, channel);
257 /* saving minor byte */
258 usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
259 /* saving major byte */
260 usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
262 /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
263 /* sending module number to card(1 .. 12) */
264 outb(module + 1, usp->usp_iobase + 5);
265 outb('W', usp->usp_iobase + 6); /* sends (W)rite command to module */
267 /* sending for bytes to module(one byte per cycle iteration) */
268 for (i = 0; i < 4; i++) {
269 while (!((inb(usp->usp_iobase + 0)) & TxBE))
270 ; /* waits while writting will be allowed */
271 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
277 static int unioxx5_subdev_read(struct comedi_device *dev,
278 struct comedi_subdevice *subdev,
279 struct comedi_insn *insn, unsigned int *data)
281 struct unioxx5_subd_priv *usp = subdev->private;
284 channel = CR_CHAN(insn->chanspec);
285 /* defining module type(analog or digital) */
286 type = usp->usp_module_type[channel / 2];
288 if (type == MODULE_DIGITAL) {
289 if (!__unioxx5_digital_read(subdev, data, channel, dev->minor))
292 if (!__unioxx5_analog_read(subdev, data, channel, dev->minor))
299 static int unioxx5_subdev_write(struct comedi_device *dev,
300 struct comedi_subdevice *subdev,
301 struct comedi_insn *insn, unsigned int *data)
303 struct unioxx5_subd_priv *usp = subdev->private;
306 channel = CR_CHAN(insn->chanspec);
307 /* defining module type(analog or digital) */
308 type = usp->usp_module_type[channel / 2];
310 if (type == MODULE_DIGITAL) {
311 if (!__unioxx5_digital_write(subdev, data, channel, dev->minor))
314 if (!__unioxx5_analog_write(subdev, data, channel, dev->minor))
321 /* for digital modules only */
322 static int unioxx5_insn_config(struct comedi_device *dev,
323 struct comedi_subdevice *subdev,
324 struct comedi_insn *insn, unsigned int *data)
326 int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
327 struct unioxx5_subd_priv *usp = subdev->private;
328 int mask = 1 << (channel & 0x07);
330 type = usp->usp_module_type[channel / 2];
332 if (type != MODULE_DIGITAL) {
333 dev_err(dev->class_dev,
334 "channel configuration accessible only for digital modules\n");
338 channel_offset = __unioxx5_define_chan_offset(channel);
339 if (channel_offset < 0) {
340 dev_err(dev->class_dev,
341 "undefined channel %d. channel range is 0 .. 23\n",
346 /* gets previously written value */
347 flags = usp->usp_prev_cn_val[channel_offset - 1];
357 dev_err(dev->class_dev, "unknown flag\n");
362 * sets channels buffer to 1(after this we are allowed to *
363 * change channel type on input or output) *
365 outb(1, usp->usp_iobase + 0);
366 /* changes type of _one_ channel */
367 outb(flags, usp->usp_iobase + channel_offset);
368 /* sets channels bank to 0(allows directly input/output) */
369 outb(0, usp->usp_iobase + 0);
370 /* saves written value */
371 usp->usp_prev_cn_val[channel_offset - 1] = flags;
376 /* initializing subdevice with given address */
377 static int __unioxx5_subdev_init(struct comedi_device *dev,
378 struct comedi_subdevice *s,
381 struct unioxx5_subd_priv *usp;
382 int i, to, ndef_flag = 0;
385 usp = comedi_alloc_spriv(s, sizeof(*usp));
389 ret = __comedi_request_region(dev, iobase, UNIOXX5_SIZE);
392 usp->usp_iobase = iobase;
394 /* defining modules types */
395 for (i = 0; i < 12; i++) {
398 __unioxx5_analog_config(usp, i * 2);
399 /* sends channel number to card */
400 outb(i + 1, iobase + 5);
401 outb('H', iobase + 6); /* requests EEPROM world */
402 while (!(inb(iobase + 0) & TxBE))
403 ; /* waits while writting will be allowed */
406 /* waits while reading of two bytes will be allowed */
407 while (!(inb(iobase + 0) & Rx2CA)) {
415 usp->usp_module_type[i] = 0;
418 usp->usp_module_type[i] = inb(iobase + 6);
423 /* initial subdevice for digital or analog i/o */
424 s->type = COMEDI_SUBD_DIO;
425 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
426 s->n_chan = UNIOXX5_NUM_OF_CHANS;
428 s->range_table = &range_digital;
429 s->insn_read = unioxx5_subdev_read;
430 s->insn_write = unioxx5_subdev_write;
431 /* for digital modules only!!! */
432 s->insn_config = unioxx5_insn_config;
437 static int unioxx5_attach(struct comedi_device *dev,
438 struct comedi_devconfig *it)
440 struct comedi_subdevice *s;
441 int iobase, i, n_subd;
445 iobase = it->options[0];
447 dev->iobase = iobase;
448 iobase += UNIOXX5_SUBDEV_BASE;
451 /* getting number of subdevices with types 'g01' */
452 for (i = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
456 if (id != 'g' || num != 1)
462 /* unioxx5 can has from two to four subdevices */
464 dev_err(dev->class_dev,
465 "your card must has at least 2 'g01' subdevices\n");
469 ret = comedi_alloc_subdevices(dev, n_subd);
473 /* initializing each of for same subdevices */
474 for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
475 s = &dev->subdevices[i];
476 ret = __unioxx5_subdev_init(dev, s, iobase);
484 static void unioxx5_detach(struct comedi_device *dev)
486 struct comedi_subdevice *s;
487 struct unioxx5_subd_priv *spriv;
490 for (i = 0; i < dev->n_subdevices; i++) {
491 s = &dev->subdevices[i];
493 if (spriv && spriv->usp_iobase)
494 release_region(spriv->usp_iobase, UNIOXX5_SIZE);
498 static struct comedi_driver unioxx5_driver = {
499 .driver_name = DRIVER_NAME,
500 .module = THIS_MODULE,
501 .attach = unioxx5_attach,
502 .detach = unioxx5_detach,
504 module_comedi_driver(unioxx5_driver);
506 MODULE_AUTHOR("Comedi http://www.comedi.org");
507 MODULE_DESCRIPTION("Comedi low-level driver");
508 MODULE_LICENSE("GPL");