Merge tag 'please-pull-pstore' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl...
[firefly-linux-kernel-4.4.55.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 #include "comedi_fc.h"
89
90 #define PARPORT_SIZE 3
91
92 #define PARPORT_A 0
93 #define PARPORT_B 1
94 #define PARPORT_C 2
95
96 struct parport_private {
97         unsigned int a_data;
98         unsigned int c_data;
99         int enable_irq;
100 };
101
102 static int parport_insn_a(struct comedi_device *dev, struct comedi_subdevice *s,
103                           struct comedi_insn *insn, unsigned int *data)
104 {
105         struct parport_private *devpriv = dev->private;
106
107         if (data[0]) {
108                 devpriv->a_data &= ~data[0];
109                 devpriv->a_data |= (data[0] & data[1]);
110
111                 outb(devpriv->a_data, dev->iobase + PARPORT_A);
112         }
113
114         data[1] = inb(dev->iobase + PARPORT_A);
115
116         return insn->n;
117 }
118
119 static int parport_insn_config_a(struct comedi_device *dev,
120                                  struct comedi_subdevice *s,
121                                  struct comedi_insn *insn, unsigned int *data)
122 {
123         struct parport_private *devpriv = dev->private;
124
125         if (data[0]) {
126                 s->io_bits = 0xff;
127                 devpriv->c_data &= ~(1 << 5);
128         } else {
129                 s->io_bits = 0;
130                 devpriv->c_data |= (1 << 5);
131         }
132         outb(devpriv->c_data, dev->iobase + PARPORT_C);
133
134         return 1;
135 }
136
137 static int parport_insn_b(struct comedi_device *dev, struct comedi_subdevice *s,
138                           struct comedi_insn *insn, unsigned int *data)
139 {
140         if (data[0]) {
141                 /* should writes be ignored? */
142                 /* anyone??? */
143         }
144
145         data[1] = (inb(dev->iobase + PARPORT_B) >> 3);
146
147         return insn->n;
148 }
149
150 static int parport_insn_c(struct comedi_device *dev, struct comedi_subdevice *s,
151                           struct comedi_insn *insn, unsigned int *data)
152 {
153         struct parport_private *devpriv = dev->private;
154
155         data[0] &= 0x0f;
156         if (data[0]) {
157                 devpriv->c_data &= ~data[0];
158                 devpriv->c_data |= (data[0] & data[1]);
159
160                 outb(devpriv->c_data, dev->iobase + PARPORT_C);
161         }
162
163         data[1] = devpriv->c_data & 0xf;
164
165         return insn->n;
166 }
167
168 static int parport_intr_insn(struct comedi_device *dev,
169                              struct comedi_subdevice *s,
170                              struct comedi_insn *insn, unsigned int *data)
171 {
172         data[1] = 0;
173         return insn->n;
174 }
175
176 static int parport_intr_cmdtest(struct comedi_device *dev,
177                                 struct comedi_subdevice *s,
178                                 struct comedi_cmd *cmd)
179 {
180         int err = 0;
181
182         /* Step 1 : check if triggers are trivially valid */
183
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);
189
190         if (err)
191                 return 1;
192
193         /* Step 2a : make sure trigger sources are unique */
194         /* Step 2b : and mutually compatible */
195
196         if (err)
197                 return 2;
198
199         /* Step 3: check if arguments are trivially valid */
200
201         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
202         err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
203         err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
204         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, 1);
205         err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
206
207         if (err)
208                 return 3;
209
210         /* step 4: ignored */
211
212         if (err)
213                 return 4;
214
215         return 0;
216 }
217
218 static int parport_intr_cmd(struct comedi_device *dev,
219                             struct comedi_subdevice *s)
220 {
221         struct parport_private *devpriv = dev->private;
222
223         devpriv->c_data |= 0x10;
224         outb(devpriv->c_data, dev->iobase + PARPORT_C);
225
226         devpriv->enable_irq = 1;
227
228         return 0;
229 }
230
231 static int parport_intr_cancel(struct comedi_device *dev,
232                                struct comedi_subdevice *s)
233 {
234         struct parport_private *devpriv = dev->private;
235
236         devpriv->c_data &= ~0x10;
237         outb(devpriv->c_data, dev->iobase + PARPORT_C);
238
239         devpriv->enable_irq = 0;
240
241         return 0;
242 }
243
244 static irqreturn_t parport_interrupt(int irq, void *d)
245 {
246         struct comedi_device *dev = d;
247         struct parport_private *devpriv = dev->private;
248         struct comedi_subdevice *s = &dev->subdevices[3];
249
250         if (!devpriv->enable_irq)
251                 return IRQ_NONE;
252
253         comedi_buf_put(s->async, 0);
254         s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
255
256         comedi_event(dev, s);
257         return IRQ_HANDLED;
258 }
259
260 static int parport_attach(struct comedi_device *dev,
261                           struct comedi_devconfig *it)
262 {
263         struct parport_private *devpriv;
264         struct comedi_subdevice *s;
265         unsigned int irq;
266         int ret;
267
268         ret = comedi_request_region(dev, it->options[0], PARPORT_SIZE);
269         if (ret)
270                 return ret;
271
272         irq = it->options[1];
273         if (irq) {
274                 ret = request_irq(irq, parport_interrupt, 0, dev->board_name,
275                                   dev);
276                 if (ret < 0) {
277                         dev_err(dev->class_dev, "irq not available\n");
278                         return -EINVAL;
279                 }
280                 dev->irq = irq;
281         }
282
283         ret = comedi_alloc_subdevices(dev, 4);
284         if (ret)
285                 return ret;
286
287         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
288         if (!devpriv)
289                 return -ENOMEM;
290         dev->private = devpriv;
291
292         s = &dev->subdevices[0];
293         s->type = COMEDI_SUBD_DIO;
294         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
295         s->n_chan = 8;
296         s->maxdata = 1;
297         s->range_table = &range_digital;
298         s->insn_bits = parport_insn_a;
299         s->insn_config = parport_insn_config_a;
300
301         s = &dev->subdevices[1];
302         s->type = COMEDI_SUBD_DI;
303         s->subdev_flags = SDF_READABLE;
304         s->n_chan = 5;
305         s->maxdata = 1;
306         s->range_table = &range_digital;
307         s->insn_bits = parport_insn_b;
308
309         s = &dev->subdevices[2];
310         s->type = COMEDI_SUBD_DO;
311         s->subdev_flags = SDF_WRITABLE;
312         s->n_chan = 4;
313         s->maxdata = 1;
314         s->range_table = &range_digital;
315         s->insn_bits = parport_insn_c;
316
317         s = &dev->subdevices[3];
318         if (irq) {
319                 dev->read_subdev = s;
320                 s->type = COMEDI_SUBD_DI;
321                 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
322                 s->n_chan = 1;
323                 s->maxdata = 1;
324                 s->range_table = &range_digital;
325                 s->insn_bits = parport_intr_insn;
326                 s->do_cmdtest = parport_intr_cmdtest;
327                 s->do_cmd = parport_intr_cmd;
328                 s->cancel = parport_intr_cancel;
329         } else {
330                 s->type = COMEDI_SUBD_UNUSED;
331         }
332
333         devpriv->a_data = 0;
334         outb(devpriv->a_data, dev->iobase + PARPORT_A);
335         devpriv->c_data = 0;
336         outb(devpriv->c_data, dev->iobase + PARPORT_C);
337
338         return 0;
339 }
340
341 static struct comedi_driver parport_driver = {
342         .driver_name    = "comedi_parport",
343         .module         = THIS_MODULE,
344         .attach         = parport_attach,
345         .detach         = comedi_legacy_detach,
346 };
347 module_comedi_driver(parport_driver);
348
349 MODULE_AUTHOR("Comedi http://www.comedi.org");
350 MODULE_DESCRIPTION("Comedi low-level driver");
351 MODULE_LICENSE("GPL");