Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/staging/comedi/drivers/adv_pci_dio.c

  1 /*
  2  * comedi/drivers/adv_pci_dio.c
  3  *
  4  * Author: Michal Dobes <dobes@tesnet.cz>
  5  *
  6  *  Hardware driver for Advantech PCI DIO cards.
  7 */
  8 /*
  9 Driver: adv_pci_dio
 10 Description: Advantech PCI-1730, PCI-1733, PCI-1734, PCI-1735U,
 11         PCI-1736UP, PCI-1739U, PCI-1750, PCI-1751, PCI-1752,
 12         PCI-1753/E, PCI-1754, PCI-1756, PCI-1760, PCI-1762
 13 Author: Michal Dobes <dobes@tesnet.cz>
 14 Devices: [Advantech] PCI-1730 (adv_pci_dio), PCI-1733,
 15   PCI-1734, PCI-1735U, PCI-1736UP, PCI-1739U, PCI-1750,
 16   PCI-1751, PCI-1752, PCI-1753,
 17   PCI-1753+PCI-1753E, PCI-1754, PCI-1756,
 18   PCI-1760, PCI-1762
 19 Status: untested
 20 Updated: Mon, 09 Jan 2012 12:40:46 +0000
 21 
 22 This driver supports now only insn interface for DI/DO/DIO.
 23 
 24 Configuration options:
 25   [0] - PCI bus of device (optional)
 26   [1] - PCI slot of device (optional)
 27         If bus/slot is not specified, the first available PCI
 28         device will be used.
 29 
 30 */
 31 
 32 #include <linux/module.h>
 33 #include <linux/pci.h>
 34 #include <linux/delay.h>
 35 
 36 #include "../comedidev.h"
 37 
 38 #include "8255.h"
 39 #include "8253.h"
 40 
 41 /* hardware types of the cards */
 42 enum hw_cards_id {
 43         TYPE_PCI1730, TYPE_PCI1733, TYPE_PCI1734, TYPE_PCI1735, TYPE_PCI1736,
 44         TYPE_PCI1739,
 45         TYPE_PCI1750,
 46         TYPE_PCI1751,
 47         TYPE_PCI1752,
 48         TYPE_PCI1753, TYPE_PCI1753E,
 49         TYPE_PCI1754, TYPE_PCI1756,
 50         TYPE_PCI1760,
 51         TYPE_PCI1762
 52 };
 53 
 54 /* which I/O instructions to use */
 55 enum hw_io_access {
 56         IO_8b, IO_16b
 57 };
 58 
 59 #define MAX_DI_SUBDEVS  2       /* max number of DI subdevices per card */
 60 #define MAX_DO_SUBDEVS  2       /* max number of DO subdevices per card */
 61 #define MAX_DIO_SUBDEVG 2       /* max number of DIO subdevices group per
 62                                  * card */
 63 #define MAX_8254_SUBDEVS   1    /* max number of 8254 counter subdevs per
 64                                  * card */
 65                                 /* (could be more than one 8254 per
 66                                  * subdevice) */
 67 
 68 #define SIZE_8254          4    /* 8254 IO space length */
 69 #define SIZE_8255          4    /* 8255 IO space length */
 70 
 71 #define PCIDIO_MAINREG     2    /* main I/O region for all Advantech cards? */
 72 
 73 /* Register offset definitions */
 74 /*  Advantech PCI-1730/3/4 */
 75 #define PCI1730_IDI        0    /* R:   Isolated digital input  0-15 */
 76 #define PCI1730_IDO        0    /* W:   Isolated digital output 0-15 */
 77 #define PCI1730_DI         2    /* R:   Digital input  0-15 */
 78 #define PCI1730_DO         2    /* W:   Digital output 0-15 */
 79 #define PCI1733_IDI        0    /* R:   Isolated digital input  0-31 */
 80 #define PCI1730_3_INT_EN        0x08    /* R/W: enable/disable interrupts */
 81 #define PCI1730_3_INT_RF        0x0c    /* R/W: set falling/raising edge for
 82                                          * interrupts */
 83 #define PCI1730_3_INT_CLR       0x10    /* R/W: clear interrupts */
 84 #define PCI1734_IDO        0    /* W:   Isolated digital output 0-31 */
 85 #define PCI173x_BOARDID    4    /* R:   Board I/D switch for 1730/3/4 */
 86 
 87 /* Advantech PCI-1735U */
 88 #define PCI1735_DI         0    /* R:   Digital input  0-31 */
 89 #define PCI1735_DO         0    /* W:   Digital output 0-31 */
 90 #define PCI1735_C8254      4    /* R/W: 8254 counter */
 91 #define PCI1735_BOARDID    8    /* R:   Board I/D switch for 1735U */
 92 
 93 /*  Advantech PCI-1736UP */
 94 #define PCI1736_IDI        0    /* R:   Isolated digital input  0-15 */
 95 #define PCI1736_IDO        0    /* W:   Isolated digital output 0-15 */
 96 #define PCI1736_3_INT_EN        0x08    /* R/W: enable/disable interrupts */
 97 #define PCI1736_3_INT_RF        0x0c    /* R/W: set falling/raising edge for
 98                                          * interrupts */
 99 #define PCI1736_3_INT_CLR       0x10    /* R/W: clear interrupts */
