adl_pci7432.c revision 0a85b6f0ab0d2edb0d41b32697111ce0e4f43496
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	{
48	PCI_VENDOR_ID_ADLINK, PCI_DEVICE_ID_PCI7432, PCI_ANY_ID,
49		    PCI_ANY_ID, 0, 0, 0}, {
50	0}
51};
52
53MODULE_DEVICE_TABLE(pci, adl_pci7432_pci_table);
54
55struct adl_pci7432_private {
56	int data;
57	struct pci_dev *pci_dev;
58};
59
60#define devpriv ((struct adl_pci7432_private *)dev->private)
61
62static int adl_pci7432_attach(struct comedi_device *dev,
63			      struct comedi_devconfig *it);
64static int adl_pci7432_detach(struct comedi_device *dev);
65static struct comedi_driver driver_adl_pci7432 = {
66	.driver_name = "adl_pci7432",
67	.module = THIS_MODULE,
68	.attach = adl_pci7432_attach,
69	.detach = adl_pci7432_detach,
70};
71
72/* Digital IO */
73
74static int adl_pci7432_di_insn_bits(struct comedi_device *dev,
75				    struct comedi_subdevice *s,
76				    struct comedi_insn *insn,
77				    unsigned int *data);
78
79static int adl_pci7432_do_insn_bits(struct comedi_device *dev,
80				    struct comedi_subdevice *s,
81				    struct comedi_insn *insn,
82				    unsigned int *data);
83
84/*            */
85
86static int adl_pci7432_attach(struct comedi_device *dev,
87			      struct comedi_devconfig *it)
88{
89	struct pci_dev *pcidev;
90	struct comedi_subdevice *s;
91	int bus, slot;
92
93	printk("comedi: attempt to attach...\n");
94	printk("comedi%d: adl_pci7432\n", dev->minor);
95
96	dev->board_name = "pci7432";
97	bus = it->options[0];
98	slot = it->options[1];
99
100	if (alloc_private(dev, sizeof(struct adl_pci7432_private)) < 0)
101		return -ENOMEM;
102
103	if (alloc_subdevices(dev, 2) < 0)
104		return -ENOMEM;
105
106	for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
107	     pcidev != NULL;
108	     pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
109
110		if (pcidev->vendor == PCI_VENDOR_ID_ADLINK &&
111		    pcidev->device == PCI_DEVICE_ID_PCI7432) {
112			if (bus || slot) {
113				/* requested particular bus/slot */
114				if (pcidev->bus->number != bus
115				    || PCI_SLOT(pcidev->devfn) != slot) {
116					continue;
117				}
118			}
119			devpriv->pci_dev = pcidev;
120			if (comedi_pci_enable(pcidev, "adl_pci7432") < 0) {
121				printk
122				    ("comedi%d: Failed to enable PCI device and request regions\n",
123				     dev->minor);
124				return -EIO;
125			}
126			dev->iobase = pci_resource_start(pcidev, 2);
127			printk("comedi: base addr %4lx\n", dev->iobase);
128
129			s = dev->subdevices + 0;
130			s->type = COMEDI_SUBD_DI;
131			s->subdev_flags =
132			    SDF_READABLE | SDF_GROUND | SDF_COMMON;
133			s->n_chan = 32;
134			s->maxdata = 1;
135			s->len_chanlist = 32;
136			s->io_bits = 0x00000000;
137			s->range_table = &range_digital;
138			s->insn_bits = adl_pci7432_di_insn_bits;
139
140			s = dev->subdevices + 1;
141			s->type = COMEDI_SUBD_DO;
142			s->subdev_flags =
143			    SDF_WRITABLE | SDF_GROUND | SDF_COMMON;
144			s->n_chan = 32;
145			s->maxdata = 1;
146			s->len_chanlist = 32;
147			s->io_bits = 0xffffffff;
148			s->range_table = &range_digital;
149			s->insn_bits = adl_pci7432_do_insn_bits;
150
151			printk("comedi: attached\n");
152
153			return 1;
154		}
155	}
156
157	printk("comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
158	       dev->minor, bus, slot);
159	return -EIO;
160}
161
162static int adl_pci7432_detach(struct comedi_device *dev)
163{
164	printk("comedi%d: pci7432: remove\n", dev->minor);
165
166	if (devpriv && devpriv->pci_dev) {
167		if (dev->iobase) {
168			comedi_pci_disable(devpriv->pci_dev);
169		}
170		pci_dev_put(devpriv->pci_dev);
171	}
172
173	return 0;
174}
175
176static int adl_pci7432_do_insn_bits(struct comedi_device *dev,
177				    struct comedi_subdevice *s,
178				    struct comedi_insn *insn,
179				    unsigned int *data)
180{
181	printk("comedi: pci7432_do_insn_bits called\n");
182	printk("comedi: data0: %8x data1: %8x\n", data[0], data[1]);
183
184	if (insn->n != 2)
185		return -EINVAL;
186
187	if (data[0]) {
188		s->state &= ~data[0];
189		s->state |= (data[0] & data[1]);
190
191		printk("comedi: out: %8x on iobase %4lx\n", s->state,
192		       dev->iobase + PCI7432_DO);
193		outl(s->state & 0xffffffff, dev->iobase + PCI7432_DO);
194	}
195	return 2;
196}
197
198static int adl_pci7432_di_insn_bits(struct comedi_device *dev,
199				    struct comedi_subdevice *s,
200				    struct comedi_insn *insn,
201				    unsigned int *data)
202{
203	printk("comedi: pci7432_di_insn_bits called\n");
204	printk("comedi: data0: %8x data1: %8x\n", data[0], data[1]);
205
206	if (insn->n != 2)
207		return -EINVAL;
208
209	data[1] = inl(dev->iobase + PCI7432_DI) & 0xffffffff;
210	printk("comedi: data1 %8x\n", data[1]);
211
212	return 2;
213}
214
215COMEDI_PCI_INITCLEANUP(driver_adl_pci7432, adl_pci7432_pci_table);
216