]> Pileus Git - ~andy/linux/blob - drivers/staging/comedi/drivers/me4000.c
staging: comedi: comedidev.h: add PCI_VENDOR_ID_MEILHAUS
[~andy/linux] / 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         /*
973          * Stage 3. Check if arguments are generally valid.
974          */
975         if (cmd->chanlist_len < 1) {
976                 dev_err(dev->class_dev, "No channel list\n");
977                 cmd->chanlist_len = 1;
978                 err++;
979         }
980         if (init_ticks < 66) {
981                 dev_err(dev->class_dev, "Start arg to low\n");
982                 cmd->start_arg = 2000;
983                 err++;
984         }
985         if (scan_ticks && scan_ticks < 67) {
986                 dev_err(dev->class_dev, "Scan begin arg to low\n");
987                 cmd->scan_begin_arg = 2031;
988                 err++;
989         }
990         if (chan_ticks < 66) {
991                 dev_err(dev->class_dev, "Convert arg to low\n");
992                 cmd->convert_arg = 2000;
993                 err++;
994         }
995
996         if (err)
997                 return 3;
998
999         /*
1000          * Stage 4. Check for argument conflicts.
1001          */
1002         if (cmd->start_src == TRIG_NOW &&
1003             cmd->scan_begin_src == TRIG_TIMER &&
1004             cmd->convert_src == TRIG_TIMER) {
1005
1006                 /* Check timer arguments */
1007                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1008                         dev_err(dev->class_dev, "Invalid start arg\n");
1009                         cmd->start_arg = 2000;  /*  66 ticks at least */
1010                         err++;
1011                 }
1012                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1013                         dev_err(dev->class_dev, "Invalid convert arg\n");
1014                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1015                         err++;
1016                 }
1017                 if (scan_ticks <= cmd->chanlist_len * chan_ticks) {
1018                         dev_err(dev->class_dev, "Invalid scan end arg\n");
1019
1020                         /*  At least one tick more */
1021                         cmd->scan_end_arg = 2000 * cmd->chanlist_len + 31;
1022                         err++;
1023                 }
1024         } else if (cmd->start_src == TRIG_NOW &&
1025                    cmd->scan_begin_src == TRIG_FOLLOW &&
1026                    cmd->convert_src == TRIG_TIMER) {
1027
1028                 /* Check timer arguments */
1029                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1030                         dev_err(dev->class_dev, "Invalid start arg\n");
1031                         cmd->start_arg = 2000;  /*  66 ticks at least */
1032                         err++;
1033                 }
1034                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1035                         dev_err(dev->class_dev, "Invalid convert arg\n");
1036                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1037                         err++;
1038                 }
1039         } else if (cmd->start_src == TRIG_EXT &&
1040                    cmd->scan_begin_src == TRIG_TIMER &&
1041                    cmd->convert_src == TRIG_TIMER) {
1042
1043                 /* Check timer arguments */
1044                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1045                         dev_err(dev->class_dev, "Invalid start arg\n");
1046                         cmd->start_arg = 2000;  /*  66 ticks at least */
1047                         err++;
1048                 }
1049                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1050                         dev_err(dev->class_dev, "Invalid convert arg\n");
1051                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1052                         err++;
1053                 }
1054                 if (scan_ticks <= cmd->chanlist_len * chan_ticks) {
1055                         dev_err(dev->class_dev, "Invalid scan end arg\n");
1056
1057                         /*  At least one tick more */
1058                         cmd->scan_end_arg = 2000 * cmd->chanlist_len + 31;
1059                         err++;
1060                 }
1061         } else if (cmd->start_src == TRIG_EXT &&
1062                    cmd->scan_begin_src == TRIG_FOLLOW &&
1063                    cmd->convert_src == TRIG_TIMER) {
1064
1065                 /* Check timer arguments */
1066                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1067                         dev_err(dev->class_dev, "Invalid start arg\n");
1068                         cmd->start_arg = 2000;  /*  66 ticks at least */
1069                         err++;
1070                 }
1071                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1072                         dev_err(dev->class_dev, "Invalid convert arg\n");
1073                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1074                         err++;
1075                 }
1076         } else if (cmd->start_src == TRIG_EXT &&
1077                    cmd->scan_begin_src == TRIG_EXT &&
1078                    cmd->convert_src == TRIG_TIMER) {
1079
1080                 /* Check timer arguments */
1081                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1082                         dev_err(dev->class_dev, "Invalid start arg\n");
1083                         cmd->start_arg = 2000;  /*  66 ticks at least */
1084                         err++;
1085                 }
1086                 if (chan_ticks < ME4000_AI_MIN_TICKS) {
1087                         dev_err(dev->class_dev, "Invalid convert arg\n");
1088                         cmd->convert_arg = 2000;        /*  66 ticks at least */
1089                         err++;
1090                 }
1091         } else if (cmd->start_src == TRIG_EXT &&
1092                    cmd->scan_begin_src == TRIG_EXT &&
1093                    cmd->convert_src == TRIG_EXT) {
1094
1095                 /* Check timer arguments */
1096                 if (init_ticks < ME4000_AI_MIN_TICKS) {
1097                         dev_err(dev->class_dev, "Invalid start arg\n");
1098                         cmd->start_arg = 2000;  /*  66 ticks at least */
1099                         err++;
1100                 }
1101         }
1102         if (cmd->stop_src == TRIG_COUNT) {
1103                 if (cmd->stop_arg == 0) {
1104                         dev_err(dev->class_dev, "Invalid stop arg\n");
1105                         cmd->stop_arg = 1;
1106                         err++;
1107                 }
1108         }
1109         if (cmd->scan_end_src == TRIG_COUNT) {
1110                 if (cmd->scan_end_arg == 0) {
1111                         dev_err(dev->class_dev, "Invalid scan end arg\n");
1112                         cmd->scan_end_arg = 1;
1113                         err++;
1114                 }
1115         }
1116
1117         if (err)
1118                 return 4;
1119
1120         /*
1121          * Stage 5. Check the channel list.
1122          */
1123         if (ai_check_chanlist(dev, s, cmd))
1124                 return 5;
1125
1126         return 0;
1127 }
1128
1129 static irqreturn_t me4000_ai_isr(int irq, void *dev_id)
1130 {
1131         unsigned int tmp;
1132         struct comedi_device *dev = dev_id;
1133         struct comedi_subdevice *s = &dev->subdevices[0];
1134         int i;
1135         int c = 0;
1136         long lval;
1137
1138         if (!dev->attached)
1139                 return IRQ_NONE;
1140
1141         /* Reset all events */
1142         s->async->events = 0;
1143
1144         /* Check if irq number is right */
1145         if (irq != dev->irq) {
1146                 dev_err(dev->class_dev, "Incorrect interrupt num: %d\n", irq);
1147                 return IRQ_HANDLED;
1148         }
1149
1150         if (inl(dev->iobase + ME4000_IRQ_STATUS_REG) &
1151             ME4000_IRQ_STATUS_BIT_AI_HF) {
1152                 /* Read status register to find out what happened */
1153                 tmp = inl(dev->iobase + ME4000_AI_CTRL_REG);
1154
1155                 if (!(tmp & ME4000_AI_STATUS_BIT_FF_DATA) &&
1156                     !(tmp & ME4000_AI_STATUS_BIT_HF_DATA) &&
1157                     (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
1158                         c = ME4000_AI_FIFO_COUNT;
1159
1160                         /*
1161                          * FIFO overflow, so stop conversion
1162                          * and disable all interrupts
1163                          */
1164                         tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1165                         tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ |
1166                                  ME4000_AI_CTRL_BIT_SC_IRQ);
1167                         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1168
1169                         s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1170
1171                         dev_err(dev->class_dev, "FIFO overflow\n");
1172                 } else if ((tmp & ME4000_AI_STATUS_BIT_FF_DATA)
1173                            && !(tmp & ME4000_AI_STATUS_BIT_HF_DATA)
1174                            && (tmp & ME4000_AI_STATUS_BIT_EF_DATA)) {
1175                         s->async->events |= COMEDI_CB_BLOCK;
1176
1177                         c = ME4000_AI_FIFO_COUNT / 2;
1178                 } else {
1179                         dev_err(dev->class_dev,
1180                                 "Can't determine state of fifo\n");
1181                         c = 0;
1182
1183                         /*
1184                          * Undefined state, so stop conversion
1185                          * and disable all interrupts
1186                          */
1187                         tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1188                         tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ |
1189                                  ME4000_AI_CTRL_BIT_SC_IRQ);
1190                         outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1191
1192                         s->async->events |= COMEDI_CB_ERROR | COMEDI_CB_EOA;
1193
1194                         dev_err(dev->class_dev, "Undefined FIFO state\n");
1195                 }
1196
1197                 for (i = 0; i < c; i++) {
1198                         /* Read value from data fifo */
1199                         lval = inl(dev->iobase + ME4000_AI_DATA_REG) & 0xFFFF;
1200                         lval ^= 0x8000;
1201
1202                         if (!comedi_buf_put(s->async, lval)) {
1203                                 /*
1204                                  * Buffer overflow, so stop conversion
1205                                  * and disable all interrupts
1206                                  */
1207                                 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1208                                 tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ |
1209                                          ME4000_AI_CTRL_BIT_SC_IRQ);
1210                                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1211
1212                                 s->async->events |= COMEDI_CB_OVERFLOW;
1213
1214                                 dev_err(dev->class_dev, "Buffer overflow\n");
1215
1216                                 break;
1217                         }
1218                 }
1219
1220                 /* Work is done, so reset the interrupt */
1221                 tmp |= ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
1222                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1223                 tmp &= ~ME4000_AI_CTRL_BIT_HF_IRQ_RESET;
1224                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1225         }
1226
1227         if (inl(dev->iobase + ME4000_IRQ_STATUS_REG) &
1228             ME4000_IRQ_STATUS_BIT_SC) {
1229                 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOA;
1230
1231                 /*
1232                  * Acquisition is complete, so stop
1233                  * conversion and disable all interrupts
1234                  */
1235                 tmp = inl(dev->iobase + ME4000_AI_CTRL_REG);
1236                 tmp |= ME4000_AI_CTRL_BIT_IMMEDIATE_STOP;
1237                 tmp &= ~(ME4000_AI_CTRL_BIT_HF_IRQ | ME4000_AI_CTRL_BIT_SC_IRQ);
1238                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1239
1240                 /* Poll data until fifo empty */
1241                 while (inl(dev->iobase + ME4000_AI_CTRL_REG) &
1242                        ME4000_AI_STATUS_BIT_EF_DATA) {
1243                         /* Read value from data fifo */
1244                         lval = inl(dev->iobase + ME4000_AI_DATA_REG) & 0xFFFF;
1245                         lval ^= 0x8000;
1246
1247                         if (!comedi_buf_put(s->async, lval)) {
1248                                 dev_err(dev->class_dev, "Buffer overflow\n");
1249                                 s->async->events |= COMEDI_CB_OVERFLOW;
1250                                 break;
1251                         }
1252                 }
1253
1254                 /* Work is done, so reset the interrupt */
1255                 tmp |= ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
1256                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1257                 tmp &= ~ME4000_AI_CTRL_BIT_SC_IRQ_RESET;
1258                 outl(tmp, dev->iobase + ME4000_AI_CTRL_REG);
1259         }
1260
1261         if (s->async->events)
1262                 comedi_event(dev, s);
1263
1264         return IRQ_HANDLED;
1265 }
1266
1267 /*=============================================================================
1268   Analog output section
1269   ===========================================================================*/
1270
1271 static int me4000_ao_insn_write(struct comedi_device *dev,
1272                                 struct comedi_subdevice *s,
1273                                 struct comedi_insn *insn, unsigned int *data)
1274 {
1275         const struct me4000_board *thisboard = comedi_board(dev);
1276         struct me4000_info *info = dev->private;
1277         int chan = CR_CHAN(insn->chanspec);
1278         int rang = CR_RANGE(insn->chanspec);
1279         int aref = CR_AREF(insn->chanspec);
1280         unsigned long tmp;
1281
1282         if (insn->n == 0) {
1283                 return 0;
1284         } else if (insn->n > 1) {
1285                 dev_err(dev->class_dev, "Invalid instruction length %d\n",
1286                         insn->n);
1287                 return -EINVAL;
1288         }
1289
1290         if (chan >= thisboard->ao_nchan) {
1291                 dev_err(dev->class_dev, "Invalid channel %d\n", insn->n);
1292                 return -EINVAL;
1293         }
1294
1295         if (rang != 0) {
1296                 dev_err(dev->class_dev, "Invalid range %d\n", insn->n);
1297                 return -EINVAL;
1298         }
1299
1300         if (aref != AREF_GROUND && aref != AREF_COMMON) {
1301                 dev_err(dev->class_dev, "Invalid aref %d\n", insn->n);
1302                 return -EINVAL;
1303         }
1304
1305         /* Stop any running conversion */
1306         tmp = inl(dev->iobase + ME4000_AO_CTRL_REG(chan));
1307         tmp |= ME4000_AO_CTRL_BIT_IMMEDIATE_STOP;
1308         outl(tmp, dev->iobase + ME4000_AO_CTRL_REG(chan));
1309
1310         /* Clear control register and set to single mode */
1311         outl(0x0, dev->iobase + ME4000_AO_CTRL_REG(chan));
1312
1313         /* Write data value */
1314         outl(data[0], dev->iobase + ME4000_AO_SINGLE_REG(chan));
1315
1316         /* Store in the mirror */
1317         info->ao_readback[chan] = data[0];
1318
1319         return 1;
1320 }
1321
1322 static int me4000_ao_insn_read(struct comedi_device *dev,
1323                                struct comedi_subdevice *s,
1324                                struct comedi_insn *insn, unsigned int *data)
1325 {
1326         struct me4000_info *info = dev->private;
1327         int chan = CR_CHAN(insn->chanspec);
1328
1329         if (insn->n == 0) {
1330                 return 0;
1331         } else if (insn->n > 1) {
1332                 dev_err(dev->class_dev, "Invalid instruction length\n");
1333                 return -EINVAL;
1334         }
1335
1336         data[0] = info->ao_readback[chan];
1337
1338         return 1;
1339 }
1340
1341 /*=============================================================================
1342   Digital I/O section
1343   ===========================================================================*/
1344
1345 static int me4000_dio_insn_bits(struct comedi_device *dev,
1346                                 struct comedi_subdevice *s,
1347                                 struct comedi_insn *insn, unsigned int *data)
1348 {
1349         /*
1350          * The insn data consists of a mask in data[0] and the new data
1351          * in data[1]. The mask defines which bits we are concerning about.
1352          * The new data must be anded with the mask.
1353          * Each channel corresponds to a bit.
1354          */
1355         if (data[0]) {
1356                 /* Check if requested ports are configured for output */
1357                 if ((s->io_bits & data[0]) != data[0])
1358                         return -EIO;
1359
1360                 s->state &= ~data[0];
1361                 s->state |= data[0] & data[1];
1362
1363                 /* Write out the new digital output lines */
1364                 outl((s->state >> 0) & 0xFF,
1365                             dev->iobase + ME4000_DIO_PORT_0_REG);
1366                 outl((s->state >> 8) & 0xFF,
1367                             dev->iobase + ME4000_DIO_PORT_1_REG);
1368                 outl((s->state >> 16) & 0xFF,
1369                             dev->iobase + ME4000_DIO_PORT_2_REG);
1370                 outl((s->state >> 24) & 0xFF,
1371                             dev->iobase + ME4000_DIO_PORT_3_REG);
1372         }
1373
1374         /* On return, data[1] contains the value of
1375            the digital input and output lines. */
1376         data[1] = ((inl(dev->iobase + ME4000_DIO_PORT_0_REG) & 0xFF) << 0) |
1377                   ((inl(dev->iobase + ME4000_DIO_PORT_1_REG) & 0xFF) << 8) |
1378                   ((inl(dev->iobase + ME4000_DIO_PORT_2_REG) & 0xFF) << 16) |
1379                   ((inl(dev->iobase + ME4000_DIO_PORT_3_REG) & 0xFF) << 24);
1380
1381         return insn->n;
1382 }
1383
1384 static int me4000_dio_insn_config(struct comedi_device *dev,
1385                                   struct comedi_subdevice *s,
1386                                   struct comedi_insn *insn, unsigned int *data)
1387 {
1388         unsigned long tmp;
1389         int chan = CR_CHAN(insn->chanspec);
1390
1391         switch (data[0]) {
1392         default:
1393                 return -EINVAL;
1394         case INSN_CONFIG_DIO_QUERY:
1395                 data[1] =
1396                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1397                 return insn->n;
1398         case INSN_CONFIG_DIO_INPUT:
1399         case INSN_CONFIG_DIO_OUTPUT:
1400                 break;
1401         }
1402
1403         /*
1404          * The input or output configuration of each digital line is
1405          * configured by a special insn_config instruction.  chanspec
1406          * contains the channel to be changed, and data[0] contains the
1407          * value INSN_CONFIG_DIO_INPUT or INSN_CONFIG_DIO_OUTPUT.
1408          * On the ME-4000 it is only possible to switch port wise (8 bit)
1409          */
1410
1411         tmp = inl(dev->iobase + ME4000_DIO_CTRL_REG);
1412
1413         if (data[0] == INSN_CONFIG_DIO_OUTPUT) {
1414                 if (chan < 8) {
1415                         s->io_bits |= 0xFF;
1416                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_0 |
1417                                  ME4000_DIO_CTRL_BIT_MODE_1);
1418                         tmp |= ME4000_DIO_CTRL_BIT_MODE_0;
1419                 } else if (chan < 16) {
1420                         /*
1421                          * Chech for optoisolated ME-4000 version.
1422                          * If one the first port is a fixed output
1423                          * port and the second is a fixed input port.
1424                          */
1425                         if (!inl(dev->iobase + ME4000_DIO_DIR_REG))
1426                                 return -ENODEV;
1427
1428                         s->io_bits |= 0xFF00;
1429                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_2 |
1430                                  ME4000_DIO_CTRL_BIT_MODE_3);
1431                         tmp |= ME4000_DIO_CTRL_BIT_MODE_2;
1432                 } else if (chan < 24) {
1433                         s->io_bits |= 0xFF0000;
1434                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_4 |
1435                                  ME4000_DIO_CTRL_BIT_MODE_5);
1436                         tmp |= ME4000_DIO_CTRL_BIT_MODE_4;
1437                 } else if (chan < 32) {
1438                         s->io_bits |= 0xFF000000;
1439                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_6 |
1440                                  ME4000_DIO_CTRL_BIT_MODE_7);
1441                         tmp |= ME4000_DIO_CTRL_BIT_MODE_6;
1442                 } else {
1443                         return -EINVAL;
1444                 }
1445         } else {
1446                 if (chan < 8) {
1447                         /*
1448                          * Chech for optoisolated ME-4000 version.
1449                          * If one the first port is a fixed output
1450                          * port and the second is a fixed input port.
1451                          */
1452                         if (!inl(dev->iobase + ME4000_DIO_DIR_REG))
1453                                 return -ENODEV;
1454
1455                         s->io_bits &= ~0xFF;
1456                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_0 |
1457                                  ME4000_DIO_CTRL_BIT_MODE_1);
1458                 } else if (chan < 16) {
1459                         s->io_bits &= ~0xFF00;
1460                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_2 |
1461                                  ME4000_DIO_CTRL_BIT_MODE_3);
1462                 } else if (chan < 24) {
1463                         s->io_bits &= ~0xFF0000;
1464                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_4 |
1465                                  ME4000_DIO_CTRL_BIT_MODE_5);
1466                 } else if (chan < 32) {
1467                         s->io_bits &= ~0xFF000000;
1468                         tmp &= ~(ME4000_DIO_CTRL_BIT_MODE_6 |
1469                                  ME4000_DIO_CTRL_BIT_MODE_7);
1470                 } else {
1471                         return -EINVAL;
1472                 }
1473         }
1474
1475         outl(tmp, dev->iobase + ME4000_DIO_CTRL_REG);
1476
1477         return 1;
1478 }
1479
1480 /*=============================================================================
1481   Counter section
1482   ===========================================================================*/
1483
1484 static int me4000_cnt_insn_config(struct comedi_device *dev,
1485                                   struct comedi_subdevice *s,
1486                                   struct comedi_insn *insn,
1487                                   unsigned int *data)
1488 {
1489         struct me4000_info *info = dev->private;
1490         int err;
1491
1492         switch (data[0]) {
1493         case GPCT_RESET:
1494                 if (insn->n != 1)
1495                         return -EINVAL;
1496
1497                 err = i8254_load(info->timer_regbase, 0, insn->chanspec, 0,
1498                                 I8254_MODE0 | I8254_BINARY);
1499                 if (err)
1500                         return err;
1501                 break;
1502         case GPCT_SET_OPERATION:
1503                 if (insn->n != 2)
1504                         return -EINVAL;
1505
1506                 err = i8254_set_mode(info->timer_regbase, 0, insn->chanspec,
1507                                 (data[1] << 1) | I8254_BINARY);
1508                 if (err)
1509                         return err;
1510                 break;
1511         default:
1512                 return -EINVAL;
1513         }
1514
1515         return insn->n;
1516 }
1517
1518 static int me4000_cnt_insn_read(struct comedi_device *dev,
1519                                 struct comedi_subdevice *s,
1520                                 struct comedi_insn *insn, unsigned int *data)
1521 {
1522         struct me4000_info *info = dev->private;
1523
1524         if (insn->n == 0)
1525                 return 0;
1526
1527         if (insn->n > 1) {
1528                 dev_err(dev->class_dev, "Invalid instruction length %d\n",
1529                         insn->n);
1530                 return -EINVAL;
1531         }
1532
1533         data[0] = i8254_read(info->timer_regbase, 0, insn->chanspec);
1534
1535         return 1;
1536 }
1537
1538 static int me4000_cnt_insn_write(struct comedi_device *dev,
1539                                  struct comedi_subdevice *s,
1540                                  struct comedi_insn *insn, unsigned int *data)
1541 {
1542         struct me4000_info *info = dev->private;
1543
1544         if (insn->n == 0) {
1545                 return 0;
1546         } else if (insn->n > 1) {
1547                 dev_err(dev->class_dev, "Invalid instruction length %d\n",
1548                         insn->n);
1549                 return -EINVAL;
1550         }
1551
1552         i8254_write(info->timer_regbase, 0, insn->chanspec, data[0]);
1553
1554         return 1;
1555 }
1556
1557 static const void *me4000_find_boardinfo(struct comedi_device *dev,
1558                                          struct pci_dev *pcidev)
1559 {
1560         const struct me4000_board *thisboard;
1561         int i;
1562
1563         for (i = 0; i < ARRAY_SIZE(me4000_boards); i++) {
1564                 thisboard = &me4000_boards[i];
1565                 if (thisboard->device_id == pcidev->device)
1566                         return thisboard;
1567         }
1568         return NULL;
1569 }
1570
1571 static int me4000_attach_pci(struct comedi_device *dev,
1572                              struct pci_dev *pcidev)
1573 {
1574         const struct me4000_board *thisboard;
1575         struct me4000_info *info;
1576         struct comedi_subdevice *s;
1577         int result;
1578
1579         thisboard = me4000_find_boardinfo(dev, pcidev);
1580         if (!thisboard)
1581                 return -ENODEV;
1582         dev->board_ptr = thisboard;
1583         dev->board_name = thisboard->name;
1584
1585         info = kzalloc(sizeof(*info), GFP_KERNEL);
1586         if (!info)
1587                 return -ENOMEM;
1588         dev->private = info;
1589
1590         result = comedi_pci_enable(pcidev, dev->board_name);
1591         if (result)
1592                 return result;
1593
1594         info->plx_regbase = pci_resource_start(pcidev, 1);
1595         dev->iobase = pci_resource_start(pcidev, 2);
1596         info->timer_regbase = pci_resource_start(pcidev, 3);
1597         if (!info->plx_regbase || !dev->iobase || !info->timer_regbase)
1598                 return -ENODEV;
1599
1600         result = xilinx_download(dev);
1601         if (result)
1602                 return result;
1603
1604         me4000_reset(dev);
1605
1606         result = comedi_alloc_subdevices(dev, 4);
1607         if (result)
1608                 return result;
1609
1610     /*=========================================================================
1611       Analog input subdevice
1612       ========================================================================*/
1613
1614         s = &dev->subdevices[0];
1615
1616         if (thisboard->ai_nchan) {
1617                 s->type = COMEDI_SUBD_AI;
1618                 s->subdev_flags =
1619                     SDF_READABLE | SDF_COMMON | SDF_GROUND | SDF_DIFF;
1620                 s->n_chan = thisboard->ai_nchan;
1621                 s->maxdata = 0xFFFF;    /*  16 bit ADC */
1622                 s->len_chanlist = ME4000_AI_CHANNEL_LIST_COUNT;
1623                 s->range_table = &me4000_ai_range;
1624                 s->insn_read = me4000_ai_insn_read;
1625
1626                 if (pcidev->irq > 0) {
1627                         if (request_irq(pcidev->irq, me4000_ai_isr,
1628                                         IRQF_SHARED, dev->board_name, dev)) {
1629                                 dev_warn(dev->class_dev,
1630                                         "request_irq failed\n");
1631                         } else {
1632                                 dev->read_subdev = s;
1633                                 s->subdev_flags |= SDF_CMD_READ;
1634                                 s->cancel = me4000_ai_cancel;
1635                                 s->do_cmdtest = me4000_ai_do_cmd_test;
1636                                 s->do_cmd = me4000_ai_do_cmd;
1637
1638                                 dev->irq = pcidev->irq;
1639                         }
1640                 } else {
1641                         dev_warn(dev->class_dev, "No interrupt available\n");
1642                 }
1643         } else {
1644                 s->type = COMEDI_SUBD_UNUSED;
1645         }
1646
1647     /*=========================================================================
1648       Analog output subdevice
1649       ========================================================================*/
1650
1651         s = &dev->subdevices[1];
1652
1653         if (thisboard->ao_nchan) {
1654                 s->type = COMEDI_SUBD_AO;
1655                 s->subdev_flags = SDF_WRITEABLE | SDF_COMMON | SDF_GROUND;
1656                 s->n_chan = thisboard->ao_nchan;
1657                 s->maxdata = 0xFFFF;    /*  16 bit DAC */
1658                 s->range_table = &range_bipolar10;
1659                 s->insn_write = me4000_ao_insn_write;
1660                 s->insn_read = me4000_ao_insn_read;
1661         } else {
1662                 s->type = COMEDI_SUBD_UNUSED;
1663         }
1664
1665     /*=========================================================================
1666       Digital I/O subdevice
1667       ========================================================================*/
1668
1669         s = &dev->subdevices[2];
1670
1671         if (thisboard->dio_nchan) {
1672                 s->type = COMEDI_SUBD_DIO;
1673                 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1674                 s->n_chan = thisboard->dio_nchan;
1675                 s->maxdata = 1;
1676                 s->range_table = &range_digital;
1677                 s->insn_bits = me4000_dio_insn_bits;
1678                 s->insn_config = me4000_dio_insn_config;
1679         } else {
1680                 s->type = COMEDI_SUBD_UNUSED;
1681         }
1682
1683         /*
1684          * Check for optoisolated ME-4000 version. If one the first
1685          * port is a fixed output port and the second is a fixed input port.
1686          */
1687         if (!inl(dev->iobase + ME4000_DIO_DIR_REG)) {
1688                 s->io_bits |= 0xFF;
1689                 outl(ME4000_DIO_CTRL_BIT_MODE_0,
1690                         dev->iobase + ME4000_DIO_DIR_REG);
1691         }
1692
1693     /*=========================================================================
1694       Counter subdevice
1695       ========================================================================*/
1696
1697         s = &dev->subdevices[3];
1698
1699         if (thisboard->has_counter) {
1700                 s->type = COMEDI_SUBD_COUNTER;
1701                 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1702                 s->n_chan = 3;
1703                 s->maxdata = 0xFFFF;    /*  16 bit counters */
1704                 s->insn_read = me4000_cnt_insn_read;
1705                 s->insn_write = me4000_cnt_insn_write;
1706                 s->insn_config = me4000_cnt_insn_config;
1707         } else {
1708                 s->type = COMEDI_SUBD_UNUSED;
1709         }
1710
1711         return 0;
1712 }
1713
1714 static void me4000_detach(struct comedi_device *dev)
1715 {
1716         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1717
1718         if (dev->irq)
1719                 free_irq(dev->irq, dev);
1720         if (pcidev) {
1721                 if (dev->iobase) {
1722                         me4000_reset(dev);
1723                         comedi_pci_disable(pcidev);
1724                 }
1725         }
1726 }
1727
1728 static struct comedi_driver me4000_driver = {
1729         .driver_name    = "me4000",
1730         .module         = THIS_MODULE,
1731         .attach_pci     = me4000_attach_pci,
1732         .detach         = me4000_detach,
1733 };
1734
1735 static int __devinit me4000_pci_probe(struct pci_dev *dev,
1736                                       const struct pci_device_id *ent)
1737 {
1738         return comedi_pci_auto_config(dev, &me4000_driver);
1739 }
1740
1741 static void __devexit me4000_pci_remove(struct pci_dev *dev)
1742 {
1743         comedi_pci_auto_unconfig(dev);
1744 }
1745
1746 static DEFINE_PCI_DEVICE_TABLE(me4000_pci_table) = {
1747         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4650)},
1748         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4660)},
1749         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4660I)},
1750         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4660S)},
1751         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4660IS)},
1752         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670)},
1753         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670I)},
1754         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670S)},
1755         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4670IS)},
1756         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680)},
1757         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680I)},
1758         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680S)},
1759         {PCI_DEVICE(PCI_VENDOR_ID_MEILHAUS, PCI_DEVICE_ID_MEILHAUS_ME4680IS)},
1760         {0}
1761 };
1762 MODULE_DEVICE_TABLE(pci, me4000_pci_table);
1763
1764 static struct pci_driver me4000_pci_driver = {
1765         .name           = "me4000",
1766         .id_table       = me4000_pci_table,
1767         .probe          = me4000_pci_probe,
1768         .remove         = __devexit_p(me4000_pci_remove),
1769 };
1770 module_comedi_pci_driver(me4000_driver, me4000_pci_driver);
1771
1772 MODULE_AUTHOR("Comedi http://www.comedi.org");
1773 MODULE_DESCRIPTION("Comedi low-level driver");
1774 MODULE_LICENSE("GPL");