summaryrefslogtreecommitdiff
path: root/backend/kodak.c
blob: 39c91e6d0b1011a6840c256b41dca681e42c0d56 (plain)
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
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
/* sane - Scanner Access Now Easy.

   This file is part of the SANE package, and implements a SANE backend
   for various large Kodak scanners.

   Copyright (C) 2008-2010 m. allan noah

   --------------------------------------------------------------------------

   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.

   --------------------------------------------------------------------------

   The source code is divided in sections which you can easily find by
   searching for the tag "@@".

   Section 1 - Init & static stuff
   Section 2 - sane_init, _get_devices, _open & friends
   Section 3 - sane_*_option functions
   Section 4 - sane_start, _get_param, _read & friends
   Section 5 - sane_close functions
   Section 6 - misc functions

   Changes:
      v0 through v5 2008-01-15, MAN
         - development versions
      v6 2009-06-22, MAN
         - improved set_window() to build descriptor from scratch
         - initial release
      v7 2010-02-10, MAN
         - add SANE_I18N to static strings
         - don't fail if scsi buffer is too small

   SANE FLOW DIAGRAM

   - sane_init() : initialize backend
   . - sane_get_devices() : query list of scanner devices
   . - sane_open() : open a particular scanner device
   . . - sane_set_io_mode : set blocking mode
   . . - sane_get_select_fd : get scanner fd
   . .
   . . - sane_get_option_descriptor() : get option information
   . . - sane_control_option() : change option values
   . . - sane_get_parameters() : returns estimated scan parameters
   . . - (repeat previous 3 functions)
   . .
   . . - sane_start() : start image acquisition
   . .   - sane_get_parameters() : returns actual scan parameters
   . .   - sane_read() : read image data (from pipe)
   . . (sane_read called multiple times; after sane_read returns EOF,
   . . loop may continue with sane_start which may return a 2nd page
   . . when doing duplex scans, or load the next page from the ADF)
   . .
   . . - sane_cancel() : cancel operation
   . - sane_close() : close opened scanner device
   - sane_exit() : terminate use of backend

*/

/*
 * @@ Section 1 - Init
 */

#include "sane/config.h"

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>

#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_LIBC_H
# include <libc.h>              /* NeXTStep/OpenStep */
#endif

#include "../include/sane/sanei_backend.h"
#include "../include/sane/sanei_scsi.h"
#include "../include/sane/saneopts.h"
#include "../include/sane/sanei_config.h"

#include "kodak-cmd.h"
#include "kodak.h"

#define DEBUG 1
#define BUILD 7

/* values for SANE_DEBUG_KODAK env var:
 - errors           5
 - function trace  10
 - function detail 15
 - get/setopt cmds 20
 - scsi cmd trace  25
 - scsi cmd detail 30
 - useless noise   35
*/

/* ------------------------------------------------------------------------- */
#define STRING_ADFFRONT SANE_I18N("ADF Front")
#define STRING_ADFBACK SANE_I18N("ADF Back")
#define STRING_ADFDUPLEX SANE_I18N("ADF Duplex")

#define STRING_LINEART SANE_VALUE_SCAN_MODE_LINEART
#define STRING_HALFTONE SANE_VALUE_SCAN_MODE_HALFTONE
#define STRING_GRAYSCALE SANE_VALUE_SCAN_MODE_GRAY
#define STRING_COLOR SANE_VALUE_SCAN_MODE_COLOR

/* Also set via config file. */
static int global_buffer_size = DEFAULT_BUFFER_SIZE;

/*
 * used by attach* and sane_get_devices
 * a ptr to a null term array of ptrs to SANE_Device structs
 * a ptr to a single-linked list of scanner structs
 */
static const SANE_Device **sane_devArray = NULL;
static struct scanner *scanner_devList = NULL;

/*
 * @@ Section 2 - SANE & scanner init code
 */

/*
 * Called by SANE initially.
 *
 * From the SANE spec:
 * This function must be called before any other SANE function can be
 * called. The behavior of a SANE backend is undefined if this
 * function is not called first. The version code of the backend is
 * returned in the value pointed to by version_code. If that pointer
 * is NULL, no version code is returned. Argument authorize is either
 * a pointer to a function that is invoked when the backend requires
 * authentication for a specific resource or NULL if the frontend does
 * not support authentication.
 */
