summaryrefslogtreecommitdiff
path: root/backend/pieusb_scancmd.c
blob: a86f8fea58b24afb9f668ddcaa0b01efac00c261 (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
/* sane - Scanner Access Now Easy.

   pieusb_scancmd.c

   Copyright (C) 2012-2015 Jan Vleeshouwers, Michael Rickmann, Klaus Kaempf

   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.  */

/* =========================================================================
 *
 * Pieusb scanner commands
 *
 * Each scanner command has its own function.
 * See the sort description preceeding each function.
 *
 * ========================================================================= */

#define DEBUG_DECLARE_ONLY

#include "pieusb.h"
#include "pieusb_scancmd.h"
#include "pieusb_usb.h"

#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif

#include <time.h> /* for time() */

static void _prep_scsi_cmd(SANE_Byte* command_bytes, SANE_Byte command, SANE_Word size);

/* =========================================================================
 *
 * Pieusb utility functions
 *
 * ========================================================================= */

/*
 * Get the unsigned char value in the array at given offset
 */
static SANE_Byte
_get_byte(SANE_Byte* array, SANE_Byte offset) {
    return *(array+offset);
}

/*
 * Set the array at given offset to the given unsigned char value
 */
static void
_set_byte(SANE_Byte val, SANE_Byte* array, SANE_Byte offset) {
    *(array+offset) = val;
}


/*
 * Get the unsigned short value in the array at given offset.
 * All data in structures is little-endian, so the LSB comes first
 * SANE_Int is 4 bytes, but that is not a problem.
 */
static SANE_Int
_get_short(SANE_Byte* array, SANE_Byte offset) {
    SANE_Int i = *(array+offset+1);
    i <<= 8;
    i += *(array+offset);
    return i;
}


/*
 * Put the bytes of a short int value into an unsigned char array
 * All data in structures is little-endian, so start with LSB
 */
static void
_set_short(SANE_Word val, SANE_Byte* array, SANE_Byte offset) {
    *(array+offset) = val & 0xFF;
    *(array+offset+1) = (val>>8) & 0xFF;
}


/*
 * Get the signed int value in the array at given offset.
 * All data in structures is little-endian, so the LSB comes first
 */
static SANE_Int
_get_int(SANE_Byte* array, SANE_Byte offset) {
    SANE_Int i = *(array+offset+3);
    i <<= 8;
    i += *(array+offset+2);
    i <<= 8;
    i += *(array+offset+1);
    i <<= 8;
    i += *(array+offset);
    return i;
}

#if 0 /* unused */
/*
 * Put the bytes of a signed int value into an unsigned char array
 * All data in structures is little-endian, so start with LSB
 */
static void
_set_int(SANE_Word val, SANE_Byte* array, SANE_Byte offset) {
    *(array+offset) = val & 0xFF;
    *(array+offset+1) = (val>>8) & 0xFF;
    *(array+offset+2) = (val>>16) & 0xFF;
    *(array+offset+3) = (val>>24) & 0xFF;
}
#endif

/*
 * Copy count unsigned char values from src to dst
 */
static void
_copy_bytes(SANE_Byte* dst, SANE_Byte* src, SANE_Byte count) {
    SANE_Byte k;
    for (k=0; k<count; k++) {
        *dst++ = *src++;
    }
}


/*
 * Get count unsigned short values in the array at given offset.
 * All data in structures is little-endian, so the MSB comes first.
 * SANE_Word is 4 bytes, but that is not a problem.
 */
static void
_get_shorts(SANE_Word* dst, SANE_Byte* src, SANE_Byte count) {
    SANE_Byte k;
    for (k=0; k<count; k++) {
        *dst = *(src+2*k+1);
        *dst <<= 8;
        *dst++ += *(src+2*k);
    }
}

/*
 * Copy an unsigned short array of given size
 * All data in structures is little-endian, so start with MSB of each short.
 * SANE_Word is 4 bytes, but that is not a problem. All MSB 2 bytes are ignored.
 */
static void
_set_shorts(SANE_Word* src, SANE_Byte* dst, SANE_Byte count) {
    SANE_Byte k;
    for (k=0; k<count; k++) {
        *(dst+2*k) = *src & 0xFF;
        *(dst+2*k+1) = ((*src++)>>8) & 0xFF;
    }
}


/**
 * Perform a TEST UNIT READY (SCSI command code 0x00)
 * Returns status->pieusb_status:
 * - PIEUSB_STATUS_GOOD if device is ready
 * - PIEUSB_STATUS_DEVICE_BUSY if device is still busy after timeout
 * - other SANE status code if TEST UNIT READY failed or if it returned
 *   CHECK CONDITION and REQUEST SENSE failed
 *
 * @param device_number Device number
 * @return Pieusb_Command_Status SANE_STATUS_GOOD if ready, SANE_STATUS_DEVICE_BUSY if not
 */
void
sanei_pieusb_cmd_test_unit_ready(SANE_Int device_number, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_test_unit_ready()\n");

    _prep_scsi_cmd (command, SCSI_TEST_UNIT_READY, 0);

    status->pieusb_status = sanei_pieusb_command (device_number, command, NULL, 0);

    DBG (DBG_info_scan, "sanei_pieusb_cmd_test_unit_ready() return status = %s\n", sane_strstatus(status->pieusb_status));
}

/**
 * slide action
 * @param action  SLIDE_NEXT, SLIDE_PREV, SLIDE_LAMP_ON, SLIDE_RELOAD
 * @return Pieusb_Command_Status
 */

void
sanei_pieusb_cmd_slide(SANE_Int device_number, slide_action action, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define SLIDE_DATA_SIZE 4
    SANE_Byte data[SLIDE_DATA_SIZE];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_slide(0x%02x)\n", action);

    _prep_scsi_cmd(command, SCSI_SLIDE, SLIDE_DATA_SIZE);
    memset(data, '\0', SLIDE_DATA_SIZE);
    data[0] = action;
    data[1] = 0x01;

    status->pieusb_status = sanei_pieusb_command(device_number, command, data, SLIDE_DATA_SIZE);
#undef SLIDE_DATA_SIZE
}

/**
 * Perform a REQUEST SENSE (SCSI command code 0x03)
 * Returns status->pieusb_status:
 * - PIEUSB_STATUS_GOOD is the command executes OK
 * - other SANE status code if REQUEST SENSE fails
 * The sense fields in status are always 0. A REQUEST SENSE is not repeated if
 * the device returns PIEUSB_STATUS_DEVICE_BUSY.
 *
 * @param device_number Device number
 * @param sense Sense data
 * @param status Command result status
 * @see struc Pieusb_Sense
 */
void
sanei_pieusb_cmd_get_sense(SANE_Int device_number, struct Pieusb_Sense* sense, struct Pieusb_Command_Status *status, PIEUSB_Status *ret)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define DATA_SIZE 14
    SANE_Int size = DATA_SIZE;
    SANE_Byte data[DATA_SIZE];
    PIEUSB_Status st;
  SANE_Char* sd;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_sense()\n");

    _prep_scsi_cmd(command, SCSI_REQUEST_SENSE, size);

    memset(data, '\0', size);
    st = sanei_pieusb_command(device_number, command, data, size);
    if (st != PIEUSB_STATUS_GOOD) {
      status->pieusb_status = st;
      /*FIXME*/
        return;
    }

    /* Decode data recieved */
    sense->errorCode = _get_byte (data, 0);
    sense->segment = _get_byte (data, 1);
    sense->senseKey = _get_byte (data, 2);
    _copy_bytes (sense->info, data+3, 4);
    sense->addLength = _get_byte (data, 7);
    _copy_bytes (sense->cmdInfo, data+8, 4);
    sense->senseCode = _get_byte (data, 12);
    sense->senseQualifier = _get_byte (data, 13);
    status->pieusb_status = PIEUSB_STATUS_GOOD;
#undef DATA_SIZE
  DBG (DBG_info_scan, "\tsense details:\n");
  DBG (DBG_info_scan, "\t\terror......... : 0x%02x\n", sense->errorCode);
  DBG (DBG_info_scan, "\t\tsegment....... : %d\n", sense->segment);
  DBG (DBG_info_scan, "\t\tsenseKey...... : 0x%02x\n", sense->senseKey);
  DBG (DBG_info_scan, "\t\tinfo.......... : %02x %02x %02x %02x\n", sense->info[0], sense->info[1], sense->info[2], sense->info[3]);
  DBG (DBG_info_scan, "\t\taddLength..... : %d\n", sense->addLength);
  DBG (DBG_info_scan, "\t\tcmdInfo....... : %02x %02x %02x %02x\n", sense->cmdInfo[0], sense->cmdInfo[1], sense->cmdInfo[2], sense->cmdInfo[3]);
  DBG (DBG_info_scan, "\t\tsenseCode..... : 0x%02x\n", sense->senseCode);
  DBG (DBG_info_scan, "\t\tsenseQualifier : 0x%02x\n", sense->senseQualifier);
  sd = sanei_pieusb_decode_sense (sense, ret?ret:&st);
  DBG (DBG_info_scan, "\tsense: %s\n", sd);
  free(sd);
}

