1 /* sane - Scanner Access Now Easy.
2
3 This file is part of the SANE package, and implements a SANE backend
4 for various large Kodak scanners.
5
6 Copyright (C) 2008-2010 m. allan noah
7
8 --------------------------------------------------------------------------
9
10 This file is part of the SANE package.
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <https://www.gnu.org/licenses/>.
24
25 As a special exception, the authors of SANE give permission for
26 additional uses of the libraries contained in this release of SANE.
27
28 The exception is that, if you link a SANE library with other files
29 to produce an executable, this does not by itself cause the
30 resulting executable to be covered by the GNU General Public
31 License. Your use of that executable is in no way restricted on
32 account of linking the SANE library code into it.
33
34 This exception does not, however, invalidate any other reasons why
35 the executable file might be covered by the GNU General Public
36 License.
37
38 If you submit changes to SANE to the maintainers to be included in
39 a subsequent release, you agree by submitting the changes that
40 those changes may be distributed with this exception intact.
41
42 If you write modifications of your own for SANE, it is your choice
43 whether to permit this exception to apply to your modifications.
44 If you do not wish that, delete this exception notice.
45
46 --------------------------------------------------------------------------
47
48 The source code is divided in sections which you can easily find by
49 searching for the tag "@@".
50
51 Section 1 - Init & static stuff
52 Section 2 - sane_init, _get_devices, _open & friends
53 Section 3 - sane_*_option functions
54 Section 4 - sane_start, _get_param, _read & friends
55 Section 5 - sane_close functions
56 Section 6 - misc functions
57
58 Changes:
59 v0 through v5 2008-01-15, MAN
60 - development versions
61 v6 2009-06-22, MAN
62 - improved set_window() to build descriptor from scratch
63 - initial release
64 v7 2010-02-10, MAN
65 - add SANE_I18N to static strings
66 - don't fail if scsi buffer is too small
67
68 SANE FLOW DIAGRAM
69
70 - sane_init() : initialize backend
71 . - sane_get_devices() : query list of scanner devices
72 . - sane_open() : open a particular scanner device
73 . . - sane_set_io_mode : set blocking mode
74 . . - sane_get_select_fd : get scanner fd
75 . .
76 . . - sane_get_option_descriptor() : get option information
77 . . - sane_control_option() : change option values
78 . . - sane_get_parameters() : returns estimated scan parameters
79 . . - (repeat previous 3 functions)
80 . .
81 . . - sane_start() : start image acquisition
82 . . - sane_get_parameters() : returns actual scan parameters
83 . . - sane_read() : read image data (from pipe)
84 . . (sane_read called multiple times; after sane_read returns EOF,
85 . . loop may continue with sane_start which may return a 2nd page
86 . . when doing duplex scans, or load the next page from the ADF)
87 . .
88 . . - sane_cancel() : cancel operation
89 . - sane_close() : close opened scanner device
90 - sane_exit() : terminate use of backend
91
92 */
93
94 /*
95 * @@ Section 1 - Init
96 */
97
98 #include "sane/config.h"
99
100 #include <errno.h>
101 #include <fcntl.h>
102 #include <limits.h>
103 #include <stdio.h>
104 #include <stdlib.h>
105 #include <string.h>
106 #include <ctype.h>
107 #include <time.h>
108 #include <math.h>
109
110 #include <sys/types.h>
111 #include <unistd.h>
112 #ifdef HAVE_LIBC_H
113 # include <libc.h> /* NeXTStep/OpenStep */
114 #endif
115
116 #include "../include/sane/sanei_backend.h"
117 #include "../include/sane/sanei_scsi.h"
118 #include "../include/sane/saneopts.h"
119 #include "../include/sane/sanei_config.h"
120
121 #include "kodak-cmd.h"
122 #include "kodak.h"
123
124 #define DEBUG 1
125 #define BUILD 7
126
127 /* values for SANE_DEBUG_KODAK env var:
128 - errors 5
129 - function trace 10
130 - function detail 15
131 - get/setopt cmds 20
132 - scsi cmd trace 25
133 - scsi cmd detail 30
134 - useless noise 35
135 */
136
137 /* ------------------------------------------------------------------------- */
138 #define STRING_ADFFRONT SANE_I18N("ADF Front")
139 #define STRING_ADFBACK SANE_I18N("ADF Back")
140 #define STRING_ADFDUPLEX SANE_I18N("ADF Duplex")
141
142 #define STRING_LINEART SANE_VALUE_SCAN_MODE_LINEART
143 #define STRING_HALFTONE SANE_VALUE_SCAN_MODE_HALFTONE
144 #define STRING_GRAYSCALE SANE_VALUE_SCAN_MODE_GRAY
145 #define STRING_COLOR SANE_VALUE_SCAN_MODE_COLOR
146
147 /* Also set via config file. */
148 static int global_buffer_size = DEFAULT_BUFFER_SIZE;
149
150 /*
151 * used by attach* and sane_get_devices
152 * a ptr to a null term array of ptrs to SANE_Device structs
153 * a ptr to a single-linked list of scanner structs
154 */
155 static const SANE_Device **sane_devArray = NULL;
156 static struct scanner *scanner_devList = NULL;
157
158 /*
159 * @@ Section 2 - SANE & scanner init code
160 */
161
162 /*
163 * Called by SANE initially.
164 *
165 * From the SANE spec:
166 * This function must be called before any other SANE function can be
167 * called. The behavior of a SANE backend is undefined if this
168 * function is not called first. The version code of the backend is
169 * returned in the value pointed to by version_code. If that pointer
170 * is NULL, no version code is returned. Argument authorize is either
171 * a pointer to a function that is invoked when the backend requires
172 * authentication for a specific resource or NULL if the frontend does
173 * not support authentication.
174 */
175 SANE_Status
sane_init(SANE_Int * version_code,SANE_Auth_Callback authorize)176 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
177 {
178 (void) authorize; /* get rid of compiler warning */
179
180 DBG_INIT ();
181 DBG (10, "sane_init: start\n");
182
183 if (version_code)
184 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD);
185
186 DBG (5, "sane_init: kodak backend %d.%d.%d, from %s\n",
187 SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD, PACKAGE_STRING);
188
189 DBG (10, "sane_init: finish\n");
190
191 return SANE_STATUS_GOOD;
192 }
193
194 /*
195 * Called by SANE to find out about supported devices.
196 *
197 * From the SANE spec:
198 * This function can be used to query the list of devices that are
199 * available. If the function executes successfully, it stores a
200 * pointer to a NULL terminated array of pointers to SANE_Device
201 * structures in *device_list. The returned list is guaranteed to
202 * remain unchanged and valid until (a) another call to this function
203 * is performed or (b) a call to sane_exit() is performed. This
204 * function can be called repeatedly to detect when new devices become
205 * available. If argument local_only is true, only local devices are
206 * returned (devices directly attached to the machine that SANE is
207 * running on). If it is false, the device list includes all remote
208 * devices that are accessible to the SANE library.
209 *
210 * SANE does not require that this function is called before a
211 * sane_open() call is performed. A device name may be specified
212 * explicitly by a user which would make it unnecessary and
213 * undesirable to call this function first.
214 */
215 /* Read the config file, find scanners with help from sanei_*
216 * store in two global lists of device structs
217 */
218 SANE_Status
sane_get_devices(const SANE_Device *** device_list,SANE_Bool local_only)219 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
220 {
221 struct scanner *dev;
222 char line[PATH_MAX];
223 const char *lp;
224 FILE *fp;
225 int num_devices=0;
226 int i=0;
227
228 (void) local_only; /* get rid of compiler warning */
229
230 DBG (10, "sane_get_devices: start\n");
231
232 /* set this to default before reading the file */
233 global_buffer_size = DEFAULT_BUFFER_SIZE;
234
235 fp = sanei_config_open (KODAK_CONFIG_FILE);
236
237 if (fp) {
238
239 DBG (15, "sane_get_devices: reading config file %s\n", KODAK_CONFIG_FILE);
240
241 while (sanei_config_read (line, PATH_MAX, fp)) {
242
243 lp = line;
244
245 /* ignore comments */
246 if (*lp == '#')
247 continue;
248
249 /* skip empty lines */
250 if (*lp == 0)
251 continue;
252
253 if ((strncmp ("option", lp, 6) == 0) && isspace (lp[6])) {
254
255 lp += 6;
256 lp = sanei_config_skip_whitespace (lp);
257
258 /* we allow setting buffersize too big */
259 if ((strncmp (lp, "buffer-size", 11) == 0) && isspace (lp[11])) {
260
261 int buf;
262 lp += 11;
263 lp = sanei_config_skip_whitespace (lp);
264 buf = atoi (lp);
265
266 if (buf < 4096) {
267 DBG (5, "sane_get_devices: config option \"buffer-size\" \
268 (%d) is < 4096, ignoring!\n", buf);
269 continue;
270 }
271
272 if (buf > DEFAULT_BUFFER_SIZE) {
273 DBG (5, "sane_get_devices: config option \"buffer-size\" \
274 (%d) is > %d, warning!\n", buf, DEFAULT_BUFFER_SIZE);
275 }
276
277 DBG (15, "sane_get_devices: setting \"buffer-size\" to %d\n",
278 buf);
279 global_buffer_size = buf;
280 }
281 else {
282 DBG (5, "sane_get_devices: config option \"%s\" \
283 unrecognized\n", lp);
284 }
285 }
286 else if ((strncmp ("scsi", lp, 4) == 0) && isspace (lp[4])) {
287 DBG (15, "sane_get_devices: looking for '%s'\n", lp);
288 sanei_config_attach_matching_devices (lp, attach_one);
289 }
290 else{
291 DBG (5, "sane_get_devices: config line \"%s\" unrecognized\n",
292 lp);
293 }
294 }
295 fclose (fp);
296 }
297
298 else {
299 DBG (5, "sane_get_devices: no config file '%s', using defaults\n",
300 KODAK_CONFIG_FILE);
301 DBG (15, "sane_get_devices: looking for 'scsi KODAK'\n");
302 sanei_config_attach_matching_devices ("scsi KODAK", attach_one);
303 }
304
305 for (dev = scanner_devList; dev; dev=dev->next) {
306 DBG (15, "sane_get_devices: found scanner %s\n",dev->device_name);
307 num_devices++;
308 }
309
310 DBG (15, "sane_get_devices: found %d scanner(s)\n",num_devices);
311
312 sane_devArray = calloc (num_devices + 1, sizeof (SANE_Device*));
313 if (!sane_devArray)
314 return SANE_STATUS_NO_MEM;
315
316 for (dev = scanner_devList; dev; dev=dev->next) {
317 sane_devArray[i++] = (SANE_Device *)&dev->sane;
318 }
319
320 sane_devArray[i] = 0;
321
322 if(device_list){
323 *device_list = sane_devArray;
324 }
325
326 DBG (10, "sane_get_devices: finish\n");
327
328 return SANE_STATUS_GOOD;
329 }
330
331 /* build the scanner struct and link to global list
332 * unless struct is already loaded, then pretend
333 */
334 static SANE_Status
attach_one(const char * device_name)335 attach_one (const char *device_name)
336 {
337 struct scanner *s;
338 int ret;
339
340 DBG (10, "attach_one: start\n");
341 DBG (15, "attach_one: looking for '%s'\n", device_name);
342
343 for (s = scanner_devList; s; s = s->next) {
344 if (strcmp (s->sane.name, device_name) == 0) {
345 DBG (10, "attach_one: already attached!\n");
346 return SANE_STATUS_GOOD;
347 }
348 }
349
350 /* build a struct to hold it */
351 if ((s = calloc (sizeof (*s), 1)) == NULL)
352 return SANE_STATUS_NO_MEM;
353
354 /* scsi command/data buffer */
355 s->buffer_size = global_buffer_size;
356
357 /* copy the device name */
358 s->device_name = strdup (device_name);
359 if (!s->device_name){
360 free (s);
361 return SANE_STATUS_NO_MEM;
362 }
363
364 /* connect the fd */
365 s->fd = -1;
366 ret = connect_fd(s);
367 if(ret != SANE_STATUS_GOOD){
368 free (s->device_name);
369 free (s);
370 return ret;
371 }
372
373 /* Now query the device to load its vendor/model/version */
374 ret = init_inquire (s);
375 if (ret != SANE_STATUS_GOOD) {
376 disconnect_fd(s);
377 free (s->device_name);
378 free (s);
379 DBG (5, "attach_one: inquiry failed\n");
380 return ret;
381 }
382
383 /* clean up the scanner struct based on model */
384 /* this is the only piece of model specific code */
385 ret = init_model (s);
386 if (ret != SANE_STATUS_GOOD) {
387 disconnect_fd(s);
388 free (s->device_name);
389 free (s);
390 DBG (5, "attach_one: model failed\n");
391 return ret;
392 }
393
394 /* sets user 'values' to good defaults */
395 ret = init_user (s);
396 if (ret != SANE_STATUS_GOOD) {
397 disconnect_fd(s);
398 free (s->device_name);
399 free (s);
400 DBG (5, "attach_one: user failed\n");
401 return ret;
402 }
403
404 /* sets SANE option 'values' to good defaults */
405 ret = init_options (s);
406 if (ret != SANE_STATUS_GOOD) {
407 disconnect_fd(s);
408 free (s->device_name);
409 free (s);
410 DBG (5, "attach_one: options failed\n");
411 return ret;
412 }
413
414 /* we close the connection, so that another backend can talk to scanner */
415 disconnect_fd(s);
416
417 /* load info into sane_device struct */
418 s->sane.name = s->device_name;
419 s->sane.vendor = s->vendor_name;
420 s->sane.model = s->product_name;
421 s->sane.type = "scanner";
422
423 s->next = scanner_devList;
424 scanner_devList = s;
425
426 DBG (10, "attach_one: finish\n");
427
428 return SANE_STATUS_GOOD;
429 }
430
431 /*
432 * connect the fd in the scanner struct
433 */
434 static SANE_Status
connect_fd(struct scanner * s)435 connect_fd (struct scanner *s)
436 {
437 SANE_Status ret = SANE_STATUS_GOOD;
438 int buffer_size = s->buffer_size;
439
440 DBG (10, "connect_fd: start\n");
441
442 if(s->fd > -1){
443 DBG (5, "connect_fd: already open\n");
444 ret = SANE_STATUS_GOOD;
445 }
446 else {
447 ret = sanei_scsi_open_extended (s->device_name, &(s->fd), sense_handler,
448 s, &s->buffer_size);
449 if(!ret && buffer_size != s->buffer_size){
450 DBG (5, "connect_fd: cannot get requested buffer size (%d/%d)\n",
451 buffer_size, s->buffer_size);
452 }
453 else{
454 DBG (15, "connect_fd: opened SCSI device\n");
455 }
456 }
457
458 DBG (10, "connect_fd: finish %d\n", ret);
459
460 return ret;
461 }
462
463 /*
464 * This routine will check if a certain device is a Kodak scanner
465 * It also copies interesting data from INQUIRY into the handle structure
466 */
467 static SANE_Status
init_inquire(struct scanner * s)468 init_inquire (struct scanner *s)
469 {
470 int i;
471 SANE_Status ret;
472
473 unsigned char cmd[INQUIRY_len];
474 size_t cmdLen = INQUIRY_len;
475
476 unsigned char in[I_data_len];
477 size_t inLen = I_data_len;
478
479 DBG (10, "init_inquire: start\n");
480
481 memset(cmd,0,cmdLen);
482 set_SCSI_opcode(cmd, INQUIRY_code);
483 set_I_evpd (cmd, 0);
484 set_I_page_code (cmd, I_page_code_default);
485 set_I_data_length (cmd, inLen);
486
487 ret = do_cmd (
488 s, 1, 0,
489 cmd, cmdLen,
490 NULL, 0,
491 in, &inLen
492 );
493
494 if (ret != SANE_STATUS_GOOD){
495 return ret;
496 }
497
498 if (get_I_periph_qual(in) != I_periph_qual_valid){
499 DBG (5, "The device at '%s' has invalid periph_qual.\n", s->device_name);
500 return SANE_STATUS_INVAL;
501 }
502
503 if (get_I_periph_devtype(in) != I_periph_devtype_scanner){
504 DBG (5, "The device at '%s' is not a scanner.\n", s->device_name);
505 return SANE_STATUS_INVAL;
506 }
507
508 get_I_vendor (in, s->vendor_name);
509 get_I_product (in, s->product_name);
510 get_I_version (in, s->version_name);
511 get_I_build (in, s->build_name);
512
513 s->vendor_name[8] = 0;
514 s->product_name[16] = 0;
515 s->version_name[4] = 0;
516 s->build_name[2] = 0;
517
518 /* gobble trailing spaces */
519 for (i = 7; s->vendor_name[i] == ' ' && i >= 0; i--)
520 s->vendor_name[i] = 0;
521 for (i = 15; s->product_name[i] == ' ' && i >= 0; i--)
522 s->product_name[i] = 0;
523 for (i = 3; s->version_name[i] == ' ' && i >= 0; i--)
524 s->version_name[i] = 0;
525 for (i = 2; s->build_name[i] == ' ' && i >= 0; i--)
526 s->build_name[i] = 0;
527
528 if (strcmp ("KODAK", s->vendor_name)) {
529 DBG (5, "The device at '%s' is reported to be made by '%s'\n", s->device_name, s->vendor_name);
530 DBG (5, "This backend only supports Kodak products.\n");
531 return SANE_STATUS_INVAL;
532 }
533
534 DBG (15, "init_inquire: Found '%s' '%s' '%s' '%s' at '%s'\n",
535 s->vendor_name, s->product_name, s->version_name, s->build_name,
536 s->device_name);
537
538 /*defined in SCSI spec*/
539 DBG (15, "standard inquiry options\n");
540
541 /*FIXME: do we need to save these?*/
542 DBG (15, " PQ: %d\n",get_I_periph_qual(in));
543 DBG (15, " PDT: %d\n",get_I_periph_devtype(in));
544
545 DBG (15, " RMB: %d\n",get_I_rmb(in));
546 DBG (15, " DTQ: %d\n",get_I_devtype_qual(in));
547
548 DBG (15, " ISO: %d\n",get_I_iso_version(in));
549 DBG (15, " ECMA: %d\n",get_I_ecma_version(in));
550 DBG (15, " ANSI: %d\n",get_I_ansi_version(in));
551
552 DBG (15, " AENC: %d\n",get_I_aenc(in));
553 DBG (15, " TrmIOP: %d\n",get_I_trmiop(in));
554 DBG (15, " RDF: %d\n",get_I_resonse_format(in));
555
556 DBG (15, " Length: %d\n",get_I_length(in));
557
558 DBG (15, " RelAdr: %d\n",get_I_reladr(in));
559 DBG (15, " WBus32: %d\n",get_I_wbus32(in));
560 DBG (15, " WBus16: %d\n",get_I_wbus16(in));
561 DBG (15, " Sync: %d\n",get_I_sync(in));
562 DBG (15, " Linked: %d\n",get_I_linked(in));
563 DBG (15, " CmdQue: %d\n",get_I_cmdque(in));
564 DBG (15, " SftRe: %d\n",get_I_sftre(in));
565
566 /*kodak specific*/
567 DBG (15, "vendor inquiry options\n");
568
569 DBG (15, " MF Disable: %d\n",get_I_mf_disable(in));
570 DBG (15, " Checkdigit: %d\n",get_I_checkdigit(in));
571 DBG (15, " Front Prism: %d\n",get_I_front_prism(in));
572 DBG (15, " Comp Gray: %d\n",get_I_compressed_gray(in));
573 DBG (15, " Front Toggle: %d\n",get_I_front_toggle(in));
574 DBG (15, " Front DP1: %d\n",get_I_front_dp1(in));
575 DBG (15, " Front Color: %d\n",get_I_front_color(in));
576 DBG (15, " Front ATP: %d\n",get_I_front_atp(in));
577
578 DBG (15, " DP1 180: %d\n",get_I_dp1_180(in));
579 DBG (15, " MF Pause: %d\n",get_I_mf_pause(in));
580 DBG (15, " Rear Prism: %d\n",get_I_rear_prism(in));
581 DBG (15, " Uncomp Gray: %d\n",get_I_uncompressed_gray(in));
582 DBG (15, " Rear Toggle: %d\n",get_I_rear_toggle(in));
583 DBG (15, " Rear DP1: %d\n",get_I_rear_dp1(in));
584 DBG (15, " Rear Color: %d\n",get_I_rear_color(in));
585 DBG (15, " Rear ATP: %d\n",get_I_rear_atp(in));
586
587 /* we actually care about these */
588 DBG (15, " Min Binary Res: %d\n",get_I_min_bin_res(in));
589 s->s_res_min[MODE_LINEART] = get_I_min_bin_res(in);
590 DBG (15, " Max Binary Res: %d\n",get_I_max_bin_res(in));
591 s->s_res_max[MODE_LINEART] = get_I_max_bin_res(in);
592 DBG (15, " Min Color Res: %d\n",get_I_min_col_res(in));
593 s->s_res_min[MODE_COLOR] = get_I_min_col_res(in);
594 DBG (15, " Max Color Res: %d\n",get_I_max_col_res(in));
595 s->s_res_max[MODE_COLOR] = get_I_max_col_res(in);
596
597 DBG (15, " Max Width: %d\n",get_I_max_image_width(in));
598 s->s_width_max = get_I_max_image_width(in);
599 DBG (15, " Max Length: %d\n",get_I_max_image_length(in));
600 s->s_length_max = get_I_max_image_length(in);
601
602 /*FIXME: do we need to save these?*/
603 DBG (15, " Finecrop: %d\n",get_I_finecrop(in));
604 DBG (15, " iThresh: %d\n",get_I_ithresh(in));
605 DBG (15, " ECD: %d\n",get_I_ecd(in));
606 DBG (15, " VBLR: %d\n",get_I_vblr(in));
607 DBG (15, " Elevator: %d\n",get_I_elevator(in));
608 DBG (15, " RelCrop: %d\n",get_I_relcrop(in));
609
610 DBG (15, " CDeskew: %d\n",get_I_cdeskew(in));
611 DBG (15, " IA: %d\n",get_I_ia(in));
612 DBG (15, " Patch: %d\n",get_I_patch(in));
613 DBG (15, " Null Mode: %d\n",get_I_nullmode(in));
614 DBG (15, " SABRE: %d\n",get_I_sabre(in));
615 DBG (15, " LDDDS: %d\n",get_I_lddds(in));
616 DBG (15, " UDDDS: %d\n",get_I_uddds(in));
617 DBG (15, " Fixed Gap: %d\n",get_I_fixedgap(in));
618
619 DBG (15, " HR Printer: %d\n",get_I_hr_printer(in));
620 DBG (15, " Elev 100/250: %d\n",get_I_elev_100_250(in));
621 DBG (15, " UDDS Individual: %d\n",get_I_udds_individual(in));
622 DBG (15, " Auto Color: %d\n",get_I_auto_color(in));
623 DBG (15, " WB: %d\n",get_I_wb(in));
624 DBG (15, " ES: %d\n",get_I_es(in));
625 DBG (15, " FC: %d\n",get_I_fc(in));
626
627 DBG (15, " Max Rate: %d\n",get_I_max_rate(in));
628 DBG (15, " Buffer Size: %d\n",get_I_buffer_size(in));
629
630 DBG (10, "init_inquire: finish\n");
631
632 return SANE_STATUS_GOOD;
633 }
634
635 /*
636 * get model specific info that is not in vpd, and correct
637 * errors in vpd data. struct is already initialized to 0.
638 */
639 static SANE_Status
init_model(struct scanner * s)640 init_model (struct scanner *s)
641 {
642
643 DBG (10, "init_model: start\n");
644
645 s->s_mode[MODE_LINEART] = 1;
646 s->s_mode[MODE_HALFTONE] = 1;
647 s->s_mode[MODE_GRAYSCALE] = 1;
648 s->s_mode[MODE_COLOR] = 1;
649
650 /* scanner did not tell us these */
651 s->s_res_min[MODE_HALFTONE] = s->s_res_min[MODE_LINEART];
652 s->s_res_max[MODE_HALFTONE] = s->s_res_max[MODE_LINEART];
653
654 s->s_res_min[MODE_GRAYSCALE] = s->s_res_min[MODE_COLOR];
655 s->s_res_max[MODE_GRAYSCALE] = s->s_res_max[MODE_COLOR];
656
657 s->s_width_min = 96;
658 s->s_length_min = 96;
659
660 s->s_brightness_steps = 0;
661 s->s_contrast_steps = 255;
662 s->s_threshold_steps = 255;
663 s->s_rif = 1;
664
665 DBG (10, "init_model: finish\n");
666
667 return SANE_STATUS_GOOD;
668 }
669
670 /*
671 * set good default user values.
672 * struct is already initialized to 0.
673 */
674 static SANE_Status
init_user(struct scanner * s)675 init_user (struct scanner *s)
676 {
677
678 DBG (10, "init_user: start\n");
679
680 /* source */
681 s->u_source = SOURCE_ADF_FRONT;
682
683 /* scan mode */
684 s->u_mode = MODE_LINEART;
685
686 /*res, minimum for this mode*/
687 s->u_res = s->s_res_min[s->u_mode];
688
689 /* page width US-Letter */
690 s->u_page_width = 8.5 * 1200;
691 if(s->u_page_width > s->s_width_max){
692 s->u_page_width = s->s_width_max;
693 }
694
695 /* page height US-Letter */
696 s->u_page_height = 11 * 1200;
697 if(s->u_page_height > s->s_length_max){
698 s->u_page_height = s->s_length_max;
699 }
700
701 /* bottom-right x */
702 s->u_br_x = s->u_page_width;
703
704 /* bottom-right y */
705 s->u_br_y = s->u_page_height;
706
707 DBG (10, "init_user: finish\n");
708
709 return SANE_STATUS_GOOD;
710 }
711
712 /*
713 * This function presets the "option" array to blank
714 */
715 static SANE_Status
init_options(struct scanner * s)716 init_options (struct scanner *s)
717 {
718 int i;
719
720 DBG (10, "init_options: start\n");
721
722 memset (s->opt, 0, sizeof (s->opt));
723 for (i = 0; i < NUM_OPTIONS; ++i) {
724 s->opt[i].name = "filler";
725 s->opt[i].size = sizeof (SANE_Word);
726 s->opt[i].cap = SANE_CAP_INACTIVE;
727 }
728
729 /* go ahead and setup the first opt, because
730 * frontend may call control_option on it
731 * before calling get_option_descriptor
732 */
733 s->opt[OPT_NUM_OPTS].name = SANE_NAME_NUM_OPTIONS;
734 s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
735 s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
736 s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
737 s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
738
739 DBG (10, "init_options: finish\n");
740
741 return SANE_STATUS_GOOD;
742 }
743
744 /*
745 * From the SANE spec:
746 * This function is used to establish a connection to a particular
747 * device. The name of the device to be opened is passed in argument
748 * name. If the call completes successfully, a handle for the device
749 * is returned in *h. As a special case, specifying a zero-length
750 * string as the device requests opening the first available device
751 * (if there is such a device).
752 */
753 SANE_Status
sane_open(SANE_String_Const name,SANE_Handle * handle)754 sane_open (SANE_String_Const name, SANE_Handle * handle)
755 {
756 struct scanner *dev = NULL;
757 struct scanner *s = NULL;
758 SANE_Status ret;
759 unsigned char cmd[SEND_len];
760 size_t cmdLen = SEND_len;
761 unsigned char out[SR_len_time]; /*longest used in this function*/
762 int try=0;
763 time_t gmt_tt;
764 struct tm * gmt_tm_p;
765 struct tm * local_tm_p;
766
767 DBG (10, "sane_open: start\n");
768
769 if(scanner_devList){
770 DBG (15, "sane_open: searching currently attached scanners\n");
771 }
772 else{
773 DBG (15, "sane_open: no scanners currently attached, attaching\n");
774
775 ret = sane_get_devices(NULL,0);
776 if(ret != SANE_STATUS_GOOD){
777 return ret;
778 }
779 }
780
781 if(name[0] == 0){
782 DBG (15, "sane_open: no device requested, using default\n");
783 s = scanner_devList;
784 }
785 else{
786 DBG (15, "sane_open: device %s requested\n", name);
787
788 for (dev = scanner_devList; dev; dev = dev->next) {
789 if (strcmp (dev->sane.name, name) == 0) {
790 s = dev;
791 break;
792 }
793 }
794 }
795
796 if (!s) {
797 DBG (5, "sane_open: no device found\n");
798 return SANE_STATUS_INVAL;
799 }
800
801 DBG (15, "sane_open: device %s found\n", s->sane.name);
802
803 *handle = s;
804
805 /* connect the fd so we can talk to scanner */
806 ret = connect_fd(s);
807 if(ret != SANE_STATUS_GOOD){
808 return ret;
809 }
810
811 /*send the end batch (GX) command*/
812 memset(cmd,0,cmdLen);
813 set_SCSI_opcode(cmd,SEND_code);
814 set_SR_datatype_code(cmd,SR_datatype_random);
815 set_SR_datatype_qual(cmd,SR_qual_end);
816 set_SR_xfer_length(cmd,SR_len_end);
817
818 /*start the following loop*/
819 ret = SANE_STATUS_DEVICE_BUSY;
820 s->rs_info = 0;
821
822 /*loop until scanner is ready*/
823 while(ret == SANE_STATUS_DEVICE_BUSY){
824 DBG (15, "sane_open: GX, try %d, sleep %lu\n", try, (unsigned long)s->rs_info);
825 try++;
826 sleep(s->rs_info);
827 ret = do_cmd (
828 s, 1, 0,
829 cmd, cmdLen,
830 NULL, 0,
831 NULL, NULL
832 );
833 if(try > 5){
834 break;
835 }
836 }
837 if(ret){
838 DBG (5, "sane_open: GX error %d\n",ret);
839 return ret;
840 }
841
842 /*send the clear buffer (CB) command*/
843 DBG (15, "sane_open: CB\n");
844 memset(cmd,0,cmdLen);
845 set_SCSI_opcode(cmd,SEND_code);
846 set_SR_datatype_code(cmd,SR_datatype_random);
847 set_SR_datatype_qual(cmd,SR_qual_clear);
848 set_SR_xfer_length(cmd,SR_len_clear);
849
850 ret = do_cmd (
851 s, 1, 0,
852 cmd, cmdLen,
853 NULL, 0,
854 NULL, NULL
855 );
856 if(ret){
857 DBG (5, "sane_open: CB error %d\n",ret);
858 return ret;
859 }
860
861 /*send the GT command*/
862 DBG (15, "sane_open: GT\n");
863 gmt_tt = time(NULL);
864 gmt_tm_p = gmtime(&gmt_tt);
865
866 memset(cmd,0,cmdLen);
867 set_SCSI_opcode(cmd,SEND_code);
868 set_SR_datatype_code(cmd,SR_datatype_random);
869 set_SR_datatype_qual(cmd,SR_qual_gmt);
870 set_SR_xfer_length(cmd,SR_len_time);
871
872 memset(out,0,SR_len_time);
873 set_SR_payload_len(out,SR_len_time);
874 set_SR_time_hour(out,gmt_tm_p->tm_hour);
875 set_SR_time_min(out,gmt_tm_p->tm_min);
876 set_SR_time_mon(out,gmt_tm_p->tm_mon);
877 set_SR_time_day(out,gmt_tm_p->tm_mday);
878 set_SR_time_year(out,gmt_tm_p->tm_year+1900);
879
880 ret = do_cmd (
881 s, 1, 0,
882 cmd, cmdLen,
883 out, SR_len_time,
884 NULL, NULL
885 );
886 if(ret){
887 DBG (5, "sane_open: GT error %d\n",ret);
888 return ret;
889 }
890
891 /*FIXME: read the LC command? */
892
893 /*send the LC command*/
894 DBG (15, "sane_open: LC\n");
895 gmt_tt = time(NULL);
896 local_tm_p = localtime(&gmt_tt);
897
898 memset(cmd,0,cmdLen);
899 set_SCSI_opcode(cmd,SEND_code);
900 set_SR_datatype_code(cmd,SR_datatype_random);
901 set_SR_datatype_qual(cmd,SR_qual_clock);
902 set_SR_xfer_length(cmd,SR_len_time);
903
904 memset(out,0,SR_len_time);
905 set_SR_payload_len(out,SR_len_time);
906 set_SR_time_hour(out,local_tm_p->tm_hour);
907 set_SR_time_min(out,local_tm_p->tm_min);
908 set_SR_time_mon(out,local_tm_p->tm_mon);
909 set_SR_time_day(out,local_tm_p->tm_mday);
910 set_SR_time_year(out,local_tm_p->tm_year+1900);
911
912 ret = do_cmd (
913 s, 1, 0,
914 cmd, cmdLen,
915 out, SR_len_time,
916 NULL, NULL
917 );
918 if(ret){
919 DBG (5, "sane_open: LC error %d\n",ret);
920 return ret;
921 }
922
923 DBG (10, "sane_open: finish\n");
924
925 return SANE_STATUS_GOOD;
926 }
927
928 /*
929 * @@ Section 3 - SANE Options functions
930 */
931
932 /*
933 * Returns the options we know.
934 *
935 * From the SANE spec:
936 * This function is used to access option descriptors. The function
937 * returns the option descriptor for option number n of the device
938 * represented by handle h. Option number 0 is guaranteed to be a
939 * valid option. Its value is an integer that specifies the number of
940 * options that are available for device handle h (the count includes
941 * option 0). If n is not a valid option index, the function returns
942 * NULL. The returned option descriptor is guaranteed to remain valid
943 * (and at the returned address) until the device is closed.
944 */
945 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle,SANE_Int option)946 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
947 {
948 struct scanner *s = handle;
949 int i;
950 SANE_Option_Descriptor *opt = &s->opt[option];
951
952 DBG (20, "sane_get_option_descriptor: %d\n", option);
953
954 if ((unsigned) option >= NUM_OPTIONS)
955 return NULL;
956
957 /* "Mode" group -------------------------------------------------------- */
958 if(option==OPT_MODE_GROUP){
959 opt->title = "Scan Mode";
960 opt->desc = "";
961 opt->type = SANE_TYPE_GROUP;
962 opt->constraint_type = SANE_CONSTRAINT_NONE;
963 }
964
965 /* source */
966 if(option==OPT_SOURCE){
967 i=0;
968 s->o_source_list[i++]=STRING_ADFFRONT;
969 s->o_source_list[i++]=STRING_ADFBACK;
970 s->o_source_list[i++]=STRING_ADFDUPLEX;
971 s->o_source_list[i]=NULL;
972
973 opt->name = SANE_NAME_SCAN_SOURCE;
974 opt->title = SANE_TITLE_SCAN_SOURCE;
975 opt->desc = SANE_DESC_SCAN_SOURCE;
976 opt->type = SANE_TYPE_STRING;
977 opt->constraint_type = SANE_CONSTRAINT_STRING_LIST;
978 opt->constraint.string_list = s->o_source_list;
979 opt->size = maxStringSize (opt->constraint.string_list);
980 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
981 }
982
983 /* scan mode */
984 if(option==OPT_MODE){
985 i=0;
986 if(s->s_mode[MODE_LINEART]){
987 s->o_mode_list[i++]=STRING_LINEART;
988 }
989 if(s->s_mode[MODE_HALFTONE]){
990 s->o_mode_list[i++]=STRING_HALFTONE;
991 }
992 if(s->s_mode[MODE_GRAYSCALE]){
993 s->o_mode_list[i++]=STRING_GRAYSCALE;
994 }
995 if(s->s_mode[MODE_COLOR]){
996 s->o_mode_list[i++]=STRING_COLOR;
997 }
998 s->o_mode_list[i]=NULL;
999
1000 opt->name = SANE_NAME_SCAN_MODE;
1001 opt->title = SANE_TITLE_SCAN_MODE;
1002 opt->desc = SANE_DESC_SCAN_MODE;
1003 opt->type = SANE_TYPE_STRING;
1004 opt->constraint_type = SANE_CONSTRAINT_STRING_LIST;
1005 opt->constraint.string_list = s->o_mode_list;
1006 opt->size = maxStringSize (opt->constraint.string_list);
1007 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1008 }
1009
1010 /* resolution */
1011 /* build a list of possible choices for current mode */
1012 if(option==OPT_RES){
1013 int reslist[]={100,150,200,240,300,400};
1014 int j;
1015
1016 i=0;
1017 for(j=0;j<6;j++){
1018 if(reslist[j] >= s->s_res_min[s->u_mode]
1019 && reslist[j] <= s->s_res_max[s->u_mode]){
1020 s->o_res_list[s->u_mode][++i] = reslist[j];
1021 }
1022 }
1023 s->o_res_list[s->u_mode][0] = i;
1024
1025 opt->name = SANE_NAME_SCAN_RESOLUTION;
1026 opt->title = SANE_TITLE_SCAN_RESOLUTION;
1027 opt->desc = SANE_DESC_SCAN_RESOLUTION;
1028 opt->type = SANE_TYPE_INT;
1029 opt->unit = SANE_UNIT_DPI;
1030 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1031 opt->constraint_type = SANE_CONSTRAINT_WORD_LIST;
1032 opt->constraint.word_list = s->o_res_list[s->u_mode];
1033 }
1034
1035 /* "Geometry" group ---------------------------------------------------- */
1036 if(option==OPT_GEOMETRY_GROUP){
1037 opt->title = "Geometry";
1038 opt->desc = "";
1039 opt->type = SANE_TYPE_GROUP;
1040 opt->constraint_type = SANE_CONSTRAINT_NONE;
1041 }
1042
1043 /* top-left x */
1044 if(option==OPT_TL_X){
1045 /* values stored in 1200 dpi units */
1046 /* must be converted to MM for sane */
1047 s->o_tl_x_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_width_min);
1048 s->o_tl_x_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_width_max);
1049 s->o_tl_x_range.quant = MM_PER_UNIT_FIX;
1050
1051 opt->name = SANE_NAME_SCAN_TL_X;
1052 opt->title = SANE_TITLE_SCAN_TL_X;
1053 opt->desc = SANE_DESC_SCAN_TL_X;
1054 opt->type = SANE_TYPE_FIXED;
1055 opt->unit = SANE_UNIT_MM;
1056 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1057 opt->constraint.range = &(s->o_tl_x_range);
1058 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1059 }
1060
1061 /* top-left y */
1062 if(option==OPT_TL_Y){
1063 /* values stored in 1200 dpi units */
1064 /* must be converted to MM for sane */
1065 s->o_tl_y_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_length_min);
1066 s->o_tl_y_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_length_max);
1067 s->o_tl_y_range.quant = MM_PER_UNIT_FIX;
1068
1069 opt->name = SANE_NAME_SCAN_TL_Y;
1070 opt->title = SANE_TITLE_SCAN_TL_Y;
1071 opt->desc = SANE_DESC_SCAN_TL_Y;
1072 opt->type = SANE_TYPE_FIXED;
1073 opt->unit = SANE_UNIT_MM;
1074 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1075 opt->constraint.range = &(s->o_tl_y_range);
1076 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1077 }
1078
1079 /* bottom-right x */
1080 if(option==OPT_BR_X){
1081 /* values stored in 1200 dpi units */
1082 /* must be converted to MM for sane */
1083 s->o_br_x_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_width_min);
1084 s->o_br_x_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_width_max);
1085 s->o_br_x_range.quant = MM_PER_UNIT_FIX;
1086
1087 opt->name = SANE_NAME_SCAN_BR_X;
1088 opt->title = SANE_TITLE_SCAN_BR_X;
1089 opt->desc = SANE_DESC_SCAN_BR_X;
1090 opt->type = SANE_TYPE_FIXED;
1091 opt->unit = SANE_UNIT_MM;
1092 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1093 opt->constraint.range = &(s->o_br_x_range);
1094 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1095 }
1096
1097 /* bottom-right y */
1098 if(option==OPT_BR_Y){
1099 /* values stored in 1200 dpi units */
1100 /* must be converted to MM for sane */
1101 s->o_br_y_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_length_min);
1102 s->o_br_y_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_length_max);
1103 s->o_br_y_range.quant = MM_PER_UNIT_FIX;
1104
1105 opt->name = SANE_NAME_SCAN_BR_Y;
1106 opt->title = SANE_TITLE_SCAN_BR_Y;
1107 opt->desc = SANE_DESC_SCAN_BR_Y;
1108 opt->type = SANE_TYPE_FIXED;
1109 opt->unit = SANE_UNIT_MM;
1110 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1111 opt->constraint.range = &(s->o_br_y_range);
1112 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1113 }
1114
1115 /* page width */
1116 if(option==OPT_PAGE_WIDTH){
1117 /* values stored in 1200 dpi units */
1118 /* must be converted to MM for sane */
1119 s->o_page_x_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_width_min);
1120 s->o_page_x_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_width_max);
1121 s->o_page_x_range.quant = MM_PER_UNIT_FIX;
1122
1123 opt->name = "pagewidth";
1124 opt->title = "ADF paper width";
1125 opt->desc = "Must be set properly to align scanning window";
1126 opt->type = SANE_TYPE_FIXED;
1127 opt->unit = SANE_UNIT_MM;
1128 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1129 opt->constraint.range = &s->o_page_x_range;
1130 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1131 }
1132
1133 /* page height */
1134 if(option==OPT_PAGE_HEIGHT){
1135 /* values stored in 1200 dpi units */
1136 /* must be converted to MM for sane */
1137 s->o_page_y_range.min = SCANNER_UNIT_TO_FIXED_MM(s->s_length_min);
1138 s->o_page_y_range.max = SCANNER_UNIT_TO_FIXED_MM(s->s_length_max);
1139 s->o_page_y_range.quant = MM_PER_UNIT_FIX;
1140
1141 opt->name = "pageheight";
1142 opt->title = "ADF paper length";
1143 opt->desc = "Must be set properly to eject pages";
1144 opt->type = SANE_TYPE_FIXED;
1145 opt->unit = SANE_UNIT_MM;
1146 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1147 opt->constraint.range = &s->o_page_y_range;
1148 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1149 }
1150
1151 /* "Enhancement" group ------------------------------------------------- */
1152 if(option==OPT_ENHANCEMENT_GROUP){
1153 opt->title = "Enhancement";
1154 opt->desc = "";
1155 opt->type = SANE_TYPE_GROUP;
1156 opt->constraint_type = SANE_CONSTRAINT_NONE;
1157 }
1158
1159 /* brightness */
1160 if(option==OPT_BRIGHTNESS){
1161 opt->name = SANE_NAME_BRIGHTNESS;
1162 opt->title = SANE_TITLE_BRIGHTNESS;
1163 opt->desc = SANE_DESC_BRIGHTNESS;
1164 opt->type = SANE_TYPE_INT;
1165 opt->unit = SANE_UNIT_NONE;
1166 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1167 opt->constraint.range = &s->o_brightness_range;
1168 s->o_brightness_range.quant=1;
1169 s->o_brightness_range.min=-(s->s_brightness_steps/2);
1170 s->o_brightness_range.max=s->s_brightness_steps/2;
1171 if(opt->constraint.range->max > opt->constraint.range->min){
1172 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1173 }
1174 else{
1175 opt->cap = SANE_CAP_INACTIVE;
1176 }
1177 }
1178
1179 /* contrast */
1180 if(option==OPT_CONTRAST){
1181 opt->name = SANE_NAME_CONTRAST;
1182 opt->title = SANE_TITLE_CONTRAST;
1183 opt->desc = SANE_DESC_CONTRAST;
1184 opt->type = SANE_TYPE_INT;
1185 opt->unit = SANE_UNIT_NONE;
1186 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1187 opt->constraint.range = &s->o_contrast_range;
1188 s->o_contrast_range.quant=1;
1189 s->o_contrast_range.min=-(s->s_contrast_steps/2);
1190 s->o_contrast_range.max=s->s_contrast_steps/2;
1191 if(opt->constraint.range->max > opt->constraint.range->min){
1192 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1193 }
1194 else{
1195 opt->cap = SANE_CAP_INACTIVE;
1196 }
1197 }
1198
1199 /*threshold*/
1200 if(option==OPT_THRESHOLD){
1201 opt->name = SANE_NAME_THRESHOLD;
1202 opt->title = SANE_TITLE_THRESHOLD;
1203 opt->desc = SANE_DESC_THRESHOLD;
1204 opt->type = SANE_TYPE_INT;
1205 opt->unit = SANE_UNIT_NONE;
1206 opt->constraint_type = SANE_CONSTRAINT_RANGE;
1207 opt->constraint.range = &s->o_threshold_range;
1208 s->o_threshold_range.min=0;
1209 s->o_threshold_range.max=s->s_threshold_steps;
1210 s->o_threshold_range.quant=1;
1211 if(opt->constraint.range->max > opt->constraint.range->min){
1212 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1213 }
1214 else{
1215 opt->cap = SANE_CAP_INACTIVE;
1216 }
1217 }
1218
1219 /*rif*/
1220 if(option==OPT_RIF){
1221 opt->name = "rif";
1222 opt->title = "RIF";
1223 opt->desc = "Reverse image format";
1224 opt->type = SANE_TYPE_BOOL;
1225 opt->unit = SANE_UNIT_NONE;
1226 if (s->s_rif)
1227 opt->cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1228 else
1229 opt->cap = SANE_CAP_INACTIVE;
1230 }
1231
1232 return opt;
1233 }
1234
1235 /**
1236 * Gets or sets an option value.
1237 *
1238 * From the SANE spec:
1239 * This function is used to set or inquire the current value of option
1240 * number n of the device represented by handle h. The manner in which
1241 * the option is controlled is specified by parameter action. The
1242 * possible values of this parameter are described in more detail
1243 * below. The value of the option is passed through argument val. It
1244 * is a pointer to the memory that holds the option value. The memory
1245 * area pointed to by v must be big enough to hold the entire option
1246 * value (determined by member size in the corresponding option
1247 * descriptor).
1248 *
1249 * The only exception to this rule is that when setting the value of a
1250 * string option, the string pointed to by argument v may be shorter
1251 * since the backend will stop reading the option value upon
1252 * encountering the first NUL terminator in the string. If argument i
1253 * is not NULL, the value of *i will be set to provide details on how
1254 * well the request has been met.
1255 */
1256 SANE_Status
sane_control_option(SANE_Handle handle,SANE_Int option,SANE_Action action,void * val,SANE_Int * info)1257 sane_control_option (SANE_Handle handle, SANE_Int option,
1258 SANE_Action action, void *val, SANE_Int * info)
1259 {
1260 struct scanner *s = (struct scanner *) handle;
1261 SANE_Int dummy = 0;
1262
1263 /* Make sure that all those statements involving *info cannot break (better
1264 * than having to do "if (info) ..." everywhere!)
1265 */
1266 if (info == 0)
1267 info = &dummy;
1268
1269 if (option >= NUM_OPTIONS) {
1270 DBG (5, "sane_control_option: %d too big\n", option);
1271 return SANE_STATUS_INVAL;
1272 }
1273
1274 if (!SANE_OPTION_IS_ACTIVE (s->opt[option].cap)) {
1275 DBG (5, "sane_control_option: %d inactive\n", option);
1276 return SANE_STATUS_INVAL;
1277 }
1278
1279 /*
1280 * SANE_ACTION_GET_VALUE: We have to find out the current setting and
1281 * return it in a human-readable form (often, text).
1282 */
1283 if (action == SANE_ACTION_GET_VALUE) {
1284 SANE_Word * val_p = (SANE_Word *) val;
1285
1286 DBG (20, "sane_control_option: get value for '%s' (%d)\n", s->opt[option].name,option);
1287
1288 switch (option) {
1289
1290 case OPT_NUM_OPTS:
1291 *val_p = NUM_OPTIONS;
1292 return SANE_STATUS_GOOD;
1293
1294 case OPT_SOURCE:
1295 if(s->u_source == SOURCE_ADF_FRONT){
1296 strcpy (val, STRING_ADFFRONT);
1297 }
1298 else if(s->u_source == SOURCE_ADF_BACK){
1299 strcpy (val, STRING_ADFBACK);
1300 }
1301 else if(s->u_source == SOURCE_ADF_DUPLEX){
1302 strcpy (val, STRING_ADFDUPLEX);
1303 }
1304 else{
1305 DBG(5,"missing option val for source\n");
1306 }
1307 return SANE_STATUS_GOOD;
1308
1309 case OPT_MODE:
1310 if(s->u_mode == MODE_LINEART){
1311 strcpy (val, STRING_LINEART);
1312 }
1313 else if(s->u_mode == MODE_HALFTONE){
1314 strcpy (val, STRING_HALFTONE);
1315 }
1316 else if(s->u_mode == MODE_GRAYSCALE){
1317 strcpy (val, STRING_GRAYSCALE);
1318 }
1319 else if(s->u_mode == MODE_COLOR){
1320 strcpy (val, STRING_COLOR);
1321 }
1322 return SANE_STATUS_GOOD;
1323
1324 case OPT_RES:
1325 *val_p = s->u_res;
1326 return SANE_STATUS_GOOD;
1327
1328 case OPT_TL_X:
1329 *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_tl_x);
1330 return SANE_STATUS_GOOD;
1331
1332 case OPT_TL_Y:
1333 *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_tl_y);
1334 return SANE_STATUS_GOOD;
1335
1336 case OPT_BR_X:
1337 *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_br_x);
1338 return SANE_STATUS_GOOD;
1339
1340 case OPT_BR_Y:
1341 *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_br_y);
1342 return SANE_STATUS_GOOD;
1343
1344 case OPT_PAGE_WIDTH:
1345 *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_page_width);
1346 return SANE_STATUS_GOOD;
1347
1348 case OPT_PAGE_HEIGHT:
1349 *val_p = SCANNER_UNIT_TO_FIXED_MM(s->u_page_height);
1350 return SANE_STATUS_GOOD;
1351
1352 case OPT_BRIGHTNESS:
1353 *val_p = s->u_brightness;
1354 return SANE_STATUS_GOOD;
1355
1356 case OPT_CONTRAST:
1357 *val_p = s->u_contrast;
1358 return SANE_STATUS_GOOD;
1359
1360 case OPT_THRESHOLD:
1361 *val_p = s->u_threshold;
1362 return SANE_STATUS_GOOD;
1363
1364 case OPT_RIF:
1365 *val_p = s->u_rif;
1366 return SANE_STATUS_GOOD;
1367 }
1368 }
1369 else if (action == SANE_ACTION_SET_VALUE) {
1370 int tmp;
1371 SANE_Word val_c;
1372 SANE_Status status;
1373
1374 DBG (20, "sane_control_option: set value for '%s' (%d)\n", s->opt[option].name,option);
1375
1376 if ( s->started ) {
1377 DBG (5, "sane_control_option: can't set, device busy\n");
1378 return SANE_STATUS_DEVICE_BUSY;
1379 }
1380
1381 if (!SANE_OPTION_IS_SETTABLE (s->opt[option].cap)) {
1382 DBG (5, "sane_control_option: not settable\n");
1383 return SANE_STATUS_INVAL;
1384 }
1385
1386 status = sanei_constrain_value (s->opt + option, val, info);
1387 if (status != SANE_STATUS_GOOD) {
1388 DBG (5, "sane_control_option: bad value\n");
1389 return status;
1390 }
1391
1392 /* may have been changed by constrain, so don't copy until now */
1393 val_c = *(SANE_Word *)val;
1394
1395 /*
1396 * Note - for those options which can assume one of a list of
1397 * valid values, we can safely assume that they will have
1398 * exactly one of those values because that's what
1399 * sanei_constrain_value does. Hence no "else: invalid" branches
1400 * below.
1401 */
1402 switch (option) {
1403
1404 /* Mode Group */
1405 case OPT_SOURCE:
1406 if (!strcmp (val, STRING_ADFFRONT)) {
1407 tmp = SOURCE_ADF_FRONT;
1408 }
1409 else if (!strcmp (val, STRING_ADFBACK)) {
1410 tmp = SOURCE_ADF_BACK;
1411 }
1412 else{
1413 tmp = SOURCE_ADF_DUPLEX;
1414 }
1415
1416 if (s->u_source != tmp) {
1417 s->u_source = tmp;
1418 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
1419 }
1420 return SANE_STATUS_GOOD;
1421
1422 case OPT_MODE:
1423 if (!strcmp (val, STRING_LINEART)) {
1424 tmp = MODE_LINEART;
1425 }
1426 else if (!strcmp (val, STRING_HALFTONE)) {
1427 tmp = MODE_HALFTONE;
1428 }
1429 else if (!strcmp (val, STRING_GRAYSCALE)) {
1430 tmp = MODE_GRAYSCALE;
1431 }
1432 else{
1433 tmp = MODE_COLOR;
1434 }
1435
1436 if (tmp != s->u_mode){
1437 s->u_mode = tmp;
1438 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
1439 }
1440 return SANE_STATUS_GOOD;
1441
1442 case OPT_RES:
1443
1444 if (s->u_res != val_c) {
1445 s->u_res = val_c;
1446 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
1447 }
1448 return SANE_STATUS_GOOD;
1449
1450 /* Geometry Group */
1451 case OPT_TL_X:
1452 if (s->u_tl_x != FIXED_MM_TO_SCANNER_UNIT(val_c)){
1453 s->u_tl_x = FIXED_MM_TO_SCANNER_UNIT(val_c);
1454 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
1455 }
1456 return SANE_STATUS_GOOD;
1457
1458 case OPT_TL_Y:
1459 if (s->u_tl_y != FIXED_MM_TO_SCANNER_UNIT(val_c)){
1460 s->u_tl_y = FIXED_MM_TO_SCANNER_UNIT(val_c);
1461 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
1462 }
1463 return SANE_STATUS_GOOD;
1464
1465 case OPT_BR_X:
1466 if (s->u_br_x != FIXED_MM_TO_SCANNER_UNIT(val_c)){
1467 s->u_br_x = FIXED_MM_TO_SCANNER_UNIT(val_c);
1468 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
1469 }
1470 return SANE_STATUS_GOOD;
1471
1472 case OPT_BR_Y:
1473 if (s->u_br_y != FIXED_MM_TO_SCANNER_UNIT(val_c)){
1474 s->u_br_y = FIXED_MM_TO_SCANNER_UNIT(val_c);
1475 *info |= SANE_INFO_RELOAD_PARAMS | SANE_INFO_RELOAD_OPTIONS;
1476 }
1477 return SANE_STATUS_GOOD;
1478
1479 case OPT_PAGE_WIDTH:
1480 if (s->u_page_width != FIXED_MM_TO_SCANNER_UNIT(val_c)){
1481 s->u_page_width = FIXED_MM_TO_SCANNER_UNIT(val_c);
1482 *info |= SANE_INFO_RELOAD_OPTIONS;
1483 }
1484 return SANE_STATUS_GOOD;
1485
1486 case OPT_PAGE_HEIGHT:
1487 if (s->u_page_height != FIXED_MM_TO_SCANNER_UNIT(val_c)){
1488 s->u_page_height = FIXED_MM_TO_SCANNER_UNIT(val_c);
1489 *info |= SANE_INFO_RELOAD_OPTIONS;
1490 }
1491 return SANE_STATUS_GOOD;
1492
1493 /* Enhancement Group */
1494 case OPT_BRIGHTNESS:
1495 if (s->u_brightness != val_c){
1496 s->u_brightness = val_c;
1497 }
1498 return SANE_STATUS_GOOD;
1499
1500 case OPT_CONTRAST:
1501 if (s->u_contrast != val_c){
1502 s->u_contrast = val_c;
1503 }
1504 return SANE_STATUS_GOOD;
1505
1506 case OPT_THRESHOLD:
1507 if (s->u_threshold != val_c){
1508 s->u_threshold = val_c;
1509 }
1510 return SANE_STATUS_GOOD;
1511
1512 case OPT_RIF:
1513 if (s->u_rif != val_c){
1514 s->u_rif = val_c;
1515 }
1516 return SANE_STATUS_GOOD;
1517
1518 } /* switch */
1519 } /* else */
1520
1521 return SANE_STATUS_INVAL;
1522 }
1523
1524 /*
1525 * @@ Section 4 - SANE scanning functions
1526 */
1527 /*
1528 * Called by SANE to retrieve information about the type of data
1529 * that the current scan will return.
1530 *
1531 * From the SANE spec:
1532 * This function is used to obtain the current scan parameters. The
1533 * returned parameters are guaranteed to be accurate between the time
1534 * a scan has been started (sane_start() has been called) and the
1535 * completion of that request. Outside of that window, the returned
1536 * values are best-effort estimates of what the parameters will be
1537 * when sane_start() gets invoked.
1538 *
1539 * Calling this function before a scan has actually started allows,
1540 * for example, to get an estimate of how big the scanned image will
1541 * be. The parameters passed to this function are the handle h of the
1542 * device for which the parameters should be obtained and a pointer p
1543 * to a parameter structure.
1544 */
1545 /* SANE_Parameters is defined as a struct containing:
1546 SANE_Frame format;
1547 SANE_Bool last_frame;
1548 SANE_Int lines;
1549 SANE_Int depth; ( binary=1, gray=8, color=8 (!24) )
1550 SANE_Int pixels_per_line;
1551 SANE_Int bytes_per_line;
1552 */
1553 SANE_Status
sane_get_parameters(SANE_Handle handle,SANE_Parameters * params)1554 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
1555 {
1556 SANE_Status ret = SANE_STATUS_GOOD;
1557 struct scanner *s = (struct scanner *) handle;
1558
1559 DBG (10, "sane_get_parameters: start\n");
1560
1561 /* started? get param data from image header */
1562 if(s->started){
1563 DBG (15, "sane_get_parameters: image settings:\n");
1564
1565 DBG (15, " tlx=%d, brx=%d, iw=%d, maxx=%d\n",
1566 s->i_tlx, (s->i_tlx+s->i_width), s->i_width, s->s_width_max/1200);
1567 DBG (15, " tly=%d, bry=%d, il=%d, maxy=%d\n",
1568 s->i_tly, (s->i_tly+s->i_length), s->i_length, s->s_length_max/1200);
1569 DBG (15, " res=%d, id=%d, bytes=%d\n",
1570 s->i_dpi, s->i_id, s->i_bytes);
1571
1572 params->last_frame = 1;
1573 params->lines = s->i_length;
1574 params->pixels_per_line = s->i_width;
1575
1576 /* bitonal */
1577 if (s->i_bpp == 1) {
1578 params->format = SANE_FRAME_GRAY;
1579 params->depth = 1;
1580 params->bytes_per_line = params->pixels_per_line / 8;
1581
1582 #ifdef SANE_FRAME_G42D
1583 /*G4 fax compression*/
1584 if (s->i_compr) {
1585 params->format = SANE_FRAME_G42D;
1586 }
1587 #endif
1588 }
1589 /* gray */
1590 else if (s->i_bpp == 8) {
1591 params->format = SANE_FRAME_GRAY;
1592 params->depth = 8;
1593 params->bytes_per_line = params->pixels_per_line;
1594
1595 #ifdef SANE_FRAME_JPEG
1596 /*jpeg compression*/
1597 if (s->i_compr) {
1598 params->format = SANE_FRAME_JPEG;
1599 }
1600 #endif
1601 }
1602 /* color */
1603 else if (s->i_bpp == 24 || s->i_bpp == 96) {
1604 params->format = SANE_FRAME_RGB;
1605 params->depth = 8;
1606 params->bytes_per_line = params->pixels_per_line * 3;
1607
1608 #ifdef SANE_FRAME_JPEG
1609 /*jpeg compression*/
1610 if (s->i_compr) {
1611 params->format = SANE_FRAME_JPEG;
1612 }
1613 #endif
1614 }
1615 else{
1616 DBG(5,"sane_get_parameters: unsupported depth %d\n", s->i_bpp);
1617 return SANE_STATUS_INVAL;
1618 }
1619 }
1620
1621 /* not started? get param data from user input */
1622 else{
1623
1624 DBG (15, "sane_get_parameters: user settings:\n");
1625
1626 DBG (15, " tlx=%d, brx=%d, pw=%d, maxx=%d\n",
1627 s->u_tl_x, s->u_br_x, s->u_page_width, s->s_width_max);
1628 DBG (15, " tly=%d, bry=%d, ph=%d, maxy=%d\n",
1629 s->u_tl_y, s->u_br_y, s->u_page_height, s->s_length_max);
1630 DBG (15, " res=%d, user_x=%d, user_y=%d\n",
1631 s->u_res, (s->u_res * (s->u_br_x - s->u_tl_x) / 1200),
1632 (s->u_res * (s->u_br_y - s->u_tl_y) / 1200));
1633
1634 if (s->u_mode == MODE_COLOR) {
1635 params->format = SANE_FRAME_RGB;
1636 params->depth = 8;
1637 }
1638 else if (s->u_mode == MODE_GRAYSCALE) {
1639 params->format = SANE_FRAME_GRAY;
1640 params->depth = 8;
1641 }
1642 else {
1643 params->format = SANE_FRAME_GRAY;
1644 params->depth = 1;
1645 }
1646
1647 params->last_frame = 1;
1648 params->lines = s->u_res * (s->u_br_y - s->u_tl_y) / 1200;
1649 params->pixels_per_line = s->u_res * (s->u_br_x - s->u_tl_x) / 1200;
1650
1651 /* bytes per line differs by mode */
1652 if (s->u_mode == MODE_COLOR) {
1653 params->bytes_per_line = params->pixels_per_line * 3;
1654 }
1655 else if (s->u_mode == MODE_GRAYSCALE) {
1656 params->bytes_per_line = params->pixels_per_line;
1657 }
1658 else {
1659 params->bytes_per_line = params->pixels_per_line / 8;
1660 }
1661
1662 }
1663
1664 DBG (15, "sane_get_parameters: returning:\n");
1665 DBG (15, " scan_x=%d, Bpl=%d, depth=%d\n",
1666 params->pixels_per_line, params->bytes_per_line, params->depth );
1667
1668 DBG (15, " scan_y=%d, frame=%d, last=%d\n",
1669 params->lines, params->format, params->last_frame );
1670
1671 DBG (10, "sane_get_parameters: finish\n");
1672
1673 return ret;
1674 }
1675
1676 /*
1677 * Called by SANE when a page acquisition operation is to be started.
1678 * commands: scanner control (lampon), send (lut), send (dither),
1679 * set window, object pos, and scan
1680 *
1681 * this will be called before each image, including duplex backsides,
1682 * and at the start of adf batch.
1683 * hence, we spend a lot of time playing with s->started, etc.
1684 */
1685 SANE_Status
sane_start(SANE_Handle handle)1686 sane_start (SANE_Handle handle)
1687 {
1688 struct scanner *s = handle;
1689 SANE_Status ret;
1690
1691 DBG (10, "sane_start: start\n");
1692
1693 DBG (15, "started=%d, source=%d\n", s->started, s->u_source);
1694
1695 /* batch already running */
1696 if(s->started){
1697 /* not finished with current image, error */
1698 if (s->bytes_tx != s->i_bytes) {
1699 DBG(5,"sane_start: previous transfer not finished?");
1700 return do_cancel(s);
1701 }
1702 }
1703
1704 /* first page of batch */
1705 else{
1706
1707 unsigned char cmd[SCAN_len];
1708 unsigned char pay[SR_len_startstop];
1709
1710 /* set window command */
1711 ret = set_window(s);
1712 if (ret != SANE_STATUS_GOOD) {
1713 DBG (5, "sane_start: ERROR: cannot set window\n");
1714 do_cancel(s);
1715 return ret;
1716 }
1717
1718 /* read/send JQ command */
1719
1720 /* read/send SC command */
1721 ret = send_sc(s);
1722 if (ret != SANE_STATUS_GOOD) {
1723 DBG (5, "sane_start: ERROR: cannot send SC\n");
1724 do_cancel(s);
1725 return ret;
1726 }
1727
1728 /* read/send CT command */
1729
1730 DBG (15, "sane_start: send SCAN\n");
1731 memset(cmd, 0, SCAN_len);
1732 set_SCSI_opcode(cmd, SCAN_code);
1733
1734 ret = do_cmd (
1735 s, 1, 0,
1736 cmd, SCAN_len,
1737 NULL, 0,
1738 NULL, NULL
1739 );
1740 if (ret != SANE_STATUS_GOOD) {
1741 DBG (5, "sane_start: ERROR sending SCAN\n");
1742 do_cancel(s);
1743 return ret;
1744 }
1745
1746 /* send SS command */
1747 DBG (15, "sane_start: send SS\n");
1748 memset(cmd,0,SEND_len);
1749 set_SCSI_opcode(cmd,SEND_code);
1750 set_SR_datatype_code(cmd,SR_datatype_random);
1751 set_SR_datatype_qual(cmd,SR_qual_startstop);
1752 set_SR_xfer_length(cmd,SR_len_startstop);
1753
1754 memset(pay,0,SR_len_startstop);
1755 set_SR_payload_len(pay,SR_len_startstop);
1756 set_SR_startstop_cmd(pay,1);
1757
1758 ret = do_cmd (
1759 s, 1, 0,
1760 cmd, SEND_len,
1761 pay, SR_len_startstop,
1762 NULL, NULL
1763 );
1764 if(ret){
1765 DBG (5, "sane_open: SS error %d\n",ret);
1766 return ret;
1767 }
1768
1769 DBG (15, "sane_start: sleeping\n");
1770 sleep(2);
1771
1772 s->started=1;
1773 }
1774
1775 ret = read_imageheader(s);
1776 if(ret){
1777 DBG (5, "sane_open: error reading imageheader %d\n",ret);
1778 return ret;
1779 }
1780
1781 /* set clean defaults */
1782 s->bytes_rx = 0;
1783 s->bytes_tx = 0;
1784
1785 /* make large buffer to hold the images */
1786 DBG (15, "sane_start: setup buffer\n");
1787
1788 /* free current buffer if too small */
1789 if (s->buffer && s->bytes_buf < s->i_bytes) {
1790 DBG (15, "sane_start: free buffer.\n");
1791 free(s->buffer);
1792 s->buffer = NULL;
1793 s->bytes_buf = 0;
1794 }
1795
1796 /* grab new buffer if don't have one */
1797 if (!s->buffer) {
1798 DBG (15, "sane_start: calloc buffer.\n");
1799 s->buffer = calloc (1,s->i_bytes);
1800 if (!s->buffer) {
1801 DBG (5, "sane_start: Error, no buffer\n");
1802 do_cancel(s);
1803 return SANE_STATUS_NO_MEM;
1804 }
1805 }
1806
1807 DBG (15, "started=%d, source=%d\n", s->started, s->u_source);
1808
1809 DBG (10, "sane_start: finish\n");
1810
1811 return SANE_STATUS_GOOD;
1812 }
1813
1814 /*
1815 * This routine issues a SCSI SET WINDOW command to the scanner, using the
1816 * values currently in the scanner data structure.
1817 * the scanner has 4 separate windows, and all must be set similarly,
1818 * even if you don't intend to acquire images from all of them.
1819 */
1820 static SANE_Status
set_window(struct scanner * s)1821 set_window (struct scanner *s)
1822 {
1823 SANE_Status ret = SANE_STATUS_GOOD;
1824
1825 unsigned char cmd[SET_WINDOW_len];
1826 size_t cmdLen = SET_WINDOW_len;
1827
1828 /* the data phase has a header, followed by a window desc block
1829 * the header specifies the number of bytes in 1 window desc block */
1830 unsigned char pay[WINDOW_HEADER_len + WINDOW_DESCRIPTOR_len];
1831 size_t payLen = WINDOW_HEADER_len + WINDOW_DESCRIPTOR_len;
1832
1833 unsigned char * desc = pay + WINDOW_HEADER_len;
1834
1835 int width = (s->u_br_x - s->u_tl_x) * s->u_res/1200;
1836 int length = (s->u_br_y - s->u_tl_y) * s->u_res/1200;
1837
1838 DBG (10, "set_window: start\n");
1839
1840 /* binary window settings */
1841 memset(cmd,0,cmdLen);
1842 set_SCSI_opcode(cmd,SET_WINDOW_code);
1843 set_SW_xferlen(cmd,payLen);
1844
1845 memset(pay,0,payLen);
1846 set_WH_desc_len(pay,WINDOW_DESCRIPTOR_len);
1847
1848 set_WD_wid(desc,WD_wid_front_binary);
1849
1850 /* common settings */
1851 set_WD_Xres (desc, s->u_res);
1852 set_WD_Yres (desc, s->u_res);
1853
1854 set_WD_ULX (desc, s->u_tl_x);
1855 set_WD_ULY (desc, s->u_tl_y);
1856
1857 /* width % 32 == 0 && length % 1 == 0 */
1858 width -= width % 32;
1859 width = width*1200/s->u_res;
1860
1861 length = length*1200/s->u_res;
1862
1863 set_WD_width (desc, width);
1864 set_WD_length (desc, length);
1865
1866 /* brightness not supported? */
1867 set_WD_brightness (desc, 0);
1868 set_WD_threshold (desc, s->u_threshold);
1869 set_WD_contrast (desc, 0);
1870 if(s->s_contrast_steps){
1871 /*convert our common -127 to +127 range into HW's range
1872 *FIXME: this code assumes hardware range of 1-255 */
1873 set_WD_contrast (desc, s->u_contrast+128);
1874 }
1875
1876 if(s->u_mode == MODE_HALFTONE){
1877 set_WD_composition (desc, WD_compo_HALFTONE);
1878 set_WD_bitsperpixel (desc, 1);
1879 }
1880 else{
1881 set_WD_composition (desc, WD_compo_LINEART);
1882 set_WD_bitsperpixel (desc, 1);
1883 }
1884
1885 /* FIXME ht pattern */
1886
1887 set_WD_rif (desc, s->u_rif);
1888
1889 set_WD_bitorder (desc, 1);
1890
1891 /* compression options */
1892 if(s->u_compr)
1893 set_WD_compress_type (desc, WD_compr_FAXG4);
1894
1895 /*FIXME: noise filter */
1896
1897 set_WD_allow_zero(desc,1);
1898 set_WD_cropping (desc, WD_crop_RELATIVE);
1899
1900 /*FIXME: more settings here*/
1901
1902 hexdump(15, "front binary window:", desc, WINDOW_DESCRIPTOR_len);
1903
1904 DBG (15, "set_window: set window binary back\n");
1905 ret = do_cmd (
1906 s, 1, 0,
1907 cmd, cmdLen,
1908 pay, payLen,
1909 NULL, NULL
1910 );
1911 if(ret){
1912 DBG (5, "set_window: error setting binary front window %d\n",ret);
1913 return ret;
1914 }
1915
1916 /*send the window for backside too*/
1917 set_WD_wid(desc,WD_wid_back_binary);
1918
1919 DBG (15, "set_window: set window binary back\n");
1920 ret = do_cmd (
1921 s, 1, 0,
1922 cmd, cmdLen,
1923 pay, payLen,
1924 NULL, NULL
1925 );
1926 if(ret){
1927 DBG (5, "set_window: error setting binary back window %d\n",ret);
1928 return ret;
1929 }
1930
1931 #if 0
1932 memset(cmd,0,cmdLen);
1933 set_SCSI_opcode(cmd,GET_WINDOW_code);
1934 set_GW_single(cmd,1);
1935 set_GW_wid(cmd,WD_wid_front_color);
1936 set_GW_xferlen(cmd,payLen);
1937
1938 ret = do_cmd (
1939 s, 1, 0,
1940 cmd, cmdLen,
1941 NULL, 0,
1942 pay, &payLen
1943 );
1944 if(ret){
1945 DBG (5, "set_window: error getting window %d\n",ret);
1946 return ret;
1947 }
1948 hexdump(15,"foo",pay,payLen);
1949 #endif
1950
1951 /* color window settings */
1952 memset(cmd,0,cmdLen);
1953 set_SCSI_opcode(cmd,SET_WINDOW_code);
1954 set_SW_xferlen(cmd,payLen);
1955
1956 memset(pay,0,payLen);
1957 set_WH_desc_len(pay,WINDOW_DESCRIPTOR_len);
1958
1959 set_WD_wid(desc,WD_wid_front_color);
1960
1961 /* common settings */
1962 set_WD_Xres (desc, s->u_res);
1963 set_WD_Yres (desc, s->u_res);
1964
1965 set_WD_ULX (desc, s->u_tl_x);
1966 set_WD_ULY (desc, s->u_tl_y);
1967
1968 set_WD_width (desc, width);
1969 set_WD_length (desc, length);
1970
1971 /*gray mode*/
1972 if(s->u_mode == MODE_GRAYSCALE){
1973 /*
1974 gamma
1975 width % 8 == 0 && length % 8 == 0
1976 */
1977 set_WD_composition (desc, WD_compo_MULTILEVEL);
1978 set_WD_bitsperpixel (desc, 8);
1979 }
1980 /*color mode or color window in binary mode*/
1981 else{
1982 /*
1983 width % 16 == 0 && length % 8 == 0
1984 */
1985 set_WD_composition (desc, WD_compo_MULTILEVEL);
1986 set_WD_bitsperpixel (desc, 24);
1987
1988 /* compression options */
1989 if(s->u_compr)
1990 set_WD_compress_type (desc, WD_compr_JPEG);
1991 }
1992
1993 set_WD_bitorder (desc, 1);
1994
1995 /*FIXME: noise filter */
1996
1997 set_WD_allow_zero(desc,1);
1998 set_WD_cropping (desc, WD_crop_RELATIVE);
1999
2000 /*FIXME: more settings here*/
2001
2002 DBG (15, "set_window: set window color front\n");
2003
2004 ret = do_cmd (
2005 s, 1, 0,
2006 cmd, cmdLen,
2007 pay, payLen,
2008 NULL, NULL
2009 );
2010 if(ret){
2011 DBG (5, "set_window: error setting color front window %d\n",ret);
2012 return ret;
2013 }
2014
2015 /*send the window for backside too*/
2016 set_WD_wid(desc,WD_wid_back_color);
2017
2018 DBG (15, "set_window: set window color back\n");
2019
2020 ret = do_cmd (
2021 s, 1, 0,
2022 cmd, cmdLen,
2023 pay, payLen,
2024 NULL, NULL
2025 );
2026 if(ret){
2027 DBG (5, "set_window: error setting color back window %d\n",ret);
2028 return ret;
2029 }
2030
2031 DBG (10, "set_window: finish\n");
2032
2033 return ret;
2034 }
2035
2036 /*
2037 * This routine reads the SC (scanner config) data from the scanner
2038 * modifies a few params based on user data, and sends it back
2039 */
2040 static SANE_Status
send_sc(struct scanner * s)2041 send_sc(struct scanner *s)
2042 {
2043 SANE_Status ret = SANE_STATUS_GOOD;
2044
2045 unsigned char cmd[READ_len];
2046 size_t cmdLen = READ_len;
2047 unsigned char pay[SR_len_config];
2048 size_t payLen = SR_len_config;
2049
2050 /* send SC command */
2051 DBG (10, "send_sc: start\n");
2052
2053 DBG (15, "send_sc: reading config\n");
2054 memset(cmd,0,READ_len);
2055 set_SCSI_opcode(cmd,READ_code);
2056
2057 set_SR_datatype_code(cmd,SR_datatype_random);
2058 set_SR_datatype_qual(cmd,SR_qual_config);
2059 set_SR_xfer_length(cmd,SR_len_config);
2060
2061 ret = do_cmd (
2062 s, 1, 0,
2063 cmd, cmdLen,
2064 NULL, 0,
2065 pay, &payLen
2066 );
2067 if(ret || !payLen){
2068 DBG (5, "send_sc: error reading: %d\n",ret);
2069 return ret;
2070 }
2071
2072 memset(cmd,0,SEND_len);
2073 set_SCSI_opcode(cmd,SEND_code);
2074
2075 set_SR_datatype_code(cmd,SR_datatype_random);
2076 set_SR_datatype_qual(cmd,SR_qual_config);
2077 set_SR_xfer_length(cmd,payLen);
2078
2079 if(s->u_source == SOURCE_ADF_FRONT){
2080 if(s->u_mode == MODE_COLOR || s->u_mode == MODE_GRAYSCALE){
2081 set_SR_sc_io1(pay,SR_sc_io_front_color);
2082 }
2083 else{
2084 set_SR_sc_io1(pay,SR_sc_io_front_binary);
2085 }
2086 set_SR_sc_io2(pay,SR_sc_io_none);
2087 set_SR_sc_io3(pay,SR_sc_io_none);
2088 set_SR_sc_io4(pay,SR_sc_io_none);
2089 }
2090 else if(s->u_source == SOURCE_ADF_BACK){
2091 if(s->u_mode == MODE_COLOR || s->u_mode == MODE_GRAYSCALE){
2092 set_SR_sc_io1(pay,SR_sc_io_rear_color);
2093 }
2094 else{
2095 set_SR_sc_io1(pay,SR_sc_io_rear_binary);
2096 }
2097 set_SR_sc_io2(pay,SR_sc_io_none);
2098 set_SR_sc_io3(pay,SR_sc_io_none);
2099 set_SR_sc_io4(pay,SR_sc_io_none);
2100 }
2101 else{
2102 if(s->u_mode == MODE_COLOR || s->u_mode == MODE_GRAYSCALE){
2103 set_SR_sc_io1(pay,SR_sc_io_front_color);
2104 set_SR_sc_io2(pay,SR_sc_io_rear_color);
2105 }
2106 else{
2107 set_SR_sc_io1(pay,SR_sc_io_front_binary);
2108 set_SR_sc_io2(pay,SR_sc_io_rear_binary);
2109 }
2110 set_SR_sc_io3(pay,SR_sc_io_none);
2111 set_SR_sc_io4(pay,SR_sc_io_none);
2112 }
2113
2114 /*FIXME: there are hundreds of other settings in this payload*/
2115
2116 ret = do_cmd (
2117 s, 1, 0,
2118 cmd, cmdLen,
2119 pay, payLen,
2120 NULL, NULL
2121 );
2122
2123 DBG (10, "send_sc: finish %d\n",ret);
2124
2125 return ret;
2126 }
2127
2128 /*
2129 * This routine reads the image header from the scanner, and updates
2130 * values currently in the scanner data structure.
2131 */
2132 static SANE_Status
read_imageheader(struct scanner * s)2133 read_imageheader (struct scanner *s)
2134 {
2135 SANE_Status ret = SANE_STATUS_GOOD;
2136
2137 unsigned char cmd[READ_len];
2138 unsigned char pay[SR_len_imageheader];
2139 size_t payLen = SR_len_imageheader;
2140 int pass = 0;
2141
2142 /* read img header */
2143 DBG (10, "read_imageheader: start\n");
2144
2145 memset(cmd,0,READ_len);
2146 set_SCSI_opcode(cmd,READ_code);
2147 set_SR_datatype_code(cmd,SR_datatype_imageheader);
2148 set_SR_xfer_length(cmd,SR_len_imageheader);
2149
2150 while (pass++ < 1000){
2151
2152 DBG (15, "read_imageheader: pass %d\n", pass);
2153
2154 payLen = SR_len_imageheader;
2155
2156 ret = do_cmd (
2157 s, 1, 0,
2158 cmd, READ_len,
2159 NULL, 0,
2160 pay, &payLen
2161 );
2162
2163 DBG (15, "read_imageheader: pass status %d\n", ret);
2164
2165 if(ret != SANE_STATUS_DEVICE_BUSY){
2166 break;
2167 }
2168
2169 usleep(50000);
2170 }
2171
2172 if (ret == SANE_STATUS_GOOD){
2173
2174 DBG (15, "image header:\n");
2175
2176 DBG (15, " bytes: %d\n",get_SR_ih_image_length(pay));
2177 s->i_bytes = get_SR_ih_image_length(pay);
2178
2179 DBG (15, " id: %d\n",get_SR_ih_image_id(pay));
2180 s->i_id = get_SR_ih_image_id(pay);
2181
2182 DBG (15, " dpi: %d\n",get_SR_ih_resolution(pay));
2183 s->i_dpi = get_SR_ih_resolution(pay);
2184
2185 DBG (15, " tlx: %d\n",get_SR_ih_ulx(pay));
2186 s->i_tlx = get_SR_ih_ulx(pay);
2187
2188 DBG (15, " tly: %d\n",get_SR_ih_uly(pay));
2189 s->i_tly = get_SR_ih_uly(pay);
2190
2191 DBG (15, " width: %d\n",get_SR_ih_width(pay));
2192 s->i_width = get_SR_ih_width(pay);
2193
2194 DBG (15, " length: %d\n",get_SR_ih_length(pay));
2195 s->i_length = get_SR_ih_length(pay);
2196
2197 DBG (15, " bpp: %d\n",get_SR_ih_bpp(pay));
2198 s->i_bpp = get_SR_ih_bpp(pay);
2199
2200 DBG (15, " comp: %d\n",get_SR_ih_comp_type(pay));
2201 s->i_compr = get_SR_ih_comp_type(pay);
2202
2203 /*FIXME: there are a lot more of these?*/
2204 }
2205
2206 DBG (10, "read_imageheader: finish %d\n", ret);
2207
2208 return ret;
2209 }
2210
2211 /*
2212 * Called by SANE to read data.
2213 *
2214 * From the SANE spec:
2215 * This function is used to read image data from the device
2216 * represented by handle h. Argument buf is a pointer to a memory
2217 * area that is at least maxlen bytes long. The number of bytes
2218 * returned is stored in *len. A backend must set this to zero when
2219 * the call fails (i.e., when a status other than SANE_STATUS_GOOD is
2220 * returned).
2221 *
2222 * When the call succeeds, the number of bytes returned can be
2223 * anywhere in the range from 0 to maxlen bytes.
2224 */
2225 SANE_Status
sane_read(SANE_Handle handle,SANE_Byte * buf,SANE_Int max_len,SANE_Int * len)2226 sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len)
2227 {
2228 struct scanner *s = (struct scanner *) handle;
2229 SANE_Status ret=0;
2230
2231 DBG (10, "sane_read: start\n");
2232
2233 *len=0;
2234
2235 /* maybe cancelled? */
2236 if(!s->started){
2237 DBG (5, "sane_read: not started, call sane_start\n");
2238 return SANE_STATUS_CANCELLED;
2239 }
2240
2241 /* sane_start required between images */
2242 if(s->bytes_tx == s->i_bytes){
2243 DBG (15, "sane_read: returning eof\n");
2244 return SANE_STATUS_EOF;
2245 }
2246
2247 if(s->i_bytes > s->bytes_rx ){
2248 ret = read_from_scanner(s);
2249 if(ret){
2250 DBG(5,"sane_read: returning %d\n",ret);
2251 return ret;
2252 }
2253 }
2254
2255 /* copy a block from buffer to frontend */
2256 ret = read_from_buffer(s,buf,max_len,len);
2257
2258 DBG (10, "sane_read: finish\n");
2259
2260 return ret;
2261 }
2262
2263 static SANE_Status
read_from_scanner(struct scanner * s)2264 read_from_scanner(struct scanner *s)
2265 {
2266 SANE_Status ret=SANE_STATUS_GOOD;
2267 int bytes = s->buffer_size;
2268 int remain = s->i_bytes - s->bytes_rx;
2269 unsigned char * buf;
2270 size_t inLen = 0;
2271
2272 unsigned char cmd[READ_len];
2273 int cmdLen=READ_len;
2274
2275 DBG (10, "read_from_scanner: start\n");
2276
2277 memset(cmd, 0, cmdLen);
2278 set_SCSI_opcode(cmd, READ_code);
2279
2280 /* figure out the max amount to transfer */
2281 if(bytes > remain){
2282 bytes = remain;
2283 }
2284
2285 DBG(15, "read_from_scanner: to:%d rx:%d re:%d bu:%d pa:%d\n",
2286 s->i_bytes, s->bytes_rx, remain, s->buffer_size, bytes);
2287
2288 if(ret){
2289 return ret;
2290 }
2291
2292 inLen = bytes;
2293
2294 buf = malloc(bytes);
2295 if(!buf){
2296 DBG(5, "read_from_scanner: not enough mem for buffer: %d\n",bytes);
2297 return SANE_STATUS_NO_MEM;
2298 }
2299
2300 set_SR_datatype_code (cmd, SR_datatype_imagedata);
2301 set_SR_xfer_length (cmd, bytes);
2302
2303 ret = do_cmd (
2304 s, 1, 0,
2305 cmd, cmdLen,
2306 NULL, 0,
2307 buf, &inLen
2308 );
2309
2310 if (ret == SANE_STATUS_GOOD) {
2311 DBG(15, "read_from_scanner: got GOOD, returning GOOD\n");
2312 }
2313 else if (ret == SANE_STATUS_EOF) {
2314 DBG(15, "read_from_scanner: got EOF, finishing\n");
2315 }
2316 else if (ret == SANE_STATUS_DEVICE_BUSY) {
2317 DBG(5, "read_from_scanner: got BUSY, returning GOOD\n");
2318 inLen = 0;
2319 ret = SANE_STATUS_GOOD;
2320 }
2321 else {
2322 DBG(5, "read_from_scanner: error reading data block status = %d\n",ret);
2323 inLen = 0;
2324 }
2325
2326 if(inLen){
2327 copy_buffer (s, buf, inLen);
2328 }
2329
2330 free(buf);
2331
2332 if(ret == SANE_STATUS_EOF){
2333 DBG (5, "read_from_scanner: unexpected EOF, shortening image\n");
2334 s->i_bytes = s->bytes_rx;
2335 ret = SANE_STATUS_GOOD;
2336 }
2337
2338 DBG (10, "read_from_scanner: finish\n");
2339
2340 return ret;
2341 }
2342
2343 static SANE_Status
copy_buffer(struct scanner * s,unsigned char * buf,int len)2344 copy_buffer(struct scanner *s, unsigned char * buf, int len)
2345 {
2346 SANE_Status ret=SANE_STATUS_GOOD;
2347
2348 DBG (10, "copy_buffer: start\n");
2349
2350 memcpy(s->buffer+s->bytes_rx,buf,len);
2351 s->bytes_rx += len;
2352
2353 DBG (10, "copy_buffer: finish\n");
2354
2355 return ret;
2356 }
2357
2358 static SANE_Status
read_from_buffer(struct scanner * s,SANE_Byte * buf,SANE_Int max_len,SANE_Int * len)2359 read_from_buffer(struct scanner *s, SANE_Byte * buf,
2360 SANE_Int max_len, SANE_Int * len)
2361 {
2362 SANE_Status ret=SANE_STATUS_GOOD;
2363 int bytes = max_len;
2364 int remain = s->bytes_rx - s->bytes_tx;
2365
2366 DBG (10, "read_from_buffer: start\n");
2367
2368 /* figure out the max amount to transfer */
2369 if(bytes > remain){
2370 bytes = remain;
2371 }
2372
2373 *len = bytes;
2374
2375 DBG(15, "read_from_buffer: to:%d tx:%d re:%d bu:%d pa:%d\n",
2376 s->i_bytes, s->bytes_tx, remain, max_len, bytes);
2377
2378 /*FIXME this needs to timeout eventually */
2379 if(!bytes){
2380 DBG(5,"read_from_buffer: nothing to do\n");
2381 return SANE_STATUS_GOOD;
2382 }
2383
2384 memcpy(buf,s->buffer+s->bytes_tx,bytes);
2385
2386 s->bytes_tx += *len;
2387
2388 DBG (10, "read_from_buffer: finish\n");
2389
2390 return ret;
2391 }
2392
2393
2394 /*
2395 * @@ Section 4 - SANE cleanup functions
2396 */
2397 /*
2398 * Cancels a scan.
2399 *
2400 * It has been said on the mailing list that sane_cancel is a bit of a
2401 * misnomer because it is routinely called to signal the end of a
2402 * batch - quoting David Mosberger-Tang:
2403 *
2404 * > In other words, the idea is to have sane_start() be called, and
2405 * > collect as many images as the frontend wants (which could in turn
2406 * > consist of multiple frames each as indicated by frame-type) and
2407 * > when the frontend is done, it should call sane_cancel().
2408 * > Sometimes it's better to think of sane_cancel() as "sane_stop()"
2409 * > but that name would have had some misleading connotations as
2410 * > well, that's why we stuck with "cancel".
2411 *
2412 * The current consensus regarding duplex and ADF scans seems to be
2413 * the following call sequence: sane_start; sane_read (repeat until
2414 * EOF); sane_start; sane_read... and then call sane_cancel if the
2415 * batch is at an end. I.e. do not call sane_cancel during the run but
2416 * as soon as you get a SANE_STATUS_NO_DOCS.
2417 *
2418 * From the SANE spec:
2419 * This function is used to immediately or as quickly as possible
2420 * cancel the currently pending operation of the device represented by
2421 * handle h. This function can be called at any time (as long as
2422 * handle h is a valid handle) but usually affects long-running
2423 * operations only (such as image is acquisition). It is safe to call
2424 * this function asynchronously (e.g., from within a signal handler).
2425 * It is important to note that completion of this operation does not
2426 * imply that the currently pending operation has been cancelled. It
2427 * only guarantees that cancellation has been initiated. Cancellation
2428 * completes only when the cancelled call returns (typically with a
2429 * status value of SANE_STATUS_CANCELLED). Since the SANE API does
2430 * not require any other operations to be re-entrant, this implies
2431 * that a frontend must not call any other operation until the
2432 * cancelled operation has returned.
2433 */
2434 void
sane_cancel(SANE_Handle handle)2435 sane_cancel (SANE_Handle handle)
2436 {
2437 DBG (10, "sane_cancel: start\n");
2438 do_cancel ((struct scanner *) handle);
2439 DBG (10, "sane_cancel: finish\n");
2440 }
2441
2442 /*
2443 * Performs cleanup.
2444 * FIXME: do better cleanup if scanning is ongoing...
2445 */
2446 static SANE_Status
do_cancel(struct scanner * s)2447 do_cancel (struct scanner *s)
2448 {
2449 DBG (10, "do_cancel: start\n");
2450
2451 s->started = 0;
2452
2453 DBG (10, "do_cancel: finish\n");
2454
2455 return SANE_STATUS_CANCELLED;
2456 }
2457
2458 /*
2459 * Ends use of the scanner.
2460 *
2461 * From the SANE spec:
2462 * This function terminates the association between the device handle
2463 * passed in argument h and the device it represents. If the device is
2464 * presently active, a call to sane_cancel() is performed first. After
2465 * this function returns, handle h must not be used anymore.
2466 */
2467 void
sane_close(SANE_Handle handle)2468 sane_close (SANE_Handle handle)
2469 {
2470 DBG (10, "sane_close: start\n");
2471
2472 do_cancel((struct scanner *) handle);
2473 disconnect_fd((struct scanner *) handle);
2474
2475 DBG (10, "sane_close: finish\n");
2476 }
2477
2478 static SANE_Status
disconnect_fd(struct scanner * s)2479 disconnect_fd (struct scanner *s)
2480 {
2481 DBG (10, "disconnect_fd: start\n");
2482
2483 if(s->fd > -1){
2484 DBG (15, "disconnecting scsi device\n");
2485 sanei_scsi_close (s->fd);
2486 s->fd = -1;
2487 }
2488
2489 DBG (10, "disconnect_fd: finish\n");
2490
2491 return SANE_STATUS_GOOD;
2492 }
2493
2494 /*
2495 * Terminates the backend.
2496 *
2497 * From the SANE spec:
2498 * This function must be called to terminate use of a backend. The
2499 * function will first close all device handles that still might be
2500 * open (it is recommended to close device handles explicitly through
2501 * a call to sane_close(), but backends are required to release all
2502 * resources upon a call to this function). After this function
2503 * returns, no function other than sane_init() may be called
2504 * (regardless of the status value returned by sane_exit(). Neglecting
2505 * to call this function may result in some resources not being
2506 * released properly.
2507 */
2508 void
sane_exit(void)2509 sane_exit (void)
2510 {
2511 struct scanner *dev, *next;
2512
2513 DBG (10, "sane_exit: start\n");
2514
2515 for (dev = scanner_devList; dev; dev = next) {
2516 disconnect_fd(dev);
2517 next = dev->next;
2518 free (dev->device_name);
2519 free (dev);
2520 }
2521
2522 if (sane_devArray)
2523 free (sane_devArray);
2524
2525 scanner_devList = NULL;
2526 sane_devArray = NULL;
2527
2528 DBG (10, "sane_exit: finish\n");
2529 }
2530
2531
2532 /*
2533 * @@ Section 5 - misc helper functions
2534 */
2535 /*
2536 * Called by the SANE SCSI core on device errors
2537 * parses the request sense return data buffer,
2538 * decides the best SANE_Status for the problem
2539 * and produces debug msgs
2540 */
2541 static SANE_Status
sense_handler(int fd,unsigned char * sensed_data,void * arg)2542 sense_handler (int fd, unsigned char * sensed_data, void *arg)
2543 {
2544 struct scanner *s = arg;
2545 unsigned int ili = get_RS_ILI (sensed_data);
2546 unsigned int sk = get_RS_sense_key (sensed_data);
2547 unsigned int asc = get_RS_ASC (sensed_data);
2548 unsigned int ascq = get_RS_ASCQ (sensed_data);
2549
2550 DBG (5, "sense_handler: start\n");
2551
2552 /* kill compiler warning */
2553 (void) fd;
2554
2555 /* save for later */
2556 s->rs_info = get_RS_information (sensed_data);
2557
2558 DBG (5, "SK=%#02x, ASC=%#02x, ASCQ=%#02x, ILI=%d, info=%#08lx\n",
2559 sk, asc, ascq, ili, (unsigned long)s->rs_info);
2560
2561 switch (sk) {
2562
2563 /* no sense */
2564 case 0x0:
2565 if (0x00 != asc) {
2566 DBG (5, "No sense: unknown asc\n");
2567 return SANE_STATUS_IO_ERROR;
2568 }
2569 if (0x00 != ascq) {
2570 DBG (5, "No sense: unknown ascq\n");
2571 return SANE_STATUS_IO_ERROR;
2572 }
2573 if (ili) {
2574 DBG (5, "No sense: ILI set\n");
2575 return SANE_STATUS_EOF;
2576 }
2577 DBG (5, "No sense: ready\n");
2578 return SANE_STATUS_GOOD;
2579
2580 /* not ready */
2581 case 0x2:
2582 if (0x80 != asc) {
2583 DBG (5, "Not ready: unknown asc\n");
2584 return SANE_STATUS_IO_ERROR;
2585 }
2586 if (0x00 != ascq) {
2587 DBG (5, "Not ready: unknown ascq\n");
2588 return SANE_STATUS_IO_ERROR;
2589 }
2590 DBG (5, "Not ready: end of job\n");
2591 return SANE_STATUS_NO_DOCS;
2592 break;
2593
2594 /* hardware error */
2595 case 0x4:
2596 if (0x3b != asc) {
2597 DBG (5, "Hardware error: unknown asc\n");
2598 return SANE_STATUS_IO_ERROR;
2599 }
2600 if (0x05 == ascq) {
2601 DBG (5, "Hardware error: paper jam\n");
2602 return SANE_STATUS_JAMMED;
2603 }
2604 if (0x80 == ascq) {
2605 DBG (5, "Hardware error: multi-feed\n");
2606 return SANE_STATUS_JAMMED;
2607 }
2608 DBG (5, "Hardware error: unknown ascq\n");
2609 return SANE_STATUS_IO_ERROR;
2610 break;
2611
2612 /* illegal request */
2613 case 0x5:
2614 if (asc != 0x20 && asc != 0x24 && asc != 0x25 && asc != 0x26
2615 && asc != 0x83 && asc != 0x8f) {
2616 DBG (5, "Illegal request: unknown asc\n");
2617 return SANE_STATUS_IO_ERROR;
2618 }
2619 if (0x20 == asc && 0x00 == ascq) {
2620 DBG (5, "Illegal request: invalid opcode\n");
2621 return SANE_STATUS_INVAL;
2622 }
2623 if (0x24 == asc && 0x00 == ascq) {
2624 DBG (5, "Illegal request: invalid field in CDB\n");
2625 return SANE_STATUS_INVAL;
2626 }
2627 if (0x25 == asc && 0x00 == ascq) {
2628 DBG (5, "Illegal request: invalid LUN\n");
2629 return SANE_STATUS_INVAL;
2630 }
2631 if (0x26 == asc && 0x00 == ascq) {
2632 DBG (5, "Illegal request: invalid field in params\n");
2633 return SANE_STATUS_INVAL;
2634 }
2635 if (0x83 == asc && 0x00 == ascq) {
2636 DBG (5, "Illegal request: command failed, check log\n");
2637 return SANE_STATUS_INVAL;
2638 }
2639 if (0x83 == asc && 0x01 == ascq) {
2640 DBG (5, "Illegal request: command failed, invalid state\n");
2641 return SANE_STATUS_INVAL;
2642 }
2643 if (0x83 == asc && 0x02 == ascq) {
2644 DBG (5, "Illegal request: command failed, critical error\n");
2645 return SANE_STATUS_INVAL;
2646 }
2647 if (0x8f == asc && 0x00 == ascq) {
2648 DBG (5, "Illegal request: no image\n");
2649 return SANE_STATUS_DEVICE_BUSY;
2650 }
2651 DBG (5, "Illegal request: unknown asc/ascq\n");
2652 return SANE_STATUS_IO_ERROR;
2653 break;
2654
2655 /* unit attention */
2656 case 0x6:
2657 if (asc != 0x29 && asc != 0x80) {
2658 DBG (5, "Unit attention: unknown asc\n");
2659 return SANE_STATUS_IO_ERROR;
2660 }
2661 if (0x29 == asc && 0x60 == ascq) {
2662 DBG (5, "Unit attention: device reset\n");
2663 return SANE_STATUS_GOOD;
2664 }
2665 if (0x80 == asc && 0x00 == ascq) {
2666 DBG (5, "Unit attention: Energy Star warm up\n");
2667 return SANE_STATUS_DEVICE_BUSY;
2668 }
2669 if (0x80 == asc && 0x01 == ascq) {
2670 DBG (5, "Unit attention: lamp warm up for scan\n");
2671 return SANE_STATUS_DEVICE_BUSY;
2672 }
2673 if (0x80 == asc && 0x02 == ascq) {
2674 DBG (5, "Unit attention: lamp warm up for cal\n");
2675 return SANE_STATUS_DEVICE_BUSY;
2676 }
2677 if (0x80 == asc && 0x04 == ascq) {
2678 DBG (5, "Unit attention: calibration failed\n");
2679 return SANE_STATUS_INVAL;
2680 }
2681 DBG (5, "Unit attention: unknown asc/ascq\n");
2682 return SANE_STATUS_IO_ERROR;
2683 break;
2684
2685 /* ia overflow */
2686 case 0x9:
2687 if (0x80 == asc && 0x00 == ascq) {
2688 DBG (5, "IA overflow: IA field overflow\n");
2689 return SANE_STATUS_IO_ERROR;
2690 }
2691 DBG (5, "IA overflow: unknown asc/ascq\n");
2692 return SANE_STATUS_IO_ERROR;
2693 break;
2694
2695 /* volume overflow */
2696 case 0xd:
2697 if (0x80 == asc && 0x00 == ascq) {
2698 DBG (5, "Volume overflow: Image buffer full\n");
2699 return SANE_STATUS_IO_ERROR;
2700 }
2701 DBG (5, "Volume overflow: unknown asc/ascq\n");
2702 return SANE_STATUS_IO_ERROR;
2703 break;
2704
2705 default:
2706 DBG (5, "Unknown Sense Code\n");
2707 return SANE_STATUS_IO_ERROR;
2708 }
2709
2710 DBG (5, "sense_handler: should never happen!\n");
2711
2712 return SANE_STATUS_IO_ERROR;
2713 }
2714
2715 /*
2716 SANE_Status
2717 do_rs(scanner * s)
2718 {
2719 SANE_Status ret;
2720 unsigned char cmd[REQUEST_SENSE_len];
2721 size_t cmdLen = REQUEST_SENSE_len;
2722
2723 DBG (10, "do_rs: start\n");
2724
2725 memset(cmd,0,cmdLen);
2726 set_SCSI_opcode(cmd,REQUEST_SENSE_code);
2727 set_SR_datatype_code(cmd,SR_datatype_random);
2728 set_SR_datatype_qual(cmd,SR_qual_end);
2729
2730 ret = do_cmd (
2731 s, 1, 0,
2732 cmd, cmdLen,
2733 NULL, 0,
2734 NULL, NULL
2735 );
2736
2737 while(ret == SANE_STATUS_DEVICE_BUSY){
2738 ret = run_rs(s);
2739 }
2740
2741 DBG (10, "do_rs: finish\n");
2742
2743 return SANE_STATUS_GOOD;
2744 }
2745 */
2746
2747 SANE_Status
do_cmd(struct scanner * s,int runRS,int shortTime,unsigned char * cmdBuff,size_t cmdLen,unsigned char * outBuff,size_t outLen,unsigned char * inBuff,size_t * inLen)2748 do_cmd(struct scanner *s, int runRS, int shortTime,
2749 unsigned char * cmdBuff, size_t cmdLen,
2750 unsigned char * outBuff, size_t outLen,
2751 unsigned char * inBuff, size_t * inLen
2752 )
2753 {
2754 SANE_Status ret = SANE_STATUS_GOOD;
2755
2756 /*shut up compiler*/
2757 (void) runRS;
2758 (void) shortTime;
2759
2760 DBG(10, "do_cmd: start\n");
2761
2762 DBG(25, "cmd: writing %d bytes\n", (int)cmdLen);
2763 hexdump(30, "cmd: >>", cmdBuff, cmdLen);
2764
2765 if(outBuff && outLen){
2766 DBG(25, "out: writing %d bytes\n", (int)outLen);
2767 hexdump(30, "out: >>", outBuff, outLen);
2768 }
2769 if (inBuff && inLen){
2770 DBG(25, "in: reading %d bytes\n", (int)*inLen);
2771 }
2772
2773 ret = sanei_scsi_cmd2(s->fd, cmdBuff, cmdLen, outBuff, outLen, inBuff, inLen);
2774
2775 if(ret != SANE_STATUS_GOOD && ret != SANE_STATUS_EOF){
2776 DBG(5,"do_cmd: return '%s'\n",sane_strstatus(ret));
2777 return ret;
2778 }
2779
2780 /* FIXME: should we look at s->rs_info here? */
2781 if (inBuff && inLen){
2782 hexdump(30, "in: <<", inBuff, *inLen);
2783 DBG(25, "in: read %d bytes\n", (int)*inLen);
2784 }
2785
2786 DBG(10, "do_cmd: finish\n");
2787
2788 return ret;
2789 }
2790
2791 #if 0 /* unused */
2792 static SANE_Status
2793 wait_scanner(struct scanner *s)
2794 {
2795 int ret;
2796
2797 unsigned char cmd[TEST_UNIT_READY_len];
2798 size_t cmdLen = TEST_UNIT_READY_len;
2799
2800 DBG (10, "wait_scanner: start\n");
2801
2802 memset(cmd,0,cmdLen);
2803 set_SCSI_opcode(cmd,TEST_UNIT_READY_code);
2804
2805 ret = do_cmd (
2806 s, 0, 1,
2807 cmd, cmdLen,
2808 NULL, 0,
2809 NULL, NULL
2810 );
2811
2812 if (ret != SANE_STATUS_GOOD) {
2813 DBG(5,"WARNING: Brain-dead scanner. Hitting with stick\n");
2814 ret = do_cmd (
2815 s, 0, 1,
2816 cmd, cmdLen,
2817 NULL, 0,
2818 NULL, NULL
2819 );
2820 }
2821 if (ret != SANE_STATUS_GOOD) {
2822 DBG(5,"WARNING: Brain-dead scanner. Hitting with stick again\n");
2823 ret = do_cmd (
2824 s, 0, 1,
2825 cmd, cmdLen,
2826 NULL, 0,
2827 NULL, NULL
2828 );
2829 }
2830
2831 if (ret != SANE_STATUS_GOOD) {
2832 DBG (5, "wait_scanner: error '%s'\n", sane_strstatus (ret));
2833 }
2834
2835 DBG (10, "wait_scanner: finish\n");
2836
2837 return ret;
2838 }
2839 #endif /* 0 - unused */
2840
2841 /**
2842 * Convenience method to determine longest string size in a list.
2843 */
2844 static size_t
maxStringSize(const SANE_String_Const strings[])2845 maxStringSize (const SANE_String_Const strings[])
2846 {
2847 size_t size, max_size = 0;
2848 int i;
2849
2850 for (i = 0; strings[i]; ++i) {
2851 size = strlen (strings[i]) + 1;
2852 if (size > max_size)
2853 max_size = size;
2854 }
2855
2856 return max_size;
2857 }
2858
2859 /**
2860 * Prints a hex dump of the given buffer onto the debug output stream.
2861 */
2862 static void
hexdump(int level,char * comment,unsigned char * p,int l)2863 hexdump (int level, char *comment, unsigned char *p, int l)
2864 {
2865 int i;
2866 char line[128];
2867 char *ptr;
2868
2869 if(DBG_LEVEL < level)
2870 return;
2871
2872 DBG (level, "%s\n", comment);
2873 ptr = line;
2874 for (i = 0; i < l; i++, p++)
2875 {
2876 if ((i % 16) == 0)
2877 {
2878 if (ptr != line)
2879 {
2880 *ptr = '\0';
2881 DBG (level, "%s\n", line);
2882 ptr = line;
2883 }
2884 sprintf (ptr, "%3.3x:", i);
2885 ptr += 4;
2886 }
2887 sprintf (ptr, " %2.2x", *p);
2888 ptr += 3;
2889 }
2890 *ptr = '\0';
2891 DBG (level, "%s\n", line);
2892 }
2893
2894 /**
2895 * An advanced method we don't support but have to define.
2896 */
2897 SANE_Status
sane_set_io_mode(SANE_Handle h,SANE_Bool non_blocking)2898 sane_set_io_mode (SANE_Handle h, SANE_Bool non_blocking)
2899 {
2900 DBG (10, "sane_set_io_mode\n");
2901 DBG (15, "%d %p\n", non_blocking, h);
2902 return SANE_STATUS_UNSUPPORTED;
2903 }
2904
2905 /**
2906 * An advanced method we don't support but have to define.
2907 */
2908 SANE_Status
sane_get_select_fd(SANE_Handle h,SANE_Int * fdp)2909 sane_get_select_fd (SANE_Handle h, SANE_Int *fdp)
2910 {
2911 DBG (10, "sane_get_select_fd\n");
2912 DBG (15, "%p %d\n", h, *fdp);
2913 return SANE_STATUS_UNSUPPORTED;
2914 }
2915