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