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 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815
|
/* sane - Scanner Access Now Easy.
Copyright (C) 2007-2013 stef.dev@free.fr
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
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.
--------------------------------------------------------------------------
SANE FLOW DIAGRAM
- sane_init() : initialize backend, attach scanners
. - sane_get_devices() : query list of scanner devices
. - sane_open() : open a particular scanner device, adding a handle
to the opened 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_start() : start image acquisition
. . - sane_get_parameters() : returns actual scan parameters
. . - sane_read() : read image data
. .
. . - sane_cancel() : cancel operation
. - sane_close() : close opened scanner device, freeing scanner handle
- sane_exit() : terminate use of backend, freeing all resources for attached
devices
BACKEND USB locking policy
- interface is released at the end of sane_open() to allow
multiple usage by fronteds
- if free, interface is claimed at sane_start(), and held until
sane_cancel() is called
- if free interface is claimed then released just during the access
to buttons registers
*/
/* ------------------------------------------------------------------------- */
#include "../include/sane/config.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_LIBC_H
# include <libc.h>
#endif
#include "../include/sane/sane.h"
#include "../include/sane/sanei_usb.h"
#include "../include/sane/saneopts.h"
#include "../include/sane/sanei_config.h"
#include "../include/sane/sanei_backend.h"
#define DARK_TARGET 3.1 /* 3.5 target average for dark calibration */
#define DARK_MARGIN 0.3 /* acceptable margin for dark average */
#define OFFSET_TARGET 3.5 /* target average for offset calibration */
#define OFFSET_MARGIN 0.3 /* acceptable margin for offset average */
#define RED_GAIN_TARGET 170 /* target average for gain calibration for blue color */
#define GREEN_GAIN_TARGET 170 /* target average for gain calibration for blue color */
#define BLUE_GAIN_TARGET 180 /* target average for gain calibration for blue color */
#define GAIN_MARGIN 2 /* acceptable margin for gain average */
#define MARGIN_LEVEL 128 /* white level for margin detection */
/* width used for calibration */
#define CALIBRATION_WIDTH 637
/* data size for calibration: one RGB line*/
#define CALIBRATION_SIZE CALIBRATION_WIDTH*3
/* #define FAST_INIT 1 */
#define BUILD 2401
#define MOVE_DPI 100
#include "rts8891.h"
#include "rts88xx_lib.h"
#include "rts8891_low.c"
#include "rts8891_devices.c"
#define DEEP_DEBUG 1
/**
* Pointer to the first Rts8891_Session in the linked list of
* opened device. Sessions are inserted here on sane_open() and
* removed on sane_close().
*/
static Rts8891_Session *first_handle = NULL;
/* pointer to the first device attached to the backend
* the same device may be opened several time
* entry are inserted here by attach_rts8891 */
static Rts8891_Device *first_device = NULL;
static SANE_Int num_devices = 0;
/*
* needed by sane_get_devices
*/
static const SANE_Device **devlist = 0;
static SANE_String_Const mode_list[] = {
SANE_VALUE_SCAN_MODE_COLOR,
SANE_VALUE_SCAN_MODE_GRAY,
SANE_VALUE_SCAN_MODE_LINEART,
0
};
static SANE_Range x_range = {
SANE_FIX (0.0), /* minimum */
SANE_FIX (216.0), /* maximum */
SANE_FIX (0.0) /* quantization */
};
static SANE_Range y_range = {
SANE_FIX (0.0), /* minimum */
SANE_FIX (299.0), /* maximum */
SANE_FIX (0.0) /* no quantization */
};
/* model number ranges from 0 to 2, must be changed if
* Rts8891_USB_Device_Entry changes */
static const SANE_Range model_range = {
0, /* minimum */
2, /* maximum */
0 /* no quantization */
};
/* sensor number ranges from 0 to SENSOR_TYPE_MAX, must be changed if */
static const SANE_Range sensor_range = {
0, /* minimum */
SENSOR_TYPE_MAX, /* maximum */
0 /* no quantization */
};
static const SANE_Range u8_range = {
0, /* minimum */
255, /* maximum */
0 /* no quantization */
};
static const SANE_Range threshold_percentage_range = {
SANE_FIX (0), /* minimum */
SANE_FIX (100), /* maximum */
SANE_FIX (1) /* quantization */
};
static size_t
max_string_size (const SANE_String_Const strings[])
{
size_t size, max_size = 0;
SANE_Int i;
for (i = 0; strings[i]; ++i)
{
size = strlen (strings[i]) + 1;
if (size > max_size)
max_size = size;
}
return max_size;
}
/**> placeholders for decoded configuration values */
static Rts8891_Config rtscfg;
/* ------------------------------------------------------------------------- */
static SANE_Status probe_rts8891_devices (void);
static SANE_Status config_attach_rts8891 (SANEI_Config * config,
const char *devname);
static SANE_Status attach_rts8891 (const char *name);
static SANE_Status set_lamp_brightness (struct Rts8891_Device *dev,
int level);
static SANE_Status init_options (struct Rts8891_Session *session);
#ifndef FAST_INIT
static SANE_Status init_device (struct Rts8891_Device *dev);
#else
static SANE_Status detect_device (struct Rts8891_Device *dev);
static SANE_Status initialize_device (struct Rts8891_Device *dev);
#endif
static SANE_Status init_lamp (struct Rts8891_Device *dev);
static SANE_Status find_origin (struct Rts8891_Device *dev,
SANE_Bool * changed);
static SANE_Status find_margin (struct Rts8891_Device *dev);
static SANE_Status dark_calibration (struct Rts8891_Device *dev, int mode,
int light);
static SANE_Status gain_calibration (struct Rts8891_Device *dev, int mode,
int light);
static SANE_Status offset_calibration (struct Rts8891_Device *dev, int mode,
int light);
static SANE_Status shading_calibration (struct Rts8891_Device *dev,
SANE_Bool color, int mode, int light);
static SANE_Status send_calibration_data (struct Rts8891_Session *session);
static SANE_Status write_scan_registers (struct Rts8891_Session *session);
static SANE_Status read_data (struct Rts8891_Session *session,
SANE_Byte * dest, SANE_Int length);
static SANE_Status compute_parameters (struct Rts8891_Session *session);
static SANE_Status move_to_scan_area (struct Rts8891_Session *session);
static SANE_Status park_head (struct Rts8891_Device *dev, SANE_Bool wait);
static SANE_Status update_button_status (struct Rts8891_Session *session);
static SANE_Status set_lamp_state (struct Rts8891_Session *session, int on);
/* ------------------------------------------------------------------------- */
/* writes gray data to a pnm file */
static void
write_gray_data (unsigned char *image, char *name, SANE_Int width,
SANE_Int height)
{
FILE *fdbg = NULL;
fdbg = fopen (name, "wb");
if (fdbg == NULL)
return;
fprintf (fdbg, "P5\n%d %d\n255\n", width, height);
fwrite (image, width, height, fdbg);
fclose (fdbg);
}
/* ------------------------------------------------------------------------- */
/* writes rgb data to a pnm file */
static void
write_rgb_data (char *name, unsigned char *image, SANE_Int width,
SANE_Int height)
{
FILE *fdbg = NULL;
fdbg = fopen (name, "wb");
if (fdbg == NULL)
return;
fprintf (fdbg, "P6\n%d %d\n255\n", width, height);
fwrite (image, width * 3, height, fdbg);
fclose (fdbg);
}
/* ------------------------------------------------------------------------- */
/*
* SANE Interface
*/
/**
* 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)
{
SANE_Status status;
authorize = authorize; /* get rid of compiler warning */
/* init ASIC libraries */
sanei_rts88xx_lib_init ();
rts8891_low_init ();
/* init backend debug */
DBG_INIT ();
DBG (DBG_info, "SANE Rts8891 backend version %d.%d-%d\n",
SANE_CURRENT_MAJOR, V_MINOR, BUILD);
DBG (DBG_proc, "sane_init: start\n");
if (version_code)
*version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR, BUILD);
/* cold-plugging case : probe for already plugged devices */
status = probe_rts8891_devices ();
DBG (DBG_proc, "sane_init: exit\n");
return status;
}
/**
* 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.
*/
SANE_Status
sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
{
SANE_Int dev_num;
struct Rts8891_Device *device;
SANE_Device *sane_device;
int i;
DBG (DBG_proc, "sane_get_devices: start: local_only = %s\n",
local_only == SANE_TRUE ? "true" : "false");
/* hot-plugging case : probe for devices plugged since sane_init called */
probe_rts8891_devices ();
/* reset devlist first if needed */
if (devlist)
{
for (i = 0; i < num_devices; i++)
free ((char *) devlist[i]);
free (devlist);
devlist = NULL;
}
devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
if (!devlist)
return SANE_STATUS_NO_MEM;
*device_list = devlist;
dev_num = 0;
/* we build a list of SANE_Device from the list of attached devices */
for (device = first_device; dev_num < num_devices; device = device->next)
{
sane_device = malloc (sizeof (*sane_device));
if (!sane_device)
return SANE_STATUS_NO_MEM;
sane_device->name = device->file_name;
sane_device->vendor = device->model->vendor;
sane_device->model = device->model->product;
sane_device->type = device->model->type;
devlist[dev_num++] = sane_device;
}
devlist[dev_num++] = 0;
*device_list = devlist;
DBG (DBG_proc, "sane_get_devices: exit\n");
return SANE_STATUS_GOOD;
}
/**
* Called to establish connection with the session. This function will
* also establish meaningful defaults and initialize the options.
*
* 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).
* TODO handle SANE_STATUS_BUSY
*/
SANE_Status
sane_open (SANE_String_Const name, SANE_Handle * handle)
{
struct Rts8891_Session *session = NULL;
struct Rts8891_Device *device = NULL;
SANE_Status status;
DBG (DBG_proc, "sane_open: start (devicename=%s)\n", name);
if (name[0] == 0 || strncmp (name, "rts8891", 7) == 0)
{
DBG (DBG_info, "sane_open: no device requested, using default\n");
if (first_device)
{
device = first_device;
DBG (DBG_info, "sane_open: device %s used as default device\n",
device->file_name);
}
}
else
{
DBG (DBG_info, "sane_open: device %s requested\n", name);
/* walk the device list until we find a matching name */
device = first_device;
while (device && strcmp (device->file_name, name) != 0)
{
DBG (DBG_info, "sane_open: device %s doesn't match\n",
device->file_name);
device = device->next;
}
}
/* check wether we have found a match or reach the end of the device list */
if (!device)
{
DBG (DBG_info, "sane_open: no device found\n");
return SANE_STATUS_INVAL;
}
/* now we have a device, duplicate it and return it in handle */
DBG (DBG_info, "sane_open: device %s found\n", name);
if (device->model->flags & RTS8891_FLAG_UNTESTED)
{
DBG (DBG_error0,
"WARNING: Your scanner is not fully supported or at least \n");
DBG (DBG_error0,
" had only limited testing. Please be careful and \n");
DBG (DBG_error0, " report any failure/success to \n");
DBG (DBG_error0,
" sane-devel@lists.alioth.debian.org. Please provide as many\n");
DBG (DBG_error0,
" details as possible, e.g. the exact name of your\n");
DBG (DBG_error0, " scanner and what does (not) work.\n");
}
/* open USB link */
status = sanei_usb_open (device->file_name, &device->devnum);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_warn, "sane_open: couldn't open device `%s': %s\n",
device->file_name, sane_strstatus (status));
return status;
}
/* device initialization */
if (device->initialized == SANE_FALSE)
{
#ifdef FAST_INIT
status = detect_device (device);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "sane_open: detect_device failed\n");
DBG (DBG_proc, "sane_open: exit on error\n");
return status;
}
status = initialize_device (device);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "sane_open: initialize_device failed\n");
DBG (DBG_proc, "sane_open: exit on error\n");
return status;
}
#else
status = init_device (device);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "sane_open: init_device failed\n");
DBG (DBG_proc, "sane_open: exit on error\n");
return status;
}
device->initialized = SANE_TRUE;
#endif
}
/* prepare handle to return */
session = (Rts8891_Session *) malloc (sizeof (Rts8891_Session));
session->scanning = SANE_FALSE;
session->dev = device;
init_options (session);
session->scanning = SANE_FALSE;
session->non_blocking = SANE_FALSE;
*handle = session;
/* add the handle to the list */
session->next = first_handle;
first_handle = session;
/* release the interface to allow device sharing */
if (session->dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (device->devnum, 0);
}
DBG (DBG_proc, "sane_open: exit\n");
return SANE_STATUS_GOOD;
}
/**
* Set non blocking mode. In this mode, read return immediatly when
* no data is available, instead of polling the scanner.
*/
SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
Rts8891_Session *session = (Rts8891_Session *) handle;
DBG (DBG_proc, "sane_set_io_mode: start\n");
if (session->scanning != SANE_TRUE)
{
DBG (DBG_error, "sane_set_io_mode: called out of a scan\n");
return SANE_STATUS_INVAL;
}
session->non_blocking = non_blocking;
DBG (DBG_warn, "sane_set_io_mode: I/O mode set to %sblocking.\n",
non_blocking ? "non " : " ");
DBG (DBG_proc, "sane_set_io_mode: exit\n");
return SANE_STATUS_GOOD;
}
/**
* An advanced method we don't support but have to define.
*/
SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int * fdp)
{
DBG (DBG_proc, "sane_get_select_fd: start\n");
if(handle==0 || fdp==NULL)
{
return SANE_STATUS_INVAL;
}
DBG (DBG_warn, "sane_get_select_fd: unsupported ...\n");
DBG (DBG_proc, "sane_get_select_fd: exit\n");
return SANE_STATUS_UNSUPPORTED;
}
/**
* 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 Rts8891_Session *session = handle;
DBG (DBG_proc, "sane_get_option_descriptor: start\n");
if ((unsigned) option >= NUM_OPTIONS)
return NULL;
DBG (DBG_info, "sane_get_option_descriptor: \"%s\"\n",
session->opt[option].name);
DBG (DBG_proc, "sane_get_option_descriptor: exit\n");
return &(session->opt[option]);
}
/**
* sets automatic value for an option , called by sane_control_option after
* all checks have been done */
static SANE_Status
set_automatic_value (Rts8891_Session * s, int option, SANE_Int * myinfo)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Int i, min;
const SANE_Word *dpi_list;
switch (option)
{
case OPT_TL_X:
s->val[OPT_TL_X].w = x_range.min;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_TL_Y:
s->val[OPT_TL_Y].w = y_range.min;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_BR_X:
s->val[OPT_BR_X].w = x_range.max;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_BR_Y:
s->val[OPT_BR_Y].w = y_range.max;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_RESOLUTION:
/* we set up to the lowest available dpi value */
dpi_list = s->opt[OPT_RESOLUTION].constraint.word_list;
min = 65536;
for (i = 1; i < dpi_list[0]; i++)
{
if (dpi_list[i] < min)
min = dpi_list[i];
}
s->val[OPT_RESOLUTION].w = min;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_THRESHOLD:
s->val[OPT_THRESHOLD].w = SANE_FIX (50);
break;
case OPT_PREVIEW:
s->val[OPT_PREVIEW].w = SANE_FALSE;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_MODE:
if (s->val[OPT_MODE].s)
free (s->val[OPT_MODE].s);
s->val[OPT_MODE].s = strdup (mode_list[0]);
*myinfo |= SANE_INFO_RELOAD_OPTIONS;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_CUSTOM_GAMMA:
s->val[option].b = SANE_FALSE;
DISABLE (OPT_GAMMA_VECTOR);
DISABLE (OPT_GAMMA_VECTOR_R);
DISABLE (OPT_GAMMA_VECTOR_G);
DISABLE (OPT_GAMMA_VECTOR_B);
break;
case OPT_GAMMA_VECTOR:
case OPT_GAMMA_VECTOR_R:
case OPT_GAMMA_VECTOR_G:
case OPT_GAMMA_VECTOR_B:
if (s->dev->model->gamma != s->val[option].wa)
free (s->val[option].wa);
s->val[option].wa = s->dev->model->gamma;
break;
default:
DBG (DBG_warn, "set_automatic_value: can't set unknown option %d\n",
option);
}
return status;
}
/**
* sets an option , called by sane_control_option after all
* checks have been done */
static SANE_Status
set_option_value (Rts8891_Session * s, int option, void *val,
SANE_Int * myinfo)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Int i;
SANE_Word tmpw;
switch (option)
{
case OPT_TL_X:
case OPT_TL_Y:
case OPT_BR_X:
case OPT_BR_Y:
s->val[option].w = *(SANE_Word *) val;
/* we ensure geometry is coherent */
/* this happens when user drags TL corner right or below the BR point */
if (s->val[OPT_BR_Y].w < s->val[OPT_TL_Y].w)
{
tmpw = s->val[OPT_BR_Y].w;
s->val[OPT_BR_Y].w = s->val[OPT_TL_Y].w;
s->val[OPT_TL_Y].w = tmpw;
}
if (s->val[OPT_BR_X].w < s->val[OPT_TL_X].w)
{
tmpw = s->val[OPT_BR_X].w;
s->val[OPT_BR_X].w = s->val[OPT_TL_X].w;
s->val[OPT_TL_X].w = tmpw;
}
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_RESOLUTION:
case OPT_THRESHOLD:
case OPT_PREVIEW:
s->val[option].w = *(SANE_Word *) val;
*myinfo |= SANE_INFO_RELOAD_PARAMS;
break;
case OPT_MODE:
if (s->val[option].s)
free (s->val[option].s);
s->val[option].s = strdup (val);
if (strcmp (s->val[option].s, LINEART_MODE) == 0)
{
ENABLE (OPT_THRESHOLD);
}
else
{
DISABLE (OPT_THRESHOLD);
}
/* if custom gamma, toggle gamma table options according to the mode */
if (s->val[OPT_CUSTOM_GAMMA].b == SANE_TRUE)
{
if (strcmp (s->val[option].s, COLOR_MODE) == 0)
{
DISABLE (OPT_GAMMA_VECTOR);
ENABLE (OPT_GAMMA_VECTOR_R);
ENABLE (OPT_GAMMA_VECTOR_G);
ENABLE (OPT_GAMMA_VECTOR_B);
}
else
{
ENABLE (OPT_GAMMA_VECTOR);
DISABLE (OPT_GAMMA_VECTOR_R);
DISABLE (OPT_GAMMA_VECTOR_G);
DISABLE (OPT_GAMMA_VECTOR_B);
}
}
*myinfo |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
break;
case OPT_CUSTOM_GAMMA:
*myinfo |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
s->val[OPT_CUSTOM_GAMMA].b = *(SANE_Bool *) val;
if (s->val[OPT_CUSTOM_GAMMA].b == SANE_TRUE)
{
if (strcmp (s->val[OPT_MODE].s, COLOR_MODE) == 0)
{
DISABLE (OPT_GAMMA_VECTOR);
ENABLE (OPT_GAMMA_VECTOR_R);
ENABLE (OPT_GAMMA_VECTOR_G);
ENABLE (OPT_GAMMA_VECTOR_B);
}
else
{
ENABLE (OPT_GAMMA_VECTOR);
DISABLE (OPT_GAMMA_VECTOR_R);
DISABLE (OPT_GAMMA_VECTOR_G);
DISABLE (OPT_GAMMA_VECTOR_B);
}
}
else
{
DISABLE (OPT_GAMMA_VECTOR);
DISABLE (OPT_GAMMA_VECTOR_R);
DISABLE (OPT_GAMMA_VECTOR_G);
DISABLE (OPT_GAMMA_VECTOR_B);
}
break;
case OPT_GAMMA_VECTOR:
case OPT_GAMMA_VECTOR_R:
case OPT_GAMMA_VECTOR_G:
case OPT_GAMMA_VECTOR_B:
/* sanity checks */
for (i = 0; i < (int) (s->opt[option].size / sizeof (SANE_Word)); i++)
{
/* avoid 0xaa values since they will be problematic */
if (((SANE_Int *) val)[i] == 0xaa)
((SANE_Int *) val)[i] = 0xab;
}
/* free memory from previous set */
if (s->dev->model->gamma != s->val[option].wa)
free (s->val[option].wa);
/* then alloc memory */
s->val[option].wa = (SANE_Word *) malloc (256 * sizeof (SANE_Word));
if (s->val[option].wa == NULL)
{
s->val[option].wa = s->dev->model->gamma;
DBG (DBG_error0,
"set_option_value: not enough memory for %lu bytes!\n",
(u_long) (256 * sizeof (SANE_Word)));
return SANE_STATUS_NO_MEM;
}
/* data copy */
memcpy (s->val[option].wa, val, s->opt[option].size);
break;
case OPT_LAMP_ON:
return set_lamp_state (s, 1);
break;
case OPT_LAMP_OFF:
return set_lamp_state (s, 0);
break;
default:
DBG (DBG_warn, "set_option_value: can't set unknown option %d\n",
option);
}
return status;
}
/**
* gets an option , called by sane_control_option after all checks
* have been done */
static SANE_Status
get_option_value (Rts8891_Session * s, int option, void *val)
{
switch (option)
{
/* word or word equivalent options: */
case OPT_NUM_OPTS:
case OPT_RESOLUTION:
case OPT_PREVIEW:
case OPT_TL_X:
case OPT_TL_Y:
case OPT_BR_X:
case OPT_BR_Y:
case OPT_THRESHOLD:
case OPT_CUSTOM_GAMMA:
*(SANE_Word *) val = s->val[option].w;
break;
case OPT_BUTTON_1:
case OPT_BUTTON_2:
case OPT_BUTTON_3:
case OPT_BUTTON_4:
case OPT_BUTTON_5:
case OPT_BUTTON_6:
case OPT_BUTTON_7:
case OPT_BUTTON_8:
case OPT_BUTTON_9:
case OPT_BUTTON_10:
case OPT_BUTTON_11:
/* no button pressed by default */
*(SANE_Word *) val = SANE_FALSE;
if (option - OPT_BUTTON_1 > s->dev->model->buttons)
{
DBG (DBG_warn,
"get_option_value: invalid button option %d\n", option);
}
else
{
update_button_status (s);
/* copy the button state */
*(SANE_Word *) val = s->val[option].w;
/* clear the button state */
s->val[option].w = SANE_FALSE;
DBG (DBG_io,
"get_option_value: button option %d=%d\n", option,
*(SANE_Word *) val);
}
break;
/* string options: */
case OPT_MODE:
strcpy (val, s->val[option].s);
break;
case OPT_GAMMA_VECTOR:
case OPT_GAMMA_VECTOR_R:
case OPT_GAMMA_VECTOR_G:
case OPT_GAMMA_VECTOR_B:
memcpy (val, s->val[option].wa, s->opt[option].size);
break;
default:
DBG (DBG_warn, "get_option_value: can't get unknown option %d\n",
option);
}
return SANE_STATUS_GOOD;
}
/**
* 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.
* action is SANE_ACTION_GET_VALUE, SANE_ACTION_SET_VALUE or SANE_ACTION_SET_AUTO
*/
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
SANE_Action action, void *val, SANE_Int * info)
{
Rts8891_Session *s = handle;
SANE_Status status;
SANE_Word cap;
SANE_Int myinfo = 0;
DBG (DBG_io2,
"sane_control_option: start: action = %s, option = %s (%d)\n",
(action == SANE_ACTION_GET_VALUE) ? "get" : (action ==
SANE_ACTION_SET_VALUE) ?
"set" : (action == SANE_ACTION_SET_AUTO) ? "set_auto" : "unknown",
s->opt[option].name, option);
if (info)
*info = 0;
/* do checks before trying to apply action */
if (s->scanning)
{
DBG (DBG_warn, "sane_control_option: don't call this function while "
"scanning (option = %s (%d))\n", s->opt[option].name, option);
return SANE_STATUS_DEVICE_BUSY;
}
/* option must be within existing range */
if (option >= NUM_OPTIONS || option < 0)
{
DBG (DBG_warn,
"sane_control_option: option %d >= NUM_OPTIONS || option < 0\n",
option);
return SANE_STATUS_INVAL;
}
/* don't access an inactive option */
cap = s->opt[option].cap;
if (!SANE_OPTION_IS_ACTIVE (cap))
{
DBG (DBG_warn, "sane_control_option: option %d is inactive\n", option);
return SANE_STATUS_INVAL;
}
/* now checks have been done, apply action */
switch (action)
{
case SANE_ACTION_GET_VALUE:
status = get_option_value (s, option, val);
break;
case SANE_ACTION_SET_VALUE:
if (!SANE_OPTION_IS_SETTABLE (cap))
{
DBG (DBG_warn, "sane_control_option: option %d is not settable\n",
option);
return SANE_STATUS_INVAL;
}
status = sanei_constrain_value (s->opt + option, val, &myinfo);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_warn,
"sane_control_option: sanei_constrain_value returned %s\n",
sane_strstatus (status));
return status;
}
/* return immediatly if no change */
if (s->opt[option].type == SANE_TYPE_INT
&& *(SANE_Word *) val == s->val[option].w)
{
status = SANE_STATUS_GOOD;
}
else
{ /* apply change */
status = set_option_value (s, option, val, &myinfo);
}
break;
case SANE_ACTION_SET_AUTO:
/* sets automatic values */
if (!(cap & SANE_CAP_AUTOMATIC))
{
DBG (DBG_warn,
"sane_control_option: option %d is not autosettable\n",
option);
return SANE_STATUS_INVAL;
}
status = set_automatic_value (s, option, &myinfo);
break;
default:
DBG (DBG_error, "sane_control_option: invalid action %d\n", action);
status = SANE_STATUS_INVAL;
break;
}
if (info)
*info = myinfo;
DBG (DBG_io2, "sane_control_option: exit\n");
return status;
}
/**
* returns the name of the sensor
* @param sensor sensor numnber
* @return string holding the name of the sensor
*/
static char *sensor_name (int sensor)
{
switch (sensor)
{
case SENSOR_TYPE_BARE:
return "SENSOR_TYPE_BARE";
case SENSOR_TYPE_XPA:
return "SENSOR_TYPE_XPA";
case SENSOR_TYPE_4400:
return "SENSOR_TYPE_4400";
case SENSOR_TYPE_4400_BARE:
return "SENSOR_TYPE_4400_BARE";
default:
return "BOGUS";
}
}
/**
* Called by SANE when a page acquisition operation is to be started.
* @param handle opaque handle to a frontend session
* @return SANE_STATUS_GOOD on success, SANE_STATUS_BUSY if the device is
* in use by another session or SANE_STATUS_WARMING_UP if the device is
* warming up. In this case the fronted as to call sane_start again until
* warming up is done. Any other values returned are error status.
*/
SANE_Status
sane_start (SANE_Handle handle)
{
struct Rts8891_Session *session = handle;
int light, mode;
struct Rts8891_Device *dev = session->dev;
SANE_Status status;
SANE_Bool changed;
#ifdef HAVE_SYS_TIME_H
struct timeval current;
#endif
DBG (DBG_proc, "sane_start: start\n");
/* if already scanning, tell we're busy */
if (session->scanning == SANE_TRUE)
{
DBG (DBG_warn, "sane_start: device is already scanning\n");
return SANE_STATUS_DEVICE_BUSY;
}
/* claim the interface reserve device */
if (dev->conf.allowsharing == SANE_TRUE)
{
status = sanei_usb_claim_interface (dev->devnum, 0);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_warn, "sane_start: cannot claim usb interface\n");
return SANE_STATUS_DEVICE_BUSY;
}
}
/* check if we need warming-up */
sanei_rts88xx_get_lamp_status (dev->devnum, dev->regs);
if ((dev->regs[0x8e] & 0x60) != 0x60)
{
DBG (DBG_info, "sane_start: lamp needs warming (0x%02x)\n",
dev->regs[0x8e]);
dev->needs_warming = SANE_TRUE;
#ifdef HAVE_SYS_TIME_H
dev->start_time.tv_sec = current.tv_sec;
dev->last_scan.tv_sec = current.tv_sec;
#endif
}
#ifdef HAVE_SYS_TIME_H
/* if last scan time is more than 10 minutes ago, scanner also needs
* warming-up */
gettimeofday (¤t, NULL);
if ((current.tv_sec - dev->last_scan.tv_sec) / 60 > 10)
{
DBG (DBG_info,
"sane_start: more than 10 minutes without scanning, lamp needs warming\n");
dev->needs_warming = SANE_TRUE;
dev->start_time.tv_sec = current.tv_sec;
dev->last_scan.tv_sec = current.tv_sec;
}
#endif
/* if parking, wait for head to stop */
if(dev->parking==SANE_TRUE)
{
rts8891_wait_for_home (dev, dev->regs);
}
/* reinit registers to start values */
rts8891_set_default_regs (dev->regs);
/* step 0: light up */
init_lamp (dev);
/* do warming up if needed: just detected or at sane_open() */
if (dev->needs_warming == SANE_TRUE)
{
DBG (DBG_info, "sane_start: warming lamp ...\n");
#ifdef HAVE_SYS_TIME_H
/* check if current time is >15s after warming-up start */
gettimeofday (¤t, NULL);
if ((current.tv_sec - dev->start_time.tv_sec) < 15)
{
#ifdef SANE_STATUS_WARMING_UP
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
return SANE_STATUS_WARMING_UP;
#else
DBG (DBG_info,
"sane_start: waiting to let lamp get warm enough ...\n");
sleep (15 - (current.tv_sec - dev->start_time.tv_sec));
#endif
}
#else
DBG (DBG_info,
"sane_start: waiting 15s to let lamp get warm enough ...\n");
sleep (15);
#endif
}
dev->needs_warming = SANE_FALSE;
/* restore default brightness */
dev->regs[LAMP_BRIGHT_REG] = 0xA7;
sanei_rts88xx_write_reg (dev->devnum, LAMP_BRIGHT_REG,
dev->regs + LAMP_BRIGHT_REG);
DBG (DBG_info, "sane_start: sensor initial type is %s (%d)\n",
sensor_name (dev->sensor), dev->sensor);
/* step 1: locate scan area by doing a scan, then goto calibration area */
/* we also detect if the sensor type is inadequate and then change it */
do
{
changed = SANE_FALSE;
status = find_origin (dev, &changed);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to find origin!\n");
return status;
}
/* in case find_origin detected we need to change sensor */
if (changed)
{
/* for a sensor 'pair', we switch */
switch (dev->sensor)
{
case SENSOR_TYPE_BARE:
DBG (DBG_info,
"sane_start: sensor changed to type 'SENSOR_TYPE_XPA'!\n");
dev->sensor = SENSOR_TYPE_XPA;
break;
case SENSOR_TYPE_XPA:
DBG (DBG_info,
"sane_start: sensor changed to type 'SENSOR_TYPE_BARE'!\n");
dev->sensor = SENSOR_TYPE_BARE;
break;
case SENSOR_TYPE_4400:
DBG (DBG_info,
"sane_start: sensor changed to type '4400 SENSOR_TYPE_4400_BARE'!\n");
dev->sensor = SENSOR_TYPE_4400_BARE;
break;
case SENSOR_TYPE_4400_BARE:
DBG (DBG_info,
"sane_start: sensor changed to type 'SENSOR_TYPE_4400'!\n");
dev->sensor = SENSOR_TYPE_4400;
break;
}
}
}
while (changed);
/* light source to use */
switch (dev->sensor)
{
case SENSOR_TYPE_XPA:
light = 0x3f;
mode = 0x20;
break;
case SENSOR_TYPE_BARE:
light = 0x3b;
mode = 0x20;
break;
case SENSOR_TYPE_4400:
case SENSOR_TYPE_4400_BARE:
light = 0x2a;
mode = 0x10;
break;
default:
light = 0x3b;
mode = 0x20;
break;
}
DBG (DBG_info, "sane_start: sensor final type is %s (%d)\n",
sensor_name (dev->sensor), dev->sensor);
DBG (DBG_info, "sane_start: mode=0x%02x, light=0x%02x\n", mode, light);
/* step 2: dark calibration */
status = dark_calibration (dev, mode, light);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to do dark calibration!\n");
return status;
}
/* step 3: find left margin */
status = find_margin (dev);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed find left margin!\n");
return status;
}
/* restore brightness */
dev->regs[LAMP_BRIGHT_REG] = 0xA7;
sanei_rts88xx_write_reg (dev->devnum, LAMP_BRIGHT_REG,
dev->regs + LAMP_BRIGHT_REG);
/* step 4: gain calibration */
status = gain_calibration (dev, mode, light);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to do gain calibration!\n");
return status;
}
/* step 5: fine offset calibration */
status = offset_calibration (dev, mode, light);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to do offset calibration!\n");
return status;
}
/* we compute all the scan parameters so that */
/* we will be able to set up the registers correctly */
compute_parameters (session);
/* allocate buffer and compute start pointer */
if (dev->scanned_data != NULL)
free (dev->scanned_data);
dev->scanned_data =
(SANE_Byte *) malloc (dev->data_size + dev->lds_max + dev->ripple);
dev->start = dev->scanned_data + dev->lds_max + dev->ripple;
/* computes data end pointer */
dev->end = dev->start + dev->data_size;
/* no bytes sent yet to frontend */
session->sent = 0;
sanei_rts88xx_set_offset (dev->regs, dev->red_offset, dev->green_offset,
dev->blue_offset);
sanei_rts88xx_set_gain (dev->regs, dev->red_gain, dev->green_gain,
dev->blue_gain);
/* step 6: shading calibration */
status =
shading_calibration (dev, session->params.format == SANE_FRAME_RGB
|| session->emulated_gray == SANE_TRUE, mode, light);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to do shading calibration!\n");
return status;
}
/* step 6b: move at dev->model->min_ydpi to target area */
if (dev->ydpi > dev->model->min_ydpi
&& (dev->ystart * MOVE_DPI) / dev->ydpi > 150)
{
status = move_to_scan_area (session);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to move to scan area!\n");
return status;
}
}
/* step 7: effective scan start */
/* build register set and send it */
status = write_scan_registers (session);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to write scan registers!\n");
return status;
}
/* step 8: send calibration data */
status = send_calibration_data (session);
if (status != SANE_STATUS_GOOD)
{
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_error, "sane_start: failed to send calibration data!\n");
return status;
}
/* commit the scan */
/* TODO send_calibration seems to embbed its commit */
sanei_rts88xx_cancel (dev->devnum);
sanei_rts88xx_write_control (dev->devnum, 0x08);
status = sanei_rts88xx_write_control (dev->devnum, 0x08);
session->scanning = SANE_TRUE;
DBG (DBG_proc, "sane_start: exit\n");
return status;
}
/**
* This function computes two set of parameters. The one for the SANE's standard
* and the other for the hardware. Among these parameters are the bit depth, total
* number of lines, total number of columns, extra line to read for data reordering...
*/
static SANE_Status
compute_parameters (Rts8891_Session * session)
{
Rts8891_Device *dev = session->dev;
SANE_Int dpi; /* dpi for scan */
SANE_String mode;
SANE_Status status = SANE_STATUS_GOOD;
SANE_Int yshift;
SANE_Int xshift;
int tl_x, tl_y, br_x, br_y;
mode = session->val[OPT_MODE].s;
dpi = session->val[OPT_RESOLUTION].w;
/* scan coordinates */
tl_x = SANE_UNFIX (session->val[OPT_TL_X].w);
tl_y = SANE_UNFIX (session->val[OPT_TL_Y].w);
br_x = SANE_UNFIX (session->val[OPT_BR_X].w);
br_y = SANE_UNFIX (session->val[OPT_BR_Y].w);
/* only single pass scanning supported */
session->params.last_frame = SANE_TRUE;
session->emulated_gray = SANE_FALSE;
/* gray modes */
dev->threshold = (255 * SANE_UNFIX (session->val[OPT_THRESHOLD].w)) / 100;
if (strcmp (mode, GRAY_MODE) == 0 || strcmp (mode, LINEART_MODE) == 0)
{
session->params.format = SANE_FRAME_GRAY;
if (dev->model->flags & RTS8891_FLAG_EMULATED_GRAY_MODE)
session->emulated_gray = SANE_TRUE;
}
else
{
/* Color */
session->params.format = SANE_FRAME_RGB;
}
/* SANE level values */
session->params.lines = ((br_y - tl_y) * dpi) / MM_PER_INCH;
if (session->params.lines == 0)
session->params.lines = 1;
session->params.pixels_per_line = ((br_x - tl_x) * dpi) / MM_PER_INCH;
if (session->params.pixels_per_line == 0)
session->params.pixels_per_line = 1;
DBG (DBG_data, "compute_parameters: pixels_per_line =%d\n",
session->params.pixels_per_line);
if (strcmp (mode, LINEART_MODE) == 0)
{
session->params.depth = 1;
/* in lineart, having pixels multiple of 8 avoids a costly test */
/* at each bit to see we must go to the next byte */
/* TODO : implement this requirement in sane_control_option */
session->params.pixels_per_line =
((session->params.pixels_per_line + 7) / 8) * 8;
}
else
session->params.depth = 8;
/* width needs to be even */
if (session->params.pixels_per_line & 1)
session->params.pixels_per_line++;
/* Hardware settings : they can differ from the ones at SANE level */
/* for instance the effective DPI used by a sensor may be higher */
/* than the one needed for the SANE scan parameters */
dev->lines = session->params.lines;
dev->pixels = session->params.pixels_per_line;
/* motor and sensor DPI */
dev->xdpi = dpi;
dev->ydpi = dpi;
/* handle bounds of motor's dpi range */
if (dev->ydpi > dev->model->max_ydpi)
{
dev->ydpi = dev->model->max_ydpi;
dev->lines = (dev->lines * dev->model->max_ydpi) / dpi;
if (dev->lines == 0)
dev->lines = 1;
/* round number of lines */
session->params.lines =
(session->params.lines / dev->lines) * dev->lines;
if (session->params.lines == 0)
session->params.lines = 1;
}
if (dev->ydpi < dev->model->min_ydpi)
{
dev->ydpi = dev->model->min_ydpi;
dev->lines = (dev->lines * dev->model->min_ydpi) / dpi;
}
/* hardware values */
dev->xstart =
((SANE_UNFIX (dev->model->x_offset) + tl_x) * dev->xdpi) / MM_PER_INCH;
dev->ystart =
((SANE_UNFIX (dev->model->y_offset) + tl_y) * dev->ydpi) / MM_PER_INCH;
/* xstart needs to be even */
if (dev->xstart & 1)
dev->xstart++;
/* computes bytes per line */
session->params.bytes_per_line = session->params.pixels_per_line;
dev->bytes_per_line = dev->pixels;
if (session->params.format == SANE_FRAME_RGB
|| session->emulated_gray == SANE_TRUE)
{
if (session->emulated_gray != SANE_TRUE)
{
session->params.bytes_per_line *= 3;
}
dev->bytes_per_line *= 3;
}
session->to_send = session->params.bytes_per_line * session->params.lines;
/* in lineart mode we adjust bytes_per_line needed by frontend */
/* we do that here because we needed sent/to_send to be as if */
/* there was no lineart */
if (session->params.depth == 1)
{
session->params.bytes_per_line =
(session->params.bytes_per_line + 7) / 8;
}
/* compute how many extra bytes we need to reorder data */
dev->ripple = 0;
if (session->params.format == SANE_FRAME_RGB
|| session->emulated_gray == SANE_TRUE)
{
dev->lds_r
= ((dev->model->ld_shift_r * dev->ydpi) / dev->model->max_ydpi)
* dev->bytes_per_line;
dev->lds_g
= ((dev->model->ld_shift_g * dev->ydpi) / dev->model->max_ydpi)
* dev->bytes_per_line;
dev->lds_b
= ((dev->model->ld_shift_b * dev->ydpi) / dev->model->max_ydpi)
* dev->bytes_per_line;
if (dev->xdpi == dev->model->max_xdpi)
{
dev->ripple = 2 * dev->bytes_per_line;
}
}
else
{
dev->lds_r = 0;
dev->lds_g = 0;
dev->lds_b = 0;
}
/* pick max lds value */
dev->lds_max = dev->lds_r;
if (dev->lds_g > dev->lds_max)
dev->lds_max = dev->lds_g;
if (dev->lds_b > dev->lds_max)
dev->lds_max = dev->lds_b;
/* since the extra lines for reordering are before data */
/* we substract lds_max */
dev->lds_r -= dev->lds_max;
dev->lds_g -= dev->lds_max;
dev->lds_b -= dev->lds_max;
/* we need to add extra line to handle line distance reordering */
/* highest correction value is the blue one for our case */
/* decrease y start to take these extra lines into account */
dev->lines += (dev->lds_max + dev->ripple) / dev->bytes_per_line;
/* shading calibration is allways 66 lines regardless of ydpi, so */
/* we take this into account to compute ystart */
if (dev->ydpi > dev->model->min_ydpi)
{
/* no that clean, but works ... */
switch (dev->ydpi)
{
case 300:
yshift = 0;
break;
case 600:
yshift = 33;
break;
default:
yshift = 0;
}
dev->ystart += yshift;
}
dev->ystart -= (dev->lds_max + dev->ripple) / dev->bytes_per_line;
/* 1200 and 600 dpi scans seems to have other timings that
* need their own xstart */
/* no that clean, but works ... */
switch (dev->xdpi)
{
case 600:
xshift = -38;
break;
case 1200:
xshift = -76;
break;
default:
xshift = 0;
}
dev->xstart += xshift;
dev->read = 0;
dev->to_read = dev->lines * dev->bytes_per_line;
/* compute best size for scanned data buffer */
/* we must have a round number of lines, with */
/* enough space to handle line distance shift */
if (dev->xdpi < dev->model->max_ydpi)
{
dev->data_size =
(PREFERED_BUFFER_SIZE / dev->bytes_per_line) * (dev->bytes_per_line);
}
else
{
dev->data_size =
(((PREFERED_BUFFER_SIZE / 2) - dev->lds_max -
dev->ripple) / dev->bytes_per_line) * dev->bytes_per_line;
}
/* 32 lines minimum in buffer */
if (dev->data_size < 32 * dev->bytes_per_line)
dev->data_size = 32 * dev->bytes_per_line;
/* buffer must be smaller than total amount to read from session */
if (dev->data_size > dev->to_read)
dev->data_size = dev->to_read;
DBG (DBG_data, "compute_parameters: bytes_per_line =%d\n",
session->params.bytes_per_line);
DBG (DBG_data, "compute_parameters: depth =%d\n",
session->params.depth);
DBG (DBG_data, "compute_parameters: lines =%d\n",
session->params.lines);
DBG (DBG_data, "compute_parameters: pixels_per_line =%d\n",
session->params.pixels_per_line);
DBG (DBG_data, "compute_parameters: image size =%d\n",
session->to_send);
DBG (DBG_data, "compute_parameters: xstart =%d\n", dev->xstart);
DBG (DBG_data, "compute_parameters: ystart =%d\n", dev->ystart);
DBG (DBG_data, "compute_parameters: dev lines =%d\n", dev->lines);
DBG (DBG_data, "compute_parameters: dev extra lines =%d\n",
(dev->lds_max + dev->ripple) / dev->bytes_per_line);
DBG (DBG_data, "compute_parameters: dev bytes per line=%d\n",
dev->bytes_per_line);
DBG (DBG_data, "compute_parameters: dev pixels =%d\n", dev->pixels);
DBG (DBG_data, "compute_parameters: data size =%d\n",
dev->data_size);
DBG (DBG_data, "compute_parameters: to read =%d\n", dev->to_read);
DBG (DBG_data, "compute_parameters: threshold =%d\n",
dev->threshold);
return status;
}
/**
* 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 of the
* device for which the parameters should be obtained and a pointer
* to a parameter structure.
*/
SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
SANE_Status status;
struct Rts8891_Session *session = (struct Rts8891_Session *) handle;
DBG (DBG_proc, "sane_get_parameters: start\n");
/* call parameters computing function */
status = compute_parameters (session);
if (status == SANE_STATUS_GOOD && params)
*params = session->params;
DBG (DBG_proc, "sane_get_parameters: exit\n");
return status;
}
/**
* 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 Rts8891_Session *session;
struct Rts8891_Device *dev;
SANE_Status status;
SANE_Int length;
SANE_Int data_size;
SANE_Byte val = 0;
SANE_Int bit;
DBG (DBG_proc, "sane_read: start\n");
DBG (DBG_io, "sane_read: up to %d bytes required by frontend\n", max_len);
/* some sanity checks first to protect from would be buggy frontends */
if (!handle)
{
DBG (DBG_error, "sane_read: handle is null!\n");
return SANE_STATUS_INVAL;
}
session = (struct Rts8891_Session *) handle;
if (!session)
{
DBG (DBG_error, "sane_read: session is null!\n");
return SANE_STATUS_INVAL;
}
dev = session->dev;
if (!buf)
{
DBG (DBG_error, "sane_read: buf is null!\n");
return SANE_STATUS_INVAL;
}
if (!len)
{
DBG (DBG_error, "sane_read: len is null!\n");
return SANE_STATUS_INVAL;
}
/* no data read yet */
*len = 0;
buf[*len] = 0;
/* check if session is scanning */
if (!session->scanning)
{
DBG (DBG_warn,
"sane_read: scan was cancelled, is over or has not been initiated yet\n");
return SANE_STATUS_CANCELLED;
}
/* check for EOF, must be done before any physical read */
if (session->sent >= session->to_send)
{
DBG (DBG_io, "sane_read: end of scan reached\n");
/* signal end of scanning */
session->scanning=SANE_FALSE;
/* send asynchronous head parking command then return */
park_head (session->dev, SANE_FALSE);
return SANE_STATUS_EOF;
}
dev->regs[LAMP_REG] = 0xad;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, &(dev->regs[LAMP_REG]));
/* byte length for high dpi mode */
length = (session->params.bytes_per_line * 8) / session->params.depth;
/* checks if buffer has been pre filled with the extra data needed for */
/* line distance reordering */
if (dev->read == 0
&& (session->params.format == SANE_FRAME_RGB
|| session->emulated_gray == SANE_TRUE))
{
/* the data need if the size of the highest line distance shift */
/* we must only read what's needed */
data_size = dev->data_size + dev->lds_max + dev->ripple;
if (dev->to_read - dev->read < data_size)
data_size = dev->to_read - dev->read;
status = read_data (session, dev->scanned_data, data_size);
if (status == SANE_STATUS_DEVICE_BUSY)
{
DBG (DBG_io, "sane_read: no data currently available\n");
return SANE_STATUS_GOOD;
}
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "sane_read: failed to read data from scanner\n");
return status;
}
/* now buffer is filled with data and an extra amount for line reordering */
dev->current = dev->start;
}
/* if all buffer has been used, get the next block of data from scanner */
if (dev->read == 0 || dev->current >= dev->end)
{
/* effective buffer filling */
/* we must only read what's needed */
data_size = dev->data_size;
if (dev->to_read - dev->read < data_size)
data_size = dev->to_read - dev->read;
status = read_data (session, dev->start, data_size);
if (status == SANE_STATUS_DEVICE_BUSY)
{
DBG (DBG_io, "sane_read: no data currently available\n");
return SANE_STATUS_GOOD;
}
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "sane_read: failed to read data from scanner\n");
return status;
}
/* reset current pointer */
dev->current = dev->start;
}
dev->regs[LAMP_REG] = 0x8d;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, &(dev->regs[LAMP_REG]));
/* it seems there is no gray or lineart hardware mode for the rts8891 */
if (session->params.format == SANE_FRAME_GRAY
&& session->emulated_gray == SANE_FALSE)
{
DBG (DBG_error0, "sane_read: unimplemented native gray scanning\n");
return SANE_STATUS_INVAL;
}
else /* hardware data is 3 bytes/per pixels */
{
/* we return as much data as possible from current buffer */
/* if buffer is smaller than what is asked, we only return */
/* here we have made sure there is something to read from data buffers */
/* data from scanner is in R,G,B format, but we have to cope with line */
/* distance correction. We also take care that motor resolution may be */
/* higher than the scan resolution asked for some modes. */
/* We also handle emulated color mode */
/* we loop until we reach max_len or end of data buffer */
/* or we sent what we advised to frontend */
while (dev->current < dev->end && *len < max_len
&& session->sent < session->to_send)
{
/* data is received in RGB format */
/* these switches are there to handle reads not aligned
* on pixels that are allowed by the SANE standard */
if (dev->xdpi == dev->model->max_xdpi)
{ /* at max xdpi, data received is distorted and ydpi is half of xdpi */
if (session->emulated_gray == SANE_TRUE)
{
/* in emulated gray mode we are allways reading 3 bytes of raw data */
/* at a time */
switch (((session->sent * 3) % dev->bytes_per_line) % 6)
{
case 0:
case 1:
case 2:
val = dev->current[dev->lds_g];
break;
case 3:
case 4:
case 5:
val = dev->current[dev->lds_g - dev->ripple];
break;
}
if (session->params.depth == 1)
{
bit = 7 - (session->sent) % 8;
if (val <= dev->threshold)
{
buf[*len] |= 1 << bit;
}
if (bit == 0)
{
(*len)++;
buf[*len] = 0;
}
}
else
{
buf[*len] = val;
(*len)++;
}
session->sent++;
dev->current += 3;
}
else
{
switch ((session->sent % dev->bytes_per_line) % 6)
{
case 0:
buf[*len] = dev->current[dev->lds_r];
break;
case 1:
buf[*len] = dev->current[dev->lds_g];
break;
case 2:
buf[*len] = dev->current[dev->lds_b];
break;
case 3:
buf[*len] = dev->current[dev->lds_r - dev->ripple];
break;
case 4:
buf[*len] = dev->current[dev->lds_g - dev->ripple];
break;
case 5:
buf[*len] = dev->current[dev->lds_b - dev->ripple];
break;
}
(*len)++;
session->sent++;
dev->current++;
}
/* we currently double lines to have match xdpy */
/* so we logically rewind for each odd line */
/* we test at start of a scanned picture line */
if (((session->sent / length) % 2 == 1)
&& (session->sent % length == 0))
{
DBG (DBG_io,
"sane_read: rewind by %d bytes after %d bytes sent\n",
dev->bytes_per_line, session->sent);
dev->current -= dev->bytes_per_line;
}
}
else if (dev->ydpi == session->val[OPT_RESOLUTION].w)
{
if (session->emulated_gray == SANE_TRUE)
{
/* in emulated gray mode we are allways reading 3 bytes of raw data */
/* at a time, so we know where we are */
val = dev->current[dev->lds_g];
if (session->params.depth == 1)
{
bit = 7 - (session->sent) % 8;
if (val <= dev->threshold)
{
buf[*len] |= 1 << bit;
}
else
{
buf[*len] &= ~(1 << bit);
}
if (bit == 0)
{
(*len)++;
}
}
else
{
buf[*len] = val;
(*len)++;
}
session->sent++;
dev->current += 3;
}
else
{
switch ((dev->current - dev->start) % 3)
{
case 0:
buf[*len] = dev->current[dev->lds_r];
break;
case 1:
buf[*len] = dev->current[dev->lds_g];
break;
case 2:
buf[*len] = dev->current[dev->lds_b];
break;
}
(*len)++;
session->sent++;
dev->current++;
}
}
else
{
/* we currently handle ydi=2*dpi */
if (session->emulated_gray == SANE_TRUE)
{
/* in emulated gray mode we are allways reading 3 bytes of raw data */
/* at a time, so we know where we are */
val = (dev->current[dev->lds_g]
+ dev->current[dev->lds_g +
dev->bytes_per_line]) / 2;
if (session->params.depth == 1)
{
bit = 7 - (session->sent) % 8;
if (val <= dev->threshold)
{
buf[*len] |= 1 << bit;
}
else
{
buf[*len] &= ~(1 << bit);
}
if (bit == 0)
{
(*len)++;
}
}
else
{
buf[*len] = val;
(*len)++;
}
dev->current += 3;
}
else
{
switch ((dev->current - dev->start) % 3)
{
case 0:
buf[*len] = (dev->current[dev->lds_r]
+ dev->current[dev->lds_r +
dev->bytes_per_line]) / 2;
break;
case 1:
buf[*len] = (dev->current[dev->lds_g]
+ dev->current[dev->lds_g +
dev->bytes_per_line]) / 2;
break;
case 2:
buf[*len] = (dev->current[dev->lds_b]
+ dev->current[dev->lds_b +
dev->bytes_per_line]) / 2;
break;
}
(*len)++;
dev->current++;
}
session->sent++;
/* at the end of each line, we must count another one because */
/* 2 lines are used to produce one */
if ((dev->current - dev->start) % dev->bytes_per_line == 0)
dev->current += dev->bytes_per_line;
}
}
}
/* if we exhausted data buffer, we prepare for the next read */
/* in color mode, we need to copy the remainder of the */
/* buffer because of line distance handling, when blue data */
/* is exhausted, read and green haven't been fully read yet */
if (dev->current >= dev->end
&& (session->params.format == SANE_FRAME_RGB
|| session->emulated_gray == SANE_TRUE))
{
memcpy (dev->scanned_data, dev->end - (dev->lds_max + dev->ripple),
dev->lds_max + dev->ripple);
}
status = SANE_STATUS_GOOD;
DBG (DBG_io, "sane_read: sent %d bytes to frontend\n", *len);
DBG (DBG_proc, "sane_read: exit\n");
return status;
}
/**
* Cancels a scan.
*
* 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 operaton 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)
{
Rts8891_Session *session = handle;
struct Rts8891_Device *dev = session->dev;
#ifdef HAVE_SYS_TIME_H
struct timeval current;
#endif
DBG (DBG_proc, "sane_cancel: start\n");
#ifdef HAVE_SYS_TIME_H
/* store last scan time for the device */
gettimeofday (¤t, NULL);
dev->last_scan.tv_sec = current.tv_sec;
#endif
/* if scanning, abort and park head */
if (session->scanning == SANE_TRUE)
{
/* canceling while all data hasn't bee read */
if (dev->read < dev->to_read)
{
sanei_rts88xx_cancel (dev->devnum);
usleep (500000);
sanei_rts88xx_cancel (dev->devnum);
}
session->scanning = SANE_FALSE;
/* head parking */
if (park_head (dev, SANE_FALSE) != SANE_STATUS_GOOD)
{
DBG (DBG_error, "sane_cancel: failed to park head!\n");
}
}
/* free ressources used by scanning */
if (dev->scanned_data != NULL)
{
free (dev->scanned_data);
dev->scanned_data = NULL;
}
if (dev->shading_data != NULL)
{
free (dev->shading_data);
dev->shading_data = NULL;
}
/* release the interface to allow device sharing */
if (dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (dev->devnum, 0);
}
DBG (DBG_proc, "sane_cancel: exit\n");
}
/**
* Ends use of the session.
*
* 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.
*
* Handle resources are free'd before disposing the handle. But devices
* resources must not be mdofied, since it could be used or reused until
* sane_exit() is called.
*/
void
sane_close (SANE_Handle handle)
{
Rts8891_Session *prev, *session;
Rts8891_Device *dev;
int i;
DBG (DBG_proc, "sane_close: start\n");
/* remove handle from list of open handles: */
prev = NULL;
for (session = first_handle; session; session = session->next)
{
if (session == handle)
break;
prev = session;
}
if (!session)
{
DBG (DBG_error, "close: invalid handle %p\n", handle);
return; /* oops, not a handle we know about */
}
dev=session->dev;
/* cancel any active scan */
if (session->scanning == SANE_TRUE)
{
sane_cancel (handle);
}
/* if head is parking, wait for completion */
if(dev->parking==SANE_TRUE)
{
rts8891_wait_for_home (dev, dev->regs);
}
set_lamp_brightness (dev, 0);
if (prev)
prev->next = session->next;
else
first_handle = session->next;
/* switch off lamp and close usb */
if (dev->conf.allowsharing == SANE_TRUE)
{
SANE_Status status = sanei_usb_claim_interface (dev->devnum, 0);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_warn, "sane_close: cannot claim usb interface: %s\n",
sane_strstatus(status));
DBG (DBG_warn, "sane_close: continuing anyway\n");
}
}
set_lamp_state (session, 0);
sanei_usb_close (dev->devnum);
/* free per session data */
if (session->dev->model->gamma != session->val[OPT_GAMMA_VECTOR].wa)
free (session->val[OPT_GAMMA_VECTOR].wa);
if (session->dev->model->gamma != session->val[OPT_GAMMA_VECTOR_R].wa)
free (session->val[OPT_GAMMA_VECTOR_R].wa);
if (session->dev->model->gamma != session->val[OPT_GAMMA_VECTOR_G].wa)
free (session->val[OPT_GAMMA_VECTOR_G].wa);
if (session->dev->model->gamma != session->val[OPT_GAMMA_VECTOR_B].wa)
free (session->val[OPT_GAMMA_VECTOR_B].wa);
free (session->val[OPT_MODE].s);
free ((void *) session->opt[OPT_RESOLUTION].constraint.word_list);
for (i = OPT_BUTTON_1; i <= OPT_BUTTON_11; i++)
{
free ((void *) session->opt[i].name);
free ((void *) session->opt[i].title);
}
free (session);
DBG (DBG_proc, "sane_close: exit\n");
}
/**
* 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 Rts8891_Session *session, *next;
struct Rts8891_Device *dev, *nextdev;
int i;
DBG (DBG_proc, "sane_exit: start\n");
/* free session structs */
for (session = first_handle; session; session = next)
{
next = session->next;
sane_close ((SANE_Handle *) session);
free (session);
}
first_handle = NULL;
/* free devices structs */
for (dev = first_device; dev; dev = nextdev)
{
nextdev = dev->next;
free (dev->file_name);
free (dev);
}
first_device = NULL;
/* now list of devices */
if (devlist)
{
for (i = 0; i < num_devices; i++)
{
free ((SANE_Device *) devlist[i]);
}
free (devlist);
devlist = NULL;
}
num_devices = 0;
DBG (DBG_proc, "sane_exit: exit\n");
}
/** This function tries to find plugged relevant devices
*/
static SANE_Status
probe_rts8891_devices (void)
{
/**> configuration structure used during attach */
SANEI_Config config;
/**> list of configuration options */
SANE_Option_Descriptor *options[NUM_CFG_OPTIONS];
/**> placeholders pointers for option values */
void *values[NUM_CFG_OPTIONS];
int i;
SANE_Status status;
DBG (DBG_proc, "probe_rts8891_devices: start\n");
/* sharing is off by default and no model option */
rtscfg.allowsharing = SANE_FALSE;
rtscfg.modelnumber = -1;
rtscfg.sensornumber = -1;
/* initialize configuration options */
options[CFG_MODEL_NUMBER] =
(SANE_Option_Descriptor *) malloc (sizeof (SANE_Option_Descriptor));
options[CFG_MODEL_NUMBER]->name = "modelnumber";
options[CFG_MODEL_NUMBER]->desc =
"user provided scanner's internal model number";
options[CFG_MODEL_NUMBER]->type = SANE_TYPE_INT;
options[CFG_MODEL_NUMBER]->unit = SANE_UNIT_NONE;
options[CFG_MODEL_NUMBER]->size = sizeof (SANE_Word);
options[CFG_MODEL_NUMBER]->cap = SANE_CAP_SOFT_SELECT;
options[CFG_MODEL_NUMBER]->constraint_type = SANE_CONSTRAINT_RANGE;
options[CFG_MODEL_NUMBER]->constraint.range = &model_range;
values[CFG_MODEL_NUMBER] = &rtscfg.modelnumber;
options[CFG_ALLOW_SHARING] =
(SANE_Option_Descriptor *) malloc (sizeof (SANE_Option_Descriptor));
options[CFG_ALLOW_SHARING]->name = "allowsharing";
options[CFG_ALLOW_SHARING]->desc =
"allow sharing of the scanner by several frontends";
options[CFG_ALLOW_SHARING]->type = SANE_TYPE_BOOL;
options[CFG_ALLOW_SHARING]->unit = SANE_UNIT_NONE;
options[CFG_ALLOW_SHARING]->size = sizeof (SANE_Bool);
options[CFG_ALLOW_SHARING]->cap = SANE_CAP_SOFT_SELECT;
options[CFG_ALLOW_SHARING]->constraint_type = SANE_CONSTRAINT_NONE;
values[CFG_ALLOW_SHARING] = &rtscfg.allowsharing;
/* sensor type override */
options[CFG_SENSOR_NUMBER] =
(SANE_Option_Descriptor *) malloc (sizeof (SANE_Option_Descriptor));
options[CFG_SENSOR_NUMBER]->name = "sensornumber";
options[CFG_SENSOR_NUMBER]->desc =
"user provided scanner's internal sensor number";
options[CFG_SENSOR_NUMBER]->type = SANE_TYPE_INT;
options[CFG_SENSOR_NUMBER]->unit = SANE_UNIT_NONE;
options[CFG_SENSOR_NUMBER]->size = sizeof (SANE_Word);
options[CFG_SENSOR_NUMBER]->cap = SANE_CAP_SOFT_SELECT;
options[CFG_SENSOR_NUMBER]->constraint_type = SANE_CONSTRAINT_RANGE;
options[CFG_SENSOR_NUMBER]->constraint.range = &sensor_range;
values[CFG_SENSOR_NUMBER] = &rtscfg.sensornumber;
/* set configuration options structure */
config.descriptors = options;
config.values = values;
config.count = NUM_CFG_OPTIONS;
/* init usb use */
sanei_usb_init ();
/* generic configure and attach function */
status = sanei_configure_attach (RTS8891_CONFIG_FILE, &config,
config_attach_rts8891);
/* free allocated options */
for (i = 0; i < NUM_CFG_OPTIONS; i++)
{
free (options[i]);
}
DBG (DBG_proc, "probe_rts8891_devices: end\n");
return status;
}
/** This function is called by sanei_configure_attach to try
* to attach the backend to a device specified by the configuration file.
*
* @param config configuration structure filled with values read
* from configuration file
* @param devname name of the device to try to attach to, it is
* the unprocessed line of the configuration file
*
* @return status SANE_STATUS_GOOD if no errors (even if no matching
* devices found)
* SANE_STATUS_INVAL in case of error
*/
static SANE_Status
config_attach_rts8891 (SANEI_Config * config, const char *devname)
{
/* currently, the config is a global variable so config is useless here */
/* the correct thing would be to have a generic sanei_attach_matching_devices
* using an attach function with a config parameter */
if(config==NULL)
{
return SANE_STATUS_INVAL;
}
/* the devname has been processed and is ready to be used
* directly. Since the backend is an USB only one, we can
* call sanei_usb_attach_matching_devices straight */
sanei_usb_attach_matching_devices (devname, attach_rts8891);
return SANE_STATUS_GOOD;
}
/**
* The attach tries to open the given usb device and match it
* with devices handled by the backend. The configuration parameter
* contains the values of the allready parsed configuration options
* from the conf file.
* @param config configuration structure filled with values read
* from configuration file
* @param devicename name of the device to try to attach to, it is
* the unprocessed line of the configuration file
*
* @return status SANE_STATUS_GOOD if no errors (even if no matching
* devices found)
* SANE_STATUS_NOM_MEM if there isn't enough memory to allocate the
* device structure
* SANE_STATUS_UNSUPPORTED if the device if unknown by the backend
* SANE_STATUS_INVAL in case of other error
*/
static SANE_Status
attach_rts8891 (const char *devicename)
{
struct Rts8891_Device *device;
SANE_Int dn, vendor, product;
SANE_Status status;
DBG (DBG_proc, "attach_rts8891(%s): start\n", devicename);
/* search if we already have it attached */
for (device = first_device; device; device = device->next)
{
if (strcmp (device->file_name, devicename) == 0)
{
DBG (DBG_warn,
"attach_rts8891: device already attached (is ok)!\n");
DBG (DBG_proc, "attach_rts8891: exit\n");
return SANE_STATUS_GOOD;
}
}
/* open usb device */
status = sanei_usb_open (devicename, &dn);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "attach_rts8891: couldn't open device `%s': %s\n",
devicename, sane_strstatus (status));
return status;
}
else
{
DBG (DBG_info, "attach_rts8891: device `%s' successfully opened\n",
devicename);
}
/* get vendor and product id */
status = sanei_usb_get_vendor_product (dn, &vendor, &product);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error,
"attach_rts8891: couldn't get vendor and product ids of device `%s': %s\n",
devicename, sane_strstatus (status));
sanei_usb_close (dn);
DBG (DBG_proc, "attach_rts8891: exit\n");
return status;
}
sanei_usb_close (dn);
/* get the index of the device in the device description table */
/* if the value is provided by configuration option, just use it */
if (rtscfg.modelnumber < 0)
{
/* walk the list of devices to find matching entry */
dn = 0;
while ((vendor != rts8891_usb_device_list[dn].vendor_id
|| product != rts8891_usb_device_list[dn].product_id)
&& (rts8891_usb_device_list[dn].vendor_id != 0))
dn++;
/* if we reach the list termination entry, the device is unknown */
if (rts8891_usb_device_list[dn].vendor_id == 0)
{
DBG (DBG_info,
"attach_rts8891: unknown device `%s': 0x%04x:0x%04x\n",
devicename, vendor, product);
DBG (DBG_proc, "attach_rts8891: exit\n");
return SANE_STATUS_UNSUPPORTED;
}
}
else
{
/* user provided value */
dn = rtscfg.modelnumber;
}
/* allocate device struct */
device = malloc (sizeof (*device));
if (device == NULL)
{
return SANE_STATUS_NO_MEM;
}
memset (device, 0, sizeof (*device));
/* struct describing low lvel device */
device->model = rts8891_usb_device_list[dn].model;
/* name of the device */
device->file_name = strdup (devicename);
DBG (DBG_info, "attach_rts8891: found %s %s %s at %s\n",
device->model->vendor,
device->model->product, device->model->type, device->file_name);
/* we insert new device at start of the chained list */
/* head of the list becomes the next, and start is replaced */
/* with the new session struct */
num_devices++;
device->next = first_device;
first_device = device;
device->reg_count = 244;
/* intialization is done at sane_open */
device->initialized = SANE_FALSE;
device->needs_warming = SANE_TRUE;
device->parking = SANE_FALSE;
#ifdef HAVE_SYS_TIME_H
device->last_scan.tv_sec = 0;
device->start_time.tv_sec = 0;
#endif
/* in case autodection au sensor doesn't work, use the given override */
device->sensor = rtscfg.sensornumber;
/* copy configuration settings to device */
device->conf.modelnumber = dn;
device->conf.allowsharing = rtscfg.allowsharing;
DBG (DBG_proc, "attach_rts8891: exit\n");
return SANE_STATUS_GOOD;
}
/* set initial value for the scanning options */
static SANE_Status
init_options (struct Rts8891_Session *session)
{
SANE_Int option, count, i, min, idx;
SANE_Word *dpi_list;
Rts8891_Model *model = session->dev->model;
DBG (DBG_proc, "init_options: start\n");
/* we first initialize each options with a default value */
memset (session->opt, 0, sizeof (session->opt));
memset (session->val, 0, sizeof (session->val));
for (option = 0; option < NUM_OPTIONS; option++)
{
session->opt[option].size = sizeof (SANE_Word);
session->opt[option].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
}
/* we set up all the options listed in the Rts8891_Option enum */
/* last option / end of list marker */
session->opt[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS;
session->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
session->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
session->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
session->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
session->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
/* "Standard" group: */
session->opt[OPT_STANDARD_GROUP].title = SANE_TITLE_STANDARD;
session->opt[OPT_STANDARD_GROUP].name = SANE_NAME_STANDARD;
session->opt[OPT_STANDARD_GROUP].desc = SANE_DESC_STANDARD;
session->opt[OPT_STANDARD_GROUP].type = SANE_TYPE_GROUP;
session->opt[OPT_STANDARD_GROUP].size = 0;
session->opt[OPT_STANDARD_GROUP].cap = 0;
session->opt[OPT_STANDARD_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* scan mode */
session->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
session->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
session->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
session->opt[OPT_MODE].type = SANE_TYPE_STRING;
session->opt[OPT_MODE].cap |= SANE_CAP_AUTOMATIC;
session->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
session->opt[OPT_MODE].size = max_string_size (mode_list);
session->opt[OPT_MODE].constraint.string_list = mode_list;
session->val[OPT_MODE].s = strdup (mode_list[0]);
/* preview */
session->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
session->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
session->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
session->opt[OPT_PREVIEW].type = SANE_TYPE_BOOL;
session->opt[OPT_PREVIEW].cap |= SANE_CAP_AUTOMATIC;
session->opt[OPT_PREVIEW].unit = SANE_UNIT_NONE;
session->opt[OPT_PREVIEW].constraint_type = SANE_CONSTRAINT_NONE;
session->val[OPT_PREVIEW].w = SANE_FALSE;
/* build resolution list */
/* TODO we build it from xdpi, one could prefer building it from
* ydpi list, or even allow for independent X and Y dpi (with 2 options then)
*/
for (count = 0; model->xdpi_values[count] != 0; count++);
dpi_list = malloc ((count + 1) * sizeof (SANE_Word));
if (!dpi_list)
return SANE_STATUS_NO_MEM;
dpi_list[0] = count;
for (count = 0; model->xdpi_values[count] != 0; count++)
dpi_list[count + 1] = model->xdpi_values[count];
session->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
session->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
session->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
session->opt[OPT_RESOLUTION].type = SANE_TYPE_INT;
session->opt[OPT_RESOLUTION].cap |= SANE_CAP_AUTOMATIC;
session->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
session->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_WORD_LIST;
session->opt[OPT_RESOLUTION].constraint.word_list = dpi_list;
/* initial value is lowest available dpi */
min = 65536;
for (i = 1; i < count; i++)
{
if (dpi_list[i] < min)
min = dpi_list[i];
}
session->val[OPT_RESOLUTION].w = min;
/* "Geometry" group: */
session->opt[OPT_GEOMETRY_GROUP].title = SANE_TITLE_GEOMETRY;
session->opt[OPT_GEOMETRY_GROUP].name = SANE_NAME_GEOMETRY;
session->opt[OPT_GEOMETRY_GROUP].desc = SANE_DESC_GEOMETRY;
session->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
session->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
session->opt[OPT_GEOMETRY_GROUP].size = 0;
session->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* adapt the constraint range to the detected model */
x_range.max = model->x_size;
y_range.max = model->y_size;
/* top-left x */
session->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
session->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
session->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
session->opt[OPT_TL_X].type = SANE_TYPE_FIXED;
session->opt[OPT_TL_X].cap |= SANE_CAP_AUTOMATIC;
session->opt[OPT_TL_X].unit = SANE_UNIT_MM;
session->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_TL_X].constraint.range = &x_range;
session->val[OPT_TL_X].w = 0;
/* top-left y */
session->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
session->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
session->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
session->opt[OPT_TL_Y].type = SANE_TYPE_FIXED;
session->opt[OPT_TL_Y].cap |= SANE_CAP_AUTOMATIC;
session->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
session->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_TL_Y].constraint.range = &y_range;
session->val[OPT_TL_Y].w = 0;
/* bottom-right x */
session->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
session->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
session->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
session->opt[OPT_BR_X].type = SANE_TYPE_FIXED;
session->opt[OPT_BR_X].cap |= SANE_CAP_AUTOMATIC;
session->opt[OPT_BR_X].unit = SANE_UNIT_MM;
session->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_BR_X].constraint.range = &x_range;
session->val[OPT_BR_X].w = x_range.max;
/* bottom-right y */
session->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
session->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
session->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
session->opt[OPT_BR_Y].type = SANE_TYPE_FIXED;
session->opt[OPT_BR_Y].cap |= SANE_CAP_AUTOMATIC;
session->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
session->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_BR_Y].constraint.range = &y_range;
session->val[OPT_BR_Y].w = y_range.max;
/* "Enhancement" group: */
session->opt[OPT_ENHANCEMENT_GROUP].title = SANE_TITLE_ENHANCEMENT;
session->opt[OPT_ENHANCEMENT_GROUP].name = SANE_NAME_ENHANCEMENT;
session->opt[OPT_ENHANCEMENT_GROUP].desc = SANE_DESC_ENHANCEMENT;
session->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
session->opt[OPT_ENHANCEMENT_GROUP].cap = SANE_CAP_ADVANCED;
session->opt[OPT_ENHANCEMENT_GROUP].size = 0;
session->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* BW threshold */
session->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD;
session->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD;
session->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD;
session->opt[OPT_THRESHOLD].type = SANE_TYPE_FIXED;
session->opt[OPT_THRESHOLD].unit = SANE_UNIT_PERCENT;
session->opt[OPT_THRESHOLD].cap |=
SANE_CAP_INACTIVE | SANE_CAP_AUTOMATIC | SANE_CAP_EMULATED;
session->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_THRESHOLD].constraint.range = &threshold_percentage_range;
session->val[OPT_THRESHOLD].w = SANE_FIX (50);
/* custom-gamma table */
session->opt[OPT_CUSTOM_GAMMA].name = SANE_NAME_CUSTOM_GAMMA;
session->opt[OPT_CUSTOM_GAMMA].title = SANE_TITLE_CUSTOM_GAMMA;
session->opt[OPT_CUSTOM_GAMMA].desc = SANE_DESC_CUSTOM_GAMMA;
session->opt[OPT_CUSTOM_GAMMA].type = SANE_TYPE_BOOL;
session->opt[OPT_CUSTOM_GAMMA].cap |= SANE_CAP_ADVANCED;
session->val[OPT_CUSTOM_GAMMA].b = SANE_FALSE;
/* grayscale gamma vector */
session->opt[OPT_GAMMA_VECTOR].name = SANE_NAME_GAMMA_VECTOR;
session->opt[OPT_GAMMA_VECTOR].title = SANE_TITLE_GAMMA_VECTOR;
session->opt[OPT_GAMMA_VECTOR].desc = SANE_DESC_GAMMA_VECTOR;
session->opt[OPT_GAMMA_VECTOR].type = SANE_TYPE_INT;
session->opt[OPT_GAMMA_VECTOR].cap |=
SANE_CAP_INACTIVE | SANE_CAP_AUTOMATIC | SANE_CAP_ADVANCED;
session->opt[OPT_GAMMA_VECTOR].unit = SANE_UNIT_NONE;
session->opt[OPT_GAMMA_VECTOR].size = 256 * sizeof (SANE_Word);
session->opt[OPT_GAMMA_VECTOR].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_GAMMA_VECTOR].constraint.range = &u8_range;
session->val[OPT_GAMMA_VECTOR].wa = model->gamma;
/* red gamma vector */
session->opt[OPT_GAMMA_VECTOR_R].name = SANE_NAME_GAMMA_VECTOR_R;
session->opt[OPT_GAMMA_VECTOR_R].title = SANE_TITLE_GAMMA_VECTOR_R;
session->opt[OPT_GAMMA_VECTOR_R].desc = SANE_DESC_GAMMA_VECTOR_R;
session->opt[OPT_GAMMA_VECTOR_R].type = SANE_TYPE_INT;
session->opt[OPT_GAMMA_VECTOR_R].cap |=
SANE_CAP_INACTIVE | SANE_CAP_AUTOMATIC | SANE_CAP_ADVANCED;
session->opt[OPT_GAMMA_VECTOR_R].unit = SANE_UNIT_NONE;
session->opt[OPT_GAMMA_VECTOR_R].size = 256 * sizeof (SANE_Word);
session->opt[OPT_GAMMA_VECTOR_R].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_GAMMA_VECTOR_R].constraint.range = &u8_range;
session->val[OPT_GAMMA_VECTOR_R].wa = model->gamma;
/* green gamma vector */
session->opt[OPT_GAMMA_VECTOR_G].name = SANE_NAME_GAMMA_VECTOR_G;
session->opt[OPT_GAMMA_VECTOR_G].title = SANE_TITLE_GAMMA_VECTOR_G;
session->opt[OPT_GAMMA_VECTOR_G].desc = SANE_DESC_GAMMA_VECTOR_G;
session->opt[OPT_GAMMA_VECTOR_G].type = SANE_TYPE_INT;
session->opt[OPT_GAMMA_VECTOR_G].cap |=
SANE_CAP_INACTIVE | SANE_CAP_AUTOMATIC | SANE_CAP_ADVANCED;
session->opt[OPT_GAMMA_VECTOR_G].unit = SANE_UNIT_NONE;
session->opt[OPT_GAMMA_VECTOR_G].size = 256 * sizeof (SANE_Word);
session->opt[OPT_GAMMA_VECTOR_G].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_GAMMA_VECTOR_G].constraint.range = &u8_range;
session->val[OPT_GAMMA_VECTOR_G].wa = model->gamma;
/* blue gamma vector */
session->opt[OPT_GAMMA_VECTOR_B].name = SANE_NAME_GAMMA_VECTOR_B;
session->opt[OPT_GAMMA_VECTOR_B].title = SANE_TITLE_GAMMA_VECTOR_B;
session->opt[OPT_GAMMA_VECTOR_B].desc = SANE_DESC_GAMMA_VECTOR_B;
session->opt[OPT_GAMMA_VECTOR_B].type = SANE_TYPE_INT;
session->opt[OPT_GAMMA_VECTOR_B].cap |=
SANE_CAP_INACTIVE | SANE_CAP_AUTOMATIC | SANE_CAP_ADVANCED;
session->opt[OPT_GAMMA_VECTOR_B].unit = SANE_UNIT_NONE;
session->opt[OPT_GAMMA_VECTOR_B].size = 256 * sizeof (SANE_Word);
session->opt[OPT_GAMMA_VECTOR_B].constraint_type = SANE_CONSTRAINT_RANGE;
session->opt[OPT_GAMMA_VECTOR_B].constraint.range = &u8_range;
session->val[OPT_GAMMA_VECTOR_B].wa = model->gamma;
/* "Sensors" group */
session->opt[OPT_SENSOR_GROUP].title = SANE_TITLE_SENSORS;
session->opt[OPT_SENSOR_GROUP].name = SANE_NAME_SENSORS;
session->opt[OPT_SENSOR_GROUP].desc = SANE_DESC_SENSORS;
session->opt[OPT_SENSOR_GROUP].type = SANE_TYPE_GROUP;
session->opt[OPT_SENSOR_GROUP].cap = SANE_CAP_ADVANCED;
session->opt[OPT_SENSOR_GROUP].size = 0;
session->opt[OPT_SENSOR_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* scanner buttons */
for (i = OPT_BUTTON_1; i <= OPT_BUTTON_11; i++)
{
char name[39];
char title[64];
idx = i - OPT_BUTTON_1;
if (idx < model->buttons)
{
sprintf (name, "button-%s", model->button_name[idx]);
sprintf (title, "%s", model->button_title[idx]);
session->opt[i].name = strdup (name);
session->opt[i].title = strdup (title);
session->opt[i].desc = SANE_I18N ("This option reflects the status "
"of a scanner button.");
session->opt[i].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_ADVANCED;
}
else
{
session->opt[i].name = strdup ("unused");
session->opt[i].title = strdup ("unused button");
session->opt[i].cap |= SANE_CAP_INACTIVE;
}
session->opt[i].type = SANE_TYPE_BOOL;
session->opt[i].unit = SANE_UNIT_NONE;
session->opt[i].size = sizeof (SANE_Bool);
session->opt[i].constraint_type = SANE_CONSTRAINT_NONE;
session->opt[i].constraint.range = 0;
session->val[i].w = SANE_FALSE;
}
update_button_status (session);
/* "Advanced" group: */
session->opt[OPT_ADVANCED_GROUP].title = SANE_I18N ("Advanced");
session->opt[OPT_ADVANCED_GROUP].desc = "";
session->opt[OPT_ADVANCED_GROUP].type = SANE_TYPE_GROUP;
session->opt[OPT_ADVANCED_GROUP].cap = SANE_CAP_ADVANCED;
session->opt[OPT_ADVANCED_GROUP].size = 0;
session->opt[OPT_ADVANCED_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* lamp on */
session->opt[OPT_LAMP_ON].name = "lamp-on";
session->opt[OPT_LAMP_ON].title = SANE_I18N ("Lamp on");
session->opt[OPT_LAMP_ON].desc = SANE_I18N ("Turn on scanner lamp");
session->opt[OPT_LAMP_ON].type = SANE_TYPE_BUTTON;
session->opt[OPT_LAMP_ON].cap |= SANE_CAP_ADVANCED;
session->opt[OPT_LAMP_ON].unit = SANE_UNIT_NONE;
session->opt[OPT_LAMP_ON].size = 0;
session->opt[OPT_LAMP_ON].constraint_type = SANE_CONSTRAINT_NONE;
session->val[OPT_LAMP_ON].w = 0;
/* lamp off */
session->opt[OPT_LAMP_OFF].name = "lamp-off";
session->opt[OPT_LAMP_OFF].title = SANE_I18N ("Lamp off");
session->opt[OPT_LAMP_OFF].desc = SANE_I18N ("Turn off scanner lamp");
session->opt[OPT_LAMP_OFF].type = SANE_TYPE_BUTTON;
session->opt[OPT_LAMP_OFF].cap |= SANE_CAP_ADVANCED;
session->opt[OPT_LAMP_OFF].unit = SANE_UNIT_NONE;
session->opt[OPT_LAMP_OFF].size = 0;
session->opt[OPT_LAMP_OFF].constraint_type = SANE_CONSTRAINT_NONE;
session->val[OPT_LAMP_OFF].w = 0;
DBG (DBG_proc, "init_options: exit\n");
return SANE_STATUS_GOOD;
}
/**
* average scanned rgb data, returns the global average. Each channel avearge is also
* returned.
*/
static float
average_area (int color, SANE_Byte * data, int width, int height,
float *ra, float *ga, float *ba)
{
int x, y;
float global, pixels;
float rc, gc, bc;
pixels = width * height;
*ra = 0;
*ga = 0;
*ba = 0;
rc = 0;
gc = 0;
bc = 0;
if (color == SANE_TRUE)
{
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
rc += data[3 * width * y + x];
gc += data[3 * width * y + x + 1];
bc += data[3 * width * y + x + 2];
}
}
global = (rc + gc + bc) / (3 * pixels);
*ra = rc / pixels;
*ga = gc / pixels;
*ba = bc / pixels;
}
else
{
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
gc += data[width * y + x];
}
}
global = gc / pixels;
*ga = gc / pixels;
}
DBG (7, "average_area: global=%.2f, red=%.2f, green=%.2f, blue=%.2f\n",
global, *ra, *ga, *ba);
return global;
}
/**
* Sets lamp brightness (hum, maybe some timing before light off)
*/
static SANE_Status
set_lamp_brightness (struct Rts8891_Device *dev, int level)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Byte reg;
reg = 0xA0 | (level & 0x0F);
sanei_rts88xx_write_reg (dev->devnum, LAMP_BRIGHT_REG, ®);
switch (level)
{
case 0:
reg = 0x8d;
break;
case 7:
reg = 0x82;
break;
default:
reg = 0x8d;
break;
}
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
reg = (reg & 0xF0) | 0x20 | ((~reg) & 0x0F);
dev->regs[LAMP_REG] = reg;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_get_status (dev->devnum, dev->regs);
DBG (DBG_io, "set_lamp_brightness: status=0x%02x 0x%02x\n", dev->regs[0x10],
dev->regs[0x11]);
if (dev->sensor != SENSOR_TYPE_4400)
{
dev->regs[0x10] = 0x28;
dev->regs[0x11] = 0x3f;
}
else
{
dev->regs[0x10] = 0x10;
dev->regs[0x11] = 0x2f;
}
reg = dev->regs[LAMP_REG];
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
status = sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, ®);
if (reg != 0x00)
{
DBG (DBG_warn,
"set_lamp_brightness: unexpected CONTROL_REG value, 0x%02x instead of 0x00\n",
reg);
}
return status;
}
/**
* Sets the lamp to half brightness and standard parameters
*/
static SANE_Status
init_lamp (struct Rts8891_Device *dev)
{
SANE_Byte reg;
sanei_rts88xx_write_control (dev->devnum, 0x01);
sanei_rts88xx_write_control (dev->devnum, 0x01);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_cancel (dev->devnum);
dev->regs[0x12] = 0xff;
dev->regs[0x13] = 0x20;
sanei_rts88xx_write_regs (dev->devnum, 0x12, dev->regs + 0x12, 2);
/* 0xF8/0x28 expected */
sanei_rts88xx_write_regs (dev->devnum, 0x14, dev->regs + 0x14, 2);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
if (dev->sensor != SENSOR_TYPE_4400 && dev->sensor != SENSOR_TYPE_4400_BARE)
{
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x28, 0x3f);
dev->regs[0x11] = 0x3f;
}
else
{
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x10, 0x22);
dev->regs[0x11] = 0x22;
}
reg = 0x8d;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
dev->regs[LAMP_REG] = 0xa2;
dev->regs[LAMP_BRIGHT_REG] = 0xa0;
rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
return set_lamp_brightness (dev, 7);
}
/**
* This function detects physical start of scanning area find finding a black area below the "roof"
* if during scan we detect that sensor is inadequate, changed is set to SANE_TRUE
*/
static SANE_Status
find_origin (struct Rts8891_Device *dev, SANE_Bool * changed)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Byte control, reg;
unsigned int max = 0;
unsigned char *image = NULL;
unsigned char *data = NULL;
SANE_Word total;
int startx = 300;
int width = 1200;
int x, y, sum, current;
int starty = 18;
int height = 180;
int timing;
DBG (DBG_proc, "find_origin: start\n");
/* check if head is at home
* once sensor is correctly set up, we are allways park here,
* but in case sensor has just changed, we are not so we park head */
sanei_rts88xx_read_reg (dev->devnum, CONTROLER_REG, ®);
if ((reg & 0x02) == 0)
{
if (park_head (dev, SANE_TRUE) != SANE_STATUS_GOOD)
{
DBG (DBG_error, "find_origin: failed to park head!\n");
return SANE_STATUS_IO_ERROR;
}
DBG (DBG_info, "find_origin: head parked\n");
}
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
if (control != 0)
{
DBG (DBG_warn, "find_origin: unexpected control value 0x%02x\n",
control);
}
sanei_rts88xx_reset_lamp (dev->devnum, dev->regs);
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
if (control != 0)
{
DBG (DBG_warn, "find_origin: unexpected control value 0x%02x\n",
control);
}
sanei_rts88xx_reset_lamp (dev->devnum, dev->regs);
/* set lamp to standard settings */
init_lamp (dev);
/* set scan parameters */
dev->regs[0x00] = 0xe5;
dev->regs[0x32] = 0x80;
dev->regs[0x33] = 0x81;
dev->regs[0x39] = 0x02;
dev->regs[0x64] = 0x01;
dev->regs[0x65] = 0x20;
dev->regs[0x79] = 0x20;
dev->regs[0x7a] = 0x01;
dev->regs[0x90] = 0x1c;
dev->regs[0x35] = 0x1b;
dev->regs[0x36] = 0x29;
dev->regs[0x3a] = 0x1b;
timing=0x32;
dev->regs[0x85] = 0x00;
dev->regs[0x86] = 0x06;
dev->regs[0x87] = 0x00;
dev->regs[0x88] = 0x06;
dev->regs[0x8d] = 0x80;
dev->regs[0x8e] = 0x68;
dev->regs[0xb2] = 0x02; /* 0x04 -> no data */
dev->regs[0xc0] = 0xff;
dev->regs[0xc1] = 0x0f;
dev->regs[0xc3] = 0xff;
dev->regs[0xc4] = 0xff;
dev->regs[0xc5] = 0xff;
dev->regs[0xc6] = 0xff;
dev->regs[0xc7] = 0xff;
dev->regs[0xc8] = 0xff;
dev->regs[0xc9] = 0x00;
dev->regs[0xca] = 0x0e;
dev->regs[0xcb] = 0x00;
dev->regs[0xcd] = 0xf0;
dev->regs[0xce] = 0xff;
dev->regs[0xcf] = 0xf5;
dev->regs[0xd0] = 0xf7;
dev->regs[0xd1] = 0xea;
dev->regs[0xd2] = 0x0b;
dev->regs[0xd3] = 0x03;
dev->regs[0xd4] = 0x05;
dev->regs[0xd6] = 0xab;
dev->regs[0xd7] = 0x30;
dev->regs[0xd8] = 0xf6;
dev->regs[0xe2] = 0x01;
dev->regs[0xe3] = 0x00;
dev->regs[0xe4] = 0x00;
SET_DOUBLE (dev->regs, EXPOSURE_REG, 4124);
/* dev->regs[0xe5] = 0x1c;
dev->regs[0xe6] = 0x10; 101c=4124 */
dev->regs[0xe7] = 0x00;
dev->regs[0xe8] = 0x00;
dev->regs[0xe9] = 0x00;
if (dev->sensor == SENSOR_TYPE_XPA || dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0xc3] = 0x00;
dev->regs[0xc4] = 0xf0;
dev->regs[0xc7] = 0x0f;
dev->regs[0xc8] = 0x00;
dev->regs[0xc9] = 0xff;
dev->regs[0xca] = 0xf1;
dev->regs[0xcb] = 0xff;
dev->regs[0xd7] = 0x10;
}
if (dev->sensor == SENSOR_TYPE_4400 || dev->sensor == SENSOR_TYPE_4400_BARE)
{
/* 4400 values / 'XPA' values */
dev->regs[0x13] = 0x39; /* 0x20 */
dev->regs[0x14] = 0xf0; /* 0xf8 */
dev->regs[0x15] = 0x29; /* 0x28 */
dev->regs[0x16] = 0x0f; /* 0x07 */
dev->regs[0x17] = 0x10; /* 0x00 */
dev->regs[0x23] = 0x00; /* 0xff */
dev->regs[0x39] = 0x00; /* 0x02 */
}
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
dev->regs[0xc3] = 0xff; /* 0x00 */
dev->regs[0xc4] = 0xff; /* 0xf0 */
dev->regs[0xc7] = 0xff; /* 0x0f */
dev->regs[0xc8] = 0xff; /* 0x00 */
dev->regs[0xc9] = 0x00; /* 0xff */
dev->regs[0xca] = 0x0e; /* 0xf1 */
dev->regs[0xcb] = 0x00; /* 0xff */
dev->regs[0xd7] = 0x30; /* 0x10 */
dev->regs[0xda] = 0xa7; /* 0xa0 */
}
SET_DOUBLE (dev->regs, TIMING_REG, timing);
SET_DOUBLE (dev->regs, TIMING1_REG, timing+1);
SET_DOUBLE (dev->regs, TIMING2_REG, timing+2);
/* allocate memory for the data */
total = width * height;
data = (unsigned char *) malloc (total);
if (data == NULL)
{
DBG (DBG_error, "find_origin: failed to allocate %d bytes\n", total);
return SANE_STATUS_NO_MEM;
}
image = (unsigned char *) malloc (total);
if (image == NULL)
{
free(data);
DBG (DBG_error, "find_origin: failed to allocate %d bytes\n", total);
return SANE_STATUS_NO_MEM;
}
/* set vertical and horizontal start/end positions */
sanei_rts88xx_set_scan_area (dev->regs, starty, starty + height, startx,
startx + width);
sanei_rts88xx_set_offset (dev->regs, 0x7f, 0x7f, 0x7f);
sanei_rts88xx_set_gain (dev->regs, 0x10, 0x10, 0x10);
/* gray level scan */
dev->regs[LAMP_REG] = 0xad;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, dev->regs + LAMP_REG);
if (dev->sensor != SENSOR_TYPE_4400
&& (dev->sensor != SENSOR_TYPE_4400_BARE))
{
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x20, 0x3b);
}
else
{
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x10, 0x22);
}
status =
rts8891_simple_scan (dev->devnum, dev->regs, dev->reg_count, 0x03,
total, data);
if (status != SANE_STATUS_GOOD)
{
free(image);
free(data);
DBG (DBG_error, "find_origin: failed to scan\n");
return status;
}
if (DBG_LEVEL > DBG_io2)
{
write_gray_data (data, "find_origin.pnm", width, height);
}
/* now we have the data, search for the black area so that we can */
/* deduce start of scan area */
/* we apply an Y direction sobel filter to get reliable edge detection */
/* Meanwhile we check that picture is correct, if not, sensor needs to */
/* be changed . */
/* TODO possibly check for insufficient warming */
for (y = 1; y < height - 1; y++)
for (x = 1; x < width - 1; x++)
{
/* if sensor is wrong, picture is black, so max will be low */
if (data[y * width + x] > max)
max = data[y * width + x];
/* sobel y filter */
current =
-data[(y - 1) * width + x + 1] - 2 * data[(y - 1) * width + x] -
data[(y - 1) * width + x - 1] + data[(y + 1) * width + x + 1] +
2 * data[(y + 1) * width + x] + data[(y + 1) * width + x - 1];
if (current < 0)
current = -current;
image[y * width + x] = current;
}
if (DBG_LEVEL > DBG_io2)
{
write_gray_data (image, "find_origin_ysobel.pnm", width, height);
}
/* sensor test */
if (max < 10)
{
DBG (DBG_info,
"find_origin: sensor type needs to be changed (max=%d)\n", max);
*changed = SANE_TRUE;
}
else
{
*changed = SANE_FALSE;
}
/* the edge will be where the white area stops */
/* we average y position of white->black transition on each column */
sum = 0;
for (x = 1; x < width - 1; x++)
{
for (y = 1; y < height - 2; y++)
{
/* egde detection on each line */
if (image[x + (y + 1) * width] - image[x + y * width] >= 20)
{
sum += y;
break;
}
}
}
sum /= width;
DBG (DBG_info, "find_origin: scan area offset=%d lines\n", sum);
/* convert the detected value into max ydpi */
dev->top_offset = (48 * dev->model->max_ydpi) / 300;
/* no we're done */
free (image);
free (data);
/* safe guard test against moving to far toward the top */
if (sum > 11)
{
/* now go back to the white area so that calibration can work on it */
dev->regs[0x35] = 0x0e;
dev->regs[0x3a] = 0x0e;
dev->regs[0xb2] = 0x06; /* no data (0x04) */
if (dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0x36] = 0x21; /* direction reverse (& ~0x08) */
dev->regs[0xe2] = 0x03; /* 0x01 */
/* dev->regs[0xe5] = 0x0d; 0x1c
dev->regs[0xe6] = 0x08; 0x10 080d=2061=1030*2+1 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 2061);
}
else
{
dev->regs[0x11] = 0x3f; /* 0x3b */
dev->regs[0x36] = 0x24; /* direction reverse (& ~0x08) */
dev->regs[0xe2] = 0x07;
/*
dev->regs[0xe5] = 0x06;
dev->regs[0xe6] = 0x04; 406=1030 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 1030);
}
/* move by a fixed amount relative to the 'top' of the scanner */
sanei_rts88xx_set_scan_area (dev->regs, height - sum + 10,
height - sum + 11, 637, 893);
rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
rts8891_commit (dev->devnum, 0x03);
/* wait for the motor to stop */
do
{
status = sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, ®);
}
while ((reg & 0x08) == 0x08);
}
DBG (DBG_proc, "find_origin: exit\n");
return status;
}
/**
* This function detects the left margin (ie x offset of scanning area) by doing
* a grey scan of the very beginning of the sensor and finding the start of the
* white area of calibration zone, which is start of usable pixels.
*/
static SANE_Status
find_margin (struct Rts8891_Device *dev)
{
SANE_Status status = SANE_STATUS_GOOD;
unsigned char *data = NULL;
SANE_Word total;
int startx = 33;
int width = 250;
int x;
int starty = 1;
int height = 1;
SANE_Byte reg = 0xa2;
int timing=0;
DBG (DBG_proc, "find_margin: start\n");
/* increase brightness to have better margin detection */
sanei_rts88xx_write_reg (dev->devnum, LAMP_BRIGHT_REG, ®);
/* maximum gain, offsets untouched */
if (dev->sensor == SENSOR_TYPE_4400 || dev->sensor == SENSOR_TYPE_4400_BARE)
{
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x10, 0x23);
}
else
{
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x28, 0x3b);
}
sanei_rts88xx_set_gain (dev->regs, 0x3f, 0x3f, 0x3f);
/* set scan parameters */
dev->regs[0x33] = 0x01;
dev->regs[0x34] = 0x10;
dev->regs[0x35] = 0x1b;
dev->regs[0x3a] = 0x1b;
dev->regs[0x72] = 0x3a;
dev->regs[0x73] = 0x15;
dev->regs[0x74] = 0x62;
timing=0;
dev->regs[0xc0] = 0xff;
dev->regs[0xc1] = 0xff;
dev->regs[0xc2] = 0xff;
dev->regs[0xc3] = 0x00;
dev->regs[0xc4] = 0xf0;
dev->regs[0xc7] = 0x0f;
dev->regs[0xc8] = 0x00;
dev->regs[0xcb] = 0xe0;
dev->regs[0xcc] = 0xff;
dev->regs[0xcd] = 0xff;
dev->regs[0xce] = 0xff;
dev->regs[0xcf] = 0xe9;
dev->regs[0xd0] = 0xeb;
dev->regs[0xd3] = 0x0c;
dev->regs[0xd4] = 0x0e;
dev->regs[0xd6] = 0xab;
dev->regs[0xd7] = 0x14;
dev->regs[0xd8] = 0xf6;
dev->regs[0xda] = 0xa7; /* XXX STEF XXX l'origine, pas 'bare' */
dev->regs[0xe2] = 0x01;
/* dev->regs[0xe5] = 0x7b;
dev->regs[0xe6] = 0x15; 157b=5499 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 5499);
dev->regs[0xe7] = 0x00;
dev->regs[0xe8] = 0x00;
dev->regs[0xe9] = 0x00;
dev->regs[0xea] = 0x00;
dev->regs[0xeb] = 0x00;
dev->regs[0xec] = 0x00;
dev->regs[0xed] = 0x00;
dev->regs[0xef] = 0x00;
dev->regs[0xf0] = 0x00;
dev->regs[0xf2] = 0x00;
if (dev->sensor == SENSOR_TYPE_XPA || dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0xc0] = 0x00;
dev->regs[0xc1] = 0xf8;
dev->regs[0xc2] = 0x7f;
dev->regs[0xc4] = 0xf8;
dev->regs[0xc5] = 0x7f;
dev->regs[0xc6] = 0x00;
dev->regs[0xc7] = 0xf8;
dev->regs[0xc8] = 0x7f;
dev->regs[0xc9] = 0xff;
dev->regs[0xca] = 0xff;
dev->regs[0xcb] = 0x8f;
dev->regs[0xcd] = 0x07;
dev->regs[0xce] = 0x80;
dev->regs[0xcf] = 0xea;
dev->regs[0xd0] = 0xec;
dev->regs[0xd1] = 0xf7;
dev->regs[0xd2] = 0x00;
dev->regs[0xd3] = 0x10;
dev->regs[0xd4] = 0x12;
dev->regs[0xd7] = 0x31;
}
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
dev->regs[0x13] = 0x39; /* 0x20 */
dev->regs[0x14] = 0xf0; /* 0xf8 */
dev->regs[0x15] = 0x29; /* 0x28 */
dev->regs[0x16] = 0x0f; /* 0x07 */
dev->regs[0x17] = 0x10; /* 0x00 */
dev->regs[0x23] = 0x00; /* 0xff */
dev->regs[0x39] = 0x00; /* 0x02 */
dev->regs[0x85] = 0x46; /* 0x00 */
dev->regs[0x86] = 0x0b; /* 0x06 */
dev->regs[0x87] = 0x8c; /* 0x00 */
dev->regs[0x88] = 0x10; /* 0x06 */
dev->regs[0x8d] = 0x3b; /* 0x00 */
timing=0x00b0;
}
SET_DOUBLE (dev->regs, TIMING_REG, timing);
SET_DOUBLE (dev->regs, TIMING1_REG, timing+1);
SET_DOUBLE (dev->regs, TIMING2_REG, timing+2);
/* set vertical and horizontal start/end positions */
sanei_rts88xx_set_scan_area (dev->regs, starty, starty + height, startx,
startx + width);
/* allocate memory for the data */
total = width * height;
data = (unsigned char *) malloc (total);
if (data == NULL)
{
DBG (DBG_error, "find_margin: failed to allocate %d bytes\n", total);
return SANE_STATUS_NO_MEM;
}
/* gray level scan */
status =
rts8891_simple_scan (dev->devnum, dev->regs, dev->reg_count, 0x0c,
total, data);
if (status != SANE_STATUS_GOOD)
{
free(data);
DBG (DBG_error, "find_margin: failed to scan\n");
return status;
}
if (DBG_LEVEL > DBG_io2)
{
write_gray_data (data, "find_margin.pnm", width, height);
}
/* we search from left to right the first white pixel */
x = 0;
while (x < width && data[x] < MARGIN_LEVEL)
x++;
if (x == width)
{
DBG (DBG_warn, "find_margin: failed to find left margin!\n");
DBG (DBG_warn, "find_margin: using default...\n");
x = 48 + 40;
}
DBG (DBG_info, "find_margin: scan area margin=%d pixels\n", x);
/* convert the detected value into max ydpi */
dev->left_offset = ((x - 40) * dev->model->max_xdpi) / 150;
DBG (DBG_info, "find_margin: left_offset=%d pixels\n", x);
/* no we're done */
free (data);
DBG (DBG_proc, "find_margin: exit\n");
return status;
}
#ifdef FAST_INIT
/*
* This function intializes the device:
* - initial registers values
* - test if at home
* - head parking if needed
*/
static SANE_Status
initialize_device (struct Rts8891_Device *dev)
{
SANE_Status status;
SANE_Byte reg, control;
DBG (DBG_proc, "initialize_device: start\n");
if (dev->initialized == SANE_TRUE)
return SANE_STATUS_GOOD;
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x20, 0x28);
sanei_rts88xx_write_control (dev->devnum, 0x01);
sanei_rts88xx_write_control (dev->devnum, 0x01);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_read_reg (dev->devnum, 0xb0, &control);
DBG (DBG_io, "initialize_device: reg[0xb0]=0x%02x\n", control);
/* we expect to get 0x80 */
if (control != 0x80)
{
DBG (DBG_warn,
"initialize_device: expected reg[0xb0]=0x80, got 0x%02x\n",
control);
/* TODO fail here ??? */
}
/* get lamp status */
status = sanei_rts88xx_get_lamp_status (dev->devnum, dev->regs);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "initialize_device: failed to read lamp status!\n");
return SANE_STATUS_IO_ERROR;
}
DBG (DBG_io, "initialize_device: lamp status=0x%02x\n", dev->regs[0x8e]);
/* sensor type the one for 4470c sold with XPA is slightly different
* than those sold bare, for this model we allways start with xpa type sensor,
* and change it later if we detect black scans in find_origin(). In case the
* attach function set up the sensor type, we don't modify it */
if (dev->sensor == -1)
{
dev->sensor = device->model->sensor;
}
DBG (DBG_info, "initialize_device: initial sensor type is %d\n", dev->sensor);
DBG (DBG_info, "initialize_device: reg[8e]=0x%02x\n", dev->regs[0x8e]);
/* detects if warming up is needed */
if ((dev->regs[0x8e] & 0x60) != 0x60)
{
DBG (DBG_info, "initialize_device: lamp needs warming\n");
dev->needs_warming = SANE_TRUE;
}
else
{
dev->needs_warming = SANE_FALSE;
}
sanei_rts88xx_read_reg (dev->devnum, LAMP_REG, ®);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
/* read scanner present register */
sanei_rts88xx_read_reg (dev->devnum, LINK_REG, &control);
if (control != 0x00 && control != 0x01)
{
DBG (DBG_warn,
"initialize_device: unexpected LINK_REG=0x%02x\n", control);
}
/* head parking if needed */
sanei_rts88xx_read_reg (dev->devnum, CONTROLER_REG, &control);
if (!(control & 0x02))
{
if (park_head (dev, SANE_TRUE) != SANE_STATUS_GOOD)
{
DBG (DBG_error, "initialize_device: failed to park head!\n");
return SANE_STATUS_IO_ERROR;
}
}
/* writes initial register set */
dev->regs[0x00] = 0xe5; /* 0xf5 */
dev->regs[0x02] = 0x1f; /* 0x00 */
dev->regs[0x03] = 0x1f; /* 0x00 */
dev->regs[0x04] = 0x1f; /* 0x00 */
dev->regs[0x05] = 0x1f; /* 0x00 */
dev->regs[0x06] = 0x1f; /* 0x00 */
dev->regs[0x07] = 0x1f; /* 0x00 */
dev->regs[0x08] = 0x0a; /* 0x00 */
dev->regs[0x09] = 0x0a; /* 0x00 */
dev->regs[0x0a] = 0x0a; /* 0x00 */
dev->regs[0x10] = 0x28; /* 0x60 */
dev->regs[0x11] = 0x28; /* 0x1b */
dev->regs[0x13] = 0x20; /* 0x3e */
dev->regs[0x14] = 0xf8; /* 0x00 */
dev->regs[0x15] = 0x28; /* 0x00 */
dev->regs[0x16] = 0x07; /* 0xff */
dev->regs[0x17] = 0x00; /* 0x3e */
dev->regs[0x18] = 0xff; /* 0x00 */
dev->regs[0x1d] = 0x20; /* 0x22 */
/* LCD display */
dev->regs[0x20] = 0x3a; /* 0x00 */
dev->regs[0x21] = 0xf2; /* 0x00 */
dev->regs[0x24] = 0xff; /* 0x00 */
dev->regs[0x25] = 0x00; /* 0xff */
dev->regs[0x30] = 0x00; /* 0x01 */
dev->regs[0x31] = 0x00; /* 0x42 */
dev->regs[0x36] = 0x07; /* 0x00 */
dev->regs[0x39] = 0x00; /* 0x02 */
dev->regs[0x40] = 0x20; /* 0x80 */
dev->regs[0x44] = 0x8c; /* 0x18 */
dev->regs[0x45] = 0x76; /* 0x00 */
dev->regs[0x50] = 0x00; /* 0x20 */
dev->regs[0x51] = 0x00; /* 0x24 */
dev->regs[0x52] = 0x00; /* 0x04 */
dev->regs[0x64] = 0x00; /* 0x10 */
dev->regs[0x68] = 0x00; /* 0x10 */
dev->regs[0x6a] = 0x00; /* 0x01 */
dev->regs[0x70] = 0x00; /* 0x20 */
dev->regs[0x71] = 0x00; /* 0x20 */
dev->regs[0x72] = 0xe1; /* 0x00 */
dev->regs[0x73] = 0x14; /* 0x00 */
dev->regs[0x74] = 0x18; /* 0x00 */
dev->regs[0x75] = 0x15; /* 0x00 */
dev->regs[0x76] = 0x00; /* 0x20 */
dev->regs[0x77] = 0x00; /* 0x01 */
dev->regs[0x79] = 0x00; /* 0x02 */
dev->regs[0x81] = 0x00; /* 0x41 */
dev->regs[0x83] = 0x00; /* 0x10 */
dev->regs[0x84] = 0x00; /* 0x21 */
dev->regs[0x85] = 0x00; /* 0x20 */
dev->regs[0x87] = 0x00; /* 0x20 */
dev->regs[0x88] = 0x00; /* 0x81 */
dev->regs[0x89] = 0x00; /* 0x20 */
dev->regs[0x8a] = 0x00; /* 0x01 */
dev->regs[0x8b] = 0x00; /* 0x01 */
dev->regs[0x8d] = 0x80; /* 0x22 */
dev->regs[0x8e] = 0x68; /* 0x00 */
dev->regs[0x8f] = 0x00; /* 0x40 */
dev->regs[0x90] = 0x00; /* 0x05 */
dev->regs[0x93] = 0x02; /* 0x01 */
dev->regs[0x94] = 0x0e; /* 0x1e */
dev->regs[0xb0] = 0x00; /* 0x80 */
dev->regs[0xb2] = 0x02; /* 0x06 */
dev->regs[0xc0] = 0xff; /* 0x00 */
dev->regs[0xc1] = 0x0f; /* 0x00 */
dev->regs[0xc3] = 0xff; /* 0x00 */
dev->regs[0xc4] = 0xff; /* 0x00 */
dev->regs[0xc5] = 0xff; /* 0x00 */
dev->regs[0xc6] = 0xff; /* 0x00 */
dev->regs[0xc7] = 0xff; /* 0x00 */
dev->regs[0xc8] = 0xff; /* 0x00 */
dev->regs[0xca] = 0x0e; /* 0x00 */
dev->regs[0xcd] = 0xf0; /* 0x00 */
dev->regs[0xce] = 0xff; /* 0x00 */
dev->regs[0xcf] = 0xf5; /* 0x00 */
dev->regs[0xd0] = 0xf7; /* 0x00 */
dev->regs[0xd1] = 0xea; /* 0x00 */
dev->regs[0xd2] = 0x0b; /* 0x00 */
dev->regs[0xd3] = 0x03; /* 0x00 */
dev->regs[0xd4] = 0x05; /* 0x01 */
dev->regs[0xd5] = 0x86; /* 0x06 */
dev->regs[0xd7] = 0x30; /* 0x10 */
dev->regs[0xd8] = 0xf6; /* 0x7a */
dev->regs[0xd9] = 0x80; /* 0x00 */
dev->regs[0xda] = 0x00; /* 0x15 */
dev->regs[0xe2] = 0x01; /* 0x00 */
/* dev->regs[0xe5] = 0x14; 0x0f */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 20);
status = rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
DBG (DBG_proc, "initialize_device: exit\n");
dev->initialized = SANE_TRUE;
return status;
}
#else /* FAST_INIT */
#ifndef UNIT_TESTING
static
#endif
SANE_Status
init_registers (struct Rts8891_Device *dev)
{
int i;
/* initial set written to scanner
* 0xe5 0x41 0x1f 0x1f 0x1f 0x1f 0x1f 0x1f 0x0a 0x0a 0x0a 0x70 0x00 0x00 0x00 0x00 0x60 0x1b 0x08 0x20 0x00 0x20 0x08 0x00 0x00 0x00 0x00 0x00 0x00 0x20 0x00 0x00 0x3a 0xf2 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x10 0x00 0x07 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x20 0x00 0x00 0x00 0x8c 0x76 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x80 0x68 0x00 0x00 0x00 0x00 0x02 0x0e 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xcc 0x27 0x64 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 ---- 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xe0 0x00 0x00 0x00 0x00 0x86 0x1b 0x00 0xff 0x00 0x27 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
*/
/* lengthy set up to make register content obvious, may be some day I will
* group corresponding values between models */
switch (dev->sensor)
{
case SENSOR_TYPE_BARE:
case SENSOR_TYPE_XPA:
dev->regs[0x01] = 0x41;
dev->regs[0x0b] = 0x70;
dev->regs[0x0c] = 0x00;
dev->regs[0x0d] = 0x00;
dev->regs[0x0e] = 0x00;
dev->regs[0x0f] = 0x00;
dev->regs[0x12] = 0x00;
dev->regs[0x13] = 0x20;
dev->regs[0x14] = 0x00;
dev->regs[0x15] = 0x20;
dev->regs[0x16] = 0x00;
dev->regs[0x17] = 0x00;
dev->regs[0x18] = 0x00;
dev->regs[0x19] = 0x00;
dev->regs[0x1a] = 0x00;
dev->regs[0x1b] = 0x00;
dev->regs[0x1c] = 0x00;
dev->regs[0x1d] = 0x20;
dev->regs[0x1e] = 0x00;
dev->regs[0x1f] = 0x00;
/* LCD display */
dev->regs[0x20] = 0x3a;
dev->regs[0x21] = 0xf2;
dev->regs[0x22] = 0x00;
dev->regs[0x23] = 0x00;
dev->regs[0x24] = 0x00;
dev->regs[0x25] = 0x00;
dev->regs[0x26] = 0x00;
dev->regs[0x27] = 0x00;
dev->regs[0x28] = 0x00;
dev->regs[0x29] = 0x00;
dev->regs[0x2a] = 0x00;
dev->regs[0x2b] = 0x00;
dev->regs[0x2c] = 0x00;
dev->regs[0x2d] = 0x00;
dev->regs[0x2e] = 0x00;
dev->regs[0x2f] = 0x00;
dev->regs[0x30] = 0x00;
dev->regs[0x31] = 0x00;
dev->regs[0x32] = 0x00;
dev->regs[0x33] = 0x00;
dev->regs[0x34] = 0x10;
dev->regs[0x35] = 0x00;
dev->regs[0x36] = 0x07;
dev->regs[0x37] = 0x00;
dev->regs[0x38] = 0x00;
dev->regs[0x39] = 0x00;
dev->regs[0x3a] = 0x00;
dev->regs[0x3b] = 0x00;
dev->regs[0x3c] = 0x00;
dev->regs[0x3d] = 0x00;
dev->regs[0x3e] = 0x00;
dev->regs[0x3f] = 0x00;
dev->regs[0x40] = 0x20;
dev->regs[0x41] = 0x00;
dev->regs[0x42] = 0x00;
dev->regs[0x43] = 0x00;
dev->regs[0x44] = 0x8c;
dev->regs[0x45] = 0x76;
dev->regs[0x46] = 0x00;
dev->regs[0x47] = 0x00;
dev->regs[0x48] = 0x00;
dev->regs[0x49] = 0x00;
dev->regs[0x4a] = 0x00;
dev->regs[0x4b] = 0x00;
dev->regs[0x4c] = 0x00;
dev->regs[0x4d] = 0x00;
dev->regs[0x4e] = 0x00;
dev->regs[0x4f] = 0x00;
dev->regs[0x50] = 0x00;
dev->regs[0x51] = 0x00;
dev->regs[0x52] = 0x00;
dev->regs[0x53] = 0x00;
dev->regs[0x54] = 0x00;
dev->regs[0x55] = 0x00;
dev->regs[0x56] = 0x00;
dev->regs[0x57] = 0x00;
dev->regs[0x58] = 0x00;
dev->regs[0x59] = 0x00;
dev->regs[0x5a] = 0x00;
dev->regs[0x5b] = 0x00;
dev->regs[0x5c] = 0x00;
dev->regs[0x5d] = 0x00;
dev->regs[0x5e] = 0x00;
dev->regs[0x5f] = 0x00;
dev->regs[0x64] = 0x00;
dev->regs[0x65] = 0x00;
dev->regs[0x66] = 0x00;
dev->regs[0x67] = 0x00;
dev->regs[0x68] = 0x00;
dev->regs[0x69] = 0x00;
dev->regs[0x6a] = 0x00;
dev->regs[0x6b] = 0x00;
dev->regs[0x6e] = 0x00;
dev->regs[0x6f] = 0x00;
dev->regs[0x70] = 0x00;
dev->regs[0x71] = 0x00;
dev->regs[0x72] = 0x00;
dev->regs[0x73] = 0x00;
dev->regs[0x74] = 0x00;
dev->regs[0x75] = 0x00;
dev->regs[0x76] = 0x00;
dev->regs[0x77] = 0x00;
dev->regs[0x78] = 0x00;
dev->regs[0x79] = 0x00;
dev->regs[0x7a] = 0x00;
dev->regs[0x7b] = 0x00;
dev->regs[0x7c] = 0x00;
dev->regs[0x7d] = 0x00;
dev->regs[0x7e] = 0x00;
dev->regs[0x7f] = 0x00;
dev->regs[0x80] = 0x00;
dev->regs[0x81] = 0x00;
dev->regs[0x82] = 0x00;
dev->regs[0x83] = 0x00;
dev->regs[0x84] = 0x00;
dev->regs[0x85] = 0x00;
dev->regs[0x86] = 0x00;
dev->regs[0x87] = 0x00;
dev->regs[0x88] = 0x00;
dev->regs[0x89] = 0x00;
dev->regs[0x8a] = 0x00;
dev->regs[0x8b] = 0x00;
dev->regs[0x8c] = 0x00;
dev->regs[0x8d] = 0x80;
dev->regs[0x8e] = 0x68;
dev->regs[0x8f] = 0x00;
dev->regs[0x90] = 0x00;
dev->regs[0x91] = 0x00;
dev->regs[0x92] = 0x00;
dev->regs[0x93] = 0x02;
dev->regs[0x94] = 0x0e;
dev->regs[0x95] = 0x00;
dev->regs[0x96] = 0x00;
dev->regs[0x97] = 0x00;
dev->regs[0x98] = 0x00;
dev->regs[0x99] = 0x00;
dev->regs[0x9a] = 0x00;
dev->regs[0x9b] = 0x00;
dev->regs[0x9c] = 0x00;
dev->regs[0x9d] = 0x00;
dev->regs[0x9e] = 0x00;
dev->regs[0x9f] = 0x00;
dev->regs[0xa0] = 0x00;
dev->regs[0xa1] = 0x00;
dev->regs[0xa2] = 0x00;
dev->regs[0xa3] = 0xcc;
dev->regs[0xa4] = 0x27;
dev->regs[0xa5] = 0x64;
dev->regs[0xa6] = 0x00;
dev->regs[0xa7] = 0x00;
dev->regs[0xa8] = 0x00;
dev->regs[0xa9] = 0x00;
dev->regs[0xaa] = 0x00;
dev->regs[0xab] = 0x00;
dev->regs[0xac] = 0x00;
dev->regs[0xad] = 0x00;
dev->regs[0xae] = 0x00;
dev->regs[0xaf] = 0x00;
dev->regs[0xb0] = 0x00;
dev->regs[0xb1] = 0x00;
dev->regs[0xb2] = 0x02;
dev->regs[0xb4] = 0x00;
dev->regs[0xb5] = 0x00;
dev->regs[0xb6] = 0x00;
dev->regs[0xb7] = 0x00;
dev->regs[0xb8] = 0x00;
dev->regs[0xb9] = 0x00;
dev->regs[0xba] = 0x00;
dev->regs[0xbb] = 0x00;
dev->regs[0xbc] = 0x00;
dev->regs[0xbd] = 0x00;
dev->regs[0xbe] = 0x00;
dev->regs[0xbf] = 0x00;
dev->regs[0xc0] = 0x00;
dev->regs[0xc1] = 0x00;
dev->regs[0xc2] = 0x00;
dev->regs[0xc3] = 0x00;
dev->regs[0xc4] = 0x00;
dev->regs[0xc5] = 0x00;
dev->regs[0xc6] = 0x00;
dev->regs[0xc7] = 0x00;
dev->regs[0xc8] = 0x00;
dev->regs[0xc9] = 0x00;
dev->regs[0xca] = 0x00;
dev->regs[0xcb] = 0x00;
dev->regs[0xcc] = 0x00;
dev->regs[0xcd] = 0x00;
dev->regs[0xce] = 0x00;
dev->regs[0xcf] = 0x00;
dev->regs[0xd0] = 0x00;
dev->regs[0xd1] = 0x00;
dev->regs[0xd2] = 0x00;
dev->regs[0xd3] = 0x00;
dev->regs[0xd4] = 0x00;
dev->regs[0xd5] = 0x86;
dev->regs[0xd6] = 0x1b;
dev->regs[0xd7] = 0x00;
dev->regs[0xd8] = 0xff;
dev->regs[0xd9] = 0x00;
dev->regs[0xda] = 0x27;
dev->regs[0xdb] = 0x00;
dev->regs[0xdc] = 0x00;
dev->regs[0xdd] = 0x00;
dev->regs[0xde] = 0x00;
dev->regs[0xdf] = 0x00;
dev->regs[0xe0] = 0x00;
dev->regs[0xe1] = 0x00;
dev->regs[0xe2] = 0x01;
dev->regs[0xe3] = 0x00;
dev->regs[0xe4] = 0x00;
/*dev->regs[0xe5] = 0x14;
dev->regs[0xe6] = 0x00; 14=20 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 20);
dev->regs[0xe7] = 0x00;
dev->regs[0xe8] = 0x00;
dev->regs[0xe9] = 0x00;
dev->regs[0xea] = 0x00;
dev->regs[0xeb] = 0x00;
dev->regs[0xec] = 0x00;
dev->regs[0xed] = 0x00;
dev->regs[0xee] = 0x00;
dev->regs[0xef] = 0x00;
dev->regs[0xf0] = 0x00;
dev->regs[0xf1] = 0x00;
dev->regs[0xf2] = 0x00;
dev->regs[0xf3] = 0x00;
break;
case SENSOR_TYPE_4400:
case SENSOR_TYPE_4400_BARE:
for (i = 0; i < dev->reg_count; i++)
dev->regs[i] = 0x00;
/* 2d may be 0x20 or 0x22: signals something */
dev->regs[0x00] = 0xf5;
dev->regs[0x01] = 0x41;
dev->regs[0x0b] = 0x70;
dev->regs[0x10] = 0x50;
dev->regs[0x11] = 0x03;
dev->regs[0x12] = 0xff;
dev->regs[0x13] = 0x3e;
dev->regs[0x16] = 0xff;
dev->regs[0x17] = 0x3e;
dev->regs[0x1d] = 0x22;
dev->regs[0x22] = 0x01;
dev->regs[0x23] = 0xff;
dev->regs[0x25] = 0xfe;
dev->regs[0x34] = 0x10;
dev->regs[0x39] = 0x02;
dev->regs[0x40] = 0x80;
dev->regs[0x44] = 0x8c;
dev->regs[0x45] = 0x76;
dev->regs[0x50] = 0x20;
dev->regs[0x8b] = 0xff;
dev->regs[0x8c] = 0x3f;
dev->regs[0x8d] = 0x80;
dev->regs[0x8e] = 0x68;
dev->regs[0x93] = 0x01;
dev->regs[0x94] = 0x1e;
dev->regs[0xa3] = 0xcc;
dev->regs[0xa4] = 0x27;
dev->regs[0xa5] = 0x64;
dev->regs[0xb0] = 0x80;
dev->regs[0xb1] = 0x01;
dev->regs[0xb2] = 0x06;
dev->regs[0xd4] = 0x01;
dev->regs[0xd5] = 0x06;
dev->regs[0xd6] = 0x1b;
dev->regs[0xd7] = 0x10;
dev->regs[0xd8] = 0x7a;
dev->regs[0xda] = 0xa7;
dev->regs[0xe5] = 0x0f;
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
dev->regs[0x30] = 0x40; /* 0x00 */
dev->regs[0x31] = 0x80; /* 0x00 */
dev->regs[0x44] = 0x00; /* 0x8c */
dev->regs[0x45] = 0x00; /* 0x76 */
dev->regs[0x52] = 0x04; /* 0x00 */
dev->regs[0x69] = 0x01; /* 0x00 */
dev->regs[0x6a] = 0x10; /* 0x00 */
dev->regs[0x6e] = 0x08; /* 0x00 */
dev->regs[0x70] = 0x40; /* 0x00 */
dev->regs[0x71] = 0x08; /* 0x00 */
dev->regs[0x78] = 0x08; /* 0x00 */
dev->regs[0x79] = 0x11; /* 0x00 */
dev->regs[0x82] = 0x40; /* 0x00 */
dev->regs[0x83] = 0x30; /* 0x00 */
dev->regs[0x84] = 0x45; /* 0x00 */
dev->regs[0x87] = 0x02; /* 0x00 */
dev->regs[0x88] = 0x05; /* 0x00 */
dev->regs[0x8b] = 0x01; /* 0xff */
dev->regs[0x8c] = 0x80; /* 0x3f */
dev->regs[0x8d] = 0x04; /* 0x80 */
dev->regs[0x8e] = 0x10; /* 0x68 */
dev->regs[0x8f] = 0x01; /* 0x00 */
dev->regs[0x90] = 0x80; /* 0x00 */
}
break;
}
return SANE_STATUS_GOOD;
}
/*
* This function intializes the device:
* - initial registers values
* - test if at home
* - head parking if needed
*/
static SANE_Status
init_device (struct Rts8891_Device *dev)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Byte control, reg, id;
SANE_Int i, page;
SANE_Byte buffer[2072];
char message[256 * 6];
SANE_Int val;
/* these commands are used to acces NVRAM through a serial manner */
/* we ignore NVRAM settingsd for now */
SANE_Byte nv_cmd1[21] =
{ 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x28, 0x38,
0x08, 0x18, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x08
};
SANE_Byte nv_cmd2[21] =
{ 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x28, 0x38,
0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x08
};
SANE_Byte nv_cmd3[21] =
{ 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x28, 0x38,
0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x28, 0x38, 0x08
};
SANE_Byte nv_cmd4[21] =
{ 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x08, 0x18,
0x08, 0x18, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x08
};
SANE_Byte nv_cmd5[21] =
{ 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x08, 0x18,
0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x08
};
SANE_Byte nv_cmd6[21] =
{ 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x08, 0x18,
0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x28, 0x38, 0x08
};
SANE_Byte nv_cmd7[21] =
{ 0x28, 0x38, 0x28, 0x38, 0x08, 0x18, 0x28, 0x38, 0x28, 0x38, 0x08, 0x18,
0x28, 0x38, 0x08, 0x18, 0x08, 0x18, 0x08, 0x18, 0x08
};
DBG (DBG_proc, "init_device: start\n");
if (dev->initialized == SANE_TRUE)
return SANE_STATUS_GOOD;
/* read control register, if busy, something will have to be done ... */
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
DBG (DBG_io, "init_device: control=0x%02x\n", control);
/* when just plugged, we expect to get 0x04 */
if (control != 0x04)
{
DBG (DBG_warn, "init_device: expected control=0x04, got 0x%02x\n",
control);
}
/* then read "link" register */
sanei_rts88xx_read_reg (dev->devnum, 0xb0, &control);
DBG (DBG_io, "init_device: link=0x%02x\n", control);
/* we expect to get 0x80 */
if (control != 0x80)
{
DBG (DBG_warn, "init_device: expected link=0x80, got 0x%02x\n",
control);
}
/* reads scanner status */
sanei_rts88xx_get_status (dev->devnum, dev->regs);
DBG (DBG_io, "init_device: status=0x%02x 0x%02x\n", dev->regs[0x10],
dev->regs[0x11]);
/* reads lamp status and sensor information */
sanei_rts88xx_get_lamp_status (dev->devnum, dev->regs);
DBG (DBG_io, "init_device: lamp status=0x%02x\n", dev->regs[0x8e]);
/* initalize sensor with default from model */
dev->sensor = dev->model->sensor;
DBG (DBG_info, "init_device: reg[8e]=0x%02x\n", dev->regs[0x8e]);
/* reset lamp */
sanei_rts88xx_reset_lamp (dev->devnum, dev->regs);
if ((dev->regs[0x8e] & 0x60) != 0x60)
{
DBG (DBG_info, "init_device: lamp needs warming\n");
dev->needs_warming = SANE_TRUE;
}
else
{
dev->needs_warming = SANE_FALSE;
}
/* reads lcd panel status */
sanei_rts88xx_get_lcd (dev->devnum, dev->regs);
DBG (DBG_io, "init_device: lcd panel=0x%02x 0x%02x 0x%02x\n",
dev->regs[0x20], dev->regs[0x21], dev->regs[0x22]);
/* read mainboard ID/scanner present register */
sanei_rts88xx_read_reg (dev->devnum, LINK_REG, &id);
DBG (DBG_io, "init_device: link=0x%02x\n", id);
/* only known ID is currently 0x00 or 0x01 */
if (id != 0x00 && id != 0x01)
{
DBG (DBG_warn, "init_device: expected id=0x00 or 0x01, got 0x%02x\n",
id);
}
/* write 0x00 twice to control */
control = 0x00;
sanei_rts88xx_write_control (dev->devnum, control);
sanei_rts88xx_write_control (dev->devnum, control);
/* read initial register set */
sanei_rts88xx_read_regs (dev->devnum, 0, dev->regs, dev->reg_count);
if (DBG_LEVEL > DBG_io2)
{
sprintf (message, "init_device: initial register settings: ");
for (i = 0; i < dev->reg_count; i++)
sprintf (message + strlen (message), "0x%02x ", dev->regs[i]);
DBG (DBG_io2, "%s\n", message);
}
/* initial sensor guess */
val = dev->regs[0x44] + 256 * dev->regs[0x45];
DBG (DBG_io, "init_device: R44/45=0x%04x\n", val);
if (dev->sensor == SENSOR_TYPE_4400)
{
if(val != 0x00)
{
DBG (DBG_info, "init_device: SENSOR_TYPE_4400 detected\n");
}
else
{
DBG (DBG_info, "init_device: SENSOR_TYPE_4400_BARE detected\n");
dev->sensor = SENSOR_TYPE_4400_BARE;
}
}
init_registers(dev);
sanei_rts88xx_set_offset (dev->regs, 31, 31, 31);
sanei_rts88xx_set_gain (dev->regs, 10, 10, 10);
dev->regs[0] = dev->regs[0] & 0xef;
rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
/* now read button status read_reg(0x1a,2)=0x00 0x00 */
sanei_rts88xx_read_regs (dev->devnum, 0x1a, dev->regs + 0x1a, 2);
/* we expect 0x00 0x00 here */
DBG (DBG_io, "init_device: 0x%02x 0x%02x\n", dev->regs[0x1a],
dev->regs[0x1b]);
dev->regs[0xcf] = 0x00;
dev->regs[0xd0] = 0xe0;
rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
sanei_rts88xx_read_regs (dev->devnum, 0x1a, dev->regs + 0x1a, 2);
/* we expect 0x08 0x00 here */
DBG (DBG_io, "init_device: 0x%02x 0x%02x\n", dev->regs[0x1a],
dev->regs[0x1b]);
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x20, 0x28);
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x28, 0x28);
sanei_rts88xx_cancel (dev->devnum);
sanei_rts88xx_read_reg (dev->devnum, LAMP_REG, &control);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_read_reg (dev->devnum, LINK_REG, &id);
if (id != 0x00 && id != 0x01)
{
DBG (DBG_warn, "init_device: got unexpected id 0x%02x\n", id);
}
dev->regs[0x12] = 0xff;
dev->regs[0x14] = 0xf8;
dev->regs[0x15] = 0x28;
dev->regs[0x16] = 0x07;
dev->regs[0x18] = 0xff;
dev->regs[0x23] = 0xff;
dev->regs[0x24] = 0xff;
dev->regs[0x72] = 0xe1;
dev->regs[0x73] = 0x14;
dev->regs[0x74] = 0x18;
dev->regs[0x75] = 0x15;
dev->regs[0xc0] = 0xff;
dev->regs[0xc1] = 0x0f;
dev->regs[0xc3] = 0xff;
dev->regs[0xc4] = 0xff;
dev->regs[0xc5] = 0xff;
dev->regs[0xc6] = 0xff;
dev->regs[0xc7] = 0xff;
dev->regs[0xc8] = 0xff;
dev->regs[0xca] = 0x0e;
dev->regs[0xcd] = 0xf0;
dev->regs[0xce] = 0xff;
dev->regs[0xcf] = 0xf5;
dev->regs[0xd0] = 0xf7;
dev->regs[0xd1] = 0xea;
dev->regs[0xd2] = 0x0b;
dev->regs[0xd3] = 0x03;
dev->regs[0xd4] = 0x05;
dev->regs[0xd7] = 0x30;
dev->regs[0xd8] = 0xf6;
dev->regs[0xd9] = 0x80;
rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
/* now we are writing and reading back from memory, it is surely a memory test since the written data
* don't look usefull at first glance
*/
reg = 0x06;
sanei_rts88xx_write_reg (dev->devnum, 0x93, ®);
for (i = 0; i < 2072; i++)
{
buffer[i] = i % 97;
}
sanei_rts88xx_set_mem (dev->devnum, 0x81, 0x00, 2072, buffer);
sanei_rts88xx_get_mem (dev->devnum, 0x81, 0x00, 2072, buffer);
/* check the data returned */
for (i = 0; i < 2072; i++)
{
if (buffer[i] != id)
{
DBG (DBG_error,
"init_device: memory at %d is not 0x%02d (0x%02x)\n", i, id,
buffer[i]);
/* XXX STEF XXX return SANE_STATUS_IO_ERROR; */
}
}
DBG (DBG_info, "init_device: memory set #1 passed\n");
/* now test (or set?) #2 */
reg = 0x02;
sanei_rts88xx_write_reg (dev->devnum, 0x93, ®);
for (i = 0; i < 2072; i++)
{
buffer[i] = i % 97;
}
sanei_rts88xx_set_mem (dev->devnum, 0x81, 0x00, 2072, buffer);
sanei_rts88xx_get_mem (dev->devnum, 0x81, 0x00, 2072, buffer);
/* check the data returned */
for (i = 0; i < 970; i++)
{
if (buffer[i] != (i + 0x36) % 97)
{
DBG (DBG_error,
"init_device: memory at %d is not 0x%02x (0x%02x)\n", i,
(i + 0x36) % 97, buffer[i]);
/* XXX STEF XXX return SANE_STATUS_IO_ERROR; */
}
}
for (i = 993; i < 2072; i++)
{
if (buffer[i] != i % 97)
{
DBG (DBG_error,
"init_device: memory at %d is invalid 0x%02x, instead of 0x%02x\n",
i, buffer[i], i % 97);
/* XXX STEF XXX return SANE_STATUS_IO_ERROR; */
}
}
DBG (DBG_info, "init_device: memory set #2 passed\n");
/* now test (or set?) #3 */
reg = 0x01;
sanei_rts88xx_write_reg (dev->devnum, 0x93, ®);
for (i = 0; i < 2072; i++)
{
buffer[i] = i % 97;
}
sanei_rts88xx_set_mem (dev->devnum, 0x81, 0x00, 2072, buffer);
sanei_rts88xx_get_mem (dev->devnum, 0x81, 0x00, 2072, buffer);
/* check the data returned */
for (i = 0; i < 2072; i++)
{
if (buffer[i] != i % 97)
{
DBG (DBG_error,
"init_device: memory at %d is invalid 0x%02x, instead of 0x%02x\n",
i, buffer[i], i % 97);
return SANE_STATUS_IO_ERROR;
}
}
DBG (DBG_info, "init_device: memory set #3 passed\n");
/* we are writing page after page the same pattern, and reading at page 0 until we find we have 'wrapped' */
/* this is surely some memory amount/number pages detection */
dev->regs[0x91] = 0x00; /* page 0 ? */
dev->regs[0x92] = 0x00;
dev->regs[0x93] = 0x01;
rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
/* write pattern at page 0 */
for (i = 0; i < 32; i += 2)
{
buffer[i] = i;
buffer[i + 1] = 0x00;
}
sanei_rts88xx_set_mem (dev->devnum, 0x00, 0x00, 32, buffer);
page = 0;
do
{
/* next page */
page++;
/* fill buffer with pattern for page */
for (i = 0; i < 32; i += 2)
{
buffer[i] = i;
buffer[i + 1] = page;
}
/* write it to memory */
sanei_rts88xx_set_mem (dev->devnum, 0x00, page * 16, 32, buffer);
/* read memory from page 0 */
sanei_rts88xx_get_mem (dev->devnum, 0x00, 0x00, 32, buffer);
for (i = 0; i < 32; i += 2)
{
if (buffer[i] != i)
{
DBG (DBG_error,
"init_device: memory at %d is invalid: 0x%02x instead of 0x%02x\n",
i, buffer[i], i);
return SANE_STATUS_IO_ERROR;
}
if (buffer[i + 1] != page && buffer[i + 1] != 0)
{
DBG (DBG_error,
"init_device: page %d, memory at %d is invalid: 0x%02x instead of 0x%02x\n",
page, i + 1, buffer[i + 1], 0);
return SANE_STATUS_IO_ERROR;
}
}
}
while (buffer[1] == 0);
DBG (DBG_info, "init_device: %d pages detected\n", page - 1);
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
sanei_rts88xx_read_reg (dev->devnum, CONTROLER_REG, &control);
if (!(control & 0x02))
{
if (park_head (dev, SANE_TRUE) != SANE_STATUS_GOOD)
{
DBG (DBG_error, "init_device: failed to park head!\n");
return SANE_STATUS_IO_ERROR;
}
}
reg = 0x80;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
reg = 0xad;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
sanei_rts88xx_read_regs (dev->devnum, 0x14, dev->regs + 0x14, 2);
/* we expect 0xF8 0x28 here */
dev->regs[0x14] = dev->regs[0x14] & 0x7F;
sanei_rts88xx_write_regs (dev->devnum, 0x14, dev->regs + 0x14, 2);
/* reads scanner status */
sanei_rts88xx_get_status (dev->devnum, dev->regs);
DBG (DBG_io, "init_device: status=0x%02x 0x%02x\n", dev->regs[0x10],
dev->regs[0x11]);
sanei_rts88xx_read_reg (dev->devnum, LINK_REG, &control);
DBG (DBG_io, "init_device: link=0x%02x\n", control);
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
sanei_rts88xx_reset_lamp (dev->devnum, dev->regs);
reg = 0x8d;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
reg = 0xad;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
/* here, we are in iddle state */
/* check link status (scanner still plugged) */
sanei_rts88xx_read_reg (dev->devnum, LINK_REG, &control);
DBG (DBG_io, "init_device: link=0x%02x\n", control);
/* this block appears twice */
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
if (control != 0)
{
DBG (DBG_warn, "init_device: unexpected control value 0x%02x\n",
control);
}
sanei_rts88xx_reset_lamp (dev->devnum, dev->regs);
sanei_rts88xx_read_reg (dev->devnum, 0x40, ®);
reg |= 0x80;
sanei_rts88xx_read_reg (dev->devnum, 0x40, ®);
sanei_rts88xx_read_reg (dev->devnum, 0x10, ®);
sanei_rts88xx_read_reg (dev->devnum, 0x14, ®);
reg = 0x78;
sanei_rts88xx_write_reg (dev->devnum, 0x10, ®);
reg = 0x38;
sanei_rts88xx_write_reg (dev->devnum, 0x14, ®);
reg = 0x78;
sanei_rts88xx_write_reg (dev->devnum, 0x10, ®);
reg = 0x01;
sanei_rts88xx_write_reg (dev->devnum, CONTROLER_REG, ®);
/* now we init nvram */
/* this is highly dangerous and thus desactivated
* in sanei_rts88xx_setup_nvram (HAZARDOUS_EXPERIMENT #define) */
sanei_rts88xx_setup_nvram (dev->devnum, 21, nv_cmd1);
sanei_rts88xx_setup_nvram (dev->devnum, 21, nv_cmd2);
sanei_rts88xx_setup_nvram (dev->devnum, 21, nv_cmd3);
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x28, 0x28);
/* second occurence of this block */
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
if (control != 0)
{
DBG (DBG_warn, "init_device: unexpected control value 0x%02x\n",
control);
}
sanei_rts88xx_reset_lamp (dev->devnum, dev->regs);
sanei_rts88xx_read_reg (dev->devnum, 0x40, ®);
reg |= 0x80;
sanei_rts88xx_read_reg (dev->devnum, 0x40, ®);
sanei_rts88xx_read_reg (dev->devnum, 0x10, ®);
sanei_rts88xx_read_reg (dev->devnum, 0x14, ®);
reg = 0x78;
sanei_rts88xx_write_reg (dev->devnum, 0x10, ®);
reg = 0x38;
sanei_rts88xx_write_reg (dev->devnum, 0x14, ®);
reg = 0x78;
sanei_rts88xx_write_reg (dev->devnum, 0x10, ®);
reg = 0x01;
sanei_rts88xx_write_reg (dev->devnum, CONTROLER_REG, ®);
/* nvram again */
/* this is highly dangerous and commented out in rts88xx_lib */
sanei_rts88xx_setup_nvram (dev->devnum, 21, nv_cmd4);
sanei_rts88xx_setup_nvram (dev->devnum, 21, nv_cmd5);
sanei_rts88xx_setup_nvram (dev->devnum, 21, nv_cmd6);
sanei_rts88xx_setup_nvram (dev->devnum, 21, nv_cmd7);
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x28, 0x28);
/* then we do a simple scan to detect a black zone to locate scan area */
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
if (control != 0)
{
DBG (DBG_warn, "init_device: unexpected control value 0x%02x\n",
control);
}
sanei_rts88xx_reset_lamp (dev->devnum, dev->regs);
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
if (control != 0)
{
DBG (DBG_warn, "init_device: unexpected control value 0x%02x\n",
control);
}
sanei_rts88xx_write_control (dev->devnum, 0x01);
sanei_rts88xx_write_control (dev->devnum, 0x01);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
dev->regs[0x12] = 0xff;
dev->regs[0x13] = 0x20;
sanei_rts88xx_write_regs (dev->devnum, 0x12, dev->regs + 0x12, 2);
dev->regs[0x14] = 0xf8;
dev->regs[0x15] = 0x28;
sanei_rts88xx_write_regs (dev->devnum, 0x14, dev->regs + 0x14, 2);
reg = 0;
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_write_control (dev->devnum, 0x00);
sanei_rts88xx_set_status (dev->devnum, dev->regs, 0x28, 0x28);
reg = 0x80;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
dev->regs[0x8b] = 0xff;
dev->regs[0x8c] = 0x3f;
dev->regs[LAMP_REG] = 0xad;
rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
set_lamp_brightness (dev, 0);
dev->initialized = SANE_TRUE;
DBG (DBG_proc, "init_device: exit\n");
return status;
}
#endif /* FAST_INIT */
#ifdef FAST_INIT
/*
* This function detects the scanner
*/
static SANE_Status
detect_device (struct Rts8891_Device *dev)
{
/*
--- 5 74945 bulk_out len 4 wrote 0x80 0xb0 0x00 0x01
--- 6 74946 bulk_in len 1 read 0x80
--- 7 74947 bulk_out len 4 wrote 0x80 0xb3 0x00 0x01
--- 8 74948 bulk_in len 1 read 0x04
--- 9 74949 bulk_out len 4 wrote 0x80 0xb1 0x00 0x01
--- 10 74950 bulk_in len 1 read 0x00
--- 11 74951 bulk_out len 5 wrote 0x88 0xb3 0x00 0x01 0x00
--- 12 74952 bulk_out len 5 wrote 0x88 0xb3 0x00 0x01 0x00
--- 13 74953 bulk_out len 4 wrote 0x80 0x00 0x00 0xf4
--- 14 74954 bulk_in len 192 read 0xf5 0x41 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x70 0x00 0x00 0x00 0x00 0x60 0x1b 0xff 0x3e 0x00 0x00 0xff 0x3e 0x00 0x00 0x00 0x00 0x00 0x22 0x00 0x00 0x00 0x00 0x00 0xff 0x00 0xff 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x42 0x00 0x00 0x10 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x80 0x00 0x00 0x00 0x18 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x20 0x24 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x80 0x10 0x00 0x80 0x00 0x10 0x00 0x01 0x00 0x00 0x10 0x00 0x00 0x20 0x20 0x00 0x00 0x00 0x00 0x20 0x01 0x00 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x41 0x00 0x10 0x21 0x20 0x00 0x20 0x81 0x20 0x01 0x01 0x00 0x22 0x00 0x40 0x05 0x00 0x00 0x01 0x1e 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xcc 0x27 0x64 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x80 0x00 0x06 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
--- 15 74955 bulk_in len 52 read 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x06 0x1b 0x10 0x7a 0x00 0x15 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0f 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
*/
SANE_Status status = SANE_STATUS_GOOD;
SANE_Byte control;
char message[256 * 5];
int i;
DBG (DBG_proc, "detect_device: start\n");
/* read "b0" register */
sanei_rts88xx_read_reg (dev->devnum, 0xb0, &control);
DBG (DBG_io, "detect_device: reg[b0]=0x%02x\n", control);
/* we expect to get 0x80 */
if (control != 0x80)
{
DBG (DBG_warn, "detect_device: expected reg[b0]=0x80, got 0x%02x\n",
control);
/* TODO : fail here ? */
}
/* read control register, if busy, something will have to be done ... */
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
/* when just plugged, we expect to get 0x04 */
if (control != 0x04)
{
DBG (DBG_warn, "detect_device: expected control=0x04, got 0x%02x\n",
control);
/* TODO ok if re-run without plugging */
}
/* read "LINK" register */
sanei_rts88xx_read_reg (dev->devnum, LINK_REG, &control);
DBG (DBG_io, "detect_device: LINK_REG=0x%02x\n", control);
/* we expect to get 0x00 */
if (control != 0x00)
{
DBG (DBG_warn, "detect_device: expected LINK_REG=0x00, got 0x%02x\n",
control);
}
/* write 0x00 twice to control: cancel/stop ? */
control = 0x00;
sanei_rts88xx_write_control (dev->devnum, control);
sanei_rts88xx_write_control (dev->devnum, control);
/* read initial register set */
sanei_rts88xx_read_regs (dev->devnum, 0, dev->regs, dev->reg_count);
if (DBG_LEVEL >= DBG_io2)
{
sprintf (message, "init_device: initial register settings: ");
for (i = 0; i < dev->reg_count; i++)
sprintf (message + strlen (message), "0x%02x ", dev->regs[i]);
sprintf (message + strlen (message), "\n");
DBG (DBG_io2, message);
}
/* initial sensor guess */
val = dev->regs[0x44] + 256 * dev->regs[0x45];
DBG (DBG_io, "detect_device: R44/45=0x%04x\n", val);
if (dev->sensor == SENSOR_TYPE_4400 && val == 0x00)
{
dev->sensor = SENSOR_TYPE_4400_BARE;
DBG (DBG_info, "detect_device: changing to SENSOR_TYPE_4400_BARE\n");
}
DBG (DBG_io, "detect_device: status=0x%02x 0x%02x\n", dev->regs[0x10],
dev->regs[0x11]);
DBG (DBG_io, "detect_device: lamp status=0x%02x\n", dev->regs[0x8e]);
dev->initialized = SANE_FALSE;
DBG (DBG_proc, "detect_device: exit\n");
return status;
}
#endif
/**
* Do dark calibration. We scan a well defined area until average pixel value
* of the black area is about 0x03 for each color channel. This calibration is
* currently done at 75 dpi regardless of the final scan dpi.
*/
static SANE_Status
dark_calibration (struct Rts8891_Device *dev, int mode, int light)
{
SANE_Status status = SANE_STATUS_GOOD;
/* red, green and blue offset, within a 't'op and 'b'ottom value */
int ro = 250, tro = 250, bro = 0;
int bo = 250, tbo = 250, bbo = 0;
int go = 250, tgo = 250, bgo = 0;
unsigned char image[CALIBRATION_SIZE];
float global, ra, ga, ba;
int num = 0;
char name[32];
DBG (DBG_proc, "dark_calibration: start\n");
/* set up sensor specific bottom values */
if (dev->sensor == SENSOR_TYPE_4400)
{
bro = 128;
bgo = 128;
bbo = 128;
}
/* set up starting values */
sanei_rts88xx_set_gain (dev->regs, 0, 0, 0);
sanei_rts88xx_set_scan_area (dev->regs, 1, 2, 4, 4 + CALIBRATION_WIDTH);
sanei_rts88xx_set_status (dev->devnum, dev->regs, mode, light);
dev->regs[0x00] = 0xe5; /* scan */
dev->regs[0x32] = 0x00;
dev->regs[0x33] = 0x03;
dev->regs[0x35] = 0x45;
dev->regs[0x36] = 0x22;
dev->regs[0x3a] = 0x43;
dev->regs[0x8d] = 0x00;
dev->regs[0x8e] = 0x60;
dev->regs[0xb2] = 0x02;
dev->regs[0xc0] = 0x06;
dev->regs[0xc1] = 0xe6;
dev->regs[0xc2] = 0x67;
dev->regs[0xc9] = 0x07;
dev->regs[0xca] = 0x00;
dev->regs[0xcb] = 0xfe;
dev->regs[0xcc] = 0xf9;
dev->regs[0xcd] = 0x19;
dev->regs[0xce] = 0x98;
dev->regs[0xcf] = 0xe8;
dev->regs[0xd0] = 0xea;
dev->regs[0xd1] = 0xf3;
dev->regs[0xd2] = 0x14;
dev->regs[0xd3] = 0x02;
dev->regs[0xd4] = 0x04;
dev->regs[0xd6] = 0x0f;
dev->regs[0xd8] = 0x52;
dev->regs[0xe2] = 0x1f;
/*dev->regs[0xe5] = 0x28; 28=40
dev->regs[0xe6] = 0x00; */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 40);
dev->regs[0xe7] = 0x75;
dev->regs[0xe8] = 0x01;
dev->regs[0xe9] = 0x0b;
dev->regs[0xea] = 0x54;
dev->regs[0xeb] = 0x01;
dev->regs[0xec] = 0x04;
dev->regs[0xed] = 0xb8;
dev->regs[0xef] = 0x03;
dev->regs[0xf0] = 0x70;
dev->regs[0xf2] = 0x01;
/* handling of different sensors */
if (dev->sensor == SENSOR_TYPE_XPA || dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0xc0] = 0x67;
dev->regs[0xc1] = 0x06;
dev->regs[0xc2] = 0xe6;
dev->regs[0xc3] = 0x98;
dev->regs[0xc4] = 0xf9;
dev->regs[0xc5] = 0x19;
dev->regs[0xc6] = 0x67;
dev->regs[0xc7] = 0x06;
dev->regs[0xc8] = 0xe6;
dev->regs[0xc9] = 0x01;
dev->regs[0xca] = 0xf8;
dev->regs[0xcb] = 0xff;
dev->regs[0xcc] = 0x98;
dev->regs[0xcd] = 0xf9;
dev->regs[0xce] = 0x19;
dev->regs[0xcf] = 0xe0;
dev->regs[0xd0] = 0xe2;
dev->regs[0xd1] = 0xeb;
dev->regs[0xd2] = 0x0c;
dev->regs[0xd7] = 0x10;
dev->regs[0xda] = 0xa7;
}
if (dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0x13] = 0x39; /* 0x20 */
dev->regs[0x14] = 0xf0; /* 0xf8 */
dev->regs[0x15] = 0x29; /* 0x28 */
dev->regs[0x16] = 0x0f; /* 0x07 */
dev->regs[0x17] = 0x10; /* 0x00 */
dev->regs[0x23] = 0x00; /* 0xff */
dev->regs[0x35] = 0x48; /* 0x45 */
dev->regs[0x39] = 0x00; /* 0x02 */
dev->regs[0xe2] = 0x0f; /* 0x1f */
/* dev->regs[0xe5] = 0x52; 0x28 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 82); /* 2*40+2 */
dev->regs[0xe7] = 0x0e; /* 0x75 */
dev->regs[0xe9] = 0x0a; /* 0x0b */
dev->regs[0xea] = 0xc2; /* 0x54 */
dev->regs[0xed] = 0xf6; /* 0xb8 */
dev->regs[0xef] = 0x02; /* 0x03 */
dev->regs[0xf0] = 0xa8; /* 0x70 */
}
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
dev->regs[0x13] = 0x39; /* 0x20 */
dev->regs[0x14] = 0xf0; /* 0xf8 */
dev->regs[0x15] = 0x29; /* 0x28 */
dev->regs[0x16] = 0x0f; /* 0x07 */
dev->regs[0x17] = 0x10; /* 0x00 */
dev->regs[0x23] = 0x00; /* 0xff */
dev->regs[0x35] = 0x48; /* 0x45 */
dev->regs[0x39] = 0x00; /* 0x02 */
dev->regs[0xda] = 0xa7; /* 0xa0 */
dev->regs[0xe2] = 0x0f; /* 0x1f */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 82);
dev->regs[0xe7] = 0x0e; /* 0x75 */
dev->regs[0xe9] = 0x0a; /* 0x0b */
dev->regs[0xea] = 0xc2; /* 0x54 */
dev->regs[0xed] = 0xf6; /* 0xb8 */
dev->regs[0xef] = 0x02; /* 0x03 */
dev->regs[0xf0] = 0xa8; /* 0x70 */
}
/* we loop scanning a 637 (1911 bytes) pixels wide area in color mode
* until each black average reaches the desired value */
sanei_rts88xx_set_status (dev->devnum, dev->regs, mode, light);
do
{
/* set scan with the values to try */
sanei_rts88xx_set_offset (dev->regs, ro, go, bo);
DBG (DBG_info, "dark_calibration: trying offsets=(%d,%d,%d)\n", ro, go,
bo);
/* do scan */
status =
rts8891_simple_scan (dev->devnum, dev->regs, dev->reg_count, 0x02,
CALIBRATION_SIZE, image);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "dark_calibration: failed to scan\n");
return status;
}
/* save scanned picture for data debugging */
if (DBG_LEVEL >= DBG_io2)
{
sprintf (name, "dark%03d.pnm", num);
write_rgb_data (name, image, CALIBRATION_WIDTH, 1);
num++;
}
/* we now compute the average of red pixels from the first 15 pixels */
global = average_area (SANE_TRUE, image, 15, 1, &ra, &ga, &ba);
DBG (DBG_info,
"dark_calibration: global=%.2f, channels=(%.2f,%.2f,%.2f)\n",
global, ra, ga, ba);
/* dichotomie ... */
if (abs (ra - DARK_TARGET) < DARK_MARGIN)
{
/* offset is OK */
tro = ro;
bro = ro;
}
else
{ /* NOK */
if (ra > DARK_TARGET)
{
tro = ro;
ro = (tro + bro) / 2;
}
if (ra < DARK_TARGET)
{
bro = ro;
ro = (tro + bro + 1) / 2;
}
}
/* same for blue channel */
if (abs (ba - DARK_TARGET) < DARK_MARGIN)
{
bbo = bo;
tbo = bo;
}
else
{ /* NOK */
if (ba > DARK_TARGET)
{
tbo = bo;
bo = (tbo + bbo) / 2;
}
if (ba < DARK_TARGET)
{
bbo = bo;
bo = (tbo + bbo + 1) / 2;
}
}
/* and for green channel */
if (abs (ga - DARK_TARGET) < DARK_MARGIN)
{
tgo = go;
bgo = go;
}
else
{ /* NOK */
if (ga > DARK_TARGET)
{
tgo = go;
go = (tgo + bgo) / 2;
}
if (ga < DARK_TARGET)
{
bgo = go;
go = (tgo + bgo + 1) / 2;
}
}
}
while ((tro != bro) || (tgo != bgo) || (tbo != bbo));
/* store detected values in device struct */
dev->red_offset = bro;
dev->green_offset = bgo;
dev->blue_offset = bbo;
DBG (DBG_info, "dark_calibration: final offsets=(%d,%d,%d)\n", bro, bgo,
bbo);
DBG (DBG_proc, "dark_calibration: exit\n");
return status;
}
/*
* do gain calibration. We do scans until averaged values of the area match
* the target code. We're doing a dichotomy again.
*/
static SANE_Status
gain_calibration (struct Rts8891_Device *dev, int mode, int light)
{
SANE_Status status = SANE_STATUS_GOOD;
float global, ra, ga, ba;
/* previous values */
int xrg, xbg, xgg;
/* current gains */
int rg, bg, gg;
/* intervals for dichotomy */
int trg, tbg, tgg, bgg, brg, bbg;
int num = 0;
char name[32];
int width = CALIBRATION_WIDTH;
int length = CALIBRATION_SIZE;
unsigned char image[CALIBRATION_SIZE];
int pass = 0;
int timing=0;
int xstart = (dev->left_offset * 75) / dev->model->max_xdpi;
DBG (DBG_proc, "gain_calibration: start\n");
/* set up starting values */
sanei_rts88xx_set_scan_area (dev->regs, 1, 2, xstart, xstart + width);
sanei_rts88xx_set_offset (dev->regs, dev->red_offset, dev->green_offset,
dev->blue_offset);
/* same register set than dark calibration */
dev->regs[0x32] = 0x00;
dev->regs[0x33] = 0x03;
dev->regs[0x35] = 0x45;
dev->regs[0x36] = 0x22;
dev->regs[0x3a] = 0x43;
dev->regs[0x8d] = 0x00;
dev->regs[0x8e] = 0x60;
dev->regs[0xb2] = 0x02;
dev->regs[0xc0] = 0x06;
dev->regs[0xc1] = 0xe6;
dev->regs[0xc2] = 0x67;
dev->regs[0xc9] = 0x07;
dev->regs[0xca] = 0x00;
dev->regs[0xcb] = 0xfe;
dev->regs[0xcc] = 0xf9;
dev->regs[0xcd] = 0x19;
dev->regs[0xce] = 0x98;
dev->regs[0xcf] = 0xe8;
dev->regs[0xd0] = 0xea;
dev->regs[0xd1] = 0xf3;
dev->regs[0xd2] = 0x14;
dev->regs[0xd3] = 0x02;
dev->regs[0xd4] = 0x04;
dev->regs[0xd6] = 0x0f;
dev->regs[0xd8] = 0x52;
dev->regs[0xe2] = 0x1f;
/* dev->regs[0xe5] = 0x28;
dev->regs[0xe6] = 0x00; 28=40 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 40);
dev->regs[0xe7] = 0x75;
dev->regs[0xe8] = 0x01;
dev->regs[0xe9] = 0x0b;
dev->regs[0xea] = 0x54;
dev->regs[0xeb] = 0x01;
dev->regs[0xec] = 0x04;
dev->regs[0xed] = 0xb8;
dev->regs[0xef] = 0x03;
dev->regs[0xf0] = 0x70;
dev->regs[0xf2] = 0x01;
dev->regs[0x72] = 0xe1;
dev->regs[0x73] = 0x14;
dev->regs[0x74] = 0x18;
dev->regs[0xc3] = 0xff;
dev->regs[0xc4] = 0xff;
dev->regs[0xc7] = 0xff;
dev->regs[0xc8] = 0xff;
dev->regs[0xd7] = 0x30;
if (dev->sensor == SENSOR_TYPE_XPA || dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0xc0] = 0x67;
dev->regs[0xc1] = 0x06;
dev->regs[0xc2] = 0xe6;
dev->regs[0xc3] = 0x98;
dev->regs[0xc4] = 0xf9;
dev->regs[0xc5] = 0x19;
dev->regs[0xc6] = 0x67;
dev->regs[0xc7] = 0x06;
dev->regs[0xc8] = 0xe6;
dev->regs[0xc9] = 0x01;
dev->regs[0xca] = 0xf8;
dev->regs[0xcb] = 0xff;
dev->regs[0xcc] = 0x98;
dev->regs[0xcd] = 0xf9;
dev->regs[0xce] = 0x19;
dev->regs[0xcf] = 0xe0;
dev->regs[0xd0] = 0xe2;
dev->regs[0xd1] = 0xeb;
dev->regs[0xd2] = 0x0c;
dev->regs[0xd3] = 0x02;
dev->regs[0xd4] = 0x04;
dev->regs[0xd6] = 0x0f;
dev->regs[0xd7] = 0x10;
dev->regs[0xd8] = 0x52;
dev->regs[0xe2] = 0x1f;
/* dev->regs[0xe5] = 0x28; 0028 -> 40
dev->regs[0xe6] = 0x00; */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 40);
dev->regs[0xe7] = 0x75;
dev->regs[0xe8] = 0x01;
dev->regs[0xe9] = 0x0b;
dev->regs[0xea] = 0x54;
dev->regs[0xeb] = 0x01;
dev->regs[0xec] = 0x04;
dev->regs[0xed] = 0xb8;
dev->regs[0xef] = 0x03;
dev->regs[0xf0] = 0x70;
dev->regs[0xf2] = 0x01;
}
if (dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0x35] = 0x48; /* 0x45 */
/* c5, c6 ? : untouched from previous scan ... */
dev->regs[0xe2] = 0x0f; /* 0x1f */
/* dev->regs[0xe5] = 0x52; */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 82); /* 2*40+2 */
dev->regs[0xe7] = 0x0e; /* 0x75 */
dev->regs[0xe9] = 0x0a; /* 0x0b */
dev->regs[0xea] = 0xc2; /* 0x54 */
dev->regs[0xed] = 0xf6; /* 0xb8 */
dev->regs[0xef] = 0x02; /* 0x03 */
dev->regs[0xf0] = 0xa8; /* 0x70 */
}
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
dev->regs[0x13] = 0x39;
dev->regs[0x14] = 0xf0;
dev->regs[0x15] = 0x29;
dev->regs[0x16] = 0x0f;
dev->regs[0x17] = 0x10;
dev->regs[0x23] = 0x00;
dev->regs[0x35] = 0x48;
dev->regs[0x39] = 0x00;
dev->regs[0xe2] = 0x0f;
SET_DOUBLE (dev->regs, EXPOSURE_REG, 82);
dev->regs[0xe7] = 0x0e;
dev->regs[0xe9] = 0x0a;
dev->regs[0xea] = 0xc2;
dev->regs[0xed] = 0xf6;
dev->regs[0xef] = 0x02;
dev->regs[0xf0] = 0xa8;
timing=0x32;
dev->regs[0x85] = 0x00;
dev->regs[0x86] = 0x06;
dev->regs[0x87] = 0x00;
dev->regs[0x88] = 0x06;
}
SET_DOUBLE (dev->regs, TIMING_REG, timing);
SET_DOUBLE (dev->regs, TIMING1_REG, timing+1);
SET_DOUBLE (dev->regs, TIMING2_REG, timing+2);
/* we loop scanning a 637 (1911 bytes) pixels wide area in color mode until each white average
* reaches the desired value, doing a dichotomy */
rg = 0x1f;
bg = 0x1f;
gg = 0x1f;
/* dichotomy interval */
/* bottom values */
brg = 0;
bgg = 0;
bbg = 0;
/* top values */
trg = 0x1f;
tgg = 0x1f;
tbg = 0x1f;
/* loop on gain calibration until we find until we find stable value
* or we do more than 20 tries */
do
{
/* store current values as previous */
xrg = rg;
xbg = bg;
xgg = gg;
/* set up for scan */
DBG (DBG_info,
"gain_calibration: trying gains=(0x%02x,0x%02x,0x%02x)\n", rg, gg,
bg);
sanei_rts88xx_set_gain (dev->regs, rg, gg, bg);
sanei_rts88xx_set_status (dev->devnum, dev->regs, mode, light);
/* scan on line in RGB */
status =
rts8891_simple_scan (dev->devnum, dev->regs, dev->reg_count, 0x02,
length, image);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "gain_calibration: failed scan data\n");
return status;
}
/* save scanned picture for data debugging */
if (DBG_LEVEL >= DBG_io2)
{
sprintf (name, "gain%03d.pnm", num);
write_rgb_data (name, image, width, 1);
num++;
}
/* we now compute the average pixel value on the scanned line */
/* TODO use computed left margin */
global = average_area (SANE_TRUE, image, width, 1, &ra, &ga, &ba);
DBG (DBG_info,
"gain_calibration: global=%.2f, channels=(%.2f,%.2f,%.2f)\n",
global, ra, ga, ba);
/* dichotomy again ... */
if (abs (ra - RED_GAIN_TARGET) < GAIN_MARGIN)
{
/* gain is OK, it is whitin the tolerance margin */
trg = rg;
brg = rg;
}
else
{ /* average is higher than the desired value */
if (ra > RED_GAIN_TARGET)
{
/* changing gain for another channel alters other output */
/* a stable value is shown by top value == bottom value */
if (trg == brg)
{
brg--;
rg = brg;
}
else
{
/* since we'ra above target value, current gain becomes top value */
trg = rg;
rg = (trg + brg) / 2;
}
}
else /* below target value */
{
if (trg == brg)
{
trg++;
rg = trg;
}
else
{
/* since we are below target, current value is assigned to bottom value */
brg = rg;
rg = (trg + brg + 1) / 2;
}
}
}
/* same for blue channel */
if (abs (ba - BLUE_GAIN_TARGET) < GAIN_MARGIN)
{
bbg = bg;
tbg = bg;
}
else
{ /* NOK */
if (ba > BLUE_GAIN_TARGET)
{
if (tbg == bbg)
{
bbg--;
bg = bbg;
}
else
{
tbg = bg;
bg = (tbg + bbg) / 2;
}
}
else
{
if (tbg == bbg)
{
tbg++;
bg = tbg;
}
else
{
bbg = bg;
bg = (tbg + bbg + 1) / 2;
}
}
}
/* and for green channel */
if (abs (ga - GREEN_GAIN_TARGET) < GAIN_MARGIN)
{
tgg = gg;
bgg = gg;
}
else
{ /* NOK */
if (ga > GREEN_GAIN_TARGET)
{
if (tgg == bgg)
{
bgg--;
gg = bgg;
}
else
{
tgg = gg;
gg = (tgg + bgg) / 2;
}
}
else
{
if (tgg == bgg)
{
tgg++;
gg = tgg;
}
else
{
bgg = gg;
gg = (tgg + bgg + 1) / 2;
}
}
}
pass++;
}
while ((pass < 20)
&& ((trg != brg && xrg != rg)
|| (tgg != bgg && xgg != gg) || (tbg != bbg && xbg != bg)));
/* store detected values in device struct */
dev->red_gain = brg;
dev->green_gain = bgg;
dev->blue_gain = bbg;
DBG (DBG_info, "gain_calibration: gain=(0x%02x,0x%02x,0x%02x)\n", brg, bgg,
bbg);
DBG (DBG_proc, "gain_calibration: exit\n");
return status;
}
/**
* Do fine offset calibration. Scans are done with gains from gain calibration
* at 75 dpi regardless of the dpi of the final scan. We loop scanning a 637
* (1911 bytes) pixels wide area in color mode until each black average
* reaches the desired value (OFFSET_TARGET).
*/
static SANE_Status
offset_calibration (struct Rts8891_Device *dev, int mode, int light)
{
SANE_Status status = SANE_STATUS_GOOD;
/* red, green and blue offset, within a 't'op and 'b'ottom value */
int ro = 250, tro = 250, bro = 123;
int go = 250, tgo = 250, bgo = 123;
int bo = 250, tbo = 250, bbo = 123;
unsigned char image[CALIBRATION_SIZE];
float global, ra, ga, ba;
int num = 0;
char name[32];
DBG (DBG_proc, "offset_calibration: start\n");
/* set up starting values */
/* gains from previous step are a little higher than the one used */
sanei_rts88xx_set_gain (dev->regs, dev->red_gain, dev->green_gain,
dev->blue_gain);
sanei_rts88xx_set_scan_area (dev->regs, 1, 2, 4, 4 + CALIBRATION_WIDTH);
dev->regs[0x32] = 0x00;
dev->regs[0x33] = 0x03;
dev->regs[0x35] = 0x45;
dev->regs[0x36] = 0x22;
dev->regs[0x3a] = 0x43;
dev->regs[0x8d] = 0x00;
dev->regs[0x8e] = 0x60;
dev->regs[0xb2] = 0x02;
dev->regs[0xc0] = 0x06;
dev->regs[0xc1] = 0xe6;
dev->regs[0xc2] = 0x67;
dev->regs[0xc9] = 0x07;
dev->regs[0xca] = 0x00;
dev->regs[0xcb] = 0xfe;
dev->regs[0xcc] = 0xf9;
dev->regs[0xcd] = 0x19;
dev->regs[0xce] = 0x98;
dev->regs[0xcf] = 0xe8;
dev->regs[0xd0] = 0xea;
dev->regs[0xd1] = 0xf3;
dev->regs[0xd2] = 0x14;
dev->regs[0xd3] = 0x02;
dev->regs[0xd4] = 0x04;
dev->regs[0xd6] = 0x0f;
dev->regs[0xd8] = 0x52;
dev->regs[0xe2] = 0x1f;
/* dev->regs[0xe5] = 0x28;
dev->regs[0xe6] = 0x00; 0x0028=40 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 40);
dev->regs[0xe7] = 0x75;
dev->regs[0xe8] = 0x01;
dev->regs[0xe9] = 0x0b;
dev->regs[0xea] = 0x54;
dev->regs[0xeb] = 0x01;
dev->regs[0xec] = 0x04;
dev->regs[0xed] = 0xb8;
dev->regs[0xef] = 0x03;
dev->regs[0xf0] = 0x70;
dev->regs[0xf2] = 0x01;
if (dev->sensor == SENSOR_TYPE_XPA || dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0x72] = 0xe1;
dev->regs[0x73] = 0x14;
dev->regs[0x74] = 0x18;
dev->regs[0xc0] = 0x67;
dev->regs[0xc1] = 0x06;
dev->regs[0xc2] = 0xe6;
dev->regs[0xc3] = 0x98;
dev->regs[0xc4] = 0xf9;
dev->regs[0xc5] = 0x19;
dev->regs[0xc6] = 0x67;
dev->regs[0xc7] = 0x06;
dev->regs[0xc8] = 0xe6;
dev->regs[0xc9] = 0x01;
dev->regs[0xca] = 0xf8;
dev->regs[0xcb] = 0xff;
dev->regs[0xcc] = 0x98;
dev->regs[0xcd] = 0xf9;
dev->regs[0xce] = 0x19;
dev->regs[0xcf] = 0xe0;
dev->regs[0xd0] = 0xe2;
dev->regs[0xd1] = 0xeb;
dev->regs[0xd2] = 0x0c;
dev->regs[0xd7] = 0x10;
}
if (dev->sensor == SENSOR_TYPE_4400)
{
dev->regs[0x35] = 0x48; /* 0x45 */
/* c5,c6 ?? */
dev->regs[0xe2] = 0x0f; /* 0x1f */
/* dev->regs[0xe5] = 0x52; 0x28 */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 82); /* 2*40+2 */
dev->regs[0xe7] = 0x0e; /* 0x75 */
dev->regs[0xe9] = 0x0a; /* 0x0b */
dev->regs[0xea] = 0xc2; /* 0x54 */
dev->regs[0xed] = 0xf6; /* 0xb8 */
dev->regs[0xef] = 0x02; /* 0x03 */
dev->regs[0xf0] = 0xa8; /* 0x70 */
}
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
dev->regs[0x13] = 0x39; /* 0x20 */
dev->regs[0x14] = 0xf0; /* 0xf8 */
dev->regs[0x15] = 0x29; /* 0x28 */
dev->regs[0x16] = 0x0f; /* 0x07 */
dev->regs[0x17] = 0x10; /* 0x00 */
dev->regs[0x23] = 0x00; /* 0xff */
dev->regs[0x35] = 0x48; /* 0x45 */
dev->regs[0x39] = 0x00; /* 0x02 */
dev->regs[0xe2] = 0x0f; /* 0x1f */
SET_DOUBLE (dev->regs, EXPOSURE_REG, 82); /* 2*40+2 */
dev->regs[0xe7] = 0x0e; /* 0x75 */
dev->regs[0xe9] = 0x0a; /* 0x0b */
dev->regs[0xea] = 0xc2; /* 0x54 */
dev->regs[0xed] = 0xf6; /* 0xb8 */
dev->regs[0xef] = 0x02; /* 0x03 */
dev->regs[0xf0] = 0xa8; /* 0x70 */
}
/* we loop scanning a 637 (1911 bytes) pixels wide area in color mode until each black average
* reaches the desired value */
do
{
DBG (DBG_info, "offset_calibration: trying offsets=(%d,%d,%d) ...\n",
ro, go, bo);
sanei_rts88xx_set_offset (dev->regs, ro, go, bo);
sanei_rts88xx_set_status (dev->devnum, dev->regs, mode, light);
rts8891_simple_scan (dev->devnum, dev->regs, dev->reg_count, 0x02,
CALIBRATION_SIZE, image);
/* save scanned picture for data debugging */
if (DBG_LEVEL >= DBG_io2)
{
sprintf (name, "offset%03d.pnm", num);
write_rgb_data (name, image, CALIBRATION_WIDTH, 1);
num++;
}
/* we now compute the average of red pixels from the first 15 pixels */
global = average_area (SANE_TRUE, image, 15, 1, &ra, &ga, &ba);
DBG (DBG_info,
"offset_calibration: global=%.2f, channels=(%.2f,%.2f,%.2f)\n",
global, ra, ga, ba);
/* dichotomie ... */
if (abs (ra - OFFSET_TARGET) < OFFSET_MARGIN)
{
/* offset is OK */
tro = ro;
bro = ro;
}
else
{ /* NOK */
if (ra > OFFSET_TARGET)
{
tro = ro;
ro = (tro + bro) / 2;
}
if (ra < OFFSET_TARGET)
{
bro = ro;
ro = (tro + bro + 1) / 2;
}
}
/* same for blue channel */
if (abs (ba - OFFSET_TARGET) < OFFSET_MARGIN)
{
bbo = bo;
tbo = bo;
}
else
{
if (ba > OFFSET_TARGET)
{
tbo = bo;
bo = (tbo + bbo) / 2;
}
if (ba < OFFSET_TARGET)
{
bbo = bo;
bo = (tbo + bbo + 1) / 2;
}
}
/* and for green channel */
if (abs (ga - OFFSET_TARGET) < OFFSET_MARGIN)
{
tgo = go;
bgo = go;
}
else
{
if (ga > OFFSET_TARGET)
{
tgo = go;
go = (tgo + bgo) / 2;
}
if (ga < OFFSET_TARGET)
{
bgo = go;
go = (tgo + bgo + 1) / 2;
}
}
}
while ((tro != bro) || (tgo != bgo) || (tbo != bbo));
/* store detected values in device struct */
dev->red_offset = bro;
dev->green_offset = bgo;
dev->blue_offset = bbo;
DBG (DBG_proc, "offset_calibration: exit\n");
return status;
}
/*
* do shading calibration
* We scan a 637 pixels by 66 linesxoffset=24 , xend=661, pixels=637
y offset=1 , yend=67, lines =66
*/
#ifndef UNIT_TESTING
static
#endif
SANE_Status
setup_shading_calibration (struct Rts8891_Device *dev, int mode, int *light, int *status1, SANE_Byte * regs)
{
SANE_Status status = SANE_STATUS_GOOD;
int lines = 66;
int exposure=0;
int timing=0;
DBG (DBG_proc, "setup_shading_calibration: start\n");
DBG (DBG_info, "setup_shading_calibration: sensor type is %s (%d)\n", sensor_name (dev->sensor), dev->sensor);
/* set up registers */
/* 0x20/0x28 0x3b/0x3f seen in logs */
*status1 = mode;
if (dev->xdpi > 300)
{
*status1 |= 0x08;
}
/* we default to 75 dpi then override needed registers */
timing=0x00b0;
regs[0x32] = 0x20;
regs[0x33] = 0x83;
regs[0x35] = 0x0e;
regs[0x36] = 0x2c;
regs[0x3a] = 0x0e;
regs[0xe2] = 0x05;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
/* regs[0xe5] = 0xdd; */
exposure=221;
if (dev->sensor == SENSOR_TYPE_XPA || dev->sensor == SENSOR_TYPE_4400)
{
regs[0xc0] = 0x67;
regs[0xc1] = 0x06;
regs[0xc2] = 0xe6;
regs[0xc3] = 0x98;
regs[0xc4] = 0xf9;
regs[0xc5] = 0x19;
regs[0xc6] = 0x67;
regs[0xc7] = 0x06;
regs[0xc8] = 0xe6;
regs[0xc9] = 0x01;
regs[0xca] = 0xf8;
regs[0xcb] = 0xff;
regs[0xcc] = 0x98;
regs[0xcd] = 0xf9;
regs[0xce] = 0x19;
regs[0xcf] = 0xe0;
regs[0xd0] = 0xe2;
regs[0xd1] = 0xeb;
regs[0xd2] = 0x0c;
regs[0xd7] = 0x10;
}
if (dev->sensor == SENSOR_TYPE_4400)
{
*light &= 0xf7; /* clear bit 3 */
regs[0x36] = 0x29; /* 0x2c */
timing=0x0032;
regs[0x85] = 0x00; /* 0x8c */
regs[0x86] = 0x06; /* 0x10 */
regs[0x87] = 0x00; /* 0x18 */
regs[0x88] = 0x06; /* 0x1b */
regs[0x8d] = 0x00; /* 0x77 */
/* c5,c6 ?? */
/* regs[0xd3] = 0x02; 0x0e */
regs[0xd4] = 0x04; /* 0x10 */
regs[0xe2] = 0x02; /* 0x05 */
/* regs[0xe5] = 0xbb;
regs[0xe6] = 0x01; 1bb =443 */
exposure=443;
}
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
regs[0x13] = 0x39; /* 0x20 */
regs[0x14] = 0xf0; /* 0xf8 */
regs[0x15] = 0x29; /* 0x28 */
regs[0x16] = 0x0f; /* 0x07 */
regs[0x17] = 0x10; /* 0x00 */
regs[0x23] = 0x00; /* 0xff */
regs[0x36] = 0x29; /* 0x2c */
regs[0x39] = 0x00; /* 0x02 */
regs[0xe2] = 0x02; /* 0x05 */
exposure=443;
}
switch (dev->xdpi)
{
case 75:
break;
case 150:
/* X resolution related registers */
switch (dev->sensor)
{
case SENSOR_TYPE_XPA:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_XPA for 150 dpi\n");
timing=0x022b;
regs[0x85] = 0x18;
regs[0x86] = 0x1b;
regs[0x87] = 0x30;
regs[0x88] = 0x30;
regs[0x8d] = 0xef;
regs[0xc0] = 0x00;
regs[0xc1] = 0x8e;
regs[0xc2] = 0xff;
regs[0xc3] = 0xff;
regs[0xc4] = 0x71;
regs[0xc5] = 0x00;
regs[0xc6] = 0x00;
regs[0xc7] = 0x8e;
regs[0xc8] = 0xff;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0x1f;
regs[0xcc] = 0xff;
regs[0xcd] = 0x71;
regs[0xce] = 0x00;
regs[0xcf] = 0xe6;
regs[0xd0] = 0xe8;
regs[0xd1] = 0xf6;
regs[0xd2] = 0x17;
/* regs[0xd3] = 0x0b; */
regs[0xd4] = 0x0d;
/* regs[0xe5] = 0xe4; */
exposure=228;
break;
case SENSOR_TYPE_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_BARE for 150 dpi\n");
timing=0x012e;
regs[0x85] = 0x8c;
regs[0x86] = 0x10;
regs[0x87] = 0x18;
regs[0x88] = 0x1b;
regs[0x8d] = 0x77;
regs[0xc0] = 0x80;
regs[0xc1] = 0x87;
regs[0xc2] = 0x7f;
regs[0xc9] = 0x00;
regs[0xcb] = 0x78;
regs[0xcc] = 0x7f;
regs[0xcd] = 0x78;
regs[0xce] = 0x80;
regs[0xcf] = 0xe6;
regs[0xd0] = 0xe8;
regs[0xd1] = 0xf7;
regs[0xd2] = 0x00;
/* regs[0xd3] = 0x0e; */
regs[0xd4] = 0x10;
/* regs[0xe5] = 0xe4; */
exposure=228;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400 for 150 dpi\n");
timing=0x012e;
regs[0x85] = 0x8c;
regs[0x86] = 0x10;
regs[0x87] = 0x18;
regs[0x88] = 0x1b;
regs[0x8d] = 0x77;
regs[0xc0] = 0x00;
regs[0xc1] = 0x8e;
regs[0xc2] = 0xff;
regs[0xc3] = 0xff;
regs[0xc4] = 0x71;
regs[0xc5] = 0x00;
regs[0xc6] = 0x00;
regs[0xc7] = 0x8e;
regs[0xc8] = 0xff;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0x1f;
regs[0xcc] = 0xff;
regs[0xcd] = 0x71;
regs[0xce] = 0x00;
regs[0xcf] = 0xe6;
regs[0xd0] = 0xe8;
regs[0xd1] = 0xf6;
regs[0xd2] = 0x17;
/* regs[0xd3] = 0x0b; */
regs[0xd4] = 0x0d;
exposure=457;
break;
case SENSOR_TYPE_4400_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400_BARE for 150 dpi\n");
regs[0x13] = 0x39; /* 0x20 */
regs[0x14] = 0xf0; /* 0xf8 */
regs[0x15] = 0x29; /* 0x28 */
regs[0x16] = 0x0f; /* 0x07 */
regs[0x17] = 0x10; /* 0x00 */
regs[0x23] = 0x00; /* 0xff */
regs[0x36] = 0x29; /* 0x2c */
regs[0x39] = 0x00; /* 0x02 */
timing=0x00b0;
regs[0x85] = 0x46; /* 0x00 */
regs[0x86] = 0x0b; /* 0x06 */
regs[0x87] = 0x8c; /* 0x00 */
regs[0x88] = 0x10; /* 0x06 */
regs[0x8d] = 0x3b; /* 0x00 */
regs[0xc0] = 0xff; /* 0x06 */
regs[0xc1] = 0x0f; /* 0xe6 */
regs[0xc2] = 0x00; /* 0x67 */
regs[0xc9] = 0x00; /* 0x07 */
regs[0xca] = 0x0e; /* 0x00 */
regs[0xcb] = 0x00; /* 0xfe */
regs[0xcc] = 0x00; /* 0xf9 */
regs[0xcd] = 0xf0; /* 0x19 */
regs[0xce] = 0xff; /* 0x98 */
regs[0xcf] = 0xf5; /* 0xe8 */
regs[0xd0] = 0xf7; /* 0xea */
regs[0xd1] = 0xea; /* 0xf3 */
regs[0xd2] = 0x0b; /* 0x14 */
/* regs[0xd3] = 0x17; 0x02 */
regs[0xd4] = 0x01; /* 0x04 */
regs[0xe2] = 0x02; /* 0x05 */
/* regs[0xe5] = 0x93; regs[0xe6] = 0x03; */
exposure=915;
break;
}
break;
case 300:
switch (dev->sensor)
{
case SENSOR_TYPE_XPA:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_XPA for 300 dpi\n");
timing=0x00b0;
regs[0x85] = 0x46;
regs[0x86] = 0x0b;
regs[0x87] = 0x8c;
regs[0x88] = 0x10;
regs[0x8d] = 0x3b;
regs[0xc0] = 0x00;
regs[0xc1] = 0xff;
regs[0xc2] = 0x0f;
regs[0xc3] = 0xff;
regs[0xc4] = 0x00;
regs[0xc5] = 0xf0;
regs[0xc6] = 0x00;
regs[0xc7] = 0xff;
regs[0xc8] = 0x0f;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0xf1;
regs[0xcc] = 0xff;
regs[0xcd] = 0x00;
regs[0xce] = 0xf0;
regs[0xcf] = 0xed;
regs[0xd0] = 0xef;
regs[0xd1] = 0xe2;
regs[0xd2] = 0x03;
/* regs[0xd3] = 0x17; */
regs[0xd4] = 0x01;
/* regs[0xe5] = 0xc9;
regs[0xe6] = 0x01; 0x01c9=457 */
exposure=457;
break;
case SENSOR_TYPE_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_BARE for 300 dpi\n");
timing=0x012e;
regs[0x85] = 0x8c;
regs[0x86] = 0x10;
regs[0x87] = 0x18;
regs[0x88] = 0x1b;
regs[0x8d] = 0xde;
regs[0x8e] = 0x61; /* low nibble of 8e and 8d are proportional to
the scanned width 1de => 5100 wide scan */
regs[0xc0] = 0xff;
regs[0xc1] = 0x0f;
regs[0xc2] = 0x00;
regs[0xc9] = 0x00;
regs[0xca] = 0x0e;
regs[0xcb] = 0x00;
regs[0xcc] = 0x00;
regs[0xcd] = 0xf0;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0xd1] = 0xea;
regs[0xd2] = 0x0b;
/* regs[0xd3] = 0x17; */
regs[0xd4] = 0x01;
/* regs[0xe5] = 0xc9;
regs[0xe6] = 0x01; 0x1c9=457 */
exposure=457;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400 for 300 dpi\n");
timing=0x022b;
regs[0x85] = 0x18;
regs[0x86] = 0x1b;
regs[0x87] = 0x30;
regs[0x88] = 0x30;
regs[0x8d] = 0xef;
regs[0xc0] = 0x00;
regs[0xc1] = 0xff;
regs[0xc2] = 0x0f;
regs[0xc3] = 0xff;
regs[0xc4] = 0x00;
regs[0xc5] = 0xf0;
regs[0xc6] = 0x00;
regs[0xc7] = 0xff;
regs[0xc8] = 0x0f;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0xf1;
regs[0xcc] = 0xff;
regs[0xcd] = 0x00;
regs[0xce] = 0xf0;
regs[0xcf] = 0xed;
regs[0xd0] = 0xef;
regs[0xd1] = 0xe2;
regs[0xd2] = 0x03;
/* regs[0xd3] = 0x17; */
regs[0xd4] = 0x01;
/* 0x0393 = 915 = 2*457+1 */
exposure=915;
break;
case SENSOR_TYPE_4400_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400_BARE for 300 dpi\n");
return SANE_STATUS_INVAL;
break;
}
break;
case 600:
switch (dev->sensor)
{
case SENSOR_TYPE_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_BARE for 600 dpi\n");
regs[0x34] = 0x10;
regs[0x72] = 0x3a;
regs[0x73] = 0x15;
regs[0x74] = 0x62;
timing=0x022b;
regs[0x85] = 0x18;
regs[0x86] = 0x1b;
regs[0x87] = 0x30;
regs[0x88] = 0x30;
regs[0x8d] = 0xde;
regs[0x8e] = 0x61; /* low nibble of 8e and 8d are proportional to
the scanned width 1de => 5100 wide scan */
regs[0xc0] = 0xff;
regs[0xc1] = 0xff;
regs[0xc2] = 0xff;
regs[0xc3] = 0x00;
regs[0xc4] = 0xf0;
regs[0xc7] = 0x0f;
regs[0xc8] = 0x00;
regs[0xcb] = 0xe0;
regs[0xcc] = 0xff;
regs[0xcd] = 0xff;
regs[0xce] = 0xff;
regs[0xcf] = 0xe9;
regs[0xd0] = 0xeb;
regs[0xd7] = 0x14;
/* regs[0xe5] = 0x93;
regs[0xe6] = 0x03; 0x393=915 = 457*2+1 */
exposure=915;
break;
case SENSOR_TYPE_XPA:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_XPA for 600 dpi\n");
*light &= 0xfb; /* clear bit 2 */
regs[0x16] = 0x07;
regs[0x33] = 0x83; /* 0x86 */
regs[0x34] = 0x10;
regs[0x50] = 0x00; /* 0x18 */
regs[0x64] = 0x01; /* 0x02 */
regs[0x65] = 0x20; /* 0x10 */
regs[0x72] = 0x3a;
regs[0x73] = 0x15;
regs[0x74] = 0x62;
regs[0x85] = 0x00;
regs[0x86] = 0x06;
regs[0x87] = 0x00;
regs[0x88] = 0x06;
regs[0x8d] = 0x00;
regs[0x8e] = 0x60; /* XXX STEF XXX */
regs[0xc0] = 0xf8;
regs[0xc1] = 0x7f;
regs[0xc2] = 0x00;
regs[0xc3] = 0xf8;
regs[0xc4] = 0x7f;
regs[0xc5] = 0x00;
regs[0xc6] = 0xf8;
regs[0xc7] = 0x7f;
regs[0xc8] = 0x00;
regs[0xc9] = 0xff;
regs[0xca] = 0x8f;
regs[0xcb] = 0xff;
regs[0xcc] = 0x07;
regs[0xcd] = 0x80;
regs[0xce] = 0xff;
regs[0xcf] = 0xf2;
regs[0xd0] = 0xf4;
regs[0xd1] = 0xe7;
regs[0xd2] = 0x08;
regs[0xd3] = 0x0e;
regs[0xd4] = 0x10;
regs[0xd7] = 0x31;
timing=0x0032;
exposure=915;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400 for 600 dpi\n");
*light = 0x23;
regs[0x13] = 0x39;
regs[0x14] = 0xf0;
regs[0x15] = 0x29;
regs[0x16] = 0x0f;
regs[0x17] = 0x10;
regs[0x23] = 0x00;
regs[0x34] = 0xf0;
regs[0x36] = 0x29;
regs[0x39] = 0x00;
regs[0x72] = 0x3a;
regs[0x73] = 0x15;
regs[0x74] = 0x62;
timing=0x0426;
regs[0x85] = 0x30;
regs[0x86] = 0x30;
regs[0x87] = 0x60;
regs[0x88] = 0x5a;
regs[0x8d] = 0xde;
regs[0x8e] = 0x61;
regs[0xc0] = 0xf8;
regs[0xc1] = 0x7f;
regs[0xc2] = 0x00;
regs[0xc3] = 0xf8;
regs[0xc4] = 0x7f;
regs[0xc5] = 0x00;
regs[0xc6] = 0xf8;
regs[0xc7] = 0x7f;
regs[0xc8] = 0x00;
regs[0xc9] = 0xff;
regs[0xca] = 0x8f;
regs[0xcb] = 0xff;
regs[0xcc] = 0x07;
regs[0xcd] = 0x80;
regs[0xce] = 0xff;
regs[0xcf] = 0xf2;
regs[0xd0] = 0xf4;
regs[0xd1] = 0xe7;
regs[0xd2] = 0x08;
regs[0xd3] = 0x0e;
regs[0xd4] = 0x10;
regs[0xd7] = 0x31;
regs[0xe2] = 0x02;
exposure=1832;
break;
case SENSOR_TYPE_4400_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400_BARE for 600 dpi\n");
return SANE_STATUS_INVAL;
break;
}
break;
case 1200:
switch (dev->sensor)
{
case SENSOR_TYPE_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_BARE for 1200 dpi\n");
regs[0x34] = 0xf0;
regs[0x40] = 0xa0;
timing=0x0426;
regs[0x85] = 0x30;
regs[0x86] = 0x30;
regs[0x87] = 0x60;
regs[0x88] = 0x5a;
regs[0x8d] = 0xbd;
regs[0x8e] = 0x63;
regs[0xc0] = 0xff;
regs[0xc1] = 0xff;
regs[0xc2] = 0xff;
regs[0xc4] = 0x0f;
regs[0xc5] = 0x00;
regs[0xc6] = 0x00;
regs[0xc7] = 0xf0;
regs[0xc9] = 0x00;
regs[0xca] = 0x0e;
regs[0xcb] = 0x00;
regs[0xcc] = 0xff;
regs[0xcd] = 0xff;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0xd1] = 0xea;
regs[0xd2] = 0x0b;
/* regs[0xd3] = 0x17; */
regs[0xd4] = 0xc1;
regs[0xd7] = 0x14;
regs[0xd8] = 0xa4;
/* regs[0xe5] = 0x28;
regs[0xe6] = 0x07; 0x728=1832=915*2+2 */
exposure=1832;
break;
case SENSOR_TYPE_XPA:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_XPA for 1200 dpi\n");
regs[0x34] = 0x10;
regs[0x40] = 0xa0;
timing=0x00b0;
exposure=1832;
/* XXX STEF XXX */
regs[0x85] = 0x46;
regs[0x86] = 0x0b;
regs[0x87] = 0x8c;
regs[0x88] = 0x10;
regs[0x8e] = 0x3b;
regs[0x8d] = 0x60;
regs[0xc1] = 0xff;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0x33] = 0x83;
regs[0x50] = 0x00;
regs[0x64] = 0x01;
regs[0x65] = 0x20;
regs[0x8d] = 0xbc;
regs[0xc0] = 0xe0;
regs[0xc2] = 0x01;
regs[0xc3] = 0x1f;
regs[0xc4] = 0x00;
regs[0xc5] = 0xfe;
regs[0xc6] = 0xff;
regs[0xc7] = 0xff;
regs[0xc8] = 0x00;
regs[0xc9] = 0x3f;
regs[0xca] = 0xfe;
regs[0xcb] = 0xff;
regs[0xcc] = 0x00;
regs[0xcd] = 0x00;
regs[0xd1] = 0xec;
regs[0xd2] = 0x0d;
regs[0xd3] = 0x05;
regs[0xd4] = 0x67;
regs[0xd7] = 0x10;
regs[0xd8] = 0x52;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400 for 1200 dpi\n");
regs[0x13] = 0x39;
regs[0x14] = 0xf0;
regs[0x15] = 0x29;
regs[0x16] = 0x0f;
regs[0x17] = 0x10;
regs[0x23] = 0x00;
regs[0x34] = 0xf0;
regs[0x36] = 0x29;
regs[0x39] = 0x00;
regs[0x40] = 0xa0;
timing=0x0426;
regs[0x85] = 0x30;
regs[0x86] = 0x30;
regs[0x87] = 0x60;
regs[0x88] = 0x5a;
regs[0x8d] = 0xde;
regs[0x8e] = 0x61;
regs[0xc0] = 0xe0;
regs[0xc1] = 0xff;
regs[0xc2] = 0x01;
regs[0xc3] = 0x1f;
regs[0xc4] = 0x00;
regs[0xc5] = 0xfe;
regs[0xc6] = 0xff;
regs[0xc7] = 0xff;
regs[0xc8] = 0x00;
regs[0xc9] = 0x3f;
regs[0xca] = 0xfe;
regs[0xcb] = 0xff;
regs[0xcc] = 0x00;
regs[0xcd] = 0x00;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0xd1] = 0xec;
regs[0xd2] = 0x0d;
/* regs[0xd3] = 0x05; */
regs[0xd4] = 0x67;
regs[0xd7] = 0x10;
regs[0xd8] = 0x52;
regs[0xe2] = 0x02;
*light = 0x23;
exposure=3665;
break;
case SENSOR_TYPE_4400_BARE:
DBG (DBG_io,
"setup_shading_calibration: setting up SENSOR_TYPE_4400_BARE for 1200 dpi\n");
return SANE_STATUS_INVAL;
break;
}
break;
}
/* apply computed settings */
SET_DOUBLE (regs, EXPOSURE_REG, exposure);
SET_DOUBLE (regs, TIMING_REG, timing);
SET_DOUBLE (regs, TIMING1_REG, timing+1);
SET_DOUBLE (regs, TIMING2_REG, timing+2);
/* in logs, the driver use the computed offset minus 2 */
sanei_rts88xx_set_offset (regs, dev->red_offset, dev->green_offset, dev->blue_offset);
sanei_rts88xx_set_gain (regs, dev->red_gain, dev->green_gain, dev->blue_gain);
sanei_rts88xx_set_scan_area (regs, 1, 1 + lines, dev->xstart, dev->xstart + dev->pixels);
DBG (DBG_proc, "setup_shading_calibration: exit\n");
return status;
}
/*
* do shading calibration
* We scan a 637 pixels by 66 linesxoffset=24 , xend=661, pixels=637
y offset=1 , yend=67, lines =66
*/
static SANE_Status
shading_calibration (struct Rts8891_Device *dev, SANE_Bool color, int mode, int light)
{
SANE_Status status = SANE_STATUS_GOOD;
int width;
int lines = 66;
SANE_Byte format;
int size;
int status1;
int x, y, sum;
SANE_Byte *image = NULL;
FILE *dbg = NULL;
DBG (DBG_proc, "shading_calibration: start\n");
DBG (DBG_info, "shading_calibration: sensor type is %s (%d)\n",
sensor_name (dev->sensor), dev->sensor);
width = dev->pixels;
size = lines * dev->bytes_per_line;
image = (SANE_Byte *) malloc (size);
if (image == NULL)
{
DBG (DBG_error,
"shading_calibration: failed to allocate memory for image\n");
return SANE_STATUS_NO_MEM;
}
if (dev->shading_data != NULL)
free (dev->shading_data);
dev->shading_data = (unsigned char *) malloc (dev->bytes_per_line);
if (dev->shading_data == NULL)
{
free (image);
DBG (DBG_error,
"shading_calibration: failed to allocate memory for data\n");
return SANE_STATUS_NO_MEM;
}
/* set up registers */
status=setup_shading_calibration(dev, mode, &light, &status1, dev->regs);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "shading_calibration: failed to set up registers\n");
free (dev->shading_data);
dev->shading_data = NULL;
free (image);
return status;
}
/* scan shading area */
sanei_rts88xx_set_status (dev->devnum, dev->regs, status1, light);
format = rts8891_data_format (dev->xdpi, dev->sensor);
status = rts8891_simple_scan (dev->devnum, dev->regs, dev->reg_count, format, size, image);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "shading_calibration: failed scan shading area\n");
free (dev->shading_data);
dev->shading_data = NULL;
free (image);
return status;
}
if (DBG_LEVEL >= DBG_io2)
{
dbg = fopen ("shading.pnm", "wb");
if (color)
{
fprintf (dbg, "P6\n%d %d\n255\n", width, lines);
fwrite (image, width * 3, lines, dbg);
}
else
{
fprintf (dbg, "P5\n%d %d\n255\n", width, lines);
fwrite (image, width, lines, dbg);
}
fclose (dbg);
}
/* now we can compute shading calibration data */
/* we average each row */
/* we take the maximum of each row */
/* we skip first 3 lines and last 10 lines due to some bugs */
if (color)
{
width = width * 3;
}
for (x = 0; x < width; x++)
{
sum = 0;
for (y = 3; y < lines - 10; y++)
{
sum += image[x + y * width];
}
dev->shading_data[x] = sum / (lines - 13);
}
if (DBG_LEVEL >= DBG_io2)
{
dbg = fopen ("shading_data.pnm", "wb");
if (color)
{
fprintf (dbg, "P6\n%d %d\n255\n", width / 3, 1);
fwrite (dev->shading_data, width, 1, dbg);
}
else
{
fprintf (dbg, "P5\n%d %d\n255\n", width, 1);
fwrite (dev->shading_data, width, 1, dbg);
}
fclose (dbg);
}
free (image);
DBG (DBG_proc, "shading_calibration: exit\n");
return status;
}
static void
fill_gamma (SANE_Byte * calibration, int *idx, SANE_Word * gamma)
{
int i;
calibration[*idx] = 0;
(*idx)++;
for (i = 0; i < 255; i++)
{
calibration[*idx] = gamma[i];
/* escape 0xaa with 0 */
if (calibration[*idx] == 0xaa)
{
(*idx)++;
calibration[*idx] = 0;
}
(*idx)++;
calibration[*idx] = gamma[i];
/* escape 0xaa with 0 */
if (calibration[*idx] == 0xaa)
{
(*idx)++;
calibration[*idx] = 0;
}
(*idx)++;
}
calibration[*idx] = 0xff;
(*idx)++;
}
/*
* build and send calibration data which contains gamma table and
* shading correction coefficient
*
*/
static SANE_Status
send_calibration_data (struct Rts8891_Session *session)
{
SANE_Status status = SANE_STATUS_GOOD;
int size, width, data_size;
SANE_Byte *calibration = NULL, format, val;
struct Rts8891_Device *dev = session->dev;
int i, idx;
unsigned int value, red_code, blue_code, green_code;
FILE *calib = NULL;
SANE_Word *gamma_r, *gamma_g, *gamma_b;
DBG (DBG_proc, "send_calibration_data: start\n");
/* 675 pixels at 75 DPI, 16 bits values, 3 color channels */
/* 5400 pixels at max sensor 600 dpi */
/* 3 16bits 256 value gamma tables plus start/end markers */
/* must multple of 32 */
data_size = (675 * dev->xdpi) / 75;
width = dev->pixels;
/* effective data calibration size */
size = data_size * 2 * 3 + 3 * (512 + 2);
size = ((size + 31) / 32) * 32;
DBG (DBG_io, "send_calibration_data: size=%d\n", size);
/*
* FORMAT:
* 00
* 512 bytes gamma table (256 16 bit entry)
* FF
* 00
* 512 bytes gamma table (256 16 bit entry)
* FF
* 00
* 512 bytes gamma table (256 16 bit entry)
* FF
* 5400 max shading coefficients at 600 dpi repeated 3 times
* overall size rounded at 32 bytes multiple
* 675 CCD elements at 75 DPI. 16 bit per element. 1 or 3 channels.
* there is a 0xFF marker at end of each coefficients row.
* a gamma table comes first
* 75 DPI: 5600=1542+(675)*2*3+8 ->size rounded to 'upper 32'
* 150 DPI: 9664=675*2*2*3=1542+(675*2)*2*3+22
* 17760 4 +18
* 33952 8 +10
* 65472+896=66368= 16 +26
*
* COEFFICIENT 16 bit value
* first is 00, 0x40, 0x80 or 0xC0 => 10 significant bit
* coeff*average=K => coeff=K/average
*/
calibration = (SANE_Byte *) malloc (size);
if (calibration == NULL)
{
DBG (DBG_error,
"send_calibration_data: failed to allocate memory for calibration data\n");
return SANE_STATUS_NO_MEM;
}
memset (calibration, 0x00, size);
/* fill gamma tables first, markers and static gamma table */
idx = 0;
/* select red gamma table */
if (session->params.format == SANE_FRAME_RGB)
{
/* 3 different gamma table */
gamma_r = session->val[OPT_GAMMA_VECTOR_R].wa;
gamma_g = session->val[OPT_GAMMA_VECTOR_G].wa;
gamma_b = session->val[OPT_GAMMA_VECTOR_B].wa;
}
else
{
/* 3 time the same gamma table */
gamma_r = session->val[OPT_GAMMA_VECTOR].wa;
gamma_g = session->val[OPT_GAMMA_VECTOR].wa;
gamma_b = session->val[OPT_GAMMA_VECTOR].wa;
}
fill_gamma (calibration, &idx, gamma_r);
fill_gamma (calibration, &idx, gamma_g);
fill_gamma (calibration, &idx, gamma_b);
/* compute calibration coefficients */
/* real witdh != 675 --> 637
* shading data calibration starts at 1542. There are 3 rows of 16 bits values
* first row is green calibration
*/
/* to avoid problems with 0xaa values which must be escaped, we change them
* into 0xab values, which unnoticeable on scans */
for (i = 0; i < width; i++)
{
/* average TARGET CODE 3431046 */
/* #define RED_SHADING_TARGET_CODE 3000000
#define GREEN_SHADING_TARGET_CODE 300000
#define BLUE_SHADING_TARGET_CODE 2800000*/
#define RED_SHADING_TARGET_CODE 2800000
#define GREEN_SHADING_TARGET_CODE 2800000
#define BLUE_SHADING_TARGET_CODE 2700000
red_code = RED_SHADING_TARGET_CODE;
green_code = GREEN_SHADING_TARGET_CODE;
blue_code = BLUE_SHADING_TARGET_CODE;
/* target code debug, will be removed for the release */
if (getenv ("RED_CODE") != NULL)
{
red_code = atoi (getenv ("RED_CODE"));
}
if (getenv ("GREEN_CODE") != NULL)
{
blue_code = atoi (getenv ("GREEN_CODE"));
}
if (getenv ("BLUE_CODE") != NULL)
{
green_code = atoi (getenv ("BLUE_CODE"));
}
/* correction coefficient is target code divided by average scanned value
* but it is put in a 16 bits number. Only 10 first bits are significants.
*/
/* first color component red data */
if (gamma_r[dev->shading_data[i * 3]] < 5)
value = 0x8000;
else
value = red_code / gamma_r[dev->shading_data[i * 3]];
val = (SANE_Byte) (value / 256);
if (val == 0xaa)
val++;
calibration[idx + i * 2 + 1] = val;
calibration[idx + i * 2] = (SANE_Byte) (value % 256) & 0xC0;
/* second color component: green data */
if (gamma_r[dev->shading_data[i * 3 + 1]] < 5)
value = 0x8000;
else
value = green_code / gamma_g[dev->shading_data[i * 3 + 1]];
val = (SANE_Byte) (value / 256);
if (val == 0xaa)
val++;
calibration[idx + data_size * 2 + i * 2 + 1] = val;
calibration[idx + data_size * 2 + i * 2] =
(SANE_Byte) (value % 256) & 0xC0;
/* third color component: blue data */
if (gamma_r[dev->shading_data[i * 3 + 2]] < 5)
value = 0x8000;
else
value = blue_code / gamma_b[dev->shading_data[i * 3 + 2]];
val = (SANE_Byte) (value / 256);
if (val == 0xaa)
val++;
calibration[idx + data_size * 4 + i * 2 + 1] = val;
calibration[idx + data_size * 4 + i * 2] =
(SANE_Byte) (value % 256) & 0xC0;
}
/* DEEP TRACING */
if (DBG_LEVEL >= DBG_io2)
{
calib = fopen ("calibration.hex", "wb");
fprintf (calib, "shading_data(%d)=", width);
for (i = 0; i < width * 3; i++)
{
fprintf (calib, "%02x ", dev->shading_data[i]);
}
fprintf (calib, "\n");
fprintf (calib, "write_mem(0x00,%d)=", size);
for (i = 0; i < size; i++)
{
fprintf (calib, "%02x ", calibration[i]);
}
fclose (calib);
}
/* signals color format/divisor from hardware */
format = rts8891_data_format (dev->xdpi, dev->sensor);
status = sanei_rts88xx_write_reg (dev->devnum, 0xd3, &format);
/* for some reason, we have to add 6 to the size for the first write */
/* related to the 6 0xaa in gamma table ? */
if (size > RTS88XX_MAX_XFER_SIZE)
{
status =
sanei_rts88xx_write_mem (dev->devnum, RTS88XX_MAX_XFER_SIZE, 6,
calibration);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error,
"send_calibration_data: failed to write calibration data (part 1)\n");
return status;
}
size -= RTS88XX_MAX_XFER_SIZE;
status =
sanei_rts88xx_write_mem (dev->devnum, size, 0,
calibration + RTS88XX_MAX_XFER_SIZE);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error,
"send_calibration_data: failed to write calibration data (part 2)\n");
return status;
}
}
else
{
status = sanei_rts88xx_write_mem (dev->devnum, size, 6, calibration);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error,
"send_calibration_data: failed to write calibration data\n");
return status;
}
}
/* set mem start */
dev->regs[0x91] = 0x00;
dev->regs[0x92] = 0x00;
sanei_rts88xx_write_regs (dev->devnum, 0x91, dev->regs + 0x91, 2);
free (calibration);
DBG (DBG_proc, "send_calibration_data: exit\n");
return status;
}
/* move at dev->model->min_ydpi dpi up to the scanning area. Which speeds
* up scanning
*/
static SANE_Status
move_to_scan_area (struct Rts8891_Session *session)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Byte control;
SANE_Byte regs[RTS8891_MAX_REGISTERS];
struct Rts8891_Device *dev = session->dev;
SANE_Int distance;
DBG (DBG_proc, "move_to_scan_area: start\n");
/* compute line number to move and fix scan values */
distance = ((dev->ystart - 1) * MOVE_DPI) / dev->ydpi;
dev->ystart = dev->ystart - (distance * dev->ydpi) / MOVE_DPI;
/* extra lines to let head stop */
distance -= 30;
DBG (DBG_proc, "move_to_scan_area: distance=%d, ystart=%d\n", distance,
dev->ystart);
/* then send move command */
rts8891_move (dev, regs, distance, SANE_TRUE);
/* wait for completion */
do
{
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
}
while ((control & 0x08) == 0x08);
DBG (DBG_proc, "move_to_scan_area: exit\n");
return status;
}
/* set up the shadow registers for scan, depending on scan parameters */
/* the ultimate goal is to have no direct access to registers, but to */
/* set them through helper functions */
/* NOTE : I couldn't manage to get scans that really uses gray settings. */
/* The windows driver is allways scanning in color, so we do the same. */
/* For now, the only mode that could be done would be 300 dpi gray scan, */
/* based on the register settings of find_origin() */
#ifndef UNIT_TESTING
static
#endif
SANE_Status
setup_scan_registers (struct Rts8891_Session *session, SANE_Byte *status1, SANE_Byte *status2, SANE_Byte *regs)
{
SANE_Status status = SANE_STATUS_GOOD;
struct Rts8891_Device *dev = session->dev;
int exposure=0;
int timing=0;
/* only software gray modes for now */
if (session->params.format == SANE_FRAME_GRAY
&& (session->dev->model->flags & RTS8891_FLAG_EMULATED_GRAY_MODE) == 0)
{
DBG (DBG_warn,
"setup_scan_registers: native gray modes not implemented for this model, failure expected\n");
}
sanei_rts88xx_set_scan_area (regs, dev->ystart, dev->ystart + dev->lines, dev->xstart, dev->xstart + dev->pixels);
DBG (DBG_info, "setup_scan_registers: xstart=%d, pixels=%d\n", dev->xstart, dev->pixels);
DBG (DBG_info, "setup_scan_registers: ystart=%d, lines =%d\n", dev->ystart, dev->lines);
/* this is full register set from a color preview */
regs[0x00] = regs[0x00] & 0xef;
regs[0x01] = 0x41;
sanei_rts88xx_set_offset (regs, dev->red_offset, dev->green_offset, dev->blue_offset);
sanei_rts88xx_set_gain (regs, dev->red_gain, dev->green_gain, dev->blue_gain);
switch (dev->sensor)
{
case SENSOR_TYPE_4400:
case SENSOR_TYPE_4400_BARE:
*status1 = 0x10;
*status2 = 0x2a;
break;
case SENSOR_TYPE_XPA:
*status1 = 0x20;
*status2 = 0x3f;
break;
default:
*status1 = 0x20;
*status2 = 0x3b;
}
/* default to 75 dpi color scan */
regs[0x0b] = 0x70;
regs[0x0c] = 0x00;
regs[0x0d] = 0x00;
regs[0x0e] = 0x00;
regs[0x0f] = 0x00;
regs[0x12] = 0xff;
regs[0x13] = 0x20;
regs[0x14] = 0xf8;
regs[0x15] = 0x28;
regs[0x16] = 0x01;
regs[0x17] = 0x00;
regs[0x18] = 0xff;
regs[0x19] = 0x00;
regs[0x1a] = 0x00;
regs[0x1b] = 0x00;
regs[0x1c] = 0x00;
regs[0x1d] = 0x20;
regs[0x1e] = 0x00;
regs[0x1f] = 0x00;
/* LCD display */
regs[0x20] = 0x3a;
regs[0x21] = 0xf2;
regs[0x22] = 0x00;
regs[0x23] = 0x80;
regs[0x24] = 0xff;
regs[0x25] = 0x00;
regs[0x26] = 0x00;
regs[0x27] = 0x00;
regs[0x28] = 0x00;
regs[0x29] = 0x00;
regs[0x2a] = 0x00;
regs[0x2b] = 0x00;
regs[0x2c] = 0x00;
regs[0x2d] = 0x00;
regs[0x2e] = 0x00;
regs[0x2f] = 0x00;
regs[0x30] = 0x00;
regs[0x31] = 0x00;
regs[0x32] = 0x20;
regs[0x33] = 0x83;
regs[0x34] = 0x10;
regs[0x35] = 0x47;
regs[0x36] = 0x2c;
regs[0x37] = 0x00;
regs[0x38] = 0x00;
regs[0x39] = 0x02;
regs[0x3a] = 0x43;
regs[0x3b] = 0x00;
regs[0x3c] = 0x00;
regs[0x3d] = 0x00;
regs[0x3e] = 0x00;
regs[0x3f] = 0x00;
regs[0x40] = 0x2c; /* 0x0c -> use shading data */
regs[0x41] = 0x00;
regs[0x42] = 0x00;
regs[0x43] = 0x00;
regs[0x44] = 0x8c;
regs[0x45] = 0x76;
regs[0x46] = 0x00;
regs[0x47] = 0x00;
regs[0x48] = 0x00;
regs[0x49] = 0x00;
regs[0x4a] = 0x00;
regs[0x4b] = 0x00;
regs[0x4c] = 0x00;
regs[0x4d] = 0x00;
regs[0x4e] = 0x00;
regs[0x4f] = 0x00;
regs[0x50] = 0x00;
regs[0x51] = 0x00;
regs[0x52] = 0x00;
regs[0x53] = 0x00;
regs[0x54] = 0x00;
regs[0x55] = 0x00;
regs[0x56] = 0x00;
regs[0x57] = 0x00;
regs[0x58] = 0x00;
regs[0x59] = 0x00;
regs[0x5a] = 0x00;
regs[0x5b] = 0x00;
regs[0x5c] = 0x00;
regs[0x5d] = 0x00;
regs[0x5e] = 0x00;
regs[0x5f] = 0x00;
regs[0x64] = 0x01;
regs[0x65] = 0x20;
regs[0x68] = 0x00;
regs[0x69] = 0x00;
regs[0x6a] = 0x00;
regs[0x6b] = 0x00;
regs[0x6e] = 0x00;
regs[0x6f] = 0x00;
regs[0x70] = 0x00;
regs[0x71] = 0x00;
regs[0x72] = 0xe1;
regs[0x73] = 0x14;
regs[0x74] = 0x18;
regs[0x75] = 0x15;
regs[0x76] = 0x00;
regs[0x77] = 0x00;
regs[0x78] = 0x00;
regs[0x79] = 0x20;
regs[0x7a] = 0x01;
regs[0x7b] = 0x00;
regs[0x7c] = 0x00;
regs[0x7d] = 0x00;
regs[0x7e] = 0x00;
regs[0x7f] = 0x00;
timing=0x00af;
regs[0x84] = 0x00;
regs[0x85] = 0x46;
regs[0x86] = 0x0b;
regs[0x87] = 0x8c;
regs[0x88] = 0x10;
regs[0x8b] = 0xff;
regs[0x8c] = 0x3f;
regs[0x8d] = 0x3b;
regs[0x8e] = 0x60;
regs[0x8f] = 0x00;
regs[0x90] = 0x18; /* 0x1c when shading calibration */
/* overwritten when calibration data is sent */
regs[0x91] = 0x00;
regs[0x92] = 0x00;
regs[0x93] = 0x01;
regs[0x94] = 0x0e;
regs[0x95] = 0x00;
regs[0x96] = 0x00;
regs[0x97] = 0x00;
regs[0x98] = 0x00;
regs[0x99] = 0x00;
regs[0x9a] = 0x00;
regs[0x9b] = 0x00;
regs[0x9c] = 0x00;
regs[0x9d] = 0x00;
regs[0x9e] = 0x00;
regs[0x9f] = 0x00;
regs[0xa0] = 0x00;
regs[0xa1] = 0x00;
regs[0xa2] = 0x00;
regs[0xa3] = 0xcc;
regs[0xa4] = 0x27;
regs[0xa5] = 0x64;
regs[0xa6] = 0x00;
regs[0xa7] = 0x00;
regs[0xa8] = 0x00;
regs[0xa9] = 0x00;
regs[0xaa] = 0x00;
regs[0xab] = 0x00;
regs[0xac] = 0x00;
regs[0xad] = 0x00;
regs[0xae] = 0x00;
regs[0xaf] = 0x00;
regs[0xb0] = 0x00;
regs[0xb1] = 0x00;
regs[0xb2] = 0x02;
regs[0xb4] = 0x00;
regs[0xb5] = 0x00;
regs[0xb6] = 0x00;
regs[0xb7] = 0x00;
regs[0xb8] = 0x00;
regs[0xb9] = 0x00;
regs[0xba] = 0x00;
regs[0xbb] = 0x00;
regs[0xbc] = 0x00;
regs[0xbd] = 0x00;
regs[0xbe] = 0x00;
regs[0xbf] = 0x00;
regs[0xc0] = 0x06;
regs[0xc1] = 0xe6;
regs[0xc2] = 0x67;
regs[0xc3] = 0xff;
regs[0xc4] = 0xff;
regs[0xc5] = 0xff;
regs[0xc6] = 0xff;
regs[0xc7] = 0xff;
regs[0xc8] = 0xff;
regs[0xc9] = 0x07;
regs[0xca] = 0x00;
regs[0xcb] = 0xfe;
regs[0xcc] = 0xf9;
regs[0xcd] = 0x19;
regs[0xce] = 0x98;
regs[0xcf] = 0xe8;
regs[0xd0] = 0xea;
regs[0xd1] = 0xf3;
regs[0xd2] = 0x14;
regs[0xd3] = 0x02;
regs[0xd4] = 0x04;
regs[0xd5] = 0x86;
regs[0xd6] = 0x0f;
regs[0xd7] = 0x30; /* 12303 */
regs[0xd8] = 0x52; /* 0x5230=21040 */
regs[0xd9] = 0xad;
regs[0xda] = 0xa7;
regs[0xdb] = 0x00;
regs[0xdc] = 0x00;
regs[0xdd] = 0x00;
regs[0xde] = 0x00;
regs[0xdf] = 0x00;
regs[0xe0] = 0x00;
regs[0xe1] = 0x00;
regs[0xe2] = 0x0f;
regs[0xe3] = 0x85;
regs[0xe4] = 0x03;
/* regs[0xe5] = 0x52;
regs[0xe6] = 0x00; exposure time 0x0052=82 */
exposure=82;
regs[0xe7] = 0x75;
regs[0xe8] = 0x01;
regs[0xe9] = 0x0b;
regs[0xea] = 0x54;
regs[0xeb] = 0x01;
regs[0xec] = 0x04;
regs[0xed] = 0xb8;
regs[0xee] = 0x00;
regs[0xef] = 0x03;
regs[0xf0] = 0x70;
regs[0xf1] = 0x00;
regs[0xf2] = 0x01;
regs[0xf3] = 0x00;
if (dev->sensor == SENSOR_TYPE_XPA || dev->sensor == SENSOR_TYPE_4400)
{
regs[0xc0] = 0x67;
regs[0xc1] = 0x06;
regs[0xc2] = 0xe6;
regs[0xc3] = 0x98;
regs[0xc4] = 0xf9;
regs[0xc5] = 0x19;
regs[0xc6] = 0x67;
regs[0xc7] = 0x06;
regs[0xc8] = 0xe6;
regs[0xc9] = 0x01;
regs[0xca] = 0xf8;
regs[0xcb] = 0xff;
regs[0xcc] = 0x98;
regs[0xcd] = 0xf9;
regs[0xce] = 0x19;
regs[0xcf] = 0xe0;
regs[0xd0] = 0xe2;
regs[0xd1] = 0xeb;
regs[0xd2] = 0x0c;
regs[0xd7] = 0x10;
}
if (dev->sensor == SENSOR_TYPE_4400)
{
regs[0x13] = 0x39; /* 0x20 */
regs[0x14] = 0xf0; /* 0xf8 */
regs[0x15] = 0x29; /* 0x28 */
regs[0x16] = 0x00; /* 0x01 */
regs[0x17] = 0x10; /* 0x00 */
regs[0x23] = 0x00; /* 0x80 */
regs[0x35] = 0x47; /* 0x45 */
regs[0x36] = 0x29; /* 0x2c */
regs[0x39] = 0x00; /* 0x02 */
timing=0x00af;
regs[0x85] = 0x46; /* 0x8c */
regs[0x86] = 0x0b; /* 0x10 */
regs[0x87] = 0x8c; /* 0x18 */
regs[0x88] = 0x10; /* 0x1b */
regs[0x8d] = 0x3b; /* 0x77 */
regs[0xd3] = 0x02; /* 0x0e */
regs[0xd4] = 0x04; /* 0x10 */
regs[0xe2] = 0x07; /* 0x0f */
regs[0xe3] = 0x84; /* 0x87 */
/*regs[0xe5] = 0xa5; 0x54 */
exposure=165;
regs[0xe7] = 0x0e; /* 0xa8 */
regs[0xe8] = 0x01; /* 0x00 */
regs[0xe9] = 0x0a; /* 0x0b */
regs[0xea] = 0xc2; /* 0x56 */
regs[0xed] = 0xf6; /* 0xba */
regs[0xef] = 0x02; /* 0x03 */
regs[0xf0] = 0xa8; /* 0x72 */
}
if (dev->sensor == SENSOR_TYPE_4400_BARE)
{
regs[0x13] = 0x39; /* 0x20 */
regs[0x14] = 0xf0; /* 0xf8 */
regs[0x15] = 0x29; /* 0x28 */
regs[0x16] = 0x00; /* 0x07 */
regs[0x17] = 0x10; /* 0x00 */
regs[0x23] = 0x00; /* 0xff */
regs[0x35] = 0x47; /* 0x0e */
regs[0x36] = 0x29; /* 0x2c */
regs[0x39] = 0x00; /* 0x02 */
regs[0x3a] = 0x43; /* 0x0e */
regs[0x40] = 0x2c; /* 0x20 */
regs[0x85] = 0x46; /* 0x00 */
regs[0x86] = 0x0b; /* 0x06 */
regs[0x87] = 0x8c; /* 0x00 */
regs[0x88] = 0x10; /* 0x06 */
regs[0x8d] = 0x3b; /* 0x00 */
regs[0x90] = 0x18; /* 0x1c */
regs[0xe2] = 0x07; /* 0x05 */
regs[0xe3] = 0x84; /* 0x00 */
regs[0xe4] = 0x03; /* 0x00 */
timing=0x00af;
exposure=165;
regs[0xe7] = 0x0e; /* 0x00 */
regs[0xe8] = 0x01; /* 0x00 */
regs[0xe9] = 0x0a; /* 0x00 */
regs[0xea] = 0xc2; /* 0x00 */
regs[0xeb] = 0x01; /* 0x00 */
regs[0xec] = 0x04; /* 0x00 */
regs[0xed] = 0xf6; /* 0x00 */
regs[0xef] = 0x02; /* 0x00 */
regs[0xf0] = 0xa8; /* 0x00 */
regs[0xf2] = 0x01; /* 0x00 */
}
switch (dev->xdpi)
{
case 75:
break;
case 150:
regs[0x35] = 0x45;
regs[0x85] = 0x8c;
regs[0x86] = 0x10;
regs[0x87] = 0x18;
regs[0x88] = 0x1b;
regs[0x8d] = 0x77;
regs[0xe3] = 0x87;
/* regs[0xe5] = 0x54;
regs[0xe6] = 0x00; exposure time 0x0054=84 */
exposure=84;
timing=0x012e;
regs[0xe7] = 0xa8;
regs[0xe8] = 0x00;
regs[0xea] = 0x56;
regs[0xed] = 0xba;
regs[0xf0] = 0x72;
switch (dev->sensor)
{
case SENSOR_TYPE_XPA:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_XPA for 150 dpi\n");
regs[0xc0] = 0x00;
regs[0xc1] = 0x8e;
regs[0xc2] = 0xff;
regs[0xc3] = 0xff;
regs[0xc4] = 0x71;
regs[0xc5] = 0x00;
regs[0xc6] = 0x00;
regs[0xc7] = 0x8e;
regs[0xc8] = 0xff;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0x1f;
regs[0xcc] = 0xff;
regs[0xcd] = 0x71;
regs[0xce] = 0x00;
regs[0xcf] = 0xe6;
regs[0xd0] = 0xe8;
regs[0xd1] = 0xf6;
regs[0xd2] = 0x17;
regs[0xd3] = 0x0b;
regs[0xd4] = 0x0d;
break;
case SENSOR_TYPE_BARE:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_BARE for 150 dpi\n");
regs[0xc0] = 0x80;
regs[0xc1] = 0x87;
regs[0xc2] = 0x7f;
regs[0xc9] = 0x00;
regs[0xcb] = 0x78;
regs[0xcc] = 0x7f;
regs[0xcd] = 0x78;
regs[0xce] = 0x80;
regs[0xcf] = 0xe6;
regs[0xd0] = 0xe8;
regs[0xd1] = 0xf7;
regs[0xd2] = 0x00;
regs[0xd3] = 0x0e;
regs[0xd4] = 0x10;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_4400 for 150 dpi\n");
regs[0x35] = 0x47;
exposure=170;
timing=0x012e;
regs[0x85] = 0x8c;
regs[0x86] = 0x10;
regs[0x87] = 0x18;
regs[0x88] = 0x1b;
regs[0x8d] = 0x77;
regs[0xc0] = 0x00;
regs[0xc1] = 0x8e;
regs[0xc2] = 0xff;
regs[0xc3] = 0xff;
regs[0xc4] = 0x71;
regs[0xc5] = 0x00;
regs[0xc6] = 0x00;
regs[0xc7] = 0x8e;
regs[0xc8] = 0xff;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0x1f;
regs[0xcc] = 0xff;
regs[0xcd] = 0x71;
regs[0xce] = 0x00;
regs[0xcf] = 0xe6;
regs[0xd0] = 0xe8;
regs[0xd1] = 0xf6;
regs[0xd2] = 0x17;
regs[0xd3] = 0x0b;
regs[0xd4] = 0x0d;
regs[0xe3] = 0x86;
regs[0xe7] = 0x00;
regs[0xe8] = 0x1c;
regs[0xe9] = 0x01;
regs[0xea] = 0x0a;
regs[0xeb] = 0xc4;
regs[0xec] = 0x01;
regs[0xed] = 0x04;
regs[0xee] = 0xf8;
regs[0xef] = 0x00;
regs[0xf0] = 0x02;
regs[0xf2] = 0x00;
break;
}
break;
case 300:
switch (dev->sensor)
{
case SENSOR_TYPE_XPA:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_XPA for 300 dpi\n");
regs[0x35] = 0x0e; /* fast ? */
regs[0x3a] = 0x0e;
regs[0x85] = 0x18;
regs[0x86] = 0x1b;
regs[0x87] = 0x30;
regs[0x88] = 0x30;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0x8d] = 0xef;
regs[0xc0] = 0x00;
regs[0xc1] = 0xff;
regs[0xc2] = 0x0f;
regs[0xc3] = 0xff;
regs[0xc4] = 0x00;
regs[0xc5] = 0xf0;
regs[0xc6] = 0x00;
regs[0xc7] = 0xff;
regs[0xc8] = 0x0f;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0xf1;
regs[0xcc] = 0xff;
regs[0xcd] = 0x00;
regs[0xce] = 0xf0;
regs[0xcf] = 0xed;
regs[0xd0] = 0xef;
regs[0xd1] = 0xe2;
regs[0xd2] = 0x03;
regs[0xd3] = 0x17;
regs[0xd4] = 0x01;
regs[0xe2] = 0x07;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
timing=0x022b;
exposure=342;
regs[0xf2] = 0x00;
break;
case SENSOR_TYPE_BARE:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_BARE for 300 dpi\n");
regs[0x35] = 0x0e; /* fast ? */
regs[0x3a] = 0x0e;
timing=0x022b;
regs[0x85] = 0x18;
regs[0x86] = 0x1b;
regs[0x87] = 0x30;
regs[0x88] = 0x30;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0x8d] = 0xf0;
regs[0x8e] = 0x60; /* low nibble of 8e and 8d are proportional to
the scanned width 1de => 5100 wide scan */
regs[0xc0] = 0xff;
regs[0xc1] = 0x0f;
regs[0xc2] = 0x00;
regs[0xc9] = 0x00;
regs[0xca] = 0x0e;
regs[0xcb] = 0x00;
regs[0xcc] = 0x00;
regs[0xcd] = 0xf0;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0xd1] = 0xea;
regs[0xd2] = 0x0b;
regs[0xd3] = 0x17;
regs[0xd4] = 0x01;
regs[0xe2] = 0x07;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
exposure=342;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_4400 for 300 dpi\n");
regs[0x11] = 0x22;
regs[0x35] = 0x0e;
regs[0x3a] = 0x0e;
regs[0x85] = 0x18;
regs[0x86] = 0x1b;
regs[0x87] = 0x30;
regs[0x88] = 0x30;
regs[0x8d] = 0xef;
regs[0xc0] = 0x00;
regs[0xc1] = 0xff;
regs[0xc2] = 0x0f;
regs[0xc3] = 0xff;
regs[0xc4] = 0x00;
regs[0xc5] = 0xf0;
regs[0xc6] = 0x00;
regs[0xc7] = 0xff;
regs[0xc8] = 0x0f;
regs[0xc9] = 0xff;
regs[0xca] = 0xff;
regs[0xcb] = 0xf1;
regs[0xcc] = 0xff;
regs[0xcd] = 0x00;
regs[0xce] = 0xf0;
regs[0xcf] = 0xed;
regs[0xd0] = 0xef;
regs[0xd1] = 0xe2;
regs[0xd2] = 0x03;
regs[0xd3] = 0x17;
regs[0xd4] = 0x01;
regs[0xe2] = 0x03;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
timing=0x022b;
exposure=686;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
break;
}
break;
case 600:
*status1 = 0x28;
switch (dev->sensor)
{
case SENSOR_TYPE_BARE:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_BARE for 600 dpi\n");
regs[0x34] = 0xf0;
regs[0x35] = 0x1b;
regs[0x36] = 0x29;
regs[0x3a] = 0x1b;
regs[0x72] = 0x3a;
regs[0x73] = 0x15;
regs[0x74] = 0x62;
timing=0x0425;
regs[0x85] = 0x30;
regs[0x86] = 0x30;
regs[0x87] = 0x60;
regs[0x88] = 0x5a;
regs[0x8d] = 0xde;
regs[0x8e] = 0x61; /* low nibble of 8e and 8d are proportional to
the scanned width 1de => 5100 wide scan */
regs[0xc0] = 0xff;
regs[0xc1] = 0xff;
regs[0xc2] = 0xff;
regs[0xc3] = 0x00;
regs[0xc4] = 0xf0;
regs[0xc7] = 0x0f;
regs[0xc8] = 0x00;
regs[0xcb] = 0xe0;
regs[0xcc] = 0xff;
regs[0xcd] = 0xff;
regs[0xce] = 0xff;
regs[0xcf] = 0xe9;
regs[0xd0] = 0xeb;
regs[0xd7] = 0x14;
regs[0xe2] = 0x01;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
/* regs[0xe5] = 0xbd;
regs[0xe6] = 0x0a; exposure time = 0x0abd=2749 (5500/2-1) */
exposure=2749;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
break;
case SENSOR_TYPE_XPA:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_XPA for 600 dpi\n");
*status2 = 0x3b;
regs[0x33] = 0x83; /* 0x86 */
regs[0x34] = 0xf0;
regs[0x35] = 0x1b;
regs[0x36] = 0x29;
regs[0x3a] = 0x1b;
regs[0x40] = 0x2c; /* 0x24 */
regs[0x50] = 0x00; /* 0x18 */
regs[0x64] = 0x01; /* 0x02 */
regs[0x65] = 0x20; /* 0x10 */
regs[0x72] = 0x3a;
regs[0x73] = 0x15;
regs[0x74] = 0x62;
regs[0x85] = 0x30;
regs[0x86] = 0x30;
regs[0x87] = 0x60;
regs[0x88] = 0x5a;
regs[0x8d] = 0xde;
regs[0x8e] = 0x61; /* 25054 */
regs[0xc0] = 0xf8;
regs[0xc1] = 0x7f;
regs[0xc2] = 0x00;
regs[0xc3] = 0xf8;
regs[0xc4] = 0x7f;
regs[0xc5] = 0x00;
regs[0xc6] = 0xf8;
regs[0xc7] = 0x7f;
regs[0xc8] = 0x00;
regs[0xc9] = 0xff;
regs[0xca] = 0x8f;
regs[0xcb] = 0xff;
regs[0xcc] = 0x07;
regs[0xcd] = 0x80;
regs[0xce] = 0xff;
regs[0xcf] = 0xf2;
regs[0xd0] = 0xf4;
regs[0xd1] = 0xe7;
regs[0xd2] = 0x08;
regs[0xd3] = 0x02;
regs[0xd4] = 0x10;
regs[0xd7] = 0x31;
regs[0xe2] = 0x01;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
exposure=2749;
timing=0x0425;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_4400 for 600 dpi\n");
*status1 = 0x10;
*status2 = 0x23;
regs[0x13] = 0x39;
regs[0x14] = 0xf0;
regs[0x15] = 0x29;
regs[0x16] = 0x00;
regs[0x17] = 0x10;
regs[0x23] = 0x00;
regs[0x34] = 0xf0;
regs[0x35] = 0x1b;
regs[0x36] = 0x29;
regs[0x39] = 0x00;
regs[0x3a] = 0x1b;
regs[0x72] = 0x3a;
regs[0x73] = 0x15;
regs[0x74] = 0x62;
regs[0x85] = 0x30;
regs[0x86] = 0x30;
regs[0x87] = 0x60;
regs[0x88] = 0x5a;
regs[0x8d] = 0xde;
regs[0x8e] = 0x61;
regs[0xc0] = 0xf8;
regs[0xc1] = 0x7f;
regs[0xc2] = 0x00;
regs[0xc3] = 0xf8;
regs[0xc4] = 0x7f;
regs[0xc5] = 0x00;
regs[0xc6] = 0xf8;
regs[0xc7] = 0x7f;
regs[0xc8] = 0x00;
regs[0xc9] = 0xff;
regs[0xca] = 0x8f;
regs[0xcb] = 0xff;
regs[0xcc] = 0x07;
regs[0xcd] = 0x80;
regs[0xce] = 0xff;
regs[0xcf] = 0xf2;
regs[0xd0] = 0xf4;
regs[0xd1] = 0xe7;
regs[0xd2] = 0x08;
regs[0xd3] = 0x0e;
regs[0xd4] = 0x10;
regs[0xd7] = 0x31;
regs[0xe2] = 0x01;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
timing=0x0425;
exposure=2749;
break;
case SENSOR_TYPE_4400_BARE:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_4400_BARE for 600 dpi\n");
return SANE_STATUS_INVAL;
break;
}
break;
case 1200:
*status1 = 0x28;
switch (dev->sensor)
{
case SENSOR_TYPE_BARE:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_BARE for 1200 dpi\n");
regs[0x34] = 0xf0;
regs[0x35] = 0x1b;
regs[0x36] = 0x29;
regs[0x3a] = 0x1b;
regs[0x40] = 0xac;
timing=0x081a;
regs[0x85] = 0x60;
regs[0x86] = 0x5a;
regs[0x87] = 0xc0;
regs[0x88] = 0xae;
regs[0x8d] = 0xbd; /* about twice the 600 dpi values */
regs[0x8e] = 0x63; /* low nibble of 8e and 8d are proportional to
the scanned width 3b5 => 10124 wide scan */
regs[0xc0] = 0xff;
regs[0xc1] = 0xff;
regs[0xc2] = 0xff;
regs[0xc4] = 0x0f;
regs[0xc5] = 0x00;
regs[0xc6] = 0x00;
regs[0xc7] = 0xf0;
regs[0xc9] = 0x00;
regs[0xca] = 0x0e;
regs[0xcb] = 0x00;
regs[0xcc] = 0xff;
regs[0xcd] = 0xff;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0xd1] = 0xea;
regs[0xd2] = 0x0b;
regs[0xd3] = 0x17;
regs[0xd4] = 0xc1;
regs[0xd7] = 0x14;
regs[0xd8] = 0xa4;
regs[0xe2] = 0x01;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
/* regs[0xe5] = 0x7b;
regs[0xe6] = 0x15; exposure time = 0x157b=5499 */
exposure=5499;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
break;
case SENSOR_TYPE_XPA:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_XPA for 1200 dpi\n");
*status2 = 0x3f;
regs[0x34] = 0xf0;
regs[0x35] = 0x1b;
regs[0x36] = 0x29;
regs[0x3a] = 0x1b;
regs[0x85] = 0x60;
regs[0x86] = 0x5a;
regs[0x87] = 0xc0;
regs[0x8a] = 0x08; /* 81c=2076 */
regs[0xc1] = 0xff;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0xe2] = 0x01;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
timing=0x081a;
exposure=5499;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
regs[0x33] = 0x83;
regs[0x40] = 0xac;
regs[0x50] = 0x00;
regs[0x64] = 0x01;
regs[0x65] = 0x20;
regs[0x88] = 0xae;
regs[0x8d] = 0xbc;
regs[0x8e] = 0x63;
regs[0xc0] = 0xe0;
regs[0xc2] = 0x01;
regs[0xc3] = 0x1f;
regs[0xc4] = 0x00;
regs[0xc5] = 0xfe;
regs[0xc6] = 0xff;
regs[0xc7] = 0xff;
regs[0xc8] = 0x00;
regs[0xc9] = 0x3f;
regs[0xca] = 0xfe;
regs[0xcb] = 0xff;
regs[0xcc] = 0x00;
regs[0xcd] = 0x00;
regs[0xd1] = 0xec;
regs[0xd2] = 0x0d;
regs[0xd3] = 0x05;
regs[0xd4] = 0x67;
regs[0xd7] = 0x10;
regs[0xd8] = 0x52;
break;
case SENSOR_TYPE_4400:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_4400 for 1200 dpi\n");
regs[0x13] = 0x39;
regs[0x14] = 0xf0;
regs[0x15] = 0x29;
regs[0x16] = 0x00;
regs[0x17] = 0x10;
regs[0x23] = 0x00;
regs[0x33] = 0x86;
regs[0x34] = 0xf0;
regs[0x35] = 0x0e;
regs[0x39] = 0x00;
regs[0x3a] = 0x0e;
regs[0x40] = 0xac;
regs[0x64] = 0x02;
regs[0x65] = 0x10;
timing=0x081a;
regs[0x85] = 0x60;
regs[0x86] = 0x5a;
regs[0x87] = 0xc0;
regs[0x88] = 0xae;
regs[0x8d] = 0xbc;
regs[0x8e] = 0x63;
regs[0xc0] = 0xe0;
regs[0xc1] = 0xff;
regs[0xc2] = 0x01;
regs[0xc3] = 0x1f;
regs[0xc4] = 0x00;
regs[0xc5] = 0xfe;
regs[0xc6] = 0xff;
regs[0xc7] = 0xff;
regs[0xc8] = 0x00;
regs[0xc9] = 0x3f;
regs[0xca] = 0xfe;
regs[0xcb] = 0xff;
regs[0xcc] = 0x00;
regs[0xcd] = 0x00;
regs[0xce] = 0xff;
regs[0xcf] = 0xf5;
regs[0xd0] = 0xf7;
regs[0xd1] = 0xec;
regs[0xd2] = 0x0d;
regs[0xd3] = 0x05;
regs[0xd4] = 0x67;
regs[0xd7] = 0x10;
regs[0xd8] = 0x52;
regs[0xe2] = 0x00;
regs[0xe3] = 0x00;
regs[0xe4] = 0x00;
regs[0xe7] = 0x00;
regs[0xe8] = 0x00;
regs[0xe9] = 0x00;
regs[0xea] = 0x00;
regs[0xeb] = 0x00;
regs[0xec] = 0x00;
regs[0xed] = 0x00;
regs[0xef] = 0x00;
regs[0xf0] = 0x00;
regs[0xf2] = 0x00;
exposure=10999;
*status1 = 0x10;
*status2 = 0x23;
break;
case SENSOR_TYPE_4400_BARE:
DBG (DBG_io, "setup_scan_registers: setting up SENSOR_TYPE_4400_BARE for 1200 dpi\n");
return SANE_STATUS_INVAL;
break;
}
break;
}
/* apply computed settings */
SET_DOUBLE (regs, EXPOSURE_REG, exposure);
SET_DOUBLE (regs, TIMING_REG, timing);
SET_DOUBLE (regs, TIMING1_REG, timing+1);
SET_DOUBLE (regs, TIMING2_REG, timing+2);
/* sets divisor */
regs[0xd3] = rts8891_data_format (dev->xdpi, dev->sensor);
/* toggle front panel light to signal gray scan */
if (session->params.format == SANE_FRAME_GRAY)
{
*status1 = (*status1 & 0x0F) | 0x10;
}
return status;
}
/* set up the shadow registers for scan, depending on scan parameters */
/* the ultimate goal is to have no direct access to registers, but to */
/* set them through helper functions */
/* NOTE : I couldn't manage to get scans that really uses gray settings. */
/* The windows driver is allways scanning in color, so we do the same. */
/* For now, the only mode that could be done would be 300 dpi gray scan, */
/* based on the register settings of find_origin() */
static SANE_Status
write_scan_registers (struct Rts8891_Session *session)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Byte control;
SANE_Byte status1, status2;
struct Rts8891_Device *dev = session->dev;
/* setup registers for scan */
status=setup_scan_registers (session, &status1, &status2, dev->regs);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error0, "write_scan_registers: failed to setup registers\n");
return status;
}
/* check if session is idle */
control = 0x00;
sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
if (control != 0)
{
DBG (DBG_error0, "write_scan_registers: scanner is not idle!\n");
return SANE_STATUS_IO_ERROR;
}
/* effective write of register set for scan */
status = rts8891_write_all (dev->devnum, dev->regs, dev->reg_count);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error0, "write_scan_registers: failed to write registers\n");
}
return status;
}
/**
* This function parks head relying by moving head backward by a
* very large amount without scanning
*/
static SANE_Status
park_head (struct Rts8891_Device *dev, SANE_Bool wait)
{
SANE_Status status;
SANE_Byte reg, control;
/* the hammer way : set all regs */
SANE_Byte regs[244];
DBG (DBG_proc, "park_head: start\n");
reg = 0x8d;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
reg = 0xad;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
status = sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
reg = 0xff;
sanei_rts88xx_write_reg (dev->devnum, 0x23, ®);
/* TODO create write_double_reg */
if (dev->sensor != SENSOR_TYPE_4400)
{
dev->regs[0x16] = 0x07;
dev->regs[0x17] = 0x00;
}
else
{
dev->regs[0x16] = 0x0f;
dev->regs[0x17] = 0x10;
}
sanei_rts88xx_write_regs (dev->devnum, 0x16, dev->regs + 0x16, 2);
reg = 0x8d;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
reg = 0xad;
sanei_rts88xx_write_reg (dev->devnum, LAMP_REG, ®);
/* 0x20 expected */
sanei_rts88xx_read_reg (dev->devnum, CONTROLER_REG, ®);
if (reg != 0x20)
{
DBG (DBG_warn, "park_head: unexpected controller value 0x%02x\n", reg);
}
/* head parking */
status = rts8891_park (dev, regs, wait);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_error, "park_head: failed to park head!\n");
}
DBG (DBG_proc, "park_head: end\n");
return status;
}
/* update button status
* button access is allowed during scan, which is usefull for 'cancel' button
*/
static SANE_Status
update_button_status (struct Rts8891_Session *session)
{
SANE_Int mask = 0, i;
SANE_Status status = SANE_STATUS_GOOD;
SANE_Bool lock = SANE_FALSE;
/* while scanning, interface is reserved, so don't claim/release it */
if (session->scanning != SANE_TRUE)
{
lock = SANE_TRUE;
/* claim the interface to reserve device */
if (session->dev->conf.allowsharing == SANE_TRUE)
{
status = sanei_usb_claim_interface (session->dev->devnum, 0);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_warn,
"update_button_status: cannot claim usb interface\n");
return SANE_STATUS_DEVICE_BUSY;
}
}
}
/* effective button reading */
status = rts8891_read_buttons (session->dev->devnum, &mask);
/* release interface if needed */
if (lock == SANE_TRUE)
{
if (session->dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (session->dev->devnum, 0);
}
}
for (i = 0; i < session->dev->model->buttons; i++)
{
if (mask & (1 << i))
{
session->val[OPT_BUTTON_1 + i].w = SANE_TRUE;
DBG (DBG_io2, "update_button_status: setting button %d to TRUE\n",
i + 1);
}
}
return status;
}
/* set lamp status, 0 for lamp off
* other values set lamp on */
static SANE_Status
set_lamp_state (struct Rts8891_Session *session, int on)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Byte reg;
/* claim the interface reserve device */
if (session->dev->conf.allowsharing == SANE_TRUE)
{
status = sanei_usb_claim_interface (session->dev->devnum, 0);
if (status != SANE_STATUS_GOOD)
{
DBG (DBG_warn, "set_lamp_state: cannot claim usb interface\n");
return SANE_STATUS_DEVICE_BUSY;
}
}
status = sanei_rts88xx_read_reg (session->dev->devnum, LAMP_REG, ®);
if (on)
{
DBG (DBG_info, "set_lamp_state: lamp on\n");
reg = session->dev->regs[LAMP_REG] | 0x80;
}
else
{
DBG (DBG_info, "set_lamp_state: lamp off\n");
reg = session->dev->regs[LAMP_REG] & 0x7F;
#ifdef HAVE_SYS_TIME_H
/* if lamp is switched off, warming up will be needed */
session->dev->last_scan.tv_sec = 0;
#endif
}
status = sanei_rts88xx_write_reg (session->dev->devnum, LAMP_REG, ®);
/* release interface and return status from lamp setting */
if (session->dev->conf.allowsharing == SANE_TRUE)
{
sanei_usb_release_interface (session->dev->devnum, 0);
}
return status;
}
/* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */
|