]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/staging/comedi/drivers/ni_670x.c
staging: comedi: ni_670x: fix dereference of an invalid pointer
[mirror_ubuntu-hirsute-kernel.git] / drivers / staging / comedi / drivers / ni_670x.c
CommitLineData
3c5e1722
BJ
1/*
2 comedi/drivers/ni_670x.c
3 Hardware driver for NI 670x devices
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23/*
24Driver: ni_670x
25Description: National Instruments 670x
26Author: Bart Joris <bjoris@advalvas.be>
27Updated: Wed, 11 Dec 2002 18:25:35 -0800
28Devices: [National Instruments] PCI-6703 (ni_670x), PCI-6704
29Status: unknown
30
31Commands are not supported.
32*/
33
34/*
35 Bart Joris <bjoris@advalvas.be> Last updated on 20/08/2001
36
37 Manuals:
38
39 322110a.pdf PCI/PXI-6704 User Manual
40 322110b.pdf PCI/PXI-6703/6704 User Manual
41
42*/
43
25436dc9 44#include <linux/interrupt.h>
5a0e3ad6 45#include <linux/slab.h>
3c5e1722
BJ
46#include "../comedidev.h"
47
48#include "mite.h"
49
3c5e1722
BJ
50#define AO_VALUE_OFFSET 0x00
51#define AO_CHAN_OFFSET 0x0c
52#define AO_STATUS_OFFSET 0x10
53#define AO_CONTROL_OFFSET 0x10
54#define DIO_PORT0_DIR_OFFSET 0x20
55#define DIO_PORT0_DATA_OFFSET 0x24
56#define DIO_PORT1_DIR_OFFSET 0x28
57#define DIO_PORT1_DATA_OFFSET 0x2c
58#define MISC_STATUS_OFFSET 0x14
59#define MISC_CONTROL_OFFSET 0x14
60
61/* Board description*/
62
43313b07 63struct ni_670x_board {
3c5e1722
BJ
64 unsigned short dev_id;
65 const char *name;
66 unsigned short ao_chans;
67 unsigned short ao_bits;
43313b07
BP
68};
69
70static const struct ni_670x_board ni_670x_boards[] = {
3c5e1722 71 {
0a85b6f0
MT
72 .dev_id = 0x2c90,
73 .name = "PCI-6703",
74 .ao_chans = 16,
75 .ao_bits = 16,
76 },
3c5e1722 77 {
0a85b6f0
MT
78 .dev_id = 0x1920,
79 .name = "PXI-6704",
80 .ao_chans = 32,
81 .ao_bits = 16,
82 },
3c5e1722 83 {
0a85b6f0
MT
84 .dev_id = 0x1290,
85 .name = "PCI-6704",
86 .ao_chans = 32,
87 .ao_bits = 16,
88 },
3c5e1722
BJ
89};
90
8ce8a1ff
BP
91struct ni_670x_private {
92
3c5e1722
BJ
93 struct mite_struct *mite;
94 int boardtype;
95 int dio;
790c5541 96 unsigned int ao_readback[32];
8ce8a1ff
BP
97};
98
9ced1de6 99static struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
3c5e1722 100
0a85b6f0
MT
101static int ni_670x_ao_winsn(struct comedi_device *dev,
102 struct comedi_subdevice *s,
103 struct comedi_insn *insn, unsigned int *data)
3c5e1722 104{
289a4033 105 struct ni_670x_private *devpriv = dev->private;
3c5e1722
BJ
106 int i;
107 int chan = CR_CHAN(insn->chanspec);
108
109 /* Channel number mapping :
110
111 NI 6703/ NI 6704 | NI 6704 Only
112 ----------------------------------------------------
113 vch(0) : 0 | ich(16) : 1
114 vch(1) : 2 | ich(17) : 3
115 . : . | . .
116 . : . | . .
117 . : . | . .
118 vch(15) : 30 | ich(31) : 31 */
119
120 for (i = 0; i < insn->n; i++) {
c733110a
BA
121 /* First write in channel register which channel to use */
122 writel(((chan & 15) << 1) | ((chan & 16) >> 4),
123 devpriv->mite->daq_io_addr + AO_CHAN_OFFSET);
124 /* write channel value */
125 writel(data[i], devpriv->mite->daq_io_addr + AO_VALUE_OFFSET);
3c5e1722
BJ
126 devpriv->ao_readback[chan] = data[i];
127 }
128
129 return i;
130}
131
0a85b6f0
MT
132static int ni_670x_ao_rinsn(struct comedi_device *dev,
133 struct comedi_subdevice *s,
134 struct comedi_insn *insn, unsigned int *data)
3c5e1722 135{
289a4033 136 struct ni_670x_private *devpriv = dev->private;
3c5e1722
BJ
137 int i;
138 int chan = CR_CHAN(insn->chanspec);
139
140 for (i = 0; i < insn->n; i++)
141 data[i] = devpriv->ao_readback[chan];
142
143 return i;
144}
145
0a85b6f0
MT
146static int ni_670x_dio_insn_bits(struct comedi_device *dev,
147 struct comedi_subdevice *s,
148 struct comedi_insn *insn, unsigned int *data)
3c5e1722 149{
289a4033
HS
150 struct ni_670x_private *devpriv = dev->private;
151
3c5e1722
BJ
152 /* The insn data is a mask in data[0] and the new data
153 * in data[1], each channel cooresponding to a bit. */
154 if (data[0]) {
155 s->state &= ~data[0];
156 s->state |= data[0] & data[1];
157 writel(s->state,
0a85b6f0 158 devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
3c5e1722
BJ
159 }
160
161 /* on return, data[1] contains the value of the digital
162 * input lines. */
163 data[1] = readl(devpriv->mite->daq_io_addr + DIO_PORT0_DATA_OFFSET);
164
a2714e3e 165 return insn->n;
3c5e1722
BJ
166}
167
0a85b6f0
MT
168static int ni_670x_dio_insn_config(struct comedi_device *dev,
169 struct comedi_subdevice *s,
170 struct comedi_insn *insn, unsigned int *data)
3c5e1722 171{
289a4033 172 struct ni_670x_private *devpriv = dev->private;
3c5e1722
BJ
173 int chan = CR_CHAN(insn->chanspec);
174
175 switch (data[0]) {
176 case INSN_CONFIG_DIO_OUTPUT:
177 s->io_bits |= 1 << chan;
178 break;
179 case INSN_CONFIG_DIO_INPUT:
180 s->io_bits &= ~(1 << chan);
181 break;
182 case INSN_CONFIG_DIO_QUERY:
183 data[1] =
0a85b6f0 184 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
3c5e1722
BJ
185 return insn->n;
186 break;
187 default:
188 return -EINVAL;
189 break;
190 }
191 writel(s->io_bits, devpriv->mite->daq_io_addr + DIO_PORT0_DIR_OFFSET);
192
193 return insn->n;
194}
195
da91b269 196static int ni_670x_find_device(struct comedi_device *dev, int bus, int slot)
3c5e1722 197{
289a4033 198 struct ni_670x_private *devpriv = dev->private;
3c5e1722
BJ
199 struct mite_struct *mite;
200 int i;
201
202 for (mite = mite_devices; mite; mite = mite->next) {
203 if (mite->used)
204 continue;
205 if (bus || slot) {
206 if (bus != mite->pcidev->bus->number
0a85b6f0 207 || slot != PCI_SLOT(mite->pcidev->devfn))
3c5e1722
BJ
208 continue;
209 }
210
2be03665 211 for (i = 0; i < ARRAY_SIZE(ni_670x_boards); i++) {
3c5e1722
BJ
212 if (mite_device_id(mite) == ni_670x_boards[i].dev_id) {
213 dev->board_ptr = ni_670x_boards + i;
214 devpriv->mite = mite;
215
216 return 0;
217 }
218 }
219 }
b48ad330 220 dev_warn(dev->class_dev, "no device found\n");
3c5e1722
BJ
221 mite_list_devices();
222 return -EIO;
223}
3c323c01 224
944a2f11
HS
225static int ni_670x_attach(struct comedi_device *dev,
226 struct comedi_devconfig *it)
227{
289a4033
HS
228 const struct ni_670x_board *thisboard;
229 struct ni_670x_private *devpriv;
944a2f11
HS
230 struct comedi_subdevice *s;
231 int ret;
232 int i;
233
289a4033 234 ret = alloc_private(dev, sizeof(*devpriv));
944a2f11
HS
235 if (ret < 0)
236 return ret;
289a4033 237 devpriv = dev->private;
944a2f11
HS
238
239 ret = ni_670x_find_device(dev, it->options[0], it->options[1]);
240 if (ret < 0)
241 return ret;
289a4033 242 thisboard = comedi_board(dev);
944a2f11
HS
243
244 ret = mite_setup(devpriv->mite);
245 if (ret < 0) {
b48ad330 246 dev_warn(dev->class_dev, "error setting up mite\n");
944a2f11
HS
247 return ret;
248 }
249 dev->board_name = thisboard->name;
250 dev->irq = mite_irq(devpriv->mite);
944a2f11
HS
251
252 ret = comedi_alloc_subdevices(dev, 2);
253 if (ret)
254 return ret;
255
256 s = dev->subdevices + 0;
257 /* analog output subdevice */
258 s->type = COMEDI_SUBD_AO;
259 s->subdev_flags = SDF_WRITABLE;
260 s->n_chan = thisboard->ao_chans;
261 s->maxdata = 0xffff;
262 if (s->n_chan == 32) {
263 const struct comedi_lrange **range_table_list;
264
265 range_table_list = kmalloc(sizeof(struct comedi_lrange *) * 32,
266 GFP_KERNEL);
267 if (!range_table_list)
268 return -ENOMEM;
269 s->range_table_list = range_table_list;
270 for (i = 0; i < 16; i++) {
271 range_table_list[i] = &range_bipolar10;
272 range_table_list[16 + i] = &range_0_20mA;
273 }
274 } else {
275 s->range_table = &range_bipolar10;
276 }
277 s->insn_write = &ni_670x_ao_winsn;
278 s->insn_read = &ni_670x_ao_rinsn;
279
280 s = dev->subdevices + 1;
281 /* digital i/o subdevice */
282 s->type = COMEDI_SUBD_DIO;
283 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
284 s->n_chan = 8;
285 s->maxdata = 1;
286 s->range_table = &range_digital;
287 s->insn_bits = ni_670x_dio_insn_bits;
288 s->insn_config = ni_670x_dio_insn_config;
289
290 /* Config of misc registers */
291 writel(0x10, devpriv->mite->daq_io_addr + MISC_CONTROL_OFFSET);
292 /* Config of ao registers */
293 writel(0x00, devpriv->mite->daq_io_addr + AO_CONTROL_OFFSET);
294
b48ad330
HS
295 dev_info(dev->class_dev, "%s: %s attached\n",
296 dev->driver->driver_name, dev->board_name);
944a2f11 297
464c9451 298 return 0;
944a2f11
HS
299}
300
301static void ni_670x_detach(struct comedi_device *dev)
302{
289a4033 303 struct ni_670x_private *devpriv = dev->private;
70fcd1b7 304 struct comedi_subdevice *s;
289a4033 305
70fcd1b7
HS
306 if (dev->n_subdevices) {
307 s = dev->subdevices + 0;
308 if (s)
309 kfree(s->range_table_list);
310 }
289a4033 311 if (devpriv && devpriv->mite)
944a2f11
HS
312 mite_unsetup(devpriv->mite);
313 if (dev->irq)
314 free_irq(dev->irq, dev);
315}
316
6f37e288
HS
317static struct comedi_driver ni_670x_driver = {
318 .driver_name = "ni_670x",
319 .module = THIS_MODULE,
320 .attach = ni_670x_attach,
321 .detach = ni_670x_detach,
322};
323
324static int __devinit ni_670x_pci_probe(struct pci_dev *dev,
325 const struct pci_device_id *ent)
326{
327 return comedi_pci_auto_config(dev, &ni_670x_driver);
328}
329
330static void __devexit ni_670x_pci_remove(struct pci_dev *dev)
331{
332 comedi_pci_auto_unconfig(dev);
333}
334
335static DEFINE_PCI_DEVICE_TABLE(ni_670x_pci_table) = {
336 { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x2c90) },
337 { PCI_DEVICE(PCI_VENDOR_ID_NI, 0x1920) },
338 { 0 }
339};
340MODULE_DEVICE_TABLE(pci, ni_670x_pci_table);
341
342static struct pci_driver ni_670x_pci_driver = {
343 .name ="ni_670x",
344 .id_table = ni_670x_pci_table,
345 .probe = ni_670x_pci_probe,
346 .remove = __devexit_p(ni_670x_pci_remove),
347};
348module_comedi_pci_driver(ni_670x_driver, ni_670x_pci_driver);
349
3c323c01
IA
350MODULE_AUTHOR("Comedi http://www.comedi.org");
351MODULE_DESCRIPTION("Comedi low-level driver");
352MODULE_LICENSE("GPL");