adl_pci7230.c revision 90f703d30dd3e0c16ff80f35e34e511385a05ad5
1/*
2    comedi/drivers/adl_pci7230.c
3
4    Hardware comedi driver fot PCI7230 Adlink card
5    Copyright (C) 2010 David Fernandez <dfcastelao@gmail.com>
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/*
23Driver: adl_pci7230
24Description: Driver for the Adlink PCI-7230 32 ch. isolated digital io board
25Devices: [ADLink] PCI-7230 (adl_pci7230)
26Author: David Fernandez <dfcastelao@gmail.com>
27Status: experimental
28Updated: Mon, 14 Apr 2008 15:08:14 +0100
29
30Configuration 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#include <linux/kernel.h>
39#include "comedi_pci.h"
40
41#define PCI7230_DI      0x00
42#define PCI7230_DO	    0x00
43
44#define PCI_DEVICE_ID_PCI7230 0x7230
45
46static DEFINE_PCI_DEVICE_TABLE(adl_pci7230_pci_table) = {
47	{
48		PCI_VENDOR_ID_ADLINK,
49		PCI_DEVICE_ID_PCI7230,
50		PCI_ANY_ID,
51		PCI_ANY_ID,
52		0,
53		0,
54		0
55	},
56	{0}
57};
58
59MODULE_DEVICE_TABLE(pci, adl_pci7230_pci_table);
60
61struct adl_pci7230_private {
62	int data;
63	struct pci_dev *pci_dev;
64};
65
66#define devpriv ((struct adl_pci7230_private *)dev->private)
67
68static int adl_pci7230_attach(struct comedi_device *dev,
69	struct comedi_devconfig *it);
70static int adl_pci7230_detach(struct comedi_device *dev);
71static struct comedi_driver driver_adl_pci7230 = {
72	.driver_name = "adl_pci7230",
73	.module = THIS_MODULE,
74	.attach = adl_pci7230_attach,
75	.detach = adl_pci7230_detach,
76};
77
78/* Digital IO */
79
80static int adl_pci7230_di_insn_bits(struct comedi_device *dev,
81	struct comedi_subdevice *s,
82	struct comedi_insn *insn,
83	unsigned int *data);
84
85static int adl_pci7230_do_insn_bits(struct comedi_device *dev,
86	struct comedi_subdevice *s,
87	struct comedi_insn *insn,
88	unsigned int *data);
89
90static int adl_pci7230_attach(struct comedi_device *dev,
91	struct comedi_devconfig *it)
92{
93	struct pci_dev *pcidev;
94	struct comedi_subdevice *s;
95	int bus, slot;
96
97	printk(KERN_INFO "comedi%d: adl_pci7230\n", dev->minor);
98
99	dev->board_name = "pci7230";
100	bus = it->options[0];
101	slot = it->options[1];
102
103	if (alloc_private(dev, sizeof(struct adl_pci7230_private)) < 0)
104		return -ENOMEM;
105
106	if (alloc_subdevices(dev, 2) < 0)
107		return -ENOMEM;
108
109	for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
110		pcidev != NULL;
111		pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
112
113		if (pcidev->vendor == PCI_VENDOR_ID_ADLINK &&
114			pcidev->device == PCI_DEVICE_ID_PCI7230) {
115			if (bus || slot) {
116				/* requested particular bus/slot */
117				if (pcidev->bus->number != bus ||
118					PCI_SLOT(pcidev->devfn) != slot) {
119					continue;
120				}
121			}
122			devpriv->pci_dev = pcidev;
123			break;
124		}
125	}
126	if (pcidev == NULL) {
127		printk(KERN_ERR "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
128			dev->minor, bus, slot);
129		return -EIO;
130	}
131	if (comedi_pci_enable(pcidev, "adl_pci7230") < 0) {
132		printk(KERN_ERR "comedi%d: Failed to enable PCI device and request regions\n",
133			dev->minor);
134		return -EIO;
135	}
136	dev->iobase = pci_resource_start(pcidev, 2);
137	printk(KERN_DEBUG "comedi: base addr %4lx\n", dev->iobase);
138
139	s = dev->subdevices + 0;
140	/* Isolated do */
141	s->type = COMEDI_SUBD_DO;
142	s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
143	s->n_chan = 16;
144	s->maxdata = 1;
145	s->range_table = &range_digital;
146	s->insn_bits = adl_pci7230_do_insn_bits;
147
148	s = dev->subdevices + 1;
149	/* Isolated di */
150	s->type = COMEDI_SUBD_DI;
151	s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_COMMON;
152	s->n_chan = 16;
153	s->maxdata = 1;
154	s->range_table = &range_digital;
155	s->insn_bits = adl_pci7230_di_insn_bits;
156
157	printk(KERN_DEBUG "comedi: attached\n");
158
159	return 1;
160}
161
162static int adl_pci7230_detach(struct comedi_device *dev)
163{
164	printk(KERN_DEBUG "comedi%d: pci7230: remove\n", dev->minor);
165
166	if (devpriv && devpriv->pci_dev) {
167		if (dev->iobase)
168			comedi_pci_disable(devpriv->pci_dev);
169		pci_dev_put(devpriv->pci_dev);
170	}
171
172	return 0;
173}
174
175static int adl_pci7230_do_insn_bits(struct comedi_device *dev,
176	struct comedi_subdevice *s,
177	struct comedi_insn *insn,
178	unsigned int *data)
179{
180	if (insn->n != 2)
181		return -EINVAL;
182
183	if (data[0]) {
184		s->state &= ~data[0];
185		s->state |= (data[0] & data[1]);
186
187		outl((s->state  << 16) & 0xffffffff, dev->iobase + PCI7230_DO);
188	}
189
190	return 2;
191}
192
193static int adl_pci7230_di_insn_bits(struct comedi_device *dev,
194	struct comedi_subdevice *s,
195	struct comedi_insn *insn,
196	unsigned int *data)
197{
198	if (insn->n != 2)
199		return -EINVAL;
200
201	data[1] = inl(dev->iobase + PCI7230_DI) & 0xffffffff;
202
203	return 2;
204}
205
206COMEDI_PCI_INITCLEANUP(driver_adl_pci7230, adl_pci7230_pci_table);
207
208MODULE_AUTHOR("Comedi http://www.comedi.org");
209MODULE_DESCRIPTION("Comedi low-level driver");
210MODULE_LICENSE("GPL");
211