summaryrefslogtreecommitdiff
path: root/backend/epson2-commands.c
blob: a84630b8fe1d57e48722243b5c7cadf460e1e458 (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
/*
 * ESC/I commands for Epson scanners
 *
 * Based on Kazuhiro Sasayama previous
 * Work on epson.[ch] file from the SANE package.
 * Please see those files for original copyrights.
 *
 * Copyright (C) 2006 Tower Technologies
 * Author: Alessandro Zummo <a.zummo@towertech.it>
 *
 * 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, version 2.
 */

#define DEBUG_DECLARE_ONLY

#include "sane/config.h"

#include <byteorder.h>
#include <math.h>
#include <sys/types.h>

#include "epson2.h"
#include "epson2-io.h"
#include "epson2-commands.h"


/* ESC H, set zoom */
SANE_Status
esci_set_zoom(Epson_Scanner * s, unsigned char x, unsigned char y)
{
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s: x = %d, y = %d\n", __func__, x, y);

	if (!s->hw->cmd->set_zoom) {
		DBG(1, "%s: not supported\n", __func__);
		return SANE_STATUS_GOOD;
	}

	params[0] = ESC;
	params[1] = s->hw->cmd->set_zoom;

	status = e2_cmd_simple(s, params, 2);
	if (status != SANE_STATUS_GOOD)
		return status;

	params[0] = x;
	params[1] = y;

	return e2_cmd_simple(s, params, 2);
}

/* ESC R */
SANE_Status
esci_set_resolution(Epson_Scanner * s, int x, int y)
{
	SANE_Status status;
	unsigned char params[4];

	DBG(8, "%s: x = %d, y = %d\n", __func__, x, y);

	if (!s->hw->cmd->set_resolution) {
		DBG(1, "%s: not supported\n", __func__);
		return SANE_STATUS_GOOD;
	}

	params[0] = ESC;
	params[1] = s->hw->cmd->set_resolution;

	status = e2_cmd_simple(s, params, 2);
	if (status != SANE_STATUS_GOOD)
		return status;

	params[0] = x;
	params[1] = x >> 8;
	params[2] = y;
	params[3] = y >> 8;

	return e2_cmd_simple(s, params, 4);
}

/*
 * Sends the "set scan area" command to the scanner with the currently selected
 * scan area. This scan area must be already corrected for "color shuffling" if
 * necessary.
 */

SANE_Status
esci_set_scan_area(Epson_Scanner * s, int x, int y, int width, int height)
{
	SANE_Status status;
	unsigned char params[8];

	DBG(8, "%s: x = %d, y = %d, w = %d, h = %d\n",
	    __func__, x, y, width, height);

	if (!s->hw->cmd->set_scan_area) {
		DBG(1, "%s: not supported\n", __func__);
		return SANE_STATUS_UNSUPPORTED;
	}

	/* verify the scan area */
	if (x < 0 || y < 0 || width <= 0 || height <= 0)
		return SANE_STATUS_INVAL;

	params[0] = ESC;
	params[1] = s->hw->cmd->set_scan_area;

	status = e2_cmd_simple(s, params, 2);
	if (status != SANE_STATUS_GOOD)
		return status;

	params[0] = x;
	params[1] = x >> 8;
	params[2] = y;
	params[3] = y >> 8;
	params[4] = width;
	params[5] = width >> 8;
	params[6] = height;
	params[7] = height >> 8;

	return e2_cmd_simple(s, params, 8);
}

static int
get_roundup_index(double frac[], int n)
{
	int i, index = -1;
	double max_val = 0.0;

	for (i = 0; i < n; i++) {

		if (frac[i] < 0)
			continue;

		if (max_val < frac[i]) {
			index = i;
			max_val = frac[i];
		}
	}

	return index;
}

static int
get_rounddown_index(double frac[], int n)
{
	int i, index = -1;
	double min_val = 1.0;

	for (i = 0; i < n; i++) {

		if (frac[i] > 0)
			continue;

		if (min_val > frac[i]) {
			index = i;
			min_val = frac[i];
		}
	}

	return index;
}

static unsigned char
int2cpt(int val)
{
	if (val >= 0) {

		if (val > 127)
			val = 127;

		return (unsigned char) val;

	} else {

		val = -val;

		if (val > 127)
			val = 127;

		return (unsigned char) (0x80 | val);
	}
}

static void
round_cct(double org_cct[], int rnd_cct[])
{
	int loop = 0;
	int i, j, sum[3];
	double mult_cct[9], frac[9];

	for (i = 0; i < 9; i++) {
  		mult_cct[i] = org_cct[i] * 32;
		rnd_cct[i] = (int) floor(mult_cct[i] + 0.5);
	}
	
	do {
		for (i = 0; i < 3; i++) {

			int k = i * 3;

			if ((rnd_cct[k] == 11) &&
				(rnd_cct[k] == rnd_cct[k + 1]) &&
				(rnd_cct[k] == rnd_cct[k + 2])) {

				rnd_cct[k + i]--;
				mult_cct[k + i] = rnd_cct[k + i];
			}
		}

		for (i = 0; i < 3; i++) {

			int k = i * 3;

			for (sum[i] = j = 0; j < 3; j++)
				sum[i] += rnd_cct[k + j];
		}

		for (i = 0; i < 9; i++)
			frac[i] = mult_cct[i] - rnd_cct[i];

		for (i = 0; i < 3; i++) {

			int k = i * 3;

			if (sum[i] < 32) {

				int index = get_roundup_index(&frac[k], 3);
				if (index != -1) {
					rnd_cct[k + index]++;
					mult_cct[k + index] = rnd_cct[k + index];
					sum[i]++;
				}

			} else if (sum[i] > 32) {

				int index = get_rounddown_index(&frac[k], 3);
				if (index != -1) {
					rnd_cct[k + index]--;
					mult_cct[k + index] = rnd_cct[k + index];
					sum[i]--;
				}
			}
		}
	}

	while ((++loop < 2)
		&& ((sum[0] != 32) || (sum[1] != 32) || (sum[2] != 32)));
}

static void
profile_to_colorcoeff(double *profile, unsigned char *color_coeff)
{
	int cc_idx[] = { 4, 1, 7, 3, 0, 6, 5, 2, 8 };
	int i, color_table[9];

  	round_cct(profile, color_table);

  	for (i = 0; i < 9; i++)
  		color_coeff[i] = int2cpt(color_table[cc_idx[i]]);
}
                                            
                                            
/*
 * Sends the "set color correction coefficients" command with the
 * currently selected parameters to the scanner.
 */

SANE_Status
esci_set_color_correction_coefficients(Epson_Scanner * s, SANE_Word *table)
{
	SANE_Status status;
	unsigned char params[2];
	unsigned char data[9];
	double cct[9];

	DBG(8, "%s\n", __func__);
	if (!s->hw->cmd->set_color_correction_coefficients) {
		DBG(1, "%s: not supported\n", __func__);
		return SANE_STATUS_UNSUPPORTED;
	}

	params[0] = ESC;
	params[1] = s->hw->cmd->set_color_correction_coefficients;

	status = e2_cmd_simple(s, params, 2);
	if (status != SANE_STATUS_GOOD)
		return status;

	cct[0] = SANE_UNFIX(table[0]);
	cct[1] = SANE_UNFIX(table[1]);
	cct[2] = SANE_UNFIX(table[2]);
	cct[3] = SANE_UNFIX(table[3]);
	cct[4] = SANE_UNFIX(table[4]);
	cct[5] = SANE_UNFIX(table[5]);
	cct[6] = SANE_UNFIX(table[6]);
	cct[7] = SANE_UNFIX(table[7]);
	cct[8] = SANE_UNFIX(table[8]);

	profile_to_colorcoeff(cct, data);

	DBG(11, "%s: %d,%d,%d %d,%d,%d %d,%d,%d\n", __func__,
	    data[0] , data[1], data[2], data[3],
	    data[4], data[5], data[6], data[7], data[8]);

	return e2_cmd_simple(s, data, 9);
}

SANE_Status
esci_set_gamma_table(Epson_Scanner * s)
{
	SANE_Status status;
	unsigned char params[2];
	unsigned char gamma[257];
	int n;
	int table;

/*	static const char gamma_cmds[] = { 'M', 'R', 'G', 'B' }; */
	static const char gamma_cmds[] = { 'R', 'G', 'B' };

	DBG(8, "%s\n", __func__);
	if (!s->hw->cmd->set_gamma_table)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = ESC;
	params[1] = s->hw->cmd->set_gamma_table;

	/* Print the gamma tables before sending them to the scanner */

	if (DBG_LEVEL >= 10) {
		int c, i, j;

		for (c = 0; c < 3; c++) {
			for (i = 0; i < 256; i += 16) {
				char gammaValues[16 * 3 + 1], newValue[4];

				gammaValues[0] = '\0';

				for (j = 0; j < 16; j++) {
					sprintf(newValue, " %02x",
						s->gamma_table[c][i + j]);
					strcat(gammaValues, newValue);
				}
				DBG(11, "gamma table[%d][%d] %s\n", c, i,
				    gammaValues);
			}
		}
	}

	for (table = 0; table < 3; table++) {
		gamma[0] = gamma_cmds[table];

		for (n = 0; n < 256; ++n)
			gamma[n + 1] = s->gamma_table[table][n];

		status = e2_cmd_simple(s, params, 2);
		if (status != SANE_STATUS_GOOD)
			return status;

		status = e2_cmd_simple(s, gamma, 257);
		if (status != SANE_STATUS_GOOD)
			return status;
	}

	return status;
}

/* ESC F - Request Status
 * -> ESC f
 * <- Information block
 */

SANE_Status
esci_request_status(SANE_Handle handle, unsigned char *scanner_status)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (s->hw->cmd->request_status == 0)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = ESC;
	params[1] = s->hw->cmd->request_status;

	e2_send(s, params, 2, 4, &status);
	if (status != SANE_STATUS_GOOD)
		return status;

	status = e2_recv_info_block(s, params, 4, NULL);
	if (status != SANE_STATUS_GOOD)
		return status;

	if (scanner_status)
		*scanner_status = params[0];

	DBG(1, "status: %02x\n", params[0]);

	if (params[0] & STATUS_NOT_READY)
		DBG(1, " scanner in use on another interface\n");
	else
		DBG(1, " ready\n");

	if (params[0] & STATUS_FER)
		DBG(1, " system error\n");

	if (params[0] & STATUS_OPTION)
		DBG(1, " option equipment is installed\n");
	else
		DBG(1, " no option equipment installed\n");

	if (params[0] & STATUS_EXT_COMMANDS)
		DBG(1, " support extended commands\n");
	else
		DBG(1, " does NOT support extended commands\n");

	if (params[0] & STATUS_RESERVED)
		DBG(0,
		    " a reserved bit is set, please contact the author.\n");

	return status;
}

