Linux preempt-rt

Check our new training course

Real-Time Linux with PREEMPT_RT

Check our new training course
with Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/***************************************************************************
 *                                                                         *
 *  comedi/drivers/unioxx5.c                                               *
 *  Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.           *
 *                                                                         *
 *  Copyright (C) 2006 Kruchinin Daniil (asgard) [asgard@etersoft.ru]      *
 *                                                                         *
 *  COMEDI - Linux Control and Measurement Device Interface                *
 *  Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>              *
 *                                                                         *
 *  This program is free software; you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 2 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program; if not, write to the Free Software            *
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              *
 *                                                                         *
 ***************************************************************************/
/*

Driver: unioxx5
Description: Driver for Fastwel UNIOxx-5 (analog and digital i/o) boards.
Author: Kruchinin Daniil (asgard) <asgard@etersoft.ru>
Status: unknown
Updated: 2006-10-09
Devices: [Fastwel] UNIOxx-5 (unioxx5),

 This card supports digital and analog I/O. It written for g01
 subdevices only.
 channels range: 0 .. 23 dio channels
 and 0 .. 11 analog modules range
 During attaching unioxx5 module displays modules identifiers
 (see dmesg after comedi_config) in format:
 | [module_number] module_id |

*/

#include "../comedidev.h"
#include <linux/ioport.h>

#define DRIVER_NAME "unioxx5"
#define UNIOXX5_SIZE 0x10
#define UNIOXX5_SUBDEV_BASE 0xA000	/* base addr of first subdev */
#define UNIOXX5_SUBDEV_ODDS 0x400

/* modules types */
#define MODULE_DIGITAL 0
#define MODULE_OUTPUT_MASK 0x80	/* analog input/output */

/* constants for digital i/o */
#define UNIOXX5_NUM_OF_CHANS 24

/* constants for analog i/o */
#define TxBE  0x10		/* transmit buffer enable */
#define RxCA  0x20		/* 1 receive character available */
#define Rx2CA 0x40		/* 2 receive character available */
#define Rx4CA 0x80		/* 4 receive character available */

/* bytes mask errors */
#define Rx2CA_ERR_MASK 0x04	/* 2 bytes receiving error */
#define Rx4CA_ERR_MASK 0x08	/* 4 bytes receiving error */

/* channel modes */
#define ALL_2_INPUT  0		/* config all digital channels to input */
#define ALL_2_OUTPUT 1		/* config all digital channels to output */

/* 'private' structure for each subdevice */
struct unioxx5_subd_priv {
	int usp_iobase;
	unsigned char usp_module_type[12];	/* 12 modules. each can be 70L or 73L */
	unsigned char usp_extra_data[12][4];	/* for saving previous written value for analog modules */
	unsigned char usp_prev_wr_val[3];	/* previous written value */
	unsigned char usp_prev_cn_val[3];	/* previous channel value */
};

static int unioxx5_attach(struct comedi_device *dev,
			  struct comedi_devconfig *it);
static int unioxx5_subdev_write(struct comedi_device *dev,
				struct comedi_subdevice *subdev,
				struct comedi_insn *insn, unsigned int *data);
static int unioxx5_subdev_read(struct comedi_device *dev,
			       struct comedi_subdevice *subdev,
			       struct comedi_insn *insn, unsigned int *data);
static int unioxx5_insn_config(struct comedi_device *dev,
			       struct comedi_subdevice *subdev,
			       struct comedi_insn *insn, unsigned int *data);
static int unioxx5_detach(struct comedi_device *dev);
static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
				 int subdev_iobase, int minor);
static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
				   unsigned int *data, int channel, int minor);
static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
				  unsigned int *data, int channel, int minor);
/* static void __unioxx5_digital_config(struct unioxx5_subd_priv* usp, int mode); */
static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
				  unsigned int *data, int channel, int minor);
static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
				 unsigned int *data, int channel, int minor);
static int __unioxx5_define_chan_offset(int chan_num);
static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel);

static struct comedi_driver unioxx5_driver = {
	.driver_name = DRIVER_NAME,
	.module = THIS_MODULE,
	.attach = unioxx5_attach,
	.detach = unioxx5_detach
};

COMEDI_INITCLEANUP(unioxx5_driver);

