]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/comedi_parport.c
Merge branch 'i2c-embedded/for-current' of git://git.pengutronix.de/git/wsa/linux
[karo-tx-linux.git] / drivers / staging / comedi / drivers / comedi_parport.c
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 /*
24 Driver: comedi_parport
25 Description: Standard PC parallel port
26 Author: ds
27 Status: works in immediate mode
28 Devices: [standard] parallel port (comedi_parport)
29 Updated: Tue, 30 Apr 2002 21:11:45 -0700
30
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'
33 computers.
34
35 Option list:
36  0: I/O port base for the parallel port.
37  1: IRQ
38
39 Parallel Port Lines:
40
41 pin     subdev  chan    aka
42 ---     ------  ----    ---
43 1       2       0       strobe
44 2       0       0       data 0
45 3       0       1       data 1
46 4       0       2       data 2
47 5       0       3       data 3
48 6       0       4       data 4
49 7       0       5       data 5
50 8       0       6       data 6
51 9       0       7       data 7
52 10      1       3       acknowledge
53 11      1       4       busy
54 12      1       2       output
55 13      1       1       printer selected
56 14      2       1       auto LF
57 15      1       0       error
58 16      2       2       init
59 17      2       3       select printer
60 18-25   ground
61
62 Notes:
63
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.
67
68 Pins 13 and 14 are inverted once by Comedi and once by the
69 hardware, thus cancelling the effect.
70
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.
73
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.
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"
85 #include <linux/interrupt.h>
86 #include <linux/ioport.h>
87
88 #define PARPORT_SIZE 3
89
90 #define PARPORT_A 0
91 #define PARPORT_B 1
92 #define PARPORT_C 2
93
94 struct parport_private {
95         unsigned int a_data;
96         unsigned int c_data;
97         int enable_irq;
98 };
99 #define devpriv ((struct parport_private *)(dev->private))
100
101 static int parport_insn_a(struct comedi_device *dev, struct comedi_subdevice *s,
102                           struct comedi_insn *insn, unsigned int *data)
103 {
104         if (data[0]) {
105                 devpriv->a_data &= ~data[0];
106                 devpriv->a_data |= (data[0] & data[1]);
107
108                 outb(devpriv->a_data, dev->iobase + PARPORT_A);
109         }
110
111         data[1] = inb(dev->iobase + PARPORT_A);
112
113         return insn->n;
114 }
115
116 static int parport_insn_config_a(struct comedi_device *dev,
117                                  struct comedi_subdevice *s,
118                                  struct comedi_insn *insn, unsigned int *data)
119 {
120         if (data[0]) {
121                 s->io_bits = 0xff;
122                 devpriv->c_data &= ~(1 << 5);
123         } else {
124                 s->io_bits = 0;
125                 devpriv->c_data |= (1 << 5);
126         }
127         outb(devpriv->c_data, dev->iobase + PARPORT_C);
128
129         return 1;
130 }
131
132 static int parport_insn_b(struct comedi_device *dev, struct comedi_subdevice *s,
133                           struct comedi_insn *insn, unsigned int *data)
134 {
135         if (data[0]) {
136                 /* should writes be ignored? */
137                 /* anyone??? */
138         }
139
140         data[1] = (inb(dev->iobase + PARPORT_B) >> 3);
141
142         return insn->n;
143 }
144
145 static int parport_insn_c(struct comedi_device *dev, struct comedi_subdevice *s,
146                           struct comedi_insn *insn, unsigned int *data)
147 {
148         data[0] &= 0x0f;
149         if (data[0]) {
150                 devpriv->c_data &= ~data[0];
151                 devpriv->c_data |= (data[0] & data[1]);
152
153                 outb(devpriv->c_data, dev->iobase + PARPORT_C);
154         }
155
156         data[1] = devpriv->c_data & 0xf;
157
158         return insn->n;
159 }
160
161 static int parport_intr_insn(struct comedi_device *dev,
162                              struct comedi_subdevice *s,
163                              struct comedi_insn *insn, unsigned int *data)
164 {
165         data[1] = 0;
166         return insn->n;
167 }
168
169 static int parport_intr_cmdtest(struct comedi_device *dev,
170                                 struct comedi_subdevice *s,
171                                 struct comedi_cmd *cmd)
172 {
173         int err = 0;
174         int tmp;
175
176         /* step 1 */
177
178         tmp = cmd->start_src;
179         cmd->start_src &= TRIG_NOW;
180         if (!cmd->start_src || tmp != cmd->start_src)
181                 err++;
182
183         tmp = cmd->scan_begin_src;
184         cmd->scan_begin_src &= TRIG_EXT;
185         if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
186                 err++;
187
188         tmp = cmd->convert_src;
189         cmd->convert_src &= TRIG_FOLLOW;
190         if (!cmd->convert_src || tmp != cmd->convert_src)
191                 err++;
192
193         tmp = cmd->scan_end_src;
194         cmd->scan_end_src &= TRIG_COUNT;
195         if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
196                 err++;
197
198         tmp = cmd->stop_src;
199         cmd->stop_src &= TRIG_NONE;
200         if (!cmd->stop_src || tmp != cmd->stop_src)
201                 err++;
202
203         if (err)
204                 return 1;
205
206         /* step 2: ignored */
207
208         if (err)
209                 return 2;
210
211         /* step 3: */
212
213         if (cmd->start_arg != 0) {
214                 cmd->start_arg = 0;
215                 err++;
216         }
217         if (cmd->scan_begin_arg != 0) {
218                 cmd->scan_begin_arg = 0;
219                 err++;
220         }
221         if (cmd->convert_arg != 0) {
222                 cmd->convert_arg = 0;
223                 err++;
224         }
225         if (cmd->scan_end_arg != 1) {
226                 cmd->scan_end_arg = 1;
227                 err++;
228         }
229         if (cmd->stop_arg != 0) {
230                 cmd->stop_arg = 0;
231                 err++;
232         }
233
234         if (err)
235                 return 3;
236
237         /* step 4: ignored */
238
239         if (err)
240                 return 4;
241
242         return 0;
243 }
244
245 static int parport_intr_cmd(struct comedi_device *dev,
246                             struct comedi_subdevice *s)
247 {
248         devpriv->c_data |= 0x10;
249         outb(devpriv->c_data, dev->iobase + PARPORT_C);
250
251         devpriv->enable_irq = 1;
252
253         return 0;
254 }
255
256 static int parport_intr_cancel(struct comedi_device *dev,
257                                struct comedi_subdevice *s)
258 {
259         printk(KERN_DEBUG "parport_intr_cancel()\n");
260
261         devpriv->c_data &= ~0x10;
262         outb(devpriv->c_data, dev->iobase + PARPORT_C);
263
264         devpriv->enable_irq = 0;
265
266         return 0;
267 }
268
269 static irqreturn_t parport_interrupt(int irq, void *d)
270 {
271         struct comedi_device *dev = d;
272         struct comedi_subdevice *s = dev->subdevices + 3;
273
274         if (!devpriv->enable_irq) {
275                 printk(KERN_ERR "comedi_parport: bogus irq, ignored\n");
276                 return IRQ_NONE;
277         }
278
279         comedi_buf_put(s->async, 0);
280         s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
281
282         comedi_event(dev, s);
283         return IRQ_HANDLED;
284 }
285
286 static int parport_attach(struct comedi_device *dev,
287                           struct comedi_devconfig *it)
288 {
289         int ret;
290         unsigned int irq;
291         unsigned long iobase;
292         struct comedi_subdevice *s;
293
294         iobase = it->options[0];
295         printk(KERN_INFO "comedi%d: parport: 0x%04lx ", dev->minor, iobase);
296         if (!request_region(iobase, PARPORT_SIZE, "parport (comedi)")) {
297                 printk(KERN_ERR "I/O port conflict\n");
298                 return -EIO;
299         }
300         dev->iobase = iobase;
301
302         irq = it->options[1];
303         if (irq) {
304                 printk(KERN_INFO " irq=%u", irq);
305                 ret = request_irq(irq, parport_interrupt, 0, "comedi_parport",
306                                   dev);
307                 if (ret < 0) {
308                         printk(KERN_ERR " irq not available\n");
309                         return -EINVAL;
310                 }
311                 dev->irq = irq;
312         }
313         dev->board_name = "parport";
314
315         ret = comedi_alloc_subdevices(dev, 4);
316         if (ret)
317                 return ret;
318
319         ret = alloc_private(dev, sizeof(struct parport_private));
320         if (ret < 0)
321                 return ret;
322
323         s = dev->subdevices + 0;
324         s->type = COMEDI_SUBD_DIO;
325         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
326         s->n_chan = 8;
327         s->maxdata = 1;
328         s->range_table = &range_digital;
329         s->insn_bits = parport_insn_a;
330         s->insn_config = parport_insn_config_a;
331
332         s = dev->subdevices + 1;
333         s->type = COMEDI_SUBD_DI;
334         s->subdev_flags = SDF_READABLE;
335         s->n_chan = 5;
336         s->maxdata = 1;
337         s->range_table = &range_digital;
338         s->insn_bits = parport_insn_b;
339
340         s = dev->subdevices + 2;
341         s->type = COMEDI_SUBD_DO;
342         s->subdev_flags = SDF_WRITABLE;
343         s->n_chan = 4;
344         s->maxdata = 1;
345         s->range_table = &range_digital;
346         s->insn_bits = parport_insn_c;
347
348         s = dev->subdevices + 3;
349         if (irq) {
350                 dev->read_subdev = s;
351                 s->type = COMEDI_SUBD_DI;
352                 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
353                 s->n_chan = 1;
354                 s->maxdata = 1;
355                 s->range_table = &range_digital;
356                 s->insn_bits = parport_intr_insn;
357                 s->do_cmdtest = parport_intr_cmdtest;
358                 s->do_cmd = parport_intr_cmd;
359                 s->cancel = parport_intr_cancel;
360         } else {
361                 s->type = COMEDI_SUBD_UNUSED;
362         }
363
364         devpriv->a_data = 0;
365         outb(devpriv->a_data, dev->iobase + PARPORT_A);
366         devpriv->c_data = 0;
367         outb(devpriv->c_data, dev->iobase + PARPORT_C);
368
369         printk(KERN_INFO "\n");
370         return 1;
371 }
372
373 static void parport_detach(struct comedi_device *dev)
374 {
375         if (dev->iobase)
376                 release_region(dev->iobase, PARPORT_SIZE);
377         if (dev->irq)
378                 free_irq(dev->irq, dev);
379 }
380
381 static struct comedi_driver parport_driver = {
382         .driver_name    = "comedi_parport",
383         .module         = THIS_MODULE,
384         .attach         = parport_attach,
385         .detach         = parport_detach,
386 };
387 module_comedi_driver(parport_driver);
388
389 MODULE_AUTHOR("Comedi http://www.comedi.org");
390 MODULE_DESCRIPTION("Comedi low-level driver");
391 MODULE_LICENSE("GPL");