SANE_Status
sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
{
  authorize = authorize;        /* get rid of compiler warning */

  DBG_INIT ();
  DBG (10, "sane_init: start\n");

  if (version_code)
    *version_code = SANE_VERSION_CODE (V_MAJOR, V_MINOR, BUILD);

  DBG (5, "sane_init: kodak backend %d.%d.%d, from %s\n",
    V_MAJOR, V_MINOR, BUILD, PACKAGE_STRING);

  DBG (10, "sane_init: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * Called by SANE to find out about supported devices.
 *
 * From the SANE spec:
 * This function can be used to query the list of devices that are
 * available. If the function executes successfully, it stores a
 * pointer to a NULL terminated array of pointers to SANE_Device
 * structures in *device_list. The returned list is guaranteed to
 * remain unchanged and valid until (a) another call to this function
 * is performed or (b) a call to sane_exit() is performed. This
 * function can be called repeatedly to detect when new devices become
 * available. If argument local_only is true, only local devices are
 * returned (devices directly attached to the machine that SANE is
 * running on). If it is false, the device list includes all remote
 * devices that are accessible to the SANE library.
 *
 * SANE does not require that this function is called before a
 * sane_open() call is performed. A device name may be specified
 * explicitly by a user which would make it unnecessary and
 * undesirable to call this function first.
 */
/* Read the config file, find scanners with help from sanei_*
 * store in two global lists of device structs
 */
SANE_Status
sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
{
  struct scanner *dev;
  char line[PATH_MAX];
  const char *lp;
  FILE *fp;
  int num_devices=0;
  int i=0;

  local_only = local_only;        /* get rid of compiler warning */

  DBG (10, "sane_get_devices: start\n");

  /* set this to default before reading the file */
  global_buffer_size = DEFAULT_BUFFER_SIZE;

  fp = sanei_config_open (KODAK_CONFIG_FILE);

  if (fp) {

      DBG (15, "sane_get_devices: reading config file %s\n", KODAK_CONFIG_FILE);

      while (sanei_config_read (line, PATH_MAX, fp)) {

          lp = line;

          /* ignore comments */
          if (*lp == '#')
            continue;

          /* skip empty lines */
          if (*lp == 0)
            continue;

          if ((strncmp ("option", lp, 6) == 0) && isspace (lp[6])) {

              lp += 6;
              lp = sanei_config_skip_whitespace (lp);

              /* we allow setting buffersize too big */
              if ((strncmp (lp, "buffer-size", 11) == 0) && isspace (lp[11])) {

                  int buf;
                  lp += 11;
                  lp = sanei_config_skip_whitespace (lp);
                  buf = atoi (lp);

                  if (buf < 4096) {
                    DBG (5, "sane_get_devices: config option \"buffer-size\" \
                      (%d) is < 4096, ignoring!\n", buf);
                    continue;
                  }

                  if (buf > DEFAULT_BUFFER_SIZE) {
                    DBG (5, "sane_get_devices: config option \"buffer-size\" \
                      (%d) is > %d, warning!\n", buf, DEFAULT_BUFFER_SIZE);
                  }

                  DBG (15, "sane_get_devices: setting \"buffer-size\" to %d\n",
                    buf);
                  global_buffer_size = buf;
              }
              else {
                  DBG (5, "sane_get_devices: config option \"%s\" \
                    unrecognized\n", lp);
              }
          }
          else if ((strncmp ("scsi", lp, 4) == 0) && isspace (lp[4])) {
              DBG (15, "sane_get_devices: looking for '%s'\n", lp);
              sanei_config_attach_matching_devices (lp, attach_one);
          }
          else{
              DBG (5, "sane_get_devices: config line \"%s\" unrecognized\n",
                lp);
          }
      }
      fclose (fp);
  }

  else {
      DBG (5, "sane_get_devices: no config file '%s', using defaults\n",
        KODAK_CONFIG_FILE);
      DBG (15, "sane_get_devices: looking for 'scsi KODAK'\n");
      sanei_config_attach_matching_devices ("scsi KODAK", attach_one);
  }

  for (dev = scanner_devList; dev; dev=dev->next) {
    DBG (15, "sane_get_devices: found scanner %s\n",dev->device_name);
    num_devices++;
  }

  DBG (15, "sane_get_devices: found %d scanner(s)\n",num_devices);

  sane_devArray = calloc (num_devices + 1, sizeof (SANE_Device*));
  if (!sane_devArray)
    return SANE_STATUS_NO_MEM;

  for (dev = scanner_devList; dev; dev=dev->next) {
    sane_devArray[i++] = (SANE_Device *)&dev->sane;
  }

  sane_devArray[i] = 0;

  if(device_list){
    *device_list = sane_devArray;
  }

  DBG (10, "sane_get_devices: finish\n");

  return SANE_STATUS_GOOD;
}

/* build the scanner struct and link to global list
 * unless struct is already loaded, then pretend
 */
static SANE_Status
attach_one (const char *device_name)
{
  struct scanner *s;
  int ret;

  DBG (10, "attach_one: start\n");
  DBG (15, "attach_one: looking for '%s'\n", device_name);

  for (s = scanner_devList; s; s = s->next) {
    if (strcmp (s->sane.name, device_name) == 0) {
      DBG (10, "attach_one: already attached!\n");
      return SANE_STATUS_GOOD;
    }
  }

  /* build a struct to hold it */
  if ((s = calloc (sizeof (*s), 1)) == NULL)
    return SANE_STATUS_NO_MEM;

  /* scsi command/data buffer */
  s->buffer_size = global_buffer_size;

  /* copy the device name */
  s->device_name = strdup (device_name);
  if (!s->device_name){
    free (s);
    return SANE_STATUS_NO_MEM;
  }

  /* connect the fd */
  s->fd = -1;
  ret = connect_fd(s);
  if(ret != SANE_STATUS_GOOD){
    free (s->device_name);
    free (s);
    return ret;
  }

  /* Now query the device to load its vendor/model/version */
  ret = init_inquire (s);
  if (ret != SANE_STATUS_GOOD) {
    disconnect_fd(s);
    free (s->device_name);
    free (s);
    DBG (5, "attach_one: inquiry failed\n");
    return ret;
  }

  /* clean up the scanner struct based on model */
  /* this is the only piece of model specific code */
  ret = init_model (s);
  if (ret != SANE_STATUS_GOOD) {
    disconnect_fd(s);
    free (s->device_name);
    free (s);
    DBG (5, "attach_one: model failed\n");
    return ret;
  }

  /* sets user 'values' to good defaults */
  ret = init_user (s);
  if (ret != SANE_STATUS_GOOD) {
    disconnect_fd(s);
    free (s->device_name);
    free (s);
    DBG (5, "attach_one: user failed\n");
    return ret;
  }

  /* sets SANE option 'values' to good defaults */
  ret = init_options (s);
  if (ret != SANE_STATUS_GOOD) {
    disconnect_fd(s);
    free (s->device_name);
    free (s);
    DBG (5, "attach_one: options failed\n");
    return ret;
  }

  /* we close the connection, so that another backend can talk to scanner */
  disconnect_fd(s);

  /* load info into sane_device struct */
  s->sane.name = s->device_name;
  s->sane.vendor = s->vendor_name;
  s->sane.model = s->product_name;
  s->sane.type = "scanner";

  s->next = scanner_devList;
  scanner_devList = s;

  DBG (10, "attach_one: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * connect the fd in the scanner struct
 */
static SANE_Status
connect_fd (struct scanner *s)
{
  SANE_Status ret = SANE_STATUS_GOOD;
  int buffer_size = s->buffer_size;

  DBG (10, "connect_fd: start\n");

  if(s->fd > -1){
    DBG (5, "connect_fd: already open\n");
    ret = SANE_STATUS_GOOD;
  }
  else {
    ret = sanei_scsi_open_extended (s->device_name, &(s->fd), sense_handler,
      s, &s->buffer_size);
    if(!ret && buffer_size != s->buffer_size){
      DBG (5, "connect_fd: cannot get requested buffer size (%d/%d)\n",
        buffer_size, s->buffer_size);
    }
    else{
      DBG (15, "connect_fd: opened SCSI device\n");
    }
  }

  DBG (10, "connect_fd: finish %d\n", ret);

  return ret;
}

/*
 * This routine will check if a certain device is a Kodak scanner
 * It also copies interesting data from INQUIRY into the handle structure
 */
static SANE_Status
init_inquire (struct scanner *s)
{
  int i;
  SANE_Status ret;

  unsigned char cmd[INQUIRY_len];
  size_t cmdLen = INQUIRY_len;

  unsigned char in[I_data_len];
  size_t inLen = I_data_len;

  DBG (10, "init_inquire: start\n");

  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd, INQUIRY_code);
  set_I_evpd (cmd, 0);
  set_I_page_code (cmd, I_page_code_default);
  set_I_data_length (cmd, inLen);

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    NULL, 0,
    in, &inLen
  );

  if (ret != SANE_STATUS_GOOD){
    return ret;
  }

  if (get_I_periph_qual(in) != I_periph_qual_valid){
    DBG (5, "The device at '%s' has invalid periph_qual.\n", s->device_name);
    return SANE_STATUS_INVAL;
  }

  if (get_I_periph_devtype(in) != I_periph_devtype_scanner){
    DBG (5, "The device at '%s' is not a scanner.\n", s->device_name);
    return SANE_STATUS_INVAL;
  }

  get_I_vendor (in, s->vendor_name);
  get_I_product (in, s->product_name);
  get_I_version (in, s->version_name);
  get_I_build (in, s->build_name);

  s->vendor_name[8] = 0;
  s->product_name[16] = 0;
  s->version_name[4] = 0;
  s->build_name[2] = 0;

  /* gobble trailing spaces */
  for (i = 7; s->vendor_name[i] == ' ' && i >= 0; i--)
    s->vendor_name[i] = 0;
  for (i = 15; s->product_name[i] == ' ' && i >= 0; i--)
    s->product_name[i] = 0;
  for (i = 3; s->version_name[i] == ' ' && i >= 0; i--)
    s->version_name[i] = 0;
  for (i = 2; s->build_name[i] == ' ' && i >= 0; i--)
    s->build_name[i] = 0;

  if (strcmp ("KODAK", s->vendor_name)) {
    DBG (5, "The device at '%s' is reported to be made by '%s'\n", s->device_name, s->vendor_name);
    DBG (5, "This backend only supports Kodak products.\n");
    return SANE_STATUS_INVAL;
  }

  DBG (15, "init_inquire: Found '%s' '%s' '%s' '%s' at '%s'\n",
    s->vendor_name, s->product_name, s->version_name, s->build_name,
    s->device_name);

  /*defined in SCSI spec*/
  DBG (15, "standard inquiry options\n");

  /*FIXME: do we need to save these?*/
  DBG (15, "  PQ: %d\n",get_I_periph_qual(in));
  DBG (15, "  PDT: %d\n",get_I_periph_devtype(in));

  DBG (15, "  RMB: %d\n",get_I_rmb(in));
  DBG (15, "  DTQ: %d\n",get_I_devtype_qual(in));

  DBG (15, "  ISO: %d\n",get_I_iso_version(in));
  DBG (15, "  ECMA: %d\n",get_I_ecma_version(in));
  DBG (15, "  ANSI: %d\n",get_I_ansi_version(in));

  DBG (15, "  AENC: %d\n",get_I_aenc(in));
  DBG (15, "  TrmIOP: %d\n",get_I_trmiop(in));
  DBG (15, "  RDF: %d\n",get_I_resonse_format(in));

  DBG (15, "  Length: %d\n",get_I_length(in));

  DBG (15, "  RelAdr: %d\n",get_I_reladr(in));
  DBG (15, "  WBus32: %d\n",get_I_wbus32(in));
  DBG (15, "  WBus16: %d\n",get_I_wbus16(in));
  DBG (15, "  Sync: %d\n",get_I_sync(in));
  DBG (15, "  Linked: %d\n",get_I_linked(in));
  DBG (15, "  CmdQue: %d\n",get_I_cmdque(in));
  DBG (15, "  SftRe: %d\n",get_I_sftre(in));

  /*kodak specific*/
  DBG (15, "vendor inquiry options\n");

  DBG (15, "  MF Disable: %d\n",get_I_mf_disable(in));
  DBG (15, "  Checkdigit: %d\n",get_I_checkdigit(in));
  DBG (15, "  Front Prism: %d\n",get_I_front_prism(in));
  DBG (15, "  Comp Gray: %d\n",get_I_compressed_gray(in));
  DBG (15, "  Front Toggle: %d\n",get_I_front_toggle(in));
  DBG (15, "  Front DP1: %d\n",get_I_front_dp1(in));
  DBG (15, "  Front Color: %d\n",get_I_front_color(in));
  DBG (15, "  Front ATP: %d\n",get_I_front_atp(in));

  DBG (15, "  DP1 180: %d\n",get_I_dp1_180(in));
  DBG (15, "  MF Pause: %d\n",get_I_mf_pause(in));
  DBG (15, "  Rear Prism: %d\n",get_I_rear_prism(in));
  DBG (15, "  Uncomp Gray: %d\n",get_I_uncompressed_gray(in));
  DBG (15, "  Rear Toggle: %d\n",get_I_rear_toggle(in));
  DBG (15, "  Rear DP1: %d\n",get_I_rear_dp1(in));
  DBG (15, "  Rear Color: %d\n",get_I_rear_color(in));
  DBG (15, "  Rear ATP: %d\n",get_I_rear_atp(in));

  /* we actually care about these */
  DBG (15, "  Min Binary Res: %d\n",get_I_min_bin_res(in));
  s->s_res_min[MODE_LINEART] = get_I_min_bin_res(in);
  DBG (15, "  Max Binary Res: %d\n",get_I_max_bin_res(in));
  s->s_res_max[MODE_LINEART] = get_I_max_bin_res(in);
  DBG (15, "  Min Color Res: %d\n",get_I_min_col_res(in));
  s->s_res_min[MODE_COLOR] = get_I_min_col_res(in);
  DBG (15, "  Max Color Res: %d\n",get_I_max_col_res(in));
  s->s_res_max[MODE_COLOR] = get_I_max_col_res(in);

  DBG (15, "  Max Width: %d\n",get_I_max_image_width(in));
  s->s_width_max = get_I_max_image_width(in);
  DBG (15, "  Max Length: %d\n",get_I_max_image_length(in));
  s->s_length_max = get_I_max_image_length(in);

  /*FIXME: do we need to save these?*/
  DBG (15, "  Finecrop: %d\n",get_I_finecrop(in));
  DBG (15, "  iThresh: %d\n",get_I_ithresh(in));
  DBG (15, "  ECD: %d\n",get_I_ecd(in));
  DBG (15, "  VBLR: %d\n",get_I_vblr(in));
  DBG (15, "  Elevator: %d\n",get_I_elevator(in));
  DBG (15, "  RelCrop: %d\n",get_I_relcrop(in));

  DBG (15, "  CDeskew: %d\n",get_I_cdeskew(in));
  DBG (15, "  IA: %d\n",get_I_ia(in));
  DBG (15, "  Patch: %d\n",get_I_patch(in));
  DBG (15, "  Null Mode: %d\n",get_I_nullmode(in));
  DBG (15, "  SABRE: %d\n",get_I_sabre(in));
  DBG (15, "  LDDDS: %d\n",get_I_lddds(in));
  DBG (15, "  UDDDS: %d\n",get_I_uddds(in));
  DBG (15, "  Fixed Gap: %d\n",get_I_fixedgap(in));

  DBG (15, "  HR Printer: %d\n",get_I_hr_printer(in));
  DBG (15, "  Elev 100/250: %d\n",get_I_elev_100_250(in));
  DBG (15, "  UDDS Individual: %d\n",get_I_udds_individual(in));
  DBG (15, "  Auto Color: %d\n",get_I_auto_color(in));
  DBG (15, "  WB: %d\n",get_I_wb(in));
  DBG (15, "  ES: %d\n",get_I_es(in));
  DBG (15, "  FC: %d\n",get_I_fc(in));

  DBG (15, "  Max Rate: %d\n",get_I_max_rate(in));
  DBG (15, "  Buffer Size: %d\n",get_I_buffer_size(in));

  DBG (10, "init_inquire: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * get model specific info that is not in vpd, and correct
 * errors in vpd data. struct is already initialized to 0.
 */
static SANE_Status
init_model (struct scanner *s)
{

  DBG (10, "init_model: start\n");

  s->s_mode[MODE_LINEART] = 1;
  s->s_mode[MODE_HALFTONE] = 1;
  s->s_mode[MODE_GRAYSCALE] = 1;
  s->s_mode[MODE_COLOR] = 1;

  /* scanner did not tell us these */
  s->s_res_min[MODE_HALFTONE] = s->s_res_min[MODE_LINEART];
  s->s_res_max[MODE_HALFTONE] = s->s_res_max[MODE_LINEART];

  s->s_res_min[MODE_GRAYSCALE] = s->s_res_min[MODE_COLOR];
  s->s_res_max[MODE_GRAYSCALE] = s->s_res_max[MODE_COLOR];

  s->s_width_min = 96;
  s->s_length_min = 96;

  s->s_brightness_steps = 0;
  s->s_contrast_steps = 255;
  s->s_threshold_steps = 255;
  s->s_rif = 1;

  DBG (10, "init_model: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * set good default user values.
 * struct is already initialized to 0.
 */
static SANE_Status
init_user (struct scanner *s)
{

  DBG (10, "init_user: start\n");

  /* source */
  s->u_source = SOURCE_ADF_FRONT;

  /* scan mode */
  s->u_mode = MODE_LINEART;

  /*res, minimum for this mode*/
  s->u_res = s->s_res_min[s->u_mode];

  /* page width US-Letter */
  s->u_page_width = 8.5 * 1200;
  if(s->u_page_width > s->s_width_max){
    s->u_page_width = s->s_width_max;
  }

  /* page height US-Letter */
  s->u_page_height = 11 * 1200;
  if(s->u_page_height > s->s_length_max){
    s->u_page_height = s->s_length_max;
  }

  /* bottom-right x */
  s->u_br_x = s->u_page_width;

  /* bottom-right y */
  s->u_br_y = s->u_page_height;

  DBG (10, "init_user: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * This function presets the "option" array to blank
 */
static SANE_Status
init_options (struct scanner *s)
{
  int i;

  DBG (10, "init_options: start\n");

  memset (s->opt, 0, sizeof (s->opt));
  for (i = 0; i < NUM_OPTIONS; ++i) {
      s->opt[i].name = "filler";
      s->opt[i].size = sizeof (SANE_Word);
      s->opt[i].cap = SANE_CAP_INACTIVE;
  }

  /* go ahead and setup the first opt, because
   * frontend may call control_option on it
   * before calling get_option_descriptor
   */
  s->opt[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS;
  s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
  s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
  s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
  s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;

  DBG (10, "init_options: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * From the SANE spec:
 * This function is used to establish a connection to a particular
 * device. The name of the device to be opened is passed in argument
 * name. If the call completes successfully, a handle for the device
 * is returned in *h. As a special case, specifying a zero-length
 * string as the device requests opening the first available device
 * (if there is such a device).
 */
SANE_Status
sane_open (SANE_String_Const name, SANE_Handle * handle)
{
  struct scanner *dev = NULL;
  struct scanner *s = NULL;
  SANE_Status ret;
  unsigned char cmd[SEND_len];
  size_t cmdLen = SEND_len;
  unsigned char out[SR_len_time]; /*longest used in this function*/
  int try=0;
  time_t gmt_tt;
  struct tm * gmt_tm_p;
  struct tm * local_tm_p;

  DBG (10, "sane_open: start\n");

  if(scanner_devList){
    DBG (15, "sane_open: searching currently attached scanners\n");
  }
  else{
    DBG (15, "sane_open: no scanners currently attached, attaching\n");

    ret = sane_get_devices(NULL,0);
    if(ret != SANE_STATUS_GOOD){
      return ret;
    }
  }

  if(name[0] == 0){
    DBG (15, "sane_open: no device requested, using default\n");
    s = scanner_devList;
  }
  else{
    DBG (15, "sane_open: device %s requested\n", name);

    for (dev = scanner_devList; dev; dev = dev->next) {
      if (strcmp (dev->sane.name, name) == 0) {
        s = dev;
        break;
      }
    }
  }

  if (!s) {
    DBG (5, "sane_open: no device found\n");
    return SANE_STATUS_INVAL;
  }

  DBG (15, "sane_open: device %s found\n", s->sane.name);

  *handle = s;

  /* connect the fd so we can talk to scanner */
  ret = connect_fd(s);
  if(ret != SANE_STATUS_GOOD){
    return ret;
  }

  /*send the end batch (GX) command*/
  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,SEND_code);
  set_SR_datatype_code(cmd,SR_datatype_random);
  set_SR_datatype_qual(cmd,SR_qual_end);
  set_SR_xfer_length(cmd,SR_len_end);

  /*start the following loop*/
  ret = SANE_STATUS_DEVICE_BUSY;
  s->rs_info = 0;

  /*loop until scanner is ready*/
  while(ret == SANE_STATUS_DEVICE_BUSY){
    DBG (15, "sane_open: GX, try %d, sleep %lu\n", try, (unsigned long)s->rs_info);
    try++;
    sleep(s->rs_info);
    ret = do_cmd (
      s, 1, 0,
      cmd, cmdLen,
      NULL, 0,
      NULL, NULL
    );
    if(try > 5){
      break;
    }
  }
  if(ret){
    DBG (5, "sane_open: GX error %d\n",ret);
    return ret;
  }

  /*send the clear buffer (CB) command*/
  DBG (15, "sane_open: CB\n");
  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,SEND_code);
  set_SR_datatype_code(cmd,SR_datatype_random);
  set_SR_datatype_qual(cmd,SR_qual_clear);
  set_SR_xfer_length(cmd,SR_len_clear);

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    NULL, 0,
    NULL, NULL
  );
  if(ret){
    DBG (5, "sane_open: CB error %d\n",ret);
    return ret;
  }

  /*send the GT command*/
  DBG (15, "sane_open: GT\n");
  gmt_tt = time(NULL);
  gmt_tm_p = gmtime(&gmt_tt);

  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,SEND_code);
  set_SR_datatype_code(cmd,SR_datatype_random);
  set_SR_datatype_qual(cmd,SR_qual_gmt);
  set_SR_xfer_length(cmd,SR_len_time);

  memset(out,0,SR_len_time);
  set_SR_payload_len(out,SR_len_time);
  set_SR_time_hour(out,gmt_tm_p->tm_hour);
  set_SR_time_min(out,gmt_tm_p->tm_min);
  set_SR_time_mon(out,gmt_tm_p->tm_mon);
  set_SR_time_day(out,gmt_tm_p->tm_mday);
  set_SR_time_year(out,gmt_tm_p->tm_year+1900);

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    out, SR_len_time,
    NULL, NULL
  );
  if(ret){
    DBG (5, "sane_open: GT error %d\n",ret);
    return ret;
  }

  /*FIXME: read the LC command? */

  /*send the LC command*/
  DBG (15, "sane_open: LC\n");
  gmt_tt = time(NULL);
  local_tm_p = localtime(&gmt_tt);

  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,SEND_code);
  set_SR_datatype_code(cmd,SR_datatype_random);
  set_SR_datatype_qual(cmd,SR_qual_clock);
  set_SR_xfer_length(cmd,SR_len_time);

  memset(out,0,SR_len_time);
  set_SR_payload_len(out,SR_len_time);
  set_SR_time_hour(out,local_tm_p->tm_hour);
  set_SR_time_min(out,local_tm_p->tm_min);
  set_SR_time_mon(out,local_tm_p->tm_mon);
  set_SR_time_day(out,local_tm_p->tm_mday);
  set_SR_time_year(out,local_tm_p->tm_year+1900);

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    out, SR_len_time,
    NULL, NULL
  );
  if(ret){
    DBG (5, "sane_open: LC error %d\n",ret);
    return ret;
  }

  DBG (10, "sane_open: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * @@ Section 3 - SANE Options functions
 */

/*
 * Returns the options we know.
 *
 * From the SANE spec:
 * This function is used to access option descriptors. The function
 * returns the option descriptor for option number n of the device
 * represented by handle h. Option number 0 is guaranteed to be a
 * valid option. Its value is an integer that specifies the number of
 * options that are available for device handle h (the count includes
 * option 0). If n is not a valid option index, the function returns
 * NULL. The returned option descriptor is guaranteed to remain valid
 * (and at the returned address) until the device is closed.
 */
const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
  struct scanner *s = handle;
  int i;
  SANE_Option_Descriptor *opt = &s->opt[option];

  DBG (20, "sane_get_option_descriptor: %d\n", option);

  if ((unsigned) option >= NUM_OPTIONS)
    return NULL;

  /* "Mode" group -------------------------------------------------------- */
  if(option==OPT_MODE_GROUP){
    opt->title = "Scan Mode";
    opt->desc = "";
    opt->type = SANE_TYPE_GROUP;
    opt->constraint_type = SANE_CONSTRAINT_NONE;
  }

  /* source */
  if(option==OPT_SOURCE){
    i=0;
    s->o_source_list[i++]=STRING_ADFFRONT;
    s->o_source_list[i++]=STRING_ADFBACK;
    s->o_source_list[i++]=STRING_ADFDUPLEX;
    s->o_source_list[i]=NULL;

    opt->name = SANE_NAME_SCAN_SOURCE;
    opt->title = SANE_TITLE_SCAN_SOURCE;
    opt->desc = SANE_DESC_SCAN_SOURCE;
    opt->type = SANE_TYPE_STRING;
    opt->constraint_type = SANE_CONSTRAINT_STRING_LIST;
    opt->constraint.string_list = s->o_source_list;
    opt->size = maxStringSize (opt->constraint.string_list);
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* scan mode */
  if(option==OPT_MODE){
    i=0;
    if(s->s_mode[MODE_LINEART]){
      s->o_mode_list[i++]=STRING_LINEART;
    }
    if(s->s_mode[MODE_HALFTONE]){
      s->o_mode_list[i++]=STRING_HALFTONE;
    }
    if(s->s_mode[MODE_GRAYSCALE]){
      s->o_mode_list[i++]=STRING_GRAYSCALE;
    }
    if(s->s_mode[MODE_COLOR]){
      s->o_mode_list[i++]=STRING_COLOR;
    }
    s->o_mode_list[i]=NULL;

    opt->name = SANE_NAME_SCAN_MODE;
    opt->title = SANE_TITLE_SCAN_MODE;
    opt->desc = SANE_DESC_SCAN_MODE;
    opt->type = SANE_TYPE_STRING;
    opt->constraint_type = SANE_CONSTRAINT_STRING_LIST;
    opt->constraint.string_list = s->o_mode_list;
    opt->size = maxStringSize (opt->constraint.string_list);
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* resolution */
  /* build a list of possible choices for current mode */
  if(option==OPT_RES){
    int reslist[]={100,150,200,240,300,400};
    int j;

    i=0;
    for(j=0;j<6;j++){
      if(reslist[j] >= s->s_res_min[s->u_mode]
        && reslist[j] <= s->s_res_max[s->u_mode]){
          s->o_res_list[s->u_mode][++i] = reslist[j];
      }
    }
    s->o_res_list[s->u_mode][0] = i;

    opt->name = SANE_NAME_SCAN_RESOLUTION;
    opt->title = SANE_TITLE_SCAN_RESOLUTION;
    opt->desc = SANE_DESC_SCAN_RESOLUTION;
    opt->type = SANE_TYPE_INT;
    opt->unit = SANE_UNIT_DPI;
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
    opt->constraint_type = SANE_CONSTRAINT_WORD_LIST;
    opt->constraint.word_list = s->o_res_list[s->u_mode];
  }

  /* "Geometry" group ---------------------------------------------------- */
  if(option==OPT_GEOMETRY_GROUP){
    opt->title = "Geometry";
    opt->desc = "";
    opt->type = SANE_TYPE_GROUP;
    opt->constraint_type = SANE_CONSTRAINT_NONE;
  }

  /* top-left x */
  if(option==OPT_TL_X){
    /* values stored in 1200 dpi units */
    /* must be converted to MM for sane */
    s->o_tl_x_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_width_min);
    s->o_tl_x_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_width_max);
    s->o_tl_x_range.quant = MM_PER_UNIT_FIX;

    opt->name = SANE_NAME_SCAN_TL_X;
    opt->title = SANE_TITLE_SCAN_TL_X;
    opt->desc = SANE_DESC_SCAN_TL_X;
    opt->type = SANE_TYPE_FIXED;
    opt->unit = SANE_UNIT_MM;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &(s->o_tl_x_range);
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* top-left y */
  if(option==OPT_TL_Y){
    /* values stored in 1200 dpi units */
    /* must be converted to MM for sane */
    s->o_tl_y_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_length_min);
    s->o_tl_y_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_length_max);
    s->o_tl_y_range.quant = MM_PER_UNIT_FIX;

    opt->name = SANE_NAME_SCAN_TL_Y;
    opt->title = SANE_TITLE_SCAN_TL_Y;
    opt->desc = SANE_DESC_SCAN_TL_Y;
    opt->type = SANE_TYPE_FIXED;
    opt->unit = SANE_UNIT_MM;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &(s->o_tl_y_range);
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* bottom-right x */
  if(option==OPT_BR_X){
    /* values stored in 1200 dpi units */
    /* must be converted to MM for sane */
    s->o_br_x_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_width_min);
    s->o_br_x_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_width_max);
    s->o_br_x_range.quant = MM_PER_UNIT_FIX;

    opt->name = SANE_NAME_SCAN_BR_X;
    opt->title = SANE_TITLE_SCAN_BR_X;
    opt->desc = SANE_DESC_SCAN_BR_X;
    opt->type = SANE_TYPE_FIXED;
    opt->unit = SANE_UNIT_MM;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &(s->o_br_x_range);
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* bottom-right y */
  if(option==OPT_BR_Y){
    /* values stored in 1200 dpi units */
    /* must be converted to MM for sane */
    s->o_br_y_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_length_min);
    s->o_br_y_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_length_max);
    s->o_br_y_range.quant = MM_PER_UNIT_FIX;

    opt->name = SANE_NAME_SCAN_BR_Y;
    opt->title = SANE_TITLE_SCAN_BR_Y;
    opt->desc = SANE_DESC_SCAN_BR_Y;
    opt->type = SANE_TYPE_FIXED;
    opt->unit = SANE_UNIT_MM;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &(s->o_br_y_range);
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* page width */
  if(option==OPT_PAGE_WIDTH){
    /* values stored in 1200 dpi units */
    /* must be converted to MM for sane */
    s->o_page_x_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_width_min);
    s->o_page_x_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_width_max);
    s->o_page_x_range.quant = MM_PER_UNIT_FIX;

    opt->name = "pagewidth";
    opt->title = "ADF paper width";
    opt->desc = "Must be set properly to align scanning window";
    opt->type = SANE_TYPE_FIXED;
    opt->unit = SANE_UNIT_MM;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &s->o_page_x_range;
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* page height */
  if(option==OPT_PAGE_HEIGHT){
    /* values stored in 1200 dpi units */
    /* must be converted to MM for sane */
    s->o_page_y_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_length_min);
    s->o_page_y_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_length_max);
    s->o_page_y_range.quant = MM_PER_UNIT_FIX;

    opt->name = "pageheight";
    opt->title = "ADF paper length";
    opt->desc = "Must be set properly to eject pages";
    opt->type = SANE_TYPE_FIXED;
    opt->unit = SANE_UNIT_MM;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &s->o_page_y_range;
    opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
  }

  /* "Enhancement" group ------------------------------------------------- */
  if(option==OPT_ENHANCEMENT_GROUP){
    opt->title = "Enhancement";
    opt->desc = "";
    opt->type = SANE_TYPE_GROUP;
    opt->constraint_type = SANE_CONSTRAINT_NONE;
  }

  /* brightness */
  if(option==OPT_BRIGHTNESS){
    opt->name = SANE_NAME_BRIGHTNESS;
    opt->title = SANE_TITLE_BRIGHTNESS;
    opt->desc = SANE_DESC_BRIGHTNESS;
    opt->type = SANE_TYPE_INT;
    opt->unit = SANE_UNIT_NONE;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &s->o_brightness_range;
    s->o_brightness_range.quant=1;
    s->o_brightness_range.min=-(s->s_brightness_steps/2);
    s->o_brightness_range.max=s->s_brightness_steps/2;
    if(opt->constraint.range->max > opt->constraint.range->min){
      opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
    }
    else{
      opt->cap = SANE_CAP_INACTIVE;
    }
  }

  /* contrast */
  if(option==OPT_CONTRAST){
    opt->name = SANE_NAME_CONTRAST;
    opt->title = SANE_TITLE_CONTRAST;
    opt->desc = SANE_DESC_CONTRAST;
    opt->type = SANE_TYPE_INT;
    opt->unit = SANE_UNIT_NONE;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &s->o_contrast_range;
    s->o_contrast_range.quant=1;
    s->o_contrast_range.min=-(s->s_contrast_steps/2);
    s->o_contrast_range.max=s->s_contrast_steps/2;
    if(opt->constraint.range->max > opt->constraint.range->min){
      opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
    }
    else{
      opt->cap = SANE_CAP_INACTIVE;
    }
  }

  /*threshold*/
  if(option==OPT_THRESHOLD){
    opt->name = SANE_NAME_THRESHOLD;
    opt->title = SANE_TITLE_THRESHOLD;
    opt->desc = SANE_DESC_THRESHOLD;
    opt->type = SANE_TYPE_INT;
    opt->unit = SANE_UNIT_NONE;
    opt->constraint_type = SANE_CONSTRAINT_RANGE;
    opt->constraint.range = &s->o_threshold_range;
    s->o_threshold_range.min=0;
    s->o_threshold_range.max=s->s_threshold_steps;
    s->o_threshold_range.quant=1;
    if(opt->constraint.range->max > opt->constraint.range->min){
      opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
    }
    else{
      opt->cap = SANE_CAP_INACTIVE;
    }
  }

  /*rif*/
  if(option==OPT_RIF){
    opt->name = "rif";
    opt->title = "RIF";
    opt->desc = "Reverse image format";
    opt->type = SANE_TYPE_BOOL;
    opt->unit = SANE_UNIT_NONE;
    if (s->s_rif)
      opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
    else
      opt->cap = SANE_CAP_INACTIVE;
  }

  return opt;
}