100 #define PCI1736_BOARDID    4    /* R:   Board I/D switch for 1736UP */
101 #define PCI1736_MAINREG    0    /* Normal register (2) doesn't work */
102 
103 /* Advantech PCI-1739U */
104 #define PCI1739_DIO        0    /* R/W: begin of 8255 registers block */
105 #define PCI1739_ICR       32    /* W:   Interrupt control register */
106 #define PCI1739_ISR       32    /* R:   Interrupt status register */
107 #define PCI1739_BOARDID    8    /* R:   Board I/D switch for 1739U */
108 
109 /*  Advantech PCI-1750 */
110 #define PCI1750_IDI        0    /* R:   Isolated digital input  0-15 */
111 #define PCI1750_IDO        0    /* W:   Isolated digital output 0-15 */
112 #define PCI1750_ICR       32    /* W:   Interrupt control register */
113 #define PCI1750_ISR       32    /* R:   Interrupt status register */
114 
115 /*  Advantech PCI-1751/3/3E */
116 #define PCI1751_DIO        0    /* R/W: begin of 8255 registers block */
117 #define PCI1751_CNT       24    /* R/W: begin of 8254 registers block */
118 #define PCI1751_ICR       32    /* W:   Interrupt control register */
119 #define PCI1751_ISR       32    /* R:   Interrupt status register */
120 #define PCI1753_DIO        0    /* R/W: begin of 8255 registers block */
121 #define PCI1753_ICR0      16    /* R/W: Interrupt control register group 0 */
122 #define PCI1753_ICR1      17    /* R/W: Interrupt control register group 1 */
123 #define PCI1753_ICR2      18    /* R/W: Interrupt control register group 2 */
124 #define PCI1753_ICR3      19    /* R/W: Interrupt control register group 3 */
125 #define PCI1753E_DIO      32    /* R/W: begin of 8255 registers block */
126 #define PCI1753E_ICR0     48    /* R/W: Interrupt control register group 0 */
127 #define PCI1753E_ICR1     49    /* R/W: Interrupt control register group 1 */
128 #define PCI1753E_ICR2     50    /* R/W: Interrupt control register group 2 */
129 #define PCI1753E_ICR3     51    /* R/W: Interrupt control register group 3 */
130 
131 /*  Advantech PCI-1752/4/6 */
132 #define PCI1752_IDO        0    /* R/W: Digital output  0-31 */
133 #define PCI1752_IDO2       4    /* R/W: Digital output 32-63 */
134 #define PCI1754_IDI        0    /* R:   Digital input   0-31 */
135 #define PCI1754_IDI2       4    /* R:   Digital input  32-64 */
136 #define PCI1756_IDI        0    /* R:   Digital input   0-31 */
137 #define PCI1756_IDO        4    /* R/W: Digital output  0-31 */
138 #define PCI1754_6_ICR0  0x08    /* R/W: Interrupt control register group 0 */
139 #define PCI1754_6_ICR1  0x0a    /* R/W: Interrupt control register group 1 */
140 #define PCI1754_ICR2    0x0c    /* R/W: Interrupt control register group 2 */
141 #define PCI1754_ICR3    0x0e    /* R/W: Interrupt control register group 3 */
142 #define PCI1752_6_CFC   0x12    /* R/W: set/read channel freeze function */
143 #define PCI175x_BOARDID 0x10    /* R:   Board I/D switch for 1752/4/6 */
144 
145 /*  Advantech PCI-1762 registers */
146 #define PCI1762_RO         0    /* R/W: Relays status/output */
147 #define PCI1762_IDI        2    /* R:   Isolated input status */
148 #define PCI1762_BOARDID    4    /* R:   Board I/D switch */
149 #define PCI1762_ICR        6    /* W:   Interrupt control register */
150 #define PCI1762_ISR        6    /* R:   Interrupt status register */
151 
152 /*  Advantech PCI-1760 registers */
153 #define OMB0            0x0c    /* W:   Mailbox outgoing registers */
154 #define OMB1            0x0d
155 #define OMB2            0x0e
156 #define OMB3            0x0f
157 #define IMB0            0x1c    /* R:   Mailbox incoming registers */
158 #define IMB1            0x1d
159 #define IMB2            0x1e
160 #define IMB3            0x1f
161 #define INTCSR0         0x38    /* R/W: Interrupt control registers */
162 #define INTCSR1         0x39
163 #define INTCSR2         0x3a
164 #define INTCSR3         0x3b
165 
166 /*  PCI-1760 mailbox commands */
167 #define CMD_ClearIMB2           0x00    /* Clear IMB2 status and return actual
168                                          * DI status in IMB3 */
169 #define CMD_SetRelaysOutput     0x01    /* Set relay output from OMB0 */
170 #define CMD_GetRelaysStatus     0x02    /* Get relay status to IMB0 */
171 #define CMD_ReadCurrentStatus   0x07    /* Read the current status of the
172                                          * register in OMB0, result in IMB0 */
173 #define CMD_ReadFirmwareVersion 0x0e    /* Read the firmware ver., result in
174                                          * IMB1.IMB0 */
175 #define CMD_ReadHardwareVersion 0x0f    /* Read the hardware ver., result in
176                                          * IMB1.IMB0 */
177 #define CMD_EnableIDIFilters    0x20    /* Enable IDI filters based on bits in
178                                          * OMB0 */
179 #define CMD_EnableIDIPatternMatch 0x21  /* Enable IDI pattern match based on
180                                          * bits in OMB0 */
181 #define CMD_SetIDIPatternMatch  0x22    /* Enable IDI pattern match based on
182                                          * bits in OMB0 */
183 #define CMD_EnableIDICounters   0x28    /* Enable IDI counters based on bits in
184                                          * OMB0 */
185 #define CMD_ResetIDICounters    0x29    /* Reset IDI counters based on bits in
186                                          * OMB0 to its reset values */
187 #define CMD_OverflowIDICounters 0x2a    /* Enable IDI counters overflow
188                                          * interrupts  based on bits in OMB0 */
189 #define CMD_MatchIntIDICounters 0x2b    /* Enable IDI counters match value
190                                          * interrupts  based on bits in OMB0 */
191 #define CMD_EdgeIDICounters     0x2c    /* Set IDI up counters count edge (bit=0
192                                          * - rising, =1 - falling) */
193 #define CMD_GetIDICntCurValue   0x2f    /* Read IDI{OMB0} up counter current
194                                          * value */
195 #define CMD_SetIDI0CntResetValue 0x40   /* Set IDI0 Counter Reset Value
196                                          * 256*OMB1+OMB0 */
197 #define CMD_SetIDI1CntResetValue 0x41   /* Set IDI1 Counter Reset Value
198                                          * 256*OMB1+OMB0 */
199 #define CMD_SetIDI2CntResetValue 0x42   /* Set IDI2 Counter Reset Value
200                                          * 256*OMB1+OMB0 */
201 #define CMD_SetIDI3CntResetValue 0x43   /* Set IDI3 Counter Reset Value
202                                          * 256*OMB1+OMB0 */
203 #define CMD_SetIDI4CntResetValue 0x44   /* Set IDI4 Counter Reset Value
204                                          * 256*OMB1+OMB0 */
205 #define CMD_SetIDI5CntResetValue 0x45   /* Set IDI5 Counter Reset Value
206                                          * 256*OMB1+OMB0 */
207 #define CMD_SetIDI6CntResetValue 0x46   /* Set IDI6 Counter Reset Value
208                                          * 256*OMB1+OMB0 */
209 #define CMD_SetIDI7CntResetValue 0x47   /* Set IDI7 Counter Reset Value
210                                          * 256*OMB1+OMB0 */
211 #define CMD_SetIDI0CntMatchValue 0x48   /* Set IDI0 Counter Match Value
212                                          * 256*OMB1+OMB0 */
213 #define CMD_SetIDI1CntMatchValue 0x49   /* Set IDI1 Counter Match Value
214                                          * 256*OMB1+OMB0 */
215 #define CMD_SetIDI2CntMatchValue 0x4a   /* Set IDI2 Counter Match Value
216                                          * 256*OMB1+OMB0 */
217 #define CMD_SetIDI3CntMatchValue 0x4b   /* Set IDI3 Counter Match Value
218                                          * 256*OMB1+OMB0 */
219 #define CMD_SetIDI4CntMatchValue 0x4c   /* Set IDI4 Counter Match Value
220                                          * 256*OMB1+OMB0 */
221 #define CMD_SetIDI5CntMatchValue 0x4d   /* Set IDI5 Counter Match Value
222                                          * 256*OMB1+OMB0 */
223 #define CMD_SetIDI6CntMatchValue 0x4e   /* Set IDI6 Counter Match Value
224                                          * 256*OMB1+OMB0 */
225 #define CMD_SetIDI7CntMatchValue 0x4f   /* Set IDI7 Counter Match Value
226                                          * 256*OMB1+OMB0 */
227 
228 #define OMBCMD_RETRY    0x03    /* 3 times try request before error */
229 
230 struct diosubd_data {
231         int chans;              /*  num of chans */
232         int addr;               /*  PCI address ofset */
233         int regs;               /*  number of registers to read or 8255
234                                     subdevices or 8254 chips */
235         unsigned int specflags; /*  addon subdevice flags */
236 };
237 
238 struct dio_boardtype {
239         const char *name;       /*  board name */
240         int main_pci_region;    /*  main I/O PCI region */
241         enum hw_cards_id cardtype;
242         int nsubdevs;
243         struct diosubd_data sdi[MAX_DI_SUBDEVS];        /*  DI chans */
244         struct diosubd_data sdo[MAX_DO_SUBDEVS];        /*  DO chans */
245         struct diosubd_data sdio[MAX_DIO_SUBDEVG];      /*  DIO 8255 chans */
246         struct diosubd_data boardid;    /*  card supports board ID switch */
247         struct diosubd_data s8254[MAX_8254_SUBDEVS];    /* 8254 subdevices */
248         enum hw_io_access io_access;
249 };
250 
251 static const struct dio_boardtype boardtypes[] = {
252         [TYPE_PCI1730] = {
253                 .name           = "pci1730",
254                 .main_pci_region = PCIDIO_MAINREG,
255                 .cardtype       = TYPE_PCI1730,
256                 .nsubdevs       = 5,
257                 .sdi[0]         = { 16, PCI1730_DI, 2, 0, },
258                 .sdi[1]         = { 16, PCI1730_IDI, 2, 0, },
259                 .sdo[0]         = { 16, PCI1730_DO, 2, 0, },
260                 .sdo[1]         = { 16, PCI1730_IDO, 2, 0, },
261                 .boardid        = { 4, PCI173x_BOARDID, 1, SDF_INTERNAL, },
262                 .io_access      = IO_8b,
263         },
264         [TYPE_PCI1733] = {
265                 .name           = "pci1733",
266                 .main_pci_region = PCIDIO_MAINREG,
267                 .cardtype       = TYPE_PCI1733,
268                 .nsubdevs       = 2,
269                 .sdi[1]         = { 32, PCI1733_IDI, 4, 0, },
270                 .boardid        = { 4, PCI173x_BOARDID, 1, SDF_INTERNAL, },
271                 .io_access      = IO_8b,
272         },
273         [TYPE_PCI1734] = {
274                 .name           = "pci1734",
275                 .main_pci_region = PCIDIO_MAINREG,
276                 .cardtype       = TYPE_PCI1734,
277                 .nsubdevs       = 2,
278                 .sdo[1]         = { 32, PCI1734_IDO, 4, 0, },
279                 .boardid        = { 4, PCI173x_BOARDID, 1, SDF_INTERNAL, },
280                 .io_access      = IO_8b,
281         },
282         [TYPE_PCI1735] = {
283                 .name           = "pci1735",
284                 .main_pci_region = PCIDIO_MAINREG,
285                 .cardtype       = TYPE_PCI1735,
286                 .nsubdevs       = 4,
287                 .sdi[0]         = { 32, PCI1735_DI, 4, 0, },
288                 .sdo[0]         = { 32, PCI1735_DO, 4, 0, },
289                 .boardid        = { 4, PCI1735_BOARDID, 1, SDF_INTERNAL, },
290                 .s8254[0]       = { 3, PCI1735_C8254, 1, 0, },
291                 .io_access      = IO_8b,
292         },
293         [TYPE_PCI1736] = {
294                 .name           = "pci1736",
295                 .main_pci_region = PCI1736_MAINREG,
296                 .cardtype       = TYPE_PCI1736,
297                 .nsubdevs       = 3,
298                 .sdi[1]         = { 16, PCI1736_IDI, 2, 0, },
299                 .sdo[1]         = { 16, PCI1736_IDO, 2, 0, },
300                 .boardid        = { 4, PCI1736_BOARDID, 1, SDF_INTERNAL, },
301                 .io_access      = IO_8b,
302         },
303         [TYPE_PCI1739] = {
304                 .name           = "pci1739",
305                 .main_pci_region = PCIDIO_MAINREG,
306                 .cardtype       = TYPE_PCI1739,
307                 .nsubdevs       = 2,
308                 .sdio[0]        = { 48, PCI1739_DIO, 2, 0, },
309                 .io_access      = IO_8b,
310         },
311         [TYPE_PCI1750] = {
312                 .name           = "pci1750",
313                 .main_pci_region = PCIDIO_MAINREG,
314                 .cardtype       = TYPE_PCI1750,
315                 .nsubdevs       = 2,
316                 .sdi[1]         = { 16, PCI1750_IDI, 2, 0, },
317                 .sdo[1]         = { 16, PCI1750_IDO, 2, 0, },
318                 .io_access      = IO_8b,
319         },
320         [TYPE_PCI1751] = {
321                 .name           = "pci1751",
322                 .main_pci_region = PCIDIO_MAINREG,
323                 .cardtype       = TYPE_PCI1751,
324                 .nsubdevs       = 3,
325                 .sdio[0]        = { 48, PCI1751_DIO, 2, 0, },
326                 .s8254[0]       = { 3, PCI1751_CNT, 1, 0, },
327                 .io_access      = IO_8b,
328         },
329         [TYPE_PCI1752] = {
330                 .name           = "pci1752",
331                 .main_pci_region = PCIDIO_MAINREG,
332                 .cardtype       = TYPE_PCI1752,
333                 .nsubdevs       = 3,
334                 .sdo[0]         = { 32, PCI1752_IDO, 2, 0, },
335                 .sdo[1]         = { 32, PCI1752_IDO2, 2, 0, },
336                 .boardid        = { 4, PCI175x_BOARDID, 1, SDF_INTERNAL, },
337                 .io_access      = IO_16b,
338         },
339         [TYPE_PCI1753] = {
340                 .name           = "pci1753",
341                 .main_pci_region = PCIDIO_MAINREG,
342                 .cardtype       = TYPE_PCI1753,
343                 .nsubdevs       = 4,
344                 .sdio[0]        = { 96, PCI1753_DIO, 4, 0, },
345                 .io_access      = IO_8b,
346         },
347         [TYPE_PCI1753E] = {
348                 .name           = "pci1753e",
349                 .main_pci_region = PCIDIO_MAINREG,
350                 .cardtype       = TYPE_PCI1753E,
351                 .nsubdevs       = 8,
352                 .sdio[0]        = { 96, PCI1753_DIO, 4, 0, },
353                 .sdio[1]        = { 96, PCI1753E_DIO, 4, 0, },
354                 .io_access      = IO_8b,
355         },
356         [TYPE_PCI1754] = {
357                 .name           = "pci1754",
358                 .main_pci_region = PCIDIO_MAINREG,
359                 .cardtype       = TYPE_PCI1754,
360                 .nsubdevs       = 3,
361                 .sdi[0]         = { 32, PCI1754_IDI, 2, 0, },
362                 .sdi[1]         = { 32, PCI1754_IDI2, 2, 0, },
363                 .boardid        = { 4, PCI175x_BOARDID, 1, SDF_INTERNAL, },
364                 .io_access      = IO_16b,
365         },
366         [TYPE_PCI1756] = {
367                 .name           = "pci1756",
368                 .main_pci_region = PCIDIO_MAINREG,
369                 .cardtype       = TYPE_PCI1756,
370                 .nsubdevs       = 3,
371                 .sdi[1]         = { 32, PCI1756_IDI, 2, 0, },
372                 .sdo[1]         = { 32, PCI1756_IDO, 2, 0, },
373                 .boardid        = { 4, PCI175x_BOARDID, 1, SDF_INTERNAL, },
374                 .io_access      = IO_16b,
375         },
376         [TYPE_PCI1760] = {
377                 /* This card has its own 'attach' */
378                 .name           = "pci1760",
379                 .main_pci_region = 0,
380                 .cardtype       = TYPE_PCI1760,
381                 .nsubdevs       = 4,
382                 .io_access      = IO_8b,
383         },
384         [TYPE_PCI1762] = {
385                 .name           = "pci1762",
386                 .main_pci_region = PCIDIO_MAINREG,
387                 .cardtype       = TYPE_PCI1762,
388                 .nsubdevs       = 3,
389                 .sdi[1]         = { 16, PCI1762_IDI, 1, 0, },
390                 .sdo[1]         = { 16, PCI1762_RO, 1, 0, },
391                 .boardid        = { 4, PCI1762_BOARDID, 1, SDF_INTERNAL, },
392                 .io_access      = IO_16b,
393         },
394 };
395 
396 struct pci_dio_private {
397         char valid;             /*  card is usable */
398         char GlobalIrqEnabled;  /*  1= any IRQ source is enabled */
399         /*  PCI-1760 specific data */
400         unsigned char IDICntEnable;     /* counter's counting enable status */
401         unsigned char IDICntOverEnable; /* counter's overflow interrupts enable
402                                          * status */
403         unsigned char IDICntMatchEnable;        /* counter's match interrupts
404                                                  * enable status */
405         unsigned char IDICntEdge;       /* counter's count edge value
406                                          * (bit=0 - rising, =1 - falling) */
407         unsigned short CntResValue[8];  /*  counters' reset value */
408         unsigned short CntMatchValue[8]; /*  counters' match interrupt value */
409         unsigned char IDIFiltersEn; /*  IDI's digital filters enable status */
410         unsigned char IDIPatMatchEn;    /*  IDI's pattern match enable status */
411         unsigned char IDIPatMatchValue; /*  IDI's pattern match value */
412         unsigned short IDIFiltrLow[8];  /*  IDI's filter value low signal */
413         unsigned short IDIFiltrHigh[8]; /*  IDI's filter value high signal */
414 };
415 
416 /*
417 ==============================================================================
418 */
419 static int pci_dio_insn_bits_di_b(struct comedi_device *dev,
420                                   struct comedi_subdevice *s,
421                                   struct comedi_insn *insn, unsigned int *data)
422 {
423         const struct diosubd_data *d = (const struct diosubd_data *)s->private;
424         int i;
425 
426         data[1] = 0;
427         for (i = 0; i < d->regs; i++)
428                 data[1] |= inb(dev->iobase + d->addr + i) << (8 * i);
429 
430 
431         return insn->n;
432 }
433 
434 /*
435 ==============================================================================
436 */
437 static int pci_dio_insn_bits_di_w(struct comedi_device *dev,
438                                   struct comedi_subdevice *s,
439                                   struct comedi_insn *insn, unsigned int *data)
440 {
441         const struct diosubd_data *d = (const struct diosubd_data *)s->private;
442         int i;
443 
444         data[1] = 0;
445         for (i = 0; i < d->regs; i++)
446                 data[1] |= inw(dev->iobase + d->addr + 2 * i) << (16 * i);
447 
448         return insn->n;
449 }
450 
451 static int pci_dio_insn_bits_do_b(struct comedi_device *dev,
452                                   struct comedi_subdevice *s,
453                                   struct comedi_insn *insn,
454                                   unsigned int *data)
455 {
456         const struct diosubd_data *d = (const struct diosubd_data *)s->private;
457         int i;
458 
459         if (comedi_dio_update_state(s, data)) {
460                 for (i = 0; i < d->regs; i++)
461                         outb((s->state >> (8 * i)) & 0xff,
462                              dev->iobase + d->addr + i);
463         }
464 
465         data[1] = s->state;
466 
467         return insn->n;
468 }
469 
470 static int pci_dio_insn_bits_do_w(struct comedi_device *dev,
471                                   struct comedi_subdevice *s,
472                                   struct comedi_insn *insn,
473                                   unsigned int *data)
474 {
475         const struct diosubd_data *d = (const struct diosubd_data *)s->private;
476         int i;
477 
478         if (comedi_dio_update_state(s, data)) {
479                 for (i = 0; i < d->regs; i++)
480                         outw((s->state >> (16 * i)) & 0xffff,
481                              dev->iobase + d->addr + 2 * i);
482         }
483 
484         data[1] = s->state;
485 
486         return insn->n;
487 }
488 
489 /*
490 ==============================================================================
491 */
492 static int pci_8254_insn_read(struct comedi_device *dev,
493                               struct comedi_subdevice *s,
494                               struct comedi_insn *insn, unsigned int *data)
495 {
496         const struct diosubd_data *d = (const struct diosubd_data *)s->private;
497         unsigned int chan, chip, chipchan;
498         unsigned long flags;
499 
500         chan = CR_CHAN(insn->chanspec); /* channel on subdevice */
501         chip = chan / 3;                /* chip on subdevice */
502         chipchan = chan - (3 * chip);   /* channel on chip on subdevice */
503         spin_lock_irqsave(&s->spin_lock, flags);
504         data[0] = i8254_read(dev->iobase + d->addr + (SIZE_8254 * chip),
505                         0, chipchan);
506         spin_unlock_irqrestore(&s->spin_lock, flags);
507         return 1;
508 }
509 
510 /*
511 ==============================================================================
512 */
513 static int pci_8254_insn_write(struct comedi_device *dev,
514                                struct comedi_subdevice *s,
515                                struct comedi_insn *insn, unsigned int *data)
516 {
517         const struct diosubd_data *d = (const struct diosubd_data *)s->private;
518         unsigned int chan, chip, chipchan;
519         unsigned long flags;
520 
521         chan = CR_CHAN(insn->chanspec); /* channel on subdevice */
522         chip = chan / 3;                /* chip on subdevice */
523         chipchan = chan - (3 * chip);   /* channel on chip on subdevice */
524         spin_lock_irqsave(&s->spin_lock, flags);
525         i8254_write(dev->iobase + d->addr + (SIZE_8254 * chip),
526                         0, chipchan, data[0]);
527         spin_unlock_irqrestore(&s->spin_lock, flags);
528         return 1;
529 }
530 
531 /*
532 ==============================================================================
533 */
534 static int pci_8254_insn_config(struct comedi_device *dev,
535                                 struct comedi_subdevice *s,
536                                 struct comedi_insn *insn, unsigned int *data)
537 {
538         const struct diosubd_data *d = (const struct diosubd_data *)s->private;
539         unsigned int chan, chip, chipchan;
540         unsigned long iobase;
541         int ret = 0;
542         unsigned long flags;
543 
544         chan = CR_CHAN(insn->chanspec); /* channel on subdevice */
545         chip = chan / 3;                /* chip on subdevice */
546         chipchan = chan - (3 * chip);   /* channel on chip on subdevice */
547         iobase = dev->iobase + d->addr + (SIZE_8254 * chip);
548         spin_lock_irqsave(&s->spin_lock, flags);
549         switch (data[0]) {
550         case INSN_CONFIG_SET_COUNTER_MODE:
551                 ret = i8254_set_mode(iobase, 0, chipchan, data[1]);
552                 if (ret < 0)
553                         ret = -EINVAL;
554                 break;
555         case INSN_CONFIG_8254_READ_STATUS:
556                 data[1] = i8254_status(iobase, 0, chipchan);
557                 break;
558         default:
559                 ret = -EINVAL;
560                 break;
561         }
562         spin_unlock_irqrestore(&s->spin_lock, flags);
563         return ret < 0 ? ret : insn->n;
564 }
565 
566 /*
567 ==============================================================================
568 */
569 static int pci1760_unchecked_mbxrequest(struct comedi_device *dev,
570                                         unsigned char *omb, unsigned char *imb,
571                                         int repeats)
572 {
573         int cnt, tout, ok = 0;
574 
575         for (cnt = 0; cnt < repeats; cnt++) {
576                 outb(omb[0], dev->iobase + OMB0);
577                 outb(omb[1], dev->iobase + OMB1);
578                 outb(omb[2], dev->iobase + OMB2);
579                 outb(omb[3], dev->iobase + OMB3);
580                 for (tout = 0; tout < 251; tout++) {
581                         imb[2] = inb(dev->iobase + IMB2);
582                         if (imb[2] == omb[2]) {
583                                 imb[0] = inb(dev->iobase + IMB0);
584                                 imb[1] = inb(dev->iobase + IMB1);
585                                 imb[3] = inb(dev->iobase + IMB3);
586                                 ok = 1;
587                                 break;
588                         }
589                         udelay(1);
590                 }
591                 if (ok)
592                         return 0;
593         }
594 
595         dev_err(dev->class_dev, "PCI-1760 mailbox request timeout!\n");
596         return -ETIME;
597 }
598 
599 static int pci1760_clear_imb2(struct comedi_device *dev)
600 {
601         unsigned char omb[4] = { 0x0, 0x0, CMD_ClearIMB2, 0x0 };
602         unsigned char imb[4];
603         /* check if imb2 is already clear */
604         if (inb(dev->iobase + IMB2) == CMD_ClearIMB2)
605                 return 0;
606         return pci1760_unchecked_mbxrequest(dev, omb, imb, OMBCMD_RETRY);
607 }
608 
609 static int pci1760_mbxrequest(struct comedi_device *dev,
610                               unsigned char *omb, unsigned char *imb)
611 {
612         if (omb[2] == CMD_ClearIMB2) {
613                 dev_err(dev->class_dev,
614                         "bug! this function should not be used for CMD_ClearIMB2 command\n");
615                 return -EINVAL;
616         }
617         if (inb(dev->iobase + IMB2) == omb[2]) {
618                 int retval;
619 
620                 retval = pci1760_clear_imb2(dev);
621                 if (retval < 0)
622                         return retval;
623         }
624         return pci1760_unchecked_mbxrequest(dev, omb, imb, OMBCMD_RETRY);
625 }
626 
627 /*
628 ==============================================================================
629 */
630 static int pci1760_insn_bits_di(struct comedi_device *dev,
631                                 struct comedi_subdevice *s,
632                                 struct comedi_insn *insn, unsigned int *data)
633 {
634         data[1] = inb(dev->iobase + IMB3);
635 
636         return insn->n;
637 }
638 
639 static int pci1760_insn_bits_do(struct comedi_device *dev,
640                                 struct comedi_subdevice *s,
641                                 struct comedi_insn *insn,
642                                 unsigned int *data)
643 {
644         int ret;
645         unsigned char omb[4] = {
646                 0x00,
647                 0x00,
648                 CMD_SetRelaysOutput,
649                 0x00
650         };
651         unsigned char imb[4];
652 
653         if (comedi_dio_update_state(s, data)) {
654                 omb[0] = s->state;
655                 ret = pci1760_mbxrequest(dev, omb, imb);
656                 if (!ret)
657                         return ret;
658         }
659 
660         data[1] = s->state;
661 
662         return insn->n;
663 }
664 
665 /*
666 ==============================================================================
667 */
668 static int pci1760_insn_cnt_read(struct comedi_device *dev,
669                                  struct comedi_subdevice *s,
670                                  struct comedi_insn *insn, unsigned int *data)
671 {
672         int ret, n;
673         unsigned char omb[4] = {
674                 CR_CHAN(insn->chanspec) & 0x07,
675                 0x00,
676                 CMD_GetIDICntCurValue,
677                 0x00
678         };
679         unsigned char imb[4];
680 
681         for (n = 0; n < insn->n; n++) {
682                 ret = pci1760_mbxrequest(dev, omb, imb);
683                 if (!ret)
684                         return ret;
685                 data[n] = (imb[1] << 8) + imb[0];
686         }
687 
688         return n;
689 }
690 
691 /*
692 ==============================================================================
693 */
694 static int pci1760_insn_cnt_write(struct comedi_device *dev,
695                                   struct comedi_subdevice *s,
696                                   struct comedi_insn *insn, unsigned int *data)
697 {
698         struct pci_dio_private *devpriv = dev->private;
699         int ret;
700         unsigned char chan = CR_CHAN(insn->chanspec) & 0x07;
701         unsigned char bitmask = 1 << chan;
702         unsigned char omb[4] = {
703                 data[0] & 0xff,
704                 (data[0] >> 8) & 0xff,
705                 CMD_SetIDI0CntResetValue + chan,
706                 0x00
707         };
708         unsigned char imb[4];
709 
710         /* Set reset value if different */
711         if (devpriv->CntResValue[chan] != (data[0] & 0xffff)) {
712                 ret = pci1760_mbxrequest(dev, omb, imb);
713                 if (!ret)
714                         return ret;
715                 devpriv->CntResValue[chan] = data[0] & 0xffff;
716         }
717 
718         omb[0] = bitmask;       /*  reset counter to it reset value */
719         omb[2] = CMD_ResetIDICounters;
720         ret = pci1760_mbxrequest(dev, omb, imb);
721         if (!ret)
722                 return ret;
723 
724         /*  start counter if it don't run */
725         if (!(bitmask & devpriv->IDICntEnable)) {
726                 omb[0] = bitmask;
727                 omb[2] = CMD_EnableIDICounters;
728                 ret = pci1760_mbxrequest(dev, omb, imb);
729                 if (!ret)
730                         return ret;
731                 devpriv->IDICntEnable |= bitmask;
732         }
733         return 1;
734 }
735 
736 /*
737 ==============================================================================
738 */
739 static int pci1760_reset(struct comedi_device *dev)
740 {
741         struct pci_dio_private *devpriv = dev->private;
742         int i;
743         unsigned char omb[4] = { 0x00, 0x00, 0x00, 0x00 };
744         unsigned char imb[4];
745 
746         outb(0, dev->iobase + INTCSR0); /*  disable IRQ */
747         outb(0, dev->iobase + INTCSR1);
748         outb(0, dev->iobase + INTCSR2);
749         outb(0, dev->iobase + INTCSR3);
750         devpriv->GlobalIrqEnabled = 0;
751 
752         omb[0] = 0x00;
753         omb[2] = CMD_SetRelaysOutput;   /*  reset relay outputs */
754         pci1760_mbxrequest(dev, omb, imb);
755 
756         omb[0] = 0x00;
757         omb[2] = CMD_EnableIDICounters; /*  disable IDI up counters */
758         pci1760_mbxrequest(dev, omb, imb);
759         devpriv->IDICntEnable = 0;
760 
761         omb[0] = 0x00;
762         omb[2] = CMD_OverflowIDICounters; /* disable counters overflow
763                                            * interrupts */
764         pci1760_mbxrequest(dev, omb, imb);
765         devpriv->IDICntOverEnable = 0;
766 
767         omb[0] = 0x00;
768         omb[2] = CMD_MatchIntIDICounters; /* disable counters match value
769                                            * interrupts */
770         pci1760_mbxrequest(dev, omb, imb);
771         devpriv->IDICntMatchEnable = 0;
772 
773         omb[0] = 0x00;
774         omb[1] = 0x80;
775         for (i = 0; i < 8; i++) {       /*  set IDI up counters match value */
776                 omb[2] = CMD_SetIDI0CntMatchValue + i;
777                 pci1760_mbxrequest(dev, omb, imb);
778                 devpriv->CntMatchValue[i] = 0x8000;
779         }
780 
781         omb[0] = 0x00;
782         omb[1] = 0x00;
783         for (i = 0; i < 8; i++) {       /*  set IDI up counters reset value */
784                 omb[2] = CMD_SetIDI0CntResetValue + i;
785                 pci1760_mbxrequest(dev, omb, imb);
786                 devpriv->CntResValue[i] = 0x0000;
787         }
788 
789         omb[0] = 0xff;
790         omb[2] = CMD_ResetIDICounters; /* reset IDI up counters to reset
791                                         * values */
792         pci1760_mbxrequest(dev, omb, imb);
793 
794         omb[0] = 0x00;
795         omb[2] = CMD_EdgeIDICounters;   /*  set IDI up counters count edge */
796         pci1760_mbxrequest(dev, omb, imb);
797         devpriv->IDICntEdge = 0x00;
798 
799         omb[0] = 0x00;
800         omb[2] = CMD_EnableIDIFilters;  /*  disable all digital in filters */
801         pci1760_mbxrequest(dev, omb, imb);
802         devpriv->IDIFiltersEn = 0x00;
803 
804         omb[0] = 0x00;
805         omb[2] = CMD_EnableIDIPatternMatch;     /*  disable pattern matching */
806         pci1760_mbxrequest(dev, omb, imb);
807         devpriv->IDIPatMatchEn = 0x00;
808 
809         omb[0] = 0x00;
810         omb[2] = CMD_SetIDIPatternMatch;        /*  set pattern match value */
811         pci1760_mbxrequest(dev, omb, imb);
812         devpriv->IDIPatMatchValue = 0x00;
813 
814         return 0;
815 }
816 
817 /*
818 ==============================================================================
819 */
820 static int pci_dio_reset(struct comedi_device *dev)
821 {
822         const struct dio_boardtype *this_board = comedi_board(dev);
823 
824         switch (this_board->cardtype) {
825         case TYPE_PCI1730:
826                 outb(0, dev->iobase + PCI1730_DO);      /*  clear outputs */
827                 outb(0, dev->iobase + PCI1730_DO + 1);
828                 outb(0, dev->iobase + PCI1730_IDO);
829                 outb(0, dev->iobase + PCI1730_IDO + 1);
830                 /* fallthrough */
831         case TYPE_PCI1733:
832                 /* disable interrupts */
833                 outb(0, dev->iobase + PCI1730_3_INT_EN);
834                 /* clear interrupts */
835                 outb(0x0f, dev->iobase + PCI1730_3_INT_CLR);
836                 /* set rising edge trigger */
837                 outb(0, dev->iobase + PCI1730_3_INT_RF);
838                 break;
839         case TYPE_PCI1734:
840                 outb(0, dev->iobase + PCI1734_IDO);     /*  clear outputs */
841                 outb(0, dev->iobase + PCI1734_IDO + 1);
842                 outb(0, dev->iobase + PCI1734_IDO + 2);
843                 outb(0, dev->iobase + PCI1734_IDO + 3);
844                 break;
845         case TYPE_PCI1735:
846                 outb(0, dev->iobase + PCI1735_DO);      /*  clear outputs */
847                 outb(0, dev->iobase + PCI1735_DO + 1);
848                 outb(0, dev->iobase + PCI1735_DO + 2);
849                 outb(0, dev->iobase + PCI1735_DO + 3);
850                 i8254_set_mode(dev->iobase + PCI1735_C8254, 0, 0, I8254_MODE0);
851                 i8254_set_mode(dev->iobase + PCI1735_C8254, 0, 1, I8254_MODE0);
852                 i8254_set_mode(dev->iobase + PCI1735_C8254, 0, 2, I8254_MODE0);
853                 break;
854 
855         case TYPE_PCI1736:
856                 outb(0, dev->iobase + PCI1736_IDO);
857                 outb(0, dev->iobase + PCI1736_IDO + 1);
858                 /* disable interrupts */
859                 outb(0, dev->iobase + PCI1736_3_INT_EN);
860                 /* clear interrupts */
861                 outb(0x0f, dev->iobase + PCI1736_3_INT_CLR);
862                 /* set rising edge trigger */
863                 outb(0, dev->iobase + PCI1736_3_INT_RF);
864                 break;
865 
866         case TYPE_PCI1739:
867                 /* disable & clear interrupts */
868                 outb(0x88, dev->iobase + PCI1739_ICR);
869                 break;
870 
871         case TYPE_PCI1750:
872         case TYPE_PCI1751:
873                 /* disable & clear interrupts */
874                 outb(0x88, dev->iobase + PCI1750_ICR);
875                 break;
876         case TYPE_PCI1752:
877                 outw(0, dev->iobase + PCI1752_6_CFC); /* disable channel freeze
878                                                        * function */
879                 outw(0, dev->iobase + PCI1752_IDO);     /*  clear outputs */
880                 outw(0, dev->iobase + PCI1752_IDO + 2);
881                 outw(0, dev->iobase + PCI1752_IDO2);
882                 outw(0, dev->iobase + PCI1752_IDO2 + 2);
883                 break;
884         case TYPE_PCI1753E:
885                 outb(0x88, dev->iobase + PCI1753E_ICR0); /* disable & clear
886                                                           * interrupts */
887                 outb(0x80, dev->iobase + PCI1753E_ICR1);
888                 outb(0x80, dev->iobase + PCI1753E_ICR2);
889                 outb(0x80, dev->iobase + PCI1753E_ICR3);
890                 /* fallthrough */
891         case TYPE_PCI1753:
892                 outb(0x88, dev->iobase + PCI1753_ICR0); /* disable & clear
893                                                          * interrupts */
894                 outb(0x80, dev->iobase + PCI1753_ICR1);
895                 outb(0x80, dev->iobase + PCI1753_ICR2);
896                 outb(0x80, dev->iobase + PCI1753_ICR3);
897                 break;
898         case TYPE_PCI1754:
899                 outw(0x08, dev->iobase + PCI1754_6_ICR0); /* disable and clear
900                                                            * interrupts */
901                 outw(0x08, dev->iobase + PCI1754_6_ICR1);
902                 outw(0x08, dev->iobase + PCI1754_ICR2);
903                 outw(0x08, dev->iobase + PCI1754_ICR3);
904                 break;
905         case TYPE_PCI1756:
906                 outw(0, dev->iobase + PCI1752_6_CFC); /* disable channel freeze
907                                                        * function */
908                 outw(0x08, dev->iobase + PCI1754_6_ICR0); /* disable and clear
909                                                            * interrupts */
910                 outw(0x08, dev->iobase + PCI1754_6_ICR1);
911                 outw(0, dev->iobase + PCI1756_IDO);     /*  clear outputs */
912                 outw(0, dev->iobase + PCI1756_IDO + 2);
913                 break;
914         case TYPE_PCI1760:
915                 pci1760_reset(dev);
916                 break;
917         case TYPE_PCI1762:
918                 outw(0x0101, dev->iobase + PCI1762_ICR); /* disable & clear
919                                                           * interrupts */
920                 break;
921         }
922 
923         return 0;
924 }
925 
926 /*
927 ==============================================================================
928 */
929 static int pci1760_attach(struct comedi_device *dev)
930 {
931         struct comedi_subdevice *s;
932 
933         s = &dev->subdevices[0];
934         s->type = COMEDI_SUBD_DI;
935         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON;
936         s->n_chan = 8;
937         s->maxdata = 1;
938         s->len_chanlist = 8;
939         s->range_table = &range_digital;
940         s->insn_bits = pci1760_insn_bits_di;
941 
942         s = &dev->subdevices[1];
943         s->type = COMEDI_SUBD_DO;
944         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
945         s->n_chan = 8;
946         s->maxdata = 1;
947         s->len_chanlist = 8;
948         s->range_table = &range_digital;
949         s->state = 0;
950         s->insn_bits = pci1760_insn_bits_do;
951 
952         s = &dev->subdevices[2];
953         s->type = COMEDI_SUBD_TIMER;
954         s->subdev_flags = SDF_WRITABLE | SDF_LSAMPL;
955         s->n_chan = 2;
956         s->maxdata = 0xffffffff;
957         s->len_chanlist = 2;
958 /*       s->insn_config=pci1760_insn_pwm_cfg; */
959 
960         s = &dev->subdevices[3];
961         s->type = COMEDI_SUBD_COUNTER;
962         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
963         s->n_chan = 8;
964         s->maxdata = 0xffff;
965         s->len_chanlist = 8;
966         s->insn_read = pci1760_insn_cnt_read;
967         s->insn_write = pci1760_insn_cnt_write;
968 /*       s->insn_config=pci1760_insn_cnt_cfg; */
969 
970         return 0;
971 }
972 
973 /*
974 ==============================================================================
975 */
976 static int pci_dio_add_di(struct comedi_device *dev,
977                           struct comedi_subdevice *s,
978                           const struct diosubd_data *d)
979 {
980         const struct dio_boardtype *this_board = comedi_board(dev);
981 
982         s->type = COMEDI_SUBD_DI;
983         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON | d->specflags;
984         if (d->chans > 16)
985                 s->subdev_flags |= SDF_LSAMPL;
986         s->n_chan = d->chans;
987         s->maxdata = 1;
988         s->len_chanlist = d->chans;
989         s->range_table = &range_digital;
990         switch (this_board->io_access) {
991         case IO_8b:
992                 s->insn_bits = pci_dio_insn_bits_di_b;
993                 break;
994         case IO_16b:
995                 s->insn_bits = pci_dio_insn_bits_di_w;
996                 break;
997         }
998         s->private = (void *)d;
999 
1000         return 0;
1001 }
1002 
1003 /*
1004 ==============================================================================
1005 */
1006 static int pci_dio_add_do(struct comedi_device *dev,
1007                           struct comedi_subdevice *s,
1008                           const struct diosubd_data *d)
1009 {
1010         const struct dio_boardtype *this_board = comedi_board(dev);
1011 
1012         s->type = COMEDI_SUBD_DO;
1013         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
1014         if (d->chans > 16)
1015                 s->subdev_flags |= SDF_LSAMPL;
1016         s->n_chan = d->chans;
1017         s->maxdata = 1;
1018         s->len_chanlist = d->chans;
1019         s->range_table = &range_digital;
1020         s->state = 0;
1021         switch (this_board->io_access) {
1022         case IO_8b:
1023                 s->insn_bits = pci_dio_insn_bits_do_b;
1024                 break;
1025         case IO_16b:
1026                 s->insn_bits = pci_dio_insn_bits_do_w;
1027                 break;
1028         }
1029         s->private = (void *)d;
1030 
1031         return 0;
1032 }
1033 
1034 /*
1035 ==============================================================================
1036 */
1037 static int pci_dio_add_8254(struct comedi_device *dev,
1038                             struct comedi_subdevice *s,
1039                             const struct diosubd_data *d)
1040 {
1041         s->type = COMEDI_SUBD_COUNTER;
1042         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1043         s->n_chan = d->chans;
1044         s->maxdata = 65535;
1045         s->len_chanlist = d->chans;
1046         s->insn_read = pci_8254_insn_read;
1047         s->insn_write = pci_8254_insn_write;
1048         s->insn_config = pci_8254_insn_config;
1049         s->private = (void *)d;
1050 
1051         return 0;
1052 }
1053 
1054 static unsigned long pci_dio_override_cardtype(struct pci_dev *pcidev,
1055                                                unsigned long cardtype)
1056 {
1057         /*
1058          * Change cardtype from TYPE_PCI1753 to TYPE_PCI1753E if expansion
1059          * board available.  Need to enable PCI device and request the main
1060          * registers PCI BAR temporarily to perform the test.
1061          */
1062         if (cardtype != TYPE_PCI1753)
1063                 return cardtype;
1064         if (pci_enable_device(pcidev) < 0)
1065                 return cardtype;
1066         if (pci_request_region(pcidev, PCIDIO_MAINREG, "adv_pci_dio") == 0) {
1067                 /*
1068                  * This test is based on Advantech's "advdaq" driver source
1069                  * (which declares its module licence as "GPL" although the
1070                  * driver source does not include a "COPYING" file).
1071                  */
1072                 unsigned long reg =
1073                         pci_resource_start(pcidev, PCIDIO_MAINREG) + 53;
1074 
1075                 outb(0x05, reg);
1076                 if ((inb(reg) & 0x07) == 0x02) {
1077                         outb(0x02, reg);
1078                         if ((inb(reg) & 0x07) == 0x05)
1079                                 cardtype = TYPE_PCI1753E;
1080                 }
1081                 pci_release_region(pcidev, PCIDIO_MAINREG);
1082         }
1083         pci_disable_device(pcidev);
1084         return cardtype;
1085 }
1086 
1087 static int pci_dio_auto_attach(struct comedi_device *dev,
1088                                unsigned long context)
1089 {
1090         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1091         const struct dio_boardtype *this_board = NULL;
1092         struct pci_dio_private *devpriv;
1093         struct comedi_subdevice *s;
1094         int ret, subdev, i, j;
1095 
1096         if (context < ARRAY_SIZE(boardtypes))
1097                 this_board = &boardtypes[context];
1098         if (!this_board)
1099                 return -ENODEV;
1100         dev->board_ptr = this_board;
1101         dev->board_name = this_board->name;
1102 
1103         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1104         if (!devpriv)
1105                 return -ENOMEM;
1106 
1107         ret = comedi_pci_enable(dev);
1108         if (ret)
1109                 return ret;
1110         dev->iobase = pci_resource_start(pcidev, this_board->main_pci_region);
1111 
1112         ret = comedi_alloc_subdevices(dev, this_board->nsubdevs);
1113         if (ret)
1114                 return ret;
1115 
1116         subdev = 0;
1117         for (i = 0; i < MAX_DI_SUBDEVS; i++)
1118                 if (this_board->sdi[i].chans) {
1119                         s = &dev->subdevices[subdev];
1120                         pci_dio_add_di(dev, s, &this_board->sdi[i]);
1121                         subdev++;
1122                 }
1123 
1124         for (i = 0; i < MAX_DO_SUBDEVS; i++)
1125                 if (this_board->sdo[i].chans) {
1126                         s = &dev->subdevices[subdev];
1127                         pci_dio_add_do(dev, s, &this_board->sdo[i]);
1128                         subdev++;
1129                 }
1130 
1131         for (i = 0; i < MAX_DIO_SUBDEVG; i++)
1132                 for (j = 0; j < this_board->sdio[i].regs; j++) {
1133                         s = &dev->subdevices[subdev];
1134                         ret = subdev_8255_init(dev, s, NULL,
1135                                                dev->iobase +
1136                                                this_board->sdio[i].addr +
1137                                                SIZE_8255 * j);
1138                         if (ret)
1139                                 return ret;
1140                         subdev++;
1141                 }
1142 
1143         if (this_board->boardid.chans) {
1144                 s = &dev->subdevices[subdev];
1145                 s->type = COMEDI_SUBD_DI;
1146                 pci_dio_add_di(dev, s, &this_board->boardid);
1147                 subdev++;
1148         }
1149 
1150         for (i = 0; i < MAX_8254_SUBDEVS; i++)
1151                 if (this_board->s8254[i].chans) {
1152                         s = &dev->subdevices[subdev];
1153                         pci_dio_add_8254(dev, s, &this_board->s8254[i]);
1154                         subdev++;
1155                 }
1156 
1157         if (this_board->cardtype == TYPE_PCI1760)
1158                 pci1760_attach(dev);
1159 
1160         devpriv->valid = 1;
1161 
1162         pci_dio_reset(dev);
1163 
1164         return 0;
1165 }
1166 
1167 static void pci_dio_detach(struct comedi_device *dev)
1168 {
1169         struct pci_dio_private *devpriv = dev->private;
1170 
1171         if (devpriv) {
1172                 if (devpriv->valid)
1173                         pci_dio_reset(dev);
1174         }
1175         comedi_pci_disable(dev);
1176 }
1177 
1178 static struct comedi_driver adv_pci_dio_driver = {
1179         .driver_name    = "adv_pci_dio",
1180         .module         = THIS_MODULE,
1181         .auto_attach    = pci_dio_auto_attach,
1182         .detach         = pci_dio_detach,
1183 };
1184 
1185 static int adv_pci_dio_pci_probe(struct pci_dev *dev,
1186                                  const struct pci_device_id *id)
1187 {
1188         unsigned long cardtype;
1189 
1190         cardtype = pci_dio_override_cardtype(dev, id->driver_data);
1191         return comedi_pci_auto_config(dev, &adv_pci_dio_driver, cardtype);
1192 }
1193 
1194 static const struct pci_device_id adv_pci_dio_pci_table[] = {
1195         { PCI_VDEVICE(ADVANTECH, 0x1730), TYPE_PCI1730 },
1196         { PCI_VDEVICE(ADVANTECH, 0x1733), TYPE_PCI1733 },
1197         { PCI_VDEVICE(ADVANTECH, 0x1734), TYPE_PCI1734 },
1198         { PCI_VDEVICE(ADVANTECH, 0x1735), TYPE_PCI1735 },
1199         { PCI_VDEVICE(ADVANTECH, 0x1736), TYPE_PCI1736 },
1200         { PCI_VDEVICE(ADVANTECH, 0x1739), TYPE_PCI1739 },
1201         { PCI_VDEVICE(ADVANTECH, 0x1750), TYPE_PCI1750 },
1202         { PCI_VDEVICE(ADVANTECH, 0x1751), TYPE_PCI1751 },
1203         { PCI_VDEVICE(ADVANTECH, 0x1752), TYPE_PCI1752 },
1204         { PCI_VDEVICE(ADVANTECH, 0x1753), TYPE_PCI1753 },
1205         { PCI_VDEVICE(ADVANTECH, 0x1754), TYPE_PCI1754 },
1206         { PCI_VDEVICE(ADVANTECH, 0x1756), TYPE_PCI1756 },
1207         { PCI_VDEVICE(ADVANTECH, 0x1760), TYPE_PCI1760 },
1208         { PCI_VDEVICE(ADVANTECH, 0x1762), TYPE_PCI1762 },
1209         { 0 }
1210 };
1211 MODULE_DEVICE_TABLE(pci, adv_pci_dio_pci_table);
1212 
1213 static struct pci_driver adv_pci_dio_pci_driver = {
1214         .name           = "adv_pci_dio",
1215         .id_table       = adv_pci_dio_pci_table,
1216         .probe          = adv_pci_dio_pci_probe,
1217         .remove         = comedi_pci_auto_unconfig,
1218 };
1219 module_comedi_pci_driver(adv_pci_dio_driver, adv_pci_dio_pci_driver);
1220 
1221 MODULE_AUTHOR("Comedi http://www.comedi.org");
1222 MODULE_DESCRIPTION("Comedi low-level driver");
1223 MODULE_LICENSE("GPL");
1224 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us