/**
 * Read the halftone pattern with the specified index. This requires two
 * commands, one to ask the device to prepare the pattern, and one to read it.
 *
 * @param device_number Device number
 * @param index index of halftone pattern
 * @param pattern Halftone pattern (not implemented)
 * @return Pieusb_Command_Status
 * @see Pieusb_Halftone_Pattern
 */
void
sanei_pieusb_cmd_get_halftone_pattern(SANE_Int device_number, SANE_Int index, struct Pieusb_Halftone_Pattern* pattern, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define PATTERN_SIZE 256 /* Assumed maximum pattern size */
    SANE_Int size = PATTERN_SIZE;
    SANE_Byte data[PATTERN_SIZE];
    int psize;
    SANE_Char* desc;
    PIEUSB_Status st;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_halftone_pattern()\n");

    /* Ask scanner to prepare the pattern with the given index. Only SCSI_COMMAND_LEN bytes of data. */
    _prep_scsi_cmd(command, SCSI_WRITE, SCSI_COMMAND_LEN);
    memset(data, '\0', SCSI_COMMAND_LEN);
    data[0] = SCSI_HALFTONE_PATTERN | 0x80; /* set bit 7 means prepare read */
    data[4] = index;

    st = sanei_pieusb_command(device_number, command, data, SCSI_COMMAND_LEN);
    if (st != PIEUSB_STATUS_GOOD) {
      status->pieusb_status = st;
      /* FIXME */
        return;
    }

    /* Read pattern */
    _prep_scsi_cmd(command, SCSI_READ, size);

    memset(data, '\0', size);
    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);

    /*FIXME: analyse */
    fprintf(stderr, "Halftone pattern %d:\n", index);
    psize = (data[3]<<8) + data[2];
    desc = (SANE_Char*)(data + 4 + psize);
    data[4 + psize + 16] = '\0';
    fprintf(stderr,"Descr. offset from byte 4 = %d, %16s, index = %d, size = %dx%d\n", psize, desc, data[4]&0x7F, data[6], data[7]);
#undef PATTERN_SIZE
}

/**
 * Read the scan frame with the specified index. This requires two
 * commands, one to ask the device to prepare the pattern, and one to read it.
 *
 * @param device_number Device number
 * @param frame Scan frame
 * @return Pieusb_Command_Status
 * @see Pieusb_Scan_Frame
 */
void
sanei_pieusb_cmd_get_scan_frame(SANE_Int device_number, SANE_Int index, struct Pieusb_Scan_Frame* frame, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define FRAME_SIZE 256 /* Assumed maximum frame size */
    SANE_Int size = FRAME_SIZE;
    SANE_Byte data[FRAME_SIZE];
    PIEUSB_Status st;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_scan_frame()\n");

    /* Ask scanner to prepare the scan frame with the given index. Only SCSI_COMMAND_LEN bytes of data. */
    _prep_scsi_cmd (command, SCSI_WRITE, SCSI_COMMAND_LEN);
    memset (data, '\0', SCSI_COMMAND_LEN);
    data[0] = SCSI_SCAN_FRAME | 0x80; /* set bit 7 means prepare read */
    data[4] = index;

    st = sanei_pieusb_command (device_number, command, data, SCSI_COMMAND_LEN);
    if (st != PIEUSB_STATUS_GOOD) {
      status->pieusb_status = st;
      /* FIXME */
        return;
    }

    /* Read scan frame */
    _prep_scsi_cmd (command, SCSI_READ, size);

    memset(data, '\0', size);
    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);

    /* Decode data */
    frame->index = _get_byte (data, 4);
    frame->x0 = _get_short (data, 6);
    frame->y0 = _get_short (data, 8);
    frame->x1 = _get_short (data, 10);
    frame->y1 = _get_short (data, 12);

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_scan_frame() set:\n");
    DBG (DBG_info_scan, " x0,y0 = %d,%d\n", frame->x0, frame->y0);
    DBG (DBG_info_scan, " x1,y1 = %d,%d\n", frame->x1, frame->y1);
    DBG (DBG_info_scan, " index = %d\n", frame->index);
