2 comedi/drivers/comedi_parport.c
3 hardware driver for standard parallel port
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1998,2001 David A. Schleef <ds@schleef.org>
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.
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.
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.
24 Driver: comedi_parport
25 Description: Standard PC parallel port
27 Status: works in immediate mode
28 Devices: [standard] parallel port (comedi_parport)
29 Updated: Tue, 30 Apr 2002 21:11:45 -0700
31 A cheap and easy way to get a few more digital I/O lines. Steal
32 additional parallel ports from old computers or your neighbors'
36 0: I/O port base for the parallel port.
55 13 1 1 printer selected
64 Subdevices 0 is digital I/O, subdevice 1 is digital input, and
65 subdevice 2 is digital output. Unlike other Comedi devices,
66 subdevice 0 defaults to output.
68 Pins 13 and 14 are inverted once by Comedi and once by the
69 hardware, thus cancelling the effect.
71 Pin 1 is a strobe, thus acts like one. There's no way in software
72 to change this, at least on a standard parallel port.
74 Subdevice 3 pretends to be a digital input subdevice, but it always
75 returns 0 when read. However, if you run a command with
76 scan_begin_src=TRIG_EXT, it uses pin 10 as a external triggering
77 pin, which can be used to wake up tasks.
80 see http://www.beyondlogic.org/ for information.
81 or http://www.linux-magazin.de/ausgabe/1999/10/IO/io.html
84 #include "../comedidev.h"
85 #include <linux/interrupt.h>
86 #include <linux/ioport.h>
88 #define PARPORT_SIZE 3
94 static int parport_attach(struct comedi_device *dev,
95 struct comedi_devconfig *it);
96 static int parport_detach(struct comedi_device *dev);
97 static struct comedi_driver driver_parport = {
98 .driver_name = "comedi_parport",
99 .module = THIS_MODULE,
100 .attach = parport_attach,
101 .detach = parport_detach,
104 COMEDI_INITCLEANUP(driver_parport);
106 struct parport_private {
111 #define devpriv ((struct parport_private *)(dev->private))
113 static int parport_insn_a(struct comedi_device *dev, struct comedi_subdevice *s,
114 struct comedi_insn *insn, unsigned int *data)
117 devpriv->a_data &= ~data[0];
118 devpriv->a_data |= (data[0] & data[1]);
120 outb(devpriv->a_data, dev->iobase + PARPORT_A);
123 data[1] = inb(dev->iobase + PARPORT_A);
128 static int parport_insn_config_a(struct comedi_device *dev,
129 struct comedi_subdevice *s,
130 struct comedi_insn *insn, unsigned int *data)
134 devpriv->c_data &= ~(1 << 5);
137 devpriv->c_data |= (1 << 5);
139 outb(devpriv->c_data, dev->iobase + PARPORT_C);
144 static int parport_insn_b(struct comedi_device *dev, struct comedi_subdevice *s,
145 struct comedi_insn *insn, unsigned int *data)
148 /* should writes be ignored? */
152 data[1] = (inb(dev->iobase + PARPORT_B) >> 3);
157 static int parport_insn_c(struct comedi_device *dev, struct comedi_subdevice *s,
158 struct comedi_insn *insn, unsigned int *data)
162 devpriv->c_data &= ~data[0];
163 devpriv->c_data |= (data[0] & data[1]);
165 outb(devpriv->c_data, dev->iobase + PARPORT_C);
168 data[1] = devpriv->c_data & 0xf;
173 static int parport_intr_insn(struct comedi_device *dev,
174 struct comedi_subdevice *s,
175 struct comedi_insn *insn, unsigned int *data)
184 static int parport_intr_cmdtest(struct comedi_device *dev,
185 struct comedi_subdevice *s,
186 struct comedi_cmd *cmd)
193 tmp = cmd->start_src;
194 cmd->start_src &= TRIG_NOW;
195 if (!cmd->start_src || tmp != cmd->start_src)
198 tmp = cmd->scan_begin_src;
199 cmd->scan_begin_src &= TRIG_EXT;
200 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
203 tmp = cmd->convert_src;
204 cmd->convert_src &= TRIG_FOLLOW;
205 if (!cmd->convert_src || tmp != cmd->convert_src)
208 tmp = cmd->scan_end_src;
209 cmd->scan_end_src &= TRIG_COUNT;
210 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
214 cmd->stop_src &= TRIG_NONE;
215 if (!cmd->stop_src || tmp != cmd->stop_src)
221 /* step 2: ignored */
228 if (cmd->start_arg != 0) {
232 if (cmd->scan_begin_arg != 0) {
233 cmd->scan_begin_arg = 0;
236 if (cmd->convert_arg != 0) {
237 cmd->convert_arg = 0;
240 if (cmd->scan_end_arg != 1) {
241 cmd->scan_end_arg = 1;
244 if (cmd->stop_arg != 0) {
252 /* step 4: ignored */
260 static int parport_intr_cmd(struct comedi_device *dev,
261 struct comedi_subdevice *s)
263 devpriv->c_data |= 0x10;
264 outb(devpriv->c_data, dev->iobase + PARPORT_C);
266 devpriv->enable_irq = 1;
271 static int parport_intr_cancel(struct comedi_device *dev,
272 struct comedi_subdevice *s)
274 printk(KERN_DEBUG "parport_intr_cancel()\n");
276 devpriv->c_data &= ~0x10;
277 outb(devpriv->c_data, dev->iobase + PARPORT_C);
279 devpriv->enable_irq = 0;
284 static irqreturn_t parport_interrupt(int irq, void *d)
286 struct comedi_device *dev = d;
287 struct comedi_subdevice *s = dev->subdevices + 3;
289 if (!devpriv->enable_irq) {
290 printk(KERN_ERR "comedi_parport: bogus irq, ignored\n");
294 comedi_buf_put(s->async, 0);
295 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
297 comedi_event(dev, s);
301 static int parport_attach(struct comedi_device *dev,
302 struct comedi_devconfig *it)
306 unsigned long iobase;
307 struct comedi_subdevice *s;
309 iobase = it->options[0];
310 printk(KERN_INFO "comedi%d: parport: 0x%04lx ", dev->minor, iobase);
311 if (!request_region(iobase, PARPORT_SIZE, "parport (comedi)")) {
312 printk("I/O port conflict\n");
315 dev->iobase = iobase;
317 irq = it->options[1];
319 printk(" irq=%u", irq);
320 ret = request_irq(irq, parport_interrupt, 0, "comedi_parport",
323 printk(" irq not available\n");
328 dev->board_name = "parport";
330 ret = alloc_subdevices(dev, 4);
333 ret = alloc_private(dev, sizeof(struct parport_private));
337 s = dev->subdevices + 0;
338 s->type = COMEDI_SUBD_DIO;
339 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
342 s->range_table = &range_digital;
343 s->insn_bits = parport_insn_a;
344 s->insn_config = parport_insn_config_a;
346 s = dev->subdevices + 1;
347 s->type = COMEDI_SUBD_DI;
348 s->subdev_flags = SDF_READABLE;
351 s->range_table = &range_digital;
352 s->insn_bits = parport_insn_b;
354 s = dev->subdevices + 2;
355 s->type = COMEDI_SUBD_DO;
356 s->subdev_flags = SDF_WRITABLE;
359 s->range_table = &range_digital;
360 s->insn_bits = parport_insn_c;
362 s = dev->subdevices + 3;
364 dev->read_subdev = s;
365 s->type = COMEDI_SUBD_DI;
366 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
369 s->range_table = &range_digital;
370 s->insn_bits = parport_intr_insn;
371 s->do_cmdtest = parport_intr_cmdtest;
372 s->do_cmd = parport_intr_cmd;
373 s->cancel = parport_intr_cancel;
375 s->type = COMEDI_SUBD_UNUSED;
379 outb(devpriv->a_data, dev->iobase + PARPORT_A);
381 outb(devpriv->c_data, dev->iobase + PARPORT_C);
387 static int parport_detach(struct comedi_device *dev)
389 printk("comedi%d: parport: remove\n", dev->minor);
392 release_region(dev->iobase, PARPORT_SIZE);
395 free_irq(dev->irq, dev);