staging: comedi: propogate error code from comedi_alloc_subdevices
[deliverable/linux.git] / drivers / staging / comedi / drivers / ke_counter.c
CommitLineData
2f82613d
MH
1/*
2 comedi/drivers/ke_counter.c
3 Comedi driver for Kolter-Electronic PCI Counter 1 Card
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23/*
24Driver: ke_counter
25Description: Driver for Kolter Electronic Counter Card
26Devices: [Kolter Electronic] PCI Counter Card (ke_counter)
27Author: Michael Hillmann
28Updated: Mon, 14 Apr 2008 15:42:42 +0100
29Status: tested
30
31Configuration Options:
32 [0] - PCI bus of device (optional)
33 [1] - PCI slot of device (optional)
34 If bus/slot is not specified, the first supported
35 PCI device found will be used.
36
37This driver is a simple driver to read the counter values from
38Kolter Electronic PCI Counter Card.
39*/
40
41#include "../comedidev.h"
42
2f82613d
MH
43#define CNT_DRIVER_NAME "ke_counter"
44#define PCI_VENDOR_ID_KOLTER 0x1001
45#define CNT_CARD_DEVICE_ID 0x0014
46
2f82613d
MH
47/*-- board specification structure ------------------------------------------*/
48
9beff277
BP
49struct cnt_board_struct {
50
2f82613d
MH
51 const char *name;
52 int device_id;
53 int cnt_channel_nbr;
54 int cnt_bits;
9beff277
BP
55};
56
9beff277 57static const struct cnt_board_struct cnt_boards[] = {
2f82613d 58 {
0a85b6f0
MT
59 .name = CNT_DRIVER_NAME,
60 .device_id = CNT_CARD_DEVICE_ID,
61 .cnt_channel_nbr = 3,
62 .cnt_bits = 24}
2f82613d
MH
63};
64
9beff277 65#define cnt_board_nbr (sizeof(cnt_boards)/sizeof(struct cnt_board_struct))
2f82613d
MH
66
67/*-- device private structure -----------------------------------------------*/
68
2e2269f9
BP
69struct cnt_device_private {
70
2f82613d 71 struct pci_dev *pcidev;
2e2269f9
BP
72};
73
2e2269f9 74#define devpriv ((struct cnt_device_private *)dev->private)
2f82613d 75
2f82613d
MH
76/*-- counter write ----------------------------------------------------------*/
77
78/* This should be used only for resetting the counters; maybe it is better
79 to make a special command 'reset'. */
da91b269 80static int cnt_winsn(struct comedi_device *dev,
0a85b6f0
MT
81 struct comedi_subdevice *s, struct comedi_insn *insn,
82 unsigned int *data)
2f82613d
MH
83{
84 int chan = CR_CHAN(insn->chanspec);
85
86 outb((unsigned char)((data[0] >> 24) & 0xff),
0a85b6f0 87 dev->iobase + chan * 0x20 + 0x10);
2f82613d 88 outb((unsigned char)((data[0] >> 16) & 0xff),
0a85b6f0 89 dev->iobase + chan * 0x20 + 0x0c);
2f82613d 90 outb((unsigned char)((data[0] >> 8) & 0xff),
0a85b6f0 91 dev->iobase + chan * 0x20 + 0x08);
2f82613d 92 outb((unsigned char)((data[0] >> 0) & 0xff),
0a85b6f0 93 dev->iobase + chan * 0x20 + 0x04);
2f82613d
MH
94
95 /* return the number of samples written */
96 return 1;
97}
98
99/*-- counter read -----------------------------------------------------------*/
100
da91b269 101static int cnt_rinsn(struct comedi_device *dev,
0a85b6f0
MT
102 struct comedi_subdevice *s, struct comedi_insn *insn,
103 unsigned int *data)
2f82613d
MH
104{
105 unsigned char a0, a1, a2, a3, a4;
106 int chan = CR_CHAN(insn->chanspec);
107 int result;
108
109 a0 = inb(dev->iobase + chan * 0x20);
110 a1 = inb(dev->iobase + chan * 0x20 + 0x04);
111 a2 = inb(dev->iobase + chan * 0x20 + 0x08);
112 a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
113 a4 = inb(dev->iobase + chan * 0x20 + 0x10);
114
115 result = (a1 + (a2 * 256) + (a3 * 65536));
116 if (a4 > 0)
117 result = result - s->maxdata;
118
0a85b6f0 119 *data = (unsigned int)result;
2f82613d
MH
120
121 /* return the number of samples read */
122 return 1;
123}
124
da91b269 125static int cnt_attach(struct comedi_device *dev, struct comedi_devconfig *it)
2f82613d 126{
34c43922 127 struct comedi_subdevice *subdevice;
20fb2280 128 struct pci_dev *pci_device = NULL;
9beff277 129 struct cnt_board_struct *board;
2f82613d
MH
130 unsigned long io_base;
131 int error, i;
132
133 /* allocate device private structure */
c3744138
BP
134 error = alloc_private(dev, sizeof(struct cnt_device_private));
135 if (error < 0)
2f82613d 136 return error;
2f82613d
MH
137
138 /* Probe the device to determine what device in the series it is. */
20fb2280 139 for_each_pci_dev(pci_device) {
2f82613d
MH
140 if (pci_device->vendor == PCI_VENDOR_ID_KOLTER) {
141 for (i = 0; i < cnt_board_nbr; i++) {
142 if (cnt_boards[i].device_id ==
0a85b6f0 143 pci_device->device) {
2f82613d
MH
144 /* was a particular bus/slot requested? */
145 if ((it->options[0] != 0)
0a85b6f0 146 || (it->options[1] != 0)) {
2f82613d
MH
147 /* are we on the wrong bus/slot? */
148 if (pci_device->bus->number !=
0a85b6f0
MT
149 it->options[0]
150 ||
151 PCI_SLOT(pci_device->devfn)
152 != it->options[1]) {
2f82613d
MH
153 continue;
154 }
155 }
156
157 dev->board_ptr = cnt_boards + i;
0a85b6f0
MT
158 board =
159 (struct cnt_board_struct *)
160 dev->board_ptr;
2f82613d
MH
161 goto found;
162 }
163 }
164 }
165 }
26ac8785
DH
166 printk(KERN_WARNING
167 "comedi%d: no supported board found! (req. bus/slot: %d/%d)\n",
0a85b6f0 168 dev->minor, it->options[0], it->options[1]);
2f82613d
MH
169 return -EIO;
170
0a85b6f0 171found:
26ac8785
DH
172 printk(KERN_INFO
173 "comedi%d: found %s at PCI bus %d, slot %d\n", dev->minor,
0a85b6f0
MT
174 board->name, pci_device->bus->number,
175 PCI_SLOT(pci_device->devfn));
2f82613d
MH
176 devpriv->pcidev = pci_device;
177 dev->board_name = board->name;
178
179 /* enable PCI device and request regions */
c3744138
BP
180 error = comedi_pci_enable(pci_device, CNT_DRIVER_NAME);
181 if (error < 0) {
26ac8785
DH
182 printk(KERN_WARNING "comedi%d: "
183 "failed to enable PCI device and request regions!\n",
184 dev->minor);
2f82613d
MH
185 return error;
186 }
187
188 /* read register base address [PCI_BASE_ADDRESS #0] */
189 io_base = pci_resource_start(pci_device, 0);
190 dev->iobase = io_base;
191
2f0b9d08 192 error = comedi_alloc_subdevices(dev, 1);
8b6c5694 193 if (error)
2f82613d 194 return error;
2f82613d
MH
195
196 subdevice = dev->subdevices + 0;
197 dev->read_subdev = subdevice;
198
199 subdevice->type = COMEDI_SUBD_COUNTER;
200 subdevice->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
201 subdevice->n_chan = board->cnt_channel_nbr;
202 subdevice->maxdata = (1 << board->cnt_bits) - 1;
203 subdevice->insn_read = cnt_rinsn;
204 subdevice->insn_write = cnt_winsn;
205
2696fb57 206 /* select 20MHz clock */
2f82613d
MH
207 outb(3, dev->iobase + 248);
208
2696fb57 209 /* reset all counters */
2f82613d
MH
210 outb(0, dev->iobase);
211 outb(0, dev->iobase + 0x20);
212 outb(0, dev->iobase + 0x40);
213
26ac8785
DH
214 printk(KERN_INFO "comedi%d: " CNT_DRIVER_NAME " attached.\n",
215 dev->minor);
2f82613d
MH
216 return 0;
217}
218
484ecc95 219static void cnt_detach(struct comedi_device *dev)
2f82613d
MH
220{
221 if (devpriv && devpriv->pcidev) {
26ac8785 222 if (dev->iobase)
2f82613d 223 comedi_pci_disable(devpriv->pcidev);
2f82613d
MH
224 pci_dev_put(devpriv->pcidev);
225 }
2f82613d 226}
90f703d3 227
75e6301b
HS
228static struct comedi_driver ke_counter_driver = {
229 .driver_name = "ke_counter",
236788fc
HS
230 .module = THIS_MODULE,
231 .attach = cnt_attach,
232 .detach = cnt_detach,
233};
234
75e6301b 235static int __devinit ke_counter_pci_probe(struct pci_dev *dev,
236788fc
HS
236 const struct pci_device_id *ent)
237{
75e6301b 238 return comedi_pci_auto_config(dev, &ke_counter_driver);
236788fc
HS
239}
240
75e6301b 241static void __devexit ke_counter_pci_remove(struct pci_dev *dev)
236788fc
HS
242{
243 comedi_pci_auto_unconfig(dev);
244}
245
75e6301b 246static DEFINE_PCI_DEVICE_TABLE(ke_counter_pci_table) = {
236788fc
HS
247 { PCI_DEVICE(PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID) },
248 { 0 }
249};
75e6301b 250MODULE_DEVICE_TABLE(pci, ke_counter_pci_table);
236788fc 251
75e6301b
HS
252static struct pci_driver ke_counter_pci_driver = {
253 .name = "ke_counter",
254 .id_table = ke_counter_pci_table,
255 .probe = ke_counter_pci_probe,
256 .remove = __devexit_p(ke_counter_pci_remove),
236788fc 257};
75e6301b 258module_comedi_pci_driver(ke_counter_driver, ke_counter_pci_driver);
236788fc 259
90f703d3
AT
260MODULE_AUTHOR("Comedi http://www.comedi.org");
261MODULE_DESCRIPTION("Comedi low-level driver");
262MODULE_LICENSE("GPL");
This page took 0.328684 seconds and 5 git commands to generate.