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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/* sane - Scanner Access Now Easy.
Copyright (C) 1998 Christian Bucher
Copyright (C) 1998 Kling & Hautzinger GmbH
This file is part of the SANE package.
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, see <https://www.gnu.org/licenses/>.
As a special exception, the authors of SANE give permission for
additional uses of the libraries contained in this release of SANE.
The exception is that, if you link a SANE library with other files
to produce an executable, this does not by itself cause the
resulting executable to be covered by the GNU General Public
License. Your use of that executable is in no way restricted on
account of linking the SANE library code into it.
This exception does not, however, invalidate any other reasons why
the executable file might be covered by the GNU General Public
License.
If you submit changes to SANE to the maintainers to be included in
a subsequent release, you agree by submitting the changes that
those changes may be distributed with this exception intact.
If you write modifications of your own for SANE, it is your choice
whether to permit this exception to apply to your modifications.
If you do not wish that, delete this exception notice.
This file implements the bi-directional parallel-port
interface. */
/*
RESTRICTIONS:
- This interface is very timing sensitive, be careful with setting
debug levels.
*/
#include "../include/sane/config.h"
#include "../include/sane/sanei_directio.h"
/* pick up compatibility defs */
#define BACKEND_NAME sanei_pio
#include "../include/sane/sanei_backend.h" /* pick up compatibility defs */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/types.h>
#include "../include/sane/sane.h"
#include "../include/sane/sanei.h"
#include "../include/sane/sanei_pio.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
#include "../include/sane/saneopts.h"
#define PORT_DEV "/dev/port"
/* base 278 (lpt2)
ioport stat ctrl
offs 0 1 2
len 1 1 1 */
/* Port definitions (`N' at end begin of label means negated signal) */
#define PIO_IOPORT 0 /* rel. addr io port */
#define PIO_STAT 1 /* rel. addr status port */
#define PIO_STAT_BUSY (1<<7) /* BUSY Pin */
#define PIO_STAT_NACKNLG (1<<6) /* ~ACKNLG Pin */
#define PIO_CTRL 2 /* rel. addr control port */
#define PIO_CTRL_IE (1<<5) /* Input enable */
#define PIO_CTRL_IRQE (1<<4) /* enable IRQ */
#define PIO_CTRL_DIR (1<<3) /* DIR pin, DIR=1 => out */
#define PIO_CTRL_NINIT (1<<2) /* reset output */
#define PIO_CTRL_FDXT (1<<1) /* Paper FEED (unused) */
#define PIO_CTRL_NSTROBE (1<<0) /* strobe pin */
#define PIO_APPLYRESET 2000 /* reset in 10us at init time */
#define DL40 6
#define DL50 7
#define DL60 8
#define DL61 9
#define DL70 10
#define DL71 11
#ifdef NDEBUG
# define DBG_INIT()
#endif
typedef struct
{
u_long base; /* i/o base address */
int fd; /* >= 0 when using /dev/port */
int max_time_seconds;/* forever if <= 0 */
u_int in_use; /* port in use? */
}
PortRec, *Port;
static PortRec port[] =
{
{0x378, -1, 0, 0},
{0x278, -1, 0, 0}
};
extern int setuid (uid_t);
static inline int pio_wait (const Port port, u_char val, u_char mask);
static inline void pio_ctrl (const Port port, u_char val);
static inline void pio_delay (const Port port);
static inline void pio_init (const Port port);
static void pio_reset (const Port port);
static int pio_write (const Port port, const u_char * buf, int n);
static int pio_read (const Port port, u_char * buf, int n);
static int pio_open (const char *dev, SANE_Status * status);
static inline int
pio_wait (const Port port, u_char val, u_char mask)
{
int stat = 0;
long poll_count = 0;
time_t start = time(NULL);
DBG (DL60, "wait on port 0x%03lx for %02x mask %02x\n",
port->base, (int) val, (int) mask);
DBG (DL61, " BUSY %s\n", (mask & PIO_STAT_BUSY) ?
(val & PIO_STAT_BUSY ? "on" : "off") : "-");
DBG (DL61, " NACKNLG %s\n",
(mask & PIO_STAT_NACKNLG) ? (val & PIO_STAT_NACKNLG ? "on" : "off")
: "-");
for (;;)
{
++poll_count;
stat = sanei_inb (port->base + PIO_STAT);
if ((stat & mask) == (val & mask))
{
DBG (DL60, "got %02x after %ld tries\n", stat, poll_count);
DBG (DL61, " BUSY %s\n", stat & PIO_STAT_BUSY ? "on" : "off");
DBG (DL61, " NACKNLG %s\n",
stat & PIO_STAT_NACKNLG ? "on" : "off");
return stat;
}
if(poll_count>1000)
{
if ((port->max_time_seconds>0) && (time(NULL)-start >= port->max_time_seconds))
break;
usleep(1);
}
}
DBG (DL60, "got %02x aborting after %ld\n", stat, poll_count);
DBG (DL61, " BUSY %s\n", stat & PIO_STAT_BUSY ? "on" : "off");
DBG (DL61, " NACKNLG %s\n", stat & PIO_STAT_NACKNLG ? "on" : "off");
DBG (1, "polling time out, abort\n");
exit (-1);
}
static inline void
pio_ctrl (const Port port, u_char val)
{
DBG (DL60, "ctrl on port 0x%03lx %02x %02x\n",
port->base, (int) val, (int) val ^ PIO_CTRL_NINIT);
val ^= PIO_CTRL_NINIT;
DBG (DL61, " IE %s\n", val & PIO_CTRL_IE ? "on" : "off");
DBG (DL61, " IRQE %s\n", val & PIO_CTRL_IRQE ? "on" : "off");
DBG (DL61, " DIR %s\n", val & PIO_CTRL_DIR ? "on" : "off");
DBG (DL61, " NINIT %s\n", val & PIO_CTRL_NINIT ? "on" : "off");
DBG (DL61, " FDXT %s\n", val & PIO_CTRL_FDXT ? "on" : "off");
DBG (DL61, " NSTROBE %s\n", val & PIO_CTRL_NSTROBE ? "on" : "off");
sanei_outb (port->base + PIO_CTRL, val);
return;
}
static inline void
pio_delay (const Port port)
{
sanei_inb (port->base + PIO_STAT); /* delay */
return;
}
static inline void
pio_init (const Port port)
{
pio_ctrl (port, PIO_CTRL_IE);
return;
}
static void
pio_reset (const Port port)
{
int n;
DBG (DL40, "reset\n");
for (n = PIO_APPLYRESET; --n >= 0;)
{
sanei_outb (port->base + PIO_CTRL,
(PIO_CTRL_IE | PIO_CTRL_NINIT) ^ PIO_CTRL_NINIT);
}
pio_init (port);
DBG (DL40, "end reset\n");
return;
}
static int
pio_write (const Port port, const u_char * buf, int n)
{
int k;
DBG (DL40, "write\n");
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY); /* busy */
pio_ctrl (port, PIO_CTRL_DIR | PIO_CTRL_IE); /* praeoutput */
pio_wait (port, PIO_STAT_NACKNLG, PIO_STAT_NACKNLG); /* acknlg */
pio_ctrl (port, PIO_CTRL_DIR); /* output */
for (k = 0; k < n; k++, buf++)
{
DBG (DL40, "write byte\n");
#ifdef HANDSHAKE_BUSY
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY); /* busy */
#else
pio_wait (port, PIO_STAT_BUSY | PIO_STAT_NACKNLG,
PIO_STAT_BUSY | PIO_STAT_NACKNLG); /* busyack */
#endif
DBG (DL60, "out %02x\n", (int) *buf);
sanei_outb (port->base + PIO_IOPORT, *buf);
pio_delay (port);
pio_delay (port);
pio_delay (port);
pio_ctrl (port, PIO_CTRL_DIR | PIO_CTRL_NSTROBE); /* outputstrobe */
pio_delay (port);
pio_delay (port);
pio_delay (port);
pio_ctrl (port, PIO_CTRL_DIR); /* output */
pio_delay (port);
pio_delay (port);
pio_delay (port);
DBG (DL40, "end write byte\n");
}
#ifdef HANDSHAKE_BUSY
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY); /* busy */
#else
pio_wait (port, PIO_STAT_BUSY | PIO_STAT_NACKNLG,
PIO_STAT_BUSY | PIO_STAT_NACKNLG); /* busyack */
#endif
pio_ctrl (port, PIO_CTRL_DIR | PIO_CTRL_IE); /* praeoutput */
DBG (DL40, "end write\n");
return k;
}
static int
pio_read (const Port port, u_char * buf, int n)
{
int k;
DBG (DL40, "read\n");
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY); /* busy */
pio_ctrl (port, PIO_CTRL_IE); /* input */
for (k = 0; k < n; k++, buf++)
{
DBG (DL40, "read byte\n");
#ifdef HANDSHAKE_BUSY
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY); /* busy */
#else
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY | PIO_STAT_NACKNLG);
/* busynack */
#endif
pio_ctrl (port, PIO_CTRL_IE | PIO_CTRL_NSTROBE); /* inputstrobe */
pio_delay (port);
pio_delay (port);
pio_delay (port);
pio_ctrl (port, PIO_CTRL_IE); /* input */
#ifdef HANDSHAKE_BUSY
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY); /* busy */
#else
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY | PIO_STAT_NACKNLG);
/* busynack */
#endif
*buf = sanei_inb (port->base + PIO_IOPORT);
DBG (DL60, "in %02x\n", (int) *buf);
DBG (DL40, "end read byte\n");
}
pio_wait (port, PIO_STAT_BUSY, PIO_STAT_BUSY); /* busy */
pio_ctrl (port, PIO_CTRL_IE); /* input */
DBG (DL40, "end read\n");
return k;
}
/*
Open the device, <dev> must contain a valid port number (as string).
*/
static int
pio_open (const char *dev, SANE_Status * status)
{
static int first_time = 1;
u_long base;
int n;
if (first_time)
{
first_time = 0;
DBG_INIT ();
/* set root uid */
if (0 > setuid (0))
{
DBG (1, "sanei_pio_open: setuid failed: errno = %d\n", errno);
*status = SANE_STATUS_INVAL;
return -1;
}
}
/* read port number */
{
char *end;
base = strtol (dev, &end, 0);
if ((end == dev) || *end)
{
DBG (1, "sanei_pio_open: `%s' is not a valid port number\n", dev);
*status = SANE_STATUS_INVAL;
return -1;
}
}
if (0 == base)
{
DBG (1, "sanei_pio_open: 0x%03lx is not a valid base address\n", base);
*status = SANE_STATUS_INVAL;
return -1;
}
for (n = 0; n < NELEMS (port); n++)
if (port[n].base == base)
break;
if (NELEMS (port) <= n)
{
DBG (1, "sanei_pio_open: 0x%03lx is not a valid base address\n", base);
*status = SANE_STATUS_INVAL;
return -1;
}
if (port[n].in_use)
{
DBG (1, "sanei_pio_open: port 0x%03lx is already in use\n", base);
*status = SANE_STATUS_DEVICE_BUSY;
return -1;
}
port[n].base = base;
port[n].fd = -1;
port[n].max_time_seconds = 10;
port[n].in_use = 1;
if (sanei_ioperm (port[n].base, 3, 1))
{
DBG (1, "sanei_pio_open: cannot get io privilege for port 0x%03lx\n",
port[n].base);
*status = SANE_STATUS_IO_ERROR;
return -1;
}
pio_reset (&port[n]);
*status = SANE_STATUS_GOOD;
return n;
}
SANE_Status
sanei_pio_open (const char *dev, int *fdp)
{
SANE_Status status;
*fdp = pio_open (dev, &status);
return status;
}
void
sanei_pio_close (int fd)
{
Port p = port + fd;
if ((0 > fd) || (NELEMS (port) <= fd))
return;
if (!p->in_use)
return;
if (-1 != p->fd)
{
close (p->fd);
p->fd = -1;
}
p->in_use = 0;
return;
}
int
sanei_pio_read (int fd, u_char * buf, int n)
{
if ((0 > fd) || (NELEMS (port) <= fd))
return -1;
if (!port[fd].in_use)
return -1;
return pio_read (&port[fd], buf, n);
}
int
sanei_pio_write (int fd, const u_char * buf, int n)
{
if ((0 > fd) || (NELEMS (port) <= fd))
return -1;
if (!port[fd].in_use)
return -1;
return pio_write (&port[fd], buf, n);
}
|