]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/me4000.c
Linux 3.12-rc6
[karo-tx-linux.git] / drivers / staging / comedi / drivers / me4000.c
1 /*
2    comedi/drivers/me4000.c
3    Source code for the Meilhaus ME-4000 board family.
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17  */
18 /*
19 Driver: me4000
20 Description: Meilhaus ME-4000 series boards
21 Devices: [Meilhaus] ME-4650 (me4000), ME-4670i, ME-4680, ME-4680i, ME-4680is
22 Author: gg (Guenter Gebhardt <g.gebhardt@meilhaus.com>)
23 Updated: Mon, 18 Mar 2002 15:34:01 -0800
24 Status: broken (no support for loading firmware)
25
26 Supports:
27
28     - Analog Input
29     - Analog Output
30     - Digital I/O
31     - Counter
32
33 Configuration Options: not applicable, uses PCI auto config
34
35 The firmware required by these boards is available in the
36 comedi_nonfree_firmware tarball available from
37 http://www.comedi.org.  However, the driver's support for
38 loading the firmware through comedi_config is currently
39 broken.
40
41  */
42
43 #include <linux/module.h>
44 #include <linux/pci.h>
45 #include <linux/delay.h>
46 #include <linux/interrupt.h>
47 #include <linux/list.h>
48 #include <linux/spinlock.h>
49
50 #include "../comedidev.h"
51
52 #include "comedi_fc.h"
53 #include "8253.h"
54 #include "plx9052.h"
55
56 #if 0
57 /* file removed due to GPL incompatibility */
58 #include "me4000_fw.h"
59 #endif
60
61 /*
62  * ME4000 Register map and bit defines
63  */
64 #define ME4000_AO_CHAN(x)                       ((x) * 0x18)
65
66 #define ME4000_AO_CTRL_REG(x)                   (0x00 + ME4000_AO_CHAN(x))
67 #define ME4000_AO_CTRL_BIT_MODE_0               (1 << 0)
68 #define ME4000_AO_CTRL_BIT_MODE_1               (1 << 1)
69 #define ME4000_AO_CTRL_MASK_MODE                (3 << 0)
70 #define ME4000_AO_CTRL_BIT_STOP                 (1 << 2)
71 #define ME4000_AO_CTRL_BIT_ENABLE_FIFO          (1 << 3)
72 #define ME4000_AO_CTRL_BIT_ENABLE_EX_TRIG       (1 << 4)
73 #define ME4000_AO_CTRL_BIT_EX_TRIG_EDGE         (1 << 5)
74 #define ME4000_AO_CTRL_BIT_IMMEDIATE_STOP       (1 << 7)
75 #define ME4000_AO_CTRL_BIT_ENABLE_DO            (1 << 8)
76 #define ME4000_AO_CTRL_BIT_ENABLE_IRQ           (1 << 9)
77 #define ME4000_AO_CTRL_BIT_RESET_IRQ            (1 << 10)
78 #define ME4000_AO_STATUS_REG(x)                 (0x04 + ME4000_AO_CHAN(x))
79 #define ME4000_AO_STATUS_BIT_FSM                (1 << 0)
80 #define ME4000_AO_STATUS_BIT_FF                 (1 << 1)
81 #define ME4000_AO_STATUS_BIT_HF                 (1 << 2)
82 #define ME4000_AO_STATUS_BIT_EF                 (1 << 3)
83 #define ME4000_AO_FIFO_REG(x)                   (0x08 + ME4000_AO_CHAN(x))
84 #define ME4000_AO_SINGLE_REG(x)                 (0x0c + ME4000_AO_CHAN(x))
85 #define ME4000_AO_TIMER_REG(x)                  (0x10 + ME4000_AO_CHAN(x))
86 #define ME4000_AI_CTRL_REG                      0x74
87 #define ME4000_AI_STATUS_REG                    0x74
88 #define ME4000_AI_CTRL_BIT_MODE_0               (1 << 0)
89 #define ME4000_AI_CTRL_BIT_MODE_1               (1 << 1)
90 #define ME4000_AI_CTRL_BIT_MODE_2               (1 << 2)
91 #define ME4000_AI_CTRL_BIT_SAMPLE_HOLD          (1 << 3)
92 #define ME4000_AI_CTRL_BIT_IMMEDIATE_STOP       (1 << 4)
93 #define ME4000_AI_CTRL_BIT_STOP                 (1 << 5)
94 #define ME4000_AI_CTRL_BIT_CHANNEL_FIFO         (1 << 6)
95 #define ME4000_AI_CTRL_BIT_DATA_FIFO            (1 << 7)
96 #define ME4000_AI_CTRL_BIT_FULLSCALE            (1 << 8)
97 #define ME4000_AI_CTRL_BIT_OFFSET               (1 << 9)
98 #define ME4000_AI_CTRL_BIT_EX_TRIG_ANALOG       (1 << 10)
99 #define ME4000_AI_CTRL_BIT_EX_TRIG              (1 << 11)
100 #define ME4000_AI_CTRL_BIT_EX_TRIG_FALLING      (1 << 12)
101 #define ME4000_AI_CTRL_BIT_EX_IRQ               (1 << 13)
102 #define ME4000_AI_CTRL_BIT_EX_IRQ_RESET         (1 << 14)
103 #define ME4000_AI_CTRL_BIT_LE_IRQ               (1 << 15)
104 #define ME4000_AI_CTRL_BIT_LE_IRQ_RESET         (1 << 16)
105 #define ME4000_AI_CTRL_BIT_HF_IRQ               (1 << 17)
106 #define ME4000_AI_CTRL_BIT_HF_IRQ_RESET         (1 << 18)
107 #define ME4000_AI_CTRL_BIT_SC_IRQ               (1 << 19)
108 #define ME4000_AI_CTRL_BIT_SC_IRQ_RESET         (1 << 20)
109 #define ME4000_AI_CTRL_BIT_SC_RELOAD            (1 << 21)
110 #define ME4000_AI_STATUS_BIT_EF_CHANNEL         (1 << 22)
111 #define ME4000_AI_STATUS_BIT_HF_CHANNEL         (1 << 23)
112 #define ME4000_AI_STATUS_BIT_FF_CHANNEL         (1 << 24)
113 #define ME4000_AI_STATUS_BIT_EF_DATA            (1 << 25)
114 #define ME4000_AI_STATUS_BIT_HF_DATA            (1 << 26)
115 #define ME4000_AI_STATUS_BIT_FF_DATA            (1 << 27)
116 #define ME4000_AI_STATUS_BIT_LE                 (1 << 28)
117 #define ME4000_AI_STATUS_BIT_FSM                (1 << 29)
118 #define ME4000_AI_CTRL_BIT_EX_TRIG_BOTH         (1 << 31)
119 #define ME4000_AI_CHANNEL_LIST_REG              0x78
120 #define ME4000_AI_LIST_INPUT_SINGLE_ENDED       (0 << 5)
121 #define ME4000_AI_LIST_INPUT_DIFFERENTIAL       (1 << 5)
122 #define ME4000_AI_LIST_RANGE_BIPOLAR_10         (0 << 6)
123 #define ME4000_AI_LIST_RANGE_BIPOLAR_2_5        (1 << 6)
124 #define ME4000_AI_LIST_RANGE_UNIPOLAR_10        (2 << 6)
125 #define ME4000_AI_LIST_RANGE_UNIPOLAR_2_5       (3 << 6)
126 #define ME4000_AI_LIST_LAST_ENTRY               (1 << 8)
127 #define ME4000_AI_DATA_REG                      0x7c
128 #define ME4000_AI_CHAN_TIMER_REG                0x80
129 #define ME4000_AI_CHAN_PRE_TIMER_REG            0x84
130 #define ME4000_AI_SCAN_TIMER_LOW_REG            0x88
131 #define ME4000_AI_SCAN_TIMER_HIGH_REG           0x8c
132 #define ME4000_AI_SCAN_PRE_TIMER_LOW_REG        0x90
133 #define ME4000_AI_SCAN_PRE_TIMER_HIGH_REG       0x94
134 #define ME4000_AI_START_REG                     0x98
135 #define ME4000_IRQ_STATUS_REG                   0x9c
136 #define ME4000_IRQ_STATUS_BIT_EX                (1 << 0)
137 #define ME4000_IRQ_STATUS_BIT_LE                (1 << 1)
138 #define ME4000_IRQ_STATUS_BIT_AI_HF             (1 << 2)
139 #define ME4000_IRQ_STATUS_BIT_AO_0_HF           (1 << 3)
140 #define ME4000_IRQ_STATUS_BIT_AO_1_HF           (1 << 4)
141 #define ME4000_IRQ_STATUS_BIT_AO_2_HF           (1 << 5)
142 #define ME4000_IRQ_STATUS_BIT_AO_3_HF           (1 << 6)
143 #define ME4000_IRQ_STATUS_BIT_SC                (1 << 7)
144 #define ME4000_DIO_PORT_0_REG                   0xa0
145 #define ME4000_DIO_PORT_1_REG                   0xa4
146 #define ME4000_DIO_PORT_2_REG                   0xa8
147 #define ME4000_DIO_PORT_3_REG                   0xac
148 #define ME4000_DIO_DIR_REG                      0xb0
149 #define ME4000_AO_LOADSETREG_XX                 0xb4
150 #define ME4000_DIO_CTRL_REG                     0xb8
151 #define ME4000_DIO_CTRL_BIT_MODE_0              (1 << 0)
152 #define ME4000_DIO_CTRL_BIT_MODE_1              (1 << 1)
153 #define ME4000_DIO_CTRL_BIT_MODE_2              (1 << 2)
154 #define ME4000_DIO_CTRL_BIT_MODE_3              (1 << 3)
155 #define ME4000_DIO_CTRL_BIT_MODE_4              (1 << 4)
156 #define ME4000_DIO_CTRL_BIT_MODE_5              (1 << 5)
157 #define ME4000_DIO_CTRL_BIT_MODE_6              (1 << 6)
158 #define ME4000_DIO_CTRL_BIT_MODE_7              (1 << 7)
159 #define ME4000_DIO_CTRL_BIT_FUNCTION_0          (1 << 8)
160 #define ME4000_DIO_CTRL_BIT_FUNCTION_1          (1 << 9)
161 #define ME4000_DIO_CTRL_BIT_FIFO_HIGH_0         (1 << 10)
162 #define ME4000_DIO_CTRL_BIT_FIFO_HIGH_1         (1 << 11)
163 #define ME4000_DIO_CTRL_BIT_FIFO_HIGH_2         (1 << 12)
164 #define ME4000_DIO_CTRL_BIT_FIFO_HIGH_3         (1 << 13)
165 #define ME4000_AO_DEMUX_ADJUST_REG              0xbc
166 #define ME4000_AO_DEMUX_ADJUST_VALUE            0x4c
167 #define ME4000_AI_SAMPLE_COUNTER_REG            0xc0
168
169 #define ME4000_AI_FIFO_COUNT                    2048
170
171 #define ME4000_AI_MIN_TICKS                     66
172 #define ME4000_AI_MIN_SAMPLE_TIME               2000
173 #define ME4000_AI_BASE_FREQUENCY                (unsigned int) 33E6
174
175 #define ME4000_AI_CHANNEL_LIST_COUNT            1024
176
177 struct me4000_info {
178         unsigned long plx_regbase;
179         unsigned long timer_regbase;
180
181         unsigned int ao_readback[4];
182 };
183
184 enum me4000_boardid {
185         BOARD_ME4650,
186         BOARD_ME4660,
187         BOARD_ME4660I,
188         BOARD_ME4660S,
189         BOARD_ME4660IS,
190         BOARD_ME4670,
191         BOARD_ME4670I,
192         BOARD_ME4670S,
193         BOARD_ME4670IS,
194         BOARD_ME4680,
195         BOARD_ME4680I,
196         BOARD_ME4680S,
197         BOARD_ME4680IS,
198 };
199
200 struct me4000_board {
201         const char *name;
202         int ao_nchan;
203         int ao_fifo;
204         int ai_nchan;
205         int ai_diff_nchan;
206         int ai_sh_nchan;
207         int ex_trig_analog;
208         int dio_nchan;
209         int has_counter;
210 };
211
212 static const struct me4000_board me4000_boards[] = {
213         [BOARD_ME4650] = {
214                 .name           = "ME-4650",
215                 .ai_nchan       = 16,
216                 .dio_nchan      = 32,
217         },
218         [BOARD_ME4660] = {
219                 .name           = "ME-4660",
220                 .ai_nchan       = 32,
221                 .ai_diff_nchan  = 16,
222                 .dio_nchan      = 32,
223                 .has_counter    = 1,
224         },
225         [BOARD_ME4660I] = {
226                 .name           = "ME-4660i",
227                 .ai_nchan       = 32,
228                 .ai_diff_nchan  = 16,
229                 .dio_nchan      = 32,
230                 .has_counter    = 1,
231         },
232         [BOARD_ME4660S] = {
233                 .name           = "ME-4660s",
234                 .ai_nchan       = 32,
235                 .ai_diff_nchan  = 16,
236                 .ai_sh_nchan    = 8,
237                 .dio_nchan      = 32,
238                 .has_counter    = 1,
239         },
240         [BOARD_ME4660IS] = {
241                 .name           = "ME-4660is",
242                 .ai_nchan       = 32,
243                 .ai_diff_nchan  = 16,
244                 .ai_sh_nchan    = 8,
245                 .dio_nchan      = 32,
246                 .has_counter    = 1,
247         },
248         [BOARD_ME4670] = {
249                 .name           = "ME-4670",
250                 .ao_nchan       = 4,
251                 .ai_nchan       = 32,
252                 .ai_diff_nchan  = 16,
253                 .ex_trig_analog = 1,
254                 .dio_nchan      = 32,
255                 .has_counter    = 1,
256         },
257         [BOARD_ME4670I] = {
258                 .name           = "ME-4670i",
259                 .ao_nchan       = 4,
260                 .ai_nchan       = 32,
261                 .ai_diff_nchan  = 16,
262                 .ex_trig_analog = 1,
263                 .dio_nchan      = 32,
264                 .has_counter    = 1,
265         },
266         [BOARD_ME4670S] = {
267                 .name           = "ME-4670s",
268                 .ao_nchan       = 4,
269                 .ai_nchan       = 32,
270                 .ai_diff_nchan  = 16,
271                 .ai_sh_nchan    = 8,
272                 .ex_trig_analog = 1,
273                 .dio_nchan      = 32,
274                 .has_counter    = 1,
275         },
276         [BOARD_ME4670IS] = {
277                 .name           = "ME-4670is",
278                 .ao_nchan       = 4,
279                 .ai_nchan       = 32,
280                 .ai_diff_nchan  = 16,
281                 .ai_sh_nchan    = 8,
282                 .ex_trig_analog = 1,
283                 .dio_nchan      = 32,
284                 .has_counter    = 1,
285         },
286         [BOARD_ME4680] = {
287                 .name           = "ME-4680",
288                 .ao_nchan       = 4,
289                 .ao_fifo        = 4,
290                 .ai_nchan       = 32,
291                 .ai_diff_nchan  = 16,
292                 .ex_trig_analog = 1,
293                 .dio_nchan      = 32,
294                 .has_counter    = 1,
295         },
296         [BOARD_ME4680I] = {
297                 .name           = "ME-4680i",
298                 .ao_nchan       = 4,
299                 .ao_fifo        = 4,
300                 .ai_nchan       = 32,
301                 .ai_diff_nchan  = 16,
302                 .ex_trig_analog = 1,
303                 .dio_nchan      = 32,
304                 .has_counter    = 1,
305         },
306         [BOARD_ME4680S] = {
307                 .name           = "ME-4680s",
308                 .ao_nchan       = 4,
309                 .ao_fifo        = 4,
310                 .ai_nchan       = 32,
311                 .ai_diff_nchan  = 16,
312                 .ai_sh_nchan    = 8,
313                 .ex_trig_analog = 1,
314                 .dio_nchan      = 32,
315                 .has_counter    = 1,
316         },
317         [BOARD_ME4680IS] = {
318                 .name           = "ME-4680is",
319                 .ao_nchan       = 4,
320                 .ao_fifo        = 4,
321                 .ai_nchan       = 32,
322                 .ai_diff_nchan  = 16,
323                 .ai_sh_nchan    = 8,
324                 .ex_trig_analog = 1,
325                 .dio_nchan      = 32,
326                 .has_counter    = 1,
327         },
328 };
329
330 static const struct comedi_lrange me4000_ai_range = {
331         4,
332         {
333          UNI_RANGE(2.5),
334          UNI_RANGE(10),
335          BIP_RANGE(2.5),
336          BIP_RANGE(10),
337          }
338 };
339
340 #define FIRMWARE_NOT_AVAILABLE 1
341 #if FIRMWARE_NOT_AVAILABLE
342 extern unsigned char *xilinx_firm;
343 #endif
344
345 static int xilinx_download(struct comedi_device *dev)
346 {
347         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
348         struct me4000_info *info = dev->private;
349         unsigned long xilinx_iobase = pci_resource_start(pcidev, 5);
350         u32 value = 0;
351         wait_queue_head_t queue;
352         int idx = 0;
353         int size = 0;
354         unsigned int intcsr;
355
356         if (!xilinx_iobase)
357                 return -ENODEV;
358
359         init_waitqueue_head(&queue);
360
361         /*
362          * Set PLX local interrupt 2 polarity to high.
363          * Interrupt is thrown by init pin of xilinx.
364          */
365         outl(PLX9052_INTCSR_LI2POL, info->plx_regbase + PLX9052_INTCSR);
366
367         /* Set /CS and /WRITE of the Xilinx */
368         value = inl(info->plx_regbase + PLX9052_CNTRL);
369         value |= PLX9052_CNTRL_UIO2_DATA;
370         outl(value, info->plx_regbase + PLX9052_CNTRL);
371
372         /* Init Xilinx with CS1 */
373         inb(xilinx_iobase + 0xC8);
374
375         /* Wait until /INIT pin is set */
376         udelay(20);
377         intcsr = inl(info->plx_regbase + PLX9052_INTCSR);
378         if (!(intcsr & PLX9052_INTCSR_LI2STAT)) {
379                 dev_err(dev->class_dev, "Can't init Xilinx\n");
380                 return -EIO;
381         }
382
383         /* Reset /CS and /WRITE of the Xilinx */
384         value = inl(info->plx_regbase + PLX9052_CNTRL);
385         value &= ~PLX9052_CNTRL_UIO2_DATA;
386         outl(value, info->plx_regbase + PLX9052_CNTRL);
387         if (FIRMWARE_NOT_AVAILABLE) {
388                 dev_err(dev->class_dev,
389                         "xilinx firmware unavailable due to licensing, aborting");
390                 return -EIO;
391         } else {
392                 /* Download Xilinx firmware */
393                 size = (xilinx_firm[0] << 24) + (xilinx_firm[1] << 16) +
394                     (xilinx_firm[2] << 8) + xilinx_firm[3];
395                 udelay(10);
396
397                 for (idx = 0; idx < size; idx++) {
398                         outb(xilinx_firm[16 + idx], xilinx_iobase);
399                         udelay(10);
400
401                         /* Check if BUSY flag is low */
402                         if (inl(info->plx_regbase + PLX9052_CNTRL) & PLX9052_CNTRL_UIO1_DATA) {
403                                 dev_err(dev->class_dev,
404                                         "Xilinx is still busy (idx = %d)\n",
405                                         idx);
406                                 return -EIO;
407                         }
408                 }
409         }
410
411         /* If done flag is high download was successful */
412         if (inl(info->plx_regbase + PLX9052_CNTRL) & PLX9052_CNTRL_UIO0_DATA) {
413         } else {
414                 dev_err(dev->class_dev, "DONE flag is not set\n");
415                 dev_err(dev->class_dev, "Download not successful\n");
416                 return -EIO;
417         }
418
419         /* Set /CS and /WRITE */
420         value = inl(info->plx_regbase + PLX9052_CNTRL);
421         value |= PLX9052_CNTRL_UIO2_DATA;
422         outl(value, info->plx_regbase + PLX9052_CNTRL);
423
424         return 0;
425 }
426
427 static void me4000_reset(struct comedi_device *dev)
428 {
429         struct me4000_info *info = dev->private;
430         unsigned long val;
431         int chan;
432
433         /* Make a hardware reset */
434         val = inl(info->plx_regbase + PLX9052_CNTRL);
435         val |= PLX9052_CNTRL_PCI_RESET;
436         outl(val, info->plx_regbase + PLX9052_CNTRL);
437         val &= ~PLX9052_CNTRL_PCI_RESET;
438         outl(val , info->plx_regbase + PLX9052_CNTRL);
439
440         /* 0x8000 to the DACs means an output voltage of 0V */
441         for (chan = 0; chan < 4; chan++)
442                 outl(0x8000, dev->iobase + ME4000_AO_SINGLE_REG(chan));
443
444         /* Set both stop bits in the analog input control register */
445         outl(ME4000_AI_CTRL_BIT_IMMEDIATE_STOP | ME4000_AI_CTRL_BIT_STOP,
446                 dev->iobase + ME4000_AI_CTRL_REG);
447
448         /* Set both stop bits in the analog output control register */
449         val = ME4000_AO_CTRL_BIT_IMMEDIATE_STOP | ME4000_AO_CTRL_BIT_STOP;
450         for (chan = 0; chan < 4; chan++)
451                 outl(val, dev->iobase + ME4000_AO_CTRL_REG(chan));
452
453         /* Enable interrupts on the PLX */
454         outl(PLX9052_INTCSR_LI1ENAB |
455              PLX9052_INTCSR_LI1POL |
456              PLX9052_INTCSR_PCIENAB, info->plx_regbase + PLX9052_INTCSR);
457
458         /* Set the adustment register for AO demux */
459         outl(ME4000_AO_DEMUX_ADJUST_VALUE,
460                     dev->iobase + ME4000_AO_DEMUX_ADJUST_REG);
461
462         /*
463          * Set digital I/O direction for port 0
464          * to output on isolated versions
465          */
466         if (!(inl(dev->iobase + ME4000_DIO_DIR_REG) & 0x1))
467                 outl(0x1, dev->iobase + ME4000_DIO_CTRL_REG);
468 }
469
470 /*=============================================================================
471   Analog input section
472   ===========================================================================*/
473
474 static int me4000_ai_insn_read(struct comedi_device *dev,
475                                struct comedi_subdevice *subdevice,
476                                struct comedi_insn *insn, unsigned int *data)
477 {
478         const struct me4000_board *thisboard = comedi_board(dev);
479         int chan = CR_CHAN(insn->chanspec);
480         int rang = CR_RANGE(insn->chanspec);
481         int aref = CR_AREF(insn->chanspec);
482
483         unsigned long entry = 0;
484         unsigned long tmp;
485         long lval;
486
487         if (insn->n == 0) {
488                 return 0;
489         } else if (insn->n > 1) {
490                 dev_err(dev->class_dev, "Invalid instruction length %d\n",
491                         insn->n);
492                 return -EINVAL;
493         }
494
495         switch (rang) {
496         case 0:
497                 entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_2_5;
498                 break;
499         case 1:
500                 entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_10;
501                 break;
502         case 2:
503                 entry |= ME4000_AI_LIST_RANGE_BIPOLAR_2_5;
504                 break;
505         case 3:
506                 entry |= ME4000_AI_LIST_RANGE_BIPOLAR_10;
507                 break;
508         default:
509                 dev_err(dev->class_dev, "Invalid range specified\n");
510                 return -EINVAL;
511         }
512
513         switch (aref) {
514         case AREF_GROUND:
515         case AREF_COMMON:
516                 if (chan >= thisboard->ai_nchan) {
517                         dev_err(dev->class_dev,
518                                 "Analog input is not available\n");
519                         return -EINVAL;
520                 }
521                 entry |= ME4000_AI_LIST_INPUT_SINGLE_ENDED | chan;
522                 break;
523
524         case AREF_DIFF:
525                 if (rang == 0 || rang == 1) {
526                         dev_err(dev->class_dev,
527                                 "Range must be bipolar when aref = diff\n");
528                         return -EINVAL;
529                 }
530
531                 if (chan >= thisboard->ai_diff_nchan) {
532                         dev_err(dev->class_dev,
533                                 "Analog input is not available\n");
534                         return -EINVAL;
535                 }
536                 entry |= ME4000_AI_LIST_INPUT_DIFFERENTIAL | chan;
537                 break;
538         default:
539                 dev_err(dev->class_dev, "Invalid aref specified\n");
540                 return -EINVAL;
541         }
542
543         entry |= ME4000_AI_LIST_LAST_ENTRY;
544
545         /* Clear channel list, data fifo and both stop bits */
546         tmp = inl(dev->iobase + ME4000_AI_CTRL_REG);
547         tmp &= ~(ME4000_AI_CTRL_BIT_CHANNEL_FIFO |
548                  ME4000_AI_CTRL_BIT_DATA_FIFO |
549                  ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
550         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
551
552         /* Set the acquisition mode to single */
553         tmp &= ~(ME4000_AI_CTRL_BIT_MODE_0 | ME4000_AI_CTRL_BIT_MODE_1 |
554                  ME4000_AI_CTRL_BIT_MODE_2);
555         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
556
557         /* Enable channel list and data fifo */
558         tmp |= ME4000_AI_CTRL_BIT_CHANNEL_FIFO | ME4000_AI_CTRL_BIT_DATA_FIFO;
559         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
560
561         /* Generate channel list entry */
562         outl(entry, dev->iobase + ME4000_AI_CHANNEL_LIST_REG);
563
564         /* Set the timer to maximum sample rate */
565         outl(ME4000_AI_MIN_TICKS, dev->iobase + ME4000_AI_CHAN_TIMER_REG);
566         outl(ME4000_AI_MIN_TICKS, dev->iobase + ME4000_AI_CHAN_PRE_TIMER_REG);
567
568         /* Start conversion by dummy read */
569         inl(dev->iobase + ME4000_AI_START_REG);
570
571         /* Wait until ready */
572         udelay(10);
573         if (!(inl(dev->iobase + ME4000_AI_STATUS_REG) &
574              ME4000_AI_STATUS_BIT_EF_DATA)) {
575                 dev_err(dev->class_dev, "Value not available after wait\n");
576                 return -EIO;
577         }
578
579         /* Read value from data fifo */
580         lval = inl(dev->iobase + ME4000_AI_DATA_REG) & 0xFFFF;
581         data[0] = lval ^ 0x8000;
582
583         return 1;
584 }
585
586 static int me4000_ai_cancel(struct comedi_device *dev,
587                             struct comedi_subdevice *s)
588 {
589         unsigned long tmp;
590
591         /* Stop any running conversion */
592         tmp = inl(dev->iobase + ME4000_AI_CTRL_REG);
593         tmp &= ~(ME4000_AI_CTRL_BIT_STOP | ME4000_AI_CTRL_BIT_IMMEDIATE_STOP);
594         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
595
596         /* Clear the control register */
597         outl(0x0, dev->iobase + ME4000_AI_CTRL_REG);
598
599         return 0;
600 }
601
602 static int ai_check_chanlist(struct comedi_device *dev,
603                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
604 {
605         const struct me4000_board *thisboard = comedi_board(dev);
606         int aref;
607         int i;
608
609         /* Check whether a channel list is available */
610         if (!cmd->chanlist_len) {
611                 dev_err(dev->class_dev, "No channel list available\n");
612                 return -EINVAL;
613         }
614
615         /* Check the channel list size */
616         if (cmd->chanlist_len > ME4000_AI_CHANNEL_LIST_COUNT) {
617                 dev_err(dev->class_dev, "Channel list is to large\n");
618                 return -EINVAL;
619         }
620
621         /* Check the pointer */
622         if (!cmd->chanlist) {
623                 dev_err(dev->class_dev, "NULL pointer to channel list\n");
624                 return -EFAULT;
625         }
626
627         /* Check whether aref is equal for all entries */
628         aref = CR_AREF(cmd->chanlist[0]);
629         for (i = 0; i < cmd->chanlist_len; i++) {
630                 if (CR_AREF(cmd->chanlist[i]) != aref) {
631                         dev_err(dev->class_dev,
632                                 "Mode is not equal for all entries\n");
633                         return -EINVAL;
634                 }
635         }
636
637         /* Check whether channels are available for this ending */
638         if (aref == SDF_DIFF) {
639                 for (i = 0; i < cmd->chanlist_len; i++) {
640                         if (CR_CHAN(cmd->chanlist[i]) >=
641                             thisboard->ai_diff_nchan) {
642                                 dev_err(dev->class_dev,
643                                         "Channel number to high\n");
644                                 return -EINVAL;
645                         }
646                 }
647         } else {
648                 for (i = 0; i < cmd->chanlist_len; i++) {
649                         if (CR_CHAN(cmd->chanlist[i]) >= thisboard->ai_nchan) {
650                                 dev_err(dev->class_dev,
651                                         "Channel number to high\n");
652                                 return -EINVAL;
653                         }
654                 }
655         }
656
657         /* Check if bipolar is set for all entries when in differential mode */
658         if (aref == SDF_DIFF) {
659                 for (i = 0; i < cmd->chanlist_len; i++) {
660                         if (CR_RANGE(cmd->chanlist[i]) != 1 &&
661                             CR_RANGE(cmd->chanlist[i]) != 2) {
662                                 dev_err(dev->class_dev,
663                                        "Bipolar is not selected in differential mode\n");
664                                 return -EINVAL;
665                         }
666                 }
667         }
668
669         return 0;
670 }
671
672 static int ai_round_cmd_args(struct comedi_device *dev,
673                              struct comedi_subdevice *s,
674                              struct comedi_cmd *cmd,
675                              unsigned int *init_ticks,
676                              unsigned int *scan_ticks, unsigned int *chan_ticks)
677 {
678
679         int rest;
680
681         *init_ticks = 0;
682         *scan_ticks = 0;
683         *chan_ticks = 0;
684
685         if (cmd->start_arg) {
686                 *init_ticks = (cmd->start_arg * 33) / 1000;
687                 rest = (cmd->start_arg * 33) % 1000;
688
689                 if ((cmd->flags & TRIG_ROUND_MASK) == TRIG_ROUND_NEAREST) {
690                         if (rest > 33)
691                                 (*init_ticks)++;
692                 } else if ((cmd->flags & TRIG_ROUND_MASK) == TRIG_ROUND_UP) {
693                         if (rest)
694                                 (*init_ticks)++;
695                 }
696         }
697
698         if (cmd->scan_begin_arg) {
699                 *scan_ticks = (cmd->scan_begin_arg * 33) / 1000;
700                 rest = (cmd->scan_begin_arg * 33) % 1000;
701
702                 if ((cmd->flags & TRIG_ROUND_MASK) == TRIG_ROUND_NEAREST) {
703                         if (rest > 33)
704                                 (*scan_ticks)++;
705                 } else if ((cmd->flags & TRIG_ROUND_MASK) == TRIG_ROUND_UP) {
706                         if (rest)
707                                 (*scan_ticks)++;
708                 }
709         }
710
711         if (cmd->convert_arg) {
712                 *chan_ticks = (cmd->convert_arg * 33) / 1000;
713                 rest = (cmd->convert_arg * 33) % 1000;
714
715                 if ((cmd->flags & TRIG_ROUND_MASK) == TRIG_ROUND_NEAREST) {
716                         if (rest > 33)
717                                 (*chan_ticks)++;
718                 } else if ((cmd->flags & TRIG_ROUND_MASK) == TRIG_ROUND_UP) {
719                         if (rest)
720                                 (*chan_ticks)++;
721                 }
722         }
723
724         return 0;
725 }
726
727 static void ai_write_timer(struct comedi_device *dev,
728                            unsigned int init_ticks,
729                            unsigned int scan_ticks, unsigned int chan_ticks)
730 {
731         outl(init_ticks - 1, dev->iobase + ME4000_AI_SCAN_PRE_TIMER_LOW_REG);
732         outl(0x0, dev->iobase + ME4000_AI_SCAN_PRE_TIMER_HIGH_REG);
733
734         if (scan_ticks) {
735                 outl(scan_ticks - 1, dev->iobase + ME4000_AI_SCAN_TIMER_LOW_REG);
736                 outl(0x0, dev->iobase + ME4000_AI_SCAN_TIMER_HIGH_REG);
737         }
738
739         outl(chan_ticks - 1, dev->iobase + ME4000_AI_CHAN_PRE_TIMER_REG);
740         outl(chan_ticks - 1, dev->iobase + ME4000_AI_CHAN_TIMER_REG);
741 }
742
743 static int ai_write_chanlist(struct comedi_device *dev,
744                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
745 {
746         unsigned int entry;
747         unsigned int chan;
748         unsigned int rang;
749         unsigned int aref;
750         int i;
751
752         for (i = 0; i < cmd->chanlist_len; i++) {
753                 chan = CR_CHAN(cmd->chanlist[i]);
754                 rang = CR_RANGE(cmd->chanlist[i]);
755                 aref = CR_AREF(cmd->chanlist[i]);
756
757                 entry = chan;
758
759                 if (rang == 0)
760                         entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_2_5;
761                 else if (rang == 1)
762                         entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_10;
763                 else if (rang == 2)
764                         entry |= ME4000_AI_LIST_RANGE_BIPOLAR_2_5;
765                 else
766                         entry |= ME4000_AI_LIST_RANGE_BIPOLAR_10;
767
768                 if (aref == SDF_DIFF)
769                         entry |= ME4000_AI_LIST_INPUT_DIFFERENTIAL;
770                 else
771                         entry |= ME4000_AI_LIST_INPUT_SINGLE_ENDED;
772
773                 outl(entry, dev->iobase + ME4000_AI_CHANNEL_LIST_REG);
774         }
775
776         return 0;
777 }
778
779 static int ai_prepare(struct comedi_device *dev,
780                       struct comedi_subdevice *s,
781                       struct comedi_cmd *cmd,
782                       unsigned int init_ticks,
783                       unsigned int scan_ticks, unsigned int chan_ticks)
784 {
785
786         unsigned long tmp = 0;
787
788         /* Write timer arguments */
789         ai_write_timer(dev, init_ticks, scan_ticks, chan_ticks);
790
791         /* Reset control register */
792         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
793
794         /* Start sources */
795         if ((cmd->start_src == TRIG_EXT &&
796              cmd->scan_begin_src == TRIG_TIMER &&
797              cmd->convert_src == TRIG_TIMER) ||
798             (cmd->start_src == TRIG_EXT &&
799              cmd->scan_begin_src == TRIG_FOLLOW &&
800              cmd->convert_src == TRIG_TIMER)) {
801                 tmp = ME4000_AI_CTRL_BIT_MODE_1 |
802                     ME4000_AI_CTRL_BIT_CHANNEL_FIFO |
803                     ME4000_AI_CTRL_BIT_DATA_FIFO;
804         } else if (cmd->start_src == TRIG_EXT &&
805                    cmd->scan_begin_src == TRIG_EXT &&
806                    cmd->convert_src == TRIG_TIMER) {
807                 tmp = ME4000_AI_CTRL_BIT_MODE_2 |
808                     ME4000_AI_CTRL_BIT_CHANNEL_FIFO |
809                     ME4000_AI_CTRL_BIT_DATA_FIFO;
810         } else if (cmd->start_src == TRIG_EXT &&
811                    cmd->scan_begin_src == TRIG_EXT &&
812                    cmd->convert_src == TRIG_EXT) {
813                 tmp = ME4000_AI_CTRL_BIT_MODE_0 |
814                     ME4000_AI_CTRL_BIT_MODE_1 |
815                     ME4000_AI_CTRL_BIT_CHANNEL_FIFO |
816                     ME4000_AI_CTRL_BIT_DATA_FIFO;
817         } else {
818                 tmp = ME4000_AI_CTRL_BIT_MODE_0 |
819                     ME4000_AI_CTRL_BIT_CHANNEL_FIFO |
820                     ME4000_AI_CTRL_BIT_DATA_FIFO;
821         }
822
823         /* Stop triggers */
824         if (cmd->stop_src == TRIG_COUNT) {
825                 outl(cmd->chanlist_len * cmd->stop_arg,
826                             dev->iobase + ME4000_AI_SAMPLE_COUNTER_REG);
827                 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ;
828         } else if (cmd->stop_src == TRIG_NONE &&
829                    cmd->scan_end_src == TRIG_COUNT) {
830                 outl(cmd->scan_end_arg,
831                             dev->iobase + ME4000_AI_SAMPLE_COUNTER_REG);
832                 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ;
833         } else {
834                 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ;
835         }
836
837         /* Write the setup to the control register */
838         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
839
840         /* Write the channel list */
841         ai_write_chanlist(dev, s, cmd);
842
843         return 0;
844 }
845
846 static int me4000_ai_do_cmd(struct comedi_device *dev,
847                             struct comedi_subdevice *s)
848 {
849         int err;
850         unsigned int init_ticks = 0;
851         unsigned int scan_ticks = 0;
852         unsigned int chan_ticks = 0;
853         struct comedi_cmd *cmd = &s->async->cmd;
854
855         /* Reset the analog input */
856         err = me4000_ai_cancel(dev, s);
857         if (err)
858                 return err;
859
860         /* Round the timer arguments */
861         err = ai_round_cmd_args(dev,
862                                 s, cmd, &init_ticks, &scan_ticks, &chan_ticks);
863         if (err)
864                 return err;
865
866         /* Prepare the AI for acquisition */
867         err = ai_prepare(dev, s, cmd, init_ticks, scan_ticks, chan_ticks);
868         if (err)
869                 return err;
870
871         /* Start acquistion by dummy read */
872         inl(dev->iobase + ME4000_AI_START_REG);
873
874         return 0;
875 }
876
877 static int me4000_ai_do_cmd_test(struct comedi_device *dev,
878                                  struct comedi_subdevice *s,
879                                  struct comedi_cmd *cmd)
880 {
881
882         unsigned int init_ticks;
883         unsigned int chan_ticks;
884         unsigned int scan_ticks;
885         int err = 0;
886
887         /* Only rounding flags are implemented */
888         cmd->flags &= TRIG_ROUND_NEAREST | TRIG_ROUND_UP | TRIG_ROUND_DOWN;
889
890         /* Round the timer arguments */
891         ai_round_cmd_args(dev, s, cmd, &init_ticks, &scan_ticks, &chan_ticks);
892
893         /* Step 1 : check if triggers are trivially valid */
894
895         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
896         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
897                                         TRIG_FOLLOW | TRIG_TIMER | TRIG_EXT);
898         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
899         err |= cfc_check_trigger_src(&cmd->scan_end_src,
900                                         TRIG_NONE | TRIG_COUNT);
901         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_NONE | TRIG_COUNT);
902
903         if (err)
904                 return 1;
905
906         /* Step 2a : make sure trigger sources are unique */
907
908         err |= cfc_check_trigger_is_unique(cmd->start_src);
909         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
910         err |= cfc_check_trigger_is_unique(cmd->convert_src);
911         err |= cfc_check_trigger_is_unique(cmd->scan_end_src);
912         err |= cfc_check_trigger_is_unique(cmd->stop_src);
913
914         /* Step 2b : and mutually compatible */
915
916         if (cmd->start_src == TRIG_NOW &&
917             cmd->scan_begin_src == TRIG_TIMER &&
918             cmd->convert_src == TRIG_TIMER) {
919         } else if (cmd->start_src == TRIG_NOW &&
920                    cmd->scan_begin_src == TRIG_FOLLOW &&
921                    cmd->convert_src == TRIG_TIMER) {
922         } else if (cmd->start_src == TRIG_EXT &&
923                    cmd->scan_begin_src == TRIG_TIMER &&
924                    cmd->convert_src == TRIG_TIMER) {
925         } else if (cmd->start_src == TRIG_EXT &&
926                    cmd->scan_begin_src == TRIG_FOLLOW &&
927                    cmd->convert_src == TRIG_TIMER) {
928         } else if (cmd->start_src == TRIG_EXT &&
929                    cmd->scan_begin_src == TRIG_EXT &&
930                    cmd->convert_src == TRIG_TIMER) {
931         } else if (cmd->start_src == TRIG_EXT &&
932                    cmd->scan_begin_src == TRIG_EXT &&
933                    cmd->convert_src == TRIG_EXT) {
934         } else {
935                 err |= -EINVAL;
936         }
937
938         if (cmd->stop_src == TRIG_NONE && cmd->scan_end_src == TRIG_NONE) {
939         } else if (cmd->stop_src == TRIG_COUNT &&
940                    cmd->scan_end_src == TRIG_NONE) {
941         } else if (cmd->stop_src == TRIG_NONE &&
942                    cmd->scan_end_src == TRIG_COUNT) {
943         } else if (cmd->stop_src == TRIG_COUNT &&
944                    cmd->scan_end_src == TRIG_COUNT) {
945         } else {
946                 err |= -EINVAL;
947         }
948
949         if (err)
950                 return 2;
951
952         /* Step 3: check if arguments are trivially valid */
953
954         if (cmd->chanlist_len < 1) {
955                 cmd->chanlist_len = 1;
956                 err |= -EINVAL;
957         }
958         if (init_ticks < 66) {
959                 cmd->start_arg = 2000;
960                 err |= -EINVAL;
961         }
962         if (scan_ticks && scan_ticks < 67) {
963                 cmd->scan_begin_arg = 2031;
964                 err |= -EINVAL;
965         }
966         if (chan_ticks < 66) {
967                 cmd->convert_arg = 2000;
968                 err |= -EINVAL;
969         }
970
971         if (err)
972                 return 3;
973
974         /*
975          * Stage 4. Check for argument conflicts.
976          */
977         if (cmd->start_src == TRIG_NOW &&
978             cmd->scan_begin_src == TRIG_TIMER &&
979             cmd->convert_src == TRIG_TIMER) {
980
981                 /* Check timer arguments */
982                 if (init_ticks < ME4000_AI_MIN_TICKS) {
983                         dev_err(dev->class_dev, "Invalid start arg\n");
984                         cmd->start_arg = 2000;  /*  66 ticks at least */
985                         err++;
986                 }
987                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
988                         dev_err(dev->class_dev, "Invalid convert arg\n");
989                         cmd->convert_arg = 2000;        /*  66 ticks at least */
990                         err++;
991                 }
992                 if (scan_ticks <= cmd->chanlist_len * chan_ticks) {
993                         dev_err(dev->class_dev, "Invalid scan end arg\n");
994
995                         /*  At least one tick more */
996                         cmd->scan_end_arg = 2000 * cmd->chanlist_len + 31;
997                         err++;
998                 }
999         } else if (cmd->start_src == TRIG_NOW &&
1000                    cmd->scan_begin_src == TRIG_FOLLOW &&
1001                    cmd->convert_src == TRIG_TIMER) {
1002
1003                 /* Check timer arguments */
1004                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1005                         dev_err(dev->class_dev, "Invalid start arg\n");
1006                         cmd->start_arg = 2000;  /*  66 ticks at least */
1007                         err++;
1008                 }
1009                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1010                         dev_err(dev->class_dev, "Invalid convert arg\n");
1011                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1012                         err++;
1013                 }
1014         } else if (cmd->start_src == TRIG_EXT &&
1015                    cmd->scan_begin_src == TRIG_TIMER &&
1016                    cmd->convert_src == TRIG_TIMER) {
1017
1018                 /* Check timer arguments */
1019                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1020                         dev_err(dev->class_dev, "Invalid start arg\n");
1021                         cmd->start_arg = 2000;  /*  66 ticks at least */
1022                         err++;
1023                 }
1024                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1025                         dev_err(dev->class_dev, "Invalid convert arg\n");
1026                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1027                         err++;
1028                 }
1029                 if (scan_ticks <= cmd->chanlist_len * chan_ticks) {
1030                         dev_err(dev->class_dev, "Invalid scan end arg\n");
1031
1032                         /*  At least one tick more */
1033                         cmd->scan_end_arg = 2000 * cmd->chanlist_len + 31;
1034                         err++;
1035                 }
1036         } else if (cmd->start_src == TRIG_EXT &&
1037                    cmd->scan_begin_src == TRIG_FOLLOW &&
1038                    cmd->convert_src == TRIG_TIMER) {
1039
1040                 /* Check timer arguments */
1041                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1042                         dev_err(dev->class_dev, "Invalid start arg\n");
1043                         cmd->start_arg = 2000;  /*  66 ticks at least */
1044                         err++;
1045                 }
1046                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1047                         dev_err(dev->class_dev, "Invalid convert arg\n");
1048                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1049                         err++;
1050                 }
1051         } else if (cmd->start_src == TRIG_EXT &&
1052                    cmd->scan_begin_src == TRIG_EXT &&
1053                    cmd->convert_src == TRIG_TIMER) {
1054
1055                 /* Check timer arguments */
1056                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1057                         dev_err(dev->class_dev, "Invalid start arg\n");
1058                         cmd->start_arg = 2000;  /*  66 ticks at least */
1059                         err++;
1060                 }
1061                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1062                         dev_err(dev->class_dev, "Invalid convert arg\n");
1063                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1064                         err++;
1065                 }
1066         } else if (cmd->start_src == TRIG_EXT &&
1067                    cmd->scan_begin_src == TRIG_EXT &&
1068                    cmd->convert_src == TRIG_EXT) {
1069
1070                 /* Check timer arguments */
1071                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1072                         dev_err(dev->class_dev, "Invalid start arg\n");
1073                         cmd->start_arg = 2000;  /*  66 ticks at least */
1074                         err++;
1075                 }
1076         }
1077         if (cmd->stop_src == TRIG_COUNT) {
1078                 if (cmd->stop_arg == 0) {
1079                         dev_err(dev->class_dev, "Invalid stop arg\n");
1080                         cmd->stop_arg = 1;
1081                         err++;
1082                 }
1083         }
1084         if (cmd->scan_end_src == TRIG_COUNT) {
1085                 if (cmd->scan_end_arg == 0) {
1086                         dev_err(dev->class_dev, "Invalid scan end arg\n");
1087                         cmd->scan_end_arg = 1;
1088                         err++;
1089                 }
1090         }
1091
1092         if (err)
1093                 return 4;
1094
1095         /*
1096          * Stage 5. Check the channel list.
1097          */
1098         if (ai_check_chanlist(dev, s, cmd))
1099                 return 5;
1100
1101         return 0;
1102 }
1103
1104 static irqreturn_t me4000_ai_isr(int irq, void *dev_id)
1105 {
1106         unsigned int tmp;
1107         struct comedi_device *dev = dev_id;
1108         struct comedi_subdevice *s = &dev->subdevices[0];
1109         int i;
1110         int c = 0;
1111         long lval;
1112
1113         if (!dev->attached)
1114                 return IRQ_NONE;
1115
1116         /* Reset all events */
1117         s->async->events = 0;
1118
1119         /* Check if irq number is right */
1120         if (irq != dev->irq) {
1121                 dev_err(dev->class_dev, "Incorrect interrupt num: %d\n", irq);
1122                 return IRQ_HANDLED;
1123         }
1124
1125         if (inl(dev->iobase + ME4000_IRQ_STATUS_REG) &
1126             ME4000_IRQ_STATUS_BIT_AI_HF) {
1127                 /* Read status register to find out what happened */
1128                 tmp = inl(dev->iobase + ME4000_AI_CTRL_REG);
1129
1130                 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
1131                     !(tmp & ME4000_AI_STATUS_BIT_HF_DATA) &&
1132                     (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
1133                         c = ME4000_AI_FIFO_COUNT;
1134
1135                         /*
1136                          * FIFO overflow, so stop conversion
1137                          * and disable all interrupts
1138                          */
1139                         tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1140                         tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ |
1141                                  ME4000_AI_CTRL_BIT_SC_IRQ);
1142                         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1143
1144                         s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1145
1146                         dev_err(dev->class_dev, "FIFO overflow\n");
1147                 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
1148                            && !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
1149                            && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
1150                         s->async->events |= COMEDI_CB_BLOCK;
1151
1152                         c = ME4000_AI_FIFO_COUNT / 2;
1153                 } else {
1154                         dev_err(dev->class_dev,
1155                                 "Can't determine state of fifo\n");
1156                         c = 0;
1157
1158                         /*
1159                          * Undefined state, so stop conversion
1160                          * and disable all interrupts
1161                          */
1162                         tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1163                         tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ |
1164                                  ME4000_AI_CTRL_BIT_SC_IRQ);
1165                         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1166
1167                         s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1168
1169                         dev_err(dev->class_dev, "Undefined FIFO state\n");
1170                 }
1171
1172                 for (i = 0; i < c; i++) {
1173                         /* Read value from data fifo */
1174                         lval = inl(dev->iobase + ME4000_AI_DATA_REG) & 0xFFFF;
1175                         lval ^= 0x8000;
1176
1177                         if (!comedi_buf_put(s->async, lval)) {
1178                                 /*
1179                                  * Buffer overflow, so stop conversion
1180                                  * and disable all interrupts
1181                                  */
1182                                 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1183                                 tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ |
1184                                          ME4000_AI_CTRL_BIT_SC_IRQ);
1185                                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1186
1187                                 s->async->events |= COMEDI_CB_OVERFLOW;
1188
1189                                 dev_err(dev->class_dev, "Buffer overflow\n");
1190
1191                                 break;
1192                         }
1193                 }
1194
1195                 /* Work is done, so reset the interrupt */
1196                 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
1197                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1198                 tmp &= ~ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
1199                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1200         }
1201
1202         if (inl(dev->iobase + ME4000_IRQ_STATUS_REG) &
1203             ME4000_IRQ_STATUS_BIT_SC) {
1204                 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOA;
1205
1206                 /*
1207                  * Acquisition is complete, so stop
1208                  * conversion and disable all interrupts
1209                  */
1210                 tmp = inl(dev->iobase + ME4000_AI_CTRL_REG);
1211                 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1212                 tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ);
1213                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1214
1215                 /* Poll data until fifo empty */
1216                 while (inl(dev->iobase + ME4000_AI_CTRL_REG) &
1217                        ME4000_AI_STATUS_BIT_EF_DATA) {
1218                         /* Read value from data fifo */
1219                         lval = inl(dev->iobase + ME4000_AI_DATA_REG) & 0xFFFF;
1220                         lval ^= 0x8000;
1221
1222                         if (!comedi_buf_put(s->async, lval)) {
1223                                 dev_err(dev->class_dev, "Buffer overflow\n");
1224                                 s->async->events |= COMEDI_CB_OVERFLOW;
1225                                 break;
1226                         }
1227                 }
1228
1229                 /* Work is done, so reset the interrupt */
1230                 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
1231                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1232                 tmp &= ~ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
1233                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1234         }
1235
1236         if (s->async->events)
1237                 comedi_event(dev, s);
1238
1239         return IRQ_HANDLED;
1240 }
1241
1242 /*=============================================================================
1243   Analog output section
1244   ===========================================================================*/
1245
1246 static int me4000_ao_insn_write(struct comedi_device *dev,
1247                                 struct comedi_subdevice *s,
1248                                 struct comedi_insn *insn, unsigned int *data)
1249 {
1250         const struct me4000_board *thisboard = comedi_board(dev);
1251         struct me4000_info *info = dev->private;
1252         int chan = CR_CHAN(insn->chanspec);
1253         int rang = CR_RANGE(insn->chanspec);
1254         int aref = CR_AREF(insn->chanspec);
1255         unsigned long tmp;
1256
1257         if (insn->n == 0) {
1258                 return 0;
1259         } else if (insn->n > 1) {
1260                 dev_err(dev->class_dev, "Invalid instruction length %d\n",
1261                         insn->n);
1262                 return -EINVAL;
1263         }
1264
1265         if (chan >= thisboard->ao_nchan) {
1266                 dev_err(dev->class_dev, "Invalid channel %d\n", insn->n);
1267                 return -EINVAL;
1268         }
1269
1270         if (rang != 0) {
1271                 dev_err(dev->class_dev, "Invalid range %d\n", insn->n);
1272                 return -EINVAL;
1273         }
1274
1275         if (aref != AREF_GROUND && aref != AREF_COMMON) {
1276                 dev_err(dev->class_dev, "Invalid aref %d\n", insn->n);
1277                 return -EINVAL;
1278         }
1279
1280         /* Stop any running conversion */
1281         tmp = inl(dev->iobase + ME4000_AO_CTRL_REG(chan));
1282         tmp |= ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
1283         outl(tmp, dev->iobase + ME4000_AO_CTRL_REG(chan));
1284
1285         /* Clear control register and set to single mode */
1286         outl(0x0, dev->iobase + ME4000_AO_CTRL_REG(chan));
1287
1288         /* Write data value */
1289         outl(data[0], dev->iobase + ME4000_AO_SINGLE_REG(chan));
1290
1291         /* Store in the mirror */
1292         info->ao_readback[chan] = data[0];
1293
1294         return 1;
1295 }
1296
1297 static int me4000_ao_insn_read(struct comedi_device *dev,
1298                                struct comedi_subdevice *s,
1299                                struct comedi_insn *insn, unsigned int *data)
1300 {
1301         struct me4000_info *info = dev->private;
1302         int chan = CR_CHAN(insn->chanspec);
1303
1304         if (insn->n == 0) {
1305                 return 0;
1306         } else if (insn->n > 1) {
1307                 dev_err(dev->class_dev, "Invalid instruction length\n");
1308                 return -EINVAL;
1309         }
1310
1311         data[0] = info->ao_readback[chan];
1312
1313         return 1;
1314 }
1315
1316 /*=============================================================================
1317   Digital I/O section
1318   ===========================================================================*/
1319
1320 static int me4000_dio_insn_bits(struct comedi_device *dev,
1321                                 struct comedi_subdevice *s,
1322                                 struct comedi_insn *insn, unsigned int *data)
1323 {
1324         /*
1325          * The insn data consists of a mask in data[0] and the new data
1326          * in data[1]. The mask defines which bits we are concerning about.
1327          * The new data must be anded with the mask.
1328          * Each channel corresponds to a bit.
1329          */
1330         if (data[0]) {
1331                 /* Check if requested ports are configured for output */
1332                 if ((s->io_bits & data[0]) != data[0])
1333                         return -EIO;
1334
1335                 s->state &= ~data[0];
1336                 s->state |= data[0] & data[1];
1337
1338                 /* Write out the new digital output lines */
1339                 outl((s->state >> 0) & 0xFF,
1340                             dev->iobase + ME4000_DIO_PORT_0_REG);
1341                 outl((s->state >> 8) & 0xFF,
1342                             dev->iobase + ME4000_DIO_PORT_1_REG);
1343                 outl((s->state >> 16) & 0xFF,
1344                             dev->iobase + ME4000_DIO_PORT_2_REG);
1345                 outl((s->state >> 24) & 0xFF,
1346                             dev->iobase + ME4000_DIO_PORT_3_REG);
1347         }
1348
1349         /* On return, data[1] contains the value of
1350            the digital input and output lines. */
1351         data[1] = ((inl(dev->iobase + ME4000_DIO_PORT_0_REG) & 0xFF) << 0) |
1352                   ((inl(dev->iobase + ME4000_DIO_PORT_1_REG) & 0xFF) << 8) |
1353                   ((inl(dev->iobase + ME4000_DIO_PORT_2_REG) & 0xFF) << 16) |
1354                   ((inl(dev->iobase + ME4000_DIO_PORT_3_REG) & 0xFF) << 24);
1355
1356         return insn->n;
1357 }
1358
1359 static int me4000_dio_insn_config(struct comedi_device *dev,
1360                                   struct comedi_subdevice *s,
1361                                   struct comedi_insn *insn,
1362                                   unsigned int *data)
1363 {
1364         unsigned int chan = CR_CHAN(insn->chanspec);
1365         unsigned int mask;
1366         unsigned int tmp;
1367         int ret;
1368
1369         if (chan < 8)
1370                 mask = 0x000000ff;
1371         else if (chan < 16)
1372                 mask = 0x0000ff00;
1373         else if (chan < 24)
1374                 mask = 0x00ff0000;
1375         else
1376                 mask = 0xff000000;
1377
1378         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
1379         if (ret)
1380                 return ret;
1381
1382         tmp = inl(dev->iobase + ME4000_DIO_CTRL_REG);
1383         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_0 | ME4000_DIO_CTRL_BIT_MODE_1 |
1384                  ME4000_DIO_CTRL_BIT_MODE_2 | ME4000_DIO_CTRL_BIT_MODE_3 |
1385                  ME4000_DIO_CTRL_BIT_MODE_4 | ME4000_DIO_CTRL_BIT_MODE_5 |
1386                  ME4000_DIO_CTRL_BIT_MODE_6 | ME4000_DIO_CTRL_BIT_MODE_7);
1387         if (s->io_bits & 0x000000ff)
1388                 tmp |= ME4000_DIO_CTRL_BIT_MODE_0;
1389         if (s->io_bits & 0x0000ff00)
1390                 tmp |= ME4000_DIO_CTRL_BIT_MODE_2;
1391         if (s->io_bits & 0x00ff0000)
1392                 tmp |= ME4000_DIO_CTRL_BIT_MODE_4;
1393         if (s->io_bits & 0xff000000)
1394                 tmp |= ME4000_DIO_CTRL_BIT_MODE_6;
1395
1396         /*
1397          * Check for optoisolated ME-4000 version.
1398          * If one the first port is a fixed output
1399          * port and the second is a fixed input port.
1400          */
1401         if (inl(dev->iobase + ME4000_DIO_DIR_REG)) {
1402                 s->io_bits |= 0x000000ff;
1403                 s->io_bits &= ~0x0000ff00;
1404                 tmp |= ME4000_DIO_CTRL_BIT_MODE_0;
1405                 tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_2 |
1406                          ME4000_DIO_CTRL_BIT_MODE_3);
1407         }
1408
1409         outl(tmp, dev->iobase + ME4000_DIO_CTRL_REG);
1410
1411         return insn->n;
1412 }
1413
1414 /*=============================================================================
1415   Counter section
1416   ===========================================================================*/
1417
1418 static int me4000_cnt_insn_config(struct comedi_device *dev,
1419                                   struct comedi_subdevice *s,
1420                                   struct comedi_insn *insn,
1421                                   unsigned int *data)
1422 {
1423         struct me4000_info *info = dev->private;
1424         int err;
1425
1426         switch (data[0]) {
1427         case GPCT_RESET:
1428                 if (insn->n != 1)
1429                         return -EINVAL;
1430
1431                 err = i8254_load(info->timer_regbase, 0, insn->chanspec, 0,
1432                                 I8254_MODE0 | I8254_BINARY);
1433                 if (err)
1434                         return err;
1435                 break;
1436         case GPCT_SET_OPERATION:
1437                 if (insn->n != 2)
1438                         return -EINVAL;
1439
1440                 err = i8254_set_mode(info->timer_regbase, 0, insn->chanspec,
1441                                 (data[1] << 1) | I8254_BINARY);
1442                 if (err)
1443                         return err;
1444                 break;
1445         default:
1446                 return -EINVAL;
1447         }
1448
1449         return insn->n;
1450 }
1451
1452 static int me4000_cnt_insn_read(struct comedi_device *dev,
1453                                 struct comedi_subdevice *s,
1454                                 struct comedi_insn *insn, unsigned int *data)
1455 {
1456         struct me4000_info *info = dev->private;
1457
1458         if (insn->n == 0)
1459                 return 0;
1460
1461         if (insn->n > 1) {
1462                 dev_err(dev->class_dev, "Invalid instruction length %d\n",
1463                         insn->n);
1464                 return -EINVAL;
1465         }
1466
1467         data[0] = i8254_read(info->timer_regbase, 0, insn->chanspec);
1468
1469         return 1;
1470 }
1471
1472 static int me4000_cnt_insn_write(struct comedi_device *dev,
1473                                  struct comedi_subdevice *s,
1474                                  struct comedi_insn *insn, unsigned int *data)
1475 {
1476         struct me4000_info *info = dev->private;
1477
1478         if (insn->n == 0) {
1479                 return 0;
1480         } else if (insn->n > 1) {
1481                 dev_err(dev->class_dev, "Invalid instruction length %d\n",
1482                         insn->n);
1483                 return -EINVAL;
1484         }
1485
1486         i8254_write(info->timer_regbase, 0, insn->chanspec, data[0]);
1487
1488         return 1;
1489 }
1490
1491 static int me4000_auto_attach(struct comedi_device *dev,
1492                               unsigned long context)
1493 {
1494         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1495         const struct me4000_board *thisboard = NULL;
1496         struct me4000_info *info;
1497         struct comedi_subdevice *s;
1498         int result;
1499
1500         if (context < ARRAY_SIZE(me4000_boards))
1501                 thisboard = &me4000_boards[context];
1502         if (!thisboard)
1503                 return -ENODEV;
1504         dev->board_ptr = thisboard;
1505         dev->board_name = thisboard->name;
1506
1507         info = comedi_alloc_devpriv(dev, sizeof(*info));
1508         if (!info)
1509                 return -ENOMEM;
1510
1511         result = comedi_pci_enable(dev);
1512         if (result)
1513                 return result;
1514
1515         info->plx_regbase = pci_resource_start(pcidev, 1);
1516         dev->iobase = pci_resource_start(pcidev, 2);
1517         info->timer_regbase = pci_resource_start(pcidev, 3);
1518         if (!info->plx_regbase || !dev->iobase || !info->timer_regbase)
1519                 return -ENODEV;
1520
1521         result = xilinx_download(dev);
1522         if (result)
1523                 return result;
1524
1525         me4000_reset(dev);
1526
1527         result = comedi_alloc_subdevices(dev, 4);
1528         if (result)
1529                 return result;
1530
1531     /*=========================================================================
1532       Analog input subdevice
1533       ========================================================================*/
1534
1535         s = &dev->subdevices[0];
1536
1537         if (thisboard->ai_nchan) {
1538                 s->type = COMEDI_SUBD_AI;
1539                 s->subdev_flags =
1540                     SDF_READABLE | SDF_COMMON | SDF_GROUND | SDF_DIFF;
1541                 s->n_chan = thisboard->ai_nchan;
1542                 s->maxdata = 0xFFFF;    /*  16 bit ADC */
1543                 s->len_chanlist = ME4000_AI_CHANNEL_LIST_COUNT;
1544                 s->range_table = &me4000_ai_range;
1545                 s->insn_read = me4000_ai_insn_read;
1546
1547                 if (pcidev->irq > 0) {
1548                         if (request_irq(pcidev->irq, me4000_ai_isr,
1549                                         IRQF_SHARED, dev->board_name, dev)) {
1550                                 dev_warn(dev->class_dev,
1551                                         "request_irq failed\n");
1552                         } else {
1553                                 dev->read_subdev = s;
1554                                 s->subdev_flags |= SDF_CMD_READ;
1555                                 s->cancel = me4000_ai_cancel;
1556                                 s->do_cmdtest = me4000_ai_do_cmd_test;
1557                                 s->do_cmd = me4000_ai_do_cmd;
1558
1559                                 dev->irq = pcidev->irq;
1560                         }
1561                 } else {
1562                         dev_warn(dev->class_dev, "No interrupt available\n");
1563                 }
1564         } else {
1565                 s->type = COMEDI_SUBD_UNUSED;
1566         }
1567
1568     /*=========================================================================
1569       Analog output subdevice
1570       ========================================================================*/
1571
1572         s = &dev->subdevices[1];
1573
1574         if (thisboard->ao_nchan) {
1575                 s->type = COMEDI_SUBD_AO;
1576                 s->subdev_flags = SDF_WRITEABLE | SDF_COMMON | SDF_GROUND;
1577                 s->n_chan = thisboard->ao_nchan;
1578                 s->maxdata = 0xFFFF;    /*  16 bit DAC */
1579                 s->range_table = &range_bipolar10;
1580                 s->insn_write = me4000_ao_insn_write;
1581                 s->insn_read = me4000_ao_insn_read;
1582         } else {
1583                 s->type = COMEDI_SUBD_UNUSED;
1584         }
1585
1586     /*=========================================================================
1587       Digital I/O subdevice
1588       ========================================================================*/
1589
1590         s = &dev->subdevices[2];
1591
1592         if (thisboard->dio_nchan) {
1593                 s->type = COMEDI_SUBD_DIO;
1594                 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1595                 s->n_chan = thisboard->dio_nchan;
1596                 s->maxdata = 1;
1597                 s->range_table = &range_digital;
1598                 s->insn_bits = me4000_dio_insn_bits;
1599                 s->insn_config = me4000_dio_insn_config;
1600         } else {
1601                 s->type = COMEDI_SUBD_UNUSED;
1602         }
1603
1604         /*
1605          * Check for optoisolated ME-4000 version. If one the first
1606          * port is a fixed output port and the second is a fixed input port.
1607          */
1608         if (!inl(dev->iobase + ME4000_DIO_DIR_REG)) {
1609                 s->io_bits |= 0xFF;
1610                 outl(ME4000_DIO_CTRL_BIT_MODE_0,
1611                         dev->iobase + ME4000_DIO_DIR_REG);
1612         }
1613
1614     /*=========================================================================
1615       Counter subdevice
1616       ========================================================================*/
1617
1618         s = &dev->subdevices[3];
1619
1620         if (thisboard->has_counter) {
1621                 s->type = COMEDI_SUBD_COUNTER;
1622                 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1623                 s->n_chan = 3;
1624                 s->maxdata = 0xFFFF;    /*  16 bit counters */
1625                 s->insn_read = me4000_cnt_insn_read;
1626                 s->insn_write = me4000_cnt_insn_write;
1627                 s->insn_config = me4000_cnt_insn_config;
1628         } else {
1629                 s->type = COMEDI_SUBD_UNUSED;
1630         }
1631
1632         return 0;
1633 }
1634
1635 static void me4000_detach(struct comedi_device *dev)
1636 {
1637         if (dev->irq)
1638                 free_irq(dev->irq, dev);
1639         if (dev->iobase)
1640                 me4000_reset(dev);
1641         comedi_pci_disable(dev);
1642 }
1643
1644 static struct comedi_driver me4000_driver = {
1645         .driver_name    = "me4000",
1646         .module         = THIS_MODULE,
1647         .auto_attach    = me4000_auto_attach,
1648         .detach         = me4000_detach,
1649 };
1650
1651 static int me4000_pci_probe(struct pci_dev *dev,
1652                             const struct pci_device_id *id)
1653 {
1654         return comedi_pci_auto_config(dev, &me4000_driver, id->driver_data);
1655 }
1656
1657 static DEFINE_PCI_DEVICE_TABLE(me4000_pci_table) = {
1658         { PCI_VDEVICE(MEILHAUS, 0x4650), BOARD_ME4650 },
1659         { PCI_VDEVICE(MEILHAUS, 0x4660), BOARD_ME4660 },
1660         { PCI_VDEVICE(MEILHAUS, 0x4661), BOARD_ME4660I },
1661         { PCI_VDEVICE(MEILHAUS, 0x4662), BOARD_ME4660S },
1662         { PCI_VDEVICE(MEILHAUS, 0x4663), BOARD_ME4660IS },
1663         { PCI_VDEVICE(MEILHAUS, 0x4670), BOARD_ME4670 },
1664         { PCI_VDEVICE(MEILHAUS, 0x4671), BOARD_ME4670I },
1665         { PCI_VDEVICE(MEILHAUS, 0x4672), BOARD_ME4670S },
1666         { PCI_VDEVICE(MEILHAUS, 0x4673), BOARD_ME4670IS },
1667         { PCI_VDEVICE(MEILHAUS, 0x4680), BOARD_ME4680 },
1668         { PCI_VDEVICE(MEILHAUS, 0x4681), BOARD_ME4680I },
1669         { PCI_VDEVICE(MEILHAUS, 0x4682), BOARD_ME4680S },
1670         { PCI_VDEVICE(MEILHAUS, 0x4683), BOARD_ME4680IS },
1671         { 0 }
1672 };
1673 MODULE_DEVICE_TABLE(pci, me4000_pci_table);
1674
1675 static struct pci_driver me4000_pci_driver = {
1676         .name           = "me4000",
1677         .id_table       = me4000_pci_table,
1678         .probe          = me4000_pci_probe,
1679         .remove         = comedi_pci_auto_unconfig,
1680 };
1681 module_comedi_pci_driver(me4000_driver, me4000_pci_driver);
1682
1683 MODULE_AUTHOR("Comedi http://www.comedi.org");
1684 MODULE_DESCRIPTION("Comedi low-level driver");
1685 MODULE_LICENSE("GPL");