1 /* sane - Scanner Access Now Easy.
2
3 This file is part of the SANE package, and implements a SANE backend
4 for various Corex Cardscan scanners.
5
6 Copyright (C) 2007-2015 m. allan noah
7
8 --------------------------------------------------------------------------
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <https://www.gnu.org/licenses/>.
22
23 As a special exception, the authors of SANE give permission for
24 additional uses of the libraries contained in this release of SANE.
25
26 The exception is that, if you link a SANE library with other files
27 to produce an executable, this does not by itself cause the
28 resulting executable to be covered by the GNU General Public
29 License. Your use of that executable is in no way restricted on
30 account of linking the SANE library code into it.
31
32 This exception does not, however, invalidate any other reasons why
33 the executable file might be covered by the GNU General Public
34 License.
35
36 If you submit changes to SANE to the maintainers to be included in
37 a subsequent release, you agree by submitting the changes that
38 those changes may be distributed with this exception intact.
39
40 If you write modifications of your own for SANE, it is your choice
41 whether to permit this exception to apply to your modifications.
42 If you do not wish that, delete this exception notice.
43
44 --------------------------------------------------------------------------
45
46 The source code is divided in sections which you can easily find by
47 searching for the tag "@@".
48
49 Section 1 - Init & static stuff
50 Section 2 - sane_init, _get_devices, _open & friends
51 Section 3 - sane_*_option functions
52 Section 4 - sane_start, _get_param, _read & friends
53 Section 5 - sane_close functions
54 Section 6 - misc functions
55
56 Changes:
57 v0, 2007-05-09, MAN (SANE v1.0.19)
58 - initial release
59 v1, 2008-02-14, MAN
60 - sanei_config_read has already cleaned string (#310597)
61 v2, 2010-02-10, MAN
62 - add lines_per_block config option
63 - add has_cal_buffer config option
64 - basic support for 600c
65 - clean #include lines
66 v3, 2015-11-04, MAN
67 - add USB IDs for newer model 800c
68
69 ##################################################
70 DATA FROM TRACE OF WINDOWS DRIVER:
71
72 cmd packet format:
73 cmdcode cmdlenlow cmdlenhigh cmdpayloadbytes
74
75 resp packet format:
76 respcode paperfound resplenlow resplenhigh respayloadbytes
77
78 ############ status read loop? ##################
79 >> 01 01 00 00
80 << 81 00 07 00 00 09 0c 61 c2 7a 0a
81 >> 34 00 00
82 << b4 00 00 00
83 >> 01 01 00 00
84 << 81 00 07 00 00 09 0c 61 c2 7a 0a
85 >> 34 00 00
86 << b4 00 00 00
87 >> 01 01 00 00
88 << 81 00 07 00 00 09 0c 61 c2 7a 0a
89
90 ############# scanner settings read? (0x04b8 is scan width) #############
91 >> 48 00 00
92 << c8 00 0c 00 b8 04 60 00 00 80 00 00 00 58 ca 7d
93
94 ############## color and gray calibration data read ############
95 >> 45 00 00
96 << 0x2600 bytes, bbbBBBgggGGGrrrRRRxxxXXX
97
98 ############ 34/b4 and 01/81 status loop til paper inserted ##############
99
100 >> 35 01 00 00
101 << b5 01 01 00 00
102
103 always together? {
104 >> 14 05 00 80 1b 28 00 0f
105 << 94 01 05 00 80 1b 28 00 0f
106 >> 22 01 00 00
107 << a2 01 01 00 00
108 }
109
110 >> 1a 01 00 66
111 << 9a 01 01 00 66
112
113 >> 19 03 00 51 62 49
114 << 99 01 03 00 51 62 49
115
116 ############# heat up lamp? #################
117 ===========color===================
118 three times {
119 >> 18 07 00 00 01 60 00 61 00 07
120 << 0x40 read and 0x03 read
121 the 3 byte drops from f4 f4 f4 to 17 10 08 etc.
122 }
123 ===========gray===================
124 three times {
125 >> 12 06 00 00 01 60 00 61 00
126 << 0x40 read and 0x01 read
127 }
128 the 1 byte drops from f4 to 02
129 ==================================
130
131 >> 35 01 00 00
132 << b5 01 01 00 00
133
134 >> 13 01 00 28
135 << 93 01 01 00 28
136
137 ===========color===================
138 three times {
139 >> 18 07 00 01 10 60 00 18 05 07
140 << 0xe2c0 read
141 }
142
143 14/94 and 22/a2
144
145 many times {
146 >> 18 07 00 01 10 60 00 18 05 07
147 << 0xe2c0 read
148 }
149 ===========gray===================
150 two times {
151 >> 12 06 00 01 10 60 00 18 05
152 << 0x4bc0 read
153 }
154
155 14/94 and 22/a2
156
157 many times {
158 >> 12 06 00 01 10 60 00 18 05
159 << 0x4bc0 read
160 }
161 ==================================
162
163 >> 35 01 00 ff
164 << b5 00 01 00 ff
165
166 14/94 and 22/a2
167
168 ########### discharge capacitor? ###########
169 four times {
170 >> 21 02 00 0a 00
171 << a1 00 02 00 0a 00
172 }
173
174 >> 01 01 00 00
175 << 81 00 07 00 00 09 0c 61 c2 7a 0a
176
177 >> 35 01 00 ff
178 << b5 00 01 00 ff
179
180 >> 34 00 00
181 << b4 00 00 00
182 #############################################
183
184 SANE FLOW DIAGRAM
185
186 - sane_init() : initialize backend
187 . - sane_get_devices() : query list of scanner devices
188 . - sane_open() : open a particular scanner device
189 . . - sane_set_io_mode : set blocking mode
190 . . - sane_get_select_fd : get scanner fd
191 . .
192 . . - sane_get_option_descriptor() : get option information
193 . . - sane_control_option() : change option values
194 . . - sane_get_parameters() : returns estimated scan parameters
195 . . - (repeat previous 3 functions)
196 . .
197 . . - sane_start() : start image acquisition
198 . . - sane_get_parameters() : returns actual scan parameters
199 . . - sane_read() : read image data (from pipe)
200 . . (sane_read called multiple times; after sane_read returns EOF,
201 . . loop may continue with sane_start which may return a 2nd page
202 . . when doing duplex scans, or load the next page from the ADF)
203 . .
204 . . - sane_cancel() : cancel operation
205 . - sane_close() : close opened scanner device
206 - sane_exit() : terminate use of backend
207
208 */
209
210 /*
211 * @@ Section 1 - Init
212 */
213
214 #include "../include/sane/config.h"
215
216 #include <string.h> /*memcpy...*/
217 #include <ctype.h> /*isspace*/
218
219 #include "../include/sane/sanei_backend.h"
220 #include "../include/sane/sanei_usb.h"
221 #include "../include/sane/saneopts.h"
222 #include "../include/sane/sanei_config.h"
223
224 #include "cardscan.h"
225
226 #define DEBUG 1
227 #define BUILD 3
228
229 /* values for SANE_DEBUG_CARDSCAN env var:
230 - errors 5
231 - function trace 10
232 - function detail 15
233 - get/setopt cmds 20
234 - usb cmd trace 25
235 - usb cmd detail 30
236 - useless noise 35
237 */
238
239 int global_has_cal_buffer = 1;
240 int global_lines_per_block = 16;
241
242 /* ------------------------------------------------------------------------- */
243 #define STRING_GRAYSCALE SANE_VALUE_SCAN_MODE_GRAY
244 #define STRING_COLOR SANE_VALUE_SCAN_MODE_COLOR
245
246 /*
247 * used by attach* and sane_get_devices
248 * a ptr to a null term array of ptrs to SANE_Device structs
249 * a ptr to a single-linked list of scanner structs
250 */
251 static const SANE_Device **sane_devArray = NULL;
252 static struct scanner *scanner_devList = NULL;
253
254 /*
255 * @@ Section 2 - SANE & scanner init code
256 */
257
258 /*
259 * Called by SANE initially.
260 *
261 * From the SANE spec:
262 * This function must be called before any other SANE function can be
263 * called. The behavior of a SANE backend is undefined if this
264 * function is not called first. The version code of the backend is
265 * returned in the value pointed to by version_code. If that pointer
266 * is NULL, no version code is returned. Argument authorize is either
267 * a pointer to a function that is invoked when the backend requires
268 * authentication for a specific resource or NULL if the frontend does
269 * not support authentication.
270 */
271 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback authorize)272 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
273 {
274 (void) authorize; /* get rid of compiler warning */
275
276 DBG_INIT ();
277 DBG (10, "sane_init: start\n");
278
279 sanei_usb_init();
280
281 if (version_code)
282 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD);
283
284 DBG (5, "sane_init: cardscan backend %d.%d.%d, from %s\n",
285 SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD, PACKAGE_STRING);
286
287 DBG (10, "sane_init: finish\n");
288
289 return SANE_STATUS_GOOD;
290 }
291
292 /*
293 * Called by SANE to find out about supported devices.
294 *
295 * From the SANE spec:
296 * This function can be used to query the list of devices that are
297 * available. If the function executes successfully, it stores a
298 * pointer to a NULL terminated array of pointers to SANE_Device
299 * structures in *device_list. The returned list is guaranteed to
300 * remain unchanged and valid until (a) another call to this function
301 * is performed or (b) a call to sane_exit() is performed. This
302 * function can be called repeatedly to detect when new devices become
303 * available. If argument local_only is true, only local devices are
304 * returned (devices directly attached to the machine that SANE is
305 * running on). If it is false, the device list includes all remote
306 * devices that are accessible to the SANE library.
307 *
308 * SANE does not require that this function is called before a
309 * sane_open() call is performed. A device name may be specified
310 * explicitly by a user which would make it unnecessary and
311 * undesirable to call this function first.
312 *
313 * Read the config file, find scanners with help from sanei_*
314 * store in global device structs
315 */
316 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool local_only)317 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
318 {
319 struct scanner *dev;
320 char line[PATH_MAX];
321 const char *lp;
322 FILE *fp;
323 int num_devices=0;
324 int i=0;
325
326 (void) local_only; /* get rid of compiler warning */
327
328 DBG (10, "sane_get_devices: start\n");
329
330 global_has_cal_buffer = 1;
331 global_lines_per_block = 16;
332
333 fp = sanei_config_open (CONFIG_FILE);
334
335 if (fp) {
336
337 DBG (15, "sane_get_devices: reading config file %s\n", CONFIG_FILE);
338
339 while (sanei_config_read (line, PATH_MAX, fp)) {
340
341 lp = line;
342
343 /* ignore comments */
344 if (*lp == '#')
345 continue;
346
347 /* skip empty lines */
348 if (*lp == 0)
349 continue;
350
351 if ((strncmp ("usb", lp, 3) == 0) && isspace (lp[3])) {
352 DBG (15, "sane_get_devices: looking for '%s'\n", lp);
353 sanei_usb_attach_matching_devices(lp, attach_one);
354 }
355
356 else if (!strncmp(lp, "has_cal_buffer", 14) && isspace (lp[14])) {
357
358 int buf;
359 lp += 14;
360 lp = sanei_config_skip_whitespace (lp);
361 buf = atoi (lp);
362
363 if(buf){
364 global_has_cal_buffer = 1;
365 }
366 else{
367 global_has_cal_buffer = 0;
368 }
369
370 DBG (15, "sane_get_devices: setting \"has_cal_buffer\" to %d\n",
371 global_has_cal_buffer);
372 }
373
374 else if (!strncmp(lp, "lines_per_block", 15) && isspace (lp[15])) {
375
376 int buf;
377 lp += 15;
378 lp = sanei_config_skip_whitespace (lp);
379 buf = atoi (lp);
380
381 if(buf < 1 || buf > 32){
382 DBG (15,
383 "sane_get_devices: \"lines_per_block\"=%d\n out of range",
384 buf
385 );
386 continue;
387 }
388
389 DBG (15, "sane_get_devices: \"lines_per_block\" is %d\n", buf);
390 global_lines_per_block = buf;
391 }
392
393 else{
394 DBG (5, "sane_get_devices: config line \"%s\" ignored.\n", lp);
395 }
396 }
397 fclose (fp);
398 }
399
400 else {
401 DBG (5, "sane_get_devices: no config file '%s', using defaults\n",
402 CONFIG_FILE);
403
404 DBG (15, "sane_get_devices: looking for 'usb 0x08F0 0x0005'\n");
405 sanei_usb_attach_matching_devices("usb 0x08F0 0x0005", attach_one);
406 }
407
408 for (dev = scanner_devList; dev; dev=dev->next) {
409 DBG (15, "sane_get_devices: found scanner %s\n",dev->device_name);
410 num_devices++;
411 }
412
413 DBG (15, "sane_get_devices: found %d scanner(s)\n",num_devices);
414
415 sane_devArray = calloc (num_devices + 1, sizeof (SANE_Device*));
416 if (!sane_devArray)
417 return SANE_STATUS_NO_MEM;
418
419 for (dev = scanner_devList; dev; dev=dev->next) {
420 sane_devArray[i++] = (SANE_Device *)&dev->sane;
421 }
422
423 sane_devArray[i] = 0;
424
425 *device_list = sane_devArray;
426
427 DBG (10, "sane_get_devices: finish\n");
428
429 return SANE_STATUS_GOOD;
430 }
431
432 /* callback used by sane_get_devices
433 * build the scanner struct and link to global list
434 * unless struct is already loaded, then pretend
435 */
436 static SANE_Status
attach_one(const char * device_name)437 attach_one (const char *device_name)
438 {
439 struct scanner *s;
440 int ret, i;
441 SANE_Word vid, pid;
442
443 DBG (10, "attach_one: start '%s'\n", device_name);
444
445 for (s = scanner_devList; s; s = s->next) {
446 if (strcmp (s->sane.name, device_name) == 0) {
447 DBG (10, "attach_one: already attached!\n");
448 return SANE_STATUS_GOOD;
449 }
450 }
451
452 /* build a scanner struct to hold it */
453 DBG (15, "attach_one: init struct\n");
454
455 if ((s = calloc (sizeof (*s), 1)) == NULL)
456 return SANE_STATUS_NO_MEM;
457
458 /* copy the device name */
459 s->device_name = strdup (device_name);
460 if (!s->device_name){
461 free (s);
462 return SANE_STATUS_NO_MEM;
463 }
464
465 /* connect the fd */
466 DBG (15, "attach_one: connect fd\n");
467
468 s->fd = -1;
469 ret = connect_fd(s);
470 if(ret != SANE_STATUS_GOOD){
471 free (s->device_name);
472 free (s);
473 return ret;
474 }
475
476 /* clean up the scanner struct based on model */
477 /* this is the only piece of model specific code */
478 sanei_usb_get_vendor_product(s->fd,&vid,&pid);
479
480 if(vid == 0x08f0){
481 s->vendor_name = "CardScan";
482 if(pid == 0x0005){
483 s->product_name = "800c";
484 }
485 else if(pid == 0x0002){
486 s->product_name = "600c";
487 }
488 else{
489 DBG (5, "Unknown product, using default settings\n");
490 s->product_name = "Unknown";
491 }
492 }
493 else if(vid == 0x0451){
494 s->vendor_name = "Sanford";
495 if(pid == 0x6250){
496 s->product_name = "800c";
497 }
498 else{
499 DBG (5, "Unknown product, using default settings\n");
500 s->product_name = "Unknown";
501 }
502 }
503 else{
504 DBG (5, "Unknown vendor/product, using default settings\n");
505 s->vendor_name = "Unknown";
506 s->product_name = "Unknown";
507 }
508
509 DBG (15, "attach_one: Found %s scanner %s at %s\n",
510 s->vendor_name, s->product_name, s->device_name);
511
512 /*copy config file settings*/
513 s->has_cal_buffer = global_has_cal_buffer;
514 s->lines_per_block = global_lines_per_block;
515 s->color_block_size = s->lines_per_block * PIXELS_PER_LINE * 3;
516 s->gray_block_size = s->lines_per_block * PIXELS_PER_LINE;
517
518 /* try to get calibration */
519 if(s->has_cal_buffer){
520 DBG (15, "attach_one: scanner calibration\n");
521
522 ret = load_calibration(s);
523 if (ret != SANE_STATUS_GOOD) {
524 DBG (5, "sane_start: ERROR: cannot calibrate, incompatible?\n");
525 free (s->device_name);
526 free (s);
527 return ret;
528 }
529 }
530 else{
531 DBG (15, "attach_one: skipping calibration\n");
532 }
533
534 /* set SANE option 'values' to good defaults */
535 DBG (15, "attach_one: init options\n");
536
537 /* go ahead and setup the first opt, because
538 * frontend may call control_option on it
539 * before calling get_option_descriptor
540 */
541 memset (s->opt, 0, sizeof (s->opt));
542 for (i = 0; i < NUM_OPTIONS; ++i) {
543 s->opt[i].name = "filler";
544 s->opt[i].size = sizeof (SANE_Word);
545 s->opt[i].cap = SANE_CAP_INACTIVE;
546 }
547
548 s->opt[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS;
549 s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
550 s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
551 s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
552 s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
553
554 DBG (15, "attach_one: init settings\n");
555
556 /* we close the connection, so that another backend can talk to scanner */
557 disconnect_fd(s);
558
559 /* load info into sane_device struct */
560 s->sane.name = s->device_name;
561 s->sane.vendor = s->vendor_name;
562 s->sane.model = s->product_name;
563 s->sane.type = "scanner";
564
565 s->next = scanner_devList;
566 scanner_devList = s;
567
568 DBG (10, "attach_one: finish\n");
569
570 return SANE_STATUS_GOOD;
571 }
572
573 /*
574 * connect the fd in the scanner struct
575 */
576 static SANE_Status
connect_fd(struct scanner * s)577 connect_fd (struct scanner *s)
578 {
579 SANE_Status ret;
580
581 DBG (10, "connect_fd: start\n");
582
583 if(s->fd > -1){
584 DBG (5, "connect_fd: already open\n");
585 ret = SANE_STATUS_GOOD;
586 }
587 else {
588 DBG (15, "connect_fd: opening USB device\n");
589 ret = sanei_usb_open (s->device_name, &(s->fd));
590 }
591
592 if(ret != SANE_STATUS_GOOD){
593 DBG (5, "connect_fd: could not open device: %d\n", ret);
594 }
595
596 DBG (10, "connect_fd: finish\n");
597
598 return ret;
599 }
600
601 static SANE_Status
load_calibration(struct scanner * s)602 load_calibration(struct scanner *s)
603 {
604 SANE_Status ret=SANE_STATUS_GOOD;
605 unsigned char cmd[] = {0x45, 0x00, 0x00};
606 unsigned char * buf;
607 size_t bytes = HEADER_SIZE + CAL_COLOR_SIZE*2 + CAL_GRAY_SIZE*2;
608 int j;
609
610 DBG (10, "load_calibration: start\n");
611
612 buf = malloc(bytes);
613 if(!buf){
614 DBG(5, "load_calibration: not enough mem for buffer: %ld\n",(long)bytes);
615 return SANE_STATUS_NO_MEM;
616 }
617
618 ret = do_cmd(
619 s, 0,
620 cmd, sizeof(cmd),
621 NULL, 0,
622 buf, &bytes
623 );
624
625 if (ret == SANE_STATUS_GOOD) {
626 DBG(15, "load_calibration: got GOOD\n");
627
628 /*
629 * color cal data comes from scaner like:
630 * bbbbbbbBBBBBBBgggggggGGGGGGGrrrrrrrRRRRRRR
631 * where b=darkblue, B=lightblue, etc
632 * reorder the data into two buffers
633 * bbbbbbbgggggggrrrrrrr and BBBBBBBGGGGGGGRRRRRRR
634 */
635
636 /*dark/light blue*/
637 memcpy(s->cal_color_b, buf+HEADER_SIZE, PIXELS_PER_LINE);
638 memcpy(s->cal_color_w,
639 buf+HEADER_SIZE+PIXELS_PER_LINE, PIXELS_PER_LINE);
640
641 /*dark/light green*/
642 memcpy(s->cal_color_b+PIXELS_PER_LINE,
643 buf+HEADER_SIZE+(PIXELS_PER_LINE*2), PIXELS_PER_LINE);
644 memcpy(s->cal_color_w+PIXELS_PER_LINE,
645 buf+HEADER_SIZE+(PIXELS_PER_LINE*3), PIXELS_PER_LINE);
646
647 /*dark/light red*/
648 memcpy(s->cal_color_b+(PIXELS_PER_LINE*2),
649 buf+HEADER_SIZE+(PIXELS_PER_LINE*4), PIXELS_PER_LINE);
650 memcpy(s->cal_color_w+(PIXELS_PER_LINE*2),
651 buf+HEADER_SIZE+(PIXELS_PER_LINE*5), PIXELS_PER_LINE);
652
653 /* then slide the light data down using the dark offset */
654 for(j=0;j<CAL_COLOR_SIZE;j++){
655 s->cal_color_w[j] -= s->cal_color_b[j];
656 }
657
658 /*dark/light gray*/
659 memcpy(s->cal_gray_b,
660 buf+HEADER_SIZE+(CAL_COLOR_SIZE*2), PIXELS_PER_LINE);
661 memcpy(s->cal_gray_w,
662 buf+HEADER_SIZE+(CAL_COLOR_SIZE*2)+PIXELS_PER_LINE, PIXELS_PER_LINE);
663
664 /* then slide the light data down using the dark offset */
665 for(j=0;j<CAL_GRAY_SIZE;j++){
666 s->cal_gray_w[j] -= s->cal_gray_b[j];
667 }
668
669 hexdump(35, "cal_color_b:", s->cal_color_b, CAL_COLOR_SIZE);
670 hexdump(35, "cal_color_w:", s->cal_color_w, CAL_COLOR_SIZE);
671 hexdump(35, "cal_gray_b:", s->cal_gray_b, CAL_GRAY_SIZE);
672 hexdump(35, "cal_gray_w:", s->cal_gray_w, CAL_GRAY_SIZE);
673 }
674 else {
675 DBG(5, "load_calibration: error reading data block status = %d\n", ret);
676 }
677
678 DBG (10, "load_calibration: finish\n");
679
680 return ret;
681 }
682
683 /*
684 * From the SANE spec:
685 * This function is used to establish a connection to a particular
686 * device. The name of the device to be opened is passed in argument
687 * name. If the call completes successfully, a handle for the device
688 * is returned in *h. As a special case, specifying a zero-length
689 * string as the device requests opening the first available device
690 * (if there is such a device).
691 */
692 SANE_Status
sane_open(SANE_String_Const name,SANE_Handle * handle)693 sane_open (SANE_String_Const name, SANE_Handle * handle)
694 {
695 struct scanner *dev = NULL;
696 struct scanner *s = NULL;
697 SANE_Status ret;
698
699 DBG (10, "sane_open: start\n");
700
701 if(name[0] == 0){
702 if(scanner_devList){
703 DBG (15, "sane_open: no device requested, using first\n");
704 s = scanner_devList;
705 }
706 else{
707 DBG (15, "sane_open: no device requested, none found\n");
708 }
709 }
710 else{
711 DBG (15, "sane_open: device %s requested, attaching\n", name);
712
713 ret = attach_one(name);
714 if(ret){
715 DBG (5, "sane_open: attach error %d\n",ret);
716 return ret;
717 }
718
719 for (dev = scanner_devList; dev; dev = dev->next) {
720 if (strcmp (dev->sane.name, name) == 0) {
721 s = dev;
722 break;
723 }
724 }
725 }
726
727 if (!s) {
728 DBG (5, "sane_open: no device found\n");
729 return SANE_STATUS_INVAL;
730 }
731
732 DBG (15, "sane_open: device %s found\n", s->sane.name);
733
734 *handle = s;
735
736 /* connect the fd so we can talk to scanner */
737 ret = connect_fd(s);
738 if(ret != SANE_STATUS_GOOD){
739 return ret;
740 }
741
742 DBG (10, "sane_open: finish\n");
743
744 return SANE_STATUS_GOOD;
745 }
746
747 /*
748 * @@ Section 3 - SANE Options functions
749 */
750
751 /*
752 * Returns the options we know.
753 *
754 * From the SANE spec:
755 * This function is used to access option descriptors. The function
756 * returns the option descriptor for option number n of the device
757 * represented by handle h. Option number 0 is guaranteed to be a
758 * valid option. Its value is an integer that specifies the number of
759 * options that are available for device handle h (the count includes
760 * option 0). If n is not a valid option index, the function returns
761 * NULL. The returned option descriptor is guaranteed to remain valid
762 * (and at the returned address) until the device is closed.
763 */
764 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle,SANE_Int option)765 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
766 {
767 struct scanner *s = handle;
768 int i;
769 SANE_Option_Descriptor *opt = &s->opt[option];
770
771 DBG (20, "sane_get_option_descriptor: %d\n", option);
772
773 if ((unsigned) option >= NUM_OPTIONS)
774 return NULL;
775
776 /* "Mode" group -------------------------------------------------------- */
777 if(option==OPT_MODE_GROUP){
778 opt->title = "Scan Mode";
779 opt->desc = "";
780 opt->type = SANE_TYPE_GROUP;
781 opt->constraint_type = SANE_CONSTRAINT_NONE;
782 }
783
784 /* scan mode */
785 else if(option==OPT_MODE){
786 i=0;
787 s->mode_list[i++]=STRING_GRAYSCALE;
788 s->mode_list[i++]=STRING_COLOR;
789 s->mode_list[i]=NULL;
790
791 opt->name = SANE_NAME_SCAN_MODE;
792 opt->title = SANE_TITLE_SCAN_MODE;
793 opt->desc = SANE_DESC_SCAN_MODE;
794 opt->type = SANE_TYPE_STRING;
795 opt->constraint_type = SANE_CONSTRAINT_STRING_LIST;
796 opt->constraint.string_list = s->mode_list;
797 opt->size = maxStringSize (opt->constraint.string_list);
798 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
799 }
800
801 return opt;
802 }
803
804 /**
805 * Gets or sets an option value.
806 *
807 * From the SANE spec:
808 * This function is used to set or inquire the current value of option
809 * number n of the device represented by handle h. The manner in which
810 * the option is controlled is specified by parameter action. The
811 * possible values of this parameter are described in more detail
812 * below. The value of the option is passed through argument val. It
813 * is a pointer to the memory that holds the option value. The memory
814 * area pointed to by v must be big enough to hold the entire option
815 * value (determined by member size in the corresponding option
816 * descriptor).
817 *
818 * The only exception to this rule is that when setting the value of a
819 * string option, the string pointed to by argument v may be shorter
820 * since the backend will stop reading the option value upon
821 * encountering the first NUL terminator in the string. If argument i
822 * is not NULL, the value of *i will be set to provide details on how
823 * well the request has been met.
824 */
825 SANE_Status
sane_control_option(SANE_Handle handle,SANE_Int option,SANE_Action action,void * val,SANE_Int * info)826 sane_control_option (SANE_Handle handle, SANE_Int option,
827 SANE_Action action, void *val, SANE_Int * info)
828 {
829 struct scanner *s = (struct scanner *) handle;
830 SANE_Int dummy = 0;
831
832 /* Make sure that all those statements involving *info cannot break (better
833 * than having to do "if (info) ..." everywhere!)
834 */
835 if (info == 0)
836 info = &dummy;
837
838 if (option >= NUM_OPTIONS) {
839 DBG (5, "sane_control_option: %d too big\n", option);
840 return SANE_STATUS_INVAL;
841 }
842
843 if (!SANE_OPTION_IS_ACTIVE (s->opt[option].cap)) {
844 DBG (5, "sane_control_option: %d inactive\n", option);
845 return SANE_STATUS_INVAL;
846 }
847
848 /*
849 * SANE_ACTION_GET_VALUE: We have to find out the current setting and
850 * return it in a human-readable form (often, text).
851 */
852 if (action == SANE_ACTION_GET_VALUE) {
853 SANE_Word * val_p = (SANE_Word *) val;
854
855 DBG (20, "sane_control_option: get value for '%s' (%d)\n", s->opt[option].name,option);
856
857 switch (option) {
858
859 case OPT_NUM_OPTS:
860 *val_p = NUM_OPTIONS;
861 return SANE_STATUS_GOOD;
862
863 case OPT_MODE:
864 if(s->mode == MODE_GRAYSCALE){
865 strcpy (val, STRING_GRAYSCALE);
866 }
867 else if(s->mode == MODE_COLOR){
868 strcpy (val, STRING_COLOR);
869 }
870 return SANE_STATUS_GOOD;
871 }
872 }
873 else if (action == SANE_ACTION_SET_VALUE) {
874 int tmp;
875 SANE_Status status;
876
877 DBG (20, "sane_control_option: set value for '%s' (%d)\n", s->opt[option].name,option);
878
879 if ( s->started ) {
880 DBG (5, "sane_control_option: can't set, device busy\n");
881 return SANE_STATUS_DEVICE_BUSY;
882 }
883
884 if (!SANE_OPTION_IS_SETTABLE (s->opt[option].cap)) {
885 DBG (5, "sane_control_option: not settable\n");
886 return SANE_STATUS_INVAL;
887 }
888
889 status = sanei_constrain_value (s->opt + option, val, info);
890 if (status != SANE_STATUS_GOOD) {
891 DBG (5, "sane_control_option: bad value\n");
892 return status;
893 }
894
895 /*
896 * Note - for those options which can assume one of a list of
897 * valid values, we can safely assume that they will have
898 * exactly one of those values because that's what
899 * sanei_constrain_value does. Hence no "else: invalid" branches
900 * below.
901 */
902 switch (option) {
903
904 /* Mode Group */
905 case OPT_MODE:
906 if (!strcmp (val, STRING_GRAYSCALE)) {
907 tmp = MODE_GRAYSCALE;
908 }
909 else{
910 tmp = MODE_COLOR;
911 }
912
913 if (tmp == s->mode)
914 return SANE_STATUS_GOOD;
915
916 s->mode = tmp;
917 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
918 return SANE_STATUS_GOOD;
919
920 } /* switch */
921 } /* else */
922
923 return SANE_STATUS_INVAL;
924 }
925
926 /*
927 * @@ Section 4 - SANE scanning functions
928 */
929 /*
930 * Called by SANE to retrieve information about the type of data
931 * that the current scan will return.
932 *
933 * From the SANE spec:
934 * This function is used to obtain the current scan parameters. The
935 * returned parameters are guaranteed to be accurate between the time
936 * a scan has been started (sane_start() has been called) and the
937 * completion of that request. Outside of that window, the returned
938 * values are best-effort estimates of what the parameters will be
939 * when sane_start() gets invoked.
940 *
941 * Calling this function before a scan has actually started allows,
942 * for example, to get an estimate of how big the scanned image will
943 * be. The parameters passed to this function are the handle h of the
944 * device for which the parameters should be obtained and a pointer p
945 * to a parameter structure.
946 */
947 SANE_Status
sane_get_parameters(SANE_Handle handle,SANE_Parameters * params)948 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
949 {
950 struct scanner *s = (struct scanner *) handle;
951
952 DBG (10, "sane_get_parameters: start\n");
953
954 params->pixels_per_line = PIXELS_PER_LINE;
955 params->lines = -1;
956 params->last_frame = 1;
957
958 if (s->mode == MODE_COLOR) {
959 params->format = SANE_FRAME_RGB;
960 params->depth = 8;
961 params->bytes_per_line = params->pixels_per_line * 3;
962 }
963 else if (s->mode == MODE_GRAYSCALE) {
964 params->format = SANE_FRAME_GRAY;
965 params->depth = 8;
966 params->bytes_per_line = params->pixels_per_line;
967 }
968
969 DBG (15, "\tdepth %d\n", params->depth);
970 DBG (15, "\tlines %d\n", params->lines);
971 DBG (15, "\tpixels_per_line %d\n", params->pixels_per_line);
972 DBG (15, "\tbytes_per_line %d\n", params->bytes_per_line);
973
974 DBG (10, "sane_get_parameters: finish\n");
975
976 return SANE_STATUS_GOOD;
977 }
978
979 /*
980 * Called by SANE when a page acquisition operation is to be started.
981 */
982 SANE_Status
sane_start(SANE_Handle handle)983 sane_start (SANE_Handle handle)
984 {
985 struct scanner *s = handle;
986 SANE_Status ret;
987
988 DBG (10, "sane_start: start\n");
989
990 /* first page of batch */
991 if(s->started){
992 DBG(5,"sane_start: previous transfer not finished?");
993 sane_cancel((SANE_Handle)s);
994 return SANE_STATUS_CANCELLED;
995 }
996
997 /* set clean defaults */
998 s->started=1;
999 s->bytes_rx=0;
1000 s->bytes_tx=0;
1001 s->paperless_lines=0;
1002
1003 /* heat up the lamp */
1004 if(s->mode == MODE_COLOR){
1005 ret = heat_lamp_color(s);
1006 }
1007 else{
1008 ret = heat_lamp_gray(s);
1009 }
1010
1011 if (ret != SANE_STATUS_GOOD) {
1012 DBG (5, "sane_start: ERROR: failed to heat lamp\n");
1013 sane_cancel((SANE_Handle)s);
1014 return ret;
1015 }
1016
1017 DBG (10, "sane_start: finish\n");
1018
1019 return SANE_STATUS_GOOD;
1020 }
1021
1022 static SANE_Status
heat_lamp_gray(struct scanner * s)1023 heat_lamp_gray(struct scanner *s)
1024 {
1025 SANE_Status ret = SANE_STATUS_GOOD;
1026 SANE_Status ret2 = SANE_STATUS_GOOD;
1027 unsigned char cmd[] =
1028 {0x12, 0x06, 0x00, 0x00, 0x01, 0x60, 0x00, 0x61, 0x00};
1029 size_t bytes = HEADER_SIZE + 1;
1030 unsigned char * buf;
1031 int i;
1032
1033 DBG (10, "heat_lamp_gray: start\n");
1034
1035 buf = malloc(bytes);
1036 if(!buf){
1037 DBG(5, "heat_lamp_gray: not enough mem for buffer: %lu\n",
1038 (long unsigned)bytes);
1039 return SANE_STATUS_NO_MEM;
1040 }
1041
1042 for(i=0;i<10;i++){
1043
1044 ret2 = do_cmd(
1045 s, 0,
1046 cmd, sizeof(cmd),
1047 NULL, 0,
1048 buf, &bytes
1049 );
1050
1051 if (ret2 != SANE_STATUS_GOOD) {
1052 DBG(5, "heat_lamp_gray: %d error\n",i);
1053 ret = ret2;
1054 break;
1055 }
1056
1057 if(!buf[1]){
1058 DBG(5, "heat_lamp_gray: %d got no docs\n",i);
1059 ret = SANE_STATUS_NO_DOCS;
1060 break;
1061 }
1062
1063 DBG(15, "heat_lamp_gray: %d got: %d %d\n",i,
1064 buf[HEADER_SIZE],s->cal_gray_b[0]);
1065
1066 if(buf[HEADER_SIZE] < 0x20){
1067 DBG(15, "heat_lamp_gray: hot\n");
1068 ret = SANE_STATUS_GOOD;
1069 break;
1070 }
1071 else{
1072 DBG(15, "heat_lamp_gray: cold\n");
1073 ret = SANE_STATUS_DEVICE_BUSY;
1074 }
1075 }
1076
1077 free(buf);
1078
1079 DBG (10, "heat_lamp_gray: finish %d\n",ret);
1080
1081 return ret;
1082 }
1083
1084 static SANE_Status
heat_lamp_color(struct scanner * s)1085 heat_lamp_color(struct scanner *s)
1086 {
1087 SANE_Status ret = SANE_STATUS_GOOD;
1088 SANE_Status ret2 = SANE_STATUS_GOOD;
1089 unsigned char cmd[] =
1090 {0x18, 0x07, 0x00, 0x00, 0x01, 0x60, 0x00, 0x61, 0x00, 0x07};
1091 size_t bytes = HEADER_SIZE + 3;
1092 unsigned char * buf;
1093 int i;
1094
1095 DBG (10, "heat_lamp_color: start\n");
1096
1097 buf = malloc(bytes);
1098 if(!buf){
1099 DBG(5, "heat_lamp_color: not enough mem for buffer: %lu\n",
1100 (long unsigned)bytes);
1101 return SANE_STATUS_NO_MEM;
1102 }
1103
1104 for(i=0;i<10;i++){
1105
1106 ret2 = do_cmd(
1107 s, 0,
1108 cmd, sizeof(cmd),
1109 NULL, 0,
1110 buf, &bytes
1111 );
1112
1113 if (ret2 != SANE_STATUS_GOOD) {
1114 DBG(5, "heat_lamp_color: %d error\n",i);
1115 ret = ret2;
1116 break;
1117 }
1118
1119 if(!buf[1]){
1120 DBG(5, "heat_lamp_color: %d got no docs\n",i);
1121 ret = SANE_STATUS_NO_DOCS;
1122 break;
1123 }
1124
1125 DBG(15, "heat_lamp_color: %d got: %d,%d,%d %d,%d,%d\n",i,
1126 buf[HEADER_SIZE],buf[HEADER_SIZE+1],buf[HEADER_SIZE+2],
1127 s->cal_color_b[0],s->cal_color_b[1],s->cal_color_b[2]);
1128
1129 if(buf[HEADER_SIZE] < 0x20
1130 && buf[HEADER_SIZE+1] < 0x20
1131 && buf[HEADER_SIZE+2] < 0x20){
1132 DBG(15, "heat_lamp_color: hot\n");
1133 ret = SANE_STATUS_GOOD;
1134 break;
1135 }
1136 else{
1137 DBG(15, "heat_lamp_color: cold\n");
1138 ret = SANE_STATUS_DEVICE_BUSY;
1139 }
1140 }
1141
1142 free(buf);
1143
1144 DBG (10, "heat_lamp_color: finish %d\n",ret);
1145
1146 return ret;
1147 }
1148
1149 /*
1150 * Called by SANE to read data.
1151 *
1152 * From the SANE spec:
1153 * This function is used to read image data from the device
1154 * represented by handle h. Argument buf is a pointer to a memory
1155 * area that is at least maxlen bytes long. The number of bytes
1156 * returned is stored in *len. A backend must set this to zero when
1157 * the call fails (i.e., when a status other than SANE_STATUS_GOOD is
1158 * returned).
1159 *
1160 * When the call succeeds, the number of bytes returned can be
1161 * anywhere in the range from 0 to maxlen bytes.
1162 */
1163 SANE_Status
sane_read(SANE_Handle handle,SANE_Byte * buf,SANE_Int max_len,SANE_Int * len)1164 sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len)
1165 {
1166 struct scanner *s = (struct scanner *) handle;
1167 SANE_Status ret=SANE_STATUS_GOOD;
1168
1169 DBG (10, "sane_read: start\n");
1170
1171 *len = 0;
1172
1173 /* cancelled? */
1174 if(!s->started){
1175 DBG (5, "sane_read: call sane_start first\n");
1176 return SANE_STATUS_CANCELLED;
1177 }
1178
1179 /* have sent all of current buffer */
1180 if(s->bytes_tx == s->bytes_rx){
1181
1182 /* at end of data, stop */
1183 if(s->paperless_lines >= MAX_PAPERLESS_LINES){
1184 DBG (15, "sane_read: returning eof\n");
1185 power_down(s);
1186 return SANE_STATUS_EOF;
1187 }
1188
1189 /* more to get, reset and go */
1190 s->bytes_tx = 0;
1191 s->bytes_rx = 0;
1192
1193 if(s->mode == MODE_COLOR){
1194 ret = read_from_scanner_color(s);
1195 }
1196 else{
1197 ret = read_from_scanner_gray(s);
1198 }
1199
1200 if(ret){
1201 DBG(5,"sane_read: returning %d\n",ret);
1202 return ret;
1203 }
1204 }
1205
1206 /* data in current buffer, send some of it */
1207 *len = s->bytes_rx - s->bytes_tx;
1208 if(*len > max_len){
1209 *len = max_len;
1210 }
1211
1212 memcpy(buf,s->buffer+s->bytes_tx,*len);
1213 s->bytes_tx += *len;
1214
1215 DBG (10, "sane_read: %d,%d,%d finish\n", *len,s->bytes_rx,s->bytes_tx);
1216
1217 return ret;
1218 }
1219
1220 static SANE_Status
read_from_scanner_gray(struct scanner * s)1221 read_from_scanner_gray(struct scanner *s)
1222 {
1223 SANE_Status ret=SANE_STATUS_GOOD;
1224 /*cmd len-le16 move lines ??? ??? ??? ???*/
1225 unsigned char cmd[] =
1226 {0x12, 0x06, 0x00, 0x01, 0x01, 0x60, 0x00, 0x18, 0x05};
1227 size_t bytes = HEADER_SIZE + s->gray_block_size;
1228 unsigned char * buf;
1229 int i,j;
1230
1231 DBG (10, "read_from_scanner_gray: start\n");
1232
1233 cmd[4] = s->lines_per_block;
1234
1235 buf = malloc(bytes);
1236 if(!buf){
1237 DBG(5, "read_from_scanner_gray: not enough mem for buffer: %lu\n",
1238 (long unsigned)bytes);
1239 return SANE_STATUS_NO_MEM;
1240 }
1241
1242 ret = do_cmd(
1243 s, 0,
1244 cmd, sizeof(cmd),
1245 NULL, 0,
1246 buf, &bytes
1247 );
1248
1249 if (ret == SANE_STATUS_GOOD) {
1250
1251 DBG(15, "read_from_scanner_gray: got GOOD\n");
1252
1253 if(!buf[1]){
1254 s->paperless_lines += s->lines_per_block;
1255 }
1256
1257 s->bytes_rx = s->gray_block_size;
1258
1259 /*memcpy(s->buffer,buf+HEADER_SIZE,s->gray_block_size);*/
1260
1261 /* reorder the gray data into the struct's buffer */
1262 for(i=0;i<s->gray_block_size;i+=PIXELS_PER_LINE){
1263 for(j=0;j<PIXELS_PER_LINE;j++){
1264
1265 unsigned char byte = buf[ HEADER_SIZE + i + j ];
1266 unsigned char bcal = s->cal_gray_b[j];
1267 unsigned char wcal = s->cal_gray_w[j];
1268
1269 byte = (byte <= bcal)?0:(byte-bcal);
1270 byte = (byte >= wcal)?255:(byte*255/wcal);
1271 s->buffer[i+j] = byte;
1272 }
1273 }
1274 }
1275 else {
1276 DBG(5, "read_from_scanner_gray: error reading status = %d\n", ret);
1277 }
1278
1279 free(buf);
1280
1281 DBG (10, "read_from_scanner_gray: finish\n");
1282
1283 return ret;
1284 }
1285
1286 static SANE_Status
read_from_scanner_color(struct scanner * s)1287 read_from_scanner_color(struct scanner *s)
1288 {
1289 SANE_Status ret=SANE_STATUS_GOOD;
1290 unsigned char cmd[] =
1291 {0x18, 0x07, 0x00, 0x01, 0x01, 0x60, 0x00, 0x18, 0x05, 0x07};
1292 size_t bytes = HEADER_SIZE + s->color_block_size;
1293 unsigned char * buf;
1294 int i,j,k;
1295
1296 DBG (10, "read_from_scanner_color: start\n");
1297
1298 cmd[4] = s->lines_per_block;
1299
1300 buf = malloc(bytes);
1301 if(!buf){
1302 DBG(5, "read_from_scanner_color: not enough mem for buffer: %lu\n",
1303 (long unsigned)bytes);
1304 return SANE_STATUS_NO_MEM;
1305 }
1306
1307 ret = do_cmd(
1308 s, 0,
1309 cmd, sizeof(cmd),
1310 NULL, 0,
1311 buf, &bytes
1312 );
1313
1314 if (ret == SANE_STATUS_GOOD) {
1315
1316 DBG(15, "read_from_scanner_color: got GOOD\n");
1317
1318 if(!buf[1]){
1319 s->paperless_lines += s->lines_per_block;
1320 }
1321
1322 s->bytes_rx = s->color_block_size;
1323
1324 /*memcpy(s->buffer,buf+HEADER_SIZE,s->color_block_size);*/
1325
1326 /* reorder the color data into the struct's buffer */
1327 for(i=0;i<s->color_block_size;i+=PIXELS_PER_LINE*3){
1328 for(j=0;j<PIXELS_PER_LINE;j++){
1329 for(k=0;k<3;k++){
1330
1331 int offset = PIXELS_PER_LINE*(2-k) + j;
1332 unsigned char byte = buf[ HEADER_SIZE + i + offset ];
1333 unsigned char bcal = s->cal_color_b[offset];
1334 unsigned char wcal = s->cal_color_w[offset];
1335
1336 byte = (byte <= bcal)?0:(byte-bcal);
1337 byte = (byte >= wcal)?255:(byte*255/wcal);
1338 s->buffer[i+j*3+k] = byte;
1339 }
1340 }
1341 }
1342 }
1343 else {
1344 DBG(5, "read_from_scanner_color: error reading status = %d\n", ret);
1345 }
1346
1347 free(buf);
1348
1349 DBG (10, "read_from_scanner_color: finish\n");
1350
1351 return ret;
1352 }
1353
1354 /*
1355 * @@ Section 4 - SANE cleanup functions
1356 */
1357 /*
1358 * Cancels a scan.
1359 *
1360 * From the SANE spec:
1361 * This function is used to immediately or as quickly as possible
1362 * cancel the currently pending operation of the device represented by
1363 * handle h. This function can be called at any time (as long as
1364 * handle h is a valid handle) but usually affects long-running
1365 * operations only (such as image is acquisition). It is safe to call
1366 * this function asynchronously (e.g., from within a signal handler).
1367 * It is important to note that completion of this operation does not
1368 * imply that the currently pending operation has been cancelled. It
1369 * only guarantees that cancellation has been initiated. Cancellation
1370 * completes only when the cancelled call returns (typically with a
1371 * status value of SANE_STATUS_CANCELLED). Since the SANE API does
1372 * not require any other operations to be re-entrant, this implies
1373 * that a frontend must not call any other operation until the
1374 * cancelled operation has returned.
1375 */
1376 void
sane_cancel(SANE_Handle handle)1377 sane_cancel (SANE_Handle handle)
1378 {
1379 struct scanner * s = (struct scanner *) handle;
1380 DBG (10, "sane_cancel: start\n");
1381 s->started = 0;
1382 DBG (10, "sane_cancel: finish\n");
1383 }
1384
1385 static SANE_Status
power_down(struct scanner * s)1386 power_down(struct scanner *s)
1387 {
1388 SANE_Status ret = SANE_STATUS_GOOD;
1389
1390 unsigned char cmd[] = {0x21, 0x02, 0x00, 0x0a, 0x00};
1391 unsigned char buf[6];
1392 size_t bytes = sizeof(buf);
1393 int i;
1394
1395 DBG (10, "power_down: start\n");
1396
1397 for(i=0;i<5;i++){
1398 ret = do_cmd(
1399 s, 0,
1400 cmd, sizeof(cmd),
1401 NULL, 0,
1402 buf, &bytes
1403 );
1404
1405 if(ret != SANE_STATUS_GOOD){
1406 break;
1407 }
1408 }
1409
1410 #if 0
1411 unsigned char cmd[] = {0x35, 0x01, 0x00, 0xff};
1412 unsigned char buf[5];
1413 size_t bytes = sizeof(buf);
1414
1415 DBG (10, "power_down: start\n");
1416
1417 ret = do_cmd(
1418 s, 0,
1419 cmd, sizeof(cmd),
1420 NULL, 0,
1421 buf, &bytes
1422 );
1423 #endif
1424
1425 DBG (10, "power_down: finish %d\n",ret);
1426
1427 return ret;
1428 }
1429
1430 /*
1431 * Ends use of the scanner.
1432 *
1433 * From the SANE spec:
1434 * This function terminates the association between the device handle
1435 * passed in argument h and the device it represents. If the device is
1436 * presently active, a call to sane_cancel() is performed first. After
1437 * this function returns, handle h must not be used anymore.
1438 */
1439 void
sane_close(SANE_Handle handle)1440 sane_close (SANE_Handle handle)
1441 {
1442 DBG (10, "sane_close: start\n");
1443
1444 sane_cancel(handle);
1445 disconnect_fd((struct scanner *) handle);
1446
1447 DBG (10, "sane_close: finish\n");
1448 }
1449
1450 static SANE_Status
disconnect_fd(struct scanner * s)1451 disconnect_fd (struct scanner *s)
1452 {
1453 DBG (10, "disconnect_fd: start\n");
1454
1455 if(s->fd > -1){
1456 DBG (15, "disconnecting usb device\n");
1457 sanei_usb_close (s->fd);
1458 s->fd = -1;
1459 }
1460
1461 DBG (10, "disconnect_fd: finish\n");
1462
1463 return SANE_STATUS_GOOD;
1464 }
1465
1466 /*
1467 * Terminates the backend.
1468 *
1469 * From the SANE spec:
1470 * This function must be called to terminate use of a backend. The
1471 * function will first close all device handles that still might be
1472 * open (it is recommended to close device handles explicitly through
1473 * a call to sane_close(), but backends are required to release all
1474 * resources upon a call to this function). After this function
1475 * returns, no function other than sane_init() may be called
1476 * (regardless of the status value returned by sane_exit(). Neglecting
1477 * to call this function may result in some resources not being
1478 * released properly.
1479 */
1480 void
sane_exit(void)1481 sane_exit (void)
1482 {
1483 struct scanner *dev, *next;
1484
1485 DBG (10, "sane_exit: start\n");
1486
1487 for (dev = scanner_devList; dev; dev = next) {
1488 disconnect_fd(dev);
1489 next = dev->next;
1490 free (dev->device_name);
1491 free (dev);
1492 }
1493
1494 if (sane_devArray)
1495 free (sane_devArray);
1496
1497 scanner_devList = NULL;
1498 sane_devArray = NULL;
1499
1500 DBG (10, "sane_exit: finish\n");
1501 }
1502
1503
1504 /*
1505 * @@ Section 5 - misc helper functions
1506 */
1507 /*
1508 * take a bunch of pointers, send commands to scanner
1509 */
1510 static SANE_Status
do_cmd(struct scanner * s,int shortTime,unsigned char * cmdBuff,size_t cmdLen,unsigned char * outBuff,size_t outLen,unsigned char * inBuff,size_t * inLen)1511 do_cmd(struct scanner *s, int shortTime,
1512 unsigned char * cmdBuff, size_t cmdLen,
1513 unsigned char * outBuff, size_t outLen,
1514 unsigned char * inBuff, size_t * inLen
1515 )
1516 {
1517 /* sanei_usb overwrites the transfer size, so make some local copies */
1518 size_t loc_cmdLen = cmdLen;
1519 size_t loc_outLen = outLen;
1520 size_t loc_inLen = *inLen;
1521
1522 int cmdTime = USB_COMMAND_TIME;
1523 int outTime = USB_DATA_TIME;
1524 int inTime = USB_DATA_TIME;
1525
1526 int ret = 0;
1527
1528 DBG (10, "do_cmd: start\n");
1529
1530 if(shortTime){
1531 cmdTime /= 20;
1532 outTime /= 20;
1533 inTime /= 20;
1534 }
1535
1536 /* change timeout */
1537 sanei_usb_set_timeout(cmdTime);
1538
1539 /* write the command out */
1540 DBG(25, "cmd: writing %ld bytes, timeout %d\n", (long)cmdLen, cmdTime);
1541 hexdump(30, "cmd: >>", cmdBuff, cmdLen);
1542 ret = sanei_usb_write_bulk(s->fd, cmdBuff, &cmdLen);
1543 DBG(25, "cmd: wrote %ld bytes, retVal %d\n", (long)cmdLen, ret);
1544
1545 if(ret == SANE_STATUS_EOF){
1546 DBG(5,"cmd: got EOF, returning IO_ERROR\n");
1547 return SANE_STATUS_IO_ERROR;
1548 }
1549 if(ret != SANE_STATUS_GOOD){
1550 DBG(5,"cmd: return error '%s'\n",sane_strstatus(ret));
1551 return ret;
1552 }
1553 if(loc_cmdLen != cmdLen){
1554 DBG(5,"cmd: wrong size %ld/%ld\n", (long)loc_cmdLen, (long)cmdLen);
1555 return SANE_STATUS_IO_ERROR;
1556 }
1557
1558 /* this command has a write component, and a place to get it */
1559 if(outBuff && outLen && outTime){
1560
1561 /* change timeout */
1562 sanei_usb_set_timeout(outTime);
1563
1564 DBG(25, "out: writing %ld bytes, timeout %d\n", (long)outLen, outTime);
1565 hexdump(30, "out: >>", outBuff, outLen);
1566 ret = sanei_usb_write_bulk(s->fd, outBuff, &outLen);
1567 DBG(25, "out: wrote %ld bytes, retVal %d\n", (long)outLen, ret);
1568
1569 if(ret == SANE_STATUS_EOF){
1570 DBG(5,"out: got EOF, returning IO_ERROR\n");
1571 return SANE_STATUS_IO_ERROR;
1572 }
1573 if(ret != SANE_STATUS_GOOD){
1574 DBG(5,"out: return error '%s'\n",sane_strstatus(ret));
1575 return ret;
1576 }
1577 if(loc_outLen != outLen){
1578 DBG(5,"out: wrong size %ld/%ld\n", (long)loc_outLen, (long)outLen);
1579 return SANE_STATUS_IO_ERROR;
1580 }
1581 }
1582
1583 /* this command has a read component, and a place to put it */
1584 if(inBuff && inLen && inTime){
1585
1586 memset(inBuff,0,*inLen);
1587
1588 /* change timeout */
1589 sanei_usb_set_timeout(inTime);
1590
1591 DBG(25, "in: reading %ld bytes, timeout %d\n", (long)*inLen, inTime);
1592 ret = sanei_usb_read_bulk(s->fd, inBuff, inLen);
1593 DBG(25, "in: retVal %d\n", ret);
1594
1595 if(ret == SANE_STATUS_EOF){
1596 DBG(5,"in: got EOF, continuing\n");
1597 }
1598 else if(ret != SANE_STATUS_GOOD){
1599 DBG(5,"in: return error '%s'\n",sane_strstatus(ret));
1600 return ret;
1601 }
1602
1603 DBG(25, "in: read %ld bytes\n", (long)*inLen);
1604 if(*inLen){
1605 hexdump(30, "in: <<", inBuff, *inLen);
1606 }
1607
1608 if(loc_inLen != *inLen){
1609 ret = SANE_STATUS_EOF;
1610 DBG(5,"in: short read %ld/%ld\n", (long)loc_inLen, (long)*inLen);
1611 }
1612 }
1613
1614 DBG (10, "do_cmd: finish\n");
1615
1616 return ret;
1617 }
1618
1619 /**
1620 * Convenience method to determine longest string size in a list.
1621 */
1622 static size_t
maxStringSize(const SANE_String_Const strings[])1623 maxStringSize (const SANE_String_Const strings[])
1624 {
1625 size_t size, max_size = 0;
1626 int i;
1627
1628 for (i = 0; strings[i]; ++i) {
1629 size = strlen (strings[i]) + 1;
1630 if (size > max_size)
1631 max_size = size;
1632 }
1633
1634 return max_size;
1635 }
1636
1637 /**
1638 * Prints a hex dump of the given buffer onto the debug output stream.
1639 */
1640 static void
hexdump(int level,char * comment,unsigned char * p,int l)1641 hexdump (int level, char *comment, unsigned char *p, int l)
1642 {
1643 int i;
1644 char line[128];
1645 char *ptr;
1646
1647 if(DBG_LEVEL < level)
1648 return;
1649
1650 DBG (level, "%s\n", comment);
1651 ptr = line;
1652 for (i = 0; i < l; i++, p++)
1653 {
1654 if ((i % 16) == 0)
1655 {
1656 if (ptr != line)
1657 {
1658 *ptr = '\0';
1659 DBG (level, "%s\n", line);
1660 ptr = line;
1661 }
1662 sprintf (ptr, "%3.3x:", i);
1663 ptr += 4;
1664 }
1665 sprintf (ptr, " %2.2x", *p);
1666 ptr += 3;
1667 }
1668 *ptr = '\0';
1669 DBG (level, "%s\n", line);
1670 }
1671
1672 /**
1673 * An advanced method we don't support but have to define.
1674 */
1675 SANE_Status
sane_set_io_mode(SANE_Handle h,SANE_Bool non_blocking)1676 sane_set_io_mode (SANE_Handle h, SANE_Bool non_blocking)
1677 {
1678 DBG (10, "sane_set_io_mode\n");
1679 DBG (15, "%d %p\n", non_blocking, h);
1680 return SANE_STATUS_UNSUPPORTED;
1681 }
1682
1683 /**
1684 * An advanced method we don't support but have to define.
1685 */
1686 SANE_Status
sane_get_select_fd(SANE_Handle h,SANE_Int * fdp)1687 sane_get_select_fd (SANE_Handle h, SANE_Int *fdp)
1688 {
1689 DBG (10, "sane_get_select_fd\n");
1690 DBG (15, "%p %d\n", h, *fdp);
1691 return SANE_STATUS_UNSUPPORTED;
1692 }
1693