amplc_dio200: fix subdevice type for 'timer' subdevice
[comedi.git] / comedi / drivers / amplc_dio200.c
1 /*
2     comedi/drivers/amplc_dio200.c
3     Driver for various Amplicon 200 series DIO boards.
4     (Support for other boards in Amplicon 200 series may be added at
5     a later date, e.g. PCI215.)
6
7     Copyright (C) 2005-2012 MEV Ltd. <http://www.mev.co.uk/>
8
9     COMEDI - Linux Control and Measurement Device Interface
10     Copyright (C) 1998,2000 David A. Schleef <ds@schleef.org>
11
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 */
27 /*
28 Driver: amplc_dio200
29 Description: Amplicon 200 Series Digital I/O
30 Author: Ian Abbott <abbotti@mev.co.uk>
31 Devices: [Amplicon] PC212E (pc212e), PC214E (pc214e), PC215E (pc215e),
32   PCI215 (pci215 or amplc_dio200), PCIe215 (pcie215 or amplc_dio200),
33   PC218E (pc218e), PCIe236 (pcie236 or amplc_dio200), PC272E (pc272e),
34   PCI272 (pci272 or amplc_dio200), PCIe296 (pcie296 or amplc_dio200)
35 Updated: Wed, 03 Oct 2012 17:54:05 +0100
36 Status: works
37
38 Configuration options - PC212E, PC214E, PC215E, PC218E, PC272E:
39   [0] - I/O port base address
40   [1] - IRQ (optional, but commands won't work without it)
41
42 Configuration options - PCI215, PCIe215, PCIe236, PCI272, PCIe296:
43   [0] - PCI bus of device (optional)
44   [1] - PCI slot of device (optional)
45   If bus/slot is not specified, the first available PCI device will
46   be used.
47
48 Passing a zero for an option is the same as leaving it unspecified.
49
50 SUBDEVICES
51
52                     PC212E         PC214E      PC215E/PCI215
53                  -------------  -------------  -------------
54   Subdevices           6              4              5
55    0                 PPI-X          PPI-X          PPI-X
56    1                 CTR-Y1         PPI-Y          PPI-Y
57    2                 CTR-Y2         CTR-Z1*        CTR-Z1
58    3                 CTR-Z1       INTERRUPT*       CTR-Z2
59    4                 CTR-Z2                      INTERRUPT
60    5               INTERRUPT
61
62                     PCIe215        PC218E         PCIe236
63                  -------------  -------------  -------------
64   Subdevices           8              7              8
65    0                 PPI-X          CTR-X1         PPI-X
66    1                 UNUSED         CTR-X2         UNUSED
67    2                 PPI-Y          CTR-Y1         UNUSED
68    3                 UNUSED         CTR-Y2         UNUSED
69    4                 CTR-Z1         CTR-Z1         CTR-Z1
70    5                 CTR-Z2         CTR-Z2         CTR-Z2
71    6                 TIMER        INTERRUPT        TIMER
72    7               INTERRUPT                     INTERRUPT
73
74                  PC272E/PCI272     PCIe296
75                  -------------  -------------
76   Subdevices           4              8
77    0                 PPI-X          PPI-X1
78    1                 PPI-Y          PPI-X2
79    2                 PPI-Z          PPI-Y1
80    3               INTERRUPT        PPI-Y2
81    4                                CTR-Z1
82    5                                CTR-Z2
83    6                                TIMER
84    7                              INTERRUPT
85
86 Each PPI is a 8255 chip providing 24 DIO channels.  The DIO channels
87 are configurable as inputs or outputs in four groups:
88
89   Port A  - channels  0 to  7
90   Port B  - channels  8 to 15
91   Port CL - channels 16 to 19
92   Port CH - channels 20 to 23
93
94 Only mode 0 of the 8255 chips is supported.
95
96 Each CTR is a 8254 chip providing 3 16-bit counter channels.  Each
97 channel is configured individually with INSN_CONFIG instructions.  The
98 specific type of configuration instruction is specified in data[0].
99 Some configuration instructions expect an additional parameter in
100 data[1]; others return a value in data[1].  The following configuration
101 instructions are supported:
102
103   INSN_CONFIG_SET_COUNTER_MODE.  Sets the counter channel's mode and
104     BCD/binary setting specified in data[1].
105
106   INSN_CONFIG_8254_READ_STATUS.  Reads the status register value for the
107     counter channel into data[1].
108
109   INSN_CONFIG_SET_CLOCK_SRC.  Sets the counter channel's clock source as
110     specified in data[1] (this is a hardware-specific value).  Not
111     supported on PC214E.  For the other boards, valid clock sources are
112     0 to 7 as follows:
113
114       0.  CLK n, the counter channel's dedicated CLK input from the SK1
115         connector.  (N.B. for other values, the counter channel's CLKn
116         pin on the SK1 connector is an output!)
117       1.  Internal 10 MHz clock.
118       2.  Internal 1 MHz clock.
119       3.  Internal 100 kHz clock.
120       4.  Internal 10 kHz clock.
121       5.  Internal 1 kHz clock.
122       6.  OUT n-1, the output of counter channel n-1 (see note 1 below).
123       7.  Ext Clock, the counter chip's dedicated Ext Clock input from
124         the SK1 connector.  This pin is shared by all three counter
125         channels on the chip.
126
127   INSN_CONFIG_GET_CLOCK_SRC.  Returns the counter channel's current
128     clock source in data[1].  For internal clock sources, data[2] is set
129     to the period in ns.
130
131   INSN_CONFIG_SET_GATE_SRC.  Sets the counter channel's gate source as
132     specified in data[2] (this is a hardware-specific value).  Not
133     supported on PC214E.  For the other boards, valid gate sources are 0
134     to 7 as follows:
135
136       0.  VCC (internal +5V d.c.), i.e. gate permanently enabled.
137       1.  GND (internal 0V d.c.), i.e. gate permanently disabled.
138       2.  GAT n, the counter channel's dedicated GAT input from the SK1
139         connector.  (N.B. for other values, the counter channel's GATn
140         pin on the SK1 connector is an output!)
141       3.  /OUT n-2, the inverted output of counter channel n-2 (see note
142         2 below).
143       4.  Reserved.
144       5.  Reserved.
145       6.  Reserved.
146       7.  Reserved.
147
148   INSN_CONFIG_GET_GATE_SRC.  Returns the counter channel's current gate
149     source in data[2].
150
151 Clock and gate interconnection notes:
152
153   1.  Clock source OUT n-1 is the output of the preceding channel on the
154   same counter subdevice if n > 0, or the output of channel 2 on the
155   preceding counter subdevice (see note 3) if n = 0.
156
157   2.  Gate source /OUT n-2 is the inverted output of channel 0 on the
158   same counter subdevice if n = 2, or the inverted output of channel n+1
159   on the preceding counter subdevice (see note 3) if n < 2.
160
161   3.  The counter subdevices are connected in a ring, so the highest
162   counter subdevice precedes the lowest.
163
164 The 'TIMER' subdevice is a free-running 32-bit timer subdevice.
165
166 The 'INTERRUPT' subdevice pretends to be a digital input subdevice.  The
167 digital inputs come from the interrupt status register.  The number of
168 channels matches the number of interrupt sources.  The PC214E does not
169 have an interrupt status register; see notes on 'INTERRUPT SOURCES'
170 below.
171
172 INTERRUPT SOURCES
173
174                     PC212E         PC214E      PC215E/PCI215
175                  -------------  -------------  -------------
176   Sources              6              1              6
177    0               PPI-X-C0       JUMPER-J5      PPI-X-C0
178    1               PPI-X-C3                      PPI-X-C3
179    2              CTR-Y1-OUT1                    PPI-Y-C0
180    3              CTR-Y2-OUT1                    PPI-Y-C3
181    4              CTR-Z1-OUT1                   CTR-Z1-OUT1
182    5              CTR-Z2-OUT1                   CTR-Z2-OUT1
183
184                     PCIe215        PC218E         PCIe236
185                  -------------  -------------  -------------
186   Sources              6              6              6
187    0               PPI-X-C0      CTR-X1-OUT1     PPI-X-C0
188    1               PPI-X-C3      CTR-X2-OUT1     PPI-X-C3
189    2               PPI-Y-C0      CTR-Y1-OUT1      unused
190    3               PPI-Y-C3      CTR-Y2-OUT1      unused
191    4              CTR-Z1-OUT1    CTR-Z1-OUT1    CTR-Z1-OUT1
192    5              CTR-Z2-OUT1    CTR-Z2-OUT1    CTR-Z2-OUT1
193
194                  PC272E/PCI272     PCIe296
195                  -------------  -------------
196   Sources              6              6
197    0               PPI-X-C0       PPI-X1-C0
198    1               PPI-X-C3       PPI-X1-C3
199    2               PPI-Y-C0       PPI-Y1-C0
200    3               PPI-Y-C3       PPI-Y1-C3
201    4               PPI-Z-C0      CTR-Z1-OUT1
202    5               PPI-Z-C3      CTR-Z2-OUT1
203
204 When an interrupt source is enabled in the interrupt source enable
205 register, a rising edge on the source signal latches the corresponding
206 bit to 1 in the interrupt status register.
207
208 When the interrupt status register value as a whole (actually, just the
209 6 least significant bits) goes from zero to non-zero, the board will
210 generate an interrupt.  For level-triggered hardware interrupts (PCI
211 card), the interrupt will remain asserted until the interrupt status
212 register is cleared to zero.  For edge-triggered hardware interrupts
213 (ISA card), no further interrupts will occur until the interrupt status
214 register is cleared to zero.  To clear a bit to zero in the interrupt
215 status register, the corresponding interrupt source must be disabled
216 in the interrupt source enable register (there is no separate interrupt
217 clear register).
218
219 The PC214E does not have an interrupt source enable register or an
220 interrupt status register; its 'INTERRUPT' subdevice has a single
221 channel and its interrupt source is selected by the position of jumper
222 J5.
223
224 COMMANDS
225
226 The driver supports a read streaming acquisition command on the
227 'INTERRUPT' subdevice.  The channel list selects the interrupt sources
228 to be enabled.  All channels will be sampled together (convert_src ==
229 TRIG_NOW).  The scan begins a short time after the hardware interrupt
230 occurs, subject to interrupt latencies (scan_begin_src == TRIG_EXT,
231 scan_begin_arg == 0).  The value read from the interrupt status register
232 is packed into a sampl_t value, one bit per requested channel, in the
233 order they appear in the channel list.
234 */
235
236 #include <linux/comedidev.h>
237
238 #include "comedi_pci.h"
239
240 #include "8255.h"
241 #include "8253.h"
242
243 #define DIO200_DRIVER_NAME      "amplc_dio200"
244
245 /* PCI IDs */
246 /* #define PCI_VENDOR_ID_AMPLICON 0x14dc */
247 #define PCI_DEVICE_ID_AMPLICON_PCI272 0x000a
248 #define PCI_DEVICE_ID_AMPLICON_PCI215 0x000b
249 #define PCI_DEVICE_ID_AMPLICON_PCIE236 0x0011
250 #define PCI_DEVICE_ID_AMPLICON_PCIE215 0x0012
251 #define PCI_DEVICE_ID_AMPLICON_PCIE296 0x0014
252 #define PCI_DEVICE_ID_INVALID 0xffff
253
254 /* 8255 control register bits */
255 #define CR_C_LO_IO      0x01
256 #define CR_B_IO         0x02
257 #define CR_B_MODE       0x04
258 #define CR_C_HI_IO      0x08
259 #define CR_A_IO         0x10
260 #define CR_A_MODE(a)    ((a)<<5)
261 #define CR_CW           0x80
262
263 /* 200 series registers */
264 #define DIO200_IO_SIZE          0x20
265 #define DIO200_PCIE_IO_SIZE     0x4000
266 #define DIO200_XCLK_SCE         0x18    /* Group X clock selection register */
267 #define DIO200_YCLK_SCE         0x19    /* Group Y clock selection register */
268 #define DIO200_ZCLK_SCE         0x1a    /* Group Z clock selection register */
269 #define DIO200_XGAT_SCE         0x1b    /* Group X gate selection register */
270 #define DIO200_YGAT_SCE         0x1c    /* Group Y gate selection register */
271 #define DIO200_ZGAT_SCE         0x1d    /* Group Z gate selection register */
272 #define DIO200_INT_SCE          0x1e    /* Interrupt enable/status register */
273 /* Extra registers for new PCIe boards */
274 #define DIO200_ENHANCE          0x20    /* 1 to enable enhanced features */
275 #define DIO200_VERSION          0x24    /* Hardware version */
276 #define DIO200_TS_CONFIG        0x600   /* Timestamp timer config register */
277 #define DIO200_TS_COUNT         0x602   /* Timestamp timer count register */
278
279 /*
280  * Macros for constructing value for DIO_200_?CLK_SCE and
281  * DIO_200_?GAT_SCE registers:
282  *
283  * 'which' is: 0 for CTR-X1, CTR-Y1, CTR-Z1; 1 for CTR-X2, CTR-Y2 or CTR-Z2.
284  * 'chan' is the channel: 0, 1 or 2.
285  * 'source' is the signal source: 0 to 7, or 0 to 31 for "enhanced" boards.
286  */
287 #define CLK_SCE(which, chan, source) (((which) << 5) | ((chan) << 3) | \
288                 (((source) & 030) << 3) | ((source) & 007))
289 #define GAT_SCE(which, chan, source) (((which) << 5) | ((chan) << 3) | \
290                 (((source) & 030) << 3) | ((source) & 007))
291
292 /*
293  * Timestamp timer configuration register (for new PCIe boards).
294  */
295 #define TS_CONFIG_RESET         0x100   /* Reset counter to zero. */
296 #define TS_CONFIG_CLK_SRC_MASK  0x0FF   /* Clock source. */
297 #define TS_CONFIG_MAX_CLK_SRC   2       /* Maximum clock source value. */
298
299 /*
300  * Periods of the timestamp timer clock sources in nanoseconds.
301  */
302 static const unsigned ts_clock_period[TS_CONFIG_MAX_CLK_SRC + 1] = {
303         1,                      /* 1 nanosecond (but with 20 ns granularity). */
304         1000,                   /* 1 microsecond. */
305         1000000,                /* 1 millisecond. */
306 };
307
308 /*
309  * Periods of the internal counter clock sources in nanoseconds.
310  */
311 static const unsigned clock_period[32] = {
312         0,                      /* dedicated clock input/output pin */
313         100,                    /* 10 MHz */
314         1000,                   /* 1 MHz */
315         10000,                  /* 100 kHz */
316         100000,                 /* 10 kHz */
317         1000000,                /* 1 kHz */
318         0,                      /* OUT N-1 */
319         0,                      /* group clock input pin */
320         0,                      /* HIGH (VCC) (enhanced) */
321         0,                      /* LOW (GND) (enhanced) */
322         0,                      /* pattern present (enhanced) */
323         50,                     /* 20 MHz (enhanced) */
324         /* remaining clock sources reserved (enhanced) */
325 };
326
327 /*
328  * Register region.
329  */
330 enum dio200_regtype { no_regtype, io_regtype, mmio_regtype };
331 struct dio200_region {
332         union {
333                 unsigned long iobase;           /* I/O base address */
334                 unsigned char __iomem *membase; /* Mapped MMIO base address */
335         } u;
336         unsigned char regtype;
337         unsigned char regshift;
338 };
339
340 /*
341  * Board descriptions.
342  */
343
344 enum dio200_bustype { isa_bustype, pci_bustype };
345
346 enum dio200_model {
347         pc212e_model,
348         pc214e_model,
349         pc215e_model, pci215_model, pcie215_model,
350         pc218e_model,
351         pcie236_model,
352         pc272e_model, pci272_model,
353         pcie296_model,
354         anypci_model
355 };
356
357 enum dio200_layout {
358         pc212_layout,
359         pc214_layout,
360         pc215_layout,
361         pc218_layout,
362         pc272_layout,
363 #ifdef CONFIG_COMEDI_PCI
364         pcie215_layout,
365         pcie236_layout,
366         pcie296_layout,
367 #endif
368         num_layouts
369 };
370
371 typedef struct dio200_board_struct {
372         const char *name;
373         unsigned short devid;
374         enum dio200_bustype bustype;
375         enum dio200_model model;
376         enum dio200_layout layout;
377         unsigned char mainbar;
378         unsigned char mainshift;
379         unsigned int mainsize;
380 } dio200_board;
381
382 static const dio200_board dio200_boards[] = {
383         {
384               name:     "pc212e",
385               bustype:  isa_bustype,
386               model:    pc212e_model,
387               layout:   pc212_layout,
388               mainsize: DIO200_IO_SIZE,
389                 },
390         {
391               name:     "pc214e",
392               bustype:  isa_bustype,
393               model:    pc214e_model,
394               layout:   pc214_layout,
395               mainsize: DIO200_IO_SIZE,
396                 },
397         {
398               name:     "pc215e",
399               bustype:  isa_bustype,
400               model:    pc215e_model,
401               layout:   pc215_layout,
402               mainsize: DIO200_IO_SIZE,
403                 },
404 #ifdef CONFIG_COMEDI_PCI
405         {
406               name:     "pci215",
407               devid:    PCI_DEVICE_ID_AMPLICON_PCI215,
408               bustype:  pci_bustype,
409               model:    pci215_model,
410               layout:   pc215_layout,
411               mainsize: DIO200_IO_SIZE,
412               mainbar:  2,
413                 },
414 #endif
415 #ifdef CONFIG_COMEDI_PCI
416         {
417               name:     "pcie215",
418               devid:    PCI_DEVICE_ID_AMPLICON_PCIE215,
419               bustype:  pci_bustype,
420               model:    pcie215_model,
421               layout:   pcie215_layout,
422               mainsize: DIO200_PCIE_IO_SIZE,
423               mainbar:  1,
424               mainshift: 3,
425                 },
426 #endif
427         {
428               name:     "pc218e",
429               bustype:  isa_bustype,
430               model:    pc218e_model,
431               layout:   pc218_layout,
432               mainsize: DIO200_IO_SIZE,
433                 },
434 #ifdef CONFIG_COMEDI_PCI
435         {
436               name:     "pcie236",
437               devid:    PCI_DEVICE_ID_AMPLICON_PCIE236,
438               bustype:  pci_bustype,
439               model:    pcie236_model,
440               layout:   pcie236_layout,
441               mainsize: DIO200_PCIE_IO_SIZE,
442               mainbar:  1,
443               mainshift: 3,
444                 },
445 #endif
446         {
447               name:     "pc272e",
448               bustype:  isa_bustype,
449               model:    pc272e_model,
450               layout:   pc272_layout,
451               mainsize: DIO200_IO_SIZE,
452                 },
453 #ifdef CONFIG_COMEDI_PCI
454         {
455               name:     "pci272",
456               devid:    PCI_DEVICE_ID_AMPLICON_PCI272,
457               bustype:  pci_bustype,
458               model:    pci272_model,
459               layout:   pc272_layout,
460               mainsize: DIO200_IO_SIZE,
461               mainbar:  2,
462                 },
463 #endif
464 #ifdef CONFIG_COMEDI_PCI
465         {
466               name:     "pcie296",
467               devid:    PCI_DEVICE_ID_AMPLICON_PCIE296,
468               bustype:  pci_bustype,
469               model:    pcie296_model,
470               layout:   pcie296_layout,
471               mainsize: DIO200_PCIE_IO_SIZE,
472               mainbar:  1,
473               mainshift: 3,
474                 },
475 #endif
476 #ifdef CONFIG_COMEDI_PCI
477         {
478               name:     DIO200_DRIVER_NAME,
479               devid:    PCI_DEVICE_ID_INVALID,
480               bustype:  pci_bustype,
481               model:    anypci_model,   /* wildcard */
482                 },
483 #endif
484 };
485
486 /*
487  * Layout descriptions - some ISA and PCI board descriptions share the same
488  * layout.
489  */
490
491 enum dio200_sdtype { sd_none, sd_intr, sd_8255, sd_8254, sd_timer };
492
493 #define DIO200_MAX_SUBDEVS      8
494 #define DIO200_MAX_ISNS         6
495
496 typedef struct dio200_layout_struct {
497         unsigned short n_subdevs;       /* number of subdevices */
498         unsigned char sdtype[DIO200_MAX_SUBDEVS];       /* enum dio200_sdtype */
499         unsigned char sdinfo[DIO200_MAX_SUBDEVS];       /* depends on sdtype */
500         char has_int_sce;       /* has interrupt enable/status register */
501         char has_clk_gat_sce;   /* has clock/gate selection registers */
502         char has_enhancements;  /* has enhanced features */
503 } dio200_layout;
504
505 static const dio200_layout dio200_layouts[] = {
506         [pc212_layout] = {
507               n_subdevs:6,
508               sdtype:   {sd_8255, sd_8254, sd_8254, sd_8254,
509                                         sd_8254,
510                                 sd_intr},
511               sdinfo:   {0x00, 0x08, 0x0C, 0x10, 0x14,
512                                 0x3F},
513               has_int_sce:1,
514               has_clk_gat_sce:1,
515               has_enhancements:0,
516                 },
517         [pc214_layout] = {
518               n_subdevs:4,
519               sdtype:   {sd_8255, sd_8255, sd_8254,
520                                 sd_intr},
521               sdinfo:   {0x00, 0x08, 0x10, 0x01},
522               has_int_sce:0,
523               has_clk_gat_sce:0,
524               has_enhancements:0,
525                 },
526         [pc215_layout] = {
527               n_subdevs:5,
528               sdtype:   {sd_8255, sd_8255, sd_8254,
529                                         sd_8254,
530                                 sd_intr},
531               sdinfo:   {0x00, 0x08, 0x10, 0x14, 0x3F},
532               has_int_sce:1,
533               has_clk_gat_sce:1,
534               has_enhancements:0,
535                 },
536         [pc218_layout] = {
537               n_subdevs:7,
538               sdtype:   {sd_8254, sd_8254, sd_8255, sd_8254,
539                                         sd_8254,
540                                 sd_intr},
541               sdinfo:   {0x00, 0x04, 0x08, 0x0C, 0x10,
542                                         0x14,
543                                 0x3F},
544               has_int_sce:1,
545               has_clk_gat_sce:1,
546               has_enhancements:0,
547                 },
548         [pc272_layout] = {
549               n_subdevs:4,
550               sdtype:   {sd_8255, sd_8255, sd_8255,
551                                 sd_intr},
552               sdinfo:   {0x00, 0x08, 0x10, 0x3F},
553               has_int_sce:1,
554               has_clk_gat_sce:0,
555               has_enhancements:0,
556                 },
557 #ifdef CONFIG_COMEDI_PCI
558         [pcie215_layout] = {
559               n_subdevs:8,
560               sdtype:   {sd_8255, sd_none, sd_8255, sd_none, sd_8254, sd_8254,
561                                 sd_timer, sd_intr},
562               sdinfo:   {0x00, 0x00, 0x08, 0x00, 0x10, 0x14, 0x00, 0x3F},
563               has_int_sce:1,
564               has_clk_gat_sce:1,
565               has_enhancements:1,
566                 },
567         [pcie236_layout] = {
568               n_subdevs:8,
569               sdtype:   {sd_8255, sd_none, sd_none, sd_none, sd_8254, sd_8254,
570                                 sd_timer, sd_intr},
571               sdinfo:   {0x00, 0x00, 0x00, 0x00, 0x10, 0x14, 0x00, 0x3F},
572               has_int_sce:1,
573               has_clk_gat_sce:1,
574               has_enhancements:1,
575                 },
576         [pcie296_layout] = {
577               n_subdevs:8,
578               sdtype:   {sd_8255, sd_8255, sd_8255, sd_8255, sd_8254, sd_8254,
579                                 sd_timer, sd_intr},
580               sdinfo:   {0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x00, 0x3F},
581               has_int_sce:1,
582               has_clk_gat_sce:1,
583               has_enhancements:1,
584                 },
585 #endif
586 };
587
588 /*
589  * PCI driver table.
590  */
591
592 #ifdef CONFIG_COMEDI_PCI
593 static DEFINE_PCI_DEVICE_TABLE(dio200_pci_table) = {
594         {PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI215,
595                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
596         {PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI272,
597                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
598         {PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCIE236,
599                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
600         {PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCIE215,
601                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
602         {PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCIE296,
603                 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
604         {0}
605 };
606
607 MODULE_DEVICE_TABLE(pci, dio200_pci_table);
608 #endif /* CONFIG_COMEDI_PCI */
609
610 /*
611  * Useful for shorthand access to the particular board structure
612  */
613 #define thisboard ((const dio200_board *)dev->board_ptr)
614 #define thislayout (&dio200_layouts[((dio200_board *)dev->board_ptr)->layout])
615
616 /* this structure is for data unique to this hardware driver.  If
617    several hardware drivers keep similar information in this structure,
618    feel free to suggest moving the variable to the comedi_device struct.  */
619 typedef struct {
620 #ifdef CONFIG_COMEDI_PCI
621         struct pci_dev *pci_dev;        /* PCI device */
622 #endif
623         struct dio200_region io;        /* Register region */
624         int intr_sd;
625 } dio200_private;
626
627 #define devpriv ((dio200_private *)dev->private)
628
629 typedef struct {
630         unsigned int ofs;               /* Counter base offset */
631         unsigned int clk_sce_ofs;       /* CLK_SCE base offset */
632         unsigned int gat_sce_ofs;       /* GAT_SCE base offset */
633         int which;                      /* Bit 5 of CLK_SCE or GAT_SCE */
634         unsigned int clock_src[3];      /* Current clock sources */
635         unsigned int gate_src[3];       /* Current gate sources */
636         spinlock_t spinlock;
637 } dio200_subdev_8254;
638
639 typedef struct {
640         unsigned int ofs;               /* DIO base offset */
641 } dio200_subdev_8255;
642
643 typedef struct {
644         unsigned int ofs;
645         spinlock_t spinlock;
646         int active;
647         unsigned int valid_isns;
648         unsigned int enabled_isns;
649         unsigned int stopcount;
650         int continuous;
651 } dio200_subdev_intr;
652
653 /*
654  * The comedi_driver structure tells the Comedi core module
655  * which functions to call to configure/deconfigure (attach/detach)
656  * the board, and also about the kernel module that contains
657  * the device code.
658  */
659 static int dio200_attach(comedi_device * dev, comedi_devconfig * it);
660 static int dio200_detach(comedi_device * dev);
661 static comedi_driver driver_amplc_dio200 = {
662       driver_name:DIO200_DRIVER_NAME,
663       module:THIS_MODULE,
664       attach:dio200_attach,
665       detach:dio200_detach,
666       board_name:&dio200_boards[0].name,
667       offset:sizeof(dio200_board),
668       num_names:sizeof(dio200_boards) / sizeof(dio200_board),
669 };
670
671 #ifdef CONFIG_COMEDI_PCI
672 COMEDI_PCI_INITCLEANUP(driver_amplc_dio200, dio200_pci_table);
673 #else
674 COMEDI_INITCLEANUP(driver_amplc_dio200);
675 #endif
676
677 /*
678  * Read 8-bit register.
679  */
680 static unsigned char dio200_read8(comedi_device * dev, unsigned int offset)
681 {
682         offset <<= devpriv->io.regshift;
683         if (devpriv->io.regtype == io_regtype)
684                 return inb(devpriv->io.u.iobase + offset);
685         else
686                 return readb(devpriv->io.u.membase + offset);
687 }
688
689 /*
690  * Write 8-bit register.
691  */
692 static void dio200_write8(comedi_device * dev, unsigned int offset,
693                 unsigned char val)
694 {
695         offset <<= devpriv->io.regshift;
696         if (devpriv->io.regtype == io_regtype)
697                 outb(val, devpriv->io.u.iobase + offset);
698         else
699                 writeb(val, devpriv->io.u.membase + offset);
700 }
701
702 /*
703  * Read 32-bit register.
704  */
705 #ifdef CONFIG_COMEDI_PCI
706 static unsigned int dio200_read32(comedi_device * dev, unsigned int offset)
707 {
708         offset <<= devpriv->io.regshift;
709         if (devpriv->io.regtype == io_regtype)
710                 return inl(devpriv->io.u.iobase + offset);
711         else
712                 return readl(devpriv->io.u.membase + offset);
713 }
714 #endif
715
716 /*
717  * Write 32-bit register.
718  */
719 #ifdef CONFIG_COMEDI_PCI
720 static void dio200_write32(comedi_device * dev, unsigned int offset,
721                 unsigned int val)
722 {
723         offset <<= devpriv->io.regshift;
724         if (devpriv->io.regtype == io_regtype)
725                 outl(val, devpriv->io.u.iobase + offset);
726         else
727                 writel(val, devpriv->io.u.membase + offset);
728 }
729 #endif
730
731 /*
732  * This function looks for a PCI device matching the requested board name,
733  * bus and slot.
734  */
735 #ifdef CONFIG_COMEDI_PCI
736 static int
737 dio200_find_pci(comedi_device * dev, int bus, int slot,
738         struct pci_dev **pci_dev_p)
739 {
740         struct pci_dev *pci_dev = NULL;
741
742         *pci_dev_p = NULL;
743
744         /* Look for matching PCI device. */
745         for (pci_dev = pci_get_device(PCI_VENDOR_ID_AMPLICON, PCI_ANY_ID, NULL);
746                 pci_dev != NULL;
747                 pci_dev = pci_get_device(PCI_VENDOR_ID_AMPLICON,
748                         PCI_ANY_ID, pci_dev)) {
749                 /* If bus/slot specified, check them. */
750                 if (bus || slot) {
751                         if (bus != pci_dev->bus->number
752                                 || slot != PCI_SLOT(pci_dev->devfn))
753                                 continue;
754                 }
755                 if (thisboard->model == anypci_model) {
756                         /* Match any supported model. */
757                         int i;
758
759                         for (i = 0; i < ARRAY_SIZE(dio200_boards); i++) {
760                                 if (dio200_boards[i].bustype != pci_bustype)
761                                         continue;
762                                 if (pci_dev->device == dio200_boards[i].devid) {
763                                         /* Change board_ptr to matched board. */
764                                         dev->board_ptr = &dio200_boards[i];
765                                         break;
766                                 }
767                         }
768                         if (i == ARRAY_SIZE(dio200_boards))
769                                 continue;
770                 } else {
771                         /* Match specific model name. */
772                         if (pci_dev->device != thisboard->devid)
773                                 continue;
774                 }
775
776                 /* Found a match. */
777                 *pci_dev_p = pci_dev;
778                 return 0;
779         }
780         /* No match found. */
781         if (bus || slot) {
782                 printk(KERN_ERR
783                         "comedi%d: error! no %s found at pci %02x:%02x!\n",
784                         dev->minor, thisboard->name, bus, slot);
785         } else {
786                 printk(KERN_ERR "comedi%d: error! no %s found!\n",
787                         dev->minor, thisboard->name);
788         }
789         return -EIO;
790 }
791 #endif
792
793 /*
794  * This function checks and requests an I/O region, reporting an error
795  * if there is a conflict.
796  */
797 static int
798 dio200_request_region(unsigned minor, unsigned long from, unsigned long extent)
799 {
800         if (!from || !request_region(from, extent, DIO200_DRIVER_NAME)) {
801                 printk(KERN_ERR "comedi%d: I/O port conflict (%#lx,%lu)!\n",
802                         minor, from, extent);
803                 return -EIO;
804         }
805         return 0;
806 }
807
808 /*
809  * 'insn_bits' function for an 'INTERRUPT' subdevice.
810  */
811 static int
812 dio200_subdev_intr_insn_bits(comedi_device * dev, comedi_subdevice * s,
813         comedi_insn * insn, lsampl_t * data)
814 {
815         dio200_subdev_intr *subpriv = s->private;
816
817         if (thislayout->has_int_sce) {
818                 /* Just read the interrupt status register.  */
819                 data[1] = dio200_read8(dev, subpriv->ofs) & subpriv->valid_isns;
820         } else {
821                 /* No interrupt status register. */
822                 data[0] = 0;
823         }
824
825         return 2;
826 }
827
828 /*
829  * Called to stop acquisition for an 'INTERRUPT' subdevice.
830  */
831 static void dio200_stop_intr(comedi_device * dev, comedi_subdevice * s)
832 {
833         dio200_subdev_intr *subpriv = s->private;
834
835         subpriv->active = 0;
836         subpriv->enabled_isns = 0;
837         if (thislayout->has_int_sce) {
838                 dio200_write8(dev, subpriv->ofs, 0);
839         }
840 }
841
842 /*
843  * Called to start acquisition for an 'INTERRUPT' subdevice.
844  */
845 static int dio200_start_intr(comedi_device * dev, comedi_subdevice * s)
846 {
847         unsigned int n;
848         unsigned isn_bits;
849         dio200_subdev_intr *subpriv = s->private;
850         comedi_cmd *cmd = &s->async->cmd;
851         int retval = 0;
852
853         if (!subpriv->continuous && subpriv->stopcount == 0) {
854                 /* An empty acquisition! */
855                 s->async->events |= COMEDI_CB_EOA;
856                 subpriv->active = 0;
857                 retval = 1;
858         } else {
859                 /* Determine interrupt sources to enable. */
860                 isn_bits = 0;
861                 if (cmd->chanlist) {
862                         for (n = 0; n < cmd->chanlist_len; n++) {
863                                 isn_bits |= (1U << CR_CHAN(cmd->chanlist[n]));
864                         }
865                 }
866                 isn_bits &= subpriv->valid_isns;
867                 /* Enable interrupt sources. */
868                 subpriv->enabled_isns = isn_bits;
869                 if (thislayout->has_int_sce) {
870                         dio200_write8(dev, subpriv->ofs, isn_bits);
871                 }
872         }
873
874         return retval;
875 }
876
877 /*
878  * Internal trigger function to start acquisition for an 'INTERRUPT' subdevice.
879  */
880 static int
881 dio200_inttrig_start_intr(comedi_device * dev, comedi_subdevice * s,
882         unsigned int trignum)
883 {
884         dio200_subdev_intr *subpriv;
885         unsigned long flags;
886         int event = 0;
887
888         if (trignum != 0)
889                 return -EINVAL;
890
891         subpriv = s->private;
892
893         comedi_spin_lock_irqsave(&subpriv->spinlock, flags);
894         s->async->inttrig = 0;
895         if (subpriv->active) {
896                 event = dio200_start_intr(dev, s);
897         }
898         comedi_spin_unlock_irqrestore(&subpriv->spinlock, flags);
899
900         if (event) {
901                 comedi_event(dev, s);
902         }
903
904         return 1;
905 }
906
907 /*
908  * This is called from the interrupt service routine to handle a read
909  * scan on an 'INTERRUPT' subdevice.
910  */
911 static int dio200_handle_read_intr(comedi_device * dev, comedi_subdevice * s)
912 {
913         dio200_subdev_intr *subpriv = s->private;
914         unsigned triggered;
915         unsigned intstat;
916         unsigned cur_enabled;
917         unsigned int oldevents;
918         unsigned long flags;
919         unsigned int int_sce_ofs;
920
921         int_sce_ofs = subpriv->ofs;
922         triggered = 0;
923
924         comedi_spin_lock_irqsave(&subpriv->spinlock, flags);
925         oldevents = s->async->events;
926         if (thislayout->has_int_sce) {
927                 /*
928                  * Collect interrupt sources that have triggered and disable
929                  * them temporarily.  Loop around until no extra interrupt
930                  * sources have triggered, at which point, the valid part of
931                  * the interrupt status register will read zero, clearing the
932                  * cause of the interrupt.
933                  *
934                  * Mask off interrupt sources already seen to avoid infinite
935                  * loop in case of misconfiguration.
936                  */
937                 cur_enabled = subpriv->enabled_isns;
938                 while ((intstat = (dio200_read8(dev, int_sce_ofs)
939                                         & subpriv->valid_isns & ~triggered))
940                                 != 0) {
941                         triggered |= intstat;
942                         cur_enabled &= ~triggered;
943                         dio200_write8(dev, int_sce_ofs, cur_enabled);
944                 }
945         } else {
946                 /*
947                  * No interrupt status register.  Assume the single interrupt
948                  * source has triggered.
949                  */
950                 triggered = subpriv->enabled_isns;
951         }
952
953         if (triggered) {
954                 /*
955                  * Some interrupt sources have triggered and have been
956                  * temporarily disabled to clear the cause of the interrupt.
957                  *
958                  * Reenable them NOW to minimize the time they are disabled.
959                  */
960                 cur_enabled = subpriv->enabled_isns;
961                 if (thislayout->has_int_sce) {
962                         dio200_write8(dev, int_sce_ofs, cur_enabled);
963                 }
964
965                 if (subpriv->active) {
966                         /*
967                          * The command is still active.
968                          *
969                          * Ignore interrupt sources that the command isn't
970                          * interested in (just in case there's a race
971                          * condition).
972                          */
973                         if (triggered & subpriv->enabled_isns) {
974                                 /* Collect scan data. */
975                                 sampl_t val;
976                                 unsigned int n, ch, len;
977
978                                 val = 0;
979                                 len = s->async->cmd.chanlist_len;
980                                 for (n = 0; n < len; n++) {
981                                         ch = CR_CHAN(s->async->cmd.chanlist[n]);
982                                         if (triggered & (1U << ch)) {
983                                                 val |= (1U << n);
984                                         }
985                                 }
986                                 /* Write the scan to the buffer. */
987                                 if (comedi_buf_put(s->async, val)) {
988                                         s->async->events |= (COMEDI_CB_BLOCK |
989                                                 COMEDI_CB_EOS);
990                                 } else {
991                                         /* Error!  Stop acquisition.  */
992                                         dio200_stop_intr(dev, s);
993                                         s->async->events |= COMEDI_CB_ERROR
994                                                 | COMEDI_CB_OVERFLOW;
995                                         comedi_error(dev, "buffer overflow");
996                                 }
997
998                                 /* Check for end of acquisition. */
999                                 if (!subpriv->continuous) {
1000                                         /* stop_src == TRIG_COUNT */
1001                                         if (subpriv->stopcount > 0) {
1002                                                 subpriv->stopcount--;
1003                                                 if (subpriv->stopcount == 0) {
1004                                                         s->async->events |=
1005                                                                 COMEDI_CB_EOA;
1006                                                         dio200_stop_intr(dev,
1007                                                                 s);
1008                                                 }
1009                                         }
1010                                 }
1011                         }
1012                 }
1013         }
1014         comedi_spin_unlock_irqrestore(&subpriv->spinlock, flags);
1015
1016         if (oldevents != s->async->events) {
1017                 comedi_event(dev, s);
1018         }
1019
1020         return (triggered != 0);
1021 }
1022
1023 /*
1024  * 'cancel' function for an 'INTERRUPT' subdevice.
1025  */
1026 static int dio200_subdev_intr_cancel(comedi_device * dev, comedi_subdevice * s)
1027 {
1028         dio200_subdev_intr *subpriv = s->private;
1029         unsigned long flags;
1030
1031         comedi_spin_lock_irqsave(&subpriv->spinlock, flags);
1032         if (subpriv->active) {
1033                 dio200_stop_intr(dev, s);
1034         }
1035         comedi_spin_unlock_irqrestore(&subpriv->spinlock, flags);
1036
1037         return 0;
1038 }
1039
1040 /*
1041  * 'do_cmdtest' function for an 'INTERRUPT' subdevice.
1042  */
1043 static int
1044 dio200_subdev_intr_cmdtest(comedi_device * dev, comedi_subdevice * s,
1045         comedi_cmd * cmd)
1046 {
1047         int err = 0;
1048         unsigned int tmp;
1049
1050         /* step 1: make sure trigger sources are trivially valid */
1051
1052         tmp = cmd->start_src;
1053         cmd->start_src &= (TRIG_NOW | TRIG_INT);
1054         if (!cmd->start_src || tmp != cmd->start_src)
1055                 err++;
1056
1057         tmp = cmd->scan_begin_src;
1058         cmd->scan_begin_src &= TRIG_EXT;
1059         if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1060                 err++;
1061
1062         tmp = cmd->convert_src;
1063         cmd->convert_src &= TRIG_NOW;
1064         if (!cmd->convert_src || tmp != cmd->convert_src)
1065                 err++;
1066
1067         tmp = cmd->scan_end_src;
1068         cmd->scan_end_src &= TRIG_COUNT;
1069         if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1070                 err++;
1071
1072         tmp = cmd->stop_src;
1073         cmd->stop_src &= (TRIG_COUNT | TRIG_NONE);
1074         if (!cmd->stop_src || tmp != cmd->stop_src)
1075                 err++;
1076
1077         if (err)
1078                 return 1;
1079
1080         /* step 2: make sure trigger sources are unique and mutually compatible */
1081
1082         /* these tests are true if more than one _src bit is set */
1083         if ((cmd->start_src & (cmd->start_src - 1)) != 0)
1084                 err++;
1085         if ((cmd->scan_begin_src & (cmd->scan_begin_src - 1)) != 0)
1086                 err++;
1087         if ((cmd->convert_src & (cmd->convert_src - 1)) != 0)
1088                 err++;
1089         if ((cmd->scan_end_src & (cmd->scan_end_src - 1)) != 0)
1090                 err++;
1091         if ((cmd->stop_src & (cmd->stop_src - 1)) != 0)
1092                 err++;
1093
1094         if (err)
1095                 return 2;
1096
1097         /* step 3: make sure arguments are trivially compatible */
1098
1099         /* cmd->start_src == TRIG_NOW || cmd->start_src == TRIG_INT */
1100         if (cmd->start_arg != 0) {
1101                 cmd->start_arg = 0;
1102                 err++;
1103         }
1104
1105         /* cmd->scan_begin_src == TRIG_EXT */
1106         if (cmd->scan_begin_arg != 0) {
1107                 cmd->scan_begin_arg = 0;
1108                 err++;
1109         }
1110
1111         /* cmd->convert_src == TRIG_NOW */
1112         if (cmd->convert_arg != 0) {
1113                 cmd->convert_arg = 0;
1114                 err++;
1115         }
1116
1117         /* cmd->scan_end_src == TRIG_COUNT */
1118         if (cmd->scan_end_arg != cmd->chanlist_len) {
1119                 cmd->scan_end_arg = cmd->chanlist_len;
1120                 err++;
1121         }
1122
1123         switch (cmd->stop_src) {
1124         case TRIG_COUNT:
1125                 /* any count allowed */
1126                 break;
1127         case TRIG_NONE:
1128                 if (cmd->stop_arg != 0) {
1129                         cmd->stop_arg = 0;
1130                         err++;
1131                 }
1132                 break;
1133         default:
1134                 break;
1135         }
1136
1137         if (err)
1138                 return 3;
1139
1140         /* step 4: fix up any arguments */
1141
1142         /* if (err) return 4; */
1143
1144         return 0;
1145 }
1146
1147 /*
1148  * 'do_cmd' function for an 'INTERRUPT' subdevice.
1149  */
1150 static int dio200_subdev_intr_cmd(comedi_device * dev, comedi_subdevice * s)
1151 {
1152         comedi_cmd *cmd = &s->async->cmd;
1153         dio200_subdev_intr *subpriv = s->private;
1154         unsigned long flags;
1155         int event = 0;
1156
1157         comedi_spin_lock_irqsave(&subpriv->spinlock, flags);
1158         subpriv->active = 1;
1159
1160         /* Set up end of acquisition. */
1161         switch (cmd->stop_src) {
1162         case TRIG_COUNT:
1163                 subpriv->continuous = 0;
1164                 subpriv->stopcount = cmd->stop_arg;
1165                 break;
1166         default:
1167                 /* TRIG_NONE */
1168                 subpriv->continuous = 1;
1169                 subpriv->stopcount = 0;
1170                 break;
1171         }
1172
1173         /* Set up start of acquisition. */
1174         switch (cmd->start_src) {
1175         case TRIG_INT:
1176                 s->async->inttrig = dio200_inttrig_start_intr;
1177                 break;
1178         default:
1179                 /* TRIG_NOW */
1180                 event = dio200_start_intr(dev, s);
1181                 break;
1182         }
1183         comedi_spin_unlock_irqrestore(&subpriv->spinlock, flags);
1184
1185         if (event) {
1186                 comedi_event(dev, s);
1187         }
1188
1189         return 0;
1190 }
1191
1192 /*
1193  * This function initializes an 'INTERRUPT' subdevice.
1194  */
1195 static int
1196 dio200_subdev_intr_init(comedi_device * dev, comedi_subdevice * s,
1197         unsigned int offset, unsigned valid_isns)
1198 {
1199         dio200_subdev_intr *subpriv;
1200
1201         subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
1202         if (!subpriv) {
1203                 printk(KERN_ERR "comedi%d: error! out of memory!\n",
1204                         dev->minor);
1205                 return -ENOMEM;
1206         }
1207         subpriv->ofs = offset;
1208         subpriv->valid_isns = valid_isns;
1209         spin_lock_init(&subpriv->spinlock);
1210
1211         if (thislayout->has_int_sce) {
1212                 /* Disable interrupt sources. */
1213                 dio200_write8(dev, subpriv->ofs, 0);
1214         }
1215
1216         s->private = subpriv;
1217         s->type = COMEDI_SUBD_DI;
1218         s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
1219         if (thislayout->has_int_sce) {
1220                 s->n_chan = DIO200_MAX_ISNS;
1221                 s->len_chanlist = DIO200_MAX_ISNS;
1222         } else {
1223                 /* No interrupt source register.  Support single channel. */
1224                 s->n_chan = 1;
1225                 s->len_chanlist = 1;
1226         }
1227         s->range_table = &range_digital;
1228         s->maxdata = 1;
1229         s->insn_bits = dio200_subdev_intr_insn_bits;
1230         s->do_cmdtest = dio200_subdev_intr_cmdtest;
1231         s->do_cmd = dio200_subdev_intr_cmd;
1232         s->cancel = dio200_subdev_intr_cancel;
1233
1234         return 0;
1235 }
1236
1237 /*
1238  * This function cleans up an 'INTERRUPT' subdevice.
1239  */
1240 static void
1241 dio200_subdev_intr_cleanup(comedi_device * dev, comedi_subdevice * s)
1242 {
1243         dio200_subdev_intr *subpriv = s->private;
1244
1245         if (subpriv) {
1246                 kfree(subpriv);
1247         }
1248 }
1249
1250 /*
1251  * Interrupt service routine.
1252  */
1253 static irqreturn_t dio200_interrupt(int irq, void *d PT_REGS_ARG)
1254 {
1255         comedi_device *dev = d;
1256         int handled;
1257
1258         if (!dev->attached) {
1259                 return IRQ_NONE;
1260         }
1261
1262         if (devpriv->intr_sd >= 0) {
1263                 handled = dio200_handle_read_intr(dev,
1264                         dev->subdevices + devpriv->intr_sd);
1265         } else {
1266                 handled = 0;
1267         }
1268
1269         return IRQ_RETVAL(handled);
1270 }
1271
1272 /*
1273  * Read an '8254' counter subdevice channel.
1274  */
1275 static inline unsigned int
1276 dio200_subdev_8254_read_chan(comedi_device * dev, comedi_subdevice * s,
1277         unsigned int chan)
1278 {
1279         unsigned int i8254_ofs = ((dio200_subdev_8254 *)s->private)->ofs;
1280         unsigned int val;
1281
1282         /* latch counter */
1283         val = chan << 6;
1284         dio200_write8(dev, i8254_ofs + i8254_control_reg, val);
1285
1286         /* read lsb */
1287         val = dio200_read8(dev, i8254_ofs + chan);
1288         /* read msb */
1289         val += dio200_read8(dev, i8254_ofs + chan) << 8;
1290
1291         return val;
1292 }
1293
1294 /*
1295  * Write an '8254' counter subdevice channel.
1296  */
1297 static inline void
1298 dio200_subdev_8254_write_chan(comedi_device * dev, comedi_subdevice * s,
1299         unsigned int chan, unsigned int count)
1300 {
1301         unsigned int i8254_ofs = ((dio200_subdev_8254 *)s->private)->ofs;
1302
1303         /* write lsb */
1304         dio200_write8(dev, i8254_ofs + chan, count & 0xff);
1305         /* write msb */
1306         dio200_write8(dev, i8254_ofs + chan, (count >> 8) & 0xff);
1307 }
1308
1309 /*
1310  * Set mode of an '8254' counter subdevice channel.
1311  */
1312 static inline void
1313 dio200_subdev_8254_set_mode(comedi_device * dev, comedi_subdevice * s,
1314         unsigned int chan, unsigned int mode)
1315 {
1316         unsigned int i8254_ofs = ((dio200_subdev_8254 *)s->private)->ofs;
1317         unsigned int byte;
1318
1319         byte = chan << 6;
1320         byte |= 0x30;           /* load lsb then msb */
1321         byte |= (mode & 0xf);   /* set counter mode and BCD|binary */
1322         dio200_write8(dev, i8254_ofs + i8254_control_reg, byte);
1323 }
1324
1325 /*
1326  * Read status byte of an '8254' counter subdevice channel.
1327  */
1328 static inline unsigned int
1329 dio200_subdev_8254_status(comedi_device * dev, comedi_subdevice * s,
1330         unsigned int chan)
1331 {
1332         unsigned int i8254_ofs = ((dio200_subdev_8254 *)s->private)->ofs;
1333
1334         dio200_write8(dev, i8254_ofs + i8254_control_reg, (0xE0 | 2 << chan));
1335         return dio200_read8(dev, i8254_ofs + chan);
1336 }
1337
1338 /*
1339  * Handle 'insn_read' for an '8254' counter subdevice.
1340  */
1341 static int
1342 dio200_subdev_8254_read(comedi_device * dev, comedi_subdevice * s,
1343         comedi_insn * insn, lsampl_t * data)
1344 {
1345         dio200_subdev_8254 *subpriv = s->private;
1346         int chan = CR_CHAN(insn->chanspec);
1347         unsigned long flags;
1348
1349         if (insn->n == 0)
1350                 return 0;
1351
1352         comedi_spin_lock_irqsave(&subpriv->spinlock, flags);
1353         data[0] = dio200_subdev_8254_read_chan(dev, s, chan);
1354         comedi_spin_unlock_irqrestore(&subpriv->spinlock, flags);
1355
1356         return 1;
1357 }
1358
1359 /*
1360  * Handle 'insn_write' for an '8254' counter subdevice.
1361  */
1362 static int
1363 dio200_subdev_8254_write(comedi_device * dev, comedi_subdevice * s,
1364         comedi_insn * insn, lsampl_t * data)
1365 {
1366         dio200_subdev_8254 *subpriv = s->private;
1367         int chan = CR_CHAN(insn->chanspec);
1368         unsigned long flags;
1369
1370         if (insn->n == 0)
1371                 return 0;
1372
1373         comedi_spin_lock_irqsave(&subpriv->spinlock, flags);
1374         dio200_subdev_8254_write_chan(dev, s, chan, data[0]);
1375         comedi_spin_unlock_irqrestore(&subpriv->spinlock, flags);
1376
1377         return 1;
1378 }
1379
1380 /*
1381  * Set gate source for an '8254' counter subdevice channel.
1382  */
1383 static int
1384 dio200_subdev_8254_set_gate_src(comedi_device * dev, comedi_subdevice *s,
1385         unsigned int counter_number, unsigned int gate_src)
1386 {
1387         dio200_subdev_8254 *subpriv = s->private;
1388         unsigned char byte;
1389
1390         if (!thislayout->has_clk_gat_sce)
1391                 return -1;
1392         if (counter_number > 2)
1393                 return -1;
1394         if (gate_src > (thislayout->has_enhancements ? 31 : 7))
1395                 return -1;
1396
1397         subpriv->gate_src[counter_number] = gate_src;
1398         byte = GAT_SCE(subpriv->which, counter_number, gate_src);
1399         dio200_write8(dev, subpriv->gat_sce_ofs, byte);
1400
1401         return 0;
1402 }
1403
1404 /*
1405  * Get gate source for an '8254' counter subdevice channel.
1406  */
1407 static int
1408 dio200_subdev_8254_get_gate_src(comedi_device * dev, comedi_subdevice *s,
1409         unsigned int counter_number)
1410 {
1411         dio200_subdev_8254 *subpriv = s->private;
1412
1413         if (!thislayout->has_clk_gat_sce)
1414                 return -1;
1415         if (counter_number > 2)
1416                 return -1;
1417
1418         return subpriv->gate_src[counter_number];
1419 }
1420
1421 /*
1422  * Set clock source for an '8254' counter subdevice channel.
1423  */
1424 static int
1425 dio200_subdev_8254_set_clock_src(comedi_device * dev, comedi_subdevice *s,
1426         unsigned int counter_number, unsigned int clock_src)
1427 {
1428         dio200_subdev_8254 *subpriv = s->private;
1429         unsigned char byte;
1430
1431         if (!thislayout->has_clk_gat_sce)
1432                 return -1;
1433         if (counter_number > 2)
1434                 return -1;
1435         if (clock_src > (thislayout->has_enhancements ? 31 : 7))
1436                 return -1;
1437
1438         subpriv->clock_src[counter_number] = clock_src;
1439         byte = CLK_SCE(subpriv->which, counter_number, clock_src);
1440         dio200_write8(dev, subpriv->clk_sce_ofs, byte);
1441
1442         return 0;
1443 }
1444
1445 /*
1446  * Get clock source for an '8254' counter subdevice channel.
1447  */
1448 static int
1449 dio200_subdev_8254_get_clock_src(comedi_device * dev, comedi_subdevice *s,
1450         unsigned int counter_number, lsampl_t * period_ns)
1451 {
1452         dio200_subdev_8254 *subpriv = s->private;
1453         unsigned clock_src;
1454
1455         if (!thislayout->has_clk_gat_sce)
1456                 return -1;
1457         if (counter_number > 2)
1458                 return -1;
1459
1460         clock_src = subpriv->clock_src[counter_number];
1461         *period_ns = clock_period[clock_src];
1462         return clock_src;
1463 }
1464
1465 /*
1466  * Handle 'insn_config' for an '8254' counter subdevice.
1467  */
1468 static int
1469 dio200_subdev_8254_config(comedi_device * dev, comedi_subdevice * s,
1470         comedi_insn * insn, lsampl_t * data)
1471 {
1472         dio200_subdev_8254 *subpriv = s->private;
1473         int ret = 0;
1474         int chan = CR_CHAN(insn->chanspec);
1475         unsigned long flags;
1476
1477         comedi_spin_lock_irqsave(&subpriv->spinlock, flags);
1478         switch (data[0]) {
1479         case INSN_CONFIG_SET_COUNTER_MODE:
1480                 if (data[1] > (I8254_MODE5 | I8254_BINARY))
1481                         ret = -EINVAL;
1482                 else
1483                         dio200_subdev_8254_set_mode(dev, s, chan, data[1]);
1484                 break;
1485         case INSN_CONFIG_8254_READ_STATUS:
1486                 data[1] = dio200_subdev_8254_status(dev, s, chan);
1487                 break;
1488         case INSN_CONFIG_SET_GATE_SRC:
1489                 ret = dio200_subdev_8254_set_gate_src(dev, s, chan, data[2]);
1490                 if (ret < 0)
1491                         ret = -EINVAL;
1492                 break;
1493         case INSN_CONFIG_GET_GATE_SRC:
1494                 ret = dio200_subdev_8254_get_gate_src(dev, s, chan);
1495                 if (ret < 0) {
1496                         ret = -EINVAL;
1497                         break;
1498                 }
1499                 data[2] = ret;
1500                 break;
1501         case INSN_CONFIG_SET_CLOCK_SRC:
1502                 ret = dio200_subdev_8254_set_clock_src(dev, s, chan, data[1]);
1503                 if (ret < 0)
1504                         ret = -EINVAL;
1505                 break;
1506         case INSN_CONFIG_GET_CLOCK_SRC:
1507                 ret = dio200_subdev_8254_get_clock_src(dev, s, chan, &data[2]);
1508                 if (ret < 0) {
1509                         ret = -EINVAL;
1510                         break;
1511                 }
1512                 data[1] = ret;
1513                 break;
1514         default:
1515                 ret = -EINVAL;
1516                 break;
1517         }
1518         comedi_spin_unlock_irqrestore(&subpriv->spinlock, flags);
1519         return ret < 0 ? ret : insn->n;
1520 }
1521
1522 /*
1523  * This function initializes an '8254' counter subdevice.
1524  *
1525  * offset is the offset to the 8254 chip.
1526  */
1527 static int
1528 dio200_subdev_8254_init(comedi_device * dev, comedi_subdevice * s,
1529         unsigned int offset)
1530 {
1531         dio200_subdev_8254 *subpriv;
1532         unsigned int chan;
1533
1534         subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
1535         if (!subpriv) {
1536                 printk(KERN_ERR "comedi%d: error! out of memory!\n",
1537                         dev->minor);
1538                 return -ENOMEM;
1539         }
1540
1541         s->private = subpriv;
1542         s->type = COMEDI_SUBD_COUNTER;
1543         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1544         s->n_chan = 3;
1545         s->maxdata = 0xFFFF;
1546         s->insn_read = dio200_subdev_8254_read;
1547         s->insn_write = dio200_subdev_8254_write;
1548         s->insn_config = dio200_subdev_8254_config;
1549
1550         spin_lock_init(&subpriv->spinlock);
1551         subpriv->ofs = offset;
1552         if (thislayout->has_clk_gat_sce) {
1553                 /* Derive CLK_SCE and GAT_SCE register offsets from
1554                  * 8254 offset. */
1555                 subpriv->clk_sce_ofs =
1556                         DIO200_XCLK_SCE + (offset >> 3);
1557                 subpriv->gat_sce_ofs =
1558                         DIO200_XGAT_SCE + (offset >> 3);
1559                 subpriv->which = (offset >> 2) & 1;
1560         }
1561
1562         /* Initialize channels. */
1563         for (chan = 0; chan < 3; chan++) {
1564                 dio200_subdev_8254_set_mode(dev, s, chan,
1565                         (I8254_MODE0 | I8254_BINARY));
1566                 if (thislayout->has_clk_gat_sce) {
1567                         /* Gate source 0 is VCC (logic 1). */
1568                         dio200_subdev_8254_set_gate_src(dev, s, chan, 0);
1569                         /* Clock source 0 is the dedicated clock input. */
1570                         dio200_subdev_8254_set_clock_src(dev, s, chan, 0);
1571                 }
1572         }
1573
1574         return 0;
1575 }
1576
1577 /*
1578  * This function cleans up an '8254' counter subdevice.
1579  */
1580 static void
1581 dio200_subdev_8254_cleanup(comedi_device * dev, comedi_subdevice * s)
1582 {
1583         dio200_subdev_8254 *subpriv = s->private;
1584
1585         if (subpriv) {
1586                 kfree(subpriv);
1587         }
1588 }
1589
1590 /*
1591  * This function sets I/O directions for an '8255' DIO subdevice.
1592  */
1593 static void
1594 dio200_subdev_8255_set_dir(comedi_device * dev, comedi_subdevice * s)
1595 {
1596         dio200_subdev_8255 *subpriv = s->private;
1597         int config;
1598
1599         config = CR_CW;
1600         /* 1 in io_bits indicates output, 1 in config indicates input */
1601         if (!(s->io_bits & 0x0000ff))
1602                 config |= CR_A_IO;
1603         if (!(s->io_bits & 0x00ff00))
1604                 config |= CR_B_IO;
1605         if (!(s->io_bits & 0x0f0000))
1606                 config |= CR_C_LO_IO;
1607         if (!(s->io_bits & 0xf00000))
1608                 config |= CR_C_HI_IO;
1609
1610         dio200_write8(dev, subpriv->ofs + 3, config);
1611 }
1612
1613 /*
1614  * Handle 'insn_bits' for an '8255' DIO subdevice.
1615  */
1616 static int
1617 dio200_subdev_8255_bits(comedi_device * dev, comedi_subdevice * s,
1618         comedi_insn * insn, lsampl_t * data)
1619 {
1620         unsigned int i8255_ofs = ((dio200_subdev_8255 *)s->private)->ofs;
1621
1622         if (data[0]) {
1623                 s->state &= ~data[0];
1624                 s->state |= (data[0] & data[1]);
1625
1626                 if (data[0] & 0xff) {
1627                         dio200_write8(dev, i8255_ofs, s->state & 0xff);
1628                 }
1629                 if (data[0] & 0xff00) {
1630                         dio200_write8(dev, i8255_ofs + 1,
1631                                 (s->state >> 8) & 0xff);
1632                 }
1633                 if (data[0] & 0xff0000) {
1634                         dio200_write8(dev, i8255_ofs + 2,
1635                                 (s->state >> 16) & 0xff);
1636                 }
1637         }
1638
1639         data[1] = dio200_read8(dev, i8255_ofs);
1640         data[1] |= dio200_read8(dev, i8255_ofs + 1) << 8;
1641         data[1] |= dio200_read8(dev, i8255_ofs + 2) << 16;
1642
1643         return 2;
1644 }
1645
1646 /*
1647  * Handle 'insn_config' for an '8255' DIO subdevice.
1648  */
1649 static int
1650 dio200_subdev_8255_config(comedi_device * dev, comedi_subdevice * s,
1651         comedi_insn * insn, lsampl_t * data)
1652 {
1653         unsigned int mask;
1654         unsigned int bits;
1655
1656         mask = 1 << CR_CHAN(insn->chanspec);
1657         if (mask & 0x0000ff) {
1658                 bits = 0x0000ff;
1659         } else if (mask & 0x00ff00) {
1660                 bits = 0x00ff00;
1661         } else if (mask & 0x0f0000) {
1662                 bits = 0x0f0000;
1663         } else {
1664                 bits = 0xf00000;
1665         }
1666
1667         switch (data[0]) {
1668         case INSN_CONFIG_DIO_INPUT:
1669                 s->io_bits &= ~bits;
1670                 break;
1671         case INSN_CONFIG_DIO_OUTPUT:
1672                 s->io_bits |= bits;
1673                 break;
1674         case INSN_CONFIG_DIO_QUERY:
1675                 data[1] = (s->io_bits & bits) ? COMEDI_OUTPUT : COMEDI_INPUT;
1676                 return insn->n;
1677                 break;
1678         default:
1679                 return -EINVAL;
1680         }
1681
1682         dio200_subdev_8255_set_dir(dev, s);
1683
1684         return 1;
1685 }
1686
1687 /*
1688  * This function initializes an '8255' DIO subdevice.
1689  *
1690  * offset is the offset to the 8255 chip.
1691  */
1692 static int
1693 dio200_subdev_8255_init(comedi_device * dev, comedi_subdevice * s,
1694         unsigned int offset)
1695 {
1696         dio200_subdev_8255 *subpriv;
1697
1698         subpriv = kzalloc(sizeof(*subpriv), GFP_KERNEL);
1699         if (!subpriv) {
1700                 printk(KERN_ERR "comedi%d: error! out of memory!\n",
1701                         dev->minor);
1702                 return -ENOMEM;
1703         }
1704
1705         subpriv->ofs = offset;
1706
1707         s->private = subpriv;
1708         s->type = COMEDI_SUBD_DIO;
1709         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1710         s->n_chan = 24;
1711         s->range_table = &range_digital;
1712         s->maxdata = 1;
1713         s->insn_bits = dio200_subdev_8255_bits;
1714         s->insn_config = dio200_subdev_8255_config;
1715
1716         s->state = 0;
1717         s->io_bits = 0;
1718         dio200_subdev_8255_set_dir(dev, s);
1719
1720         return 0;
1721 }
1722
1723 /*
1724  * This function cleans up an '8255' DIO subdevice.
1725  */
1726 static void
1727 dio200_subdev_8255_cleanup(comedi_device * dev, comedi_subdevice * s)
1728 {
1729         dio200_subdev_8255 *subpriv = s->private;
1730
1731         if (subpriv) {
1732                 kfree(subpriv);
1733         }
1734 }
1735
1736 /*
1737  * Handle 'insn_read' for a timer subdevice.
1738  */
1739 #ifdef CONFIG_COMEDI_PCI
1740 static int
1741 dio200_subdev_timer_read(comedi_device * dev, comedi_subdevice * s,
1742         comedi_insn * insn, lsampl_t * data)
1743 {
1744         int n;
1745
1746         for (n = 0; n < insn->n; n++) {
1747                 data[n] = dio200_read32(dev, DIO200_TS_COUNT);
1748         }
1749         return n;
1750 }
1751 #endif
1752
1753 /*
1754  * Reset timer subdevice.
1755  */
1756 #ifdef CONFIG_COMEDI_PCI
1757 static void
1758 dio200_subdev_timer_reset(comedi_device * dev, comedi_subdevice * s)
1759 {
1760         unsigned int clock;
1761
1762         clock = dio200_read32(dev, DIO200_TS_CONFIG) & TS_CONFIG_CLK_SRC_MASK;
1763         dio200_write32(dev, DIO200_TS_CONFIG, clock | TS_CONFIG_RESET);
1764         dio200_write32(dev, DIO200_TS_CONFIG, clock);
1765 }
1766 #endif
1767
1768 /*
1769  * Get timer subdevice clock source and period.
1770  */
1771 #ifdef CONFIG_COMEDI_PCI
1772 static void
1773 dio200_subdev_timer_get_clock_src(comedi_device * dev, comedi_subdevice * s,
1774                 lsampl_t *src, lsampl_t *period)
1775 {
1776         unsigned int clk;
1777
1778         clk = dio200_read32(dev, DIO200_TS_CONFIG) & TS_CONFIG_CLK_SRC_MASK;
1779         *src = clk;
1780         *period = (clk < ARRAY_SIZE(ts_clock_period))
1781                 ? ts_clock_period[clk] : 0;
1782 }
1783 #endif
1784
1785 /*
1786  * Set timer subdevice clock source.
1787  */
1788 #ifdef CONFIG_COMEDI_PCI
1789 static int
1790 dio200_subdev_timer_set_clock_src(comedi_device * dev, comedi_subdevice * s,
1791                 lsampl_t src)
1792 {
1793         if (src > TS_CONFIG_MAX_CLK_SRC) {
1794                 return -EINVAL;
1795         }
1796         dio200_write32(dev, DIO200_TS_CONFIG, src);
1797         return 0;
1798 }
1799 #endif
1800
1801 /*
1802  * Handle 'insn_config' for a timer subdevice.
1803  */
1804 #ifdef CONFIG_COMEDI_PCI
1805 static int
1806 dio200_subdev_timer_config(comedi_device * dev, comedi_subdevice * s,
1807         comedi_insn * insn, lsampl_t * data)
1808 {
1809         int ret = 0;
1810
1811         switch (data[0]) {
1812         case INSN_CONFIG_RESET:
1813                 dio200_subdev_timer_reset(dev, s);
1814                 break;
1815         case INSN_CONFIG_SET_CLOCK_SRC:
1816                 ret = dio200_subdev_timer_set_clock_src(dev, s, data[1]);
1817                 if (ret < 0)
1818                         ret = -EINVAL;
1819                 break;
1820         case INSN_CONFIG_GET_CLOCK_SRC:
1821                 dio200_subdev_timer_get_clock_src(dev, s, &data[1], &data[2]);
1822                 break;
1823         default:
1824                 ret = -EINVAL;
1825                 break;
1826         }
1827         return ret < 0 ? ret : insn->n;
1828 }
1829 #endif
1830
1831 /*
1832  * This function initializes a timer subdevice.
1833  *
1834  * Uses the timestamp timer registers.  There is only one timestamp timer.
1835  */
1836 #ifdef CONFIG_COMEDI_PCI
1837 static int
1838 dio200_subdev_timer_init(comedi_device * dev, comedi_subdevice * s)
1839 {
1840         s->type = COMEDI_SUBD_TIMER;
1841         s->subdev_flags = SDF_READABLE | SDF_LSAMPL;
1842         s->n_chan = 1;
1843         s->maxdata = 0xFFFFFFFF;
1844         s->insn_read = dio200_subdev_timer_read;
1845         s->insn_config = dio200_subdev_timer_config;
1846         return 0;
1847 }
1848 #endif
1849
1850 /*
1851  * This function cleans up a timer subdevice.
1852  */
1853 #ifdef CONFIG_COMEDI_PCI
1854 static void
1855 dio200_subdev_timer_cleanup(comedi_device * dev, comedi_subdevice * s)
1856 {
1857         /* Nothing to do. */
1858 }
1859 #endif
1860
1861 #ifdef CONFIG_COMEDI_PCI
1862 /*
1863  * This function does some special set-up for the PCIe boards
1864  * PCIe215, PCIe236, PCIe296.
1865  */
1866 static int
1867 dio200_pcie_board_setup(comedi_device * dev)
1868 {
1869         struct pci_dev *pci_dev = devpriv->pci_dev;
1870         unsigned char __iomem *brbase;
1871         resource_size_t brlen;
1872
1873         /*
1874          * The board uses Altera Cyclone IV with PCI-Express hard IP.
1875          * The FPGA configuration has the PCI-Express Avalon-MM Bridge
1876          * Control registers in PCI BAR 0, offset 0, and the length of
1877          * these registers is 0x4000.
1878          *
1879          * We need to write 0x80 to the "Avalon-MM to PCI-Express Interrupt
1880          * Enable" register at offset 0x50 to allow generation of PCIe
1881          * interrupts when RXmlrq_i is asserted in the SOPC Builder system.
1882          */
1883         brlen = pci_resource_len(pci_dev, 0);
1884         if (brlen < 0x4000 ||
1885                         !(pci_resource_flags(pci_dev, 0) & IORESOURCE_MEM)) {
1886                 printk(KERN_ERR "comedi%d: error! bad PCI region!\n",
1887                         dev->minor);
1888                 return -EINVAL;
1889         }
1890         brbase = ioremap_nocache(pci_resource_start(pci_dev, 0), brlen);
1891         if (!brbase) {
1892                 printk(KERN_ERR "comedi%d: error! failed to map registers!\n",
1893                         dev->minor);
1894                 return -ENOMEM;
1895         }
1896         writel(0x80, brbase + 0x50);
1897         iounmap(brbase);
1898         /*
1899          * Enable "enhanced" features of board.
1900          */
1901         dio200_write8(dev, DIO200_ENHANCE, 1);
1902         return 0;
1903 }
1904 #endif
1905
1906 /*
1907  * Attach is called by the Comedi core to configure the driver
1908  * for a particular board.  If you specified a board_name array
1909  * in the driver structure, dev->board_ptr contains that
1910  * address.
1911  */
1912 static int dio200_attach(comedi_device * dev, comedi_devconfig * it)
1913 {
1914         comedi_subdevice *s;
1915         unsigned long iobase = 0;
1916         unsigned int irq = 0;
1917 #ifdef CONFIG_COMEDI_PCI
1918         struct pci_dev *pci_dev = NULL;
1919         int bus = 0, slot = 0;
1920 #endif
1921         const dio200_layout *layout;
1922         int share_irq = 0;
1923         int sdx;
1924         unsigned n;
1925         int ret;
1926
1927         printk(KERN_DEBUG "comedi%d: %s: attach\n", dev->minor,
1928                 DIO200_DRIVER_NAME);
1929
1930         if ((ret = alloc_private(dev, sizeof(dio200_private))) < 0) {
1931                 printk(KERN_ERR "comedi%d: error! out of memory!\n",
1932                         dev->minor);
1933                 return ret;
1934         }
1935
1936         /* Process options. */
1937         switch (thisboard->bustype) {
1938         case isa_bustype:
1939                 iobase = it->options[0];
1940                 irq = it->options[1];
1941                 share_irq = 0;
1942                 break;
1943 #ifdef CONFIG_COMEDI_PCI
1944         case pci_bustype:
1945                 bus = it->options[0];
1946                 slot = it->options[1];
1947                 share_irq = 1;
1948
1949                 if ((ret = dio200_find_pci(dev, bus, slot, &pci_dev)) < 0)
1950                         return ret;
1951                 devpriv->pci_dev = pci_dev;
1952                 break;
1953 #endif
1954         default:
1955                 printk(KERN_ERR
1956                         "comedi%d: %s: BUG! cannot determine board type!\n",
1957                         dev->minor, DIO200_DRIVER_NAME);
1958                 return -EINVAL;
1959                 break;
1960         }
1961
1962         devpriv->intr_sd = -1;
1963         devpriv->io.regtype = no_regtype;
1964         devpriv->io.regshift = thisboard->mainshift;
1965
1966         /* Enable device and reserve I/O spaces. */
1967 #ifdef CONFIG_COMEDI_PCI
1968         if (pci_dev) {
1969                 resource_size_t base, len;
1970                 unsigned int bar;
1971
1972                 ret = comedi_pci_enable(pci_dev, DIO200_DRIVER_NAME);
1973                 if (ret < 0) {
1974                         printk(KERN_ERR
1975                                 "comedi%d: error! cannot enable PCI device and request regions!\n",
1976                                 dev->minor);
1977                         return ret;
1978                 }
1979                 bar = thisboard->mainbar;
1980                 base = pci_resource_start(pci_dev, bar);
1981                 len = pci_resource_len(pci_dev, bar);
1982                 if (len < thisboard->mainsize) {
1983                         printk(KERN_ERR
1984                                 "comedi%d: error! PCI region size too small!\n",
1985                                 dev->minor);
1986                         return -EINVAL;
1987                 }
1988                 if ((pci_resource_flags(pci_dev, bar) & IORESOURCE_MEM) != 0) {
1989                         devpriv->io.u.membase = ioremap_nocache(base, len);
1990                         if (!devpriv->io.u.membase) {
1991                                 printk(KERN_ERR
1992                                         "comedi%d: error! cannot remap registers!\n",
1993                                         dev->minor);
1994                                 return -ENOMEM;
1995                         }
1996                         devpriv->io.regtype = mmio_regtype;
1997                 } else {
1998                         devpriv->io.u.iobase = (unsigned long)base;
1999                         devpriv->io.regtype = io_regtype;
2000                 }
2001                 irq = pci_dev->irq;
2002         } else
2003 #endif
2004         {
2005                 ret = dio200_request_region(dev->minor, iobase,
2006                         thisboard->mainsize);
2007                 if (ret < 0) {
2008                         return ret;
2009                 }
2010                 devpriv->io.u.iobase = iobase;
2011                 devpriv->io.regtype = io_regtype;
2012         }
2013
2014         switch (thisboard->model) {
2015 #ifdef CONFIG_COMEDI_PCI
2016         case pcie215_model:
2017         case pcie236_model:
2018         case pcie296_model:
2019                 ret = dio200_pcie_board_setup(dev);
2020                 if (ret < 0) {
2021                         return ret;
2022                 }
2023                 break;
2024 #endif
2025         default:
2026                 break;
2027         }
2028
2029         layout = thislayout;
2030         if ((ret = alloc_subdevices(dev, layout->n_subdevs)) < 0) {
2031                 printk(KERN_ERR "comedi%d: error! out of memory!\n",
2032                         dev->minor);
2033                 return ret;
2034         }
2035
2036         for (n = 0; n < dev->n_subdevices; n++) {
2037                 s = &dev->subdevices[n];
2038                 switch (layout->sdtype[n]) {
2039                 case sd_8254:
2040                         /* counter subdevice (8254) */
2041                         ret = dio200_subdev_8254_init(dev, s,
2042                                 layout->sdinfo[n]);
2043                         if (ret < 0) {
2044                                 return ret;
2045                         }
2046                         break;
2047                 case sd_8255:
2048                         /* digital i/o subdevice (8255) */
2049                         ret = dio200_subdev_8255_init(dev, s,
2050                                 layout->sdinfo[n]);
2051                         if (ret < 0) {
2052                                 return ret;
2053                         }
2054                         break;
2055                 case sd_intr:
2056                         /* 'INTERRUPT' subdevice */
2057                         if (irq) {
2058                                 ret = dio200_subdev_intr_init(dev, s,
2059                                         DIO200_INT_SCE, layout->sdinfo[n]);
2060                                 if (ret < 0) {
2061                                         return ret;
2062                                 }
2063                                 devpriv->intr_sd = n;
2064                         } else {
2065                                 s->type = COMEDI_SUBD_UNUSED;
2066                         }
2067                         break;
2068                 case sd_timer:
2069                         /* timer subdevice */
2070 #ifdef CONFIG_COMEDI_PCI
2071                         ret = dio200_subdev_timer_init(dev, s);
2072                         if (ret < 0) {
2073                                 return ret;
2074                         }
2075 #else
2076                         s->type = COMEDI_SUBD_UNUSED;
2077 #endif
2078                         break;
2079                 default:
2080                         s->type = COMEDI_SUBD_UNUSED;
2081                         break;
2082                 }
2083         }
2084
2085         sdx = devpriv->intr_sd;
2086         if (sdx >= 0 && sdx < dev->n_subdevices) {
2087                 dev->read_subdev = &dev->subdevices[sdx];
2088         }
2089
2090         dev->board_name = thisboard->name;
2091
2092         if (irq) {
2093                 unsigned long flags = share_irq ? IRQF_SHARED : 0;
2094
2095                 if (comedi_request_irq(irq, dio200_interrupt, flags,
2096                                 DIO200_DRIVER_NAME, dev) >= 0) {
2097                         dev->irq = irq;
2098                 } else {
2099                         printk(KERN_WARNING
2100                                 "comedi%d: warning! irq %u unavailable!\n",
2101                                 dev->minor, irq);
2102                 }
2103         }
2104
2105         printk(KERN_INFO "comedi%d: %s ", dev->minor, dev->board_name);
2106         if (thisboard->bustype == isa_bustype) {
2107                 printk("(base %#lx) ", iobase);
2108         } else {
2109 #ifdef CONFIG_COMEDI_PCI
2110                 printk("(pci %s) ", pci_name(pci_dev));
2111 #endif
2112         }
2113         if (irq) {
2114                 printk("(irq %u%s) ", irq, (dev->irq ? "" : " UNAVAILABLE"));
2115         } else {
2116                 printk("(no irq) ");
2117         }
2118
2119         printk("attached\n");
2120
2121         return 1;
2122 }
2123
2124 /*
2125  * _detach is called to deconfigure a device.  It should deallocate
2126  * resources.
2127  * This function is also called when _attach() fails, so it should be
2128  * careful not to release resources that were not necessarily
2129  * allocated by _attach().  dev->private and dev->subdevices are
2130  * deallocated automatically by the core.
2131  */
2132 static int dio200_detach(comedi_device * dev)
2133 {
2134         const dio200_layout *layout;
2135         unsigned n;
2136
2137         printk(KERN_DEBUG "comedi%d: %s: detach\n", dev->minor,
2138                 DIO200_DRIVER_NAME);
2139
2140         if (dev->irq) {
2141                 comedi_free_irq(dev->irq, dev);
2142         }
2143         if (dev->subdevices) {
2144                 layout = thislayout;
2145                 for (n = 0; n < dev->n_subdevices; n++) {
2146                         comedi_subdevice *s = &dev->subdevices[n];
2147                         switch (layout->sdtype[n]) {
2148                         case sd_8254:
2149                                 dio200_subdev_8254_cleanup(dev, s);
2150                                 break;
2151                         case sd_8255:
2152                                 dio200_subdev_8255_cleanup(dev, s);
2153                                 break;
2154                         case sd_intr:
2155                                 dio200_subdev_intr_cleanup(dev, s);
2156                                 break;
2157                         case sd_timer:
2158 #ifdef CONFIG_COMEDI_PCI
2159                                 dio200_subdev_timer_cleanup(dev, s);
2160 #endif
2161                                 break;
2162                         default:
2163                                 break;
2164                         }
2165                 }
2166         }
2167         if (devpriv) {
2168                 if (devpriv->io.regtype == mmio_regtype) {
2169                         iounmap(devpriv->io.u.membase);
2170                 }
2171 #ifdef CONFIG_COMEDI_PCI
2172                 if (devpriv->pci_dev) {
2173                         if (devpriv->io.regtype != no_regtype) {
2174                                 comedi_pci_disable(devpriv->pci_dev);
2175                         }
2176                         pci_dev_put(devpriv->pci_dev);
2177                 } else
2178 #endif
2179                 {
2180                         if (devpriv->io.regtype == io_regtype) {
2181                                 release_region(devpriv->io.u.iobase,
2182                                         thisboard->mainsize);
2183                         }
2184                 }
2185         }
2186         if (dev->board_name) {
2187                 printk(KERN_INFO "comedi%d: %s removed\n",
2188                         dev->minor, dev->board_name);
2189         }
2190
2191         return 0;
2192 }