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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/***************************************************************************
byteblaster.c - Low-level functions to access the byteblaster
-------------------------------------------------------------
begin : Sun Jan 09 2005
copyright : (C) 2005 by Aurelien Jarno
email : aurelien@aurel32.net
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/* Application specific includes */
#include "byteblaster.h"
#include "main.h"
/* Standard includes */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#ifdef HAVE_LINUX_PPDEV_H
# include <linux/ppdev.h>
#endif
#ifdef HAVE_LINUX_PARPORT_H
# include <linux/parport.h>
#endif
#ifdef HAVE_DEV_PPBUS_PPI_H
# include <dev/ppbus/ppi.h>
#endif
#ifdef HAVE_DEV_PPBUS_PPBCONF_H
# include <dev/ppbus/ppbconf.h>
# define PPRDATA PPIGDATA
# define PPWDATA PPISDATA
# define PPRSTATUS PPIGSTATUS
# define PPWSTATUS PPISSTATUS
# define PPRCONTROL PPIGCTRL
# define PPWCONTROL PPISCTRL
# define PARPORT_CONTROL_AUTOFD AUTOFEED
# define PARPORT_STATUS_ACK nACK
# define PARPORT_STATUS_SELECT SELECT
# define PARPORT_STATUS_BUSY nBUSY
#endif
/***************************************************************************
* *
* Private functions *
* *
***************************************************************************/
static void out_data(int device, unsigned char mask, unsigned char val)
{
static unsigned char data = 0;
data = (data & ~mask) | val;
ioctl(device, PPWDATA, &data);
}
static void set_AUTOFEED(int device, int value)
{
unsigned char control;
ioctl(device, PPRCONTROL, &control);
control &= ~PARPORT_CONTROL_AUTOFD;
if (value != 0)
control |= PARPORT_CONTROL_AUTOFD;
ioctl(device, PPWCONTROL, &control);
usleep(100000);
}
static void set_D5(int device, int value)
{
/* D5 */
out_data(device, 0x20, (value != 0)? 0x20 : 0x00);
}
static int get_ACK(int device)
{
unsigned char status;
ioctl(device, PPRSTATUS, &status);
return (status & PARPORT_STATUS_ACK) == PARPORT_STATUS_ACK;
}
static int open_parport(char *device_name)
{
int dev;
if (debug)
printf("Opening %s\n", device_name);
dev = open(device_name, O_RDWR);
#ifdef PPCLAIM
if (dev < 0)
{
if (!quiet)
perror("open");
return -1;
}
if (ioctl(dev, PPCLAIM) < 0)
{
if (!quiet)
perror("open");
perror("ioctl");
close(dev);
return -1;
}
#endif
return dev;
}
static void close_parport(int device)
{
if (device)
{
#ifdef PPRELEASE
ioctl(device, PPRELEASE);
#endif
close(device);
}
}
static int detect_byteblaster(int device)
{
if (debug)
printf("Detecting byteblaster\n");
/* ACK and D5 are connected together */
if (debug)
printf("Driving D5 low\n");
set_D5(device, 0);
usleep(10000);
if (get_ACK(device) == 1)
{
if (debug)
fprintf(stderr, "ACK stuck high\n");
return -1;
}
if (debug)
printf("Driving D5 high\n");
set_D5(device, 1);
usleep(10000);
if (get_ACK(device) == 0)
{
if (debug)
fprintf(stderr, "ACK stuck low\n");
return -1;
}
if (debug)
printf("Driving D5 low\n");
set_D5(device, 0);
usleep(10000);
if (get_ACK(device) == 1)
{
if (debug)
fprintf(stderr, "ACK stuck high\n");
return -1;
}
return 0;
}
/***************************************************************************
* *
* Public functions *
* *
***************************************************************************/
int get_nSTATUS(int device)
{
unsigned char status;
ioctl(device, PPRSTATUS, &status);
return (status & PARPORT_STATUS_SELECT) == PARPORT_STATUS_SELECT;
}
int get_CONF_DONE(int device)
{
unsigned char status;
ioctl(device, PPRSTATUS, &status);
return (status & PARPORT_STATUS_BUSY) != PARPORT_STATUS_BUSY;
}
void set_DCLK(int device, int value)
{
/* D0 */
out_data(device, 0x01, (value != 0)? 0x01 : 0x00);
}
void set_nCONFIG(int device, int value)
{
/* D1 */
out_data(device, 0x02, (value != 0)? 0x02 : 0x00);
}
void set_DATA0(int device, int value)
{
/* D6 */
out_data(device, 0x40, (value != 0)? 0x40 : 0x00);
}
int open_byteblaster(char *devicename)
{
int dev;
dev = open_parport(devicename);
if (dev < 0 )
{
if (!quiet)
fprintf(stderr, "Can't open %s!\n", devicename);
return -1;
}
if (detect_byteblaster(dev) < 0)
{
if (!quiet)
fprintf(stderr, "Byteblaster not found on %s!\n", devicename);
close_byteblaster(dev);
return -1;
}
return dev;
}
void close_byteblaster(int device)
{
enable_byteblaster(device, 0);
close_parport(device);
}
void enable_byteblaster(int device, int value)
{
set_AUTOFEED(device, value);
}
|