/* extended commands */

/* FS I, Request Extended Identity
 * -> FS I
 * <- Extended identity data (80)
 *
 * Request the properties of the scanner.
 */

SANE_Status
esci_request_extended_identity(SANE_Handle handle, unsigned char *buf)
{
	unsigned char model[17];
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (buf == NULL)
		return SANE_STATUS_INVAL;

	if (s->hw->cmd->request_extended_identity == 0)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = FS;
	params[1] = s->hw->cmd->request_extended_identity;

	status = e2_txrx(s, params, 2, buf, 80);
	if (status != SANE_STATUS_GOOD)
		return status;

	DBG(1, " command level   : %c%c\n", buf[0], buf[1]);
	DBG(1, " basic resolution: %lu\n", (unsigned long) le32atoh(&buf[4]));
	DBG(1, " min resolution  : %lu\n", (unsigned long) le32atoh(&buf[8]));
	DBG(1, " max resolution  : %lu\n", (unsigned long) le32atoh(&buf[12]));
	DBG(1, " max pixel num   : %lu\n", (unsigned long) le32atoh(&buf[16]));
	DBG(1, " scan area       : %lux%lu\n",
	    (unsigned long) le32atoh(&buf[20]), (unsigned long) le32atoh(&buf[24]));

	DBG(1, " adf area        : %lux%lu\n",
	    (unsigned long) le32atoh(&buf[28]), (unsigned long) le32atoh(&buf[32]));

	DBG(1, " tpu area        : %lux%lu\n",
	    (unsigned long) le32atoh(&buf[36]), (unsigned long) le32atoh(&buf[40]));

	DBG(1, " capabilities (1): 0x%02x\n", buf[44]);
	DBG(1, " capabilities (2): 0x%02x\n", buf[45]);
	DBG(1, " input depth     : %d\n", buf[66]);
	DBG(1, " max output depth: %d\n", buf[67]);
	DBG(1, " rom version     : %c%c%c%c\n",
	    buf[62], buf[63], buf[64], buf[65]);

	memcpy(model, &buf[46], 16);
	model[16] = '\0';
	DBG(1, " model name      : %s\n", model);

	DBG(1, "options:\n");

	if (le32atoh(&buf[28]) > 0)
		DBG(1, " ADF detected\n");

	if (le32atoh(&buf[36]) > 0)
		DBG(1, " TPU detected\n");

	if (buf[44])
		DBG(1, "capabilities (1):\n");

	if (buf[44] & EXT_IDTY_CAP1_DLF)
		DBG(1, " main lamp change is supported\n");

	if (buf[44] & EXT_IDTY_CAP1_NOTFBF)
		DBG(1, " the device is NOT flatbed\n");

	if (buf[44] & EXT_IDTY_CAP1_ADFT)
		DBG(1, " page type ADF is installed\n");

	if (buf[44] & EXT_IDTY_CAP1_ADFS)
		DBG(1, " ADF is duplex capable\n");

	if (buf[44] & EXT_IDTY_CAP1_ADFO)
		DBG(1, " page type ADF loads from the first sheet\n");

	if (buf[44] & EXT_IDTY_CAP1_LID)
		DBG(1, " lid type option is installed\n");

	if (buf[44] & EXT_IDTY_CAP1_TPIR)
		DBG(1, " infrared scanning is supported\n");

	if (buf[44] & EXT_IDTY_CAP1_PB)
		DBG(1, " push button is supported\n");


	if (buf[45])
		DBG(1, "capabilities (2):\n");

	if (buf[45] & EXT_IDTY_CAP2_AFF)
		DBG(1, " ADF has auto form feed\n");

	if (buf[45] & EXT_IDTY_CAP2_DFD)
		DBG(1, " ADF has double feed detection\n");

	if (buf[45] & EXT_IDTY_CAP2_ADFAS)
		DBG(1, " ADF has auto scan\n");

	return SANE_STATUS_GOOD;
}

