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 #include "comedi_fc.h"
90 #define PARPORT_SIZE 3
96 struct parport_private {
102 static int parport_insn_a(struct comedi_device *dev, struct comedi_subdevice *s,
103 struct comedi_insn *insn, unsigned int *data)
105 struct parport_private *devpriv = dev->private;
108 devpriv->a_data &= ~data[0];
109 devpriv->a_data |= (data[0] & data[1]);
111 outb(devpriv->a_data, dev->iobase + PARPORT_A);
114 data[1] = inb(dev->iobase + PARPORT_A);
119 static int parport_insn_config_a(struct comedi_device *dev,
120 struct comedi_subdevice *s,
121 struct comedi_insn *insn, unsigned int *data)
123 struct parport_private *devpriv = dev->private;
127 devpriv->c_data &= ~(1 << 5);
130 devpriv->c_data |= (1 << 5);
132 outb(devpriv->c_data, dev->iobase + PARPORT_C);
137 static int parport_insn_b(struct comedi_device *dev, struct comedi_subdevice *s,
138 struct comedi_insn *insn, unsigned int *data)
141 /* should writes be ignored? */
145 data[1] = (inb(dev->iobase + PARPORT_B) >> 3);
150 static int parport_insn_c(struct comedi_device *dev, struct comedi_subdevice *s,
151 struct comedi_insn *insn, unsigned int *data)
153 struct parport_private *devpriv = dev->private;
157 devpriv->c_data &= ~data[0];
158 devpriv->c_data |= (data[0] & data[1]);
160 outb(devpriv->c_data, dev->iobase + PARPORT_C);
163 data[1] = devpriv->c_data & 0xf;
168 static int parport_intr_insn(struct comedi_device *dev,
169 struct comedi_subdevice *s,
170 struct comedi_insn *insn, unsigned int *data)
176 static int parport_intr_cmdtest(struct comedi_device *dev,
177 struct comedi_subdevice *s,
178 struct comedi_cmd *cmd)
182 /* Step 1 : check if triggers are trivially valid */
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);
193 /* Step 2a : make sure trigger sources are unique */
194 /* Step 2b : and mutually compatible */
201 if (cmd->start_arg != 0) {
205 if (cmd->scan_begin_arg != 0) {
206 cmd->scan_begin_arg = 0;
209 if (cmd->convert_arg != 0) {
210 cmd->convert_arg = 0;
213 if (cmd->scan_end_arg != 1) {
214 cmd->scan_end_arg = 1;
217 if (cmd->stop_arg != 0) {
225 /* step 4: ignored */
233 static int parport_intr_cmd(struct comedi_device *dev,
234 struct comedi_subdevice *s)
236 struct parport_private *devpriv = dev->private;
238 devpriv->c_data |= 0x10;
239 outb(devpriv->c_data, dev->iobase + PARPORT_C);
241 devpriv->enable_irq = 1;
246 static int parport_intr_cancel(struct comedi_device *dev,
247 struct comedi_subdevice *s)
249 struct parport_private *devpriv = dev->private;
251 devpriv->c_data &= ~0x10;
252 outb(devpriv->c_data, dev->iobase + PARPORT_C);
254 devpriv->enable_irq = 0;
259 static irqreturn_t parport_interrupt(int irq, void *d)
261 struct comedi_device *dev = d;
262 struct parport_private *devpriv = dev->private;
263 struct comedi_subdevice *s = &dev->subdevices[3];
265 if (!devpriv->enable_irq)
268 comedi_buf_put(s->async, 0);
269 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
271 comedi_event(dev, s);
275 static int parport_attach(struct comedi_device *dev,
276 struct comedi_devconfig *it)
278 struct parport_private *devpriv;
281 unsigned long iobase;
282 struct comedi_subdevice *s;
284 dev->board_name = dev->driver->driver_name;
286 iobase = it->options[0];
287 if (!request_region(iobase, PARPORT_SIZE, dev->board_name)) {
288 dev_err(dev->class_dev, "I/O port conflict\n");
291 dev->iobase = iobase;
293 irq = it->options[1];
295 ret = request_irq(irq, parport_interrupt, 0, dev->board_name,
298 dev_err(dev->class_dev, "irq not available\n");
304 ret = comedi_alloc_subdevices(dev, 4);
308 ret = alloc_private(dev, sizeof(*devpriv));
311 devpriv = dev->private;
313 s = &dev->subdevices[0];
314 s->type = COMEDI_SUBD_DIO;
315 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
318 s->range_table = &range_digital;
319 s->insn_bits = parport_insn_a;
320 s->insn_config = parport_insn_config_a;
322 s = &dev->subdevices[1];
323 s->type = COMEDI_SUBD_DI;
324 s->subdev_flags = SDF_READABLE;
327 s->range_table = &range_digital;
328 s->insn_bits = parport_insn_b;
330 s = &dev->subdevices[2];
331 s->type = COMEDI_SUBD_DO;
332 s->subdev_flags = SDF_WRITABLE;
335 s->range_table = &range_digital;
336 s->insn_bits = parport_insn_c;
338 s = &dev->subdevices[3];
340 dev->read_subdev = s;
341 s->type = COMEDI_SUBD_DI;
342 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
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;
351 s->type = COMEDI_SUBD_UNUSED;
355 outb(devpriv->a_data, dev->iobase + PARPORT_A);
357 outb(devpriv->c_data, dev->iobase + PARPORT_C);
359 dev_info(dev->class_dev, "%s: iobase=0x%04lx, irq %sabled",
360 dev->board_name, dev->iobase, dev->irq ? "en" : "dis");
365 static void parport_detach(struct comedi_device *dev)
368 release_region(dev->iobase, PARPORT_SIZE);
370 free_irq(dev->irq, dev);
373 static struct comedi_driver parport_driver = {
374 .driver_name = "comedi_parport",
375 .module = THIS_MODULE,
376 .attach = parport_attach,
377 .detach = parport_detach,
379 module_comedi_driver(parport_driver);
381 MODULE_AUTHOR("Comedi http://www.comedi.org");
382 MODULE_DESCRIPTION("Comedi low-level driver");
383 MODULE_LICENSE("GPL");