static int unioxx5_attach(struct comedi_device *dev,
			  struct comedi_devconfig *it)
{
	int iobase, i, n_subd;
	int id, num, ba;

	iobase = it->options[0];

	dev->board_name = DRIVER_NAME;
	dev->iobase = iobase;
	iobase += UNIOXX5_SUBDEV_BASE;

	/* defining number of subdevices and getting they types (it must be 'g01')  */
	for (i = n_subd = 0, ba = iobase; i < 4; i++, ba += UNIOXX5_SUBDEV_ODDS) {
		id = inb(ba + 0xE);
		num = inb(ba + 0xF);

		if (id != 'g' || num != 1)
			continue;

		n_subd++;
	}

	/* unioxx5 can has from two to four subdevices */
	if (n_subd < 2) {
		printk(KERN_ERR
		       "your card must has at least 2 'g01' subdevices\n");
		return -1;
	}

	if (alloc_subdevices(dev, n_subd) < 0) {
		printk(KERN_ERR "out of memory\n");
		return -ENOMEM;
	}

	/* initializing each of for same subdevices */
	for (i = 0; i < n_subd; i++, iobase += UNIOXX5_SUBDEV_ODDS) {
		if (__unioxx5_subdev_init(&dev->subdevices[i], iobase,
					  dev->minor) < 0)
			return -1;
	}

	printk("attached\n");
	return 0;
}

static int unioxx5_subdev_read(struct comedi_device *dev,
			       struct comedi_subdevice *subdev,
			       struct comedi_insn *insn, unsigned int *data)
{
	struct unioxx5_subd_priv *usp = subdev->private;
	int channel, type;

	channel = CR_CHAN(insn->chanspec);
	type = usp->usp_module_type[channel / 2];	/* defining module type(analog or digital) */

	if (type == MODULE_DIGITAL) {
		if (!__unioxx5_digital_read(usp, data, channel, dev->minor))
			return -1;
	} else {
		if (!__unioxx5_analog_read(usp, data, channel, dev->minor))
			return -1;
	}

	return 1;
}

static int unioxx5_subdev_write(struct comedi_device *dev,
				struct comedi_subdevice *subdev,
				struct comedi_insn *insn, unsigned int *data)
{
	struct unioxx5_subd_priv *usp = subdev->private;
	int channel, type;

	channel = CR_CHAN(insn->chanspec);
	type = usp->usp_module_type[channel / 2];	/* defining module type(analog or digital) */

	if (type == MODULE_DIGITAL) {
		if (!__unioxx5_digital_write(usp, data, channel, dev->minor))
			return -1;
	} else {
		if (!__unioxx5_analog_write(usp, data, channel, dev->minor))
			return -1;
	}

	return 1;
}

/* for digital modules only */
static int unioxx5_insn_config(struct comedi_device *dev,
			       struct comedi_subdevice *subdev,
			       struct comedi_insn *insn, unsigned int *data)
{
	int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
	struct unioxx5_subd_priv *usp = subdev->private;
	int mask = 1 << (channel & 0x07);

	type = usp->usp_module_type[channel / 2];

	if (type != MODULE_DIGITAL) {
		printk(KERN_ERR
		       "comedi%d: channel configuration accessible only for digital modules\n",
		       dev->minor);
		return -1;
	}

	channel_offset = __unioxx5_define_chan_offset(channel);
	if (channel_offset < 0) {
		printk(KERN_ERR
		       "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
		       dev->minor, channel);
		return -1;
	}

	/* gets previously written value */
	flags = usp->usp_prev_cn_val[channel_offset - 1];

	switch (*data) {
	case COMEDI_INPUT:
		flags &= ~mask;
		break;
	case COMEDI_OUTPUT:
		flags |= mask;
		break;
	default:
		printk(KERN_ERR "comedi%d: unknown flag\n", dev->minor);
		return -1;
	}

	/*                                                        *\
	 * sets channels buffer to 1(after this we are allowed to *
	 * change channel type on input or output)                *
	 \*                                                        */
	outb(1, usp->usp_iobase + 0);
	outb(flags, usp->usp_iobase + channel_offset);	/* changes type of _one_ channel */
	outb(0, usp->usp_iobase + 0);	/* sets channels bank to 0(allows directly input/output) */
	usp->usp_prev_cn_val[channel_offset - 1] = flags;	/* saves written value */

