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