1 /* sane - Scanner Access Now Easy.
2
3 pieusb_usb.c
4
5 Copyright (C) 2012-2015 Jan Vleeshouwers, Michael Rickmann, Klaus Kaempf
6
7 This file is part of the SANE package.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22 As a special exception, the authors of SANE give permission for
23 additional uses of the libraries contained in this release of SANE.
24
25 The exception is that, if you link a SANE library with other files
26 to produce an executable, this does not by itself cause the
27 resulting executable to be covered by the GNU General Public
28 License. Your use of that executable is in no way restricted on
29 account of linking the SANE library code into it.
30
31 This exception does not, however, invalidate any other reasons why
32 the executable file might be covered by the GNU General Public
33 License.
34
35 If you submit changes to SANE to the maintainers to be included in
36 a subsequent release, you agree by submitting the changes that
37 those changes may be distributed with this exception intact.
38
39 If you write modifications of your own for SANE, it is your choice
40 whether to permit this exception to apply to your modifications.
41 If you do not wish that, delete this exception notice. */
42
43 #define DEBUG_DECLARE_ONLY
44 #include "pieusb.h"
45 #include "pieusb_scancmd.h"
46 #include "pieusb_usb.h"
47
48 #include "../include/sane/sanei_usb.h"
49 #include <unistd.h> /* usleep */
50 #include <time.h> /* time */
51
52 /* USB functions */
53
54 static SANE_Status _ctrl_out_byte(SANE_Int device_number, SANE_Int port, SANE_Byte b);
55 static SANE_Status _bulk_size(SANE_Int device_number, unsigned int size);
56 static SANE_Status _ctrl_in_byte(SANE_Int device_number, SANE_Byte* b);
57 static SANE_Status _bulk_in(SANE_Int device_number, SANE_Byte* data, size_t *size);
58 static SANE_Status _ieee_command(SANE_Int device_number, SANE_Byte command);
59
60 /* Defines for use in USB functions */
61
62 #define REQUEST_TYPE_IN (USB_TYPE_VENDOR | USB_DIR_IN)
63 #define REQUEST_TYPE_OUT (USB_TYPE_VENDOR | USB_DIR_OUT)
64 #define REQUEST_REGISTER 0x0c
65 #define REQUEST_BUFFER 0x04
66 #define ANYINDEX 0x00 /* wIndex value for USB control transfer - value is irrelevant */
67
68 /* from libieee1284 */
69 #define C1284_NSTROBE 0x01
70 #define C1284_NINIT 0x04
71
72 /* usb via ieee1284 */
73 #define IEEE1284_ADDR 0x00
74 #define IEEE1284_RESET 0x30
75 #define IEEE1284_SCSI 0xe0
76
77 #define PORT_SCSI_SIZE 0x0082
78 #define PORT_SCSI_STATUS 0x0084
79 #define PORT_SCSI_CMD 0x0085
80 #define PORT_PAR_CTRL 0x0087 /* IEEE1284 parallel control */
81 #define PORT_PAR_DATA 0x0088 /* IEEE1284 parallel data */
82
83 /* see also: SCSI Status Codes http://www.t10.org/lists/2status.htm */
84 typedef enum {
85 USB_STATUS_OK = 0x00, /* ok */
86 USB_STATUS_READ = 0x01, /* read: send expected length, then read data */
87 USB_STATUS_CHECK = 0x02, /* check condition */
88 USB_STATUS_BUSY = 0x03, /* wait on usb */
89 USB_STATUS_AGAIN = 0x08, /* re-send scsi cmd */
90 USB_STATUS_FAIL = 0x88, /* ??? */
91 USB_STATUS_ERROR = 0xff /* usb i/o error */
92 } PIEUSB_USB_Status;
93
94 static PIEUSB_USB_Status _pieusb_scsi_command(SANE_Int device_number, SANE_Byte command[], SANE_Byte data[], SANE_Int size);
95
96 #define SENSE_CODE_WARMING_UP 4
97
98 /* Standard SCSI Sense codes*/
99 #define SCSI_NO_ADDITIONAL_SENSE_INFORMATION 0x00
100
101 struct code_text_t { int code; char *text; };
102 static struct code_text_t usb_code_text[] = {
103 { 0x00, "Ok" },
104 { 0x01, "Read" },
105 { 0x02, "Check" },
106 { 0x03, "Busy" },
107 { 0x08, "Again" },
108 { 0xff, "Error" },
109 { -1, NULL }
110 };
111
112 static struct code_text_t scsi_code_text[] = {
113 { 0x00, "Test Unit Ready" }
114 ,{ 0x01, "Calibrate" }
115 ,{ 0x03, "Request Sense" }
116 ,{ 0x04, "Format" }
117 ,{ 0x08, "Read" }
118 ,{ 0x0a, "Write" }
119 ,{ 0x0f, "Get Param" }
120 ,{ 0x10, "Mark" }
121 ,{ 0x11, "Space" }
122 ,{ 0x12, "Inquiry" }
123 ,{ 0x15, "Mode Select" }
124 ,{ 0x16, "Reserve Unit" }
125 ,{ 0x18, "Copy" }
126 ,{ 0x1a, "Mode Sense" }
127 ,{ 0x1b, "Scan" }
128 ,{ 0x1d, "Diagnose" }
129 ,{ 0xa8, "Read Extended" }
130 ,{ 0xd1, "Slide" }
131 ,{ 0xd2, "Set Scan Head" }
132 ,{ 0xd7, "Read Gain Offset" }
133 ,{ 0xdc, "Write Gain Offset" }
134 ,{ 0xdd, "Read State" }
135 ,{ -1, NULL }
136 };
137
138 static char *
code_to_text(struct code_text_t * list,int code)139 code_to_text(struct code_text_t *list, int code)
140 {
141 while (list && list->text) {
142 if (list->code == code)
143 return list->text;
144 list++;
145 }
146 return "**unknown**";
147 }
148
149 /**
150 * Convert PIEUSB_Status to SANE_Status
151 */
152 SANE_Status
sanei_pieusb_convert_status(PIEUSB_Status status)153 sanei_pieusb_convert_status(PIEUSB_Status status)
154 {
155 return (SANE_Status)status;
156 }
157
158
159 /**
160 * hex dump 'size' bytes starting at 'ptr'
161 */
162 static void
_hexdump(char * msg,unsigned char * ptr,int size)163 _hexdump(char *msg, unsigned char *ptr, int size)
164 {
165 unsigned char *lptr = ptr;
166 int count = 0;
167 long start = 0;
168 long clipped = 0;
169
170 if (DBG_info_proc > DBG_LEVEL)
171 return;
172
173 if (size > 127) {
174 clipped = size;
175 size = 128;
176 }
177 while (size-- > 0)
178 {
179 if ((count % 16) == 0)
180 {
181 fprintf (stderr, "%s\t%08lx:", msg?msg:"", start);
182 msg = NULL;
183 }
184 fprintf (stderr, " %02x", *ptr++);
185 count++;
186 start++;
187 if (size == 0)
188 {
189 while ((count % 16) != 0)
190 {
191 fprintf (stderr, " ");
192 count++;
193 }
194 }
195 if ((count % 16) == 0)
196 {
197 fprintf (stderr, " ");
198 while (lptr < ptr)
199 {
200 unsigned char c = *lptr & 0x7f;
201 fprintf (stderr, "%c", ((c < 0x20)||(c == 0x7f)) ? '.' : c);
202 lptr++;
203 }
204 fprintf (stderr, "\n");
205 }
206 }
207 if ((count % 16) != 0)
208 fprintf (stderr, "\n");
209 if (clipped > 0)
210 fprintf (stderr, "\t%08lx bytes clipped\n", clipped);
211
212 fflush(stderr);
213 return;
214 }
215
216
217 /* =========================================================================
218 *
219 * USB functions
220 *
221 * ========================================================================= */
222
223 /**
224 * Send a command to the device, retry 10 times if device is busy
225 * and return SENSE data in the sense fields of status if there is a CHECK
226 * CONDITION response from the command.
227 * If the REQUEST SENSE command fails, the SANE status code is unequal to
228 * PIEUSB_STATUS_GOOD and the sense fields are empty.
229 *
230 * @param device_number Device number
231 * @param command Command array
232 * @param data Input or output data buffer
233 * @param size Size of the data buffer
234 * @param status Pieusb_Command_Status
235 */
236
237 PIEUSB_Status
sanei_pieusb_command(SANE_Int device_number,SANE_Byte command[],SANE_Byte data[],SANE_Int size)238 sanei_pieusb_command(SANE_Int device_number, SANE_Byte command[], SANE_Byte data[], SANE_Int size)
239 {
240 #define MAXTIME 60 /* max 60 seconds */
241 time_t start;
242 SANE_Status sane_status;
243 PIEUSB_Status ret = PIEUSB_STATUS_DEVICE_BUSY;
244 SANE_Byte usbstat;
245 PIEUSB_USB_Status usb_status = USB_STATUS_AGAIN;
246
247 DBG (DBG_info_usb, "*** sanei_pieusb_command(%02x:%s): size 0x%02x\n", command[0], code_to_text (scsi_code_text, command[0]), size);
248
249 start = time(NULL);
250 while ((time(NULL)-start) < MAXTIME) {
251 DBG (DBG_info_usb, "\tsanei_pieusb_command loop, status %d:%s\n", usb_status, code_to_text (usb_code_text, usb_status));
252 if (usb_status == USB_STATUS_AGAIN) {
253 usb_status = _pieusb_scsi_command (device_number, command, data, size);
254 DBG (DBG_info_usb, "\t_pieusb_scsi_command returned %d:%s\n", usb_status, code_to_text (usb_code_text, usb_status));
255 continue;
256 }
257 if (usb_status == USB_STATUS_OK) {
258 ret = PIEUSB_STATUS_GOOD;
259 break;
260 }
261 if (usb_status == USB_STATUS_READ) {
262 DBG (DBG_error, "\tsanei_pieusb_command() 2nd STATUS_READ ?!\n");
263 ret = PIEUSB_STATUS_IO_ERROR;
264 break;
265 }
266 if (usb_status == USB_STATUS_CHECK) {
267 /* check condition */
268 struct Pieusb_Sense sense;
269 struct Pieusb_Command_Status senseStatus;
270
271 #define SCSI_REQUEST_SENSE 0x03
272
273 if (command[0] == SCSI_REQUEST_SENSE) {
274 DBG (DBG_error, "\tsanei_pieusb_command() recursive SCSI_REQUEST_SENSE\n");
275 ret = PIEUSB_STATUS_INVAL;
276 break;
277 }
278
279 /* A check sense may be a busy state in disguise
280 * It is also practical to execute a request sense command by
281 * default. The calling function should interpret
282 * PIEUSB_STATUS_CHECK_SENSE as 'sense data available'. */
283
284 sanei_pieusb_cmd_get_sense (device_number, &sense, &senseStatus, &ret);
285 if (senseStatus.pieusb_status != PIEUSB_STATUS_GOOD) {
286 DBG (DBG_error, "\tsanei_pieusb_command(): CHECK CONDITION, but REQUEST SENSE fails\n");
287 ret = senseStatus.pieusb_status;
288 }
289 break;
290 }
291 if (usb_status == USB_STATUS_BUSY) {
292 /* wait on usb */
293 sane_status = _ctrl_in_byte (device_number, &usbstat);
294 if (sane_status != SANE_STATUS_GOOD) {
295 DBG (DBG_error, "\tpieusb_scsi_command() fails status in: %d\n", sane_status);
296 ret = PIEUSB_STATUS_IO_ERROR;
297 break;
298 }
299 usb_status = usbstat;
300 if (usb_status == USB_STATUS_AGAIN) {
301 sleep(1);
302 }
303 continue;
304 }
305 if (usb_status == USB_STATUS_AGAIN) {
306 /* re-send scsi cmd */
307 continue;
308 }
309 if (usb_status == USB_STATUS_FAIL) {
310 DBG (DBG_error, "\tsanei_pieusb_command() usb status again2\n");
311 usb_status = USB_STATUS_ERROR;
312 sanei_pieusb_usb_reset(device_number);
313 ret = PIEUSB_STATUS_IO_ERROR;
314 break;
315 }
316 if (usb_status == USB_STATUS_ERROR) {
317 sanei_pieusb_usb_reset(device_number);
318 ret = PIEUSB_STATUS_IO_ERROR;
319 break;
320 }
321
322 DBG (DBG_error, "\tsanei_pieusb_command() unhandled usb status 0x%02x\n", usb_status);
323 ret = PIEUSB_STATUS_IO_ERROR;
324 break;
325 }
326 if ((time(NULL)-start) > MAXTIME) {
327 DBG (DBG_info_usb, "\tsanei_pieusb_command() timeout !\n");
328 }
329
330 DBG (DBG_info_usb, "\tsanei_pieusb_command() finished with state %d\n", ret);
331 return ret;
332 }
333
334 /**
335 * Reset IEEE1284 interface
336 *
337 * @param device_number Device number
338 * @returns SANE_Status
339 */
340
341 SANE_Status
sanei_pieusb_usb_reset(SANE_Int device_number)342 sanei_pieusb_usb_reset(SANE_Int device_number)
343 {
344 DBG (DBG_info_sane, "\tsanei_pieusb_usb_reset()\n");
345 return _ieee_command (device_number, IEEE1284_RESET);
346 }
347
348 /* http://www.t10.org/lists/2sensekey.htm */
349 static struct code_text_t sense_code_text[] = {
350 { SCSI_SENSE_NO_SENSE, "No Sense" },
351 { SCSI_SENSE_RECOVERED_ERROR, "Recovered Error" },
352 { SCSI_SENSE_NOT_READY, "Not Ready" },
353 { SCSI_SENSE_MEDIUM_ERROR, "Medium Error" },
354 { SCSI_SENSE_HARDWARE_ERROR, "Hardware Error" },
355 { SCSI_SENSE_ILLEGAL_REQUEST, "Illegal Request" },
356 { SCSI_SENSE_UNIT_ATTENTION, "Unit Attention" },
357 { SCSI_SENSE_DATA_PROTECT, "Data Protect" },
358 { SCSI_SENSE_BLANK_CHECK, "Blank Check" },
359 { SCSI_SENSE_VENDOR_SPECIFIC, "Vendor Specific" },
360 { SCSI_SENSE_COPY_ABORTED, "Copy Aborted" },
361 { SCSI_SENSE_ABORTED_COMMAND, "Aborted Command" },
362 { SCSI_SENSE_EQUAL, "Equal" },
363 { SCSI_SENSE_VOLUME_OVERFLOW, "Volume Overflow" },
364 { SCSI_SENSE_MISCOMPARE, "Miscompare" },
365 { SCSI_SENSE_COMPLETED, "Completed" },
366 { -1, NULL }
367 };
368
369
370 /**
371 * Return a textual description of the given sense code.
372 *
373 * See http://www.t10.org/lists/asc-num.txt
374 *
375 * @param sense
376 * @return description
377 */
378
379 SANE_String
sanei_pieusb_decode_sense(struct Pieusb_Sense * sense,PIEUSB_Status * status)380 sanei_pieusb_decode_sense(struct Pieusb_Sense* sense, PIEUSB_Status *status)
381 {
382 SANE_Char* desc = malloc(200);
383 SANE_Char* ptr;
384 strcpy (desc, code_to_text (sense_code_text, sense->senseKey));
385 ptr = desc + strlen(desc);
386
387 switch (sense->senseKey) {
388 case SCSI_SENSE_NOT_READY:
389 if (sense->senseCode == SENSE_CODE_WARMING_UP && sense->senseQualifier == 1) {
390 strcpy (ptr, ": Logical unit is in the process of becoming ready");
391 *status = PIEUSB_STATUS_WARMING_UP;
392 }
393 else {
394 sprintf (ptr, ": senseCode 0x%02x, senseQualifier 0x%02x", sense->senseCode, sense->senseQualifier);
395 *status = PIEUSB_STATUS_INVAL;
396 }
397 break;
398 case SCSI_SENSE_UNIT_ATTENTION:
399 if (sense->senseCode == 0x1a && sense->senseQualifier == 0) {
400 strcpy (ptr, ": Invalid field in parameter list");
401 *status = PIEUSB_STATUS_INVAL;
402 break;
403 } else if (sense->senseCode == 0x20 && sense->senseQualifier == 0) {
404 strcpy (ptr, ": Invalid command operation code");
405 *status = PIEUSB_STATUS_INVAL;
406 break;
407 } else if (sense->senseCode == 0x82 && sense->senseQualifier == 0) {
408 strcpy (ptr, ": Calibration disable not granted");
409 *status = PIEUSB_STATUS_MUST_CALIBRATE;
410 break;
411 } else if (sense->senseCode == 0x00 && sense->senseQualifier == 6) {
412 strcpy (ptr, ": I/O process terminated");
413 *status = PIEUSB_STATUS_IO_ERROR;
414 break;
415 } else if (sense->senseCode == 0x26 && sense->senseQualifier == 0x82) {
416 strcpy (ptr, ": MODE SELECT value invalid: resolution too high (vs)");
417 *status = PIEUSB_STATUS_INVAL;
418 break;
419 } else if (sense->senseCode == 0x26 && sense->senseQualifier == 0x83) {
420 strcpy (ptr, ": MODE SELECT value invalid: select only one color (vs)");
421 *status = PIEUSB_STATUS_INVAL;
422 break;
423 } else if (sense->senseCode == 0x26 && sense->senseQualifier == 0x83) {
424 strcpy (ptr, ": MODE SELECT value invalid: unsupported bit depth (vs)");
425 *status = PIEUSB_STATUS_INVAL;
426 break;
427 }
428 /*fallthru*/
429 case SCSI_SENSE_NO_SENSE:
430 case SCSI_SENSE_RECOVERED_ERROR:
431 case SCSI_SENSE_MEDIUM_ERROR:
432 case SCSI_SENSE_HARDWARE_ERROR:
433 case SCSI_SENSE_ILLEGAL_REQUEST:
434 case SCSI_SENSE_DATA_PROTECT:
435 case SCSI_SENSE_BLANK_CHECK:
436 case SCSI_SENSE_VENDOR_SPECIFIC:
437 case SCSI_SENSE_COPY_ABORTED:
438 case SCSI_SENSE_ABORTED_COMMAND:
439 case SCSI_SENSE_EQUAL:
440 case SCSI_SENSE_VOLUME_OVERFLOW:
441 case SCSI_SENSE_MISCOMPARE:
442 case SCSI_SENSE_COMPLETED:
443 default:
444 sprintf (ptr, ": senseCode 0x%02x, senseQualifier 0x%02x", sense->senseCode, sense->senseQualifier);
445 *status = PIEUSB_STATUS_INVAL;
446 }
447 return desc;
448 }
449
450 /**
451 * Prepare IEEE1284 interface
452 * Issue one of IEEE1284_ADDR, IEEE1284_RESET, or IEEE1284_SCSI
453 *
454 * @param device_number Device number
455 * @param command - IEEE1284 command
456 * @returns SANE_Status
457 */
458
459 static SANE_Status
_ieee_command(SANE_Int device_number,SANE_Byte command)460 _ieee_command(SANE_Int device_number, SANE_Byte command)
461 {
462 SANE_Status st;
463 static int sequence[] = { 0xff, 0xaa, 0x55, 0x00, 0xff, 0x87, 0x78 };
464 #define SEQUENCE_LEN 7
465 unsigned int i;
466 /* 2 x 4 + 3 bytes preceding command, then SCSI_COMMAND_LEN bytes command */
467 /* IEEE1284 command, see hpsj5s.c:cpp_daisy() */
468 for (i = 0; i < SEQUENCE_LEN; ++i) {
469 st = _ctrl_out_byte (device_number, PORT_PAR_DATA, sequence[i]);
470 if (st != SANE_STATUS_GOOD) {
471 DBG (DBG_error, "\t\t_ieee_command fails after %d bytes\n", i);
472 return st;
473 }
474 }
475 st = _ctrl_out_byte (device_number, PORT_PAR_DATA, command);
476 if (st == SANE_STATUS_GOOD) {
477 usleep(3000); /* 3.000 usec -> 3 msec */
478 st = _ctrl_out_byte (device_number, PORT_PAR_CTRL, C1284_NINIT|C1284_NSTROBE); /* CTRL_VAL_FINAL */
479 if (st == SANE_STATUS_GOOD) {
480 st = _ctrl_out_byte (device_number, PORT_PAR_CTRL, C1284_NINIT);
481 if (st == SANE_STATUS_GOOD) {
482 st = _ctrl_out_byte (device_number, PORT_PAR_DATA, 0xff);
483 if (st != SANE_STATUS_GOOD) {
484 DBG (DBG_error, "\t\t_ieee_command fails to write final data\n");
485 }
486 }
487 else {
488 DBG (DBG_error, "\t\t_ieee_command fails to reset strobe\n");
489 }
490 }
491 else {
492 DBG (DBG_error, "\t\t_ieee_command fails to set strobe\n");
493 }
494 }
495
496 return st;
497 #undef SEQUENCE_LEN
498 }
499
500 /**
501 * Send a command to the device.
502 * The command is a SCSI_COMMAND_LEN-byte array. The data-array is used for input and output.
503 * The sense-fields of Pieusb_Command_Status are cleared.
504 *
505 * @param device_number Device number
506 * @param command Command array
507 * @param data Input or output data buffer
508 * @param size Size of the data buffer
509 * @returns PIEUSB_SCSI_Status
510 */
511 static PIEUSB_USB_Status
_pieusb_scsi_command(SANE_Int device_number,SANE_Byte command[],SANE_Byte data[],SANE_Int size)512 _pieusb_scsi_command(SANE_Int device_number, SANE_Byte command[], SANE_Byte data[], SANE_Int size)
513 {
514 SANE_Status st;
515 SANE_Byte usbstat;
516 int i;
517
518 DBG (DBG_info_usb, "\t\t_pieusb_scsi_command(): %02x:%s\n", command[0], code_to_text (scsi_code_text, command[0]));
519
520 st = _ieee_command (device_number, IEEE1284_SCSI);
521 if (st != SANE_STATUS_GOOD) {
522 DBG (DBG_error, "\t\t_pieusb_scsi_command can't prep scsi cmd: %d\n", st);
523 return USB_STATUS_ERROR;
524 }
525
526 /* output command */
527 for (i = 0; i < SCSI_COMMAND_LEN; ++i) {
528 SANE_Status st;
529 st = _ctrl_out_byte (device_number, PORT_SCSI_CMD, command[i]);
530 if (st != SANE_STATUS_GOOD) {
531 DBG (DBG_error, "\t\t_pieusb_scsi_command fails command out, after %d bytes: %d\n", i, st);
532 return USB_STATUS_ERROR;
533 }
534 }
535 _hexdump ("Cmd", command, SCSI_COMMAND_LEN);
536
537 /* Verify this sequence */
538 st = _ctrl_in_byte (device_number, &usbstat);
539 if (st != SANE_STATUS_GOOD) {
540 DBG (DBG_error, "\t\t_pieusb_scsi_command fails status after command out: %d\n", st);
541 return USB_STATUS_ERROR;
542 }
543 /* Process rest of the data, if present; either input or output, possibly bulk */
544 DBG (DBG_info_usb, "\t\t_pieusb_scsi_command usbstat 0x%02x\n", usbstat);
545 if (usbstat == USB_STATUS_OK && size > 0) {
546 /*
547 * send additional data to usb
548 */
549 _hexdump ("Out", data, size);
550 for (i = 0; i < size; ++i) {
551 st = _ctrl_out_byte (device_number, PORT_SCSI_CMD, data[i]);
552 if (st != SANE_STATUS_GOOD) {
553 DBG (DBG_error, "\t\t_pieusb_scsi_command fails data out after %d bytes: %d\n", i, st);
554 return USB_STATUS_ERROR;
555 }
556 }
557 /* Verify data out */
558 st = _ctrl_in_byte (device_number, &usbstat);
559 if (st != SANE_STATUS_GOOD) {
560 DBG (DBG_error, "\t\t_pieusb_scsi_command fails status after data out: %d\n", st);
561 return USB_STATUS_ERROR;
562 }
563 }
564 else if (usbstat == USB_STATUS_READ) {
565 /* Intermediate status OK, device has made data available for reading */
566 /* Read data */
567 size_t remsize;
568 size_t partsize;
569
570 remsize = (size_t)size;
571
572 DBG (DBG_info_usb, "\t\t_pieusb_scsi_command data in\n");
573 while (remsize > 0) {
574 partsize = remsize > 0x1000000 ? 0x1000000 : remsize; /* 0xc000 must be multiples of 0x4000, see _bulk_in() */
575 /* send expected length */
576 st = _bulk_size (device_number, partsize);
577 if (st != SANE_STATUS_GOOD) {
578 DBG (DBG_error, "\t\t_pieusb_scsi_command prepare read data failed for size %u: %d\n", (unsigned int)partsize, st);
579 return USB_STATUS_ERROR;
580 }
581 /* read expected length bytes */
582 st = _bulk_in (device_number, data + size - remsize, &partsize);
583 if (st != SANE_STATUS_GOOD) {
584 DBG (DBG_error, "\t\t_pieusb_scsi_command read data failed for size %u: %d\n", (unsigned int)partsize, st);
585 return USB_STATUS_ERROR;
586 }
587 remsize -= partsize;
588 /* DBG (DBG_info, "\t\t_pieusb_scsi_command partsize %08x, remsize %08x\n", (unsigned int)partsize, (unsigned int)remsize); */
589 }
590 /* Verify data in */
591 st = _ctrl_in_byte (device_number, &usbstat);
592 if (st != SANE_STATUS_GOOD) {
593 DBG (DBG_error, "\t\t_pieusb_scsi_command fails status after data in: %d\n", st);
594 return USB_STATUS_ERROR;
595 }
596 _hexdump ("In", data, size);
597 }
598
599 return usbstat;
600 }
601
602
603 /**
604 * Simplified control transfer: one byte to given port
605 *
606 * @param device_number device number
607 * @param b byte to send to device
608 * @return SANE status
609 */
_ctrl_out_byte(SANE_Int device_number,SANE_Int port,SANE_Byte b)610 static SANE_Status _ctrl_out_byte(SANE_Int device_number, SANE_Int port, SANE_Byte b) {
611 /* int r = libusb_control_transfer(scannerHandle, CTRL_OUT, 0x0C, 0x0088, ANYINDEX, &b, 1, TIMEOUT); */
612 return sanei_usb_control_msg(device_number, REQUEST_TYPE_OUT, REQUEST_REGISTER, port, ANYINDEX, 1, &b);
613 }
614
615
616 /**
617 * Simplified control transfer for port/wValue = 0x82 - prepare bulk
618 *
619 * @param device_number device number
620 * @param size Size of bulk transfer which follows (number of bytes)
621 * @return SANE status
622 */
_bulk_size(SANE_Int device_number,unsigned int size)623 static SANE_Status _bulk_size(SANE_Int device_number, unsigned int size) {
624 SANE_Byte bulksize[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
625 bulksize[4] = size & 0xff;
626 bulksize[5] = (size >> 8) & 0xff;
627 bulksize[6] = (size >> 16) & 0xff;
628 bulksize[7] = (size >> 24) & 0xff;
629 return sanei_usb_control_msg(device_number, REQUEST_TYPE_OUT, REQUEST_BUFFER, PORT_SCSI_SIZE, ANYINDEX, 8, bulksize);
630 }
631
632
633 /*
634 * Ctrl inbound, single byte
635 */
636 /**
637 * Inbound control transfer
638 *
639 * @param device_number device number
640 * @param b byte received from device
641 * @return SANE status
642 */
_ctrl_in_byte(SANE_Int device_number,SANE_Byte * b)643 static SANE_Status _ctrl_in_byte(SANE_Int device_number, SANE_Byte* b) {
644 /* int r = libusb_control_transfer(scannerHandle, CTRL_IN, 0x0C, 0x0084, ANYINDEX, &b, 1, TIMEOUT); */
645 /* int r = libusb_control_transfer(scannerHandle, CTRL_IN, 0x0C, 0x0084, ANYINDEX, &b, 1, TIMEOUT); */
646 return sanei_usb_control_msg(device_number, REQUEST_TYPE_IN, REQUEST_REGISTER, PORT_SCSI_STATUS, ANYINDEX, 1, b);
647 }
648
649
650 /**
651 * Bulk in transfer for data, in parts of 0x4000 bytes max
652 *
653 * @param device_number device number
654 * @param data array holding or receiving data (must be preallocated)
655 * @param size ptr to size of the data array / actual size on output
656 * @return SANE status
657 */
658 static SANE_Status
_bulk_in(SANE_Int device_number,SANE_Byte * data,size_t * size)659 _bulk_in(SANE_Int device_number, SANE_Byte *data, size_t *size) {
660 size_t remaining = 0;
661 SANE_Status r = SANE_STATUS_GOOD;
662 size_t part;
663
664 remaining = *size;
665 while (remaining > 0) {
666 /* Determine bulk size */
667 part = (remaining >= 0x4000) ? 0x4000 : remaining; /* max 16k per chunk */
668 /* DBG (DBG_info, "\t\t_bulk_in: %08x @ %p, %08x rem\n", (unsigned int)part, data, (unsigned int)remaining); */
669 r = sanei_usb_read_bulk(device_number, data, &part);
670 if (r != SANE_STATUS_GOOD) {
671 break;
672 }
673 /* DBG (DBG_info, "\t\t_bulk_in: -> %d : %08x\n", r, (unsigned int)part);*/
674 remaining -= part;
675 data += part;
676 }
677 *size -= remaining;
678 return r;
679 }
680