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
, struct comedi_devconfig
*it
);
95 static int parport_detach(struct comedi_device
*dev
);
96 static struct comedi_driver driver_parport
= {
97 .driver_name
= "comedi_parport",
98 .module
= THIS_MODULE
,
99 .attach
= parport_attach
,
100 .detach
= parport_detach
,
103 COMEDI_INITCLEANUP(driver_parport
);
105 struct parport_private
{
110 #define devpriv ((struct parport_private *)(dev->private))
112 static int parport_insn_a(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
113 struct comedi_insn
*insn
, unsigned int *data
)
116 devpriv
->a_data
&= ~data
[0];
117 devpriv
->a_data
|= (data
[0] & data
[1]);
119 outb(devpriv
->a_data
, dev
->iobase
+ PARPORT_A
);
122 data
[1] = inb(dev
->iobase
+ PARPORT_A
);
127 static int parport_insn_config_a(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
128 struct comedi_insn
*insn
, unsigned int *data
)
132 devpriv
->c_data
&= ~(1 << 5);
135 devpriv
->c_data
|= (1 << 5);
137 outb(devpriv
->c_data
, dev
->iobase
+ PARPORT_C
);
142 static int parport_insn_b(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
143 struct comedi_insn
*insn
, unsigned int *data
)
146 /* should writes be ignored? */
150 data
[1] = (inb(dev
->iobase
+ PARPORT_B
) >> 3);
155 static int parport_insn_c(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
156 struct comedi_insn
*insn
, unsigned int *data
)
160 devpriv
->c_data
&= ~data
[0];
161 devpriv
->c_data
|= (data
[0] & data
[1]);
163 outb(devpriv
->c_data
, dev
->iobase
+ PARPORT_C
);
166 data
[1] = devpriv
->c_data
& 0xf;
171 static int parport_intr_insn(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
172 struct comedi_insn
*insn
, unsigned int *data
)
181 static int parport_intr_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
182 struct comedi_cmd
*cmd
)
189 tmp
= cmd
->start_src
;
190 cmd
->start_src
&= TRIG_NOW
;
191 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
194 tmp
= cmd
->scan_begin_src
;
195 cmd
->scan_begin_src
&= TRIG_EXT
;
196 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
199 tmp
= cmd
->convert_src
;
200 cmd
->convert_src
&= TRIG_FOLLOW
;
201 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
204 tmp
= cmd
->scan_end_src
;
205 cmd
->scan_end_src
&= TRIG_COUNT
;
206 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
210 cmd
->stop_src
&= TRIG_NONE
;
211 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
217 /* step 2: ignored */
224 if (cmd
->start_arg
!= 0) {
228 if (cmd
->scan_begin_arg
!= 0) {
229 cmd
->scan_begin_arg
= 0;
232 if (cmd
->convert_arg
!= 0) {
233 cmd
->convert_arg
= 0;
236 if (cmd
->scan_end_arg
!= 1) {
237 cmd
->scan_end_arg
= 1;
240 if (cmd
->stop_arg
!= 0) {
248 /* step 4: ignored */
256 static int parport_intr_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
258 devpriv
->c_data
|= 0x10;
259 outb(devpriv
->c_data
, dev
->iobase
+ PARPORT_C
);
261 devpriv
->enable_irq
= 1;
266 static int parport_intr_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
268 printk(KERN_DEBUG
"parport_intr_cancel()\n");
270 devpriv
->c_data
&= ~0x10;
271 outb(devpriv
->c_data
, dev
->iobase
+ PARPORT_C
);
273 devpriv
->enable_irq
= 0;
278 static irqreturn_t
parport_interrupt(int irq
, void *d
)
280 struct comedi_device
*dev
= d
;
281 struct comedi_subdevice
*s
= dev
->subdevices
+ 3;
283 if (!devpriv
->enable_irq
) {
284 printk(KERN_ERR
"comedi_parport: bogus irq, ignored\n");
288 comedi_buf_put(s
->async
, 0);
289 s
->async
->events
|= COMEDI_CB_BLOCK
| COMEDI_CB_EOS
;
291 comedi_event(dev
, s
);
295 static int parport_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
299 unsigned long iobase
;
300 struct comedi_subdevice
*s
;
302 iobase
= it
->options
[0];
303 printk(KERN_INFO
"comedi%d: parport: 0x%04lx ", dev
->minor
, iobase
);
304 if (!request_region(iobase
, PARPORT_SIZE
, "parport (comedi)")) {
305 printk("I/O port conflict\n");
308 dev
->iobase
= iobase
;
310 irq
= it
->options
[1];
312 printk(" irq=%u", irq
);
313 ret
= request_irq(irq
, parport_interrupt
, 0, "comedi_parport",
316 printk(" irq not available\n");
321 dev
->board_name
= "parport";
323 ret
= alloc_subdevices(dev
, 4);
326 ret
= alloc_private(dev
, sizeof(struct parport_private
));
330 s
= dev
->subdevices
+ 0;
331 s
->type
= COMEDI_SUBD_DIO
;
332 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
335 s
->range_table
= &range_digital
;
336 s
->insn_bits
= parport_insn_a
;
337 s
->insn_config
= parport_insn_config_a
;
339 s
= dev
->subdevices
+ 1;
340 s
->type
= COMEDI_SUBD_DI
;
341 s
->subdev_flags
= SDF_READABLE
;
344 s
->range_table
= &range_digital
;
345 s
->insn_bits
= parport_insn_b
;
347 s
= dev
->subdevices
+ 2;
348 s
->type
= COMEDI_SUBD_DO
;
349 s
->subdev_flags
= SDF_WRITABLE
;
352 s
->range_table
= &range_digital
;
353 s
->insn_bits
= parport_insn_c
;
355 s
= dev
->subdevices
+ 3;
357 dev
->read_subdev
= s
;
358 s
->type
= COMEDI_SUBD_DI
;
359 s
->subdev_flags
= SDF_READABLE
| SDF_CMD_READ
;
362 s
->range_table
= &range_digital
;
363 s
->insn_bits
= parport_intr_insn
;
364 s
->do_cmdtest
= parport_intr_cmdtest
;
365 s
->do_cmd
= parport_intr_cmd
;
366 s
->cancel
= parport_intr_cancel
;
368 s
->type
= COMEDI_SUBD_UNUSED
;
372 outb(devpriv
->a_data
, dev
->iobase
+ PARPORT_A
);
374 outb(devpriv
->c_data
, dev
->iobase
+ PARPORT_C
);
380 static int parport_detach(struct comedi_device
*dev
)
382 printk("comedi%d: parport: remove\n", dev
->minor
);
385 release_region(dev
->iobase
, PARPORT_SIZE
);
388 free_irq(dev
->irq
, dev
);