#undef FRAME_SIZE
}

/**
 * command 17 - unknown
 */

void
sanei_pieusb_cmd_17(SANE_Int device_number, SANE_Int value, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define CMD_17_SIZE 6
    SANE_Byte data[CMD_17_SIZE];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_17(%d)\n", value);

    _prep_scsi_cmd (command, SCSI_WRITE, CMD_17_SIZE);
    memset (data, '\0', CMD_17_SIZE);
    _set_short (SCSI_CMD_17, data, 0);
    _set_short (2, data, 2);
    _set_short (value, data, 4);

    status->pieusb_status = sanei_pieusb_command (device_number, command, data, CMD_17_SIZE);
#undef CMD_17_SIZE
    if (status->pieusb_status != PIEUSB_STATUS_GOOD) {
      DBG (DBG_info_scan, "sanei_pieusb_cmd_17 failed: 0x%02x\n", status->pieusb_status);
      return;
    }
}

/**
 * Read the relative exposure time for the specified colorbits. This requires two
 * commands, one to ask the device to prepare the value, and one to read it.
 *
 * @param device_number Device number
 * @param time Relative exposure time(s)
 * @return Pieusb_Command_Status
 * @see Pieusb_Exposure_Time
 */
void
sanei_pieusb_cmd_get_exposure_time(SANE_Int device_number, SANE_Int colorbits, struct Pieusb_Exposure_Time* time, struct Pieusb_Command_Status *status)
{
    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_exposure_time(): not implemented\n");
    status->pieusb_status = PIEUSB_STATUS_INVAL;
}

/**
 * Read the highlight and shadow levels with the specified colorbits. This requires two
 * commands, one to ask the device to prepare the value, and one to read it.
 *
 * @param device_number Device number
 * @param hgltshdw Highlight and shadow level(s)
 * @return Pieusb_Command_Status
 * @see Pieusb_Highlight_Shadow
 */
void
sanei_pieusb_cmd_get_highlight_shadow(SANE_Int device_number, SANE_Int colorbits, struct Pieusb_Highlight_Shadow* hgltshdw, struct Pieusb_Command_Status *status)
{
    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_highlight_shadow(): not implemented\n");
    status->pieusb_status = PIEUSB_STATUS_INVAL;
}

/**
 * Read the shading data parameters. This requires two
 * commands, one to ask the device to prepare the value, and one to read it.
 *
 * @param device_number Device number
 * @param shading Shading data parameters
 * @return Pieusb_Command_Status
 * @see Pieusb_Shading_Parameters
 */
void
sanei_pieusb_cmd_get_shading_parms(SANE_Int device_number, struct Pieusb_Shading_Parameters_Info* shading, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define SHADING_SIZE 32
#define PREP_READ_SIZE 6
    SANE_Int size = SHADING_SIZE;
    SANE_Byte data[SHADING_SIZE];
    int k;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_shading_parms()\n");

    /* Ask scanner to prepare the scan frame with the given index. Only SCSI_COMMAND_LEN bytes of data. */
    _prep_scsi_cmd (command, SCSI_WRITE, SCSI_COMMAND_LEN);
    memset (data, '\0', PREP_READ_SIZE);
    data[0] = SCSI_CALIBRATION_INFO | 0x80; /* set bit 7 means prepare read */

    status->pieusb_status = sanei_pieusb_command (device_number, command, data, PREP_READ_SIZE);
    if (status->pieusb_status != PIEUSB_STATUS_GOOD) {
      return;
    }

    /* Read shading parameters */
    _prep_scsi_cmd(command, SCSI_READ, size);

    memset (data, '\0', size);
    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);
    if (status->pieusb_status != PIEUSB_STATUS_GOOD) {
      return;
    }

    /* Decode data [32 bytes]
       0: 95 00 type
       2: 1c 00 payload len
       4: 04    entries
       5: 06    entry size
       6: 04 00 ?
       8: 00 10 10 14 1a 1d type send recv nlines pixPerLine(2bytes)
      14: 08 10 10 14 1a 1d
      20: 10 10 10 14 1a 1d
      26: 20 10 10 14 1a 1d
     */
    for (k = 0; k < data[4]; k++) {
        shading[k].type = _get_byte (data, 8 + data[5]*k);
        shading[k].sendBits = _get_byte (data, 9 + data[5]*k);
        shading[k].recieveBits = _get_byte (data, 10 + data[5]*k);
        shading[k].nLines = _get_byte (data, 11 + data[5]*k);
        shading[k].pixelsPerLine = _get_short (data, 12 + data[5]*k);
    }
#undef PREP_READ_SIZE
#undef SHADING_SIZE
}

/**
 * Read scanned data from the scanner memory into a byte array. The lines
 * argument specifies how many lines will be read, the size argument specifies
 * the total amount of bytes in these lines. Use sanei_pieusb_cmd_get_parameters() to
 * determine the current line size and the number of available lines.\n
 * If there is scanned data available, it should be read. Waiting too long
 * causes the scan to stop, probably because a buffer is filled to its limits
 * (if so, it is approximately 2Mb in size). I haven't tried what happens if you
 * start reading after a stop. Reading to fast causes the scanner to return
 * a busy status, which is not a problem.
 * This is a SCSI READ command (code 0x08). It is distinguished from the other
 * READ commands by the context in which it is issued: see sanei_pieusb_cmd_start_scan().
 *
 * @param device_number
 * @param data
 * @param lines
 * @param size
 * @return Pieusb_Command_Status
 */
void
sanei_pieusb_cmd_get_scanned_lines(SANE_Int device_number, SANE_Byte* data, SANE_Int lines, SANE_Int size, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_scanned_lines(): %d lines (%d bytes)\n", lines, size);

    _prep_scsi_cmd (command, SCSI_READ, lines);
    memset (data, '\0', size);

    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);
}

/**
 * Set the halftone pattern with the given index to the specified pattern. The
 * command is a SCSI WRITE command (code 0x0A, write code 0x11).
 *
 * @param device_number Device number
 * @param index Pattern index (0-7)
 * @param pattern Halftone pattern (not implemented)
 * @return Pieusb_Command_Status
 * @see Pieusb_Halftone_Pattern
 */
void
sanei_pieusb_cmd_set_halftone_pattern(SANE_Int device_number, SANE_Int index, struct Pieusb_Halftone_Pattern* pattern, struct Pieusb_Command_Status *status)
{
    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_halftone_pattern(): not implemented\n");
    status->pieusb_status = PIEUSB_STATUS_INVAL;
}

