1
/* $Id: drv_generic_parport.c 905 2008-12-23 13:37:07Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_generic_parport.c $
4
* generic driver helper for serial and parport access
6
* Copyright (C) 1999, 2000 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* This file is part of LCD4Linux.
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)
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.
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.
38
#include <sys/types.h>
40
#include <sys/ioctl.h>
52
#if defined (HAVE_LINUX_PARPORT_H) && defined (HAVE_LINUX_PPDEV_H)
54
#include <linux/parport.h>
55
#include <linux/ppdev.h>
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
68
/* these signals are inverted by hardware on the parallel port */
69
#define PARPORT_CONTROL_INVERTED (PARPORT_CONTROL_STROBE | PARPORT_CONTROL_SELECT | PARPORT_CONTROL_AUTOFD)
71
#if !defined(WITH_OUTB) && !defined(WITH_PPDEV)
72
#error neither outb() nor ppdev() possible
73
#error cannot compile parallel port driver
80
#include "drv_generic_parport.h"
83
static char *Driver = "";
84
static char *Section = "";
85
static unsigned short Port = 0;
86
static char *PPdev = NULL;
88
/* Any bits set here will have their logic inverted at the parallel port */
89
static unsigned char inverted_control_bits = 0;
91
/* initial value taken from linux/parport_pc.c */
92
static unsigned char ctr = 0xc;
99
int drv_generic_parport_open(const char *section, const char *driver)
103
Section = (char *) section;
104
Driver = (char *) driver;
109
error("The files include/linux/parport.h and/or include/linux/ppdev.h");
110
error("were missing at compile time. Even if your system supports");
111
error("ppdev, it will not be used.");
112
error("You *really* should install these files and recompile LCD4linux!");
115
s = cfg_get(Section, "Port", NULL);
116
if (s == NULL || *s == '\0') {
117
error("%s: no '%s.Port' entry from %s", Driver, Section, cfg_source());
122
if ((Port = strtol(s, &e, 0)) == 0 || *e != '\0') {
127
error("%s: bad %s.Port '%s' from %s", Driver, Section, s, cfg_source());
135
info("%s: using ppdev %s", Driver, PPdev);
136
PPfd = open(PPdev, O_RDWR);
138
error("%s: open(%s) failed: %s", Driver, PPdev, strerror(errno));
142
/* PPEXCL fails if someone else uses the port (e.g. lp.ko) */
143
if (ioctl(PPfd, PPEXCL)) {
144
info("%s: ioctl(%s, PPEXCL) failed: %s", PPdev, Driver, strerror(errno));
145
info("%s: could not get exclusive access to %s.", Driver, PPdev);
147
info("%s: got exclusive access to %s.", Driver, PPdev);
151
if (ioctl(PPfd, PPCLAIM)) {
152
error("%s: ioctl(%s, PPCLAIM) failed: %d %s", Driver, PPdev, errno, strerror(errno));
159
error("using raw port 0x%x (deprecated!)", Port);
160
error("You *really* should change your setup and use ppdev!");
161
if ((Port + 3) <= 0x3ff) {
162
if (ioperm(Port, 3, 1) != 0) {
163
error("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno));
168
error("%s: iopl(1) failed: %s", Driver, strerror(errno));
177
int drv_generic_parport_close(void)
181
debug("closing ppdev %s", PPdev);
182
if (ioctl(PPfd, PPRELEASE)) {
183
error("%s: ioctl(%s, PPRELEASE) failed: %s", Driver, PPdev, strerror(errno));
185
if (close(PPfd) == -1) {
186
error("%s: close(%s) failed: %s", Driver, PPdev, strerror(errno));
193
debug("closing raw port 0x%x", Port);
194
if ((Port + 3) <= 0x3ff) {
195
if (ioperm(Port, 3, 0) != 0) {
196
error("%s: ioperm(0x%x) failed: %s", Driver, Port, strerror(errno));
201
error("%s: iopl(0) failed: %s", Driver, strerror(errno));
211
static unsigned char drv_generic_parport_signal_ctrl(const char *name, const char *signal)
216
/* Prefixing a signal name with '-' inverts the logic */
217
if (signal[0] == '-') {
222
if (strcasecmp(signal, "STROBE") == 0) {
223
wire = PARPORT_CONTROL_STROBE;
224
info("%s: wiring: DISPLAY:%-9s - PARPORT:STROBE (Pin 1)%s", Driver, name, invert ? " [inverted]" : "");
225
} else if (strcasecmp(signal, "AUTOFD") == 0) {
226
wire = PARPORT_CONTROL_AUTOFD;
227
info("%s: wiring: DISPLAY:%-9s - PARPORT:AUTOFD (Pin 14)%s", Driver, name, invert ? " [inverted]" : "");
228
} else if (strcasecmp(signal, "INIT") == 0) {
229
wire = PARPORT_CONTROL_INIT;
230
info("%s: wiring: DISPLAY:%-9s - PARPORT:INIT (Pin 16)%s", Driver, name, invert ? " [inverted]" : "");
231
} else if (strcasecmp(signal, "SLCTIN") == 0) {
232
wire = PARPORT_CONTROL_SELECT;
233
info("%s: wiring: DISPLAY:%-9s - PARPORT:SLCTIN (Pin 17)%s", Driver, name, invert ? " [inverted]" : "");
234
} else if (strcasecmp(signal, "SELECT") == 0) {
235
wire = PARPORT_CONTROL_SELECT;
236
error("%s: SELECT is deprecated. Please use SLCTIN instead!", Driver);
237
info("%s: wiring: DISPLAY:%-9s - PARPORT:SLCTIN (Pin 17)%s", Driver, name, invert ? " [inverted]" : "");
238
} else if (strcasecmp(signal, "GND") == 0) {
240
info("%s: wiring: DISPLAY:%-9s - PARPORT:GND", Driver, name);
242
error("%s: unknown signal <%s> for control line <%s>", Driver, signal, name);
243
error("%s: should be STROBE, AUTOFD, INIT, SLCTIN or GND", Driver);
248
inverted_control_bits |= wire;
255
unsigned char drv_generic_parport_wire_ctrl(const char *name, const char *deflt)
261
qprintf(key, sizeof(key), "Wire.%s", name);
262
val = cfg_get(Section, key, deflt);
264
wire = drv_generic_parport_signal_ctrl(name, val);
272
unsigned char drv_generic_parport_hardwire_ctrl(const char *name, const char *signal)
278
qprintf(key, sizeof(key), "Wire.%s", name);
279
val = cfg_get(Section, key, "");
281
/* maybe warn the user */
282
if (*val != '\0' && strcasecmp(signal, val) != 0) {
283
error("%s: ignoring configured signal <%s> for control line <%s>", Driver, val, name);
287
wire = drv_generic_parport_signal_ctrl(name, signal);
293
static unsigned char drv_generic_parport_signal_status(const char *name, const char *signal)
297
if (strcasecmp(signal, "ERROR") == 0) {
298
wire = PARPORT_STATUS_ERROR;
299
info("%s: wiring: DISPLAY:%-9s - PARPORT:ERROR (Pin 15)", Driver, name);
300
} else if (strcasecmp(signal, "SELECT") == 0) {
301
wire = PARPORT_STATUS_SELECT;
302
info("%s: wiring: DISPLAY:%-9s - PARPORT:SELECT (Pin 13)", Driver, name);
303
} else if (strcasecmp(signal, "PAPEROUT") == 0) {
304
wire = PARPORT_STATUS_PAPEROUT;
305
info("%s: wiring: DISPLAY:%-9s - PARPORT:PAPEROUT (Pin 12)", Driver, name);
306
} else if (strcasecmp(signal, "ACK") == 0) {
307
wire = PARPORT_STATUS_ACK;
308
info("%s: wiring: DISPLAY:%-9s - PARPORT:ACK (Pin 10)", Driver, name);
309
} else if (strcasecmp(signal, "BUSY") == 0) {
310
wire = PARPORT_STATUS_BUSY;
311
info("%s: wiring: DISPLAY:%-9s - PARPORT:BUSY (Pin 11)", Driver, name);
312
} else if (strcasecmp(signal, "GND") == 0) {
314
info("%s: wiring: DISPLAY:%-9s - PARPORT:GND", Driver, name);
316
error("%s: unknown signal <%s> for status line <%s>", Driver, signal, name);
317
error("%s: should be ERROR, SELECT, PAPEROUT, ACK, BUSY or GND", Driver);
325
unsigned char drv_generic_parport_wire_status(const char *name, const char *deflt)
331
qprintf(key, sizeof(key), "Wire.%s", name);
332
val = cfg_get(Section, key, deflt);
334
wire = drv_generic_parport_signal_status(name, val);
342
unsigned char drv_generic_parport_wire_data(const char *name, const char *deflt)
348
qprintf(wire, sizeof(wire), "Wire.%s", name);
349
s = cfg_get(Section, wire, deflt);
350
if (strlen(s) == 3 && strncasecmp(s, "DB", 2) == 0 && s[2] >= '0' && s[2] <= '7') {
352
} else if (strcasecmp(s, "GND") == 0) {
355
error("%s: unknown signal <%s> for data line <%s>", Driver, s, name);
356
error("%s: should be DB0..7 or GND", Driver);
361
info("%s: wiring: DISPLAY:%-9s - PARPORT:GND", Driver, name);
363
info("%s: wiring: DISPLAY:%-9s - PARPORT:DB%d (Pin %2d)", Driver, name, w, w + 2);
372
void drv_generic_parport_direction(const int direction)
376
ioctl(PPfd, PPDATADIR, &direction);
380
/* code stolen from linux/parport_pc.h */
381
ctr = (ctr & ~0x20) ^ (direction ? 0x20 : 0x00);
387
unsigned char drv_generic_parport_status(void)
390
PARPORT_STATUS_ERROR | PARPORT_STATUS_SELECT | PARPORT_STATUS_PAPEROUT | PARPORT_STATUS_ACK |
397
ioctl(PPfd, PPRSTATUS, &data);
401
data = inb(Port + 1);
404
/* clear unused bits */
411
void drv_generic_parport_control(const unsigned char mask, const unsigned char value)
415
/* any signal affected? */
416
/* Note: this may happen in case a signal is hardwired to GND */
420
/* Strobe, Select and AutoFeed are inverted! */
421
val = mask & (value ^ PARPORT_CONTROL_INVERTED ^ inverted_control_bits);
425
struct ppdev_frob_struct frob;
428
ioctl(PPfd, PPFCONTROL, &frob);
432
/* code stolen from linux/parport_pc.h */
433
ctr = (ctr & ~mask) ^ val;
439
void drv_generic_parport_toggle(const unsigned char bits, const int level, const unsigned long delay)
441
unsigned char value1, value2;
443
/* any signal affected? */
444
/* Note: this may happen in case a signal is hardwired to GND */
449
value1 = level ? bits : 0;
450
value2 = level ? 0 : bits;
452
/* Strobe, Select and AutoFeed are inverted! */
453
value1 = bits & (value1 ^ PARPORT_CONTROL_INVERTED ^ inverted_control_bits);
454
value2 = bits & (value2 ^ PARPORT_CONTROL_INVERTED ^ inverted_control_bits);
458
struct ppdev_frob_struct frob;
463
ioctl(PPfd, PPFCONTROL, &frob);
470
ioctl(PPfd, PPFCONTROL, &frob);
476
ctr = (ctr & ~bits) ^ value1;
483
ctr = (ctr & ~bits) ^ value2;
489
void drv_generic_parport_data(const unsigned char data)
493
ioctl(PPfd, PPWDATA, &data);
501
unsigned char drv_generic_parport_read(void)
507
ioctl(PPfd, PPRDATA, &data);
517
void drv_generic_parport_debug(void)
519
unsigned char control;
523
ioctl(PPfd, PPRCONTROL, &control);
530
debug("%cSTROBE %cAUTOFD %cINIT %cSLCTIN",
531
control & PARPORT_CONTROL_STROBE ? '-' : '+',
532
control & PARPORT_CONTROL_AUTOFD ? '-' : '+', control & PARPORT_CONTROL_INIT ? '+' : '-',
533
control & PARPORT_CONTROL_SELECT ? '-' : '+');