1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 1997-2005, 2013 Franck Schnefra, Michel Roelofs,
4 Emmanuel Blot, Mikko Tyolajarvi, David Mosberger-Tang, Wolfgang Goeller,
5 Simon Munton, Petter Reinholdtsen, Gary Plewa, Sebastien Sable,
6 Mikael Magnusson, Max Ushakov, Andrew Goodbody, Oliver Schwartz
7 and Kevin Charter
8
9 This file is part of the SANE package.
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <https://www.gnu.org/licenses/>.
23
24 As a special exception, the authors of SANE give permission for
25 additional uses of the libraries contained in this release of SANE.
26
27 The exception is that, if you link a SANE library with other files
28 to produce an executable, this does not by itself cause the
29 resulting executable to be covered by the GNU General Public
30 License. Your use of that executable is in no way restricted on
31 account of linking the SANE library code into it.
32
33 This exception does not, however, invalidate any other reasons why
34 the executable file might be covered by the GNU General Public
35 License.
36
37 If you submit changes to SANE to the maintainers to be included in
38 a subsequent release, you agree by submitting the changes that
39 those changes may be distributed with this exception intact.
40
41 If you write modifications of your own for SANE, it is your choice
42 whether to permit this exception to apply to your modifications.
43 If you do not wish that, delete this exception notice.
44
45 This file is a component of the implementation of a backend for many
46 of the AGFA SnapScan and Acer Vuego/Prisa flatbed scanners.
47 */
48
49 /*
50 SANE SnapScan backend
51 */
52
53 #include "../include/sane/config.h"
54
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <limits.h>
58 #include <math.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 #include <sys/stat.h>
66 #include <sys/time.h>
67 #include <sys/types.h>
68 #include <sys/wait.h>
69
70 #include "../include/sane/sane.h"
71 #include "../include/sane/sanei.h"
72 #include "../include/sane/sanei_scsi.h"
73 #include "../include/sane/sanei_usb.h"
74 #include "../include/sane/sanei_thread.h"
75
76 #ifndef PATH_MAX
77 #define PATH_MAX 1024
78 #endif
79
80 #define MINOR_VERSION 4
81 #define BUILD 53
82 #define BACKEND_NAME snapscan
83
84 #include "../include/sane/sanei_backend.h"
85 #include "../include/sane/saneopts.h"
86
87 #include "snapscan.h"
88
89 #define MIN(x,y) ((x)<(y) ? (x) : (y))
90 #define MAX(x,y) ((x)>(y) ? (x) : (y))
91 #define LIMIT(x,min,max) MIN(MAX(x, min), max)
92
93 #ifdef INOPERATIVE
94 #define P_200_TO_255(per) SANE_UNFIX(255.0*((per + 100)/200.0))
95 #endif
96
97 #include "../include/sane/sanei_config.h"
98
99 /* debug levels */
100 #define DL_INFO 10
101 #define DL_MINOR_INFO 15
102 #define DL_MAJOR_ERROR 1
103 #define DL_MINOR_ERROR 2
104 #define DL_DATA_TRACE 50
105 #define DL_OPTION_TRACE 70
106 #define DL_CALL_TRACE 30
107 #define DL_VERBOSE 20
108
109 #define CHECK_STATUS(s,caller,cmd) \
110 if ((s) != SANE_STATUS_GOOD) { DBG(DL_MAJOR_ERROR, "%s: %s command failed: %s\n", caller, (cmd), sane_strstatus(s)); return s; }
111
112 /*----- internal scanner operations -----*/
113
114 /* hardware configuration byte masks */
115
116 #define HCFG_ADC 0x80 /* AD converter 1 ==> 10bit, 0 ==> 8bit */
117 #define HCFG_ADF 0x40 /* automatic document feeder */
118 #define HCFG_TPO 0x20 /* transparency option */
119 #define HCFG_RB 0x10 /* ring buffer */
120 #define HCFG_HT16 0x08 /* 16x16 halftone matrices */
121 #define HCFG_HT8 0x04 /* 8x8 halftone matrices */
122 #define HCFG_SRA 0x02 /* scanline row average (high-speed colour) */
123 #define HCFG_CAL_ALLOWED 0x01 /* 1 ==> calibration allowed */
124
125 #define HCFG_HT 0x0C /* support halftone matrices at all */
126
127 #define MM_PER_IN 25.4 /* # millimetres per inch */
128 #define IN_PER_MM 0.03937 /* # inches per millimetre */
129
130 #define GAMMA_8BIT 0
131 #define GAMMA_16BIT 1
132 #define GAMMA_12_16BIT 2
133
134 #ifndef SANE_I18N
135 #define SANE_I18N(text) text
136 #endif
137
138 /* authorization stuff */
139 static SANE_Auth_Callback auth = NULL;
140 #if UNUSED
141 static SANE_Char username[SANE_MAX_USERNAME_LEN];
142 static SANE_Char password[SANE_MAX_PASSWORD_LEN];
143 #endif
144
145 /* function prototypes */
146
147 static void gamma_n (double gamma, int brightness, int contrast,
148 u_char *buf, int length, int gamma_mode);
149 static void gamma_to_sane (int length, u_char *in, SANE_Int *out);
150
151 static size_t max_string_size(SANE_String_Const strings[]);
152
153 /* inline functions */
actual_mode(SnapScan_Scanner *pss)154 static inline SnapScan_Mode actual_mode (SnapScan_Scanner *pss)
155 {
156 if (pss->preview == SANE_TRUE)
157 return pss->preview_mode;
158 return pss->mode;
159 }
160
is_colour_mode(SnapScan_Mode m)161 static inline int is_colour_mode (SnapScan_Mode m)
162 {
163 return (m == MD_COLOUR) || (m == MD_BILEVELCOLOUR);
164 }
165
calibration_line_length(SnapScan_Scanner *pss)166 static inline int calibration_line_length(SnapScan_Scanner *pss)
167 {
168 int pos_factor;
169 int pixel_length;
170
171 switch (pss->pdev->model)
172 {
173 case STYLUS_CX1500:
174 case PRISA5000E:
175 case PRISA5000:
176 case PRISA5150:
177 case PERFECTION1270:
178 case PERFECTION1670:
179 case PERFECTION2480:
180 case PERFECTION3490:
181 pos_factor = pss->actual_res / 2;
182 pixel_length = pos_factor * 8.5;
183 break;
184 case SCANWIT2720S:
185 pixel_length = 2550;
186 break;
187 default:
188 pos_factor = pss->actual_res;
189 pixel_length = pos_factor * 8.5;
190 break;
191 }
192
193 if(is_colour_mode(actual_mode(pss))) {
194 return 3 * pixel_length;
195 } else {
196 return pixel_length;
197 }
198 }
199
200 /*----- global data structures and access utilities -----*/
201
202 /* available device list */
203
204 static SnapScan_Device *first_device = NULL; /* device list head */
205 static SANE_Int n_devices = 0; /* the device count */
206 static SANE_Char *default_firmware_filename;
207 static SANE_Bool cancelRead;
208
209 /* list returned from sane_get_devices() */
210 static const SANE_Device **get_devices_list = NULL;
211
212 /* external routines */
213 #include "snapscan-scsi.c"
214 #include "snapscan-sources.c"
215 #include "snapscan-usb.c"
216 #include "snapscan-options.c"
217
218 /* Initialize gamma tables */
alloc_gamma_tables(SnapScan_Scanner * ps)219 static SANE_Status alloc_gamma_tables(SnapScan_Scanner * ps)
220 {
221 static const char me[] = "alloc_gamma_tables";
222
223 ps->gamma_length = 1 << ps->bpp;
224 DBG (DL_MINOR_INFO, "%s: using 4*%d bytes for gamma table\n",
225 me,
226 ps->gamma_length);
227
228 ps->gamma_tables =
229 (SANE_Int *) malloc(4 * ps->gamma_length * sizeof(SANE_Int));
230
231 if (!ps->gamma_tables)
232 {
233 return SANE_STATUS_NO_MEM;
234 }
235
236 ps->gamma_table_gs = &ps->gamma_tables[0 * ps->gamma_length];
237 ps->gamma_table_r = &ps->gamma_tables[1 * ps->gamma_length];
238 ps->gamma_table_g = &ps->gamma_tables[2 * ps->gamma_length];
239 ps->gamma_table_b = &ps->gamma_tables[3 * ps->gamma_length];
240
241 return SANE_STATUS_GOOD;
242 }
243
init_gamma(SnapScan_Scanner * ps)244 static SANE_Status init_gamma(SnapScan_Scanner * ps)
245 {
246 u_char *gamma;
247
248 gamma = (u_char*) malloc(ps->gamma_length * sizeof(u_char) * 2);
249
250 if (!gamma)
251 {
252 return SANE_STATUS_NO_MEM;
253 }
254
255 /* Default tables */
256 gamma_n (SANE_UNFIX(ps->gamma_gs), ps->bright, ps->contrast, gamma, ps->bpp, 1);
257 gamma_to_sane (ps->gamma_length, gamma, ps->gamma_table_gs);
258
259 gamma_n (SANE_UNFIX(ps->gamma_r), ps->bright, ps->contrast, gamma, ps->bpp, 1);
260 gamma_to_sane (ps->gamma_length, gamma, ps->gamma_table_r);
261
262 gamma_n (SANE_UNFIX(ps->gamma_g), ps->bright, ps->contrast, gamma, ps->bpp, 1);
263 gamma_to_sane (ps->gamma_length, gamma, ps->gamma_table_g);
264
265 gamma_n (SANE_UNFIX(ps->gamma_b), ps->bright, ps->contrast, gamma, ps->bpp, 1);
266 gamma_to_sane (ps->gamma_length, gamma, ps->gamma_table_b);
267
268 free (gamma);
269 return SANE_STATUS_GOOD;
270 }
271
272 /* Max string size */
273
max_string_size(SANE_String_Const strings[])274 static size_t max_string_size (SANE_String_Const strings[])
275 {
276 size_t size;
277 size_t max_size = 0;
278 int i;
279
280 for (i = 0; strings[i]; ++i)
281 {
282 size = strlen (strings[i]) + 1;
283 if (size > max_size)
284 max_size = size;
285 }
286 return max_size;
287 }
288
289 /* gamma table computation */
gamma_n(double gamma, int brightness, int contrast, u_char *buf, int bpp, int gamma_mode)290 static void gamma_n (double gamma, int brightness, int contrast,
291 u_char *buf, int bpp, int gamma_mode)
292 {
293 int i;
294 double i_gamma = 1.0/gamma;
295 int length = 1 << bpp;
296 int max = length - 1;
297 double mid = max / 2.0;
298
299 for (i = 0; i < length; i++)
300 {
301 int x;
302 double val = (i - mid) * (1.0 + contrast / 100.0)
303 + (1.0 + brightness / 100.0) * mid;
304 val = LIMIT(val, 0, max);
305 switch (gamma_mode)
306 {
307 case GAMMA_16BIT:
308 x = LIMIT(65535*pow ((double) val/max, i_gamma) + 0.5, 0, 65535);
309
310 buf[2*i] = (u_char) x;
311 buf[2*i + 1] = (u_char) (x >> 8);
312 break;
313 case GAMMA_12_16BIT:
314 buf[2*i] = (u_char) i;
315 buf[2*i + 1] = (u_char) (i >> 8);
316 break;
317 case GAMMA_8BIT:
318 buf[i] =
319 (u_char) LIMIT(255*pow ((double) val/max, i_gamma) + 0.5, 0, 255);
320 break;
321 default:
322 break;
323 }
324 }
325 }
326
gamma_from_sane(int length, SANE_Int *in, u_char *out, int gamma_mode)327 static void gamma_from_sane (int length, SANE_Int *in, u_char *out, int gamma_mode)
328 {
329 int i;
330 for (i = 0; i < length; i++)
331 if (gamma_mode != GAMMA_8BIT)
332 {
333 out[2*i] = (u_char) LIMIT(in[i], 0, 65535);
334 out[2*i + 1] = (u_char) (LIMIT(in[i], 0, 65535) >> 8);
335 }
336 else
337 out[i] = (u_char) LIMIT(in[i] / 256, 0, 255);
338 }
339
gamma_to_sane(int length, u_char *in, SANE_Int *out)340 static void gamma_to_sane (int length, u_char *in, SANE_Int *out)
341 {
342 int i;
343 for (i = 0; i < length; i++)
344 out[i] = in[2*i] + 256 * in[2*i + 1];
345 }
346
347 /* dispersed-dot dither matrices; this is discussed in Foley, Van Dam,
348 Feiner and Hughes: Computer Graphics: principles and practice,
349 2nd ed. (Addison-Wesley), pp 570-571.
350
351 The function mfDn computes the nth dispersed-dot dither matrix Dn
352 given D(n/2) and n; n is presumed to be a power of 2. D8 and D16
353 are the matrices of interest to us, since the SnapScan supports
354 only 8x8 and 16x16 dither matrices. */
355
356 static u_char D2[] ={0, 2, 3, 1};
357
358 static u_char D4[16], D8[64], D16[256];
359
mkDn(u_char *Dn, u_char *Dn_half, unsigned n)360 static void mkDn (u_char *Dn, u_char *Dn_half, unsigned n)
361 {
362 unsigned int x, y;
363 for (y = 0; y < n; y++) {
364 for (x = 0; x < n; x++) {
365 /* Dn(x,y) = D2(2*x/n, 2*y/n) +4*Dn_half(x%(n/2), y%(n/2)) */
366 Dn[y*n + x] = D2[((int)(2*y/n))*2 + (int)(2*x/n)]
367 + 4*Dn_half[(y%(n/2))*(n/2) + x%(n/2)];
368 }
369 }
370 }
371
device_already_in_list(SnapScan_Device *current, SANE_String_Const name)372 static SANE_Bool device_already_in_list (SnapScan_Device *current,
373 SANE_String_Const name)
374 {
375 for ( ; NULL != current; current = current->pnext)
376 {
377 if (0 == strcmp (name, current->dev.name))
378 return SANE_TRUE;
379 }
380 return SANE_FALSE;
381 }
382
get_driver_name(SnapScan_Model model_num)383 static SANE_Char* get_driver_name(SnapScan_Model model_num) {
384 SANE_Int i;
385 for (i=0; i<known_drivers; i++) {
386 if (drivers[i].id == model_num) break;
387 }
388 if (i == known_drivers) {
389 DBG(0, "Implementation error: Driver name not found\n");
390 return ("Unknown");
391 }
392 return (drivers[i].driver_name);
393 }
394
snapscani_check_device( int fd, SnapScan_Bus bus_type, char* vendor, char* model, SnapScan_Model* model_num )395 static SANE_Status snapscani_check_device(
396 int fd,
397 SnapScan_Bus bus_type,
398 char* vendor,
399 char* model,
400 SnapScan_Model* model_num
401 ) {
402 static const char me[] = "snapscani_check_device";
403 SANE_Status status = SANE_STATUS_GOOD;
404 int supported_vendor = 0;
405 int i;
406
407 DBG (DL_CALL_TRACE, "%s()\n", me);
408
409 /* check that the device is legitimate */
410 if ((status = mini_inquiry (bus_type, fd, vendor, model)) != SANE_STATUS_GOOD)
411 {
412 DBG (DL_MAJOR_ERROR,
413 "%s: mini_inquiry failed with %s.\n",
414 me,
415 sane_strstatus (status));
416 return status;
417 }
418
419 DBG (DL_VERBOSE,
420 "%s: Is vendor \"%s\" model \"%s\" a supported scanner?\n",
421 me,
422 vendor,
423 model);
424
425 /* check if this is one of our supported vendors */
426 for (i = 0; i < known_vendors; i++)
427 {
428 if (0 == strcasecmp (vendor, vendors[i]))
429 {
430 supported_vendor = 1;
431 break;
432 }
433 }
434 if (supported_vendor)
435 {
436 /* Known vendor. Check if it is one of our supported models */
437 *model_num = snapscani_get_model_id(model, fd, bus_type);
438 }
439 if (!supported_vendor || UNKNOWN == model_num)
440 {
441 DBG (DL_MINOR_ERROR,
442 "%s: \"%s %s\" is not one of %s\n",
443 me,
444 vendor,
445 model,
446 "AGFA SnapScan 300, 310, 600, 1212, 1236, e10, e20, e25, e26, "
447 "e40, e42, e50, e52 or e60\n"
448 "Acer 300, 310, 610, 610+, "
449 "620, 620+, 640, 1240, 3300, 4300 or 5300\n"
450 "Guillemot MaxiScan A4 Deluxe");
451 status = SANE_STATUS_INVAL;
452 } else {
453 DBG(DL_VERBOSE, "%s: Autodetected driver: %s\n", me, get_driver_name(*model_num));
454 }
455 return status;
456 }
457
snapscani_init_device_structure( SnapScan_Device **pd, const SnapScan_Bus bus_type, SANE_String_Const name, const char* vendor, const char* model, const SnapScan_Model model_num )458 static SANE_Status snapscani_init_device_structure(
459 SnapScan_Device **pd,
460 const SnapScan_Bus bus_type,
461 SANE_String_Const name,
462 const char* vendor,
463 const char* model,
464 const SnapScan_Model model_num
465 ) {
466 static const char me[] = "snapscani_init_device_structure";
467 SANE_Status status = SANE_STATUS_GOOD;;
468
469 DBG (DL_CALL_TRACE, "%s()\n", me);
470
471 (*pd) = (SnapScan_Device *) malloc (sizeof (SnapScan_Device));
472 if (!(*pd))
473 {
474 DBG (DL_MAJOR_ERROR, "%s: out of memory allocating device.", me);
475 return SANE_STATUS_NO_MEM;
476 }
477 (*pd)->dev.name = strdup (name);
478 if (strcmp(vendor, "Color") == 0) {
479 (*pd)->dev.vendor = strdup ("Acer");
480 } else {
481 (*pd)->dev.vendor = strdup (vendor);
482 }
483 (*pd)->dev.model = strdup (model);
484 if (model_num == SCANWIT2720S)
485 {
486 (*pd)->dev.type = strdup (SNAPSCAN_FS_TYPE);
487 }
488 else
489 {
490 (*pd)->dev.type = strdup (SNAPSCAN_TYPE);
491 }
492 (*pd)->bus = bus_type;
493 (*pd)->model = model_num;
494
495 if (!(*pd)->dev.name || !(*pd)->dev.vendor || !(*pd)->dev.model || !(*pd)->dev.type)
496 {
497 DBG (DL_MAJOR_ERROR,
498 "%s: out of memory allocating device descriptor strings.\n",
499 me);
500 free (*pd);
501 return SANE_STATUS_NO_MEM;
502 }
503 (*pd)->x_range.min = x_range_fb.min;
504 (*pd)->x_range.quant = x_range_fb.quant;
505 (*pd)->x_range.max = x_range_fb.max;
506 (*pd)->y_range.min = y_range_fb.min;
507 (*pd)->y_range.quant = y_range_fb.quant;
508 (*pd)->y_range.max = y_range_fb.max;
509 (*pd)->firmware_filename = NULL;
510
511 (*pd)->pnext = first_device;
512 first_device = (*pd);
513 n_devices++;
514 return status;
515 }
516
add_scsi_device(SANE_String_Const full_name)517 static SANE_Status add_scsi_device (SANE_String_Const full_name)
518 {
519 int fd;
520 static const char me[] = "add_scsi_device";
521 SANE_Status status = SANE_STATUS_GOOD;
522 SnapScan_Device *pd;
523 SnapScan_Model model_num = UNKNOWN;
524 SnapScan_Bus bus_type = SCSI;
525 char vendor[8];
526 char model[17];
527 SANE_Char *name = NULL;
528
529 DBG (DL_CALL_TRACE, "%s(%s)\n", me, full_name);
530
531 sanei_config_get_string(full_name, &name);
532 if (!name)
533 {
534 return SANE_STATUS_INVAL;
535 }
536 /* Avoid adding the same device more then once */
537 if (device_already_in_list (first_device, name)) {
538 free(name);
539 name = 0;
540 return SANE_STATUS_GOOD;
541 }
542
543 vendor[0] = model[0] = '\0';
544
545 DBG (DL_VERBOSE, "%s: Detected (kind of) a SCSI device\n", me);
546
547 status = sanei_scsi_open (name, &fd, sense_handler, NULL);
548 if (status != SANE_STATUS_GOOD)
549 {
550 DBG (DL_MAJOR_ERROR,
551 "%s: error opening device %s: %s\n",
552 me,
553 name,
554 sane_strstatus (status));
555 } else {
556 status = snapscani_check_device(fd, bus_type, vendor, model, &model_num);
557 sanei_scsi_close(fd);
558 }
559 if (status == SANE_STATUS_GOOD) {
560 status = snapscani_init_device_structure(
561 &pd,
562 bus_type,
563 name,
564 vendor,
565 model,
566 model_num
567 );
568 }
569 free(name);
570 name = 0;
571 return status;
572 }
573
add_usb_device(SANE_String_Const full_name)574 static SANE_Status add_usb_device (SANE_String_Const full_name) {
575 static const char me[] = "add_usb_device";
576 int fd;
577 SnapScan_Device *pd;
578 SnapScan_Model model_num = UNKNOWN;
579 SANE_Word vendor_id, product_id;
580 int supported_usb_vendor = 0;
581 char vendor[8];
582 char model[17];
583 SANE_Status status = SANE_STATUS_GOOD;
584 SnapScan_Bus bus_type = USB;
585 int i;
586 SANE_Char *name = NULL;
587
588 DBG (DL_CALL_TRACE, "%s(%s)\n", me, full_name);
589 sanei_config_get_string(full_name, &name);
590 if (!name)
591 {
592 return SANE_STATUS_INVAL;
593 }
594 /* Avoid adding the same device more then once */
595 if (device_already_in_list (first_device, name)) {
596 free(name);
597 name = 0;
598 return SANE_STATUS_GOOD;
599 }
600
601 vendor[0] = model[0] = '\0';
602
603 DBG (DL_VERBOSE, "%s: Detected (kind of) an USB device\n", me);
604 bus_type = USB;
605 status = snapscani_usb_shm_init();
606 if (status != SANE_STATUS_GOOD)
607 {
608 return status;
609 }
610 status = snapscani_usb_open (name, &fd, sense_handler, NULL);
611 if (status != SANE_STATUS_GOOD)
612 {
613 DBG (DL_MAJOR_ERROR,
614 "%s: error opening device %s: %s\n",
615 me,
616 name,
617 sane_strstatus (status));
618 } else {
619 if (sanei_usb_get_vendor_product(fd, &vendor_id, &product_id) ==
620 SANE_STATUS_GOOD)
621 {
622 /* check for known USB vendors to avoid hanging scanners by
623 inquiry-command.
624 */
625 DBG(DL_INFO, "%s: Checking if 0x%04x is a supported USB vendor ID\n",
626 me, vendor_id);
627 for (i = 0; i < known_usb_vendor_ids; i++) {
628 if (vendor_id == usb_vendor_ids[i]) {
629 supported_usb_vendor = 1;
630 }
631 }
632 if (!supported_usb_vendor) {
633 DBG(DL_MINOR_ERROR,
634 "%s: USB vendor ID 0x%04x is currently NOT supported by the snapscan backend.\n",
635 me, vendor_id);
636 status=SANE_STATUS_INVAL;
637 snapscani_usb_close(fd);
638 }
639 }
640 }
641 if (status == SANE_STATUS_GOOD) {
642 status = snapscani_check_device(fd, bus_type, vendor, model, &model_num);
643 snapscani_usb_close(fd);
644 }
645 /* deinit shared memory, will be initialized again in open_scanner */
646 snapscani_usb_shm_exit();
647 if (status == SANE_STATUS_GOOD) {
648 status = snapscani_init_device_structure(
649 &pd,
650 bus_type,
651 name,
652 vendor,
653 model,
654 model_num
655 );
656 }
657 free(name);
658 name = 0;
659 return status;
660 }
661
662 /* find_device: find a device in the available list by name
663
664 ARG: the device name
665
666 RET: a pointer to the corresponding device record, or NULL if there
667 is no such device */
668
find_device(SANE_String_Const name)669 static SnapScan_Device *find_device (SANE_String_Const name)
670 {
671 static char me[] = "find_device";
672 SnapScan_Device *psd;
673
674 DBG (DL_CALL_TRACE, "%s\n", me);
675
676 for (psd = first_device; psd; psd = psd->pnext)
677 {
678 if (strcmp (psd->dev.name, name) == 0)
679 return psd;
680 }
681 return NULL;
682 }
683
684 /*----- functions in the scanner interface -----*/
685
sane_init(SANE_Int *version_code, SANE_Auth_Callback authorize)686 SANE_Status sane_init (SANE_Int *version_code,
687 SANE_Auth_Callback authorize)
688 {
689 static const char me[] = "sane_snapscan_init";
690 char dev_name[PATH_MAX];
691 size_t len;
692 FILE *fp;
693 SANE_Status status;
694
695 DBG_INIT ();
696
697 DBG (DL_CALL_TRACE, "%s\n", me);
698 DBG (DL_VERBOSE, "%s: Snapscan backend version %d.%d.%d\n",
699 me,
700 SANE_CURRENT_MAJOR, MINOR_VERSION, BUILD);
701
702 if (version_code != NULL)
703 {
704 *version_code =
705 SANE_VERSION_CODE (SANE_CURRENT_MAJOR, MINOR_VERSION, BUILD);
706 }
707
708 auth = authorize;
709 /* Initialize data structures */
710 default_firmware_filename = NULL;
711 first_device = NULL;
712 n_devices = 0;
713
714 sanei_usb_init();
715 sanei_thread_init();
716 /* build a device structure */
717 fp = sanei_config_open (SNAPSCAN_CONFIG_FILE);
718 if (!fp)
719 {
720 /* default to DEFAULT_DEVICE instead of insisting on config file */
721 DBG (DL_INFO,
722 "%s: configuration file not found, defaulting to %s.\n",
723 me,
724 DEFAULT_DEVICE);
725 status = add_scsi_device (DEFAULT_DEVICE);
726 if (status != SANE_STATUS_GOOD)
727 {
728 DBG (DL_MINOR_ERROR,
729 "%s: failed to add device \"%s\"\n",
730 me,
731 dev_name);
732 }
733 }
734 else
735 {
736 while (sanei_config_read (dev_name, sizeof (dev_name), fp))
737 {
738 len = strlen (dev_name);
739 if (!len)
740 continue; /* ignore empty lines */
741 if (dev_name[0] == '#') /* ignore line comments */
742 continue;
743 if (strncasecmp(dev_name, FIRMWARE_KW, strlen(FIRMWARE_KW)) == 0) {
744 if (!default_firmware_filename) {
745 sanei_config_get_string(dev_name + strlen(FIRMWARE_KW), &default_firmware_filename);
746 if (default_firmware_filename == NULL) {
747 DBG (0, "%s: Illegal firmware entry %s.\n", me, dev_name);
748 }
749 }
750 }
751 else if (strncasecmp(dev_name, OPTIONS_KW, strlen(OPTIONS_KW)) == 0)
752 continue; /* ignore options lines */
753
754 else if (strncmp(dev_name, "usb", 3) == 0) {
755 sanei_usb_attach_matching_devices (dev_name, add_usb_device);
756 }
757 else if (strncmp(dev_name, "scsi", 4) == 0) {
758 sanei_config_attach_matching_devices (dev_name, add_scsi_device);
759 }
760 else if (strstr (dev_name, "usb")) {
761 add_usb_device(dev_name);
762 }
763 else {
764 add_scsi_device(dev_name);
765 }
766 }
767 fclose (fp);
768 }
769
770 /* compute the dither matrices */
771
772 mkDn (D4, D2, 4);
773 mkDn (D8, D4, 8);
774 mkDn (D16, D8, 16);
775 /* scale the D8 matrix from 0..63 to 0..255 */
776 {
777 u_char i;
778 for (i = 0; i < 64; i++)
779 D8[i] = (u_char) (4 * D8[i] + 2);
780 }
781 return SANE_STATUS_GOOD;
782 }
783
free_device_list(SnapScan_Device *psd)784 static void free_device_list(SnapScan_Device *psd) {
785 if (psd->pnext != NULL) {
786 free_device_list(psd->pnext);
787 }
788 free(psd);
789 }
790
sane_exit(void)791 void sane_exit (void)
792 {
793 DBG (DL_CALL_TRACE, "sane_snapscan_exit\n");
794
795 if (get_devices_list)
796 free (get_devices_list);
797 get_devices_list = NULL;
798
799 /* just for safety, reset things to known values */
800 auth = NULL;
801
802 if (first_device) {
803 free_device_list(first_device);
804 first_device = NULL;
805 }
806 n_devices = 0;
807 }
808
809
sane_get_devices(const SANE_Device ***device_list, SANE_Bool local_only)810 SANE_Status sane_get_devices (const SANE_Device ***device_list,
811 SANE_Bool local_only)
812 {
813 static const char *me = "sane_snapscan_get_devices";
814 DBG (DL_CALL_TRACE,
815 "%s (%p, %ld)\n",
816 me,
817 (const void *) device_list,
818 (long) local_only);
819
820 /* Waste the last list returned from this function */
821 if (NULL != get_devices_list)
822 free (get_devices_list);
823
824 *device_list =
825 (const SANE_Device **) malloc ((n_devices + 1) * sizeof (SANE_Device *));
826
827 if (*device_list)
828 {
829 int i;
830 SnapScan_Device *pdev;
831 for (i = 0, pdev = first_device; pdev; i++, pdev = pdev->pnext)
832 (*device_list)[i] = &(pdev->dev);
833 (*device_list)[i] = 0x0000 /*NULL */;
834 }
835 else
836 {
837 DBG (DL_MAJOR_ERROR, "%s: out of memory\n", me);
838 return SANE_STATUS_NO_MEM;
839 }
840
841 get_devices_list = *device_list;
842
843 return SANE_STATUS_GOOD;
844 }
845
sane_open(SANE_String_Const name, SANE_Handle * h)846 SANE_Status sane_open (SANE_String_Const name, SANE_Handle * h)
847 {
848 static const char *me = "sane_snapscan_open";
849 SnapScan_Device *psd;
850 SANE_Status status;
851
852 DBG (DL_CALL_TRACE, "%s (%s, %p)\n", me, name, (void *) h);
853
854 /* possible authorization required */
855
856 /* no device name: use first device */
857 if ((strlen(name) == 0) && (first_device != NULL))
858 {
859 name = first_device->dev.name;
860 }
861
862 /* device exists? */
863 psd = find_device (name);
864 if (!psd)
865 {
866 DBG (DL_MINOR_ERROR,
867 "%s: device \"%s\" not in current device list.\n",
868 me,
869 name);
870 return SANE_STATUS_INVAL;
871 }
872
873 /* create and initialize the scanner structure */
874
875 *h = (SnapScan_Scanner *) calloc (sizeof (SnapScan_Scanner), 1);
876 if (!*h)
877 {
878 DBG (DL_MAJOR_ERROR,
879 "%s: out of memory creating scanner structure.\n",
880 me);
881 return SANE_STATUS_NO_MEM;
882 }
883
884 {
885 SnapScan_Scanner *pss = *(SnapScan_Scanner **) h;
886
887 {
888 pss->devname = strdup (name);
889 if (!pss->devname)
890 {
891 free (*h);
892 DBG (DL_MAJOR_ERROR,
893 "%s: out of memory copying device name.\n",
894 me);
895 return SANE_STATUS_NO_MEM;
896 }
897 pss->pdev = psd;
898 pss->opens = 0;
899 pss->sense_str = NULL;
900 pss->as_str = NULL;
901 pss->phys_buf_sz = DEFAULT_SCANNER_BUF_SZ;
902 if ((pss->pdev->model == PERFECTION2480) || (pss->pdev->model == PERFECTION3490))
903 pss->phys_buf_sz *= 2;
904 if (psd->bus == SCSI) {
905 pss->phys_buf_sz = sanei_scsi_max_request_size;
906 }
907 DBG (DL_DATA_TRACE,
908 "%s: Allocating %lu bytes as scanner buffer.\n",
909 me, (u_long) pss->phys_buf_sz);
910 pss->buf = (u_char *) malloc(pss->phys_buf_sz);
911 if (!pss->buf) {
912 DBG (DL_MAJOR_ERROR,
913 "%s: out of memory creating scanner buffer.\n",
914 me);
915 return SANE_STATUS_NO_MEM;
916 }
917
918 DBG (DL_VERBOSE,
919 "%s: allocated scanner structure at %p\n",
920 me,
921 (void *) pss);
922 }
923 status = snapscani_usb_shm_init();
924 if (status != SANE_STATUS_GOOD)
925 {
926 return status;
927 }
928 status = open_scanner (pss);
929 if (status != SANE_STATUS_GOOD)
930 {
931 DBG (DL_MAJOR_ERROR,
932 "%s: open_scanner failed, status: %s\n",
933 me,
934 sane_strstatus (status));
935 free (pss);
936 return SANE_STATUS_ACCESS_DENIED;
937 }
938
939 DBG (DL_MINOR_INFO, "%s: waiting for scanner to warm up.\n", me);
940 status = wait_scanner_ready (pss);
941 if (status != SANE_STATUS_GOOD)
942 {
943 DBG (DL_MAJOR_ERROR,
944 "%s: error waiting for scanner to warm up: %s\n",
945 me,
946 sane_strstatus(status));
947 free (pss);
948 return status;
949 }
950 DBG (DL_MINOR_INFO, "%s: performing scanner self test.\n", me);
951 status = send_diagnostic (pss);
952 if (status != SANE_STATUS_GOOD)
953 {
954 DBG (DL_MINOR_INFO, "%s: send_diagnostic reports %s\n",
955 me, sane_strstatus (status));
956 free (pss);
957 return status;
958 }
959 DBG (DL_MINOR_INFO, "%s: self test passed.\n", me);
960
961 /* option initialization depends on getting the hardware configuration
962 byte */
963 status = inquiry (pss);
964 if (status != SANE_STATUS_GOOD)
965 {
966 DBG (DL_MAJOR_ERROR,
967 "%s: error in inquiry command: %s\n",
968 me,
969 sane_strstatus (status));
970 free (pss);
971 return status;
972 }
973
974 if (pss->pdev->bus == USB)
975 {
976 if (sanei_usb_get_vendor_product(pss->fd, &pss->usb_vendor, &pss->usb_product) != SANE_STATUS_GOOD)
977 {
978 pss->usb_vendor = 0;
979 pss->usb_product = 0;
980 }
981 /* Download Firmware for USB scanners */
982 if (pss->hwst & 0x02)
983 {
984 char vendor[8];
985 char model[17];
986
987 status = download_firmware(pss);
988 CHECK_STATUS (status, me, "download_firmware");
989 /* send inquiry command again, wait for scanner to initialize */
990 status = wait_scanner_ready(pss);
991 CHECK_STATUS (status, me, "wait_scanner_ready after firmware upload");
992 status = mini_inquiry (pss->pdev->bus, pss->fd, vendor, model);
993 CHECK_STATUS (status, me, "mini_inquiry after firmware upload");
994 /* The model identifier may change after firmware upload */
995 DBG (DL_INFO,
996 "%s (after firmware upload): Checking if \"%s\" is a supported scanner\n",
997 me,
998 model);
999 /* Check if it is one of our supported models */
1000 pss->pdev->model = snapscani_get_model_id(model, pss->fd, pss->pdev->bus);
1001
1002 if (pss->pdev->model == UNKNOWN) {
1003 DBG (DL_MINOR_ERROR,
1004 "%s (after firmware upload): \"%s\" is not a supported scanner\n",
1005 me,
1006 model);
1007 }
1008 /* run "real" inquiry command once again for option initialization */
1009 status = inquiry (pss);
1010 CHECK_STATUS (status, me, "inquiry after firmware upload");
1011 }
1012 }
1013 close_scanner(pss);
1014
1015 status = alloc_gamma_tables (pss);
1016 if (status != SANE_STATUS_GOOD)
1017 {
1018 DBG (DL_MAJOR_ERROR,
1019 "%s: error in alloc_gamma_tables: %s\n",
1020 me,
1021 sane_strstatus (status));
1022 free (pss);
1023 return status;
1024 }
1025
1026 init_options (pss);
1027 status = init_gamma (pss);
1028 if (status != SANE_STATUS_GOOD)
1029 {
1030 DBG (DL_MAJOR_ERROR,
1031 "%s: error in init_gamma: %s\n",
1032 me,
1033 sane_strstatus (status));
1034 free (pss);
1035 return status;
1036 }
1037
1038 pss->state = ST_IDLE;
1039 }
1040 return SANE_STATUS_GOOD;
1041 }
1042
sane_close(SANE_Handle h)1043 void sane_close (SANE_Handle h)
1044 {
1045 SnapScan_Scanner *pss = (SnapScan_Scanner *) h;
1046 DBG (DL_CALL_TRACE, "sane_snapscan_close (%p)\n", (void *) h);
1047 switch (pss->state)
1048 {
1049 case ST_SCAN_INIT:
1050 case ST_SCANNING:
1051 release_unit (pss);
1052 break;
1053 default:
1054 break;
1055 }
1056 close_scanner (pss);
1057 snapscani_usb_shm_exit();
1058 free (pss->gamma_tables);
1059 free (pss->buf);
1060 free (pss);
1061 }
1062
1063
1064
sane_get_parameters(SANE_Handle h, SANE_Parameters *p)1065 SANE_Status sane_get_parameters (SANE_Handle h,
1066 SANE_Parameters *p)
1067 {
1068 static const char *me = "sane_snapscan_get_parameters";
1069 SnapScan_Scanner *pss = (SnapScan_Scanner *) h;
1070 SANE_Status status = SANE_STATUS_GOOD;
1071 SnapScan_Mode mode = actual_mode(pss);
1072
1073 DBG (DL_CALL_TRACE, "%s (%p, %p)\n", me, (void *) h, (void *) p);
1074
1075 p->last_frame = SANE_TRUE; /* we always do only one frame */
1076
1077 if ((pss->state == ST_SCAN_INIT) || (pss->state == ST_SCANNING))
1078 {
1079 /* we are in the middle of a scan, so we can use the data
1080 that the scanner has reported */
1081 if (pss->psrc != NULL)
1082 {
1083 DBG(DL_DATA_TRACE, "%s: Using source chain data\n", me);
1084 /* use what the source chain says */
1085 p->pixels_per_line = pss->psrc->pixelsPerLine(pss->psrc);
1086 p->bytes_per_line = pss->psrc->bytesPerLine(pss->psrc);
1087 /* p->lines = pss->psrc->remaining(pss->psrc)/p->bytes_per_line; */
1088 p->lines = pss->lines;
1089 }
1090 else
1091 {
1092 DBG(DL_DATA_TRACE, "%s: Using current data\n", me);
1093 /* estimate based on current data */
1094 p->pixels_per_line = pss->pixels_per_line;
1095 p->bytes_per_line = pss->bytes_per_line;
1096 p->lines = pss->lines;
1097 if (mode == MD_BILEVELCOLOUR)
1098 p->bytes_per_line = p->pixels_per_line*3;
1099 }
1100 }
1101 else
1102 {
1103 /* no scan in progress. The scanner data may not be up to date.
1104 we have to calculate an estimate. */
1105 double width, height;
1106 int dpi;
1107 double dots_per_mm;
1108
1109 DBG(DL_DATA_TRACE, "%s: Using estimated data\n", me);
1110 width = SANE_UNFIX (pss->brx - pss->tlx);
1111 height = SANE_UNFIX (pss->bry - pss->tly);
1112 dpi = pss->res;
1113 dots_per_mm = dpi / MM_PER_IN;
1114 p->pixels_per_line = width * dots_per_mm;
1115 p->lines = height * dots_per_mm;
1116 switch (mode)
1117 {
1118 case MD_COLOUR:
1119 case MD_BILEVELCOLOUR:
1120 p->bytes_per_line = 3 * p->pixels_per_line * ((pss->bpp_scan+7)/8);
1121 break;
1122 case MD_LINEART:
1123 p->bytes_per_line = (p->pixels_per_line + 7) / 8;
1124 break;
1125 default:
1126 /* greyscale */
1127 p->bytes_per_line = p->pixels_per_line * ((pss->bpp_scan+7)/8);
1128 break;
1129 }
1130 }
1131 p->format = (is_colour_mode(mode)) ? SANE_FRAME_RGB : SANE_FRAME_GRAY;
1132 if (mode == MD_LINEART)
1133 p->depth = 1;
1134 else if (pss->pdev->model == SCANWIT2720S)
1135 p->depth = 16;
1136 else if (pss->preview)
1137 p->depth = 8;
1138 else
1139 p->depth = pss->val[OPT_BIT_DEPTH].w;
1140
1141 DBG (DL_DATA_TRACE, "%s: depth = %ld\n", me, (long) p->depth);
1142 DBG (DL_DATA_TRACE, "%s: lines = %ld\n", me, (long) p->lines);
1143 DBG (DL_DATA_TRACE,
1144 "%s: pixels per line = %ld\n",
1145 me,
1146 (long) p->pixels_per_line);
1147 DBG (DL_DATA_TRACE,
1148 "%s: bytes per line = %ld\n",
1149 me,
1150 (long) p->bytes_per_line);
1151
1152 return status;
1153 }
1154
1155 /* scan data reader routine for child process */
1156
1157 #define READER_WRITE_SIZE 4096
1158
reader(SnapScan_Scanner *pss)1159 static void reader (SnapScan_Scanner *pss)
1160 {
1161 static char me[] = "Child reader process";
1162 SANE_Status status;
1163 SANE_Byte *wbuf = NULL;
1164
1165
1166 DBG (DL_CALL_TRACE, "%s\n", me);
1167
1168 wbuf = (SANE_Byte*) malloc(READER_WRITE_SIZE);
1169 if (wbuf == NULL)
1170 {
1171 DBG (DL_MAJOR_ERROR, "%s: failed to allocate write buffer.\n", me);
1172 return;
1173 }
1174
1175 while ((pss->preadersrc->remaining(pss->preadersrc) > 0) && !cancelRead)
1176 {
1177 SANE_Int ndata = READER_WRITE_SIZE;
1178 status = pss->preadersrc->get(pss->preadersrc, wbuf, &ndata);
1179 if (status != SANE_STATUS_GOOD)
1180 {
1181 DBG (DL_MAJOR_ERROR,
1182 "%s: %s on read.\n",
1183 me,
1184 sane_strstatus (status));
1185 return;
1186 }
1187 {
1188 SANE_Byte *buf = wbuf;
1189 DBG (DL_DATA_TRACE, "READ %d BYTES (%d)\n", ndata, cancelRead);
1190 while (ndata > 0)
1191 {
1192 int written = write (pss->rpipe[1], buf, ndata);
1193 DBG (DL_DATA_TRACE, "WROTE %d BYTES\n", written);
1194 if (written == -1)
1195 {
1196 DBG (DL_MAJOR_ERROR,
1197 "%s: error writing scan data on parent pipe.\n",
1198 me);
1199 perror ("pipe error: ");
1200 }
1201 else
1202 {
1203 ndata -= written;
1204 buf += written;
1205 }
1206 }
1207 }
1208 }
1209 }
1210
1211 /** signal handler to kill the child process
1212 */
usb_reader_process_sigterm_handler( int signo )1213 static void usb_reader_process_sigterm_handler( int signo )
1214 {
1215 DBG( DL_INFO, "(SIG) reader_process: terminated by signal %d\n", signo );
1216 cancelRead = SANE_TRUE;
1217 }
1218
sigalarm_handler( int signo __sane_unused__)1219 static void sigalarm_handler( int signo __sane_unused__)
1220 {
1221 DBG( DL_INFO, "ALARM!!!\n" );
1222 }
1223
1224 /** executed as a child process
1225 * read the data from the driver and send them to the parent process
1226 */
reader_process( void *args )1227 static int reader_process( void *args )
1228 {
1229 SANE_Status status;
1230 struct SIGACTION act;
1231 sigset_t ignore_set;
1232 SnapScan_Scanner *pss = (SnapScan_Scanner *) args;
1233
1234 if( sanei_thread_is_forked()) {
1235 DBG( DL_MINOR_INFO, "reader_process started (forked)\n" );
1236 /* child process - close read side, make stdout the write side of the pipe */
1237 close( pss->rpipe[0] );
1238 pss->rpipe[0] = -1;
1239 } else {
1240 DBG( DL_MINOR_INFO, "reader_process started (as thread)\n" );
1241 }
1242
1243 sigfillset ( &ignore_set );
1244 sigdelset ( &ignore_set, SIGUSR1 );
1245 sigprocmask( SIG_SETMASK, &ignore_set, 0 );
1246
1247 memset ( &act, 0, sizeof (act));
1248 sigaction( SIGTERM, &act, 0 );
1249
1250 cancelRead = SANE_FALSE;
1251
1252 /* install the signal handler */
1253 sigemptyset(&(act.sa_mask));
1254 act.sa_flags = 0;
1255
1256 act.sa_handler = usb_reader_process_sigterm_handler;
1257 sigaction( SIGUSR1, &act, 0 );
1258
1259 status = create_base_source (pss, SCSI_SRC, &(pss->preadersrc));
1260 if (status == SANE_STATUS_GOOD)
1261 {
1262 reader (pss);
1263 }
1264 else
1265 {
1266 DBG (DL_MAJOR_ERROR,
1267 "Reader process: failed to create SCSISource.\n");
1268 }
1269 pss->preadersrc->done(pss->preadersrc);
1270 free(pss->preadersrc);
1271 pss->preadersrc = 0;
1272 close( pss->rpipe[1] );
1273 pss->rpipe[1] = -1;
1274 DBG( DL_MINOR_INFO, "reader_process: finished reading data\n" );
1275 return SANE_STATUS_GOOD;
1276 }
1277
1278
start_reader(SnapScan_Scanner *pss)1279 static SANE_Status start_reader (SnapScan_Scanner *pss)
1280 {
1281 SANE_Status status = SANE_STATUS_GOOD;
1282 static char me[] = "start_reader";
1283
1284 DBG (DL_CALL_TRACE, "%s\n", me);
1285
1286 pss->nonblocking = SANE_FALSE;
1287 pss->rpipe[0] = pss->rpipe[1] = -1;
1288 sanei_thread_initialize (pss->child);
1289
1290 if (pipe (pss->rpipe) != -1)
1291 {
1292 pss->orig_rpipe_flags = fcntl (pss->rpipe[0], F_GETFL, 0);
1293 pss->child = sanei_thread_begin(reader_process, (void *) pss);
1294
1295 cancelRead = SANE_FALSE;
1296
1297 if (!sanei_thread_is_valid (pss->child))
1298 {
1299 /* we'll have to read in blocking mode */
1300 DBG (DL_MAJOR_ERROR,
1301 "%s: Error while calling sanei_thread_begin; must read in blocking mode.\n",
1302 me);
1303 close (pss->rpipe[0]);
1304 close (pss->rpipe[1]);
1305 status = SANE_STATUS_UNSUPPORTED;
1306 }
1307 if (sanei_thread_is_forked())
1308 {
1309 /* parent; close write side */
1310 close (pss->rpipe[1]);
1311 pss->rpipe[1] = -1;
1312 }
1313 pss->nonblocking = SANE_TRUE;
1314 }
1315 return status;
1316 }
1317
send_gamma_table(SnapScan_Scanner *pss, u_char dtc, u_char dtcq)1318 static SANE_Status send_gamma_table (SnapScan_Scanner *pss, u_char dtc, u_char dtcq)
1319 {
1320 static char me[] = "send_gamma_table";
1321 SANE_Status status = SANE_STATUS_GOOD;
1322 status = send (pss, dtc, dtcq);
1323 CHECK_STATUS (status, me, "send");
1324 switch (pss->pdev->model)
1325 {
1326 case PERFECTION1270:
1327 case PERFECTION1670:
1328 case PERFECTION2480:
1329 case PERFECTION3490:
1330 /* Some epson scanners need the gamma table twice */
1331 status = send (pss, dtc, dtcq);
1332 CHECK_STATUS (status, me, "2nd send");
1333 break;
1334 case PRISA5150:
1335 /* 5150 needs the gamma table twice, with dtc = 0x04 for the second one */
1336 status = send (pss, DTC_GAMMA2, dtcq);
1337 CHECK_STATUS (status, me, "2nd send");
1338 break;
1339 default:
1340 break;
1341 }
1342 return status;
1343 }
1344
download_gamma_tables(SnapScan_Scanner *pss)1345 static SANE_Status download_gamma_tables (SnapScan_Scanner *pss)
1346 {
1347 static char me[] = "download_gamma_tables";
1348 SANE_Status status = SANE_STATUS_GOOD;
1349 double gamma_gs = SANE_UNFIX (pss->gamma_gs);
1350 double gamma_r = SANE_UNFIX (pss->gamma_r);
1351 double gamma_g = SANE_UNFIX (pss->gamma_g);
1352 double gamma_b = SANE_UNFIX (pss->gamma_b);
1353 SnapScan_Mode mode = actual_mode (pss);
1354 int dtcq_gamma_gray;
1355 int dtcq_gamma_red;
1356 int dtcq_gamma_green;
1357 int dtcq_gamma_blue;
1358 int gamma_mode = GAMMA_8BIT;
1359
1360 DBG (DL_CALL_TRACE, "%s\n", me);
1361 switch (mode)
1362 {
1363 case MD_COLOUR:
1364 break;
1365 case MD_BILEVELCOLOUR:
1366 if (!pss->halftone)
1367 {
1368 gamma_r =
1369 gamma_g =
1370 gamma_b = 1.0;
1371 }
1372 break;
1373 case MD_LINEART:
1374 if (!pss->halftone)
1375 gamma_gs = 1.0;
1376 break;
1377 default:
1378 /* no further action for greyscale */
1379 break;
1380 }
1381
1382 switch (pss->bpp)
1383 {
1384 case 10:
1385 DBG (DL_DATA_TRACE, "%s: Sending 8bit gamma table for %d bpp\n", me, pss->bpp);
1386 dtcq_gamma_gray = DTCQ_GAMMA_GRAY10;
1387 dtcq_gamma_red = DTCQ_GAMMA_RED10;
1388 dtcq_gamma_green = DTCQ_GAMMA_GREEN10;
1389 dtcq_gamma_blue = DTCQ_GAMMA_BLUE10;
1390 break;
1391 case 12:
1392 if (pss->pdev->model == SCANWIT2720S)
1393 {
1394 DBG (DL_DATA_TRACE, "%s: Sending 16bit gamma table for %d bpp\n", me, pss->bpp);
1395 dtcq_gamma_gray = DTCQ_GAMMA_GRAY12_16BIT;
1396 dtcq_gamma_red = DTCQ_GAMMA_RED12_16BIT;
1397 dtcq_gamma_green = DTCQ_GAMMA_GREEN12_16BIT;
1398 dtcq_gamma_blue = DTCQ_GAMMA_BLUE12_16BIT;
1399 gamma_mode = GAMMA_12_16BIT;
1400 }
1401 else
1402 {
1403 DBG (DL_DATA_TRACE, "%s: Sending 8bit gamma table for %d bpp\n", me, pss->bpp);
1404 dtcq_gamma_gray = DTCQ_GAMMA_GRAY12;
1405 dtcq_gamma_red = DTCQ_GAMMA_RED12;
1406 dtcq_gamma_green = DTCQ_GAMMA_GREEN12;
1407 dtcq_gamma_blue = DTCQ_GAMMA_BLUE12;
1408 }
1409 break;
1410 case 14:
1411 if (pss->bpp_scan == 16)
1412 {
1413 DBG (DL_DATA_TRACE, "%s: Sending 16bit gamma table for %d bpp\n", me, pss->bpp);
1414 dtcq_gamma_gray = DTCQ_GAMMA_GRAY14_16BIT;
1415 dtcq_gamma_red = DTCQ_GAMMA_RED14_16BIT;
1416 dtcq_gamma_green = DTCQ_GAMMA_GREEN14_16BIT;
1417 dtcq_gamma_blue = DTCQ_GAMMA_BLUE14_16BIT;
1418 gamma_mode = GAMMA_16BIT;
1419 }
1420 else
1421 {
1422 DBG (DL_DATA_TRACE, "%s: Sending 8bit gamma table for %d bpp\n", me, pss->bpp);
1423 dtcq_gamma_gray = DTCQ_GAMMA_GRAY14;
1424 dtcq_gamma_red = DTCQ_GAMMA_RED14;
1425 dtcq_gamma_green = DTCQ_GAMMA_GREEN14;
1426 dtcq_gamma_blue = DTCQ_GAMMA_BLUE14;
1427 }
1428 break;
1429 default:
1430 DBG (DL_DATA_TRACE, "%s: Sending 8bit gamma table for %d bpp\n", me, pss->bpp);
1431 dtcq_gamma_gray = DTCQ_GAMMA_GRAY8;
1432 dtcq_gamma_red = DTCQ_GAMMA_RED8;
1433 dtcq_gamma_green = DTCQ_GAMMA_GREEN8;
1434 dtcq_gamma_blue = DTCQ_GAMMA_BLUE8;
1435 break;
1436 }
1437
1438 if (is_colour_mode(mode))
1439 {
1440 if (pss->val[OPT_CUSTOM_GAMMA].b)
1441 {
1442 if (pss->val[OPT_GAMMA_BIND].b)
1443 {
1444 /* Use greyscale gamma for all rgb channels */
1445 gamma_from_sane (pss->gamma_length, pss->gamma_table_gs,
1446 pss->buf + SEND_LENGTH, gamma_mode);
1447 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_red);
1448 CHECK_STATUS (status, me, "send");
1449
1450 gamma_from_sane (pss->gamma_length, pss->gamma_table_gs,
1451 pss->buf + SEND_LENGTH, gamma_mode);
1452 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_green);
1453 CHECK_STATUS (status, me, "send");
1454
1455 gamma_from_sane (pss->gamma_length, pss->gamma_table_gs,
1456 pss->buf + SEND_LENGTH, gamma_mode);
1457 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_blue);
1458 CHECK_STATUS (status, me, "send");
1459 }
1460 else
1461 {
1462 gamma_from_sane (pss->gamma_length, pss->gamma_table_r,
1463 pss->buf + SEND_LENGTH, gamma_mode);
1464 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_red);
1465 CHECK_STATUS (status, me, "send");
1466
1467 gamma_from_sane (pss->gamma_length, pss->gamma_table_g,
1468 pss->buf + SEND_LENGTH, gamma_mode);
1469 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_green);
1470 CHECK_STATUS (status, me, "send");
1471
1472 gamma_from_sane (pss->gamma_length, pss->gamma_table_b,
1473 pss->buf + SEND_LENGTH, gamma_mode);
1474 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_blue);
1475 CHECK_STATUS (status, me, "send");
1476 }
1477 }
1478 else
1479 {
1480 if (pss->val[OPT_GAMMA_BIND].b)
1481 {
1482 /* Use greyscale gamma for all rgb channels */
1483 gamma_n (gamma_gs, pss->bright, pss->contrast,
1484 pss->buf + SEND_LENGTH, pss->bpp, gamma_mode);
1485 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_red);
1486 CHECK_STATUS (status, me, "send");
1487
1488 gamma_n (gamma_gs, pss->bright, pss->contrast,
1489 pss->buf + SEND_LENGTH, pss->bpp, gamma_mode);
1490 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_green);
1491 CHECK_STATUS (status, me, "send");
1492
1493 gamma_n (gamma_gs, pss->bright, pss->contrast,
1494 pss->buf + SEND_LENGTH, pss->bpp, gamma_mode);
1495 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_blue);
1496 CHECK_STATUS (status, me, "send");
1497 }
1498 else
1499 {
1500 gamma_n (gamma_r, pss->bright, pss->contrast,
1501 pss->buf + SEND_LENGTH, pss->bpp, gamma_mode);
1502 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_red);
1503 CHECK_STATUS (status, me, "send");
1504
1505 gamma_n (gamma_g, pss->bright, pss->contrast,
1506 pss->buf + SEND_LENGTH, pss->bpp, gamma_mode);
1507 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_green);
1508 CHECK_STATUS (status, me, "send");
1509
1510 gamma_n (gamma_b, pss->bright, pss->contrast,
1511 pss->buf + SEND_LENGTH, pss->bpp, gamma_mode);
1512 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_blue);
1513 CHECK_STATUS (status, me, "send");
1514 }
1515 }
1516 }
1517 else
1518 {
1519 if(pss->val[OPT_CUSTOM_GAMMA].b)
1520 {
1521 gamma_from_sane (pss->gamma_length, pss->gamma_table_gs,
1522 pss->buf + SEND_LENGTH, gamma_mode);
1523 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_gray);
1524 CHECK_STATUS (status, me, "send");
1525 }
1526 else
1527 {
1528 gamma_n (gamma_gs, pss->bright, pss->contrast,
1529 pss->buf + SEND_LENGTH, pss->bpp, gamma_mode);
1530 status = send_gamma_table(pss, DTC_GAMMA, dtcq_gamma_gray);
1531 CHECK_STATUS (status, me, "send");
1532 }
1533 }
1534 return status;
1535 }
1536
download_halftone_matrices(SnapScan_Scanner *pss)1537 static SANE_Status download_halftone_matrices (SnapScan_Scanner *pss)
1538 {
1539 static char me[] = "download_halftone_matrices";
1540 SANE_Status status = SANE_STATUS_GOOD;
1541 if ((pss->halftone) &&
1542 ((actual_mode(pss) == MD_LINEART) || (actual_mode(pss) == MD_BILEVELCOLOUR)))
1543 {
1544 u_char *matrix;
1545 size_t matrix_sz;
1546 u_char dtcq;
1547
1548 if (pss->dither_matrix == dm_dd8x8)
1549 {
1550 matrix = D8;
1551 matrix_sz = sizeof (D8);
1552 }
1553 else
1554 {
1555 matrix = D16;
1556 matrix_sz = sizeof (D16);
1557 }
1558
1559 memcpy (pss->buf + SEND_LENGTH, matrix, matrix_sz);
1560
1561 if (is_colour_mode(actual_mode(pss)))
1562 {
1563 if (matrix_sz == sizeof (D8))
1564 dtcq = DTCQ_HALFTONE_COLOR8;
1565 else
1566 dtcq = DTCQ_HALFTONE_COLOR16;
1567
1568 /* need copies for green and blue bands */
1569 memcpy (pss->buf + SEND_LENGTH + matrix_sz,
1570 matrix,
1571 matrix_sz);
1572 memcpy (pss->buf + SEND_LENGTH + 2 * matrix_sz,
1573 matrix,
1574 matrix_sz);
1575 }
1576 else
1577 {
1578 if (matrix_sz == sizeof (D8))
1579 dtcq = DTCQ_HALFTONE_BW8;
1580 else
1581 dtcq = DTCQ_HALFTONE_BW16;
1582 }
1583
1584 status = send (pss, DTC_HALFTONE, dtcq);
1585 CHECK_STATUS (status, me, "send");
1586 }
1587 return status;
1588 }
1589
measure_transfer_rate(SnapScan_Scanner *pss)1590 static SANE_Status measure_transfer_rate (SnapScan_Scanner *pss)
1591 {
1592 static char me[] = "measure_transfer_rate";
1593 SANE_Status status = SANE_STATUS_GOOD;
1594
1595 if (pss->hconfig & HCFG_RB)
1596 {
1597 /* We have a ring buffer. We simulate one round of a read-store
1598 cycle on the size of buffer we will be using. For this read only,
1599 the buffer size must be rounded to a 128-byte boundary. */
1600
1601 DBG (DL_VERBOSE, "%s: have ring buffer\n", me);
1602 if ((pss->pdev->model == PERFECTION2480) || (pss->pdev->model == PERFECTION3490))
1603 {
1604 /* Epson 2480: read a multiple of bytes per line, limit to less than 0xfff0 */
1605 if (pss->bytes_per_line > 0xfff0)
1606 pss->expected_read_bytes = 0xfff0;
1607 else
1608 pss->expected_read_bytes = (0xfff0 / pss->bytes_per_line) * pss->bytes_per_line;
1609 }
1610 else
1611 pss->expected_read_bytes =
1612 (pss->buf_sz%128) ? (pss->buf_sz/128 + 1)*128 : pss->buf_sz;
1613
1614 status = scsi_read (pss, READ_TRANSTIME);
1615 CHECK_STATUS (status, me, "scsi_read");
1616 pss->expected_read_bytes = 0;
1617 status = scsi_read (pss, READ_TRANSTIME);
1618 CHECK_STATUS (status, me, "scsi_read");
1619 }
1620 else
1621 {
1622 /* we don't have a ring buffer. The test requires transferring one
1623 scan line of data (rounded up to next 128 byte boundary). */
1624
1625 DBG (DL_VERBOSE, "%s: we don't have a ring buffer.\n", me);
1626 pss->expected_read_bytes = pss->bytes_per_line;
1627
1628 if (pss->expected_read_bytes%128)
1629 {
1630 pss->expected_read_bytes =
1631 (pss->expected_read_bytes/128 + 1)*128;
1632 }
1633 status = scsi_read (pss, READ_TRANSTIME);
1634 CHECK_STATUS (status, me, "scsi_read");
1635 DBG (DL_VERBOSE, "%s: read %ld bytes.\n", me, (long) pss->read_bytes);
1636 }
1637
1638 pss->expected_read_bytes = 0;
1639 status = scsi_read (pss, READ_TRANSTIME);
1640 if (status != SANE_STATUS_GOOD)
1641 {
1642 DBG (DL_MAJOR_ERROR, "%s: test read failed.\n", me);
1643 return status;
1644 }
1645
1646 DBG (DL_VERBOSE, "%s: successfully calibrated transfer rate.\n", me);
1647 return status;
1648 }
1649
1650
sane_start(SANE_Handle h)1651 SANE_Status sane_start (SANE_Handle h)
1652 {
1653 static const char *me = "sane_snapscan_start";
1654 SANE_Status status;
1655 SnapScan_Scanner *pss = (SnapScan_Scanner *) h;
1656
1657 DBG (DL_CALL_TRACE, "%s (%p)\n", me, (void *) h);
1658
1659 /* possible authorization required */
1660
1661 status = open_scanner (pss);
1662 CHECK_STATUS (status, me, "open_scanner");
1663
1664 status = wait_scanner_ready (pss);
1665 CHECK_STATUS (status, me, "wait_scanner_ready");
1666
1667 /* start scanning; reserve the unit first, because a release_unit is
1668 necessary to abort a scan in progress */
1669
1670 pss->state = ST_SCAN_INIT;
1671
1672 if ((pss->pdev->model == SCANWIT2720S) && (pss->focus_mode == MD_AUTO))
1673 {
1674 status = get_focus(pss);
1675 CHECK_STATUS (status, me, "get_focus");
1676 }
1677
1678 reserve_unit(pss);
1679
1680 if (pss->pdev->model == SCANWIT2720S)
1681 {
1682 status = set_frame(pss, 0);
1683 CHECK_STATUS (status, me, "set_frame");
1684 status = set_focus(pss, pss->focus);
1685 CHECK_STATUS (status, me, "set_focus");
1686 }
1687
1688 /* set up the window and fetch the resulting scanner parameters */
1689 status = set_window(pss);
1690 CHECK_STATUS (status, me, "set_window");
1691
1692 status = inquiry(pss);
1693 CHECK_STATUS (status, me, "inquiry");
1694
1695 /* download the gamma and halftone tables */
1696
1697 status = download_gamma_tables(pss);
1698 CHECK_STATUS (status, me, "download_gamma_tables");
1699
1700 status = download_halftone_matrices(pss);
1701 CHECK_STATUS (status, me, "download_halftone_matrices");
1702
1703 if (pss->val[OPT_QUALITY_CAL].b && (pss->usb_vendor == USB_VENDOR_EPSON))
1704 {
1705 status = calibrate(pss);
1706 if (status != SANE_STATUS_GOOD)
1707 {
1708 DBG (DL_MAJOR_ERROR, "%s: calibration failed.\n", me);
1709 release_unit (pss);
1710 return status;
1711 }
1712 }
1713
1714 /* we must measure the data transfer rate between the host and the
1715 scanner, and the method varies depending on whether there is a
1716 ring buffer or not. */
1717
1718 status = measure_transfer_rate(pss);
1719 CHECK_STATUS (status, me, "measure_transfer_rate");
1720
1721 /* now perform an inquiry again to retrieve the scan speed */
1722 status = inquiry(pss);
1723 CHECK_STATUS (status, me, "inquiry");
1724
1725 DBG (DL_DATA_TRACE,
1726 "%s: after measuring speed:\n\t%lu bytes per scan line\n"
1727 "\t%f milliseconds per scan line.\n\t==>%f bytes per millisecond\n",
1728 me,
1729 (u_long) pss->bytes_per_line,
1730 pss->ms_per_line,
1731 pss->bytes_per_line/pss->ms_per_line);
1732
1733
1734 if (pss->val[OPT_QUALITY_CAL].b && (pss->usb_vendor != USB_VENDOR_EPSON))
1735 {
1736 status = calibrate(pss);
1737 if (status != SANE_STATUS_GOOD)
1738 {
1739 DBG (DL_MAJOR_ERROR, "%s: calibration failed.\n", me);
1740 release_unit (pss);
1741 return status;
1742 }
1743 }
1744
1745 status = scan(pss);
1746 if (status != SANE_STATUS_GOOD)
1747 {
1748 DBG (DL_MAJOR_ERROR, "%s: scan command failed: %s.\n", me, sane_strstatus(status));
1749 release_unit (pss);
1750 return status;
1751 }
1752
1753 if (pss->pdev->model == SCANWIT2720S)
1754 {
1755 status = set_frame(pss, pss->frame_no);
1756 CHECK_STATUS (status, me, "set_frame");
1757 }
1758
1759 if (pss->source == SRC_ADF)
1760 {
1761 /* Wait for scanner ready again (e.g. until paper is loaded from an ADF) */
1762 /* Maybe replace with get_data_buffer_status()? */
1763 status = wait_scanner_ready (pss);
1764 if (status != SANE_STATUS_GOOD)
1765 {
1766 DBG (DL_MAJOR_ERROR, "%s: scan command failed while waiting for scanner: %s.\n", me, sane_strstatus(status));
1767 release_unit (pss);
1768 return status;
1769 }
1770 }
1771
1772 DBG (DL_MINOR_INFO, "%s: starting the reader process.\n", me);
1773 status = start_reader(pss);
1774 {
1775 BaseSourceType st = FD_SRC;
1776 if (status != SANE_STATUS_GOOD)
1777 st = SCSI_SRC;
1778 status = create_source_chain (pss, st, &(pss->psrc));
1779 }
1780
1781 return status;
1782 }
1783
1784
sane_read(SANE_Handle h, SANE_Byte *buf, SANE_Int maxlen, SANE_Int *plen)1785 SANE_Status sane_read (SANE_Handle h,
1786 SANE_Byte *buf,
1787 SANE_Int maxlen,
1788 SANE_Int *plen)
1789 {
1790 static const char *me = "sane_snapscan_read";
1791 SnapScan_Scanner *pss = (SnapScan_Scanner *) h;
1792 SANE_Status status = SANE_STATUS_GOOD;
1793
1794 DBG (DL_CALL_TRACE,
1795 "%s (%p, %p, %ld, %p)\n",
1796 me,
1797 (void *) h,
1798 (void *) buf,
1799 (long) maxlen,
1800 (void *) plen);
1801
1802 *plen = 0;
1803
1804 if (pss->state == ST_CANCEL_INIT) {
1805 pss->state = ST_IDLE;
1806 return SANE_STATUS_CANCELLED;
1807 }
1808
1809 if (pss->psrc == NULL || pss->psrc->remaining(pss->psrc) == 0)
1810 {
1811 if (sanei_thread_is_valid (pss->child))
1812 {
1813 sanei_thread_waitpid (pss->child, 0); /* ensure no zombies */
1814 sanei_thread_invalidate (pss->child);
1815 }
1816 release_unit (pss);
1817 close_scanner (pss);
1818 if (pss->psrc != NULL)
1819 {
1820 pss->psrc->done(pss->psrc);
1821 free(pss->psrc);
1822 pss->psrc = NULL;
1823 }
1824 pss->state = ST_IDLE;
1825 return SANE_STATUS_EOF;
1826 }
1827
1828 *plen = maxlen;
1829 status = pss->psrc->get(pss->psrc, buf, plen);
1830
1831 switch (pss->state)
1832 {
1833 case ST_IDLE:
1834 DBG (DL_MAJOR_ERROR,
1835 "%s: weird error: scanner state should not be idle on call to "
1836 "sane_read.\n",
1837 me);
1838 break;
1839 case ST_SCAN_INIT:
1840 /* we've read some data */
1841 pss->state = ST_SCANNING;
1842 break;
1843 case ST_CANCEL_INIT:
1844 /* stop scanning */
1845 status = SANE_STATUS_CANCELLED;
1846 break;
1847 default:
1848 break;
1849 }
1850
1851 return status;
1852 }
1853
sane_cancel(SANE_Handle h)1854 void sane_cancel (SANE_Handle h)
1855 {
1856 char *me = "sane_snapscan_cancel";
1857 SnapScan_Scanner *pss = (SnapScan_Scanner *) h;
1858 struct SIGACTION act;
1859 SANE_Pid res;
1860
1861 DBG (DL_CALL_TRACE, "%s\n", me);
1862 switch (pss->state)
1863 {
1864 case ST_IDLE:
1865 break;
1866 case ST_SCAN_INIT:
1867 case ST_SCANNING:
1868 /* signal a cancellation has occurred */
1869 pss->state = ST_CANCEL_INIT;
1870 /* signal the reader, if any */
1871 if (sanei_thread_is_valid (pss->child))
1872 {
1873 DBG( DL_INFO, "---- killing reader_process ----\n" );
1874
1875 sigemptyset(&(act.sa_mask));
1876 act.sa_flags = 0;
1877
1878 act.sa_handler = sigalarm_handler;
1879 sigaction( SIGALRM, &act, 0 );
1880
1881 if (sanei_thread_is_forked())
1882 {
1883 /* use SIGUSR1 to set cancelRead in child process */
1884 sanei_thread_sendsig( pss->child, SIGUSR1 );
1885 }
1886 else
1887 {
1888 cancelRead = SANE_TRUE;
1889 }
1890
1891 /* give'em 10 seconds 'til done...*/
1892 alarm(10);
1893 res = sanei_thread_waitpid( pss->child, 0 );
1894 alarm(0);
1895
1896 if( res != pss->child ) {
1897 DBG( DL_MINOR_ERROR,"sanei_thread_waitpid() failed !\n");
1898
1899 /* do it the hard way...*/
1900 #ifdef USE_PTHREAD
1901 sanei_thread_kill( pss->child );
1902 #else
1903 sanei_thread_sendsig( pss->child, SIGKILL );
1904 #endif
1905 }
1906 sanei_thread_invalidate( pss->child );
1907 DBG( DL_INFO,"reader_process killed\n");
1908 }
1909 release_unit (pss);
1910 close_scanner (pss);
1911 break;
1912 case ST_CANCEL_INIT:
1913 DBG (DL_INFO, "%s: cancellation already initiated.\n", me);
1914 break;
1915 default:
1916 DBG (DL_MAJOR_ERROR,
1917 "%s: weird error: invalid scanner state (%ld).\n",
1918 me,
1919 (long) pss->state);
1920 break;
1921 }
1922 }
1923
sane_set_io_mode(SANE_Handle h, SANE_Bool m)1924 SANE_Status sane_set_io_mode (SANE_Handle h, SANE_Bool m)
1925 {
1926 static char me[] = "sane_snapscan_set_io_mode";
1927 SnapScan_Scanner *pss = (SnapScan_Scanner *) h;
1928 char *op;
1929
1930 DBG (DL_CALL_TRACE, "%s\n", me);
1931
1932 if (pss->state != ST_SCAN_INIT)
1933 return SANE_STATUS_INVAL;
1934
1935 if (m)
1936 {
1937 if (!sanei_thread_is_valid (pss->child))
1938 {
1939 DBG (DL_MINOR_INFO,
1940 "%s: no reader child; must use blocking mode.\n",
1941 me);
1942 return SANE_STATUS_UNSUPPORTED;
1943 }
1944 op = "ON";
1945 fcntl (pss->rpipe[0], F_SETFL, O_NONBLOCK | pss->orig_rpipe_flags);
1946 }
1947 else
1948 {
1949 op = "OFF";
1950 fcntl (pss->rpipe[0], F_SETFL, pss->orig_rpipe_flags);
1951 }
1952 DBG (DL_MINOR_INFO, "%s: turning nonblocking mode %s.\n", me, op);
1953 pss->nonblocking = m;
1954 return SANE_STATUS_GOOD;
1955 }
1956
sane_get_select_fd(SANE_Handle h, SANE_Int * fd)1957 SANE_Status sane_get_select_fd (SANE_Handle h, SANE_Int * fd)
1958 {
1959 static char me[] = "sane_snapscan_get_select_fd";
1960 SnapScan_Scanner *pss = (SnapScan_Scanner *) h;
1961
1962 DBG (DL_CALL_TRACE, "%s\n", me);
1963
1964 if (pss->state != ST_SCAN_INIT)
1965 return SANE_STATUS_INVAL;
1966
1967 if (!sanei_thread_is_valid (pss->child))
1968 {
1969 DBG (DL_MINOR_INFO,
1970 "%s: no reader child; cannot provide select file descriptor.\n",
1971 me);
1972 return SANE_STATUS_UNSUPPORTED;
1973 }
1974 *fd = pss->rpipe[0];
1975 return SANE_STATUS_GOOD;
1976 }
1977
1978 /*
1979 * Revision 1.73 2008/11/26 21:21:29 kitno-guest
1980 * * backend/ *.[ch]: nearly every backend used V_MAJOR
1981 * instead of SANE_CURRENT_MAJOR in sane_init()
1982 * * backend/snapscan.c: remove EXPECTED_VERSION check
1983 * since new SANE standard is forward compatible
1984 *
1985 * Revision 1.72 2008-05-15 12:50:24 ellert-guest
1986 * Fix for bug #306751: sanei-thread with pthreads on 64 bit
1987 *
1988 * Revision 1.71 2008-01-29 17:48:42 kitno-guest
1989 * fix snapscan bug, add LiDE 600F
1990 *
1991 * Revision 1.70 2007-11-18 10:59:18 ellert-guest
1992 * Fix handling of valid "negative" PIDs
1993 *
1994 * Revision 1.69 2007-11-16 08:04:02 ellert-guest
1995 * Correct the test of the return value from sanei_thread_begin
1996 *
1997 * Revision 1.68 2006-09-03 10:00:11 oliver-guest
1998 * Bugfix for firmware download by Paul Smedley
1999 *
2000 * Revision 1.67 2006/01/10 19:32:16 oliver-guest
2001 * Added 12 bit gamma tables for Epson Stylus CX-1500
2002 *
2003 * Revision 1.66 2006/01/06 20:59:17 oliver-guest
2004 * Some fixes for the Epson Stylus CX 1500
2005 *
2006 * Revision 1.65 2006/01/01 23:02:55 oliver-guest
2007 * Added snapscan-data.c to Makefile.in
2008 *
2009 * Revision 1.64 2005/12/05 20:38:23 oliver-guest
2010 * Small bugfix for Benq 5150
2011 *
2012 * Revision 1.63 2005/12/04 15:03:00 oliver-guest
2013 * Some fixes for Benq 5150
2014 *
2015 * Revision 1.62 2005/12/02 19:15:42 oliver-guest
2016 * Change SnapScan version number to 1.4.50
2017 *
2018 * Revision 1.61 2005/11/15 20:11:19 oliver-guest
2019 * Enabled quality calibration for the Epson 3490
2020 *
2021 * Revision 1.60 2005/11/10 19:42:02 oliver-guest
2022 * Added deinterlacing for Epson 3490
2023 *
2024 * Revision 1.59 2005/11/02 22:12:54 oliver-guest
2025 * Correct cut'n'paste error
2026 *
2027 * Revision 1.58 2005/11/02 19:22:06 oliver-guest
2028 * Fixes for Benq 5000
2029 *
2030 * Revision 1.57 2005/10/31 21:08:47 oliver-guest
2031 * Distinguish between Benq 5000/5000E/5000U
2032 *
2033 * Revision 1.56 2005/10/24 19:46:40 oliver-guest
2034 * Preview and range fix for Epson 2480/2580
2035 *
2036 * Revision 1.55 2005/10/23 21:28:58 oliver-guest
2037 * Fix for buffer size in high res modes, fixes for delay code
2038 *
2039 * Revision 1.54 2005/10/13 22:43:30 oliver-guest
2040 * Fixes for 16 bit scan mode from Simon Munton
2041 *
2042 * Revision 1.53 2005/10/11 18:47:07 oliver-guest
2043 * Fixes for Epson 3490 and 16 bit scan mode
2044 *
2045 * Revision 1.52 2005/09/28 21:33:11 oliver-guest
2046 * Added 16 bit option for Epson scanners (untested)
2047 *
2048 * Revision 1.51 2005/08/15 18:56:55 oliver-guest
2049 * Added temporary debug code for 2480/2580 distinction
2050 *
2051 * Revision 1.50 2005/08/15 18:06:37 oliver-guest
2052 * Added support for Epson 3490/3590 (thanks to Matt Judge)
2053 *
2054 * Revision 1.49 2005/08/07 12:37:29 oliver-guest
2055 * Use first known device if no device is specified
2056 *
2057 * Revision 1.48 2004/12/09 23:21:48 oliver-guest
2058 * Added quality calibration for Epson 2480 (by Simon Munton)
2059 *
2060 * Revision 1.47 2004/12/01 22:49:14 oliver-guest
2061 * Fix for allocation of gamma tables by Simon Munton
2062 *
2063 * Revision 1.46 2004/12/01 22:12:03 oliver-guest
2064 * Added support for Epson 1270
2065 *
2066 * Revision 1.45 2004/10/03 17:34:36 hmg-guest
2067 * 64 bit platform fixes (bug #300799).
2068 *
2069 * Revision 1.44 2004/09/02 20:59:12 oliver-guest
2070 * Added support for Epson 2480
2071 *
2072 * Revision 1.43 2004/06/16 19:52:26 oliver-guest
2073 * Don't enforce even number of URB packages on 1212u_2. Fixes bug #300753.
2074 *
2075 * Revision 1.42 2004/06/15 12:17:37 hmg-guest
2076 * Only use __attribute__ if gcc is used for compilation. Some other compilers
2077 * don't know __attribute__ and therefore can't compile sane-backends without this
2078 * fix. See bug #300803.
2079 *
2080 * Revision 1.41 2004/05/26 22:37:01 oliver-guest
2081 * Use shared memory for urb counters in snapscan backend
2082 *
2083 * Revision 1.40 2004/04/09 11:59:02 oliver-guest
2084 * Fixes for pthread implementation
2085 *
2086 * Revision 1.39 2004/04/08 21:53:10 oliver-guest
2087 * Use sanei_thread in snapscan backend
2088 *
2089 * Revision 1.37 2003/11/27 23:11:32 oliver-guest
2090 * Send gamma table twice for Epson Perfection 1670
2091 *
2092 * Revision 1.36 2003/11/09 21:43:45 oliver-guest
2093 * Disabled quality calibration for Epson Perfection 1670
2094 *
2095 * Revision 1.35 2003/11/07 23:26:49 oliver-guest
2096 * Final bugfixes for bascic support of Epson 1670
2097 *
2098 * Revision 1.34 2003/10/21 20:43:25 oliver-guest
2099 * Bugfixes for SnapScan backend
2100 *
2101 * Revision 1.33 2003/10/07 18:29:20 oliver-guest
2102 * Initial support for Epson 1670, minor bugfix
2103 *
2104 * Revision 1.32 2003/09/24 18:05:39 oliver-guest
2105 * Bug #300198: Check second argument of sanei_config_get_string
2106 *
2107 * Revision 1.31 2003/09/12 16:10:33 hmg-guest
2108 * Moved union Option_Value from backend header files to sanei_backend.h. No need
2109 * to copy it over and over again. Changed header inclusion order in backend
2110 * files to include backend.h after sanei_backend.h. Based on a patch from stef
2111 * <stef-listes@wanadoo.fr>.
2112 *
2113 * Revision 1.30 2003/08/19 21:05:08 oliverschwartz
2114 * Scanner ID cleanup
2115 *
2116 * Revision 1.29 2003/04/30 20:49:40 oliverschwartz
2117 * SnapScan backend 1.4.26
2118 *
2119 * Revision 1.58 2003/04/30 20:43:07 oliverschwartz
2120 * Set backend version number to 1.4.26
2121 *
2122 * Revision 1.57 2003/04/02 21:17:14 oliverschwartz
2123 * Fix for 1200 DPI with Acer 5000
2124 *
2125 * Revision 1.56 2003/02/08 10:45:09 oliverschwartz
2126 * Use 600 DPI as optical resolution for Benq 5000
2127 *
2128 * Revision 1.55 2003/01/08 21:16:17 oliverschwartz
2129 * Added support for Acer / Benq 310U
2130 *
2131 * Revision 1.54 2002/12/10 20:14:12 oliverschwartz
2132 * Enable color offset correction for SnapScan300
2133 *
2134 * Revision 1.53 2002/10/31 19:29:41 oliverschwartz
2135 * Set version to 1.4.17
2136 *
2137 * Revision 1.52 2002/10/12 10:40:48 oliverschwartz
2138 * Added support for Snapscan e10
2139 *
2140 * Revision 1.51 2002/09/26 19:27:44 oliverschwartz
2141 * Version 1.4.16
2142 *
2143 * Revision 1.50 2002/09/24 16:07:44 oliverschwartz
2144 * Added support for Benq 5000
2145 *
2146 * Revision 1.49 2002/07/12 22:53:54 oliverschwartz
2147 * Version 1.4.15
2148 *
2149 * Revision 1.48 2002/07/12 22:53:16 oliverschwartz
2150 * call sanei_usb_init() before sanei_usb_attach_matching_devices()
2151 *
2152 * Revision 1.47 2002/06/06 21:16:23 oliverschwartz
2153 * Set backend version to 1.4.14
2154 *
2155 * Revision 1.46 2002/06/06 20:40:01 oliverschwartz
2156 * Changed default scan area for transparency unit of SnapScan e50
2157 *
2158 * Revision 1.45 2002/05/02 18:29:34 oliverschwartz
2159 * - Added ADF support
2160 * - Fixed status handling after cancel
2161 *
2162 * Revision 1.44 2002/04/27 14:42:30 oliverschwartz
2163 * Cleanup of debug logging
2164 *
2165 * Revision 1.43 2002/04/23 22:40:33 oliverschwartz
2166 * Improve config file reading
2167 *
2168 * Revision 1.42 2002/04/10 21:00:09 oliverschwartz
2169 * Check for NULL pointer before deleting device list
2170 *
2171 * Revision 1.41 2002/03/24 12:12:36 oliverschwartz
2172 * - Moved option functions to snapscan-options.c
2173 * - Autodetect USB scanners
2174 * - Cleanup
2175 *
2176 * Revision 1.40 2002/02/09 14:55:23 oliverschwartz
2177 * Added language translation support (SANE_I18N)
2178 *
2179 * Revision 1.39 2002/01/23 20:40:54 oliverschwartz
2180 * Don't use quantization for scan area parameter
2181 * Improve recognition of Acer 320U
2182 * Version 1.4.7
2183 *
2184 * Revision 1.38 2002/01/14 21:11:56 oliverschwartz
2185 * Add workaround for bug semctl() call in libc for PPC
2186 *
2187 * Revision 1.37 2002/01/10 21:33:12 oliverschwartz
2188 * Set version number to 1.4.4
2189 *
2190 * Revision 1.36 2002/01/06 18:34:02 oliverschwartz
2191 * Added support for Snapscan e42 thanks to Yari Ad� Petralanda
2192 *
2193 * Revision 1.35 2001/12/20 23:18:01 oliverschwartz
2194 * Remove tmpfname
2195 *
2196 * Revision 1.34 2001/12/18 18:28:35 oliverschwartz
2197 * Removed temporary file
2198 *
2199 * Revision 1.33 2001/12/12 19:43:30 oliverschwartz
2200 * - Set version number to 1.4.3
2201 * - Clean up CVS Log
2202 *
2203 * Revision 1.32 2001/12/09 23:06:45 oliverschwartz
2204 * - use sense handler for USB if scanner reports CHECK_CONDITION
2205 *
2206 * Revision 1.31 2001/12/08 11:50:34 oliverschwartz
2207 * Fix dither matrix computation
2208 *
2209 * Revision 1.30 2001/11/29 22:50:14 oliverschwartz
2210 * Add support for SnapScan e52
2211 *
2212 * Revision 1.29 2001/11/27 23:16:17 oliverschwartz
2213 * - Fix color alignment for SnapScan 600
2214 * - Added documentation in snapscan-sources.c
2215 * - Guard against TL_X < BR_X and TL_Y < BR_Y
2216 *
2217 * Revision 1.28 2001/11/25 18:51:41 oliverschwartz
2218 * added support for SnapScan e52 thanks to Rui Lopes
2219 *
2220 * Revision 1.27 2001/11/16 20:28:35 oliverschwartz
2221 * add support for Snapscan e26
2222 *
2223 * Revision 1.26 2001/11/16 20:23:16 oliverschwartz
2224 * Merge with sane-1.0.6
2225 * - Check USB vendor IDs to avoid hanging scanners
2226 * - fix bug in dither matrix computation
2227 *
2228 * Revision 1.25 2001/10/25 11:06:22 oliverschwartz
2229 * Change snapscan backend version number to 1.4.0
2230 *
2231 * Revision 1.24 2001/10/11 14:02:10 oliverschwartz
2232 * Distinguish between e20/e25 and e40/e50
2233 *
2234 * Revision 1.23 2001/10/09 22:34:23 oliverschwartz
2235 * fix compiler warnings
2236 *
2237 * Revision 1.22 2001/10/08 19:26:01 oliverschwartz
2238 * - Disable quality calibration for scanners that do not support it
2239 *
2240 * Revision 1.21 2001/10/08 18:22:02 oliverschwartz
2241 * - Disable quality calibration for Acer Vuego 310F
2242 * - Use sanei_scsi_max_request_size as scanner buffer size
2243 * for SCSI devices
2244 * - Added new devices to snapscan.desc
2245 *
2246 * Revision 1.20 2001/09/18 15:01:07 oliverschwartz
2247 * - Read scanner id string again after firmware upload
2248 * to identify correct model
2249 * - Make firmware upload work for AGFA scanners
2250 * - Change copyright notice
2251 *
2252 * Revision 1.19 2001/09/17 10:01:08 sable
2253 * Added model AGFA 1236U
2254 *
2255 * Revision 1.18 2001/09/10 10:16:32 oliverschwartz
2256 * better USB / SCSI recognition, correct max scan area for 1236+TPO
2257 *
2258 * Revision 1.17 2001/09/09 18:06:32 oliverschwartz
2259 * add changes from Acer (new models; automatic firmware upload for USB scanners); fix distorted colour scans after greyscale scans (call set_window only in sane_start); code cleanup
2260 *
2261 * Revision 1.16 2001/09/07 09:42:13 oliverschwartz
2262 * Sync with Sane-1.0.5
2263 *
2264 * Revision 1.15 2001/05/15 20:51:14 oliverschwartz
2265 * check for pss->devname instead of name in sane_open()
2266 *
2267 * Revision 1.14 2001/04/10 13:33:06 sable
2268 * Transparency adapter bug and xsane crash corrections thanks to Oliver Schwartz
2269 *
2270 * Revision 1.13 2001/04/10 13:00:31 sable
2271 * Moving sanei_usb_* to snapscani_usb*
2272 *
2273 * Revision 1.12 2001/04/10 11:04:31 sable
2274 * Adding support for snapscan e40 an e50 thanks to Giuseppe Tanzilli
2275 *
2276 * Revision 1.11 2001/03/17 22:53:21 sable
2277 * Applying Mikael Magnusson patch concerning Gamma correction
2278 * Support for 1212U_2
2279 *
2280 * Revision 1.4 2001/03/04 16:50:53 mikael
2281 * Added Scan Mode, Geometry, Enhancement and Advanced groups. Implemented brightness and contrast controls with gamma tables. Added Quality Calibration, Analog Gamma Bind, Custom Gamma and Gamma Vector GS,R,G,B options.
2282 *
2283 * Revision 1.3 2001/02/16 18:32:28 mikael
2284 * impl calibration, signed position, increased buffer size
2285 *
2286 * Revision 1.2 2001/02/10 18:18:29 mikael
2287 * Extended x and y ranges
2288 *
2289 * Revision 1.1.1.1 2001/02/10 17:09:29 mikael
2290 * Imported from snapscan-11282000.tar.gz
2291 *
2292 * Revision 1.10 2000/11/10 01:01:59 sable
2293 * USB (kind of) autodetection
2294 *
2295 * Revision 1.9 2000/11/01 01:26:43 sable
2296 * Support for 1212U
2297 *
2298 * Revision 1.8 2000/10/30 22:31:13 sable
2299 * Auto preview mode
2300 *
2301 * Revision 1.7 2000/10/28 14:16:10 sable
2302 * Bug correction for SnapScan310
2303 *
2304 * Revision 1.6 2000/10/28 14:06:35 sable
2305 * Add support for Acer300f
2306 *
2307 * Revision 1.5 2000/10/15 19:52:06 cbagwell
2308 * Changed USB support to a 1 line modification instead of multi-file
2309 * changes.
2310 *
2311 * Revision 1.4 2000/10/13 03:50:27 cbagwell
2312 * Updating to source from SANE 1.0.3. Calling this version 1.1
2313 *
2314 * Revision 1.3 2000/08/12 15:09:35 pere
2315 * Merge devel (v1.0.3) into head branch.
2316 *
2317 * Revision 1.1.1.1.2.5 2000/07/29 16:04:33 hmg
2318 * 2000-07-29 Henning Meier-Geinitz <hmg@gmx.de>
2319 *
2320 * * backend/GUIDE: Added some comments about portability and
2321 * documentation.
2322 * * backend/abaton.c backend/agfafocus.c backend/apple.c
2323 * backend/canon.c backend/coolscan.c backend/dc210.c backend/dc25.c
2324 * backend/dll.c backend/dmc.c backend/microtek.c backend/microtek2.c
2325 * backend/microtek2.c backend/mustek_pp.c backend/net.c backend/pint.c
2326 * backend/pnm.c backend/qcam.c backend/ricoh.c backend/s9036.c
2327 * backend/sane_strstatus.c backend/sharp.c backend/snapscan.c
2328 * backend/st400.c backend/stubs.c backend/tamarack.c backend/v4l.c:
2329 * Changed include statements from #include <sane/...> to
2330 * #include "sane...".
2331 * * backend/avision.c backend/dc25.c: Use DBG(0, ...) instead of
2332 * fprintf (stderr, ...)
2333 * * backend/avision.c backend/canon-sane.c backend/coolscan.c
2334 * backend/dc25.c backend/microtek.c backend/microtek2.c
2335 * backend/st400.c: Use sanei_config_read() instead of fgets().
2336 * * backend/coolscan.desc backend/microtek.desc backend/microtek2.desc
2337 * backend/st400.desc: Added :interface and :manpage entries.
2338 * * backend/nec.desc: Status is beta now (was: new). Fixed typo.
2339 * * doc/canon.README: Removed, because the information is included in
2340 * the manpage now.
2341 * * doc/Makefile.in: Added sane-coolscan to list of mapages to install.
2342 * * README: Added Link to coolscan manpage.
2343 * * backend/mustek.*: Update to Mustek backend 1.0-94. Fixed the
2344 * #include <sane/...> bug.
2345 *
2346 * Revision 1.1.1.1.2.4 2000/07/25 21:47:43 hmg
2347 * 2000-07-25 Henning Meier-Geinitz <hmg@gmx.de>
2348 *
2349 * * backend/snapscan.c: Use DBG(0, ...) instead of fprintf (stderr, ...).
2350 * * backend/abaton.c backend/agfafocus.c backend/apple.c backend/dc210.c
2351 * backend/dll.c backend/dmc.c backend/microtek2.c backend/pint.c
2352 * backend/qcam.c backend/ricoh.c backend/s9036.c backend/snapscan.c
2353 * backend/tamarack.c: Use sanei_config_read instead of fgets.
2354 * * backend/dc210.c backend/microtek.c backend/pnm.c: Added
2355 * #include "../include/sane/config.h".
2356 * * backend/dc25.c backend/m3096.c backend/sp15.c
2357 * backend/st400.c: Moved #include "../include/sane/config.h" to the beginning.
2358 * * AUTHORS: Changed agfa to agfafocus.
2359 *
2360 * Revision 1.1.1.1.2.3 2000/07/17 21:37:28 hmg
2361 * 2000-07-17 Henning Meier-Geinitz <hmg@gmx.de>
2362 *
2363 * * backend/snapscan.c backend/snapscan-scsi.c: Replace C++ comment
2364 * with C comment.
2365 *
2366 * Revision 1.1.1.1.2.2 2000/07/13 04:47:46 pere
2367 * New snapscan backend version dated 20000514 from Steve Underwood.
2368 *
2369 * Revision 1.2 2000/05/14 13:30:20 coppice
2370 * R, G and B images now merge correctly. Still some outstanding issues,
2371 * but a lot more useful than before.
2372 *
2373 * Revision 1.2 2000/03/05 13:55:20 pere
2374 * Merged main branch with current DEVEL_1_9.
2375 *
2376 * Revision 1.1.1.1.2.1 1999/09/15 18:20:44 charter
2377 * Early version 1.0 snapscan.c
2378 *
2379 * Revision 2.2 1999/09/09 18:22:45 charter
2380 * Checkpoint. Now using Sources for scanner data, and have removed
2381 * references to the old snapscan-310.c stuff. This stuff must still
2382 * be incorporated into the RGBRouter to get trilinear CCD SnapScan
2383 * models working.
2384 *
2385 * Revision 2.1 1999/09/08 03:07:05 charter
2386 * Start of branch 2; same as 1.47.
2387 *
2388 * Revision 1.47 1999/09/08 03:03:53 charter
2389 * The actions for the scanner command options now use fprintf for
2390 * printing, rather than DGB. I want the output to come out no matter
2391 * what the value of the snapscan debug level.
2392 *
2393 * Revision 1.46 1999/09/07 20:53:41 charter
2394 * Changed expected_data_len to bytes_remaining.
2395 *
2396 * Revision 1.45 1999/09/06 23:32:37 charter
2397 * Split up sane_start() into sub-functions to improve readability (again).
2398 * Introduced actual_mode() and is_colour_mode() (again).
2399 * Fixed problems with cancellation. Works fine with my system now.
2400 *
2401 * Revision 1.44 1999/09/02 05:28:01 charter
2402 * Added Gary Plewa's name to the list of copyrighted contributors.
2403 *
2404 * Revision 1.43 1999/09/02 05:23:54 charter
2405 * Added Gary Plewa's patch for the Acer PRISA 620s.
2406 *
2407 * Revision 1.42 1999/09/02 02:05:34 charter
2408 * Check-in of revision 1.42 (release 0.7 of the backend).
2409 * This is part of the recovery from the great disk crash of Sept 1, 1999.
2410 *
2411 * Revision 1.42 1999/07/09 22:37:55 charter
2412 * Potential bugfix for problems with sane_get_parameters() and
2413 * the new generic scsi driver (suggested by Francois Desarmeni,
2414 * Douglas Gilbert, Abel Deuring).
2415 *
2416 * Revision 1.41 1999/07/09 20:58:07 charter
2417 * Changes to support SnapScan 1236s (Petter Reinholdsten).
2418 *
2419 * Revision 1.40 1998/12/16 18:43:06 charter
2420 * Fixed major version problem precipitated by release of SANE-1.00.
2421 *
2422 * Revision 1.39 1998/09/07 06:09:26 charter
2423 * Formatting (whitespace) changes.
2424 *
2425 * Revision 1.38 1998/09/07 06:06:01 charter
2426 * Merged in Wolfgang Goeller's changes (Vuego 310S, bugfixes).
2427 *
2428 * Revision 1.37 1998/08/06 06:16:39 charter
2429 * Now using sane_config_attach_matching_devices() in sane_snapscan_init().
2430 * Change contributed by David Mosberger-Tang.
2431 *
2432 * Revision 1.36 1998/05/11 17:02:53 charter
2433 * Added Mikko's threshold stuff.
2434 *
2435 * Revision 1.35 1998/03/10 23:43:23 eblot
2436 * Bug correction
2437 *
2438 * Revision 0.72 1998/03/10 23:40:42 eblot
2439 * More support for 310/600 models: color preview, large window
2440 *
2441 * Revision 1.35 1998/03/10 21:32:07 eblot
2442 * Debugging
2443 *
2444 * Revision 1.34 1998/02/15 21:55:53 charter
2445 * From Emmanuel Blot:
2446 * First routines to support SnapScan 310 scanned data.
2447 *
2448 * Revision 1.33 1998/02/06 02:30:28 charter
2449 * Now using a mode enum (instead of the static string pointers directly).
2450 * Now check for the SnapScan 310 and 600 explicitly (start of support
2451 * for these models).
2452 *
2453 * Revision 1.32 1998/02/01 21:56:48 charter
2454 * Patches to fix compilation problems on Solaris supplied by
2455 * Jim McBeath.
2456 *
2457 * Revision 1.31 1998/02/01 03:36:40 charter
2458 * Now check for BRX < TLX and BRY < TLY and whether the area of the
2459 * scanning window is approaching zero in set_window. I'm setting a
2460 * minimum window size of 75x75 hardware pixels (0.25 inches a side).
2461 * If the area falls to zero, the scanner seems to hang in the middle
2462 * of the set_window command.
2463 *
2464 * Revision 1.30 1998/02/01 00:00:33 charter
2465 * TLX, TLY, BRX and BRY are now lengths expressed in mm. The frontends
2466 * can now allow changes in the units, and units that are more user-
2467 * friendly.
2468 *
2469 * Revision 1.29 1998/01/31 21:09:19 charter
2470 * Fixed another problem with add_device(): if mini_inquiry ends
2471 * up indirectly invoking the sense handler, there'll be a segfault
2472 * because the sense_handler isn't set. Had to fix sense_handler so
2473 * it can handle a NULL pss pointer and then use the sanei_scsi stuff
2474 * everywhere. This error is most likely to occur if the scanner is
2475 * turned off.
2476 *
2477 * Revision 1.28 1998/01/31 18:45:22 charter
2478 * Last fix botched, produced a compile error. Thought I'd already
2479 * compiled successfully.
2480 *
2481 * Revision 1.27 1998/01/31 18:32:42 charter
2482 * Fixed stupid bug in add_device that causes segfault when no snapscan
2483 * found: closing a scsi fd opened with open() using sanei_scsi_close().
2484 *
2485 * Revision 1.26 1998/01/30 21:19:02 charter
2486 * sane_snapscan_init() handles failure of add_device() in the same
2487 * way when there is no snapscan.conf file available as when there is
2488 * one.
2489 *
2490 * Revision 1.25 1998/01/30 19:41:11 charter
2491 * Waiting for child process termination at regular end of scan (not
2492 * just on cancellation); before I was getting zombies.
2493 *
2494 * Revision 1.24 1998/01/30 19:19:27 charter
2495 * Changed from strncmp() to strncasecmp() to do vendor and model
2496 * comparisons in sane_snapscan_init. There are some snapcsan models
2497 * that use lower case.
2498 * Now have debug level defines instead of raw numbers, and better debug
2499 * information categories.
2500 * Don't complain at debug level 0 when a snapscan isn't found on a
2501 * requested device.
2502 * Changed CHECK_STATUS to take caller parameter instead of always
2503 * assuming an available string "me".
2504 *
2505 * Revision 1.23 1998/01/30 11:03:04 charter
2506 * Fixed * vs [] operator precedence screwup in sane_snapscan_get_devices()
2507 * that caused a segfault in scanimage -h.
2508 * Fixed problem with not closing the scsi fd between certain commands
2509 * that caused scanimage to hang; now using open_scanner() and close_scanner().
2510 *
2511 * Revision 1.22 1998/01/28 09:02:55 charter
2512 * Fixed bug: zero allocation length in request sense command buffer
2513 * was preventing sense information from being received. The
2514 * backend now correctly waits for the scanner to warm up.
2515 * Now using the hardware configuration byte to check whether
2516 * both 8x8 and 16x16 halftoning should be made available.
2517 *
2518 * Revision 1.21 1998/01/25 09:57:57 charter
2519 * Added more SCSI command buttons (and a group for them).
2520 * Made the output of the Inquiry command a bit nicer.
2521 *
2522 * Revision 1.20 1998/01/25 08:53:14 charter
2523 * Have added bi-level colour mode, with halftones too.
2524 * Can now select preview mode (but it's an advanced option, since
2525 * you usually don't want to do it).
2526 *
2527 * Revision 1.19 1998/01/25 02:25:02 charter
2528 * Fixed bug: preview mode gives blank image at initial startup.
2529 * Fixed bug: lineart mode goes weird after a preview or gs image.
2530 * More changes to option relationships;
2531 * now using test_unit_ready and send_diagnostic in sane_snapscan_open().
2532 * Added negative option.
2533 *
2534 * Revision 1.18 1998/01/24 05:15:32 charter
2535 * Now have RGB gamma correction and dispersed-dot dither halftoning
2536 * for BW images. Cleaned up some spots in the code and have set up
2537 * option interactions a bit better (e.g. halftoning and GS gamma
2538 * correction made inactive in colour mode, etc). TL_[XY] and BR_[XY]
2539 * now change in ten-pixel increments (I had problems with screwed-up
2540 * scan lines when the dimensions were weird at low res... could be the
2541 * problem).
2542 *
2543 * Revision 1.17 1998/01/23 13:03:17 charter
2544 * Several changes, all aimed at getting scanning performance working
2545 * correctly, and the progress/cancel window functioning. Cleaned up
2546 * a few nasty things as well.
2547 *
2548 * Revision 1.16 1998/01/23 07:40:23 charter
2549 * Reindented using GNU convention at David Mosberger-Tang's request.
2550 * Also applied David's patch fixing problems on 64-bit architectures.
2551 * Now using scanner's reported speed to gauge amount of data to request
2552 * in a read on the scsi fd---nonblocking mode operates better now.
2553 * Fixed stupid bug I introduced in preview mode data transfer.
2554 *
2555 * Revision 1.15 1998/01/22 06:18:57 charter
2556 * Raised the priority of a couple of DBG messages in reserve_unit()
2557 * and release_unit(), and got rid of some unnecessary ones.
2558 *
2559 * Revision 1.14 1998/01/22 05:15:35 charter
2560 * Have replaced the bit depth option with a mode option; various
2561 * changes associated with that.
2562 * Also, I again close the STDERR_FILENO in the reader child and
2563 * dup the STDOUT file descriptor onto it. This prevents an "X io"
2564 * error when the child exits, while still allowing the use of
2565 * DBG.
2566 *
2567 * Revision 1.13 1998/01/21 20:41:22 charter
2568 * Added copyright info.
2569 * Also now seem to have cancellation working. This requires using a
2570 * new scanner state variable and checking in all the right places
2571 * in the reader child and the sane_snapscan_read function. I've
2572 * tested it using both blocking and nonblocking I/O and it seems
2573 * to work both ways.
2574 * I've also switched to GTK+-0.99.2 and sane-0.69, and the
2575 * mysterious problems with the preview window have disappeared.
2576 * Problems with scanimage doing weird things to options have also
2577 * gone away and the frontends seem more stable.
2578 *
2579 * Revision 1.12 1998/01/21 11:05:53 charter
2580 * Inoperative code largely #defined out; I had the preview window
2581 * working correctly by having the window coordinates properly
2582 * constrained, but now the preview window bombs with a floating-
2583 * point error each time... I'm not sure yet what happened.
2584 * I've also figured out that we need to use reserve_unit and
2585 * release_unit in order to cancel scans in progress. This works
2586 * under scanimage, but I can't seem to find a way to fit cancellation
2587 * into xscanimage properly.
2588 *
2589 * Revision 1.11 1998/01/20 22:42:08 charter
2590 * Applied Franck's patch from Dec 17; preview mode is now grayscale.
2591 *
2592 * Revision 1.10 1997/12/10 23:33:12 charter
2593 * Slight change to some floating-point computations in the brightness
2594 * and contrast stuff. The controls don't seem to do anything to the
2595 * scanner though (I think these aren't actually supported in the
2596 * SnapScan).
2597 *
2598 * Revision 1.9 1997/11/26 15:40:50 charter
2599 * Brightness and contrast added by Michel.
2600 *
2601 * Revision 1.8 1997/11/12 12:55:40 charter
2602 * No longer exec after forking to do nonblocking scanning; found how
2603 * to fix the problems with SIGPIPEs from before.
2604 * Now support a config file like the other scanner drivers, and
2605 * can check whether a given device is an AGFA SnapScan (mini_inquiry()).
2606 *
2607 * Revision 1.7 1997/11/10 05:52:08 charter
2608 * Now have the child reader process and pipe stuff working, and
2609 * nonblocking mode. For large scans the nonblocking mode actually
2610 * seems to cut down on cpu hogging (though there is still a hit).
2611 *
2612 * Revision 1.6 1997/11/03 07:45:54 charter
2613 * Added the predef_window stuff. I've tried it with 6x4, and it seems
2614 * to work; I think something gets inconsistent if a preview is
2615 * performed though.
2616 *
2617 * Revision 1.5 1997/11/03 03:15:27 charter
2618 * Global static variables have now become part of the scanner structure;
2619 * the inquiry command automatically retrieves window parameters into
2620 * scanner structure members. Things are a bit cleaned up.
2621 *
2622 * Revision 1.4 1997/11/02 23:35:28 charter
2623 * After much grief.... I can finally scan reliably. Now it's a matter
2624 * of getting the band arrangement sorted out.
2625 *
2626 * Revision 1.3 1997/10/30 07:36:37 charter
2627 * Fixed a stupid bug in the #defines for the inquiry command, pointed out
2628 * by Franck.
2629 *
2630 * Revision 1.2 1997/10/14 06:00:11 charter
2631 * Option manipulation and some basic SCSI commands done; the basics
2632 * for scanning are written but there are bugs. A full scan always hangs
2633 * the SCSI driver, and preview mode scans complete but it isn't clear
2634 * whether any meaningful data is received.
2635 *
2636 * Revision 1.1 1997/10/13 02:25:54 charter
2637 * Initial revision
2638 * */
2639