contec_pci_dio.c revision ce157f8032bbd46d9427034c335b0afd751da25d
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/*
18Driver: contec_pci_dio
19Description: Contec PIO1616L digital I/O board
20Devices: [Contec] PIO1616L (contec_pci_dio)
21Author: Stefano Rivoir <s.rivoir@gts.it>
22Updated: Wed, 27 Jun 2007 13:00:06 +0100
23Status: works
24
25Configuration Options: not applicable, uses comedi PCI auto config
26*/
27
28#include <linux/module.h>
29#include <linux/pci.h>
30
31#include "../comedidev.h"
32
33#define PCI_DEVICE_ID_PIO1616L 0x8172
34
35/*
36 * Register map
37 */
38#define PIO1616L_DI_REG		0x00
39#define PIO1616L_DO_REG		0x02
40
41static int contec_do_insn_bits(struct comedi_device *dev,
42			       struct comedi_subdevice *s,
43			       struct comedi_insn *insn, unsigned int *data)
44{
45	unsigned int mask = data[0];
46	unsigned int bits = data[1];
47
48	if (mask) {
49		s->state &= ~mask;
50		s->state |= (bits & mask);
51
52		outw(s->state, dev->iobase + PIO1616L_DO_REG);
53	}
54
55	data[1] = s->state;
56
57	return insn->n;
58}
59
60static int contec_di_insn_bits(struct comedi_device *dev,
61			       struct comedi_subdevice *s,
62			       struct comedi_insn *insn, unsigned int *data)
63{
64	data[1] = inw(dev->iobase + PIO1616L_DI_REG);
65
66	return insn->n;
67}
68
69static int contec_auto_attach(struct comedi_device *dev,
70					unsigned long context_unused)
71{
72	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
73	struct comedi_subdevice *s;
74	int ret;
75
76	ret = comedi_pci_enable(dev);
77	if (ret)
78		return ret;
79	dev->iobase = pci_resource_start(pcidev, 0);
80
81	ret = comedi_alloc_subdevices(dev, 2);
82	if (ret)
83		return ret;
84
85	s = &dev->subdevices[0];
86	s->type		= COMEDI_SUBD_DI;
87	s->subdev_flags	= SDF_READABLE;
88	s->n_chan	= 16;
89	s->maxdata	= 1;
90	s->range_table	= &range_digital;
91	s->insn_bits	= contec_di_insn_bits;
92
93	s = &dev->subdevices[1];
94	s->type		= COMEDI_SUBD_DO;
95	s->subdev_flags	= SDF_WRITABLE;
96	s->n_chan	= 16;
97	s->maxdata	= 1;
98	s->range_table	= &range_digital;
99	s->insn_bits	= contec_do_insn_bits;
100
101	dev_info(dev->class_dev, "%s attached\n", dev->board_name);
102
103	return 0;
104}
105
106static struct comedi_driver contec_pci_dio_driver = {
107	.driver_name	= "contec_pci_dio",
108	.module		= THIS_MODULE,
109	.auto_attach	= contec_auto_attach,
110	.detach		= comedi_pci_disable,
111};
112
113static int contec_pci_dio_pci_probe(struct pci_dev *dev,
114				    const struct pci_device_id *id)
115{
116	return comedi_pci_auto_config(dev, &contec_pci_dio_driver,
117				      id->driver_data);
118}
119
120static DEFINE_PCI_DEVICE_TABLE(contec_pci_dio_pci_table) = {
121	{ PCI_DEVICE(PCI_VENDOR_ID_CONTEC, PCI_DEVICE_ID_PIO1616L) },
122	{ 0 }
123};
124MODULE_DEVICE_TABLE(pci, contec_pci_dio_pci_table);
125
126static struct pci_driver contec_pci_dio_pci_driver = {
127	.name		= "contec_pci_dio",
128	.id_table	= contec_pci_dio_pci_table,
129	.probe		= contec_pci_dio_pci_probe,
130	.remove		= comedi_pci_auto_unconfig,
131};
132module_comedi_pci_driver(contec_pci_dio_driver, contec_pci_dio_pci_driver);
133
134MODULE_AUTHOR("Comedi http://www.comedi.org");
135MODULE_DESCRIPTION("Comedi low-level driver");
136MODULE_LICENSE("GPL");
137