/**
 * Set the scan frame with the given index to the frame. The command is a SCSI
 * WRITE command (code SCSI_WRITE, write code SCSI_SCAN_FRAME).
 *
 * @param device_number Device number
 * @param index Frame index (0-7)
 * @param frame Scan frame
 * @return Pieusb_Command_Status
 * @see Pieusb_Scan_Frame
 */
void
sanei_pieusb_cmd_set_scan_frame(SANE_Int device_number, SANE_Int index, struct Pieusb_Scan_Frame* frame, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define FRAME_SIZE 14
    SANE_Int size = FRAME_SIZE;
    SANE_Byte data[FRAME_SIZE];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_scan_frame()\n");

    _prep_scsi_cmd(command, SCSI_WRITE, size);

    DBG (DBG_info_scan, " x0,y0 = %d,%d\n", frame->x0, frame->y0);
    DBG (DBG_info_scan, " x1,y1 = %d,%d\n", frame->x1, frame->y1);
    DBG (DBG_info_scan, " index = %d\n", index);

    /* Code data */
    memset (data, '\0', size);
    _set_short (SCSI_SCAN_FRAME, data, 0);
    _set_short (size-4, data, 2); /* size: one frame, 5 shorts */
    _set_short (index, data, 4);
    _set_short (frame->x0, data, 6);
    _set_short (frame->y0, data, 8);
    _set_short (frame->x1, data, 10);
    _set_short (frame->y1, data, 12);

    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);
#undef FRAME_SIZE
}

/**
 * Set the relative exposure time to the given values. Only the first
 * Pieusb_Exposure_Time_Color is used. The command is a SCSI
 * WRITE command (code SCSI_WRITE, write code SCSI_EXPOSURE).
 *
 * @param device_number Device number
 * @param time Relative exposure time
 * @return Pieusb_Command_Status
 * @see Pieusb_Exposure_Time
 */
void
sanei_pieusb_cmd_set_exposure_time(SANE_Int device_number, struct Pieusb_Exposure_Time* time, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define EXPOSURE_DATA_SIZE 8
    SANE_Byte data[EXPOSURE_DATA_SIZE];
    struct Pieusb_Exposure_Time_Color *exptime;
    int i;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_exposure_time()\n");

    for (i = 0; i < 3; ++i) { /* R, G, B */
      _prep_scsi_cmd (command, SCSI_WRITE, EXPOSURE_DATA_SIZE);
      memset (data, '\0', EXPOSURE_DATA_SIZE);
      exptime = &(time->color[i]);
      _set_short (SCSI_EXPOSURE, data, 0);
      _set_short (EXPOSURE_DATA_SIZE-4, data, 2); /* short: RGB, short: value */
      _set_short (exptime->filter, data, 4);      /* 1: neutral, 2: R, 4: G, 8: B */
      _set_short (exptime->value, data, 6);
      status->pieusb_status = sanei_pieusb_command (device_number, command, data, EXPOSURE_DATA_SIZE);
      if (status->pieusb_status != PIEUSB_STATUS_GOOD)
	break;
    }

#undef EXPOSURE_DATA_SIZE
}

/**
 * Set the highlight and shadow levels to the given values. Only the first
 * Pieusb_Highlight_Shadow_Color is used. The command is a SCSI
 * WRITE command (code SCSI_WRITE, write code SCSI_HIGHLIGHT_SHADOW).
 *
 * @param device_number Device number
 * @param hgltshdw highlight and shadow level
 * @return Pieusb_Command_Status
 * @see Pieusb_Highlight_Shadow
 */
void
sanei_pieusb_cmd_set_highlight_shadow(SANE_Int device_number, struct Pieusb_Highlight_Shadow* hgltshdw, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define HIGHLIGHT_SHADOW_SIZE 8
    SANE_Byte data[HIGHLIGHT_SHADOW_SIZE];
    struct Pieusb_Highlight_Shadow_Color *color;
    int i;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_highlight_shadow()\n");

    for (i = 0; i < 3; ++i) { /* R, G, B */
      _prep_scsi_cmd (command, SCSI_WRITE, HIGHLIGHT_SHADOW_SIZE);
      memset (data, '\0', HIGHLIGHT_SHADOW_SIZE);
      color = &(hgltshdw->color[i]);
      _set_short (SCSI_HIGHLIGHT_SHADOW, data, 0);
      _set_short (HIGHLIGHT_SHADOW_SIZE-4, data, 2); /* short: RGB, short: value */
      _set_short (color->filter, data, 4);      /* 1: neutral, 2: R, 4: G, 8: B */
      _set_short (color->value, data, 6);
      status->pieusb_status = sanei_pieusb_command (device_number, command, data, HIGHLIGHT_SHADOW_SIZE);
      if (status->pieusb_status != PIEUSB_STATUS_GOOD)
	break;
    }

#undef HIGHLIGHT_SHADOW_SIZE
}

/**
 * Set the CCD-mask for the colors set in the given color bit mask. The mask
 * array must contain mask_size. The command is a SCSI WRITE command
 * (code 0x0A, write code 0x16).
 * (The command is able to handle more masks at once, but that is not implemented.)
 *
 * @param device_number Device number
 * @param colorbits 0000RGB0 color bit mask; at least one color bit must be set
 * @param mask CCD mask to use
 * @return Pieusb_Command_Status
 */
void
sanei_pieusb_cmd_set_ccd_mask(SANE_Int device_number, SANE_Byte colorbits, SANE_Byte* mask, SANE_Int mask_size, struct Pieusb_Command_Status *status)
{
    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_ccd_mask(): not implemented\n");
    status->pieusb_status = PIEUSB_STATUS_INVAL;
}

/* SCSI PARAM, code 0x0F */
/**
 * Get the parameters of an executed scan, such as width, lines and bytes, which
 * are needed to calculate the parameters of the READ-commands which read the
 * actual scan data.
 *
 * @param device_number Device number
 * @param parameters Scan parameters
 * @return Pieusb_Command_Status
 * @see Pieusb_Scan_Parameters
 */