	return 0;
}

static int unioxx5_detach(struct comedi_device *dev)
{
	int i;
	struct comedi_subdevice *subdev;
	struct unioxx5_subd_priv *usp;

	for (i = 0; i < dev->n_subdevices; i++) {
		subdev = &dev->subdevices[i];
		usp = subdev->private;
		release_region(usp->usp_iobase, UNIOXX5_SIZE);
		kfree(subdev->private);
	}

	return 0;
}

/* initializing subdevice with given address */
static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
				 int subdev_iobase, int minor)
{
	struct unioxx5_subd_priv *usp;
	int i, to, ndef_flag = 0;

	if (!request_region(subdev_iobase, UNIOXX5_SIZE, DRIVER_NAME)) {
		printk(KERN_ERR "comedi%d: I/O port conflict\n", minor);
		return -EIO;
	}

	usp = (struct unioxx5_subd_priv *)kzalloc(sizeof(*usp), GFP_KERNEL);

	if (usp == NULL) {
		printk(KERN_ERR "comedi%d: erorr! --> out of memory!\n", minor);
		return -1;
	}

	usp->usp_iobase = subdev_iobase;
	printk("comedi%d: |", minor);

	/* defining modules types */
	for (i = 0; i < 12; i++) {
		to = 10000;

		__unioxx5_analog_config(usp, i * 2);
		outb(i + 1, subdev_iobase + 5);	/* sends channel number to card */
		outb('H', subdev_iobase + 6);	/* requests EEPROM world */
		while (!(inb(subdev_iobase + 0) & TxBE)) ;	/* waits while writting will be allowed */
		outb(0, subdev_iobase + 6);

		/* waits while reading of two bytes will be allowed */
		while (!(inb(subdev_iobase + 0) & Rx2CA)) {
			if (--to <= 0) {
				ndef_flag = 1;
				break;
			}
		}

		if (ndef_flag) {
			usp->usp_module_type[i] = 0;
			ndef_flag = 0;
		} else
			usp->usp_module_type[i] = inb(subdev_iobase + 6);

		printk(" [%d] 0x%02x |", i, usp->usp_module_type[i]);
		udelay(1);
	}

	printk("\n");

	/* initial subdevice for digital or analog i/o */
	subdev->type = COMEDI_SUBD_DIO;
	subdev->private = usp;
	subdev->subdev_flags = SDF_READABLE | SDF_WRITABLE;
	subdev->n_chan = UNIOXX5_NUM_OF_CHANS;
	subdev->maxdata = 0xFFF;
	subdev->range_table = &range_digital;
	subdev->insn_read = unioxx5_subdev_read;
	subdev->insn_write = unioxx5_subdev_write;
	subdev->insn_config = unioxx5_insn_config;	/* for digital modules only!!! */

	printk("subdevice configured\n");

	return 0;
}

static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
				   unsigned int *data, int channel, int minor)
{
	int channel_offset, val;
	int mask = 1 << (channel & 0x07);

	channel_offset = __unioxx5_define_chan_offset(channel);
	if (channel_offset < 0) {
		printk(KERN_ERR
		       "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
		       minor, channel);
		return 0;
	}

	val = usp->usp_prev_wr_val[channel_offset - 1];	/* getting previous written value */

	if (*data)
		val |= mask;
	else
		val &= ~mask;

	outb(val, usp->usp_iobase + channel_offset);
	usp->usp_prev_wr_val[channel_offset - 1] = val;	/* saving new written value */

	return 1;
}

/* function for digital reading */
static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
				  unsigned int *data, int channel, int minor)
{
	int channel_offset, mask = 1 << (channel & 0x07);

	channel_offset = __unioxx5_define_chan_offset(channel);
	if (channel_offset < 0) {
		printk(KERN_ERR
		       "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
		       minor, channel);
		return 0;
	}

	*data = inb(usp->usp_iobase + channel_offset);
	*data &= mask;

	if (channel_offset > 1)
		channel -= 2 << channel_offset;	/* this operation is created for correct readed value to 0 or 1 */

	*data >>= channel;
	return 1;
}

