1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 2003 James Perry
3    This file is part of the SANE package.
4 
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 
18    As a special exception, the authors of SANE give permission for
19    additional uses of the libraries contained in this release of SANE.
20 
21    The exception is that, if you link a SANE library with other files
22    to produce an executable, this does not by itself cause the
23    resulting executable to be covered by the GNU General Public
24    License.  Your use of that executable is in no way restricted on
25    account of linking the SANE library code into it.
26 
27    This exception does not, however, invalidate any other reasons why
28    the executable file might be covered by the GNU General Public
29    License.
30 
31    If you submit changes to SANE to the maintainers to be included in
32    a subsequent release, you agree by submitting the changes that
33    those changes may be distributed with this exception intact.
34 
35    If you write modifications of your own for SANE, it is your choice
36    whether to permit this exception to apply to your modifications.
37    If you do not wish that, delete this exception notice.
38 
39    This file implements the Mustek SCSI-over-parallel port protocol
40    used by, for example, the Paragon 600 II EP
41 */
42 
43 
44 /**************************************************************************/
45 #include "../include/sane/config.h"
46 
47 #include <ctype.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/wait.h>
60 
61 #include <time.h>
62 
63 #include "../include/sane/sane.h"
64 #include "../include/sane/sanei.h"
65 #include "../include/sane/saneopts.h"
66 #include "../include/sane/sanei_debug.h"
67 #include "../include/sane/sanei_pa4s2.h"
68 
69 /*
70  * Number of times to retry sending a SCSI command before giving up
71  */
72 #define MUSTEK_SCSI_PP_NUM_RETRIES 4
73 
74 /*
75  * Internal middle-level API functionality
76  */
77 static int mustek_scsi_pp_timeout = 5000;
78 
79 /* FIXME: use same method as mustek.c ? */
80 static int
mustek_scsi_pp_get_timenull81 mustek_scsi_pp_get_time ()
82 {
83   struct timeval tv;
84   int retval;
85 
86   gettimeofday (&tv, 0);
87 
88   retval = tv.tv_sec * 1000 + tv.tv_usec / 1000;
89 
90   return retval;
91 }
92 
93 static u_char mustek_scsi_pp_register = 0;
94 
95 
96 static SANE_Status
mustek_scsi_pp_select_register(int fd, u_char reg)97 mustek_scsi_pp_select_register (int fd, u_char reg)
98 {
99   DBG (5, "mustek_scsi_pp_select_register: selecting register %d on fd %d\n",
100        reg, fd);
101 
102   mustek_scsi_pp_register = reg;
103 
104   return sanei_pa4s2_scsi_pp_reg_select (fd, reg);
105 }
106 
107 static SANE_Status
mustek_scsi_pp_wait_for_valid_status(int fd)108 mustek_scsi_pp_wait_for_valid_status (int fd)
109 {
110   int start_time;
111   u_char status;
112 
113   DBG (5, "mustek_scsi_pp_wait_for_valid_status: entering\n");
114 
115   start_time = mustek_scsi_pp_get_time ();
116 
117   do
118     {
119       if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
120 	{
121 	  DBG (2,
122 	       "mustek_scsi_pp_wait_for_valid_status: I/O error while getting status\n");
123 	  return SANE_STATUS_IO_ERROR;
124 	}
125 
126       status &= 0xf0;
127 
128       if ((status != 0xf0) && (!(status & 0x40)) && (status & 0x20))
129 	{
130 	  DBG (5,
131 	       "mustek_scsi_pp_wait_for_valid_status: returning success\n");
132 	  return SANE_STATUS_GOOD;
133 	}
134     }
135   while ((mustek_scsi_pp_get_time () - start_time) < mustek_scsi_pp_timeout);
136 
137   DBG (2, "mustek_scsi_pp_wait_for_valid_status: timed out\n");
138   return SANE_STATUS_DEVICE_BUSY;
139 }
140 
141 static SANE_Status
mustek_scsi_pp_wait_for_status_bit_5_set(int fd)142 mustek_scsi_pp_wait_for_status_bit_5_set (int fd)
143 {
144   int t;
145   u_char status;
146 
147   DBG (5, "mustek_scsi_pp_wait_for_status_bit_5_set: entering\n");
148 
149   t = mustek_scsi_pp_get_time ();
150 
151   do
152     {
153       if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
154 	{
155 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_5_set: I/O error\n");
156 	  return SANE_STATUS_IO_ERROR;
157 	}
158       if (status & 0x20)
159 	{
160 	  DBG (5,
161 	       "mustek_scsi_pp_wait_for_status_bit_5_set: returning success\n");
162 	  return SANE_STATUS_GOOD;
163 	}
164     }
165   while ((mustek_scsi_pp_get_time () - t) < mustek_scsi_pp_timeout);
166 
167   DBG (2, "mustek_scsi_pp_wait_for_status_bit_5_set: timed out\n");
168   return SANE_STATUS_DEVICE_BUSY;
169 }
170 
171 static SANE_Status
mustek_scsi_pp_wait_for_status_bit_5_clear(int fd)172 mustek_scsi_pp_wait_for_status_bit_5_clear (int fd)
173 {
174   int t;
175   u_char status;
176 
177   DBG (5, "mustek_scsi_pp_wait_for_status_bit_5_clear: entering\n");
178 
179   t = mustek_scsi_pp_get_time ();
180 
181   do
182     {
183       if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
184 	{
185 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_5_clear: I/O error\n");
186 	  return SANE_STATUS_IO_ERROR;
187 	}
188 
189       if (!(status & 0x20))
190 	{
191 	  DBG (5,
192 	       "mustek_scsi_pp_wait_for_status_bit_5_clear: returning success\n");
193 	  return SANE_STATUS_GOOD;
194 	}
195     }
196   while ((mustek_scsi_pp_get_time () - t) < mustek_scsi_pp_timeout);
197 
198   DBG (2, "mustek_scsi_pp_wait_for_status_bit_5_clear: timed out\n");
199   return SANE_STATUS_DEVICE_BUSY;
200 }
201 
202 static SANE_Status
mustek_scsi_pp_wait_for_status_bit_7_set(int fd)203 mustek_scsi_pp_wait_for_status_bit_7_set (int fd)
204 {
205   int t;
206   u_char status;
207 
208   DBG (5, "mustek_scsi_pp_wait_for_status_bit_7_set: entering\n");
209 
210   t = mustek_scsi_pp_get_time ();
211   do
212     {
213       if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
214 	{
215 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_7_set: I/O error\n");
216 	  return SANE_STATUS_IO_ERROR;
217 	}
218       if (status & 0x80)
219 	{
220 	  DBG (5,
221 	       "mustek_scsi_pp_wait_for_status_bit_7_set: returning success\n");
222 	  return SANE_STATUS_GOOD;
223 	}
224     }
225   while ((mustek_scsi_pp_get_time () - t) < mustek_scsi_pp_timeout);
226   mustek_scsi_pp_select_register (fd, 0);
227 
228   DBG (2, "mustek_scsi_pp_wait_for_status_bit_7_set: timed out\n");
229   return SANE_STATUS_DEVICE_BUSY;
230 }
231 
232 static SANE_Status
mustek_scsi_pp_wait_for_status_bit_7_clear(int fd)233 mustek_scsi_pp_wait_for_status_bit_7_clear (int fd)
234 {
235   int t;
236   u_char status;
237 
238   DBG (5, "mustek_scsi_pp_wait_for_status_bit_7_clear: entering\n");
239 
240   t = mustek_scsi_pp_get_time ();
241   do
242     {
243       if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
244 	{
245 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_7_clear: I/O error\n");
246 	  return SANE_STATUS_IO_ERROR;
247 	}
248       if (!(status & 0x80))
249 	{
250 	  DBG (5,
251 	       "mustek_scsi_pp_wait_for_status_bit_7_clear: returning success\n");
252 	  return SANE_STATUS_GOOD;
253 	}
254     }
255   while ((mustek_scsi_pp_get_time () - t) < mustek_scsi_pp_timeout);
256   mustek_scsi_pp_select_register (fd, 0);
257 
258   DBG (2, "mustek_scsi_pp_wait_for_status_bit_7_clear: timed out\n");
259   return SANE_STATUS_DEVICE_BUSY;
260 }
261 
262 static SANE_Status
mustek_scsi_pp_wait_for_status_bit_4_set(int fd)263 mustek_scsi_pp_wait_for_status_bit_4_set (int fd)
264 {
265   int t;
266   u_char status;
267 
268   DBG (5, "mustek_scsi_pp_wait_for_status_bit_4_set: entering\n");
269 
270   if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
271     {
272       DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_set: I/O error\n");
273       return SANE_STATUS_IO_ERROR;
274     }
275 
276   if (status & 0x10)
277     {
278       DBG (5,
279 	   "mustek_scsi_pp_wait_for_status_bit_4_set: returning success\n");
280       return SANE_STATUS_GOOD;
281     }
282 
283   t = mustek_scsi_pp_get_time ();
284   do
285     {
286       if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
287 	{
288 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_set: I/O error\n");
289 	  return SANE_STATUS_IO_ERROR;
290 	}
291       if (status & 0x40)
292 	{
293 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_set: bit 6 set\n");
294 	  return SANE_STATUS_IO_ERROR;
295 	}
296       if (status & 0x10)
297 	{
298 	  DBG (5,
299 	       "mustek_scsi_pp_wait_for_status_bit_4_set: returning success\n");
300 	  return SANE_STATUS_GOOD;
301 	}
302     }
303   while ((mustek_scsi_pp_get_time () - t) < mustek_scsi_pp_timeout);
304 
305   DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_set: timed out\n");
306   return SANE_STATUS_DEVICE_BUSY;
307 }
308 
309 static SANE_Status
mustek_scsi_pp_wait_for_status_bit_4_clear(int fd)310 mustek_scsi_pp_wait_for_status_bit_4_clear (int fd)
311 {
312   int t;
313   u_char status;
314 
315   DBG (5, "mustek_scsi_pp_wait_for_status_bit_4_clear: entering\n");
316 
317   if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
318     {
319       DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_clear: I/O error\n");
320       return SANE_STATUS_IO_ERROR;
321     }
322 
323   if (!(status & 0x10))
324     {
325       DBG (5,
326 	   "mustek_scsi_pp_wait_for_status_bit_4_clear: returning success\n");
327       return SANE_STATUS_GOOD;
328     }
329 
330   t = mustek_scsi_pp_get_time ();
331   do
332     {
333       if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
334 	{
335 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_clear: I/O error\n");
336 	  return SANE_STATUS_IO_ERROR;
337 	}
338       if (status & 0x40)
339 	{
340 	  DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_clear: bit 6 set\n");
341 	  return SANE_STATUS_IO_ERROR;
342 	}
343       if (!(status & 0x10))
344 	{
345 	  DBG (5,
346 	       "mustek_scsi_pp_wait_for_status_bit_4_clear: returning success\n");
347 	  return SANE_STATUS_GOOD;
348 	}
349     }
350   while ((mustek_scsi_pp_get_time () - t) < mustek_scsi_pp_timeout);
351 
352   DBG (2, "mustek_scsi_pp_wait_for_status_bit_4_clear: timed out\n");
353   return SANE_STATUS_DEVICE_BUSY;
354 }
355 
356 static u_char mustek_scsi_pp_bit_4_state = 0;
357 
358 static SANE_Status
mustek_scsi_pp_wait_for_status_bit_4_toggle(int fd)359 mustek_scsi_pp_wait_for_status_bit_4_toggle (int fd)
360 {
361   SANE_Status result;
362 
363   DBG (5, "mustek_scsi_pp_wait_for_status_bit_4_toggle: entering\n");
364 
365   mustek_scsi_pp_bit_4_state ^= 0xff;
366   if (mustek_scsi_pp_bit_4_state)
367     {
368       DBG (5,
369 	   "mustek_scsi_pp_wait_for_status_bit_4_toggle: waiting for set\n");
370       result = mustek_scsi_pp_wait_for_status_bit_4_set (fd);
371       mustek_scsi_pp_timeout = 5000;
372     }
373   else
374     {
375       DBG (5,
376 	   "mustek_scsi_pp_wait_for_status_bit_4_toggle: waiting for clear\n");
377       result = mustek_scsi_pp_wait_for_status_bit_4_clear (fd);
378     }
379 
380   return result;
381 }
382 
383 static SANE_Status
mustek_scsi_pp_send_command_byte(int fd, u_char cmd)384 mustek_scsi_pp_send_command_byte (int fd, u_char cmd)
385 {
386   DBG (5, "mustek_scsi_pp_send_command byte: sending 0x%02X\n", cmd);
387 
388   mustek_scsi_pp_select_register (fd, 0);
389 
390   if (mustek_scsi_pp_wait_for_status_bit_7_clear (fd) != SANE_STATUS_GOOD)
391     {
392       mustek_scsi_pp_select_register (fd, 0);
393       return SANE_STATUS_IO_ERROR;
394     }
395 
396   if (sanei_pa4s2_writebyte (fd, mustek_scsi_pp_register, cmd) !=
397       SANE_STATUS_GOOD)
398     {
399       return SANE_STATUS_IO_ERROR;
400     }
401 
402   mustek_scsi_pp_select_register (fd, 1);
403 
404   if (mustek_scsi_pp_wait_for_status_bit_7_set (fd) != SANE_STATUS_GOOD)
405     {
406       mustek_scsi_pp_select_register (fd, 0);
407       return SANE_STATUS_IO_ERROR;
408     }
409   mustek_scsi_pp_select_register (fd, 0);
410 
411   DBG (5, "mustek_scsi_pp_send_command_byte: returning success\n");
412   return SANE_STATUS_GOOD;
413 }
414 
415 static u_char
mustek_scsi_pp_read_response(int fd)416 mustek_scsi_pp_read_response (int fd)
417 {
418   u_char result;
419 
420   DBG (5, "mustek_scsi_pp_read_response: entering\n");
421 
422   if (mustek_scsi_pp_wait_for_status_bit_7_set (fd) != SANE_STATUS_GOOD)
423     {
424       mustek_scsi_pp_select_register (fd, 0);
425       return 0xff;
426     }
427 
428   if (sanei_pa4s2_readbegin (fd, mustek_scsi_pp_register) != SANE_STATUS_GOOD)
429     {
430       return 0xff;
431     }
432   if (sanei_pa4s2_readbyte (fd, &result) != SANE_STATUS_GOOD)
433     {
434       return 0xff;
435     }
436   if (sanei_pa4s2_readend (fd) != SANE_STATUS_GOOD)
437     {
438       return 0xff;
439     }
440 
441   mustek_scsi_pp_select_register (fd, 1);
442   if (mustek_scsi_pp_wait_for_status_bit_7_clear (fd) != SANE_STATUS_GOOD)
443     {
444       result = 0xff;
445     }
446   mustek_scsi_pp_select_register (fd, 0);
447 
448   DBG (5, "mustek_scsi_pp_read_response: returning 0x%02X\n", result);
449   return result;
450 }
451 
452 static SANE_Status
mustek_scsi_pp_check_response(int fd)453 mustek_scsi_pp_check_response (int fd)
454 {
455   if (mustek_scsi_pp_wait_for_status_bit_5_clear (fd) != SANE_STATUS_GOOD)
456     {
457       return SANE_STATUS_IO_ERROR;
458     }
459 
460   if (mustek_scsi_pp_read_response (fd) != 0xA5)
461     {
462       DBG (2, "mustek_scsi_pp_check_response: response!=0xA5\n");
463       return SANE_STATUS_IO_ERROR;
464     }
465 
466   DBG (5, "mustek_scsi_pp_check_response: returning success\n");
467   return SANE_STATUS_GOOD;
468 }
469 
470 static SANE_Status
mustek_scsi_pp_send_command(int fd, const u_char * cmd)471 mustek_scsi_pp_send_command (int fd, const u_char * cmd)
472 {
473   int i;
474   signed char checksum;
475 
476   DBG (5, "mustek_scsi_pp_send_command: sending SCSI command 0x%02X\n",
477        cmd[0]);
478 
479   /* Set timeout depending on command type */
480   switch (cmd[0])
481     {
482     case 0xf:
483     case 0x8:
484       mustek_scsi_pp_timeout = 1000;
485       break;
486     case 0x2:
487       mustek_scsi_pp_timeout = 80;
488       break;
489     case 0x12:
490     case 0x3:
491     case 0x11:
492       mustek_scsi_pp_timeout = 500;
493       break;
494     default:
495       mustek_scsi_pp_timeout = 1000;
496       break;
497     }
498 
499   if (mustek_scsi_pp_wait_for_status_bit_5_set (fd) != SANE_STATUS_GOOD)
500     {
501       DBG (2,
502 	   "mustek_scsi_pp_send_command: timed out waiting for bit 5 to set\n");
503       return SANE_STATUS_DEVICE_BUSY;
504     }
505 
506   checksum = 0;
507   for (i = 0; i < 6; i++)
508     {
509       if (mustek_scsi_pp_send_command_byte (fd, cmd[i]) != SANE_STATUS_GOOD)
510 	{
511 	  DBG (2,
512 	       "mustek_scsi_pp_send_command: error sending byte %d (0x%02X)\n",
513 	       i, cmd[i]);
514 	  return SANE_STATUS_IO_ERROR;
515 	}
516       checksum += cmd[i];
517     }
518   if (mustek_scsi_pp_send_command_byte (fd, -checksum) != SANE_STATUS_GOOD)
519     {
520       DBG (2,
521 	   "mustek_scsi_pp_send_command: error sending checksum (0x%02X)\n",
522 	   -checksum);
523       return SANE_STATUS_IO_ERROR;
524     }
525   return mustek_scsi_pp_check_response (fd);
526 }
527 
528 static SANE_Status
mustek_scsi_pp_send_data_block(int fd, const u_char * data, int len)529 mustek_scsi_pp_send_data_block (int fd, const u_char * data, int len)
530 {
531   int i;
532   signed char checksum;
533 
534   DBG (5, "mustek_scsi_pp_send_data_block: sending block of length %d\n",
535        len);
536 
537   if (mustek_scsi_pp_wait_for_status_bit_5_set (fd) != SANE_STATUS_GOOD)
538     {
539       DBG (2,
540 	   "mustek_scsi_pp_send_data_block: timed out waiting for bit 5 to set\n");
541       return SANE_STATUS_DEVICE_BUSY;
542     }
543 
544   checksum = 0;
545   for (i = 0; i < len; i++)
546     {
547       if (mustek_scsi_pp_send_command_byte (fd, data[i]) != SANE_STATUS_GOOD)
548 	{
549 	  DBG (2,
550 	       "mustek_scsi_pp_send_data_block: error sending byte %d (0x%02X)\n",
551 	       i, data[i]);
552 	  return SANE_STATUS_IO_ERROR;
553 	}
554       checksum += data[i];
555     }
556   if (mustek_scsi_pp_send_command_byte (fd, -checksum) != SANE_STATUS_GOOD)
557     {
558       DBG (2,
559 	   "mustek_scsi_pp_send_data_block: error sending checksum (0x%02X)\n",
560 	   -checksum);
561       return SANE_STATUS_IO_ERROR;
562     }
563   return mustek_scsi_pp_check_response (fd);
564 }
565 
566 static SANE_Status
mustek_scsi_pp_read_data_block(int fd, u_char * buffer, int len)567 mustek_scsi_pp_read_data_block (int fd, u_char * buffer, int len)
568 {
569   int i;
570   signed char checksum;
571 
572   DBG (5, "mustek_scsi_pp_read_data_block: reading block of length %d\n",
573        len);
574 
575   if (mustek_scsi_pp_wait_for_status_bit_5_clear (fd) != SANE_STATUS_GOOD)
576     {
577       DBG (2,
578 	   "mustek_scsi_pp_read_data_block: timed out waiting for bit 5 to clear\n");
579       return SANE_STATUS_DEVICE_BUSY;
580     }
581 
582   checksum = 0;
583   for (i = 0; i < len; i++)
584     {
585       buffer[i] = mustek_scsi_pp_read_response (fd);
586       checksum += buffer[i];
587     }
588   if ((signed char) mustek_scsi_pp_read_response (fd) != (-checksum))
589     {
590       mustek_scsi_pp_send_command_byte (fd, 0xff);
591       DBG (2, "mustek_scsi_pp_read_data_block: checksums do not match\n");
592       return SANE_STATUS_IO_ERROR;
593     }
594   if (mustek_scsi_pp_wait_for_status_bit_5_set (fd) != SANE_STATUS_GOOD)
595     {
596       DBG (2,
597 	   "mustek_scsi_pp_read_data_block: error waiting for bit 5 to set\n");
598       return SANE_STATUS_IO_ERROR;
599     }
600   if (mustek_scsi_pp_send_command_byte (fd, 0) != SANE_STATUS_GOOD)
601     {
602       mustek_scsi_pp_send_command_byte (fd, 0xff);
603       DBG (2, "mustek_scsi_pp_read_data_block: error sending final 0 byte\n");
604       return SANE_STATUS_IO_ERROR;
605     }
606 
607   DBG (5, "mustek_scsi_pp_read_data_block: returning success\n");
608   return SANE_STATUS_GOOD;
609 }
610 
611 
612 
613 /*
614  * Externally visible functions
615  */
616 SANE_Status
mustek_scsi_pp_open(const char *dev, int *fd)617 mustek_scsi_pp_open (const char *dev, int *fd)
618 {
619   SANE_Status status;
620 
621   status = sanei_pa4s2_scsi_pp_open (dev, fd);
622   if (status == SANE_STATUS_GOOD)
623     {
624       DBG (5, "mustek_scsi_pp_open: device %s opened as fd %d\n", dev, *fd);
625     }
626   else
627     {
628       DBG (2, "mustek_scsi_pp_open: error opening device %s\n", dev);
629     }
630   return status;
631 }
632 
633 static void
mustek_scsi_pp_close(int fd)634 mustek_scsi_pp_close (int fd)
635 {
636   DBG (5, "mustek_scsi_pp_close: closing fd %d\n", fd);
637   sanei_pa4s2_close (fd);
638 }
639 
640 static void
mustek_scsi_pp_exit(void)641 mustek_scsi_pp_exit (void)
642 {
643   DBG (5, "mustek_scsi_pp_exit: entering\n");
644 }
645 
646 static SANE_Status
mustek_scsi_pp_test_ready(int fd)647 mustek_scsi_pp_test_ready (int fd)
648 {
649   u_char status;
650   SANE_Status retval;
651 
652   DBG (5, "mustek_scsi_pp_test_ready: entering with fd=%d\n", fd);
653 
654   if (sanei_pa4s2_enable (fd, SANE_TRUE) != SANE_STATUS_GOOD)
655     {
656       DBG (2, "mustek_scsi_pp_test_ready: error enabling scanner\n");
657       return SANE_STATUS_IO_ERROR;
658     }
659 
660   if (sanei_pa4s2_scsi_pp_get_status (fd, &status) != SANE_STATUS_GOOD)
661     {
662       DBG (2, "mustek_scsi_pp_test_ready: error getting status\n");
663       sanei_pa4s2_enable (fd, SANE_FALSE);
664       return SANE_STATUS_INVAL;
665     }
666 
667   retval = SANE_STATUS_GOOD;
668 
669   status &= 0xf0;
670 
671   if (status == 0xf0)
672     {
673       retval = SANE_STATUS_DEVICE_BUSY;
674     }
675   if (status & 0x40)
676     {
677       retval = SANE_STATUS_DEVICE_BUSY;
678     }
679   if (!(status & 0x20))
680     {
681       retval = SANE_STATUS_DEVICE_BUSY;
682     }
683 
684   if (sanei_pa4s2_enable (fd, SANE_FALSE) != SANE_STATUS_GOOD)
685     {
686       DBG (2, "mustek_scsi_pp_test_ready: error disabling scanner\n");
687       return SANE_STATUS_IO_ERROR;
688     }
689 
690   if (retval == SANE_STATUS_GOOD)
691     {
692       DBG (5, "mustek_scsi_pp_test_ready: returning SANE_STATUS_GOOD\n");
693     }
694   else
695     {
696       DBG (5,
697 	   "mustek_scsi_pp_test_ready: returning SANE_STATUS_DEVICE_BUSY\n");
698     }
699 
700   return retval;
701 }
702 
703 static SANE_Status
mustek_scsi_pp_cmd(int fd, const void *src, size_t src_size, void *dst, size_t * dst_size)704 mustek_scsi_pp_cmd (int fd, const void *src, size_t src_size,
705 		    void *dst, size_t * dst_size)
706 {
707   SANE_Status stat;
708   int num_tries = 0;
709   static u_char scan_options = 0;
710   const u_char *cmd;
711   u_char stop_cmd[6] = { 0x1b, 0, 0, 0, 0, 0 };
712   int max_tries;
713 
714   max_tries = MUSTEK_SCSI_PP_NUM_RETRIES;
715 
716   cmd = (const u_char *) src;
717 
718   DBG (5, "mustek_scsi_pp_cmd: sending command 0x%02X to device %d\n",
719        cmd[0], fd);
720 
721   if (sanei_pa4s2_enable (fd, SANE_TRUE) != SANE_STATUS_GOOD)
722     {
723       DBG (2, "mustek_scsi_pp_cmd: error enabling scanner\n");
724       return SANE_STATUS_IO_ERROR;
725     }
726 
727   if (cmd[0] == 0x1b)
728     {
729       if (!(cmd[4] & 0x1))
730 	{
731 	  unsigned char c;
732 	  int i;
733 
734 	  DBG (5, "mustek_scsi_pp_cmd: doing stop-specific stuff\n");
735 
736 	  /*
737 	   * Remembers what flags were sent with a 'start' command, and
738 	   * replicate them with a stop command.
739 	   */
740 	  stop_cmd[4] = scan_options & 0xfe;
741 	  cmd = &stop_cmd[0];
742 
743 	  /*
744 	   * In color mode at least, the scanner doesn't seem to like stopping at
745 	   * the end. It's a bit of a horrible hack, but reading loads of bytes and
746 	   * allowing 20 tries for the stop command is the only way I've found that
747 	   * solves the problem.
748 	   */
749 	  max_tries = 20;
750 
751 	  if (sanei_pa4s2_readbegin (fd, mustek_scsi_pp_register) !=
752 	      SANE_STATUS_GOOD)
753 	    {
754 	      DBG (2, "mustek_scsi_pp_cmd: error in readbegin for stop\n");
755 	    }
756 
757 	  for (i = 0; i < 10000; i++)
758 	    {
759 	      if (sanei_pa4s2_readbyte (fd, &c) != SANE_STATUS_GOOD)
760 		{
761 		  DBG (2,
762 		       "mustek_scsi_pp_cmd: error reading byte for stop\n");
763 		  break;
764 		}
765 	      DBG (5, "mustek_scsi_pp_cmd: successfully read byte %d\n", i);
766 	    }
767 	  if (sanei_pa4s2_readend (fd) != SANE_STATUS_GOOD)
768 	    {
769 	      DBG (2, "mustek_scsi_pp_cmd: error in readend for stop\n");
770 	    }
771 	}
772     }
773 
774   if (cmd[0] == 0x08)
775     {
776       DBG (5, "mustek_scsi_pp_cmd: doing read-specific stuff\n");
777       mustek_scsi_pp_timeout = 30000;
778       mustek_scsi_pp_bit_4_state = 0xff;
779     }
780 
781   /*
782    * Send the command itself in one block, then any extra input data in a second
783    * block. Not sure if that's necessary.
784    */
785   if (src_size < 6)
786     {
787       sanei_pa4s2_enable (fd, SANE_FALSE);
788       DBG (2, "mustek_scsi_pp_cmd: source size is only %lu (<6)\n", (u_long) src_size);
789       return SANE_STATUS_INVAL;
790     }
791 
792   /*
793    * Retry the command several times, as occasionally it doesn't
794    * work first time.
795    */
796   do
797     {
798       stat = mustek_scsi_pp_send_command (fd, cmd);
799       num_tries++;
800     }
801   while ((stat != SANE_STATUS_GOOD) && (num_tries < max_tries));
802 
803   if (stat != SANE_STATUS_GOOD)
804     {
805       sanei_pa4s2_enable (fd, SANE_FALSE);
806       DBG (2, "mustek_scsi_pp_cmd: sending command failed\n");
807       return stat;
808     }
809 
810   if (src_size > 6)
811     {
812       DBG (5, "mustek_scsi_pp_cmd: sending data block of length %lu\n",
813 	   (u_long) (src_size - 6));
814 
815       stat =
816 	mustek_scsi_pp_send_data_block (fd, ((const u_char *) src) + 6,
817 					src_size - 6);
818       if (stat != SANE_STATUS_GOOD)
819 	{
820 	  sanei_pa4s2_enable (fd, SANE_FALSE);
821 	  DBG (2, "mustek_scsi_pp_cmd: sending data block failed\n");
822 	  return stat;
823 	}
824     }
825 
826 
827   if (dst)
828     {
829       unsigned int length;
830 
831       /* check buffer is big enough to receive data */
832       length = (cmd[3] << 8) | cmd[4];
833 
834       DBG (5, "mustek_scsi_pp_cmd: reading %d bytes\n", length);
835 
836       if (length > *dst_size)
837 	{
838 	  sanei_pa4s2_enable (fd, SANE_FALSE);
839 	  DBG (2,
840 	       "mustek_scsi_pp_cmd: buffer (size %lu) not big enough for data (size %d)\n",
841 	       (u_long) *dst_size, length);
842 	  return SANE_STATUS_INVAL;
843 	}
844 
845       stat = mustek_scsi_pp_read_data_block (fd, dst, length);
846       if (stat != SANE_STATUS_GOOD)
847 	{
848 	  DBG (2, "mustek_scsi_pp_cmd: error reading data block\n");
849 	}
850     }
851 
852   if (cmd[0] == 0x1b)
853     {
854       if (cmd[4] & 0x1)
855 	{
856 	  DBG (5, "mustek_scsi_pp_cmd: doing start-specific stuff\n");
857 
858 	  scan_options = cmd[4];
859 
860 	  /* 'Start' command - wait for valid status */
861 	  mustek_scsi_pp_timeout = 70000;
862 	  stat = mustek_scsi_pp_wait_for_valid_status (fd);
863 
864 	  if (stat != SANE_STATUS_GOOD)
865 	    {
866 	      DBG (2,
867 		   "mustek_scsi_pp_cmd: error waiting for valid status after start\n");
868 	    }
869 	}
870     }
871 
872   if (sanei_pa4s2_enable (fd, SANE_FALSE) != SANE_STATUS_GOOD)
873     {
874       DBG (2, "mustek_scsi_pp_cmd: error disabling scanner\n");
875       return SANE_STATUS_IO_ERROR;
876     }
877 
878   if (stat == SANE_STATUS_GOOD)
879     {
880       DBG (5, "mustek_scsi_pp_cmd: returning success\n");
881     }
882 
883   return stat;
884 }
885 
886 static SANE_Status
mustek_scsi_pp_rdata(int fd, int planes, SANE_Byte * buf, int lines, int bpl)887 mustek_scsi_pp_rdata (int fd, int planes, SANE_Byte * buf, int lines, int bpl)
888 {
889   int i, j;
890 
891   DBG (5,
892        "mustek_scsi_pp_rdata: reading %d lines at %d bpl, %d planes from %d\n",
893        lines, bpl, planes, fd);
894 
895   if ((planes != 1) && (planes != 3))
896     {
897       DBG (2, "mustek_scsi_pp_rdata: invalid number of planes (%d)\n",
898 	   planes);
899       return SANE_STATUS_INVAL;
900     }
901 
902   if (sanei_pa4s2_enable (fd, SANE_TRUE) != SANE_STATUS_GOOD)
903     {
904       DBG (2, "mustek_scsi_pp_rdata: error enabling scanner\n");
905       return SANE_STATUS_IO_ERROR;
906     }
907 
908   for (i = 0; i < lines; i++)
909     {
910       if (planes == 3)
911 	{
912 	  if (mustek_scsi_pp_wait_for_status_bit_4_toggle (fd) !=
913 	      SANE_STATUS_GOOD)
914 	    {
915 	      sanei_pa4s2_enable (fd, SANE_FALSE);
916 	      DBG (2,
917 		   "mustek_scsi_pp_rdata: error waiting for bit 4 toggle for red, line %d\n",
918 		   i);
919 	      return SANE_STATUS_IO_ERROR;
920 	    }
921 	  if (sanei_pa4s2_readbegin (fd, mustek_scsi_pp_register) !=
922 	      SANE_STATUS_GOOD)
923 	    {
924 	      sanei_pa4s2_enable (fd, SANE_FALSE);
925 	      DBG (2,
926 		   "mustek_scsi_pp_rdata: error in readbegin for red, line %d\n",
927 		   i);
928 	      return SANE_STATUS_IO_ERROR;
929 	    }
930 	  for (j = 0; j < (bpl / 3); j++)
931 	    {
932 	      if (sanei_pa4s2_readbyte (fd, &buf[j]) != SANE_STATUS_GOOD)
933 		{
934 		  sanei_pa4s2_readend (fd);
935 		  sanei_pa4s2_enable (fd, SANE_FALSE);
936 		  DBG (2,
937 		       "mustek_scsi_pp_rdata: error reading red byte, line %d, byte %d\n",
938 		       i, j);
939 		  return SANE_STATUS_IO_ERROR;
940 		}
941 	    }
942 	  if (sanei_pa4s2_readend (fd) != SANE_STATUS_GOOD)
943 	    {
944 	      sanei_pa4s2_enable (fd, SANE_FALSE);
945 	      DBG (2,
946 		   "mustek_scsi_pp_rdata: error in readend for red, line %d\n",
947 		   i);
948 	      return SANE_STATUS_IO_ERROR;
949 	    }
950 
951 
952 	  if (mustek_scsi_pp_wait_for_status_bit_4_toggle (fd) !=
953 	      SANE_STATUS_GOOD)
954 	    {
955 	      sanei_pa4s2_enable (fd, SANE_FALSE);
956 	      DBG (2,
957 		   "mustek_scsi_pp_rdata: error waiting for bit 4 toggle for green, line %d\n",
958 		   i);
959 	      return SANE_STATUS_IO_ERROR;
960 	    }
961 	  if (sanei_pa4s2_readbegin (fd, mustek_scsi_pp_register) !=
962 	      SANE_STATUS_GOOD)
963 	    {
964 	      sanei_pa4s2_enable (fd, SANE_FALSE);
965 	      DBG (2,
966 		   "mustek_scsi_pp_rdata: error in readbegin for green, line %d\n",
967 		   i);
968 	      return SANE_STATUS_IO_ERROR;
969 	    }
970 	  for (j = 0; j < (bpl / 3); j++)
971 	    {
972 	      if (sanei_pa4s2_readbyte (fd, &buf[j + (bpl / 3)]) !=
973 		  SANE_STATUS_GOOD)
974 		{
975 		  sanei_pa4s2_readend (fd);
976 		  sanei_pa4s2_enable (fd, SANE_FALSE);
977 		  DBG (2,
978 		       "mustek_scsi_pp_rdata: error reading green byte, line %d, byte %d\n",
979 		       i, j);
980 		  return SANE_STATUS_IO_ERROR;
981 		}
982 	    }
983 	  if (sanei_pa4s2_readend (fd) != SANE_STATUS_GOOD)
984 	    {
985 	      sanei_pa4s2_enable (fd, SANE_FALSE);
986 	      DBG (2,
987 		   "mustek_scsi_pp_rdata: error in readend for green, line %d\n",
988 		   i);
989 	      return SANE_STATUS_IO_ERROR;
990 	    }
991 
992 
993 	  if (mustek_scsi_pp_wait_for_status_bit_4_toggle (fd) !=
994 	      SANE_STATUS_GOOD)
995 	    {
996 	      sanei_pa4s2_enable (fd, SANE_FALSE);
997 	      DBG (2,
998 		   "mustek_scsi_pp_rdata: error waiting for bit 4 toggle for blue, line %d\n",
999 		   i);
1000 	      return SANE_STATUS_IO_ERROR;
1001 	    }
1002 	  if (sanei_pa4s2_readbegin (fd, mustek_scsi_pp_register) !=
1003 	      SANE_STATUS_GOOD)
1004 	    {
1005 	      sanei_pa4s2_enable (fd, SANE_FALSE);
1006 	      DBG (2,
1007 		   "mustek_scsi_pp_rdata: error in readbegin for blue, line %d\n",
1008 		   i);
1009 	      return SANE_STATUS_IO_ERROR;
1010 	    }
1011 	  for (j = 0; j < (bpl / 3); j++)
1012 	    {
1013 	      if (sanei_pa4s2_readbyte (fd, &buf[j + (2 * (bpl / 3))]) !=
1014 		  SANE_STATUS_GOOD)
1015 		{
1016 		  sanei_pa4s2_readend (fd);
1017 		  sanei_pa4s2_enable (fd, SANE_FALSE);
1018 		  DBG (2,
1019 		       "mustek_scsi_pp_rdata: error reading blue byte, line %d, byte %d\n",
1020 		       i, j);
1021 		  return SANE_STATUS_IO_ERROR;
1022 		}
1023 	    }
1024 	  if (sanei_pa4s2_readend (fd) != SANE_STATUS_GOOD)
1025 	    {
1026 	      sanei_pa4s2_enable (fd, SANE_FALSE);
1027 	      DBG (2,
1028 		   "mustek_scsi_pp_rdata: error in readend for blue, line %d\n",
1029 		   i);
1030 	      return SANE_STATUS_IO_ERROR;
1031 	    }
1032 	}
1033       else
1034 	{
1035 	  if (mustek_scsi_pp_wait_for_status_bit_4_toggle (fd) !=
1036 	      SANE_STATUS_GOOD)
1037 	    {
1038 	      sanei_pa4s2_enable (fd, SANE_FALSE);
1039 	      DBG (2,
1040 		   "mustek_scsi_pp_rdata: error waiting for bit 4 toggle, line %d\n",
1041 		   i);
1042 	      return SANE_STATUS_IO_ERROR;
1043 	    }
1044 	  if (sanei_pa4s2_readbegin (fd, mustek_scsi_pp_register) !=
1045 	      SANE_STATUS_GOOD)
1046 	    {
1047 	      sanei_pa4s2_enable (fd, SANE_FALSE);
1048 	      DBG (2, "mustek_scsi_pp_rdata: error in readbegin, line %d\n",
1049 		   i);
1050 	      return SANE_STATUS_IO_ERROR;
1051 	    }
1052 
1053 	  for (j = 0; j < bpl; j++)
1054 	    {
1055 	      if (sanei_pa4s2_readbyte (fd, &buf[j]) != SANE_STATUS_GOOD)
1056 		{
1057 		  sanei_pa4s2_readend (fd);
1058 		  sanei_pa4s2_enable (fd, SANE_FALSE);
1059 		  DBG (2,
1060 		       "mustek_scsi_pp_rdata: error reading byte, line %d, byte %d\n",
1061 		       i, j);
1062 		  return SANE_STATUS_IO_ERROR;
1063 		}
1064 	    }
1065 
1066 	  if (sanei_pa4s2_readend (fd) != SANE_STATUS_GOOD)
1067 	    {
1068 	      sanei_pa4s2_enable (fd, SANE_FALSE);
1069 	      DBG (2, "mustek_scsi_pp_rdata: error in readend, line %d\n", i);
1070 	      return SANE_STATUS_IO_ERROR;
1071 	    }
1072 	}
1073       buf += bpl;
1074     }
1075 
1076   if (sanei_pa4s2_enable (fd, SANE_FALSE) != SANE_STATUS_GOOD)
1077     {
1078       DBG (2, "mustek_scsi_pp_rdata: error enabling scanner\n");
1079       return SANE_STATUS_IO_ERROR;
1080     }
1081 
1082   DBG (5, "mustek_scsi_pp_rdata: returning success\n");
1083   return SANE_STATUS_GOOD;
1084 }
1085