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