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
|
/*.............................................................................
* Project : SANE library for Plustek flatbed scanners.
*.............................................................................
*/
/** @file plustek-usbscan.c
* @brief Scanning...
*
* Based on sources acquired from Plustek Inc.<br>
* Copyright (C) 2001-2013 Gerhard Jaeger <gerhard@gjaeger.de>
*
* History:
* - 0.40 - starting version of the USB support
* - 0.41 - minor fixes
* - 0.42 - added some stuff for CIS devices
* - 0.43 - no changes
* - 0.44 - added CIS specific settings and calculations
* - removed usb_IsEscPressed
* - 0.45 - fixed the special setting stuff for CanoScan
* - fixed pixel calculation for binary modes
* - fixed cancel hang problem
* - fixed CIS PhyBytes adjustment
* - removed CanoScan specific setting stuff
* - 0.46 - fixed problem in usb_SetScanParameters()
* - 0.47 - no changes
* - 0.48 - minor fixes
* - 0.49 - a_bRegs is now part of the device structure
* - using now PhyDpi.y as selector for the motor MCLK range
* - changed usb_MapDownload prototype
* - 0.50 - cleanup
* - 0.51 - added usb_get_res() and usb_GetPhyPixels()
* - 0.52 - removed stuff, that will most probably never be used
* .
* <hr>
* This file is part of the SANE package.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
* As a special exception, the authors of SANE give permission for
* additional uses of the libraries contained in this release of SANE.
*
* The exception is that, if you link a SANE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public
* License. Your use of that executable is in no way restricted on
* account of linking the SANE library code into it.
*
* This exception does not, however, invalidate any other reasons why
* the executable file might be covered by the GNU General Public
* License.
*
* If you submit changes to SANE to the maintainers to be included in
* a subsequent release, you agree by submitting the changes that
* those changes may be distributed with this exception intact.
*
* If you write modifications of your own for SANE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
* <hr>
*/
#define DIVIDER 8
/** array used to get motor-settings and mclk-settings
*/
static int dpi_ranges[] = { 75,100,150,200,300,400,600,800,1200,2400 };
static u_char bMaxITA;
static SANE_Bool m_fAutoPark;
static SANE_Bool m_fFirst;
static double m_dHDPIDivider;
static double m_dMCLKDivider;
static ScanParam *m_pParam;
static u_char m_bLineRateColor;
static u_char m_bCM;
static u_char m_bIntTimeAdjust;
static u_char m_bOldScanData;
static u_short m_wFastFeedStepSize;
static u_short m_wLineLength;
static u_short m_wStepSize;
static u_long m_dwPauseLimit;
static SANE_Bool m_fStart = SANE_FALSE;
/* Prototype... */
static SANE_Bool usb_DownloadShadingData( Plustek_Device*, u_char );
/** returns the min of the two values val1 and val2
* @param val1 - first parameter
* @param val2 - second parameter
* @return val1 if val1 < val2, else val1
*/
static u_long
usb_min( u_long val1, u_long val2 )
{
if( val1 > val2 )
return val2;
return val1;
}
/** returns the max of the two values val1 and val2
* @param val1 - first parameter
* @param val2 - second parameter
* @return val1 if val1 > val2, else val2
*/
static u_long
usb_max( u_long val1, u_long val2 )
{
if( val1 > val2 )
return val1;
return val2;
}
/** function to get the possible resolution for a specific base resolution,
* according to the dividers a LM983x offers.
* @param base - scanners optical resolution
* @param idx - which divider to use
*/
static u_short
usb_get_res( u_short base, u_short idx )
{
double div_list[DIVIDER] = { 12.0, 8.0, 6.0, 4.0, 3.0, 2.0, 1.5, 1.0 };
if( idx == 0 || idx > DIVIDER )
return 0;
return (u_short)((double)base/div_list[idx-1]);
}
/** Set the horizontal DPI divider.
* Affected registers:<br>
* 0x09 - Horizontal DPI divider HDPI_DIV<br>
*
* @param dev - pointer to our device structure,
* it should contain all we need
* @param xdpi - user specified horizontal resolution
* @return - the function returns the "normalized" horizontal resolution.
*/
static u_short
usb_SetAsicDpiX( Plustek_Device *dev, u_short xdpi )
{
u_short res;
ScanDef *scanning = &dev->scanning;
DCapsDef *scaps = &dev->usbDev.Caps;
u_char *regs = dev->usbDev.a_bRegs;
/* limit xdpi to lower value for certain devices...
*/
if( scaps->OpticDpi.x == 1200 &&
scanning->sParam.bDataType != SCANDATATYPE_Color &&
xdpi < 150 &&
scanning->sParam.bDataType == SCANDATATYPE_BW ) {
xdpi = 150;
DBG( _DBG_INFO2, "* LIMIT XDPI to %udpi\n", xdpi );
}
m_dHDPIDivider = (double)scaps->OpticDpi.x / xdpi;
if (m_dHDPIDivider < 1.5)
{
m_dHDPIDivider = 1.0;
regs[0x09] = 0;
}
else if (m_dHDPIDivider < 2.0)
{
m_dHDPIDivider = 1.5;
regs[0x09] = 1;
}
else if (m_dHDPIDivider < 3.0)
{
m_dHDPIDivider = 2.0;
regs[0x09] = 2;
}
else if (m_dHDPIDivider < 4.0)
{
m_dHDPIDivider = 3.0;
regs[0x09] = 3;
}
else if (m_dHDPIDivider < 6.0)
{
m_dHDPIDivider = 4.0;
regs[0x09] = 4;
}
else if (m_dHDPIDivider < 8.0)
{
m_dHDPIDivider = 6.0;
regs[0x09] = 5;
}
else if (m_dHDPIDivider < 12.0)
{
m_dHDPIDivider = 8.0;
regs[0x09] = 6;
}
else
{
m_dHDPIDivider = 12.0;
regs[0x09] = 7;
}
/* adjust, if any turbo/preview mode is set, should be 0 here... */
if( regs[0x0a] )
regs[0x09] -= ((regs[0x0a] >> 2) + 2);
DBG( _DBG_INFO2, "* HDPI: %.3f\n", m_dHDPIDivider );
res = (u_short)((double)scaps->OpticDpi.x / m_dHDPIDivider);
DBG( _DBG_INFO2, "* XDPI=%u, HDPI=%.3f\n", res, m_dHDPIDivider );
return res;
}
/**
* @param dev - pointer to our device structure,
* it should contain all we need
* @param ydpi - user specified vertical resolution
* @return -
*/
static u_short
usb_SetAsicDpiY( Plustek_Device *dev, u_short ydpi )
{
ScanDef *scanning = &dev->scanning;
DCapsDef *sCaps = &dev->usbDev.Caps;
HWDef *hw = &dev->usbDev.HwSetting;
u_short wMinDpi, wDpi;
if(0 != sCaps->bSensorDistance )
wMinDpi = sCaps->OpticDpi.y / sCaps->bSensorDistance;
else
wMinDpi = 75;
/* Here we might have to check against the MinDpi value ! */
wDpi = (ydpi + wMinDpi - 1) / wMinDpi * wMinDpi;
/*
* HEINER: added '*2'
*/
if( wDpi > sCaps->OpticDpi.y * 2 )
wDpi = sCaps->OpticDpi.y * 2;
if( (hw->motorModel == MODEL_Tokyo600) ||
!_IS_PLUSTEKMOTOR(hw->motorModel)) {
/* return wDpi; */
} else if( sCaps->wFlags & DEVCAPSFLAG_Adf && sCaps->OpticDpi.x == 600 ) {
/* for ADF scanner color mode 300 dpi big noise */
if( scanning->sParam.bDataType == SCANDATATYPE_Color &&
scanning->sParam.bBitDepth > 8 && wDpi < 300 ) {
wDpi = 300;
}
} else if( sCaps->OpticDpi.x == 1200 ) {
if( scanning->sParam.bDataType != SCANDATATYPE_Color && wDpi < 200) {
wDpi = 200;
}
}
DBG( _DBG_INFO2, "* YDPI=%u, MinDPIY=%u\n", wDpi, wMinDpi );
return wDpi;
}
/** set color mode and sensor configuration stuff, according to the data mode
* Affected registers:<br>
* 0x26 - 0x27 - Color Mode settings<br>
* 0x0f - 0x18 - Sensor Configuration - directly from the HwDefs<br>
* 0x09 - add Data Mode and Pixel Packing<br>
*
* @param dev - pointer to our device structure,
* it should contain all we need
* @param pParam - pointer to the current scan parameters
* @return - Nothing
*/
static void
usb_SetColorAndBits( Plustek_Device *dev, ScanParam *pParam )
{
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
/* Set pixel packing, data mode and AFE operation */
switch( pParam->bDataType ) {
case SCANDATATYPE_Color:
m_bCM = 3;
regs[0x26] = hw->bReg_0x26 & 0x7;
/* if set to one channel color, we select the blue channel
* as input source, this is the default, but I don't know
* what happens, if we deselect this
*/
if( regs[0x26] & _ONE_CH_COLOR )
regs[0x26] |= (_BLUE_CH | 0x01);
memcpy( ®s[0x0f], hw->bReg_0x0f_Color, 10 );
break;
case SCANDATATYPE_Gray:
m_bCM = 1;
regs[0x26] = (hw->bReg_0x26 & 0x18) | 0x04;
memcpy( ®s[0x0f], hw->bReg_0x0f_Mono, 10 );
break;
case SCANDATATYPE_BW:
m_bCM = 1;
regs[0x26] = (hw->bReg_0x26 & 0x18) | 0x04;
memcpy( ®s[0x0f], hw->bReg_0x0f_Mono, 10 );
break;
}
regs[0x27] = hw->bReg_0x27;
if( pParam->bBitDepth > 8 ) {
regs[0x09] |= 0x20; /* 14/16bit image data */
} else if( pParam->bBitDepth == 8 ) {
regs[0x09] |= 0x18; /* 8bits/per pixel */
}
}
/**
* Data output from NS983X should be times of 2-byte and every line
* will append 2 status bytes
*/
static void
usb_GetPhyPixels( Plustek_Device *dev, ScanParam *sp )
{
sp->Size.dwValidPixels = sp->Size.dwPixels * sp->PhyDpi.x / sp->UserDpi.x;
if (sp->bBitDepth == 1) {
/* Pixels should be times of 16 */
sp->Size.dwPhyPixels =
(sp->Size.dwValidPixels + 15UL) & 0xfffffff0UL;
sp->Size.dwPhyBytes = sp->Size.dwPhyPixels / 8UL + 2UL;
} else if (sp->bBitDepth == 8) {
/* Pixels should be times of 2 */
sp->Size.dwPhyPixels = (sp->Size.dwValidPixels + 1UL) & 0xfffffffeUL;
sp->Size.dwPhyBytes = sp->Size.dwPhyPixels * sp->bChannels + 2UL;
/* need to be adjusted fo CIS devices in color mode */
if(usb_IsCISDevice( dev ) && (sp->bDataType == SCANDATATYPE_Color)) {
sp->Size.dwPhyBytes *= 3;
}
}
else /* sp->bBitDepth == 16 */
{
sp->Size.dwPhyPixels = sp->Size.dwValidPixels;
sp->Size.dwPhyBytes = sp->Size.dwPhyPixels * 2 * sp->bChannels + 2UL;
/* need to be adjusted fo CIS devices in color mode */
if(usb_IsCISDevice( dev ) && (sp->bDataType == SCANDATATYPE_Color)) {
sp->Size.dwPhyBytes *= 3;
}
}
}
/** Calculate basic image settings like the number of physical bytes per line
* etc...
* Affected registers:<br>
* 0x22/0x23 - Data Pixels Start<br>
* 0x24/0x25 - Data Pixels End<br>
* 0x4a/0x4b - Full Steps to Skip at Start of Scan
*
* @param dev - pointer to our device structure,
* it should contain all we need
* @param pParam - pointer to the current scan parameters
* @return - Nothing
*/
static void
usb_GetScanRect( Plustek_Device *dev, ScanParam *sp )
{
u_short wDataPixelStart, wLineEnd;
DCapsDef *sCaps = &dev->usbDev.Caps;
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
/* Convert pixels to physical dpi based */
usb_GetPhyPixels( dev, sp );
/* Compute data start pixel */
#if 0
/* HEINER: check ADF stuff... */
if(sp->bCalibration != PARAM_Gain &&
sp->bCalibration != PARAM_Offset && ScanInf.m_fADF)
wDataPixelStart = 2550 * sCaps->OpticDpi.x / 300UL -
(u_short)(m_dHDPIDivider * sp->Size.dwValidPixels + 0.5);
else
#endif
wDataPixelStart = (u_short)((u_long)sp->Origin.x *
sCaps->OpticDpi.x / 300UL);
/* during the calibration steps, we read the entire CCD data
*/
if((sp->bCalibration != PARAM_Gain)&&(sp->bCalibration != PARAM_Offset)) {
/* HEINER: check ADF stuff... */
#if 0
if(ScanInf.m_fADF) {
wDataPixelStart = 2550 * sCaps->OpticDpi.x / 300UL -
(u_short)(m_dHDPIDivider * sp->Size.dwValidPixels + 0.5);
}
#endif
wDataPixelStart += hw->wActivePixelsStart;
}
wLineEnd = wDataPixelStart + (u_short)(m_dHDPIDivider *
sp->Size.dwPhyPixels + 0.5);
DBG( _DBG_INFO2, "* DataPixelStart=%u, LineEnd=%u\n",
wDataPixelStart, wLineEnd );
if( wDataPixelStart & 1 ) {
wDataPixelStart++;
wLineEnd++;
DBG( _DBG_INFO2, "* DataPixelStart=%u, LineEnd=%u (ADJ)\n",
wDataPixelStart, wLineEnd );
}
regs[0x22] = _HIBYTE( wDataPixelStart );
regs[0x23] = _LOBYTE( wDataPixelStart );
/* should match: wLineEnd-wDataPixelStart%(m_dHDPIDivider*2) = 0!! */
regs[0x24] = _HIBYTE( wLineEnd );
regs[0x25] = _LOBYTE( wLineEnd );
DBG( _DBG_INFO2, ">> End-Start=%u, HDPI=%.2f\n",
wLineEnd-wDataPixelStart, m_dHDPIDivider);
/* Y origin */
if( sp->bCalibration == PARAM_Scan ) {
if( hw->motorModel == MODEL_Tokyo600 ) {
if(sp->PhyDpi.x <= 75)
sp->Origin.y += 20;
else if(sp->PhyDpi.x <= 100)
{
if (sp->bDataType == SCANDATATYPE_Color)
sp->Origin.y += 0;
else
sp->Origin.y -= 6;
}
else if(sp->PhyDpi.x <= 150)
{
if (sp->bDataType == SCANDATATYPE_Color)
sp->Origin.y -= 0;
}
else if(sp->PhyDpi.x <= 200)
{
if (sp->bDataType == SCANDATATYPE_Color)
sp->Origin.y -= 10;
else
sp->Origin.y -= 4;
}
else if(sp->PhyDpi.x <= 300)
{
if (sp->bDataType == SCANDATATYPE_Color)
sp->Origin.y += 16;
else
sp->Origin.y -= 18;
}
else if(sp->PhyDpi.x <= 400)
{
if (sp->bDataType == SCANDATATYPE_Color)
sp->Origin.y += 15;
else if(sp->bDataType == SCANDATATYPE_BW)
sp->Origin.y += 4;
}
else /* if(sp->PhyDpi.x <= 600) */
{
if (sp->bDataType == SCANDATATYPE_Gray)
sp->Origin.y += 4;
}
}
/* Add gray mode offset (Green offset, we assume the CCD are
* always be RGB or BGR order).
*/
if (sp->bDataType != SCANDATATYPE_Color)
sp->Origin.y += (u_long)(300UL *
sCaps->bSensorDistance / sCaps->OpticDpi.y);
}
sp->Origin.y=(u_short)((u_long)sp->Origin.y * hw->wMotorDpi/300UL);
/* Something wrong, but I can not find it. */
if( hw->motorModel == MODEL_HuaLien && sCaps->OpticDpi.x == 600)
sp->Origin.y = sp->Origin.y * 297 / 298;
DBG(_DBG_INFO2,"* Full Steps to Skip at Start = 0x%04x\n",
sp->Origin.y);
regs[0x4a] = _HIBYTE( sp->Origin.y );
regs[0x4b] = _LOBYTE( sp->Origin.y );
}
/** preset scan stepsize and fastfeed stepsize
*/
static void
usb_PresetStepSize( Plustek_Device *dev, ScanParam *pParam )
{
u_short ssize;
double mclkdiv = pParam->dMCLK;
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
ssize = (u_short)((double)CRYSTAL_FREQ / ( mclkdiv * 8.0 *
(double)m_bCM * hw->dMaxMotorSpeed * 4.0 * (double)hw->wMotorDpi));
regs[0x46] = _HIBYTE( ssize );
regs[0x47] = _LOBYTE( ssize );
regs[0x48] = _HIBYTE( ssize );
regs[0x49] = _LOBYTE( ssize );
DBG( _DBG_INFO2, "* StepSize(Preset) = %u (0x%04x)\n", ssize, ssize );
}
/** calculate default phase difference DPD
*/
static void
usb_GetDPD( Plustek_Device *dev )
{
int qtcnt; /* quarter speed count count reg 51 b2..3 */
int hfcnt; /* half speed count reg 51 b0..1 */
int strev; /* steps to reverse reg 50 */
int dpd; /* calculated dpd reg 52:53 */
int st; /* step size reg 46:47 */
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
qtcnt = (regs[0x51] & 0x30) >> 4; /* quarter speed count */
hfcnt = (regs[0x51] & 0xc0) >> 6; /* half speed count */
if( _LM9831 == hw->chip )
strev = regs[0x50] & 0x3f; /* steps to reverse */
else /* LM9832/3 */
{
if (qtcnt == 3)
qtcnt = 8;
if (hfcnt == 3)
hfcnt = 8;
strev = regs[0x50]; /* steps to reverse */
}
st = regs[0x46] * 256 + regs[0x47]; /* scan step size */
if (m_wLineLength == 0)
dpd = 0;
else
{
dpd = (((qtcnt * 4) + (hfcnt * 2) + strev) * 4 * st) %
(m_wLineLength * m_bLineRateColor);
DBG( _DBG_INFO2, "* DPD =%u (0x%04x)\n", dpd, dpd );
dpd = m_wLineLength * m_bLineRateColor - dpd;
}
DBG( _DBG_INFO2, "* DPD =%u (0x%04x), step size=%u, steps2rev=%u\n",
dpd, dpd, st, strev);
DBG( _DBG_INFO2, "* llen=%u, lineRateColor=%u, qtcnt=%u, hfcnt=%u\n",
m_wLineLength, m_bLineRateColor, qtcnt, hfcnt );
regs[0x51] |= (u_char)((dpd >> 16) & 0x03);
regs[0x52] = (u_char)(dpd >> 8);
regs[0x53] = (u_char)(dpd & 0xFF);
}
#define MCLKDIV_SCALING 2
#define _MIN(a,b) ((a) < (b) ? (a) : (b))
#define _MAX(a,b) ((a) > (b) ? (a) : (b))
/**
*/
static int
usb_GetMCLKDiv( Plustek_Device *dev )
{
int j, pixelbits, pixelsperline, r;
int minmclk, maxmclk, mclkdiv;
double hdpi, min_int_time;
u_char *regs = dev->usbDev.a_bRegs;
HWDef *hw = &dev->usbDev.HwSetting;
DBG( _DBG_INFO, "usb_GetMCLKDiv()\n" );
r = 8; /* line rate */
if ((regs[0x26] & 7) == 0)
r = 24; /* pixel rate */
/* use high or low res min integration time */
min_int_time = ((regs[0x9]&7) > 2 ? hw->dMinIntegrationTimeLowres:
hw->dMinIntegrationTimeHighres);
minmclk = (int)ceil((double) MCLKDIV_SCALING * CRYSTAL_FREQ *
min_int_time /((double)1000. * r * m_wLineLength));
minmclk = _MAX(minmclk,MCLKDIV_SCALING);
maxmclk = (int)(32.5*MCLKDIV_SCALING + .5);
DBG(_DBG_INFO2,"- lower mclkdiv limit=%f\n",(double)minmclk/MCLKDIV_SCALING);
DBG(_DBG_INFO2,"- upper mclkdiv limit=%f\n",(double)maxmclk/MCLKDIV_SCALING);
/* get the bits per pixel */
switch (regs[0x9] & 0x38) {
case 0: pixelbits=1; break;
case 0x8: pixelbits=2; break;
case 0x10: pixelbits=4; break;
case 0x18: pixelbits=8; break;
default: pixelbits=16;break;
}
/* compute the horizontal dpi (pixels per inch) */
j = regs[0x9] & 0x7;
hdpi = ((j&1)*.5+1)*(j&2?2:1)*(j&4?4:1);
pixelsperline = (int)((256*regs[0x24]+regs[0x25]-256*regs[0x22]-regs[0x23])
*pixelbits/(hdpi * 8));
mclkdiv = (int)ceil((double)MCLKDIV_SCALING * pixelsperline * CRYSTAL_FREQ
/((double) 8. * m_wLineLength * dev->transferRate));
DBG( _DBG_INFO2, "- hdpi = %.3f\n", hdpi );
DBG( _DBG_INFO2, "- pixelbits = %u\n", pixelbits );
DBG( _DBG_INFO2, "- pixelsperline = %u\n", pixelsperline );
DBG( _DBG_INFO2, "- linelen = %u\n", m_wLineLength );
DBG( _DBG_INFO2, "- transferrate = %lu\n", dev->transferRate );
DBG( _DBG_INFO2, "- MCLK Divider = %u\n", mclkdiv/MCLKDIV_SCALING );
mclkdiv = _MAX(mclkdiv,minmclk);
mclkdiv = _MIN(mclkdiv,maxmclk);
DBG( _DBG_INFO2, "- Current MCLK Divider = %u\n", mclkdiv/MCLKDIV_SCALING );
if( dev->transferRate == 2000000 ) {
while (mclkdiv * hdpi < 6.*MCLKDIV_SCALING) {
mclkdiv++;
}
DBG( _DBG_INFO2, "- HIGHSPEED MCLK Divider = %u\n",
mclkdiv/MCLKDIV_SCALING );
}
return mclkdiv;
}
/** Plusteks' poor-man MCLK calculation...
* at least we give the master clock divider and adjust the step size
* and integration time (for 14/16 bit modes)
*/
static double
usb_GetMCLKDivider( Plustek_Device *dev, ScanParam *pParam )
{
double dMaxIntegrationTime;
double dMaxMCLKDivider;
DCapsDef *sCaps = &dev->usbDev.Caps;
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
DBG( _DBG_INFO, "usb_GetMCLKDivider()\n" );
if( dev->transferRate == 2000000 ) {
int mclkdiv = usb_GetMCLKDiv(dev);
pParam->dMCLK = (double)mclkdiv/MCLKDIV_SCALING;
}
m_dMCLKDivider = pParam->dMCLK;
if (m_dHDPIDivider*m_dMCLKDivider >= 5.3/*6*/)
m_bIntTimeAdjust = 0;
else
m_bIntTimeAdjust = ceil( 5.3/*6.0*/ / (m_dHDPIDivider*m_dMCLKDivider));
if( pParam->bCalibration == PARAM_Scan ) {
usb_GetMCLKDiv(dev);
/* Compare Integration with USB speed to find the best ITA value */
if( pParam->bBitDepth > 8 ) {
while( pParam->Size.dwPhyBytes >
(m_dMCLKDivider * m_bCM * m_wLineLength / 6 * 9 / 10) *
(1 + m_bIntTimeAdjust)) {
m_bIntTimeAdjust++;
}
if( hw->motorModel == MODEL_HuaLien &&
sCaps->bCCD == kNEC3799 && m_bIntTimeAdjust > bMaxITA) {
m_bIntTimeAdjust = bMaxITA;
}
if(((hw->motorModel == MODEL_HP) && (sCaps->bCCD == kNECSLIM))/* ||
( regs[0x26] & _ONE_CH_COLOR )*/) {
bMaxITA = (u_char)floor((m_dMCLKDivider + 1) / 2.0);
DBG( _DBG_INFO2, "* MaxITA (HP) = %u\n", bMaxITA );
if( m_bIntTimeAdjust > bMaxITA ) {
DBG( _DBG_INFO, "* ITA (%u) limited\n", m_bIntTimeAdjust );
m_bIntTimeAdjust = bMaxITA;
}
}
}
}
DBG( _DBG_INFO2, "* Integration Time Adjust = %u (HDPI=%.3f,MCLKD=%.3f)\n",
m_bIntTimeAdjust, m_dHDPIDivider, m_dMCLKDivider );
regs[0x08] = (u_char)((m_dMCLKDivider - 1) * 2);
regs[0x19] = m_bIntTimeAdjust;
if( m_bIntTimeAdjust != 0 ) {
m_wStepSize = (u_short)((u_long) m_wStepSize *
(m_bIntTimeAdjust + 1) / m_bIntTimeAdjust);
if( m_wStepSize < 2 )
m_wStepSize = 2;
regs[0x46] = _HIBYTE(m_wStepSize);
regs[0x47] = _LOBYTE(m_wStepSize);
DBG( _DBG_INFO2, "* Stepsize = %u, 0x46=0x%02x 0x47=0x%02x\n",
m_wStepSize, regs[0x46], regs[0x47] );
usb_GetDPD( dev );
}
/* Compute maximum MCLK divider base on maximum integration time for
* high lamp PWM, use equation 4
*/
dMaxIntegrationTime = hw->dIntegrationTimeHighLamp;
dMaxMCLKDivider = (double)CRYSTAL_FREQ * dMaxIntegrationTime /
(1000 * 8 * m_bCM * m_wLineLength);
/* Determine lamp PWM setting */
if( m_dMCLKDivider > dMaxMCLKDivider ) {
DBG( _DBG_INFO2, "* Setting GreenPWMDutyCycleLow\n" );
regs[0x2a] = _HIBYTE( hw->wGreenPWMDutyCycleLow );
regs[0x2b] = _LOBYTE( hw->wGreenPWMDutyCycleLow );
} else {
DBG( _DBG_INFO2, "* Setting GreenPWMDutyCycleHigh\n" );
regs[0x2a] = _HIBYTE( hw->wGreenPWMDutyCycleHigh );
regs[0x2b] = _LOBYTE( hw->wGreenPWMDutyCycleHigh );
}
DBG( _DBG_INFO2, "* Current MCLK Divider = %f\n", m_dMCLKDivider );
return m_dMCLKDivider;
}
/** calculate the step size of each scan step
*/
static void
usb_GetStepSize( Plustek_Device *dev, ScanParam *pParam )
{
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
/* Compute step size using equation 1 */
if (m_bIntTimeAdjust != 0) {
m_wStepSize = (u_short)(((u_long) pParam->PhyDpi.y * m_wLineLength *
m_bLineRateColor * (m_bIntTimeAdjust + 1)) /
(4 * hw->wMotorDpi * m_bIntTimeAdjust));
} else {
m_wStepSize = (u_short)(((u_long) pParam->PhyDpi.y * m_wLineLength *
m_bLineRateColor) / (4 * hw->wMotorDpi));
}
if (m_wStepSize < 2)
m_wStepSize = 2;
m_wStepSize = m_wStepSize * 298 / 297;
regs[0x46] = _HIBYTE( m_wStepSize );
regs[0x47] = _LOBYTE( m_wStepSize );
DBG( _DBG_INFO2, "* Stepsize = %u, 0x46=0x%02x 0x47=0x%02x\n",
m_wStepSize, regs[0x46], regs[0x47] );
}
/**
*/
static void
usb_GetLineLength( Plustek_Device *dev, ScanParam *param )
{
/* [note]
* The ITA in this moment is always 0, it will be changed later when we
* calculate MCLK. This is very strange why this routine will not call
* again to get all new value after ITA was changed? If this routine
* never call again, maybe we remove all factor with ITA here.
*/
int tr;
int tpspd; /* turbo/preview mode speed reg 0a b2..3 */
int tpsel; /* turbo/preview mode select reg 0a b0..1 */
int gbnd; /* guardband duration reg 0e b4..7 */
int dur; /* pulse duration reg 0e b0..3 */
int ntr; /* number of tr pulses reg 0d b7 */
int afeop; /* scan mode, 0=pixel rate, 1=line rate, */
/* 4=1 channel mode a, 5=1 channel mode b, reg 26 b0..2 */
int ctmode; /* CIS tr timing mode reg 19 b0..1 */
int tp; /* tpspd or 1 if tpsel=0 */
int b; /* if ctmode=0, (ntr+1)*((2*gbnd)+dur+1), otherwise 1 */
int tradj; /* ITA */
int en_tradj;
u_short le;
HWDef *hw = &dev->usbDev.HwSetting;
ClkMotorDef *motor = usb_GetMotorSet( hw->motorModel );
u_char *regs = dev->usbDev.a_bRegs;
tpspd = (regs[0x0a] & 0x0c) >> 2; /* turbo/preview mode speed */
tpsel = regs[0x0a] & 3; /* turbo/preview mode select */
gbnd = (regs[0x0e] & 0xf0) >> 4; /* TR fi1 guardband duration */
dur = (regs[0x0e] & 0xf); /* TR pulse duration */
ntr = regs[0x0d] / 128; /* number of tr pulses */
afeop = regs[0x26] & 7; /* afe op - 3 channel or 1 channel */
tradj = regs[0x19] & 0x7f; /* integration time adjust */
en_tradj = (tradj) ? 1 : 0;
ctmode = (regs[0x0b] >> 3) & 3; /* cis tr timing mode */
m_bLineRateColor = 1;
if (afeop == 1 || afeop == 5) /* if 3 channel line or 1 channel mode b */
m_bLineRateColor = 3;
/* according to turbo/preview mode to set value */
if( tpsel == 0 ) {
tp = 1;
} else {
tp = tpspd + 2;
if( tp == 5 )
tp++;
}
b = 1;
if( ctmode == 0 ) { /* CCD mode scanner*/
b = (ntr + 1) * ((2 * gbnd) + dur + 1);
b += (1 - ntr) * en_tradj;
}
if( ctmode == 2 ) /* CIS mode scanner */
b = 3;
/* it might be necessary to tweak this value, to keep the
* MCLK constant - see some sheet-fed devices
*/
le = hw->wLineEnd;
if (motor->dpi_thresh != 0) {
if( param->PhyDpi.y <= motor->dpi_thresh) {
le = motor->lineend;
DBG( _DBG_INFO2, "* Adjusting lineend: %u\n", le);
}
regs[0x20] = _HIBYTE( le );
regs[0x21] = _LOBYTE( le );
}
tr = m_bLineRateColor * (le + tp * (b + 3 - ntr));
if( tradj == 0 ) {
if( ctmode == 0 )
tr += m_bLineRateColor;
} else {
int le_phi, num_byteclk, num_mclkf, tr_fast_pix, extra_pix;
/* Line color or gray mode */
if( afeop != 0 ) {
le_phi = (tradj + 1) / 2 + 1 + 6;
num_byteclk = ((le_phi + 8 * le + 8 * b + 4) /
(8 * tradj)) + 1;
num_mclkf = 8 * tradj * num_byteclk;
tr_fast_pix = num_byteclk;
extra_pix = (num_mclkf - le_phi) % 8;
}
else /* 3 channel pixel rate color */
{
le_phi = (tradj + 1) / 2 + 1 + 10 + 12;
num_byteclk = ((le_phi + 3 * 8 * le + 3 * 8 * b + 3 * 4) /
(3 * 8 * tradj)) + 1;
num_mclkf = 3 * 8 * tradj * num_byteclk;
tr_fast_pix = num_byteclk;
extra_pix = (num_mclkf - le_phi) % (3 * 8);
}
tr = b + le + 4 + tr_fast_pix;
if (extra_pix == 0)
tr++;
tr *= m_bLineRateColor;
}
m_wLineLength = tr / m_bLineRateColor;
DBG( _DBG_INFO2, "* LineLength=%d, LineRateColor=%u\n",
m_wLineLength, m_bLineRateColor );
}
/** usb_GetMotorParam
* registers 0x56, 0x57
*/
static void
usb_GetMotorParam( Plustek_Device *dev, ScanParam *pParam )
{
int idx, i;
ClkMotorDef *clk;
MDef *md;
DCapsDef *sCaps = &dev->usbDev.Caps;
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
if (!_IS_PLUSTEKMOTOR(hw->motorModel)) {
clk = usb_GetMotorSet( hw->motorModel );
md = clk->motor_sets;
idx = 0;
for( i = 0; i < _MAX_CLK; i++ ) {
if( pParam->PhyDpi.y <= dpi_ranges[i] )
break;
idx++;
}
if( idx >= _MAX_CLK )
idx = _MAX_CLK - 1;
regs[0x56] = md[idx].pwm;
regs[0x57] = md[idx].pwm_duty;
regs[0x43] = 0;
regs[0x44] = 0;
if( md[idx].scan_lines_per_line > 1 ) {
if((pParam->bBitDepth > 8) &&
(pParam->bDataType == SCANDATATYPE_Color)) {
regs[0x43] = 0xff;
regs[0x44] = md[idx].scan_lines_per_line;
DBG( _DBG_INFO2, "* Line Skipping : 0x43=0x%02x, 0x44=0x%02x\n",
regs[0x43], regs[0x44] );
}
}
} else {
if( sCaps->OpticDpi.x == 1200 ) {
switch( hw->motorModel ) {
case MODEL_HuaLien:
case MODEL_KaoHsiung:
default:
if(pParam->PhyDpi.x <= 200)
{
regs[0x56] = 1;
regs[0x57] = 48; /* 63; */
}
else if(pParam->PhyDpi.x <= 300)
{
regs[0x56] = 2; /* 8; */
regs[0x57] = 48; /* 56; */
}
else if(pParam->PhyDpi.x <= 400)
{
regs[0x56] = 8;
regs[0x57] = 48;
}
else if(pParam->PhyDpi.x <= 600)
{
regs[0x56] = 2; /* 10; */
regs[0x57] = 48; /* 56; */
}
else /* pParam->PhyDpi.x == 1200) */
{
regs[0x56] = 1; /* 8; */
regs[0x57] = 48; /* 56; */
}
break;
}
} else {
switch ( hw->motorModel ) {
case MODEL_Tokyo600:
regs[0x56] = 16;
regs[0x57] = 4; /* 2; */
break;
case MODEL_HuaLien:
{
if(pParam->PhyDpi.x <= 200)
{
regs[0x56] = 64; /* 24; */
regs[0x57] = 4; /* 16; */
}
else if(pParam->PhyDpi.x <= 300)
{
regs[0x56] = 64; /* 16; */
regs[0x57] = 4; /* 16; */
}
else if(pParam->PhyDpi.x <= 400)
{
regs[0x56] = 64; /* 16; */
regs[0x57] = 4; /* 16; */
}
else /* if(pParam->PhyDpi.x <= 600) */
{
/* HEINER: check ADF stuff... */
#if 0
if(ScanInf.m_fADF)
{
regs[0x56] = 8;
regs[0x57] = 48;
}
else
#endif
{
regs[0x56] = 64; /* 2; */
regs[0x57] = 4; /* 48; */
}
}
}
break;
case MODEL_KaoHsiung:
default:
if(pParam->PhyDpi.x <= 200)
{
regs[0x56] = 24;
regs[0x57] = 16;
}
else if(pParam->PhyDpi.x <= 300)
{
regs[0x56] = 16;
regs[0x57] = 16;
}
else if(pParam->PhyDpi.x <= 400)
{
regs[0x56] = 16;
regs[0x57] = 16;
}
else /* if(pParam->PhyDpi.x <= 600) */
{
regs[0x56] = 2;
regs[0x57] = 48;
}
break;
}
}
}
DBG( _DBG_INFO2, "* MOTOR-Settings: PWM=0x%02x, PWM_DUTY=0x%02x\n",
regs[0x56], regs[0x57] );
}
/**
*/
static void
usb_GetPauseLimit( Plustek_Device *dev, ScanParam *pParam )
{
int coeffsize, scaler;
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
scaler = 1;
if( hw->bReg_0x26 & _ONE_CH_COLOR ) {
if( pParam->bDataType == SCANDATATYPE_Color ) {
scaler = 3;
}
}
/* compute size of coefficient ram */
coeffsize = 4 + 16 + 16; /* gamma and shading and offset */
/* if 16 bit, then not all is used */
if( regs[0x09] & 0x20 ) {
coeffsize = 16 + 16; /* no gamma */
}
coeffsize *= (2*3); /* 3 colors and 2 bytes/word */
/* Get available buffer size in KB
* for 512kb this will be 296
* for 2Mb this will be 1832
*/
m_dwPauseLimit = (u_long)(hw->wDRAMSize - (u_long)(coeffsize));
m_dwPauseLimit -= ((pParam->Size.dwPhyBytes*scaler) / 1024 + 1);
/* If not reversing, take into account the steps to reverse */
if( regs[0x50] == 0 )
m_dwPauseLimit -= ((regs[0x54] & 7) *
(pParam->Size.dwPhyBytes * scaler) + 1023) / 1024;
DBG( _DBG_INFO2, "* PL=%lu, coeffsize=%u, scaler=%u\n",
m_dwPauseLimit, coeffsize, scaler );
m_dwPauseLimit = usb_max( usb_min(m_dwPauseLimit,
(u_long)ceil(pParam->Size.dwTotalBytes / 1024.0)), 2);
regs[0x4e] = (u_char)floor((m_dwPauseLimit*512.0)/(2.0*hw->wDRAMSize));
if( regs[0x4e] > 1 ) {
regs[0x4e]--;
if(regs[0x4e] > 1)
regs[0x4e]--;
} else
regs[0x4e] = 1;
/* resume, when buffer is 2/8 kbytes full (512k/2M memory)
*/
regs[0x4f] = 1;
DBG( _DBG_INFO2, "* PauseLimit = %lu, [0x4e] = 0x%02x, [0x4f] = 0x%02x\n",
m_dwPauseLimit, regs[0x4e], regs[0x4f] );
}
/** usb_GetScanLinesAndSize
*/
static void
usb_GetScanLinesAndSize( Plustek_Device *dev, ScanParam *pParam )
{
DCapsDef *sCaps = &dev->usbDev.Caps;
pParam->Size.dwPhyLines = (u_long)ceil((double) pParam->Size.dwLines *
pParam->PhyDpi.y / pParam->UserDpi.y);
/* Calculate color offset */
if (pParam->bCalibration == PARAM_Scan && pParam->bChannels == 3) {
dev->scanning.bLineDistance = sCaps->bSensorDistance *
pParam->PhyDpi.y / sCaps->OpticDpi.x;
pParam->Size.dwPhyLines += (dev->scanning.bLineDistance << 1);
}
else
dev->scanning.bLineDistance = 0;
pParam->Size.dwTotalBytes = pParam->Size.dwPhyBytes * pParam->Size.dwPhyLines;
DBG( _DBG_INFO, "* PhyBytes = %lu\n", pParam->Size.dwPhyBytes );
DBG( _DBG_INFO, "* PhyLines = %lu\n", pParam->Size.dwPhyLines );
DBG( _DBG_INFO, "* TotalBytes = %lu\n", pParam->Size.dwTotalBytes );
}
/** function to preset/reset the merlin registers
*/
static SANE_Bool
usb_SetScanParameters( Plustek_Device *dev, ScanParam *pParam )
{
static u_char reg8, reg38[6], reg48[2];
ScanDef *scan = &dev->scanning;
ScanParam *pdParam = &dev->scanning.sParam;
HWDef *hw = &dev->usbDev.HwSetting;
u_char *regs = dev->usbDev.a_bRegs;
m_pParam = pParam;
DBG( _DBG_INFO, "usb_SetScanParameters()\n" );
if( !usb_IsScannerReady(dev))
return SANE_FALSE;
if(pParam->bCalibration == PARAM_Scan && pParam->bSource == SOURCE_ADF) {
/* HEINER: dSaveMoveSpeed is only used in func EjectPaper!!!
dSaveMoveSpeed = hw->dMaxMoveSpeed;
*/
hw->dMaxMoveSpeed = 1.0;
usb_MotorSelect( dev, SANE_TRUE );
usb_MotorOn( dev, SANE_TRUE );
}
/*
* calculate the basic settings...
*/
pParam->PhyDpi.x = usb_SetAsicDpiX( dev, pParam->UserDpi.x );
pParam->PhyDpi.y = usb_SetAsicDpiY( dev, pParam->UserDpi.y );
usb_SetColorAndBits( dev, pParam );
usb_GetScanRect ( dev, pParam );
usb_PresetStepSize( dev, pParam );
if( dev->caps.dwFlag & SFLAG_ADF ) {
if( pParam->bCalibration == PARAM_Scan ) {
if( pdParam->bSource == SOURCE_ADF ) {
regs[0x50] = 0;
regs[0x51] = 0x40;
if( pParam->PhyDpi.x <= 300)
regs[0x54] = (regs[0x54] & ~7) | 4; /* 3; */
else
regs[0x54] = (regs[0x54] & ~7) | 5; /* 4; */
} else {
regs[0x50] = hw->bStepsToReverse;
regs[0x51] = hw->bReg_0x51;
regs[0x54] &= ~7;
}
} else
regs[0x50] = 0;
} else {
if( pParam->bCalibration == PARAM_Scan )
regs[0x50] = hw->bStepsToReverse;
else
regs[0x50] = 0;
}
/* Assume we will not use ITA */
regs[0x19] = m_bIntTimeAdjust = 0;
/* Get variables from calculation algorithms */
if(!(pParam->bCalibration == PARAM_Scan &&
pParam->bSource == SOURCE_ADF && dev->usbDev.fLastScanIsAdf )) {
DBG( _DBG_INFO2, "* Scan calculations...\n" );
usb_GetLineLength ( dev, pParam );
usb_GetStepSize ( dev, pParam );
usb_GetDPD ( dev );
usb_GetMCLKDivider( dev, pParam );
usb_GetMotorParam ( dev, pParam );
}
/* Compute fast feed step size, use equation 3 and equation 8 */
if( m_dMCLKDivider < 1.0)
m_dMCLKDivider = 1.0;
m_wFastFeedStepSize = (u_short)(CRYSTAL_FREQ /
(m_dMCLKDivider * 8 * m_bCM * hw->dMaxMoveSpeed *
4 * hw->wMotorDpi));
/* CIS special ;-) */
if((hw->bReg_0x26 & _ONE_CH_COLOR) && (m_bCM == 1)) {
DBG( _DBG_INFO2, "* CIS FFStep-Speedup\n" );
m_wFastFeedStepSize /= 3;
}
if( m_bIntTimeAdjust != 0 )
m_wFastFeedStepSize /= m_bIntTimeAdjust;
if(regs[0x0a])
m_wFastFeedStepSize *= ((regs[0x0a] >> 2) + 2);
regs[0x48] = _HIBYTE( m_wFastFeedStepSize );
regs[0x49] = _LOBYTE( m_wFastFeedStepSize );
DBG( _DBG_INFO2, "* FFStepSize = %u, [0x48] = 0x%02x, [0x49] = 0x%02x\n",
m_wFastFeedStepSize, regs[0x48], regs[0x49] );
/* Compute the number of lines to scan using actual Y resolution */
usb_GetScanLinesAndSize( dev, pParam );
/* Pause limit should be bounded by total bytes to read
* so that the chassis will not move too far.
*/
usb_GetPauseLimit( dev, pParam );
/* For ADF .... */
if(pParam->bCalibration == PARAM_Scan && pParam->bSource == SOURCE_ADF) {
if( dev->usbDev.fLastScanIsAdf ) {
regs[0x08] = reg8;
memcpy( ®s[0x38], reg38, sizeof(reg38));
memcpy( ®s[0x48], reg48, sizeof(reg48));
} else {
reg8 = regs[0x08];
memcpy( reg38, ®s[0x38], sizeof(reg38));
memcpy( reg48, ®s[0x48], sizeof(reg48));
}
usb_MotorSelect( dev, SANE_TRUE );
}
/* Reset LM983x's state machine before setting register values */
if( !usbio_WriteReg( dev->fd, 0x18, 0x18 ))
return SANE_FALSE;
usleep(200 * 1000); /* Need to delay at least xxx microseconds */
if( !usbio_WriteReg( dev->fd, 0x07, 0x20 ))
return SANE_FALSE;
if( !usbio_WriteReg( dev->fd, 0x19, 6 ))
return SANE_FALSE;
regs[0x07] = 0;
regs[0x28] = 0;
/* set unused registers to 0 */
memset( ®s[0x03], 0, 3 );
memset( ®s[0x5f], 0, 0x7f-0x5f+1 );
if( !usb_IsSheetFedDevice(dev)) {
/* we limit the possible scansteps to avoid, that the sensors bumps
* against the scanbed - not necessary for sheet-fed devices
*/
if(pParam->bCalibration==PARAM_Scan && pParam->bSource!=SOURCE_ADF) {
u_long lines = pParam->Size.dwPhyLines + scan->bLinesToSkip +
scan->dwLinesDiscard + 5;
u_short scansteps = (u_short)ceil((double)lines*
hw->wMotorDpi / pParam->PhyDpi.y);
DBG( _DBG_INFO, "* Scansteps=%u (%lu*%u/%u)\n", scansteps, lines,
hw->wMotorDpi, pParam->PhyDpi.y );
regs[0x4c] = _HIBYTE(scansteps);
regs[0x4d] = _LOBYTE(scansteps);
}
}
/* set the merlin registers */
_UIO(sanei_lm983x_write( dev->fd, 0x03, ®s[0x03], 3, SANE_TRUE));
_UIO(sanei_lm983x_write( dev->fd, 0x08, ®s[0x08], 0x7f - 0x08+1, SANE_TRUE));
usleep(100);
if( !usbio_WriteReg( dev->fd, 0x07, 0 ))
return SANE_FALSE;
DBG( _DBG_INFO, "usb_SetScanParameters() done.\n" );
return SANE_TRUE;
}
/**
*/
static SANE_Bool
usb_ScanBegin( Plustek_Device *dev, SANE_Bool auto_park )
{
u_char value;
u_short inches;
HWDef *hw = &dev->usbDev.HwSetting;
DCapsDef *sc = &dev->usbDev.Caps;
u_char *regs = dev->usbDev.a_bRegs;
DBG( _DBG_INFO, "usb_ScanBegin()\n" );
if( !usb_Wait4ScanSample( dev ))
return SANE_FALSE;
/* save the request for usb_ScanEnd () */
m_fAutoPark = auto_park;
/* Disable home sensor during scan, or the chassis cannot move */
value = ((m_pParam->bCalibration == PARAM_Scan &&
m_pParam->bSource == SOURCE_ADF)? (regs[0x58] & ~7): 0);
if(!usbio_WriteReg( dev->fd, 0x58, value ))
return SANE_FALSE;
/* Check if scanner is ready for receiving command */
if( !usb_IsScannerReady(dev))
return SANE_FALSE;
/* Flush cache - only LM9831 (Source: National Semiconductors) */
if( _LM9831 == hw->chip ) {
for(;;) {
if( SANE_TRUE == cancelRead ) {
DBG( _DBG_INFO, "ScanBegin() - Cancel detected...\n" );
return SANE_FALSE;
}
_UIO(usbio_ReadReg( dev->fd, 0x01, &m_bOldScanData ));
if( m_bOldScanData ) {
u_long dwBytesToRead = m_bOldScanData * hw->wDRAMSize * 4;
u_char *pBuffer = malloc( sizeof(u_char) * dwBytesToRead );
DBG(_DBG_INFO,"Flushing cache - %lu bytes (bOldScanData=%u)\n",
dwBytesToRead, m_bOldScanData );
_UIO(sanei_lm983x_read( dev->fd, 0x00, pBuffer,
dwBytesToRead, SANE_FALSE ));
free( pBuffer );
} else
break;
}
}
/* Download map & Shading data */
if(( m_pParam->bCalibration == PARAM_Scan && !usb_MapDownload( dev )) ||
!usb_DownloadShadingData( dev, m_pParam->bCalibration )) {
return SANE_FALSE;
}
/* Move chassis and start to read image data */
if (!usbio_WriteReg( dev->fd, 0x07, 3 ))
return SANE_FALSE;
usbio_ReadReg( dev->fd, 0x01, &m_bOldScanData );
m_bOldScanData = 0; /* No data at all */
m_fStart = m_fFirst = SANE_TRUE; /* Prepare to read */
DBG( _DBG_DREGS, "Register Dump before reading data:\n" );
dumpregs( dev->fd, NULL );
inches = (u_short)((m_pParam->Origin.y *300UL)/hw->wMotorDpi);
DBG( _DBG_INFO2, ">>> INCH=%u, DOY=%u\n", inches, sc->Normal.DataOrigin.y );
if( inches > sc->Normal.DataOrigin.y )
usb_WaitPos( dev, 150, SANE_FALSE );
DBG( _DBG_INFO, "usb_ScanBegin() done.\n" );
return SANE_TRUE;
}
/** usb_ScanEnd
* stop all the processing stuff and reposition sensor back home
*/
static SANE_Bool
usb_ScanEnd( Plustek_Device *dev )
{
u_char value;
DBG( _DBG_INFO, "usbDev_ScanEnd(), start=%u, park=%u\n",
m_fStart, m_fAutoPark );
usbio_ReadReg( dev->fd, 0x07, &value );
if( value == 3 || value != 2 )
usbio_WriteReg( dev->fd, 0x07, 0 );
if( m_fStart ) {
m_fStart = SANE_FALSE;
if( m_fAutoPark )
usb_ModuleToHome( dev, SANE_FALSE );
}
else if( SANE_TRUE == cancelRead ) {
usb_ModuleToHome( dev, SANE_FALSE );
}
return SANE_TRUE;
}
/**
*/
static SANE_Bool
usb_IsDataAvailableInDRAM( Plustek_Device *dev )
{
/* Compute polling timeout
* Height (Inches) / MaxScanSpeed (Inches/Second) = Seconds to move the
* module from top to bottom. Then convert the seconds to miliseconds
* by multiply 1000. We add extra 2 seconds to get some tolerance.
*/
u_char a_bBand[3];
long dwTicks;
struct timeval t;
u_char *regs = dev->usbDev.a_bRegs;
DBG( _DBG_INFO, "usb_IsDataAvailableInDRAM()\n" );
gettimeofday( &t, NULL);
dwTicks = t.tv_sec + 30;
for(;;) {
_UIO( sanei_lm983x_read( dev->fd, 0x01, a_bBand, 3, SANE_FALSE ));
gettimeofday( &t, NULL);
if( t.tv_sec > dwTicks )
break;
if( usb_IsEscPressed()) {
DBG(_DBG_INFO,"usb_IsDataAvailableInDRAM() - Cancel detected...\n");
return SANE_FALSE;
}
/* It is not stable for read */
if((a_bBand[0] != a_bBand[1]) && (a_bBand[1] != a_bBand[2]))
continue;
if( a_bBand[0] > m_bOldScanData ) {
if( m_pParam->bSource != SOURCE_Reflection )
usleep(1000*(30 * regs[0x08] * dev->usbDev.Caps.OpticDpi.x / 600));
else
usleep(1000*(20 * regs[0x08] * dev->usbDev.Caps.OpticDpi.x / 600));
DBG( _DBG_INFO, "Data is available\n" );
return SANE_TRUE;
}
}
DBG( _DBG_INFO, "NO Data available\n" );
return SANE_FALSE;
}
/**
*/
static SANE_Bool
usb_ScanReadImage( Plustek_Device *dev, void *pBuf, u_long dwSize )
{
u_char *regs = dev->usbDev.a_bRegs;
SANE_Status res;
DBG( _DBG_READ, "usb_ScanReadImage(%lu)\n", dwSize );
if( m_fFirst ) {
m_fFirst = SANE_FALSE;
/* Wait for data band ready */
if (!usb_IsDataAvailableInDRAM( dev )) {
DBG( _DBG_ERROR, "Nothing to read...\n" );
return SANE_FALSE;
}
/* restore the fast forward stepsize...*/
sanei_lm983x_write(dev->fd, 0x48, ®s[0x48], 2, SANE_TRUE);
}
res = sanei_lm983x_read(dev->fd, 0x00, (u_char *)pBuf, dwSize, SANE_FALSE);
/* check for pressed ESC button, as sanei_lm983x_read() may take some time
*/
if( usb_IsEscPressed()) {
DBG(_DBG_INFO,"usb_ScanReadImage() - Cancel detected...\n");
return SANE_FALSE;
}
DBG( _DBG_READ, "usb_ScanReadImage() done, result: %d\n", res );
if( SANE_STATUS_GOOD == res ) {
return SANE_TRUE;
}
DBG( _DBG_ERROR, "usb_ScanReadImage() failed\n" );
return SANE_FALSE;
}
/** calculate the number of pixels per line and lines out of a given
* crop-area. The size of the area is given on a 300dpi base!
*/
static void
usb_GetImageInfo( Plustek_Device *dev, ImgDef *pInfo, WinInfo *pSize )
{
DBG( _DBG_INFO, "usb_GetImageInfo()\n" );
pSize->dwPixels = (u_long)pInfo->crArea.cx * pInfo->xyDpi.x / 300UL;
pSize->dwLines = (u_long)pInfo->crArea.cy * pInfo->xyDpi.y / 300UL;
DBG( _DBG_INFO2,"Area: cx=%u, cy=%u\n",pInfo->crArea.cx,pInfo->crArea.cy);
switch( pInfo->wDataType ) {
case COLOR_TRUE48:
pSize->dwBytes = pSize->dwPixels * 6UL;
break;
case COLOR_TRUE24:
if( dev->scanning.fGrayFromColor > 7 ){
pSize->dwBytes = (pSize->dwPixels + 7UL) >> 3;
pSize->dwPixels = pSize->dwBytes * 8;
} else {
pSize->dwBytes = pSize->dwPixels * 3UL;
}
break;
case COLOR_GRAY16:
pSize->dwBytes = pSize->dwPixels << 1;
break;
case COLOR_256GRAY:
pSize->dwBytes = pSize->dwPixels;
break;
default:
pSize->dwBytes = (pSize->dwPixels + 7UL) >> 3;
pSize->dwPixels = pSize->dwBytes * 8;
break;
}
}
/**
*/
static void
usb_SaveImageInfo( Plustek_Device *dev, ImgDef *pInfo )
{
HWDef *hw = &dev->usbDev.HwSetting;
ScanParam *pParam = &dev->scanning.sParam;
DBG( _DBG_INFO, "usb_SaveImageInfo()\n" );
/* Dpi & Origins */
pParam->UserDpi = pInfo->xyDpi;
pParam->Origin.x = pInfo->crArea.x;
pParam->Origin.y = pInfo->crArea.y;
/* Source & Bits */
pParam->bBitDepth = 8;
switch( pInfo->wDataType ) {
case COLOR_TRUE48:
pParam->bBitDepth = 16;
/* fall through... */
case COLOR_TRUE24:
pParam->bDataType = SCANDATATYPE_Color;
/* AFE operation: one or 3 channels ! */
if( hw->bReg_0x26 & _ONE_CH_COLOR )
pParam->bChannels = 1;
else
pParam->bChannels = 3;
break;
case COLOR_GRAY16:
pParam->bBitDepth = 16;
/* fall through... */
case COLOR_256GRAY:
pParam->bDataType = SCANDATATYPE_Gray;
pParam->bChannels = 1;
break;
default:
pParam->bBitDepth = 1;
pParam->bDataType = SCANDATATYPE_BW;
pParam->bChannels = 1;
}
DBG( _DBG_INFO, "* dwFlag = 0x%08lx\n", pInfo->dwFlag );
if( pInfo->dwFlag & SCANDEF_Transparency )
pParam->bSource = SOURCE_Transparency;
else if( pInfo->dwFlag & SCANDEF_Negative )
pParam->bSource = SOURCE_Negative;
else if( pInfo->dwFlag & SCANDEF_Adf )
pParam->bSource = SOURCE_ADF;
else
pParam->bSource = SOURCE_Reflection;
/* it seems, that we need to adjust the Origin.x when we have a
* sheetfed device to avoid stripes in the resulting pictures
*/
if( usb_IsSheetFedDevice(dev)) {
int step, div, org, xdpi;
xdpi = usb_SetAsicDpiX( dev, pParam->UserDpi.x );
if ((xdpi * 2) <= 300)
div = 300;
else if ((xdpi * 2) <= 600)
div = 600;
else if ((xdpi * 2) <= 1200)
div = 1200;
else
div = 2400;
step = div / xdpi;
org = pParam->Origin.x;
pParam->Origin.x = (pParam->Origin.x / step) * step;
if (org != pParam->Origin.x)
DBG(_DBG_INFO, "* Origin.x adjusted: %i -> %i\n",
org, pParam->Origin.x);
}
}
/* END PLUSTEK-USBSCAN.C ....................................................*/
|