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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43 #include "../comedidev.h"
44 #include <linux/ioport.h>
45 #include <linux/slab.h>
47 #define DRIVER_NAME "unioxx5"
48 #define UNIOXX5_SIZE 0x10
49 #define UNIOXX5_SUBDEV_BASE 0xA000 /* base addr of first subdev */
50 #define UNIOXX5_SUBDEV_ODDS 0x400
53 #define MODULE_DIGITAL 0
54 #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */
56 /* constants for digital i/o */
57 #define UNIOXX5_NUM_OF_CHANS 24
59 /* constants for analog i/o */
60 #define TxBE 0x10 /* transmit buffer enable */
61 #define RxCA 0x20 /* 1 receive character available */
62 #define Rx2CA 0x40 /* 2 receive character available */
63 #define Rx4CA 0x80 /* 4 receive character available */
65 /* bytes mask errors */
66 #define Rx2CA_ERR_MASK 0x04 /* 2 bytes receiving error */
67 #define Rx4CA_ERR_MASK 0x08 /* 4 bytes receiving error */
70 #define ALL_2_INPUT 0 /* config all digital channels to input */
71 #define ALL_2_OUTPUT 1 /* config all digital channels to output */
73 /* 'private' structure for each subdevice */
74 struct unioxx5_subd_priv {
76 /* 12 modules. each can be 70L or 73L */
77 unsigned char usp_module_type[12];
78 /* for saving previous written value for analog modules */
79 unsigned char usp_extra_data[12][4];
80 unsigned char usp_prev_wr_val[3]; /* previous written value */
81 unsigned char usp_prev_cn_val[3]; /* previous channel value */
84 static int __unioxx5_define_chan_offset(int chan_num)
87 if (chan_num < 0 || chan_num > 23)
90 return (chan_num >> 3) + 1;
94 static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
98 mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
99 printk("COMEDI: mode = %d\n", mask);
101 outb(1, usp->usp_iobase + 0);
103 for (i = 0; i < 3; i++)
104 outb(mask, usp->usp_iobase + i);
106 outb(0, usp->usp_iobase + 0);
110 /* configure channels for analog i/o (even to output, odd to input) */
111 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
113 int chan_a, chan_b, conf, channel_offset;
115 channel_offset = __unioxx5_define_chan_offset(channel);
116 conf = usp->usp_prev_cn_val[channel_offset - 1];
119 /* setting channel A and channel B mask */
120 if (channel % 2 == 0) {
121 chan_a <<= channel & 0x07;
122 chan_b <<= (channel + 1) & 0x07;
124 chan_a <<= (channel - 1) & 0x07;
125 chan_b <<= channel & 0x07;
128 conf |= chan_a; /* even channel ot output */
129 conf &= ~chan_b; /* odd channel to input */
131 outb(1, usp->usp_iobase + 0);
132 outb(conf, usp->usp_iobase + channel_offset);
133 outb(0, usp->usp_iobase + 0);
135 usp->usp_prev_cn_val[channel_offset - 1] = conf;
138 static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
139 unsigned int *data, int channel, int minor)
141 int channel_offset, mask = 1 << (channel & 0x07);
143 channel_offset = __unioxx5_define_chan_offset(channel);
144 if (channel_offset < 0) {
145 pr_err("comedi%d: undefined channel %d. channel range is 0 .. 23\n",
150 *data = inb(usp->usp_iobase + channel_offset);
153 /* correct the read value to 0 or 1 */
154 if (channel_offset > 1)
155 channel -= 2 << channel_offset;
160 static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
161 unsigned int *data, int channel, int minor)
163 int module_no, read_ch;
166 module_no = channel / 2;
167 read_ch = channel % 2; /* depend on type of channel (A or B) */
169 /* defining if given module can work on input */
170 if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
171 pr_err("comedi%d: module in position %d with id 0x%02x is for output only",
172 minor, module_no, usp->usp_module_type[module_no]);
176 __unioxx5_analog_config(usp, channel);
177 /* sends module number to card(1 .. 12) */
178 outb(module_no + 1, usp->usp_iobase + 5);
179 outb('V', usp->usp_iobase + 6); /* sends to module (V)erify command */
180 control = inb(usp->usp_iobase); /* get control register byte */
182 /* waits while reading four bytes will be allowed */
183 while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
186 /* if four bytes readding error occurs - return 0(false) */
187 if ((control & Rx4CA_ERR_MASK)) {
188 printk("COMEDI: 4 bytes error\n");
193 *data = inw(usp->usp_iobase + 6); /* channel B */
195 *data = inw(usp->usp_iobase + 4); /* channel A */
200 static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
201 unsigned int *data, int channel, int minor)
203 int channel_offset, val;
204 int mask = 1 << (channel & 0x07);
206 channel_offset = __unioxx5_define_chan_offset(channel);
207 if (channel_offset < 0) {
208 pr_err("comedi%d: undefined channel %d. channel range is 0 .. 23\n",
213 /* getting previous written value */
214 val = usp->usp_prev_wr_val[channel_offset - 1];
221 outb(val, usp->usp_iobase + channel_offset);
222 /* saving new written value */
223 usp->usp_prev_wr_val[channel_offset - 1] = val;
228 static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
229 unsigned int *data, int channel, int minor)
233 module = channel / 2; /* definig module number(0 .. 11) */
234 i = (channel % 2) << 1; /* depends on type of channel (A or B) */
236 /* defining if given module can work on output */
237 if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
238 pr_err("comedi%d: module in position %d with id 0x%0x is for input only!\n",
239 minor, module, usp->usp_module_type[module]);
243 __unioxx5_analog_config(usp, channel);
244 /* saving minor byte */
245 usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
246 /* saving major byte */
247 usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
249 /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
250 /* sending module number to card(1 .. 12) */
251 outb(module + 1, usp->usp_iobase + 5);
252 outb('W', usp->usp_iobase + 6); /* sends (W)rite command to module */
254 /* sending for bytes to module(one byte per cycle iteration) */
255 for (i = 0; i < 4; i++) {
256 while (!((inb(usp->usp_iobase + 0)) & TxBE))
257 ; /* waits while writting will be allowed */
258 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
264 static int unioxx5_subdev_read(struct comedi_device *dev,
265 struct comedi_subdevice *subdev,
266 struct comedi_insn *insn, unsigned int *data)
268 struct unioxx5_subd_priv *usp = subdev->private;
271 channel = CR_CHAN(insn->chanspec);
272 /* defining module type(analog or digital) */
273 type = usp->usp_module_type[channel / 2];
275 if (type == MODULE_DIGITAL) {
276 if (!__unioxx5_digital_read(usp, data, channel, dev->minor))
279 if (!__unioxx5_analog_read(usp, data, channel, dev->minor))
286 static int unioxx5_subdev_write(struct comedi_device *dev,
287 struct comedi_subdevice *subdev,
288 struct comedi_insn *insn, unsigned int *data)
290 struct unioxx5_subd_priv *usp = subdev->private;
293 channel = CR_CHAN(insn->chanspec);
294 /* defining module type(analog or digital) */
295 type = usp->usp_module_type[channel / 2];
297 if (type == MODULE_DIGITAL) {
298 if (!__unioxx5_digital_write(usp, data, channel, dev->minor))
301 if (!__unioxx5_analog_write(usp, data, channel, dev->minor))
308 /* for digital modules only */
309 static int unioxx5_insn_config(struct comedi_device *dev,
310 struct comedi_subdevice *subdev,
311 struct comedi_insn *insn, unsigned int *data)
313 int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
314 struct unioxx5_subd_priv *usp = subdev->private;
315 int mask = 1 << (channel & 0x07);
317 type = usp->usp_module_type[channel / 2];
319 if (type != MODULE_DIGITAL) {
320 dev_err(dev->class_dev,
321 "comedi%d: channel configuration accessible only for digital modules\n",
326 channel_offset = __unioxx5_define_chan_offset(channel);
327 if (channel_offset < 0) {
328 dev_err(dev->class_dev,
329 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
330 dev->minor, channel);
334 /* gets previously written value */
335 flags = usp->usp_prev_cn_val[channel_offset - 1];
345 dev_err(dev->class_dev,
346 "comedi%d: unknown flag\n", dev->minor);
351 * sets channels buffer to 1(after this we are allowed to *
352 * change channel type on input or output) *
354 outb(1, usp->usp_iobase + 0);
355 /* changes type of _one_ channel */
356 outb(flags, usp->usp_iobase + channel_offset);
357 /* sets channels bank to 0(allows directly input/output) */
358 outb(0, usp->usp_iobase + 0);
359 /* saves written value */
360 usp->usp_prev_cn_val[channel_offset - 1] = flags;
365 /* initializing subdevice with given address */
366 static int __unioxx5_subdev_init(struct comedi_device *dev,
367 struct comedi_subdevice *s,
370 struct unioxx5_subd_priv *usp;
371 int i, to, ndef_flag = 0;
374 usp = kzalloc(sizeof(*usp), GFP_KERNEL);
378 ret = __comedi_request_region(dev, iobase, UNIOXX5_SIZE);
383 usp->usp_iobase = iobase;
385 /* defining modules types */
386 for (i = 0; i < 12; i++) {
389 __unioxx5_analog_config(usp, i * 2);
390 /* sends channel number to card */
391 outb(i + 1, iobase + 5);
392 outb('H', iobase + 6); /* requests EEPROM world */
393 while (!(inb(iobase + 0) & TxBE))
394 ; /* waits while writting will be allowed */
397 /* waits while reading of two bytes will be allowed */
398 while (!(inb(iobase + 0) & Rx2CA)) {
406 usp->usp_module_type[i] = 0;
409 usp->usp_module_type[i] = inb(iobase + 6);
414 /* initial subdevice for digital or analog i/o */
415 s->type = COMEDI_SUBD_DIO;
417 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
418 s->n_chan = UNIOXX5_NUM_OF_CHANS;
420 s->range_table = &range_digital;
421 s->insn_read = unioxx5_subdev_read;
422 s->insn_write = unioxx5_subdev_write;
423 /* for digital modules only!!! */
424 s->insn_config = unioxx5_insn_config;
429 static int unioxx5_attach(struct comedi_device *dev,
430 struct comedi_devconfig *it)
432 struct comedi_subdevice *s;
433 int iobase, i, n_subd;
437 iobase = it->options[0];
439 dev->iobase = iobase;
440 iobase += UNIOXX5_SUBDEV_BASE;
442 /* defining number of subdevices and getting they types (it must be 'g01') */
443 for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
447 if (id != 'g' || num != 1)
453 /* unioxx5 can has from two to four subdevices */
455 dev_err(dev->class_dev,
456 "your card must has at least 2 'g01' subdevices\n");
460 ret = comedi_alloc_subdevices(dev, n_subd);
464 /* initializing each of for same subdevices */
465 for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
466 s = &dev->subdevices[i];
467 ret = __unioxx5_subdev_init(dev, s, iobase);
475 static void unioxx5_detach(struct comedi_device *dev)
478 struct comedi_subdevice *subdev;
479 struct unioxx5_subd_priv *usp;
481 for (i = 0; i < dev->n_subdevices; i++) {
482 subdev = &dev->subdevices[i];
483 usp = subdev->private;
484 release_region(usp->usp_iobase, UNIOXX5_SIZE);
485 kfree(subdev->private);
489 static struct comedi_driver unioxx5_driver = {
490 .driver_name = DRIVER_NAME,
491 .module = THIS_MODULE,
492 .attach = unioxx5_attach,
493 .detach = unioxx5_detach,
495 module_comedi_driver(unioxx5_driver);
497 MODULE_AUTHOR("Comedi http://www.comedi.org");
498 MODULE_DESCRIPTION("Comedi low-level driver");
499 MODULE_LICENSE("GPL");