unioxx5.c revision 0a85b6f0ab0d2edb0d41b32697111ce0e4f43496
1/***************************************************************************
2 *                                                                         *
3 *  comedi/drivers/unioxx5.c                                               *
4 *  Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.           *
5 *                                                                         *
6 *  Copyright (C) 2006 Kruchinin Daniil (asgard) [asgard@etersoft.ru]      *
7 *                                                                         *
8 *  COMEDI - Linux Control and Measurement Device Interface                *
9 *  Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>              *
10 *                                                                         *
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.                                    *
15 *                                                                         *
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.                           *
20 *                                                                         *
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.              *
24 *                                                                         *
25 ***************************************************************************/
26/*
27
28Driver: unioxx5
29Description: Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.
30Author: Kruchinin Daniil (asgard) <asgard@etersoft.ru>
31Status: unknown
32Updated: 2006-10-09
33Devices: [Fastwel] UNIOxx-5 (unioxx5),
34
35 This card supports digital and analog I/O. It written for g01
36 subdevices only.
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 |
42
43*/
44
45#include "../comedidev.h"
46#include <linux/ioport.h>
47
48#define DRIVER_NAME "unioxx5"
49#define UNIOXX5_SIZE 0x10
50#define UNIOXX5_SUBDEV_BASE 0xA000	/* base addr of first subdev */
51#define UNIOXX5_SUBDEV_ODDS 0x400
52
53/* modules types */
54#define MODULE_DIGITAL 0
55#define MODULE_OUTPUT_MASK 0x80	/* analog input/output */
56
57/* constants for digital i/o */
58#define UNIOXX5_NUM_OF_CHANS 24
59
60/* constants for analog i/o */
61#define TxBE  0x10		/* transmit buffer enable */
62#define RxCA  0x20		/* 1 receive character available */
63#define Rx2CA 0x40		/* 2 receive character available */
64#define Rx4CA 0x80		/* 4 receive character available */
65
66/* bytes mask errors */
67#define Rx2CA_ERR_MASK 0x04	/* 2 bytes receiving error */
68#define Rx4CA_ERR_MASK 0x08	/* 4 bytes receiving error */
69
70/* channel modes */
71#define ALL_2_INPUT  0		/* config all digital channels to input */
72#define ALL_2_OUTPUT 1		/* config all digital channels to output */
73
74/* 'private' structure for each subdevice */
75struct unioxx5_subd_priv {
76	int usp_iobase;
77	unsigned char usp_module_type[12];	/* 12 modules. each can be 70L or 73L */
78	unsigned char usp_extra_data[12][4];	/* for saving previous written value for analog modules */
79	unsigned char usp_prev_wr_val[3];	/* previous written value */
80	unsigned char usp_prev_cn_val[3];	/* previous channel value */
81};
82
83static int unioxx5_attach(struct comedi_device *dev,
84			  struct comedi_devconfig *it);
85static int unioxx5_subdev_write(struct comedi_device *dev,
86				struct comedi_subdevice *subdev,
87				struct comedi_insn *insn, unsigned int *data);
88static int unioxx5_subdev_read(struct comedi_device *dev,
89			       struct comedi_subdevice *subdev,
90			       struct comedi_insn *insn, unsigned int *data);
91static int unioxx5_insn_config(struct comedi_device *dev,
92			       struct comedi_subdevice *subdev,
93			       struct comedi_insn *insn, unsigned int *data);
94static int unioxx5_detach(struct comedi_device *dev);
95static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
96				 int subdev_iobase, int minor);
97static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
98				   unsigned int *data, int channel, int minor);
99static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
100				  unsigned int *data, int channel, int minor);
101/* static void __unioxx5_digital_config(struct unioxx5_subd_priv* usp, int mode); */
102static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
103				  unsigned int *data, int channel, int minor);
104static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
105				 unsigned int *data, int channel, int minor);
106static int __unioxx5_define_chan_offset(int chan_num);
107static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel);
108
109static struct comedi_driver unioxx5_driver = {
110	.driver_name = DRIVER_NAME,
111	.module = THIS_MODULE,
112	.attach = unioxx5_attach,
113	.detach = unioxx5_detach
114};
115
116COMEDI_INITCLEANUP(unioxx5_driver);
117
118static int unioxx5_attach(struct comedi_device *dev,
119			  struct comedi_devconfig *it)
120{
121	int iobase, i, n_subd;
122	int id, num, ba;
123
124	iobase = it->options[0];
125
126	dev->board_name = DRIVER_NAME;
127	dev->iobase = iobase;
128	iobase += UNIOXX5_SUBDEV_BASE;
129
130	/* defining number of subdevices and getting they types (it must be 'g01')  */
131	for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
132		id = inb(ba + 0xE);
133		num = inb(ba + 0xF);
134
135		if (id != 'g' || num != 1)
136			continue;
137
138		n_subd++;
139	}
140
141	/* unioxx5 can has from two to four subdevices */
142	if (n_subd < 2) {
143		printk(KERN_ERR
144		       "your card must has at least 2 'g01' subdevices\n");
145		return -1;
146	}
147
148	if (alloc_subdevices(dev, n_subd) < 0) {
149		printk(KERN_ERR "out of memory\n");
150		return -ENOMEM;
151	}
152
153	/* initializing each of for same subdevices */
154	for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
155		if (__unioxx5_subdev_init(&dev->subdevices[i], iobase,
156					  dev->minor) < 0)
157			return -1;
158	}
159
160	printk("attached\n");
161	return 0;
162}
163
164static int unioxx5_subdev_read(struct comedi_device *dev,
165			       struct comedi_subdevice *subdev,
166			       struct comedi_insn *insn, unsigned int *data)
167{
168	struct unioxx5_subd_priv *usp = subdev->private;
169	int channel, type;
170
171	channel = CR_CHAN(insn->chanspec);
172	type = usp->usp_module_type[channel / 2];	/* defining module type(analog or digital) */
173
174	if (type == MODULE_DIGITAL) {
175		if (!__unioxx5_digital_read(usp, data, channel, dev->minor))
176			return -1;
177	} else {
178		if (!__unioxx5_analog_read(usp, data, channel, dev->minor))
179			return -1;
180	}
181
182	return 1;
183}
184
185static int unioxx5_subdev_write(struct comedi_device *dev,
186				struct comedi_subdevice *subdev,
187				struct comedi_insn *insn, unsigned int *data)
188{
189	struct unioxx5_subd_priv *usp = subdev->private;
190	int channel, type;
191
192	channel = CR_CHAN(insn->chanspec);
193	type = usp->usp_module_type[channel / 2];	/* defining module type(analog or digital) */
194
195	if (type == MODULE_DIGITAL) {
196		if (!__unioxx5_digital_write(usp, data, channel, dev->minor))
197			return -1;
198	} else {
199		if (!__unioxx5_analog_write(usp, data, channel, dev->minor))
200			return -1;
201	}
202
203	return 1;
204}
205
206/* for digital modules only */
207static int unioxx5_insn_config(struct comedi_device *dev,
208			       struct comedi_subdevice *subdev,
209			       struct comedi_insn *insn, unsigned int *data)
210{
211	int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
212	struct unioxx5_subd_priv *usp = subdev->private;
213	int mask = 1 << (channel & 0x07);
214
215	type = usp->usp_module_type[channel / 2];
216
217	if (type != MODULE_DIGITAL) {
218		printk(KERN_ERR
219		       "comedi%d: channel configuration accessible only for digital modules\n",
220		       dev->minor);
221		return -1;
222	}
223
224	channel_offset = __unioxx5_define_chan_offset(channel);
225	if (channel_offset < 0) {
226		printk(KERN_ERR
227		       "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
228		       dev->minor, channel);
229		return -1;
230	}
231
232	/* gets previously written value */
233	flags = usp->usp_prev_cn_val[channel_offset - 1];
234
235	switch (*data) {
236	case COMEDI_INPUT:
237		flags &= ~mask;
238		break;
239	case COMEDI_OUTPUT:
240		flags |= mask;
241		break;
242	default:
243		printk(KERN_ERR "comedi%d: unknown flag\n", dev->minor);
244		return -1;
245	}
246
247	/*                                                        *\
248	 * sets channels buffer to 1(after this we are allowed to *
249	 * change channel type on input or output)                *
250	 \*                                                        */
251	outb(1, usp->usp_iobase + 0);
252	outb(flags, usp->usp_iobase + channel_offset);	/* changes type of _one_ channel */
253	outb(0, usp->usp_iobase + 0);	/* sets channels bank to 0(allows directly input/output) */
254	usp->usp_prev_cn_val[channel_offset - 1] = flags;	/* saves written value */
255
256	return 0;
257}
258
259static int unioxx5_detach(struct comedi_device *dev)
260{
261	int i;
262	struct comedi_subdevice *subdev;
263	struct unioxx5_subd_priv *usp;
264
265	for (i = 0; i < dev->n_subdevices; i++) {
266		subdev = &dev->subdevices[i];
267		usp = subdev->private;
268		release_region(usp->usp_iobase, UNIOXX5_SIZE);
269		kfree(subdev->private);
270	}
271
272	return 0;
273}
274
275/* initializing subdevice with given address */
276static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
277				 int subdev_iobase, int minor)
278{
279	struct unioxx5_subd_priv *usp;
280	int i, to, ndef_flag = 0;
281
282	if (!request_region(subdev_iobase, UNIOXX5_SIZE, DRIVER_NAME)) {
283		printk(KERN_ERR "comedi%d: I/O port conflict\n", minor);
284		return -EIO;
285	}
286
287	usp = (struct unioxx5_subd_priv *)kzalloc(sizeof(*usp), GFP_KERNEL);
288
289	if (usp == NULL) {
290		printk(KERN_ERR "comedi%d: erorr! --> out of memory!\n", minor);
291		return -1;
292	}
293
294	usp->usp_iobase = subdev_iobase;
295	printk("comedi%d: |", minor);
296
297	/* defining modules types */
298	for (i = 0; i < 12; i++) {
299		to = 10000;
300
301		__unioxx5_analog_config(usp, i * 2);
302		outb(i + 1, subdev_iobase + 5);	/* sends channel number to card */
303		outb('H', subdev_iobase + 6);	/* requests EEPROM world */
304		while (!(inb(subdev_iobase + 0) & TxBE)) ;	/* waits while writting will be allowed */
305		outb(0, subdev_iobase + 6);
306
307		/* waits while reading of two bytes will be allowed */
308		while (!(inb(subdev_iobase + 0) & Rx2CA)) {
309			if (--to <= 0) {
310				ndef_flag = 1;
311				break;
312			}
313		}
314
315		if (ndef_flag) {
316			usp->usp_module_type[i] = 0;
317			ndef_flag = 0;
318		} else
319			usp->usp_module_type[i] = inb(subdev_iobase + 6);
320
321		printk(" [%d] 0x%02x |", i, usp->usp_module_type[i]);
322		udelay(1);
323	}
324
325	printk("\n");
326
327	/* initial subdevice for digital or analog i/o */
328	subdev->type = COMEDI_SUBD_DIO;
329	subdev->private = usp;
330	subdev->subdev_flags = SDF_READABLE | SDF_WRITABLE;
331	subdev->n_chan = UNIOXX5_NUM_OF_CHANS;
332	subdev->maxdata = 0xFFF;
333	subdev->range_table = &range_digital;
334	subdev->insn_read = unioxx5_subdev_read;
335	subdev->insn_write = unioxx5_subdev_write;
336	subdev->insn_config = unioxx5_insn_config;	/* for digital modules only!!! */
337
338	printk("subdevice configured\n");
339
340	return 0;
341}
342
343static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
344				   unsigned int *data, int channel, int minor)
345{
346	int channel_offset, val;
347	int mask = 1 << (channel & 0x07);
348
349	channel_offset = __unioxx5_define_chan_offset(channel);
350	if (channel_offset < 0) {
351		printk(KERN_ERR
352		       "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
353		       minor, channel);
354		return 0;
355	}
356
357	val = usp->usp_prev_wr_val[channel_offset - 1];	/* getting previous written value */
358
359	if (*data)
360		val |= mask;
361	else
362		val &= ~mask;
363
364	outb(val, usp->usp_iobase + channel_offset);
365	usp->usp_prev_wr_val[channel_offset - 1] = val;	/* saving new written value */
366
367	return 1;
368}
369
370/* function for digital reading */
371static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
372				  unsigned int *data, int channel, int minor)
373{
374	int channel_offset, mask = 1 << (channel & 0x07);
375
376	channel_offset = __unioxx5_define_chan_offset(channel);
377	if (channel_offset < 0) {
378		printk(KERN_ERR
379		       "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
380		       minor, channel);
381		return 0;
382	}
383
384	*data = inb(usp->usp_iobase + channel_offset);
385	*data &= mask;
386
387	if (channel_offset > 1)
388		channel -= 2 << channel_offset;	/* this operation is created for correct readed value to 0 or 1 */
389
390	*data >>= channel;
391	return 1;
392}
393
394#if 0				/* not used? */
395static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
396{
397	int i, mask;
398
399	mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
400	printk("COMEDI: mode = %d\n", mask);
401
402	outb(1, usp->usp_iobase + 0);
403
404	for (i = 0; i < 3; i++)
405		outb(mask, usp->usp_iobase + i);
406
407	outb(0, usp->usp_iobase + 0);
408}
409#endif
410
411static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
412				  unsigned int *data, int channel, int minor)
413{
414	int module, i;
415
416	module = channel / 2;	/* definig module number(0 .. 11) */
417	i = (channel % 2) << 1;	/* depends on type of channel (A or B) */
418
419	/* defining if given module can work on output */
420	if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
421		printk(KERN_ERR
422		       "comedi%d: module in position %d with id 0x%0x is for input only!\n",
423		       minor, module, usp->usp_module_type[module]);
424		return 0;
425	}
426
427	__unioxx5_analog_config(usp, channel);
428	/* saving minor byte */
429	usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
430	/* saving major byte */
431	usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);
432
433	/* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
434	outb(module + 1, usp->usp_iobase + 5);	/* sending module number to card(1 .. 12) */
435	outb('W', usp->usp_iobase + 6);	/* sends (W)rite command to module */
436
437	/* sending for bytes to module(one byte per cycle iteration) */
438	for (i = 0; i < 4; i++) {
439		while (!((inb(usp->usp_iobase + 0)) & TxBE)) ;	/* waits while writting will be allowed */
440		outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
441	}
442
443	return 1;
444}
445
446static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
447				 unsigned int *data, int channel, int minor)
448{
449	int module_no, read_ch;
450	char control;
451
452	module_no = channel / 2;
453	read_ch = channel % 2;	/* depend on type of channel (A or B) */
454
455	/* defining if given module can work on input */
456	if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
457		printk(KERN_ERR
458		       "comedi%d: module in position %d with id 0x%02x is for output only",
459		       minor, module_no, usp->usp_module_type[module_no]);
460		return 0;
461	}
462
463	__unioxx5_analog_config(usp, channel);
464	outb(module_no + 1, usp->usp_iobase + 5);	/* sends module number to card(1 .. 12) */
465	outb('V', usp->usp_iobase + 6);	/* sends to module (V)erify command */
466	control = inb(usp->usp_iobase);	/* get control register byte */
467
468	/* waits while reading four bytes will be allowed */
469	while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA)) ;
470
471	/* if four bytes readding error occurs - return 0(false) */
472	if ((control & Rx4CA_ERR_MASK)) {
473		printk("COMEDI: 4 bytes error\n");
474		return 0;
475	}
476
477	if (read_ch)
478		*data = inw(usp->usp_iobase + 6);	/* channel B */
479	else
480		*data = inw(usp->usp_iobase + 4);	/* channel A */
481
482	return 1;
483}
484
485/* configure channels for analog i/o (even to output, odd to input) */
486static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
487{
488	int chan_a, chan_b, conf, channel_offset;
489
490	channel_offset = __unioxx5_define_chan_offset(channel);
491	conf = usp->usp_prev_cn_val[channel_offset - 1];
492	chan_a = chan_b = 1;
493
494	/* setting channel A and channel B mask */
495	if (channel % 2 == 0) {
496		chan_a <<= channel & 0x07;
497		chan_b <<= (channel + 1) & 0x07;
498	} else {
499		chan_a <<= (channel - 1) & 0x07;
500		chan_b <<= channel & 0x07;
501	}
502
503	conf |= chan_a;		/* even channel ot output */
504	conf &= ~chan_b;	/* odd channel to input */
505
506	outb(1, usp->usp_iobase + 0);
507	outb(conf, usp->usp_iobase + channel_offset);
508	outb(0, usp->usp_iobase + 0);
509
510	usp->usp_prev_cn_val[channel_offset - 1] = conf;
511}
512
513/*                                                    *\
514 * this function defines if the given channel number  *
515 * enters in default numeric interspace(from 0 to 23) *
516 * and it returns address offset for usage needed     *
517 * channel.                                           *
518\*                                                    */
519
520static int __unioxx5_define_chan_offset(int chan_num)
521{
522
523	if (chan_num < 0 || chan_num > 23)
524		return -1;
525
526	return (chan_num >> 3) + 1;
527}
528