staging: comedi: pcmuio: fix possible NULL deref on detach
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / comedi / drivers / 8255_pci.c
CommitLineData
bb71f8b3 1/*
f7c22868
HS
2 * COMEDI driver for generic PCI based 8255 digital i/o boards
3 * Copyright (C) 2012 H Hartley Sweeten <hsweeten@visionengravers.com>
4 *
5 * Based on the tested adl_pci7296 driver written by:
6 * Jon Grierson <jd@renko.co.uk>
df1a3f87
HS
7 * and the experimental cb_pcidio driver written by:
8 * Yoshiya Matsuzaka
32bb1544
HS
9 *
10 * COMEDI - Linux Control and Measurement Device Interface
11 * Copyright (C) 2000 David A. Schleef <ds@schleef.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
bb71f8b3 27
bb71f8b3 28/*
f7c22868
HS
29Driver: 8255_pci
30Description: Generic PCI based 8255 Digital I/O boards
31Devices: (ADLink) PCI-7224 [adl_pci-7224] - 24 channels
32 (ADLink) PCI-7248 [adl_pci-7248] - 48 channels
33 (ADLink) PCI-7296 [adl_pci-7296] - 96 channels
df1a3f87
HS
34 (Measurement Computing) PCI-DIO24 [cb_pci-dio24] - 24 channels
35 (Measurement Computing) PCI-DIO24H [cb_pci-dio24h] - 24 channels
36 (Measurement Computing) PCI-DIO48H [cb_pci-dio48h] - 48 channels
606b0470 37 (Measurement Computing) PCI-DIO96H [cb_pci-dio96h] - 96 channels
b37c1aee
HS
38 (National Instruments) PCI-DIO-96 [ni_pci-dio-96] - 96 channels
39 (National Instruments) PCI-DIO-96B [ni_pci-dio-96b] - 96 channels
40 (National Instruments) PXI-6508 [ni_pxi-6508] - 96 channels
41 (National Instruments) PCI-6503 [ni_pci-6503] - 24 channels
42 (National Instruments) PCI-6503B [ni_pci-6503b] - 24 channels
43 (National Instruments) PCI-6503X [ni_pci-6503x] - 24 channels
44 (National Instruments) PXI-6503 [ni_pxi-6503] - 24 channels
f7c22868
HS
45Author: H Hartley Sweeten <hsweeten@visionengravers.com>
46Updated: Wed, 12 Sep 2012 11:52:01 -0700
47Status: untested
48
49Some of these boards also have an 8254 programmable timer/counter
50chip. This chip is not currently supported by this driver.
32bb1544
HS
51
52Interrupt support for these boards is also not currently supported.
53
f7c22868 54Configuration Options: not applicable, uses PCI auto config
bb71f8b3
JG
55*/
56
33782dd5
HS
57#include <linux/pci.h>
58
bb71f8b3 59#include "../comedidev.h"
bb71f8b3 60
bb71f8b3 61#include "8255.h"
bb71f8b3 62
af48bd8c
HS
63enum pci_8255_boardid {
64 BOARD_ADLINK_PCI7224,
65 BOARD_ADLINK_PCI7248,
66 BOARD_ADLINK_PCI7296,
67 BOARD_CB_PCIDIO24,
68 BOARD_CB_PCIDIO24H,
69 BOARD_CB_PCIDIO48H,
70 BOARD_CB_PCIDIO96H,
71 BOARD_NI_PCIDIO96,
72 BOARD_NI_PCIDIO96B,
73 BOARD_NI_PXI6508,
74 BOARD_NI_PCI6503,
75 BOARD_NI_PCI6503B,
76 BOARD_NI_PCI6503X,
77 BOARD_NI_PXI_6503,
78};
b37c1aee 79
f7c22868 80struct pci_8255_boardinfo {
32bb1544 81 const char *name;
df1a3f87 82 int dio_badr;
f7c22868 83 int n_8255;
32bb1544 84};
bb71f8b3 85
f7c22868 86static const struct pci_8255_boardinfo pci_8255_boards[] = {
af48bd8c 87 [BOARD_ADLINK_PCI7224] = {
f7c22868 88 .name = "adl_pci-7224",
df1a3f87 89 .dio_badr = 2,
f7c22868 90 .n_8255 = 1,
af48bd8c
HS
91 },
92 [BOARD_ADLINK_PCI7248] = {
f7c22868 93 .name = "adl_pci-7248",
df1a3f87 94 .dio_badr = 2,
f7c22868 95 .n_8255 = 2,
af48bd8c
HS
96 },
97 [BOARD_ADLINK_PCI7296] = {
f7c22868 98 .name = "adl_pci-7296",
df1a3f87 99 .dio_badr = 2,
f7c22868 100 .n_8255 = 4,
af48bd8c
HS
101 },
102 [BOARD_CB_PCIDIO24] = {
df1a3f87 103 .name = "cb_pci-dio24",
df1a3f87
HS
104 .dio_badr = 2,
105 .n_8255 = 1,
af48bd8c
HS
106 },
107 [BOARD_CB_PCIDIO24H] = {
df1a3f87 108 .name = "cb_pci-dio24h",
df1a3f87
HS
109 .dio_badr = 2,
110 .n_8255 = 1,
af48bd8c
HS
111 },
112 [BOARD_CB_PCIDIO48H] = {
df1a3f87 113 .name = "cb_pci-dio48h",
df1a3f87
HS
114 .dio_badr = 1,
115 .n_8255 = 2,
af48bd8c
HS
116 },
117 [BOARD_CB_PCIDIO96H] = {
606b0470 118 .name = "cb_pci-dio96h",
606b0470
HS
119 .dio_badr = 2,
120 .n_8255 = 4,
af48bd8c
HS
121 },
122 [BOARD_NI_PCIDIO96] = {
b37c1aee 123 .name = "ni_pci-dio-96",
b37c1aee 124 .dio_badr = 1,
b37c1aee 125 .n_8255 = 4,
af48bd8c
HS
126 },
127 [BOARD_NI_PCIDIO96B] = {
b37c1aee 128 .name = "ni_pci-dio-96b",
b37c1aee 129 .dio_badr = 1,
b37c1aee 130 .n_8255 = 4,
af48bd8c
HS
131 },
132 [BOARD_NI_PXI6508] = {
b37c1aee 133 .name = "ni_pxi-6508",
b37c1aee 134 .dio_badr = 1,
b37c1aee 135 .n_8255 = 4,
af48bd8c
HS
136 },
137 [BOARD_NI_PCI6503] = {
b37c1aee 138 .name = "ni_pci-6503",
b37c1aee 139 .dio_badr = 1,
b37c1aee 140 .n_8255 = 1,
af48bd8c
HS
141 },
142 [BOARD_NI_PCI6503B] = {
b37c1aee 143 .name = "ni_pci-6503b",
b37c1aee 144 .dio_badr = 1,
b37c1aee 145 .n_8255 = 1,
af48bd8c
HS
146 },
147 [BOARD_NI_PCI6503X] = {
b37c1aee 148 .name = "ni_pci-6503x",
b37c1aee 149 .dio_badr = 1,
b37c1aee 150 .n_8255 = 1,
af48bd8c
HS
151 },
152 [BOARD_NI_PXI_6503] = {
b37c1aee 153 .name = "ni_pxi-6503",
b37c1aee 154 .dio_badr = 1,
b37c1aee 155 .n_8255 = 1,
32bb1544
HS
156 },
157};
bb71f8b3 158
77f17d37
HS
159struct pci_8255_private {
160 void __iomem *mmio_base;
161};
162
163static int pci_8255_mmio(int dir, int port, int data, unsigned long iobase)
164{
165 void __iomem *mmio_base = (void __iomem *)iobase;
166
167 if (dir) {
168 writeb(data, mmio_base + port);
169 return 0;
170 } else {
171 return readb(mmio_base + port);
172 }
173}
174
a690b7e5 175static int pci_8255_auto_attach(struct comedi_device *dev,
af48bd8c 176 unsigned long context)
bb71f8b3 177{
750af5e5 178 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
af48bd8c 179 const struct pci_8255_boardinfo *board = NULL;
77f17d37 180 struct pci_8255_private *devpriv;
34c43922 181 struct comedi_subdevice *s;
fed9fd2c 182 bool is_mmio;
bb71f8b3 183 int ret;
32bb1544 184 int i;
bb71f8b3 185
af48bd8c
HS
186 if (context < ARRAY_SIZE(pci_8255_boards))
187 board = &pci_8255_boards[context];
32bb1544
HS
188 if (!board)
189 return -ENODEV;
190 dev->board_ptr = board;
191 dev->board_name = board->name;
bb71f8b3 192
c34fa261
HS
193 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
194 if (!devpriv)
195 return -ENOMEM;
196 dev->private = devpriv;
77f17d37 197
818f569f 198 ret = comedi_pci_enable(dev);
8b6c5694
HS
199 if (ret)
200 return ret;
77f17d37 201
fed9fd2c
IA
202 is_mmio = (pci_resource_flags(pcidev, board->dio_badr) &
203 IORESOURCE_MEM) != 0;
204 if (is_mmio) {
43535526 205 devpriv->mmio_base = pci_ioremap_bar(pcidev, board->dio_badr);
77f17d37
HS
206 if (!devpriv->mmio_base)
207 return -ENOMEM;
43535526
HS
208 } else {
209 dev->iobase = pci_resource_start(pcidev, board->dio_badr);
77f17d37 210 }
262ea0d4 211
32bb1544
HS
212 /*
213 * One, two, or four subdevices are setup by this driver depending
214 * on the number of channels provided by the board. Each subdevice
215 * has 24 channels supported by the 8255 module.
216 */
f7c22868 217 ret = comedi_alloc_subdevices(dev, board->n_8255);
32bb1544 218 if (ret)
262ea0d4
HS
219 return ret;
220
f7c22868 221 for (i = 0; i < board->n_8255; i++) {
43535526
HS
222 unsigned long iobase;
223
630b713b 224 s = &dev->subdevices[i];
fed9fd2c 225 if (is_mmio) {
77f17d37
HS
226 iobase = (unsigned long)(devpriv->mmio_base + (i * 4));
227 ret = subdev_8255_init(dev, s, pci_8255_mmio, iobase);
228 } else {
229 iobase = dev->iobase + (i * 4);
230 ret = subdev_8255_init(dev, s, NULL, iobase);
231 }
32bb1544
HS
232 if (ret)
233 return ret;
234 }
262ea0d4 235
32bb1544 236 dev_info(dev->class_dev, "%s attached (%d digital i/o channels)\n",
f7c22868 237 dev->board_name, board->n_8255 * 24);
262ea0d4
HS
238
239 return 0;
bb71f8b3
JG
240}
241
f7c22868 242static void pci_8255_detach(struct comedi_device *dev)
bb71f8b3 243{
77f17d37 244 struct pci_8255_private *devpriv = dev->private;
32bb1544 245 int i;
caeb2490 246
2f69915c
HS
247 for (i = 0; i < dev->n_subdevices; i++)
248 comedi_spriv_free(dev, i);
249 if (devpriv && devpriv->mmio_base)
7f072f54
HS
250 iounmap(devpriv->mmio_base);
251 comedi_pci_disable(dev);
bb71f8b3
JG
252}
253
f7c22868
HS
254static struct comedi_driver pci_8255_driver = {
255 .driver_name = "8255_pci",
db0eaeed 256 .module = THIS_MODULE,
750af5e5 257 .auto_attach = pci_8255_auto_attach,
f7c22868 258 .detach = pci_8255_detach,
db0eaeed
HS
259};
260
a690b7e5 261static int pci_8255_pci_probe(struct pci_dev *dev,
b8f4ac23 262 const struct pci_device_id *id)
727b286b 263{
b8f4ac23 264 return comedi_pci_auto_config(dev, &pci_8255_driver, id->driver_data);
727b286b
AT
265}
266
f7c22868 267static DEFINE_PCI_DEVICE_TABLE(pci_8255_pci_table) = {
af48bd8c
HS
268 { PCI_VDEVICE(ADLINK, 0x7224), BOARD_ADLINK_PCI7224 },
269 { PCI_VDEVICE(ADLINK, 0x7248), BOARD_ADLINK_PCI7248 },
270 { PCI_VDEVICE(ADLINK, 0x7296), BOARD_ADLINK_PCI7296 },
271 { PCI_VDEVICE(CB, 0x0028), BOARD_CB_PCIDIO24 },
272 { PCI_VDEVICE(CB, 0x0014), BOARD_CB_PCIDIO24H },
273 { PCI_VDEVICE(CB, 0x000b), BOARD_CB_PCIDIO48H },
274 { PCI_VDEVICE(CB, 0x0017), BOARD_CB_PCIDIO96H },
275 { PCI_VDEVICE(NI, 0x0160), BOARD_NI_PCIDIO96 },
276 { PCI_VDEVICE(NI, 0x1630), BOARD_NI_PCIDIO96B },
277 { PCI_VDEVICE(NI, 0x13c0), BOARD_NI_PXI6508 },
278 { PCI_VDEVICE(NI, 0x0400), BOARD_NI_PCI6503 },
279 { PCI_VDEVICE(NI, 0x1250), BOARD_NI_PCI6503B },
280 { PCI_VDEVICE(NI, 0x17d0), BOARD_NI_PCI6503X },
281 { PCI_VDEVICE(NI, 0x1800), BOARD_NI_PXI_6503 },
75e6301b 282 { 0 }
db0eaeed 283};
f7c22868 284MODULE_DEVICE_TABLE(pci, pci_8255_pci_table);
db0eaeed 285
f7c22868
HS
286static struct pci_driver pci_8255_pci_driver = {
287 .name = "8255_pci",
288 .id_table = pci_8255_pci_table,
289 .probe = pci_8255_pci_probe,
9901a4d7 290 .remove = comedi_pci_auto_unconfig,
727b286b 291};
f7c22868 292module_comedi_pci_driver(pci_8255_driver, pci_8255_pci_driver);
90f703d3 293
f7c22868 294MODULE_DESCRIPTION("COMEDI - Generic PCI based 8255 Digital I/O boards");
90f703d3 295MODULE_AUTHOR("Comedi http://www.comedi.org");
90f703d3 296MODULE_LICENSE("GPL");