staging: comedi: remove debug tracing in vmk80xx driver
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / comedi / drivers / unioxx5.c
CommitLineData
110526ed
KD
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>
5a0e3ad6 47#include <linux/slab.h>
110526ed
KD
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 */
33a4640f 76struct unioxx5_subd_priv {
110526ed 77 int usp_iobase;
33e73e00
R
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];
110526ed
KD
82 unsigned char usp_prev_wr_val[3]; /* previous written value */
83 unsigned char usp_prev_cn_val[3]; /* previous channel value */
33a4640f 84};
110526ed 85
0a85b6f0
MT
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);
da91b269 97static int unioxx5_detach(struct comedi_device *dev);
0a85b6f0
MT
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);
2696fb57 104/* static void __unioxx5_digital_config(struct unioxx5_subd_priv* usp, int mode); */
0a85b6f0
MT
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);
110526ed 109static int __unioxx5_define_chan_offset(int chan_num);
da91b269 110static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel);
110526ed 111
139dfbdf 112static struct comedi_driver unioxx5_driver = {
68c3dbff
BP
113 .driver_name = DRIVER_NAME,
114 .module = THIS_MODULE,
115 .attach = unioxx5_attach,
116 .detach = unioxx5_detach
110526ed
KD
117};
118
7114a280
AT
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);
110526ed 131
0a85b6f0
MT
132static int unioxx5_attach(struct comedi_device *dev,
133 struct comedi_devconfig *it)
110526ed
KD
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
0a85b6f0 158 "your card must has at least 2 'g01' subdevices\n");
110526ed
KD
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,
0a85b6f0 170 dev->minor) < 0)
110526ed
KD
171 return -1;
172 }
173
33e73e00 174 printk(KERN_INFO "attached\n");
110526ed
KD
175 return 0;
176}
177
0a85b6f0
MT
178static int unioxx5_subdev_read(struct comedi_device *dev,
179 struct comedi_subdevice *subdev,
180 struct comedi_insn *insn, unsigned int *data)
110526ed 181{
33a4640f 182 struct unioxx5_subd_priv *usp = subdev->private;
110526ed
KD
183 int channel, type;
184
185 channel = CR_CHAN(insn->chanspec);
33e73e00
R
186 /* defining module type(analog or digital) */
187 type = usp->usp_module_type[channel / 2];
110526ed
KD
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
0a85b6f0
MT
200static int unioxx5_subdev_write(struct comedi_device *dev,
201 struct comedi_subdevice *subdev,
202 struct comedi_insn *insn, unsigned int *data)
110526ed 203{
33a4640f 204 struct unioxx5_subd_priv *usp = subdev->private;
110526ed
KD
205 int channel, type;
206
207 channel = CR_CHAN(insn->chanspec);
33e73e00
R
208 /* defining module type(analog or digital) */
209 type = usp->usp_module_type[channel / 2];
110526ed
KD
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 */
0a85b6f0
MT
223static int unioxx5_insn_config(struct comedi_device *dev,
224 struct comedi_subdevice *subdev,
225 struct comedi_insn *insn, unsigned int *data)
110526ed
KD
226{
227 int channel_offset, flags, channel = CR_CHAN(insn->chanspec), type;
33a4640f 228 struct unioxx5_subd_priv *usp = subdev->private;
110526ed
KD
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
0a85b6f0
MT
235 "comedi%d: channel configuration accessible only for digital modules\n",
236 dev->minor);
110526ed
KD
237 return -1;
238 }
239
c3744138
BP
240 channel_offset = __unioxx5_define_chan_offset(channel);
241 if (channel_offset < 0) {
110526ed 242 printk(KERN_ERR
0a85b6f0
MT
243 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
244 dev->minor, channel);
110526ed
KD
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);
33e73e00
R
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;
110526ed
KD
274
275 return 0;
276}
277
da91b269 278static int unioxx5_detach(struct comedi_device *dev)
110526ed
KD
279{
280 int i;
34c43922 281 struct comedi_subdevice *subdev;
33a4640f 282 struct unioxx5_subd_priv *usp;
110526ed
KD
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 */
0a85b6f0
MT
295static int __unioxx5_subdev_init(struct comedi_subdevice *subdev,
296 int subdev_iobase, int minor)
110526ed 297{
33a4640f 298 struct unioxx5_subd_priv *usp;
110526ed
KD
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
32414878 306 usp = kzalloc(sizeof(*usp), GFP_KERNEL);
c3744138
BP
307
308 if (usp == NULL) {
81a14956 309 printk(KERN_ERR "comedi%d: error! --> out of memory!\n", minor);
110526ed
KD
310 return -1;
311 }
312
313 usp->usp_iobase = subdev_iobase;
33e73e00 314 printk(KERN_INFO "comedi%d: |", minor);
110526ed
KD
315
316 /* defining modules types */
317 for (i = 0; i < 12; i++) {
318 to = 10000;
319
320 __unioxx5_analog_config(usp, i * 2);
33e73e00
R
321 /* sends channel number to card */
322 outb(i + 1, subdev_iobase + 5);
110526ed 323 outb('H', subdev_iobase + 6); /* requests EEPROM world */
6fd071cc
JS
324 while (!(inb(subdev_iobase + 0) & TxBE))
325 ; /* waits while writting will be allowed */
110526ed
KD
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]);
5f74ea14 343 udelay(1);
110526ed
KD
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;
33e73e00
R
357 /* for digital modules only!!! */
358 subdev->insn_config = unioxx5_insn_config;
110526ed 359
33e73e00 360 printk(KERN_INFO "subdevice configured\n");
110526ed
KD
361
362 return 0;
363}
364
0a85b6f0
MT
365static int __unioxx5_digital_write(struct unioxx5_subd_priv *usp,
366 unsigned int *data, int channel, int minor)
110526ed
KD
367{
368 int channel_offset, val;
369 int mask = 1 << (channel & 0x07);
370
c3744138
BP
371 channel_offset = __unioxx5_define_chan_offset(channel);
372 if (channel_offset < 0) {
110526ed 373 printk(KERN_ERR
0a85b6f0
MT
374 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
375 minor, channel);
110526ed
KD
376 return 0;
377 }
378
33e73e00
R
379 /* getting previous written value */
380 val = usp->usp_prev_wr_val[channel_offset - 1];
110526ed
KD
381
382 if (*data)
383 val |= mask;
384 else
385 val &= ~mask;
386
387 outb(val, usp->usp_iobase + channel_offset);
33e73e00
R
388 /* saving new written value */
389 usp->usp_prev_wr_val[channel_offset - 1] = val;
110526ed
KD
390
391 return 1;
392}
393
394/* function for digital reading */
0a85b6f0
MT
395static int __unioxx5_digital_read(struct unioxx5_subd_priv *usp,
396 unsigned int *data, int channel, int minor)
110526ed
KD
397{
398 int channel_offset, mask = 1 << (channel & 0x07);
399
c3744138
BP
400 channel_offset = __unioxx5_define_chan_offset(channel);
401 if (channel_offset < 0) {
110526ed 402 printk(KERN_ERR
0a85b6f0
MT
403 "comedi%d: undefined channel %d. channel range is 0 .. 23\n",
404 minor, channel);
110526ed
KD
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 */
110526ed
KD
413 *data >>= channel;
414 return 1;
415}
416
417#if 0 /* not used? */
da91b269 418static void __unioxx5_digital_config(struct unioxx5_subd_priv *usp, int mode)
110526ed
KD
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
0a85b6f0
MT
434static int __unioxx5_analog_write(struct unioxx5_subd_priv *usp,
435 unsigned int *data, int channel, int minor)
110526ed
KD
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
0a85b6f0
MT
445 "comedi%d: module in position %d with id 0x%0x is for input only!\n",
446 minor, module, usp->usp_module_type[module]);
110526ed
KD
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
2696fb57 456 /* while(!((inb(usp->usp_iobase + 0)) & TxBE)); */
33e73e00
R
457 /* sending module number to card(1 .. 12) */
458 outb(module + 1, usp->usp_iobase + 5);
110526ed
KD
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++) {
6fd071cc
JS
463 while (!((inb(usp->usp_iobase + 0)) & TxBE))
464 ; /* waits while writting will be allowed */
110526ed
KD
465 outb(usp->usp_extra_data[module][i], usp->usp_iobase + 6);
466 }
467
468 return 1;
469}
470
0a85b6f0
MT
471static int __unioxx5_analog_read(struct unioxx5_subd_priv *usp,
472 unsigned int *data, int channel, int minor)
110526ed
KD
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
0a85b6f0
MT
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]);
110526ed
KD
485 return 0;
486 }
487
488 __unioxx5_analog_config(usp, channel);
33e73e00
R
489 /* sends module number to card(1 .. 12) */
490 outb(module_no + 1, usp->usp_iobase + 5);
110526ed
KD
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 */
6fd071cc
JS
495 while (!((control = inb(usp->usp_iobase + 0)) & Rx4CA))
496 ;
110526ed
KD
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) */
da91b269 513static void __unioxx5_analog_config(struct unioxx5_subd_priv *usp, int channel)
110526ed
KD
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}
90f703d3
AT
555
556MODULE_AUTHOR("Comedi http://www.comedi.org");
557MODULE_DESCRIPTION("Comedi low-level driver");
558MODULE_LICENSE("GPL");