contec_pci_dio.c revision 89c5db09627c65d5fab8b71943dd493846e25d04
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	{ PCI_DEVICE(PCI_VENDOR_ID_CONTEC, PCI_DEVICE_ID_PIO1616L),
61		.driver_data = PIO1616L },
62	{0}
63};
64
65MODULE_DEVICE_TABLE(pci, contec_pci_table);
66
67#define thisboard ((const struct contec_board *)dev->board_ptr)
68
69struct contec_private {
70	int data;
71
72	struct pci_dev *pci_dev;
73
74};
75
76#define devpriv ((struct contec_private *)dev->private)
77
78static int contec_attach(struct comedi_device *dev,
79			 struct comedi_devconfig *it);
80static int contec_detach(struct comedi_device *dev);
81static struct comedi_driver driver_contec = {
82	.driver_name = "contec_pci_dio",
83	.module = THIS_MODULE,
84	.attach = contec_attach,
85	.detach = contec_detach,
86};
87
88/* Classic digital IO */
89static int contec_di_insn_bits(struct comedi_device *dev,
90			       struct comedi_subdevice *s,
91			       struct comedi_insn *insn, unsigned int *data);
92static int contec_do_insn_bits(struct comedi_device *dev,
93			       struct comedi_subdevice *s,
94			       struct comedi_insn *insn, unsigned int *data);
95
96#if 0
97static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
98			  struct comedi_cmd *cmd);
99
100static int contec_ns_to_timer(unsigned int *ns, int round);
101#endif
102
103static int contec_attach(struct comedi_device *dev, struct comedi_devconfig *it)
104{
105	struct pci_dev *pcidev = NULL;
106	struct comedi_subdevice *s;
107
108	printk("comedi%d: contec: ", dev->minor);
109
110	dev->board_name = thisboard->name;
111
112	if (alloc_private(dev, sizeof(struct contec_private)) < 0)
113		return -ENOMEM;
114
115	if (alloc_subdevices(dev, 2) < 0)
116		return -ENOMEM;
117
118	for_each_pci_dev(pcidev) {
119		if (pcidev->vendor == PCI_VENDOR_ID_CONTEC &&
120		    pcidev->device == PCI_DEVICE_ID_PIO1616L) {
121			if (it->options[0] || it->options[1]) {
122				/* Check bus and slot. */
123				if (it->options[0] != pcidev->bus->number ||
124				    it->options[1] != PCI_SLOT(pcidev->devfn)) {
125					continue;
126				}
127			}
128			devpriv->pci_dev = pcidev;
129			if (comedi_pci_enable(pcidev, "contec_pci_dio")) {
130				printk
131				    ("error enabling PCI device and request regions!\n");
132				return -EIO;
133			}
134			dev->iobase = pci_resource_start(pcidev, 0);
135			printk(" base addr %lx ", dev->iobase);
136
137			dev->board_ptr = contec_boards + 0;
138
139			s = dev->subdevices + 0;
140
141			s->type = COMEDI_SUBD_DI;
142			s->subdev_flags = SDF_READABLE;
143			s->n_chan = 16;
144			s->maxdata = 1;
145			s->range_table = &range_digital;
146			s->insn_bits = contec_di_insn_bits;
147
148			s = dev->subdevices + 1;
149			s->type = COMEDI_SUBD_DO;
150			s->subdev_flags = SDF_WRITABLE;
151			s->n_chan = 16;
152			s->maxdata = 1;
153			s->range_table = &range_digital;
154			s->insn_bits = contec_do_insn_bits;
155
156			printk("attached\n");
157
158			return 1;
159		}
160	}
161
162	printk("card not present!\n");
163
164	return -EIO;
165}
166
167static int contec_detach(struct comedi_device *dev)
168{
169	printk("comedi%d: contec: remove\n", dev->minor);
170
171	if (devpriv && devpriv->pci_dev) {
172		if (dev->iobase)
173			comedi_pci_disable(devpriv->pci_dev);
174		pci_dev_put(devpriv->pci_dev);
175	}
176
177	return 0;
178}
179
180#if 0
181static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
182			  struct comedi_cmd *cmd)
183{
184	printk("contec_cmdtest called\n");
185	return 0;
186}
187
188static int contec_ns_to_timer(unsigned int *ns, int round)
189{
190	return *ns;
191}
192#endif
193
194static int contec_do_insn_bits(struct comedi_device *dev,
195			       struct comedi_subdevice *s,
196			       struct comedi_insn *insn, unsigned int *data)
197{
198
199	dev_dbg(dev->hw_dev, "contec_do_insn_bits called\n");
200	dev_dbg(dev->hw_dev, "data: %d %d\n", data[0], data[1]);
201
202	if (insn->n != 2)
203		return -EINVAL;
204
205	if (data[0]) {
206		s->state &= ~data[0];
207		s->state |= data[0] & data[1];
208		dev_dbg(dev->hw_dev, "out: %d on %lx\n", s->state,
209			dev->iobase + thisboard->out_offs);
210		outw(s->state, dev->iobase + thisboard->out_offs);
211	}
212	return 2;
213}
214
215static int contec_di_insn_bits(struct comedi_device *dev,
216			       struct comedi_subdevice *s,
217			       struct comedi_insn *insn, unsigned int *data)
218{
219
220	dev_dbg(dev->hw_dev, "contec_di_insn_bits called\n");
221	dev_dbg(dev->hw_dev, "data: %d %d\n", data[0], data[1]);
222
223	if (insn->n != 2)
224		return -EINVAL;
225
226	data[1] = inw(dev->iobase + thisboard->in_offs);
227
228	return 2;
229}
230
231static int __devinit driver_contec_pci_probe(struct pci_dev *dev,
232					     const struct pci_device_id *ent)
233{
234	return comedi_pci_auto_config(dev, driver_contec.driver_name);
235}
236
237static void __devexit driver_contec_pci_remove(struct pci_dev *dev)
238{
239	comedi_pci_auto_unconfig(dev);
240}
241
242static struct pci_driver driver_contec_pci_driver = {
243	.id_table = contec_pci_table,
244	.probe = &driver_contec_pci_probe,
245	.remove = __devexit_p(&driver_contec_pci_remove)
246};
247
248static int __init driver_contec_init_module(void)
249{
250	int retval;
251
252	retval = comedi_driver_register(&driver_contec);
253	if (retval < 0)
254		return retval;
255
256	driver_contec_pci_driver.name = (char *)driver_contec.driver_name;
257	return pci_register_driver(&driver_contec_pci_driver);
258}
259
260static void __exit driver_contec_cleanup_module(void)
261{
262	pci_unregister_driver(&driver_contec_pci_driver);
263	comedi_driver_unregister(&driver_contec);
264}
265
266module_init(driver_contec_init_module);
267module_exit(driver_contec_cleanup_module);
268
269MODULE_AUTHOR("Comedi http://www.comedi.org");
270MODULE_DESCRIPTION("Comedi low-level driver");
271MODULE_LICENSE("GPL");
272