/**
 * Gets or sets an option value.
 *
 * From the SANE spec:
 * This function is used to set or inquire the current value of option
 * number n of the device represented by handle h. The manner in which
 * the option is controlled is specified by parameter action. The
 * possible values of this parameter are described in more detail
 * below.  The value of the option is passed through argument val. It
 * is a pointer to the memory that holds the option value. The memory
 * area pointed to by v must be big enough to hold the entire option
 * value (determined by member size in the corresponding option
 * descriptor).
 *
 * The only exception to this rule is that when setting the value of a
 * string option, the string pointed to by argument v may be shorter
 * since the backend will stop reading the option value upon
 * encountering the first NUL terminator in the string. If argument i
 * is not NULL, the value of *i will be set to provide details on how
 * well the request has been met.
 */
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
                     SANE_Action action, void *val, SANE_Int * info)
{
  struct scanner *s = (struct scanner *) handle;
  SANE_Int dummy = 0;

  /* Make sure that all those statements involving *info cannot break (better
   * than having to do "if (info) ..." everywhere!)
   */
  if (info == 0)
    info = &dummy;

  if (option >= NUM_OPTIONS) {
    DBG (5, "sane_control_option: %d too big\n", option);
    return SANE_STATUS_INVAL;
  }

  if (!SANE_OPTION_IS_ACTIVE (s->opt[option].cap)) {
    DBG (5, "sane_control_option: %d inactive\n", option);
    return SANE_STATUS_INVAL;
  }

  /*
   * SANE_ACTION_GET_VALUE: We have to find out the current setting and
   * return it in a human-readable form (often, text).
   */
  if (action == SANE_ACTION_GET_VALUE) {
      SANE_Word * val_p = (SANE_Word *) val;

      DBG (20, "sane_control_option: get value for '%s' (%d)\n", s->opt[option].name,option);

      switch (option) {

        case OPT_NUM_OPTS:
          *val_p = NUM_OPTIONS;
          return SANE_STATUS_GOOD;

        case OPT_SOURCE:
          if(s->u_source == SOURCE_ADF_FRONT){
            strcpy (val, STRING_ADFFRONT);
          }
          else if(s->u_source == SOURCE_ADF_BACK){
            strcpy (val, STRING_ADFBACK);
          }
          else if(s->u_source == SOURCE_ADF_DUPLEX){
            strcpy (val, STRING_ADFDUPLEX);
          }
          else{
            DBG(5,"missing option val for source\n");
          }
          return SANE_STATUS_GOOD;

        case OPT_MODE:
          if(s->u_mode == MODE_LINEART){
            strcpy (val, STRING_LINEART);
          }
          else if(s->u_mode == MODE_HALFTONE){
            strcpy (val, STRING_HALFTONE);
          }
          else if(s->u_mode == MODE_GRAYSCALE){
            strcpy (val, STRING_GRAYSCALE);
          }
          else if(s->u_mode == MODE_COLOR){
            strcpy (val, STRING_COLOR);
          }
          return SANE_STATUS_GOOD;

        case OPT_RES:
          *val_p = s->u_res;
          return SANE_STATUS_GOOD;

        case OPT_TL_X:
          *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_tl_x);
          return SANE_STATUS_GOOD;

        case OPT_TL_Y:
          *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_tl_y);
          return SANE_STATUS_GOOD;

        case OPT_BR_X:
          *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_br_x);
          return SANE_STATUS_GOOD;

        case OPT_BR_Y:
          *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_br_y);
          return SANE_STATUS_GOOD;

        case OPT_PAGE_WIDTH:
          *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_page_width);
          return SANE_STATUS_GOOD;

        case OPT_PAGE_HEIGHT:
          *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_page_height);
          return SANE_STATUS_GOOD;

        case OPT_BRIGHTNESS:
          *val_p = s->u_brightness;
          return SANE_STATUS_GOOD;

        case OPT_CONTRAST:
          *val_p = s->u_contrast;
          return SANE_STATUS_GOOD;

        case OPT_THRESHOLD:
          *val_p = s->u_threshold;
          return SANE_STATUS_GOOD;

        case OPT_RIF:
          *val_p = s->u_rif;
          return SANE_STATUS_GOOD;
      }
  }
  else if (action == SANE_ACTION_SET_VALUE) {
      int tmp;
      SANE_Word val_c;
      SANE_Status status;

      DBG (20, "sane_control_option: set value for '%s' (%d)\n", s->opt[option].name,option);

      if ( s->started ) {
        DBG (5, "sane_control_option: can't set, device busy\n");
        return SANE_STATUS_DEVICE_BUSY;
      }

      if (!SANE_OPTION_IS_SETTABLE (s->opt[option].cap)) {
        DBG (5, "sane_control_option: not settable\n");
        return SANE_STATUS_INVAL;
      }

      status = sanei_constrain_value (s->opt + option, val, info);
      if (status != SANE_STATUS_GOOD) {
        DBG (5, "sane_control_option: bad value\n");
        return status;
      }

      /* may have been changed by constrain, so don't copy until now */
      val_c = *(SANE_Word *)val;

      /*
       * Note - for those options which can assume one of a list of
       * valid values, we can safely assume that they will have
       * exactly one of those values because that's what
       * sanei_constrain_value does. Hence no "else: invalid" branches
       * below.
       */
      switch (option) {

        /* Mode Group */
        case OPT_SOURCE:
          if (!strcmp (val, STRING_ADFFRONT)) {
            tmp = SOURCE_ADF_FRONT;
          }
          else if (!strcmp (val, STRING_ADFBACK)) {
            tmp = SOURCE_ADF_BACK;
          }
          else{
            tmp = SOURCE_ADF_DUPLEX;
          }

          if (s->u_source != tmp) {
            s->u_source = tmp;
            *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        case OPT_MODE:
          if (!strcmp (val, STRING_LINEART)) {
            tmp = MODE_LINEART;
          }
          else if (!strcmp (val, STRING_HALFTONE)) {
            tmp = MODE_HALFTONE;
          }
          else if (!strcmp (val, STRING_GRAYSCALE)) {
            tmp = MODE_GRAYSCALE;
          }
          else{
            tmp = MODE_COLOR;
          }

          if (tmp != s->u_mode){
            s->u_mode = tmp;
            *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        case OPT_RES:

          if (s->u_res != val_c) {
            s->u_res = val_c;
            *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        /* Geometry Group */
        case OPT_TL_X:
          if (s->u_tl_x != FIXED_MM_TO_SCANNER_UNIT(val_c)){
            s->u_tl_x = FIXED_MM_TO_SCANNER_UNIT(val_c);
            *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        case OPT_TL_Y:
          if (s->u_tl_y != FIXED_MM_TO_SCANNER_UNIT(val_c)){
            s->u_tl_y = FIXED_MM_TO_SCANNER_UNIT(val_c);
            *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        case OPT_BR_X:
          if (s->u_br_x != FIXED_MM_TO_SCANNER_UNIT(val_c)){
            s->u_br_x = FIXED_MM_TO_SCANNER_UNIT(val_c);
            *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        case OPT_BR_Y:
          if (s->u_br_y != FIXED_MM_TO_SCANNER_UNIT(val_c)){
            s->u_br_y = FIXED_MM_TO_SCANNER_UNIT(val_c);
            *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        case OPT_PAGE_WIDTH:
          if (s->u_page_width != FIXED_MM_TO_SCANNER_UNIT(val_c)){
            s->u_page_width = FIXED_MM_TO_SCANNER_UNIT(val_c);
            *info |= SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        case OPT_PAGE_HEIGHT:
          if (s->u_page_height != FIXED_MM_TO_SCANNER_UNIT(val_c)){
            s->u_page_height = FIXED_MM_TO_SCANNER_UNIT(val_c);
            *info |= SANE_INFO_RELOAD_OPTIONS;
          }
          return SANE_STATUS_GOOD;

        /* Enhancement Group */
        case OPT_BRIGHTNESS:
          if (s->u_brightness != val_c){
            s->u_brightness = val_c;
          }
          return SANE_STATUS_GOOD;

        case OPT_CONTRAST:
          if (s->u_contrast != val_c){
            s->u_contrast = val_c;
          }
          return SANE_STATUS_GOOD;

        case OPT_THRESHOLD:
          if (s->u_threshold != val_c){
            s->u_threshold = val_c;
          }
          return SANE_STATUS_GOOD;

        case OPT_RIF:
          if (s->u_rif != val_c){
            s->u_rif = val_c;
          }
          return SANE_STATUS_GOOD;

      }                       /* switch */
  }                           /* else */

  return SANE_STATUS_INVAL;
}

/*
 * @@ Section 4 - SANE scanning functions
 */
/*
 * Called by SANE to retrieve information about the type of data
 * that the current scan will return.
 *
 * From the SANE spec:
 * This function is used to obtain the current scan parameters. The
 * returned parameters are guaranteed to be accurate between the time
 * a scan has been started (sane_start() has been called) and the
 * completion of that request. Outside of that window, the returned
 * values are best-effort estimates of what the parameters will be
 * when sane_start() gets invoked.
 *
 * Calling this function before a scan has actually started allows,
 * for example, to get an estimate of how big the scanned image will
 * be. The parameters passed to this function are the handle h of the
 * device for which the parameters should be obtained and a pointer p
 * to a parameter structure.
 */
/* SANE_Parameters is defined as a struct containing:
	SANE_Frame format;
	SANE_Bool last_frame;
	SANE_Int lines;
	SANE_Int depth; ( binary=1, gray=8, color=8 (!24) )
	SANE_Int pixels_per_line;
	SANE_Int bytes_per_line;
*/
SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
    SANE_Status ret = SANE_STATUS_GOOD;
    struct scanner *s = (struct scanner *) handle;

    DBG (10, "sane_get_parameters: start\n");

    /* started? get param data from image header */
    if(s->started){
        DBG (15, "sane_get_parameters: image settings:\n");

        DBG (15, "  tlx=%d, brx=%d, iw=%d, maxx=%d\n",
          s->i_tlx, (s->i_tlx+s->i_width), s->i_width, s->s_width_max/1200);
        DBG (15, "  tly=%d, bry=%d, il=%d, maxy=%d\n",
          s->i_tly, (s->i_tly+s->i_length), s->i_length, s->s_length_max/1200);
        DBG (15, "  res=%d, id=%d, bytes=%d\n",
          s->i_dpi, s->i_id, s->i_bytes);

        params->last_frame = 1;
        params->lines = s->i_length;
        params->pixels_per_line = s->i_width;

        /* bitonal */
        if (s->i_bpp == 1) {
            params->format = SANE_FRAME_GRAY;
            params->depth = 1;
            params->bytes_per_line = params->pixels_per_line / 8;

#ifdef SANE_FRAME_G42D
	    /*G4 fax compression*/
            if (s->i_compr) {
                params->format = SANE_FRAME_G42D;
	    }
#endif
        }
        /* gray */
        else if (s->i_bpp == 8) {
            params->format = SANE_FRAME_GRAY;
            params->depth = 8;
            params->bytes_per_line = params->pixels_per_line;

#ifdef SANE_FRAME_JPEG
	    /*jpeg compression*/
            if (s->i_compr) {
                params->format = SANE_FRAME_JPEG;
	    }
#endif
        }
        /* color */
        else if (s->i_bpp == 24 || s->i_bpp == 96) {
            params->format = SANE_FRAME_RGB;
            params->depth = 8;
            params->bytes_per_line = params->pixels_per_line * 3;

#ifdef SANE_FRAME_JPEG
	    /*jpeg compression*/
            if (s->i_compr) {
                params->format = SANE_FRAME_JPEG;
	    }
#endif
        }
        else{
	    DBG(5,"sane_get_parameters: unsupported depth %d\n", s->i_bpp);
	    return SANE_STATUS_INVAL;
        }
    }

    /* not started? get param data from user input */
    else{

        DBG (15, "sane_get_parameters: user settings:\n");

        DBG (15, "  tlx=%d, brx=%d, pw=%d, maxx=%d\n",
          s->u_tl_x, s->u_br_x, s->u_page_width, s->s_width_max);
        DBG (15, "  tly=%d, bry=%d, ph=%d, maxy=%d\n",
          s->u_tl_y, s->u_br_y, s->u_page_height, s->s_length_max);
        DBG (15, "  res=%d, user_x=%d, user_y=%d\n",
          s->u_res, (s->u_res * (s->u_br_x - s->u_tl_x) / 1200),
          (s->u_res * (s->u_br_y - s->u_tl_y) / 1200));

        if (s->u_mode == MODE_COLOR) {
            params->format = SANE_FRAME_RGB;
            params->depth = 8;
        }
        else if (s->u_mode == MODE_GRAYSCALE) {
            params->format = SANE_FRAME_GRAY;
            params->depth = 8;
        }
        else {
            params->format = SANE_FRAME_GRAY;
            params->depth = 1;
        }

        params->last_frame = 1;
        params->lines = s->u_res * (s->u_br_y - s->u_tl_y) / 1200;
        params->pixels_per_line = s->u_res * (s->u_br_x - s->u_tl_x) / 1200;

        /* bytes per line differs by mode */
        if (s->u_mode == MODE_COLOR) {
            params->bytes_per_line = params->pixels_per_line * 3;
        }
        else if (s->u_mode == MODE_GRAYSCALE) {
            params->bytes_per_line = params->pixels_per_line;
        }
        else {
            params->bytes_per_line = params->pixels_per_line / 8;
        }

    }

    DBG (15, "sane_get_parameters: returning:\n");
    DBG (15, "  scan_x=%d, Bpl=%d, depth=%d\n",
      params->pixels_per_line, params->bytes_per_line, params->depth );

    DBG (15, "  scan_y=%d, frame=%d, last=%d\n",
      params->lines, params->format, params->last_frame );

    DBG (10, "sane_get_parameters: finish\n");

    return ret;
}

/*
 * Called by SANE when a page acquisition operation is to be started.
 * commands: scanner control (lampon), send (lut), send (dither),
 * set window, object pos, and scan
 *
 * this will be called before each image, including duplex backsides,
 * and at the start of adf batch.
 * hence, we spend a lot of time playing with s->started, etc.
 */
SANE_Status
sane_start (SANE_Handle handle)
{
  struct scanner *s = handle;
  SANE_Status ret;

  DBG (10, "sane_start: start\n");

  DBG (15, "started=%d, source=%d\n", s->started, s->u_source);

  /* batch already running */
  if(s->started){
      /* not finished with current image, error */
      if (s->bytes_tx != s->i_bytes) {
          DBG(5,"sane_start: previous transfer not finished?");
          return do_cancel(s);
      }
  }

  /* first page of batch */
  else{

      unsigned char cmd[SCAN_len];
      unsigned char pay[SR_len_startstop];

      /* set window command */
      ret = set_window(s);
      if (ret != SANE_STATUS_GOOD) {
        DBG (5, "sane_start: ERROR: cannot set window\n");
        do_cancel(s);
        return ret;
      }

      /* read/send JQ command */

      /* read/send SC command */
      ret = send_sc(s);
      if (ret != SANE_STATUS_GOOD) {
        DBG (5, "sane_start: ERROR: cannot send SC\n");
        do_cancel(s);
        return ret;
      }

      /* read/send CT command */

      DBG (15, "sane_start: send SCAN\n");
      memset(cmd, 0, SCAN_len);
      set_SCSI_opcode(cmd, SCAN_code);

      ret = do_cmd (
        s, 1, 0,
        cmd, SCAN_len,
        NULL, 0,
        NULL, NULL
      );
      if (ret != SANE_STATUS_GOOD) {
        DBG (5, "sane_start: ERROR sending SCAN\n");
        do_cancel(s);
        return ret;
      }

      /* send SS command */
      DBG (15, "sane_start: send SS\n");
      memset(cmd,0,SEND_len);
      set_SCSI_opcode(cmd,SEND_code);
      set_SR_datatype_code(cmd,SR_datatype_random);
      set_SR_datatype_qual(cmd,SR_qual_startstop);
      set_SR_xfer_length(cmd,SR_len_startstop);

      memset(pay,0,SR_len_startstop);
      set_SR_payload_len(pay,SR_len_startstop);
      set_SR_startstop_cmd(pay,1);

      ret = do_cmd (
        s, 1, 0,
        cmd, SEND_len,
        pay, SR_len_startstop,
        NULL, NULL
      );
      if(ret){
        DBG (5, "sane_open: SS error %d\n",ret);
        return ret;
      }

      DBG (15, "sane_start: sleeping\n");
      sleep(2);

      s->started=1;
  }

  ret = read_imageheader(s);
  if(ret){
    DBG (5, "sane_open: error reading imageheader %d\n",ret);
    return ret;
  }

  /* set clean defaults */
  s->bytes_rx  = 0;
  s->bytes_tx  = 0;

  /* make large buffer to hold the images */
  DBG (15, "sane_start: setup buffer\n");

  /* free current buffer if too small */
  if (s->buffer && s->bytes_buf < s->i_bytes) {
      DBG (15, "sane_start: free buffer.\n");
      free(s->buffer);
      s->buffer = NULL;
      s->bytes_buf = 0;
  }

  /* grab new buffer if don't have one */
  if (!s->buffer) {
      DBG (15, "sane_start: calloc buffer.\n");
      s->buffer = calloc (1,s->i_bytes);
      if (!s->buffer) {
          DBG (5, "sane_start: Error, no buffer\n");
          do_cancel(s);
          return SANE_STATUS_NO_MEM;
      }
  }

  DBG (15, "started=%d, source=%d\n", s->started, s->u_source);

  DBG (10, "sane_start: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * This routine issues a SCSI SET WINDOW command to the scanner, using the
 * values currently in the scanner data structure.
 * the scanner has 4 separate windows, and all must be set similarly,
 * even if you don't intend to acquire images from all of them.
 */
static SANE_Status
set_window (struct scanner *s)
{
  SANE_Status ret = SANE_STATUS_GOOD;

  unsigned char cmd[SET_WINDOW_len];
  size_t cmdLen = SET_WINDOW_len;

  /* the data phase has a header, followed by a window desc block
   * the header specifies the number of bytes in 1 window desc block */
  unsigned char pay[WINDOW_HEADER_len + WINDOW_DESCRIPTOR_len];
  size_t payLen = WINDOW_HEADER_len + WINDOW_DESCRIPTOR_len;

  unsigned char * desc = pay + WINDOW_HEADER_len;

  int width = (s->u_br_x - s->u_tl_x) * s->u_res/1200;
  int length = (s->u_br_y - s->u_tl_y) * s->u_res/1200;

  DBG (10, "set_window: start\n");

  /* binary window settings */
  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,SET_WINDOW_code);
  set_SW_xferlen(cmd,payLen);

  memset(pay,0,payLen);
  set_WH_desc_len(pay,WINDOW_DESCRIPTOR_len);

  set_WD_wid(desc,WD_wid_front_binary);

  /* common settings */
  set_WD_Xres (desc, s->u_res);
  set_WD_Yres (desc, s->u_res);

  set_WD_ULX (desc, s->u_tl_x);
  set_WD_ULY (desc, s->u_tl_y);

  /* width % 32 == 0 && length % 1 == 0 */
  width -= width % 32;
  width = width*1200/s->u_res;

  length = length*1200/s->u_res;

  set_WD_width (desc, width);
  set_WD_length (desc, length);

  /* brightness not supported? */
  set_WD_brightness (desc, 0);
  set_WD_threshold (desc, s->u_threshold);
  set_WD_contrast (desc, 0);
  if(s->s_contrast_steps){
    /*convert our common -127 to +127 range into HW's range
     *FIXME: this code assumes hardware range of 1-255 */
    set_WD_contrast (desc, s->u_contrast+128);
  }

  if(s->u_mode == MODE_HALFTONE){
    set_WD_composition (desc, WD_compo_HALFTONE);
    set_WD_bitsperpixel (desc, 1);
  }
  else{
    set_WD_composition (desc, WD_compo_LINEART);
    set_WD_bitsperpixel (desc, 1);
  }

  /* FIXME ht pattern */

  set_WD_rif (desc, s->u_rif);

  set_WD_bitorder (desc, 1);

  /* compression options */
  if(s->u_compr)
    set_WD_compress_type (desc, WD_compr_FAXG4);

  /*FIXME: noise filter */

  set_WD_allow_zero(desc,1);
  set_WD_cropping (desc, WD_crop_RELATIVE);

  /*FIXME: more settings here*/

  hexdump(15, "front binary window:", desc, WINDOW_DESCRIPTOR_len);

  DBG (15, "set_window: set window binary back\n");
  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    pay, payLen,
    NULL, NULL
  );
  if(ret){
    DBG (5, "set_window: error setting binary front window %d\n",ret);
    return ret;
  }

  /*send the window for backside too*/
  set_WD_wid(desc,WD_wid_back_binary);

  DBG (15, "set_window: set window binary back\n");
  ret = do_cmd (
      s, 1, 0,
      cmd, cmdLen,
      pay, payLen,
      NULL, NULL
  );
  if(ret){
    DBG (5, "set_window: error setting binary back window %d\n",ret);
    return ret;
  }

#if 0
  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,GET_WINDOW_code);
  set_GW_single(cmd,1);
  set_GW_wid(cmd,WD_wid_front_color);
  set_GW_xferlen(cmd,payLen);

  ret = do_cmd (
      s, 1, 0,
      cmd, cmdLen,
      NULL, 0,
      pay, &payLen
  );
  if(ret){
    DBG (5, "set_window: error getting window %d\n",ret);
    return ret;
  }
  hexdump(15,"foo",pay,payLen);
#endif

  /* color window settings */
  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,SET_WINDOW_code);
  set_SW_xferlen(cmd,payLen);

  memset(pay,0,payLen);
  set_WH_desc_len(pay,WINDOW_DESCRIPTOR_len);

  set_WD_wid(desc,WD_wid_front_color);

  /* common settings */
  set_WD_Xres (desc, s->u_res);
  set_WD_Yres (desc, s->u_res);

  set_WD_ULX (desc, s->u_tl_x);
  set_WD_ULY (desc, s->u_tl_y);

  set_WD_width (desc, width);
  set_WD_length (desc, length);

  /*gray mode*/
  if(s->u_mode == MODE_GRAYSCALE){
    /*
     gamma
     width % 8 == 0 && length % 8 == 0
     */
    set_WD_composition (desc, WD_compo_MULTILEVEL);
    set_WD_bitsperpixel (desc, 8);
  }
  /*color mode or color window in binary mode*/
  else{
    /*
     width % 16 == 0 && length % 8 == 0
     */
    set_WD_composition (desc, WD_compo_MULTILEVEL);
    set_WD_bitsperpixel (desc, 24);

    /* compression options */
    if(s->u_compr)
      set_WD_compress_type (desc, WD_compr_JPEG);
  }

  set_WD_bitorder (desc, 1);

  /*FIXME: noise filter */

  set_WD_allow_zero(desc,1);
  set_WD_cropping (desc, WD_crop_RELATIVE);

  /*FIXME: more settings here*/

  DBG (15, "set_window: set window color front\n");

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    pay, payLen,
    NULL, NULL
  );
  if(ret){
    DBG (5, "set_window: error setting color front window %d\n",ret);
    return ret;
  }

  /*send the window for backside too*/
  set_WD_wid(desc,WD_wid_back_color);

  DBG (15, "set_window: set window color back\n");

  ret = do_cmd (
      s, 1, 0,
      cmd, cmdLen,
      pay, payLen,
      NULL, NULL
  );
  if(ret){
    DBG (5, "set_window: error setting color back window %d\n",ret);
    return ret;
  }

  DBG (10, "set_window: finish\n");

  return ret;
}

/*
 * This routine reads the SC (scanner config) data from the scanner
 * modifies a few params based on user data, and sends it back
 */
static SANE_Status
send_sc(struct scanner *s)
{
  SANE_Status ret = SANE_STATUS_GOOD;

  unsigned char cmd[READ_len];
  size_t cmdLen = READ_len;
  unsigned char pay[SR_len_config];
  size_t payLen = SR_len_config;

  /* send SC command */
  DBG (10, "send_sc: start\n");

  DBG (15, "send_sc: reading config\n");
  memset(cmd,0,READ_len);
  set_SCSI_opcode(cmd,READ_code);

  set_SR_datatype_code(cmd,SR_datatype_random);
  set_SR_datatype_qual(cmd,SR_qual_config);
  set_SR_xfer_length(cmd,SR_len_config);

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    NULL, 0,
    pay, &payLen
  );
  if(ret || !payLen){
    DBG (5, "send_sc: error reading: %d\n",ret);
    return ret;
  }

  memset(cmd,0,SEND_len);
  set_SCSI_opcode(cmd,SEND_code);

  set_SR_datatype_code(cmd,SR_datatype_random);
  set_SR_datatype_qual(cmd,SR_qual_config);
  set_SR_xfer_length(cmd,payLen);

  if(s->u_source == SOURCE_ADF_FRONT){
    if(s->u_mode == MODE_COLOR || s->u_mode == MODE_GRAYSCALE){
      set_SR_sc_io1(pay,SR_sc_io_front_color);
    }
    else{
      set_SR_sc_io1(pay,SR_sc_io_front_binary);
    }
    set_SR_sc_io2(pay,SR_sc_io_none);
    set_SR_sc_io3(pay,SR_sc_io_none);
    set_SR_sc_io4(pay,SR_sc_io_none);
  }
  else if(s->u_source == SOURCE_ADF_BACK){
    if(s->u_mode == MODE_COLOR || s->u_mode == MODE_GRAYSCALE){
      set_SR_sc_io1(pay,SR_sc_io_rear_color);
    }
    else{
      set_SR_sc_io1(pay,SR_sc_io_rear_binary);
    }
    set_SR_sc_io2(pay,SR_sc_io_none);
    set_SR_sc_io3(pay,SR_sc_io_none);
    set_SR_sc_io4(pay,SR_sc_io_none);
  }
  else{
    if(s->u_mode == MODE_COLOR || s->u_mode == MODE_GRAYSCALE){
      set_SR_sc_io1(pay,SR_sc_io_front_color);
      set_SR_sc_io2(pay,SR_sc_io_rear_color);
    }
    else{
      set_SR_sc_io1(pay,SR_sc_io_front_binary);
      set_SR_sc_io2(pay,SR_sc_io_rear_binary);
    }
    set_SR_sc_io3(pay,SR_sc_io_none);
    set_SR_sc_io4(pay,SR_sc_io_none);
  }

  /*FIXME: there are hundreds of other settings in this payload*/

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    pay, payLen,
    NULL, NULL
  );

  DBG (10, "send_sc: finish %d\n",ret);

  return ret;
}

/*
 * This routine reads the image header from the scanner, and updates
 * values currently in the scanner data structure.
 */
static SANE_Status
read_imageheader (struct scanner *s)
{
  SANE_Status ret = SANE_STATUS_GOOD;

  unsigned char cmd[READ_len];
  unsigned char pay[SR_len_imageheader];
  size_t payLen = SR_len_imageheader;
  int pass = 0;

  /* read img header */
  DBG (10, "read_imageheader: start\n");

  memset(cmd,0,READ_len);
  set_SCSI_opcode(cmd,READ_code);
  set_SR_datatype_code(cmd,SR_datatype_imageheader);
  set_SR_xfer_length(cmd,SR_len_imageheader);

  while (pass++ < 1000){

    DBG (15, "read_imageheader: pass %d\n", pass);

    payLen = SR_len_imageheader;

    ret = do_cmd (
      s, 1, 0,
      cmd, READ_len,
      NULL, 0,
      pay, &payLen
    );

    DBG (15, "read_imageheader: pass status %d\n", ret);

    if(ret != SANE_STATUS_DEVICE_BUSY){
      break;
    }

    usleep(50000);
  }

  if (ret == SANE_STATUS_GOOD){

    DBG (15, "image header:\n");

    DBG (15, "  bytes: %d\n",get_SR_ih_image_length(pay));
    s->i_bytes = get_SR_ih_image_length(pay);

    DBG (15, "  id: %d\n",get_SR_ih_image_id(pay));
    s->i_id = get_SR_ih_image_id(pay);

    DBG (15, "  dpi: %d\n",get_SR_ih_resolution(pay));
    s->i_dpi = get_SR_ih_resolution(pay);

    DBG (15, "  tlx: %d\n",get_SR_ih_ulx(pay));
    s->i_tlx = get_SR_ih_ulx(pay);

    DBG (15, "  tly: %d\n",get_SR_ih_uly(pay));
    s->i_tly = get_SR_ih_uly(pay);

    DBG (15, "  width: %d\n",get_SR_ih_width(pay));
    s->i_width = get_SR_ih_width(pay);

    DBG (15, "  length: %d\n",get_SR_ih_length(pay));
    s->i_length = get_SR_ih_length(pay);

    DBG (15, "  bpp: %d\n",get_SR_ih_bpp(pay));
    s->i_bpp = get_SR_ih_bpp(pay);

    DBG (15, "  comp: %d\n",get_SR_ih_comp_type(pay));
    s->i_compr = get_SR_ih_comp_type(pay);

    /*FIXME: there are a lot more of these?*/
  }

  DBG (10, "read_imageheader: finish %d\n", ret);

  return ret;
}

/*
 * Called by SANE to read data.
 *
 * From the SANE spec:
 * This function is used to read image data from the device
 * represented by handle h.  Argument buf is a pointer to a memory
 * area that is at least maxlen bytes long.  The number of bytes
 * returned is stored in *len. A backend must set this to zero when
 * the call fails (i.e., when a status other than SANE_STATUS_GOOD is
 * returned).
 *
 * When the call succeeds, the number of bytes returned can be
 * anywhere in the range from 0 to maxlen bytes.
 */
SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len)
{
  struct scanner *s = (struct scanner *) handle;
  SANE_Status ret=0;

  DBG (10, "sane_read: start\n");

  *len=0;

  /* maybe cancelled? */
  if(!s->started){
      DBG (5, "sane_read: not started, call sane_start\n");
      return SANE_STATUS_CANCELLED;
  }

  /* sane_start required between images */
  if(s->bytes_tx == s->i_bytes){
      DBG (15, "sane_read: returning eof\n");
      return SANE_STATUS_EOF;
  }

  if(s->i_bytes > s->bytes_rx ){
      ret = read_from_scanner(s);
      if(ret){
          DBG(5,"sane_read: returning %d\n",ret);
          return ret;
      }
  }

  /* copy a block from buffer to frontend */
  ret = read_from_buffer(s,buf,max_len,len);

  DBG (10, "sane_read: finish\n");

  return ret;
}