void
sanei_pieusb_cmd_get_parameters(SANE_Int device_number, struct Pieusb_Scan_Parameters* parameters, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define PARAMETER_SIZE 18
    SANE_Int size = PARAMETER_SIZE;
    SANE_Byte data[PARAMETER_SIZE];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_parameters()\n");

    _prep_scsi_cmd (command, SCSI_PARAM, size);
    memset (data, '\0', size);

    status->pieusb_status = sanei_pieusb_command(device_number, command, data, size);
    if (status->pieusb_status != PIEUSB_STATUS_GOOD) {
        return;
    }

    /* cyberview:
     * 0: e6 02       width 0x2e6 - 742
     * 2: e0 02       lines 0x2e0 - 736
     * 4: e6 02       bytes 0x2e6 - 742
     * 6: 08          filterOffeset1 8
     * 7: 08          filterOffset2  8
     * 8: c9 1c 00 00 period         7369
     * c: 00 00       scsi transfer rate
     * e: d7 00       available lines 215
     * 10:00 00
     */
    /* Decode data recieved */
    parameters->width = _get_short(data, 0);
    parameters->lines = _get_short(data, 2);
    parameters->bytes = _get_short(data, 4);
    parameters->filterOffset1 = _get_byte(data, 6);
    parameters->filterOffset2 = _get_byte(data, 7);
    parameters->period = _get_int(data, 8); /* unused */
    parameters->scsiTransferRate = _get_short(data, 12); /* unused */
    parameters->availableLines = _get_short(data, 14);

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_parameters() read:\n");
    DBG (DBG_info_scan, " width = %d\n", parameters->width);
    DBG (DBG_info_scan, " lines = %d\n", parameters->lines);
    DBG (DBG_info_scan, " bytes = %d\n", parameters->bytes);
    DBG (DBG_info_scan, " offset1 = %d\n", parameters->filterOffset1);
    DBG (DBG_info_scan, " offset2 = %d\n", parameters->filterOffset2);
    DBG (DBG_info_scan, " available lines = %d\n", parameters->availableLines);
#undef PARAMETER_SIZE
}

/**
 * Read INQUIRY block from device (SCSI command code 0x12). This block contains
 * information about the properties of the scanner.
 * Returns status->pieusb_status:
 * - PIEUSB_STATUS_GOOD if the INQUIRY command succeeded
 * - PIEUSB_STATUS_DEVICE_BUSY if device is busy after repeat retries
 * - other SANE status code if INQUIRY failed or if it returned CHECK CONDITION
 *   and REQUEST SENSE failed
 *
 * @param device_number Device number
 * @param data Input or output data buffer
 * @param size Size of the data buffer
 * @return Pieusb_Command_Status
 * @see Pieusb_Scanner_Properties
 */
void
sanei_pieusb_cmd_inquiry(SANE_Int device_number, struct Pieusb_Scanner_Properties* inq, SANE_Byte size, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define INQUIRY_SIZE 256
    SANE_Byte data[INQUIRY_SIZE];
    int k;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_inquiry()\n");

    _prep_scsi_cmd (command, SCSI_INQUIRY, size);
    memset (data, '\0', INQUIRY_SIZE); /* size may be less than INQUIRY_SIZE, so prevent returning noise */

    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);
    if (status->pieusb_status != PIEUSB_STATUS_GOOD) {
        return;
    }

    /* Decode data recieved */
    inq->deviceType = _get_byte(data, 0);
    inq->additionalLength = _get_byte(data, 4);
    _copy_bytes((SANE_Byte*)(inq->vendor), data+8, 8); /* Note: not 0-terminated */
    _copy_bytes((SANE_Byte*)(inq->product), data+16, 16); /* Note: not 0-terminated */
    _copy_bytes((SANE_Byte*)(inq->productRevision), data+32, 4); /* Note: not 0-terminated */
    /* 1st Vendor-specific block, 20 bytes, see pie_get_inquiry_values(), partially: */
    inq->maxResolutionX = _get_short(data, 36);
    inq->maxResolutionY = _get_short(data, 38);
    inq->maxScanWidth = _get_short(data, 40);
    inq->maxScanHeight = _get_short(data, 42);
    inq->filters = _get_byte(data, 44);
    inq->colorDepths = _get_byte(data, 45);
    inq->colorFormat = _get_byte(data, 46);
    inq->imageFormat = _get_byte(data, 48);
    inq->scanCapability = _get_byte(data, 49);
    inq->optionalDevices = _get_byte(data, 50);
    inq->enhancements = _get_byte(data, 51);
    inq->gammaBits = _get_byte(data, 52);
    inq->lastFilter = _get_byte(data, 53);
    inq->previewScanResolution = _get_short(data, 54);
    /* 2nd vendor specific block (36 bytes at offset 96) */
    _copy_bytes((SANE_Byte*)(inq->firmwareVersion), data+96, 4); inq->firmwareVersion[4]=0x00;
    inq->halftones = _get_byte(data, 100);
    inq->minumumHighlight = _get_byte(data, 101);
    inq->maximumShadow = _get_byte(data, 102);
    inq->calibrationEquation = _get_byte(data, 103);
    inq->maximumExposure = _get_short(data ,104);
    inq->minimumExposure = _get_short(data ,106);
    inq->x0 = _get_short(data, 108);
    inq->y0 = _get_short(data, 110);
    inq->x1 = _get_short(data, 112);
    inq->y1 = _get_short(data, 114);
    inq->model = _get_short(data, 116);
    _copy_bytes((SANE_Byte*)(inq->production), data+120, 4);
    _copy_bytes((SANE_Byte*)(inq->timestamp), data+124, 20);
    _copy_bytes((SANE_Byte*)(inq->signature), data+144, 40);
    /* remove newline in signature */
    for (k=0; k<40; k++) if (inq->signature[k]==0x0a || inq->signature[k]==0x0d) inq->signature[k]=' ';
#undef INQUIRY_SIZE
}

/**
 * Set scan mode parameters, such as resolution, colors to scan, color depth,
 * color format, and a couple of scan quality settings (sharpen, skip
 * calibration, fast infrared). It performs the SCSI-command MODE SELECT,
 * code 0x15.
 *
 * @param device_number Device number
 * @param mode Mode parameters
 * @return Pieusb_Command_Status
 * @see Pieusb_Mode
 */
