summaryrefslogtreecommitdiff
path: root/backend/p5_device.c
blob: a9026f051e60c6d4b7e2e142347e4ee28483f75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
/**
 * Description of the Primax PagePartner model
 */
static P5_Model pagepartner_model = {
  "Primax PagePartner",
  "Primax",
  "PagePartner",
  SANE_I18N ("sheetfed scanner"),

  {300, 200, 150, 100, 0},
  /* 500 seems also possible */
  {600, 400, 300, 200, 150, 100, 0},

  300,
  600,
  100,
  100,
  16,

  SANE_FIX (0.0),
  SANE_FIX (0.0),
  SANE_FIX (215.9),
  SANE_FIX (300.0),
};

#ifdef HAVE_LINUX_PPDEV_H
static char *
addr_name (uint16_t addr)
{
  switch (addr)
    {
    case DATA:
      return "DATA";
      break;
    case STATUS:
      return "STATUS";
      break;
    case CONTROL:
      return "CONTROL";
      break;
    case EPPADR:
      return "EPPADR";
      break;
    case EPPDATA:
      return "EPPDATA";
      break;
    default:
      return "*ERROR*";
    }
}
#endif

/** @brief low level hardware access functions
 * @{
 */

static uint8_t
inb (int fd, uint16_t addr)
{
#ifdef HAVE_LINUX_PPDEV_H
  uint8_t val = 0xff;
  int rc, mode = 0xff;

  switch (addr)
    {
    case DATA:
      rc = ioctl (fd, PPRDATA, &val);
      break;
    case STATUS:
      rc = ioctl (fd, PPRSTATUS, &val);
      break;
    case CONTROL:
      rc = ioctl (fd, PPRCONTROL, &val);
      break;
    case EPPDATA:
      mode = 1;			/* data_reverse */
      rc = ioctl (fd, PPDATADIR, &mode);
      mode = IEEE1284_MODE_EPP | IEEE1284_DATA;
      rc = ioctl (fd, PPSETMODE, &mode);
#ifdef PPSETFLAGS
      mode = PP_FASTREAD;
      rc = ioctl (fd, PPSETFLAGS, &mode);
#endif
      rc = read (fd, &val, 1);
      break;
    default:
      DBG (DBG_error, "inb(%s) escaped ppdev\n", addr_name (addr));
      return 0xFF;
    }
  if (rc < 0)
    {
      DBG (DBG_error, "ppdev ioctl returned <%s>\n", strerror (errno));
    }
  return val;
#else
  if(fd && addr)
    return 0;
  return 0;
#endif
}

static void
outb (int fd, uint16_t addr, uint8_t value)
{
#ifdef HAVE_LINUX_PPDEV_H
  int rc = 0, mode = 0xff;

  switch (addr)
    {
    case DATA:
      rc = ioctl (fd, PPWDATA, &value);
      break;
    case CONTROL:
      mode = value & 0x20;
      rc = ioctl (fd, PPDATADIR, &mode);
      if (!rc)
	{
	  value = value & 0xDF;
	  rc = ioctl (fd, PPWCONTROL, &value);
	}
      break;
    case EPPDATA:
      mode = 0;			/* data forward */
      rc = ioctl (fd, PPDATADIR, &mode);
      mode = IEEE1284_MODE_EPP | IEEE1284_DATA;
      rc = ioctl (fd, PPSETMODE, &mode);
      rc = write (fd, &value, 1);
      break;
    case EPPADR:
      mode = 0;			/* data forward */
      rc = ioctl (fd, PPDATADIR, &mode);
      mode = IEEE1284_MODE_EPP | IEEE1284_ADDR;
      rc = ioctl (fd, PPSETMODE, &mode);
      rc = write (fd, &value, 1);
      break;
    default:
      DBG (DBG_error, "outb(%s,0x%02x) escaped ppdev\n", addr_name (addr),
	   value);
      break;
    }
  if (rc < 0)
    {
      DBG (DBG_error, "ppdev ioctl returned <%s>\n", strerror (errno));
    }
#else
  if(fd && addr && value)
    return;
#endif /* HAVE_LINUX_PPDEV_H */
}

static void
write_reg (int fd, uint8_t index, uint8_t value)
{
  uint8_t idx;

  /* both nibbles hold the same value */
  idx = index & 0x0F;
  DBG (DBG_io2, "write_reg(REG%X,0x%x)\n", idx, value);
  idx = idx << 4 | idx;
  outb (fd, EPPADR, idx);
  outb (fd, EPPDATA, value);
}

static uint8_t
read_reg (int fd, uint8_t index)
{
  uint8_t idx;

  /* both nibbles hold the same value */
  idx = index & 0x0F;
  idx = idx << 4 | idx;
  outb (fd, EPPADR, idx);
  return inb (fd, EPPDATA);
}

#ifdef HAVE_LINUX_PPDEV_H
static int
read_data (int fd, uint8_t * data, int length)
{
  int mode, rc, nb;
  unsigned char bval;

  bval = REG8;
  mode = IEEE1284_MODE_EPP | IEEE1284_ADDR;
  rc = ioctl (fd, PPSETMODE, &mode);
  rc = write (fd, &bval, 1);

  mode = 1;			/* data_reverse */
  rc = ioctl (fd, PPDATADIR, &mode);
#ifdef PPSETFLAGS
  mode = PP_FASTREAD;
  rc = ioctl (fd, PPSETFLAGS, &mode);
#endif
  mode = IEEE1284_MODE_EPP | IEEE1284_DATA;
  rc = ioctl (fd, PPSETMODE, &mode);
  nb = 0;
  while (nb < length)
    {
      rc = read (fd, data + nb, length - nb);
      if (rc < 0)
	{
	  DBG (DBG_error, "memtest: error reading data back!\n");
	  return 0;
	}
      else
	{
	  nb += rc;
	}
    }

  return 1;
}