static SANE_Status
read_from_scanner(struct scanner *s)
{
    SANE_Status ret=SANE_STATUS_GOOD;
    int bytes = s->buffer_size;
    int remain = s->i_bytes - s->bytes_rx;
    unsigned char * buf;
    size_t inLen = 0;

    unsigned char cmd[READ_len];
    int cmdLen=READ_len;

    DBG (10, "read_from_scanner: start\n");

    memset(cmd, 0, cmdLen);
    set_SCSI_opcode(cmd, READ_code);

    /* figure out the max amount to transfer */
    if(bytes > remain){
        bytes = remain;
    }

    DBG(15, "read_from_scanner: to:%d rx:%d re:%d bu:%d pa:%d\n",
      s->i_bytes, s->bytes_rx, remain, s->buffer_size, bytes);

    if(ret){
        return ret;
    }

    inLen = bytes;

    buf = malloc(bytes);
    if(!buf){
        DBG(5, "read_from_scanner: not enough mem for buffer: %d\n",bytes);
        return SANE_STATUS_NO_MEM;
    }

    set_SR_datatype_code (cmd, SR_datatype_imagedata);
    set_SR_xfer_length (cmd, bytes);

    ret = do_cmd (
      s, 1, 0,
      cmd, cmdLen,
      NULL, 0,
      buf, &inLen
    );

    if (ret == SANE_STATUS_GOOD) {
        DBG(15, "read_from_scanner: got GOOD, returning GOOD\n");
    }
    else if (ret == SANE_STATUS_EOF) {
        DBG(15, "read_from_scanner: got EOF, finishing\n");
    }
    else if (ret == SANE_STATUS_DEVICE_BUSY) {
        DBG(5, "read_from_scanner: got BUSY, returning GOOD\n");
        inLen = 0;
        ret = SANE_STATUS_GOOD;
    }
    else {
        DBG(5, "read_from_scanner: error reading data block status = %d\n",ret);
        inLen = 0;
    }

    if(inLen){
        copy_buffer (s, buf, inLen);
    }

    free(buf);

    if(ret == SANE_STATUS_EOF){
      DBG (5, "read_from_scanner: unexpected EOF, shortening image\n");
      s->i_bytes = s->bytes_rx;
      ret = SANE_STATUS_GOOD;
    }

    DBG (10, "read_from_scanner: finish\n");

    return ret;
}

