adl_pci7432.c revision 8c0690eaf7a666183fb9c49d275d9f986acd4239
1/*
2    comedi/drivers/adl_pci7432.c
3
4    Hardware comedi driver fot PCI7432 Adlink card
5    Copyright (C) 2004 Michel Lachine <mike@mikelachaine.ca>
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_pci7432
24Description: Driver for the Adlink PCI-7432 64 ch. isolated digital io board
25Devices: [ADLink] PCI-7432 (adl_pci7432)
26Author: Michel Lachaine <mike@mikelachaine.ca>
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 PCI7432_DI      0x00
42#define PCI7432_DO	    0x00
43
44#define PCI_DEVICE_ID_PCI7432 0x7432
45
46static DEFINE_PCI_DEVICE_TABLE(adl_pci7432_pci_table) = {
47	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI7432) },
48	{0}
49};
50
51MODULE_DEVICE_TABLE(pci, adl_pci7432_pci_table);
52
53struct adl_pci7432_private {
54	int data;
55	struct pci_dev *pci_dev;
56};
57
58#define devpriv ((struct adl_pci7432_private *)dev->private)
59
60static int adl_pci7432_attach(struct comedi_device *dev,
61			      struct comedi_devconfig *it);
62static int adl_pci7432_detach(struct comedi_device *dev);
63static struct comedi_driver driver_adl_pci7432 = {
64	.driver_name = "adl_pci7432",
65	.module = THIS_MODULE,
66	.attach = adl_pci7432_attach,
67	.detach = adl_pci7432_detach,
68};
69
70/* Digital IO */
71
72static int adl_pci7432_di_insn_bits(struct comedi_device *dev,
73				    struct comedi_subdevice *s,
74				    struct comedi_insn *insn,
75				    unsigned int *data);
76
77static int adl_pci7432_do_insn_bits(struct comedi_device *dev,
78				    struct comedi_subdevice *s,
79				    struct comedi_insn *insn,
80				    unsigned int *data);
81
82/*            */
83
84static int adl_pci7432_attach(struct comedi_device *dev,
85			      struct comedi_devconfig *it)
86{
87	struct pci_dev *pcidev = NULL;
88	struct comedi_subdevice *s;
89	int bus, slot;
90
91	printk(KERN_INFO "comedi%d: attach adl_pci7432\n", dev->minor);
92
93	dev->board_name = "pci7432";
94	bus = it->options[0];
95	slot = it->options[1];
96
97	if (alloc_private(dev, sizeof(struct adl_pci7432_private)) < 0)
98		return -ENOMEM;
99
100	if (alloc_subdevices(dev, 2) < 0)
101		return -ENOMEM;
102
103	for_each_pci_dev(pcidev) {
104		if (pcidev->vendor == PCI_VENDOR_ID_ADLINK &&
105		    pcidev->device == PCI_DEVICE_ID_PCI7432) {
106			if (bus || slot) {
107				/* requested particular bus/slot */
108				if (pcidev->bus->number != bus
109				    || PCI_SLOT(pcidev->devfn) != slot) {
110					continue;
111				}
112			}
113			devpriv->pci_dev = pcidev;
114			if (comedi_pci_enable(pcidev, "adl_pci7432") < 0) {
115				printk(KERN_ERR "comedi%d: Failed to enable PCI device and request regions\n",
116				     dev->minor);
117				return -EIO;
118			}
119			dev->iobase = pci_resource_start(pcidev, 2);
120			printk(KERN_INFO "comedi: base addr %4lx\n",
121				dev->iobase);
122
123			s = dev->subdevices + 0;
124			s->type = COMEDI_SUBD_DI;
125			s->subdev_flags =
126			    SDF_READABLE | SDF_GROUND | SDF_COMMON;
127			s->n_chan = 32;
128			s->maxdata = 1;
129			s->len_chanlist = 32;
130			s->io_bits = 0x00000000;
131			s->range_table = &range_digital;
132			s->insn_bits = adl_pci7432_di_insn_bits;
133
134			s = dev->subdevices + 1;
135			s->type = COMEDI_SUBD_DO;
136			s->subdev_flags =
137			    SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
138			s->n_chan = 32;
139			s->maxdata = 1;
140			s->len_chanlist = 32;
141			s->io_bits = 0xffffffff;
142			s->range_table = &range_digital;
143			s->insn_bits = adl_pci7432_do_insn_bits;
144
145			printk(KERN_DEBUG "comedi%d: adl_pci7432 attached\n",
146				dev->minor);
147			return 1;
148		}
149	}
150
151	printk(KERN_ERR "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
152	       dev->minor, bus, slot);
153	return -EIO;
154}
155
156static int adl_pci7432_detach(struct comedi_device *dev)
157{
158	printk(KERN_INFO "comedi%d: pci7432: remove\n", dev->minor);
159
160	if (devpriv && devpriv->pci_dev) {
161		if (dev->iobase)
162			comedi_pci_disable(devpriv->pci_dev);
163		pci_dev_put(devpriv->pci_dev);
164	}
165
166	return 0;
167}
168
169static int adl_pci7432_do_insn_bits(struct comedi_device *dev,
170				    struct comedi_subdevice *s,
171				    struct comedi_insn *insn,
172				    unsigned int *data)
173{
174	printk(KERN_DEBUG "comedi: pci7432_do_insn_bits called\n");
175	printk(KERN_DEBUG "comedi: data0: %8x data1: %8x\n", data[0], data[1]);
176
177	if (insn->n != 2)
178		return -EINVAL;
179
180	if (data[0]) {
181		s->state &= ~data[0];
182		s->state |= (data[0] & data[1]);
183
184		printk(KERN_DEBUG "comedi: out: %8x on iobase %4lx\n", s->state,
185		       dev->iobase + PCI7432_DO);
186		outl(s->state & 0xffffffff, dev->iobase + PCI7432_DO);
187	}
188	return 2;
189}
190
191static int adl_pci7432_di_insn_bits(struct comedi_device *dev,
192				    struct comedi_subdevice *s,
193				    struct comedi_insn *insn,
194				    unsigned int *data)
195{
196	printk(KERN_DEBUG "comedi: pci7432_di_insn_bits called\n");
197	printk(KERN_DEBUG "comedi: data0: %8x data1: %8x\n", data[0], data[1]);
198
199	if (insn->n != 2)
200		return -EINVAL;
201
202	data[1] = inl(dev->iobase + PCI7432_DI) & 0xffffffff;
203	printk(KERN_DEBUG "comedi: data1 %8x\n", data[1]);
204
205	return 2;
206}
207
208static int __devinit driver_adl_pci7432_pci_probe(struct pci_dev *dev,
209						  const struct pci_device_id
210						  *ent)
211{
212	return comedi_pci_auto_config(dev, driver_adl_pci7432.driver_name);
213}
214
215static void __devexit driver_adl_pci7432_pci_remove(struct pci_dev *dev)
216{
217	comedi_pci_auto_unconfig(dev);
218}
219
220static struct pci_driver driver_adl_pci7432_pci_driver = {
221	.id_table = adl_pci7432_pci_table,
222	.probe = &driver_adl_pci7432_pci_probe,
223	.remove = __devexit_p(&driver_adl_pci7432_pci_remove)
224};
225
226static int __init driver_adl_pci7432_init_module(void)
227{
228	int retval;
229
230	retval = comedi_driver_register(&driver_adl_pci7432);
231	if (retval < 0)
232		return retval;
233
234	driver_adl_pci7432_pci_driver.name =
235	    (char *)driver_adl_pci7432.driver_name;
236	return pci_register_driver(&driver_adl_pci7432_pci_driver);
237}
238
239static void __exit driver_adl_pci7432_cleanup_module(void)
240{
241	pci_unregister_driver(&driver_adl_pci7432_pci_driver);
242	comedi_driver_unregister(&driver_adl_pci7432);
243}
244
245module_init(driver_adl_pci7432_init_module);
246module_exit(driver_adl_pci7432_cleanup_module);
247
248MODULE_AUTHOR("Comedi http://www.comedi.org");
249MODULE_DESCRIPTION("Comedi low-level driver");
250MODULE_LICENSE("GPL");
251