Merge tag 'ecryptfs-3.10-rc1-ablkcipher' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / comedi / drivers / addi_apci_16xx.c
1 /*
2  * addi_apci_16xx.c
3  * Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
4  * Project manager: S. Weber
5  *
6  *      ADDI-DATA GmbH
7  *      Dieselstrasse 3
8  *      D-77833 Ottersweier
9  *      Tel: +19(0)7223/9493-0
10  *      Fax: +49(0)7223/9493-92
11  *      http://www.addi-data.com
12  *      info@addi-data.com
13  *
14  * This program is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by the
16  * Free Software Foundation; either version 2 of the License, or (at your
17  * option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22  * more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, write to the Free Software Foundation, Inc.,
26  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  * You should also find the complete GPL in the COPYING file accompanying
29  * this source code.
30  */
31
32 #include <linux/pci.h>
33
34 #include "../comedidev.h"
35
36 /*
37  * Register I/O map
38  */
39 #define APCI16XX_IN_REG(x)              (((x) * 4) + 0x08)
40 #define APCI16XX_OUT_REG(x)             (((x) * 4) + 0x14)
41 #define APCI16XX_DIR_REG(x)             (((x) * 4) + 0x20)
42
43 enum apci16xx_boardid {
44         BOARD_APCI1648,
45         BOARD_APCI1696,
46 };
47
48 struct apci16xx_boardinfo {
49         const char *name;
50         int n_chan;
51 };
52
53 static const struct apci16xx_boardinfo apci16xx_boardtypes[] = {
54         [BOARD_APCI1648] = {
55                 .name           = "apci1648",
56                 .n_chan         = 48,           /* 2 subdevices */
57         },
58         [BOARD_APCI1696] = {
59                 .name           = "apci1696",
60                 .n_chan         = 96,           /* 3 subdevices */
61         },
62 };
63
64 static int apci16xx_insn_config(struct comedi_device *dev,
65                                 struct comedi_subdevice *s,
66                                 struct comedi_insn *insn,
67                                 unsigned int *data)
68 {
69         unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
70         unsigned int bits;
71
72         /*
73          * Each 8-bit "port" is configurable as either input or
74          * output. Changing the configuration of any channel in
75          * a port changes the entire port.
76          */
77         if (chan_mask & 0x000000ff)
78                 bits = 0x000000ff;
79         else if (chan_mask & 0x0000ff00)
80                 bits = 0x0000ff00;
81         else if (chan_mask & 0x00ff0000)
82                 bits = 0x00ff0000;
83         else
84                 bits = 0xff000000;
85
86         switch (data[0]) {
87         case INSN_CONFIG_DIO_INPUT:
88                 s->io_bits &= ~bits;
89                 break;
90         case INSN_CONFIG_DIO_OUTPUT:
91                 s->io_bits |= bits;
92                 break;
93         case INSN_CONFIG_DIO_QUERY:
94                 data[1] = (s->io_bits & bits) ? COMEDI_INPUT : COMEDI_OUTPUT;
95                 return insn->n;
96         default:
97                 return -EINVAL;
98         }
99
100         outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(s->index));
101
102         return insn->n;
103 }
104
105 static int apci16xx_dio_insn_bits(struct comedi_device *dev,
106                                   struct comedi_subdevice *s,
107                                   struct comedi_insn *insn,
108                                   unsigned int *data)
109 {
110         unsigned int mask = data[0];
111         unsigned int bits = data[1];
112
113         /* Only update the channels configured as outputs */
114         mask &= s->io_bits;
115         if (mask) {
116                 s->state &= ~mask;
117                 s->state |= (bits & mask);
118
119                 outl(s->state, dev->iobase + APCI16XX_OUT_REG(s->index));
120         }
121
122         data[1] = inl(dev->iobase + APCI16XX_IN_REG(s->index));
123
124         return insn->n;
125 }
126
127 static int apci16xx_auto_attach(struct comedi_device *dev,
128                                 unsigned long context)
129 {
130         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
131         const struct apci16xx_boardinfo *board = NULL;
132         struct comedi_subdevice *s;
133         unsigned int n_subdevs;
134         unsigned int last;
135         int i;
136         int ret;
137
138         if (context < ARRAY_SIZE(apci16xx_boardtypes))
139                 board = &apci16xx_boardtypes[context];
140         if (!board)
141                 return -ENODEV;
142         dev->board_ptr = board;
143         dev->board_name = board->name;
144
145         ret = comedi_pci_enable(dev);
146         if (ret)
147                 return ret;
148
149         dev->iobase = pci_resource_start(pcidev, 0);
150
151         /*
152          * Work out the nubmer of subdevices needed to support all the
153          * digital i/o channels on the board. Each subdevice supports
154          * up to 32 channels.
155          */
156         n_subdevs = board->n_chan / 32;
157         if ((n_subdevs * 32) < board->n_chan) {
158                 last = board->n_chan - (n_subdevs * 32);
159                 n_subdevs++;
160         } else {
161                 last = 0;
162         }
163
164         ret = comedi_alloc_subdevices(dev, n_subdevs);
165         if (ret)
166                 return ret;
167
168         /* Initialize the TTL digital i/o subdevices */
169         for (i = 0; i < n_subdevs; i++) {
170                 s = &dev->subdevices[i];
171                 s->type         = COMEDI_SUBD_DIO;
172                 s->subdev_flags = SDF_WRITEABLE | SDF_READABLE;
173                 s->n_chan       = ((i * 32) < board->n_chan) ? 32 : last;
174                 s->maxdata      = 1;
175                 s->range_table  = &range_digital;
176                 s->insn_config  = apci16xx_insn_config;
177                 s->insn_bits    = apci16xx_dio_insn_bits;
178
179                 /* Default all channels to inputs */
180                 s->io_bits      = 0;
181                 outl(s->io_bits, dev->iobase + APCI16XX_DIR_REG(i));
182         }
183
184         return 0;
185 }
186
187 static struct comedi_driver apci16xx_driver = {
188         .driver_name    = "addi_apci_16xx",
189         .module         = THIS_MODULE,
190         .auto_attach    = apci16xx_auto_attach,
191         .detach         = comedi_pci_disable,
192 };
193
194 static int apci16xx_pci_probe(struct pci_dev *dev,
195                               const struct pci_device_id *id)
196 {
197         return comedi_pci_auto_config(dev, &apci16xx_driver, id->driver_data);
198 }
199
200 static DEFINE_PCI_DEVICE_TABLE(apci16xx_pci_table) = {
201         { PCI_VDEVICE(ADDIDATA, 0x1009), BOARD_APCI1648 },
202         { PCI_VDEVICE(ADDIDATA, 0x100a), BOARD_APCI1696 },
203         { 0 }
204 };
205 MODULE_DEVICE_TABLE(pci, apci16xx_pci_table);
206
207 static struct pci_driver apci16xx_pci_driver = {
208         .name           = "addi_apci_16xx",
209         .id_table       = apci16xx_pci_table,
210         .probe          = apci16xx_pci_probe,
211         .remove         = comedi_pci_auto_unconfig,
212 };
213 module_comedi_pci_driver(apci16xx_driver, apci16xx_pci_driver);
214
215 MODULE_DESCRIPTION("ADDI-DATA APCI-1648/1696, TTL I/O boards");
216 MODULE_AUTHOR("Comedi http://www.comedi.org");
217 MODULE_LICENSE("GPL");