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