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
|
/* sane - Scanner Access Now Easy.
Copyright (C) 2003 Oliver Rauch
Copyright (C) 2003, 2004 Henning Meier-Geinitz <henning@meier-geinitz.de>
Copyright (C) 2004 Gerhard Jaeger <gerhard@gjaeger.de>
Copyright (C) 2004-2013 Stéphane Voltz <stef.dev@free.fr>
Copyright (C) 2005-2009 Pierre Willenbrock <pierre@pirsoft.dnsalias.org>
Copyright (C) 2007 Luke <iceyfor@gmail.com>
Copyright (C) 2011 Alexey Osipov <simba@lerlan.ru> for HP2400 description
and tuning
This file is part of the SANE package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define DEBUG_DECLARE_ONLY
#include "gl646.h"
#include "gl646_registers.h"
#include "test_settings.h"
#include <vector>
namespace genesys {
namespace gl646 {
namespace {
constexpr unsigned CALIBRATION_LINES = 10;
} // namespace
static void write_control(Genesys_Device* dev, const Genesys_Sensor& sensor, int resolution);
static void gl646_set_fe(Genesys_Device* dev, const Genesys_Sensor& sensor, std::uint8_t set,
int dpi);
static void simple_scan(Genesys_Device* dev, const Genesys_Sensor& sensor,
const ScanSession& session, bool move,
std::vector<std::uint8_t>& data, const char* test_identifier);
/**
* Send the stop scan command
* */
static void end_scan_impl(Genesys_Device* dev, Genesys_Register_Set* reg, bool check_stop,
bool eject);
/**
* master motor settings table entry
*/
struct Motor_Master
{
MotorId motor_id;
unsigned dpi;
unsigned channels;
// settings
StepType steptype;
bool fastmod; // fast scanning
bool fastfed; // fast fed slope tables
SANE_Int mtrpwm;
MotorSlope slope1;
MotorSlope slope2;
SANE_Int fwdbwd; // forward/backward steps
};
/**
* master motor settings, for a given motor and dpi,
* it gives steps and speed information
*/
static Motor_Master motor_master[] = {
/* HP3670 motor settings */
{MotorId::HP3670, 50, 3, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(2329, 120, 229),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 75, 3, StepType::FULL, false, true, 1,
MotorSlope::create_from_steps(3429, 305, 200),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 100, 3, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(2905, 187, 143),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 150, 3, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(3429, 305, 73),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 300, 3, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(1055, 563, 11),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 600, 3, StepType::FULL, false, true, 0,
MotorSlope::create_from_steps(10687, 5126, 3),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670,1200, 3, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(15937, 6375, 3),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 50, 1, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(2329, 120, 229),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 75, 1, StepType::FULL, false, true, 1,
MotorSlope::create_from_steps(3429, 305, 200),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 100, 1, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(2905, 187, 143),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 150, 1, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(3429, 305, 73),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 300, 1, StepType::HALF, false, true, 1,
MotorSlope::create_from_steps(1055, 563, 11),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670, 600, 1, StepType::FULL, false, true, 0,
MotorSlope::create_from_steps(10687, 5126, 3),
MotorSlope::create_from_steps(3399, 337, 192), 192},
{MotorId::HP3670,1200, 1, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(15937, 6375, 3),
MotorSlope::create_from_steps(3399, 337, 192), 192},
/* HP2400/G2410 motor settings base motor dpi = 600 */
{MotorId::HP2400, 50, 3, StepType::FULL, false, true, 63,
MotorSlope::create_from_steps(8736, 601, 120),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 100, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(8736, 601, 120),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 150, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(15902, 902, 67),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 300, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(16703, 2188, 32),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 600, 3, StepType::FULL, false, true, 63,
MotorSlope::create_from_steps(18761, 18761, 3),
MotorSlope::create_from_steps(4905, 627, 192), 192},
{MotorId::HP2400,1200, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(43501, 43501, 3),
MotorSlope::create_from_steps(4905, 627, 192), 192},
{MotorId::HP2400, 50, 1, StepType::FULL, false, true, 63,
MotorSlope::create_from_steps(8736, 601, 120),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 100, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(8736, 601, 120),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 150, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(15902, 902, 67),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 300, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(16703, 2188, 32),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400, 600, 1, StepType::FULL, false, true, 63,
MotorSlope::create_from_steps(18761, 18761, 3),
MotorSlope::create_from_steps(4905, 337, 192), 192},
{MotorId::HP2400,1200, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(43501, 43501, 3),
MotorSlope::create_from_steps(4905, 337, 192), 192},
/* XP 200 motor settings */
{MotorId::XP200, 75, 3, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(6000, 2136, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 100, 3, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(6000, 2850, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 200, 3, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(6999, 5700, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 250, 3, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(6999, 6999, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 300, 3, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(13500, 13500, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 600, 3, StepType::HALF, true, true, 0,
MotorSlope::create_from_steps(31998, 31998, 4),
MotorSlope::create_from_steps(12000, 1200, 2), 1},
{MotorId::XP200, 75, 1, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(6000, 2000, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 100, 1, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(6000, 1300, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 200, 1, StepType::HALF, true, true, 0,
MotorSlope::create_from_steps(6000, 3666, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 300, 1, StepType::HALF, true, false, 0,
MotorSlope::create_from_steps(6500, 6500, 4),
MotorSlope::create_from_steps(12000, 1200, 8), 1},
{MotorId::XP200, 600, 1, StepType::HALF, true, true, 0,
MotorSlope::create_from_steps(24000, 24000, 4),
MotorSlope::create_from_steps(12000, 1200, 2), 1},
/* HP scanjet 2300c */
{MotorId::HP2300, 75, 3, StepType::FULL, false, true, 63,
MotorSlope::create_from_steps(8139, 560, 120),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 150, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(7903, 543, 67),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 300, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(2175, 1087, 3),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 600, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(8700, 4350, 3),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300,1200, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(17400, 8700, 3),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 75, 1, StepType::FULL, false, true, 63,
MotorSlope::create_from_steps(8139, 560, 120),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 150, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(7903, 543, 67),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 300, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(2175, 1087, 3),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 600, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(8700, 4350, 3),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300,1200, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(17400, 8700, 3),
MotorSlope::create_from_steps(4905, 337, 120), 16},
/* non half ccd settings for 300 dpi
{MotorId::HP2300, 300, 3, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(5386, 2175, 44),
MotorSlope::create_from_steps(4905, 337, 120), 16},
{MotorId::HP2300, 300, 1, StepType::HALF, false, true, 63,
MotorSlope::create_from_steps(5386, 2175, 44),
MotorSlope::create_from_steps(4905, 337, 120), 16},
*/
/* MD5345/6471 motor settings */
/* vfinal=(exposure/(1200/dpi))/step_type */
{MotorId::MD_5345, 50, 3, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 250, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 75, 3, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 343, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 100, 3, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 458, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 150, 3, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 687, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 200, 3, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 916, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 300, 3, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 1375, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 400, 3, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(2000, 1833, 32),
MotorSlope::create_from_steps(2000, 300, 255), 32},
{MotorId::MD_5345, 500, 3, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(2291, 2291, 32),
MotorSlope::create_from_steps(2000, 300, 255), 32},
{MotorId::MD_5345, 600, 3, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(2750, 2750, 32),
MotorSlope::create_from_steps(2000, 300, 255), 32},
{MotorId::MD_5345, 1200, 3, StepType::QUARTER, false, true, 0,
MotorSlope::create_from_steps(2750, 2750, 16),
MotorSlope::create_from_steps(2000, 300, 255), 146},
{MotorId::MD_5345, 2400, 3, StepType::QUARTER, false, true, 0,
MotorSlope::create_from_steps(5500, 5500, 16),
MotorSlope::create_from_steps(2000, 300, 255), 146},
{MotorId::MD_5345, 50, 1, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 250, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 75, 1, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 343, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 100, 1, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 458, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 150, 1, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 687, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 200, 1, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 916, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 300, 1, StepType::HALF, false, true, 2,
MotorSlope::create_from_steps(2500, 1375, 255),
MotorSlope::create_from_steps(2000, 300, 255), 64},
{MotorId::MD_5345, 400, 1, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(2000, 1833, 32),
MotorSlope::create_from_steps(2000, 300, 255), 32},
{MotorId::MD_5345, 500, 1, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(2291, 2291, 32),
MotorSlope::create_from_steps(2000, 300, 255), 32},
{MotorId::MD_5345, 600, 1, StepType::HALF, false, true, 0,
MotorSlope::create_from_steps(2750, 2750, 32),
MotorSlope::create_from_steps(2000, 300, 255), 32},
{MotorId::MD_5345, 1200, 1, StepType::QUARTER, false, true, 0,
MotorSlope::create_from_steps(2750, 2750, 16),
MotorSlope::create_from_steps(2000, 300, 255), 146},
{MotorId::MD_5345, 2400, 1, StepType::QUARTER, false, true, 0,
MotorSlope::create_from_steps(5500, 5500, 16),
MotorSlope::create_from_steps(2000, 300, 255), 146}, /* 5500 guessed */
};
/**
* reads value from gpio endpoint
*/
static void gl646_gpio_read(IUsbDevice& usb_dev, std::uint8_t* value)
{
DBG_HELPER(dbg);
usb_dev.control_msg(REQUEST_TYPE_IN, REQUEST_REGISTER, GPIO_READ, INDEX, 1, value);
}
/**
* writes the given value to gpio endpoint
*/
static void gl646_gpio_write(IUsbDevice& usb_dev, std::uint8_t value)
{
DBG_HELPER_ARGS(dbg, "(0x%02x)", value);
usb_dev.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, GPIO_WRITE, INDEX, 1, &value);
}
/**
* writes the given value to gpio output enable endpoint
*/
static void gl646_gpio_output_enable(IUsbDevice& usb_dev, std::uint8_t value)
{
DBG_HELPER_ARGS(dbg, "(0x%02x)", value);
usb_dev.control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER, GPIO_OUTPUT_ENABLE, INDEX, 1, &value);
}
/**
* stop scanner's motor
* @param dev scanner's device
*/
static void gl646_stop_motor(Genesys_Device* dev)
{
DBG_HELPER(dbg);
dev->interface->write_register(0x0f, 0x00);
}
/**
* Returns the cksel values used by the required scan mode.
* @param sensor id of the sensor
* @param required required resolution
* @param color true is color mode
* @return cksel value for mode
*/
static int get_cksel(SensorId sensor_id, int required, unsigned channels)
{
for (const auto& sensor : *s_sensors) {
// exit on perfect match
if (sensor.sensor_id == sensor_id && sensor.resolutions.matches(required) &&
sensor.matches_channel_count(channels))
{
unsigned cksel = sensor.ccd_pixels_per_system_pixel();
return cksel;
}
}
DBG(DBG_error, "%s: failed to find match for %d dpi\n", __func__, required);
/* fail safe fallback */
return 1;
}
void CommandSetGl646::init_regs_for_scan_session(Genesys_Device* dev, const Genesys_Sensor& sensor,
Genesys_Register_Set* regs,
const ScanSession& session) const
{
DBG_HELPER(dbg);
session.assert_computed();
debug_dump(DBG_info, sensor);
std::uint32_t move = session.params.starty;
Motor_Master *motor = nullptr;
std::uint32_t z1, z2;
int feedl;
/* for the given resolution, search for master
* motor mode setting */
for (unsigned i = 0; i < sizeof (motor_master) / sizeof (Motor_Master); ++i) {
if (dev->model->motor_id == motor_master[i].motor_id &&
motor_master[i].dpi == session.params.yres &&
motor_master[i].channels == session.params.channels)
{
motor = &motor_master[i];
}
}
if (motor == nullptr) {
throw SaneException("unable to find settings for motor %d at %d dpi, color=%d",
static_cast<unsigned>(dev->model->motor_id),
session.params.yres, session.params.channels);
}
scanner_setup_sensor(*dev, sensor, *regs);
/* now generate slope tables : we are not using generate_slope_table3 yet */
auto slope_table1 = create_slope_table_for_speed(motor->slope1, motor->slope1.max_speed_w,
StepType::FULL, 1, 4,
get_slope_table_max_size(AsicType::GL646));
auto slope_table2 = create_slope_table_for_speed(motor->slope2, motor->slope2.max_speed_w,
StepType::FULL, 1, 4,
get_slope_table_max_size(AsicType::GL646));
/* R01 */
/* now setup other registers for final scan (ie with shading enabled) */
/* watch dog + shading + scan enable */
regs->find_reg(0x01).value |= REG_0x01_DOGENB | REG_0x01_SCAN;
if (dev->model->is_cis) {
regs->find_reg(0x01).value |= REG_0x01_CISSET;
} else {
regs->find_reg(0x01).value &= ~REG_0x01_CISSET;
}
// if device has no calibration, don't enable shading correction
if (has_flag(dev->model->flags, ModelFlag::DISABLE_SHADING_CALIBRATION) ||
has_flag(session.params.flags, ScanFlag::DISABLE_SHADING))
{
regs->find_reg(0x01).value &= ~REG_0x01_DVDSET;
} else {
regs->find_reg(0x01).value |= REG_0x01_DVDSET;
}
regs->find_reg(0x01).value &= ~REG_0x01_FASTMOD;
if (motor->fastmod) {
regs->find_reg(0x01).value |= REG_0x01_FASTMOD;
}
/* R02 */
/* allow moving when buffer full by default */
if (!dev->model->is_sheetfed) {
dev->reg.find_reg(0x02).value &= ~REG_0x02_ACDCDIS;
} else {
dev->reg.find_reg(0x02).value |= REG_0x02_ACDCDIS;
}
/* setup motor power and direction */
sanei_genesys_set_motor_power(*regs, true);
if (has_flag(session.params.flags, ScanFlag::REVERSE)) {
regs->find_reg(0x02).value |= REG_0x02_MTRREV;
} else {
regs->find_reg(0x02).value &= ~REG_0x02_MTRREV;
}
/* fastfed enabled (2 motor slope tables) */
if (motor->fastfed) {
regs->find_reg(0x02).value |= REG_0x02_FASTFED;
} else {
regs->find_reg(0x02).value &= ~REG_0x02_FASTFED;
}
/* step type */
regs->find_reg(0x02).value &= ~REG_0x02_STEPSEL;
switch (motor->steptype)
{
case StepType::FULL:
break;
case StepType::HALF:
regs->find_reg(0x02).value |= 1;
break;
case StepType::QUARTER:
regs->find_reg(0x02).value |= 2;
break;
default:
regs->find_reg(0x02).value |= 3;
break;
}
if (dev->model->is_sheetfed || !has_flag(session.params.flags, ScanFlag::AUTO_GO_HOME)) {
regs->find_reg(0x02).value &= ~REG_0x02_AGOHOME;
} else {
regs->find_reg(0x02).value |= REG_0x02_AGOHOME;
}
/* R03 */
regs->find_reg(0x03).value &= ~REG_0x03_AVEENB;
// regs->find_reg(0x03).value |= REG_0x03_AVEENB;
regs->find_reg(0x03).value &= ~REG_0x03_LAMPDOG;
/* select XPA */
regs->find_reg(0x03).value &= ~REG_0x03_XPASEL;
if ((session.params.flags & ScanFlag::USE_XPA) != ScanFlag::NONE) {
regs->find_reg(0x03).value |= REG_0x03_XPASEL;
}
regs->state.is_xpa_on = (session.params.flags & ScanFlag::USE_XPA) != ScanFlag::NONE;
/* R04 */
/* monochrome / color scan */
switch (session.params.depth) {
case 8:
regs->find_reg(0x04).value &= ~(REG_0x04_LINEART | REG_0x04_BITSET);
break;
case 16:
regs->find_reg(0x04).value &= ~REG_0x04_LINEART;
regs->find_reg(0x04).value |= REG_0x04_BITSET;
break;
}
sanei_genesys_set_dpihw(*regs, sensor.full_resolution);
/* gamma enable for scans */
if (has_flag(dev->model->flags, ModelFlag::GAMMA_14BIT)) {
regs->find_reg(0x05).value |= REG_0x05_GMM14BIT;
}
if (!has_flag(session.params.flags, ScanFlag::DISABLE_GAMMA) &&
session.params.depth < 16)
{
regs->find_reg(REG_0x05).value |= REG_0x05_GMMENB;
} else {
regs->find_reg(REG_0x05).value &= ~REG_0x05_GMMENB;
}
/* true CIS gray if needed */
if (dev->model->is_cis && session.params.channels == 1 &&
session.params.color_filter == ColorFilter::NONE)
{
regs->find_reg(0x05).value |= REG_0x05_LEDADD;
} else {
regs->find_reg(0x05).value &= ~REG_0x05_LEDADD;
}
/* HP2400 1200dpi mode tuning */
if (dev->model->sensor_id == SensorId::CCD_HP2400) {
/* reset count of dummy lines to zero */
regs->find_reg(0x1e).value &= ~REG_0x1E_LINESEL;
if (session.params.xres >= 1200) {
/* there must be one dummy line */
regs->find_reg(0x1e).value |= 1 & REG_0x1E_LINESEL;
/* GPO12 need to be set to zero */
regs->find_reg(0x66).value &= ~0x20;
}
else
{
/* set GPO12 back to one */
regs->find_reg(0x66).value |= 0x20;
}
}
/* motor steps used */
unsigned forward_steps = motor->fwdbwd;
unsigned backward_steps = motor->fwdbwd;
// the steps count must be different by at most 128, otherwise it's impossible to construct
// a proper backtracking curve. We're using slightly lower limit to allow at least a minimum
// distance between accelerations (forward_steps, backward_steps)
if (slope_table1.table.size() > slope_table2.table.size() + 100) {
slope_table2.expand_table(slope_table1.table.size() - 100, 1);
}
if (slope_table2.table.size() > slope_table1.table.size() + 100) {
slope_table1.expand_table(slope_table2.table.size() - 100, 1);
}
if (slope_table1.table.size() >= slope_table2.table.size()) {
backward_steps += (slope_table1.table.size() - slope_table2.table.size()) * 2;
} else {
forward_steps += (slope_table2.table.size() - slope_table1.table.size()) * 2;
}
if (forward_steps > 255) {
if (backward_steps < (forward_steps - 255)) {
throw SaneException("Can't set backtracking parameters without skipping image");
}
backward_steps -= forward_steps - 255;
}
if (backward_steps > 255) {
if (forward_steps < (backward_steps - 255)) {
throw SaneException("Can't set backtracking parameters without skipping image");
}
forward_steps -= backward_steps - 255;
}
regs->find_reg(0x21).value = slope_table1.table.size();
regs->find_reg(0x24).value = slope_table2.table.size();
regs->find_reg(0x22).value = forward_steps;
regs->find_reg(0x23).value = backward_steps;
/* CIS scanners read one line per color channel
* since gray mode use 'add' we also read 3 channels even not in
* color mode */
if (dev->model->is_cis) {
regs->set24(REG_LINCNT, session.output_line_count * 3);
} else {
regs->set24(REG_LINCNT, session.output_line_count);
}
regs->set16(REG_STRPIXEL, session.pixel_startx);
regs->set16(REG_ENDPIXEL, session.pixel_endx);
regs->set24(REG_MAXWD, session.output_line_bytes);
// FIXME: the incoming sensor is selected for incorrect resolution
const auto& dpiset_sensor = sanei_genesys_find_sensor(dev, session.params.xres,
session.params.channels,
session.params.scan_method);
regs->set16(REG_DPISET, dpiset_sensor.register_dpiset);
regs->set16(REG_LPERIOD, sensor.exposure_lperiod);
/* move distance must be adjusted to take into account the extra lines
* read to reorder data */
feedl = move;
if (session.num_staggered_lines + session.max_color_shift_lines > 0 && feedl != 0) {
unsigned total_lines = session.max_color_shift_lines + session.num_staggered_lines;
int feed_offset = (total_lines * dev->motor.base_ydpi) / motor->dpi;
if (feedl > feed_offset) {
feedl = feedl - feed_offset;
}
}
/* we assume all scans are done with 2 tables */
/*
feedl = feed_steps - fast_slope_steps*2 -
(slow_slope_steps >> scan_step_type); */
/* but head has moved due to shading calibration => dev->scanhead_position_primary */
if (feedl > 0)
{
/* TODO clean up this when I'll fully understand.
* for now, special casing each motor */
switch (dev->model->motor_id) {
case MotorId::MD_5345:
switch (motor->dpi) {
case 200:
feedl -= 70;
break;
case 300:
feedl -= 70;
break;
case 400:
feedl += 130;
break;
case 600:
feedl += 160;
break;
case 1200:
feedl += 160;
break;
case 2400:
feedl += 180;
break;
default:
break;
}
break;
case MotorId::HP2300:
switch (motor->dpi) {
case 75:
feedl -= 180;
break;
case 150:
feedl += 0;
break;
case 300:
feedl += 30;
break;
case 600:
feedl += 35;
break;
case 1200:
feedl += 45;
break;
default:
break;
}
break;
case MotorId::HP2400:
switch (motor->dpi) {
case 150:
feedl += 150;
break;
case 300:
feedl += 220;
break;
case 600:
feedl += 260;
break;
case 1200:
feedl += 280; /* 300 */
break;
case 50:
feedl += 0;
break;
case 100:
feedl += 100;
break;
default:
break;
}
break;
/* theorical value */
default: {
unsigned step_shift = static_cast<unsigned>(motor->steptype);
if (motor->fastfed)
{
feedl = feedl - 2 * slope_table2.table.size() -
(slope_table1.table.size() >> step_shift);
}
else
{
feedl = feedl - (slope_table1.table.size() >> step_shift);
}
break;
}
}
/* security */
if (feedl < 0)
feedl = 0;
}
regs->set24(REG_FEEDL, feedl);
regs->find_reg(0x65).value = motor->mtrpwm;
sanei_genesys_calculate_zmod(regs->find_reg(0x02).value & REG_0x02_FASTFED,
sensor.exposure_lperiod,
slope_table1.table,
slope_table1.table.size(),
move, motor->fwdbwd, &z1, &z2);
/* no z1/z2 for sheetfed scanners */
if (dev->model->is_sheetfed) {
z1 = 0;
z2 = 0;
}
regs->set16(REG_Z1MOD, z1);
regs->set16(REG_Z2MOD, z2);
regs->find_reg(0x6b).value = slope_table2.table.size();
regs->find_reg(0x6c).value =
(regs->find_reg(0x6c).value & REG_0x6C_TGTIME) | ((z1 >> 13) & 0x38) | ((z2 >> 16)
& 0x07);
write_control(dev, sensor, session.output_resolution);
// setup analog frontend
gl646_set_fe(dev, sensor, AFE_SET, session.output_resolution);
setup_image_pipeline(*dev, session);
dev->read_active = true;
dev->session = session;
dev->total_bytes_read = 0;
dev->total_bytes_to_read = (size_t) session.output_line_bytes_requested
* (size_t) session.params.lines;
/* select color filter based on settings */
regs->find_reg(0x04).value &= ~REG_0x04_FILTER;
if (session.params.channels == 1) {
switch (session.params.color_filter) {
case ColorFilter::RED:
regs->find_reg(0x04).value |= 0x04;
break;
case ColorFilter::GREEN:
regs->find_reg(0x04).value |= 0x08;
break;
case ColorFilter::BLUE:
regs->find_reg(0x04).value |= 0x0c;
break;
default:
break;
}
}
scanner_send_slope_table(dev, sensor, 0, slope_table1.table);
scanner_send_slope_table(dev, sensor, 1, slope_table2.table);
}
/**
* Set all registers to default values after init
* @param dev scannerr's device to set
*/
static void
gl646_init_regs (Genesys_Device * dev)
{
int addr;
DBG(DBG_proc, "%s\n", __func__);
dev->reg.clear();
for (addr = 1; addr <= 0x0b; addr++)
dev->reg.init_reg(addr, 0);
for (addr = 0x10; addr <= 0x29; addr++)
dev->reg.init_reg(addr, 0);
for (addr = 0x2c; addr <= 0x39; addr++)
dev->reg.init_reg(addr, 0);
for (addr = 0x3d; addr <= 0x3f; addr++)
dev->reg.init_reg(addr, 0);
for (addr = 0x52; addr <= 0x5e; addr++)
dev->reg.init_reg(addr, 0);
for (addr = 0x60; addr <= 0x6d; addr++)
dev->reg.init_reg(addr, 0);
dev->reg.find_reg(0x01).value = 0x20 /*0x22 */ ; /* enable shading, CCD, color, 1M */
dev->reg.find_reg(0x02).value = 0x30 /*0x38 */ ; /* auto home, one-table-move, full step */
if (dev->model->motor_id == MotorId::MD_5345) {
dev->reg.find_reg(0x02).value |= 0x01; // half-step
}
switch (dev->model->motor_id) {
case MotorId::MD_5345:
dev->reg.find_reg(0x02).value |= 0x01; /* half-step */
break;
case MotorId::XP200:
/* for this sheetfed scanner, no AGOHOME, nor backtracking */
dev->reg.find_reg(0x02).value = 0x50;
break;
default:
break;
}
dev->reg.find_reg(0x03).value = 0x1f /*0x17 */ ; /* lamp on */
dev->reg.find_reg(0x04).value = 0x13 /*0x03 */ ; /* 8 bits data, 16 bits A/D, color, Wolfson fe *//* todo: according to spec, 0x0 is reserved? */
switch (dev->model->adc_id)
{
case AdcId::AD_XP200:
dev->reg.find_reg(0x04).value = 0x12;
break;
default:
/* Wolfson frontend */
dev->reg.find_reg(0x04).value = 0x13;
break;
}
const auto& sensor = sanei_genesys_find_sensor_any(dev);
dev->reg.find_reg(0x05).value = 0x00; /* 12 bits gamma, disable gamma, 24 clocks/pixel */
sanei_genesys_set_dpihw(dev->reg, sensor.full_resolution);
if (has_flag(dev->model->flags, ModelFlag::GAMMA_14BIT)) {
dev->reg.find_reg(0x05).value |= REG_0x05_GMM14BIT;
}
if (dev->model->adc_id == AdcId::AD_XP200) {
dev->reg.find_reg(0x05).value |= 0x01; /* 12 clocks/pixel */
}
if (dev->model->sensor_id == SensorId::CCD_HP2300) {
dev->reg.find_reg(0x06).value = 0x00; // PWRBIT off, shading gain=4, normal AFE image capture
} else {
dev->reg.find_reg(0x06).value = 0x18; // PWRBIT on, shading gain=8, normal AFE image capture
}
scanner_setup_sensor(*dev, sensor, dev->reg);
dev->reg.find_reg(0x1e).value = 0xf0; /* watch-dog time */
switch (dev->model->sensor_id)
{
case SensorId::CCD_HP2300:
dev->reg.find_reg(0x1e).value = 0xf0;
dev->reg.find_reg(0x1f).value = 0x10;
dev->reg.find_reg(0x20).value = 0x20;
break;
case SensorId::CCD_HP2400:
dev->reg.find_reg(0x1e).value = 0x80;
dev->reg.find_reg(0x1f).value = 0x10;
dev->reg.find_reg(0x20).value = 0x20;
break;
case SensorId::CCD_HP3670:
dev->reg.find_reg(0x19).value = 0x2a;
dev->reg.find_reg(0x1e).value = 0x80;
dev->reg.find_reg(0x1f).value = 0x10;
dev->reg.find_reg(0x20).value = 0x20;
break;
case SensorId::CIS_XP200:
dev->reg.find_reg(0x1e).value = 0x10;
dev->reg.find_reg(0x1f).value = 0x01;
dev->reg.find_reg(0x20).value = 0x50;
break;
default:
dev->reg.find_reg(0x1f).value = 0x01;
dev->reg.find_reg(0x20).value = 0x50;
break;
}
dev->reg.find_reg(0x21).value = 0x08 /*0x20 */ ; /* table one steps number for forward slope curve of the acc/dec */
dev->reg.find_reg(0x22).value = 0x10 /*0x08 */ ; /* steps number of the forward steps for start/stop */
dev->reg.find_reg(0x23).value = 0x10 /*0x08 */ ; /* steps number of the backward steps for start/stop */
dev->reg.find_reg(0x24).value = 0x08 /*0x20 */ ; /* table one steps number backward slope curve of the acc/dec */
dev->reg.find_reg(0x25).value = 0x00; /* scan line numbers (7000) */
dev->reg.find_reg(0x26).value = 0x00 /*0x1b */ ;
dev->reg.find_reg(0x27).value = 0xd4 /*0x58 */ ;
dev->reg.find_reg(0x28).value = 0x01; /* PWM duty for lamp control */
dev->reg.find_reg(0x29).value = 0xff;
dev->reg.find_reg(0x2c).value = 0x02; /* set resolution (600 DPI) */
dev->reg.find_reg(0x2d).value = 0x58;
dev->reg.find_reg(0x2e).value = 0x78; /* set black&white threshold high level */
dev->reg.find_reg(0x2f).value = 0x7f; /* set black&white threshold low level */
dev->reg.find_reg(0x30).value = 0x00; /* begin pixel position (16) */
dev->reg.find_reg(0x31).value = sensor.dummy_pixel /*0x10 */ ; /* TGW + 2*TG_SHLD + x */
dev->reg.find_reg(0x32).value = 0x2a /*0x15 */ ; /* end pixel position (5390) */
dev->reg.find_reg(0x33).value = 0xf8 /*0x0e */ ; /* TGW + 2*TG_SHLD + y */
dev->reg.find_reg(0x34).value = sensor.dummy_pixel;
dev->reg.find_reg(0x35).value = 0x01 /*0x00 */ ; /* set maximum word size per line, for buffer full control (10800) */
dev->reg.find_reg(0x36).value = 0x00 /*0x2a */ ;
dev->reg.find_reg(0x37).value = 0x00 /*0x30 */ ;
dev->reg.find_reg(0x38).value = 0x2a; // line period (exposure time = 11000 pixels) */
dev->reg.find_reg(0x39).value = 0xf8;
dev->reg.find_reg(0x3d).value = 0x00; /* set feed steps number of motor move */
dev->reg.find_reg(0x3e).value = 0x00;
dev->reg.find_reg(0x3f).value = 0x01 /*0x00 */ ;
dev->reg.find_reg(0x60).value = 0x00; /* Z1MOD, 60h:61h:(6D b5:b3), remainder for start/stop */
dev->reg.find_reg(0x61).value = 0x00; /* (21h+22h)/LPeriod */
dev->reg.find_reg(0x62).value = 0x00; /* Z2MODE, 62h:63h:(6D b2:b0), remainder for start scan */
dev->reg.find_reg(0x63).value = 0x00; /* (3Dh+3Eh+3Fh)/LPeriod for one-table mode,(21h+1Fh)/LPeriod */
dev->reg.find_reg(0x64).value = 0x00; /* motor PWM frequency */
dev->reg.find_reg(0x65).value = 0x00; /* PWM duty cycle for table one motor phase (63 = max) */
if (dev->model->motor_id == MotorId::MD_5345) {
// PWM duty cycle for table one motor phase (63 = max)
dev->reg.find_reg(0x65).value = 0x02;
}
for (const auto& reg : dev->gpo.regs) {
dev->reg.set8(reg.address, reg.value);
}
switch (dev->model->motor_id) {
case MotorId::HP2300:
case MotorId::HP2400:
dev->reg.find_reg(0x6a).value = 0x7f; /* table two steps number for acc/dec */
dev->reg.find_reg(0x6b).value = 0x78; /* table two steps number for acc/dec */
dev->reg.find_reg(0x6d).value = 0x7f;
break;
case MotorId::MD_5345:
dev->reg.find_reg(0x6a).value = 0x42; /* table two fast moving step type, PWM duty for table two */
dev->reg.find_reg(0x6b).value = 0xff; /* table two steps number for acc/dec */
dev->reg.find_reg(0x6d).value = 0x41; /* select deceleration steps whenever go home (0), accel/decel stop time (31 * LPeriod) */
break;
case MotorId::XP200:
dev->reg.find_reg(0x6a).value = 0x7f; /* table two fast moving step type, PWM duty for table two */
dev->reg.find_reg(0x6b).value = 0x08; /* table two steps number for acc/dec */
dev->reg.find_reg(0x6d).value = 0x01; /* select deceleration steps whenever go home (0), accel/decel stop time (31 * LPeriod) */
break;
case MotorId::HP3670:
dev->reg.find_reg(0x6a).value = 0x41; /* table two steps number for acc/dec */
dev->reg.find_reg(0x6b).value = 0xc8; /* table two steps number for acc/dec */
dev->reg.find_reg(0x6d).value = 0x7f;
break;
default:
dev->reg.find_reg(0x6a).value = 0x40; /* table two fast moving step type, PWM duty for table two */
dev->reg.find_reg(0x6b).value = 0xff; /* table two steps number for acc/dec */
dev->reg.find_reg(0x6d).value = 0x01; /* select deceleration steps whenever go home (0), accel/decel stop time (31 * LPeriod) */
break;
}
dev->reg.find_reg(0x6c).value = 0x00; /* period times for LPeriod, expR,expG,expB, Z1MODE, Z2MODE (one period time) */
}
// Set values of Analog Device type frontend
static void gl646_set_ad_fe(Genesys_Device* dev, std::uint8_t set)
{
DBG_HELPER(dbg);
int i;
if (set == AFE_INIT) {
dev->frontend = dev->frontend_initial;
// write them to analog frontend
dev->interface->write_fe_register(0x00, dev->frontend.regs.get_value(0x00));
dev->interface->write_fe_register(0x01, dev->frontend.regs.get_value(0x01));
}
if (set == AFE_SET)
{
for (i = 0; i < 3; i++) {
dev->interface->write_fe_register(0x02 + i, dev->frontend.get_gain(i));
}
for (i = 0; i < 3; i++) {
dev->interface->write_fe_register(0x05 + i, dev->frontend.get_offset(i));
}
}
/*
if (set == AFE_POWER_SAVE)
{
dev->interface->write_fe_register(0x00, dev->frontend.reg[0] | 0x04);
} */
}
/** set up analog frontend
* set up analog frontend
* @param dev device to set up
* @param set action from AFE_SET, AFE_INIT and AFE_POWERSAVE
* @param dpi resolution of the scan since it affects settings
*/
static void gl646_wm_hp3670(Genesys_Device* dev, const Genesys_Sensor& sensor, std::uint8_t set,
unsigned dpi)
{
DBG_HELPER(dbg);
int i;
switch (set)
{
case AFE_INIT:
dev->interface->write_fe_register(0x04, 0x80);
dev->interface->sleep_ms(200);
dev->interface->write_register(0x50, 0x00);
dev->frontend = dev->frontend_initial;
dev->interface->write_fe_register(0x01, dev->frontend.regs.get_value(0x01));
dev->interface->write_fe_register(0x02, dev->frontend.regs.get_value(0x02));
gl646_gpio_output_enable(dev->interface->get_usb_device(), 0x07);
break;
case AFE_POWER_SAVE:
dev->interface->write_fe_register(0x01, 0x06);
dev->interface->write_fe_register(0x06, 0x0f);
return;
break;
default: /* AFE_SET */
/* mode setup */
i = dev->frontend.regs.get_value(0x03);
if (dpi > sensor.full_resolution / 2) {
/* fe_reg_0x03 must be 0x12 for 1200 dpi in WOLFSON_HP3670.
* WOLFSON_HP2400 in 1200 dpi mode works well with
* fe_reg_0x03 set to 0x32 or 0x12 but not to 0x02 */
i = 0x12;
}
dev->interface->write_fe_register(0x03, i);
/* offset and sign (or msb/lsb ?) */
for (i = 0; i < 3; i++) {
dev->interface->write_fe_register(0x20 + i, dev->frontend.get_offset(i));
dev->interface->write_fe_register(0x24 + i, dev->frontend.regs.get_value(0x24 + i));
}
// gain
for (i = 0; i < 3; i++) {
dev->interface->write_fe_register(0x28 + i, dev->frontend.get_gain(i));
}
}
}
/** Set values of analog frontend
* @param dev device to set
* @param set action to execute
* @param dpi dpi to setup the AFE
*/
static void gl646_set_fe(Genesys_Device* dev, const Genesys_Sensor& sensor, std::uint8_t set,
int dpi)
{
DBG_HELPER_ARGS(dbg, "%s,%d", set == AFE_INIT ? "init" :
set == AFE_SET ? "set" :
set == AFE_POWER_SAVE ? "powersave" : "huh?", dpi);
int i;
std::uint8_t val;
/* Analog Device type frontend */
std::uint8_t frontend_type = dev->reg.find_reg(0x04).value & REG_0x04_FESET;
if (frontend_type == 0x02) {
gl646_set_ad_fe(dev, set);
return;
}
/* Wolfson type frontend */
if (frontend_type != 0x03) {
throw SaneException("unsupported frontend type %d", frontend_type);
}
/* per frontend function to keep code clean */
switch (dev->model->adc_id)
{
case AdcId::WOLFSON_HP3670:
case AdcId::WOLFSON_HP2400:
gl646_wm_hp3670(dev, sensor, set, dpi);
return;
default:
DBG(DBG_proc, "%s(): using old method\n", __func__);
break;
}
/* initialize analog frontend */
if (set == AFE_INIT) {
dev->frontend = dev->frontend_initial;
// reset only done on init
dev->interface->write_fe_register(0x04, 0x80);
/* enable GPIO for some models */
if (dev->model->sensor_id == SensorId::CCD_HP2300) {
val = 0x07;
gl646_gpio_output_enable(dev->interface->get_usb_device(), val);
}
return;
}
// set fontend to power saving mode
if (set == AFE_POWER_SAVE) {
dev->interface->write_fe_register(0x01, 0x02);
return;
}
/* here starts AFE_SET */
/* TODO : base this test on cfg reg3 or a CCD family flag to be created */
/* if (dev->model->ccd_type != SensorId::CCD_HP2300
&& dev->model->ccd_type != SensorId::CCD_HP3670
&& dev->model->ccd_type != SensorId::CCD_HP2400) */
{
dev->interface->write_fe_register(0x00, dev->frontend.regs.get_value(0x00));
dev->interface->write_fe_register(0x02, dev->frontend.regs.get_value(0x02));
}
// start with reg3
dev->interface->write_fe_register(0x03, dev->frontend.regs.get_value(0x03));
switch (dev->model->sensor_id)
{
default:
for (i = 0; i < 3; i++) {
dev->interface->write_fe_register(0x24 + i, dev->frontend.regs.get_value(0x24 + i));
dev->interface->write_fe_register(0x28 + i, dev->frontend.get_gain(i));
dev->interface->write_fe_register(0x20 + i, dev->frontend.get_offset(i));
}
break;
/* just can't have it to work ....
case SensorId::CCD_HP2300:
case SensorId::CCD_HP2400:
case SensorId::CCD_HP3670:
dev->interface->write_fe_register(0x23, dev->frontend.get_offset(1));
dev->interface->write_fe_register(0x28, dev->frontend.get_gain(1));
break; */
}
// end with reg1
dev->interface->write_fe_register(0x01, dev->frontend.regs.get_value(0x01));
}
/** Set values of analog frontend
* this this the public interface, the gl646 as to use one more
* parameter to work effectively, hence the redirection
* @param dev device to set
* @param set action to execute
*/
void CommandSetGl646::set_fe(Genesys_Device* dev, const Genesys_Sensor& sensor,
std::uint8_t set) const
{
gl646_set_fe(dev, sensor, set, dev->settings.yres);
}
/**
* enters or leaves power saving mode
* limited to AFE for now.
* @param dev scanner's device
* @param enable true to enable power saving, false to leave it
*/
void CommandSetGl646::save_power(Genesys_Device* dev, bool enable) const
{
DBG_HELPER_ARGS(dbg, "enable = %d", enable);
const auto& sensor = sanei_genesys_find_sensor_any(dev);
if (enable)
{
// gl646_set_fe(dev, sensor, AFE_POWER_SAVE);
}
else
{
gl646_set_fe(dev, sensor, AFE_INIT, 0);
}
}
void CommandSetGl646::set_powersaving(Genesys_Device* dev, int delay /* in minutes */) const
{
DBG_HELPER_ARGS(dbg, "delay = %d", delay);
Genesys_Register_Set local_reg(Genesys_Register_Set::SEQUENTIAL);
int rate, exposure_time, tgtime, time;
local_reg.init_reg(0x01, dev->reg.get8(0x01)); // disable fastmode
local_reg.init_reg(0x03, dev->reg.get8(0x03)); // Lamp power control
local_reg.init_reg(0x05, dev->reg.get8(0x05) & ~REG_0x05_BASESEL); // 24 clocks/pixel
local_reg.init_reg(0x38, 0x00); // line period low
local_reg.init_reg(0x39, 0x00); //line period high
local_reg.init_reg(0x6c, 0x00); // period times for LPeriod, expR,expG,expB, Z1MODE, Z2MODE
if (!delay)
local_reg.find_reg(0x03).value &= 0xf0; /* disable lampdog and set lamptime = 0 */
else if (delay < 20)
local_reg.find_reg(0x03).value = (local_reg.get8(0x03) & 0xf0) | 0x09; /* enable lampdog and set lamptime = 1 */
else
local_reg.find_reg(0x03).value = (local_reg.get8(0x03) & 0xf0) | 0x0f; /* enable lampdog and set lamptime = 7 */
time = delay * 1000 * 60; /* -> msec */
exposure_time = static_cast<std::uint32_t>((time * 32000.0 /
(24.0 * 64.0 * (local_reg.get8(0x03) & REG_0x03_LAMPTIM) *
1024.0) + 0.5));
/* 32000 = system clock, 24 = clocks per pixel */
rate = (exposure_time + 65536) / 65536;
if (rate > 4)
{
rate = 8;
tgtime = 3;
}
else if (rate > 2)
{
rate = 4;
tgtime = 2;
}
else if (rate > 1)
{
rate = 2;
tgtime = 1;
}
else
{
rate = 1;
tgtime = 0;
}
local_reg.find_reg(0x6c).value |= tgtime << 6;
exposure_time /= rate;
if (exposure_time > 65535)
exposure_time = 65535;
local_reg.find_reg(0x38).value = exposure_time / 256;
local_reg.find_reg(0x39).value = exposure_time & 255;
dev->interface->write_registers(local_reg);
}
/**
* loads document into scanner
* currently only used by XP200
* bit2 (0x04) of gpio is paper event (document in/out) on XP200
* HOMESNR is set if no document in front of sensor, the sequence of events is
* paper event -> document is in the sheet feeder
* HOMESNR becomes 0 -> document reach sensor
* HOMESNR becomes 1 ->document left sensor
* paper event -> document is out
*/
void CommandSetGl646::load_document(Genesys_Device* dev) const
{
DBG_HELPER(dbg);
// FIXME: sequential not really needed in this case
Genesys_Register_Set regs(Genesys_Register_Set::SEQUENTIAL);
unsigned count;
/* no need to load document is flatbed scanner */
if (!dev->model->is_sheetfed) {
DBG(DBG_proc, "%s: nothing to load\n", __func__);
DBG(DBG_proc, "%s: end\n", __func__);
return;
}
auto status = scanner_read_status(*dev);
// home sensor is set if a document is inserted
if (status.is_at_home) {
/* if no document, waits for a paper event to start loading */
/* with a 60 seconde minutes timeout */
count = 0;
std::uint8_t val = 0;
do {
gl646_gpio_read(dev->interface->get_usb_device(), &val);
DBG(DBG_info, "%s: GPIO=0x%02x\n", __func__, val);
if ((val & 0x04) != 0x04)
{
DBG(DBG_warn, "%s: no paper detected\n", __func__);
}
dev->interface->sleep_ms(200);
count++;
}
while (((val & 0x04) != 0x04) && (count < 300)); /* 1 min time out */
if (count == 300)
{
throw SaneException(SANE_STATUS_NO_DOCS, "timeout waiting for document");
}
}
/* set up to fast move before scan then move until document is detected */
regs.init_reg(0x01, 0x90);
/* AGOME, 2 slopes motor moving */
regs.init_reg(0x02, 0x79);
/* motor feeding steps to 0 */
regs.init_reg(0x3d, 0);
regs.init_reg(0x3e, 0);
regs.init_reg(0x3f, 0);
/* 50 fast moving steps */
regs.init_reg(0x6b, 50);
/* set GPO */
regs.init_reg(0x66, 0x30);
/* stesp NO */
regs.init_reg(0x21, 4);
regs.init_reg(0x22, 1);
regs.init_reg(0x23, 1);
regs.init_reg(0x24, 4);
/* generate slope table 2 */
auto slope_table = create_slope_table_for_speed(MotorSlope::create_from_steps(6000, 2400, 50),
2400, StepType::FULL, 1, 4,
get_slope_table_max_size(AsicType::GL646));
// document loading:
// send regs
// start motor
// wait e1 status to become e0
const auto& sensor = sanei_genesys_find_sensor_any(dev);
scanner_send_slope_table(dev, sensor, 1, slope_table.table);
dev->interface->write_registers(regs);
scanner_start_action(*dev, true);
count = 0;
do
{
status = scanner_read_status(*dev);
dev->interface->sleep_ms(200);
count++;
} while (status.is_motor_enabled && (count < 300));
if (count == 300)
{
throw SaneException(SANE_STATUS_JAMMED, "can't load document");
}
/* when loading OK, document is here */
dev->document = true;
/* set up to idle */
regs.set8(0x02, 0x71);
regs.set8(0x3f, 1);
regs.set8(0x6b, 8);
dev->interface->write_registers(regs);
}
/**
* detects end of document and adjust current scan
* to take it into account
* used by sheetfed scanners
*/
void CommandSetGl646::detect_document_end(Genesys_Device* dev) const
{
DBG_HELPER(dbg);
std::uint8_t gpio;
unsigned int bytes_left;
// test for document presence
scanner_read_print_status(*dev);
gl646_gpio_read(dev->interface->get_usb_device(), &gpio);
DBG(DBG_info, "%s: GPIO=0x%02x\n", __func__, gpio);
/* detect document event. There one event when the document go in,
* then another when it leaves */
if (dev->document && (gpio & 0x04) && (dev->total_bytes_read > 0)) {
DBG(DBG_info, "%s: no more document\n", __func__);
dev->document = false;
/* adjust number of bytes to read:
* total_bytes_to_read is the number of byte to send to frontend
* total_bytes_read is the number of bytes sent to frontend
* read_bytes_left is the number of bytes to read from the scanner
*/
DBG(DBG_io, "%s: total_bytes_to_read=%zu\n", __func__, dev->total_bytes_to_read);
DBG(DBG_io, "%s: total_bytes_read =%zu\n", __func__, dev->total_bytes_read);
// amount of data available from scanner is what to scan
sanei_genesys_read_valid_words(dev, &bytes_left);
unsigned lines_in_buffer = bytes_left / dev->session.output_line_bytes_raw;
// we add the number of lines needed to read the last part of the document in
unsigned lines_offset = static_cast<unsigned>(
(dev->model->y_offset * dev->session.params.yres) / MM_PER_INCH);
unsigned remaining_lines = lines_in_buffer + lines_offset;
bytes_left = remaining_lines * dev->session.output_line_bytes_raw;
if (bytes_left < dev->get_pipeline_source().remaining_bytes()) {
dev->get_pipeline_source().set_remaining_bytes(bytes_left);
dev->total_bytes_to_read = dev->total_bytes_read + bytes_left;
}
DBG(DBG_io, "%s: total_bytes_to_read=%zu\n", __func__, dev->total_bytes_to_read);
DBG(DBG_io, "%s: total_bytes_read =%zu\n", __func__, dev->total_bytes_read);
}
}
/**
* eject document from the feeder
* currently only used by XP200
* TODO we currently rely on AGOHOME not being set for sheetfed scanners,
* maybe check this flag in eject to let the document being eject automatically
*/
void CommandSetGl646::eject_document(Genesys_Device* dev) const
{
DBG_HELPER(dbg);
// FIXME: SEQUENTIAL not really needed in this case
Genesys_Register_Set regs((Genesys_Register_Set::SEQUENTIAL));
unsigned count;
std::uint8_t gpio;
/* at the end there will be no more document */
dev->document = false;
// first check for document event
gl646_gpio_read(dev->interface->get_usb_device(), &gpio);
DBG(DBG_info, "%s: GPIO=0x%02x\n", __func__, gpio);
// test status : paper event + HOMESNR -> no more doc ?
auto status = scanner_read_status(*dev);
// home sensor is set when document is inserted
if (status.is_at_home) {
dev->document = false;
DBG(DBG_info, "%s: no more document to eject\n", __func__);
return;
}
// there is a document inserted, eject it
dev->interface->write_register(0x01, 0xb0);
/* wait for motor to stop */
do {
dev->interface->sleep_ms(200);
status = scanner_read_status(*dev);
}
while (status.is_motor_enabled);
/* set up to fast move before scan then move until document is detected */
regs.init_reg(0x01, 0xb0);
/* AGOME, 2 slopes motor moving , eject 'backward' */
regs.init_reg(0x02, 0x5d);
/* motor feeding steps to 119880 */
regs.init_reg(0x3d, 1);
regs.init_reg(0x3e, 0xd4);
regs.init_reg(0x3f, 0x48);
/* 60 fast moving steps */
regs.init_reg(0x6b, 60);
/* set GPO */
regs.init_reg(0x66, 0x30);
/* stesp NO */
regs.init_reg(0x21, 4);
regs.init_reg(0x22, 1);
regs.init_reg(0x23, 1);
regs.init_reg(0x24, 4);
/* generate slope table 2 */
auto slope_table = create_slope_table_for_speed(MotorSlope::create_from_steps(10000, 1600, 60),
1600, StepType::FULL, 1, 4,
get_slope_table_max_size(AsicType::GL646));
// document eject:
// send regs
// start motor
// wait c1 status to become c8 : HOMESNR and ~MOTFLAG
// FIXME: sensor is not used.
const auto& sensor = sanei_genesys_find_sensor_any(dev);
scanner_send_slope_table(dev, sensor, 1, slope_table.table);
dev->interface->write_registers(regs);
scanner_start_action(*dev, true);
/* loop until paper sensor tells paper is out, and till motor is running */
/* use a 30 timeout */
count = 0;
do {
status = scanner_read_status(*dev);
dev->interface->sleep_ms(200);
count++;
} while (!status.is_at_home && (count < 150));
// read GPIO on exit
gl646_gpio_read(dev->interface->get_usb_device(), &gpio);
DBG(DBG_info, "%s: GPIO=0x%02x\n", __func__, gpio);
}
// Send the low-level scan command
void CommandSetGl646::begin_scan(Genesys_Device* dev, const Genesys_Sensor& sensor,
Genesys_Register_Set* reg, bool start_motor) const
{
DBG_HELPER(dbg);
(void) sensor;
// FIXME: SEQUENTIAL not really needed in this case
Genesys_Register_Set local_reg(Genesys_Register_Set::SEQUENTIAL);
local_reg.init_reg(0x03, reg->get8(0x03));
local_reg.init_reg(0x01, reg->get8(0x01) | REG_0x01_SCAN);
if (start_motor) {
local_reg.init_reg(0x0f, 0x01);
} else {
local_reg.init_reg(0x0f, 0x00); // do not start motor yet
}
dev->interface->write_registers(local_reg);
dev->advance_head_pos_by_session(ScanHeadId::PRIMARY);
}
// Send the stop scan command
static void end_scan_impl(Genesys_Device* dev, Genesys_Register_Set* reg, bool check_stop,
bool eject)
{
DBG_HELPER_ARGS(dbg, "check_stop = %d, eject = %d", check_stop, eject);
scanner_stop_action_no_move(*dev, *reg);
unsigned wait_limit_seconds = 30;
/* for sheetfed scanners, we may have to eject document */
if (dev->model->is_sheetfed) {
if (eject && dev->document) {
dev->cmd_set->eject_document(dev);
}
wait_limit_seconds = 3;
}
if (is_testing_mode()) {
return;
}
dev->interface->sleep_ms(100);
if (check_stop) {
for (unsigned i = 0; i < wait_limit_seconds * 10; i++) {
if (scanner_is_motor_stopped(*dev)) {
return;
}
dev->interface->sleep_ms(100);
}
throw SaneException(SANE_STATUS_IO_ERROR, "could not stop motor");
}
}
// Send the stop scan command
void CommandSetGl646::end_scan(Genesys_Device* dev, Genesys_Register_Set* reg,
bool check_stop) const
{
end_scan_impl(dev, reg, check_stop, false);
}
/**
* parks head
* @param dev scanner's device
* @param wait_until_home true if the function waits until head parked
*/
void CommandSetGl646::move_back_home(Genesys_Device* dev, bool wait_until_home) const
{
DBG_HELPER_ARGS(dbg, "wait_until_home = %d\n", wait_until_home);
int i;
int loop = 0;
auto status = scanner_read_status(*dev);
if (status.is_at_home) {
DBG(DBG_info, "%s: end since already at home\n", __func__);
dev->set_head_pos_zero(ScanHeadId::PRIMARY);
return;
}
/* stop motor if needed */
if (status.is_motor_enabled) {
gl646_stop_motor(dev);
dev->interface->sleep_ms(200);
}
/* when scanhead is moving then wait until scanhead stops or timeout */
DBG(DBG_info, "%s: ensuring that motor is off\n", __func__);
for (i = 400; i > 0; i--) {
// do not wait longer than 40 seconds, count down to get i = 0 when busy
status = scanner_read_status(*dev);
if (!status.is_motor_enabled && status.is_at_home) {
DBG(DBG_info, "%s: already at home and not moving\n", __func__);
dev->set_head_pos_zero(ScanHeadId::PRIMARY);
return;
}
if (!status.is_motor_enabled) {
break;
}
dev->interface->sleep_ms(100);
}
if (!i) /* the loop counted down to 0, scanner still is busy */
{
dev->set_head_pos_unknown(ScanHeadId::PRIMARY | ScanHeadId::SECONDARY);
throw SaneException(SANE_STATUS_DEVICE_BUSY, "motor is still on: device busy");
}
// setup for a backward scan of 65535 steps, with no actual data reading
auto resolution = sanei_genesys_get_lowest_dpi(dev);
const auto& sensor = sanei_genesys_find_sensor(dev, resolution, 3,
dev->model->default_method);
ScanSession session;
session.params.xres = resolution;
session.params.yres = resolution;
session.params.startx = 0;
session.params.starty = 65535;
session.params.pixels = 600;
session.params.lines = 1;
session.params.depth = 8;
session.params.channels = 3;
session.params.scan_method = dev->model->default_method;
session.params.scan_mode = ScanColorMode::COLOR_SINGLE_PASS;
session.params.color_filter = ColorFilter::RED;
session.params.contrast_adjustment = dev->settings.contrast;
session.params.brightness_adjustment = dev->settings.brightness;
session.params.flags = ScanFlag::REVERSE |
ScanFlag::AUTO_GO_HOME |
ScanFlag::DISABLE_GAMMA;
if (dev->model->default_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, sensor);
init_regs_for_scan_session(dev, sensor, &dev->reg, session);
/* backward , no actual data scanned TODO more setup flags to avoid this register manipulations ? */
regs_set_optical_off(dev->model->asic_type, dev->reg);
// sets frontend
gl646_set_fe(dev, sensor, AFE_SET, resolution);
/* write scan registers */
try {
dev->interface->write_registers(dev->reg);
} catch (...) {
DBG(DBG_error, "%s: failed to bulk write registers\n", __func__);
}
/* registers are restored to an iddl state, give up if no head to park */
if (dev->model->is_sheetfed) {
return;
}
// starts scan
{
// this is effectively the same as dev->cmd_set->begin_scan(dev, sensor, &dev->reg, true);
// except that we don't modify the head position calculations
// FIXME: SEQUENTIAL not really needed in this case
Genesys_Register_Set scan_local_reg(Genesys_Register_Set::SEQUENTIAL);
scan_local_reg.init_reg(0x03, dev->reg.get8(0x03));
scan_local_reg.init_reg(0x01, dev->reg.get8(0x01) | REG_0x01_SCAN);
scan_local_reg.init_reg(0x0f, 0x01);
dev->interface->write_registers(scan_local_reg);
}
if (is_testing_mode()) {
dev->interface->test_checkpoint("move_back_home");
dev->set_head_pos_zero(ScanHeadId::PRIMARY);
return;
}
/* loop until head parked */
if (wait_until_home)
{
while (loop < 300) /* do not wait longer then 30 seconds */
{
auto status = scanner_read_status(*dev);
if (status.is_at_home) {
DBG(DBG_info, "%s: reached home position\n", __func__);
dev->interface->sleep_ms(500);
dev->set_head_pos_zero(ScanHeadId::PRIMARY);
return;
}
dev->interface->sleep_ms(100);
++loop;
}
// when we come here then the scanner needed too much time for this, so we better
// stop the motor
catch_all_exceptions(__func__, [&](){ gl646_stop_motor (dev); });
catch_all_exceptions(__func__, [&](){ end_scan_impl(dev, &dev->reg, true, false); });
dev->set_head_pos_unknown(ScanHeadId::PRIMARY | ScanHeadId::SECONDARY);
throw SaneException(SANE_STATUS_IO_ERROR, "timeout while waiting for scanhead to go home");
}
DBG(DBG_info, "%s: scanhead is still moving\n", __func__);
}
/**
* init registers for shading calibration
* we assume that scanner's head is on an area suiting shading calibration.
* We scan a full scan width area by the shading line number for the device
*/
void CommandSetGl646::init_regs_for_shading(Genesys_Device* dev, const Genesys_Sensor& sensor,
Genesys_Register_Set& regs) const
{
DBG_HELPER(dbg);
(void) regs;
/* fill settings for scan : always a color scan */
int channels = 3;
unsigned cksel = get_cksel(dev->model->sensor_id, dev->settings.xres, channels);
unsigned resolution = sensor.get_optical_resolution() / cksel;
// FIXME: we select wrong calibration sensor
const auto& calib_sensor = sanei_genesys_find_sensor(dev, dev->settings.xres, channels,
dev->settings.scan_method);
auto pixels = dev->model->x_size_calib_mm * resolution / MM_PER_INCH;
unsigned calib_lines =
static_cast<unsigned>(dev->model->y_size_calib_mm * resolution / MM_PER_INCH);
ScanSession session;
session.params.xres = resolution;
session.params.yres = resolution;
session.params.startx = 0;
session.params.starty = 0;
session.params.pixels = pixels;
session.params.lines = calib_lines;
session.params.depth = 16;
session.params.channels = channels;
session.params.scan_method = dev->settings.scan_method;
session.params.scan_mode = ScanColorMode::COLOR_SINGLE_PASS;
session.params.color_filter = dev->settings.color_filter;
session.params.contrast_adjustment = dev->settings.contrast;
session.params.brightness_adjustment = dev->settings.brightness;
session.params.flags = ScanFlag::DISABLE_SHADING |
ScanFlag::DISABLE_GAMMA |
ScanFlag::IGNORE_COLOR_OFFSET |
ScanFlag::IGNORE_STAGGER_OFFSET;
if (dev->settings.scan_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, calib_sensor);
dev->cmd_set->init_regs_for_scan_session(dev, calib_sensor, &dev->reg, session);
dev->calib_session = session;
/* no shading */
dev->reg.find_reg(0x02).value |= REG_0x02_ACDCDIS; /* ease backtracking */
dev->reg.find_reg(0x02).value &= ~REG_0x02_FASTFED;
sanei_genesys_set_motor_power(dev->reg, false);
}
bool CommandSetGl646::needs_home_before_init_regs_for_scan(Genesys_Device* dev) const
{
return dev->is_head_pos_known(ScanHeadId::PRIMARY) &&
dev->head_pos(ScanHeadId::PRIMARY) &&
dev->settings.scan_method == ScanMethod::FLATBED;
}
/**
* this function send gamma table to ASIC
*/
void CommandSetGl646::send_gamma_table(Genesys_Device* dev, const Genesys_Sensor& sensor) const
{
DBG_HELPER(dbg);
int size;
int address;
int bits;
if (has_flag(dev->model->flags, ModelFlag::GAMMA_14BIT)) {
size = 16384;
bits = 14;
}
else
{
size = 4096;
bits = 12;
}
auto gamma = generate_gamma_buffer(dev, sensor, bits, size-1, size);
/* table address */
switch (dev->reg.find_reg(0x05).value >> 6)
{
case 0: /* 600 dpi */
address = 0x09000;
break;
case 1: /* 1200 dpi */
address = 0x11000;
break;
case 2: /* 2400 dpi */
address = 0x20000;
break;
default:
throw SaneException("invalid dpi");
}
dev->interface->write_buffer(0x3c, address, gamma.data(), size * 2 * 3);
}
/** @brief this function does the led calibration.
* this function does the led calibration by scanning one line of the calibration
* area below scanner's top on white strip. The scope of this function is
* currently limited to the XP200
*/
SensorExposure CommandSetGl646::led_calibration(Genesys_Device* dev, const Genesys_Sensor& sensor,
Genesys_Register_Set& regs) const
{
DBG_HELPER(dbg);
(void) regs;
unsigned int i, j;
int val;
int avg[3], avga, avge;
int turn;
std::uint16_t expr, expg, expb;
unsigned channels = dev->settings.get_channels();
ScanColorMode scan_mode = ScanColorMode::COLOR_SINGLE_PASS;
if (dev->settings.scan_mode != ScanColorMode::COLOR_SINGLE_PASS) {
scan_mode = ScanColorMode::GRAY;
}
// offset calibration is always done in color mode
unsigned pixels = dev->model->x_size_calib_mm * sensor.full_resolution / MM_PER_INCH;
ScanSession session;
session.params.xres = sensor.full_resolution;
session.params.yres = sensor.full_resolution;
session.params.startx = 0;
session.params.starty = 0;
session.params.pixels = pixels;
session.params.lines = 1;
session.params.depth = 16;
session.params.channels = channels;
session.params.scan_method = dev->settings.scan_method;
session.params.scan_mode = scan_mode;
session.params.color_filter = ColorFilter::RED;
session.params.contrast_adjustment = dev->settings.contrast;
session.params.brightness_adjustment = dev->settings.brightness;
session.params.flags = ScanFlag::DISABLE_SHADING;
if (dev->settings.scan_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, sensor);
// colors * bytes_per_color * scan lines
unsigned total_size = pixels * channels * 2 * 1;
std::vector<std::uint8_t> line(total_size);
/*
we try to get equal bright leds here:
loop:
average per color
adjust exposure times
*/
expr = sensor.exposure.red;
expg = sensor.exposure.green;
expb = sensor.exposure.blue;
turn = 0;
auto calib_sensor = sensor;
bool acceptable = false;
do {
calib_sensor.exposure.red = expr;
calib_sensor.exposure.green = expg;
calib_sensor.exposure.blue = expb;
DBG(DBG_info, "%s: starting first line reading\n", __func__);
dev->cmd_set->init_regs_for_scan_session(dev, calib_sensor, &dev->reg, session);
simple_scan(dev, calib_sensor, session, false, line, "led_calibration");
if (is_testing_mode()) {
return calib_sensor.exposure;
}
if (dbg_log_image_data()) {
char fn[30];
std::snprintf(fn, 30, "gl646_led_%02d.tiff", turn);
write_tiff_file(fn, line.data(), 16, channels, pixels, 1);
}
acceptable = true;
for (j = 0; j < channels; j++)
{
avg[j] = 0;
for (i = 0; i < pixels; i++) {
if (dev->model->is_cis) {
val = line[i * 2 + j * 2 * pixels + 1] * 256 + line[i * 2 + j * 2 * pixels];
} else {
val = line[i * 2 * channels + 2 * j + 1] * 256 + line[i * 2 * channels + 2 * j];
}
avg[j] += val;
}
avg[j] /= pixels;
}
DBG(DBG_info, "%s: average: %d,%d,%d\n", __func__, avg[0], avg[1], avg[2]);
acceptable = true;
if (!acceptable)
{
avga = (avg[0] + avg[1] + avg[2]) / 3;
expr = (expr * avga) / avg[0];
expg = (expg * avga) / avg[1];
expb = (expb * avga) / avg[2];
/* keep exposure time in a working window */
avge = (expr + expg + expb) / 3;
if (avge > 0x2000)
{
expr = (expr * 0x2000) / avge;
expg = (expg * 0x2000) / avge;
expb = (expb * 0x2000) / avge;
}
if (avge < 0x400)
{
expr = (expr * 0x400) / avge;
expg = (expg * 0x400) / avge;
expb = (expb * 0x400) / avge;
}
}
turn++;
}
while (!acceptable && turn < 100);
DBG(DBG_info,"%s: acceptable exposure: 0x%04x,0x%04x,0x%04x\n", __func__, expr, expg, expb);
// BUG: we don't store the result of the last iteration to the sensor
return calib_sensor.exposure;
}
/**
* average dark pixels of a scan
*/
static int dark_average(std::uint8_t * data, unsigned int pixels, unsigned int lines,
unsigned int channels, unsigned int black)
{
unsigned int i, j, k, average, count;
unsigned int avg[3];
std::uint8_t val;
/* computes average value on black margin */
for (k = 0; k < channels; k++)
{
avg[k] = 0;
count = 0;
for (i = 0; i < lines; i++)
{
for (j = 0; j < black; j++)
{
val = data[i * channels * pixels + j + k];
avg[k] += val;
count++;
}
}
if (count)
avg[k] /= count;
DBG(DBG_info, "%s: avg[%d] = %d\n", __func__, k, avg[k]);
}
average = 0;
for (i = 0; i < channels; i++)
average += avg[i];
average /= channels;
DBG(DBG_info, "%s: average = %d\n", __func__, average);
return average;
}
/** @brief calibration for AD frontend devices
* we do simple scan until all black_pixels are higher than 0,
* raising offset at each turn.
*/
static void ad_fe_offset_calibration(Genesys_Device* dev, const Genesys_Sensor& sensor)
{
DBG_HELPER(dbg);
(void) sensor;
unsigned int channels;
int pass = 0;
unsigned adr, min;
unsigned int bottom, black_pixels;
channels = 3;
// FIXME: maybe reuse `sensor`
const auto& calib_sensor = sanei_genesys_find_sensor(dev, sensor.full_resolution, 3,
ScanMethod::FLATBED);
black_pixels = (calib_sensor.black_pixels * sensor.full_resolution) / calib_sensor.full_resolution;
unsigned pixels = dev->model->x_size_calib_mm * sensor.full_resolution / MM_PER_INCH;
unsigned lines = CALIBRATION_LINES;
if (dev->model->is_cis) {
lines = ((lines + 2) / 3) * 3;
}
ScanSession session;
session.params.xres = sensor.full_resolution;
session.params.yres = sensor.full_resolution;
session.params.startx = 0;
session.params.starty = 0;
session.params.pixels = pixels;
session.params.lines = lines;
session.params.depth = 8;
session.params.channels = 3;
session.params.scan_method = dev->settings.scan_method;
session.params.scan_mode = ScanColorMode::COLOR_SINGLE_PASS;
session.params.color_filter = ColorFilter::RED;
session.params.contrast_adjustment = dev->settings.contrast;
session.params.brightness_adjustment = dev->settings.brightness;
session.params.flags = ScanFlag::DISABLE_SHADING;
if (dev->settings.scan_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, calib_sensor);
/* scan first line of data with no gain */
dev->frontend.set_gain(0, 0);
dev->frontend.set_gain(1, 0);
dev->frontend.set_gain(2, 0);
std::vector<std::uint8_t> line;
/* scan with no move */
bottom = 1;
do
{
pass++;
dev->frontend.set_offset(0, bottom);
dev->frontend.set_offset(1, bottom);
dev->frontend.set_offset(2, bottom);
dev->cmd_set->init_regs_for_scan_session(dev, calib_sensor, &dev->reg, session);
simple_scan(dev, calib_sensor, session, false, line, "ad_fe_offset_calibration");
if (is_testing_mode()) {
return;
}
if (dbg_log_image_data()) {
char title[30];
std::snprintf(title, 30, "gl646_offset%03d.tiff", static_cast<int>(bottom));
write_tiff_file(title, line.data(), 8, channels, pixels, lines);
}
min = 0;
for (unsigned y = 0; y < lines; y++) {
for (unsigned x = 0; x < black_pixels; x++) {
adr = (x + y * pixels) * channels;
if (line[adr] > min)
min = line[adr];
if (line[adr + 1] > min)
min = line[adr + 1];
if (line[adr + 2] > min)
min = line[adr + 2];
}
}
DBG(DBG_info, "%s: pass=%d, min=%d\n", __func__, pass, min);
bottom++;
}
while (pass < 128 && min == 0);
if (pass == 128)
{
throw SaneException(SANE_STATUS_INVAL, "failed to find correct offset");
}
DBG(DBG_info, "%s: offset=(%d,%d,%d)\n", __func__,
dev->frontend.get_offset(0),
dev->frontend.get_offset(1),
dev->frontend.get_offset(2));
}
/**
* This function does the offset calibration by scanning one line of the calibration
* area below scanner's top. There is a black margin and the remaining is white.
* genesys_search_start() must have been called so that the offsets and margins
* are already known.
* @param dev scanner's device
*/
void CommandSetGl646::offset_calibration(Genesys_Device* dev, const Genesys_Sensor& sensor,
Genesys_Register_Set& regs) const
{
DBG_HELPER(dbg);
(void) regs;
int pass = 0, avg;
int topavg, bottomavg;
int top, bottom, black_pixels;
if (dev->model->adc_id == AdcId::AD_XP200) {
ad_fe_offset_calibration(dev, sensor);
return;
}
/* setup for a RGB scan, one full sensor's width line */
/* resolution is the one from the final scan */
unsigned resolution = dev->settings.xres;
unsigned channels = 3;
const auto& calib_sensor = sanei_genesys_find_sensor(dev, resolution, channels,
ScanMethod::FLATBED);
black_pixels = (calib_sensor.black_pixels * resolution) / calib_sensor.full_resolution;
unsigned pixels = dev->model->x_size_calib_mm * resolution / MM_PER_INCH;
unsigned lines = CALIBRATION_LINES;
if (dev->model->is_cis) {
lines = ((lines + 2) / 3) * 3;
}
ScanSession session;
session.params.xres = resolution;
session.params.yres = resolution;
session.params.startx = 0;
session.params.starty = 0;
session.params.pixels = pixels;
session.params.lines = lines;
session.params.depth = 8;
session.params.channels = channels;
session.params.scan_method = dev->settings.scan_method;
session.params.scan_mode = ScanColorMode::COLOR_SINGLE_PASS;
session.params.color_filter = ColorFilter::RED;
session.params.contrast_adjustment = dev->settings.contrast;
session.params.brightness_adjustment = dev->settings.brightness;
session.params.flags = ScanFlag::DISABLE_SHADING;
if (dev->settings.scan_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, sensor);
/* scan first line of data with no gain, but with offset from
* last calibration */
dev->frontend.set_gain(0, 0);
dev->frontend.set_gain(1, 0);
dev->frontend.set_gain(2, 0);
/* scan with no move */
bottom = 90;
dev->frontend.set_offset(0, bottom);
dev->frontend.set_offset(1, bottom);
dev->frontend.set_offset(2, bottom);
std::vector<std::uint8_t> first_line, second_line;
dev->cmd_set->init_regs_for_scan_session(dev, sensor, &dev->reg, session);
simple_scan(dev, calib_sensor, session, false, first_line, "offset_first_line");
if (dbg_log_image_data()) {
char title[30];
std::snprintf(title, 30, "gl646_offset%03d.tiff", bottom);
write_tiff_file(title, first_line.data(), 8, channels, pixels, lines);
}
bottomavg = dark_average(first_line.data(), pixels, lines, channels, black_pixels);
DBG(DBG_info, "%s: bottom avg=%d\n", __func__, bottomavg);
/* now top value */
top = 231;
dev->frontend.set_offset(0, top);
dev->frontend.set_offset(1, top);
dev->frontend.set_offset(2, top);
dev->cmd_set->init_regs_for_scan_session(dev, calib_sensor, &dev->reg, session);
simple_scan(dev, calib_sensor, session, false, second_line, "offset_second_line");
if (dbg_log_image_data()) {
char title[30];
std::snprintf(title, 30, "gl646_offset%03d.tiff", top);
write_tiff_file(title, second_line.data(), 8, channels, pixels, lines);
}
topavg = dark_average(second_line.data(), pixels, lines, channels, black_pixels);
DBG(DBG_info, "%s: top avg=%d\n", __func__, topavg);
if (is_testing_mode()) {
return;
}
/* loop until acceptable level */
while ((pass < 32) && (top - bottom > 1))
{
pass++;
/* settings for new scan */
dev->frontend.set_offset(0, (top + bottom) / 2);
dev->frontend.set_offset(1, (top + bottom) / 2);
dev->frontend.set_offset(2, (top + bottom) / 2);
// scan with no move
dev->cmd_set->init_regs_for_scan_session(dev, calib_sensor, &dev->reg, session);
simple_scan(dev, calib_sensor, session, false, second_line,
"offset_calibration_i");
if (dbg_log_image_data()) {
char title[30];
std::snprintf(title, 30, "gl646_offset%03d.tiff", dev->frontend.get_offset(1));
write_tiff_file(title, second_line.data(), 8, channels, pixels, lines);
}
avg = dark_average(second_line.data(), pixels, lines, channels, black_pixels);
DBG(DBG_info, "%s: avg=%d offset=%d\n", __func__, avg, dev->frontend.get_offset(1));
/* compute new boundaries */
if (topavg == avg)
{
topavg = avg;
top = dev->frontend.get_offset(1);
}
else
{
bottomavg = avg;
bottom = dev->frontend.get_offset(1);
}
}
DBG(DBG_info, "%s: offset=(%d,%d,%d)\n", __func__,
dev->frontend.get_offset(0),
dev->frontend.get_offset(1),
dev->frontend.get_offset(2));
}
/**
* Alternative coarse gain calibration
* this on uses the settings from offset_calibration. First scan moves so
* we can go to calibration area for XPA.
* @param dev device for scan
* @param dpi resolutnio to calibrate at
*/
void CommandSetGl646::coarse_gain_calibration(Genesys_Device* dev, const Genesys_Sensor& sensor,
Genesys_Register_Set& regs, int dpi) const
{
DBG_HELPER(dbg);
(void) dpi;
(void) sensor;
(void) regs;
float average[3];
char title[32];
/* setup for a RGB scan, one full sensor's width line */
/* resolution is the one from the final scan */
unsigned channels = 3;
// BUG: the following comment is incorrect
// we are searching a sensor resolution */
const auto& calib_sensor = sanei_genesys_find_sensor(dev, dev->settings.xres, channels,
ScanMethod::FLATBED);
unsigned pixels = 0;
float start = 0;
if (dev->settings.scan_method == ScanMethod::FLATBED) {
pixels = dev->model->x_size_calib_mm * dev->settings.xres / MM_PER_INCH;
} else {
start = dev->model->x_offset_ta;
pixels = static_cast<unsigned>(
(dev->model->x_size_ta * dev->settings.xres) / MM_PER_INCH);
}
unsigned lines = CALIBRATION_LINES;
// round up to multiple of 3 in case of CIS scanner
if (dev->model->is_cis) {
lines = ((lines + 2) / 3) * 3;
}
start = static_cast<float>((start * dev->settings.xres) / MM_PER_INCH);
ScanSession session;
session.params.xres = dev->settings.xres;
session.params.yres = dev->settings.xres;
session.params.startx = static_cast<unsigned>(start);
session.params.starty = 0;
session.params.pixels = pixels;
session.params.lines = lines;
session.params.depth = 8;
session.params.channels = channels;
session.params.scan_method = dev->settings.scan_method;
session.params.scan_mode = ScanColorMode::COLOR_SINGLE_PASS;
session.params.color_filter = ColorFilter::RED;
session.params.contrast_adjustment = dev->settings.contrast;
session.params.brightness_adjustment = dev->settings.brightness;
session.params.flags = ScanFlag::DISABLE_SHADING;
if (dev->settings.scan_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, calib_sensor);
/* start gain value */
dev->frontend.set_gain(0, 1);
dev->frontend.set_gain(1, 1);
dev->frontend.set_gain(2, 1);
average[0] = 0;
average[1] = 0;
average[2] = 0;
unsigned pass = 0;
std::vector<std::uint8_t> line;
/* loop until each channel raises to acceptable level */
while (((average[0] < calib_sensor.gain_white_ref) ||
(average[1] < calib_sensor.gain_white_ref) ||
(average[2] < calib_sensor.gain_white_ref)) && (pass < 30))
{
// scan with no move
dev->cmd_set->init_regs_for_scan_session(dev, calib_sensor, &dev->reg, session);
simple_scan(dev, calib_sensor, session, false, line, "coarse_gain_calibration");
if (dbg_log_image_data()) {
std::sprintf(title, "gl646_gain%02d.tiff", pass);
write_tiff_file(title, line.data(), 8, channels, pixels, lines);
}
pass++;
// average high level for each channel and compute gain to reach the target code
// we only use the central half of the CCD data
for (unsigned k = 0; k < channels; k++) {
// we find the maximum white value, so we can deduce a threshold
// to average white values
unsigned maximum = 0;
for (unsigned i = 0; i < lines; i++) {
for (unsigned j = 0; j < pixels; j++) {
unsigned val = line[i * channels * pixels + j + k];
maximum = std::max(maximum, val);
}
}
maximum = static_cast<int>(maximum * 0.9);
// computes white average
average[k] = 0;
unsigned count = 0;
for (unsigned i = 0; i < lines; i++) {
for (unsigned j = 0; j < pixels; j++) {
// averaging only white points allow us not to care about dark margins
unsigned val = line[i * channels * pixels + j + k];
if (val > maximum) {
average[k] += val;
count++;
}
}
}
average[k] = average[k] / count;
// adjusts gain for the channel
if (average[k] < calib_sensor.gain_white_ref) {
dev->frontend.set_gain(k, dev->frontend.get_gain(k) + 1);
}
DBG(DBG_info, "%s: channel %d, average = %.2f, gain = %d\n", __func__, k, average[k],
dev->frontend.get_gain(k));
}
}
DBG(DBG_info, "%s: gains=(%d,%d,%d)\n", __func__,
dev->frontend.get_gain(0),
dev->frontend.get_gain(1),
dev->frontend.get_gain(2));
}
/**
* sets up the scanner's register for warming up. We scan 2 lines without moving.
*
*/
void CommandSetGl646::init_regs_for_warmup(Genesys_Device* dev, const Genesys_Sensor& sensor,
Genesys_Register_Set* local_reg) const
{
DBG_HELPER(dbg);
(void) sensor;
dev->frontend = dev->frontend_initial;
unsigned resolution = 300;
const auto& local_sensor = sanei_genesys_find_sensor(dev, resolution, 1,
dev->settings.scan_method);
// set up for a full width 2 lines gray scan without moving
unsigned pixels = dev->model->x_size_calib_mm * resolution / MM_PER_INCH;
ScanSession session;
session.params.xres = resolution;
session.params.yres = resolution;
session.params.startx = 0;
session.params.starty = 0;
session.params.pixels = pixels;
session.params.lines = 2;
session.params.depth = dev->model->bpp_gray_values.front();
session.params.channels = 1;
session.params.scan_method = dev->settings.scan_method;
session.params.scan_mode = ScanColorMode::GRAY;
session.params.color_filter = ColorFilter::RED;
session.params.contrast_adjustment = 0;
session.params.brightness_adjustment = 0;
session.params.flags = ScanFlag::DISABLE_SHADING |
ScanFlag::DISABLE_GAMMA;
if (dev->settings.scan_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, local_sensor);
dev->cmd_set->init_regs_for_scan_session(dev, local_sensor, &dev->reg, session);
/* we are not going to move, so clear these bits */
dev->reg.find_reg(0x02).value &= ~REG_0x02_FASTFED;
/* copy to local_reg */
*local_reg = dev->reg;
/* turn off motor during this scan */
sanei_genesys_set_motor_power(*local_reg, false);
// now registers are ok, write them to scanner
gl646_set_fe(dev, local_sensor, AFE_SET, session.params.xres);
}
/* *
* initialize ASIC : registers, motor tables, and gamma tables
* then ensure scanner's head is at home
* @param dev device description of the scanner to initialize
*/
void CommandSetGl646::init(Genesys_Device* dev) const
{
DBG_INIT();
DBG_HELPER(dbg);
std::uint8_t val = 0;
std::uint32_t addr = 0xdead;
size_t len;
// to detect real power up condition, we write to REG_0x41 with pwrbit set, then read it back.
// When scanner is cold (just replugged) PWRBIT will be set in the returned value
auto status = scanner_read_status(*dev);
if (status.is_replugged) {
DBG(DBG_info, "%s: device is cold\n", __func__);
} else {
DBG(DBG_info, "%s: device is hot\n", __func__);
}
const auto& sensor = sanei_genesys_find_sensor_any(dev);
/* if scanning session hasn't been initialized, set it up */
if (!dev->already_initialized)
{
dev->dark_average_data.clear();
dev->white_average_data.clear();
dev->settings.color_filter = ColorFilter::GREEN;
/* Set default values for registers */
gl646_init_regs (dev);
// Init shading data
sanei_genesys_init_shading_data(dev, sensor,
dev->model->x_size_calib_mm * sensor.full_resolution /
MM_PER_INCH);
dev->initial_regs = dev->reg;
}
// execute physical unit init only if cold
if (status.is_replugged)
{
DBG(DBG_info, "%s: device is cold\n", __func__);
val = 0x04;
dev->interface->get_usb_device().control_msg(REQUEST_TYPE_OUT, REQUEST_REGISTER,
VALUE_INIT, INDEX, 1, &val);
// ASIC reset
dev->interface->write_register(0x0e, 0x00);
dev->interface->sleep_ms(100);
// Write initial registers
dev->interface->write_registers(dev->reg);
// send gamma tables if needed
dev->cmd_set->send_gamma_table(dev, sensor);
// Set powersaving(default = 15 minutes)
dev->cmd_set->set_powersaving(dev, 15);
}
// Set analog frontend
gl646_set_fe(dev, sensor, AFE_INIT, 0);
/* GPO enabling for XP200 */
if (dev->model->sensor_id == SensorId::CIS_XP200) {
dev->interface->write_register(0x68, dev->gpo.regs.get_value(0x68));
dev->interface->write_register(0x69, dev->gpo.regs.get_value(0x69));
// enable GPIO
gl646_gpio_output_enable(dev->interface->get_usb_device(), 6);
// writes 0 to GPIO
gl646_gpio_write(dev->interface->get_usb_device(), 0);
// clear GPIO enable
gl646_gpio_output_enable(dev->interface->get_usb_device(), 0);
dev->interface->write_register(0x66, 0x10);
dev->interface->write_register(0x66, 0x00);
dev->interface->write_register(0x66, 0x10);
}
/* MD6471/G2410 and XP200 read/write data from an undocumented memory area which
* is after the second slope table */
if (dev->model->gpio_id != GpioId::HP3670 &&
dev->model->gpio_id != GpioId::HP2400)
{
switch (sensor.full_resolution)
{
case 600:
addr = 0x08200;
break;
case 1200:
addr = 0x10200;
break;
case 2400:
addr = 0x1fa00;
break;
}
sanei_genesys_set_buffer_address(dev, addr);
sanei_usb_set_timeout (2 * 1000);
len = 6;
// for some reason, read fails here for MD6471, HP2300 and XP200 one time out of
// 2 scanimage launches
try {
dev->interface->bulk_read_data(0x45, dev->control, len);
} catch (...) {
dev->interface->bulk_read_data(0x45, dev->control, len);
}
sanei_usb_set_timeout (30 * 1000);
}
else
/* HP2400 and HP3670 case */
{
dev->control[0] = 0x00;
dev->control[1] = 0x00;
dev->control[2] = 0x01;
dev->control[3] = 0x00;
dev->control[4] = 0x00;
dev->control[5] = 0x00;
}
/* ensure head is correctly parked, and check lock */
if (!dev->model->is_sheetfed) {
move_back_home(dev, true);
}
/* here session and device are initialized */
dev->already_initialized = true;
}
static void simple_scan(Genesys_Device* dev, const Genesys_Sensor& sensor,
const ScanSession& session, bool move,
std::vector<std::uint8_t>& data, const char* scan_identifier)
{
unsigned lines = session.output_line_count;
if (!dev->model->is_cis) {
lines++;
}
std::size_t size = lines * session.params.pixels;
unsigned bpp = session.params.depth == 16 ? 2 : 1;
size *= bpp * session.params.channels;
data.clear();
data.resize(size);
// initialize frontend
gl646_set_fe(dev, sensor, AFE_SET, session.params.xres);
// no watch dog for simple scan
dev->reg.find_reg(0x01).value &= ~REG_0x01_DOGENB;
/* one table movement for simple scan */
dev->reg.find_reg(0x02).value &= ~REG_0x02_FASTFED;
if (!move) {
sanei_genesys_set_motor_power(dev->reg, false);
}
/* no automatic go home when using XPA */
if (session.params.scan_method == ScanMethod::TRANSPARENCY) {
dev->reg.find_reg(0x02).value &= ~REG_0x02_AGOHOME;
}
// write scan registers
dev->interface->write_registers(dev->reg);
// starts scan
dev->cmd_set->begin_scan(dev, sensor, &dev->reg, move);
if (is_testing_mode()) {
dev->interface->test_checkpoint(scan_identifier);
return;
}
wait_until_buffer_non_empty(dev, true);
// now we're on target, we can read data
sanei_genesys_read_data_from_scanner(dev, data.data(), size);
/* in case of CIS scanner, we must reorder data */
if (dev->model->is_cis && session.params.scan_mode == ScanColorMode::COLOR_SINGLE_PASS) {
auto pixels_count = session.params.pixels;
std::vector<std::uint8_t> buffer(pixels_count * 3 * bpp);
if (bpp == 1) {
for (unsigned y = 0; y < lines; y++) {
// reorder line
for (unsigned x = 0; x < pixels_count; x++) {
buffer[x * 3] = data[y * pixels_count * 3 + x];
buffer[x * 3 + 1] = data[y * pixels_count * 3 + pixels_count + x];
buffer[x * 3 + 2] = data[y * pixels_count * 3 + 2 * pixels_count + x];
}
// copy line back
std::memcpy(data.data() + pixels_count * 3 * y, buffer.data(), pixels_count * 3);
}
} else {
for (unsigned y = 0; y < lines; y++) {
// reorder line
auto pixels_count = session.params.pixels;
for (unsigned x = 0; x < pixels_count; x++) {
buffer[x * 6] = data[y * pixels_count * 6 + x * 2];
buffer[x * 6 + 1] = data[y * pixels_count * 6 + x * 2 + 1];
buffer[x * 6 + 2] = data[y * pixels_count * 6 + 2 * pixels_count + x * 2];
buffer[x * 6 + 3] = data[y * pixels_count * 6 + 2 * pixels_count + x * 2 + 1];
buffer[x * 6 + 4] = data[y * pixels_count * 6 + 4 * pixels_count + x * 2];
buffer[x * 6 + 5] = data[y * pixels_count * 6 + 4 * pixels_count + x * 2 + 1];
}
// copy line back
std::memcpy(data.data() + pixels_count * 6 * y, buffer.data(),pixels_count * 6);
}
}
}
// end scan , waiting the motor to stop if needed (if moving), but without ejecting doc
end_scan_impl(dev, &dev->reg, true, false);
}
/**
* update the status of the required sensor in the scanner session
* the button fields are used to make events 'sticky'
*/
void CommandSetGl646::update_hardware_sensors(Genesys_Scanner* session) const
{
DBG_HELPER(dbg);
Genesys_Device *dev = session->dev;
std::uint8_t value;
// do what is needed to get a new set of events, but try to not loose any of them.
gl646_gpio_read(dev->interface->get_usb_device(), &value);
DBG(DBG_io, "%s: GPIO=0x%02x\n", __func__, value);
// scan button
if (dev->model->buttons & GENESYS_HAS_SCAN_SW) {
switch (dev->model->gpio_id) {
case GpioId::XP200:
session->buttons[BUTTON_SCAN_SW].write((value & 0x02) != 0);
break;
case GpioId::MD_5345:
session->buttons[BUTTON_SCAN_SW].write(value == 0x16);
break;
case GpioId::HP2300:
session->buttons[BUTTON_SCAN_SW].write(value == 0x6c);
break;
case GpioId::HP3670:
case GpioId::HP2400:
session->buttons[BUTTON_SCAN_SW].write((value & 0x20) == 0);
break;
default:
throw SaneException(SANE_STATUS_UNSUPPORTED, "unknown gpo type");
}
}
// email button
if (dev->model->buttons & GENESYS_HAS_EMAIL_SW) {
switch (dev->model->gpio_id) {
case GpioId::MD_5345:
session->buttons[BUTTON_EMAIL_SW].write(value == 0x12);
break;
case GpioId::HP3670:
case GpioId::HP2400:
session->buttons[BUTTON_EMAIL_SW].write((value & 0x08) == 0);
break;
default:
throw SaneException(SANE_STATUS_UNSUPPORTED, "unknown gpo type");
}
}
// copy button
if (dev->model->buttons & GENESYS_HAS_COPY_SW) {
switch (dev->model->gpio_id) {
case GpioId::MD_5345:
session->buttons[BUTTON_COPY_SW].write(value == 0x11);
break;
case GpioId::HP2300:
session->buttons[BUTTON_COPY_SW].write(value == 0x5c);
break;
case GpioId::HP3670:
case GpioId::HP2400:
session->buttons[BUTTON_COPY_SW].write((value & 0x10) == 0);
break;
default:
throw SaneException(SANE_STATUS_UNSUPPORTED, "unknown gpo type");
}
}
// power button
if (dev->model->buttons & GENESYS_HAS_POWER_SW) {
switch (dev->model->gpio_id) {
case GpioId::MD_5345:
session->buttons[BUTTON_POWER_SW].write(value == 0x14);
break;
default:
throw SaneException(SANE_STATUS_UNSUPPORTED, "unknown gpo type");
}
}
// ocr button
if (dev->model->buttons & GENESYS_HAS_OCR_SW) {
switch (dev->model->gpio_id) {
case GpioId::MD_5345:
session->buttons[BUTTON_OCR_SW].write(value == 0x13);
break;
default:
throw SaneException(SANE_STATUS_UNSUPPORTED, "unknown gpo type");
}
}
// document detection
if (dev->model->buttons & GENESYS_HAS_PAGE_LOADED_SW) {
switch (dev->model->gpio_id) {
case GpioId::XP200:
session->buttons[BUTTON_PAGE_LOADED_SW].write((value & 0x04) != 0);
break;
default:
throw SaneException(SANE_STATUS_UNSUPPORTED, "unknown gpo type");
}
}
/* XPA detection */
if (dev->model->has_method(ScanMethod::TRANSPARENCY)) {
switch (dev->model->gpio_id) {
case GpioId::HP3670:
case GpioId::HP2400:
/* test if XPA is plugged-in */
if ((value & 0x40) == 0) {
session->opt[OPT_SOURCE].cap &= ~SANE_CAP_INACTIVE;
} else {
session->opt[OPT_SOURCE].cap |= SANE_CAP_INACTIVE;
}
break;
default:
throw SaneException(SANE_STATUS_UNSUPPORTED, "unknown gpo type");
}
}
}
void CommandSetGl646::update_home_sensor_gpio(Genesys_Device& dev) const
{
DBG_HELPER(dbg);
(void) dev;
}
static void write_control(Genesys_Device* dev, const Genesys_Sensor& sensor, int resolution)
{
DBG_HELPER(dbg);
std::uint8_t control[4];
std::uint32_t addr = 0xdead;
/* 2300 does not write to 'control' */
if (dev->model->motor_id == MotorId::HP2300) {
return;
}
/* MD6471/G2410/HP2300 and XP200 read/write data from an undocumented memory area which
* is after the second slope table */
switch (sensor.full_resolution)
{
case 600:
addr = 0x08200;
break;
case 1200:
addr = 0x10200;
break;
case 2400:
addr = 0x1fa00;
break;
default:
throw SaneException("failed to compute control address");
}
/* XP200 sets dpi, what other scanner put is unknown yet */
switch (dev->model->motor_id)
{
case MotorId::XP200:
/* we put scan's dpi, not motor one */
control[0] = resolution & 0xff;
control[1] = (resolution >> 8) & 0xff;
control[2] = dev->control[4];
control[3] = dev->control[5];
break;
case MotorId::HP3670:
case MotorId::HP2400:
case MotorId::MD_5345:
default:
control[0] = dev->control[2];
control[1] = dev->control[3];
control[2] = dev->control[4];
control[3] = dev->control[5];
break;
}
dev->interface->write_buffer(0x3c, addr, control, 4);
}
void CommandSetGl646::wait_for_motor_stop(Genesys_Device* dev) const
{
(void) dev;
}
void CommandSetGl646::send_shading_data(Genesys_Device* dev, const Genesys_Sensor& sensor,
std::uint8_t* data, int size) const
{
(void) dev;
(void) sensor;
(void) data;
(void) size;
throw SaneException("not implemented");
}
ScanSession CommandSetGl646::calculate_scan_session(const Genesys_Device* dev,
const Genesys_Sensor& sensor,
const Genesys_Settings& settings) const
{
// compute distance to move
float move = 0;
if (!dev->model->is_sheetfed) {
move = dev->model->y_offset;
// add tl_y to base movement
}
move += settings.tl_y;
if (move < 0) {
DBG(DBG_error, "%s: overriding negative move value %f\n", __func__, move);
move = 0;
}
move = static_cast<float>((move * dev->motor.base_ydpi) / MM_PER_INCH);
float start = settings.tl_x;
if (settings.scan_method == ScanMethod::FLATBED) {
start += dev->model->x_offset;
} else {
start += dev->model->x_offset_ta;
}
start = static_cast<float>((start * settings.xres) / MM_PER_INCH);
ScanSession session;
session.params.xres = settings.xres;
session.params.yres = settings.yres;
session.params.startx = static_cast<unsigned>(start);
session.params.starty = static_cast<unsigned>(move);
session.params.pixels = settings.pixels;
session.params.requested_pixels = settings.requested_pixels;
session.params.lines = settings.lines;
session.params.depth = settings.depth;
session.params.channels = settings.get_channels();
session.params.scan_method = dev->settings.scan_method;
session.params.scan_mode = settings.scan_mode;
session.params.color_filter = settings.color_filter;
session.params.contrast_adjustment = settings.contrast;
session.params.brightness_adjustment = settings.brightness;
session.params.flags = ScanFlag::AUTO_GO_HOME;
if (settings.scan_method == ScanMethod::TRANSPARENCY) {
session.params.flags |= ScanFlag::USE_XPA;
}
compute_session(dev, session, sensor);
return session;
}
void CommandSetGl646::asic_boot(Genesys_Device *dev, bool cold) const
{
(void) dev;
(void) cold;
throw SaneException("not implemented");
}
} // namespace gl646
} // namespace genesys
|