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