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 |
41 #include <linux/module.h>
42 #include <linux/delay.h>
43 #include "../comedidev.h"
45 #define UNIOXX5_SIZE 0x10
46 #define UNIOXX5_SUBDEV_BASE 0xA000 /* base addr of first subdev */
47 #define UNIOXX5_SUBDEV_ODDS 0x400
50 #define MODULE_DIGITAL 0
51 #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */
53 /* constants for digital i/o */
54 #define UNIOXX5_NUM_OF_CHANS 24
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 */
62 /* bytes mask errors */
63 #define Rx2CA_ERR_MASK 0x04 /* 2 bytes receiving error */
64 #define Rx4CA_ERR_MASK 0x08 /* 4 bytes receiving error */
67 #define ALL_2_INPUT 0 /* config all digital channels to input */
68 #define ALL_2_OUTPUT 1 /* config all digital channels to output */
70 /* 'private' structure for each subdevice */
71 struct unioxx5_subd_priv {
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 */
81 static int __unioxx5_define_chan_offset(int chan_num)
83 if (chan_num < 0 || chan_num > 23)
86 return (chan_num >> 3) + 1;
90 static void __unioxx5_digital_config(struct comedi_subdevice *s, int mode)
92 struct unioxx5_subd_priv *usp = s->private;
93 struct device *csdev = s->device->class_dev;
96 mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
97 dev_dbg(csdev, "mode = %d\n", mask);
99 outb(1, usp->usp_iobase + 0);
101 for (i = 0; i < 3; i++)
102 outb(mask, usp->usp_iobase + i);
104 outb(0, usp->usp_iobase + 0);
108 /* configure channels for analog i/o (even to output, odd to input) */
109 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
111 int chan_a, chan_b, conf, channel_offset;
113 channel_offset = __unioxx5_define_chan_offset(channel);
114 conf = usp->usp_prev_cn_val[channel_offset - 1];
117 /* setting channel A and channel B mask */
118 if (channel % 2 == 0) {
119 chan_a <<= channel & 0x07;
120 chan_b <<= (channel + 1) & 0x07;
122 chan_a <<= (channel - 1) & 0x07;
123 chan_b <<= channel & 0x07;
126 conf |= chan_a; /* even channel ot output */
127 conf &= ~chan_b; /* odd channel to input */
129 outb(1, usp->usp_iobase + 0);
130 outb(conf, usp->usp_iobase + channel_offset);
131 outb(0, usp->usp_iobase + 0);
133 usp->usp_prev_cn_val[channel_offset - 1] = conf;
136 static int __unioxx5_digital_read(struct comedi_subdevice *s,
137 unsigned int *data, int channel, int minor)
139 struct unioxx5_subd_priv *usp = s->private;
140 struct device *csdev = s->device->class_dev;
141 int channel_offset, mask = 1 << (channel & 0x07);
143 channel_offset = __unioxx5_define_chan_offset(channel);
144 if (channel_offset < 0) {
146 "undefined channel %d. channel range is 0 .. 23\n",
151 *data = inb(usp->usp_iobase + channel_offset);
154 /* correct the read value to 0 or 1 */
155 if (channel_offset > 1)
156 channel -= 2 << channel_offset;
161 static int __unioxx5_analog_read(struct comedi_subdevice *s,
162 unsigned int *data, int channel, int minor)
164 struct unioxx5_subd_priv *usp = s->private;
165 struct device *csdev = s->device->class_dev;
166 int module_no, read_ch;
169 module_no = channel / 2;
170 read_ch = channel % 2; /* depend on type of channel (A or B) */
172 /* defining if given module can work on input */
173 if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
175 "module in position %d with id 0x%02x is for output only",
176 module_no, usp->usp_module_type[module_no]);
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 */
186 /* waits while reading four bytes will be allowed */
187 while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
190 /* if four bytes readding error occurs - return 0(false) */
191 if ((control & Rx4CA_ERR_MASK)) {
192 dev_err(csdev, "4 bytes error\n");
197 *data = inw(usp->usp_iobase + 6); /* channel B */
199 *data = inw(usp->usp_iobase + 4); /* channel A */
204 static int __unioxx5_digital_write(struct comedi_subdevice *s,
205 unsigned int *data, int channel, int minor)
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);
212 channel_offset = __unioxx5_define_chan_offset(channel);
213 if (channel_offset < 0) {
215 "undefined channel %d. channel range is 0 .. 23\n",
220 /* getting previous written value */
221 val = usp->usp_prev_wr_val[channel_offset - 1];
228 outb(val, usp->usp_iobase + channel_offset);
229 /* saving new written value */
230 usp->usp_prev_wr_val[channel_offset - 1] = val;
235 static int __unioxx5_analog_write(struct comedi_subdevice *s,
236 unsigned int *data, int channel, int minor)
238 struct unioxx5_subd_priv *usp = s->private;
239 struct device *csdev = s->device->class_dev;
242 module = channel / 2; /* definig module number(0 .. 11) */
243 i = (channel % 2) << 1; /* depends on type of channel (A or B) */
245 /* defining if given module can work on output */
246 if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
248 "module in position %d with id 0x%0x is for input only!\n",
249 module, usp->usp_module_type[module]);
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);
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 */
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);
274 static int unioxx5_subdev_read(struct comedi_device *dev,
275 struct comedi_subdevice *subdev,
276 struct comedi_insn *insn, unsigned int *data)
278 struct unioxx5_subd_priv *usp = subdev->private;
281 channel = CR_CHAN(insn->chanspec);
282 /* defining module type(analog or digital) */
283 type = usp->usp_module_type[channel / 2];
285 if (type == MODULE_DIGITAL) {
286 if (!__unioxx5_digital_read(subdev, data, channel, dev->minor))
289 if (!__unioxx5_analog_read(subdev, data, channel, dev->minor))
296 static int unioxx5_subdev_write(struct comedi_device *dev,
297 struct comedi_subdevice *subdev,
298 struct comedi_insn *insn, unsigned int *data)
300 struct unioxx5_subd_priv *usp = subdev->private;
303 channel = CR_CHAN(insn->chanspec);
304 /* defining module type(analog or digital) */
305 type = usp->usp_module_type[channel / 2];
307 if (type == MODULE_DIGITAL) {
308 if (!__unioxx5_digital_write(subdev, data, channel, dev->minor))
311 if (!__unioxx5_analog_write(subdev, data, channel, dev->minor))
318 /* for digital modules only */
319 static int unioxx5_insn_config(struct comedi_device *dev,
320 struct comedi_subdevice *subdev,
321 struct comedi_insn *insn, unsigned int *data)
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);
327 type = usp->usp_module_type[channel / 2];
329 if (type != MODULE_DIGITAL) {
330 dev_err(dev->class_dev,
331 "channel configuration accessible only for digital modules\n");
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",
343 /* gets previously written value */
344 flags = usp->usp_prev_cn_val[channel_offset - 1];
354 dev_err(dev->class_dev, "unknown flag\n");
359 * sets channels buffer to 1(after this we are allowed to *
360 * change channel type on input or output) *
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;
373 /* initializing subdevice with given address */
374 static int __unioxx5_subdev_init(struct comedi_device *dev,
375 struct comedi_subdevice *s,
378 struct unioxx5_subd_priv *usp;
379 int i, to, ndef_flag = 0;
382 usp = comedi_alloc_spriv(s, sizeof(*usp));
386 ret = __comedi_request_region(dev, iobase, UNIOXX5_SIZE);
389 usp->usp_iobase = iobase;
391 /* defining modules types */
392 for (i = 0; i < 12; i++) {
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 */
403 /* waits while reading of two bytes will be allowed */
404 while (!(inb(iobase + 0) & Rx2CA)) {
412 usp->usp_module_type[i] = 0;
415 usp->usp_module_type[i] = inb(iobase + 6);
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;
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;
435 static int unioxx5_attach(struct comedi_device *dev,
436 struct comedi_devconfig *it)
438 struct comedi_subdevice *s;
439 int iobase, i, n_subd;
443 iobase = it->options[0];
445 dev->iobase = iobase;
446 iobase += UNIOXX5_SUBDEV_BASE;
449 /* getting number of subdevices with types 'g01' */
450 for (i = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
454 if (id != 'g' || num != 1)
460 /* unioxx5 can has from two to four subdevices */
462 dev_err(dev->class_dev,
463 "your card must has at least 2 'g01' subdevices\n");
467 ret = comedi_alloc_subdevices(dev, n_subd);
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);
482 static void unioxx5_detach(struct comedi_device *dev)
484 struct comedi_subdevice *s;
485 struct unioxx5_subd_priv *spriv;
488 for (i = 0; i < dev->n_subdevices; i++) {
489 s = &dev->subdevices[i];
491 if (spriv && spriv->usp_iobase)
492 release_region(spriv->usp_iobase, UNIOXX5_SIZE);
496 static struct comedi_driver unioxx5_driver = {
497 .driver_name = "unioxx5",
498 .module = THIS_MODULE,
499 .attach = unioxx5_attach,
500 .detach = unioxx5_detach,
502 module_comedi_driver(unioxx5_driver);
504 MODULE_AUTHOR("Comedi http://www.comedi.org");
505 MODULE_DESCRIPTION("Comedi low-level driver");
506 MODULE_LICENSE("GPL");