void
sanei_pieusb_cmd_set_mode(SANE_Int device_number, struct Pieusb_Mode* mode, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define MODE_SIZE 16
    SANE_Int size = MODE_SIZE;
    SANE_Byte data[MODE_SIZE];
    SANE_Byte quality;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_mode()\n");

    _prep_scsi_cmd(command, SCSI_MODE_SELECT, size);

    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_mode() set:\n");
    DBG (DBG_info_scan, " resolution = %d\n", mode->resolution);
    DBG (DBG_info_scan, " passes = %02x\n", mode->passes);
    DBG (DBG_info_scan, " depth = %02x\n", mode->colorDepth);
    DBG (DBG_info_scan, " color format = %02x\n", mode->colorFormat);
    DBG (DBG_info_scan, " sharpen = %d\n", mode->sharpen);
    DBG (DBG_info_scan, " skip calibration = %d\n", mode->skipShadingAnalysis);
    DBG (DBG_info_scan, " fast infrared = %d\n", mode->fastInfrared);
    DBG (DBG_info_scan, " halftone pattern = %d\n", mode->halftonePattern);
    DBG (DBG_info_scan, " line threshold = %d\n", mode->lineThreshold);

    /* Code data */
    /* cyberview
     * 00 0f entries
     * f4 01 resolution 500
     * 80    RGB (90: RGBI)
     * 04    color depth (4: 8 bit, 20: 16 bit)
     * 04    color format
     * 00
     * 01    byte order
     * 08    quality bitmask: 80=fast infrared, 08=skip shading analysis, 02=sharpen
     * 00 00
     * 00    halftone pattern
     * 80    line threshold
     * 10 00
     *
     * pieusb
     * 0: 00 0f
     * 2: e8 03 resolution 1000
     * 4: 80    passes
     * 5: 04    color depth
     * 6: 04    color format
     * 7: 00
     * 8: 01    byte order
     * 9: 02    quality bitmask: sharpen
     * a: 00 00
     * c: 00    halftone pattern
     * d: 7f    line threshold
     * e: 00 00
     */
    memset (data, '\0', size);
    _set_byte (size-1, data, 1);
    _set_short (mode->resolution, data, 2);
    _set_byte (mode->passes, data, 4);
    _set_byte (mode->colorDepth, data, 5);
    _set_byte (mode->colorFormat, data, 6);
    _set_byte (mode->byteOrder, data, 8);
    quality = 0x00;
    if (mode->sharpen) quality |= 0x02;
    if (mode->skipShadingAnalysis) quality |= 0x08;
    if (mode->fastInfrared) quality |= 0x80;
    _set_byte (quality, data, 9);
    _set_byte (mode->halftonePattern, data, 12);
    _set_byte (mode->lineThreshold, data, 13);
    _set_byte (0x10, data, 14); /* ? */

    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);
#undef MODE_SIZE
}

/* SCSI COPY, code 0x18 */
/**
 * Get the currently used CCD-mask, which defines which pixels have been used in
 * the scan, and which allows to relate scan data to shading data. A mask is a
 * 5340 byte array which consists only contains the values 0x00 and 0x70. A
 * value of 0x00 indicates the pixel is used, a value of 0x70 that it is not.\n
 * The number of 0x00 bytes equals the number of pixels on a line.\n
 * The mask begins with a number of 0x70 bytes equal to the scan frame x0-value
 * divided by 2.\n
 * The SCSI-command COPY (code 0x18) is used for function.
 *
 * @param device_number Device number
 * @param mask
 * @return Pieusb_Command_Status
 */
void
sanei_pieusb_cmd_get_ccd_mask(SANE_Int device_number, SANE_Byte* mask, SANE_Int mask_size, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_ccd_mask()\n");

    _prep_scsi_cmd (command, SCSI_COPY, mask_size);

    memset (mask, '\0', mask_size);
    status->pieusb_status = sanei_pieusb_command (device_number, command, mask, mask_size);
}

/**
 * Get scan mode parameters, such as resolution, colors to scan, color depth,
 * color format, and a couple of scan quality settings (sharpen, skip
 * calibration, fast infrared). It performs the SCSI-command MODE SELECT,
 * code 0x1A.
 *
 * @param device_number Device number
 * @param mode Mode parameters
 * @return Pieusb_Command_Status
 * @see Pieusb_Mode
 */
void
sanei_pieusb_cmd_get_mode(SANE_Int device_number, struct Pieusb_Mode* mode, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define MODE_SIZE 16
    SANE_Int size = MODE_SIZE;
    SANE_Byte data[MODE_SIZE];
    SANE_Byte quality;

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_mode()\n");

    _prep_scsi_cmd (command, SCSI_MODE_SENSE, size);
    memset (data, '\0', size);

    status->pieusb_status = sanei_pieusb_command(device_number, command, data, size);
    if (status->pieusb_status != PIEUSB_STATUS_GOOD) {
        return;
    }

    /* Decode data recieved */
    mode->resolution = _get_short (data, 2);
    mode->passes = _get_byte (data, 4);
    mode->colorDepth = _get_byte (data, 5);
    mode->colorFormat = _get_byte (data, 6);
    mode->byteOrder = _get_byte (data, 8);
    quality = _get_byte (data, 9);
    mode->sharpen = (quality |= 0x02) ? SANE_TRUE : SANE_FALSE;
    mode->skipShadingAnalysis = (quality |= 0x08) ? SANE_TRUE : SANE_FALSE;
    mode->fastInfrared = (quality |= 0x80) ? SANE_TRUE : SANE_FALSE;
    mode->halftonePattern = _get_byte (data, 12);
    mode->lineThreshold = _get_byte (data, 13);

    DBG (DBG_info_scan, "cmdGetMode():\n");
    DBG (DBG_info_scan, " resolution = %d\n", mode->resolution);
    DBG (DBG_info_scan, " passes = %02x\n", mode->passes);
    DBG (DBG_info_scan, " depth = %02x\n", mode->colorDepth);
    DBG (DBG_info_scan, " color format = %02x\n", mode->colorFormat);
    DBG (DBG_info_scan, " sharpen = %d\n", mode->sharpen);
    DBG (DBG_info_scan, " skip calibration = %d\n", mode->skipShadingAnalysis);
    DBG (DBG_info_scan, " fast infrared = %d\n", mode->fastInfrared);
    DBG (DBG_info_scan, " halftone pattern = %d\n", mode->halftonePattern);
    DBG (DBG_info_scan, " line threshold = %d\n", mode->lineThreshold);
#undef MODE_SIZE
}

/**
 * Start a scan (SCSI SCAN command, code 0x1B, size byte = 0x01).\n
 * There are four phases in a scan process. During each phase a limited number of
 * commands is available. The phases are:\n
 * 1. Calibration phase: make previously collected shading correction data available\n
 * 2. Line-by-line scan & read phase\n
 * 3. Output CCD-mask phase\n
 * 4. Scan and output scan data phase\n
 *
 * The calibration phase is skipped if Pieusb_Mode.skipCalibration is set. If
 * the scanner determines a calibration is necessary, a CHECK CONDIDITION response
 * is returned. Available command during this phase:\n
 * 1. sanei_pieusb_cmd_test_unit_ready()\n
 * 2. sanei_pieusb_cmd_get_scanned_lines(): read shading correction lines\n
 * 3. sanei_pieusb_cmd_stop_scan: abort scanning process\n
 * 4. sanei_pieusb_cmd_get_gain_offset() : the settings are generated during the initialisation of this phase, so they are current\n
 * 5. cmdSetSettings(): settings take effect in the next scan phase\n\n
 * The line-by-line phase is only entered if Pieusb_Mode.div_10[0] bit 5 is
 * set. It is not implemented.\n\n
 * In the CCD-mask output phase the CCD-mask is read. Available command during this phase:\n
 * 1. sanei_pieusb_cmd_test_unit_ready()\n
 * 2. sanei_pieusb_cmd_get_ccd_mask()\n
 * 3. sanei_pieusb_cmd_stop_scan: abort scanning process\n\n
 * In the 'scan and output scan data' phase, the slide is scanned while data is
 * read in the mean time. Available command during this phase:\n
 * 1. sanei_pieusb_cmd_test_unit_ready()\n
 * 2. sanei_pieusb_cmd_get_scanned_lines()\n
 * 2. sanei_pieusb_cmd_get_parameters()\n
 * 4. sanei_pieusb_cmd_stop_scan: abort scanning process\n
 *
 * @param device_number Device number
 * @return Pieusb_Command_Status
 */
