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 #include "../comedidev.h"
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
49 #define DRIVER_NAME "unioxx5"
50 #define UNIOXX5_SIZE 0x10
51 #define UNIOXX5_SUBDEV_BASE 0xA000 /* base addr of first subdev */
52 #define UNIOXX5_SUBDEV_ODDS 0x400
55 #define MODULE_DIGITAL 0
56 #define MODULE_OUTPUT_MASK 0x80 /* analog input/output */
58 /* constants for digital i/o */
59 #define UNIOXX5_NUM_OF_CHANS 24
61 /* constants for analog i/o */
62 #define TxBE 0x10 /* transmit buffer enable */
63 #define RxCA 0x20 /* 1 receive character available */
64 #define Rx2CA 0x40 /* 2 receive character available */
65 #define Rx4CA 0x80 /* 4 receive character available */
67 /* bytes mask errors */
68 #define Rx2CA_ERR_MASK 0x04 /* 2 bytes receiving error */
69 #define Rx4CA_ERR_MASK 0x08 /* 4 bytes receiving error */
72 #define ALL_2_INPUT 0 /* config all digital channels to input */
73 #define ALL_2_OUTPUT 1 /* config all digital channels to output */
75 /* 'private' structure for each subdevice */
76 struct unioxx5_subd_priv {
78 unsigned char usp_module_type[12]; /* 12 modules. each can be 70L or 73L */
79 unsigned char usp_extra_data[12][4]; /* for saving previous written value for analog modules */
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_attach(struct comedi_device *dev,
85 struct comedi_devconfig *it);
86 static int unioxx5_subdev_write(struct comedi_device *dev,
87 struct comedi_subdevice *subdev,
88 struct comedi_insn *insn, unsigned int *data);
89 static int unioxx5_subdev_read(struct comedi_device *dev,
90 struct comedi_subdevice *subdev,
91 struct comedi_insn *insn, unsigned int *data);
92 static int unioxx5_insn_config(struct comedi_device *dev,
93 struct comedi_subdevice *subdev,
94 struct comedi_insn *insn, unsigned int *data);
95 static int unioxx5_detach(struct comedi_device *dev);
96 static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
97 int subdev_iobase, int minor);
98 static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
99 unsigned int *data, int channel, int minor);
100 static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
101 unsigned int *data, int channel, int minor);
102 /* static void __unioxx5_digital_config(struct unioxx5_subd_priv* usp, int mode); */
103 static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
104 unsigned int *data, int channel, int minor);
105 static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
106 unsigned int *data, int channel, int minor);
107 static int __unioxx5_define_chan_offset(int chan_num);
108 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel);
110 static struct comedi_driver unioxx5_driver = {
111 .driver_name = DRIVER_NAME,
112 .module = THIS_MODULE,
113 .attach = unioxx5_attach,
114 .detach = unioxx5_detach
117 static int __init unioxx5_driver_init_module(void)
119 return comedi_driver_register(&unioxx5_driver);
122 static void __exit unioxx5_driver_cleanup_module(void)
124 comedi_driver_unregister(&unioxx5_driver);
127 module_init(unioxx5_driver_init_module);
128 module_exit(unioxx5_driver_cleanup_module);
130 static int unioxx5_attach(struct comedi_device *dev,
131 struct comedi_devconfig *it)
133 int iobase, i, n_subd;
136 iobase = it->options[0];
138 dev->board_name = DRIVER_NAME;
139 dev->iobase = iobase;
140 iobase += UNIOXX5_SUBDEV_BASE;
142 /* defining number of subdevices and getting they types (it must be 'g01') */
143 for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
147 if (id != 'g' || num != 1)
153 /* unioxx5 can has from two to four subdevices */
156 "your card must has at least 2 'g01' subdevices\n");
160 if (alloc_subdevices(dev, n_subd) < 0) {
161 printk(KERN_ERR "out of memory\n");
165 /* initializing each of for same subdevices */
166 for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
167 if (__unioxx5_subdev_init(&dev->subdevices[i], iobase,
172 printk("attached\n");
176 static int unioxx5_subdev_read(struct comedi_device *dev,
177 struct comedi_subdevice *subdev,
178 struct comedi_insn *insn, unsigned int *data)
180 struct unioxx5_subd_priv *usp = subdev->private;
183 channel = CR_CHAN(insn->chanspec);
184 type = usp->usp_module_type[channel / 2]; /* defining module type(analog or digital) */
186 if (type == MODULE_DIGITAL) {
187 if (!__unioxx5_digital_read(usp, data, channel, dev->minor))
190 if (!__unioxx5_analog_read(usp, data, channel, dev->minor))
197 static int unioxx5_subdev_write(struct comedi_device *dev,
198 struct comedi_subdevice *subdev,
199 struct comedi_insn *insn, unsigned int *data)
201 struct unioxx5_subd_priv *usp = subdev->private;
204 channel = CR_CHAN(insn->chanspec);
205 type = usp->usp_module_type[channel / 2]; /* defining module type(analog or digital) */
207 if (type == MODULE_DIGITAL) {
208 if (!__unioxx5_digital_write(usp, data, channel, dev->minor))
211 if (!__unioxx5_analog_write(usp, data, channel, dev->minor))
218 /* for digital modules only */
219 static int unioxx5_insn_config(struct comedi_device *dev,
220 struct comedi_subdevice *subdev,
221 struct comedi_insn *insn, unsigned int *data)
223 int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
224 struct unioxx5_subd_priv *usp = subdev->private;
225 int mask = 1 << (channel & 0x07);
227 type = usp->usp_module_type[channel / 2];
229 if (type != MODULE_DIGITAL) {
231 "comedi%d: channel configuration accessible only for digital modules\n",
236 channel_offset = __unioxx5_define_chan_offset(channel);
237 if (channel_offset < 0) {
239 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
240 dev->minor, channel);
244 /* gets previously written value */
245 flags = usp->usp_prev_cn_val[channel_offset - 1];
255 printk(KERN_ERR "comedi%d: unknown flag\n", dev->minor);
260 * sets channels buffer to 1(after this we are allowed to *
261 * change channel type on input or output) *
263 outb(1, usp->usp_iobase + 0);
264 outb(flags, usp->usp_iobase + channel_offset); /* changes type of _one_ channel */
265 outb(0, usp->usp_iobase + 0); /* sets channels bank to 0(allows directly input/output) */
266 usp->usp_prev_cn_val[channel_offset - 1] = flags; /* saves written value */
271 static int unioxx5_detach(struct comedi_device *dev)
274 struct comedi_subdevice *subdev;
275 struct unioxx5_subd_priv *usp;
277 for (i = 0; i < dev->n_subdevices; i++) {
278 subdev = &dev->subdevices[i];
279 usp = subdev->private;
280 release_region(usp->usp_iobase, UNIOXX5_SIZE);
281 kfree(subdev->private);
287 /* initializing subdevice with given address */
288 static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
289 int subdev_iobase, int minor)
291 struct unioxx5_subd_priv *usp;
292 int i, to, ndef_flag = 0;
294 if (!request_region(subdev_iobase, UNIOXX5_SIZE, DRIVER_NAME)) {
295 printk(KERN_ERR "comedi%d: I/O port conflict\n", minor);
299 usp = kzalloc(sizeof(*usp), GFP_KERNEL);
302 printk(KERN_ERR "comedi%d: erorr! --> out of memory!\n", minor);
306 usp->usp_iobase = subdev_iobase;
307 printk("comedi%d: |", minor);
309 /* defining modules types */
310 for (i = 0; i < 12; i++) {
313 __unioxx5_analog_config(usp, i * 2);
314 outb(i + 1, subdev_iobase + 5); /* sends channel number to card */
315 outb('H', subdev_iobase + 6); /* requests EEPROM world */
316 while (!(inb(subdev_iobase + 0) & TxBE))
317 ; /* waits while writting will be allowed */
318 outb(0, subdev_iobase + 6);
320 /* waits while reading of two bytes will be allowed */
321 while (!(inb(subdev_iobase + 0) & Rx2CA)) {
329 usp->usp_module_type[i] = 0;
332 usp->usp_module_type[i] = inb(subdev_iobase + 6);
334 printk(" [%d] 0x%02x |", i, usp->usp_module_type[i]);
340 /* initial subdevice for digital or analog i/o */
341 subdev->type = COMEDI_SUBD_DIO;
342 subdev->private = usp;
343 subdev->subdev_flags = SDF_READABLE | SDF_WRITABLE;
344 subdev->n_chan = UNIOXX5_NUM_OF_CHANS;
345 subdev->maxdata = 0xFFF;
346 subdev->range_table = &range_digital;
347 subdev->insn_read = unioxx5_subdev_read;
348 subdev->insn_write = unioxx5_subdev_write;
349 subdev->insn_config = unioxx5_insn_config; /* for digital modules only!!! */
351 printk("subdevice configured\n");
356 static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
357 unsigned int *data, int channel, int minor)
359 int channel_offset, val;
360 int mask = 1 << (channel & 0x07);
362 channel_offset = __unioxx5_define_chan_offset(channel);
363 if (channel_offset < 0) {
365 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
370 val = usp->usp_prev_wr_val[channel_offset - 1]; /* getting previous written value */
377 outb(val, usp->usp_iobase + channel_offset);
378 usp->usp_prev_wr_val[channel_offset - 1] = val; /* saving new written value */
383 /* function for digital reading */
384 static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
385 unsigned int *data, int channel, int minor)
387 int channel_offset, mask = 1 << (channel & 0x07);
389 channel_offset = __unioxx5_define_chan_offset(channel);
390 if (channel_offset < 0) {
392 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
397 *data = inb(usp->usp_iobase + channel_offset);
400 if (channel_offset > 1)
401 channel -= 2 << channel_offset; /* this operation is created for correct readed value to 0 or 1 */
407 #if 0 /* not used? */
408 static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
412 mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
413 printk("COMEDI: mode = %d\n", mask);
415 outb(1, usp->usp_iobase + 0);
417 for (i = 0; i < 3; i++)
418 outb(mask, usp->usp_iobase + i);
420 outb(0, usp->usp_iobase + 0);
424 static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
425 unsigned int *data, int channel, int minor)
429 module = channel / 2; /* definig module number(0 .. 11) */
430 i = (channel % 2) << 1; /* depends on type of channel (A or B) */
432 /* defining if given module can work on output */
433 if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
435 "comedi%d: module in position %d with id 0x%0x is for input only!\n",
436 minor, module, usp->usp_module_type[module]);
440 __unioxx5_analog_config(usp, channel);
441 /* saving minor byte */
442 usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
443 /* saving major byte */
444 usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
446 /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
447 outb(module + 1, usp->usp_iobase + 5); /* sending module number to card(1 .. 12) */
448 outb('W', usp->usp_iobase + 6); /* sends (W)rite command to module */
450 /* sending for bytes to module(one byte per cycle iteration) */
451 for (i = 0; i < 4; i++) {
452 while (!((inb(usp->usp_iobase + 0)) & TxBE))
453 ; /* waits while writting will be allowed */
454 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
460 static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
461 unsigned int *data, int channel, int minor)
463 int module_no, read_ch;
466 module_no = channel / 2;
467 read_ch = channel % 2; /* depend on type of channel (A or B) */
469 /* defining if given module can work on input */
470 if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
472 "comedi%d: module in position %d with id 0x%02x is for output only",
473 minor, module_no, usp->usp_module_type[module_no]);
477 __unioxx5_analog_config(usp, channel);
478 outb(module_no + 1, usp->usp_iobase + 5); /* sends module number to card(1 .. 12) */
479 outb('V', usp->usp_iobase + 6); /* sends to module (V)erify command */
480 control = inb(usp->usp_iobase); /* get control register byte */
482 /* waits while reading four bytes will be allowed */
483 while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
486 /* if four bytes readding error occurs - return 0(false) */
487 if ((control & Rx4CA_ERR_MASK)) {
488 printk("COMEDI: 4 bytes error\n");
493 *data = inw(usp->usp_iobase + 6); /* channel B */
495 *data = inw(usp->usp_iobase + 4); /* channel A */
500 /* configure channels for analog i/o (even to output, odd to input) */
501 static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
503 int chan_a, chan_b, conf, channel_offset;
505 channel_offset = __unioxx5_define_chan_offset(channel);
506 conf = usp->usp_prev_cn_val[channel_offset - 1];
509 /* setting channel A and channel B mask */
510 if (channel % 2 == 0) {
511 chan_a <<= channel & 0x07;
512 chan_b <<= (channel + 1) & 0x07;
514 chan_a <<= (channel - 1) & 0x07;
515 chan_b <<= channel & 0x07;
518 conf |= chan_a; /* even channel ot output */
519 conf &= ~chan_b; /* odd channel to input */
521 outb(1, usp->usp_iobase + 0);
522 outb(conf, usp->usp_iobase + channel_offset);
523 outb(0, usp->usp_iobase + 0);
525 usp->usp_prev_cn_val[channel_offset - 1] = conf;
529 * this function defines if the given channel number *
530 * enters in default numeric interspace(from 0 to 23) *
531 * and it returns address offset for usage needed *
535 static int __unioxx5_define_chan_offset(int chan_num)
538 if (chan_num < 0 || chan_num > 23)
541 return (chan_num >> 3) + 1;
544 MODULE_AUTHOR("Comedi http://www.comedi.org");
545 MODULE_DESCRIPTION("Comedi low-level driver");
546 MODULE_LICENSE("GPL");