#if 0				/* not used? */
static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
{
	int i, mask;

	mask = (mode == ALL_2_OUTPUT) ? 0xFF : 0x00;
	printk("COMEDI: mode = %d\n", mask);

	outb(1, usp->usp_iobase + 0);

	for (i = 0; i < 3; i++)
		outb(mask, usp->usp_iobase + i);

	outb(0, usp->usp_iobase + 0);
}
#endif

static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
				  unsigned int *data, int channel, int minor)
{
	int module, i;

	module = channel / 2;	/* definig module number(0 .. 11) */
	i = (channel % 2) << 1;	/* depends on type of channel (A or B) */

	/* defining if given module can work on output */
	if (!(usp->usp_module_type[module] & MODULE_OUTPUT_MASK)) {
		printk(KERN_ERR
		       "comedi%d: module in position %d with id 0x%0x is for input only!\n",
		       minor, module, usp->usp_module_type[module]);
		return 0;
	}

	__unioxx5_analog_config(usp, channel);
	/* saving minor byte */
	usp->usp_extra_data[module][i++] = (unsigned char)(*data & 0x00FF);
	/* saving major byte */
	usp->usp_extra_data[module][i] = (unsigned char)((*data & 0xFF00) >> 8);

	/* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
	outb(module + 1, usp->usp_iobase + 5);	/* sending module number to card(1 .. 12) */
	outb('W', usp->usp_iobase + 6);	/* sends (W)rite command to module */

	/* sending for bytes to module(one byte per cycle iteration) */
	for (i = 0; i < 4; i++) {
		while (!((inb(usp->usp_iobase + 0)) & TxBE)) ;	/* waits while writting will be allowed */
		outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
	}

	return 1;
}

static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
				 unsigned int *data, int channel, int minor)
{
	int module_no, read_ch;
	char control;

	module_no = channel / 2;
	read_ch = channel % 2;	/* depend on type of channel (A or B) */

	/* defining if given module can work on input */
	if (usp->usp_module_type[module_no] & MODULE_OUTPUT_MASK) {
		printk(KERN_ERR
		       "comedi%d: module in position %d with id 0x%02x is for output only",
		       minor, module_no, usp->usp_module_type[module_no]);
		return 0;
	}

	__unioxx5_analog_config(usp, channel);
	outb(module_no + 1, usp->usp_iobase + 5);	/* sends module number to card(1 .. 12) */
	outb('V', usp->usp_iobase + 6);	/* sends to module (V)erify command */
	control = inb(usp->usp_iobase);	/* get control register byte */

	/* waits while reading four bytes will be allowed */
	while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA)) ;

	/* if four bytes readding error occurs - return 0(false) */
	if ((control & Rx4CA_ERR_MASK)) {
		printk("COMEDI: 4 bytes error\n");
		return 0;
	}

	if (read_ch)
		*data = inw(usp->usp_iobase + 6);	/* channel B */
	else
		*data = inw(usp->usp_iobase + 4);	/* channel A */

	return 1;
}

/* configure channels for analog i/o (even to output, odd to input) */
static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
{
	int chan_a, chan_b, conf, channel_offset;

	channel_offset = __unioxx5_define_chan_offset(channel);
	conf = usp->usp_prev_cn_val[channel_offset - 1];
	chan_a = chan_b = 1;

	/* setting channel A and channel B mask */
	if (channel % 2 == 0) {
		chan_a <<= channel & 0x07;
		chan_b <<= (channel + 1) & 0x07;
	} else {
		chan_a <<= (channel - 1) & 0x07;
		chan_b <<= channel & 0x07;
	}

	conf |= chan_a;		/* even channel ot output */
	conf &= ~chan_b;	/* odd channel to input */

	outb(1, usp->usp_iobase + 0);
	outb(conf, usp->usp_iobase + channel_offset);
	outb(0, usp->usp_iobase + 0);

	usp->usp_prev_cn_val[channel_offset - 1] = conf;
}

/*                                                    *\
 * this function defines if the given channel number  *
 * enters in default numeric interspace(from 0 to 23) *
 * and it returns address offset for usage needed     *
 * channel.                                           *
\*                                                    */

static int __unioxx5_define_chan_offset(int chan_num)
{

	if (chan_num < 0 || chan_num > 23)
		return -1;

	return (chan_num >> 3) + 1;
}