/* FS F, request scanner status */
SANE_Status
esci_request_scanner_status(SANE_Handle handle, unsigned char *buf)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (!s->hw->extended_commands)
		return SANE_STATUS_UNSUPPORTED;

	if (buf == NULL)
		return SANE_STATUS_INVAL;

	params[0] = FS;
	params[1] = 'F';

	status = e2_txrx(s, params, 2, buf, 16);
	if (status != SANE_STATUS_GOOD)
		return status;

	DBG(1, "global status   : 0x%02x\n", buf[0]);

	if (buf[0] & FSF_STATUS_MAIN_FER)
		DBG(1, " system error\n");

	if (buf[0] & FSF_STATUS_MAIN_NR)
		DBG(1, " not ready\n");

	if (buf[0] & FSF_STATUS_MAIN_WU)
		DBG(1, " scanner is warming up\n");

	if (buf[0] & FSF_STATUS_MAIN_CWU)
		DBG(1, " warmup can be cancelled\n");


	DBG(1, "adf status      : 0x%02x\n", buf[1]);

	if (buf[1] & FSF_STATUS_ADF_IST)
		DBG(11, " installed\n");
	else
		DBG(11, " not installed\n");

	if (buf[1] & FSF_STATUS_ADF_EN)
		DBG(11, " enabled\n");
	else
		DBG(11, " not enabled\n");

	if (buf[1] & FSF_STATUS_ADF_ERR)
		DBG(1, " error\n");

	if (buf[1] & FSF_STATUS_ADF_PE)
		DBG(1, " paper empty\n");

	if (buf[1] & FSF_STATUS_ADF_PJ)
		DBG(1, " paper jam\n");

	if (buf[1] & FSF_STATUS_ADF_OPN)
		DBG(1, " cover open\n");

	if (buf[1] & FSF_STATUS_ADF_PAG)
		DBG(1, " duplex capable\n");


	DBG(1, "tpu status      : 0x%02x\n", buf[2]);

	if (buf[2] & FSF_STATUS_TPU_IST)
		DBG(11, " installed\n");
	else
		DBG(11, " not installed\n");

	if (buf[2] & FSF_STATUS_TPU_EN)
		DBG(11, " enabled\n");
	else
		DBG(11, " not enabled\n");

	if (buf[2] & FSF_STATUS_TPU_ERR)
		DBG(1, " error\n");

	if (buf[1] & FSF_STATUS_TPU_OPN)
		DBG(1, " cover open\n");


	DBG(1, "device type     : 0x%02x\n", buf[3] & 0xC0);
	DBG(1, "main body status: 0x%02x\n", buf[3] & 0x3F);

	if (buf[3] & FSF_STATUS_MAIN2_PE)
		DBG(1, " paper empty\n");

	if (buf[3] & FSF_STATUS_MAIN2_PJ)
		DBG(1, " paper jam\n");

	if (buf[3] & FSF_STATUS_MAIN2_OPN)
		DBG(1, " cover open\n");

	return SANE_STATUS_GOOD;
}

