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