-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpport.c
161 lines (128 loc) · 4.31 KB
/
pport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Linux Canon LBP800 CAPT driver
* low level parport routines
* Copyright (c) 2007 Massimo Del Fedele <[email protected]>
*
* Adapted from a printer driver for Samsung ML-85G laser printer
* (C) Copyleft, 2000 Rildo Pragana <[email protected]>
* and from Linux Canon CAPT LBP810 driver
* Copyright (C) 2004 Nicolas Boichat <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <unistd.h>
#include <fcntl.h>
#include <pty.h>
#include <linux/ppdev.h>
#include <linux/parport.h>
#include "machine.h"
#include "pport.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
// APRE IL DEVICE DELLA PORTA
inline int Parport_Open(char *DeviceName)
{
int Device=-1;
int bCurrentPortMode=0;
int bPortMode=IEEE1284_MODE_ECP;
bPortMode=IEEE1284_MODE_ECPRLE ;
// APRE IL DEVICE
Device = open (DeviceName, O_RDWR);
// Device = open (DeviceName, O_RDWR|O_EXCL);
if(Device == -1){
fprintf(stderr, "ERROR: Parport_Open Can not open %s\n", DeviceName);
return Device;
}
// sleep(2);
// fprintf(stderr, "DEBUG - pport:Parport_Open Try to PPNEGOT to %x\n",(int)bPortMode);
// if (ioctl (Device, PPNEGOT, &bPortMode)) {
// fprintf(stderr, "DEBUG - pport:Parport_Open Can not PPNEGOT. err %x\n",(int)errno);
// / close (Device);
// return -1;
// }
// ACQUISISCE IL DEVICE
if (ioctl (Device, PPCLAIM))
{
perror("ERROR: Parport_Open Can not claim");
close (Device);
return -1;
}
ioctl (Device, PPGETMODE, &bCurrentPortMode);
fprintf(stderr, "ERROR: Parport_Open PPGETMODE %04Xh\n", (int) bCurrentPortMode);
return Device;
} // END Parport_Open()
// CHIUDE IL DEVICE DELLA PORTA
inline void Parport_Close(int Device)
{
// RILASCIA IL DEVICE
ioctl(Device, PPRELEASE);
// CHIUDE IL DEVICE
close(Device);
} // END Parport_Close()
// LEGGE UN DATO DALLA PORTA PARALLELA
inline BYTE Parport_ReadData(int Device)
{
BYTE data;
ioctl(Device, PPRDATA, &data);
return data;
} // END Parport_ReadData()
// SCRIVE UN DATO NELLA PORTA PARALLELA
inline void Parport_WriteData(int Device, BYTE data)
{
ioctl (Device, PPWDATA, &data);
} // END Parport_WriteData()
// LEGGE LO STATO DALLA PORTA PARALLELA
inline BYTE Parport_ReadStatus(int Device)
{
BYTE stat=0;
ioctl (Device, PPRSTATUS, &stat);
return stat;
} // END Parport_ReadStatus()
// SCRIVE IL BYTE DI CONTROLLO NELLA PORTA PARALLELA
inline void Parport_WriteControl(int Device, BYTE control)
{
int direction=0;
// METTE A POSTO LA DIREZIONE DEI DATI
// NECESSARIO PERCHE' LA CHIAMATA 'PPWCONTROL' GESTISCE SOLO LE
// 4 LINEE "COMPATIBILI" E NON LA DIREZIONE DATI
direction = ((control & 0x20) ? 1 : 0);
// fprintf(stderr,"pport:arport_WriteControl Set direction to %d\n",direction);
ioctl(Device, PPDATADIR, &direction);
// IMPOSTA I RIMANENTI BYTES
ioctl (Device, PPWCONTROL, &control);
} // END Parport_WriteControl()
BYTE Parport_ReadControl(int Device)
{
BYTE bControl=0;
ioctl (Device, PPRCONTROL, &bControl);
return bControl;
}
// Print port status. For debug.
void Parport_MonitorStatus(int a_dDevice, int a_dCounter){
int dIndex=0;
int bCurrentStatus=0;
int bPrevStatus=0;
// fprintf(stderr, "pport:Parport_MonitorStatus 20 \n");
for (dIndex=0;dIndex<a_dCounter;dIndex++){
bCurrentStatus=Parport_ReadStatus(a_dDevice);
if (bPrevStatus!=bCurrentStatus){
bPrevStatus=bCurrentStatus;
fprintf(stderr, "pport:Parport_MonitorStatus 30 PortStatus %02X %d\n",(int)bPrevStatus,dIndex);
}
}
fprintf(stderr, "pport:Parport_MonitorStatus 999 \n \n");
} // END Parport_MonitorStatus()