SANE_Status
esci_set_scanning_parameter(SANE_Handle handle, unsigned char *buf)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (buf == NULL)
		return SANE_STATUS_INVAL;

	params[0] = FS;
	params[1] = 'W';

	DBG(10, "resolution of main scan     : %lu\n", (unsigned long) le32atoh(&buf[0]));
	DBG(10, "resolution of sub scan      : %lu\n", (unsigned long) le32atoh(&buf[4]));
	DBG(10, "offset length of main scan  : %lu\n", (unsigned long) le32atoh(&buf[8]));
	DBG(10, "offset length of sub scan   : %lu\n", (unsigned long) le32atoh(&buf[12]));
	DBG(10, "scanning length of main scan: %lu\n", (unsigned long) le32atoh(&buf[16]));
	DBG(10, "scanning length of sub scan : %lu\n", (unsigned long) le32atoh(&buf[20]));
	DBG(10, "scanning color              : %d\n", buf[24]);
	DBG(10, "data format                 : %d\n", buf[25]);
	DBG(10, "option control              : %d\n", buf[26]);
	DBG(10, "scanning mode               : %d\n", buf[27]);
	DBG(10, "block line number           : %d\n", buf[28]);
	DBG(10, "gamma correction            : %d\n", buf[29]);
	DBG(10, "brightness                  : %d\n", buf[30]);
	DBG(10, "color correction            : %d\n", buf[31]);
	DBG(10, "halftone processing         : %d\n", buf[32]);
	DBG(10, "threshold                   : %d\n", buf[33]);
	DBG(10, "auto area segmentation      : %d\n", buf[34]);
	DBG(10, "sharpness control           : %d\n", buf[35]);
	DBG(10, "mirroring                   : %d\n", buf[36]);
	DBG(10, "film type                   : %d\n", buf[37]);
	DBG(10, "main lamp lighting mode     : %d\n", buf[38]);

	status = e2_cmd_simple(s, params, 2);
	if (status != SANE_STATUS_GOOD)
		return status;

	status = e2_cmd_simple(s, buf, 64);
	if (status != SANE_STATUS_GOOD)
		return status;

	return SANE_STATUS_GOOD;
}

