contec_pci_dio.c revision 8bba4439fb784d9f1aa74c1e13a42cd21f989ffc
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
39enum contec_model {
40	PIO1616L = 0,
41};
42
43struct contec_board {
44	const char *name;
45	int in_offs;
46	int out_offs;
47};
48static const struct contec_board contec_boards[] = {
49	{"PIO1616L", 0, 2 },
50};
51
52#define PCI_DEVICE_ID_PIO1616L 0x8172
53
54static int contec_do_insn_bits(struct comedi_device *dev,
55			       struct comedi_subdevice *s,
56			       struct comedi_insn *insn, unsigned int *data)
57{
58	const struct contec_board *thisboard = comedi_board(dev);
59
60	if (data[0]) {
61		s->state &= ~data[0];
62		s->state |= data[0] & data[1];
63
64		outw(s->state, dev->iobase + thisboard->out_offs);
65	}
66	return insn->n;
67}
68
69static int contec_di_insn_bits(struct comedi_device *dev,
70			       struct comedi_subdevice *s,
71			       struct comedi_insn *insn, unsigned int *data)
72{
73	const struct contec_board *thisboard = comedi_board(dev);
74
75	data[1] = inw(dev->iobase + thisboard->in_offs);
76
77	return insn->n;
78}
79
80static struct pci_dev *contec_find_pci_dev(struct comedi_device *dev,
81					   struct comedi_devconfig *it)
82{
83	struct pci_dev *pcidev = NULL;
84	int bus = it->options[0];
85	int slot = it->options[1];
86
87	for_each_pci_dev(pcidev) {
88		if (bus || slot) {
89			if (bus != pcidev->bus->number ||
90				slot != PCI_SLOT(pcidev->devfn))
91				continue;
92		}
93		if (pcidev->vendor != PCI_VENDOR_ID_CONTEC ||
94		    pcidev->device != PCI_DEVICE_ID_PIO1616L)
95			continue;
96
97		dev->board_ptr = contec_boards + 0;
98		return pcidev;
99	}
100	dev_err(dev->class_dev,
101		"No supported board found! (req. bus %d, slot %d)\n",
102		bus, slot);
103	return NULL;
104}
105
106static int contec_attach(struct comedi_device *dev, struct comedi_devconfig *it)
107{
108	const struct contec_board *thisboard;
109	struct pci_dev *pcidev;
110	struct comedi_subdevice *s;
111	int ret;
112
113	printk("comedi%d: contec: ", dev->minor);
114
115	ret = comedi_alloc_subdevices(dev, 2);
116	if (ret)
117		return ret;
118
119	pcidev = contec_find_pci_dev(dev, it);
120	if (!pcidev)
121		return -EIO;
122	comedi_set_hw_dev(dev, &pcidev->dev);
123	thisboard = comedi_board(dev);
124	dev->board_name = thisboard->name;
125
126	if (comedi_pci_enable(pcidev, "contec_pci_dio")) {
127		printk("error enabling PCI device and request regions!\n");
128		return -EIO;
129	}
130	dev->iobase = pci_resource_start(pcidev, 0);
131	printk(" base addr %lx ", dev->iobase);
132
133	s = dev->subdevices + 0;
134
135	s->type = COMEDI_SUBD_DI;
136	s->subdev_flags = SDF_READABLE;
137	s->n_chan = 16;
138	s->maxdata = 1;
139	s->range_table = &range_digital;
140	s->insn_bits = contec_di_insn_bits;
141
142	s = dev->subdevices + 1;
143	s->type = COMEDI_SUBD_DO;
144	s->subdev_flags = SDF_WRITABLE;
145	s->n_chan = 16;
146	s->maxdata = 1;
147	s->range_table = &range_digital;
148	s->insn_bits = contec_do_insn_bits;
149
150	printk("attached\n");
151
152	return 1;
153}
154
155static void contec_detach(struct comedi_device *dev)
156{
157	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
158
159	if (pcidev) {
160		if (dev->iobase)
161			comedi_pci_disable(pcidev);
162		pci_dev_put(pcidev);
163	}
164}
165
166static struct comedi_driver contec_pci_dio_driver = {
167	.driver_name	= "contec_pci_dio",
168	.module		= THIS_MODULE,
169	.attach		= contec_attach,
170	.detach		= contec_detach,
171};
172
173static int __devinit contec_pci_dio_pci_probe(struct pci_dev *dev,
174					      const struct pci_device_id *ent)
175{
176	return comedi_pci_auto_config(dev, &contec_pci_dio_driver);
177}
178
179static void __devexit contec_pci_dio_pci_remove(struct pci_dev *dev)
180{
181	comedi_pci_auto_unconfig(dev);
182}
183
184static DEFINE_PCI_DEVICE_TABLE(contec_pci_dio_pci_table) = {
185	{ PCI_DEVICE(PCI_VENDOR_ID_CONTEC, PCI_DEVICE_ID_PIO1616L),
186		.driver_data = PIO1616L },
187	{ 0 }
188};
189MODULE_DEVICE_TABLE(pci, contec_pci_dio_pci_table);
190
191static struct pci_driver contec_pci_dio_pci_driver = {
192	.name		= "contec_pci_dio",
193	.id_table	= contec_pci_dio_pci_table,
194	.probe		= contec_pci_dio_pci_probe,
195	.remove		= __devexit_p(contec_pci_dio_pci_remove),
196};
197module_comedi_pci_driver(contec_pci_dio_driver, contec_pci_dio_pci_driver);
198
199MODULE_AUTHOR("Comedi http://www.comedi.org");
200MODULE_DESCRIPTION("Comedi low-level driver");
201MODULE_LICENSE("GPL");
202