adl_pci7296.c revision 8c0690eaf7a666183fb9c49d275d9f986acd4239
1/*
2    comedi/drivers/adl_pci7296.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/*
23Driver: adl_pci7296
24Description: Driver for the Adlink PCI-7296 96 ch. digital io board
25Devices: [ADLink] PCI-7296 (adl_pci7296)
26Author: Jon Grierson <jd@renko.co.uk>
27Updated: Mon, 14 Apr 2008 15:05:56 +0100
28Status: testing
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
40#include "comedi_pci.h"
41#include "8255.h"
42/* #include "8253.h" */
43
44#define PORT1A 0
45#define PORT2A 4
46#define PORT3A 8
47#define PORT4A 12
48
49#define PCI_DEVICE_ID_PCI7296 0x7296
50
51static DEFINE_PCI_DEVICE_TABLE(adl_pci7296_pci_table) = {
52	{ PCI_DEVICE(PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI7296) },
53	{0}
54};
55
56MODULE_DEVICE_TABLE(pci, adl_pci7296_pci_table);
57
58struct adl_pci7296_private {
59	int data;
60	struct pci_dev *pci_dev;
61};
62
63#define devpriv ((struct adl_pci7296_private *)dev->private)
64
65static int adl_pci7296_attach(struct comedi_device *dev,
66			      struct comedi_devconfig *it);
67static int adl_pci7296_detach(struct comedi_device *dev);
68static struct comedi_driver driver_adl_pci7296 = {
69	.driver_name = "adl_pci7296",
70	.module = THIS_MODULE,
71	.attach = adl_pci7296_attach,
72	.detach = adl_pci7296_detach,
73};
74
75static int adl_pci7296_attach(struct comedi_device *dev,
76			      struct comedi_devconfig *it)
77{
78	struct pci_dev *pcidev = NULL;
79	struct comedi_subdevice *s;
80	int bus, slot;
81	int ret;
82
83	printk(KERN_INFO "comedi%d: attach adl_pci7432\n", dev->minor);
84
85	dev->board_name = "pci7432";
86	bus = it->options[0];
87	slot = it->options[1];
88
89	if (alloc_private(dev, sizeof(struct adl_pci7296_private)) < 0)
90		return -ENOMEM;
91
92	if (alloc_subdevices(dev, 4) < 0)
93		return -ENOMEM;
94
95	for_each_pci_dev(pcidev) {
96		if (pcidev->vendor == PCI_VENDOR_ID_ADLINK &&
97		    pcidev->device == PCI_DEVICE_ID_PCI7296) {
98			if (bus || slot) {
99				/* requested particular bus/slot */
100				if (pcidev->bus->number != bus
101				    || PCI_SLOT(pcidev->devfn) != slot) {
102					continue;
103				}
104			}
105			devpriv->pci_dev = pcidev;
106			if (comedi_pci_enable(pcidev, "adl_pci7296") < 0) {
107				printk(KERN_ERR "comedi%d: Failed to enable PCI device and request regions\n",
108				     dev->minor);
109				return -EIO;
110			}
111
112			dev->iobase = pci_resource_start(pcidev, 2);
113			printk(KERN_INFO "comedi: base addr %4lx\n",
114				dev->iobase);
115
116			/*  four 8255 digital io subdevices */
117			s = dev->subdevices + 0;
118			subdev_8255_init(dev, s, NULL,
119					 (unsigned long)(dev->iobase));
120
121			s = dev->subdevices + 1;
122			ret = subdev_8255_init(dev, s, NULL,
123					       (unsigned long)(dev->iobase +
124							       PORT2A));
125			if (ret < 0)
126				return ret;
127
128			s = dev->subdevices + 2;
129			ret = subdev_8255_init(dev, s, NULL,
130					       (unsigned long)(dev->iobase +
131							       PORT3A));
132			if (ret < 0)
133				return ret;
134
135			s = dev->subdevices + 3;
136			ret = subdev_8255_init(dev, s, NULL,
137					       (unsigned long)(dev->iobase +
138							       PORT4A));
139			if (ret < 0)
140				return ret;
141
142			printk(KERN_DEBUG "comedi%d: adl_pci7432 attached\n",
143				dev->minor);
144
145			return 1;
146		}
147	}
148
149	printk(KERN_ERR "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
150	       dev->minor, bus, slot);
151	return -EIO;
152}
153
154static int adl_pci7296_detach(struct comedi_device *dev)
155{
156	printk(KERN_INFO "comedi%d: pci7432: remove\n", dev->minor);
157
158	if (devpriv && devpriv->pci_dev) {
159		if (dev->iobase)
160			comedi_pci_disable(devpriv->pci_dev);
161		pci_dev_put(devpriv->pci_dev);
162	}
163	/*  detach four 8255 digital io subdevices */
164	if (dev->subdevices) {
165		subdev_8255_cleanup(dev, dev->subdevices + 0);
166		subdev_8255_cleanup(dev, dev->subdevices + 1);
167		subdev_8255_cleanup(dev, dev->subdevices + 2);
168		subdev_8255_cleanup(dev, dev->subdevices + 3);
169
170	}
171
172	return 0;
173}
174
175static int __devinit driver_adl_pci7296_pci_probe(struct pci_dev *dev,
176						  const struct pci_device_id
177						  *ent)
178{
179	return comedi_pci_auto_config(dev, driver_adl_pci7296.driver_name);
180}
181
182static void __devexit driver_adl_pci7296_pci_remove(struct pci_dev *dev)
183{
184	comedi_pci_auto_unconfig(dev);
185}
186
187static struct pci_driver driver_adl_pci7296_pci_driver = {
188	.id_table = adl_pci7296_pci_table,
189	.probe = &driver_adl_pci7296_pci_probe,
190	.remove = __devexit_p(&driver_adl_pci7296_pci_remove)
191};
192
193static int __init driver_adl_pci7296_init_module(void)
194{
195	int retval;
196
197	retval = comedi_driver_register(&driver_adl_pci7296);
198	if (retval < 0)
199		return retval;
200
201	driver_adl_pci7296_pci_driver.name =
202	    (char *)driver_adl_pci7296.driver_name;
203	return pci_register_driver(&driver_adl_pci7296_pci_driver);
204}
205
206static void __exit driver_adl_pci7296_cleanup_module(void)
207{
208	pci_unregister_driver(&driver_adl_pci7296_pci_driver);
209	comedi_driver_unregister(&driver_adl_pci7296);
210}
211
212module_init(driver_adl_pci7296_init_module);
213module_exit(driver_adl_pci7296_cleanup_module);
214
215MODULE_AUTHOR("Comedi http://www.comedi.org");
216MODULE_DESCRIPTION("Comedi low-level driver");
217MODULE_LICENSE("GPL");
218