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