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