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 static int __init driver_parport_init_module(void)
106 return comedi_driver_register(&driver_parport);
109 static void __exit driver_parport_cleanup_module(void)
111 comedi_driver_unregister(&driver_parport);
114 module_init(driver_parport_init_module);
115 module_exit(driver_parport_cleanup_module);
117 struct parport_private {
122 #define devpriv ((struct parport_private *)(dev->private))
124 static int parport_insn_a(struct comedi_device *dev, struct comedi_subdevice *s,
125 struct comedi_insn *insn, unsigned int *data)
128 devpriv->a_data &= ~data[0];
129 devpriv->a_data |= (data[0] & data[1]);
131 outb(devpriv->a_data, dev->iobase + PARPORT_A);
134 data[1] = inb(dev->iobase + PARPORT_A);
139 static int parport_insn_config_a(struct comedi_device *dev,
140 struct comedi_subdevice *s,
141 struct comedi_insn *insn, unsigned int *data)
145 devpriv->c_data &= ~(1 << 5);
148 devpriv->c_data |= (1 << 5);
150 outb(devpriv->c_data, dev->iobase + PARPORT_C);
155 static int parport_insn_b(struct comedi_device *dev, struct comedi_subdevice *s,
156 struct comedi_insn *insn, unsigned int *data)
159 /* should writes be ignored? */
163 data[1] = (inb(dev->iobase + PARPORT_B) >> 3);
168 static int parport_insn_c(struct comedi_device *dev, struct comedi_subdevice *s,
169 struct comedi_insn *insn, unsigned int *data)
173 devpriv->c_data &= ~data[0];
174 devpriv->c_data |= (data[0] & data[1]);
176 outb(devpriv->c_data, dev->iobase + PARPORT_C);
179 data[1] = devpriv->c_data & 0xf;
184 static int parport_intr_insn(struct comedi_device *dev,
185 struct comedi_subdevice *s,
186 struct comedi_insn *insn, unsigned int *data)
195 static int parport_intr_cmdtest(struct comedi_device *dev,
196 struct comedi_subdevice *s,
197 struct comedi_cmd *cmd)
204 tmp = cmd->start_src;
205 cmd->start_src &= TRIG_NOW;
206 if (!cmd->start_src || tmp != cmd->start_src)
209 tmp = cmd->scan_begin_src;
210 cmd->scan_begin_src &= TRIG_EXT;
211 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
214 tmp = cmd->convert_src;
215 cmd->convert_src &= TRIG_FOLLOW;
216 if (!cmd->convert_src || tmp != cmd->convert_src)
219 tmp = cmd->scan_end_src;
220 cmd->scan_end_src &= TRIG_COUNT;
221 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
225 cmd->stop_src &= TRIG_NONE;
226 if (!cmd->stop_src || tmp != cmd->stop_src)
232 /* step 2: ignored */
239 if (cmd->start_arg != 0) {
243 if (cmd->scan_begin_arg != 0) {
244 cmd->scan_begin_arg = 0;
247 if (cmd->convert_arg != 0) {
248 cmd->convert_arg = 0;
251 if (cmd->scan_end_arg != 1) {
252 cmd->scan_end_arg = 1;
255 if (cmd->stop_arg != 0) {
263 /* step 4: ignored */
271 static int parport_intr_cmd(struct comedi_device *dev,
272 struct comedi_subdevice *s)
274 devpriv->c_data |= 0x10;
275 outb(devpriv->c_data, dev->iobase + PARPORT_C);
277 devpriv->enable_irq = 1;
282 static int parport_intr_cancel(struct comedi_device *dev,
283 struct comedi_subdevice *s)
285 printk(KERN_DEBUG "parport_intr_cancel()\n");
287 devpriv->c_data &= ~0x10;
288 outb(devpriv->c_data, dev->iobase + PARPORT_C);
290 devpriv->enable_irq = 0;
295 static irqreturn_t parport_interrupt(int irq, void *d)
297 struct comedi_device *dev = d;
298 struct comedi_subdevice *s = dev->subdevices + 3;
300 if (!devpriv->enable_irq) {
301 printk(KERN_ERR "comedi_parport: bogus irq, ignored\n");
305 comedi_buf_put(s->async, 0);
306 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
308 comedi_event(dev, s);
312 static int parport_attach(struct comedi_device *dev,
313 struct comedi_devconfig *it)
317 unsigned long iobase;
318 struct comedi_subdevice *s;
320 iobase = it->options[0];
321 printk(KERN_INFO "comedi%d: parport: 0x%04lx ", dev->minor, iobase);
322 if (!request_region(iobase, PARPORT_SIZE, "parport (comedi)")) {
323 printk(KERN_ERR "I/O port conflict\n");
326 dev->iobase = iobase;
328 irq = it->options[1];
330 printk(KERN_INFO " irq=%u", irq);
331 ret = request_irq(irq, parport_interrupt, 0, "comedi_parport",
334 printk(KERN_ERR " irq not available\n");
339 dev->board_name = "parport";
341 ret = alloc_subdevices(dev, 4);
344 ret = alloc_private(dev, sizeof(struct parport_private));
348 s = dev->subdevices + 0;
349 s->type = COMEDI_SUBD_DIO;
350 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
353 s->range_table = &range_digital;
354 s->insn_bits = parport_insn_a;
355 s->insn_config = parport_insn_config_a;
357 s = dev->subdevices + 1;
358 s->type = COMEDI_SUBD_DI;
359 s->subdev_flags = SDF_READABLE;
362 s->range_table = &range_digital;
363 s->insn_bits = parport_insn_b;
365 s = dev->subdevices + 2;
366 s->type = COMEDI_SUBD_DO;
367 s->subdev_flags = SDF_WRITABLE;
370 s->range_table = &range_digital;
371 s->insn_bits = parport_insn_c;
373 s = dev->subdevices + 3;
375 dev->read_subdev = s;
376 s->type = COMEDI_SUBD_DI;
377 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
380 s->range_table = &range_digital;
381 s->insn_bits = parport_intr_insn;
382 s->do_cmdtest = parport_intr_cmdtest;
383 s->do_cmd = parport_intr_cmd;
384 s->cancel = parport_intr_cancel;
386 s->type = COMEDI_SUBD_UNUSED;
390 outb(devpriv->a_data, dev->iobase + PARPORT_A);
392 outb(devpriv->c_data, dev->iobase + PARPORT_C);
394 printk(KERN_INFO "\n");
398 static int parport_detach(struct comedi_device *dev)
400 printk(KERN_INFO "comedi%d: parport: remove\n", dev->minor);
403 release_region(dev->iobase, PARPORT_SIZE);
406 free_irq(dev->irq, dev);
411 MODULE_AUTHOR("Comedi http://www.comedi.org");
412 MODULE_DESCRIPTION("Comedi low-level driver");
413 MODULE_LICENSE("GPL");