/* FS S */

SANE_Status
esci_get_scanning_parameter(SANE_Handle handle, unsigned char *buf)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (buf == NULL)
		return SANE_STATUS_INVAL;

	params[0] = FS;
	params[1] = 'S';

	status = e2_txrx(s, params, 2, buf, 64);
	if (status != SANE_STATUS_GOOD)
		return status;

	DBG(10, "resolution of main scan     : %lu\n",
	    (u_long) le32atoh(&buf[0]));
	DBG(10, "resolution of sub scan      : %lu\n",
	    (u_long) le32atoh(&buf[4]));
	DBG(10, "offset length of main scan  : %lu\n",
	    (u_long) le32atoh(&buf[8]));
	DBG(10, "offset length of sub scan   : %lu\n",
	    (u_long) le32atoh(&buf[12]));
	DBG(10, "scanning length of main scan: %lu\n",
	    (u_long) le32atoh(&buf[16]));
	DBG(10, "scanning length of sub scan : %lu\n",
	    (u_long) le32atoh(&buf[20]));
	DBG(10, "scanning color              : %d\n", buf[24]);
	DBG(10, "data format                 : %d\n", buf[25]);
	DBG(10, "option control              : %d\n", buf[26]);
	DBG(10, "scanning mode               : %d\n", buf[27]);
	DBG(10, "block line number           : %d\n", buf[28]);
	DBG(10, "gamma correction            : %d\n", buf[29]);
	DBG(10, "brightness                  : %d\n", buf[30]);
	DBG(10, "color correction            : %d\n", buf[31]);
	DBG(10, "halftone processing         : %d\n", buf[32]);
	DBG(10, "threshold                   : %d\n", buf[33]);
	DBG(10, "auto area segmentation      : %d\n", buf[34]);
	DBG(10, "sharpness control           : %d\n", buf[35]);
	DBG(10, "mirroring                   : %d\n", buf[36]);
	DBG(10, "film type                   : %d\n", buf[37]);
	DBG(10, "main lamp lighting mode     : %d\n", buf[38]);

	return SANE_STATUS_GOOD;
}

