summaryrefslogtreecommitdiff
path: root/backend/kvs40xx_opt.c
blob: 2bf9a5cfe4bc0c867f7a8a58a37eeff497d3a63a (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
/*
   Copyright (C) 2009, Panasonic Russia Ltd.
   Copyright (C) 2010,2011, m. allan noah
*/
/*
   Panasonic KV-S40xx USB-SCSI scanner driver.
*/

#include "../include/sane/config.h"

#include <string.h>
#define DEBUG_DECLARE_ONLY
#define BACKEND_NAME kvs40xx

#include "../include/sane/sanei_backend.h"
#include "../include/sane/saneopts.h"
#include "../include/sane/sanei.h"
#include "../include/sane/sanei_config.h"
#include "lassert.h"

#include "kvs40xx.h"

#include "../include/sane/sanei_debug.h"

#include <stdlib.h>

static inline unsigned
mm2scanner_units (unsigned mm)
{
  return (mm * 12000 / 254.0 + .5);
}
static inline unsigned
scanner_units2mm (unsigned u)
{
  return (u * 254.0 / 12000 + .5);
}
struct restriction
{
  unsigned ux, uy, ux_pix, uy_pix;
};

static struct restriction flatbad = { 14064, 20400, 7031, 63999 };
static struct restriction cw = { 14268, 128000, 7133, 63999 };
static struct restriction cl = { 10724, 128000, 5361, 63999 };

static inline int
check_area (struct scanner *s, unsigned ux,
	    unsigned uy, unsigned bx, unsigned by)
{
  int fb = !strcmp (s->val[SOURCE].s, SANE_I18N ("fb"));
  struct restriction *r = fb ? &flatbad
    : (s->id == KV_S4085CL || s->id == KV_S4065CL) ? &cl : &cw;
  unsigned res = s->val[RESOLUTION].w;
  unsigned w = bx - ux;
  unsigned h = by - uy;
  unsigned c1 = mm2scanner_units (ux + w);
  unsigned c2 = mm2scanner_units (uy + h);
  int c = c1 <= r->ux && c1 >= 16 && c2 >= 1 && c2 <= r->uy ? 0 : -1;
  if (c)
    return c;
  if (mm2scanner_units (ux) > r->ux)
    return -1;
  if (res * mm2scanner_units (ux) / 1200 > r->ux_pix)
    return -1;

  if (res * mm2scanner_units (uy) / 1200 > r->uy_pix)
    return -1;
  return 0;
}

static size_t
max_string_size (const SANE_String_Const strings[])
{
  size_t size, max_size = 0;
  SANE_Int i;

  for (i = 0; strings[i]; ++i)
    {
      size = strlen (strings[i]) + 1;
      if (size > max_size)
	max_size = size;
    }
  return max_size;
}

static SANE_String_Const mode_list[] = {
  SANE_VALUE_SCAN_MODE_LINEART,
  SANE_VALUE_SCAN_MODE_GRAY,
  SANE_VALUE_SCAN_MODE_COLOR,
  NULL
};
static const unsigned mode_val[] = { 0, 2, 5 };
static const unsigned bps_val[] = { 1, 8, 24 };

static const SANE_Range resolutions_range = {
  100,600,1
};

/* List of feeder modes */
static SANE_String_Const feeder_mode_list[] = {
  SANE_I18N ("single"),
  SANE_I18N ("continuous"),
  NULL
};

/* List of scan sources */
static SANE_String_Const source_list[] = {
  SANE_I18N ("adf"),
  SANE_I18N ("fb"),
  NULL
};

/* List of manual feed mode */
static SANE_String_Const manual_feed_list[] = {
  SANE_I18N ("off"),
  SANE_I18N ("wait_doc"),
  SANE_I18N ("wait_doc_hopper_up"),
  SANE_I18N ("wait_key"),
  NULL
};

/* List of paper sizes */
static SANE_String_Const paper_list[] = {
  SANE_I18N ("user_def"),
  SANE_I18N ("business_card"),
  SANE_I18N ("Check"),
  SANE_I18N ("A3"),
  SANE_I18N ("A4"),
  SANE_I18N ("A5"),
  SANE_I18N ("A6"),
  SANE_I18N ("Letter"),
  SANE_I18N ("Double letter 11x17 in"),
  SANE_I18N ("B4"),
  SANE_I18N ("B5"),
  SANE_I18N ("B6"),
  SANE_I18N ("Legal"),
  NULL
};

static SANE_String_Const paper_list_woA3[] = {
  SANE_I18N ("user_def"),
  SANE_I18N ("business_card"),
  SANE_I18N ("Check"),
  /*SANE_I18N ("A3"), */
  SANE_I18N ("A4"),
  SANE_I18N ("A5"),
  SANE_I18N ("A6"),
  SANE_I18N ("Letter"),
  /*SANE_I18N ("Double letter 11x17 in"), */
  /*SANE_I18N ("B4"), */
  SANE_I18N ("B5"),
  SANE_I18N ("B6"),
  SANE_I18N ("Legal"),
  NULL
};

static const unsigned paper_val[] = { 0, 1, 2, 3, 4, 5, 6, 7,
  9, 12, 13, 14, 15
};

struct paper_size
{
  int width;
  int height;
};
static const struct paper_size paper_sizes[] = {
  {210, 297},			/* User defined, default=A4 */
  {54, 90},			/* Business card */
  {80, 170},			/* Check (China business) */
  {297, 420},			/* A3 */
  {210, 297},			/* A4 */
  {148, 210},			/* A5 */
  {105, 148},			/* A6 */
  {215, 280},			/* US Letter 8.5 x 11 in */
  {280, 432},			/* Double Letter 11 x 17 in */
  {250, 353},			/* B4 */
  {176, 250},			/* B5 */
  {125, 176},			/* B6 */
  {215, 355}			/* US Legal */
};

#define MIN_WIDTH	48
#define MIN_LENGTH	70
#define MAX_WIDTH	297
#define MAX_LENGTH	432

#define MAX_WIDTH_A4	227
#define MAX_LENGTH_A4	432

static SANE_Range tl_x_range = { 0, MAX_WIDTH - MIN_WIDTH, 0 };
static SANE_Range tl_y_range = { 0, MAX_LENGTH - MIN_LENGTH, 0 };
static SANE_Range br_x_range = { MIN_WIDTH, MAX_WIDTH, 0 };
static SANE_Range br_y_range = { MIN_LENGTH, MAX_LENGTH, 0 };

static SANE_Range tl_x_range_A4 = { 0, MAX_WIDTH_A4 - MIN_WIDTH, 0 };
static SANE_Range tl_y_range_A4 = { 0, MAX_LENGTH_A4 - MIN_LENGTH, 0 };
static SANE_Range br_x_range_A4 = { MIN_WIDTH, MAX_WIDTH_A4, 0 };
static SANE_Range br_y_range_A4 = { MIN_LENGTH, MAX_LENGTH_A4, 0 };

static SANE_Range byte_value_range = { 0, 255, 0 };
static SANE_Range compression_value_range = { 1, 0x64, 0 };

/* List of image emphasis options, 5 steps */
static SANE_String_Const image_emphasis_list[] = {
  SANE_I18N ("none"),
  SANE_I18N ("low"),
  SANE_I18N ("medium"),
  SANE_I18N ("high"),
  SANE_I18N ("smooth"),
  NULL
};

/* List of gamma */
static SANE_String_Const gamma_list[] = {
  SANE_I18N ("normal"),
  SANE_I18N ("crt"),
  NULL
};
static unsigned gamma_val[] = { 0, 1 };

/* List of lamp color dropout */
static SANE_String_Const lamp_list[] = {
  SANE_I18N ("normal"),
  SANE_I18N ("red"),
  SANE_I18N ("green"),
  SANE_I18N ("blue"),
  NULL
};
static SANE_String_Const dfeed_sence_list[] = {
  SANE_I18N ("Normal"),
  SANE_I18N ("High sensivity"),
  SANE_I18N ("Low sensivity"),
  NULL
};

/* Lists of supported halftone. They are only valid with
 * for the Black&White mode. */
static SANE_String_Const halftone_pattern[] = {
  SANE_I18N ("bayer_64"),
  SANE_I18N ("bayer_16"),
  SANE_I18N ("halftone_32"),
  SANE_I18N ("halftone_64"),
  SANE_I18N ("err_diffusion"),
  NULL
};

/*  Stapled document */
static SANE_String_Const stapeled_list[] = {
  SANE_I18N ("No detection"),
  SANE_I18N ("Normal mode"),
  SANE_I18N ("Enhanced mode"),
  NULL
};


/* List of automatic threshold options */
static SANE_String_Const automatic_threshold_list[] = {
  SANE_I18N ("normal"),
  SANE_I18N ("light"),
  SANE_I18N ("dark"),
  NULL
};
static const int automatic_threshold_val[] = {
  0,
  0x11,
  0x1f
};

/* List of white level base. */
static SANE_String_Const white_level_list[] = {
  SANE_I18N ("From scanner"),
  SANE_I18N ("From paper"),
  SANE_I18N ("Automatic"),
  NULL
};
static const int white_level_val[] = {
  0x00,
  0x80,
  0x81
};

/* List of noise reduction options. */
static SANE_String_Const noise_reduction_list[] = {
  SANE_I18N ("default"),
  "1x1",
  "2x2",
  "3x3",
  "4x4",
  "5x5",
  NULL
};

/* Reset the options for that scanner. */
void
kvs40xx_init_options (struct scanner *s)
{
  int i;
  SANE_Option_Descriptor *o;
  /* Pre-initialize the options. */
  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;
    }

  /* Number of options. */
  o = &s->opt[NUM_OPTS];
  o->name = "";
  o->title = SANE_TITLE_NUM_OPTIONS;
  o->desc = SANE_DESC_NUM_OPTIONS;
  o->type = SANE_TYPE_INT;
  o->cap = SANE_CAP_SOFT_DETECT;
  s->val[NUM_OPTS].w = NUM_OPTIONS;

  /* Mode group */
  o = &s->opt[MODE_GROUP];
  o->title = SANE_I18N ("Scan Mode");
  o->desc = "";			/* not valid for a group */
  o->type = SANE_TYPE_GROUP;
  o->cap = 0;
  o->size = 0;
  o->constraint_type = SANE_CONSTRAINT_NONE;

  /* Scanner supported modes */
  o = &s->opt[MODE];
  o->name = SANE_NAME_SCAN_MODE;
  o->title = SANE_TITLE_SCAN_MODE;
  o->desc = SANE_DESC_SCAN_MODE;
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (mode_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = mode_list;
  s->val[MODE].s = malloc (o->size);
  strcpy (s->val[MODE].s, mode_list[2]);

  /* X and Y resolution */
  o = &s->opt[RESOLUTION];
  o->name = SANE_NAME_SCAN_RESOLUTION;
  o->title = SANE_TITLE_SCAN_RESOLUTION;
  o->desc = SANE_DESC_SCAN_RESOLUTION;
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_DPI;
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &resolutions_range;
  s->val[RESOLUTION].w = 100;

  /* Duplex */
  o = &s->opt[DUPLEX];
  o->name = "duplex";
  o->title = SANE_I18N ("Duplex");
  o->desc = SANE_I18N ("Enable Duplex (Dual-Sided) Scanning");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[DUPLEX].w = SANE_FALSE;

  /*FIXME
     if (!s->support_info.support_duplex)
     o->cap |= SANE_CAP_INACTIVE;
   */

  /* Feeder mode */
  o = &s->opt[FEEDER_MODE];
  o->name = "feeder-mode";
  o->title = SANE_I18N ("Feeder mode");
  o->desc = SANE_I18N ("Sets the feeding mode");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (feeder_mode_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = feeder_mode_list;
  s->val[FEEDER_MODE].s = malloc (o->size);
  strcpy (s->val[FEEDER_MODE].s, feeder_mode_list[0]);

  /* Scan source */
  o = &s->opt[SOURCE];
  o->name = SANE_NAME_SCAN_SOURCE;
  o->title = SANE_TITLE_SCAN_SOURCE;
  o->desc = SANE_DESC_SCAN_SOURCE;
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (source_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = source_list;
  s->val[SOURCE].s = malloc (o->size);
  strcpy (s->val[SOURCE].s, source_list[0]);
  if (s->id != KV_S7075C)
    o->cap |= SANE_CAP_INACTIVE;

  /* Length control */
  o = &s->opt[LENGTHCTL];
  o->name = "length-control";
  o->title = SANE_I18N ("Length control mode");
  o->desc =
    SANE_I18N
    ("Length Control Mode is a mode that the scanner reads up to the shorter length of actual"
     " paper or logical document length.");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[LENGTHCTL].w = SANE_FALSE;

  o = &s->opt[LONG_PAPER];
  o->name = "long-paper";
  o->title = SANE_I18N ("Long paper mode");
  o->desc = SANE_I18N ("Long Paper Mode is a mode that the scanner "
		       "reads the image after it divides long paper "
		       "by the length which is set in Document Size option.");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[LONG_PAPER].w = SANE_FALSE;
  o->cap |= SANE_CAP_INACTIVE;

  /* Manual feed */
  o = &s->opt[MANUALFEED];
  o->name = "manual-feed";
  o->title = SANE_I18N ("Manual feed mode");
  o->desc = SANE_I18N ("Sets the manual feed mode");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (manual_feed_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = manual_feed_list;
  s->val[MANUALFEED].s = malloc (o->size);
  strcpy (s->val[MANUALFEED].s, manual_feed_list[0]);

  /*Manual feed timeout */
  o = &s->opt[FEED_TIMEOUT];
  o->name = "feed-timeout";
  o->title = SANE_I18N ("Manual feed timeout");
  o->desc = SANE_I18N ("Sets the manual feed timeout in seconds");
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_NONE;
  o->size = sizeof (SANE_Int);
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &(byte_value_range);
  o->cap |= SANE_CAP_INACTIVE;
  s->val[FEED_TIMEOUT].w = 30;

  /* Double feed */
  o = &s->opt[DBLFEED];
  o->name = "dfeed";
  o->title = SANE_I18N ("Double feed detection");
  o->desc = SANE_I18N ("Enable/Disable double feed detection");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[DBLFEED].w = SANE_FALSE;

  o = &s->opt[DFEED_SENCE];
  o->name = "dfeed-sense";
  o->title = SANE_I18N ("Double feed detector sensitivity");
  o->desc = SANE_I18N ("Set the double feed detector sensitivity");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (dfeed_sence_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = dfeed_sence_list;
  s->val[DFEED_SENCE].s = malloc (o->size);
  strcpy (s->val[DFEED_SENCE].s, dfeed_sence_list[0]);
  o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[DFSTOP];
  o->name = "dfstop";
  o->title = SANE_I18N ("Do not stop after double feed detection");
  o->desc = SANE_I18N ("Do not stop after double feed detection");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[DFSTOP].w = SANE_FALSE;
  o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[DFEED_L];
  o->name = "dfeed_l";
  o->title = SANE_I18N ("Ignore left double feed sensor");
  o->desc = SANE_I18N ("Ignore left double feed sensor");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[DFEED_L].w = SANE_FALSE;
  o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[DFEED_C];
  o->name = "dfeed_c";
  o->title = SANE_I18N ("Ignore center double feed sensor");
  o->desc = SANE_I18N ("Ignore center double feed sensor");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[DFEED_C].w = SANE_FALSE;
  o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[DFEED_R];
  o->name = "dfeed_r";
  o->title = SANE_I18N ("Ignore right double feed sensor");
  o->desc = SANE_I18N ("Ignore right double feed sensor");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[DFEED_R].w = SANE_FALSE;
  o->cap |= SANE_CAP_INACTIVE;

  /* Fit to page */
  o = &s->opt[FIT_TO_PAGE];
  o->name = SANE_I18N ("fit-to-page");
  o->title = SANE_I18N ("Fit to page");
  o->desc = SANE_I18N ("Scanner shrinks image to fit scanned page");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[FIT_TO_PAGE].w = SANE_FALSE;

  /* Geometry group */
  o = &s->opt[GEOMETRY_GROUP];
  o->title = SANE_I18N ("Geometry");
  o->desc = "";			/* not valid for a group */
  o->type = SANE_TYPE_GROUP;
  o->cap = 0;
  o->size = 0;
  o->constraint_type = SANE_CONSTRAINT_NONE;

  /* Paper sizes list */
  o = &s->opt[PAPER_SIZE];
  o->name = "paper-size";
  o->title = SANE_I18N ("Paper size");
  o->desc = SANE_I18N ("Physical size of the paper in the ADF");
  o->type = SANE_TYPE_STRING;
  o->constraint.string_list =
    s->id == KV_S4085CL || s->id == KV_S4065CL ? paper_list_woA3 : paper_list;


  o->size = max_string_size (o->constraint.string_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  s->val[PAPER_SIZE].s = malloc (o->size);
  strcpy (s->val[PAPER_SIZE].s, SANE_I18N ("A4"));

  /* Landscape */
  o = &s->opt[LANDSCAPE];
  o->name = "landscape";
  o->title = SANE_I18N ("Landscape");
  o->desc =
    SANE_I18N ("Set paper position : "
	       "true for landscape, false for portrait");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[LANDSCAPE].w = SANE_FALSE;

  /* Upper left X */
  o = &s->opt[TL_X];
  o->name = SANE_NAME_SCAN_TL_X;
  o->title = SANE_TITLE_SCAN_TL_X;
  o->desc = SANE_DESC_SCAN_TL_X;
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_MM;
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range =
    (s->id == KV_S4085CL || s->id == KV_S4065CL)
    ? &tl_x_range_A4 : &tl_x_range;
  o->cap |= SANE_CAP_INACTIVE;
  s->val[TL_X].w = 0;

  /* Upper left Y */
  o = &s->opt[TL_Y];
  o->name = SANE_NAME_SCAN_TL_Y;
  o->title = SANE_TITLE_SCAN_TL_Y;
  o->desc = SANE_DESC_SCAN_TL_Y;
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_MM;
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range =
    (s->id == KV_S4085CL || s->id == KV_S4065CL)
    ? &tl_y_range_A4 : &tl_y_range;
  o->cap |= SANE_CAP_INACTIVE;
  s->val[TL_Y].w = 0;

  /* Bottom-right x */
  o = &s->opt[BR_X];
  o->name = SANE_NAME_SCAN_BR_X;
  o->title = SANE_TITLE_SCAN_BR_X;
  o->desc = SANE_DESC_SCAN_BR_X;
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_MM;
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range =
    (s->id == KV_S4085CL || s->id == KV_S4065CL)
    ? &br_x_range_A4 : &br_x_range;
  o->cap |= SANE_CAP_INACTIVE;
  s->val[BR_X].w = 210;

  /* Bottom-right y */
  o = &s->opt[BR_Y];
  o->name = SANE_NAME_SCAN_BR_Y;
  o->title = SANE_TITLE_SCAN_BR_Y;
  o->desc = SANE_DESC_SCAN_BR_Y;
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_MM;
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range =
    (s->id == KV_S4085CL || s->id == KV_S4065CL)
    ? &br_y_range_A4 : &br_y_range;
  o->cap |= SANE_CAP_INACTIVE;
  s->val[BR_Y].w = 297;

  /* Enhancement group */
  o = &s->opt[ADVANCED_GROUP];
  o->title = SANE_I18N ("Advanced");
  o->desc = "";			/* not valid for a group */
  o->type = SANE_TYPE_GROUP;
  o->cap = SANE_CAP_ADVANCED;
  o->size = 0;
  o->constraint_type = SANE_CONSTRAINT_NONE;

  /* Brightness */
  o = &s->opt[BRIGHTNESS];
  o->name = SANE_NAME_BRIGHTNESS;
  o->title = SANE_TITLE_BRIGHTNESS;
  o->desc = SANE_DESC_BRIGHTNESS;
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_NONE;
  o->size = sizeof (SANE_Int);
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &(byte_value_range);
  s->val[BRIGHTNESS].w = 128;

  /* Contrast */
  o = &s->opt[CONTRAST];
  o->name = SANE_NAME_CONTRAST;
  o->title = SANE_TITLE_CONTRAST;
  o->desc = SANE_DESC_CONTRAST;
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_NONE;
  o->size = sizeof (SANE_Int);
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &(byte_value_range);
  s->val[CONTRAST].w = 128;

  /* threshold */
  o = &s->opt[THRESHOLD];
  o->name = SANE_NAME_THRESHOLD;
  o->title = SANE_TITLE_THRESHOLD;
  o->desc = SANE_DESC_THRESHOLD;
  o->type = SANE_TYPE_INT;
  o->size = sizeof (SANE_Int);
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &(byte_value_range);
  s->val[THRESHOLD].w = 128;
  o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[AUTOMATIC_THRESHOLD];
  o->name = "athreshold";
  o->title = SANE_I18N ("Automatic threshold mode");
  o->desc = SANE_I18N ("Sets the automatic threshold mode");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (automatic_threshold_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = automatic_threshold_list;
  s->val[AUTOMATIC_THRESHOLD].s = malloc (o->size);
  strcpy (s->val[AUTOMATIC_THRESHOLD].s, automatic_threshold_list[0]);
  o->cap |= SANE_CAP_INACTIVE;

  /* Image emphasis */
  o = &s->opt[IMAGE_EMPHASIS];
  o->name = "image-emphasis";
  o->title = SANE_I18N ("Image emphasis");
  o->desc = SANE_I18N ("Sets the image emphasis");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (image_emphasis_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = image_emphasis_list;
  s->val[IMAGE_EMPHASIS].s = malloc (o->size);
  strcpy (s->val[IMAGE_EMPHASIS].s, image_emphasis_list[0]);;
  o->cap |= SANE_CAP_INACTIVE;

  /* Gamma */
  o = &s->opt[GAMMA_CORRECTION];
  o->name = "gamma-cor";
  o->title = SANE_I18N ("Gamma correction");
  o->desc = SANE_I18N ("Gamma correction");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (gamma_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = gamma_list;
  s->val[GAMMA_CORRECTION].s = malloc (o->size);
  strcpy (s->val[GAMMA_CORRECTION].s, gamma_list[0]);
  o->cap |= SANE_CAP_INACTIVE;

  /* Lamp color dropout */
  o = &s->opt[LAMP];
  o->name = "lamp-color";
  o->title = SANE_I18N ("Lamp color");
  o->desc = SANE_I18N ("Sets the lamp color (color dropout)");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (lamp_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = lamp_list;
  s->val[LAMP].s = malloc (o->size);
  strcpy (s->val[LAMP].s, lamp_list[0]);

  /* Inverse image */
  o = &s->opt[INVERSE];
  o->name = "inverse";
  o->title = SANE_I18N ("Inverse Image");
  o->desc = SANE_I18N ("Inverse image in B/W mode");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  o->cap |= SANE_CAP_INACTIVE;

  /* Halftone pattern */
  o = &s->opt[HALFTONE_PATTERN];
  o->name = SANE_NAME_HALFTONE_PATTERN;
  o->title = SANE_TITLE_HALFTONE_PATTERN;
  o->desc = SANE_DESC_HALFTONE_PATTERN;
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (halftone_pattern);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = halftone_pattern;
  s->val[HALFTONE_PATTERN].s = malloc (o->size);
  strcpy (s->val[HALFTONE_PATTERN].s, halftone_pattern[0]);
  o->cap |= SANE_CAP_INACTIVE;

  /* JPEG Compression */
  o = &s->opt[COMPRESSION];
  o->name = "jpeg";
  o->title = SANE_I18N ("JPEG compression");
  o->desc =
    SANE_I18N
    ("JPEG compression (yours application must be able to uncompress)");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;

  /* Compression parameter */
  o = &s->opt[COMPRESSION_PAR];
  o->name = "comp_arg";
  o->title = "Compression Argument";
  o->desc = "Compression Argument (Q parameter for JPEG)";
  o->type = SANE_TYPE_INT;
  o->size = sizeof (SANE_Int);
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &(compression_value_range);
  s->val[COMPRESSION_PAR].w = 0x4b;
  o->cap |= SANE_CAP_INACTIVE;

  /*  Stapled document */
  o = &s->opt[STAPELED_DOC];
  o->name = "stapeled_doc";
  o->title = SANE_I18N ("Detect stapled document");
  o->desc = SANE_I18N ("Detect stapled document");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (stapeled_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = stapeled_list;
  s->val[STAPELED_DOC].s = malloc (o->size);
  strcpy (s->val[STAPELED_DOC].s, stapeled_list[0]);
  if (s->id == KV_S7075C)
    o->cap |= SANE_CAP_INACTIVE;

  /* White level base */
  o = &s->opt[WHITE_LEVEL];
  o->name = SANE_NAME_WHITE_LEVEL;
  o->title = SANE_TITLE_WHITE_LEVEL;
  o->desc = SANE_DESC_WHITE_LEVEL;
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (white_level_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = white_level_list;
  s->val[WHITE_LEVEL].s = malloc (o->size);
  strcpy (s->val[WHITE_LEVEL].s, white_level_list[0]);
  o->cap |= SANE_CAP_INACTIVE;

  /* Noise reduction */
  o = &s->opt[NOISE_REDUCTION];
  o->name = "noise-reduction";
  o->title = SANE_I18N ("Noise reduction");
  o->desc = SANE_I18N ("Reduce the isolated dot noise");
  o->type = SANE_TYPE_STRING;
  o->size = max_string_size (noise_reduction_list);
  o->constraint_type = SANE_CONSTRAINT_STRING_LIST;
  o->constraint.string_list = noise_reduction_list;
  s->val[NOISE_REDUCTION].s = malloc (o->size);
  strcpy (s->val[NOISE_REDUCTION].s, noise_reduction_list[0]);
  o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[RED_CHROMA];
  o->name = "red-chroma";
  o->title = SANE_I18N ("chroma of red");
  o->desc = SANE_I18N ("Set chroma of red");
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_NONE;
  o->size = sizeof (SANE_Int);
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &(byte_value_range);
  s->val[RED_CHROMA].w = 0;

  o = &s->opt[BLUE_CHROMA];
  o->name = "blue chroma";
  o->title = SANE_I18N ("chroma of blue");
  o->desc = SANE_I18N ("Set chroma of blue");
  o->type = SANE_TYPE_INT;
  o->unit = SANE_UNIT_NONE;
  o->size = sizeof (SANE_Int);
  o->constraint_type = SANE_CONSTRAINT_RANGE;
  o->constraint.range = &(byte_value_range);
  s->val[BLUE_CHROMA].w = 0;

  o = &s->opt[DESKEW];
  o->name = "deskew";
  o->title = SANE_I18N ("Skew adjustment");
  o->desc = SANE_I18N ("Skew adjustment");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[DESKEW].w = SANE_FALSE;
  if (s->id != KV_S4085CL && s->id != KV_S4085CW)
    o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[STOP_SKEW];
  o->name = "stop-skew";
  o->title = SANE_I18N ("Stop scanner when a paper have been skewed");
  o->desc = SANE_I18N ("Scanner will be stop  when a paper have been skewed");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[STOP_SKEW].w = SANE_FALSE;

  o = &s->opt[CROP];
  o->name = "crop";
  o->title = SANE_I18N ("Crop actual image area");
  o->desc = SANE_I18N ("Scanner automatically detect image area and crop it");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[CROP].w = SANE_FALSE;
  if (s->id != KV_S4085CL && s->id != KV_S4085CW)
    o->cap |= SANE_CAP_INACTIVE;

  o = &s->opt[MIRROR];
  o->name = "mirror";
  o->title = SANE_I18N ("Mirror image");
  o->desc = SANE_I18N ("It is right and left reversing");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[MIRROR].w = SANE_FALSE;

  o = &s->opt[TOPPOS];
  o->name = "toppos";
  o->title = SANE_I18N ("Addition of space in top position");
  o->desc = SANE_I18N ("Addition of space in top position");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[TOPPOS].w = SANE_FALSE;

  o = &s->opt[BTMPOS];
  o->name = "btmpos";
  o->title = SANE_I18N ("Addition of space in bottom position");
  o->desc = SANE_I18N ("Addition of space in bottom position");
  o->type = SANE_TYPE_BOOL;
  o->unit = SANE_UNIT_NONE;
  s->val[BTMPOS].w = SANE_FALSE;
}


/* Lookup a string list from one array and return its index. */
static int
str_index (const SANE_String_Const * list, SANE_String_Const name)
{
  int index;
  index = 0;
  while (list[index])
    {
      if (!strcmp (list[index], name))
	return (index);
      index++;
    }
  return (-1);			/* not found */
}

/* Control option */
SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
		     SANE_Action action, void *val, SANE_Int * info)
{
  int i;
  SANE_Status status;
  SANE_Word cap;
  struct scanner *s = (struct scanner *) handle;

  if (info)
    *info = 0;

  if (option < 0 || option >= NUM_OPTIONS)
    return SANE_STATUS_UNSUPPORTED;

  cap = s->opt[option].cap;
  if (!SANE_OPTION_IS_ACTIVE (cap))
    return SANE_STATUS_UNSUPPORTED;

  if (action == SANE_ACTION_GET_VALUE)
    {
      if (s->opt[option].type == SANE_TYPE_STRING)
	{
	  DBG (DBG_INFO,
	       "sane_control_option: reading opt[%d] =  %s\n",
	       option, s->val[option].s);
	  strcpy (val, s->val[option].s);
	}
      else
	{
	  *(SANE_Word *) val = s->val[option].w;
	  DBG (DBG_INFO,
	       "sane_control_option: reading opt[%d] =  %d\n",
	       option, s->val[option].w);
	}
      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;

      if (s->opt[option].type == SANE_TYPE_STRING)
	{
	  if (!strcmp (val, s->val[option].s))
	    return SANE_STATUS_GOOD;
	  DBG (DBG_INFO,
	       "sane_control_option: writing opt[%d] =  %s\n",
	       option, (SANE_String_Const) val);
	}
      else
	{
	  if (*(SANE_Word *) val == s->val[option].w)
	    return SANE_STATUS_GOOD;
	  DBG (DBG_INFO,
	       "sane_control_option: writing opt[%d] =  %d\n",
	       option, *(SANE_Word *) val);
	}

      switch (option)
	{
	  /* Side-effect options */
	case RESOLUTION:
	  s->val[option].w = *(SANE_Word *) val;
	  if (info)
	    *info |= SANE_INFO_RELOAD_PARAMS;
	  return SANE_STATUS_GOOD;

	case TL_Y:
	  if ((*(SANE_Word *) val) + MIN_LENGTH <=
	      s->val[BR_Y].w &&
	      !check_area (s, s->val[TL_X].w, *(SANE_Word *) val,
			   s->val[BR_X].w, s->val[BR_Y].w))
	    {
	      s->val[option].w = *(SANE_Word *) val;
	      if (info)
		*info |= SANE_INFO_RELOAD_PARAMS;
	    }
	  else if (info)
	    *info |= SANE_INFO_INEXACT |
	      SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
	  return SANE_STATUS_GOOD;
	case BR_Y:
	  if ((*(SANE_Word *) val) >=
	      s->val[TL_Y].w + MIN_LENGTH
	      && !check_area (s, s->val[TL_X].w, s->val[TL_Y].w,
			      s->val[BR_X].w, *(SANE_Word *) val))
	    {
	      s->val[option].w = *(SANE_Word *) val;
	      if (info)
		*info |= SANE_INFO_RELOAD_PARAMS;
	    }
	  else if (info)
	    *info |= SANE_INFO_INEXACT |
	      SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
	  return SANE_STATUS_GOOD;

	case TL_X:
	  if ((*(SANE_Word *) val) + MIN_WIDTH <=
	      s->val[BR_X].w &&
	      !check_area (s, *(SANE_Word *) val, s->val[TL_Y].w,
			   s->val[BR_X].w, s->val[BR_Y].w))
	    {
	      s->val[option].w = *(SANE_Word *) val;
	      if (info)
		*info |= SANE_INFO_RELOAD_PARAMS;
	    }
	  else if (info)
	    *info |= SANE_INFO_INEXACT |
	      SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
	  return SANE_STATUS_GOOD;

	case BR_X:
	  if (*(SANE_Word *) val >=
	      s->val[TL_X].w + MIN_WIDTH
	      && !check_area (s, s->val[TL_X].w, s->val[TL_Y].w,
			      *(SANE_Word *) val, s->val[BR_Y].w))
	    {
	      s->val[option].w = *(SANE_Word *) val;
	      if (info)
		*info |= SANE_INFO_RELOAD_PARAMS;
	    }
	  else if (info)
	    *info |= SANE_INFO_INEXACT |
	      SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
	  return SANE_STATUS_GOOD;

	case LANDSCAPE:
	  s->val[option].w = *(SANE_Word *) val;
	  if (info)
	    *info |= SANE_INFO_RELOAD_PARAMS;
	  return SANE_STATUS_GOOD;

	  /* Side-effect free options */
	case CONTRAST:
	case BRIGHTNESS:
	case DUPLEX:
	case LENGTHCTL:
	case LONG_PAPER:
	case FIT_TO_PAGE:
	case THRESHOLD:
	case INVERSE:
	case COMPRESSION_PAR:
	case DFSTOP:
	case DFEED_L:
	case DFEED_C:
	case DFEED_R:
	case STOP_SKEW:
	case DESKEW:
	case MIRROR:
	case CROP:
	case TOPPOS:
	case BTMPOS:
	case RED_CHROMA:
	case BLUE_CHROMA:
	  s->val[option].w = *(SANE_Word *) val;
	  return SANE_STATUS_GOOD;

	case FEED_TIMEOUT:
	  s->val[option].w = *(SANE_Word *) val;
	  return kvs40xx_set_timeout (s, s->val[option].w);

	  /* String mode */
	case IMAGE_EMPHASIS:
	case GAMMA_CORRECTION:
	case LAMP:
	case HALFTONE_PATTERN:
	case DFEED_SENCE:
	case AUTOMATIC_THRESHOLD:
	case WHITE_LEVEL:
	case NOISE_REDUCTION:
	  strcpy (s->val[option].s, val);
	  return SANE_STATUS_GOOD;

	case SOURCE:
	  strcpy (s->val[option].s, val);
	  if (strcmp (s->val[option].s, SANE_I18N ("adf")))
	    {
	      strcpy (s->val[FEEDER_MODE].s, feeder_mode_list[0]);
	      strcpy (s->val[MANUALFEED].s, manual_feed_list[0]);
	      s->val[DUPLEX].w = SANE_FALSE;
	      s->val[DBLFEED].w = SANE_FALSE;
	      s->val[BTMPOS].w = SANE_FALSE;
	      s->val[TOPPOS].w = SANE_FALSE;
	      s->val[STOP_SKEW].w = SANE_FALSE;
	      s->val[LENGTHCTL].w = SANE_FALSE;
	      s->val[LONG_PAPER].w = SANE_FALSE;
	      s->opt[FEEDER_MODE].cap |= SANE_CAP_INACTIVE;
	      s->opt[MANUALFEED].cap |= SANE_CAP_INACTIVE;
	      s->opt[DUPLEX].cap |= SANE_CAP_INACTIVE;
	      s->opt[DBLFEED].cap |= SANE_CAP_INACTIVE;
	      s->opt[BTMPOS].cap |= SANE_CAP_INACTIVE;
	      s->opt[TOPPOS].cap |= SANE_CAP_INACTIVE;
	      s->opt[STOP_SKEW].cap |= SANE_CAP_INACTIVE;
	      s->opt[LENGTHCTL].cap |= SANE_CAP_INACTIVE;
	      s->opt[LONG_PAPER].cap |= SANE_CAP_INACTIVE;
	    }
	  else
	    {
	      s->opt[FEEDER_MODE].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[MANUALFEED].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DUPLEX].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DBLFEED].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[BTMPOS].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[TOPPOS].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[STOP_SKEW].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[LENGTHCTL].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[LONG_PAPER].cap &= ~SANE_CAP_INACTIVE;
	    }
	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS;

	  return SANE_STATUS_GOOD;

	case FEEDER_MODE:
	  strcpy (s->val[option].s, val);
	  if (strcmp (s->val[option].s, SANE_I18N ("continuous")))
	    {
	      s->opt[LONG_PAPER].cap |= SANE_CAP_INACTIVE;
	    }
	  else
	    {
	      s->opt[LONG_PAPER].cap &= ~SANE_CAP_INACTIVE;
	    }
	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS;

	  return SANE_STATUS_GOOD;

	case MODE:
	  strcpy (s->val[option].s, val);
	  if (!strcmp (s->val[option].s, SANE_VALUE_SCAN_MODE_LINEART))
	    {
	      s->opt[GAMMA_CORRECTION].cap |= SANE_CAP_INACTIVE;
	      s->opt[COMPRESSION].cap |= SANE_CAP_INACTIVE;
	      s->opt[COMPRESSION_PAR].cap |= SANE_CAP_INACTIVE;
	      s->opt[THRESHOLD].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[HALFTONE_PATTERN].cap &= ~SANE_CAP_INACTIVE;

	      s->opt[AUTOMATIC_THRESHOLD].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[WHITE_LEVEL].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[NOISE_REDUCTION].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[INVERSE].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[RED_CHROMA].cap |= SANE_CAP_INACTIVE;
	      s->opt[BLUE_CHROMA].cap |= SANE_CAP_INACTIVE;
	    }
	  else
	    {
	      s->opt[COMPRESSION].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[COMPRESSION_PAR].cap &= ~SANE_CAP_INACTIVE;

	      s->opt[THRESHOLD].cap |= SANE_CAP_INACTIVE;
	      s->opt[INVERSE].cap |= SANE_CAP_INACTIVE;
	      s->opt[HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE;

	      s->opt[AUTOMATIC_THRESHOLD].cap |= SANE_CAP_INACTIVE;
	      s->opt[WHITE_LEVEL].cap |= SANE_CAP_INACTIVE;
	      s->opt[NOISE_REDUCTION].cap |= SANE_CAP_INACTIVE;
	      s->opt[RED_CHROMA].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[BLUE_CHROMA].cap &= ~SANE_CAP_INACTIVE;
	    }

	  if (!strcmp (s->val[option].s, SANE_VALUE_SCAN_MODE_GRAY))
	    {
	      s->opt[INVERSE].cap &= ~SANE_CAP_INACTIVE;

	      s->opt[GAMMA_CORRECTION].cap &= ~SANE_CAP_INACTIVE;
	    }
	  else
	    {
	      s->opt[GAMMA_CORRECTION].cap |= SANE_CAP_INACTIVE;
	    }

	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;

	  return SANE_STATUS_GOOD;

	case MANUALFEED:
	  strcpy (s->val[option].s, val);
	  if (strcmp (s->val[option].s, manual_feed_list[0]) == 0)	/* off */
	    s->opt[FEED_TIMEOUT].cap |= SANE_CAP_INACTIVE;
	  else
	    s->opt[FEED_TIMEOUT].cap &= ~SANE_CAP_INACTIVE;
	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS;

	  return SANE_STATUS_GOOD;

	case STAPELED_DOC:
	  strcpy (s->val[option].s, val);
	  if (strcmp (s->val[option].s, stapeled_list[0]) == 0)
	    {
	      s->opt[DBLFEED].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFSTOP].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_L].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_C].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_C].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_R].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_SENCE].cap &= ~SANE_CAP_INACTIVE;
	    }
	  else
	    {
	      s->opt[DBLFEED].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFSTOP].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_L].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_C].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_R].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_SENCE].cap |= SANE_CAP_INACTIVE;
	    }
	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS;

	  return SANE_STATUS_GOOD;

	case DBLFEED:
	  s->val[option].w = *(SANE_Word *) val;
	  if (!s->val[option].b)
	    {
	      s->opt[DFSTOP].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_L].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_C].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_R].cap |= SANE_CAP_INACTIVE;
	      s->opt[DFEED_SENCE].cap |= SANE_CAP_INACTIVE;
	    }
	  else
	    {
	      s->opt[DFSTOP].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_L].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_C].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_C].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_R].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[DFEED_SENCE].cap &= ~SANE_CAP_INACTIVE;
	    }
	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS;

	  return SANE_STATUS_GOOD;

	case PAPER_SIZE:
	  strcpy (s->val[option].s, val);
	  i = str_index (paper_list, s->val[option].s);
	  if (i == 0)
	    {			/*user def */
	      s->opt[TL_X].cap &=
		s->opt[TL_Y].cap &=
		s->opt[BR_X].cap &= s->opt[BR_Y].cap &= ~SANE_CAP_INACTIVE;
	      s->opt[LANDSCAPE].cap |= SANE_CAP_INACTIVE;
	      s->val[LANDSCAPE].w = 0;
	    }
	  else
	    {
	      s->opt[TL_X].cap |=
		s->opt[TL_Y].cap |=
		s->opt[BR_X].cap |= s->opt[BR_Y].cap |= SANE_CAP_INACTIVE;
	      if ( /*i == 4 || */ i == 5 || i == 6 /*XXX*/
		  || i == 10 || i == 11)
		{		/*A4, A5, A6, B5, B6 */
		  if ((s->id == KV_S4085CL || s->id == KV_S4065CL)
		      && i == 4 && i == 10)
		    {		/*A4, B5 */
		      s->opt[LANDSCAPE].cap |= SANE_CAP_INACTIVE;
		      s->val[LANDSCAPE].w = 0;
		    }
		  else
		    s->opt[LANDSCAPE].cap &= ~SANE_CAP_INACTIVE;
		}
	      else
		{
		  s->opt[LANDSCAPE].cap |= SANE_CAP_INACTIVE;
		  s->val[LANDSCAPE].w = 0;
		}
	    }
	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;

	  return SANE_STATUS_GOOD;

	case COMPRESSION:
	  s->val[option].w = *(SANE_Word *) val;
	  if (!s->val[option].b)
	    {
	      s->opt[COMPRESSION_PAR].cap |= SANE_CAP_INACTIVE;
	    }
	  else
	    {
	      s->opt[COMPRESSION_PAR].cap &= ~SANE_CAP_INACTIVE;
	    }
	  if (info)
	    *info |= SANE_INFO_RELOAD_OPTIONS;

	  return SANE_STATUS_GOOD;
	}
    }


  return SANE_STATUS_UNSUPPORTED;
}

