1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26
27#include "../comedi_pci.h"
28
29
30
31
32#define APCI16XX_IN_REG(x) (((x) * 4) + 0x08)
33#define APCI16XX_OUT_REG(x) (((x) * 4) + 0x14)
34#define APCI16XX_DIR_REG(x) (((x) * 4) + 0x20)
35
36enum apci16xx_boardid {
37 BOARD_APCI1648,
38 BOARD_APCI1696,
39};
40
41struct apci16xx_boardinfo {
42 const char *name;
43 int n_chan;
44};
45
46static const struct apci16xx_boardinfo apci16xx_boardtypes[] = {
47 [BOARD_APCI1648] = {
48 .name = "apci1648",
49 .n_chan = 48,
50 },
51 [BOARD_APCI1696] = {
52 .name = "apci1696",
53 .n_chan = 96,
54 },
55};
56
57static int apci16xx_insn_config(struct comedi_device *dev,
58 struct comedi_subdevice *s,
59 struct comedi_insn *insn,
60 unsigned int *data)
61{
62 unsigned int chan = CR_CHAN(insn->chanspec);
63 unsigned int mask;
64 int ret;
65
66 if (chan < 8)
67 mask = 0x000000ff;
68 else if (chan < 16)
69 mask = 0x0000ff00;
70 else if (chan < 24)
71 mask = 0x00ff0000;
72 else
73 mask = 0xff000000;
74
75 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
76 if (ret)
77 return ret;
78
79 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index));
80
81 return insn->n;
82}
83
84static int apci16xx_dio_insn_bits(struct comedi_device *dev,
85 struct comedi_subdevice *s,
86 struct comedi_insn *insn,
87 unsigned int *data)
88{
89 if (comedi_dio_update_state(s, data))
90 outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index));
91
92 data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index));
93
94 return insn->n;
95}
96
97static int apci16xx_auto_attach(struct comedi_device *dev,
98 unsigned long context)
99{
100 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
101 const struct apci16xx_boardinfo *board = NULL;
102 struct comedi_subdevice *s;
103 unsigned int n_subdevs;
104 unsigned int last;
105 int i;
106 int ret;
107
108 if (context < ARRAY_SIZE(apci16xx_boardtypes))
109 board = &apci16xx_boardtypes[context];
110 if (!board)
111 return -ENODEV;
112 dev->board_ptr = board;
113 dev->board_name = board->name;
114
115 ret = comedi_pci_enable(dev);
116 if (ret)
117 return ret;
118
119 dev->iobase = pci_resource_start(pcidev, 0);
120
121
122
123
124
125
126 n_subdevs = board->n_chan / 32;
127 if ((n_subdevs * 32) < board->n_chan) {
128 last = board->n_chan - (n_subdevs * 32);
129 n_subdevs++;
130 } else {
131 last = 0;
132 }
133
134 ret = comedi_alloc_subdevices(dev, n_subdevs);
135 if (ret)
136 return ret;
137
138
139 for (i = 0; i < n_subdevs; i++) {
140 s = &dev->subdevices[i];
141 s->type = COMEDI_SUBD_DIO;
142 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
143 s->n_chan = ((i * 32) < board->n_chan) ? 32 : last;
144 s->maxdata = 1;
145 s->range_table = &range_digital;
146 s->insn_config = apci16xx_insn_config;
147 s->insn_bits = apci16xx_dio_insn_bits;
148
149
150 s->io_bits = 0;
151 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i));
152 }
153
154 return 0;
155}
156
157static struct comedi_driver apci16xx_driver = {
158 .driver_name = "addi_apci_16xx",
159 .module = THIS_MODULE,
160 .auto_attach = apci16xx_auto_attach,
161 .detach = comedi_pci_detach,
162};
163
164static int apci16xx_pci_probe(struct pci_dev *dev,
165 const struct pci_device_id *id)
166{
167 return comedi_pci_auto_config(dev, &apci16xx_driver, id->driver_data);
168}
169
170static const struct pci_device_id apci16xx_pci_table[] = {
171 { PCI_VDEVICE(ADDIDATA, 0x1009), BOARD_APCI1648 },
172 { PCI_VDEVICE(ADDIDATA, 0x100a), BOARD_APCI1696 },
173 { 0 }
174};
175MODULE_DEVICE_TABLE(pci, apci16xx_pci_table);
176
177static struct pci_driver apci16xx_pci_driver = {
178 .name = "addi_apci_16xx",
179 .id_table = apci16xx_pci_table,
180 .probe = apci16xx_pci_probe,
181 .remove = comedi_pci_auto_unconfig,
182};
183module_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver);
184
185MODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards");
186MODULE_AUTHOR("Comedi http://www.comedi.org");
187MODULE_LICENSE("GPL");
188