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 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the Free Software *
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
25 ***************************************************************************/
29 Description: Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.
30 Author: Kruchinin Daniil (asgard) <asgard@etersoft.ru>
33 Devices: [Fastwel] UNIOxx-5 (unioxx5),
35 This card supports digital and analog I/O. It written for g01
37 channels range: 0 .. 23 dio channels
38 and 0 .. 11 analog modules range
39 During attaching unioxx5 module displays modules identifiers
40 (see dmesg after comedi_config) in format:
41 | [module_number] module_id |
45 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47 #include "../comedidev.h"
48 #include <linux/ioport.h>
49 #include <linux/slab.h>
51 #define DRIVER_NAME "unioxx5"
52 #define UNIOXX5_SIZE 0x10
53 #define UNIOXX5_SUBDEV_BASE 0xA000 /* base addr of first subdev */
54 #define UNIOXX5_SUBDEV_ODDS 0x400
57 #define MODULE_DIGITAL 0
58 #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */
60 /* constants for digital i/o */
61 #define UNIOXX5_NUM_OF_CHANS 24
63 /* constants for analog i/o */
64 #define TxBE 0x10 /* transmit buffer enable */
65 #define RxCA 0x20 /* 1 receive character available */
66 #define Rx2CA 0x40 /* 2 receive character available */
67 #define Rx4CA 0x80 /* 4 receive character available */
69 /* bytes mask errors */
70 #define Rx2CA_ERR_MASK 0x04 /* 2 bytes receiving error */
71 #define Rx4CA_ERR_MASK 0x08 /* 4 bytes receiving error */
74 #define ALL_2_INPUT 0 /* config all digital channels to input */
75 #define ALL_2_OUTPUT 1 /* config all digital channels to output */
77 /* 'private' structure for each subdevice */
78 struct unioxx5_subd_priv {
80 /* 12 modules. each can be 70L or 73L */
81 unsigned char usp_module_type[12];
82 /* for saving previous written value for analog modules */
83 unsigned char usp_extra_data[12][4];
84 unsigned char usp_prev_wr_val[3]; /* previous written value */
85 unsigned char usp_prev_cn_val[3]; /* previous channel value */
88 static int __unioxx5_define_chan_offset(int chan_num)
91 if (chan_num < 0 || chan_num > 23)
94 return (chan_num >> 3) + 1;
98 static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
102 mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
103 printk("COMEDI: mode = %d\n", mask);
105 outb(1, usp->usp_iobase + 0);
107 for (i = 0; i < 3; i++)
108 outb(mask, usp->usp_iobase + i);
110 outb(0, usp->usp_iobase + 0);
114 /* configure channels for analog i/o (even to output, odd to input) */
115 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
117 int chan_a, chan_b, conf, channel_offset;
119 channel_offset = __unioxx5_define_chan_offset(channel);
120 conf = usp->usp_prev_cn_val[channel_offset - 1];
123 /* setting channel A and channel B mask */
124 if (channel % 2 == 0) {
125 chan_a <<= channel & 0x07;
126 chan_b <<= (channel + 1) & 0x07;
128 chan_a <<= (channel - 1) & 0x07;
129 chan_b <<= channel & 0x07;
132 conf |= chan_a; /* even channel ot output */
133 conf &= ~chan_b; /* odd channel to input */
135 outb(1, usp->usp_iobase + 0);
136 outb(conf, usp->usp_iobase + channel_offset);
137 outb(0, usp->usp_iobase + 0);
139 usp->usp_prev_cn_val[channel_offset - 1] = conf;
142 static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
143 unsigned int *data, int channel, int minor)
145 int channel_offset, mask = 1 << (channel & 0x07);
147 channel_offset = __unioxx5_define_chan_offset(channel);
148 if (channel_offset < 0) {
149 pr_err("comedi%d: 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 unioxx5_subd_priv *usp,
165 unsigned int *data, int channel, int minor)
167 int module_no, read_ch;
170 module_no = channel / 2;
171 read_ch = channel % 2; /* depend on type of channel (A or B) */
173 /* defining if given module can work on input */
174 if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
175 pr_err("comedi%d: module in position %d with id 0x%02x is for output only",
176 minor, 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 printk("COMEDI: 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 unioxx5_subd_priv *usp,
205 unsigned int *data, int channel, int minor)
207 int channel_offset, val;
208 int mask = 1 << (channel & 0x07);
210 channel_offset = __unioxx5_define_chan_offset(channel);
211 if (channel_offset < 0) {
212 pr_err("comedi%d: undefined channel %d. channel range is 0 .. 23\n",
217 /* getting previous written value */
218 val = usp->usp_prev_wr_val[channel_offset - 1];
225 outb(val, usp->usp_iobase + channel_offset);
226 /* saving new written value */
227 usp->usp_prev_wr_val[channel_offset - 1] = val;
232 static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
233 unsigned int *data, int channel, int minor)
237 module = channel / 2; /* definig module number(0 .. 11) */
238 i = (channel % 2) << 1; /* depends on type of channel (A or B) */
240 /* defining if given module can work on output */
241 if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
242 pr_err("comedi%d: module in position %d with id 0x%0x is for input only!\n",
243 minor, module, usp->usp_module_type[module]);
247 __unioxx5_analog_config(usp, channel);
248 /* saving minor byte */
249 usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
250 /* saving major byte */
251 usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
253 /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
254 /* sending module number to card(1 .. 12) */
255 outb(module + 1, usp->usp_iobase + 5);
256 outb('W', usp->usp_iobase + 6); /* sends (W)rite command to module */
258 /* sending for bytes to module(one byte per cycle iteration) */
259 for (i = 0; i < 4; i++) {
260 while (!((inb(usp->usp_iobase + 0)) & TxBE))
261 ; /* waits while writting will be allowed */
262 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
268 static int unioxx5_subdev_read(struct comedi_device *dev,
269 struct comedi_subdevice *subdev,
270 struct comedi_insn *insn, unsigned int *data)
272 struct unioxx5_subd_priv *usp = subdev->private;
275 channel = CR_CHAN(insn->chanspec);
276 /* defining module type(analog or digital) */
277 type = usp->usp_module_type[channel / 2];
279 if (type == MODULE_DIGITAL) {
280 if (!__unioxx5_digital_read(usp, data, channel, dev->minor))
283 if (!__unioxx5_analog_read(usp, data, channel, dev->minor))
290 static int unioxx5_subdev_write(struct comedi_device *dev,
291 struct comedi_subdevice *subdev,
292 struct comedi_insn *insn, unsigned int *data)
294 struct unioxx5_subd_priv *usp = subdev->private;
297 channel = CR_CHAN(insn->chanspec);
298 /* defining module type(analog or digital) */
299 type = usp->usp_module_type[channel / 2];
301 if (type == MODULE_DIGITAL) {
302 if (!__unioxx5_digital_write(usp, data, channel, dev->minor))
305 if (!__unioxx5_analog_write(usp, data, channel, dev->minor))
312 /* for digital modules only */
313 static int unioxx5_insn_config(struct comedi_device *dev,
314 struct comedi_subdevice *subdev,
315 struct comedi_insn *insn, unsigned int *data)
317 int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
318 struct unioxx5_subd_priv *usp = subdev->private;
319 int mask = 1 << (channel & 0x07);
321 type = usp->usp_module_type[channel / 2];
323 if (type != MODULE_DIGITAL) {
324 dev_err(dev->class_dev,
325 "comedi%d: channel configuration accessible only for digital modules\n",
330 channel_offset = __unioxx5_define_chan_offset(channel);
331 if (channel_offset < 0) {
332 dev_err(dev->class_dev,
333 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
334 dev->minor, channel);
338 /* gets previously written value */
339 flags = usp->usp_prev_cn_val[channel_offset - 1];
349 dev_err(dev->class_dev,
350 "comedi%d: unknown flag\n", dev->minor);
355 * sets channels buffer to 1(after this we are allowed to *
356 * change channel type on input or output) *
358 outb(1, usp->usp_iobase + 0);
359 /* changes type of _one_ channel */
360 outb(flags, usp->usp_iobase + channel_offset);
361 /* sets channels bank to 0(allows directly input/output) */
362 outb(0, usp->usp_iobase + 0);
363 /* saves written value */
364 usp->usp_prev_cn_val[channel_offset - 1] = flags;
369 /* initializing subdevice with given address */
370 static int __unioxx5_subdev_init(struct comedi_device *dev,
371 struct comedi_subdevice *s,
374 struct unioxx5_subd_priv *usp;
375 int i, to, ndef_flag = 0;
378 usp = kzalloc(sizeof(*usp), GFP_KERNEL);
382 ret = __comedi_request_region(dev, iobase, UNIOXX5_SIZE);
387 usp->usp_iobase = iobase;
389 /* defining modules types */
390 for (i = 0; i < 12; i++) {
393 __unioxx5_analog_config(usp, i * 2);
394 /* sends channel number to card */
395 outb(i + 1, iobase + 5);
396 outb('H', iobase + 6); /* requests EEPROM world */
397 while (!(inb(iobase + 0) & TxBE))
398 ; /* waits while writting will be allowed */
401 /* waits while reading of two bytes will be allowed */
402 while (!(inb(iobase + 0) & Rx2CA)) {
410 usp->usp_module_type[i] = 0;
413 usp->usp_module_type[i] = inb(iobase + 6);
418 /* initial subdevice for digital or analog i/o */
419 s->type = COMEDI_SUBD_DIO;
421 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
422 s->n_chan = UNIOXX5_NUM_OF_CHANS;
424 s->range_table = &range_digital;
425 s->insn_read = unioxx5_subdev_read;
426 s->insn_write = unioxx5_subdev_write;
427 /* for digital modules only!!! */
428 s->insn_config = unioxx5_insn_config;
433 static int unioxx5_attach(struct comedi_device *dev,
434 struct comedi_devconfig *it)
436 struct comedi_subdevice *s;
437 int iobase, i, n_subd;
441 iobase = it->options[0];
443 dev->iobase = iobase;
444 iobase += UNIOXX5_SUBDEV_BASE;
446 /* defining number of subdevices and getting they types (it must be 'g01') */
447 for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
451 if (id != 'g' || num != 1)
457 /* unioxx5 can has from two to four subdevices */
459 dev_err(dev->class_dev,
460 "your card must has at least 2 'g01' subdevices\n");
464 ret = comedi_alloc_subdevices(dev, n_subd);
468 /* initializing each of for same subdevices */
469 for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
470 s = &dev->subdevices[i];
471 ret = __unioxx5_subdev_init(dev, s, iobase);
479 static void unioxx5_detach(struct comedi_device *dev)
482 struct comedi_subdevice *subdev;
483 struct unioxx5_subd_priv *usp;
485 for (i = 0; i < dev->n_subdevices; i++) {
486 subdev = &dev->subdevices[i];
487 usp = subdev->private;
488 release_region(usp->usp_iobase, UNIOXX5_SIZE);
489 kfree(subdev->private);
493 static struct comedi_driver unioxx5_driver = {
494 .driver_name = DRIVER_NAME,
495 .module = THIS_MODULE,
496 .attach = unioxx5_attach,
497 .detach = unioxx5_detach,
499 module_comedi_driver(unioxx5_driver);
501 MODULE_AUTHOR("Comedi http://www.comedi.org");
502 MODULE_DESCRIPTION("Comedi low-level driver");
503 MODULE_LICENSE("GPL");