staging: comedi: remove inline alloc_private()
[deliverable/linux.git] / drivers / staging / comedi / drivers / comedi_parport.c
CommitLineData
241ab6ad
DS
1/*
2 comedi/drivers/comedi_parport.c
3 hardware driver for standard parallel port
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1998,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: comedi_parport
25Description: Standard PC parallel port
26Author: ds
27Status: works in immediate mode
28Devices: [standard] parallel port (comedi_parport)
29Updated: Tue, 30 Apr 2002 21:11:45 -0700
30
31A cheap and easy way to get a few more digital I/O lines. Steal
32additional parallel ports from old computers or your neighbors'
33computers.
34
35Option list:
36 0: I/O port base for the parallel port.
37 1: IRQ
38
39Parallel Port Lines:
40
41pin subdev chan aka
42--- ------ ---- ---
431 2 0 strobe
442 0 0 data 0
453 0 1 data 1
464 0 2 data 2
475 0 3 data 3
486 0 4 data 4
497 0 5 data 5
508 0 6 data 6
519 0 7 data 7
5210 1 3 acknowledge
5311 1 4 busy
5412 1 2 output
5513 1 1 printer selected
5614 2 1 auto LF
5715 1 0 error
5816 2 2 init
5917 2 3 select printer
6018-25 ground
61
62Notes:
63
64Subdevices 0 is digital I/O, subdevice 1 is digital input, and
65subdevice 2 is digital output. Unlike other Comedi devices,
66subdevice 0 defaults to output.
67
68Pins 13 and 14 are inverted once by Comedi and once by the
69hardware, thus cancelling the effect.
70
71Pin 1 is a strobe, thus acts like one. There's no way in software
72to change this, at least on a standard parallel port.
73
74Subdevice 3 pretends to be a digital input subdevice, but it always
75returns 0 when read. However, if you run a command with
76scan_begin_src=TRIG_EXT, it uses pin 10 as a external triggering
77pin, which can be used to wake up tasks.
78*/
79/*
80 see http://www.beyondlogic.org/ for information.
81 or http://www.linux-magazin.de/ausgabe/1999/10/IO/io.html
82 */
83
84#include "../comedidev.h"
70265d24 85#include <linux/interrupt.h>
241ab6ad
DS
86#include <linux/ioport.h>
87
27020ffe
HS
88#include "comedi_fc.h"
89
241ab6ad
DS
90#define PARPORT_SIZE 3
91
92#define PARPORT_A 0
93#define PARPORT_B 1
94#define PARPORT_C 2
95
652dd4e7 96struct parport_private {
241ab6ad
DS
97 unsigned int a_data;
98 unsigned int c_data;
99 int enable_irq;
652dd4e7 100};
241ab6ad 101
34c43922 102static int parport_insn_a(struct comedi_device *dev, struct comedi_subdevice *s,
90035c08 103 struct comedi_insn *insn, unsigned int *data)
241ab6ad 104{
d3bcf099
HS
105 struct parport_private *devpriv = dev->private;
106
241ab6ad
DS
107 if (data[0]) {
108 devpriv->a_data &= ~data[0];
109 devpriv->a_data |= (data[0] & data[1]);
110
111 outb(devpriv->a_data, dev->iobase + PARPORT_A);
112 }
113
114 data[1] = inb(dev->iobase + PARPORT_A);
115
a2714e3e 116 return insn->n;
241ab6ad
DS
117}
118
0a85b6f0
MT
119static int parport_insn_config_a(struct comedi_device *dev,
120 struct comedi_subdevice *s,
90035c08 121 struct comedi_insn *insn, unsigned int *data)
241ab6ad 122{
d3bcf099
HS
123 struct parport_private *devpriv = dev->private;
124
241ab6ad
DS
125 if (data[0]) {
126 s->io_bits = 0xff;
127 devpriv->c_data &= ~(1 << 5);
128 } else {
129 s->io_bits = 0;
130 devpriv->c_data |= (1 << 5);
131 }
132 outb(devpriv->c_data, dev->iobase + PARPORT_C);
133
134 return 1;
135}
136
34c43922 137static int parport_insn_b(struct comedi_device *dev, struct comedi_subdevice *s,
90035c08 138 struct comedi_insn *insn, unsigned int *data)
241ab6ad
DS
139{
140 if (data[0]) {
652dd4e7
GKH
141 /* should writes be ignored? */
142 /* anyone??? */
241ab6ad
DS
143 }
144
145 data[1] = (inb(dev->iobase + PARPORT_B) >> 3);
146
a2714e3e 147 return insn->n;
241ab6ad
DS
148}
149
34c43922 150static int parport_insn_c(struct comedi_device *dev, struct comedi_subdevice *s,
90035c08 151 struct comedi_insn *insn, unsigned int *data)
241ab6ad 152{
d3bcf099
HS
153 struct parport_private *devpriv = dev->private;
154
241ab6ad
DS
155 data[0] &= 0x0f;
156 if (data[0]) {
157 devpriv->c_data &= ~data[0];
158 devpriv->c_data |= (data[0] & data[1]);
159
160 outb(devpriv->c_data, dev->iobase + PARPORT_C);
161 }
162
163 data[1] = devpriv->c_data & 0xf;
164
a2714e3e 165 return insn->n;
241ab6ad
DS
166}
167
0a85b6f0
MT
168static int parport_intr_insn(struct comedi_device *dev,
169 struct comedi_subdevice *s,
90035c08 170 struct comedi_insn *insn, unsigned int *data)
241ab6ad 171{
241ab6ad 172 data[1] = 0;
a2714e3e 173 return insn->n;
241ab6ad
DS
174}
175
0a85b6f0
MT
176static int parport_intr_cmdtest(struct comedi_device *dev,
177 struct comedi_subdevice *s,
ea6d0d4c 178 struct comedi_cmd *cmd)
241ab6ad
DS
179{
180 int err = 0;
241ab6ad 181
27020ffe 182 /* Step 1 : check if triggers are trivially valid */
241ab6ad 183
27020ffe
HS
184 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
185 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
186 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
187 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
188 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_NONE);
241ab6ad
DS
189
190 if (err)
191 return 1;
192
27020ffe
HS
193 /* Step 2a : make sure trigger sources are unique */
194 /* Step 2b : and mutually compatible */
241ab6ad
DS
195
196 if (err)
197 return 2;
198
199 /* step 3: */
200
201 if (cmd->start_arg != 0) {
202 cmd->start_arg = 0;
203 err++;
204 }
205 if (cmd->scan_begin_arg != 0) {
206 cmd->scan_begin_arg = 0;
207 err++;
208 }
209 if (cmd->convert_arg != 0) {
210 cmd->convert_arg = 0;
211 err++;
212 }
213 if (cmd->scan_end_arg != 1) {
214 cmd->scan_end_arg = 1;
215 err++;
216 }
217 if (cmd->stop_arg != 0) {
218 cmd->stop_arg = 0;
219 err++;
220 }
221
222 if (err)
223 return 3;
224
225 /* step 4: ignored */
226
227 if (err)
228 return 4;
229
230 return 0;
231}
232
0a85b6f0
MT
233static int parport_intr_cmd(struct comedi_device *dev,
234 struct comedi_subdevice *s)
241ab6ad 235{
d3bcf099
HS
236 struct parport_private *devpriv = dev->private;
237
241ab6ad
DS
238 devpriv->c_data |= 0x10;
239 outb(devpriv->c_data, dev->iobase + PARPORT_C);
240
241 devpriv->enable_irq = 1;
242
243 return 0;
244}
245
0a85b6f0
MT
246static int parport_intr_cancel(struct comedi_device *dev,
247 struct comedi_subdevice *s)
241ab6ad 248{
d3bcf099
HS
249 struct parport_private *devpriv = dev->private;
250
241ab6ad
DS
251 devpriv->c_data &= ~0x10;
252 outb(devpriv->c_data, dev->iobase + PARPORT_C);
253
254 devpriv->enable_irq = 0;
255
256 return 0;
257}
258
70265d24 259static irqreturn_t parport_interrupt(int irq, void *d)
241ab6ad 260{
71b5f4f1 261 struct comedi_device *dev = d;
d3bcf099 262 struct parport_private *devpriv = dev->private;
9da5ae29 263 struct comedi_subdevice *s = &dev->subdevices[3];
241ab6ad 264
06331fb0 265 if (!devpriv->enable_irq)
241ab6ad 266 return IRQ_NONE;
241ab6ad
DS
267
268 comedi_buf_put(s->async, 0);
269 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
270
271 comedi_event(dev, s);
272 return IRQ_HANDLED;
273}
274
0a85b6f0
MT
275static int parport_attach(struct comedi_device *dev,
276 struct comedi_devconfig *it)
241ab6ad 277{
d3bcf099 278 struct parport_private *devpriv;
241ab6ad
DS
279 int ret;
280 unsigned int irq;
281 unsigned long iobase;
34c43922 282 struct comedi_subdevice *s;
241ab6ad 283
45d63700
HS
284 dev->board_name = dev->driver->driver_name;
285
241ab6ad 286 iobase = it->options[0];
45d63700 287 if (!request_region(iobase, PARPORT_SIZE, dev->board_name)) {
b8d0f3ae 288 dev_err(dev->class_dev, "I/O port conflict\n");
241ab6ad
DS
289 return -EIO;
290 }
291 dev->iobase = iobase;
292
293 irq = it->options[1];
294 if (irq) {
45d63700 295 ret = request_irq(irq, parport_interrupt, 0, dev->board_name,
5f74ea14 296 dev);
241ab6ad 297 if (ret < 0) {
b8d0f3ae 298 dev_err(dev->class_dev, "irq not available\n");
241ab6ad
DS
299 return -EINVAL;
300 }
301 dev->irq = irq;
302 }
241ab6ad 303
2f0b9d08 304 ret = comedi_alloc_subdevices(dev, 4);
8b6c5694 305 if (ret)
241ab6ad 306 return ret;
8b6c5694 307
c34fa261
HS
308 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
309 if (!devpriv)
310 return -ENOMEM;
311 dev->private = devpriv;
241ab6ad 312
9da5ae29 313 s = &dev->subdevices[0];
241ab6ad
DS
314 s->type = COMEDI_SUBD_DIO;
315 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
316 s->n_chan = 8;
317 s->maxdata = 1;
318 s->range_table = &range_digital;
319 s->insn_bits = parport_insn_a;
320 s->insn_config = parport_insn_config_a;
321
9da5ae29 322 s = &dev->subdevices[1];
241ab6ad
DS
323 s->type = COMEDI_SUBD_DI;
324 s->subdev_flags = SDF_READABLE;
325 s->n_chan = 5;
326 s->maxdata = 1;
327 s->range_table = &range_digital;
328 s->insn_bits = parport_insn_b;
329
9da5ae29 330 s = &dev->subdevices[2];
241ab6ad
DS
331 s->type = COMEDI_SUBD_DO;
332 s->subdev_flags = SDF_WRITABLE;
333 s->n_chan = 4;
334 s->maxdata = 1;
335 s->range_table = &range_digital;
336 s->insn_bits = parport_insn_c;
337
9da5ae29 338 s = &dev->subdevices[3];
241ab6ad
DS
339 if (irq) {
340 dev->read_subdev = s;
341 s->type = COMEDI_SUBD_DI;
342 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
343 s->n_chan = 1;
344 s->maxdata = 1;
345 s->range_table = &range_digital;
346 s->insn_bits = parport_intr_insn;
347 s->do_cmdtest = parport_intr_cmdtest;
348 s->do_cmd = parport_intr_cmd;
349 s->cancel = parport_intr_cancel;
350 } else {
351 s->type = COMEDI_SUBD_UNUSED;
352 }
353
354 devpriv->a_data = 0;
355 outb(devpriv->a_data, dev->iobase + PARPORT_A);
356 devpriv->c_data = 0;
357 outb(devpriv->c_data, dev->iobase + PARPORT_C);
358
b8d0f3ae
HS
359 dev_info(dev->class_dev, "%s: iobase=0x%04lx, irq %sabled",
360 dev->board_name, dev->iobase, dev->irq ? "en" : "dis");
361
362 return 0;
241ab6ad
DS
363}
364
484ecc95 365static void parport_detach(struct comedi_device *dev)
241ab6ad 366{
241ab6ad
DS
367 if (dev->iobase)
368 release_region(dev->iobase, PARPORT_SIZE);
241ab6ad 369 if (dev->irq)
5f74ea14 370 free_irq(dev->irq, dev);
241ab6ad 371}
90f703d3 372
bfba5e78
HS
373static struct comedi_driver parport_driver = {
374 .driver_name = "comedi_parport",
375 .module = THIS_MODULE,
376 .attach = parport_attach,
377 .detach = parport_detach,
378};
379module_comedi_driver(parport_driver);
380
90f703d3
AT
381MODULE_AUTHOR("Comedi http://www.comedi.org");
382MODULE_DESCRIPTION("Comedi low-level driver");
383MODULE_LICENSE("GPL");
This page took 0.458324 seconds and 5 git commands to generate.