/* ESC # */

SANE_Status
esci_enable_infrared(SANE_Handle handle)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	int i;
	unsigned char params[2];
	unsigned char buf[64];

	unsigned char seq[32] = {
		0xCA, 0xFB, 0x77, 0x71, 0x20, 0x16, 0xDA, 0x09,
		0x5F, 0x57, 0x09, 0x12, 0x04, 0x83, 0x76, 0x77,
		0x3C, 0x73, 0x9C, 0xBE, 0x7A, 0xE0, 0x52, 0xE2,
		0x90, 0x0D, 0xFF, 0x9A, 0xEF, 0x4C, 0x2C, 0x81
	};

	DBG(8, "%s\n", __func__);

	status = esci_get_scanning_parameter(handle, buf);
	if (status != SANE_STATUS_GOOD)
		return status;

	for (i = 0; i < 32; i++) {
		buf[i] = seq[i] ^ buf[i];
	}

	params[0] = ESC;
	params[1] = '#';

	status = e2_cmd_simple(s, params, 2);
	if (status != SANE_STATUS_GOOD)
		return status;

	status = e2_cmd_simple(s, buf, 32);
	if (status != SANE_STATUS_GOOD)
		return status;

	return SANE_STATUS_GOOD;
}

SANE_Status
esci_request_command_parameter(SANE_Handle handle, unsigned char *buf)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (s->hw->cmd->request_condition == 0)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = ESC;
	params[1] = s->hw->cmd->request_condition;

	status = e2_cmd_info_block(s, params, 2, 45, &buf, NULL);
	if (status != SANE_STATUS_GOOD)
		return status;

	DBG(1, "scanning parameters:\n");
	DBG(1, "color                  : %d\n", buf[1]);
	DBG(1, "resolution             : %dx%d\n",
	    buf[4] << 8 | buf[3], buf[6] << 8 | buf[5]);
	DBG(1, "halftone               : %d\n", buf[19]);
	DBG(1, "brightness             : %d\n", buf[21]);
	DBG(1, "color correction       : %d\n", buf[28]);
	DBG(1, "gamma                  : %d\n", buf[23]);
	DBG(1, "sharpness              : %d\n", buf[30]);
	DBG(1, "threshold              : %d\n", buf[38]);
	DBG(1, "data format            : %d\n", buf[17]);
	DBG(1, "mirroring              : %d\n", buf[34]);
	DBG(1, "option unit control    : %d\n", buf[42]);
	DBG(1, "film type              : %d\n", buf[44]);
	DBG(1, "auto area segmentation : %d\n", buf[36]);
	DBG(1, "line counter           : %d\n", buf[40]);
	DBG(1, "scanning mode          : %d\n", buf[32]);
	DBG(1, "zoom                   : %d,%d\n", buf[26], buf[25]);
	DBG(1, "scan area              : %d,%d %d,%d\n",
	    buf[9] << 8 | buf[8], buf[11] << 8 | buf[10],
	    buf[13] << 8 | buf[12], buf[15] << 8 | buf[14]);
	return status;
}