void
kvs40xx_init_window (struct scanner *s, struct window *wnd, int wnd_id)
{
  int paper = str_index (paper_list, s->val[PAPER_SIZE].s), i;
  memset (wnd, 0, sizeof (struct window));
  copy16 (wnd->window_descriptor_block_length, cpu2be16 (66));

  wnd->window_identifier = wnd_id;
  copy16 (wnd->x_resolution, cpu2be16 (s->val[RESOLUTION].w));
  copy16 (wnd->y_resolution, cpu2be16 (s->val[RESOLUTION].w));
  if (!paper)
    {
      copy32 (wnd->upper_left_x,
              cpu2be32 (mm2scanner_units (s->val[TL_X].w)));
      copy32 (wnd->upper_left_y,
              cpu2be32 (mm2scanner_units (s->val[TL_Y].w)));
      copy32 (wnd->document_width,
              cpu2be32 (mm2scanner_units (s->val[BR_X].w)));
      copy32 (wnd->width,
              cpu2be32 (mm2scanner_units (s->val[BR_X].w - s->val[TL_X].w)));
      copy32 (wnd->document_length, cpu2be32 (mm2scanner_units
                                              (s->val[BR_Y].w)));
      copy32 (wnd->length,
              cpu2be32 (mm2scanner_units (s->val[BR_Y].w - s->val[TL_Y].w)));
    }
  else
    {
      u32 w = cpu2be32 (mm2scanner_units (paper_sizes[paper].width));
      u32 h = cpu2be32 (mm2scanner_units (paper_sizes[paper].height));
      copy32 (wnd->upper_left_x, cpu2be32 (mm2scanner_units (0)));
      copy32 (wnd->upper_left_y, cpu2be32 (mm2scanner_units (0)));
      if (!s->val[LANDSCAPE].b)
	{
	  copy32 (wnd->width, w);
	  copy32 (wnd->length, h);
	  copy32 (wnd->document_width, w);
	  copy32 (wnd->document_length, h);
	}
      else
	{
	  copy32 (wnd->width, h);
	  copy32 (wnd->length, w);
	  copy32 (wnd->document_width, h);
	  copy32 (wnd->document_length, w);
	}
    }
  wnd->brightness = s->val[BRIGHTNESS].w;
  wnd->threshold = s->val[THRESHOLD].w;
  wnd->contrast = s->val[CONTRAST].w;
  wnd->image_composition = mode_val[str_index (mode_list, s->val[MODE].s)];
  wnd->bit_per_pixel = bps_val[str_index (mode_list, s->val[MODE].s)];

  copy16 (wnd->halftone_pattern,
          cpu2be16 (str_index (halftone_pattern, s->val[HALFTONE_PATTERN].s)));

  wnd->rif_padding = s->val[INVERSE].b << 7;
  copy16 (wnd->bit_ordering, cpu2be16 (BIT_ORDERING));
  wnd->compression_type = s->val[COMPRESSION].b ? 0x81 : 0;
  wnd->compression_argument = s->val[COMPRESSION_PAR].w;

  wnd->vendor_unique_identifier = 0;
  wnd->nobuf_fstspeed_dfstop = str_index (source_list,
					  s->val[SOURCE].s) << 7 |
    str_index (stapeled_list,
	       s->val[STAPELED_DOC].s) << 5 |
    s->val[STOP_SKEW].b << 4 | s->val[CROP].b << 3 | s->val[DFSTOP].b << 0;

  wnd->mirror_image = s->val[MIRROR].b << 7 |
    s->val[DFEED_L].b << 2 | s->val[DFEED_C].b << 1 | s->val[DFEED_R].b << 0;
  wnd->image_emphasis = str_index (image_emphasis_list,
				   s->val[IMAGE_EMPHASIS].s);
  wnd->gamma_correction = gamma_val[str_index (gamma_list,
					       s->val[GAMMA_CORRECTION].s)];
  wnd->mcd_lamp_dfeed_sens =
    str_index (lamp_list, s->val[LAMP].s) << 4 |
    str_index (dfeed_sence_list, s->val[DFEED_SENCE].s);

  wnd->document_size = (paper != 0) << 7
    | s->val[LENGTHCTL].b << 6
    | s->val[LONG_PAPER].b << 5 | s->val[LANDSCAPE].b << 4 | paper_val[paper];

  wnd->ahead_deskew_dfeed_scan_area_fspeed_rshad =
    (s->val[DESKEW].b || s->val[CROP].b ? 2 : 0) << 5 | /*XXX*/
    s->val[DBLFEED].b << 4 | s->val[FIT_TO_PAGE].b << 2;
  wnd->continuous_scanning_pages =
    str_index (feeder_mode_list, s->val[FEEDER_MODE].s) ? 0xff : 0;
  wnd->automatic_threshold_mode = automatic_threshold_val
    [str_index (automatic_threshold_list, s->val[AUTOMATIC_THRESHOLD].s)];
  wnd->automatic_separation_mode = 0;	/*Does not supported */
  wnd->standard_white_level_mode =
    white_level_val[str_index (white_level_list, s->val[WHITE_LEVEL].s)];
  wnd->b_wnr_noise_reduction =
    str_index (noise_reduction_list, s->val[NOISE_REDUCTION].s);

  i = str_index (manual_feed_list, s->val[MANUALFEED].s);
  wnd->mfeed_toppos_btmpos_dsepa_hsepa_dcont_rstkr = i << 6 |
    s->val[TOPPOS].b << 5 | s->val[BTMPOS].b << 4;
  wnd->stop_mode = 1;
  wnd->red_chroma = s->val[RED_CHROMA].w;
  wnd->blue_chroma = s->val[BLUE_CHROMA].w;
}