void
sanei_pieusb_cmd_start_scan(SANE_Int device_number, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_start_scan()\n");

    _prep_scsi_cmd (command, SCSI_SCAN, 1);

    status->pieusb_status = sanei_pieusb_command (device_number, command, NULL, 0);
}

/**
 * Stop a scan started with sanei_pieusb_cmd_start_scan(). It issues a SCSI SCAN command,
 * code 0x1B, with size byte = 0x00.
 *
 * @param device_number Device number
 * @return Pieusb_Command_Status
 */
void
sanei_pieusb_cmd_stop_scan(SANE_Int device_number, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_stop_scan()\n");

    _prep_scsi_cmd (command, SCSI_SCAN, 0);

    status->pieusb_status = sanei_pieusb_command (device_number, command, NULL, 0);
}

/**
 * Set scan head to a specific position, depending on the value for mode:\n
 * mode = 1: Returns the scan head to the resting position, after a short move
 * forward. If this command is left out between two scans, the second scan is
 * up-down-mirrored, and scanning starts where the proevious scan stopped.\n
 * mode = 2: Resets the scan head an then moves it forward depending on 'size',
 * but it is a bit unpredictable to what position. The scanner may attempt to
 * move the head past its physical end position. The mode is not implemented.\n
 * mode = 3: This command positions the scan head to the start of the slide.\n
 * mode = 4 or 5: The command forwards (4) or retreats (5) the scan head the
 * given amount of steps (in size).\n
 * The SCSI code is 0xD2, there is no related command name.
 *
 * @param device_number Device number
 * @param mode
 * @param size
 * @return Pieusb_Command_Status
 */
void
sanei_pieusb_cmd_set_scan_head(SANE_Int device_number, SANE_Int mode, SANE_Int steps, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define SCAN_HEAD_SIZE 4
    SANE_Int size = SCAN_HEAD_SIZE;
    SANE_Byte data[SCAN_HEAD_SIZE];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_scan_head()\n");

    _prep_scsi_cmd (command, SCSI_SET_SCAN_HEAD, size);

    /* Code data */
    memset (data, '\0', size);
    switch (mode) {
        case 1:
            data[0] = 2;
            break;
        case 2:
            DBG (DBG_error, "sanei_pieusb_cmd_set_scan_head() mode 2 unreliable, possibly dangerous\n");
            status->pieusb_status = PIEUSB_STATUS_INVAL;
            return;
        case 3:
            data[0] = 8;
            break;
        case 4:
            data[0] = 0; /* forward */
            data[2] = (steps>>8) & 0xFF;
            data[3] = steps & 0xFF;
            break;
        case 5:
            data[0] = 1; /* backward */
            data[2] = (steps>>8) & 0xFF;
            data[3] = steps & 0xFF;
            break;
    }

    status->pieusb_status = sanei_pieusb_command(device_number, command, data, size);
#undef SCAN_HEAD_SIZE
}

/**
 * Get internal scanner settings which have resulted from an auto-calibration
 * procedure. This procedure only runs when calibrating (Scan phase 1), so the
 * data returned are relatively static.\n
 * The SCSI code is 0xD7, there is no related command name.
 *
 * @param device_number Device number
 * @param settings Settings for gain and offset for the four colors RGBI
 * @return Pieusb_Command_Status
 * @see Pieusb_Settings
 */
void
sanei_pieusb_cmd_get_gain_offset(SANE_Int device_number, struct Pieusb_Settings* settings, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define GAIN_OFFSET_SIZE 103
    SANE_Int size = GAIN_OFFSET_SIZE;
    SANE_Byte data[GAIN_OFFSET_SIZE];
    int k;
    SANE_Byte val[3];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_get_gain_offset()\n");

    _prep_scsi_cmd (command, SCSI_READ_GAIN_OFFSET, size);

    memset (data, '\0', size);
    status->pieusb_status = sanei_pieusb_command(device_number, command, data, size);
    if (status->pieusb_status != PIEUSB_STATUS_GOOD) {
        return;
    }

    /* Decode data received */
    _get_shorts (settings->saturationLevel, data+54, 3);
    _get_shorts (settings->exposureTime, data+60, 3);
    _copy_bytes (val, data+66, 3);
    for (k = 0; k < 3; k++) settings->offset[k] = val[k];
    _copy_bytes (val, data+72, 3);
    for (k = 0; k < 3; k++) settings->gain[k] = val[k];
    settings->light = _get_byte (data, 75);
    settings->exposureTime[3] = _get_short (data, 98);
    settings->offset[3] = _get_byte (data, 100);
    settings->gain[3] = _get_byte (data, 102);

    DBG (DBG_info, "sanei_pieusb_cmd_get_gain_offset() set:\n");
    DBG (DBG_info, " saturationlevels = %d-%d-%d\n", settings->saturationLevel[0], settings->saturationLevel[1], settings->saturationLevel[2]);
    DBG (DBG_info, " ---\n");
    DBG (DBG_info, " exposure times = %d-%d-%d-%d\n", settings->exposureTime[0], settings->exposureTime[1], settings->exposureTime[2], settings->exposureTime[3]);
    DBG (DBG_info, " gain = %d-%d-%d-%d\n", settings->gain[0], settings->gain[1], settings->gain[2], settings->gain[3]);
    DBG (DBG_info, " offset = %d-%d-%d-%d\n", settings->offset[0], settings->offset[1], settings->offset[2], settings->offset[3]);
    DBG (DBG_info, " light = %02x\n", settings->light);
    DBG (DBG_info, " double times = %02x\n", settings->doubleTimes);
    DBG (DBG_info, " extra entries = %02x\n", settings->extraEntries);
#undef GAIN_OFFSET_SIZE
}


