Merge remote-tracking branch 'upstream' into next
[cascardo/linux.git] / drivers / staging / comedi / drivers / contec_pci_dio.c
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 /*
23 Driver: contec_pci_dio
24 Description: Contec PIO1616L digital I/O board
25 Devices: [Contec] PIO1616L (contec_pci_dio)
26 Author: Stefano Rivoir <s.rivoir@gts.it>
27 Updated: Wed, 27 Jun 2007 13:00:06 +0100
28 Status: works
29
30 Configuration 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 enum contec_model {
40         PIO1616L = 0,
41 };
42
43 struct 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 };
52 static 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
60 static int contec_do_insn_bits(struct comedi_device *dev,
61                                struct comedi_subdevice *s,
62                                struct comedi_insn *insn, unsigned int *data)
63 {
64
65         dev_dbg(dev->class_dev, "contec_do_insn_bits called\n");
66         dev_dbg(dev->class_dev, "data: %d %d\n", data[0], data[1]);
67
68         if (data[0]) {
69                 s->state &= ~data[0];
70                 s->state |= data[0] & data[1];
71                 dev_dbg(dev->class_dev, "out: %d on %lx\n", s->state,
72                         dev->iobase + thisboard->out_offs);
73                 outw(s->state, dev->iobase + thisboard->out_offs);
74         }
75         return insn->n;
76 }
77
78 static int contec_di_insn_bits(struct comedi_device *dev,
79                                struct comedi_subdevice *s,
80                                struct comedi_insn *insn, unsigned int *data)
81 {
82
83         dev_dbg(dev->class_dev, "contec_di_insn_bits called\n");
84         dev_dbg(dev->class_dev, "data: %d %d\n", data[0], data[1]);
85
86         data[1] = inw(dev->iobase + thisboard->in_offs);
87
88         return insn->n;
89 }
90
91 static struct pci_dev *contec_find_pci_dev(struct comedi_device *dev,
92                                            struct comedi_devconfig *it)
93 {
94         struct pci_dev *pcidev = NULL;
95         int bus = it->options[0];
96         int slot = it->options[1];
97
98         for_each_pci_dev(pcidev) {
99                 if (bus || slot) {
100                         if (bus != pcidev->bus->number ||
101                                 slot != PCI_SLOT(pcidev->devfn))
102                                 continue;
103                 }
104                 if (pcidev->vendor != PCI_VENDOR_ID_CONTEC ||
105                     pcidev->device != PCI_DEVICE_ID_PIO1616L)
106                         continue;
107
108                 dev->board_ptr = contec_boards + 0;
109                 return pcidev;
110         }
111         dev_err(dev->class_dev,
112                 "No supported board found! (req. bus %d, slot %d)\n",
113                 bus, slot);
114         return NULL;
115 }
116
117 static int contec_attach(struct comedi_device *dev, struct comedi_devconfig *it)
118 {
119         struct pci_dev *pcidev;
120         struct comedi_subdevice *s;
121         int ret;
122
123         printk("comedi%d: contec: ", dev->minor);
124
125         dev->board_name = thisboard->name;
126
127         ret = comedi_alloc_subdevices(dev, 2);
128         if (ret)
129                 return ret;
130
131         pcidev = contec_find_pci_dev(dev, it);
132         if (!pcidev)
133                 return -EIO;
134         comedi_set_hw_dev(dev, &pcidev->dev);
135
136         if (comedi_pci_enable(pcidev, "contec_pci_dio")) {
137                 printk("error enabling PCI device and request regions!\n");
138                 return -EIO;
139         }
140         dev->iobase = pci_resource_start(pcidev, 0);
141         printk(" base addr %lx ", dev->iobase);
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 static void contec_detach(struct comedi_device *dev)
166 {
167         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
168
169         if (pcidev) {
170                 if (dev->iobase)
171                         comedi_pci_disable(pcidev);
172                 pci_dev_put(pcidev);
173         }
174 }
175
176 static struct comedi_driver contec_pci_dio_driver = {
177         .driver_name    = "contec_pci_dio",
178         .module         = THIS_MODULE,
179         .attach         = contec_attach,
180         .detach         = contec_detach,
181 };
182
183 static int __devinit contec_pci_dio_pci_probe(struct pci_dev *dev,
184                                               const struct pci_device_id *ent)
185 {
186         return comedi_pci_auto_config(dev, &contec_pci_dio_driver);
187 }
188
189 static void __devexit contec_pci_dio_pci_remove(struct pci_dev *dev)
190 {
191         comedi_pci_auto_unconfig(dev);
192 }
193
194 static DEFINE_PCI_DEVICE_TABLE(contec_pci_dio_pci_table) = {
195         { PCI_DEVICE(PCI_VENDOR_ID_CONTEC, PCI_DEVICE_ID_PIO1616L),
196                 .driver_data = PIO1616L },
197         { 0 }
198 };
199 MODULE_DEVICE_TABLE(pci, contec_pci_dio_pci_table);
200
201 static struct pci_driver contec_pci_dio_pci_driver = {
202         .name           = "contec_pci_dio",
203         .id_table       = contec_pci_dio_pci_table,
204         .probe          = contec_pci_dio_pci_probe,
205         .remove         = __devexit_p(contec_pci_dio_pci_remove),
206 };
207 module_comedi_pci_driver(contec_pci_dio_driver, contec_pci_dio_pci_driver);
208
209 MODULE_AUTHOR("Comedi http://www.comedi.org");
210 MODULE_DESCRIPTION("Comedi low-level driver");
211 MODULE_LICENSE("GPL");