Linux 3.9-rc5
[deliverable/linux.git] / drivers / staging / comedi / drivers / addi_apci_16xx.c
1 /*
2 * addi_apci_16xx.c
3 * Copyright (C) 2004,2005 ADDI-DATA GmbH for the source code of this module.
4 * Project manager: S. Weber
5 *
6 * ADDI-DATA GmbH
7 * Dieselstrasse 3
8 * D-77833 Ottersweier
9 * Tel: +19(0)7223/9493-0
10 * Fax: +49(0)7223/9493-92
11 * http://www.addi-data.com
12 * info@addi-data.com
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 * You should also find the complete GPL in the COPYING file accompanying
29 * this source code.
30 */
31
32 #include <linux/pci.h>
33
34 #include "../comedidev.h"
35
36 /*
37 * PCI device ids supported by this driver
38 */
39 #define PCI_DEVICE_ID_APCI1648 0x1009
40 #define PCI_DEVICE_ID_APCI1696 0x100a
41
42 /*
43 * Register I/O map
44 */
45 #define APCI16XX_IN_REG(x) (((x) * 4) + 0x08)
46 #define APCI16XX_OUT_REG(x) (((x) * 4) + 0x14)
47 #define APCI16XX_DIR_REG(x) (((x) * 4) + 0x20)
48
49 struct apci16xx_boardinfo {
50 const char *name;
51 unsigned short vendor;
52 unsigned short device;
53 int n_chan;
54 };
55
56 static const struct apci16xx_boardinfo apci16xx_boardtypes[] = {
57 {
58 .name = "apci1648",
59 .vendor = PCI_VENDOR_ID_ADDIDATA,
60 .device = PCI_DEVICE_ID_APCI1648,
61 .n_chan = 48, /* 2 subdevices */
62 }, {
63 .name = "apci1696",
64 .vendor = PCI_VENDOR_ID_ADDIDATA,
65 .device = PCI_DEVICE_ID_APCI1696,
66 .n_chan = 96, /* 3 subdevices */
67 },
68 };
69
70 static int apci16xx_insn_config(struct comedi_device *dev,
71 struct comedi_subdevice *s,
72 struct comedi_insn *insn,
73 unsigned int *data)
74 {
75 unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
76 unsigned int bits;
77
78 /*
79 * Each 8-bit "port" is configurable as either input or
80 * output. Changing the configuration of any channel in
81 * a port changes the entire port.
82 */
83 if (chan_mask & 0x000000ff)
84 bits = 0x000000ff;
85 else if (chan_mask & 0x0000ff00)
86 bits = 0x0000ff00;
87 else if (chan_mask & 0x00ff0000)
88 bits = 0x00ff0000;
89 else
90 bits = 0xff000000;
91
92 switch (data[0]) {
93 case INSN_CONFIG_DIO_INPUT:
94 s->io_bits &= ~bits;
95 break;
96 case INSN_CONFIG_DIO_OUTPUT:
97 s->io_bits |= bits;
98 break;
99 case INSN_CONFIG_DIO_QUERY:
100 data[1] = (s->io_bits & bits) ? COMEDI_INPUT : COMEDI_OUTPUT;
101 return insn->n;
102 default:
103 return -EINVAL;
104 }
105
106 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index));
107
108 return insn->n;
109 }
110
111 static int apci16xx_dio_insn_bits(struct comedi_device *dev,
112 struct comedi_subdevice *s,
113 struct comedi_insn *insn,
114 unsigned int *data)
115 {
116 unsigned int mask = data[0];
117 unsigned int bits = data[1];
118
119 /* Only update the channels configured as outputs */
120 mask &= s->io_bits;
121 if (mask) {
122 s->state &= ~mask;
123 s->state |= (bits & mask);
124
125 outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index));
126 }
127
128 data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index));
129
130 return insn->n;
131 }
132
133 static const void *apci16xx_find_boardinfo(struct comedi_device *dev,
134 struct pci_dev *pcidev)
135 {
136 const struct apci16xx_boardinfo *board;
137 int i;
138
139 for (i = 0; i < ARRAY_SIZE(apci16xx_boardtypes); i++) {
140 board = &apci16xx_boardtypes[i];
141 if (board->vendor == pcidev->vendor &&
142 board->device == pcidev->device)
143 return board;
144 }
145 return NULL;
146 }
147
148 static int apci16xx_auto_attach(struct comedi_device *dev,
149 unsigned long context_unused)
150 {
151 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
152 const struct apci16xx_boardinfo *board;
153 struct comedi_subdevice *s;
154 unsigned int n_subdevs;
155 unsigned int last;
156 int i;
157 int ret;
158
159 board = apci16xx_find_boardinfo(dev, pcidev);
160 if (!board)
161 return -ENODEV;
162 dev->board_ptr = board;
163 dev->board_name = board->name;
164
165 ret = comedi_pci_enable(pcidev, dev->board_name);
166 if (ret)
167 return ret;
168
169 dev->iobase = pci_resource_start(pcidev, 0);
170
171 /*
172 * Work out the nubmer of subdevices needed to support all the
173 * digital i/o channels on the board. Each subdevice supports
174 * up to 32 channels.
175 */
176 n_subdevs = board->n_chan / 32;
177 if ((n_subdevs * 32) < board->n_chan) {
178 last = board->n_chan - (n_subdevs * 32);
179 n_subdevs++;
180 } else {
181 last = 0;
182 }
183
184 ret = comedi_alloc_subdevices(dev, n_subdevs);
185 if (ret)
186 return ret;
187
188 /* Initialize the TTL digital i/o subdevices */
189 for (i = 0; i < n_subdevs; i++) {
190 s = &dev->subdevices[i];
191 s->type = COMEDI_SUBD_DIO;
192 s->subdev_flags = SDF_WRITEABLE | SDF_READABLE;
193 s->n_chan = ((i * 32) < board->n_chan) ? 32 : last;
194 s->maxdata = 1;
195 s->range_table = &range_digital;
196 s->insn_config = apci16xx_insn_config;
197 s->insn_bits = apci16xx_dio_insn_bits;
198
199 /* Default all channels to inputs */
200 s->io_bits = 0;
201 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i));
202 }
203
204 return 0;
205 }
206
207 static void apci16xx_detach(struct comedi_device *dev)
208 {
209 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
210
211 if (pcidev) {
212 if (dev->iobase)
213 comedi_pci_disable(pcidev);
214 }
215 }
216
217 static struct comedi_driver apci16xx_driver = {
218 .driver_name = "addi_apci_16xx",
219 .module = THIS_MODULE,
220 .auto_attach = apci16xx_auto_attach,
221 .detach = apci16xx_detach,
222 .num_names = ARRAY_SIZE(apci16xx_boardtypes),
223 .board_name = &apci16xx_boardtypes[0].name,
224 .offset = sizeof(struct apci16xx_boardinfo),
225 };
226
227 static int apci16xx_pci_probe(struct pci_dev *dev,
228 const struct pci_device_id *ent)
229 {
230 return comedi_pci_auto_config(dev, &apci16xx_driver);
231 }
232
233 static DEFINE_PCI_DEVICE_TABLE(apci16xx_pci_table) = {
234 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, PCI_DEVICE_ID_APCI1648) },
235 { PCI_DEVICE(PCI_VENDOR_ID_ADDIDATA, PCI_DEVICE_ID_APCI1696) },
236 { 0 }
237 };
238 MODULE_DEVICE_TABLE(pci, apci16xx_pci_table);
239
240 static struct pci_driver apci16xx_pci_driver = {
241 .name = "addi_apci_16xx",
242 .id_table = apci16xx_pci_table,
243 .probe = apci16xx_pci_probe,
244 .remove = comedi_pci_auto_unconfig,
245 };
246 module_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver);
247
248 MODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards");
249 MODULE_AUTHOR("Comedi http://www.comedi.org");
250 MODULE_LICENSE("GPL");
This page took 0.043866 seconds and 5 git commands to generate.