static void
index_write_data (int fd, uint8_t index, uint8_t * data, int length)
{
  int mode;
  unsigned char bval;

  bval = index;
  mode = IEEE1284_MODE_EPP | IEEE1284_ADDR;
  ioctl (fd, PPSETMODE, &mode);
  write (fd, &bval, 1);

  mode = IEEE1284_MODE_EPP | IEEE1284_DATA;
  ioctl (fd, PPSETMODE, &mode);
  mode = 0;			/* data forward */
  ioctl (fd, PPDATADIR, &mode);
  write (fd, data, length);
  return;
}

static void
write_data (int fd, uint8_t * data, int length)
{
  index_write_data (fd, REG8, data, length);
}

static void
write_reg2 (int fd, uint8_t index, uint16_t value)
{
  uint8_t data2[2];

  data2[0] = value & 0xff;
  data2[1] = value >> 8;
  index_write_data (fd, index, data2, 2);
}
#else

static int
read_data (int fd, uint8_t * data, int length)
{
  if(fd && data && length)
    return -1;
  return -1;
}

static void
write_data (int fd, uint8_t * data, int length)
{
  if(fd && data && length)
    return;
}

static void
write_reg2 (int fd, uint8_t index, uint16_t value)
{
  if(fd && index && value)
    return;
}
#endif

/**
 * @}
 */


/** @brief This function checks a memory buffer.
 * This function writes at the given memory address then read it back
 * to check the scanner is correctly working.
 * @param fd file descriptor used to access hardware
 * @param addr address where to write and read
 * @return SANE_TRUE on success, SANE_FALSE otherwise
 */
static int
memtest (int fd, uint16_t addr)
{
  uint8_t sent[256];
  uint8_t back[256];
  int i;

  write_reg2 (fd, REG1, addr);
  for (i = 0; i < 256; i++)
    {
      sent[i] = (uint8_t) i;
      back[i] = 0;
    }
  write_data (fd, sent, 256);
  read_data (fd, back, 256);

  /* check if data read back is the same that the one sent */
  for (i = 0; i < 256; i++)
    {
      if (back[i] != sent[i])
	{
	  return SANE_FALSE;
	}
    }

  return SANE_TRUE;
}


#define INB(k,y,z) val=inb(k,y); if(val!=z) { DBG(DBG_error,"expected 0x%02x, got 0x%02x\n",z, val); return SANE_FALSE; }

/** @brief connect to scanner
 * This function sends the connect sequence for the scanner.
 * @param fd filedescriptor of the parallel port communication channel
 * @return SANE_TRUE in case of success, SANE_FALSE otherwise
 */