/**
 * Set internal scanner settings such as gain and offset.\n
 * There are two effective moments for this command:\n
 * 1. For a scan without calibration phase: before the sanei_pieusb_cmd_start_scan() command;
 * 2. For a sccan with calibration phase: before (or during) reading the shading reference data.
 * The SCSI code is 0xDC, there is no related command name.
 *
 * @param device_number Device number
 * @param settings Settings for gain and offset for the four colors RGBI
 * @return Pieusb_Command_Status
 * @see Pieusb_Settings
 */
void
sanei_pieusb_cmd_set_gain_offset(SANE_Int device_number, struct Pieusb_Settings* settings, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define GAIN_OFFSET_SIZE 29
    SANE_Int size = GAIN_OFFSET_SIZE;
    SANE_Byte data[GAIN_OFFSET_SIZE];
    int k;
    SANE_Byte val[3];

    DBG (DBG_info_scan, "sanei_pieusb_cmd_set_gain_offset()\n");

    _prep_scsi_cmd (command, SCSI_WRITE_GAIN_OFFSET, size);

    DBG (DBG_info, "sanei_pieusb_cmd_set_gain_offset() set:\n");
    DBG (DBG_info, " exposure times = %d-%d-%d-%d\n", settings->exposureTime[0], settings->exposureTime[1], settings->exposureTime[2], settings->exposureTime[3]);
    DBG (DBG_info, " gain = %d-%d-%d-%d\n", settings->gain[0], settings->gain[1], settings->gain[2], settings->gain[3]);
    DBG (DBG_info, " offset = %d-%d-%d-%d\n", settings->offset[0], settings->offset[1], settings->offset[2], settings->offset[3]);
    DBG (DBG_info, " light = %02x\n", settings->light);
    DBG (DBG_info, " double times = %02x\n", settings->doubleTimes);
    DBG (DBG_info, " extra entries = %02x\n", settings->extraEntries);

    /* Code data */
    memset (data, '\0', size);
    _set_shorts (settings->exposureTime, data, 3);
    for (k = 0; k < 3; k++) {
      val[k] = settings->offset[k];
    }
    _copy_bytes (data + 6, val, 3);
    for (k = 0; k < 3; k++) {
      val[k] = settings->gain[k];
    }
    _copy_bytes (data + 12, val, 3);
    _set_byte (settings->light, data, 15);
    _set_byte (settings->extraEntries, data, 16);
    _set_byte (settings->doubleTimes, data, 17);
    _set_short (settings->exposureTime[3], data, 18);
    _set_byte (settings->offset[3], data, 20);
    _set_byte (settings->gain[3], data, 22);
    /*
     * pieusb-get_gain_offset:
     *  00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
     *  00000010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
     *  00000020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
     *  00000030: a9 00 88 00 b1 00 00 00 00 00 00 00 04 10 04 10 )...1...........
     *  00000040: 04 10 53 4f 6e 00 00 00 2e 21 21 05 04 10 df 2d ..SOn....!!..._-
     *  00000050: a3 5c e7 f2 a1 2c b3 c4 42 df 32 42 eb 82 8e e0 #\gr!,3DB_2Bk..`
     *  00000060: 87 be 04 10 4f 00 2c                            .>..O.,
     *
     * cyberview:
     * 00000000: 65 22  57 18  19 19 - exposure time RGB
     * 00000006: 51 4e 6a - offset RGB
     * 00000009: 00 00 00
     * 0000000c: 21 21 21 - gain RGB
     * 0000000f: 05 - light
     * 00000010: 01 - extra entries
     * 00000011: 00 - double times
     * 00000012: 04 10 - exposure time I
     * 00000014: 4e 00 - offset I
     * 00000016: 2a - gain I
     * 00000017: 00 00 00 00 00 00
     *
     * pieusb:
     * 00000000: 04 10  04 10  04 10 - exposure time RGB
     * 00000006: 53 4f 6e - offset RGB
     * 00000009: 00 00 00
     * 0000000c: 2e 21 21 - gain RGB
     * 0000000f: 05 - light
     * 00000010: 00 - extra entries
     * 00000011: 00 - double times
     * 00000012: 04 10 - exposure time I
     * 00000014: 4f 00 - offset I
     * 00000016: 2c    - gain I
     * 00000017: 00 00 00 00 00 00
     */

    status->pieusb_status = sanei_pieusb_command(device_number, command, data, size);
#undef GAIN_OFFSET_SIZE
}

/**
 * Get scanner state information: button pushed,
 * warming up, scanning.
 *
 * @param device_number Device number
 * @param state State information
 * @return Pieusb_Command_Status
 */
void
sanei_pieusb_cmd_read_state(SANE_Int device_number, struct Pieusb_Scanner_State* state, struct Pieusb_Command_Status *status)
{
    SANE_Byte command[SCSI_COMMAND_LEN];
#define GET_STATE_SIZE 12
    SANE_Byte data[GET_STATE_SIZE];
    SANE_Int size = GET_STATE_SIZE;

    /* Execute READ STATUS command */
    DBG (DBG_info_scan, "sanei_pieusb_cmd_read_state()\n");

    _prep_scsi_cmd (command, SCSI_READ_STATE, size);

    memset (data, '\0', size);
    status->pieusb_status = sanei_pieusb_command (device_number, command, data, size);

    if (status->pieusb_status == PIEUSB_STATUS_WARMING_UP
        || status->pieusb_status == PIEUSB_STATUS_DEVICE_BUSY) {
      data[5] = 1;
      status->pieusb_status = PIEUSB_STATUS_GOOD;
    }
    /* Decode data recieved */
    state->buttonPushed = _get_byte(data, 0);
    state->warmingUp = _get_byte(data, 5);
    state->scanning = _get_byte(data, 6);
/*    state->busy = _get_byte(data, 8); */
    DBG (DBG_info_scan, "sanei_pieusb_cmd_read_state(): button %d, warmingUp %d, scanning %d, busy? %d\n", state->buttonPushed, state->warmingUp, state->scanning, _get_byte(data, 8));
#undef GET_STATE_SIZE
}

/**
 * Prepare SCSI_COMMAND_LEN-byte command array with command code and size value
 *
 * @param command
 * @param code
 * @param size
 */
static void
_prep_scsi_cmd(SANE_Byte* command, SANE_Byte code, SANE_Word size)
{
    memset(command, '\0', SCSI_COMMAND_LEN);
    command[0] = code;
    command[3] = (size>>8) & 0xFF; /* lsb first */
    command[4] = size & 0xFF;
}