static SANE_Status
copy_buffer(struct scanner *s, unsigned char * buf, int len)
{
  SANE_Status ret=SANE_STATUS_GOOD;

  DBG (10, "copy_buffer: start\n");

  memcpy(s->buffer+s->bytes_rx,buf,len);
  s->bytes_rx += len;

  DBG (10, "copy_buffer: finish\n");

  return ret;
}

static SANE_Status
read_from_buffer(struct scanner *s, SANE_Byte * buf,
  SANE_Int max_len, SANE_Int * len)
{
    SANE_Status ret=SANE_STATUS_GOOD;
    int bytes = max_len;
    int remain = s->bytes_rx - s->bytes_tx;

    DBG (10, "read_from_buffer: start\n");

    /* figure out the max amount to transfer */
    if(bytes > remain){
        bytes = remain;
    }

    *len = bytes;

    DBG(15, "read_from_buffer: to:%d tx:%d re:%d bu:%d pa:%d\n",
      s->i_bytes, s->bytes_tx, remain, max_len, bytes);

    /*FIXME this needs to timeout eventually */
    if(!bytes){
        DBG(5,"read_from_buffer: nothing to do\n");
        return SANE_STATUS_GOOD;
    }

    memcpy(buf,s->buffer+s->bytes_tx,bytes);

    s->bytes_tx += *len;

    DBG (10, "read_from_buffer: finish\n");

    return ret;
}