/* ESC q - Request Focus Position 
 * -> ESC q
 * <- Information block
 * <- Focus position status (2)
 *	0 - Error status
 *	1 - Focus position
 */

SANE_Status
esci_request_focus_position(SANE_Handle handle, unsigned char *position)
{
	SANE_Status status;
	unsigned char *buf;
	Epson_Scanner *s = (Epson_Scanner *) handle;

	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (s->hw->cmd->request_focus_position == 0)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = ESC;
	params[1] = s->hw->cmd->request_focus_position;

	status = e2_cmd_info_block(s, params, 2, 2, &buf, NULL);
	if (status != SANE_STATUS_GOOD)
		return status;

	if (buf[0] & 0x01)
		DBG(1, "autofocus error\n");

	*position = buf[1];
	DBG(8, " focus position = 0x%x\n", buf[1]);

	free(buf);

	return status;
}

/* ESC ! - Request Push Button Status
 * -> ESC !
 * <- Information block
 * <- Push button status (1)
 */

SANE_Status
esci_request_push_button_status(SANE_Handle handle, unsigned char *bstatus)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	unsigned char params[2];
	unsigned char *buf;

	DBG(8, "%s\n", __func__);

	if (s->hw->cmd->request_push_button_status == 0) {
		DBG(1, "push button status unsupported\n");
		return SANE_STATUS_UNSUPPORTED;
	}

	params[0] = ESC;
	params[1] = s->hw->cmd->request_push_button_status;

	status = e2_cmd_info_block(s, params, 2, 1, &buf, NULL);
	if (status != SANE_STATUS_GOOD)
		return status;

	DBG(1, "push button status = %d\n", buf[0]);
	*bstatus = buf[0];

	free(buf);

	return status;
}


/*
 * Request Identity information from scanner and fill in information
 * into dev and/or scanner structures.
 * XXX information should be parsed separately.
 */
SANE_Status
esci_request_identity(SANE_Handle handle, unsigned char **buf, size_t *len)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (!s->hw->cmd->request_identity)
		return SANE_STATUS_INVAL;

	params[0] = ESC;
	params[1] = s->hw->cmd->request_identity;

	return e2_cmd_info_block(s, params, 2, 0, buf, len);
}


/*
 * Request information from scanner
 */
