staging: comedi: propogate error code from comedi_alloc_subdevices
[deliverable/linux.git] / drivers / staging / comedi / drivers / dyna_pci10xx.c
CommitLineData
16a7373a
PS
1/*
2 * comedi/drivers/dyna_pci10xx.c
3 * Copyright (C) 2011 Prashant Shah, pshah.mumbai@gmail.com
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20/*
21 Driver: dyna_pci10xx
22 Devices: Dynalog India PCI DAQ Cards, http://www.dynalogindia.com/
23 Author: Prashant Shah <pshah.mumbai@gmail.com>
24 Developed at Automation Labs, Chemical Dept., IIT Bombay, India.
25 Prof. Kannan Moudgalya <kannan@iitb.ac.in>
26 http://www.iitb.ac.in
27 Status: Stable
28 Version: 1.0
29 Device Supported :
30 - Dynalog PCI 1050
31
32 Notes :
33 - Dynalog India Pvt. Ltd. does not have a registered PCI Vendor ID and
34 they are using the PLX Technlogies Vendor ID since that is the PCI Chip used
35 in the card.
36 - Dynalog India Pvt. Ltd. has provided the internal register specification for
37 their cards in their manuals.
38*/
39
40#include "../comedidev.h"
16a7373a
PS
41#include <linux/mutex.h>
42
43#define PCI_VENDOR_ID_DYNALOG 0x10b5
44#define DRV_NAME "dyna_pci10xx"
45
46#define READ_TIMEOUT 50
47
48static DEFINE_MUTEX(start_stop_sem);
49
16a7373a
PS
50static const struct comedi_lrange range_pci1050_ai = { 3, {
51 BIP_RANGE(10),
52 BIP_RANGE(5),
53 UNI_RANGE(10)
54 }
55};
56
57static const char range_codes_pci1050_ai[] = { 0x00, 0x10, 0x30 };
58
59static const struct comedi_lrange range_pci1050_ao = { 1, {
60 UNI_RANGE(10)
61 }
62};
63
64static const char range_codes_pci1050_ao[] = { 0x00 };
65
66struct boardtype {
67 const char *name;
68 int device_id;
69 int ai_chans;
70 int ai_bits;
71 int ao_chans;
72 int ao_bits;
73 int di_chans;
74 int di_bits;
75 int do_chans;
76 int do_bits;
77 const struct comedi_lrange *range_ai;
78 const char *range_codes_ai;
79 const struct comedi_lrange *range_ao;
80 const char *range_codes_ao;
81};
82
83static const struct boardtype boardtypes[] = {
84 {
85 .name = "dyna_pci1050",
86 .device_id = 0x1050,
87 .ai_chans = 16,
88 .ai_bits = 12,
89 .ao_chans = 16,
90 .ao_bits = 12,
91 .di_chans = 16,
92 .di_bits = 16,
93 .do_chans = 16,
94 .do_bits = 16,
95 .range_ai = &range_pci1050_ai,
96 .range_codes_ai = range_codes_pci1050_ai,
97 .range_ao = &range_pci1050_ao,
98 .range_codes_ao = range_codes_pci1050_ao,
99 },
100 /* dummy entry corresponding to driver name */
101 {.name = DRV_NAME},
102};
103
16a7373a
PS
104struct dyna_pci10xx_private {
105 struct pci_dev *pci_dev; /* ptr to PCI device */
106 char valid; /* card is usable */
107 struct mutex mutex;
108
109 /* device base address registers */
110 unsigned long BADR0, BADR1, BADR2, BADR3, BADR4, BADR5;
111};
112
113#define thisboard ((const struct boardtype *)dev->board_ptr)
114#define devpriv ((struct dyna_pci10xx_private *)dev->private)
115
116/******************************************************************************/
117/************************** READ WRITE FUNCTIONS ******************************/
118/******************************************************************************/
119
120/* analog input callback */
121static int dyna_pci10xx_insn_read_ai(struct comedi_device *dev,
122 struct comedi_subdevice *s,
123 struct comedi_insn *insn, unsigned int *data)
124{
125 int n, counter;
126 u16 d = 0;
127 unsigned int chan, range;
128
129 /* get the channel number and range */
130 chan = CR_CHAN(insn->chanspec);
131 range = thisboard->range_codes_ai[CR_RANGE((insn->chanspec))];
132
133 mutex_lock(&devpriv->mutex);
134 /* convert n samples */
135 for (n = 0; n < insn->n; n++) {
136 /* trigger conversion */
137 smp_mb();
138 outw_p(0x0000 + range + chan, devpriv->BADR2 + 2);
139 udelay(10);
140 /* read data */
141 for (counter = 0; counter < READ_TIMEOUT; counter++) {
142 d = inw_p(devpriv->BADR2);
143
bc04bec0 144 /* check if read is successful if the EOC bit is set */
16a7373a
PS
145 if (d & (1 << 15))
146 goto conv_finish;
147 }
148 data[n] = 0;
149 printk(KERN_DEBUG "comedi: dyna_pci10xx: "
150 "timeout reading analog input\n");
151 continue;
152conv_finish:
153 /* mask the first 4 bits - EOC bits */
154 d &= 0x0FFF;
155 data[n] = d;
156 }
157 mutex_unlock(&devpriv->mutex);
158
159 /* return the number of samples read/written */
160 return n;
161}
162
163/* analog output callback */
164static int dyna_pci10xx_insn_write_ao(struct comedi_device *dev,
165 struct comedi_subdevice *s,
166 struct comedi_insn *insn, unsigned int *data)
167{
168 int n;
169 unsigned int chan, range;
170
171 chan = CR_CHAN(insn->chanspec);
172 range = thisboard->range_codes_ai[CR_RANGE((insn->chanspec))];
173
174 mutex_lock(&devpriv->mutex);
175 for (n = 0; n < insn->n; n++) {
176 smp_mb();
177 /* trigger conversion and write data */
178 outw_p(data[n], devpriv->BADR2);
179 udelay(10);
180 }
181 mutex_unlock(&devpriv->mutex);
182 return n;
183}
184
185/* digital input bit interface */
186static int dyna_pci10xx_di_insn_bits(struct comedi_device *dev,
187 struct comedi_subdevice *s,
188 struct comedi_insn *insn, unsigned int *data)
189{
190 u16 d = 0;
191
192 if (insn->n != 2)
193 return -EINVAL;
194
195 mutex_lock(&devpriv->mutex);
196 smp_mb();
197 d = inw_p(devpriv->BADR3);
198 udelay(10);
199
200 /* on return the data[0] contains output and data[1] contains input */
201 data[1] = d;
202 data[0] = s->state;
203 mutex_unlock(&devpriv->mutex);
204 return 2;
205}
206
207/* digital output bit interface */
208static int dyna_pci10xx_do_insn_bits(struct comedi_device *dev,
209 struct comedi_subdevice *s,
210 struct comedi_insn *insn, unsigned int *data)
211{
212 if (insn->n != 2)
213 return -EINVAL;
214
215 /* The insn data is a mask in data[0] and the new data
216 * in data[1], each channel cooresponding to a bit.
217 * s->state contains the previous write data
218 */
219 mutex_lock(&devpriv->mutex);
220 if (data[0]) {
221 s->state &= ~data[0];
222 s->state |= (data[0] & data[1]);
223 smp_mb();
224 outw_p(s->state, devpriv->BADR3);
225 udelay(10);
226 }
227
228 /*
229 * On return, data[1] contains the value of the digital
230 * input and output lines. We just return the software copy of the
231 * output values if it was a purely digital output subdevice.
232 */
233 data[1] = s->state;
234 mutex_unlock(&devpriv->mutex);
235 return 2;
236}
237
238/******************************************************************************/
239/*********************** INITIALIZATION FUNCTIONS *****************************/
240/******************************************************************************/
241
242static int dyna_pci10xx_attach(struct comedi_device *dev,
243 struct comedi_devconfig *it)
244{
245 struct comedi_subdevice *s;
246 struct pci_dev *pcidev;
247 unsigned int opt_bus, opt_slot;
248 int board_index, i;
8b6c5694 249 int ret;
16a7373a
PS
250
251 mutex_lock(&start_stop_sem);
252
253 if (alloc_private(dev, sizeof(struct dyna_pci10xx_private)) < 0) {
254 printk(KERN_ERR "comedi: dyna_pci10xx: "
255 "failed to allocate memory!\n");
256 mutex_unlock(&start_stop_sem);
257 return -ENOMEM;
258 }
259
260 opt_bus = it->options[0];
261 opt_slot = it->options[1];
262 dev->board_name = thisboard->name;
263 dev->irq = 0;
264
265 /*
266 * Probe the PCI bus and located the matching device
267 */
268 for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
269 pcidev != NULL;
270 pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
271
272 board_index = -1;
273 for (i = 0; i < ARRAY_SIZE(boardtypes); ++i) {
274 if ((pcidev->vendor == PCI_VENDOR_ID_DYNALOG) &&
275 (pcidev->device == boardtypes[i].device_id)) {
276 board_index = i;
277 break;
278 }
279 }
280 if (board_index < 0)
281 continue;
282
283 /* Found matching vendor/device. */
284 if (opt_bus || opt_slot) {
285 /* Check bus/slot. */
286 if (opt_bus != pcidev->bus->number
287 || opt_slot != PCI_SLOT(pcidev->devfn))
288 continue; /* no match */
289 }
290
291 goto found;
292 }
293 printk(KERN_ERR "comedi: dyna_pci10xx: no supported device found!\n");
294 mutex_unlock(&start_stop_sem);
295 return -EIO;
296
297found:
298
299 if (!pcidev) {
300 if (opt_bus || opt_slot) {
301 printk(KERN_ERR "comedi: dyna_pci10xx: "
302 "invalid PCI device at b:s %d:%d\n",
303 opt_bus, opt_slot);
304 } else {
305 printk(KERN_ERR "comedi: dyna_pci10xx: "
306 "invalid PCI device\n");
307 }
308 mutex_unlock(&start_stop_sem);
309 return -EIO;
310 }
311
312 if (comedi_pci_enable(pcidev, DRV_NAME)) {
313 printk(KERN_ERR "comedi: dyna_pci10xx: "
314 "failed to enable PCI device and request regions!");
315 mutex_unlock(&start_stop_sem);
316 return -EIO;
317 }
318
319 mutex_init(&devpriv->mutex);
320 dev->board_ptr = &boardtypes[board_index];
321 devpriv->pci_dev = pcidev;
322
323 printk(KERN_INFO "comedi: dyna_pci10xx: device found!\n");
324
325 /* initialize device base address registers */
326 devpriv->BADR0 = pci_resource_start(pcidev, 0);
327 devpriv->BADR1 = pci_resource_start(pcidev, 1);
328 devpriv->BADR2 = pci_resource_start(pcidev, 2);
329 devpriv->BADR3 = pci_resource_start(pcidev, 3);
330 devpriv->BADR4 = pci_resource_start(pcidev, 4);
331 devpriv->BADR5 = pci_resource_start(pcidev, 5);
332
8b6c5694
HS
333 ret = comedi_alloc_subdevices(dev, 4);
334 if (ret) {
16a7373a 335 mutex_unlock(&start_stop_sem);
8b6c5694 336 return ret;
16a7373a
PS
337 }
338
339 /* analog input */
340 s = dev->subdevices + 0;
341 s->type = COMEDI_SUBD_AI;
342 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
343 s->n_chan = thisboard->ai_chans;
344 s->maxdata = 0x0FFF;
345 s->range_table = thisboard->range_ai;
346 s->len_chanlist = 16;
347 s->insn_read = dyna_pci10xx_insn_read_ai;
348
349 /* analog output */
350 s = dev->subdevices + 1;
351 s->type = COMEDI_SUBD_AO;
352 s->subdev_flags = SDF_WRITABLE;
353 s->n_chan = thisboard->ao_chans;
354 s->maxdata = 0x0FFF;
355 s->range_table = thisboard->range_ao;
356 s->len_chanlist = 16;
357 s->insn_write = dyna_pci10xx_insn_write_ao;
358
359 /* digital input */
360 s = dev->subdevices + 2;
361 s->type = COMEDI_SUBD_DI;
362 s->subdev_flags = SDF_READABLE | SDF_GROUND;
363 s->n_chan = thisboard->di_chans;
364 s->maxdata = 1;
365 s->range_table = &range_digital;
366 s->len_chanlist = thisboard->di_chans;
367 s->insn_bits = dyna_pci10xx_di_insn_bits;
368
369 /* digital output */
370 s = dev->subdevices + 3;
371 s->type = COMEDI_SUBD_DO;
372 s->subdev_flags = SDF_WRITABLE | SDF_GROUND;
373 s->n_chan = thisboard->do_chans;
374 s->maxdata = 1;
375 s->range_table = &range_digital;
376 s->len_chanlist = thisboard->do_chans;
377 s->state = 0;
378 s->insn_bits = dyna_pci10xx_do_insn_bits;
379
380 devpriv->valid = 1;
381 mutex_unlock(&start_stop_sem);
382
383 printk(KERN_INFO "comedi: dyna_pci10xx: %s - device setup completed!\n",
384 boardtypes[board_index].name);
385
386 return 1;
387}
388
484ecc95 389static void dyna_pci10xx_detach(struct comedi_device *dev)
16a7373a
PS
390{
391 if (devpriv && devpriv->pci_dev) {
392 comedi_pci_disable(devpriv->pci_dev);
393 mutex_destroy(&devpriv->mutex);
394 }
16a7373a
PS
395}
396
75e6301b
HS
397static struct comedi_driver dyna_pci10xx_driver = {
398 .driver_name = "dyna_pci10xx",
de9f2db4
HS
399 .module = THIS_MODULE,
400 .attach = dyna_pci10xx_attach,
401 .detach = dyna_pci10xx_detach,
402 .board_name = &boardtypes[0].name,
403 .offset = sizeof(struct boardtype),
404 .num_names = ARRAY_SIZE(boardtypes),
405};
406
75e6301b
HS
407static int __devinit dyna_pci10xx_pci_probe(struct pci_dev *dev,
408 const struct pci_device_id *ent)
16a7373a 409{
75e6301b 410 return comedi_pci_auto_config(dev, &dyna_pci10xx_driver);
16a7373a
PS
411}
412
75e6301b 413static void __devexit dyna_pci10xx_pci_remove(struct pci_dev *dev)
16a7373a
PS
414{
415 comedi_pci_auto_unconfig(dev);
416}
417
de9f2db4
HS
418static DEFINE_PCI_DEVICE_TABLE(dyna_pci10xx_pci_table) = {
419 { PCI_DEVICE(PCI_VENDOR_ID_DYNALOG, 0x1050) },
420 { 0 }
421};
422MODULE_DEVICE_TABLE(pci, dyna_pci10xx_pci_table);
423
75e6301b
HS
424static struct pci_driver dyna_pci10xx_pci_driver = {
425 .name = "dyna_pci10xx",
de9f2db4 426 .id_table = dyna_pci10xx_pci_table,
75e6301b
HS
427 .probe = dyna_pci10xx_pci_probe,
428 .remove = __devexit_p(dyna_pci10xx_pci_remove),
16a7373a 429};
75e6301b 430module_comedi_pci_driver(dyna_pci10xx_driver, dyna_pci10xx_pci_driver);
16a7373a
PS
431
432MODULE_LICENSE("GPL");
433MODULE_AUTHOR("Prashant Shah <pshah.mumbai@gmail.com>");
434MODULE_DESCRIPTION("Comedi based drivers for Dynalog PCI DAQ cards");
This page took 0.109377 seconds and 5 git commands to generate.