/*
 * @@ Section 4 - SANE cleanup functions
 */
/*
 * Cancels a scan.
 *
 * It has been said on the mailing list that sane_cancel is a bit of a
 * misnomer because it is routinely called to signal the end of a
 * batch - quoting David Mosberger-Tang:
 *
 * > In other words, the idea is to have sane_start() be called, and
 * > collect as many images as the frontend wants (which could in turn
 * > consist of multiple frames each as indicated by frame-type) and
 * > when the frontend is done, it should call sane_cancel().
 * > Sometimes it's better to think of sane_cancel() as "sane_stop()"
 * > but that name would have had some misleading connotations as
 * > well, that's why we stuck with "cancel".
 *
 * The current consensus regarding duplex and ADF scans seems to be
 * the following call sequence: sane_start; sane_read (repeat until
 * EOF); sane_start; sane_read...  and then call sane_cancel if the
 * batch is at an end. I.e. do not call sane_cancel during the run but
 * as soon as you get a SANE_STATUS_NO_DOCS.
 *
 * From the SANE spec:
 * This function is used to immediately or as quickly as possible
 * cancel the currently pending operation of the device represented by
 * handle h.  This function can be called at any time (as long as
 * handle h is a valid handle) but usually affects long-running
 * operations only (such as image is acquisition). It is safe to call
 * this function asynchronously (e.g., from within a signal handler).
 * It is important to note that completion of this operation does not
 * imply that the currently pending operation has been cancelled. It
 * only guarantees that cancellation has been initiated. Cancellation
 * completes only when the cancelled call returns (typically with a
 * status value of SANE_STATUS_CANCELLED).  Since the SANE API does
 * not require any other operations to be re-entrant, this implies
 * that a frontend must not call any other operation until the
 * cancelled operation has returned.
 */
