Commit | Line | Data |
---|---|---|
a6136145 SR |
1 | /* |
2 | comedi/drivers/contec_pci_dio.c | |
3 | ||
4 | COMEDI - Linux Control and Measurement Device Interface | |
5 | Copyright (C) 2000 David A. Schleef <ds@schleef.org> | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | ||
21 | */ | |
22 | /* | |
23 | Driver: contec_pci_dio | |
24 | Description: Contec PIO1616L digital I/O board | |
25 | Devices: [Contec] PIO1616L (contec_pci_dio) | |
26 | Author: Stefano Rivoir <s.rivoir@gts.it> | |
27 | Updated: Wed, 27 Jun 2007 13:00:06 +0100 | |
28 | Status: works | |
29 | ||
30 | Configuration Options: | |
31 | [0] - PCI bus of device (optional) | |
32 | [1] - PCI slot of device (optional) | |
33 | If bus/slot is not specified, the first supported | |
34 | PCI device found will be used. | |
35 | */ | |
36 | ||
37 | #include "../comedidev.h" | |
38 | ||
39 | #include "comedi_pci.h" | |
40 | ||
216ed755 | 41 | enum contec_model { |
a6136145 | 42 | PIO1616L = 0, |
216ed755 | 43 | }; |
a6136145 | 44 | |
72e7b74d | 45 | struct contec_board { |
a6136145 SR |
46 | const char *name; |
47 | int model; | |
48 | int in_ports; | |
49 | int out_ports; | |
50 | int in_offs; | |
51 | int out_offs; | |
52 | int out_boffs; | |
72e7b74d BP |
53 | }; |
54 | static const struct contec_board contec_boards[] = { | |
a6136145 SR |
55 | {"PIO1616L", PIO1616L, 16, 16, 0, 2, 10}, |
56 | }; | |
57 | ||
58 | #define PCI_DEVICE_ID_PIO1616L 0x8172 | |
59 | static DEFINE_PCI_DEVICE_TABLE(contec_pci_table) = { | |
0a85b6f0 MT |
60 | { |
61 | PCI_VENDOR_ID_CONTEC, PCI_DEVICE_ID_PIO1616L, PCI_ANY_ID, | |
62 | PCI_ANY_ID, 0, 0, PIO1616L}, { | |
63 | 0} | |
a6136145 SR |
64 | }; |
65 | ||
66 | MODULE_DEVICE_TABLE(pci, contec_pci_table); | |
67 | ||
72e7b74d | 68 | #define thisboard ((const struct contec_board *)dev->board_ptr) |
a6136145 | 69 | |
4f4123ba | 70 | struct contec_private { |
a6136145 SR |
71 | int data; |
72 | ||
73 | struct pci_dev *pci_dev; | |
74 | ||
4f4123ba | 75 | }; |
a6136145 | 76 | |
4f4123ba | 77 | #define devpriv ((struct contec_private *)dev->private) |
a6136145 | 78 | |
0a85b6f0 MT |
79 | static int contec_attach(struct comedi_device *dev, |
80 | struct comedi_devconfig *it); | |
da91b269 | 81 | static int contec_detach(struct comedi_device *dev); |
139dfbdf | 82 | static struct comedi_driver driver_contec = { |
68c3dbff BP |
83 | .driver_name = "contec_pci_dio", |
84 | .module = THIS_MODULE, | |
85 | .attach = contec_attach, | |
86 | .detach = contec_detach, | |
a6136145 SR |
87 | }; |
88 | ||
89 | /* Classic digital IO */ | |
0a85b6f0 MT |
90 | static int contec_di_insn_bits(struct comedi_device *dev, |
91 | struct comedi_subdevice *s, | |
92 | struct comedi_insn *insn, unsigned int *data); | |
93 | static int contec_do_insn_bits(struct comedi_device *dev, | |
94 | struct comedi_subdevice *s, | |
95 | struct comedi_insn *insn, unsigned int *data); | |
a6136145 SR |
96 | |
97 | #if 0 | |
da91b269 | 98 | static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, |
0a85b6f0 | 99 | struct comedi_cmd *cmd); |
a6136145 SR |
100 | |
101 | static int contec_ns_to_timer(unsigned int *ns, int round); | |
102 | #endif | |
103 | ||
da91b269 | 104 | static int contec_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
a6136145 | 105 | { |
20fb2280 | 106 | struct pci_dev *pcidev = NULL; |
34c43922 | 107 | struct comedi_subdevice *s; |
a6136145 SR |
108 | |
109 | printk("comedi%d: contec: ", dev->minor); | |
110 | ||
111 | dev->board_name = thisboard->name; | |
112 | ||
4f4123ba | 113 | if (alloc_private(dev, sizeof(struct contec_private)) < 0) |
a6136145 SR |
114 | return -ENOMEM; |
115 | ||
116 | if (alloc_subdevices(dev, 2) < 0) | |
117 | return -ENOMEM; | |
118 | ||
20fb2280 | 119 | for_each_pci_dev(pcidev) { |
a6136145 | 120 | if (pcidev->vendor == PCI_VENDOR_ID_CONTEC && |
0a85b6f0 | 121 | pcidev->device == PCI_DEVICE_ID_PIO1616L) { |
a6136145 SR |
122 | if (it->options[0] || it->options[1]) { |
123 | /* Check bus and slot. */ | |
124 | if (it->options[0] != pcidev->bus->number || | |
0a85b6f0 | 125 | it->options[1] != PCI_SLOT(pcidev->devfn)) { |
a6136145 SR |
126 | continue; |
127 | } | |
128 | } | |
129 | devpriv->pci_dev = pcidev; | |
130 | if (comedi_pci_enable(pcidev, "contec_pci_dio")) { | |
0a85b6f0 MT |
131 | printk |
132 | ("error enabling PCI device and request regions!\n"); | |
a6136145 SR |
133 | return -EIO; |
134 | } | |
135 | dev->iobase = pci_resource_start(pcidev, 0); | |
136 | printk(" base addr %lx ", dev->iobase); | |
137 | ||
138 | dev->board_ptr = contec_boards + 0; | |
139 | ||
140 | s = dev->subdevices + 0; | |
141 | ||
142 | s->type = COMEDI_SUBD_DI; | |
143 | s->subdev_flags = SDF_READABLE; | |
144 | s->n_chan = 16; | |
145 | s->maxdata = 1; | |
146 | s->range_table = &range_digital; | |
147 | s->insn_bits = contec_di_insn_bits; | |
148 | ||
149 | s = dev->subdevices + 1; | |
150 | s->type = COMEDI_SUBD_DO; | |
151 | s->subdev_flags = SDF_WRITABLE; | |
152 | s->n_chan = 16; | |
153 | s->maxdata = 1; | |
154 | s->range_table = &range_digital; | |
155 | s->insn_bits = contec_do_insn_bits; | |
156 | ||
157 | printk("attached\n"); | |
158 | ||
159 | return 1; | |
160 | } | |
161 | } | |
162 | ||
163 | printk("card not present!\n"); | |
164 | ||
165 | return -EIO; | |
166 | } | |
167 | ||
da91b269 | 168 | static int contec_detach(struct comedi_device *dev) |
a6136145 SR |
169 | { |
170 | printk("comedi%d: contec: remove\n", dev->minor); | |
171 | ||
172 | if (devpriv && devpriv->pci_dev) { | |
426054ce | 173 | if (dev->iobase) |
a6136145 | 174 | comedi_pci_disable(devpriv->pci_dev); |
a6136145 SR |
175 | pci_dev_put(devpriv->pci_dev); |
176 | } | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | #if 0 | |
da91b269 | 182 | static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s, |
0a85b6f0 | 183 | struct comedi_cmd *cmd) |
a6136145 SR |
184 | { |
185 | printk("contec_cmdtest called\n"); | |
186 | return 0; | |
187 | } | |
188 | ||
189 | static int contec_ns_to_timer(unsigned int *ns, int round) | |
190 | { | |
191 | return *ns; | |
192 | } | |
193 | #endif | |
194 | ||
0a85b6f0 MT |
195 | static int contec_do_insn_bits(struct comedi_device *dev, |
196 | struct comedi_subdevice *s, | |
197 | struct comedi_insn *insn, unsigned int *data) | |
a6136145 SR |
198 | { |
199 | ||
200 | printk("contec_do_insn_bits called\n"); | |
201 | printk(" data: %d %d\n", data[0], data[1]); | |
202 | ||
203 | if (insn->n != 2) | |
204 | return -EINVAL; | |
205 | ||
206 | if (data[0]) { | |
207 | s->state &= ~data[0]; | |
208 | s->state |= data[0] & data[1]; | |
5f74ea14 | 209 | printk(" out: %d on %lx\n", s->state, |
0a85b6f0 | 210 | dev->iobase + thisboard->out_offs); |
a6136145 SR |
211 | outw(s->state, dev->iobase + thisboard->out_offs); |
212 | } | |
213 | return 2; | |
214 | } | |
215 | ||
0a85b6f0 MT |
216 | static int contec_di_insn_bits(struct comedi_device *dev, |
217 | struct comedi_subdevice *s, | |
218 | struct comedi_insn *insn, unsigned int *data) | |
a6136145 SR |
219 | { |
220 | ||
5f74ea14 GKH |
221 | printk("contec_di_insn_bits called\n"); |
222 | printk(" data: %d %d\n", data[0], data[1]); | |
a6136145 SR |
223 | |
224 | if (insn->n != 2) | |
225 | return -EINVAL; | |
226 | ||
227 | data[1] = inw(dev->iobase + thisboard->in_offs); | |
228 | ||
229 | return 2; | |
230 | } | |
231 | ||
727b286b AT |
232 | static int __devinit driver_contec_pci_probe(struct pci_dev *dev, |
233 | const struct pci_device_id *ent) | |
234 | { | |
235 | return comedi_pci_auto_config(dev, driver_contec.driver_name); | |
236 | } | |
237 | ||
238 | static void __devexit driver_contec_pci_remove(struct pci_dev *dev) | |
239 | { | |
240 | comedi_pci_auto_unconfig(dev); | |
241 | } | |
242 | ||
243 | static struct pci_driver driver_contec_pci_driver = { | |
244 | .id_table = contec_pci_table, | |
245 | .probe = &driver_contec_pci_probe, | |
246 | .remove = __devexit_p(&driver_contec_pci_remove) | |
247 | }; | |
248 | ||
249 | static int __init driver_contec_init_module(void) | |
250 | { | |
251 | int retval; | |
252 | ||
253 | retval = comedi_driver_register(&driver_contec); | |
254 | if (retval < 0) | |
255 | return retval; | |
256 | ||
257 | driver_contec_pci_driver.name = (char *)driver_contec.driver_name; | |
258 | return pci_register_driver(&driver_contec_pci_driver); | |
259 | } | |
260 | ||
261 | static void __exit driver_contec_cleanup_module(void) | |
262 | { | |
263 | pci_unregister_driver(&driver_contec_pci_driver); | |
264 | comedi_driver_unregister(&driver_contec); | |
265 | } | |
266 | ||
267 | module_init(driver_contec_init_module); | |
268 | module_exit(driver_contec_cleanup_module); | |
90f703d3 AT |
269 | |
270 | MODULE_AUTHOR("Comedi http://www.comedi.org"); | |
271 | MODULE_DESCRIPTION("Comedi low-level driver"); | |
272 | MODULE_LICENSE("GPL"); |