1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2002-2003 Frank Zago (sane at zago dot net)
4 Copyright (C) 2003-2008 Gerard Klaver (gerard at gkall dot hobby dot nl)
5
6 This file is part of the SANE package.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>.
20
21 As a special exception, the authors of SANE give permission for
22 additional uses of the libraries contained in this release of SANE.
23
24 The exception is that, if you link a SANE library with other files
25 to produce an executable, this does not by itself cause the
26 resulting executable to be covered by the GNU General Public
27 License. Your use of that executable is in no way restricted on
28 account of linking the SANE library code into it.
29
30 This exception does not, however, invalidate any other reasons why
31 the executable file might be covered by the GNU General Public
32 License.
33
34 If you submit changes to SANE to the maintainers to be included in
35 a subsequent release, you agree by submitting the changes that
36 those changes may be distributed with this exception intact.
37
38 If you write modifications of your own for SANE, it is your choice
39 whether to permit this exception to apply to your modifications.
40 If you do not wish that, delete this exception notice.
41 */
42
43 /*
44 TECO scanner VM3575, VM656A, VM6575, VM6586, VM356A, VM3564
45 update 2003/02/14, Patch for VM356A Gerard Klaver
46 update 2003/03/19, traces, tests VM356A Gerard Klaver, Michael Hoeller
47 update 2003/07/19, white level calibration, color modes VM3564, VM356A, VM3575
48 Gerard Klaver, Michael Hoeller
49 update 2004/01/15, white level , red, green, and blue calibration and
50 leave out highest and lowest value and then divide
51 VM3564, VM356A and VM3575: Gerard Klaver
52 update 2004/08/04, white level, red, green and blue calibration for the VM6575
53 changed default SANE_TECO_CAL_ALGO to 1 for VM3564 and VM6575
54 preview value changed to 75 dpi for VM6575
55 leave out highest and lowest value and then divide
56 VM656A, VM6586
57 update 2004/08/05, use of SANE_VALUE_SCAN_MODE_LINEART, _GRAY, and _COLOR,
58 changed use of %d to %ld (when bytes values are displayed)
59 update 2005/03/04, use of __sane_unused__
60 update 2005/07/29. Removed using teco_request_sense (dev) routine for VM3564
61 update 2008/01/12, Update teco_request_sense routine due to no
62 init value for size.
63 */
64
65 /*--------------------------------------------------------------------------*/
66
67 #define BUILD 10 /* 2008/01/12 */
68 #define BACKEND_NAME teco2
69 #define TECO2_CONFIG_FILE "teco2.conf"
70
71 /*--------------------------------------------------------------------------*/
72
73 #include "../include/sane/config.h"
74
75 #include <errno.h>
76 #include <fcntl.h>
77 #include <limits.h>
78 #include <signal.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <sys/types.h>
83 #include <sys/wait.h>
84 #include <unistd.h>
85
86 #include "../include/sane/sane.h"
87 #include "../include/sane/sanei.h"
88 #include "../include/sane/saneopts.h"
89 #include "../include/sane/sanei_scsi.h"
90 #include "../include/sane/sanei_debug.h"
91 #include "../include/sane/sanei_backend.h"
92 #include "../include/sane/sanei_config.h"
93 #include "../include/lassert.h"
94
95 #include "teco2.h"
96
97 /* This is used to debug the backend without having the real hardware. */
98 #undef sim
99 #ifdef sim
100 #define sanei_scsi_cmd2(a, b, c, d, e, f, g) SANE_STATUS_GOOD
101 #define sanei_scsi_open(a, b, c, d) 0
102 #define sanei_scsi_cmd(a, b, c, d, e) SANE_STATUS_GOOD
103 #define sanei_scsi_close(a) SANE_STATUS_GOOD
104 #endif
105
106 /* For debugging purposes: output a stream straight out from the
107 * scanner without reordering the colors, 0=normal, 1 = raw. */
108 static int raw_output = 0;
109
110 /*--------------------------------------------------------------------------*/
111
112 /* Lists of possible scan modes. */
113 static SANE_String_Const scan_mode_list[] = {
114 SANE_VALUE_SCAN_MODE_LINEART,
115 SANE_VALUE_SCAN_MODE_GRAY,
116 SANE_VALUE_SCAN_MODE_COLOR,
117 NULL
118 };
119
120 /*--------------------------------------------------------------------------*/
121
122 /* List of color dropout. */
123 static SANE_String_Const filter_color_list[] = {
124 "Red",
125 "Green",
126 "Blue",
127 NULL
128 };
129 static const int filter_color_val[] = {
130 0,
131 1,
132 2
133 };
134
135 /*--------------------------------------------------------------------------*/
136
137 /* List of dithering options. */
138 static SANE_String_Const dither_list[] = {
139 "Line art",
140 "2x2",
141 "3x3",
142 "4x4 bayer",
143 "4x4 smooth",
144 "8x8 bayer",
145 "8x8 smooth",
146 "8x8 horizontal",
147 "8x8 vertical",
148 NULL
149 };
150 static const int dither_val[] = {
151 0x00,
152 0x01,
153 0x02,
154 0x03,
155 0x04,
156 0x05,
157 0x06,
158 0x07,
159 0x08
160 };
161
162 /*--------------------------------------------------------------------------*/
163
164 static const SANE_Range threshold_range = {
165 0, /* minimum */
166 255, /* maximum */
167 0 /* quantization */
168 };
169
170 /*--------------------------------------------------------------------------*/
171
172 static const SANE_Range red_level_range = {
173 0, /* minimum */
174 64, /* maximum */
175 1 /* quantization */
176 };
177
178 /*--------------------------------------------------------------------------*/
179
180 static const SANE_Range green_level_range = {
181 0, /* minimum */
182 64, /* maximum */
183 1 /* quantization */
184 };
185
186 /*--------------------------------------------------------------------------*/
187
188 static const SANE_Range blue_level_range = {
189 0, /* minimum */
190 64, /* maximum */
191 1 /* quantization */
192 };
193
194 /*--------------------------------------------------------------------------*/
195
196 /* Gamma range */
197 static const SANE_Range gamma_range = {
198 0, /* minimum */
199 255, /* 255 maximum */
200 0 /* quantization */
201 };
202
203 /*--------------------------------------------------------------------------*/
204
205 static const struct dpi_color_adjust vm3564_dpi_color_adjust[] = {
206
207 /*dpi, color sequence R G or B, 0 (-) or 1 (+) color skewing, lines skewing */
208 {25, 1, 0, 2, 0, 0},
209 {50, 1, 2, 0, 0, 1},
210 {75, 1, 0, 2, 1, 1},
211 {150, 1, 0, 2, 1, 2},
212 {225, 1, 0, 2, 1, 3},
213 {300, 1, 0, 2, 1, 4},
214 {375, 1, 0, 2, 1, 5},
215 {450, 1, 0, 2, 1, 6},
216 {525, 1, 0, 2, 1, 7},
217 {600, 1, 0, 2, 1, 8},
218 /* must be the last entry */
219 {0, 0, 0, 0, 0, 0}
220 };
221
222 /* Used for VM356A only */
223
224 static const struct dpi_color_adjust vm356a_dpi_color_adjust[] = {
225
226 /* All values with ydpi > 300 result in a wrong proportion for */
227 /* the scan. The proportion can be adjusted with the following */
228 /* command: convert -geometry (dpi/max_xdpi * 100%)x100% */
229 /* max_xdpi is for the vm356a constant with 300 dpi */
230 /* e.g. 600dpi adjust with: convert -geometry 200%x100% */
231
232 /* All resolutions in increments of 25 are testede, only the shown */
233 /* bring back good results */
234
235 /*dpi, color sequence R G or B, 0 (-) or 1 (+) color skewing, lines skewing */
236 {25, 1, 0, 2, 0, 0},
237 {50, 1, 2, 0, 0, 1},
238 {75, 1, 0, 2, 1, 1},
239 {150, 1, 0, 2, 1, 2},
240 {225, 1, 0, 2, 1, 3},
241 {300, 1, 0, 2, 1, 4},
242 {375, 1, 0, 2, 1, 5},
243 {450, 1, 0, 2, 1, 6},
244 {525, 1, 0, 2, 1, 7},
245 {600, 1, 0, 2, 1, 8},
246
247 /* must be the last entry */
248 {0, 0, 0, 0, 0, 0}
249 };
250
251 /* Used for the VM3575 only */
252
253 static const struct dpi_color_adjust vm3575_dpi_color_adjust[] = {
254
255 /* All values with ydpi > 300 result in a wrong proportion for */
256 /* the scan. The proportion can be adjusted with the following */
257 /* command: convert -geometry (dpi/max_xdpi * 100%)x100% */
258 /* max_xdpi is for the vm3575 constant with 300 dpi */
259 /* e.g. 600dpi adjust with: convert -geometry 200%x100% */
260
261 {50, 2, 0, 1, 1, 1},
262 {60, 2, 1, 0, 0, 2},
263 /* {75, 2, 0, 1, 1, 2}, NOK, effects in scan, also with twain driver*/
264 {100, 2, 1, 0, 0, 3},
265 {120, 2, 0, 1, 1, 3},
266 {150, 2, 0, 1, 1, 4},
267 /* {180, 2, 1, 0, 0, 4}, NOK, skewing problem*/
268 {225, 2, 0, 1, 1, 6},
269 {300, 2, 0, 1, 1, 8},
270 {375, 2, 0, 1, 1, 10},
271 {450, 2, 0, 1, 1, 12},
272 {525, 2, 0, 1, 1, 14},
273 {600, 2, 0, 1, 1, 16},
274
275 /* must be the last entry */
276 {0, 0, 0, 0, 0, 0}
277 };
278
279 static const struct dpi_color_adjust vm656a_dpi_color_adjust[1] = {
280 {0, 0, 1, 2, 0, 0}
281 };
282
283 static const struct dpi_color_adjust vm6575_dpi_color_adjust[] = {
284 {75, 1, 0, 2, 1, 1},
285 {150, 1, 0, 2, 1, 2},
286 {300, 1, 0, 2, 1, 4},
287 {600, 1, 0, 2, 1, 8},
288 };
289
290 static const struct dpi_color_adjust vm6586_dpi_color_adjust[] = {
291 {25, 1, 0, 2, 1, 0},
292 {40, 1, 0, 2, 1, 0},
293 {50, 1, 0, 2, 1, 0},
294 {75, 1, 2, 0, 0, 1},
295 {150, 1, 0, 2, 1, 1},
296 {160, 1, 0, 2, 1, 1},
297 {175, 1, 0, 2, 1, 1},
298 {180, 1, 0, 2, 1, 1},
299 {300, 1, 0, 2, 1, 2},
300 {320, 1, 0, 2, 1, 2},
301 {325, 1, 0, 2, 1, 2},
302 {450, 1, 0, 2, 1, 3},
303 {460, 1, 0, 2, 1, 3},
304 {600, 1, 0, 2, 1, 4},
305 {620, 1, 0, 2, 1, 6},
306 {625, 1, 0, 2, 1, 6},
307 {750, 1, 0, 2, 1, 12},
308 {760, 1, 0, 2, 1, 12},
309 {900, 1, 0, 2, 1, 12},
310 {1050, 1, 0, 2, 1, 15},
311 {1200, 1, 0, 2, 1, 15},
312
313 /* must be the last entry */
314 {0, 0, 0, 0, 0, 0}
315 };
316
317 /* For all scanners. Must be reasonable (eg. between 50 and 300) and
318 * appear in the ...._dpi_color_adjust list of all supported scanners. */
319 #define DEF_RESOLUTION 150
320
321 /*--------------------------------------------------------------------------*/
322
323 /* Define the supported scanners and their characteristics. */
324 static const struct scanners_supported scanners[] = {
325
326 {6, "TECO VM3564",
327 TECO_VM3564,
328 "Relisys", "AVEC II S3",
329 {1, 600, 1}, /* resolution */
330 300, 600, /* max x and Y resolution */
331 2550, 12, 3, 1, /* calibration */
332 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
333 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
334 vm3564_dpi_color_adjust},
335
336 {6, "TECO VM356A",
337 TECO_VM356A,
338 "Relisys", "APOLLO Express 3",
339 {1, 600, 1}, /* resolution */
340 300, 600, /* max x and Y resolution */
341 2550, 12, 3, 1, /* calibration */
342 /* dots/inch * x-length, calibration samples, tot.bytes for 3 colors, default SANE_TECO2_CAL_ALGO value */
343 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
344 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
345 vm356a_dpi_color_adjust},
346
347 {6, "TECO VM356A",
348 TECO_VM356A,
349 "Primax", "Jewel 4800",
350 {1, 600, 1}, /* resolution */
351 300, 600, /* max x and Y resolution */
352 2550, 12, 3, 1, /* calibration */
353 /* dots/inch * x-length, calibration samples, tot.bytes for 3 colors, default SANE_TECO2_CAL_ALGO value */
354 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
355 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
356 vm356a_dpi_color_adjust},
357
358 {6, "TECO VM3575",
359 TECO_VM3575,
360 "Relisys", "SCORPIO Super 3",
361 {1, 600, 1}, /* resolution */
362 300, 600, /* max x and Y resolution */
363 2550, 12, 6, 1, /* calibration */
364 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
365 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
366 vm3575_dpi_color_adjust},
367
368 {6, "TECO VM3575",
369 TECO_VM3575,
370 "Relisys", "AVEC Super 3",
371 {1, 600, 1}, /* resolution */
372 300, 600, /* max x and Y resolution */
373 2550, 12, 6, 1, /* calibration */
374 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
375 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
376 vm3575_dpi_color_adjust},
377
378 {6, "TECO VM656A",
379 TECO_VM656A,
380 "Relisys", "APOLLO Express 6",
381 {1, 600, 1}, /* resolution */
382 600, 1200, /* max x and Y resolution */
383 5100, 8, 6, 0, /* calibration */
384 {SANE_FIX (0), SANE_FIX (210), 0},
385 {SANE_FIX (0), SANE_FIX (297), 0},
386 vm656a_dpi_color_adjust},
387
388 {6, "TECO VM6575",
389 TECO_VM6575,
390 "Relisys", "SCORPIO Pro",
391 {1, 600, 1}, /* resolution */
392 600, 1200, /* max x and Y resolution */
393 5100, 8, 6, 1, /* calibration */
394 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
395 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
396 vm6575_dpi_color_adjust},
397
398 {6, "TECO VM6575",
399 TECO_VM6575,
400 "Primax", "Profi 9600",
401 {1, 600, 1}, /* resolution */
402 600, 1200, /* max x and Y resolution */
403 5100, 8, 6, 1, /* calibration */
404 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
405 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
406 vm6575_dpi_color_adjust},
407
408 {6, "TECO VM6586",
409 TECO_VM6586,
410 "Relisys", "SCORPIO Pro-S",
411 {1, 600, 1}, /* resolution */
412 600, 1200, /* max x and Y resolution */
413 5100, 8, 6, 0, /* calibration */
414 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
415 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
416 vm6586_dpi_color_adjust},
417
418 {6, "TECO VM6586",
419 TECO_VM6586,
420 "Primax", "Profi 19200",
421 {1, 600, 1}, /* resolution */
422 600, 1200, /* max x and Y resolution */
423 5100, 8, 6, 0, /* calibration */
424 {SANE_FIX (0), SANE_FIX (8.5 * MM_PER_INCH), 0},
425 {SANE_FIX (0), SANE_FIX (11.7 * MM_PER_INCH), 0},
426 vm6586_dpi_color_adjust}
427 };
428
429 /*--------------------------------------------------------------------------*/
430
431 /* List of scanner attached. */
432 static Teco_Scanner *first_dev = NULL;
433 static int num_devices = 0;
434 static const SANE_Device **devlist = NULL;
435
436
437 /* Local functions. */
438
439 /* Display a buffer in the log. Display by lines of 16 bytes. */
440 static void
hexdump(int level, const char *comment, unsigned char *buf, const int length)441 hexdump (int level, const char *comment, unsigned char *buf, const int length)
442 {
443 int i;
444 char line[128];
445 char *ptr;
446 char asc_buf[17];
447 char *asc_ptr;
448
449 DBG (level, " %s\n", comment);
450
451 i = 0;
452 goto start;
453
454 do
455 {
456 if (i < length)
457 {
458 ptr += sprintf (ptr, " %2.2x", *buf);
459
460 if (*buf >= 32 && *buf <= 127)
461 {
462 asc_ptr += sprintf (asc_ptr, "%c", *buf);
463 }
464 else
465 {
466 asc_ptr += sprintf (asc_ptr, ".");
467 }
468 }
469 else
470 {
471 /* After the length; do nothing. */
472 ptr += sprintf (ptr, " ");
473 }
474
475 i++;
476 buf++;
477
478 if ((i % 16) == 0)
479 {
480 /* It's a new line */
481 DBG (level, " %s %s\n", line, asc_buf);
482
483 start:
484 ptr = line;
485 *ptr = '\0';
486 asc_ptr = asc_buf;
487 *asc_ptr = '\0';
488
489 ptr += sprintf (ptr, " %3.3d:", i);
490 }
491
492 }
493 while (i < ((length + 15) & ~15));
494 }
495
496 /* Returns the length of the longest string, including the terminating
497 * character. */
498 static size_t
max_string_size(SANE_String_Const strings[])499 max_string_size (SANE_String_Const strings[])
500 {
501 size_t size, max_size = 0;
502 int i;
503
504 for (i = 0; strings[i]; ++i)
505 {
506 size = strlen (strings[i]) + 1;
507 if (size > max_size)
508 {
509 max_size = size;
510 }
511 }
512
513 return max_size;
514 }
515
516 /* Lookup a string list from one array and return its index. */
517 static int
get_string_list_index(SANE_String_Const list[], SANE_String_Const name)518 get_string_list_index (SANE_String_Const list[], SANE_String_Const name)
519 {
520 int index;
521
522 index = 0;
523 while (list[index] != NULL)
524 {
525 if (strcmp (list[index], name) == 0)
526 {
527 return (index);
528 }
529 index++;
530 }
531
532 DBG (DBG_error, "name %s not found in list\n", name);
533
534 assert (0); /* bug in backend, core dump */
535
536 return (-1);
537 }
538
539 /* Initialize a scanner entry. Return an allocated scanner with some
540 * preset values. */
541 static Teco_Scanner *
teco_init(void)542 teco_init (void)
543 {
544 Teco_Scanner *dev;
545
546 DBG (DBG_proc, "teco_init: enter\n");
547
548 /* Allocate a new scanner entry. */
549 dev = malloc (sizeof (Teco_Scanner));
550 if (dev == NULL)
551 {
552 return NULL;
553 }
554
555 memset (dev, 0, sizeof (Teco_Scanner));
556
557 /* Allocate the buffer used to transfer the SCSI data. */
558 dev->buffer_size = 64 * 1024;
559 dev->buffer = malloc (dev->buffer_size);
560 if (dev->buffer == NULL)
561 {
562 free (dev);
563 return NULL;
564 }
565
566 dev->sfd = -1;
567
568 DBG (DBG_proc, "teco_init: exit\n");
569
570 return (dev);
571 }
572
573 /* Closes an open scanner. */
574 static void
teco_close(Teco_Scanner * dev)575 teco_close (Teco_Scanner * dev)
576 {
577 DBG (DBG_proc, "teco_close: enter\n");
578
579 if (dev->sfd != -1)
580 {
581 sanei_scsi_close (dev->sfd);
582 dev->sfd = -1;
583 }
584
585 DBG (DBG_proc, "teco_close: exit\n");
586 }
587
588 /* Frees the memory used by a scanner. */
589 static void
teco_free(Teco_Scanner * dev)590 teco_free (Teco_Scanner * dev)
591 {
592 int i;
593
594 DBG (DBG_proc, "teco_free: enter\n");
595
596 if (dev == NULL)
597 return;
598
599 teco_close (dev);
600 if (dev->devicename)
601 {
602 free (dev->devicename);
603 }
604 if (dev->buffer)
605 {
606 free (dev->buffer);
607 }
608 for (i = 1; i < OPT_NUM_OPTIONS; i++)
609 {
610 if (dev->opt[i].type == SANE_TYPE_STRING && dev->val[i].s)
611 {
612 free (dev->val[i].s);
613 }
614 }
615 if (dev->resolutions_list)
616 free (dev->resolutions_list);
617
618 free (dev);
619
620 DBG (DBG_proc, "teco_free: exit\n");
621 }
622
623 /* Inquiry a device and returns TRUE if is supported. */
624 static int
teco_identify_scanner(Teco_Scanner * dev)625 teco_identify_scanner (Teco_Scanner * dev)
626 {
627 CDB cdb;
628 SANE_Status status;
629 size_t size;
630 int i;
631
632 DBG (DBG_proc, "teco_identify_scanner: enter\n");
633
634 size = 5;
635 MKSCSI_INQUIRY (cdb, size);
636 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
637 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
638 NULL, 0, dev->buffer, &size);
639
640 if (status)
641 {
642 DBG (DBG_error,
643 "teco_identify_scanner: inquiry failed with status %s\n",
644 sane_strstatus (status));
645 return (SANE_FALSE);
646 }
647
648 size = dev->buffer[4] + 5; /* total length of the inquiry data */
649
650 #ifndef sim
651 if (size < 53)
652 {
653 DBG (DBG_error,
654 "teco_identify_scanner: not enough data to identify device\n");
655 return (SANE_FALSE);
656 }
657 #endif
658 #ifdef sim
659 {
660 #if 0
661 /* vm3564 */
662 unsigned char table[] = {
663 0x06, 0x00, 0x02, 0x02, 0x43, 0x00, 0x00, 0x10, 0x52, 0x45,
664 0x4c, 0x49, 0x53, 0x59, 0x53, 0x20, 0x41, 0x56, 0x45, 0x43,
665 0x20, 0x49, 0x49, 0x20, 0x53, 0x33, 0x20, 0x20, 0x20, 0x20,
666 0x20, 0x20, 0x31, 0x2e, 0x30, 0x37, 0x31, 0x2e, 0x30, 0x37,
667 0x00, 0x01, 0x54, 0x45, 0x43, 0x4f, 0x20, 0x56, 0x4d, 0x33,
668 0x35, 0x36, 0x34, 0x20, 0x00, 0x01, 0x01, 0x2c, 0x00, 0x01,
669 0x02, 0x58, 0x09, 0xf6, 0x0d, 0xaf, 0x01, 0x2c, 0x00, 0x08,
670 0x01, 0x00
671 };
672 #endif
673 #if 0
674 /* vm356A */
675 unsigned char table[] = {
676 0x06, 0x00, 0x02, 0x02, 0x43, 0x00, 0x00, 0x10, 0x50, 0x72,
677 0x69, 0x6d, 0x61, 0x78, 0x20, 0x20, 0x4a, 0x65, 0x77, 0x65,
678 0x6c, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
679 0x20, 0x20, 0x31, 0x2e, 0x30, 0x30, 0x31, 0x2e, 0x30, 0x30,
680 0x00, 0x01, 0x54, 0x45, 0x43, 0x4f, 0x20, 0x56, 0x4d, 0x33,
681 0x35, 0x36, 0x41, 0x20, 0x00, 0x01, 0x01, 0x2c, 0x00, 0x01,
682 0x02, 0x58, 0x09, 0xf6, 0x0d, 0xaf, 0x01, 0x2c, 0x00, 0x08,
683 0x10, 0x00
684 };
685 #endif
686 #if 1
687 /* vm3575 */
688 unsigned char table[] = {
689 0x06, 0x00, 0x02, 0x02, 0x43, 0x00, 0x00, 0x00, 0x20, 0x20,
690 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x46, 0x6c, 0x61, 0x74,
691 0x62, 0x65, 0x64, 0x20, 0x53, 0x63, 0x61, 0x6e, 0x6e, 0x65,
692 0x72, 0x20, 0x31, 0x2e, 0x30, 0x33, 0x31, 0x2e, 0x30, 0x33,
693 0x00, 0x01, 0x54, 0x45, 0x43, 0x4f, 0x20, 0x56, 0x4d, 0x33,
694 0x35, 0x37, 0x35, 0x20, 0x00, 0x01, 0x01, 0x2c, 0x00, 0x01,
695 0x02, 0x58, 0x09, 0xf6, 0x0d, 0xaf, 0x01, 0x2c, 0x00, 0x08,
696 0x01, 0x00
697 };
698 #endif
699 #if 0
700 /* vm6586 */
701 unsigned char table[] = {
702 0x06, 0x00, 0x02, 0x02, 0x43, 0x00, 0x00, 0x00, 0x20, 0x20,
703 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x46, 0x6c, 0x61, 0x74,
704 0x62, 0x65, 0x64, 0x20, 0x53, 0x63, 0x61, 0x6e, 0x6e, 0x65,
705 0x72, 0x20, 0x33, 0x2e, 0x30, 0x31, 0x33, 0x2e, 0x30, 0x31,
706 0x00, 0x01, 0x54, 0x45, 0x43, 0x4f, 0x20, 0x56, 0x4d, 0x36,
707 0x35, 0x38, 0x36, 0x20, 0x00, 0x01, 0x01, 0x2c, 0x00, 0x01,
708 0x02, 0x58, 0x09, 0xf6, 0x0d, 0xaf, 0x01, 0x2c, 0x00, 0x08,
709 0x01, 0x00
710 };
711 #endif
712 #if 0
713 /* vm656A */
714 unsigned char table[] = {
715 0x06, 0x00, 0x02, 0x02, 0x43, 0x00, 0x00, 0x00, 0x52, 0x45,
716 0x4c, 0x49, 0x53, 0x59, 0x53, 0x20, 0x41, 0x50, 0x4f, 0x4c,
717 0x4c, 0x4f, 0x20, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73,
718 0x20, 0x36, 0x31, 0x2e, 0x30, 0x33, 0x31, 0x2e, 0x30, 0x33,
719 0x00, 0x01, 0x54, 0x45, 0x43, 0x4f, 0x20, 0x56, 0x4d, 0x36,
720 0x35, 0x36, 0x41, 0x00, 0x01, 0x01, 0x2c, 0x00, 0x01, 0x02,
721 0x58, 0x09, 0xf6, 0x0d, 0xaf, 0x01, 0x2c, 0x00, 0x08, 0x01,
722 0x00, 0x00
723 };
724 #endif
725 size = sizeof (table);
726 memcpy (dev->buffer, table, sizeof (table));
727 }
728 #endif
729
730 MKSCSI_INQUIRY (cdb, size);
731 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
732 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
733 NULL, 0, dev->buffer, &size);
734
735 if (status)
736 {
737 DBG (DBG_error,
738 "teco_identify_scanner: inquiry failed with status %s\n",
739 sane_strstatus (status));
740 return (SANE_FALSE);
741 }
742
743 hexdump (DBG_info2, "inquiry", dev->buffer, size);
744
745 dev->scsi_type = dev->buffer[0] & 0x1f;
746 memcpy (dev->scsi_vendor, dev->buffer + 0x08, 0x08);
747 dev->scsi_vendor[0x08] = 0;
748 memcpy (dev->scsi_product, dev->buffer + 0x10, 0x010);
749 dev->scsi_product[0x10] = 0;
750 memcpy (dev->scsi_version, dev->buffer + 0x20, 0x04);
751 dev->scsi_version[0x04] = 0;
752 memcpy (dev->scsi_teco_name, dev->buffer + 0x2A, 0x0B);
753 dev->scsi_teco_name[0x0B] = 0;
754
755 DBG (DBG_info, "device is \"%s\" \"%s\" \"%s\" \"%s\"\n",
756 dev->scsi_vendor, dev->scsi_product, dev->scsi_version,
757 dev->scsi_teco_name);
758
759 /* Lookup through the supported scanners table to find if this
760 * backend supports that one. */
761 for (i = 0; i < NELEMS (scanners); i++)
762 {
763 if (dev->scsi_type == scanners[i].scsi_type &&
764 strcmp (dev->scsi_teco_name, scanners[i].scsi_teco_name) == 0)
765 {
766
767 DBG (DBG_error, "teco_identify_scanner: scanner supported\n");
768
769 /*patch for VM356A Jewel or Apollo text in frontend-+ others-------------- */
770
771 if (strncmp (dev->scsi_vendor, "RELISYS", 1) == 0 ||
772 strncmp (dev->scsi_vendor, " ", 1) == 0)
773 {
774 DBG (DBG_error,
775 "teco_identify_scanner: scanner detected with this teco_name and first brand/name entry in table\n");
776
777 dev->def = &(scanners[i]);
778
779 return (SANE_TRUE);
780 }
781 else
782 {
783 i++;
784
785 DBG (DBG_error,
786 "teco_identify_scanner: scanner detected with this teco_name and second brand/name entry in table\n");
787
788 dev->def = &(scanners[i]);
789
790 return (SANE_TRUE);
791 }
792
793 }
794 }
795
796 DBG (DBG_proc, "teco_identify_scanner: exit, device not supported\n");
797
798 return (SANE_FALSE);
799 }
800
801 /* Set a window. */
802 static SANE_Status
teco_set_window(Teco_Scanner * dev)803 teco_set_window (Teco_Scanner * dev)
804 {
805 size_t size; /* significant size of window */
806 CDB cdb;
807 unsigned char window[56];
808 SANE_Status status;
809 int i;
810
811 DBG (DBG_proc, "teco_set_window: enter\n");
812
813 /* size of the whole windows block */
814 size = 0; /* keep gcc quiet */
815 switch (dev->def->tecoref)
816 {
817 case TECO_VM3575:
818 size = 53;
819 break;
820 case TECO_VM3564:
821 case TECO_VM356A:
822 case TECO_VM656A:
823 case TECO_VM6575:
824 case TECO_VM6586:
825 size = 56;
826 break;
827 default:
828 assert (0);
829 }
830
831 /* Check that window is big enough */
832 assert (size <= sizeof (window));
833
834 MKSCSI_SET_WINDOW (cdb, size);
835
836 memset (window, 0, size);
837
838 /* size of the windows descriptor block */
839 window[7] = size - 8;
840
841 /* X and Y resolution */
842 Ito16 (dev->x_resolution, &window[10]);
843 Ito16 (dev->y_resolution, &window[12]);
844
845 /* Upper Left (X,Y) */
846 Ito32 (dev->x_tl, &window[14]);
847 Ito32 (dev->y_tl, &window[18]);
848
849 /* Width and length */
850 Ito32 (dev->width, &window[22]);
851 Ito32 (dev->length, &window[26]);
852
853 /* Image Composition */
854 switch (dev->scan_mode)
855 {
856 case TECO_BW:
857 window[31] = dev->val[OPT_THRESHOLD].w;
858 window[33] = 0x00;
859 i = get_string_list_index (dither_list, dev->val[OPT_DITHER].s);
860 window[36] = dither_val[i];
861 break;
862 case TECO_GRAYSCALE:
863 window[33] = 0x02;
864 break;
865 case TECO_COLOR:
866 window[33] = 0x05;
867 break;
868 }
869
870 /* Depth */
871 window[34] = dev->depth;
872
873 /* Lamp to use */
874 i = get_string_list_index (filter_color_list, dev->val[OPT_FILTER_COLOR].s);
875 window[48] = filter_color_val[i];
876
877 /* Unknown - invariants */
878 window[31] = 0x80;
879 window[37] = 0x80;
880
881 /* Set the expected size line and number of lines. */
882 if (dev->def->tecoref == TECO_VM656A ||
883 dev->def->tecoref == TECO_VM6575 || dev->def->tecoref == TECO_VM6586)
884 {
885
886 switch (dev->scan_mode)
887 {
888 case TECO_BW:
889 case TECO_GRAYSCALE:
890 Ito16 (dev->params.bytes_per_line, &window[52]);
891 break;
892 case TECO_COLOR:
893 Ito16 (dev->params.bytes_per_line / 3, &window[52]);
894 break;
895 }
896 Ito16 (dev->params.lines, &window[54]);
897 }
898
899 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
900 hexdump (DBG_info2, "windows", window, size);
901
902 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
903 window, size, NULL, NULL);
904
905 DBG (DBG_proc, "teco_set_window: exit, status=%d\n", status);
906
907 return status;
908 }
909
910 /* Park the CCD */
911 static SANE_Status
teco_reset_window(Teco_Scanner * dev)912 teco_reset_window (Teco_Scanner * dev)
913 {
914 SANE_Status status;
915 CDB cdb;
916
917 DBG (DBG_proc, "teco_reset_window: enter\n");
918
919 MKSCSI_OBJECT_POSITION (cdb, 0);
920
921 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
922 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, NULL, 0, NULL, NULL);
923
924 DBG (DBG_proc, "teco_reset_window: leave, status=%d\n", status);
925
926 return status;
927 }
928
929 #ifdef sim
930 unsigned char *image_buf;
931 int image_buf_begin;
932 #endif
933
934 /* Read the size of the scan. */
935 static SANE_Status
teco_get_scan_size(Teco_Scanner * dev)936 teco_get_scan_size (Teco_Scanner * dev)
937 {
938 size_t size;
939 CDB cdb;
940 SANE_Status status;
941
942 DBG (DBG_proc, "teco_get_scan_size: enter\n");
943
944 size = 0x12;
945 MKSCSI_GET_DATA_BUFFER_STATUS (cdb, 1, size);
946 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
947 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
948 NULL, 0, dev->buffer, &size);
949
950 assert (size == 0x12);
951
952 hexdump (DBG_info2, "teco_get_scan_size return", dev->buffer, size);
953
954 #ifndef sim
955 dev->params.lines = B16TOI (&dev->buffer[12]);
956 dev->bytes_per_raster = B16TOI (&dev->buffer[14]);
957
958 switch (dev->scan_mode)
959 {
960 case TECO_BW:
961 dev->params.bytes_per_line = B16TOI (&dev->buffer[14]);
962 dev->params.pixels_per_line = dev->params.bytes_per_line * 8;
963 break;
964 case TECO_GRAYSCALE:
965 dev->params.pixels_per_line = B16TOI (&dev->buffer[14]);
966 dev->params.bytes_per_line = dev->params.pixels_per_line;
967 break;
968 case TECO_COLOR:
969 dev->params.pixels_per_line = B16TOI (&dev->buffer[14]);
970 dev->params.bytes_per_line = dev->params.pixels_per_line * 3;
971 break;
972 }
973 #else
974 {
975 size_t s1, s2;
976 int fd;
977 char *name;
978
979 switch (dev->def->tecoref)
980 {
981 case TECO_VM3575:
982
983 if (dev->scan_mode == TECO_GRAYSCALE)
984 {
985 dev->params.pixels_per_line = 850;
986 dev->params.bytes_per_line = 850;
987 dev->params.lines = 1170;
988 dev->bytes_per_raster = 850;
989
990 name = "/home/fzago/sane/teco2/vm3575/gr100d-2.raw";
991
992 }
993 else
994 {
995 switch (dev->val[OPT_RESOLUTION].w)
996 {
997 case 50:
998 dev->params.pixels_per_line = 425;
999 dev->params.lines = 585;
1000 name = "/home/fzago/sane/teco2/vm3575/out12-co50.raw";
1001 break;
1002 case 60:
1003 dev->params.pixels_per_line = 510;
1004 dev->params.lines = 702;
1005 name = "/home/fzago/sane/teco2/vm3575/out12-co60.raw";
1006 break;
1007 case 75:
1008 dev->params.pixels_per_line = 638;
1009 dev->params.lines = 431;
1010 name = "/home/fzago/sane/teco2/vm3575/out12-co75.raw";
1011 break;
1012 case 100:
1013 dev->params.pixels_per_line = 402;
1014 dev->params.lines = 575;
1015 name = "/home/fzago/sane/teco2/vm3575/out12-co100.raw";
1016 break;
1017 }
1018
1019 dev->params.bytes_per_line = dev->params.pixels_per_line * 3;
1020 dev->bytes_per_raster = dev->params.pixels_per_line;
1021 }
1022 break;
1023
1024 case TECO_VM6586:
1025 switch (dev->val[OPT_RESOLUTION].w)
1026 {
1027 case 150:
1028 dev->params.pixels_per_line = 297;
1029 dev->params.lines = 296;
1030 name = "/home/fzago/sane/teco2/vm6586/150dpi.raw";
1031 break;
1032 case 600:
1033 dev->params.pixels_per_line = 1186;
1034 dev->params.lines = 1186;
1035 name = "/home/fzago/sane/teco2/vm6586/600dpi.raw";
1036 break;
1037 }
1038
1039 dev->params.bytes_per_line = dev->params.pixels_per_line * 3;
1040 dev->bytes_per_raster = dev->params.pixels_per_line;
1041 }
1042
1043
1044 s1 = dev->params.bytes_per_line * dev->params.lines;
1045 image_buf = malloc (s1);
1046 assert (image_buf);
1047 image_buf_begin = 0;
1048
1049 fd = open (name, O_RDONLY);
1050 assert (fd >= 0);
1051 s2 = read (fd, image_buf, s1);
1052 assert (s1 == s2);
1053 close (fd);
1054
1055 }
1056 #endif
1057
1058 DBG (DBG_proc, "teco_get_scan_size: exit, status=%d\n", status);
1059
1060 return (status);
1061 }
1062
1063 /* Wait until the scanner is ready to send the data. This is the
1064 almost the same code as teco_get_scan_size(). This function has to
1065 be called once after the scan has started. */
1066 static SANE_Status
teco_wait_for_data(Teco_Scanner * dev)1067 teco_wait_for_data (Teco_Scanner * dev)
1068 {
1069 size_t size;
1070 CDB cdb;
1071 SANE_Status status;
1072 int i;
1073
1074 #ifdef sim
1075 return SANE_STATUS_GOOD;
1076 #endif
1077
1078 DBG (DBG_proc, "teco_wait_for_data: enter\n");
1079
1080 for (i = 0; i < 60; i++)
1081 {
1082
1083 size = 0x12;
1084 MKSCSI_GET_DATA_BUFFER_STATUS (cdb, 1, size);
1085
1086 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1087 NULL, 0, dev->buffer, &size);
1088
1089 assert (size == 0x12);
1090
1091 hexdump (DBG_info2, "teco_wait_for_data return", dev->buffer, size);
1092
1093 switch (dev->def->tecoref)
1094 {
1095 case TECO_VM3564:
1096 case TECO_VM356A:
1097 if (dev->buffer[11] > 0x01)
1098 {
1099 return SANE_STATUS_GOOD;
1100 }
1101
1102 sleep (1);
1103 break;
1104 /* For VM3575, VM6575, VM656A, VM6586 until otherwise default value is used */
1105 default:
1106 if (dev->buffer[11] == 0x80)
1107 {
1108 return SANE_STATUS_GOOD;
1109 }
1110
1111 sleep (1);
1112 break;
1113 }
1114 }
1115
1116 DBG (DBG_proc, "teco_wait_for_data: scanner not ready to send data (%d)\n",
1117 status);
1118
1119 return SANE_STATUS_DEVICE_BUSY;
1120 }
1121
1122 /* Do the calibration stuff. Get 12 or 8 lines of data. Each pixel is coded
1123 * in 6 bytes (2 per color) or 3 bytes (3564 and 356A). To do the calibration,
1124 * allocates an array big enough for one line, read the 12 or 8 lines of calibration,
1125 * subtract the highest and lowest value and do a average.
1126 * The input line is 1 raster for each color. However
1127 * the output line is interlaced (ie RBG for the first pixel, then RGB
1128 * for the second, and so on...). The output line are the value to use
1129 * to compensate for the white point.
1130 * There is two algorithms:
1131 *
1132 * The range goes from 0 to 0xfff, and the average is 0x800. So if
1133 * the average input is 0x700, the output value for that dot must be
1134 * 0x1000-0x700=0x900.
1135 *
1136 * and
1137 *
1138 * the calibration needs to be a multiplication factor, to
1139 * compensate for the too strong or too weak pixel in the sensor.
1140 *
1141 * See more info in doc/teco/teco2.txt
1142 *
1143 **/
1144 static SANE_Status
teco_do_calibration(Teco_Scanner * dev)1145 teco_do_calibration (Teco_Scanner * dev)
1146 {
1147 SANE_Status status;
1148 CDB cdb;
1149 size_t size;
1150 int i;
1151 int j;
1152 int colsub0_0, colsub0_1, colsub0_2;
1153 int colsub1_0, colsub1_1, colsub1_2;
1154 int *tmp_buf, *tmp_min_buf, *tmp_max_buf; /* hold the temporary calibration */
1155 size_t tmp_buf_size, tmp_min_buf_size, tmp_max_buf_size;
1156 const char *calibration_algo;
1157 int cal_algo;
1158
1159 colsub0_0 = 0;
1160 colsub0_1 = 0;
1161 colsub0_2 = 0;
1162 colsub1_0 = 0;
1163 colsub1_1 = 0;
1164 colsub1_2 = 0;
1165
1166 DBG (DBG_proc, "teco_do_calibration: enter\n");
1167
1168 /* Get default calibration algorithm. */
1169 cal_algo = dev->def->cal_algo;
1170 if ((calibration_algo = getenv ("SANE_TECO2_CAL_ALGO")) != NULL)
1171 {
1172 cal_algo = atoi (calibration_algo);
1173 }
1174 if (cal_algo != 0 && cal_algo != 1)
1175 {
1176 DBG (DBG_error, "Invalid calibration algorithm (%d)\n", cal_algo);
1177 cal_algo = 0;
1178 }
1179
1180 switch (dev->def->tecoref)
1181 {
1182 case TECO_VM3564:
1183 case TECO_VM356A:
1184 /* white level red, green and blue calibration correction */
1185 /* 0x110 or 272 is middle value */
1186 colsub0_1 = 240 + (dev->val[OPT_WHITE_LEVEL_R].w);
1187 colsub0_2 = 240 + (dev->val[OPT_WHITE_LEVEL_G].w);
1188 colsub0_0 = 240 + (dev->val[OPT_WHITE_LEVEL_B].w);
1189 /* 14000 is middle value */
1190 colsub1_1 = 12720 + (40 * dev->val[OPT_WHITE_LEVEL_R].w);
1191 colsub1_2 = 12720 + (40 * dev->val[OPT_WHITE_LEVEL_G].w);
1192 colsub1_0 = 12720 + (40 * dev->val[OPT_WHITE_LEVEL_B].w);
1193 break;
1194 case TECO_VM3575:
1195 case TECO_VM6575:
1196 /* 0x1100 or 4352 is middle value */
1197 colsub0_1 = 4096 + (8 * dev->val[OPT_WHITE_LEVEL_R].w);
1198 colsub0_2 = 4096 + (8 * dev->val[OPT_WHITE_LEVEL_G].w);
1199 colsub0_0 = 4096 + (8 * dev->val[OPT_WHITE_LEVEL_B].w);
1200 /* 4206639 is middle value */
1201 colsub1_1 = 4078639 + (4000 * dev->val[OPT_WHITE_LEVEL_R].w);
1202 colsub1_2 = 4078639 + (4000 * dev->val[OPT_WHITE_LEVEL_G].w);
1203 colsub1_0 = 4078639 + (4000 * dev->val[OPT_WHITE_LEVEL_B].w);
1204 break;
1205 /* old default value */
1206 case TECO_VM656A:
1207 case TECO_VM6586:
1208 colsub0_1 = 0x1000;
1209 colsub0_2 = 0x1000;
1210 colsub0_0 = 0x1000;
1211 colsub1_1 = 4206639;
1212 colsub1_2 = 4206639;
1213 colsub1_0 = 4206639;
1214 break;
1215 default:
1216 colsub0_1 = 0x1000;
1217 colsub0_2 = 0x1000;
1218 colsub0_0 = 0x1000;
1219 colsub1_1 = 4206639;
1220 colsub1_2 = 4206639;
1221 colsub1_0 = 4206639;
1222 break;
1223 }
1224
1225 tmp_buf_size = dev->def->cal_length * 3 * sizeof (int);
1226 tmp_min_buf_size = dev->def->cal_length * 3 * sizeof (int);
1227 tmp_max_buf_size = dev->def->cal_length * 3 * sizeof (int);
1228 tmp_buf = malloc (tmp_buf_size);
1229 tmp_min_buf = malloc (tmp_min_buf_size);
1230 tmp_max_buf = malloc (tmp_max_buf_size);
1231 memset (tmp_buf, 0, tmp_buf_size);
1232 switch (dev->def->tecoref)
1233 {
1234 case TECO_VM3564:
1235 case TECO_VM356A:
1236 memset (tmp_min_buf, 0xff, tmp_min_buf_size);
1237 memset (tmp_max_buf, 0x00, tmp_max_buf_size);
1238 break;
1239 case TECO_VM3575:
1240 case TECO_VM656A:
1241 case TECO_VM6575:
1242 case TECO_VM6586:
1243 memset (tmp_min_buf, 0xffff, tmp_min_buf_size);
1244 memset (tmp_max_buf, 0x0000, tmp_max_buf_size);
1245 break;
1246 }
1247
1248 if ((tmp_buf == NULL) || (tmp_min_buf == NULL) || (tmp_max_buf == NULL))
1249 {
1250 DBG (DBG_proc, "teco_do_calibration: not enough memory (%ld bytes)\n",
1251 (long) tmp_buf_size);
1252 return (SANE_STATUS_NO_MEM);
1253 }
1254
1255 for (i = 0; i < dev->def->cal_lines; i++)
1256 {
1257
1258 MKSCSI_VENDOR_SPEC (cdb, SCSI_VENDOR_09, 6);
1259
1260 /* No idea what that is. */
1261 switch (dev->scan_mode)
1262 {
1263 case TECO_BW:
1264 cdb.data[2] = 0x02;
1265 break;
1266 case TECO_GRAYSCALE:
1267 cdb.data[2] = 0x01;
1268 break;
1269 case TECO_COLOR:
1270 cdb.data[2] = 0x00;
1271 break;
1272 }
1273
1274 /* Length of the scanner * number of bytes per color */
1275 size = dev->def->cal_length * dev->def->cal_col_len;
1276 cdb.data[3] = (size >> 8) & 0xff;
1277 cdb.data[4] = (size >> 0) & 0xff;
1278
1279 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
1280 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1281 NULL, 0, dev->buffer, &size);
1282
1283 if (status != SANE_STATUS_GOOD)
1284 {
1285 DBG (DBG_error,
1286 "teco_do_calibration: cannot read from the scanner\n");
1287 free (tmp_buf);
1288 return status;
1289 }
1290
1291 #ifdef sim
1292 for (j = 0; j < dev->def->cal_length; j++)
1293 {
1294 dev->buffer[6 * j + 0] = 0x10;
1295 dev->buffer[6 * j + 1] = 0x12;
1296 dev->buffer[6 * j + 2] = 0x14;
1297 dev->buffer[6 * j + 3] = 0x16;
1298 dev->buffer[6 * j + 4] = 0x20;
1299 dev->buffer[6 * j + 5] = 0x25;
1300 }
1301 #endif
1302 /*hexdump (DBG_info2, "got calibration line:", dev->buffer, size); */
1303
1304 for (j = 0; j < dev->def->cal_length; j++)
1305 {
1306 switch (dev->def->tecoref)
1307 {
1308 case TECO_VM3575:
1309 case TECO_VM6575:
1310 case TECO_VM656A:
1311 case TECO_VM6586:
1312 tmp_buf[3 * j + 0] +=
1313 (dev->buffer[6 * j + 1] << 8) + dev->buffer[6 * j + 0];
1314 /* get lowest value */
1315 if (tmp_min_buf[3 * j + 0] >> ((dev->buffer[6 * j + 1] << 8) + dev->buffer[6 * j + 0]))
1316 {
1317 tmp_min_buf[3 * j + 0] = (dev->buffer[6 * j + 1] << 8) + dev->buffer[6 * j + 0];
1318 }
1319 /* get highest value */
1320 if (tmp_max_buf[3 * j + 0] < ((dev->buffer[6 * j + 1] << 8) + dev->buffer[6 * j + 0]))
1321 {
1322 tmp_max_buf[3 * j + 0] = (dev->buffer[6 * j + 1] << 8) + dev->buffer[6 * j + 0];
1323 }
1324 tmp_buf[3 * j + 1] +=
1325 (dev->buffer[6 * j + 3] << 8) + dev->buffer[6 * j + 2];
1326 /* get lowest value */
1327 if (tmp_min_buf[3 * j + 1] >> ((dev->buffer[6 * j + 3] << 8) + dev->buffer[6 * j + 2]))
1328 {
1329 tmp_min_buf[3 * j + 1] = (dev->buffer[6 * j + 3] << 8) + dev->buffer[6 * j + 2];
1330 }
1331 /* get highest value */
1332 if (tmp_max_buf[3 * j + 1] < ((dev->buffer[6 * j + 3] << 8) + dev->buffer[6 * j + 2]))
1333 {
1334 tmp_max_buf[3 * j + 1] = (dev->buffer[6 * j + 3] << 8) + dev->buffer[6 * j + 2];
1335 }
1336 tmp_buf[3 * j + 2] +=
1337 (dev->buffer[6 * j + 5] << 8) + dev->buffer[6 * j + 4];
1338 /* get lowest value */
1339 if (tmp_min_buf[3 * j + 2] >> ((dev->buffer[6 * j + 5] << 8) + dev->buffer[6 * j + 4]))
1340 {
1341 tmp_min_buf[3 * j + 2] = (dev->buffer[6 * j + 5] << 8) + dev->buffer[6 * j + 4];
1342 }
1343 /* get highest value */
1344 if (tmp_max_buf[3 * j + 2] < ((dev->buffer[6 * j + 5] << 8) + dev->buffer[6 * j + 4]))
1345 {
1346 tmp_max_buf[3 * j + 2] = (dev->buffer[6 * j + 5] << 8) + dev->buffer[6 * j + 4];
1347 }
1348 break;
1349 case TECO_VM3564:
1350 case TECO_VM356A:
1351 tmp_buf[3 * j + 0] += dev->buffer[3 * j + 0];
1352 /* get lowest value */
1353 if (tmp_min_buf[3 * j + 0] >> dev->buffer[3 * j + 0])
1354 {
1355 tmp_min_buf[3 * j + 0] = dev->buffer[3 * j + 0];
1356 }
1357 /* get highest value */
1358 if (tmp_max_buf[3 * j + 0] < dev->buffer[3 * j + 0])
1359 {
1360 tmp_max_buf[3 * j + 0] = dev->buffer[3 * j + 0];
1361 }
1362 tmp_buf[3 * j + 1] += dev->buffer[3 * j + 1];
1363 /* get lowest value */
1364 if (tmp_min_buf[3 * j + 1] >> dev->buffer[3 * j + 1])
1365 {
1366 tmp_min_buf[3 * j + 1] = dev->buffer[3 * j + 1];
1367 }
1368 /* get highest value */
1369 if (tmp_max_buf[3 * j + 1] < dev->buffer[3 * j + 1])
1370 {
1371 tmp_max_buf[3 * j + 1] = dev->buffer[3 * j + 1];
1372 }
1373 tmp_buf[3 * j + 2] += dev->buffer[3 * j + 2];
1374 /* get lowest value */
1375 if (tmp_min_buf[3 * j + 2] >> dev->buffer[3 * j + 2])
1376 {
1377 tmp_min_buf[3 * j + 2] = dev->buffer[3 * j + 2];
1378 }
1379 /* get highest value */
1380 if (tmp_max_buf[3 * j + 2] < dev->buffer[3 * j + 2])
1381 {
1382 tmp_max_buf[3 * j + 2] = dev->buffer[3 * j + 2];
1383 }
1384 break;
1385 /* default:
1386 tmp_buf[3 * j + 0] +=
1387 (dev->buffer[6 * j + 1] << 8) + dev->buffer[6 * j + 0];
1388 tmp_buf[3 * j + 1] +=
1389 (dev->buffer[6 * j + 3] << 8) + dev->buffer[6 * j + 2];
1390 tmp_buf[3 * j + 2] +=
1391 (dev->buffer[6 * j + 5] << 8) + dev->buffer[6 * j + 4];
1392 break; */
1393 }
1394 }
1395 }
1396
1397 /* hexdump (DBG_info2, "calibration before average:", tmp_buf, tmp_buf_size); */
1398 /* hexdump (DBG_info2, "calibration before average min value:", tmp_min_buf, tmp_min_buf_size); */
1399 /* hexdump (DBG_info2, "calibration before average max value:", tmp_max_buf, tmp_max_buf_size); */
1400
1401 /* Do the average. Since we got 12 or 8 lines, divide all values by 10 or 6
1402 * and create the final calibration value that compensates for the
1403 * white values read. */
1404 switch (dev->def->tecoref)
1405 {
1406 case TECO_VM356A:
1407 case TECO_VM3564:
1408 case TECO_VM3575:
1409 case TECO_VM6575:
1410 case TECO_VM656A:
1411 case TECO_VM6586:
1412 for (j = 0; j < dev->def->cal_length; j++)
1413 {
1414 /* subtract lowest and highest value */
1415 tmp_buf[j] = tmp_buf[j] - (tmp_min_buf[j] + tmp_max_buf[j]);
1416 tmp_buf[j + dev->def->cal_length] = tmp_buf[j + dev->def->cal_length]
1417 - (tmp_min_buf[j + dev->def->cal_length]
1418 + tmp_max_buf[j + dev->def->cal_length]);
1419 tmp_buf[j + 2 * dev->def->cal_length] = tmp_buf[j + 2 * dev->def->cal_length]
1420 - (tmp_min_buf[j + 2 * dev->def->cal_length]
1421 + tmp_max_buf[j + 2 *dev->def->cal_length]);
1422 /* sequence colors first color row one then two and last three */
1423 if (cal_algo == 1)
1424 {
1425 tmp_buf[j] = (colsub1_0 * (dev->def->cal_lines - 2)) / tmp_buf[j];
1426 tmp_buf[j + dev->def->cal_length] = (colsub1_1 * (dev->def->cal_lines - 2))
1427 / tmp_buf[j + dev->def->cal_length];
1428 tmp_buf[j + 2 * dev->def->cal_length] = (colsub1_2 * (dev->def->cal_lines - 2))
1429 / tmp_buf[j + 2 * dev->def->cal_length];
1430 }
1431 else
1432 {
1433 tmp_buf[j] = colsub0_0 - (tmp_buf[j] / (dev->def->cal_lines - 2));
1434 tmp_buf[j + dev->def->cal_length] = colsub0_1 - (tmp_buf[j + dev->def->cal_length]
1435 / (dev->def->cal_lines - 2));
1436 tmp_buf[j + 2 * dev->def->cal_length] = colsub0_2
1437 - (tmp_buf[j + 2 * dev->def->cal_length] / (dev->def->cal_lines - 2));
1438 }
1439 }
1440 break;
1441 /* default:
1442 for (j = 0; j < (3 * dev->def->cal_length); j++)
1443 {
1444 if (cal_algo == 1)
1445 tmp_buf[j] = (colsub1_1 * dev->def->cal_lines) / tmp_buf[j];
1446 else
1447 tmp_buf[j] = colsub0_1 - (tmp_buf[j] / dev->def->cal_lines);
1448 }
1449 break; */
1450 }
1451
1452 /*hexdump (DBG_info2, "calibration after average:", tmp_buf, tmp_buf_size); */
1453
1454 /* Build the calibration line to send. */
1455 for (j = 0; j < dev->def->cal_length; j++)
1456 {
1457
1458 switch (dev->def->tecoref)
1459 {
1460 case TECO_VM3575:
1461 case TECO_VM6575:
1462 case TECO_VM656A:
1463 case TECO_VM6586:
1464 dev->buffer[6 * j + 0] =
1465 (tmp_buf[0 * dev->def->cal_length + j] >> 0) & 0xff;
1466 dev->buffer[6 * j + 1] =
1467 (tmp_buf[0 * dev->def->cal_length + j] >> 8) & 0xff;
1468
1469 dev->buffer[6 * j + 2] =
1470 (tmp_buf[1 * dev->def->cal_length + j] >> 0) & 0xff;
1471 dev->buffer[6 * j + 3] =
1472 (tmp_buf[1 * dev->def->cal_length + j] >> 8) & 0xff;
1473
1474 dev->buffer[6 * j + 4] =
1475 (tmp_buf[2 * dev->def->cal_length + j] >> 0) & 0xff;
1476 dev->buffer[6 * j + 5] =
1477 (tmp_buf[2 * dev->def->cal_length + j] >> 8) & 0xff;
1478 break;
1479 case TECO_VM3564:
1480 case TECO_VM356A:
1481 dev->buffer[3 * j + 0] = (tmp_buf[3 * j + 0] >> 0) & 0xff;
1482
1483 dev->buffer[3 * j + 1] = (tmp_buf[3 * j + 1] >> 0) & 0xff;
1484
1485 dev->buffer[3 * j + 2] = (tmp_buf[3 * j + 2] >> 0) & 0xff;
1486 break;
1487 }
1488 }
1489
1490 free (tmp_buf);
1491 tmp_buf = NULL;
1492
1493 /* Send the calibration line. The CDB is the same as the previous
1494 * one, except for the command. */
1495
1496 cdb.data[0] = 0x0E;
1497 size = dev->def->cal_length * dev->def->cal_col_len;
1498
1499 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
1500 /*hexdump (DBG_info2, "calibration line sent:", dev->buffer, size); */
1501 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1502 dev->buffer, size, NULL, NULL);
1503
1504 if (status != SANE_STATUS_GOOD)
1505 {
1506 DBG (DBG_error,
1507 "teco_do_calibration: calibration line was not sent correctly\n");
1508 return status;
1509 }
1510
1511 DBG (DBG_proc, "teco_do_calibration: leave\n");
1512
1513 return SANE_STATUS_GOOD;
1514 }
1515
1516 /*------------request sense command 03----------------*/
1517 static SANE_Status
teco_request_sense(Teco_Scanner * dev)1518 teco_request_sense (Teco_Scanner * dev)
1519 {
1520 SANE_Status status;
1521 unsigned char buf[255];
1522 CDB cdb;
1523 size_t size;
1524 /* size = 0; */
1525
1526 DBG (DBG_proc, "teco_request_sense: enter\n");
1527
1528 size = sizeof (buf);
1529 MKSCSI_REQUEST_SENSE (cdb, size);
1530
1531 /*size = cdb.data[5];
1532
1533 hexdump (DBG_info2, "teco_request_sense", cdb.data, cdb.len);
1534
1535 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1536 NULL, 0, dev->buffer, &size); */
1537 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1538 NULL, 0, buf, &size);
1539
1540 hexdump (DBG_info2, "teco_request_sense:", buf, size);
1541
1542 DBG (DBG_proc, "teco_request_sense: exit, status=%d\n", status);
1543
1544 return (status);
1545 }
1546
1547 /*----------------------------------------------------*/
1548
1549 /* Send the gamma */
1550 static SANE_Status
teco_send_gamma(Teco_Scanner * dev)1551 teco_send_gamma (Teco_Scanner * dev)
1552 {
1553 CDB cdb;
1554 SANE_Status status;
1555 struct
1556 {
1557 unsigned char gamma_R[GAMMA_LENGTH];
1558 unsigned char gamma_G[GAMMA_LENGTH]; /* also gray */
1559 unsigned char gamma_B[GAMMA_LENGTH];
1560 }
1561 param;
1562 size_t i;
1563 size_t size;
1564
1565 DBG (DBG_proc, "teco_send_gamma: enter\n");
1566
1567 size = sizeof (param);
1568 assert (size == 3 * GAMMA_LENGTH);
1569 MKSCSI_SEND_10 (cdb, 0x03, 0x04, size);
1570
1571 if (dev->val[OPT_CUSTOM_GAMMA].w)
1572 {
1573 /* Use the custom gamma. */
1574 if (dev->scan_mode == TECO_GRAYSCALE)
1575 {
1576 /* Gray */
1577 for (i = 0; i < GAMMA_LENGTH; i++)
1578 {
1579 param.gamma_R[i] = dev->gamma_GRAY[i];
1580 param.gamma_G[i] = dev->gamma_GRAY[i];
1581 param.gamma_B[i] = dev->gamma_GRAY[i];
1582 }
1583 }
1584 else
1585 {
1586 /* Color */
1587 for (i = 0; i < GAMMA_LENGTH; i++)
1588 {
1589 param.gamma_R[i] = dev->gamma_R[i];
1590 param.gamma_G[i] = dev->gamma_G[i];
1591 param.gamma_B[i] = dev->gamma_B[i];
1592 }
1593 }
1594 }
1595 else
1596 {
1597 /* Defaults to a linear gamma for now. */
1598 for (i = 0; i < GAMMA_LENGTH; i++)
1599 {
1600 param.gamma_R[i] = i / 4;
1601 param.gamma_G[i] = i / 4;
1602 param.gamma_B[i] = i / 4;
1603 }
1604 }
1605
1606 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
1607 switch (dev->def->tecoref)
1608 {
1609 case TECO_VM3564:
1610 case TECO_VM356A:
1611 status = 0;
1612 break;
1613 default:
1614 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1615 ¶m, size, NULL, NULL);
1616 }
1617 DBG (DBG_proc, "teco_send_gamma: exit, status=%d\n", status);
1618
1619 return (status);
1620 }
1621
1622 /* Send a vendor specific command. */
1623 static SANE_Status
teco_send_vendor_06(Teco_Scanner * dev)1624 teco_send_vendor_06 (Teco_Scanner * dev)
1625 {
1626 SANE_Status status;
1627 CDB cdb;
1628 size_t size;
1629
1630 DBG (DBG_proc, "teco_send_vendor_06: enter\n");
1631
1632 MKSCSI_VENDOR_SPEC (cdb, SCSI_VENDOR_06, 6);
1633
1634 size = 4;
1635
1636 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
1637 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1638 NULL, 0, dev->buffer, &size);
1639
1640
1641 MKSCSI_VENDOR_SPEC (cdb, SCSI_VENDOR_1C, 6);
1642
1643 size = 4;
1644 memset (dev->buffer, 0, size);
1645
1646 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
1647 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
1648 dev->buffer, size, NULL, NULL);
1649
1650 DBG (DBG_proc, "teco_send_vendor_06: exit, status=%d\n", status);
1651
1652 return (status);
1653 }
1654
1655 /* Start a scan. */
1656 static SANE_Status
teco_scan(Teco_Scanner * dev)1657 teco_scan (Teco_Scanner * dev)
1658 {
1659 CDB cdb;
1660 SANE_Status status;
1661
1662 DBG (DBG_proc, "teco_scan: enter\n");
1663
1664 MKSCSI_SCAN (cdb);
1665
1666 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
1667 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len, NULL, 0, NULL, NULL);
1668
1669 DBG (DBG_proc, "teco_scan: exit, status=%d\n", status);
1670
1671 return status;
1672 }
1673
1674 /* SCSI sense handler. Callback for SANE. */
1675 static SANE_Status
teco_sense_handler(int scsi_fd, unsigned char *result, void __sane_unused__ *arg)1676 teco_sense_handler (int scsi_fd, unsigned char *result, void __sane_unused__ *arg)
1677 {
1678 int asc, ascq, sensekey;
1679 int len;
1680
1681 DBG (DBG_proc, "teco_sense_handler (scsi_fd = %d)\n", scsi_fd);
1682
1683 sensekey = get_RS_sense_key (result);
1684 len = 7 + get_RS_additional_length (result);
1685
1686 hexdump (DBG_info2, "sense", result, len);
1687
1688 if (get_RS_error_code (result) != 0x70)
1689 {
1690 DBG (DBG_error,
1691 "teco_sense_handler: invalid sense key error code (%d)\n",
1692 get_RS_error_code (result));
1693
1694 return SANE_STATUS_IO_ERROR;
1695 }
1696
1697 if (get_RS_ILI (result) != 0)
1698 {
1699 DBG (DBG_sense, "teco_sense_handler: short read\n");
1700 }
1701
1702 if (len < 14)
1703 {
1704 DBG (DBG_error, "teco_sense_handler: sense too short, no ASC/ASCQ\n");
1705
1706 return SANE_STATUS_IO_ERROR;
1707 }
1708
1709 asc = get_RS_ASC (result);
1710 ascq = get_RS_ASCQ (result);
1711
1712 DBG (DBG_sense, "teco_sense_handler: sense=%d, ASC/ASCQ=%02x%02x\n",
1713 sensekey, asc, ascq);
1714
1715 switch (sensekey)
1716 {
1717 case 0x00: /* no sense */
1718 case 0x02: /* not ready */
1719 case 0x03: /* medium error */
1720 case 0x05:
1721 case 0x06:
1722 break;
1723 }
1724
1725 DBG (DBG_sense,
1726 "teco_sense_handler: unknown error condition. Please report it to the backend maintainer\n");
1727
1728 return SANE_STATUS_IO_ERROR;
1729 }
1730
1731 /* Attach a scanner to this backend. */
1732 static SANE_Status
attach_scanner(const char *devicename, Teco_Scanner ** devp)1733 attach_scanner (const char *devicename, Teco_Scanner ** devp)
1734 {
1735 Teco_Scanner *dev;
1736 int sfd;
1737
1738 DBG (DBG_sane_proc, "attach_scanner: %s\n", devicename);
1739
1740 if (devp)
1741 *devp = NULL;
1742
1743 /* Check if we know this device name. */
1744 for (dev = first_dev; dev; dev = dev->next)
1745 {
1746 if (strcmp (dev->sane.name, devicename) == 0)
1747 {
1748 if (devp)
1749 {
1750 *devp = dev;
1751 }
1752 DBG (DBG_info, "device is already known\n");
1753 return SANE_STATUS_GOOD;
1754 }
1755 }
1756
1757 /* Allocate a new scanner entry. */
1758 dev = teco_init ();
1759 if (dev == NULL)
1760 {
1761 DBG (DBG_error, "ERROR: not enough memory\n");
1762 return SANE_STATUS_NO_MEM;
1763 }
1764
1765 DBG (DBG_info, "attach_scanner: opening %s\n", devicename);
1766
1767 if (sanei_scsi_open (devicename, &sfd, teco_sense_handler, dev) != 0)
1768 {
1769 DBG (DBG_error, "ERROR: attach_scanner: open failed\n");
1770 teco_free (dev);
1771 return SANE_STATUS_INVAL;
1772 }
1773
1774 #ifdef sim
1775 sfd = -1;
1776 #endif
1777
1778 /* Fill some scanner specific values. */
1779 dev->devicename = strdup (devicename);
1780 dev->sfd = sfd;
1781
1782 /* Now, check that it is a scanner we support. */
1783 if (teco_identify_scanner (dev) == SANE_FALSE)
1784 {
1785 DBG (DBG_error,
1786 "ERROR: attach_scanner: scanner-identification failed\n");
1787 teco_free (dev);
1788 return SANE_STATUS_INVAL;
1789 }
1790
1791 teco_close (dev);
1792
1793 /* If this scanner only supports a given number of resolutions,
1794 * build that list now. */
1795 if (dev->def->color_adjust[0].resolution != 0)
1796 {
1797 int num_entries;
1798 int i;
1799
1800 num_entries = 0;
1801 while (dev->def->color_adjust[num_entries].resolution != 0)
1802 num_entries++;
1803
1804 dev->resolutions_list = malloc (sizeof (SANE_Word) * (num_entries + 1));
1805
1806 if (dev->resolutions_list == NULL)
1807 {
1808 DBG (DBG_error,
1809 "ERROR: attach_scanner: scanner-identification failed\n");
1810 teco_free (dev);
1811 return SANE_STATUS_NO_MEM;
1812 }
1813
1814 dev->resolutions_list[0] = num_entries;
1815 for (i = 0; i < num_entries; i++)
1816 {
1817 dev->resolutions_list[i + 1] = dev->def->color_adjust[i].resolution;
1818 }
1819 }
1820 else
1821 {
1822 dev->resolutions_list = NULL;
1823 }
1824
1825 /* Set the default options for that scanner. */
1826 dev->sane.name = dev->devicename;
1827 dev->sane.vendor = dev->def->real_vendor;
1828 dev->sane.model = dev->def->real_product;
1829 dev->sane.type = SANE_I18N ("flatbed scanner");
1830
1831 /* Link the scanner with the others. */
1832 dev->next = first_dev;
1833 first_dev = dev;
1834
1835 if (devp)
1836 {
1837 *devp = dev;
1838 }
1839
1840 num_devices++;
1841
1842 DBG (DBG_proc, "attach_scanner: exit\n");
1843
1844 return SANE_STATUS_GOOD;
1845 }
1846
1847 static SANE_Status
attach_one(const char *dev)1848 attach_one (const char *dev)
1849 {
1850 attach_scanner (dev, NULL);
1851 return SANE_STATUS_GOOD;
1852 }
1853
1854 /* Reset the options for that scanner. */
1855 static void
teco_init_options(Teco_Scanner * dev)1856 teco_init_options (Teco_Scanner * dev)
1857 {
1858 int i;
1859
1860 /* Pre-initialize the options. */
1861 memset (dev->opt, 0, sizeof (dev->opt));
1862 memset (dev->val, 0, sizeof (dev->val));
1863
1864 for (i = 0; i < OPT_NUM_OPTIONS; ++i)
1865 {
1866 dev->opt[i].size = sizeof (SANE_Word);
1867 dev->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
1868 }
1869
1870 /* Number of options. */
1871 dev->opt[OPT_NUM_OPTS].name = "";
1872 dev->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
1873 dev->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
1874 dev->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
1875 dev->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
1876 dev->val[OPT_NUM_OPTS].w = OPT_NUM_OPTIONS;
1877
1878 /* Mode group */
1879 dev->opt[OPT_MODE_GROUP].title = SANE_I18N ("Scan Mode");
1880 dev->opt[OPT_MODE_GROUP].desc = ""; /* not valid for a group */
1881 dev->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
1882 dev->opt[OPT_MODE_GROUP].cap = 0;
1883 dev->opt[OPT_MODE_GROUP].size = 0;
1884 dev->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1885
1886 /* Scanner supported modes */
1887 dev->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
1888 dev->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
1889 dev->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
1890 dev->opt[OPT_MODE].type = SANE_TYPE_STRING;
1891 dev->opt[OPT_MODE].size = max_string_size (scan_mode_list);
1892 dev->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
1893 dev->opt[OPT_MODE].constraint.string_list = scan_mode_list;
1894 dev->val[OPT_MODE].s = (SANE_Char *) strdup (""); /* will be set later */
1895
1896 /* X and Y resolution */
1897 dev->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
1898 dev->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
1899 dev->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
1900 dev->opt[OPT_RESOLUTION].type = SANE_TYPE_INT;
1901 dev->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
1902 dev->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
1903 dev->opt[OPT_RESOLUTION].constraint.range = &dev->def->res_range;
1904 dev->val[OPT_RESOLUTION].w = DEF_RESOLUTION;
1905
1906 /* Geometry group */
1907 dev->opt[OPT_GEOMETRY_GROUP].title = SANE_I18N ("Geometry");
1908 dev->opt[OPT_GEOMETRY_GROUP].desc = ""; /* not valid for a group */
1909 dev->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
1910 dev->opt[OPT_GEOMETRY_GROUP].cap = 0;
1911 dev->opt[OPT_GEOMETRY_GROUP].size = 0;
1912 dev->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1913
1914 /* Upper left X */
1915 dev->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
1916 dev->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
1917 dev->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
1918 dev->opt[OPT_TL_X].type = SANE_TYPE_FIXED;
1919 dev->opt[OPT_TL_X].unit = SANE_UNIT_MM;
1920 dev->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
1921 dev->opt[OPT_TL_X].constraint.range = &dev->def->x_range;
1922 dev->val[OPT_TL_X].w = dev->def->x_range.min;
1923
1924 /* Upper left Y */
1925 dev->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
1926 dev->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
1927 dev->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
1928 dev->opt[OPT_TL_Y].type = SANE_TYPE_FIXED;
1929 dev->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
1930 dev->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
1931 dev->opt[OPT_TL_Y].constraint.range = &dev->def->y_range;
1932 dev->val[OPT_TL_Y].w = dev->def->y_range.min;
1933
1934 /* Bottom-right x */
1935 dev->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
1936 dev->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
1937 dev->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
1938 dev->opt[OPT_BR_X].type = SANE_TYPE_FIXED;
1939 dev->opt[OPT_BR_X].unit = SANE_UNIT_MM;
1940 dev->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
1941 dev->opt[OPT_BR_X].constraint.range = &dev->def->x_range;
1942 dev->val[OPT_BR_X].w = dev->def->x_range.max;
1943
1944 /* Bottom-right y */
1945 dev->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
1946 dev->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
1947 dev->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
1948 dev->opt[OPT_BR_Y].type = SANE_TYPE_FIXED;
1949 dev->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
1950 dev->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
1951 dev->opt[OPT_BR_Y].constraint.range = &dev->def->y_range;
1952 dev->val[OPT_BR_Y].w = dev->def->y_range.max;
1953
1954 /* Enhancement group */
1955 dev->opt[OPT_ENHANCEMENT_GROUP].title = SANE_I18N ("Enhancement");
1956 dev->opt[OPT_ENHANCEMENT_GROUP].desc = ""; /* not valid for a group */
1957 dev->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
1958 dev->opt[OPT_ENHANCEMENT_GROUP].cap = SANE_CAP_ADVANCED;
1959 dev->opt[OPT_ENHANCEMENT_GROUP].size = 0;
1960 dev->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
1961
1962 /* Halftone pattern */
1963 dev->opt[OPT_DITHER].name = "dither";
1964 dev->opt[OPT_DITHER].title = SANE_I18N ("Dither");
1965 dev->opt[OPT_DITHER].desc = SANE_I18N ("Dither");
1966 dev->opt[OPT_DITHER].type = SANE_TYPE_STRING;
1967 dev->opt[OPT_DITHER].size = max_string_size (dither_list);
1968 dev->opt[OPT_DITHER].cap |= SANE_CAP_INACTIVE;
1969 dev->opt[OPT_DITHER].constraint_type = SANE_CONSTRAINT_STRING_LIST;
1970 dev->opt[OPT_DITHER].constraint.string_list = dither_list;
1971 dev->val[OPT_DITHER].s = strdup (dither_list[0]);
1972
1973 /* custom-gamma table */
1974 dev->opt[OPT_CUSTOM_GAMMA].name = SANE_NAME_CUSTOM_GAMMA;
1975 dev->opt[OPT_CUSTOM_GAMMA].title = SANE_TITLE_CUSTOM_GAMMA;
1976 dev->opt[OPT_CUSTOM_GAMMA].desc = SANE_DESC_CUSTOM_GAMMA;
1977 dev->opt[OPT_CUSTOM_GAMMA].type = SANE_TYPE_BOOL;
1978 dev->opt[OPT_CUSTOM_GAMMA].cap |= SANE_CAP_INACTIVE;
1979 dev->val[OPT_CUSTOM_GAMMA].w = SANE_FALSE;
1980
1981 /* red gamma vector */
1982 dev->opt[OPT_GAMMA_VECTOR_R].name = SANE_NAME_GAMMA_VECTOR_R;
1983 dev->opt[OPT_GAMMA_VECTOR_R].title = SANE_TITLE_GAMMA_VECTOR_R;
1984 dev->opt[OPT_GAMMA_VECTOR_R].desc = SANE_DESC_GAMMA_VECTOR_R;
1985 dev->opt[OPT_GAMMA_VECTOR_R].type = SANE_TYPE_INT;
1986 dev->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
1987 dev->opt[OPT_GAMMA_VECTOR_R].unit = SANE_UNIT_NONE;
1988 dev->opt[OPT_GAMMA_VECTOR_R].size = GAMMA_LENGTH * sizeof (SANE_Word);
1989 dev->opt[OPT_GAMMA_VECTOR_R].constraint_type = SANE_CONSTRAINT_RANGE;
1990 dev->opt[OPT_GAMMA_VECTOR_R].constraint.range = &gamma_range;
1991 dev->val[OPT_GAMMA_VECTOR_R].wa = dev->gamma_R;
1992
1993 /* green and gamma vector */
1994 dev->opt[OPT_GAMMA_VECTOR_G].name = SANE_NAME_GAMMA_VECTOR_G;
1995 dev->opt[OPT_GAMMA_VECTOR_G].title = SANE_TITLE_GAMMA_VECTOR_G;
1996 dev->opt[OPT_GAMMA_VECTOR_G].desc = SANE_DESC_GAMMA_VECTOR_G;
1997 dev->opt[OPT_GAMMA_VECTOR_G].type = SANE_TYPE_INT;
1998 dev->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
1999 dev->opt[OPT_GAMMA_VECTOR_G].unit = SANE_UNIT_NONE;
2000 dev->opt[OPT_GAMMA_VECTOR_G].size = GAMMA_LENGTH * sizeof (SANE_Word);
2001 dev->opt[OPT_GAMMA_VECTOR_G].constraint_type = SANE_CONSTRAINT_RANGE;
2002 dev->opt[OPT_GAMMA_VECTOR_G].constraint.range = &gamma_range;
2003 dev->val[OPT_GAMMA_VECTOR_G].wa = dev->gamma_G;
2004
2005 /* blue gamma vector */
2006 dev->opt[OPT_GAMMA_VECTOR_B].name = SANE_NAME_GAMMA_VECTOR_B;
2007 dev->opt[OPT_GAMMA_VECTOR_B].title = SANE_TITLE_GAMMA_VECTOR_B;
2008 dev->opt[OPT_GAMMA_VECTOR_B].desc = SANE_DESC_GAMMA_VECTOR_B;
2009 dev->opt[OPT_GAMMA_VECTOR_B].type = SANE_TYPE_INT;
2010 dev->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
2011 dev->opt[OPT_GAMMA_VECTOR_B].unit = SANE_UNIT_NONE;
2012 dev->opt[OPT_GAMMA_VECTOR_B].size = GAMMA_LENGTH * sizeof (SANE_Word);
2013 dev->opt[OPT_GAMMA_VECTOR_B].constraint_type = SANE_CONSTRAINT_RANGE;
2014 dev->opt[OPT_GAMMA_VECTOR_B].constraint.range = &gamma_range;
2015 dev->val[OPT_GAMMA_VECTOR_B].wa = dev->gamma_B;
2016
2017 /* grayscale gamma vector */
2018 dev->opt[OPT_GAMMA_VECTOR_GRAY].name = SANE_NAME_GAMMA_VECTOR;
2019 dev->opt[OPT_GAMMA_VECTOR_GRAY].title = SANE_TITLE_GAMMA_VECTOR;
2020 dev->opt[OPT_GAMMA_VECTOR_GRAY].desc = SANE_DESC_GAMMA_VECTOR;
2021 dev->opt[OPT_GAMMA_VECTOR_GRAY].type = SANE_TYPE_INT;
2022 dev->opt[OPT_GAMMA_VECTOR_GRAY].cap |= SANE_CAP_INACTIVE;
2023 dev->opt[OPT_GAMMA_VECTOR_GRAY].unit = SANE_UNIT_NONE;
2024 dev->opt[OPT_GAMMA_VECTOR_GRAY].size = GAMMA_LENGTH * sizeof (SANE_Word);
2025 dev->opt[OPT_GAMMA_VECTOR_GRAY].constraint_type = SANE_CONSTRAINT_RANGE;
2026 dev->opt[OPT_GAMMA_VECTOR_GRAY].constraint.range = &gamma_range;
2027 dev->val[OPT_GAMMA_VECTOR_GRAY].wa = dev->gamma_GRAY;
2028
2029 /* Color filter */
2030 dev->opt[OPT_FILTER_COLOR].name = "color-filter";
2031 dev->opt[OPT_FILTER_COLOR].title = "Color dropout";
2032 dev->opt[OPT_FILTER_COLOR].desc = "Color dropout";
2033 dev->opt[OPT_FILTER_COLOR].type = SANE_TYPE_STRING;
2034 dev->opt[OPT_FILTER_COLOR].size = max_string_size (filter_color_list);
2035 dev->opt[OPT_FILTER_COLOR].cap |= SANE_CAP_INACTIVE;
2036 dev->opt[OPT_FILTER_COLOR].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2037 dev->opt[OPT_FILTER_COLOR].constraint.string_list = filter_color_list;
2038 dev->val[OPT_FILTER_COLOR].s = (SANE_Char *) strdup (filter_color_list[0]);
2039
2040 /* Threshold */
2041 dev->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD;
2042 dev->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD;
2043 dev->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD;
2044 dev->opt[OPT_THRESHOLD].type = SANE_TYPE_INT;
2045 dev->opt[OPT_THRESHOLD].unit = SANE_UNIT_NONE;
2046 dev->opt[OPT_THRESHOLD].size = sizeof (SANE_Int);
2047 dev->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE;
2048 dev->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE;
2049 dev->opt[OPT_THRESHOLD].constraint.range = &threshold_range;
2050 dev->val[OPT_THRESHOLD].w = 128;
2051
2052 /* preview */
2053 dev->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
2054 dev->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
2055 dev->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
2056 dev->opt[OPT_PREVIEW].type = SANE_TYPE_BOOL;
2057 dev->opt[OPT_PREVIEW].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
2058 dev->val[OPT_PREVIEW].w = SANE_FALSE;
2059
2060 /* red level calibration manual correction */
2061 dev->opt[OPT_WHITE_LEVEL_R].name = SANE_NAME_WHITE_LEVEL_R;
2062 dev->opt[OPT_WHITE_LEVEL_R].title = SANE_TITLE_WHITE_LEVEL_R;
2063 dev->opt[OPT_WHITE_LEVEL_R].desc = SANE_DESC_WHITE_LEVEL_R;
2064 dev->opt[OPT_WHITE_LEVEL_R].type = SANE_TYPE_INT;
2065 dev->opt[OPT_WHITE_LEVEL_R].unit = SANE_UNIT_NONE;
2066 dev->opt[OPT_WHITE_LEVEL_R].constraint_type = SANE_CONSTRAINT_RANGE;
2067 dev->opt[OPT_WHITE_LEVEL_R].constraint.range = &red_level_range;
2068 dev->val[OPT_WHITE_LEVEL_R].w = 32; /* to get middle value */
2069
2070 /* green level calibration manual correction */
2071 dev->opt[OPT_WHITE_LEVEL_G].name = SANE_NAME_WHITE_LEVEL_G;
2072 dev->opt[OPT_WHITE_LEVEL_G].title = SANE_TITLE_WHITE_LEVEL_G;
2073 dev->opt[OPT_WHITE_LEVEL_G].desc = SANE_DESC_WHITE_LEVEL_G;
2074 dev->opt[OPT_WHITE_LEVEL_G].type = SANE_TYPE_INT;
2075 dev->opt[OPT_WHITE_LEVEL_G].unit = SANE_UNIT_NONE;
2076 dev->opt[OPT_WHITE_LEVEL_G].constraint_type = SANE_CONSTRAINT_RANGE;
2077 dev->opt[OPT_WHITE_LEVEL_G].constraint.range = &green_level_range;
2078 dev->val[OPT_WHITE_LEVEL_G].w = 32; /* to get middle value */
2079
2080 /* blue level calibration manual correction */
2081 dev->opt[OPT_WHITE_LEVEL_B].name = SANE_NAME_WHITE_LEVEL_B;
2082 dev->opt[OPT_WHITE_LEVEL_B].title = SANE_TITLE_WHITE_LEVEL_B;
2083 dev->opt[OPT_WHITE_LEVEL_B].desc = SANE_DESC_WHITE_LEVEL_B;
2084 dev->opt[OPT_WHITE_LEVEL_B].type = SANE_TYPE_INT;
2085 dev->opt[OPT_WHITE_LEVEL_B].unit = SANE_UNIT_NONE;
2086 dev->opt[OPT_WHITE_LEVEL_B].constraint_type = SANE_CONSTRAINT_RANGE;
2087 dev->opt[OPT_WHITE_LEVEL_B].constraint.range = &blue_level_range;
2088 dev->val[OPT_WHITE_LEVEL_B].w = 32; /* to get middle value */
2089
2090 /* Lastly, set the default scan mode. This might change some
2091 * values previously set here. */
2092 sane_control_option (dev, OPT_MODE, SANE_ACTION_SET_VALUE,
2093 (SANE_String_Const *) scan_mode_list[0], NULL);
2094 }
2095
2096 /*
2097 * Wait until the scanner is ready.
2098 */
2099 static SANE_Status
teco_wait_scanner(Teco_Scanner * dev)2100 teco_wait_scanner (Teco_Scanner * dev)
2101 {
2102 SANE_Status status;
2103 int timeout;
2104 CDB cdb;
2105
2106 DBG (DBG_proc, "teco_wait_scanner: enter\n");
2107
2108 MKSCSI_TEST_UNIT_READY (cdb);
2109
2110 /* Set the timeout to 60 seconds. */
2111 timeout = 60;
2112
2113 while (timeout > 0)
2114 {
2115
2116 /* test unit ready */
2117 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
2118 status = sanei_scsi_cmd (dev->sfd, cdb.data, cdb.len, NULL, NULL);
2119
2120 if (status == SANE_STATUS_GOOD)
2121 {
2122 return SANE_STATUS_GOOD;
2123 }
2124
2125 sleep (1);
2126 };
2127
2128 DBG (DBG_proc, "teco_wait_scanner: scanner not ready\n");
2129 return (SANE_STATUS_IO_ERROR);
2130 }
2131
2132 /*
2133 * Adjust the rasters. This function is used during a color scan,
2134 * because the scanner does not present a format sane can interpret
2135 * directly.
2136 *
2137 * The scanner sends the colors by rasters (B then G then R), whereas
2138 * sane is waiting for a group of 3 bytes per color. To make things
2139 * funnier, the rasters are shifted. As a result, color planes appear to be shifted be a few pixels.
2140 *
2141 * The order of the color is dependent on each scanners. Also the same
2142 * scanner can change the order depending on the resolution.
2143 *
2144 * For instance, the VM6586 at 300dpi has a color shift of 2 lines. The rasters sent are:
2145 * starts with two blue rasters - BB,
2146 * then red in added - BRBR
2147 * then green - BRG ... (most of the picture)
2148 * then blue is removed - RGRG
2149 * and finally only green stays - GG
2150 *
2151 * Overall there is the same number of RGB rasters.
2152 * The VM3575 is a variant (when factor_x is 0). It does not keep the same order,
2153 * but reverses it, eg:
2154 * BB RBRB GRB... GRGR GG
2155 * (ie it adds the new color in front of the previous one, instead of after).
2156 *
2157 * So this function reorders all that mess. It gets the input from
2158 * dev->buffer and write the output in dev->image. size_in the the
2159 * length of the valid data in dev->buffer. */
2160
2161 #define COLOR_0 (color_adjust->z3_color_0)
2162 #define COLOR_1 (color_adjust->z3_color_1)
2163 #define COLOR_2 (color_adjust->z3_color_2)
2164
2165 static void
teco_adjust_raster(Teco_Scanner * dev, size_t size_in)2166 teco_adjust_raster (Teco_Scanner * dev, size_t size_in)
2167 {
2168 int nb_rasters; /* number of rasters in dev->buffer */
2169
2170 int raster; /* current raster number in buffer */
2171 int line; /* line number for that raster */
2172 int color; /* color for that raster */
2173 size_t offset;
2174 const struct dpi_color_adjust *color_adjust = dev->color_adjust;
2175
2176 DBG (DBG_proc, "teco_adjust_raster: enter\n");
2177
2178 color = 0; /* keep gcc quiet */
2179
2180 assert (dev->scan_mode == TECO_COLOR);
2181 assert ((size_in % dev->params.bytes_per_line) == 0);
2182
2183 if (size_in == 0)
2184 {
2185 return;
2186 }
2187
2188 /*
2189 * The color coding is one line for each color (in the RGB order).
2190 * Recombine that stuff to create a RGB value for each pixel.
2191 */
2192
2193 nb_rasters = size_in / dev->raster_size;
2194
2195 for (raster = 0; raster < nb_rasters; raster++)
2196 {
2197
2198 /*
2199 * Find the color and the line which this raster belongs to.
2200 */
2201 line = 0;
2202 if (dev->raster_num < color_adjust->color_shift)
2203 {
2204 /* Top of the picture. */
2205 if (color_adjust->factor_x)
2206 {
2207 color = COLOR_2;
2208 }
2209 else
2210 {
2211 color = COLOR_1;
2212 }
2213 line = dev->raster_num;
2214 }
2215 else if (dev->raster_num < (3 * color_adjust->color_shift))
2216 {
2217 /* Top of the picture. */
2218 if ((dev->raster_num - color_adjust->color_shift) % 2)
2219 {
2220 color = COLOR_0;
2221 line = (dev->raster_num - color_adjust->color_shift) / 2;
2222 }
2223 else
2224 {
2225 if (color_adjust->factor_x)
2226 {
2227 color = COLOR_2;
2228 }
2229 else
2230 {
2231 color = COLOR_1;
2232 }
2233 line = (dev->raster_num + color_adjust->color_shift) / 2;
2234 }
2235 }
2236 else if (dev->raster_num >=
2237 dev->raster_real - color_adjust->color_shift)
2238 {
2239 /* Bottom of the picture. */
2240 if (color_adjust->factor_x)
2241 {
2242 color = COLOR_1;
2243 }
2244 else
2245 {
2246 color = COLOR_2;
2247 }
2248 line = dev->line;
2249 }
2250 else if (dev->raster_num >=
2251 dev->raster_real - 3 * color_adjust->color_shift)
2252 {
2253 /* Bottom of the picture. */
2254 if ((dev->raster_real - dev->raster_num -
2255 color_adjust->color_shift) % 2)
2256 {
2257 if (color_adjust->factor_x)
2258 {
2259 color = COLOR_1;
2260 line = dev->line;
2261 }
2262 else
2263 {
2264 color = COLOR_0;
2265 line = dev->line + color_adjust->color_shift - 1;
2266 }
2267 }
2268 else
2269 {
2270 if (color_adjust->factor_x)
2271 {
2272 color = COLOR_0;
2273 line = dev->line + color_adjust->color_shift;
2274 }
2275 else
2276 {
2277 color = COLOR_2;
2278 line = dev->line;
2279 }
2280 }
2281 }
2282 else
2283 {
2284 /* Middle of the picture. */
2285 switch ((dev->raster_num) % 3)
2286 {
2287 case 0:
2288 color = COLOR_2;
2289 if (color_adjust->factor_x)
2290 line = dev->raster_num / 3 + color_adjust->color_shift;
2291 else
2292 line = dev->raster_num / 3 - color_adjust->color_shift;
2293 break;
2294 case 1:
2295 color = COLOR_0;
2296 line = dev->raster_num / 3;
2297 break;
2298 case 2:
2299 color = COLOR_1;
2300 if (color_adjust->factor_x)
2301 line = dev->raster_num / 3 - color_adjust->color_shift;
2302 else
2303 line = dev->raster_num / 3 + color_adjust->color_shift;
2304 break;
2305 }
2306 }
2307
2308 /* Adjust the line number relative to the image. */
2309 line -= dev->line;
2310
2311 offset = dev->image_end + line * dev->params.bytes_per_line;
2312
2313 assert (offset <= (dev->image_size - dev->params.bytes_per_line));
2314
2315 /* Copy the raster to the temporary image. */
2316 {
2317 int i;
2318 unsigned char *src = dev->buffer + raster * dev->raster_size;
2319 unsigned char *dest = dev->image + offset + color;
2320
2321 for (i = 0; i < dev->raster_size; i++)
2322 {
2323 *dest = *src;
2324 src++;
2325 dest += 3;
2326 }
2327 }
2328
2329 DBG (DBG_info, "raster=%d, line=%d, color=%d\n", dev->raster_num,
2330 dev->line + line, color);
2331
2332 if ((color_adjust->factor_x == 0 && color == COLOR_2) ||
2333 (color_adjust->factor_x == 1 && color == COLOR_1))
2334 {
2335 /* This blue raster completes a new line */
2336 dev->line++;
2337 dev->image_end += dev->params.bytes_per_line;
2338 }
2339
2340 dev->raster_num++;
2341 }
2342
2343 DBG (DBG_proc, "teco_adjust_raster: exit\n");
2344 }
2345
2346 /* Read the image from the scanner and fill the temporary buffer with it. */
2347 static SANE_Status
teco_fill_image(Teco_Scanner * dev)2348 teco_fill_image (Teco_Scanner * dev)
2349 {
2350 SANE_Status status;
2351 size_t size;
2352 CDB cdb;
2353
2354 DBG (DBG_proc, "teco_fill_image: enter\n");
2355
2356 assert (dev->image_begin == dev->image_end);
2357 assert (dev->real_bytes_left > 0);
2358
2359 /* Copy the complete lines, plus the incompletes
2360 * ones. We don't keep the real end of data used
2361 * in image, so we copy the biggest possible.
2362 *
2363 * This is a no-op for non color images.
2364 */
2365 memmove (dev->image, dev->image + dev->image_begin, dev->raster_ahead);
2366
2367 dev->image_begin = 0;
2368 dev->image_end = 0;
2369
2370 while (dev->real_bytes_left)
2371 {
2372 /*
2373 * Try to read the maximum number of bytes.
2374 */
2375 size = dev->real_bytes_left;
2376 if (size > dev->image_size - dev->raster_ahead - dev->image_end)
2377 {
2378 size = dev->image_size - dev->raster_ahead - dev->image_end;
2379 }
2380 if (size > dev->buffer_size)
2381 {
2382 size = dev->buffer_size;
2383 }
2384
2385 /* Limit to 0x2000 bytes as does the TWAIN driver. */
2386 if (size > 0x2000)
2387 size = 0x2000;
2388
2389 /* Round down to a multiple of line size. */
2390 size = size - (size % dev->params.bytes_per_line);
2391
2392 if (size == 0)
2393 {
2394 /* Probably reached the end of the buffer.
2395 * Check, just in case. */
2396 assert (dev->image_end != 0);
2397 return (SANE_STATUS_GOOD);
2398 }
2399
2400 DBG (DBG_info, "teco_fill_image: to read = %ld bytes (bpl=%d)\n",
2401 (long) size, dev->params.bytes_per_line);
2402
2403 MKSCSI_READ_10 (cdb, 0, 0, size);
2404 cdb.data[5] = size / dev->params.bytes_per_line;
2405
2406 hexdump (DBG_info2, "teco_fill_image: READ_10 CDB", cdb.data, cdb.len);
2407
2408 hexdump (DBG_info2, "CDB:", cdb.data, cdb.len);
2409 status = sanei_scsi_cmd2 (dev->sfd, cdb.data, cdb.len,
2410 NULL, 0, dev->buffer, &size);
2411
2412 #ifdef sim
2413 memcpy (dev->buffer, image_buf + image_buf_begin, size);
2414 image_buf_begin += size;
2415 #endif
2416
2417 if (status != SANE_STATUS_GOOD)
2418 {
2419 DBG (DBG_error, "teco_fill_image: cannot read from the scanner\n");
2420 return status;
2421 }
2422
2423 DBG (DBG_info, "teco_fill_image: real bytes left = %ld\n",
2424 (long) dev->real_bytes_left);
2425
2426 if (dev->scan_mode == TECO_COLOR &&
2427 dev->def->tecoref != TECO_VM656A && raw_output == 0)
2428 {
2429 teco_adjust_raster (dev, size);
2430 }
2431 else
2432 {
2433 memcpy (dev->image + dev->image_end, dev->buffer, size);
2434 dev->image_end += size;
2435 }
2436 dev->real_bytes_left -= size;
2437 }
2438
2439 return (SANE_STATUS_GOOD); /* unreachable */
2440 }
2441
2442 /* Copy from the raw buffer to the buffer given by the backend.
2443 *
2444 * len in input is the maximum length available in buf, and, in
2445 * output, is the length written into buf.
2446 */
2447 static void
teco_copy_raw_to_frontend(Teco_Scanner * dev, SANE_Byte * buf, size_t * len)2448 teco_copy_raw_to_frontend (Teco_Scanner * dev, SANE_Byte * buf, size_t * len)
2449 {
2450 size_t size;
2451
2452 size = dev->image_end - dev->image_begin;
2453 if (size > *len)
2454 {
2455 size = *len;
2456 }
2457 *len = size;
2458
2459 switch (dev->scan_mode)
2460 {
2461 case TECO_BW:
2462 {
2463 /* Invert black and white. */
2464 unsigned char *src = dev->image + dev->image_begin;
2465 size_t i;
2466
2467 for (i = 0; i < size; i++)
2468 {
2469 *buf = *src ^ 0xff;
2470 src++;
2471 buf++;
2472 }
2473 }
2474 break;
2475
2476 case TECO_GRAYSCALE:
2477 case TECO_COLOR:
2478 memcpy (buf, dev->image + dev->image_begin, size);
2479 break;
2480 }
2481
2482 dev->image_begin += size;
2483 }
2484
2485 /* Stop a scan. */
2486 static SANE_Status
do_cancel(Teco_Scanner * dev)2487 do_cancel (Teco_Scanner * dev)
2488 {
2489 DBG (DBG_sane_proc, "do_cancel enter\n");
2490
2491 if (dev->scanning == SANE_TRUE)
2492 {
2493
2494 /* Reset the scanner */
2495 teco_reset_window (dev);
2496 teco_close (dev);
2497 }
2498
2499 dev->scanning = SANE_FALSE;
2500
2501 DBG (DBG_sane_proc, "do_cancel exit\n");
2502
2503 return SANE_STATUS_CANCELLED;
2504 }
2505
2506 /*--------------------------------------------------------------------------*/
2507
2508 /* Sane entry points */
2509
2510 SANE_Status
sane_init(SANE_Int * version_code, SANE_Auth_Callback __sane_unused__ authorize)2511 sane_init (SANE_Int * version_code, SANE_Auth_Callback __sane_unused__ authorize)
2512 {
2513 FILE *fp;
2514 char dev_name[PATH_MAX];
2515 size_t len;
2516
2517 DBG_INIT ();
2518
2519 DBG (DBG_sane_init, "sane_init\n");
2520
2521 DBG (DBG_error, "This is sane-teco2 version %d.%d-%d\n", SANE_CURRENT_MAJOR,
2522 SANE_CURRENT_MINOR, BUILD);
2523 DBG (DBG_error, "(C) 2002 - 2003 by Frank Zago, update 2003 - 2008 by Gerard Klaver\n");
2524
2525 if (version_code)
2526 {
2527 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD);
2528 }
2529
2530 fp = sanei_config_open (TECO2_CONFIG_FILE);
2531 if (!fp)
2532 {
2533 /* default to /dev/scanner instead of insisting on config file */
2534 attach_scanner ("/dev/scanner", 0);
2535 return SANE_STATUS_GOOD;
2536 }
2537
2538 while (sanei_config_read (dev_name, sizeof (dev_name), fp))
2539 {
2540 if (dev_name[0] == '#') /* ignore line comments */
2541 continue;
2542 len = strlen (dev_name);
2543
2544 if (!len)
2545 continue; /* ignore empty lines */
2546
2547 sanei_config_attach_matching_devices (dev_name, attach_one);
2548 }
2549
2550 fclose (fp);
2551
2552 DBG (DBG_proc, "sane_init: leave\n");
2553
2554 return SANE_STATUS_GOOD;
2555 }
2556
2557 SANE_Status
sane_get_devices(const SANE_Device *** device_list, SANE_Bool __sane_unused__ local_only)2558 sane_get_devices (const SANE_Device *** device_list, SANE_Bool __sane_unused__ local_only)
2559 {
2560 Teco_Scanner *dev;
2561 int i;
2562
2563 DBG (DBG_proc, "sane_get_devices: enter\n");
2564
2565 if (devlist)
2566 free (devlist);
2567
2568 devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
2569 if (!devlist)
2570 return SANE_STATUS_NO_MEM;
2571
2572 i = 0;
2573 for (dev = first_dev; i < num_devices; dev = dev->next)
2574 devlist[i++] = &dev->sane;
2575 devlist[i++] = 0;
2576
2577 *device_list = devlist;
2578
2579 DBG (DBG_proc, "sane_get_devices: exit\n");
2580
2581 return SANE_STATUS_GOOD;
2582 }
2583
2584 SANE_Status
sane_open(SANE_String_Const devicename, SANE_Handle * handle)2585 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
2586 {
2587 Teco_Scanner *dev;
2588 SANE_Status status;
2589 int i;
2590
2591 DBG (DBG_proc, "sane_open: enter\n");
2592
2593 /* search for devicename */
2594 if (devicename[0])
2595 {
2596 DBG (DBG_info, "sane_open: devicename=%s\n", devicename);
2597
2598 for (dev = first_dev; dev; dev = dev->next)
2599 {
2600 if (strcmp (dev->sane.name, devicename) == 0)
2601 {
2602 break;
2603 }
2604 }
2605
2606 if (!dev)
2607 {
2608 status = attach_scanner (devicename, &dev);
2609 if (status != SANE_STATUS_GOOD)
2610 {
2611 return status;
2612 }
2613 }
2614 }
2615 else
2616 {
2617 DBG (DBG_sane_info, "sane_open: no devicename, opening first device\n");
2618 dev = first_dev; /* empty devicename -> use first device */
2619 }
2620
2621 if (!dev)
2622 {
2623 DBG (DBG_error, "No scanner found\n");
2624
2625 return SANE_STATUS_INVAL;
2626 }
2627
2628 teco_init_options (dev);
2629
2630 /* Initialize the gamma table. */
2631 for (i = 0; i < GAMMA_LENGTH; i++)
2632 {
2633 dev->gamma_R[i] = i / 4;
2634 dev->gamma_G[i] = i / 4;
2635 dev->gamma_B[i] = i / 4;
2636 dev->gamma_GRAY[i] = i / 4;
2637 }
2638
2639 *handle = dev;
2640
2641 DBG (DBG_proc, "sane_open: exit\n");
2642
2643 return SANE_STATUS_GOOD;
2644 }
2645
2646 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle, SANE_Int option)2647 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
2648 {
2649 Teco_Scanner *dev = handle;
2650
2651 DBG (DBG_proc, "sane_get_option_descriptor: enter, option %d\n", option);
2652
2653 if ((unsigned) option >= OPT_NUM_OPTIONS)
2654 {
2655 return NULL;
2656 }
2657
2658 DBG (DBG_proc, "sane_get_option_descriptor: exit\n");
2659
2660 return dev->opt + option;
2661 }
2662
2663 SANE_Status
sane_control_option(SANE_Handle handle, SANE_Int option, SANE_Action action, void *val, SANE_Int * info)2664 sane_control_option (SANE_Handle handle, SANE_Int option,
2665 SANE_Action action, void *val, SANE_Int * info)
2666 {
2667 Teco_Scanner *dev = handle;
2668 SANE_Status status;
2669 SANE_Word cap;
2670
2671 DBG (DBG_proc, "sane_control_option: enter, option %d, action %d\n",
2672 option, action);
2673
2674 if (info)
2675 {
2676 *info = 0;
2677 }
2678
2679 if (dev->scanning)
2680 {
2681 return SANE_STATUS_DEVICE_BUSY;
2682 }
2683
2684 if (option < 0 || option >= OPT_NUM_OPTIONS)
2685 {
2686 return SANE_STATUS_INVAL;
2687 }
2688
2689 cap = dev->opt[option].cap;
2690 if (!SANE_OPTION_IS_ACTIVE (cap))
2691 {
2692 return SANE_STATUS_INVAL;
2693 }
2694
2695 if (action == SANE_ACTION_GET_VALUE)
2696 {
2697
2698 switch (option)
2699 {
2700 /* word options */
2701 case OPT_NUM_OPTS:
2702 case OPT_RESOLUTION:
2703 case OPT_TL_Y:
2704 case OPT_BR_Y:
2705 case OPT_TL_X:
2706 case OPT_BR_X:
2707 case OPT_CUSTOM_GAMMA:
2708 case OPT_PREVIEW:
2709 case OPT_THRESHOLD:
2710 case OPT_WHITE_LEVEL_R:
2711 case OPT_WHITE_LEVEL_G:
2712 case OPT_WHITE_LEVEL_B:
2713 *(SANE_Word *) val = dev->val[option].w;
2714 return SANE_STATUS_GOOD;
2715
2716 /* string options */
2717 case OPT_MODE:
2718 case OPT_DITHER:
2719 case OPT_FILTER_COLOR:
2720 strcpy (val, dev->val[option].s);
2721 return SANE_STATUS_GOOD;
2722
2723 /* Gamma */
2724 case OPT_GAMMA_VECTOR_R:
2725 case OPT_GAMMA_VECTOR_G:
2726 case OPT_GAMMA_VECTOR_B:
2727 case OPT_GAMMA_VECTOR_GRAY:
2728 memcpy (val, dev->val[option].wa, dev->opt[option].size);
2729 return SANE_STATUS_GOOD;
2730
2731 default:
2732 return SANE_STATUS_INVAL;
2733 }
2734 }
2735 else if (action == SANE_ACTION_SET_VALUE)
2736 {
2737
2738 if (!SANE_OPTION_IS_SETTABLE (cap))
2739 {
2740 DBG (DBG_error, "could not set option, not settable\n");
2741 return SANE_STATUS_INVAL;
2742 }
2743
2744 status = sanei_constrain_value (dev->opt + option, val, info);
2745 if (status != SANE_STATUS_GOOD)
2746 {
2747 DBG (DBG_error, "could not set option, invalid value\n");
2748 return status;
2749 }
2750
2751 switch (option)
2752 {
2753
2754 /* Numeric side-effect options */
2755 case OPT_TL_Y:
2756 case OPT_BR_Y:
2757 case OPT_TL_X:
2758 case OPT_BR_X:
2759 case OPT_THRESHOLD:
2760 case OPT_RESOLUTION:
2761 case OPT_WHITE_LEVEL_R:
2762 case OPT_WHITE_LEVEL_G:
2763 case OPT_WHITE_LEVEL_B:
2764 if (info)
2765 {
2766 *info |= SANE_INFO_RELOAD_PARAMS;
2767 }
2768 dev->val[option].w = *(SANE_Word *) val;
2769 return SANE_STATUS_GOOD;
2770
2771 /* Numeric side-effect free options */
2772 case OPT_PREVIEW:
2773 dev->val[option].w = *(SANE_Word *) val;
2774 return SANE_STATUS_GOOD;
2775
2776 /* String side-effect free options */
2777 case OPT_DITHER:
2778 free (dev->val[option].s);
2779 dev->val[option].s = (SANE_String) strdup (val);
2780 return SANE_STATUS_GOOD;
2781
2782 case OPT_FILTER_COLOR:
2783 free (dev->val[option].s);
2784 dev->val[option].s = (SANE_String) strdup (val);
2785 return SANE_STATUS_GOOD;
2786
2787 /* String side-effect options */
2788 case OPT_MODE:
2789 if (strcmp (dev->val[option].s, val) == 0)
2790 return SANE_STATUS_GOOD;
2791
2792 free (dev->val[OPT_MODE].s);
2793 dev->val[OPT_MODE].s = (SANE_Char *) strdup (val);
2794
2795 dev->opt[OPT_CUSTOM_GAMMA].cap |= SANE_CAP_INACTIVE;
2796 dev->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
2797 dev->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
2798 dev->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
2799 dev->opt[OPT_GAMMA_VECTOR_GRAY].cap |= SANE_CAP_INACTIVE;
2800 dev->opt[OPT_DITHER].cap |= SANE_CAP_INACTIVE;
2801 dev->opt[OPT_FILTER_COLOR].cap |= SANE_CAP_INACTIVE;
2802 dev->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE;
2803 dev->opt[OPT_WHITE_LEVEL_R].cap |= SANE_CAP_INACTIVE;
2804 dev->opt[OPT_WHITE_LEVEL_G].cap |= SANE_CAP_INACTIVE;
2805 dev->opt[OPT_WHITE_LEVEL_B].cap |= SANE_CAP_INACTIVE;
2806
2807
2808 /* This the default resolution range, except for the
2809 * VM3575, VM3564, VM356A and VM6586 in color mode. */
2810 dev->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
2811 dev->opt[OPT_RESOLUTION].constraint.range = &dev->def->res_range;
2812
2813 if (strcmp (dev->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_LINEART) == 0)
2814 {
2815 dev->scan_mode = TECO_BW;
2816 dev->depth = 8;
2817 dev->opt[OPT_DITHER].cap &= ~SANE_CAP_INACTIVE;
2818 dev->opt[OPT_FILTER_COLOR].cap &= ~SANE_CAP_INACTIVE;
2819 dev->opt[OPT_THRESHOLD].cap &= ~SANE_CAP_INACTIVE;
2820 }
2821 else if (strcmp (dev->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_GRAY) == 0)
2822 {
2823 dev->scan_mode = TECO_GRAYSCALE;
2824 dev->depth = 8;
2825
2826 switch (dev->def->tecoref)
2827 {
2828 case TECO_VM3564:
2829 case TECO_VM356A:
2830 dev->opt[OPT_WHITE_LEVEL_R].cap &= ~SANE_CAP_INACTIVE;
2831 dev->opt[OPT_WHITE_LEVEL_G].cap &= ~SANE_CAP_INACTIVE;
2832 dev->opt[OPT_WHITE_LEVEL_B].cap &= ~SANE_CAP_INACTIVE;
2833 break;
2834 case TECO_VM3575:
2835 case TECO_VM6575:
2836 dev->opt[OPT_WHITE_LEVEL_R].cap &= ~SANE_CAP_INACTIVE;
2837 dev->opt[OPT_WHITE_LEVEL_G].cap &= ~SANE_CAP_INACTIVE;
2838 dev->opt[OPT_WHITE_LEVEL_B].cap &= ~SANE_CAP_INACTIVE;
2839 dev->opt[OPT_CUSTOM_GAMMA].cap &= ~SANE_CAP_INACTIVE;
2840 if (dev->val[OPT_CUSTOM_GAMMA].w)
2841 {
2842 dev->opt[OPT_GAMMA_VECTOR_GRAY].cap &=
2843 ~SANE_CAP_INACTIVE;
2844 }
2845 break;
2846 case TECO_VM656A:
2847 case TECO_VM6586:
2848 dev->opt[OPT_CUSTOM_GAMMA].cap &= ~SANE_CAP_INACTIVE;
2849 if (dev->val[OPT_CUSTOM_GAMMA].w)
2850 {
2851 dev->opt[OPT_GAMMA_VECTOR_GRAY].cap &=
2852 ~SANE_CAP_INACTIVE;
2853 }
2854 break;
2855 }
2856 dev->opt[OPT_FILTER_COLOR].cap &= ~SANE_CAP_INACTIVE;
2857 }
2858 else if (strcmp (dev->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR) == 0)
2859 {
2860 dev->scan_mode = TECO_COLOR;
2861 dev->depth = 8;
2862
2863 switch (dev->def->tecoref)
2864 {
2865 case TECO_VM3564:
2866 case TECO_VM356A:
2867 dev->opt[OPT_WHITE_LEVEL_R].cap &= ~SANE_CAP_INACTIVE;
2868 dev->opt[OPT_WHITE_LEVEL_G].cap &= ~SANE_CAP_INACTIVE;
2869 dev->opt[OPT_WHITE_LEVEL_B].cap &= ~SANE_CAP_INACTIVE;
2870 break;
2871 case TECO_VM3575:
2872 case TECO_VM6575:
2873 dev->opt[OPT_WHITE_LEVEL_R].cap &= ~SANE_CAP_INACTIVE;
2874 dev->opt[OPT_WHITE_LEVEL_G].cap &= ~SANE_CAP_INACTIVE;
2875 dev->opt[OPT_WHITE_LEVEL_B].cap &= ~SANE_CAP_INACTIVE;
2876 dev->opt[OPT_CUSTOM_GAMMA].cap &= ~SANE_CAP_INACTIVE;
2877 if (dev->val[OPT_CUSTOM_GAMMA].w)
2878 {
2879 dev->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
2880 dev->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
2881 dev->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
2882 }
2883 break;
2884 case TECO_VM656A:
2885 case TECO_VM6586:
2886 dev->opt[OPT_CUSTOM_GAMMA].cap &= ~SANE_CAP_INACTIVE;
2887 if (dev->val[OPT_CUSTOM_GAMMA].w)
2888 {
2889 dev->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
2890 dev->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
2891 dev->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
2892 }
2893 break;
2894 }
2895 /* The VM3575, VM3564, VM356A and VM6586 supports only a handful of resolution. Do that here.
2896 * Ugly! */
2897 if (dev->resolutions_list != NULL)
2898 {
2899 int i;
2900
2901 dev->opt[OPT_RESOLUTION].constraint_type =
2902 SANE_CONSTRAINT_WORD_LIST;
2903 dev->opt[OPT_RESOLUTION].constraint.word_list =
2904 dev->resolutions_list;
2905
2906 /* If the resolution isn't in the list, set a default. */
2907 for (i = 1; i <= dev->resolutions_list[0]; i++)
2908 {
2909 if (dev->resolutions_list[i] >=
2910 dev->val[OPT_RESOLUTION].w)
2911 break;
2912 }
2913 if (i > dev->resolutions_list[0])
2914 {
2915 /* Too big. Take default. */
2916 dev->val[OPT_RESOLUTION].w = DEF_RESOLUTION;
2917 }
2918 else
2919 {
2920 /* Take immediate superioir value. */
2921 dev->val[OPT_RESOLUTION].w = dev->resolutions_list[i];
2922 }
2923 }
2924 }
2925
2926 if (info)
2927 {
2928 *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
2929 }
2930 return SANE_STATUS_GOOD;
2931
2932 case OPT_GAMMA_VECTOR_R:
2933 case OPT_GAMMA_VECTOR_G:
2934 case OPT_GAMMA_VECTOR_B:
2935 case OPT_GAMMA_VECTOR_GRAY:
2936 memcpy (dev->val[option].wa, val, dev->opt[option].size);
2937 return SANE_STATUS_GOOD;
2938
2939 case OPT_CUSTOM_GAMMA:
2940 dev->val[OPT_CUSTOM_GAMMA].w = *(SANE_Word *) val;
2941 if (dev->val[OPT_CUSTOM_GAMMA].w)
2942 {
2943 /* use custom_gamma_table */
2944 if (dev->scan_mode == TECO_GRAYSCALE)
2945 {
2946 dev->opt[OPT_GAMMA_VECTOR_GRAY].cap &= ~SANE_CAP_INACTIVE;
2947 }
2948 else
2949 {
2950 /* color mode */
2951 dev->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
2952 dev->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
2953 dev->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
2954 }
2955 }
2956 else
2957 {
2958 dev->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
2959 dev->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
2960 dev->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
2961 }
2962 if (info)
2963 {
2964 *info |= SANE_INFO_RELOAD_OPTIONS;
2965 }
2966 return SANE_STATUS_GOOD;
2967
2968 default:
2969 return SANE_STATUS_INVAL;
2970 }
2971 }
2972
2973 DBG (DBG_proc, "sane_control_option: exit, bad\n");
2974
2975 return SANE_STATUS_UNSUPPORTED;
2976 }
2977
2978 SANE_Status
sane_get_parameters(SANE_Handle handle, SANE_Parameters * params)2979 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
2980 {
2981 Teco_Scanner *dev = handle;
2982
2983 DBG (DBG_proc, "sane_get_parameters: enter\n");
2984
2985 if (!(dev->scanning))
2986 {
2987
2988 /* Setup the parameters for the scan. These values will be re-used
2989 * in the SET WINDOWS command. */
2990 if (dev->val[OPT_PREVIEW].w == SANE_TRUE)
2991 {
2992 /* for VM356A, no good 50 dpi scan possible, now leave as */
2993
2994 switch (dev->def->tecoref)
2995 {
2996 case TECO_VM356A:
2997 case TECO_VM6575:
2998 dev->x_resolution = 75;
2999 dev->y_resolution = 75;
3000 break;
3001 /* For VM3575, VM656A, VM6586 etc until otherwise default value is used */
3002 default:
3003 dev->x_resolution = 50;
3004 dev->y_resolution = 50;
3005 break;
3006 }
3007 dev->x_tl = 0;
3008 dev->y_tl = 0;
3009 dev->x_br = mmToIlu (SANE_UNFIX (dev->def->x_range.max));
3010 dev->y_br = mmToIlu (SANE_UNFIX (dev->def->y_range.max));
3011 }
3012 else
3013 {
3014 dev->x_resolution = dev->val[OPT_RESOLUTION].w;
3015 dev->y_resolution = dev->val[OPT_RESOLUTION].w;
3016
3017 dev->x_tl = mmToIlu (SANE_UNFIX (dev->val[OPT_TL_X].w));
3018 dev->y_tl = mmToIlu (SANE_UNFIX (dev->val[OPT_TL_Y].w));
3019 dev->x_br = mmToIlu (SANE_UNFIX (dev->val[OPT_BR_X].w));
3020 dev->y_br = mmToIlu (SANE_UNFIX (dev->val[OPT_BR_Y].w));
3021 }
3022
3023 if (dev->x_resolution > dev->def->x_resolution_max)
3024 {
3025 dev->x_resolution = dev->def->x_resolution_max;
3026 }
3027
3028 /* Check the corners are OK. */
3029 if (dev->x_tl > dev->x_br)
3030 {
3031 int s;
3032 s = dev->x_tl;
3033 dev->x_tl = dev->x_br;
3034 dev->x_br = s;
3035 }
3036 if (dev->y_tl > dev->y_br)
3037 {
3038 int s;
3039 s = dev->y_tl;
3040 dev->y_tl = dev->y_br;
3041 dev->y_br = s;
3042 }
3043
3044 dev->width = dev->x_br - dev->x_tl;
3045 dev->length = dev->y_br - dev->y_tl;
3046
3047 /* Prepare the parameters for the caller. */
3048 memset (&dev->params, 0, sizeof (SANE_Parameters));
3049
3050 dev->params.last_frame = SANE_TRUE;
3051
3052 switch (dev->scan_mode)
3053 {
3054 case TECO_BW:
3055 dev->params.format = SANE_FRAME_GRAY;
3056 dev->params.pixels_per_line =
3057 ((dev->width * dev->x_resolution) /
3058 dev->def->x_resolution_max) & ~0x7;
3059 dev->params.bytes_per_line = dev->params.pixels_per_line / 8;
3060 dev->params.depth = 1;
3061 dev->color_adjust = NULL;
3062 break;
3063 case TECO_GRAYSCALE:
3064 dev->params.format = SANE_FRAME_GRAY;
3065 dev->params.pixels_per_line =
3066 ((dev->width * dev->x_resolution) / dev->def->x_resolution_max);
3067 if ((dev->def->tecoref == TECO_VM656A
3068 || dev->def->tecoref == TECO_VM6586)
3069 && ((dev->width * dev->x_resolution) %
3070 dev->def->x_resolution_max != 0))
3071 {
3072 /* Round up */
3073 dev->params.pixels_per_line += 1;
3074 }
3075 dev->params.bytes_per_line = dev->params.pixels_per_line;
3076 dev->params.depth = 8;
3077 dev->color_adjust = NULL;
3078 break;
3079 case TECO_COLOR:
3080 dev->params.format = SANE_FRAME_RGB;
3081 dev->params.pixels_per_line =
3082 ((dev->width * dev->x_resolution) / dev->def->x_resolution_max);
3083 if ((dev->def->tecoref == TECO_VM656A
3084 || dev->def->tecoref == TECO_VM6586)
3085 && ((dev->width * dev->x_resolution) %
3086 dev->def->x_resolution_max != 0))
3087 {
3088 /* Round up */
3089 dev->params.pixels_per_line += 1;
3090 }
3091 dev->params.bytes_per_line = dev->params.pixels_per_line * 3;
3092 dev->params.depth = 8;
3093
3094 if (dev->resolutions_list != NULL)
3095 {
3096 /* This scanner has a fixed number of supported
3097 * resolutions. Find the color shift for that
3098 * resolution. */
3099
3100 int i;
3101 for (i = 0;
3102 dev->def->color_adjust[i].resolution != dev->y_resolution;
3103 i++);
3104
3105 dev->color_adjust = &dev->def->color_adjust[i];
3106 }
3107 else
3108 {
3109 dev->color_adjust = &dev->def->color_adjust[0];
3110 }
3111 break;
3112 }
3113
3114 dev->params.lines =
3115 (dev->length * dev->y_resolution) / dev->def->x_resolution_max;
3116 }
3117
3118 /* Return the current values. */
3119 if (params)
3120 {
3121 *params = (dev->params);
3122 }
3123
3124 DBG (DBG_proc, "sane_get_parameters: exit\n");
3125
3126 return SANE_STATUS_GOOD;
3127 }
3128
3129 SANE_Status
sane_start(SANE_Handle handle)3130 sane_start (SANE_Handle handle)
3131 {
3132 Teco_Scanner *dev = handle;
3133 SANE_Status status;
3134
3135 DBG (DBG_proc, "sane_start: enter\n");
3136
3137 if (!(dev->scanning))
3138 {
3139
3140 sane_get_parameters (dev, NULL);
3141
3142 /* Open again the scanner. */
3143 if (sanei_scsi_open
3144 (dev->devicename, &(dev->sfd), teco_sense_handler, dev) != 0)
3145 {
3146 DBG (DBG_error, "ERROR: sane_start: open failed\n");
3147 return SANE_STATUS_INVAL;
3148 }
3149
3150 /* The scanner must be ready. */
3151 status = teco_wait_scanner (dev);
3152 if (status)
3153 {
3154 teco_close (dev);
3155 return status;
3156 }
3157
3158 status = teco_set_window (dev);
3159 if (status)
3160 {
3161 teco_close (dev);
3162 return status;
3163 }
3164
3165 status = teco_get_scan_size (dev);
3166 if (status)
3167 {
3168 teco_close (dev);
3169 return status;
3170 }
3171
3172 /* Compute the length necessary in image. The first part will store
3173 * the complete lines, and the rest is used to stored ahead
3174 * rasters.
3175 */
3176 if (dev->color_adjust)
3177 {
3178 dev->raster_ahead =
3179 (2 * dev->color_adjust->color_shift) * dev->params.bytes_per_line;
3180 }
3181 else
3182 {
3183 dev->raster_ahead = 0;
3184 }
3185 dev->image_size = dev->buffer_size + dev->raster_ahead;
3186 dev->image = malloc (dev->image_size);
3187 if (dev->image == NULL)
3188 {
3189 return SANE_STATUS_NO_MEM;
3190 }
3191
3192 /* Rasters are meaningful only in color mode. */
3193 dev->raster_size = dev->params.pixels_per_line;
3194 dev->raster_real = dev->params.lines * 3;
3195 dev->raster_num = 0;
3196 dev->line = 0;
3197
3198 #if 1
3199 status = teco_do_calibration (dev);
3200 if (status)
3201 {
3202 teco_close (dev);
3203 return status;
3204 }
3205 #endif
3206
3207 switch (dev->def->tecoref)
3208 {
3209 /* case TECO_VM3564: not for VM3564 */
3210 case TECO_VM356A:
3211 /*--------------request sense for first time loop---*/
3212 status = teco_request_sense (dev);
3213 if (status)
3214 {
3215 teco_close (dev);
3216 return status;
3217 }
3218 break;
3219 default:
3220 break;
3221 }
3222
3223 status = teco_send_gamma (dev);
3224 if (status)
3225 {
3226 teco_close (dev);
3227 return status;
3228 }
3229
3230 status = teco_set_window (dev);
3231 if (status)
3232 {
3233 teco_close (dev);
3234 return status;
3235 }
3236 switch (dev->def->tecoref)
3237 {
3238 case TECO_VM3564:
3239 case TECO_VM356A:
3240 break;
3241 default:
3242 status = teco_send_vendor_06 (dev);
3243 if (status)
3244 {
3245 teco_close (dev);
3246 return status;
3247 }
3248 }
3249 status = teco_scan (dev);
3250 if (status)
3251 {
3252 teco_close (dev);
3253 return status;
3254 }
3255
3256 status = teco_wait_for_data (dev);
3257 if (status)
3258 {
3259 teco_close (dev);
3260 return status;
3261 }
3262 }
3263
3264 dev->image_end = 0;
3265 dev->image_begin = 0;
3266
3267 dev->bytes_left = dev->params.bytes_per_line * dev->params.lines;
3268 dev->real_bytes_left = dev->params.bytes_per_line * dev->params.lines;
3269
3270 dev->scanning = SANE_TRUE;
3271
3272 DBG (DBG_proc, "sane_start: exit\n");
3273
3274 return SANE_STATUS_GOOD;
3275 }
3276
3277 SANE_Status
sane_read(SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len)3278 sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
3279 SANE_Int * len)
3280 {
3281 SANE_Status status;
3282 Teco_Scanner *dev = handle;
3283 size_t size;
3284 int buf_offset; /* offset into buf */
3285
3286 DBG (DBG_proc, "sane_read: enter\n");
3287
3288 *len = 0;
3289
3290 if (!(dev->scanning))
3291 {
3292 /* OOPS, not scanning */
3293 return do_cancel (dev);
3294 }
3295
3296 if (dev->bytes_left <= 0)
3297 {
3298 return (SANE_STATUS_EOF);
3299 }
3300
3301 buf_offset = 0;
3302
3303 do
3304 {
3305 if (dev->image_begin == dev->image_end)
3306 {
3307 /* Fill image */
3308 status = teco_fill_image (dev);
3309 if (status != SANE_STATUS_GOOD)
3310 {
3311 return (status);
3312 }
3313 }
3314
3315 /* Something must have been read */
3316 #if 1
3317 assert (dev->image_begin != dev->image_end);
3318 #else
3319 if (dev->image_begin == dev->image_end)
3320 {
3321 DBG (DBG_info, "sane_read: nothing read\n");
3322 return SANE_STATUS_IO_ERROR;
3323 }
3324 #endif
3325
3326 /* Copy the data to the frontend buffer. */
3327 size = max_len - buf_offset;
3328 if (size > dev->bytes_left)
3329 {
3330 size = dev->bytes_left;
3331 }
3332 teco_copy_raw_to_frontend (dev, buf + buf_offset, &size);
3333
3334 buf_offset += size;
3335
3336 dev->bytes_left -= size;
3337 *len += size;
3338
3339 }
3340 while ((buf_offset != max_len) && dev->bytes_left);
3341
3342 DBG (DBG_info, "sane_read: leave, bytes_left=%ld\n", (long) dev->bytes_left);
3343
3344 return SANE_STATUS_GOOD;
3345 }
3346
3347 SANE_Status
sane_set_io_mode(SANE_Handle __sane_unused__ handle, SANE_Bool __sane_unused__ non_blocking)3348 sane_set_io_mode (SANE_Handle __sane_unused__ handle, SANE_Bool __sane_unused__ non_blocking)
3349 {
3350 SANE_Status status;
3351 Teco_Scanner *dev = handle;
3352
3353 DBG (DBG_proc, "sane_set_io_mode: enter\n");
3354
3355 if (dev->scanning == SANE_FALSE)
3356 {
3357 return SANE_STATUS_INVAL;
3358 }
3359
3360 if (non_blocking == SANE_FALSE)
3361 {
3362 status = SANE_STATUS_GOOD;
3363 }
3364 else
3365 {
3366 status = SANE_STATUS_UNSUPPORTED;
3367 }
3368
3369 DBG (DBG_proc, "sane_set_io_mode: exit\n");
3370
3371 return status;
3372 }
3373
3374 SANE_Status
sane_get_select_fd(SANE_Handle __sane_unused__ handle, SANE_Int __sane_unused__ * fd)3375 sane_get_select_fd (SANE_Handle __sane_unused__ handle, SANE_Int __sane_unused__ * fd)
3376 {
3377 DBG (DBG_proc, "sane_get_select_fd: enter\n");
3378
3379 DBG (DBG_proc, "sane_get_select_fd: exit\n");
3380
3381 return SANE_STATUS_UNSUPPORTED;
3382 }
3383
3384 void
sane_cancel(SANE_Handle handle)3385 sane_cancel (SANE_Handle handle)
3386 {
3387 Teco_Scanner *dev = handle;
3388
3389 DBG (DBG_proc, "sane_cancel: enter\n");
3390
3391 do_cancel (dev);
3392
3393 DBG (DBG_proc, "sane_cancel: exit\n");
3394 }
3395
3396 void
sane_close(SANE_Handle handle)3397 sane_close (SANE_Handle handle)
3398 {
3399 Teco_Scanner *dev = handle;
3400 Teco_Scanner *dev_tmp;
3401
3402 DBG (DBG_proc, "sane_close: enter\n");
3403
3404 do_cancel (dev);
3405 teco_close (dev);
3406
3407 /* Unlink dev. */
3408 if (first_dev == dev)
3409 {
3410 first_dev = dev->next;
3411 }
3412 else
3413 {
3414 dev_tmp = first_dev;
3415 while (dev_tmp->next && dev_tmp->next != dev)
3416 {
3417 dev_tmp = dev_tmp->next;
3418 }
3419 if (dev_tmp->next != NULL)
3420 {
3421 dev_tmp->next = dev_tmp->next->next;
3422 }
3423 }
3424
3425 teco_free (dev);
3426 num_devices--;
3427
3428 DBG (DBG_proc, "sane_close: exit\n");
3429 }
3430
3431 void
sane_exit(void)3432 sane_exit (void)
3433 {
3434 DBG (DBG_proc, "sane_exit: enter\n");
3435
3436 while (first_dev)
3437 {
3438 sane_close (first_dev);
3439 }
3440
3441 if (devlist)
3442 {
3443 free (devlist);
3444 devlist = NULL;
3445 }
3446
3447 DBG (DBG_proc, "sane_exit: exit\n");
3448 }
3449