void
sane_cancel (SANE_Handle handle)
{
  DBG (10, "sane_cancel: start\n");
  do_cancel ((struct scanner *) handle);
  DBG (10, "sane_cancel: finish\n");
}

/*
 * Performs cleanup.
 * FIXME: do better cleanup if scanning is ongoing...
 */
static SANE_Status
do_cancel (struct scanner *s)
{
  DBG (10, "do_cancel: start\n");

  s->started = 0;

  DBG (10, "do_cancel: finish\n");

  return SANE_STATUS_CANCELLED;
}

/*
 * Ends use of the scanner.
 *
 * From the SANE spec:
 * This function terminates the association between the device handle
 * passed in argument h and the device it represents. If the device is
 * presently active, a call to sane_cancel() is performed first. After
 * this function returns, handle h must not be used anymore.
 */
void
sane_close (SANE_Handle handle)
{
  DBG (10, "sane_close: start\n");

  do_cancel((struct scanner *) handle);
  disconnect_fd((struct scanner *) handle);

  DBG (10, "sane_close: finish\n");
}

static SANE_Status
disconnect_fd (struct scanner *s)
{
  DBG (10, "disconnect_fd: start\n");

  if(s->fd > -1){
    DBG (15, "disconnecting scsi device\n");
    sanei_scsi_close (s->fd);
    s->fd = -1;
  }

  DBG (10, "disconnect_fd: finish\n");

  return SANE_STATUS_GOOD;
}

/*
 * Terminates the backend.
 *
 * From the SANE spec:
 * This function must be called to terminate use of a backend. The
 * function will first close all device handles that still might be
 * open (it is recommended to close device handles explicitly through
 * a call to sane_close(), but backends are required to release all
 * resources upon a call to this function). After this function
 * returns, no function other than sane_init() may be called
 * (regardless of the status value returned by sane_exit(). Neglecting
 * to call this function may result in some resources not being
 * released properly.
 */
void
sane_exit (void)
{
  struct scanner *dev, *next;

  DBG (10, "sane_exit: start\n");

  for (dev = scanner_devList; dev; dev = next) {
      disconnect_fd(dev);
      next = dev->next;
      free (dev->device_name);
      free (dev);
  }

  if (sane_devArray)
    free (sane_devArray);

  scanner_devList = NULL;
  sane_devArray = NULL;

  DBG (10, "sane_exit: finish\n");
}


/*
 * @@ Section 5 - misc helper functions
 */
/*
 * Called by the SANE SCSI core on device errors
 * parses the request sense return data buffer,
 * decides the best SANE_Status for the problem
 * and produces debug msgs
 */