static int
connect (int fd)
{
  uint8_t val;

  inb (fd, CONTROL);
  outb (fd, CONTROL, 0x04);
  outb (fd, DATA, 0x02);
  INB (fd, DATA, 0x02);
  outb (fd, DATA, 0x03);
  INB (fd, DATA, 0x03);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  INB (fd, DATA, 0x83);
  outb (fd, DATA, 0x82);
  INB (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  INB (fd, DATA, 0x82);
  outb (fd, DATA, 0x82);
  INB (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  INB (fd, DATA, 0x82);
  outb (fd, DATA, 0x83);
  INB (fd, DATA, 0x83);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  INB (fd, DATA, 0x83);
  outb (fd, DATA, 0x82);
  INB (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  INB (fd, DATA, 0x82);
  outb (fd, DATA, 0x83);
  INB (fd, DATA, 0x83);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  INB (fd, DATA, 0x83);
  outb (fd, DATA, 0x83);
  INB (fd, DATA, 0x83);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  outb (fd, DATA, 0x03);
  outb (fd, DATA, 0x83);
  INB (fd, DATA, 0x83);
  outb (fd, DATA, 0x82);
  INB (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  outb (fd, DATA, 0x02);
  outb (fd, DATA, 0x82);
  outb (fd, DATA, 0xFF);
  DBG (DBG_info, "connect() OK...\n");
  return SANE_TRUE;
}

static int
disconnect (int fd)
{
  uint8_t val;

  outb (fd, CONTROL, 0x04);
  outb (fd, DATA, 0x00);
  INB (fd, DATA, 0x00);
  outb (fd, DATA, 0x01);
  INB (fd, DATA, 0x01);
  outb (fd, DATA, 0x01);
  outb (fd, DATA, 0x81);
  outb (fd, DATA, 0x01);
  outb (fd, DATA, 0x81);
  INB (fd, DATA, 0x81);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x81);
  INB (fd, DATA, 0x81);
  outb (fd, DATA, 0x01);
  outb (fd, DATA, 0x81);
  outb (fd, DATA, 0x01);
  outb (fd, DATA, 0x81);
  INB (fd, DATA, 0x81);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  INB (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  outb (fd, DATA, 0x00);
  outb (fd, DATA, 0x80);
  inb (fd, CONTROL);
  outb (fd, CONTROL, 0x0C);

  return SANE_STATUS_GOOD;
}

static void
setadresses (int fd, uint16_t start, uint16_t end)
{
  write_reg (fd, REG3, start & 0xff);
  write_reg (fd, REG4, start >> 8);
  write_reg (fd, REG5, end & 0xff);
  write_reg (fd, REG6, end >> 8);
  DBG (DBG_io, "setadresses(0x%x,0x%x); OK...\n", start, end);
}

#ifdef HAVE_LINUX_PPDEV_H
/** @brief open parallel port device
 * opens parallel port's low level device in EPP mode
 * @param devicename nam of the real device or the special value 'auto'
 * @return file descriptor in cas of successn -1 otherwise
 */
static int
open_pp (const char *devicename)
{
  int fd, mode = 0;
  char *name;

  DBG (DBG_proc, "open_pp: start, devicename=%s\n", devicename);
  /* TODO improve auto device finding */
  if (strncmp (devicename, "auto", 4) == 0)
    {
      name = strdup("/dev/parport0");
    }
  else
    {
      name = strdup(devicename);
    }

  /* open device */
  fd = open (name, O_RDWR);
  if (fd < 0)
    {
      switch (errno)
	{
	case ENOENT:
#ifdef ENIO
	case ENXIO:
#endif
#ifdef ENODEV
	case ENODEV:
#endif
	  DBG (DBG_error, "open_pp: no %s device ...\n", name);
	  break;
	case EACCES:
	  DBG (DBG_error,
	       "open_pp: current user cannot use existing %s device ...\n",
	       name);
	  break;
	default:
	  DBG (DBG_error, "open_pp: %s while opening %s\n", strerror (errno),
	       name);
	}
      return -1;
    }
  free(name);

  /* claim device and set it to EPP */
  ioctl (fd, PPCLAIM);
  ioctl (fd, PPGETMODES, &mode);
  if (mode & PARPORT_MODE_PCSPP)
    DBG (DBG_io, "PARPORT_MODE_PCSPP\n");
  if (mode & PARPORT_MODE_TRISTATE)
    DBG (DBG_io, "PARPORT_MODE_TRISTATE\n");
  if (mode & PARPORT_MODE_EPP)
    DBG (DBG_io, "PARPORT_MODE_EPP\n");
  if (mode & PARPORT_MODE_ECP)
    DBG (DBG_io, "PARPORT_MODE_ECP\n");
  if (mode & PARPORT_MODE_COMPAT)
    DBG (DBG_io, "PARPORT_MODE_COMPAT\n");
  if (mode & PARPORT_MODE_DMA)
    DBG (DBG_io, "PARPORT_MODE_DMA\n");
  if (mode & PARPORT_MODE_EPP)
    {
      mode = IEEE1284_MODE_EPP;
    }
  else
    {
      /*
         if (mode & PARPORT_MODE_ECP)
         {
         mode = IEEE1284_MODE_ECP;
         }
         else
       */
      {
	mode = -1;
      }
    }
  if (mode == -1)
    {
      DBG (DBG_error, "open_pp: no EPP mode, giving up ...\n");
      ioctl (fd, PPRELEASE);
      close (fd);
      return -1;
    }
  ioctl (fd, PPNEGOT, &mode);
  ioctl (fd, PPSETMODE, &mode);
  DBG (DBG_proc, "open_pp: exit\n");
  return fd;
}

/** close low level device
 * release and close low level hardware device
 */
static void
close_pp (int fd)
{
  int mode = IEEE1284_MODE_COMPAT;

  if (fd > 2)
    {
      ioctl (fd, PPNEGOT, &mode);
      ioctl (fd, PPRELEASE);
      close (fd);
    }
}

#else /*  HAVE_LINUX_PPDEV_H */

static int
open_pp (const char *devicename)
{
  if(devicename)
    return -1;
  return -1;
}

static void
close_pp (int fd)
{
  if(fd)
    return;
}
#endif /*  HAVE_LINUX_PPDEV_H */

/** @brief test if a document is inserted
 * Test if a document is inserted by reading register E
 * @param fd file descriptor to access scanner
 * @return SANE_STATUS_NO_DOCS if no document or SANE_STATUS_GOOD
 * if something is present.
 */
static SANE_Status
test_document (int fd)
{
  int detector;

  /* check for document presence 0xC6: present, 0xC3 no document */
  detector = read_reg (fd, REGE);
  DBG (DBG_io, "test_document: detector=0x%02X\n", detector);

  /* document inserted */
  if (detector & 0x04)
    return SANE_STATUS_GOOD;

  return SANE_STATUS_NO_DOCS;
}

/**
 * return the amount of scanned data available
 * @param fd file descriptor to access scanner
 * @return available byte number
 */
static int
available_bytes (int fd)
{
  int counter;

  /* read the number of 256 bytes block of scanned data */
  counter = read_reg (fd, REG9);
  DBG (DBG_io, "available_bytes: available_bytes=0x%02X\n", counter);
  return 256 * counter;
}

static SANE_Status
build_correction (P5_Device * dev, unsigned int dpi, unsigned int mode,
		  unsigned int start, unsigned int width)
{
  unsigned int i, j, shift, step;

  DBG (DBG_proc, "build_correction: start=%d, width=%d\n", start, width);
  DBG (DBG_trace, "build_correction: dpi=%d, mode=%d\n", dpi, mode);

  /* loop on calibration data to find the matching one */
  j = 0;
  while (dev->calibration_data[j]->dpi != dpi)
    {
      j++;
      if (j > MAX_RESOLUTIONS)
	{
	  DBG (DBG_error, "build_correction: couldn't find calibration!\n");
	  return SANE_STATUS_INVAL;
	}
    }

  if (dev->gain != NULL)
    {
      free (dev->gain);
      dev->gain = NULL;
    }
  if (dev->offset != NULL)
    {
      free (dev->offset);
      dev->offset = NULL;
    }
  dev->gain = (float *) malloc (width * sizeof (float));
  if (dev->gain == NULL)
    {
      DBG (DBG_error,
	   "build_correction: failed to allocate memory for gain!\n");
      return SANE_STATUS_NO_MEM;
    }
  dev->offset = (uint8_t *) malloc (width);
  if (dev->offset == NULL)
    {
      DBG (DBG_error,
	   "build_correction: failed to allocate memory for offset!\n");
      return SANE_STATUS_NO_MEM;
    }

  /* compute starting point of calibration data to use */
  shift = start;
  step = 1;
  if (mode == MODE_GRAY)
    {
      /* we use green data */
      shift += 1;
      step = 3;
    }
  for (i = 0; i < width; i += step)
    {
      if (dev->calibration_data[j]->white_data[shift + i] -
	  dev->calibration_data[0]->black_data[shift + i] > BLACK_LEVEL)
	{
	  dev->gain[i] =
	    WHITE_TARGET /
	    ((float)
	     (dev->calibration_data[j]->white_data[shift + i] -
	      dev->calibration_data[j]->black_data[shift + i]));
	  dev->offset[i] = dev->calibration_data[j]->black_data[shift + i];
	}
      else
	{
	  dev->gain[i] = 1.0;
	  dev->offset[i] = 0;
	}
    }
  return SANE_STATUS_GOOD;
  DBG (DBG_proc, "build_correction: end\n");
}

/** @brief start up a real scan
 * This function starts the scan with the given parameters.
 * @param dev device describing hardware
 * @param mode color, gray level or lineart.
 * @param dpi desired scan resolution.
 * @param startx coordinate of the first pixel to scan in
 * scan's resolution coordinate
 * @param width width of the scanned area
 * scanner's physical scan aread.
 * @return SANE_STATUS_GOOD if scan is successfully started
 */
static SANE_Status
start_scan (P5_Device * dev, int mode, unsigned int dpi, unsigned int startx,
	    unsigned int width)
{
  uint8_t reg0=0;
  uint8_t reg2=0;
  uint8_t regF=0;
  uint16_t addr=0;
  uint16_t start, end;
  unsigned int xdpi;

  DBG (DBG_proc, "start_scan: start \n");
  DBG (DBG_io, "start_scan: startx=%d, width=%d, dpi=%d\n", startx, width,
       dpi);

	/** @brief register values
	 * - reg2 : reg2 seems related to x dpi and provides only 100,
	 *   150, 200 and 300 resolutions.
	 * - regF : lower nibble gives y dpi resolution ranging from 150
	 *   to 1200 dpi.
	 */
  xdpi = dpi;
  switch (dpi)
    {
    case 100:
      reg2 = 0x90;
      regF = 0xA2;
      break;
    case 150:
      reg2 = 0x10;
      regF = 0xA4;
      break;
    case 200:
      reg2 = 0x80;
      regF = 0xA6;
      break;
    case 300:
      reg2 = 0x00;
      regF = 0xA8;
      break;
    case 400:
      reg2 = 0x80;		/* xdpi=200 */
      regF = 0xAA;
      xdpi = 200;
      break;
    case 500:
      reg2 = 0x00;
      regF = 0xAC;
      xdpi = 300;
      break;
    case 600:
      reg2 = 0x00;
      regF = 0xAE;
      xdpi = 300;
      break;
    }

  switch (mode)
    {
    case MODE_COLOR:
      reg0 = 0x00;
      addr = 0x0100;
      break;
    case MODE_GRAY:
      /* green channel only */
      reg0 = 0x20;
      addr = 0x0100;
      break;
    case MODE_LINEART:
      reg0 = 0x40;
      addr = 0x0908;
      break;
    }

  write_reg (dev->fd, REG1, 0x01);
  write_reg (dev->fd, REG7, 0x00);
  write_reg (dev->fd, REG0, reg0);
  write_reg (dev->fd, REG1, 0x00);
  write_reg (dev->fd, REGF, regF);
  /* the memory addr used to test need not to be related
   * to resolution, 0x0100 could be always used */
  /* TODO get rid of it */
  memtest (dev->fd, addr);

  /* handle case where dpi>xdpi */
  start = startx;
  if (dpi > xdpi)
    {
      width = (width * xdpi) / dpi;
      start = (startx * xdpi) / dpi;
    }

  /* compute and set start addr */
  if (mode == MODE_COLOR)
    {
      start = start * 3;
      width = width * 3;
    }
  end = start + width + 1;

  /* build calibration data for the scan */
  if (dev->calibrated)
    {
      build_correction (dev, xdpi, mode, start, width);
    }

  setadresses (dev->fd, start, end);

  write_reg (dev->fd, REG1, addr >> 8);
  write_reg (dev->fd, REG2, reg2);
  regF = (regF & 0x0F) | 0x80;
  write_reg (dev->fd, REGF, regF);
  write_reg (dev->fd, REG0, reg0);
  if (mode == MODE_LINEART)
    {
      write_reg (dev->fd, 0x07, 0x04);
    }
  else
    {
      write_reg (dev->fd, 0x07, 0x00);
    }
  write_reg (dev->fd, REG1, addr >> 8);
  write_reg2 (dev->fd, REG1, addr);
  write_reg (dev->fd, REGF, regF | 0x01);
  write_reg (dev->fd, REG0, reg0 | 0x0C);
  if (mode == MODE_LINEART)
    {
      write_reg (dev->fd, REG1, 0x19);
    }
  else
    {
      write_reg (dev->fd, REG1, 0x11);
    }
  DBG (DBG_proc, "start_scan: exit\n");
  return SANE_STATUS_GOOD;
}

/** read a line of scan data
 * @param dev device to read
 * @param data pointer where to store data
 * @param length total bytes to read on one line
 * @param ltr total number of lines to read
 * @param retry signals that the function must read as much lines it can
 * @param x2 tells that lines must be enlarged by a 2 factor
 * @param mode COLOR_MODE if color mode
 * @returns number of data lines read, -1 in case of error
 */
static int
read_line (P5_Device * dev, uint8_t * data, size_t length, int ltr,
	   SANE_Bool retry, SANE_Bool x2, int mode, SANE_Bool correction)
{
  uint8_t counter, read, cnt;
  uint8_t inbuffer[MAX_SENSOR_PIXELS * 2 * 3 + 2];
  unsigned int i, factor;
  float val;

  DBG (DBG_proc, "read_line: trying to read %d lines of %lu bytes\n", ltr,
       (unsigned long)length);

  counter = read_reg (dev->fd, REG9);
  DBG (DBG_io, "read_line: %d bytes available\n", counter * 256);
  read = 0;
  if (x2 == SANE_FALSE)
    {
      factor = 1;
    }
  else
    {
      factor = 2;
    }

  /* in retry mode we read until not enough data, but in no retry
   * read only one line , counter give us 256 bytes block available
   * and we want an number multiple of color channels */
  cnt = (255 + length / factor) / 256;
  while ((counter > cnt && retry == 1) || (counter > cnt && read == 0))
    {
      /* read data from scanner, first and last byte aren't picture data */
      read_data (dev->fd, inbuffer, length / factor + 2);

      /* image correction */
      if (correction == SANE_TRUE)
	{
	  for (i = 0; i < length / factor; i++)
	    {
	      val = inbuffer[i + 1] - dev->offset[i];
	      if (val > 0)
		{
		  val = val * dev->gain[i];
		  if (val < 255)
		    inbuffer[i + 1] = val;
		  else
		    inbuffer[i + 1] = 255;
		}
	      else
		{
		  inbuffer[i + 1] = 0;
		}
	    }
	}

      /* handle horizontal data doubling */
      if (x2 == SANE_FALSE)
	{
	  memcpy (data + read * length, inbuffer + 1, length);
	}
      else
	{
	  if (mode == MODE_COLOR)
	    {
	      for (i = 0; i < length / factor; i += 3)
		{
		  data[read * length + i * factor] = inbuffer[i + 1];
		  data[read * length + i * factor + 1] = inbuffer[i + 2];
		  data[read * length + i * factor + 2] = inbuffer[i + 3];
		  data[read * length + i * factor + 3] = inbuffer[i + 1];
		  data[read * length + i * factor + 4] = inbuffer[i + 2];
		  data[read * length + i * factor + 5] = inbuffer[i + 3];
		}
	    }
	  else
	    {
	      for (i = 0; i < length / factor; i++)
		{
		  data[read * length + i * factor] = inbuffer[i + 1];
		  data[read * length + i * factor + 1] = inbuffer[i + 1];
		}
	    }
	}
      read++;
      if (retry == SANE_TRUE)
	{
	  read_reg (dev->fd, REGF);
	  read_reg (dev->fd, REGA);
	  read_reg (dev->fd, REG9);
	  counter = read_reg (dev->fd, REG9);
	  read_reg (dev->fd, REGA);
	  if (read >= ltr)
	    {
	      DBG (DBG_io, "read_line returning %d lines\n", read);
	      return read;
	    }
	  counter = read_reg (dev->fd, REG9);
	}
    }
  read_reg (dev->fd, REGF);
  read_reg (dev->fd, REGA);
  read_reg (dev->fd, REG9);
  counter = read_reg (dev->fd, REG9);
  read_reg (dev->fd, REGA);
  DBG (DBG_io, "read_line returning %d lines\n", read);
  return read;
}


static SANE_Status
eject (int fd)
{
  int detector;

  DBG (DBG_proc, "eject: start ...\n");

  do
    {
      write_reg2 (fd, REG1, 0x1110);
      detector = read_reg (fd, REGE);
      detector = read_reg (fd, REGE);
    }
  while ((detector & 0x04) != 0);
  write_reg (fd, REG0, 0x00);
  write_reg (fd, REG1, 0x00);
  write_reg (fd, REGF, 0x82);
  write_reg (fd, REG7, 0x00);

  DBG (DBG_proc, "eject: end.\n");
  return SANE_STATUS_GOOD;
}

/** @brief wait for document to be present in feeder
 * Polls document sensor until something is present. Give up after 20 seconds
 * @param fd file descriptor of the physical device
 */
/* static int
wait_document (int fd, uint8_t detector)
{
  int count = 0;
  uint8_t val;

  write_reg (fd, REG1, 0x00);
  write_reg (fd, REG7, 0x00);
  detector = read_reg (fd, REGE);
  while (detector == 0xc3 && count < 20)
    {
      sleep (1);
      count++;
      detector = read_reg (fd, REGE);
    }
  setadresses (fd, 0x002d, 0x09c7);
  write_reg (fd, REG1, 0x00);
  write_reg (fd, REG2, 0x90);
  write_reg (fd, REGF, 0x82);
  write_reg (fd, REG0, 0x00);
  val = inb (fd, STATUS) & 0xf8;
  if (val != 0xf8)
    {
      DBG (DBG_error, "wait_document: unexpected STATUS value 0x%02x instead of 0xf8", val);
    }
  if (count >= 20)
    {
      DBG (DBG_error, "wait_document: failed to detect document!\n");
      return 0;
    }
  return 1;
} */

/** @brief move at 150 dpi
 * move the paper at 150 dpi motor speed by the amount specified
 * @params dev pointer to the device structure
 */
static SANE_Status
move (P5_Device * dev)
{
  int skip, done, read, count;
  SANE_Status status = SANE_STATUS_GOOD;
  unsigned char buffer[256];

  DBG (DBG_proc, "move: start\n");

  /* compute number of lines to skip */
  skip = dev->ystart;

  /* works, but remains to be explained ... */
  if (dev->ydpi > 300)
    skip = skip / 2;

  DBG (DBG_io, "move: skipping %d lines at %d dpi\n", skip, dev->ydpi);

  /* we do a real scan of small width, discarding data */
  done = 0;
  status = start_scan (dev, MODE_GRAY, dev->ydpi, 0, 256);
  if (status != SANE_STATUS_GOOD)
    {
      DBG (DBG_error, "move: failed to start scan\n");
      return SANE_STATUS_INVAL;
    }

  do
    {
      /* test if document left the feeder */
      status = test_document (dev->fd);
      if (status == SANE_STATUS_NO_DOCS)
	{
	  DBG (DBG_info,
	       "move: document was shorter than the required move\n");
	  return SANE_STATUS_INVAL;
	}
      /* test if data is available */
      count = available_bytes (dev->fd);
      if (count)
	{
	  read =
	    read_line (dev, buffer, 256, 1, SANE_FALSE, SANE_FALSE,
		       MODE_GRAY, SANE_FALSE);
	  if (read == -1)
	    {
	      DBG (DBG_error, "move: failed to read data\n");
	      return SANE_STATUS_INVAL;
	    }
	  done += read;
	}
    }
  while (done < skip);

  /* reset scanner */
  write_reg2 (dev->fd, REG1, 0x1110);
  count = read_reg (dev->fd, REGE);
  count = read_reg (dev->fd, REGE);
  write_reg (dev->fd, REG0, 0x00);
  write_reg (dev->fd, REG1, 0x00);
  write_reg (dev->fd, REGF, 0x82);
  write_reg (dev->fd, REG7, 0x00);

  DBG (DBG_proc, "move: exit\n");
  return status;
}

/** clean up calibration data
 * @param dev device to clean up
 */
static void
cleanup_calibration (P5_Device * dev)
{
  int i;

  for (i = 0; i < MAX_RESOLUTIONS * 2; i++)
    {
      if (dev->calibration_data[i] != NULL)
	{
	  free (dev->calibration_data[i]);
	  dev->calibration_data[i] = NULL;
	}
    }
  dev->calibrated = SANE_FALSE;
}

/** detect a black scan line
 * parses the given buffer and return SANE_TRUE if the line is an
 * acceptable black line for calibration
 * @param buffer data line to parse
 * @param pixels number of pixels
 * @param mode MODE_COLOR or MODE_GRAY
 * @returns SANE_TRUE if it is considered as a white line
 */
static SANE_Bool
is_black_line (uint8_t * buffer, unsigned int pixels, int mode)
{
  unsigned int i, start, end, count, width;

  /* compute width in bytes */
  if (mode == MODE_COLOR)
    {
      width = pixels * 3;
    }
  else
    {
      width = pixels;
    }

  /* we allow the calibration target to be narrower than full width, ie
   * black margin at both ends of the line */
  start = (5 * width) / 100;
  end = (95 * width) / 100;
  count = 0;

  /* count number of black bytes */
  for (i = start; i < end; i++)
    {
      if (buffer[i] > BLACK_LEVEL)
	{
	  count++;
	}
    }

  /* we allow 3% black pixels maximum */
  if (count > (3 * width) / 100)
    {
      DBG (DBG_io, "is_black_line=SANE_FALSE\n");
      return SANE_FALSE;
    }

  DBG (DBG_io, "is_black_line=SANE_TRUE\n");
  return SANE_TRUE;
}

/** detect a white scan line
 * parses the given buffer and return SANE_TRUE if the line is an
 * acceptable white line for calibration
 * @param buffer data line to parse
 * @param pixels number of pixels
 * @param mode MODE_COLOR or MODE_GRAY
 * @returns SANE_TRUE if it is considered as a white line
 */
static SANE_Bool
is_white_line (uint8_t * buffer, unsigned int pixels, int mode)
{
  unsigned int i, start, end, count, width;

  /* compute width in bytes */
  if (mode == MODE_COLOR)
    {
      width = pixels * 3;
    }
  else
    {
      width = pixels;
    }

  /* we allow the calibration target to be narrower than full width, ie
   * black margin at both ends of the line */
  start = (5 * width) / 100;
  end = (95 * width) / 100;
  count = 0;

  /* count number of black bytes */
  for (i = start; i < end; i++)
    {
      if (buffer[i] < BLACK_LEVEL)
	{
	  count++;
	}
    }

  /* we allow 3% black pixels maximum */
  if (count > (3 * width) / 100)
    {
      DBG (DBG_io, "is_white_line=SANE_FALSE\n");
      return SANE_FALSE;
    }

  DBG (DBG_io, "is_white_line=SANE_TRUE\n");
  return SANE_TRUE;
}

/* ------------------------------------------------------------------------- */
/* writes gray data to a pnm file */
/*
static void
write_gray_data (unsigned char *image, char *name, SANE_Int width,
		 SANE_Int height)
{
  FILE *fdbg = NULL;

  fdbg = fopen (name, "wb");
  if (fdbg == NULL)
    return;
  fprintf (fdbg, "P5\n%d %d\n255\n", width, height);
  fwrite (image, width, height, fdbg);
  fclose (fdbg);
}
*/

/* ------------------------------------------------------------------------- */
/* writes rgb data to a pnm file */
static void
write_rgb_data (char *name, unsigned char *image, SANE_Int width,
		SANE_Int height)
{
  FILE *fdbg = NULL;

  fdbg = fopen (name, "wb");
  if (fdbg == NULL)
    return;
  fprintf (fdbg, "P6\n%d %d\n255\n", width, height);
  fwrite (image, width * 3, height, fdbg);
  fclose (fdbg);
}

/** give calibration file name
 * computes the calibration file name to use based on the
 * backend name and device
 */
static char *
calibration_file (const char *devicename)
{
  char *ptr = NULL;
  char tmp_str[PATH_MAX];

  ptr = getenv ("HOME");
  if (ptr != NULL)
    {
      sprintf (tmp_str, "%s/.sane/p5-%s.cal", ptr, devicename);
    }
  else
    {
      ptr = getenv ("TMPDIR");
      if (ptr != NULL)
	{
	  sprintf (tmp_str, "%s/p5-%s.cal", ptr, devicename);
	}
      else
	{
	  sprintf (tmp_str, "/tmp/p5-%s.cal", devicename);
	}
    }
  DBG (DBG_trace, "calibration_file: using >%s< for calibration file name\n",
       tmp_str);
  return strdup (tmp_str);
}

/** restore calibration data
 * restore calibration data by loading previously saved calibration data
 * @param dev device to restore
 * @return SANE_STATUS_GOOD on success, otherwise error code
 */
static SANE_Status
restore_calibration (P5_Device * dev)
{
  SANE_Status status = SANE_STATUS_GOOD;
  char *fname = NULL;
  FILE *fcalib = NULL;
  size_t size;
  int i;

  DBG (DBG_proc, "restore_calibration: start\n");
  cleanup_calibration (dev);
  fname = calibration_file (dev->model->name);
  fcalib = fopen (fname, "rb");
  if (fcalib == NULL)
    {
      DBG (DBG_error, "restore_calibration: failed to open %s!\n", fname);
      free (fname);
      return SANE_STATUS_IO_ERROR;
    }

  /* loop filling calibration data until EOF reached */
  i = 0;
  while (!feof (fcalib) && (i < 2 * MAX_RESOLUTIONS))
    {
      dev->calibration_data[i] = malloc (sizeof (P5_Calibration_Data));
      if (dev->calibration_data[i] == NULL)
	{
	  cleanup_calibration (dev);
	  free (fname);
	  fclose (fcalib);
	  DBG (DBG_error,
	       "restore_calibration: failed to allocate memory for calibration\n");
	  return SANE_STATUS_NO_MEM;
	}
      size =
	fread (dev->calibration_data[i], 1, sizeof (P5_Calibration_Data),
	       fcalib);
      if (feof (fcalib))
	{
	  free (dev->calibration_data[i]);
	  dev->calibration_data[i] = NULL;
	}
      else if (size != sizeof (P5_Calibration_Data))
	{
	  cleanup_calibration (dev);
	  free (fname);
	  fclose (fcalib);
	  DBG (DBG_error, "restore_calibration: failed to read from file\n");
	  return SANE_STATUS_IO_ERROR;
	}
      DBG (DBG_trace,
	   "restore_calibration: read 1 calibration structure from file\n");
      i++;
    }

  dev->calibrated = SANE_TRUE;
  fclose (fcalib);
  free (fname);

  DBG (DBG_proc, "restore_calibration: end\n");
  return status;
}

/** save calibration data
 * save calibration data from memory to file
 * @param dev device calibration to save
 * @return SANE_STATUS_GOOD on success, otherwise error code
 */
static SANE_Status
save_calibration (P5_Device * dev)
{
  SANE_Status status = SANE_STATUS_GOOD;
  char *fname = NULL;
  FILE *fcalib = NULL;
  int i;
  size_t size;

  DBG (DBG_proc, "save_calibration: start\n");
  fname = calibration_file (dev->model->name);
  fcalib = fopen (fname, "wb");
  if (fcalib == NULL)
    {
      DBG (DBG_error, "save_calibration: failed to open %s!\n", fname);
      free (fname);
      return SANE_STATUS_IO_ERROR;
    }

  /* loop filling calibration data until EOF reached */
  i = 0;
  while (dev->calibration_data[i] != NULL && (i < 2 * MAX_RESOLUTIONS))
    {
      size =
	fwrite (dev->calibration_data[i], sizeof (P5_Calibration_Data), 1,
		fcalib);
      if (size != sizeof (P5_Calibration_Data))
	{
	  free (fname);
	  fclose (fcalib);
	  DBG (DBG_error, "save_calibration: failed to write to file\n");
	  return SANE_STATUS_IO_ERROR;
	}
      DBG (DBG_trace,
	   "save_calibration: wrote 1 calibration structure to file\n");
      i++;
    }

  fclose (fcalib);
  free (fname);

  DBG (DBG_proc, "save_calibration: end\n");
  return status;
}

/** calibrate scanner
 * calibrates scanner by scanning a white sheet to get
 * reference data. The black reference data is extracted from the lines
 * that precede the physical document.
 * Calibration is done at 300 color, then data is built for other modes
 * and resolutions.
 * @param dev device to calibrate
 */
static SANE_Status
sheetfed_calibration (P5_Device * dev)
{
  uint8_t buffer[MAX_SENSOR_PIXELS * 3];
  uint16_t white_data[MAX_SENSOR_PIXELS * 3];
  uint16_t black_data[MAX_SENSOR_PIXELS * 3];
  unsigned int i, j, k, dpi, pixels, read, black, white;
  float coeff;
  unsigned int red, green, blue;
  int line;
  SANE_Status status;
  char title[40];

  FILE *dbg = fopen ("debug.pnm", "wb");
  fprintf (dbg, "P6\n%d %d\n255\n", MAX_SENSOR_PIXELS,
	   CALIBRATION_SKIP_LINES * 4);

  DBG (DBG_proc, "sheetfed_calibration: start\n");

  /* check calibration target has been loaded in ADF */
  status = test_document (dev->fd);
  if (status == SANE_STATUS_NO_DOCS)
    {
      DBG (DBG_error,
	   "sheetfed_calibration: no calibration target present!\n");
      return SANE_STATUS_NO_DOCS;
    }

  /* clean up calibration data */
  cleanup_calibration (dev);

  /*  a RGB scan to get reference data */
  /* initialize calibration slot for the resolution */
  i = 0;
  dpi = dev->model->max_xdpi;
  pixels = MAX_SENSOR_PIXELS;
  dev->calibration_data[i] =
    (P5_Calibration_Data *) malloc (sizeof (P5_Calibration_Data));
  if (dev->calibration_data[i] == NULL)
    {
      cleanup_calibration (dev);
      DBG (DBG_error,
	   "sheetfed_calibration: failed to allocate memory for calibration\n");
      return SANE_STATUS_NO_MEM;
    }
  dev->calibration_data[i]->dpi = dpi;

  /* start scan */
  status = start_scan (dev, MODE_COLOR, dpi, 0, pixels);
  if (status != SANE_STATUS_GOOD)
    {
      cleanup_calibration (dev);
      DBG (DBG_error,
	   "sheetfed_calibration: failed to start scan at %d dpi\n", dpi);
      return SANE_STATUS_INVAL;
    }

  white = 0;
  black = 0;
  read = 0;
  for (j = 0; j < pixels * 3; j++)
    {
      black_data[j] = 0;
      white_data[j] = 0;
    }

  /* read lines and gather black and white ones until enough for sensor's
   * native resolution */
  do
    {
      status = test_document (dev->fd);
      if (status == SANE_STATUS_NO_DOCS && (white < 10 || black < 10))
	{
	  cleanup_calibration (dev);
	  DBG (DBG_error,
	       "sheetfed_calibration: calibration sheet too short!\n");
	  return SANE_STATUS_INVAL;
	}
      memset (buffer, 0x00, MAX_SENSOR_PIXELS * 3);
      line =
	read_line (dev, buffer, pixels * 3, 1, SANE_FALSE, SANE_FALSE,
		   MODE_COLOR, SANE_FALSE);
      if (line == -1)
	{
	  DBG (DBG_error, "sheetfed_calibration: failed to read data\n");
	  return SANE_STATUS_INVAL;
	}

      /* if a data line has been read, add it to reference data */
      if (line)
	{
	  read++;
	  fwrite (buffer, pixels * 3, 1, dbg);
	  if (is_white_line (buffer, pixels, MODE_COLOR) && white < 256)
	    {
	      white++;
	      /* first calibration lines are skipped */
	      for (j = 0; j < pixels * 3 && read > CALIBRATION_SKIP_LINES;
		   j++)
		{
		  white_data[j] += buffer[j];
		}
	    }
	  if (is_black_line (buffer, pixels, MODE_COLOR) && black < 256)
	    {
	      black++;
	      for (j = 0; j < pixels * 3; j++)
		{
		  black_data[j] += buffer[j];
		}
	    }
	}
    }
  while (test_document (dev->fd) != SANE_STATUS_NO_DOCS);
  DBG (DBG_trace, "sheetfed_calibration: white lines=%d, black lines=%d\n",
       white, black);

  /* average pixels and store in per dpi calibration data */
  for (j = 0; j < pixels * 3; j++)
    {
      dev->calibration_data[i]->white_data[j] = white_data[j] / white;
      dev->calibration_data[i]->black_data[j] = black_data[j] / black;
    }

  /* we average red, green and blue offset on the full sensor */
  red = 0;
  green = 0;
  blue = 0;
  for (j = 0; j < pixels * 3; j += 3)
    {
      red += dev->calibration_data[i]->black_data[j];
      green += dev->calibration_data[i]->black_data[j + 1];
      blue += dev->calibration_data[i]->black_data[j + 2];
    }
  for (j = 0; j < pixels * 3; j += 3)
    {
      dev->calibration_data[i]->black_data[j] = red / pixels;
      dev->calibration_data[i]->black_data[j + 1] = green / pixels;
      dev->calibration_data[i]->black_data[j + 2] = blue / pixels;
    }

  /* trace calibration data for debug */
  if (DBG_LEVEL > DBG_data)
    {
      sprintf (title, "calibration-white-%d.pnm",
	       dev->calibration_data[i]->dpi);
      write_rgb_data (title, dev->calibration_data[i]->white_data, pixels, 1);
      sprintf (title, "calibration-black-%d.pnm",
	       dev->calibration_data[i]->dpi);
      write_rgb_data (title, dev->calibration_data[i]->black_data, pixels, 1);
    }

  /* loop on all remaining resolution and compute calibration data from it */
  for (i = 1; i < MAX_RESOLUTIONS && dev->model->xdpi_values[i] > 0; i++)
    {
      dev->calibration_data[i] =
	(P5_Calibration_Data *) malloc (sizeof (P5_Calibration_Data));
      if (dev->calibration_data[i] == NULL)
	{
	  cleanup_calibration (dev);
	  DBG (DBG_error,
	       "sheetfed_calibration: failed to allocate memory for calibration\n");
	  return SANE_STATUS_INVAL;
	}
      dev->calibration_data[i]->dpi = dev->model->xdpi_values[i];

      coeff = ((float) dev->model->xdpi_values[i]) / (float) dpi;

      /* generate data by decimation */
      for (j = 0; j < pixels / coeff; j++)
	{
	  k = j * coeff;
	  dev->calibration_data[i]->white_data[j] =
	    dev->calibration_data[0]->white_data[k];
	  dev->calibration_data[i]->white_data[j + 1] =
	    dev->calibration_data[0]->white_data[k + 1];
	  dev->calibration_data[i]->white_data[j + 2] =
	    dev->calibration_data[0]->white_data[k + 2];
	  dev->calibration_data[i]->black_data[j] =
	    dev->calibration_data[0]->black_data[k];
	  dev->calibration_data[i]->black_data[j + 1] =
	    dev->calibration_data[0]->black_data[k + 1];
	  dev->calibration_data[i]->black_data[j + 2] =
	    dev->calibration_data[0]->black_data[k + 2];
	}
    }

  fclose (dbg);
  dev->calibrated = SANE_TRUE;

  /* eject calibration target */
  eject (dev->fd);

  DBG (DBG_proc, "sheetfed_calibration: end\n");
  return SANE_STATUS_GOOD;
}

/* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */