contec_pci_dio.c revision 426054ce11ecb416048aa0f92628fa4ba93c3a0e
1/*
2    comedi/drivers/contec_pci_dio.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: contec_pci_dio
24Description: Contec PIO1616L digital I/O board
25Devices: [Contec] PIO1616L (contec_pci_dio)
26Author: Stefano Rivoir <s.rivoir@gts.it>
27Updated: Wed, 27 Jun 2007 13:00:06 +0100
28Status: works
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
39#include "comedi_pci.h"
40
41enum contec_model {
42	PIO1616L = 0,
43};
44
45struct contec_board {
46	const char *name;
47	int model;
48	int in_ports;
49	int out_ports;
50	int in_offs;
51	int out_offs;
52	int out_boffs;
53};
54static const struct contec_board contec_boards[] = {
55	{"PIO1616L", PIO1616L, 16, 16, 0, 2, 10},
56};
57
58#define PCI_DEVICE_ID_PIO1616L 0x8172
59static DEFINE_PCI_DEVICE_TABLE(contec_pci_table) = {
60	{
61	PCI_VENDOR_ID_CONTEC, PCI_DEVICE_ID_PIO1616L, PCI_ANY_ID,
62		    PCI_ANY_ID, 0, 0, PIO1616L}, {
63	0}
64};
65
66MODULE_DEVICE_TABLE(pci, contec_pci_table);
67
68#define thisboard ((const struct contec_board *)dev->board_ptr)
69
70struct contec_private {
71	int data;
72
73	struct pci_dev *pci_dev;
74
75};
76
77#define devpriv ((struct contec_private *)dev->private)
78
79static int contec_attach(struct comedi_device *dev,
80			 struct comedi_devconfig *it);
81static int contec_detach(struct comedi_device *dev);
82static struct comedi_driver driver_contec = {
83	.driver_name = "contec_pci_dio",
84	.module = THIS_MODULE,
85	.attach = contec_attach,
86	.detach = contec_detach,
87};
88
89/* Classic digital IO */
90static int contec_di_insn_bits(struct comedi_device *dev,
91			       struct comedi_subdevice *s,
92			       struct comedi_insn *insn, unsigned int *data);
93static int contec_do_insn_bits(struct comedi_device *dev,
94			       struct comedi_subdevice *s,
95			       struct comedi_insn *insn, unsigned int *data);
96
97#if 0
98static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
99			  struct comedi_cmd *cmd);
100
101static int contec_ns_to_timer(unsigned int *ns, int round);
102#endif
103
104static int contec_attach(struct comedi_device *dev, struct comedi_devconfig *it)
105{
106	struct pci_dev *pcidev;
107	struct comedi_subdevice *s;
108
109	printk("comedi%d: contec: ", dev->minor);
110
111	dev->board_name = thisboard->name;
112
113	if (alloc_private(dev, sizeof(struct contec_private)) < 0)
114		return -ENOMEM;
115
116	if (alloc_subdevices(dev, 2) < 0)
117		return -ENOMEM;
118
119	for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
120	     pcidev != NULL;
121	     pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
122
123		if (pcidev->vendor == PCI_VENDOR_ID_CONTEC &&
124		    pcidev->device == PCI_DEVICE_ID_PIO1616L) {
125			if (it->options[0] || it->options[1]) {
126				/* Check bus and slot. */
127				if (it->options[0] != pcidev->bus->number ||
128				    it->options[1] != PCI_SLOT(pcidev->devfn)) {
129					continue;
130				}
131			}
132			devpriv->pci_dev = pcidev;
133			if (comedi_pci_enable(pcidev, "contec_pci_dio")) {
134				printk
135				    ("error enabling PCI device and request regions!\n");
136				return -EIO;
137			}
138			dev->iobase = pci_resource_start(pcidev, 0);
139			printk(" base addr %lx ", dev->iobase);
140
141			dev->board_ptr = contec_boards + 0;
142
143			s = dev->subdevices + 0;
144
145			s->type = COMEDI_SUBD_DI;
146			s->subdev_flags = SDF_READABLE;
147			s->n_chan = 16;
148			s->maxdata = 1;
149			s->range_table = &range_digital;
150			s->insn_bits = contec_di_insn_bits;
151
152			s = dev->subdevices + 1;
153			s->type = COMEDI_SUBD_DO;
154			s->subdev_flags = SDF_WRITABLE;
155			s->n_chan = 16;
156			s->maxdata = 1;
157			s->range_table = &range_digital;
158			s->insn_bits = contec_do_insn_bits;
159
160			printk("attached\n");
161
162			return 1;
163		}
164	}
165
166	printk("card not present!\n");
167
168	return -EIO;
169}
170
171static int contec_detach(struct comedi_device *dev)
172{
173	printk("comedi%d: contec: remove\n", dev->minor);
174
175	if (devpriv && devpriv->pci_dev) {
176		if (dev->iobase)
177			comedi_pci_disable(devpriv->pci_dev);
178		pci_dev_put(devpriv->pci_dev);
179	}
180
181	return 0;
182}
183
184#if 0
185static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
186			  struct comedi_cmd *cmd)
187{
188	printk("contec_cmdtest called\n");
189	return 0;
190}
191
192static int contec_ns_to_timer(unsigned int *ns, int round)
193{
194	return *ns;
195}
196#endif
197
198static int contec_do_insn_bits(struct comedi_device *dev,
199			       struct comedi_subdevice *s,
200			       struct comedi_insn *insn, unsigned int *data)
201{
202
203	printk("contec_do_insn_bits called\n");
204	printk(" data: %d %d\n", data[0], data[1]);
205
206	if (insn->n != 2)
207		return -EINVAL;
208
209	if (data[0]) {
210		s->state &= ~data[0];
211		s->state |= data[0] & data[1];
212		printk("  out: %d on %lx\n", s->state,
213		       dev->iobase + thisboard->out_offs);
214		outw(s->state, dev->iobase + thisboard->out_offs);
215	}
216	return 2;
217}
218
219static int contec_di_insn_bits(struct comedi_device *dev,
220			       struct comedi_subdevice *s,
221			       struct comedi_insn *insn, unsigned int *data)
222{
223
224	printk("contec_di_insn_bits called\n");
225	printk(" data: %d %d\n", data[0], data[1]);
226
227	if (insn->n != 2)
228		return -EINVAL;
229
230	data[1] = inw(dev->iobase + thisboard->in_offs);
231
232	return 2;
233}
234
235COMEDI_PCI_INITCLEANUP(driver_contec, contec_pci_table);
236