static SANE_Status
sense_handler (int fd, unsigned char * sensed_data, void *arg)
{
  struct scanner *s = arg;
  unsigned int ili = get_RS_ILI (sensed_data);
  unsigned int sk = get_RS_sense_key (sensed_data);
  unsigned int asc = get_RS_ASC (sensed_data);
  unsigned int ascq = get_RS_ASCQ (sensed_data);

  DBG (5, "sense_handler: start\n");

  /* kill compiler warning */
  fd = fd;

  /* save for later */
  s->rs_info = get_RS_information (sensed_data);

  DBG (5, "SK=%#02x, ASC=%#02x, ASCQ=%#02x, ILI=%d, info=%#08lx\n",
       sk, asc, ascq, ili, (unsigned long)s->rs_info);

  switch (sk) {

    /* no sense */
    case 0x0:
      if (0x00 != asc) {
        DBG (5, "No sense: unknown asc\n");
        return SANE_STATUS_IO_ERROR;
      }
      if (0x00 != ascq) {
        DBG (5, "No sense: unknown ascq\n");
        return SANE_STATUS_IO_ERROR;
      }
      if (ili) {
        DBG (5, "No sense: ILI set\n");
        return SANE_STATUS_EOF;
      }
      DBG  (5, "No sense: ready\n");
      return SANE_STATUS_GOOD;

    /* not ready */
    case 0x2:
      if (0x80 != asc) {
        DBG (5, "Not ready: unknown asc\n");
        return SANE_STATUS_IO_ERROR;
      }
      if (0x00 != ascq) {
        DBG (5, "Not ready: unknown ascq\n");
        return SANE_STATUS_IO_ERROR;
      }
      DBG (5, "Not ready: end of job\n");
      return SANE_STATUS_NO_DOCS;
      break;

    /* hardware error */
    case 0x4:
      if (0x3b != asc) {
        DBG (5, "Hardware error: unknown asc\n");
        return SANE_STATUS_IO_ERROR;
      }
      if (0x05 == ascq) {
        DBG (5, "Hardware error: paper jam\n");
        return SANE_STATUS_JAMMED;
      }
      if (0x80 == ascq) {
        DBG (5, "Hardware error: multi-feed\n");
        return SANE_STATUS_JAMMED;
      }
      DBG (5, "Hardware error: unknown ascq\n");
      return SANE_STATUS_IO_ERROR;
      break;

    /* illegal request */
    case 0x5:
      if (asc != 0x20 && asc != 0x24 && asc != 0x25 && asc != 0x26
      && asc != 0x83 && asc != 0x8f) {
        DBG (5, "Illegal request: unknown asc\n");
        return SANE_STATUS_IO_ERROR;
      }
      if (0x20 == asc && 0x00 == ascq) {
        DBG  (5, "Illegal request: invalid opcode\n");
        return SANE_STATUS_INVAL;
      }
      if (0x24 == asc && 0x00 == ascq) {
        DBG  (5, "Illegal request: invalid field in CDB\n");
        return SANE_STATUS_INVAL;
      }
      if (0x25 == asc && 0x00 == ascq) {
        DBG  (5, "Illegal request: invalid LUN\n");
        return SANE_STATUS_INVAL;
      }
      if (0x26 == asc && 0x00 == ascq) {
        DBG  (5, "Illegal request: invalid field in params\n");
        return SANE_STATUS_INVAL;
      }
      if (0x83 == asc && 0x00 == ascq) {
        DBG  (5, "Illegal request: command failed, check log\n");
        return SANE_STATUS_INVAL;
      }
      if (0x83 == asc && 0x01 == ascq) {
        DBG  (5, "Illegal request: command failed, invalid state\n");
        return SANE_STATUS_INVAL;
      }
      if (0x83 == asc && 0x02 == ascq) {
        DBG  (5, "Illegal request: command failed, critical error\n");
        return SANE_STATUS_INVAL;
      }
      if (0x8f == asc && 0x00 == ascq) {
        DBG  (5, "Illegal request: no image\n");
        return SANE_STATUS_DEVICE_BUSY;
      }
      DBG  (5, "Illegal request: unknown asc/ascq\n");
      return SANE_STATUS_IO_ERROR;
      break;

    /* unit attention */
    case 0x6:
      if (asc != 0x29 && asc != 0x80) {
        DBG (5, "Unit attention: unknown asc\n");
        return SANE_STATUS_IO_ERROR;
      }
      if (0x29 == asc && 0x60 == ascq) {
        DBG  (5, "Unit attention: device reset\n");
        return SANE_STATUS_GOOD;
      }
      if (0x80 == asc && 0x00 == ascq) {
        DBG  (5, "Unit attention: Energy Star warm up\n");
        return SANE_STATUS_DEVICE_BUSY;
      }
      if (0x80 == asc && 0x01 == ascq) {
        DBG  (5, "Unit attention: lamp warm up for scan\n");
        return SANE_STATUS_DEVICE_BUSY;
      }
      if (0x80 == asc && 0x02 == ascq) {
        DBG  (5, "Unit attention: lamp warm up for cal\n");
        return SANE_STATUS_DEVICE_BUSY;
      }
      if (0x80 == asc && 0x04 == ascq) {
        DBG  (5, "Unit attention: calibration failed\n");
        return SANE_STATUS_INVAL;
      }
      DBG  (5, "Unit attention: unknown asc/ascq\n");
      return SANE_STATUS_IO_ERROR;
      break;

    /* ia overflow */
    case 0x9:
      if (0x80 == asc && 0x00 == ascq) {
        DBG  (5, "IA overflow: IA field overflow\n");
        return SANE_STATUS_IO_ERROR;
      }
      DBG  (5, "IA overflow: unknown asc/ascq\n");
      return SANE_STATUS_IO_ERROR;
      break;

    /* volume overflow */
    case 0xd:
      if (0x80 == asc && 0x00 == ascq) {
        DBG  (5, "Volume overflow: Image buffer full\n");
        return SANE_STATUS_IO_ERROR;
      }
      DBG  (5, "Volume overflow: unknown asc/ascq\n");
      return SANE_STATUS_IO_ERROR;
      break;

    default:
      DBG (5, "Unknown Sense Code\n");
      return SANE_STATUS_IO_ERROR;
  }

  DBG (5, "sense_handler: should never happen!\n");

  return SANE_STATUS_IO_ERROR;
}

/*
SANE_Status
do_rs(scanner * s)
{
  SANE_Status ret;
  unsigned char cmd[REQUEST_SENSE_len];
  size_t cmdLen = REQUEST_SENSE_len;

  DBG (10, "do_rs: start\n");

  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,REQUEST_SENSE_code);
  set_SR_datatype_code(cmd,SR_datatype_random);
  set_SR_datatype_qual(cmd,SR_qual_end);

  ret = do_cmd (
    s, 1, 0,
    cmd, cmdLen,
    NULL, 0,
    NULL, NULL
  );

  while(ret == SANE_STATUS_DEVICE_BUSY){
    ret = run_rs(s);
  }

  DBG (10, "do_rs: finish\n");

  return SANE_STATUS_GOOD;
}
*/

SANE_Status
do_cmd(struct scanner *s, int runRS, int shortTime,
 unsigned char * cmdBuff, size_t cmdLen,
 unsigned char * outBuff, size_t outLen,
 unsigned char * inBuff, size_t * inLen
)
{
  SANE_Status ret = SANE_STATUS_GOOD;

  /*shut up compiler*/
  runRS=runRS;
  shortTime=shortTime;

  DBG(10, "do_cmd: start\n");

  DBG(25, "cmd: writing %d bytes\n", (int)cmdLen);
  hexdump(30, "cmd: >>", cmdBuff, cmdLen);

  if(outBuff && outLen){
    DBG(25, "out: writing %d bytes\n", (int)outLen);
    hexdump(30, "out: >>", outBuff, outLen);
  }
  if (inBuff && inLen){
    DBG(25, "in: reading %d bytes\n", (int)*inLen);
  }

  ret = sanei_scsi_cmd2(s->fd, cmdBuff, cmdLen, outBuff, outLen, inBuff, inLen);

  if(ret != SANE_STATUS_GOOD && ret != SANE_STATUS_EOF){
    DBG(5,"do_cmd: return '%s'\n",sane_strstatus(ret));
    return ret;
  }

  /* FIXME: should we look at s->rs_info here? */
  if (inBuff && inLen){
    hexdump(30, "in: <<", inBuff, *inLen);
    DBG(25, "in: read %d bytes\n", (int)*inLen);
  }

  DBG(10, "do_cmd: finish\n");

  return ret;
}

#if 0 /* unused */
static SANE_Status
wait_scanner(struct scanner *s)
{
  int ret;

  unsigned char cmd[TEST_UNIT_READY_len];
  size_t cmdLen = TEST_UNIT_READY_len;

  DBG (10, "wait_scanner: start\n");

  memset(cmd,0,cmdLen);
  set_SCSI_opcode(cmd,TEST_UNIT_READY_code);

  ret = do_cmd (
    s, 0, 1,
    cmd, cmdLen,
    NULL, 0,
    NULL, NULL
  );

  if (ret != SANE_STATUS_GOOD) {
    DBG(5,"WARNING: Brain-dead scanner. Hitting with stick\n");
    ret = do_cmd (
      s, 0, 1,
      cmd, cmdLen,
      NULL, 0,
      NULL, NULL
    );
  }
  if (ret != SANE_STATUS_GOOD) {
    DBG(5,"WARNING: Brain-dead scanner. Hitting with stick again\n");
    ret = do_cmd (
      s, 0, 1,
      cmd, cmdLen,
      NULL, 0,
      NULL, NULL
    );
  }

  if (ret != SANE_STATUS_GOOD) {
    DBG (5, "wait_scanner: error '%s'\n", sane_strstatus (ret));
  }

  DBG (10, "wait_scanner: finish\n");

  return ret;
}
#endif /* 0 - unused */

/**
 * Convenience method to determine longest string size in a list.
 */
static size_t
maxStringSize (const SANE_String_Const strings[])
{
  size_t size, max_size = 0;
  int i;

  for (i = 0; strings[i]; ++i) {
    size = strlen (strings[i]) + 1;
    if (size > max_size)
      max_size = size;
  }

  return max_size;
}

/**
 * Prints a hex dump of the given buffer onto the debug output stream.
 */
static void
hexdump (int level, char *comment, unsigned char *p, int l)
{
  int i;
  char line[128];
  char *ptr;

  if(DBG_LEVEL < level)
    return;

  DBG (level, "%s\n", comment);
  ptr = line;
  for (i = 0; i < l; i++, p++)
    {
      if ((i % 16) == 0)
        {
          if (ptr != line)
            {
              *ptr = '\0';
              DBG (level, "%s\n", line);
              ptr = line;
            }
          sprintf (ptr, "%3.3x:", i);
          ptr += 4;
        }
      sprintf (ptr, " %2.2x", *p);
      ptr += 3;
    }
  *ptr = '\0';
  DBG (level, "%s\n", line);
}

/**
 * An advanced method we don't support but have to define.
 */
SANE_Status
sane_set_io_mode (SANE_Handle h, SANE_Bool non_blocking)
{
  DBG (10, "sane_set_io_mode\n");
  DBG (15, "%d %p\n", non_blocking, h);
  return SANE_STATUS_UNSUPPORTED;
}

/**
 * An advanced method we don't support but have to define.
 */
SANE_Status
sane_get_select_fd (SANE_Handle h, SANE_Int *fdp)
{
  DBG (10, "sane_get_select_fd\n");
  DBG (15, "%p %d\n", h, *fdp);
  return SANE_STATUS_UNSUPPORTED;
}