SANE_Status
esci_request_identity2(SANE_Handle handle, unsigned char **buf)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status;
	size_t len;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (s->hw->cmd->request_identity2 == 0)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = ESC;
	params[1] = s->hw->cmd->request_identity2;

	status = e2_cmd_info_block(s, params, 2, 0, buf, &len);
	if (status != SANE_STATUS_GOOD)
		return status;

	return status;
}

/* Send the "initialize scanner" command to the device and reset it */

SANE_Status
esci_reset(Epson_Scanner * s)
{
	SANE_Status status;
	unsigned char params[2];

	DBG(8, "%s\n", __func__);

	if (!s->hw->cmd->initialize_scanner)
		return SANE_STATUS_GOOD;

	params[0] = ESC;
	params[1] = s->hw->cmd->initialize_scanner;

	if (s->fd == -1)
		return SANE_STATUS_GOOD;

	status = e2_cmd_simple(s, params, 2);

	return status;
}

SANE_Status
esci_feed(Epson_Scanner * s)
{
	unsigned char params[1];

	DBG(8, "%s\n", __func__);

	if (!s->hw->cmd->feed)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = s->hw->cmd->feed;

	return e2_cmd_simple(s, params, 1);
}


/*
 * Eject the current page from the ADF. The scanner is opened prior to
 * sending the command and closed afterwards.
 */

SANE_Status
esci_eject(Epson_Scanner * s)
{
	unsigned char params[1];

	DBG(8, "%s\n", __func__);

	if (!s->hw->cmd->eject)
		return SANE_STATUS_UNSUPPORTED;

	if (s->fd == -1)
		return SANE_STATUS_GOOD;

	params[0] = s->hw->cmd->eject;

	return e2_cmd_simple(s, params, 1);
}

SANE_Status
esci_request_extended_status(SANE_Handle handle, unsigned char **data,
			size_t * data_len)
{
	Epson_Scanner *s = (Epson_Scanner *) handle;
	SANE_Status status = SANE_STATUS_GOOD;
	unsigned char params[2];
	unsigned char *buf;
	size_t buf_len;

	DBG(8, "%s\n", __func__);

	if (s->hw->cmd->request_extended_status == 0)
		return SANE_STATUS_UNSUPPORTED;

	params[0] = ESC;
	params[1] = s->hw->cmd->request_extended_status;

	/* This command returns 33 bytes of data on old scanners
	 * and 42 (CMD_SIZE_EXT_STATUS) on new ones.
	 */
	status = e2_cmd_info_block(s, params, 2, CMD_SIZE_EXT_STATUS,
				       &buf, &buf_len);
	if (status != SANE_STATUS_GOOD)
		return status;

	switch (buf_len) {
	case 33:
	case 42:
		break;
	default:
		DBG(1, "%s: unknown reply length (%lu)\n", __func__,
			(unsigned long) buf_len);
		break;
	}

	DBG(4, "main = %02x, ADF = %02x, TPU = %02x, main 2 = %02x\n",
		buf[0], buf[1], buf[6], buf[11]);

	if (buf[0] & EXT_STATUS_FER)
		DBG(1, "system error\n");

	if (buf[0] & EXT_STATUS_WU)
		DBG(1, "scanner is warming up\n");

	if (buf[1] & EXT_STATUS_ERR)
		DBG(1, "ADF: other error\n");

	if (buf[1] & EXT_STATUS_PE)
		DBG(1, "ADF: no paper\n");

	if (buf[1] & EXT_STATUS_PJ)
		DBG(1, "ADF: paper jam\n");

	if (buf[1] & EXT_STATUS_OPN)
		DBG(1, "ADF: cover open\n");

	if (buf[6] & EXT_STATUS_ERR)
		DBG(1, "TPU: other error\n");

	/* give back a pointer to the payload
	 * if the user requested it, otherwise
	 * free it.
	 */

	if (data)
		*data = buf;
	else
		free(buf);

	if (data_len)
		*data_len = buf_len;

	return status;
}