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
|
/* sane - Scanner Access Now Easy.
Copyright (C) 1998 David Huggins-Daines, heavily based on the Apple
scanner driver (since Abaton scanners are very similar to old Apple
scanners), which is (C) 1998 Milon Firikis, which is, in turn, based
on the Mustek driver, (C) 1996-7 David Mosberger-Tang.
This file is part of the SANE package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
As a special exception, the authors of SANE give permission for
additional uses of the libraries contained in this release of SANE.
The exception is that, if you link a SANE library with other files
to produce an executable, this does not by itself cause the
resulting executable to be covered by the GNU General Public
License. Your use of that executable is in no way restricted on
account of linking the SANE library code into it.
This exception does not, however, invalidate any other reasons why
the executable file might be covered by the GNU General Public
License.
If you submit changes to SANE to the maintainers to be included in
a subsequent release, you agree by submitting the changes that
those changes may be distributed with this exception intact.
If you write modifications of your own for SANE, it is your choice
whether to permit this exception to apply to your modifications.
If you do not wish that, delete this exception notice.
This file implements a SANE backend for Abaton flatbed scanners. */
#include "../include/sane/config.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "../include/_stdint.h"
#include "../include/sane/sane.h"
#include "../include/sane/sanei.h"
#include "../include/sane/saneopts.h"
#include "../include/sane/sanei_scsi.h"
#define BACKEND_NAME abaton
#include "../include/sane/sanei_backend.h"
#ifndef PATH_MAX
# define PATH_MAX 1024
#endif
#include "../include/sane/sanei_config.h"
#define ABATON_CONFIG_FILE "abaton.conf"
#include "abaton.h"
static const SANE_Device **devlist = 0;
static int num_devices;
static Abaton_Device *first_dev;
static Abaton_Scanner *first_handle;
static SANE_String_Const mode_list[5];
static const SANE_String_Const halftone_pattern_list[] =
{
"spiral", "bayer",
0
};
static const SANE_Range dpi_range =
{
/* min, max, quant */
72,
300,
1
};
static const SANE_Range enhance_range =
{
1,
255,
1
};
static const SANE_Range x_range =
{
0,
8.5 * MM_PER_INCH,
1
};
static const SANE_Range y_range =
{
0,
14.0 * MM_PER_INCH,
1
};
#define ERROR_MESSAGE 1
#define USER_MESSAGE 5
#define FLOW_CONTROL 50
#define VARIABLE_CONTROL 70
#define DEBUG_SPECIAL 100
#define IO_MESSAGE 110
#define INNER_LOOP 120
/* SCSI commands that the Abaton scanners understand: */
#define TEST_UNIT_READY 0x00
#define REQUEST_SENSE 0x03
#define INQUIRY 0x12
#define START_STOP 0x1b
#define SET_WINDOW 0x24
#define READ_10 0x28
#define WRITE_10 0x2b /* not used, AFAIK */
#define GET_DATA_STATUS 0x34
#define INQ_LEN 0x60
static const uint8_t inquiry[] =
{
INQUIRY, 0x00, 0x00, 0x00, INQ_LEN, 0x00
};
static const uint8_t test_unit_ready[] =
{
TEST_UNIT_READY, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* convenience macros */
#define ENABLE(OPTION) s->opt[OPTION].cap &= ~SANE_CAP_INACTIVE
#define DISABLE(OPTION) s->opt[OPTION].cap |= SANE_CAP_INACTIVE
#define IS_ACTIVE(OPTION) (((s->opt[OPTION].cap) & SANE_CAP_INACTIVE) == 0)
/* store an 8-bit-wide value at the location specified by ptr */
#define STORE8(ptr, val) (*((uint8_t *) ptr) = val)
/* store a 16-bit-wide value in network (big-endian) byte order */
#define STORE16(ptr, val) \
{ \
*((uint8_t *) ptr) = (val >> 8) & 0xff; \
*((uint8_t *) ptr+1) = val & 0xff; \
}
/* store a 24-bit-wide value in network (big-endian) byte order */
#define STORE24(ptr, val) \
{ \
*((uint8_t *) ptr) = (val >> 16) & 0xff; \
*((uint8_t *) ptr+1) = (val >> 8) & 0xff; \
*((uint8_t *) ptr+2) = val & 0xff; \
}
/* store a 32-bit-wide value in network (big-endian) byte order */
#define STORE32(ptr, val) \
{ \
*((uint8_t *) ptr) = (val >> 24) & 0xff; \
*((uint8_t *) ptr+1) = (val >> 16) & 0xff; \
*((uint8_t *) ptr+2) = (val >> 8) & 0xff; \
*((uint8_t *) ptr+3) = val & 0xff; \
}
/* retrieve a 24-bit-wide big-endian value at ptr */
#define GET24(ptr) \
(*((uint8_t *) ptr) << 16) + \
(*((uint8_t *) ptr+1) << 8) + \
(*((uint8_t *) ptr+2))
static SANE_Status
wait_ready (int fd)
{
#define MAX_WAITING_TIME 60 /* one minute, at most */
struct timeval now, start;
SANE_Status status;
gettimeofday (&start, 0);
while (1)
{
DBG (USER_MESSAGE, "wait_ready: sending TEST_UNIT_READY\n");
status = sanei_scsi_cmd (fd, test_unit_ready, sizeof (test_unit_ready),
0, 0);
switch (status)
{
default:
/* Ignore errors while waiting for scanner to become ready.
Some SCSI drivers return EIO while the scanner is
returning to the home position. */
DBG (ERROR_MESSAGE, "wait_ready: test unit ready failed (%s)\n",
sane_strstatus (status));
/* fall through */
case SANE_STATUS_DEVICE_BUSY:
gettimeofday (&now, 0);
if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
{
DBG (ERROR_MESSAGE, "wait_ready: timed out after %ld seconds\n",
(long) (now.tv_sec - start.tv_sec));
return SANE_STATUS_INVAL;
}
usleep (100000); /* retry after 100ms */
break;
case SANE_STATUS_GOOD:
return status;
}
}
return SANE_STATUS_INVAL;
}
static SANE_Status
sense_handler (int scsi_fd, u_char * result, void *arg)
{
scsi_fd = scsi_fd; /* silence gcc */
arg = arg; /* silence gcc */
switch (result[2] & 0x0F)
{
case 0:
DBG (USER_MESSAGE, "Sense: No sense Error\n");
return SANE_STATUS_GOOD;
case 2:
DBG (ERROR_MESSAGE, "Sense: Scanner not ready\n");
return SANE_STATUS_DEVICE_BUSY;
case 4:
DBG (ERROR_MESSAGE, "Sense: Hardware Error. Read more...\n");
return SANE_STATUS_IO_ERROR;
case 5:
DBG (ERROR_MESSAGE, "Sense: Illegal request\n");
return SANE_STATUS_UNSUPPORTED;
case 6:
DBG (ERROR_MESSAGE, "Sense: Unit Attention (Wait until scanner "
"boots)\n");
return SANE_STATUS_DEVICE_BUSY;
case 9:
DBG (ERROR_MESSAGE, "Sense: Vendor Unique. Read more...\n");
return SANE_STATUS_IO_ERROR;
default:
DBG (ERROR_MESSAGE, "Sense: Unknown Sense Key. Read more...\n");
return SANE_STATUS_IO_ERROR;
}
return SANE_STATUS_GOOD;
}
static SANE_Status
request_sense (Abaton_Scanner * s)
{
uint8_t cmd[6];
uint8_t result[22];
size_t size = sizeof (result);
SANE_Status status;
memset (cmd, 0, sizeof (cmd));
memset (result, 0, sizeof (result));
cmd[0] = REQUEST_SENSE;
STORE8 (cmd + 4, sizeof (result));
sanei_scsi_cmd (s->fd, cmd, sizeof (cmd), result, &size);
if (result[7] != 14)
{
DBG (ERROR_MESSAGE, "Additional Length %u\n", (unsigned int) result[7]);
status = SANE_STATUS_IO_ERROR;
}
status = sense_handler (s->fd, result, NULL);
if (status == SANE_STATUS_IO_ERROR)
{
/* Since I haven't figured out the vendor unique error codes on
this thing, I'll just handle the normal ones for now */
if (result[18] & 0x80)
DBG (ERROR_MESSAGE, "Sense: Dim Light (output of lamp below 70%%).\n");
if (result[18] & 0x40)
DBG (ERROR_MESSAGE, "Sense: No Light at all.\n");
if (result[18] & 0x20)
DBG (ERROR_MESSAGE, "Sense: No Home.\n");
if (result[18] & 0x10)
DBG (ERROR_MESSAGE, "Sense: No Limit. Tried to scan out of range.\n");
}
DBG (USER_MESSAGE, "Sense: Optical gain %u.\n", (unsigned int) result[20]);
return status;
}
static SANE_Status
set_window (Abaton_Scanner * s)
{
uint8_t cmd[10 + 40];
uint8_t *window = cmd + 10 + 8;
int invert;
memset (cmd, 0, sizeof (cmd));
cmd[0] = SET_WINDOW;
cmd[8] = 40;
/* Just like the Apple scanners, we put the resolution here */
STORE16 (window + 2, s->val[OPT_X_RESOLUTION].w);
STORE16 (window + 4, s->val[OPT_Y_RESOLUTION].w);
/* Unlike Apple scanners, these are pixel values */
STORE16 (window + 6, s->ULx);
STORE16 (window + 8, s->ULy);
STORE16 (window + 10, s->Width);
STORE16 (window + 12, s->Height);
STORE8 (window + 14, s->val[OPT_BRIGHTNESS].w);
STORE8 (window + 15, s->val[OPT_THRESHOLD].w);
STORE8 (window + 16, s->val[OPT_CONTRAST].w);
invert = s->val[OPT_NEGATIVE].w;
if (!strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_LINEART))
{
STORE8 (window + 17, 0);
}
else if (!strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_HALFTONE))
{
STORE8 (window + 17, 1);
}
else if (!strcmp (s->val[OPT_MODE].s, "Gray256")
|| !strcmp (s->val[OPT_MODE].s, "Gray16"))
{
STORE8 (window + 17, 2);
invert = !s->val[OPT_NEGATIVE].w;
}
else
{
DBG (ERROR_MESSAGE, "Can't match mode %s\n", s->val[OPT_MODE].s);
return SANE_STATUS_INVAL;
}
STORE8 (window + 18, s->bpp);
if (!strcmp (s->val[OPT_HALFTONE_PATTERN].s, "spiral"))
{
STORE8 (window + 20, 0);
}
else if (!strcmp (s->val[OPT_HALFTONE_PATTERN].s, "bayer"))
{
STORE8 (window + 20, 1);
}
else
{
DBG (ERROR_MESSAGE, "Can't match haftone pattern %s\n",
s->val[OPT_HALFTONE_PATTERN].s);
return SANE_STATUS_INVAL;
}
/* We have to invert these ones for some reason, so why not
let the scanner do it for us... */
STORE8 (window + 21, invert ? 0x80 : 0);
STORE16 (window + 22, (s->val[OPT_MIRROR].w != 0));
return sanei_scsi_cmd (s->fd, cmd, sizeof (cmd), 0, 0);
}
static SANE_Status
start_scan (Abaton_Scanner * s)
{
SANE_Status status;
uint8_t start[7];
memset (start, 0, sizeof (start));
start[0] = START_STOP;
start[4] = 1;
status = sanei_scsi_cmd (s->fd, start, sizeof (start), 0, 0);
return status;
}
static SANE_Status
attach (const char *devname, Abaton_Device ** devp, int may_wait)
{
char result[INQ_LEN];
const char *model_name = result + 44;
int fd, abaton_scanner;
Abaton_Device *dev;
SANE_Status status;
size_t size;
for (dev = first_dev; dev; dev = dev->next)
if (strcmp (dev->sane.name, devname) == 0)
{
if (devp)
*devp = dev;
return SANE_STATUS_GOOD;
}
DBG (USER_MESSAGE, "attach: opening %s\n", devname);
status = sanei_scsi_open (devname, &fd, sense_handler, 0);
if (status != SANE_STATUS_GOOD)
{
DBG (ERROR_MESSAGE, "attach: open failed (%s)\n",
sane_strstatus (status));
return SANE_STATUS_INVAL;
}
if (may_wait)
wait_ready (fd);
DBG (USER_MESSAGE, "attach: sending INQUIRY\n");
size = sizeof (result);
status = sanei_scsi_cmd (fd, inquiry, sizeof (inquiry), result, &size);
if (status != SANE_STATUS_GOOD)
{
DBG (ERROR_MESSAGE, "attach: inquiry failed (%s)\n",
sane_strstatus (status));
sanei_scsi_close (fd);
return status;
}
status = wait_ready (fd);
sanei_scsi_close (fd);
if (status != SANE_STATUS_GOOD)
return status;
/* check that we've got an Abaton */
abaton_scanner = (strncmp (result + 8, "ABATON ", 8) == 0);
model_name = result + 16;
/* make sure it's a scanner ;-) */
abaton_scanner = abaton_scanner && (result[0] == 0x06);
if (!abaton_scanner)
{
DBG (ERROR_MESSAGE, "attach: device doesn't look like an Abaton scanner "
"(result[0]=%#02x)\n", result[0]);
return SANE_STATUS_INVAL;
}
dev = malloc (sizeof (*dev));
if (!dev)
return SANE_STATUS_NO_MEM;
memset (dev, 0, sizeof (*dev));
dev->sane.name = strdup (devname);
dev->sane.vendor = "Abaton";
dev->sane.model = strndup (model_name, 16);
dev->sane.type = "flatbed scanner";
if (!strncmp (model_name, "SCAN 300/GS", 11))
{
dev->ScannerModel = ABATON_300GS;
}
else if (!strncmp (model_name, "SCAN 300/S", 10))
{
dev->ScannerModel = ABATON_300S;
}
DBG (USER_MESSAGE, "attach: found Abaton scanner model %s (%s)\n",
dev->sane.model, dev->sane.type);
++num_devices;
dev->next = first_dev;
first_dev = dev;
if (devp)
*devp = dev;
return SANE_STATUS_GOOD;
}
static SANE_Status
attach_one (const char *devname)
{
return attach (devname, 0, SANE_FALSE);
}
static SANE_Status
calc_parameters (Abaton_Scanner * s)
{
SANE_String val = s->val[OPT_MODE].s;
SANE_Status status = SANE_STATUS_GOOD;
SANE_Int dpix = s->val[OPT_X_RESOLUTION].w;
SANE_Int dpiy = s->val[OPT_Y_RESOLUTION].w;
double ulx, uly, width, height;
DBG (FLOW_CONTROL, "Entering calc_parameters\n");
if (!strcmp (val, SANE_VALUE_SCAN_MODE_LINEART) || !strcmp (val, SANE_VALUE_SCAN_MODE_HALFTONE))
{
s->params.depth = 1;
s->bpp = 1;
}
else if (!strcmp (val, "Gray16"))
{
s->params.depth = 8;
s->bpp = 4;
}
else if (!strcmp (val, "Gray256"))
{
s->params.depth = 8;
s->bpp = 8;
}
else
{
DBG (ERROR_MESSAGE, "calc_parameters: Invalid mode %s\n", (char *) val);
status = SANE_STATUS_INVAL;
}
/* in inches */
ulx = (double) s->val[OPT_TL_X].w / MM_PER_INCH;
uly = (double) s->val[OPT_TL_Y].w / MM_PER_INCH;
width = (double) s->val[OPT_BR_X].w / MM_PER_INCH - ulx;
height = (double) s->val[OPT_BR_Y].w / MM_PER_INCH - uly;
DBG (VARIABLE_CONTROL, "(inches) ulx: %f, uly: %f, width: %f, height: %f\n",
ulx, uly, width, height);
/* turn 'em into pixel quantities */
s->ULx = ulx * dpix;
s->ULy = uly * dpiy;
s->Width = width * dpix;
s->Height = height * dpiy;
DBG (VARIABLE_CONTROL, "(pixels) ulx: %d, uly: %d, width: %d, height: %d\n",
s->ULx, s->ULy, s->Width, s->Height);
/* floor width to a byte multiple */
if ((s->Width * s->bpp) % 8)
{
s->Width /= 8;
s->Width *= 8;
DBG (VARIABLE_CONTROL, "Adapting to width %d\n", s->Width);
}
s->params.pixels_per_line = s->Width;
s->params.lines = s->Height;
s->params.bytes_per_line = s->params.pixels_per_line * s->params.depth / 8;
DBG (VARIABLE_CONTROL, "format=%d\n", s->params.format);
DBG (VARIABLE_CONTROL, "last_frame=%d\n", s->params.last_frame);
DBG (VARIABLE_CONTROL, "lines=%d\n", s->params.lines);
DBG (VARIABLE_CONTROL, "depth=%d (%d)\n", s->params.depth, s->bpp);
DBG (VARIABLE_CONTROL, "pixels_per_line=%d\n", s->params.pixels_per_line);
DBG (VARIABLE_CONTROL, "bytes_per_line=%d\n", s->params.bytes_per_line);
DBG (VARIABLE_CONTROL, "Pixels %dx%dx%d\n",
s->params.pixels_per_line, s->params.lines, 1 << s->params.depth);
DBG (FLOW_CONTROL, "Leaving calc_parameters\n");
return status;
}
static SANE_Status
mode_update (SANE_Handle handle, char *val)
{
Abaton_Scanner *s = handle;
if (!strcmp (val, SANE_VALUE_SCAN_MODE_LINEART))
{
DISABLE (OPT_BRIGHTNESS);
DISABLE (OPT_CONTRAST);
ENABLE (OPT_THRESHOLD);
DISABLE (OPT_HALFTONE_PATTERN);
}
else if (!strcmp (val, SANE_VALUE_SCAN_MODE_HALFTONE))
{
ENABLE (OPT_BRIGHTNESS);
ENABLE (OPT_CONTRAST);
DISABLE (OPT_THRESHOLD);
ENABLE (OPT_HALFTONE_PATTERN);
}
else if (!strcmp (val, "Gray16") || !strcmp (val, "Gray256"))
{
ENABLE (OPT_BRIGHTNESS);
ENABLE (OPT_CONTRAST);
DISABLE (OPT_THRESHOLD);
DISABLE (OPT_HALFTONE_PATTERN);
} /* End of Gray */
else
{
DBG (ERROR_MESSAGE, "Invalid mode %s\n", (char *) val);
return SANE_STATUS_INVAL;
}
calc_parameters (s);
return SANE_STATUS_GOOD;
}
/* find the longest of a list of strings */
static size_t
max_string_size (const SANE_String_Const strings[])
{
size_t size, max_size = 0;
int i;
for (i = 0; strings[i]; ++i)
{
size = strlen (strings[i]) + 1;
if (size > max_size)
max_size = size;
}
return max_size;
}
static SANE_Status
init_options (Abaton_Scanner * s)
{
int i;
memset (s->opt, 0, sizeof (s->opt));
memset (s->val, 0, sizeof (s->val));
for (i = 0; i < NUM_OPTIONS; ++i)
{
s->opt[i].size = sizeof (SANE_Word);
s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
}
s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
s->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
/* "Mode" group: */
s->opt[OPT_MODE_GROUP].title = "Scan Mode";
s->opt[OPT_MODE_GROUP].desc = "";
s->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
s->opt[OPT_MODE_GROUP].cap = 0;
s->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
mode_list[0]=SANE_VALUE_SCAN_MODE_LINEART;
switch (s->hw->ScannerModel)
{
case ABATON_300GS:
mode_list[1]=SANE_VALUE_SCAN_MODE_HALFTONE;
mode_list[2]="Gray16";
mode_list[3]="Gray256";
mode_list[4]=NULL;
break;
case ABATON_300S:
default:
mode_list[1]=NULL;
break;
}
/* scan mode */
s->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
s->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
s->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
s->opt[OPT_MODE].type = SANE_TYPE_STRING;
s->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
s->opt[OPT_MODE].size = max_string_size (mode_list);
s->opt[OPT_MODE].constraint.string_list = mode_list;
s->val[OPT_MODE].s = strdup (mode_list[0]);
/* resolution - horizontal */
s->opt[OPT_X_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
s->opt[OPT_X_RESOLUTION].title = SANE_TITLE_SCAN_X_RESOLUTION;
s->opt[OPT_X_RESOLUTION].desc = SANE_DESC_SCAN_X_RESOLUTION;
s->opt[OPT_X_RESOLUTION].type = SANE_TYPE_INT;
s->opt[OPT_X_RESOLUTION].unit = SANE_UNIT_DPI;
s->opt[OPT_X_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_X_RESOLUTION].constraint.range = &dpi_range;
s->val[OPT_X_RESOLUTION].w = 150;
/* resolution - vertical */
s->opt[OPT_Y_RESOLUTION].name = SANE_NAME_SCAN_Y_RESOLUTION;
s->opt[OPT_Y_RESOLUTION].title = SANE_TITLE_SCAN_Y_RESOLUTION;
s->opt[OPT_Y_RESOLUTION].desc = SANE_DESC_SCAN_Y_RESOLUTION;
s->opt[OPT_Y_RESOLUTION].type = SANE_TYPE_INT;
s->opt[OPT_Y_RESOLUTION].unit = SANE_UNIT_DPI;
s->opt[OPT_Y_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_Y_RESOLUTION].constraint.range = &dpi_range;
s->val[OPT_Y_RESOLUTION].w = 150;
/* constrain resolutions */
s->opt[OPT_RESOLUTION_BIND].name = SANE_NAME_RESOLUTION_BIND;
s->opt[OPT_RESOLUTION_BIND].title = SANE_TITLE_RESOLUTION_BIND;
s->opt[OPT_RESOLUTION_BIND].desc = SANE_DESC_RESOLUTION_BIND;
s->opt[OPT_RESOLUTION_BIND].type = SANE_TYPE_BOOL;
s->opt[OPT_RESOLUTION_BIND].unit = SANE_UNIT_NONE;
s->opt[OPT_RESOLUTION_BIND].constraint_type = SANE_CONSTRAINT_NONE;
/* until I fix it */
s->val[OPT_RESOLUTION_BIND].w = SANE_FALSE;
/* preview mode */
s->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
s->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
s->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
s->opt[OPT_PREVIEW].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
s->opt[OPT_PREVIEW].type = SANE_TYPE_BOOL;
s->val[OPT_PREVIEW].w = SANE_FALSE;
/* halftone pattern */
s->opt[OPT_HALFTONE_PATTERN].name = SANE_NAME_HALFTONE_PATTERN;
s->opt[OPT_HALFTONE_PATTERN].title = SANE_TITLE_HALFTONE_PATTERN;
s->opt[OPT_HALFTONE_PATTERN].desc = SANE_DESC_HALFTONE_PATTERN;
s->opt[OPT_HALFTONE_PATTERN].size = max_string_size (halftone_pattern_list);
s->opt[OPT_HALFTONE_PATTERN].type = SANE_TYPE_STRING;
s->opt[OPT_HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_HALFTONE_PATTERN].constraint_type = SANE_CONSTRAINT_STRING_LIST;
s->opt[OPT_HALFTONE_PATTERN].constraint.string_list = halftone_pattern_list;
s->val[OPT_HALFTONE_PATTERN].s = strdup (halftone_pattern_list[0]);
/* "Geometry" group: */
s->opt[OPT_GEOMETRY_GROUP].title = "Geometry";
s->opt[OPT_GEOMETRY_GROUP].desc = "";
s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* top-left x */
s->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
s->opt[OPT_TL_X].type = SANE_TYPE_INT;
s->opt[OPT_TL_X].unit = SANE_UNIT_MM;
s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_TL_X].constraint.range = &x_range;
s->val[OPT_TL_X].w = 0;
/* top-left y */
s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
s->opt[OPT_TL_Y].type = SANE_TYPE_INT;
s->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_TL_Y].constraint.range = &y_range;
s->val[OPT_TL_Y].w = 0;
/* bottom-right x */
s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
s->opt[OPT_BR_X].type = SANE_TYPE_INT;
s->opt[OPT_BR_X].unit = SANE_UNIT_MM;
s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_BR_X].constraint.range = &x_range;
s->val[OPT_BR_X].w = x_range.max;
/* bottom-right y */
s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
s->opt[OPT_BR_Y].type = SANE_TYPE_INT;
s->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_BR_Y].constraint.range = &y_range;
s->val[OPT_BR_Y].w = y_range.max;
/* "Enhancement" group: */
s->opt[OPT_ENHANCEMENT_GROUP].title = "Enhancement";
s->opt[OPT_ENHANCEMENT_GROUP].desc = "";
s->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
s->opt[OPT_ENHANCEMENT_GROUP].cap = 0;
s->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
/* brightness */
s->opt[OPT_BRIGHTNESS].name = SANE_NAME_BRIGHTNESS;
s->opt[OPT_BRIGHTNESS].title = SANE_TITLE_BRIGHTNESS;
s->opt[OPT_BRIGHTNESS].desc = SANE_DESC_BRIGHTNESS;
s->opt[OPT_BRIGHTNESS].type = SANE_TYPE_INT;
s->opt[OPT_BRIGHTNESS].unit = SANE_UNIT_NONE;
s->opt[OPT_BRIGHTNESS].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_BRIGHTNESS].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_BRIGHTNESS].constraint.range = &enhance_range;
s->val[OPT_BRIGHTNESS].w = 150;
/* contrast */
s->opt[OPT_CONTRAST].name = SANE_NAME_CONTRAST;
s->opt[OPT_CONTRAST].title = SANE_TITLE_CONTRAST;
s->opt[OPT_CONTRAST].desc = SANE_DESC_CONTRAST;
s->opt[OPT_CONTRAST].type = SANE_TYPE_INT;
s->opt[OPT_CONTRAST].unit = SANE_UNIT_NONE;
s->opt[OPT_CONTRAST].cap |= SANE_CAP_INACTIVE;
s->opt[OPT_CONTRAST].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_CONTRAST].constraint.range = &enhance_range;
s->val[OPT_CONTRAST].w = 150;
/* threshold */
s->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD;
s->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD;
s->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD;
s->opt[OPT_THRESHOLD].type = SANE_TYPE_INT;
s->opt[OPT_THRESHOLD].unit = SANE_UNIT_NONE;
s->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE;
s->opt[OPT_THRESHOLD].constraint.range = &enhance_range;
s->val[OPT_THRESHOLD].w = 150;
/* negative */
s->opt[OPT_NEGATIVE].name = SANE_NAME_NEGATIVE;
s->opt[OPT_NEGATIVE].title = SANE_TITLE_NEGATIVE;
s->opt[OPT_NEGATIVE].desc = SANE_DESC_NEGATIVE;
s->opt[OPT_NEGATIVE].type = SANE_TYPE_BOOL;
s->opt[OPT_NEGATIVE].unit = SANE_UNIT_NONE;
s->opt[OPT_NEGATIVE].constraint_type = SANE_CONSTRAINT_NONE;
s->val[OPT_NEGATIVE].w = SANE_FALSE;
/* mirror-image */
s->opt[OPT_MIRROR].name = "mirror";
s->opt[OPT_MIRROR].title = "Mirror Image";
s->opt[OPT_MIRROR].desc = "Scan in mirror-image";
s->opt[OPT_MIRROR].type = SANE_TYPE_BOOL;
s->opt[OPT_MIRROR].unit = SANE_UNIT_NONE;
s->opt[OPT_MIRROR].constraint_type = SANE_CONSTRAINT_NONE;
s->val[OPT_MIRROR].w = SANE_FALSE;
return SANE_STATUS_GOOD;
}
SANE_Status
sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
{
char dev_name[PATH_MAX];
size_t len;
FILE *fp;
authorize = authorize; /* silence gcc */
DBG_INIT ();
if (version_code)
*version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, 0);
fp = sanei_config_open (ABATON_CONFIG_FILE);
if (!fp)
{
/* default to /dev/scanner instead of insisting on config file */
attach ("/dev/scanner", 0, SANE_FALSE);
return SANE_STATUS_GOOD;
}
while (sanei_config_read (dev_name, sizeof (dev_name), fp))
{
if (dev_name[0] == '#') /* ignore line comments */
continue;
len = strlen (dev_name);
if (!len)
continue; /* ignore empty lines */
if (strncmp (dev_name, "option", 6) == 0
&& isspace (dev_name[6]))
{
const char *str = dev_name + 7;
while (isspace (*str))
++str;
continue;
}
sanei_config_attach_matching_devices (dev_name, attach_one);
}
fclose (fp);
return SANE_STATUS_GOOD;
}
void
sane_exit (void)
{
Abaton_Device *dev, *next;
for (dev = first_dev; dev; dev = next)
{
next = dev->next;
free ((void *) dev->sane.name);
free ((void *) dev->sane.model);
free (dev);
}
if (devlist)
free (devlist);
}
SANE_Status
sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
{
Abaton_Device *dev;
int i;
local_only = local_only; /* silence gcc */
if (devlist)
free (devlist);
devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
if (!devlist)
return SANE_STATUS_NO_MEM;
i = 0;
for (dev = first_dev; i < num_devices; dev = dev->next)
devlist[i++] = &dev->sane;
devlist[i++] = 0;
*device_list = devlist;
return SANE_STATUS_GOOD;
}
SANE_Status
sane_open (SANE_String_Const devicename, SANE_Handle * handle)
{
Abaton_Device *dev;
SANE_Status status;
Abaton_Scanner *s;
if (devicename[0])
{
for (dev = first_dev; dev; dev = dev->next)
if (strcmp (dev->sane.name, devicename) == 0)
break;
if (!dev)
{
status = attach (devicename, &dev, SANE_TRUE);
if (status != SANE_STATUS_GOOD)
return status;
}
}
else
/* empty devicname -> use first device */
dev = first_dev;
if (!dev)
return SANE_STATUS_INVAL;
s = malloc (sizeof (*s));
if (!s)
return SANE_STATUS_NO_MEM;
memset (s, 0, sizeof (*s));
s->fd = -1;
s->hw = dev;
init_options (s);
/* set up some universal parameters */
s->params.last_frame = SANE_TRUE;
s->params.format = SANE_FRAME_GRAY;
/* insert newly opened handle into list of open handles: */
s->next = first_handle;
first_handle = s;
*handle = s;
return SANE_STATUS_GOOD;
}
void
sane_close (SANE_Handle handle)
{
Abaton_Scanner *prev, *s;
/* remove handle from list of open handles: */
prev = 0;
for (s = first_handle; s; s = s->next)
{
if (s == (Abaton_Scanner *) handle)
break;
prev = s;
}
if (!s)
{
DBG (ERROR_MESSAGE, "close: invalid handle %p\n", handle);
return; /* oops, not a handle we know about */
}
if (prev)
prev->next = s->next;
else
first_handle = s->next;
free (handle);
}
const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
Abaton_Scanner *s = handle;
if ((unsigned) option >= NUM_OPTIONS)
return NULL;
return s->opt + option;
}
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
SANE_Action action, void *val, SANE_Int * info)
{
Abaton_Scanner *s = handle;
SANE_Status status;
SANE_Word cap;
if (option < 0 || option >= NUM_OPTIONS)
return SANE_STATUS_INVAL;
if (info != NULL)
*info = 0;
if (s->scanning)
return SANE_STATUS_DEVICE_BUSY;
cap = s->opt[option].cap;
if (!SANE_OPTION_IS_ACTIVE (cap))
return SANE_STATUS_INVAL;
if (action == SANE_ACTION_GET_VALUE)
{
switch (option)
{
/* word options: */
case OPT_NUM_OPTS:
case OPT_X_RESOLUTION:
case OPT_Y_RESOLUTION:
case OPT_RESOLUTION_BIND:
case OPT_PREVIEW:
case OPT_TL_X:
case OPT_TL_Y:
case OPT_BR_X:
case OPT_BR_Y:
case OPT_BRIGHTNESS:
case OPT_CONTRAST:
case OPT_THRESHOLD:
case OPT_NEGATIVE:
case OPT_MIRROR:
*(SANE_Word *) val = s->val[option].w;
return SANE_STATUS_GOOD;
/* string options */
case OPT_MODE:
case OPT_HALFTONE_PATTERN:
status = sanei_constrain_value (s->opt + option, s->val[option].s,
info);
strcpy (val, s->val[option].s);
return SANE_STATUS_GOOD;
}
}
else if (action == SANE_ACTION_SET_VALUE)
{
if (!SANE_OPTION_IS_SETTABLE (cap))
return SANE_STATUS_INVAL;
status = sanei_constrain_value (s->opt + option, val, info);
if (status != SANE_STATUS_GOOD)
return status;
switch (option)
{
/* resolution should be uniform for previews, or when the
user says so. */
case OPT_PREVIEW:
s->val[option].w = *(SANE_Word *) val;
if (*(SANE_Word *) val) {
s->val[OPT_Y_RESOLUTION].w = s->val[OPT_X_RESOLUTION].w;
if (info)
*info |= SANE_INFO_RELOAD_OPTIONS;
}
/* always recalculate! */
calc_parameters (s);
if (info)
*info |= SANE_INFO_RELOAD_PARAMS;
return SANE_STATUS_GOOD;
case OPT_RESOLUTION_BIND:
s->val[option].w = *(SANE_Word *) val;
if (*(SANE_Word *) val) {
s->val[OPT_Y_RESOLUTION].w = s->val[OPT_X_RESOLUTION].w;
calc_parameters (s);
if (info)
*info |= SANE_INFO_RELOAD_PARAMS |
SANE_INFO_RELOAD_OPTIONS;
}
return SANE_STATUS_GOOD;
case OPT_X_RESOLUTION:
if (s->val[OPT_PREVIEW].w || s->val[OPT_RESOLUTION_BIND].w) {
s->val[OPT_Y_RESOLUTION].w = *(SANE_Word *)val;
if (info)
*info |= SANE_INFO_RELOAD_OPTIONS;
}
s->val[option].w = *(SANE_Word *) val;
calc_parameters (s);
if (info)
*info |= SANE_INFO_RELOAD_PARAMS;
return SANE_STATUS_GOOD;
case OPT_Y_RESOLUTION:
if (s->val[OPT_PREVIEW].w || s->val[OPT_RESOLUTION_BIND].w) {
s->val[OPT_X_RESOLUTION].w = *(SANE_Word *)val;
if (info)
*info |= SANE_INFO_RELOAD_OPTIONS;
}
s->val[option].w = *(SANE_Word *) val;
calc_parameters (s);
if (info)
*info |= SANE_INFO_RELOAD_PARAMS;
return SANE_STATUS_GOOD;
/* these ones don't have crazy side effects */
case OPT_TL_X:
case OPT_TL_Y:
case OPT_BR_Y:
s->val[option].w = *(SANE_Word *) val;
calc_parameters (s);
if (info)
*info |= SANE_INFO_RELOAD_PARAMS;
return SANE_STATUS_GOOD;
/* this one is somewhat imprecise */
case OPT_BR_X:
s->val[option].w = *(SANE_Word *) val;
calc_parameters (s);
if (info)
*info |= SANE_INFO_RELOAD_PARAMS
| SANE_INFO_INEXACT;
return SANE_STATUS_GOOD;
/* no side-effects whatsoever */
case OPT_BRIGHTNESS:
case OPT_CONTRAST:
case OPT_THRESHOLD:
case OPT_NEGATIVE:
case OPT_MIRROR:
s->val[option].w = *(SANE_Word *) val;
return SANE_STATUS_GOOD;
/* string options */
case OPT_HALFTONE_PATTERN:
if (info)
*info |= SANE_INFO_RELOAD_OPTIONS;
if (s->val[option].s)
free (s->val[option].s);
s->val[option].s = strdup (val);
return SANE_STATUS_GOOD;
case OPT_MODE:
status = mode_update (s, val);
if (status != SANE_STATUS_GOOD)
return status;
if (s->val[option].s)
free (s->val[option].s);
s->val[option].s = strdup (val);
if (info)
*info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
return SANE_STATUS_GOOD;
} /* End of switch */
} /* End of SET_VALUE */
return SANE_STATUS_INVAL;
}
SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
Abaton_Scanner *s = handle;
DBG (FLOW_CONTROL, "Entering sane_get_parameters\n");
calc_parameters (s);
if (params)
*params = s->params;
return SANE_STATUS_GOOD;
}
SANE_Status
sane_start (SANE_Handle handle)
{
Abaton_Scanner *s = handle;
SANE_Status status;
/* First make sure we have a current parameter set. Some of the
parameters will be overwritten below, but that's OK. */
calc_parameters (s);
if (s->fd < 0)
{
/* this is the first (and maybe only) pass... */
status = sanei_scsi_open (s->hw->sane.name, &s->fd, sense_handler, 0);
if (status != SANE_STATUS_GOOD)
{
DBG (ERROR_MESSAGE, "open: open of %s failed: %s\n",
s->hw->sane.name, sane_strstatus (status));
return status;
}
}
status = wait_ready (s->fd);
if (status != SANE_STATUS_GOOD)
{
DBG (ERROR_MESSAGE, "open: wait_ready() failed: %s\n",
sane_strstatus (status));
goto stop_scanner_and_return;
}
status = request_sense (s);
if (status != SANE_STATUS_GOOD)
{
DBG (ERROR_MESSAGE, "sane_start: request_sense revealed error: %s\n",
sane_strstatus (status));
goto stop_scanner_and_return;
}
status = set_window (s);
if (status != SANE_STATUS_GOOD)
{
DBG (ERROR_MESSAGE, "open: set scan area command failed: %s\n",
sane_strstatus (status));
goto stop_scanner_and_return;
}
s->scanning = SANE_TRUE;
s->AbortedByUser = SANE_FALSE;
status = start_scan (s);
if (status != SANE_STATUS_GOOD)
goto stop_scanner_and_return;
return SANE_STATUS_GOOD;
stop_scanner_and_return:
s->scanning = SANE_FALSE;
s->AbortedByUser = SANE_FALSE;
return status;
}
SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
SANE_Int * len)
{
Abaton_Scanner *s = handle;
SANE_Status status;
uint8_t get_data_status[10];
uint8_t read[10];
uint8_t result[12];
size_t size;
SANE_Int data_av = 0;
SANE_Int data_length = 0;
SANE_Int offset = 0;
SANE_Int rread = 0;
SANE_Bool Pseudo8bit = SANE_FALSE;
*len = 0;
/* don't let bogus read requests reach the scanner */
/* this is a sub-optimal way of doing this, I'm sure */
if (!s->scanning)
return SANE_STATUS_EOF;
if (!strcmp (s->val[OPT_MODE].s, "Gray16"))
Pseudo8bit = SANE_TRUE;
memset (get_data_status, 0, sizeof (get_data_status));
get_data_status[0] = GET_DATA_STATUS;
/* This means "block" for Apple scanners, it seems to be the same
for Abaton. The scanner will do non-blocking I/O, but I don't
want to go there right now. */
get_data_status[1] = 1;
STORE8 (get_data_status + 8, sizeof (result));
memset (read, 0, sizeof (read));
read[0] = READ_10;
do
{
size = sizeof (result);
/* this isn't necessary */
/* memset (result, 0, size); */
status = sanei_scsi_cmd (s->fd, get_data_status,
sizeof (get_data_status), result, &size);
if (status != SANE_STATUS_GOOD)
return status;
if (!size)
{
DBG (ERROR_MESSAGE, "sane_read: cannot get_data_status.\n");
return SANE_STATUS_IO_ERROR;
}
/* this is not an accurate name, but oh well... */
data_length = GET24 (result);
data_av = GET24 (result + 9);
/* don't check result[3] here, because that screws things up
somewhat */
if (data_length) {
DBG (IO_MESSAGE,
"sane_read: (status) Available in scanner buffer %u.\n",
data_av);
if (Pseudo8bit)
{
if ((data_av * 2) + offset > max_len)
rread = (max_len - offset) / 2;
else
rread = data_av;
}
else if (data_av + offset > max_len)
{
rread = max_len - offset;
}
else
{
rread = data_av;
}
DBG (IO_MESSAGE,
"sane_read: (action) Actual read request for %u bytes.\n",
rread);
size = rread;
STORE24 (read + 6, rread);
status = sanei_scsi_cmd (s->fd, read, sizeof (read),
buf + offset, &size);
if (Pseudo8bit)
{
SANE_Int byte;
SANE_Int pos = offset + (rread << 1) - 1;
SANE_Byte B;
for (byte = offset + rread - 1; byte >= offset; byte--)
{
B = buf[byte];
/* don't invert these! */
buf[pos--] = B << 4; /* low (right) nibble */
buf[pos--] = B & 0xF0; /* high (left) nibble */
}
/* putting an end to bitop abuse here */
offset += size * 2;
}
else
offset += size;
DBG (IO_MESSAGE, "sane_read: Buffer %u of %u full %g%%\n",
offset, max_len, (double) (offset * 100. / max_len));
}
}
while (offset < max_len && data_length != 0 && !s->AbortedByUser);
if (s->AbortedByUser)
{
s->scanning = SANE_FALSE;
if (status != SANE_STATUS_GOOD)
{
DBG (ERROR_MESSAGE, "sane_read: request_sense revealed error: %s\n",
sane_strstatus (status));
return status;
}
status = sanei_scsi_cmd (s->fd, test_unit_ready,
sizeof (test_unit_ready), 0, 0);
if (status != SANE_STATUS_GOOD || status != SANE_STATUS_INVAL)
return status;
return SANE_STATUS_CANCELLED;
}
if (!data_length)
{
s->scanning = SANE_FALSE;
DBG (IO_MESSAGE, "sane_read: (status) No more data...");
if (!offset)
{
/* this shouldn't happen */
*len = 0;
DBG (IO_MESSAGE, "EOF\n");
return SANE_STATUS_EOF;
}
else
{
*len = offset;
DBG (IO_MESSAGE, "GOOD\n");
return SANE_STATUS_GOOD;
}
}
DBG (FLOW_CONTROL,
"sane_read: Normal Exiting, Aborted=%u, data_length=%u\n",
s->AbortedByUser, data_av);
*len = offset;
return SANE_STATUS_GOOD;
}
void
sane_cancel (SANE_Handle handle)
{
Abaton_Scanner *s = handle;
if (s->scanning)
{
if (s->AbortedByUser)
{
DBG (FLOW_CONTROL,
"sane_cancel: Already Aborted. Please Wait...\n");
}
else
{
s->scanning = SANE_FALSE;
s->AbortedByUser = SANE_TRUE;
DBG (FLOW_CONTROL, "sane_cancel: Signal Caught! Aborting...\n");
}
}
else
{
if (s->AbortedByUser)
{
DBG (FLOW_CONTROL, "sane_cancel: Scan has not been initiated yet."
"we probably received a signal while writing data.\n");
s->AbortedByUser = SANE_FALSE;
}
else
{
DBG (FLOW_CONTROL, "sane_cancel: Scan has not been initiated "
"yet (or it's over).\n");
}
}
return;
}
SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
handle = handle; /* silence gcc */
non_blocking = non_blocking; /* silence gcc */
DBG (FLOW_CONTROL, "sane_set_io_mode: Don't call me please. "
"Unimplemented function\n");
return SANE_STATUS_UNSUPPORTED;
}
SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
{
handle = handle; /* silence gcc */
fd = fd; /* silence gcc */
DBG (FLOW_CONTROL, "sane_get_select_fd: Don't call me please. "
"Unimplemented function\n");
return SANE_STATUS_UNSUPPORTED;
}
|