1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 2000-2003 Jochen Eisinger <jochen.eisinger@gmx.net>
3    Copyright (C) 2003 James Perry (scsi_pp functions)
4    This file is part of the SANE package.
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 
19    As a special exception, the authors of SANE give permission for
20    additional uses of the libraries contained in this release of SANE.
21 
22    The exception is that, if you link a SANE library with other files
23    to produce an executable, this does not by itself cause the
24    resulting executable to be covered by the GNU General Public
25    License.  Your use of that executable is in no way restricted on
26    account of linking the SANE library code into it.
27 
28    This exception does not, however, invalidate any other reasons why
29    the executable file might be covered by the GNU General Public
30    License.
31 
32    If you submit changes to SANE to the maintainers to be included in
33    a subsequent release, you agree by submitting the changes that
34    those changes may be distributed with this exception intact.
35 
36    If you write modifications of your own for SANE, it is your choice
37    whether to permit this exception to apply to your modifications.
38    If you do not wish that, delete this exception notice.
39 
40    This file implements an interface for the Mustek PP chipset A4S2 */
41 
42 /* debug levels:
43    0 - nothing
44    1 - errors
45    2 - warnings
46    3 - things nice to know
47    4 - code flow
48    5 - detailed flow
49    6 - everything
50 
51    These debug levels can be set using the environment variable
52    SANE_DEBUG_SANEI_PA4S2 */
53 
54 #include "../include/sane/config.h"
55 
56 #define BACKEND_NAME sanei_pa4s2
57 #include "../include/sane/sanei_backend.h"	/* pick up compatibility defs */
58 
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif
62 
63 #if defined(HAVE_LIBIEEE1284)
64 
65 # include <ieee1284.h>
66 
67 #elif defined(ENABLE_PARPORT_DIRECTIO)
68 
69 # if defined(HAVE_SYS_IO_H)
70 #  if defined (__ICC) && __ICC >= 700
71 #   define __GNUC__ 2
72 #  endif
73 #  include <sys/io.h>
74 #  ifndef SANE_HAVE_SYS_IO_H_WITH_INB_OUTB
75 #   define IO_SUPPORT_MISSING
76 #  endif
77 #  if defined (__ICC) && __ICC >= 700
78 #   undef __GNUC__
79 #  elif defined(__ICC) && defined(HAVE_ASM_IO_H)
80 #   include <asm/io.h>
81 #  endif
82 # elif defined(HAVE_ASM_IO_H)
83 #  include <asm/io.h>		/* ugly, but backwards compatible */
84 # elif defined(HAVE_SYS_HW_H)
85 #  include <sys/hw.h>
86 # elif defined(__i386__)  && ( defined (__GNUC__) || defined (__ICC) )
87 
88 static __inline__ void
outb(u_char value,u_long port)89 outb (u_char value, u_long port)
90 {
91   __asm__ __volatile__ ("outb %0,%1"::"a" (value), "d" ((u_short) port));
92 }
93 
94 static __inline__ u_char
inb(u_long port)95 inb (u_long port)
96 {
97   u_char value;
98 
99   __asm__ __volatile__ ("inb %1,%0":"=a" (value):"d" ((u_short) port));
100   return value;
101 }
102 
103 # else
104 #  define IO_SUPPORT_MISSING
105 # endif
106 
107 #else
108 
109 # define IO_SUPPORT_MISSING
110 
111 #endif /* HAVE_LIBIEEE1284 */
112 
113 #if defined(__FreeBSD__) && defined(__i386__)
114 #define HAVE_IOPERM
115 #define ioperm i386_set_ioperm
116 #include <machine/sysarch.h>
117 #endif
118 
119 #include "../include/sane/sane.h"
120 #include "../include/sane/sanei.h"
121 #include "../include/sane/sanei_pa4s2.h"
122 
123 
124 #ifdef NDEBUG
125 #define DBG_INIT()  /* basically, this is already done in sanei_debug.h... */
126 
127 #define TEST_DBG_INIT()
128 
129 #else /* !NDEBUG */
130 
131 static int sanei_pa4s2_dbg_init_called = SANE_FALSE;
132 
133 #if (!defined __GNUC__ || __GNUC__ < 2 || \
134      __GNUC_MINOR__ < (defined __cplusplus ? 6 : 4))
135 
136 #define TEST_DBG_INIT() if (sanei_pa4s2_dbg_init_called == SANE_FALSE) \
137                           {                                            \
138                             DBG_INIT();                                \
139                             DBG(6, "sanei_pa4s2: interface called for" \
140                               " the first time\n");                    \
141                             sanei_pa4s2_dbg_init_called = SANE_TRUE;   \
142                           }
143 #else
144 
145 #define TEST_DBG_INIT() if (sanei_pa4s2_dbg_init_called == SANE_FALSE)   \
146                           {                                              \
147                             DBG_INIT();                                  \
148                             DBG(6, "%s: interface called for"            \
149                               " the first time\n", __func__); \
150                             sanei_pa4s2_dbg_init_called = SANE_TRUE;     \
151                           }
152 
153 #endif
154 
155 #endif /* NDEBUG */
156 
157 #include <errno.h>
158 #include <stdio.h>
159 #include <stdlib.h>
160 #if defined(HAVE_STRING_H)
161 # include <string.h>
162 #elif defined(HAVE_STRINGS_H)
163 # include <strings.h>
164 #endif
165 #if defined(HAVE_SYS_TYPES_H)
166 # include <sys/types.h>
167 #endif
168 
169 #include "../include/sane/saneopts.h"
170 
171 
172 #if (defined (HAVE_IOPERM) || defined (HAVE_LIBIEEE1284)) && !defined (IO_SUPPORT_MISSING)
173 
174 #if defined(STDC_HEADERS)
175 # include <errno.h>
176 # include <stdio.h>
177 # include <stdlib.h>
178 #endif
179 #if defined(HAVE_STRING_H)
180 # include <string.h>
181 #elif defined(HAVE_STRINGS_H)
182 # include <strings.h>
183 #endif
184 #if defined(HAVE_SYS_TYPES_H)
185 # include <sys/types.h>
186 #endif
187 
188 #include "../include/sane/saneopts.h"
189 
190 #define PA4S2_MODE_NIB	0
191 #define PA4S2_MODE_UNI	1
192 #define PA4S2_MODE_EPP	2
193 
194 #define PA4S2_ASIC_ID_1013	0xA8
195 #define PA4S2_ASIC_ID_1015	0xA5
196 #define PA4S2_ASIC_ID_1505	0xA2
197 
198 
199 typedef struct
200   {
201 #ifndef HAVE_LIBIEEE1284
202     const char name[6];
203     u_long base;		/* i/o base address */
204 #endif
205     u_int in_use;		/* port in use? */
206     u_int enabled;		/* port enabled? */
207     u_int mode;			/* protocol */
208     u_char prelock[3];		/* state of port */
209 #ifdef HAVE_LIBIEEE1284
210     int caps;
211 #endif
212   }
213 PortRec, *Port;
214 
215 #if defined (HAVE_LIBIEEE1284)
216 
217 static struct parport_list pplist;
218 static PortRec *port;
219 
220 #else
221 
222 static PortRec port[] =
223 {
224   {"0x378", 0x378, SANE_FALSE, SANE_FALSE, PA4S2_MODE_NIB,
225     {0, 0, 0}},
226   {"0x278", 0x278, SANE_FALSE, SANE_FALSE, PA4S2_MODE_NIB,
227     {0, 0, 0}},
228   {"0x3BC", 0x3BC, SANE_FALSE, SANE_FALSE, PA4S2_MODE_NIB,
229     {0, 0, 0}}
230 };
231 
232 #endif
233 
234 static u_int sanei_pa4s2_interface_options = SANEI_PA4S2_OPT_DEFAULT;
235 
236 extern int setuid (uid_t);	/* should also be in unistd.h */
237 
238 static int pa4s2_open (const char *dev, SANE_Status * status);
239 static void pa4s2_readbegin_epp (int fd, u_char reg);
240 static u_char pa4s2_readbyte_epp (int fd);
241 static void pa4s2_readend_epp (int fd);
242 static void pa4s2_readbegin_uni (int fd, u_char reg);
243 static u_char pa4s2_readbyte_uni (int fd);
244 static void pa4s2_readend_uni (int fd);
245 static void pa4s2_readbegin_nib (int fd, u_char reg);
246 static u_char pa4s2_readbyte_nib (int fd);
247 static void pa4s2_readend_nib (int fd);
248 static void pa4s2_writebyte_any (int fd, u_char reg, u_char val);
249 static int pa4s2_enable (int fd, u_char * prelock);
250 static int pa4s2_disable (int fd, u_char * prelock);
251 static int pa4s2_close (int fd, SANE_Status * status);
252 
253 #if defined (HAVE_LIBIEEE1284)
254 
pa4s2_libieee1284_errorstr(int error)255 static const char * pa4s2_libieee1284_errorstr(int error)
256 {
257 
258   switch (error)
259     {
260 
261       case E1284_OK:
262         return "Everything went fine";
263 
264       case E1284_NOTIMPL:
265         return "Not implemented in libieee1284";
266 
267       case E1284_NOTAVAIL:
268         return "Not available on this system";
269 
270       case E1284_TIMEDOUT:
271         return "Operation timed out";
272 
273       case E1284_REJECTED:
274         return "IEEE 1284 negotiation rejected";
275 
276       case E1284_NEGFAILED:
277         return "Negotiation went wrong";
278 
279       case E1284_NOMEM:
280         return "No memory left";
281 
282       case E1284_INIT:
283         return "Error initializing port";
284 
285       case E1284_SYS:
286         return "Error interfacing system";
287 
288       case E1284_NOID:
289         return "No IEEE 1284 ID available";
290 
291       case E1284_INVALIDPORT:
292         return "Invalid port";
293 
294       default:
295         return "Unknown error";
296 
297     }
298 }
299 
300 #endif
301 
302 static int
pa4s2_init(SANE_Status * status)303 pa4s2_init (SANE_Status *status)
304 {
305   static int first_time = SANE_TRUE;
306 #if defined (HAVE_LIBIEEE1284)
307   int result, n;
308 #endif
309 
310   DBG (6, "pa4s2_init: static int first_time = %u\n", first_time);
311 
312   if (first_time == SANE_FALSE)
313     {
314       DBG (5, "pa4s2_init: sanei already initialized\n");
315       status = SANE_STATUS_GOOD;
316       return 0;
317     }
318 
319   DBG (5, "pa4s2_init: called for the first time\n");
320 
321   first_time = SANE_FALSE;
322 
323 #if defined (HAVE_LIBIEEE1284)
324 
325   DBG (4, "pa4s2_init: initializing libieee1284\n");
326   result = ieee1284_find_ports (&pplist, 0);
327 
328   if (result)
329     {
330       DBG (1, "pa4s2_init: initializing IEEE 1284 failed (%s)\n",
331            pa4s2_libieee1284_errorstr (result));
332       first_time = SANE_TRUE;
333       *status = SANE_STATUS_INVAL;
334       return -1;
335     }
336 
337   DBG (3, "pa4s2_init: %d ports reported by IEEE 1284 library\n", pplist.portc);
338 
339   for (n=0; n<pplist.portc; n++)
340     DBG (6, "pa4s2_init: port %d is `%s`\n", n, pplist.portv[n]->name);
341 
342 
343   DBG (6, "pa4s2_init: allocating port list\n");
344   if ((port = calloc(pplist.portc, sizeof(PortRec))) == NULL)
345     {
346       DBG (1, "pa4s2_init: not enough free memory\n");
347       ieee1284_free_ports(&pplist);
348       first_time = SANE_TRUE;
349       *status = SANE_STATUS_NO_MEM;
350       return -1;
351     }
352 
353 #else
354 
355   DBG (4, "pa4s2_init: trying to setuid root\n");
356 
357   if (0 > setuid (0))
358     {
359 
360       DBG (1, "pa4s2_init: setuid failed: errno = %d\n", errno);
361       DBG (5, "pa4s2_init: returning SANE_STATUS_INVAL\n");
362 
363       *status = SANE_STATUS_INVAL;
364       first_time = SANE_TRUE;
365       return -1;
366 
367     }
368 
369   DBG (3, "pa4s2_init: the application is now root\n");
370   DBG (3, "pa4s2_init: this is a high security risk...\n");
371 
372   DBG (6, "pa4s2_init: ... you'd better start praying\n");
373 
374   /* PS: no, i don't trust myself either */
375 
376   /* PPS: i'd try rsbac or similar if i were you */
377 
378 #endif
379 
380   DBG (5, "pa4s2_init: initialized successfully\n");
381   *status = SANE_STATUS_GOOD;
382   return 0;
383 }
384 
385 static int
pa4s2_open(const char * dev,SANE_Status * status)386 pa4s2_open (const char *dev, SANE_Status * status)
387 {
388 
389   int n, result;
390 #if !defined (HAVE_LIBIEEE1284)
391   u_long base;
392 #endif
393 
394   DBG (4, "pa4s2_open: trying to attach dev `%s`\n", dev);
395 
396   if ((result = pa4s2_init(status)) != 0)
397     {
398 
399       DBG (1, "pa4s2_open: failed to initialize\n");
400       return result;
401     }
402 
403 #if !defined (HAVE_LIBIEEE1284)
404 
405   {
406     char *end;
407 
408     DBG (5, "pa4s2_open: reading port number\n");
409 
410     base = strtol (dev, &end, 0);
411 
412     if ((end == dev) || (*end != '\0'))
413       {
414 
415 	DBG (1, "pa4s2_open: `%s` is not a valid port number\n", dev);
416 	DBG (6, "pa4s2_open: the part I did not understand was ...`%s`\n", end);
417 	DBG (5, "pa4s2_open: returning SANE_STATUS_INVAL\n");
418 
419 	*status = SANE_STATUS_INVAL;
420 
421 	return -1;
422 
423       }
424 
425   }
426 
427   DBG (6, "pa4s2_open: read port number 0x%03lx\n", base);
428 
429   if (base == 0)
430     {
431 
432       DBG (1, "pa4s2_open: 0x%03lx is not a valid base address\n", base);
433       DBG (5, "pa4s2_open: returning SANE_STATUS_INVAL\n");
434 
435       *status = SANE_STATUS_INVAL;
436       return -1;
437 
438     }
439 #endif
440 
441   DBG (5, "pa4s2_open: looking up port in list\n");
442 
443 #if defined (HAVE_LIBIEEE1284)
444 
445   for (n = 0; n < pplist.portc; n++)
446     if (!strcmp(pplist.portv[n]->name, dev))
447       break;
448 
449   if (pplist.portc <= n)
450     {
451       DBG (1, "pa4s2_open: `%s` is not a valid device name\n", dev);
452       DBG (5, "pa4s2_open: returning SANE_STATUS_INVAL\n");
453 
454       *status = SANE_STATUS_INVAL;
455       return -1;
456     }
457 
458 #else
459 
460   for (n = 0; n < NELEMS (port); n++)
461     if (port[n].base == base)
462       break;
463 
464   if (NELEMS (port) <= n)
465     {
466 
467       DBG (1, "pa4s2_open: 0x%03lx is not a valid base address\n",
468 	   base);
469       DBG (5, "pa4s2_open: returning SANE_STATUS_INVAL\n");
470 
471       *status = SANE_STATUS_INVAL;
472       return -1;
473     }
474 
475 #endif
476 
477   DBG (6, "pa4s2_open: port is in list at port[%d]\n", n);
478 
479   if (port[n].in_use == SANE_TRUE)
480     {
481 
482 #if defined (HAVE_LIBIEEE1284)
483       DBG (1, "pa4s2_open: device `%s` is already in use\n", dev);
484 #else
485       DBG (1, "pa4s2_open: port 0x%03lx is already in use\n", base);
486 #endif
487       DBG (5, "pa4s2_open: returning SANE_STATUS_DEVICE_BUSY\n");
488 
489       *status = SANE_STATUS_DEVICE_BUSY;
490       return -1;
491 
492     }
493 
494   DBG (5, "pa4s2_open: setting up port data\n");
495 
496 #if defined (HAVE_LIBIEEE1284)
497   DBG (6, "pa4s2_open: name=%s in_use=SANE_TRUE\n", dev);
498 #else
499   DBG (6, "pa4s2_open: base=0x%03lx in_use=SANE_TRUE\n", base);
500 #endif
501   DBG (6, "pa4s2_open: enabled=SANE_FALSE mode=PA4S2_MODE_NIB\n");
502   port[n].in_use = SANE_TRUE;
503   port[n].enabled = SANE_FALSE;
504   port[n].mode = PA4S2_MODE_NIB;
505 
506 
507 #if defined (HAVE_LIBIEEE1284)
508 
509   DBG (5, "pa4s2_open: opening device\n");
510   result = ieee1284_open (pplist.portv[n], 0, &port[n].caps);
511 
512   if (result)
513     {
514       DBG (1, "pa4s2_open: could not open device `%s` (%s)\n",
515            dev, pa4s2_libieee1284_errorstr (result));
516       port[n].in_use = SANE_FALSE;
517       DBG (6, "pa4s2_open: marking port %d as unused\n", n);
518       *status = SANE_STATUS_ACCESS_DENIED;
519       return -1;
520     }
521 
522 #else
523 
524   DBG (5, "pa4s2_open: getting io permissions\n");
525 
526   /* TODO: insert FreeBSD compatible code here */
527 
528   if (ioperm (port[n].base, 5, 1))
529     {
530 
531       DBG (1, "pa4s2_open: cannot get io privilege for port 0x%03lx\n",
532       		port[n].base);
533 
534 
535       DBG (5, "pa4s2_open: marking port[%d] as unused\n", n);
536       port[n].in_use = SANE_FALSE;
537 
538       DBG (5, "pa4s2_open: returning SANE_STATUS_IO_ERROR\n");
539       *status = SANE_STATUS_IO_ERROR;
540       return -1;
541 
542     }
543 #endif
544 
545   DBG (3, "pa4s2_open: device `%s` opened...\n", dev);
546 
547   DBG (5, "pa4s2_open: returning SANE_STATUS_GOOD\n");
548   *status = SANE_STATUS_GOOD;
549 
550   DBG (4, "pa4s2_open: open dev `%s` as fd %u\n", dev, n);
551 
552   return n;
553 
554 }
555 
556 #if defined(HAVE_LIBIEEE1284)
557 
558 
559 #define inbyte0(fd)	ieee1284_read_data(pplist.portv[fd]);
560 #define inbyte1(fd)	(ieee1284_read_status(pplist.portv[fd]) ^ S1284_INVERTED)
561 #define inbyte2(fd)	(ieee1284_read_control(pplist.portv[fd]) ^ C1284_INVERTED)
inbyte4(int fd)562 static u_char inbyte4(int fd)
563 {
564   char val;
565   ieee1284_epp_read_data(pplist.portv[fd], 0, &val, 1);
566   return (u_char)val;
567 }
568 
569 #define outbyte0(fd,val)	ieee1284_write_data(pplist.portv[fd], val)
570 #define outbyte1(fd,val)	/* ieee1284_write_status(pplist.portv[fd], (val) ^ S1284_INVERTED) */
571 #define outbyte2(fd,val)	ieee1284_write_control(pplist.portv[fd], (val) ^ C1284_INVERTED)
572 
outbyte3(int fd,u_char val)573 static void outbyte3(int fd, u_char val)
574 {
575   ieee1284_epp_write_addr (pplist.portv[fd], 0, (char *)&val, 1);
576 }
577 
578 #else
579 
580 #define inbyte0(fd)	inb(port[fd].base)
581 #define inbyte1(fd)	inb(port[fd].base + 1)
582 #define inbyte2(fd)	inb(port[fd].base + 2)
583 #define inbyte4(fd)	inb(port[fd].base + 4)
584 
585 #define outbyte0(fd,val)	outb(val, port[fd].base)
586 #define outbyte1(fd,val)	outb(val, port[fd].base + 1)
587 #define outbyte2(fd,val)	outb(val, port[fd].base + 2)
588 #define outbyte3(fd,val)	outb(val, port[fd].base + 3)
589 
590 #endif
591 
592 
593 static void
pa4s2_readbegin_epp(int fd,u_char reg)594 pa4s2_readbegin_epp (int fd, u_char reg)
595 {
596 
597 #if defined(HAVE_LIBIEEE1284)
598   DBG (6, "pa4s2_readbegin_epp: selecting register %u at '%s'\n",
599        (int) reg, pplist.portv[fd]->name);
600 #else
601   DBG (6, "pa4s2_readbegin_epp: selecting register %u at 0x%03lx\n",
602        (int) reg, port[fd].base);
603 #endif
604 
605   outbyte0 (fd, 0x20);
606   outbyte2 (fd, 0x04);
607   outbyte2 (fd, 0x06);
608   outbyte2 (fd, 0x04);
609   outbyte3 (fd, reg + 0x18);
610 
611 }
612 
613 static u_char
pa4s2_readbyte_epp(int fd)614 pa4s2_readbyte_epp (int fd)
615 {
616 
617   u_char val = inbyte4 (fd);
618 
619 #if defined(HAVE_LIBIEEE1284)
620   DBG (6, "pa4s2_readbyte_epp: reading value 0x%02x from '%s'\n",
621        (int) val, pplist.portv[fd]->name);
622 #else
623   DBG (6, "pa4s2_readbyte_epp: reading value 0x%02x at 0x%03lx\n",
624        (int) val, port[fd].base);
625 #endif
626 
627   return val;
628 
629 }
630 
631 static void
pa4s2_readend_epp(int fd)632 pa4s2_readend_epp (int fd)
633 {
634 
635   DBG (6, "pa4s2_readend_epp: end of reading sequence\n");
636 
637   outbyte2 (fd, 0x04);
638   outbyte2 (fd, 0x00);
639   outbyte2 (fd, 0x04);
640 
641 }
642 
643 static void
pa4s2_readbegin_uni(int fd,u_char reg)644 pa4s2_readbegin_uni (int fd, u_char reg)
645 {
646 
647 #if defined(HAVE_LIBIEEE1284)
648   DBG (6, "pa4s2_readbegin_uni: selecting register %u for '%s'\n",
649        (int) reg, pplist.portv[fd]->name);
650 #else
651   DBG (6, "pa4s2_readbegin_uni: selecting register %u at 0x%03lx\n",
652        (int) reg, port[fd].base);
653 #endif
654 
655   outbyte0 (fd, reg | 0x58);
656   outbyte2 (fd, 0x04);
657   outbyte2 (fd, 0x06);
658   outbyte2 (fd, 0x04);
659   outbyte2 (fd, 0x04);
660 
661 }
662 
663 static u_char
pa4s2_readbyte_uni(int fd)664 pa4s2_readbyte_uni (int fd)
665 {
666   u_char val;
667 
668   outbyte2 (fd, 0x05);
669   val = inbyte2(fd);
670   val <<= 4;
671   val &= 0xE0;
672   val |= (inbyte1(fd) >> 3);
673   outbyte2 (fd, 0x04);
674 
675 #if defined(HAVE_LIBIEEE1284)
676   DBG (6, "pa4s2_readbyte_uni: reading value 0x%02x from '%s'\n",
677        (int) val, pplist.portv[fd]->name);
678 #else
679   DBG (6, "pa4s2_readbyte_uni: reading value 0x%02x at 0x%03lx\n",
680        (int) val, port[fd].base);
681 #endif
682 
683   return val;
684 }
685 
686 static void
pa4s2_readend_uni(int fd)687 pa4s2_readend_uni (int fd)
688 {
689 
690   DBG (6, "pa4s2_readend_uni: end of reading sequence for fd %d\n", fd);
691 
692 }
693 
694 static void
pa4s2_readbegin_nib(int fd,u_char reg)695 pa4s2_readbegin_nib (int fd, u_char reg)
696 {
697 
698 #if defined(HAVE_LIBIEEE1284)
699   DBG (6, "pa4s2_readbegin_nib: selecting register %u at '%s'\n",
700        (int) reg, pplist.portv[fd]->name);
701 #else
702   DBG (6, "pa4s2_readbegin_nib: selecting register %u at 0x%03lx\n",
703        (int) reg, port[fd].base);
704 #endif
705 
706 
707   outbyte0 (fd, reg | 0x18);
708   outbyte2 (fd, 0x04);
709   outbyte2 (fd, 0x06);
710   outbyte2 (fd, 0x04);
711   outbyte2 (fd, 0x04);
712 
713 }
714 
715 static u_char
pa4s2_readbyte_nib(int fd)716 pa4s2_readbyte_nib (int fd)
717 {
718 
719   u_char val;
720 
721   outbyte2 (fd, 0x05);
722   val = inbyte1(fd);
723   val >>= 4;
724   outbyte0 (fd, 0x58);
725   val |= inbyte1(fd) & 0xF0;
726   val ^= 0x88;
727   outbyte0 (fd, 0x00);
728   outbyte2 (fd, 0x04);
729 
730 #if defined(HAVE_LIBIEEE1284)
731   DBG (6, "pa4s2_readbyte_nib: reading value 0x%02x from '%s'\n",
732   	(int) val, pplist.portv[fd]->name);
733 #else
734   DBG (6, "pa4s2_readbyte_nib: reading value 0x%02x at 0x%03lx\n",
735        (int) val, port[fd].base);
736 #endif
737 
738   return val;
739 
740 }
741 
742 static void
pa4s2_readend_nib(int fd)743 pa4s2_readend_nib (int fd)
744 {
745   DBG (6, "pa4s2_readend_nib: end of reading sequence for fd %d\n", fd);
746 }
747 
748 static void
pa4s2_writebyte_any(int fd,u_char reg,u_char val)749 pa4s2_writebyte_any (int fd, u_char reg, u_char val)
750 {
751 
752   /* somebody from Mustek asked me once, why I was writing the same
753      value repeatedly to a port. Well, actually I don't know, it just
754      works. Maybe the repeated writes could be replaced by appropriate
755      delays or even left out completely.
756    */
757 #if defined(HAVE_LIBIEEE1284)
758   DBG (6, "pa4s2_writebyte_any: writing value 0x%02x"
759        " in reg %u to '%s'\n", (int) val, (int) reg, pplist.portv[fd]->name);
760 #else
761   DBG (6, "pa4s2_writebyte_any: writing value 0x%02x"
762        " in reg %u at 0x%03lx\n", (int) val, (int) reg, port[fd].base);
763 #endif
764 
765   outbyte0 (fd, reg | 0x10);
766   outbyte2 (fd, 0x04);
767   outbyte2 (fd, 0x06);
768   outbyte2 (fd, 0x06);
769   outbyte2 (fd, 0x06);
770   outbyte2 (fd, 0x06);
771   outbyte2 (fd, 0x04);
772   outbyte2 (fd, 0x04);
773   outbyte0 (fd, val);
774   outbyte2 (fd, 0x05);
775   outbyte2 (fd, 0x05);
776   outbyte2 (fd, 0x05);
777   outbyte2 (fd, 0x04);
778   outbyte2 (fd, 0x04);
779   outbyte2 (fd, 0x04);
780   outbyte2 (fd, 0x04);
781 }
782 
783 static int
pa4s2_enable(int fd,u_char * prelock)784 pa4s2_enable (int fd, u_char * prelock)
785 {
786 #if defined (HAVE_LIBIEEE1284)
787   int result;
788   result = ieee1284_claim (pplist.portv[fd]);
789 
790   if (result)
791     {
792       DBG (1, "pa4s2_enable: failed to claim the port (%s)\n",
793       	pa4s2_libieee1284_errorstr(result));
794       return -1;
795     }
796 #endif
797 
798   prelock[0] = inbyte0 (fd);
799   prelock[1] = inbyte1 (fd);
800   prelock[2] = inbyte2 (fd);
801   outbyte2 (fd, (prelock[2] & 0x0F) | 0x04);
802 
803   DBG (6, "pa4s2_enable: prelock[] = {0x%02x, 0x%02x, 0x%02x}\n",
804        (int) prelock[0], (int) prelock[1], (int) prelock[2]);
805 
806   outbyte0 (fd, 0x15);
807   outbyte0 (fd, 0x95);
808   outbyte0 (fd, 0x35);
809   outbyte0 (fd, 0xB5);
810   outbyte0 (fd, 0x55);
811   outbyte0 (fd, 0xD5);
812   outbyte0 (fd, 0x75);
813   outbyte0 (fd, 0xF5);
814   outbyte0 (fd, 0x01);
815   outbyte0 (fd, 0x81);
816 
817   return 0;
818 }
819 
820 static int
pa4s2_disable(int fd,u_char * prelock)821 pa4s2_disable (int fd, u_char * prelock)
822 {
823 
824   if ((sanei_pa4s2_interface_options & SANEI_PA4S2_OPT_ALT_LOCK) != 0)
825     {
826 
827       DBG (6, "pa4s2_disable: using alternative command set\n");
828 
829       outbyte0 (fd, 0x00);
830       outbyte2 (fd, 0x04);
831       outbyte2 (fd, 0x06);
832       outbyte2 (fd, 0x04);
833 
834     }
835 
836   outbyte2 (fd, prelock[2] & 0x0F);
837 
838   outbyte0 (fd, 0x15);
839   outbyte0 (fd, 0x95);
840   outbyte0 (fd, 0x35);
841   outbyte0 (fd, 0xB5);
842   outbyte0 (fd, 0x55);
843   outbyte0 (fd, 0xD5);
844   outbyte0 (fd, 0x75);
845   outbyte0 (fd, 0xF5);
846   outbyte0 (fd, 0x00);
847   outbyte0 (fd, 0x80);
848 
849   outbyte0 (fd, prelock[0]);
850   outbyte1 (fd, prelock[1]);
851   outbyte2 (fd, prelock[2]);
852 
853 #if defined(HAVE_LIBIEEE1284)
854   ieee1284_release (pplist.portv[fd]);
855 #endif
856 
857   DBG (6, "pa4s2_disable: state restored\n");
858 
859   return 0;
860 
861 }
862 
863 static int
pa4s2_close(int fd,SANE_Status * status)864 pa4s2_close (int fd, SANE_Status * status)
865 {
866 #if defined(HAVE_LIBIEEE1284)
867   int result;
868 #endif
869   DBG (4, "pa4s2_close: fd=%d\n", fd);
870 
871 #if defined(HAVE_LIBIEEE1284)
872   DBG (6, "pa4s2_close: this is port '%s'\n", pplist.portv[fd]->name);
873 #else
874   DBG (6, "pa4s2_close: this is port 0x%03lx\n", port[fd].base);
875 #endif
876 
877   DBG (5, "pa4s2_close: checking whether port is enabled\n");
878 
879   if (port[fd].enabled == SANE_TRUE)
880     {
881 
882       DBG (6, "pa4s2_close: disabling port\n");
883       pa4s2_disable (fd, port[fd].prelock);
884 
885     }
886 
887   DBG (5, "pa4s2_close: trying to free io port\n");
888 #if defined(HAVE_LIBIEEE1284)
889   if ((result = ieee1284_close(pplist.portv[fd])) < 0)
890 #else
891   if (ioperm (port[fd].base, 5, 0))
892 #endif
893     {
894 
895 #if defined(HAVE_LIBIEEE1284)
896       DBG (1, "pa4s2_close: can't free port '%s' (%s)\n",
897       		pplist.portv[fd]->name, pa4s2_libieee1284_errorstr(result));
898 #else
899       DBG (1, "pa4s2_close: can't free port 0x%03lx\n", port[fd].base);
900 #endif
901 
902       DBG (5, "pa4s2_close: returning SANE_STATUS_IO_ERROR\n");
903       *status = SANE_STATUS_IO_ERROR;
904       return -1;
905 
906     }
907 
908   DBG (5, "pa4s2_close: marking port as unused\n");
909 
910   port[fd].in_use = SANE_FALSE;
911 
912   DBG (5, "pa4s2_close: returning SANE_STATUS_GOOD\n");
913 
914   *status = SANE_STATUS_GOOD;
915 
916   return 0;
917 
918 }
919 
920 const char **
sanei_pa4s2_devices()921 sanei_pa4s2_devices()
922 {
923 
924   SANE_Status status;
925   int n;
926   const char **devices;
927 
928   TEST_DBG_INIT();
929 
930   DBG (4, "sanei_pa4s2_devices: invoked\n");
931 
932   if ((n = pa4s2_init(&status)) != 0)
933     {
934 
935       DBG (1, "sanei_pa4s2_devices: failed to initialize (%s)\n",
936       		sane_strstatus(status));
937       return calloc(1, sizeof(char *));
938     }
939 
940 #if defined(HAVE_LIBIEEE1284)
941 
942   if ((devices = calloc((pplist.portc + 1), sizeof(char *))) == NULL)
943     {
944       DBG (2, "sanei_pa4s2_devices: not enough free memory\n");
945       return calloc(1, sizeof(char *));
946     }
947 
948   for (n=0; n<pplist.portc; n++)
949     devices[n] = pplist.portv[n]->name;
950 
951 #else
952 
953   if ((devices = calloc((NELEMS (port) + 1), sizeof(char *))) == NULL)
954     {
955       DBG (2, "sanei_pa4s2_devices: not enough free memory\n");
956       return calloc(1, sizeof(char *));
957     }
958 
959   for (n=0 ; n<NELEMS (port) ; n++)
960     devices[n] = (char *)port[n].name;
961 #endif
962 
963   return devices;
964 }
965 
966 /*
967  * Needed for SCSI-over-parallel scanners (Paragon 600 II EP)
968  */
969 SANE_Status
sanei_pa4s2_scsi_pp_get_status(int fd,u_char * status)970 sanei_pa4s2_scsi_pp_get_status(int fd, u_char *status)
971 {
972   u_char stat;
973 
974   TEST_DBG_INIT ();
975 
976   DBG (6, "sanei_pa4s2_scsi_pp_get_status: called for fd %d\n",
977        fd);
978 
979 #if defined(HAVE_LIBIEEE1284)
980   if ((fd < 0) || (fd >= pplist.portc))
981 #else
982   if ((fd < 0) || (fd >= NELEMS (port)))
983 #endif
984     {
985 
986       DBG (2, "sanei_pa4s2_scsi_pp_get_status: invalid fd %d\n", fd);
987       DBG (6, "sanei_pa4s2_scsi_pp_get_status: returning SANE_STATUS_INVAL\n");
988 
989       return SANE_STATUS_INVAL;
990 
991     }
992 
993   if (port[fd].in_use == SANE_FALSE)
994     {
995 
996       DBG (2, "sanei_pa4s2_scsi_pp_get_status: port is not in use\n");
997 #if defined(HAVE_LIBIEEE1284)
998       DBG (4, "sanei_pa4s2_scsi_pp_get_status: port is '%s'\n",
999       		pplist.portv[fd]->name);
1000 #else
1001       DBG (6, "sanei_pa4s2_scsi_pp_get_status: port is 0x%03lx\n",
1002 	   port[fd].base);
1003 #endif
1004       DBG (5, "sanei_pa4s2_scsi_pp_get_status: returning SANE_STATUS_INVAL\n");
1005 
1006       return SANE_STATUS_INVAL;
1007 
1008     }
1009 
1010   if (port[fd].enabled == SANE_FALSE)
1011     {
1012 
1013       DBG (2, "sanei_pa4s2_scsi_pp_get_status: port is not enabled\n");
1014 #if defined(HAVE_LIBIEEE1284)
1015       DBG (4, "sanei_pa4s2_scsi_pp_get_status: port is '%s'\n",
1016       		pplist.portv[fd]->name);
1017 #else
1018       DBG (6, "sanei_pa4s2_scsi_pp_get_status: port is 0x%03lx\n",
1019 	   port[fd].base);
1020 #endif
1021       DBG (5, "sanei_pa4s2_scsi_pp_get_status: returning SANE_STATUS_INVAL\n");
1022 
1023       return SANE_STATUS_INVAL;
1024 
1025     }
1026 
1027   outbyte2 (fd, 0x4);
1028   stat = inbyte1 (fd)^0x80;
1029   *status = (stat&0x2f)|((stat&0x10)<<2)|((stat&0x40)<<1)|((stat&0x80)>>3);
1030   DBG (5, "sanei_pa4s2_scsi_pp_get_status: status=0x%02X\n", *status);
1031   DBG (6, "sanei_pa4s2_scsi_pp_get_status: returning SANE_STATUS_GOOD\n");
1032 
1033   return SANE_STATUS_GOOD;
1034 }
1035 
1036 /*
1037  * SCSI-over-parallel scanners need this done when a register is
1038  * selected
1039  */
1040 SANE_Status
sanei_pa4s2_scsi_pp_reg_select(int fd,int reg)1041 sanei_pa4s2_scsi_pp_reg_select (int fd, int reg)
1042 {
1043   TEST_DBG_INIT ();
1044 
1045 #if defined(HAVE_LIBIEEE1284)
1046   if ((fd < 0) || (fd >= pplist.portc))
1047 #else
1048   if ((fd < 0) || (fd >= NELEMS (port)))
1049 #endif
1050     {
1051 
1052       DBG (2, "sanei_pa4s2_scsi_pp_reg_select: invalid fd %d\n", fd);
1053       DBG (6, "sanei_pa4s2_scsi_pp_reg_select: returning SANE_STATUS_INVAL\n");
1054 
1055       return SANE_STATUS_INVAL;
1056 
1057     }
1058 
1059   if (port[fd].in_use == SANE_FALSE)
1060     {
1061 
1062       DBG (2, "sanei_pa4s2_scsi_pp_reg_select: port is not in use\n");
1063 #if defined(HAVE_LIBIEEE1284)
1064       DBG (4, "sanei_pa4s2_scsi_pp_get_status: port is '%s'\n",
1065       		pplist.portv[fd]->name);
1066 #else
1067       DBG (6, "sanei_pa4s2_scsi_pp_get_status: port is 0x%03lx\n",
1068 	   port[fd].base);
1069 #endif
1070       DBG (5, "sanei_pa4s2_scsi_pp_reg_select: returning SANE_STATUS_INVAL\n");
1071 
1072       return SANE_STATUS_INVAL;
1073 
1074     }
1075 
1076   if (port[fd].enabled == SANE_FALSE)
1077     {
1078 
1079       DBG (2, "sanei_pa4s2_scsi_pp_reg_select: port is not enabled\n");
1080 #if defined(HAVE_LIBIEEE1284)
1081       DBG (4, "sanei_pa4s2_scsi_pp_get_status: port is '%s'\n",
1082       		pplist.portv[fd]->name);
1083 #else
1084       DBG (6, "sanei_pa4s2_scsi_pp_get_status: port is 0x%03lx\n",
1085 	   port[fd].base);
1086 #endif
1087       DBG (5, "sanei_pa4s2_scsi_pp_reg_select: returning SANE_STATUS_INVAL\n");
1088 
1089       return SANE_STATUS_INVAL;
1090 
1091     }
1092 
1093 #if defined(HAVE_LIBIEEE1284)
1094   DBG (6, "sanei_pa4s2_scsi_pp_reg_select: selecting register %u at port '%s'\n",
1095        (int) reg, pplist.portv[fd]->name);
1096 #else
1097   DBG (6, "sanei_pa4s2_scsi_pp_reg_select: selecting register %u at 0x%03lx\n",
1098        (int) reg, (u_long)port[fd].base);
1099 #endif
1100 
1101   outbyte0 (fd, reg | 0x58);
1102   outbyte2 (fd, 0x04);
1103   outbyte2 (fd, 0x06);
1104   outbyte2 (fd, 0x04);
1105   outbyte2 (fd, 0x04);
1106 
1107   return SANE_STATUS_GOOD;
1108 }
1109 
1110 /*
1111  * The SCSI-over-parallel scanners need to be handled a bit differently
1112  * when opened, as they don't return a valid ASIC ID, so this can't be
1113  * used for detecting valid read modes
1114  */
1115 SANE_Status
sanei_pa4s2_scsi_pp_open(const char * dev,int * fd)1116 sanei_pa4s2_scsi_pp_open (const char *dev, int *fd)
1117 {
1118 
1119   u_char val;
1120   SANE_Status status;
1121 
1122   TEST_DBG_INIT ();
1123 
1124   DBG(4, "sanei_pa4s2_scsi_pp_open: called for device '%s'\n", dev);
1125   DBG(5, "sanei_pa4s2_scsi_pp_open: trying to connect to port\n");
1126 
1127   if ((*fd = pa4s2_open (dev, &status)) == -1)
1128     {
1129 
1130       DBG (5, "sanei_pa4s2_scsi_pp_open: connection failed\n");
1131 
1132       return status;
1133 
1134     }
1135 
1136   DBG (6, "sanei_pa4s2_scsi_pp_open: connected to device using fd %u\n", *fd);
1137 
1138   DBG (5, "sanei_pa4s2_scsi_pp_open: checking for scanner\n");
1139 
1140   if (sanei_pa4s2_enable (*fd, SANE_TRUE)!=SANE_STATUS_GOOD)
1141     {
1142       DBG (3, "sanei_pa4s2_scsi_pp_open: error enabling device\n");
1143       return SANE_STATUS_IO_ERROR;
1144     }
1145 
1146   /*
1147    * Instead of checking ASIC ID, check device status
1148    */
1149   if (sanei_pa4s2_scsi_pp_get_status(*fd, &val)!=SANE_STATUS_GOOD)
1150     {
1151       DBG (3, "sanei_pa4s2_scsi_pp_open: error getting device status\n");
1152       sanei_pa4s2_enable (*fd, SANE_FALSE);
1153       return SANE_STATUS_IO_ERROR;
1154     }
1155   val&=0xf0;
1156 
1157   if ((val==0xf0)||(val&0x40)||(!(val&0x20)))
1158     {
1159       DBG (3, "sanei_pa4s2_scsi_pp_open: device returned status 0x%02X\n", val);
1160       sanei_pa4s2_enable (*fd, SANE_FALSE);
1161       return SANE_STATUS_DEVICE_BUSY;
1162     }
1163 
1164   if (sanei_pa4s2_enable (*fd, SANE_FALSE)!=SANE_STATUS_GOOD)
1165     {
1166       DBG (3, "sanei_pa4s2_scsi_pp_open: error disabling device\n");
1167       return SANE_STATUS_IO_ERROR;
1168     }
1169 
1170   /* FIXME: it would be nice to try to use a better mode here, but how to
1171    * know if it's going to work? */
1172 
1173   DBG (4, "sanei_pa4s2_scsi_pp_open: returning SANE_STATUS_GOOD\n");
1174 
1175   return SANE_STATUS_GOOD;
1176 }
1177 
1178 SANE_Status
sanei_pa4s2_open(const char * dev,int * fd)1179 sanei_pa4s2_open (const char *dev, int *fd)
1180 {
1181 
1182   u_char asic, val;
1183   SANE_Status status;
1184 
1185   TEST_DBG_INIT ();
1186 
1187   DBG(4, "sanei_pa4s2_open: called for device '%s'\n", dev);
1188   DBG(5, "sanei_pa4s2_open: trying to connect to port\n");
1189 
1190   if ((*fd = pa4s2_open (dev, &status)) == -1)
1191     {
1192 
1193       DBG (5, "sanei_pa4s2_open: connection failed\n");
1194 
1195       return status;
1196 
1197     }
1198 
1199   DBG (6, "sanei_pa4s2_open: connected to device using fd %u\n", *fd);
1200 
1201   DBG (5, "sanei_pa4s2_open: checking for scanner\n");
1202 
1203   sanei_pa4s2_enable (*fd, SANE_TRUE);
1204 
1205   DBG (6, "sanei_pa4s2_open: reading ASIC id\n");
1206 
1207   sanei_pa4s2_readbegin (*fd, 0);
1208 
1209   sanei_pa4s2_readbyte (*fd, &asic);
1210 
1211   sanei_pa4s2_readend (*fd);
1212 
1213   switch (asic)
1214     {
1215 
1216     case PA4S2_ASIC_ID_1013:
1217       DBG (3, "sanei_pa4s2_open: detected ASIC id 1013\n");
1218       break;
1219 
1220     case PA4S2_ASIC_ID_1015:
1221       DBG (3, "sanei_pa4s2_open: detected ASIC id 1015\n");
1222       break;
1223 
1224     case PA4S2_ASIC_ID_1505:
1225       DBG (3, "sanei_pa4s2_open: detected ASIC id 1505\n");
1226       break;
1227 
1228     default:
1229       DBG (1, "sanei_pa4s2_open: could not find scanner\n");
1230       DBG (3, "sanei_pa4s2_open: reported ASIC id 0x%02x\n",
1231 	   asic);
1232 
1233       sanei_pa4s2_enable (*fd, SANE_FALSE);
1234       DBG (5, "sanei_pa4s2_open: closing port\n");
1235 
1236       sanei_pa4s2_close (*fd);
1237 
1238       DBG (5, "sanei_pa4s2_open: returning SANE_STATUS_INVAL\n");
1239 
1240       return SANE_STATUS_INVAL;
1241 
1242     }
1243 
1244   sanei_pa4s2_enable (*fd, SANE_FALSE);
1245 
1246   DBG (4, "sanei_pa4s2_open: trying better modes\n");
1247 
1248   while (port[*fd].mode <= PA4S2_MODE_EPP)
1249     {
1250 
1251       if ((port[*fd].mode == PA4S2_MODE_UNI) &&
1252       ((sanei_pa4s2_interface_options & SANEI_PA4S2_OPT_TRY_MODE_UNI) == 0))
1253 	{
1254 
1255 	  DBG (3, "sanei_pa4s2_open: skipping mode UNI\n");
1256 	  port[*fd].mode++;
1257 	  continue;
1258 
1259 	}
1260 
1261       if ((port[*fd].mode == PA4S2_MODE_EPP) &&
1262       ((sanei_pa4s2_interface_options & SANEI_PA4S2_OPT_NO_EPP) != 0))
1263 	{
1264 	  DBG (3, "sanei_pa4s2_open: skipping mode EPP\n");
1265 	  break;
1266 	}
1267 
1268 
1269       DBG (5, "sanei_pa4s2_open: trying mode %u\n", port[*fd].mode);
1270 
1271       sanei_pa4s2_enable (*fd, SANE_TRUE);
1272 
1273       sanei_pa4s2_readbegin (*fd, 0);
1274 
1275       sanei_pa4s2_readbyte (*fd, &val);
1276 
1277       if (val != asic)
1278 	{
1279 
1280 	  sanei_pa4s2_readend (*fd);
1281 	  sanei_pa4s2_enable (*fd, SANE_FALSE);
1282 	  DBG (5, "sanei_pa4s2_open: mode failed\n");
1283 	  DBG (6, "sanei_pa4s2_open: returned ASIC-ID 0x%02x\n",
1284 	       (int) val);
1285 	  break;
1286 
1287 	}
1288 
1289       sanei_pa4s2_readend (*fd);
1290       sanei_pa4s2_enable (*fd, SANE_FALSE);
1291 
1292       DBG (5, "sanei_pa4s2_open: mode works\n");
1293 
1294       port[*fd].mode++;
1295 
1296     }
1297 
1298   port[*fd].mode--;
1299 
1300   if ((port[*fd].mode == PA4S2_MODE_UNI) &&
1301       ((sanei_pa4s2_interface_options & SANEI_PA4S2_OPT_TRY_MODE_UNI) == 0))
1302     {
1303       port[*fd].mode--;
1304     }
1305 
1306   DBG (5, "sanei_pa4s2_open: using mode %u\n", port[*fd].mode);
1307 
1308   DBG (4, "sanei_pa4s2_open: returning SANE_STATUS_GOOD\n");
1309 
1310   return SANE_STATUS_GOOD;
1311 
1312 }
1313 
1314 void
sanei_pa4s2_close(int fd)1315 sanei_pa4s2_close (int fd)
1316 {
1317 
1318   SANE_Status status;
1319 
1320   TEST_DBG_INIT ();
1321 
1322   DBG (4, "sanei_pa4s2_close: fd = %d\n", fd);
1323 
1324 #if defined(HAVE_LIBIEEE1284)
1325   if ((fd < 0) || (fd >= pplist.portc))
1326 #else
1327   if ((fd < 0) || (fd >= NELEMS (port)))
1328 #endif
1329     {
1330 
1331       DBG (2, "sanei_pa4s2_close: fd %d is invalid\n", fd);
1332       DBG (5, "sanei_pa4s2_close: failed\n");
1333       return;
1334 
1335     }
1336 
1337   if (port[fd].in_use == SANE_FALSE)
1338     {
1339 
1340       DBG (2, "sanei_pa4s2_close: port is not in use\n");
1341 #if defined(HAVE_LIBIEEE1284)
1342       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1343 #else
1344       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1345 #endif
1346       DBG (5, "sanei_pa4s2_close: failed\n");
1347       return;
1348 
1349     }
1350 
1351   DBG (5, "sanei_pa4s2_close: freeing resources\n");
1352 
1353   if (pa4s2_close (fd, &status) == -1)
1354     {
1355 
1356       DBG (2, "sanei_pa4s2_close: could not close scanner\n");
1357       DBG (5, "sanei_pa4s2_close: failed\n");
1358       return;
1359     }
1360 
1361   DBG (5, "sanei_pa4s2_close: finished\n");
1362 
1363 }
1364 
1365 SANE_Status
sanei_pa4s2_enable(int fd,int enable)1366 sanei_pa4s2_enable (int fd, int enable)
1367 {
1368 
1369   TEST_DBG_INIT ();
1370 
1371   DBG (4, "sanei_pa4s2_enable: called for fd %d with value %d\n",
1372        fd, enable);
1373 
1374 #if defined(HAVE_LIBIEEE1284)
1375   if ((fd < 0) || (fd >= pplist.portc))
1376 #else
1377   if ((fd < 0) || (fd >= NELEMS (port)))
1378 #endif
1379     {
1380 
1381       DBG (2, "sanei_pa4s2_enable: fd %d is invalid\n", fd);
1382       DBG (5, "sanei_pa4s2_enable: returning SANE_STATUS_INVAL\n");
1383 
1384       return SANE_STATUS_INVAL;
1385 
1386     }
1387 
1388   if (port[fd].in_use == SANE_FALSE)
1389     {
1390 
1391       DBG (2, "sanei_pa4s2_enable: port is not in use\n");
1392 #if defined(HAVE_LIBIEEE1284)
1393       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1394 #else
1395       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1396 #endif
1397       DBG (5, "sanei_pa4s2_enable: returning SANE_STATUS_INVAL\n");
1398 
1399       return SANE_STATUS_INVAL;
1400 
1401     }
1402 
1403   if ((enable != SANE_TRUE) && (enable != SANE_FALSE))
1404     {
1405 
1406       DBG (2, "sanei_pa4s2_enable: invalid value %d\n", enable);
1407       DBG (5, "sanei_pa4s2_enable: returning SANE_STATUS_INVAL\n");
1408 
1409       return SANE_STATUS_INVAL;
1410 
1411     }
1412 
1413   if ((unsigned int) enable == port[fd].enabled)
1414     {
1415 
1416       DBG (3, "sanei_pa4s2_enable: senseless call...\n");
1417       DBG (4, "sanei_pa4s2_enable: aborting\n");
1418       DBG (5, "sanei_pa4s2_enable: returning SANE_STATUS_GOOD\n");
1419 
1420       return SANE_STATUS_GOOD;
1421 
1422     }
1423 
1424   if (enable == SANE_TRUE)
1425     {
1426 
1427 #if defined(HAVE_LIBIEEE1284)
1428       DBG (4, "sanei_pa4s2_enable: enable port '%s'\n", pplist.portv[fd]->name);
1429 #else
1430       DBG (4, "sanei_pa4s2_enable: enable port 0x%03lx\n", port[fd].base);
1431 
1432       /* io-permissions are not inherited after fork (at least not on
1433          linux 2.2, although they seem to be inherited on linux 2.4),
1434          so we should make sure we get the permission */
1435 
1436       if (ioperm (port[fd].base, 5, 1))
1437       {
1438           DBG (1, "sanei_pa4s2_enable: cannot get io privilege for port"
1439 	       " 0x%03lx\n", port[fd].base);
1440 
1441           DBG (5, "sanei_pa4s2_enable:: marking port[%d] as unused\n", fd);
1442           port[fd].in_use = SANE_FALSE;
1443 
1444           DBG (5, "sanei_pa4s2_enable:: returning SANE_STATUS_IO_ERROR\n");
1445           return SANE_STATUS_IO_ERROR;
1446       }
1447 #endif
1448 
1449       if (pa4s2_enable (fd, port[fd].prelock) != 0)
1450         {
1451      	  DBG (1, "sanei_pa4s2_enable: failed to enable port\n");
1452 	  DBG (5, "sanei_pa4s2_enable: returning SANE_STATUS_IO_ERROR\n");
1453 
1454 	  return SANE_STATUS_IO_ERROR;
1455 	}
1456 
1457     }
1458   else
1459     {
1460 
1461 #if defined(HAVE_LIBIEEE1284)
1462       DBG (4, "sanei_pa4s2_enable: disable port '%s'\n",
1463       		pplist.portv[fd]->name);
1464 #else
1465       DBG (4, "sanei_pa4s2_enable: disable port 0x%03lx\n", port[fd].base);
1466 #endif
1467 
1468       pa4s2_disable (fd, port[fd].prelock);
1469 
1470     }
1471 
1472   port[fd].enabled = enable;
1473 
1474   DBG (5, "sanei_pa4s2_enable: returning SANE_STATUS_GOOD\n");
1475 
1476   return SANE_STATUS_GOOD;
1477 }
1478 
1479 SANE_Status
sanei_pa4s2_readbegin(int fd,u_char reg)1480 sanei_pa4s2_readbegin (int fd, u_char reg)
1481 {
1482 
1483   TEST_DBG_INIT ();
1484 
1485   DBG (4, "sanei_pa4s2_readbegin: called for fd %d and register %u\n",
1486        fd, (int) reg);
1487 
1488 #if defined(HAVE_LIBIEEE1284)
1489   if ((fd < 0) || (fd >= pplist.portc))
1490 #else
1491   if ((fd < 0) || (fd >= NELEMS (port)))
1492 #endif
1493     {
1494 
1495       DBG (2, "sanei_pa4s2_readbegin: invalid fd %d\n", fd);
1496       DBG (5, "sanei_pa4s2_readbegin: returning SANE_STATUS_INVAL\n");
1497 
1498       return SANE_STATUS_INVAL;
1499 
1500     }
1501 
1502   if (port[fd].in_use == SANE_FALSE)
1503     {
1504 
1505       DBG (2, "sanei_pa4s2_readbegin: port is not in use\n");
1506 #if defined(HAVE_LIBIEEE1284)
1507       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1508 #else
1509       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1510 #endif
1511       DBG (5, "sanei_pa4s2_readbegin: returning SANE_STATUS_INVAL\n");
1512 
1513       return SANE_STATUS_INVAL;
1514 
1515     }
1516 
1517   if (port[fd].enabled == SANE_FALSE)
1518     {
1519 
1520       DBG (2, "sanei_pa4s2_readbegin: port is not enabled\n");
1521 #if defined(HAVE_LIBIEEE1284)
1522       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1523 #else
1524       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1525 #endif
1526       DBG (5, "sanei_pa4s2_readbegin: returning SANE_STATUS_INVAL\n");
1527 
1528       return SANE_STATUS_INVAL;
1529 
1530     }
1531 
1532   switch (port[fd].mode)
1533     {
1534 
1535     case PA4S2_MODE_EPP:
1536 
1537       DBG (5, "sanei_pa4s2_readbegin: EPP readbegin\n");
1538       pa4s2_readbegin_epp (fd, reg);
1539       break;
1540 
1541     case PA4S2_MODE_UNI:
1542 
1543       DBG (5, "sanei_pa4s2_readbegin: UNI readbegin\n");
1544       pa4s2_readbegin_uni (fd, reg);
1545       break;
1546 
1547     case PA4S2_MODE_NIB:
1548 
1549       DBG (5, "sanei_pa4s2_readbegin: NIB readbegin\n");
1550       pa4s2_readbegin_nib (fd, reg);
1551       break;
1552 
1553     default:
1554 
1555       DBG (1, "sanei_pa4s2_readbegin: port info broken\n");
1556       DBG (3, "sanei_pa4s2_readbegin: invalid port mode\n");
1557 #if defined(HAVE_LIBIEEE1284)
1558       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1559 #else
1560       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1561 #endif
1562       DBG (5, "sanei_pa4s2_readbegin: return SANE_STATUS_INVAL\n");
1563 
1564       return SANE_STATUS_INVAL;
1565 
1566     }
1567 
1568   DBG (5, "sanei_pa4s2_readbegin: returning SANE_STATUS_GOOD\n");
1569 
1570   return SANE_STATUS_GOOD;
1571 }
1572 
1573 SANE_Status
sanei_pa4s2_readbyte(int fd,u_char * val)1574 sanei_pa4s2_readbyte (int fd, u_char * val)
1575 {
1576 
1577   TEST_DBG_INIT ();
1578 
1579   DBG (4, "sanei_pa4s2_readbyte: called with fd %d\n", fd);
1580 
1581   if (val == NULL)
1582     {
1583 
1584       DBG (1, "sanei_pa4s2_readbyte: got NULL pointer as result buffer\n");
1585       return SANE_STATUS_INVAL;
1586 
1587     }
1588 
1589 #if defined(HAVE_LIBIEEE1284)
1590   if ((fd < 0) || (fd >= pplist.portc))
1591 #else
1592   if ((fd < 0) || (fd >= NELEMS (port)))
1593 #endif
1594     {
1595 
1596       DBG (2, "sanei_pa4s2_readbyte: invalid fd %d\n", fd);
1597       DBG (5, "sanei_pa4s2_readbyte: returning SANE_STATUS_INVAL\n");
1598       return SANE_STATUS_INVAL;
1599 
1600     }
1601 
1602   if (port[fd].in_use == SANE_FALSE)
1603     {
1604 
1605       DBG (2, "sanei_pa4s2_readbyte: port is not in use\n");
1606 #if defined(HAVE_LIBIEEE1284)
1607       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1608 #else
1609       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1610 #endif
1611       DBG (5, "sanei_pa4s2_readbyte: returning SANE_STATUS_INVAL\n");
1612 
1613       return SANE_STATUS_INVAL;
1614 
1615     }
1616 
1617   if (port[fd].enabled == SANE_FALSE)
1618     {
1619 
1620       DBG (2, "sanei_pa4s2_readbyte: port is not enabled\n");
1621 #if defined(HAVE_LIBIEEE1284)
1622       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1623 #else
1624       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1625 #endif
1626       DBG (5, "sanei_pa4s2_readbyte: returning SANE_STATUS_INVAL\n");
1627 
1628       return SANE_STATUS_INVAL;
1629 
1630     }
1631 
1632   DBG (4, "sanei_pa4s2_readbyte: we hope, the backend called\n");
1633   DBG (4, "sanei_pa4s2_readbyte: readbegin, so the port is ok...\n");
1634 
1635   DBG (6, "sanei_pa4s2_readbyte: this means, I did not check it - it's\n");
1636   DBG (6, "sanei_pa4s2_readbyte: not my fault, if your PC burns down.\n");
1637 
1638   switch (port[fd].mode)
1639     {
1640 
1641     case PA4S2_MODE_EPP:
1642 
1643       DBG (5, "sanei_pa4s2_readbyte: read in EPP mode\n");
1644       *val = pa4s2_readbyte_epp (fd);
1645       break;
1646 
1647 
1648     case PA4S2_MODE_UNI:
1649 
1650       DBG (5, "sanei_pa4s2_readbyte: read in UNI mode\n");
1651       *val = pa4s2_readbyte_uni (fd);
1652       break;
1653 
1654 
1655     case PA4S2_MODE_NIB:
1656 
1657       DBG (5, "sanei_pa4s2_readbyte: read in NIB mode\n");
1658       *val = pa4s2_readbyte_nib (fd);
1659       break;
1660 
1661     default:
1662 
1663       DBG (1, "sanei_pa4s2_readbyte: port info broken\n");
1664       DBG (2, "sanei_pa4s2_readbyte: probably the port wasn't"
1665 	   " correct configured...\n");
1666       DBG (3, "sanei_pa4s2_readbyte: invalid port mode\n");
1667       DBG (6, "sanei_pa4s2_readbyte: port mode %u\n",
1668 	   port[fd].mode);
1669       DBG (6, "sanei_pa4s2_readbyte: I told you!!!\n");
1670       DBG (5, "sanei_pa4s2_readbyte: return"
1671 	   " SANE_STATUS_INVAL\n");
1672 
1673       return SANE_STATUS_INVAL;
1674     }
1675 
1676   DBG (5, "sanei_pa4s2_readbyte: read finished\n");
1677 
1678   DBG (6, "sanei_pa4s2_readbyte: got value 0x%02x\n", (int) *val);
1679 
1680   DBG (5, "sanei_pa4s2_readbyte: returning SANE_STATUS_GOOD\n");
1681 
1682   return SANE_STATUS_GOOD;
1683 
1684 }
1685 
1686 SANE_Status
sanei_pa4s2_readend(int fd)1687 sanei_pa4s2_readend (int fd)
1688 {
1689 
1690   TEST_DBG_INIT ();
1691 
1692   DBG (4, "sanei_pa4s2_readend: called for fd %d\n", fd);
1693 
1694 #if defined(HAVE_LIBIEEE1284)
1695   if ((fd < 0) || (fd >= pplist.portc))
1696 #else
1697   if ((fd < 0) || (fd >= NELEMS (port)))
1698 #endif
1699     {
1700 
1701       DBG (2, "sanei_pa4s2_readend: invalid fd %d\n", fd);
1702       DBG (5, "sanei_pa4s2_readend: returning SANE_STATUS_INVAL\n");
1703 
1704       return SANE_STATUS_INVAL;
1705 
1706     }
1707 
1708   if (port[fd].in_use == SANE_FALSE)
1709     {
1710 
1711       DBG (2, "sanei_pa4s2_readend: port is not in use\n");
1712 #if defined(HAVE_LIBIEEE1284)
1713       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1714 #else
1715       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1716 #endif
1717       DBG (5, "sanei_pa4s2_readend: returning SANE_STATUS_INVAL\n");
1718 
1719       return SANE_STATUS_INVAL;
1720 
1721     }
1722 
1723   if (port[fd].enabled == SANE_FALSE)
1724     {
1725 
1726       DBG (2, "sanei_pa4s2_readend: port is not enabled\n");
1727 #if defined(HAVE_LIBIEEE1284)
1728       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1729 #else
1730       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1731 #endif
1732       DBG (5, "sanei_pa4s2_readend: returning SANE_STATUS_INVAL\n");
1733 
1734       return SANE_STATUS_INVAL;
1735 
1736     }
1737 
1738   DBG (4, "sanei_pa4s2_readend: we hope, the backend called\n");
1739   DBG (4, "sanei_pa4s2_readend: readbegin, so the port is ok...\n");
1740 
1741   DBG (6, "sanei_pa4s2_readend: this means, I did not check it - it's\n");
1742   DBG (6, "sanei_pa4s2_readend: not my fault, if your PC burns down.\n");
1743 
1744   switch (port[fd].mode)
1745     {
1746 
1747     case PA4S2_MODE_EPP:
1748 
1749       DBG (5, "sanei_pa4s2_readend: EPP mode readend\n");
1750       pa4s2_readend_epp (fd);
1751       break;
1752 
1753 
1754     case PA4S2_MODE_UNI:
1755 
1756       DBG (5, "sanei_pa4s2_readend: UNI mode readend\n");
1757       pa4s2_readend_uni (fd);
1758       break;
1759 
1760 
1761     case PA4S2_MODE_NIB:
1762 
1763       DBG (5, "sanei_pa4s2_readend: NIB mode readend\n");
1764       pa4s2_readend_nib (fd);
1765       break;
1766 
1767     default:
1768 
1769       DBG (1, "sanei_pa4s2_readend: port info broken\n");
1770       DBG (2, "sanei_pa4s2_readend: probably the port wasn't"
1771 	   " correct configured...\n");
1772       DBG (3, "sanei_pa4s2_readend: invalid port mode\n");
1773       DBG (6, "sanei_pa4s2_readend: port mode %u\n",
1774 	   port[fd].mode);
1775       DBG (6, "sanei_pa4s2_readend: I told you!!!\n");
1776       DBG (5, "sanei_pa4s2_readend: return"
1777 	   " SANE_STATUS_INVAL\n");
1778 
1779       return SANE_STATUS_INVAL;
1780     }
1781 
1782 
1783   DBG (5, "sanei_pa4s2_readend: returning SANE_STATUS_GOOD\n");
1784 
1785   return SANE_STATUS_GOOD;
1786 
1787 }
1788 
1789 SANE_Status
sanei_pa4s2_writebyte(int fd,u_char reg,u_char val)1790 sanei_pa4s2_writebyte (int fd, u_char reg, u_char val)
1791 {
1792 
1793   TEST_DBG_INIT ();
1794 
1795   DBG (4, "sanei_pa4s2_writebyte: called for fd %d, reg %u and val %u\n",
1796        fd, (int) reg, (int) val);
1797 
1798 #if defined(HAVE_LIBIEEE1284)
1799   if ((fd < 0) || (fd >= pplist.portc))
1800 #else
1801   if ((fd < 0) || (fd >= NELEMS (port)))
1802 #endif
1803     {
1804 
1805       DBG (2, "sanei_pa4s2_writebyte: invalid fd %d\n", fd);
1806       DBG (5, "sanei_pa4s2_writebyte: returning SANE_STATUS_INVAL\n");
1807 
1808       return SANE_STATUS_INVAL;
1809 
1810     }
1811 
1812   if (port[fd].in_use == SANE_FALSE)
1813     {
1814 
1815       DBG (2, "sanei_pa4s2_writebyte: port is not in use\n");
1816 #if defined(HAVE_LIBIEEE1284)
1817       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1818 #else
1819       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1820 #endif
1821       DBG (5, "sanei_pa4s2_writebyte: returning SANE_STATUS_INVAL\n");
1822 
1823       return SANE_STATUS_INVAL;
1824 
1825     }
1826 
1827   if (port[fd].enabled == SANE_FALSE)
1828     {
1829 
1830       DBG (2, "sanei_pa4s2_writebyte: port is not enabled\n");
1831 #if defined(HAVE_LIBIEEE1284)
1832       DBG (6, "sanei_pa4s2_close: port is '%s'\n", pplist.portv[fd]->name);
1833 #else
1834       DBG (6, "sanei_pa4s2_close: port is 0x%03lx\n", port[fd].base);
1835 #endif
1836       DBG (5, "sanei_pa4s2_readbegin: returning SANE_STATUS_INVAL\n");
1837 
1838       return SANE_STATUS_INVAL;
1839 
1840     }
1841 
1842   switch (port[fd].mode)
1843     {
1844 
1845     case PA4S2_MODE_EPP:
1846     case PA4S2_MODE_UNI:
1847     case PA4S2_MODE_NIB:
1848 
1849       DBG (5, "sanei_pa4s2_writebyte: NIB/UNI/EPP write\n");
1850       pa4s2_writebyte_any (fd, reg, val);
1851       break;
1852 
1853     default:
1854 
1855       DBG (1, "sanei_pa4s2_writebyte: port info broken\n");
1856       DBG (3, "sanei_pa4s2_writebyte: invalid port mode\n");
1857       DBG (6, "sanei_pa4s2_writebyte: port mode %u\n",
1858 	   port[fd].mode);
1859       DBG (5, "sanei_pa4s2_writebyte: return"
1860 	   " SANE_STATUS_INVAL\n");
1861 
1862       return SANE_STATUS_INVAL;
1863 
1864     }
1865 
1866   DBG (5, "sanei_pa4s2_writebyte: returning SANE_STATUS_GOOD\n");
1867 
1868   return SANE_STATUS_GOOD;
1869 }
1870 
1871 SANE_Status
sanei_pa4s2_options(u_int * options,int set)1872 sanei_pa4s2_options (u_int * options, int set)
1873 {
1874 
1875   TEST_DBG_INIT ();
1876 
1877   DBG (4, "sanei_pa4s2_options: called with options %u and set = %d\n",
1878        *options, set);
1879 
1880   if ((set != SANE_TRUE) && (set != SANE_FALSE))
1881     DBG (2, "sanei_pa4s2_options: value of set is invalid\n");
1882 
1883   if ((set == SANE_TRUE) && (*options > 7))
1884     DBG (2, "sanei_pa4s2_options: value of *options is invalid\n");
1885 
1886   if (set == SANE_TRUE)
1887     {
1888 
1889       DBG (5, "sanei_pa4s2_options: setting options to %u\n", *options);
1890 
1891       sanei_pa4s2_interface_options = *options;
1892 
1893     }
1894   else
1895     {
1896 
1897       DBG (5, "sanei_pa4s2_options: options are set to %u\n",
1898 	   sanei_pa4s2_interface_options);
1899 
1900       *options = sanei_pa4s2_interface_options;
1901 
1902     }
1903 
1904   DBG (5, "sanei_pa4s2_options: returning SANE_STATUS_GOOD\n");
1905 
1906   return SANE_STATUS_GOOD;
1907 
1908 }
1909 
1910 #else /* !HAVE_IOPERM */
1911 
1912 
1913 SANE_Status
sanei_pa4s2_open(const char * dev,int * fd)1914 sanei_pa4s2_open (const char *dev, int *fd)
1915 {
1916 
1917   TEST_DBG_INIT ();
1918 
1919   if (fd)
1920   	*fd = -1;
1921 
1922   DBG (4, "sanei_pa4s2_open: called for device `%s`\n", dev);
1923   DBG (3, "sanei_pa4s2_open: A4S2 support not compiled\n");
1924   DBG (6, "sanei_pa4s2_open: basically, this backend does only compile\n");
1925   DBG (6, "sanei_pa4s2_open: on x86 architectures. Furthermore it\n");
1926   DBG (6, "sanei_pa4s2_open: needs ioperm() and inb()/outb() calls.\n");
1927   DBG (6, "sanei_pa4s2_open: alternatively it makes use of libieee1284\n");
1928   DBG (6, "sanei_pa4s2_open: (which isn't present either)\n");
1929   DBG (5, "sanei_pa4s2_open: returning SANE_STATUS_INVAL\n");
1930 
1931   return SANE_STATUS_INVAL;
1932 
1933 }
1934 
1935 void
sanei_pa4s2_close(int fd)1936 sanei_pa4s2_close (int fd)
1937 {
1938 
1939   TEST_DBG_INIT ();
1940 
1941   DBG (4, "sanei_pa4s2_close: called for fd %d\n", fd);
1942   DBG (2, "sanei_pa4s2_close: fd %d is invalid\n", fd);
1943   DBG (3, "sanei_pa4s2_close: A4S2 support not compiled\n");
1944   DBG (6, "sanei_pa4s2_close: so I wonder, why this function is called"
1945        " anyway.\n");
1946   DBG (6, "sanei_pa4s2_close: maybe this is a bug in the backend.\n");
1947   DBG (5, "sanei_pa4s2_close: returning\n");
1948 
1949   return;
1950 }
1951 
1952 SANE_Status
sanei_pa4s2_enable(int fd,int enable)1953 sanei_pa4s2_enable (int fd, int enable)
1954 {
1955 
1956   TEST_DBG_INIT ();
1957 
1958   DBG (4, "sanei_pa4s2_enable: called for fd %d with value=%d\n",
1959        fd, enable);
1960   DBG (2, "sanei_pa4s2_enable: fd %d is invalid\n", fd);
1961 
1962   if ((enable != SANE_TRUE) && (enable != SANE_FALSE))
1963     DBG (2, "sanei_pa4s2_enable: value %d is invalid\n", enable);
1964 
1965   DBG (3, "sanei_pa4s2_enable: A4S2 support not compiled\n");
1966   DBG (6, "sanei_pa4s2_enable: oops, I think there's someone going to\n");
1967   DBG (6, "sanei_pa4s2_enable: produce a lot of garbage...\n");
1968   DBG (5, "sanei_pa4s2_enable: returning SANE_STATUS_INVAL\n");
1969 
1970   return SANE_STATUS_INVAL;
1971 }
1972 
1973 SANE_Status
sanei_pa4s2_readbegin(int fd,u_char reg)1974 sanei_pa4s2_readbegin (int fd, u_char reg)
1975 {
1976 
1977   TEST_DBG_INIT ();
1978 
1979   DBG (4, "sanei_pa4s2_readbegin: called for fd %d and register %d\n",
1980        fd, (int) reg);
1981   DBG (2, "sanei_pa4s2_readbegin: fd %d is invalid\n", fd);
1982 
1983   DBG (3, "sanei_pa4s2_readbegin: A4S2 support not compiled\n");
1984   DBG (6, "sanei_pa4s2_readbegin: don't look - this is going to be\n");
1985   DBG (6, "sanei_pa4s2_readbegin: worse then you'd expect...\n");
1986   DBG (5, "sanei_pa4s2_readbegin: returning SANE_STATUS_INVAL\n");
1987 
1988   return SANE_STATUS_INVAL;
1989 
1990 }
1991 
1992 SANE_Status
sanei_pa4s2_readbyte(int fd,u_char * val)1993 sanei_pa4s2_readbyte (int fd, u_char * val)
1994 {
1995 
1996   TEST_DBG_INIT ();
1997 
1998   if (val)
1999   	*val = 0;
2000 
2001   DBG (4, "sanei_pa4s2_readbyte: called for fd %d\n", fd);
2002   DBG (2, "sanei_pa4s2_readbyte: fd %d is invalid\n", fd);
2003   DBG (3, "sanei_pa4s2_readbyte: A4S2 support not compiled\n");
2004   DBG (6, "sanei_pa4s2_readbyte: shit happens\n");
2005   DBG (5, "sanei_pa4s2_readbyte: returning SANE_STATUS_INVAL\n");
2006 
2007   return SANE_STATUS_INVAL;
2008 }
2009 
2010 SANE_Status
sanei_pa4s2_readend(int fd)2011 sanei_pa4s2_readend (int fd)
2012 {
2013 
2014   TEST_DBG_INIT ();
2015 
2016   DBG (4, "sanei_pa4s2_readend: called for fd %d\n", fd);
2017   DBG (2, "sanei_pa4s2_readend: fd %d is invalid\n", fd);
2018   DBG (3, "sanei_pa4s2_readend: A4S2 support not compiled\n");
2019   DBG (6, "sanei_pa4s2_readend: it's too late anyway\n");
2020   DBG (5, "sanei_pa4s2_readend: returning SANE_STATUS_INVAL\n");
2021 
2022   return SANE_STATUS_INVAL;
2023 
2024 }
2025 
2026 SANE_Status
sanei_pa4s2_writebyte(int fd,u_char reg,u_char val)2027 sanei_pa4s2_writebyte (int fd, u_char reg, u_char val)
2028 {
2029 
2030   TEST_DBG_INIT ();
2031 
2032   DBG (4, "sanei_pa4s2_writebyte: called for fd %d and register %d, "
2033        "value = %u\n", fd, (int) reg, (int) val);
2034   DBG (2, "sanei_pa4s2_writebyte: fd %d is invalid\n", fd);
2035   DBG (3, "sanei_pa4s2_writebyte: A4S2 support not compiled\n");
2036   DBG (6, "sanei_pa4s2_writebyte: whatever backend you're using, tell\n");
2037   DBG (6, "sanei_pa4s2_writebyte: the maintainer his code has bugs...\n");
2038   DBG (5, "sanei_pa4s2_writebyte: returning SANE_STATUS_INVAL\n");
2039 
2040   return SANE_STATUS_INVAL;
2041 
2042 }
2043 
2044 SANE_Status
sanei_pa4s2_options(u_int * options,int set)2045 sanei_pa4s2_options (u_int * options, int set)
2046 {
2047 
2048   TEST_DBG_INIT ();
2049 
2050   DBG (4, "sanei_pa4s2_options: called with options %u and set = %d\n",
2051        *options, set);
2052 
2053   if ((set != SANE_TRUE) && (set != SANE_FALSE))
2054     DBG (2, "sanei_pa4s2_options: value of set is invalid\n");
2055 
2056   if ((set == SANE_TRUE) && (*options > 3))
2057     DBG (2, "sanei_pa4s2_options: value of *options is invalid\n");
2058 
2059   DBG (3, "sanei_pa4s2_options: A4S2 support not compiled\n");
2060   DBG (5, "sanei_pa4s2_options: returning SANE_STATUS_INVAL\n");
2061 
2062   return SANE_STATUS_INVAL;
2063 
2064 }
2065 
2066 const char **
sanei_pa4s2_devices()2067 sanei_pa4s2_devices()
2068 {
2069   TEST_DBG_INIT ();
2070   DBG (4, "sanei_pa4s2_devices: invoked\n");
2071 
2072   DBG (3, "sanei_pa4s2_devices: A4S2 support not compiled\n");
2073   DBG (5, "sanei_pa4s2_devices: returning empty list\n");
2074 
2075   return calloc(1, sizeof(char *));
2076 }
2077 
2078 SANE_Status
sanei_pa4s2_scsi_pp_get_status(int fd,u_char * status)2079 sanei_pa4s2_scsi_pp_get_status(int fd, u_char *status)
2080 {
2081   TEST_DBG_INIT ();
2082   DBG (4, "sanei_pa4s2_scsi_pp_get_status: fd=%d, status=%p\n",
2083        fd, (void *) status);
2084   DBG (3, "sanei_pa4s2_scsi_pp_get_status: A4S2 support not compiled\n");
2085   return SANE_STATUS_UNSUPPORTED;
2086 }
2087 
2088 SANE_Status
sanei_pa4s2_scsi_pp_reg_select(int fd,int reg)2089 sanei_pa4s2_scsi_pp_reg_select (int fd, int reg)
2090 {
2091   TEST_DBG_INIT ();
2092   DBG (4, "sanei_pa4s2_scsi_pp_reg_select: fd=%d, reg=%d\n",
2093        fd, reg);
2094   DBG (3, "sanei_pa4s2_devices: A4S2 support not compiled\n");
2095   return SANE_STATUS_UNSUPPORTED;
2096 }
2097 
2098 SANE_Status
sanei_pa4s2_scsi_pp_open(const char * dev,int * fd)2099 sanei_pa4s2_scsi_pp_open (const char *dev, int *fd)
2100 {
2101   TEST_DBG_INIT ();
2102   DBG (4, "sanei_pa4s2_scsi_pp_open: dev=%s, fd=%p\n",
2103        dev, (void *) fd);
2104   DBG (3, "sanei_pa4s2_scsi_pp_open: A4S2 support not compiled\n");
2105   return SANE_STATUS_UNSUPPORTED;
2106 }
2107 
2108 #endif /* !HAVE_IOPERM */
2109