/* Get scan parameters */
SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
  struct scanner *s = (struct scanner *) handle;
  SANE_Parameters *p = &s->params;

  if (!s->scanning)
    {
      unsigned w, h, res = s->val[RESOLUTION].w;
      unsigned i = str_index (paper_list,
			      s->val[PAPER_SIZE].s);
      if (i)
	{
	  if (s->val[LANDSCAPE].b)
	    {
	      w = paper_sizes[i].height;
	      h = paper_sizes[i].width;
	    }
	  else
	    {
	      w = paper_sizes[i].width;
	      h = paper_sizes[i].height;
	    }
	}
      else
	{
	  w = s->val[BR_X].w - s->val[TL_X].w;
	  h = s->val[BR_Y].w - s->val[TL_Y].w;
	}
      p->pixels_per_line = w * res / 25.4 + .5;
      p->lines = h * res / 25.4 + .5;
    }

  p->format = !strcmp (s->val[MODE].s,
		       SANE_VALUE_SCAN_MODE_COLOR) ? SANE_FRAME_RGB :
    SANE_FRAME_GRAY;
  p->last_frame = SANE_TRUE;
  p->depth = bps_val[str_index (mode_list, s->val[MODE].s)];
  p->bytes_per_line = p->depth * p->pixels_per_line / 8;
  if (p->depth > 8)
    p->depth = 8;
  if (params)
    memcpy (params, p, sizeof (SANE_Parameters));
  s->side_size = p->bytes_per_line * p->lines;

  return SANE_STATUS_GOOD;
}