root/branches/volker_dev/drv_generic_parport.c

Revision 728, 12.5 kB (checked in by michael, 2 years ago)

changed $Revision to $Rev

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * generic driver helper for serial and parport access
5 *
6 * Copyright (C) 1999, 2000 Michael Reinelt <reinelt@eunet.at>
7 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
8 *
9 * This file is part of LCD4Linux.
10 *
11 * LCD4Linux is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * LCD4Linux is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
26
27#include "config.h"
28
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <errno.h>
33#include <unistd.h>
34#include <termios.h>
35#include <fcntl.h>
36#include <time.h>
37#include <signal.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/ioctl.h>
41
42#ifdef HAVE_SYS_IO_H
43#include <sys/io.h>
44#define WITH_OUTB
45#else
46#ifdef HAVE_ASM_IO_H
47#include <asm/io.h>
48#define WITH_OUTB
49#endif
50#endif
51
52#if defined (HAVE_LINUX_PARPORT_H) && defined (HAVE_LINUX_PPDEV_H)
53#define WITH_PPDEV
54#include <linux/parport.h>
55#include <linux/ppdev.h>
56#else
57#define PARPORT_CONTROL_STROBE    0x1
58#define PARPORT_CONTROL_AUTOFD    0x2
59#define PARPORT_CONTROL_INIT      0x4
60#define PARPORT_CONTROL_SELECT    0x8
61#define PARPORT_STATUS_ERROR      0x8
62#define PARPORT_STATUS_SELECT     0x10
63#define PARPORT_STATUS_PAPEROUT   0x20
64#define PARPORT_STATUS_ACK        0x40
65#define PARPORT_STATUS_BUSY       0x80
66#endif
67
68#if !defined(WITH_OUTB) && !defined(WITH_PPDEV)
69#error neither outb() nor ppdev() possible
70#error cannot compile parallel port driver
71#endif
72
73#include "debug.h"
74#include "qprintf.h"
75#include "cfg.h"
76#include "udelay.h"
77#include "drv_generic_parport.h"
78
79
80static char *Driver = "";
81static char *Section = "";
82static unsigned short Port = 0;
83static char *PPdev = NULL;
84
85/* initial value taken from linux/parport_pc.c */
86static unsigned char ctr = 0xc;
87
88#ifdef WITH_PPDEV
89static int PPfd = -1;
90#endif
91
92
93int drv_generic_parport_open(const char *section, const char *driver)
94{
95    char *s, *e;
96
97    Section = (char *) section;
98    Driver = (char *) driver;
99
100    udelay_init();
101
102#ifndef WITH_PPDEV
103    error("The files include/linux/parport.h and/or include/linux/ppdev.h");
104    error("were missing at compile time. Even if your system supports");
105    error("ppdev, it will not be used.");
106    error("You *really* should install these files and recompile LCD4linux!");
107#endif
108
109    s = cfg_get(Section, "Port", NULL);
110    if (s == NULL || *s == '\0') {
111  error("%s: no '%s.Port' entry from %s", Driver, Section, cfg_source());
112  return -1;
113    }
114
115    PPdev = NULL;
116    if ((Port = strtol(s, &e, 0)) == 0 || *e != '\0') {
117#ifdef WITH_PPDEV
118  Port = 0;
119  PPdev = s;
120#else
121  error("%s: bad %s.Port '%s' from %s", Driver, Section, s, cfg_source());
122  free(s);
123  return -1;
124#endif
125    }
126#ifdef WITH_PPDEV
127
128    if (PPdev) {
129  info("%s: using ppdev %s", Driver, PPdev);
130  PPfd = open(PPdev, O_RDWR);
131  if (PPfd == -1) {
132      error("%s: open(%s) failed: %s", Driver, PPdev, strerror(errno));
133      return -1;
134  }
135#if 0
136  /* PPEXCL fails if someone else uses the port (e.g. lp.ko) */
137  if (ioctl(PPfd, PPEXCL)) {
138      info("%s: ioctl(%s, PPEXCL) failed: %s", PPdev, Driver, strerror(errno));
139      info("%s: could not get exclusive access to %s.", Driver, PPdev);
140  } else {
141      info("%s: got exclusive access to %s.", Driver, PPdev);
142  }
143#endif
144
145  if (ioctl(PPfd, PPCLAIM)) {
146      error("%s: ioctl(%s, PPCLAIM) failed: %d %s", Driver, PPdev, errno, strerror(errno));
147      return -1;
148  }
149    } else
150#endif
151
152    {
153  error("using raw port 0x%x (deprecated!)", Port);
154  error("You *really* should change your setup and use ppdev!");
155  if ((Port + 3) <= 0x3ff) {
156      if (ioperm(Port, 3, 1) != 0) {
157    error("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno));
158    return -1;
159      }
160  } else {
161      if (iopl(3) != 0) {
162    error("%s: iopl(1) failed: %s", Driver, strerror(errno));
163    return -1;
164      }
165  }
166    }
167    return 0;
168}
169
170
171int drv_generic_parport_close(void)
172{
173#ifdef WITH_PPDEV
174    if (PPdev) {
175  debug("closing ppdev %s", PPdev);
176  if (ioctl(PPfd, PPRELEASE)) {
177      error("%s: ioctl(%s, PPRELEASE) failed: %s", Driver, PPdev, strerror(errno));
178  }
179  if (close(PPfd) == -1) {
180      error("%s: close(%s) failed: %s", Driver, PPdev, strerror(errno));
181      return -1;
182  }
183  free(PPdev);
184    } else
185#endif
186    {
187  debug("closing raw port 0x%x", Port);
188  if ((Port + 3) <= 0x3ff) {
189      if (ioperm(Port, 3, 0) != 0) {
190    error("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno));
191    return -1;
192      }
193  } else {
194      if (iopl(0) != 0) {
195    error("%s: iopl(0) failed: %s", Driver, strerror(errno));
196    return -1;
197      }
198  }
199    }
200
201    return 0;
202}
203
204
205static unsigned char drv_generic_parport_signal_ctrl(const char *name, const char *signal)
206{
207    unsigned char wire;
208
209    if (strcasecmp(signal, "STROBE") == 0) {
210  wire = PARPORT_CONTROL_STROBE;
211  info("%s: wiring: DISPLAY:%-9s - PARPORT:STROBE (Pin  1)", Driver, name);
212    } else if (strcasecmp(signal, "AUTOFD") == 0) {
213  wire = PARPORT_CONTROL_AUTOFD;
214  info("%s: wiring: DISPLAY:%-9s - PARPORT:AUTOFD (Pin 14)", Driver, name);
215    } else if (strcasecmp(signal, "INIT") == 0) {
216  wire = PARPORT_CONTROL_INIT;
217  info("%s: wiring: DISPLAY:%-9s - PARPORT:INIT   (Pin 16)", Driver, name);
218    } else if (strcasecmp(signal, "SLCTIN") == 0) {
219  wire = PARPORT_CONTROL_SELECT;
220  info("%s: wiring: DISPLAY:%-9s - PARPORT:SLCTIN (Pin 17)", Driver, name);
221    } else if (strcasecmp(signal, "SELECT") == 0) {
222  wire = PARPORT_CONTROL_SELECT;
223  error("%s: SELECT is deprecated. Please use SLCTIN instead!", Driver);
224  info("%s: wiring: DISPLAY:%-9s - PARPORT:SLCTIN (Pin 17)", Driver, name);
225    } else if (strcasecmp(signal, "GND") == 0) {
226  wire = 0;
227  info("%s: wiring: DISPLAY:%-9s - PARPORT:GND", Driver, name);
228    } else {
229  error("%s: unknown signal <%s> for control line <%s>", Driver, signal, name);
230  error("%s: should be STROBE, AUTOFD, INIT, SLCTIN or GND", Driver);
231  return 0xff;
232    }
233
234    return wire;
235}
236
237
238unsigned char drv_generic_parport_wire_ctrl(const char *name, const char *deflt)
239{
240    unsigned char wire;
241    char key[256];
242    char *val;
243
244    qprintf(key, sizeof(key), "Wire.%s", name);
245    val = cfg_get(Section, key, deflt);
246
247    wire = drv_generic_parport_signal_ctrl(name, val);
248
249    free(val);
250
251    return wire;
252}
253
254
255unsigned char drv_generic_parport_hardwire_ctrl(const char *name, const char *signal)
256{
257    unsigned char wire;
258    char key[256];
259    char *val;
260
261    qprintf(key, sizeof(key), "Wire.%s", name);
262    val = cfg_get(Section, key, "");
263
264    /* maybe warn the user */
265    if (*val != '\0' && strcasecmp(signal, val) != 0) {
266  error("%s: ignoring configured signal <%s> for control line <%s>", Driver, val, name);
267    }
268    free(val);
269
270    wire = drv_generic_parport_signal_ctrl(name, signal);
271
272    return wire;
273}
274
275
276static unsigned char drv_generic_parport_signal_status(const char *name, const char *signal)
277{
278    unsigned char wire;
279
280    if (strcasecmp(signal, "ERROR") == 0) {
281  wire = PARPORT_STATUS_ERROR;
282  info("%s: wiring: DISPLAY:%-9s - PARPORT:ERROR    (Pin 15)", Driver, name);
283    } else if (strcasecmp(signal, "SELECT") == 0) {
284  wire = PARPORT_STATUS_SELECT;
285  info("%s: wiring: DISPLAY:%-9s - PARPORT:SELECT   (Pin 13)", Driver, name);
286    } else if (strcasecmp(signal, "PAPEROUT") == 0) {
287  wire = PARPORT_STATUS_PAPEROUT;
288  info("%s: wiring: DISPLAY:%-9s - PARPORT:PAPEROUT (Pin 12)", Driver, name);
289    } else if (strcasecmp(signal, "ACK") == 0) {
290  wire = PARPORT_STATUS_ACK;
291  info("%s: wiring: DISPLAY:%-9s - PARPORT:ACK      (Pin 10)", Driver, name);
292    } else if (strcasecmp(signal, "BUSY") == 0) {
293  wire = PARPORT_STATUS_BUSY;
294  info("%s: wiring: DISPLAY:%-9s - PARPORT:BUSY     (Pin 11)", Driver, name);
295    } else if (strcasecmp(signal, "GND") == 0) {
296  wire = 0;
297  info("%s: wiring: DISPLAY:%-9s - PARPORT:GND", Driver, name);
298    } else {
299  error("%s: unknown signal <%s> for status line <%s>", Driver, signal, name);
300  error("%s: should be ERROR, SELECT, PAPEROUT, ACK, BUSY or GND", Driver);
301  return 0xff;
302    }
303
304    return wire;
305}
306
307
308unsigned char drv_generic_parport_wire_status(const char *name, const char *deflt)
309{
310    unsigned char wire;
311    char key[256];
312    char *val;
313
314    qprintf(key, sizeof(key), "Wire.%s", name);
315    val = cfg_get(Section, key, deflt);
316
317    wire = drv_generic_parport_signal_status(name, val);
318
319    free(val);
320
321    return wire;
322}
323
324
325unsigned char drv_generic_parport_wire_data(const char *name, const char *deflt)
326{
327    unsigned char w;
328    char wire[256];
329    char *s;
330
331    qprintf(wire, sizeof(wire), "Wire.%s", name);
332    s = cfg_get(Section, wire, deflt);
333    if (strlen(s) == 3 && strncasecmp(s, "DB", 2) == 0 && s[2] >= '0' && s[2] <= '7') {
334  w = s[2] - '0';
335    } else if (strcasecmp(s, "GND") == 0) {
336  w = 0;
337    } else {
338  error("%s: unknown signal <%s> for data line <%s>", Driver, s, name);
339  error("%s: should be DB0..7 or GND", Driver);
340  return 0xff;
341    }
342    free(s);
343    if (w == 0) {
344  info("%s: wiring: DISPLAY:%-9s - PARPORT:GND", Driver, name);
345    } else {
346  info("%s: wiring: DISPLAY:%-9s - PARPORT:DB%d (Pin %2d)", Driver, name, w, w + 2);
347    }
348
349    w = 1 << w;
350
351    return w;
352}
353
354
355void drv_generic_parport_direction(const int direction)
356{
357#ifdef WITH_PPDEV
358    if (PPdev) {
359  ioctl(PPfd, PPDATADIR, &direction);
360    } else
361#endif
362    {
363  /* code stolen from linux/parport_pc.h */
364  ctr = (ctr & ~0x20) ^ (direction ? 0x20 : 0x00);
365  outb(ctr, Port + 2);
366    }
367}
368
369
370unsigned char drv_generic_parport_status(void)
371{
372    unsigned char mask =
373  PARPORT_STATUS_ERROR | PARPORT_STATUS_SELECT | PARPORT_STATUS_PAPEROUT | PARPORT_STATUS_ACK |
374  PARPORT_STATUS_BUSY;
375
376    unsigned char data;
377
378#ifdef WITH_PPDEV
379    if (PPdev) {
380  ioctl(PPfd, PPRSTATUS, &data);
381    } else
382#endif
383    {
384  data = inb(Port + 1);
385    }
386
387    /* clear unused bits */
388    data &= mask;
389
390    return data;
391}
392
393
394void drv_generic_parport_control(const unsigned char mask, const unsigned char value)
395{
396    unsigned char val;
397
398    /* any signal affected? */
399    /* Note: this may happen in case a signal is hardwired to GND */
400    if (mask == 0)
401  return;
402
403    /* Strobe, Select and AutoFeed are inverted! */
404    val = mask & (value ^ (PARPORT_CONTROL_STROBE | PARPORT_CONTROL_SELECT | PARPORT_CONTROL_AUTOFD));
405
406#ifdef WITH_PPDEV
407    if (PPdev) {
408  struct ppdev_frob_struct frob;
409  frob.mask = mask;
410  frob.val = val;
411  ioctl(PPfd, PPFCONTROL, &frob);
412    } else
413#endif
414    {
415  /* code stolen from linux/parport_pc.h */
416  ctr = (ctr & ~mask) ^ val;
417  outb(ctr, Port + 2);
418    }
419}
420
421
422void drv_generic_parport_toggle(const unsigned char bits, const int level, const unsigned long delay)
423{
424    unsigned char value1, value2;
425
426    /* any signal affected? */
427    /* Note: this may happen in case a signal is hardwired to GND */
428    if (bits == 0)
429  return;
430
431    /* prepare value */
432    value1 = level ? bits : 0;
433    value2 = level ? 0 : bits;
434
435    /* Strobe, Select and AutoFeed are inverted! */
436    value1 = bits & (value1 ^ (PARPORT_CONTROL_STROBE | PARPORT_CONTROL_SELECT | PARPORT_CONTROL_AUTOFD));
437    value2 = bits & (value2 ^ (PARPORT_CONTROL_STROBE | PARPORT_CONTROL_SELECT | PARPORT_CONTROL_AUTOFD));
438
439
440#ifdef WITH_PPDEV
441    if (PPdev) {
442  struct ppdev_frob_struct frob;
443  frob.mask = bits;
444
445  /* rise */
446  frob.val = value1;
447  ioctl(PPfd, PPFCONTROL, &frob);
448
449  /* pulse width */
450  ndelay(delay);
451
452  /* lower */
453  frob.val = value2;
454  ioctl(PPfd, PPFCONTROL, &frob);
455
456    } else
457#endif
458    {
459  /* rise */
460  ctr = (ctr & ~bits) ^ value1;
461  outb(ctr, Port + 2);
462
463  /* pulse width */
464  ndelay(delay);
465
466  /* lower */
467  ctr = (ctr & ~bits) ^ value2;
468  outb(ctr, Port + 2);
469    }
470}
471
472
473void drv_generic_parport_data(const unsigned char data)
474{
475#ifdef WITH_PPDEV
476    if (PPdev) {
477  ioctl(PPfd, PPWDATA, &data);
478    } else
479#endif
480    {
481  outb(data, Port);
482    }
483}
484
485unsigned char drv_generic_parport_read(void)
486{
487    unsigned char data;
488
489#ifdef WITH_PPDEV
490    if (PPdev) {
491  ioctl(PPfd, PPRDATA, &data);
492    } else
493#endif
494    {
495  data = inb(Port);
496    }
497    return data;
498}
499
500
501void drv_generic_parport_debug(void)
502{
503    unsigned char control;
504
505#ifdef WITH_PPDEV
506    if (PPdev) {
507  ioctl(PPfd, PPRCONTROL, &control);
508    } else
509#endif
510    {
511  control = ctr;
512    }
513
514    debug("%cSTROBE %cAUTOFD %cINIT %cSLCTIN",
515    control & PARPORT_CONTROL_STROBE ? '-' : '+',
516    control & PARPORT_CONTROL_AUTOFD ? '-' : '+', control & PARPORT_CONTROL_INIT ? '+' : '-',
517    control & PARPORT_CONTROL_SELECT ? '-' : '+');
518
519}
Note: See TracBrowser for help on using the browser.