1 /* sane - Scanner Access Now Easy.
2 Copyright (C) 1996, 1997 David Mosberger-Tang and Andreas Czechanowski,
3 1998 Andreas Bolsch for extension to ScanExpress models version 0.6,
4 2000-2005 Henning Meier-Geinitz,
5 2003 James Perry (600 EP).
6
7 This file is part of the SANE package.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22 As a special exception, the authors of SANE give permission for
23 additional uses of the libraries contained in this release of SANE.
24
25 The exception is that, if you link a SANE library with other files
26 to produce an executable, this does not by itself cause the
27 resulting executable to be covered by the GNU General Public
28 License. Your use of that executable is in no way restricted on
29 account of linking the SANE library code into it.
30
31 This exception does not, however, invalidate any other reasons why
32 the executable file might be covered by the GNU General Public
33 License.
34
35 If you submit changes to SANE to the maintainers to be included in
36 a subsequent release, you agree by submitting the changes that
37 those changes may be distributed with this exception intact.
38
39 If you write modifications of your own for SANE, it is your choice
40 whether to permit this exception to apply to your modifications.
41 If you do not wish that, delete this exception notice.
42
43 This file implements a SANE backend for Mustek and some Trust flatbed
44 scanners with SCSI, parallel port (600 EP) or proprietary interface. */
45
46
47 /**************************************************************************/
48 /* Mustek backend version */
49 #define BUILD 138
50 /**************************************************************************/
51
52 #include "../include/sane/config.h"
53
54 #include <ctype.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <limits.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include <sys/time.h>
65 #include <sys/types.h>
66
67 #include "../include/_stdint.h"
68
69 #include "../include/sane/sane.h"
70 #include "../include/sane/sanei.h"
71 #include "../include/sane/saneopts.h"
72 #include "../include/sane/sanei_scsi.h"
73 #include "../include/sane/sanei_ab306.h"
74 #include "../include/sane/sanei_thread.h"
75
76 #define BACKEND_NAME mustek
77 #include "../include/sane/sanei_backend.h"
78 #include "../include/sane/sanei_config.h"
79
80 #include "mustek.h"
81 #include "mustek_scsi_pp.h"
82
83 #ifndef SANE_I18N
84 #define SANE_I18N(text) text
85 #endif
86
87 /* Debug level from sanei_init_debug */
88 static SANE_Int debug_level;
89
90 /* Maximum # of inches to scan in one swoop. 0 means "unlimited."
91 This is here to be nice on the SCSI bus---Mustek scanners don't
92 disconnect while scanning is in progress, which confuses some
93 drivers that expect no reasonable SCSI request would take more than
94 10 seconds. That's not really true for Mustek scanners operating
95 in certain modes, hence this limit. Usually you don't need to set it. */
96 static double strip_height;
97
98 /* Should we wait for the scan slider to return after each scan? */
99 static SANE_Bool force_wait;
100
101 /* Should we disable double buffering when reading data from the scanner? */
102 static SANE_Bool disable_double_buffering;
103
104 static SANE_Int num_devices;
105 static Mustek_Device *first_dev;
106 static Mustek_Scanner *first_handle;
107 static const SANE_Device **devlist = 0;
108
109 /* Array of newly attached devices */
110 static Mustek_Device **new_dev;
111
112 /* Length of new_dev array */
113 static SANE_Int new_dev_len;
114
115 /* Number of entries allocated for new_dev */
116 static SANE_Int new_dev_alloced;
117
118 static SANE_Int lamp_off_time = 60;
119
120 /* Used for line-distance correction: */
121 static const SANE_Int color_seq[] = {
122 1, 2, 0 /* green, blue, red */
123 };
124
125 /* Which modes are supported? */
126 static SANE_String_Const mode_list_paragon[] = {
127 SANE_VALUE_SCAN_MODE_LINEART,
128 SANE_VALUE_SCAN_MODE_HALFTONE,
129 SANE_VALUE_SCAN_MODE_GRAY,
130 SANE_VALUE_SCAN_MODE_COLOR,
131 0
132 };
133 static SANE_String_Const mode_list_se[] = {
134 SANE_VALUE_SCAN_MODE_LINEART,
135 SANE_VALUE_SCAN_MODE_GRAY,
136 SANE_VALUE_SCAN_MODE_COLOR,
137 0
138 };
139
140 static SANE_String_Const bit_depth_list_pro[] = {
141 "8", "12",
142 0
143 };
144
145 /* Some scanners support setting speed manually */
146 static SANE_String_Const speed_list[] = {
147 SANE_I18N ("Slowest"), SANE_I18N ("Slower"), SANE_I18N ("Normal"),
148 SANE_I18N ("Faster"), SANE_I18N ("Fastest"),
149 0
150 };
151
152 /* Which scan-sources are supported? */
153 static const SANE_String_Const source_list[] = {
154 SANE_I18N ("Flatbed"),
155 0
156 };
157 static SANE_String_Const adf_source_list[] = {
158 SANE_I18N ("Flatbed"), SANE_I18N ("Automatic Document Feeder"),
159 0
160 };
161 static SANE_String_Const ta_source_list[] = {
162 SANE_I18N ("Flatbed"), SANE_I18N ("Transparency Adapter"),
163 0
164 };
165
166 /* Range used for gamma and halftone pattern */
167 static const SANE_Range u8_range = {
168 0, /* minimum */
169 255, /* maximum */
170 0 /* quantization */
171 };
172
173 /* Which kind of halftone patterns are available? */
174 static SANE_String_Const halftone_list[] = {
175 SANE_I18N ("8x8 coarse"), SANE_I18N ("8x8 normal"), SANE_I18N ("8x8 fine"),
176 SANE_I18N ("8x8 very fine"), SANE_I18N ("6x6 normal"),
177 SANE_I18N ("5x5 coarse"), SANE_I18N ("5x5 fine"), SANE_I18N ("4x4 coarse"),
178 SANE_I18N ("4x4 normal"), SANE_I18N ("4x4 fine"), SANE_I18N ("3x3 normal"),
179 SANE_I18N ("2x2 normal"), SANE_I18N ("8x8 custom"),
180 SANE_I18N ("6x6 custom"),
181 SANE_I18N ("5x5 custom"), SANE_I18N ("4x4 custom"),
182 SANE_I18N ("3x3 custom"),
183 SANE_I18N ("2x2 custom"),
184 0
185 };
186
187 /* Range used for brightness and contrast */
188 static const SANE_Range percentage_range = {
189 SANE_FIX(-100), /* minimum */
190 SANE_FIX(100), /* maximum */
191 SANE_FIX(1) /* quantization */
192 };
193
194 /* SCSI command buffers used by the backend */
195 static const SANE_Byte scsi_inquiry[] = {
196 MUSTEK_SCSI_INQUIRY, 0x00, 0x00, 0x00, INQ_LEN, 0x00
197 };
198 static const SANE_Byte scsi_test_unit_ready[] = {
199 MUSTEK_SCSI_TEST_UNIT_READY, 0x00, 0x00, 0x00, 0x00, 0x00
200 };
201 static const SANE_Byte scsi_request_sense[] = {
202 MUSTEK_SCSI_REQUEST_SENSE, 0x00, 0x00, 0x00, 0x04, 0x00
203 };
204 static const SANE_Byte scsi_start_stop[] = {
205 MUSTEK_SCSI_START_STOP, 0x00, 0x00, 0x00, 0x00, 0x00
206 };
207 static const SANE_Byte scsi_ccd_distance[] = {
208 MUSTEK_SCSI_CCD_DISTANCE, 0x00, 0x00, 0x00, 0x05, 0x00
209 };
210 static const SANE_Byte scsi_get_image_status[] = {
211 MUSTEK_SCSI_GET_IMAGE_STATUS, 0x00, 0x00, 0x00, 0x06, 0x00
212 };
213 static const SANE_Byte scsi_get_window[] = {
214 MUSTEK_SCSI_GET_WINDOW, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00
215 };
216
217 /* Sizes for fixed-length, non-vendor-specific CDB formats (others are 0) */
218 #define CDB_SIZE(opcode) ((opcode) < 0x20 ? 6 : \
219 (opcode) < 0x60 ? 10 : \
220 (opcode) < 0x80 ? 0 : \
221 (opcode) < 0xa0 ? 16 : \
222 (opcode) < 0xc0 ? 12 : 0)
223
224 /* prototypes */
225 static SANE_Status area_and_windows (Mustek_Scanner * s);
226 static SANE_Status inquiry (Mustek_Scanner * s);
227
228 /* Test if this machine is little endian (from coolscan.c) */
229 static SANE_Bool
little_endian(void)230 little_endian (void)
231 {
232 SANE_Int testvalue = 255;
233 uint8_t *firstbyte = (uint8_t *) & testvalue;
234
235 if (*firstbyte == 255)
236 return SANE_TRUE;
237 return SANE_FALSE;
238 }
239
240 /* Used for Pro series. First value seems to be always 85, second one varies.
241 First bit of second value clear == device ready (?) */
242 static SANE_Status
scsi_sense_wait_ready(Mustek_Scanner * s)243 scsi_sense_wait_ready (Mustek_Scanner * s)
244 {
245 struct timeval now, start;
246 SANE_Status status;
247 size_t len;
248 SANE_Byte sense_buffer[4];
249 SANE_Byte bytetxt[300], dbgtxt[300], *pp;
250
251 gettimeofday (&start, 0);
252
253 while (1)
254 {
255 len = sizeof (sense_buffer);
256
257 DBG (5, "scsi_sense_wait_ready: command size = %ld, sense size = %ld\n",
258 (long int) sizeof (scsi_request_sense), (long int) len);
259 status = sanei_scsi_cmd (s->fd, scsi_request_sense,
260 sizeof (scsi_request_sense), sense_buffer,
261 &len);
262 if (status != SANE_STATUS_GOOD)
263 {
264 DBG (1, "scsi_sense_wait_ready: failed: %s\n",
265 sane_strstatus (status));
266 return status;
267 }
268
269 dbgtxt[0] = '\0';
270 for (pp = sense_buffer; pp < (sense_buffer + 4); pp++)
271 {
272 sprintf ((SANE_String) bytetxt, " %02x", *pp);
273 strcat ((SANE_String) dbgtxt, (SANE_String) bytetxt);
274 }
275 DBG (5, "scsi_sense_wait_ready: sensebuffer: %s\n", dbgtxt);
276
277 if (!(sense_buffer[1] & 0x01))
278 {
279 DBG (4, "scsi_sense_wait_ready: ok\n");
280 return SANE_STATUS_GOOD;
281 }
282 else
283 {
284 gettimeofday (&now, 0);
285 if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
286 {
287 DBG (1, "scsi_sense_wait_ready: timed out after %lu seconds\n",
288 (u_long) (now.tv_sec - start.tv_sec));
289 return SANE_STATUS_INVAL;
290 }
291 usleep (100000); /* retry after 100ms */
292 }
293 }
294 return SANE_STATUS_INVAL;
295 }
296
297 /* Used for 3pass series */
298 static SANE_Status
scsi_area_wait_ready(Mustek_Scanner * s)299 scsi_area_wait_ready (Mustek_Scanner * s)
300 {
301 struct timeval now, start;
302 SANE_Status status;
303
304 gettimeofday (&start, 0);
305
306 DBG (5, "scsi_area_wait_ready\n");
307 while (1)
308 {
309 status = area_and_windows (s);
310 switch (status)
311 {
312 default:
313 /* Ignore errors while waiting for scanner to become ready.
314 Some SCSI drivers return EIO while the scanner is
315 returning to the home position. */
316 DBG (3, "scsi_area_wait_ready: failed (%s)\n",
317 sane_strstatus (status));
318 /* fall through */
319 case SANE_STATUS_DEVICE_BUSY:
320 gettimeofday (&now, 0);
321 if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
322 {
323 DBG (1, "scsi_area_wait_ready: timed out after %lu seconds\n",
324 (u_long) (now.tv_sec - start.tv_sec));
325 return SANE_STATUS_INVAL;
326 }
327 usleep (100000); /* retry after 100ms */
328 break;
329
330 case SANE_STATUS_GOOD:
331 return status;
332 }
333 }
334 return SANE_STATUS_INVAL;
335 }
336
337 static SANE_Status
scsi_unit_wait_ready(Mustek_Scanner * s)338 scsi_unit_wait_ready (Mustek_Scanner * s)
339 {
340 struct timeval now, start;
341 SANE_Status status;
342
343 gettimeofday (&start, 0);
344
345 while (1)
346 {
347 DBG (5, "scsi_unit_wait_ready: sending TEST_UNIT_READY\n");
348 status = sanei_scsi_cmd (s->fd, scsi_test_unit_ready,
349 sizeof (scsi_test_unit_ready), 0, 0);
350 DBG (5, "scsi_unit_wait_ready: TEST_UNIT_READY finished\n");
351 switch (status)
352 {
353 default:
354 /* Ignore errors while waiting for scanner to become ready.
355 Some SCSI drivers return EIO while the scanner is
356 returning to the home position. */
357 DBG (3, "scsi_unit_wait_ready: test unit ready failed (%s)\n",
358 sane_strstatus (status));
359 /* fall through */
360 case SANE_STATUS_DEVICE_BUSY:
361 gettimeofday (&now, 0);
362 if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
363 {
364 DBG (1, "scsi_unit_wait_ready: timed out after %lu seconds\n",
365 (u_long) (now.tv_sec - start.tv_sec));
366 return SANE_STATUS_INVAL;
367 }
368 usleep (100000); /* retry after 100ms */
369 break;
370
371 case SANE_STATUS_GOOD:
372 return status;
373 }
374 }
375 return SANE_STATUS_INVAL;
376 }
377
378 static SANE_Status
scsi_inquiry_wait_ready(Mustek_Scanner * s)379 scsi_inquiry_wait_ready (Mustek_Scanner * s)
380 {
381 struct timeval now, start;
382 SANE_Status status;
383
384 gettimeofday (&start, 0);
385
386 while (1)
387 {
388 DBG (5, "scsi_inquiry_wait_ready: sending INQUIRY\n");
389 status = inquiry (s);
390 DBG (5, "scsi_inquiry_wait_ready: INQUIRY finished\n");
391 switch (status)
392 {
393 default:
394 /* Ignore errors while waiting for scanner to become ready.
395 Some SCSI drivers return EIO while the scanner is
396 returning to the home position. */
397 DBG (3, "scsi_unit_wait_ready: inquiry failed (%s)\n",
398 sane_strstatus (status));
399 /* fall through */
400 case SANE_STATUS_DEVICE_BUSY:
401 gettimeofday (&now, 0);
402 if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
403 {
404 DBG (1, "scsi_unit_wait_ready: timed out after %lu seconds\n",
405 (u_long) (now.tv_sec - start.tv_sec));
406 return SANE_STATUS_INVAL;
407 }
408 usleep (500000); /* retry after 500ms */
409 break;
410
411 case SANE_STATUS_GOOD:
412 return status;
413 }
414 }
415 return SANE_STATUS_INVAL;
416 }
417
418 static SANE_Status
n_wait_ready(Mustek_Scanner * s)419 n_wait_ready (Mustek_Scanner * s)
420 {
421 struct timeval now, start;
422 SANE_Status status;
423
424 gettimeofday (&start, 0);
425
426 DBG (5, "n_wait_ready\n");
427 while (1)
428 {
429 status = sanei_ab306_test_ready (s->fd);
430 if (status == SANE_STATUS_GOOD)
431 return SANE_STATUS_GOOD;
432
433 gettimeofday (&now, 0);
434 if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
435 {
436 DBG (1, "n_wait_ready: timed out after %lu seconds\n",
437 (u_long) (now.tv_sec - start.tv_sec));
438 return SANE_STATUS_INVAL;
439 }
440 usleep (100000); /* retry after 100ms */
441 }
442 }
443
444 static SANE_Status
scsi_pp_wait_ready(Mustek_Scanner * s)445 scsi_pp_wait_ready (Mustek_Scanner * s)
446 {
447 struct timeval now, start;
448 SANE_Status status;
449
450 gettimeofday (&start, 0);
451
452 DBG (5, "scsi_pp_wait_ready\n");
453 while (1)
454 {
455 status = mustek_scsi_pp_test_ready (s->fd);
456 if (status == SANE_STATUS_GOOD)
457 return SANE_STATUS_GOOD;
458
459 gettimeofday (&now, 0);
460 if (now.tv_sec - start.tv_sec >= MAX_WAITING_TIME)
461 {
462 DBG (1, "scsi_pp_wait_ready: timed out after %lu seconds\n",
463 (u_long) (now.tv_sec - start.tv_sec));
464 return SANE_STATUS_INVAL;
465 }
466 usleep (100000); /* retry after 100ms */
467 }
468 }
469
470 static SANE_Status
dev_wait_ready(Mustek_Scanner * s)471 dev_wait_ready (Mustek_Scanner * s)
472 {
473 if (s->hw->flags & MUSTEK_FLAG_N)
474 return n_wait_ready (s);
475 else if (s->hw->flags & MUSTEK_FLAG_SCSI_PP)
476 return scsi_pp_wait_ready (s);
477 else if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
478 {
479 SANE_Status status;
480
481 /* some 3-pass scanners seem to need the inquiry wait, too */
482 status = scsi_area_wait_ready (s);
483 if (status != SANE_STATUS_GOOD)
484 return status;
485 return scsi_inquiry_wait_ready (s);
486 }
487 else if ((s->hw->flags & MUSTEK_FLAG_PARAGON_1)
488 || (s->hw->flags & MUSTEK_FLAG_PARAGON_2))
489 return scsi_inquiry_wait_ready (s);
490 else if (s->hw->flags & MUSTEK_FLAG_PRO)
491 return scsi_sense_wait_ready (s);
492 else
493 return scsi_unit_wait_ready (s);
494 }
495
496 static SANE_Status
dev_open(SANE_String_Const devname, Mustek_Scanner * s, SANEI_SCSI_Sense_Handler handler)497 dev_open (SANE_String_Const devname, Mustek_Scanner * s,
498 SANEI_SCSI_Sense_Handler handler)
499 {
500 SANE_Status status;
501
502 DBG (5, "dev_open %s\n", devname);
503
504 #ifdef HAVE_SANEI_SCSI_OPEN_EXTENDED
505 s->hw->buffer_size = s->hw->max_buffer_size;
506 status = sanei_scsi_open_extended (devname, &s->fd, handler, 0,
507 &s->hw->buffer_size);
508 #else
509 s->hw->buffer_size = MIN (sanei_scsi_max_request_size,
510 s->hw->max_buffer_size);
511 status = sanei_scsi_open (devname, &s->fd, handler, 0);
512 #endif
513
514 if (status == SANE_STATUS_GOOD)
515 {
516 DBG (3, "dev_open: %s is a SCSI device\n", devname);
517 DBG (4, "dev_open: wanted %d kbytes, got %d kbytes buffer\n",
518 s->hw->max_buffer_size / 1024, s->hw->buffer_size / 1024);
519 if (s->hw->buffer_size < 4096)
520 {
521 DBG (1, "dev_open: sanei_scsi_open buffer too small\n");
522 sanei_scsi_close (s->fd);
523 return SANE_STATUS_NO_MEM;
524 }
525 }
526 else
527 {
528 DBG (3, "dev_open: %s: can't open %s as a SCSI device\n",
529 sane_strstatus (status), devname);
530
531 status = sanei_ab306_open (devname, &s->fd);
532 if (status == SANE_STATUS_GOOD)
533 {
534 s->hw->flags |= MUSTEK_FLAG_N;
535 DBG (3, "dev_open: %s is an AB306N device\n", devname);
536 }
537 else
538 {
539 DBG (3, "dev_open: %s: can't open %s as an AB306N device\n",
540 sane_strstatus (status), devname);
541
542 status = mustek_scsi_pp_open (devname, &s->fd);
543 if (status == SANE_STATUS_GOOD)
544 {
545 s->hw->flags |= MUSTEK_FLAG_SCSI_PP;
546 DBG (3, "dev_open: %s is a SCSI-over-parallel device\n",
547 devname);
548 }
549 else
550 {
551 DBG (3,
552 "dev_open: %s: can't open %s as a SCSI-over-parallel device\n",
553 sane_strstatus (status), devname);
554 DBG (1, "dev_open: can't open %s\n", devname);
555 return SANE_STATUS_INVAL;
556 }
557 }
558 }
559 return SANE_STATUS_GOOD;
560 }
561
562 static SANE_Status
dev_cmd(Mustek_Scanner * s, const void *src, size_t src_size, void *dst, size_t * dst_size)563 dev_cmd (Mustek_Scanner * s, const void *src, size_t src_size,
564 void *dst, size_t * dst_size)
565 {
566 SANE_Status status;
567 SANE_Byte cmd_byte_list[50];
568 SANE_Byte cmd_byte[5];
569 const SANE_Byte *pp;
570
571 DBG (5, "dev_cmd: fd=%d, src=%p, src_size=%ld, dst=%p, dst_size=%ld\n",
572 s->fd, src, (long int) src_size, dst,
573 (long int) (dst_size ? *dst_size : 0));
574
575 if (src && (debug_level >= 5)) /* output data sent to SCSI device */
576 {
577 cmd_byte_list[0] = '\0';
578 for (pp = (const SANE_Byte *) src;
579 pp < (((const SANE_Byte *) src) + src_size); pp++)
580 {
581 sprintf ((SANE_String) cmd_byte, " %02x", *pp);
582 strcat ((SANE_String) cmd_byte_list, (SANE_String) cmd_byte);
583 if (((pp - (const SANE_Byte *) src) % 0x10 == 0x0f)
584 || (pp >= (((const SANE_Byte *) src) + src_size - 1)))
585 {
586 DBG (5, "dev_cmd: sending: %s\n", cmd_byte_list);
587 cmd_byte_list[0] = '\0';
588 }
589 }
590 }
591 if (s->hw->flags & MUSTEK_FLAG_N)
592 status = sanei_ab306_cmd (s->fd, src, src_size, dst, dst_size);
593 else if (s->hw->flags & MUSTEK_FLAG_SCSI_PP)
594 status = mustek_scsi_pp_cmd (s->fd, src, src_size, dst, dst_size);
595 else
596 status = sanei_scsi_cmd (s->fd, src, src_size, dst, dst_size);
597
598 if (dst && dst_size && (debug_level >= 5))
599 /* output data received from SCSI device */
600 {
601 cmd_byte_list[0] = '\0';
602 for (pp = (const SANE_Byte *) dst;
603 pp < (((const SANE_Byte *) dst) + *dst_size); pp++)
604 {
605 sprintf ((SANE_String) cmd_byte, " %02x", *pp);
606 strcat ((SANE_String) cmd_byte_list, (SANE_String) cmd_byte);
607 if (((pp - (const SANE_Byte *) dst) % 0x10 == 0x0f)
608 || (pp >= (((const SANE_Byte *) dst) + *dst_size - 1)))
609 {
610 DBG (5, "dev_cmd: receiving: %s\n", cmd_byte_list);
611 cmd_byte_list[0] = '\0';
612 }
613 }
614 }
615
616 DBG (5, "dev_cmd: finished: dst_size=%ld, status=%s\n",
617 (long int) (dst_size ? *dst_size : 0), sane_strstatus (status));
618 return status;
619 }
620
621 static SANE_Status
dev_req_wait(void *id)622 dev_req_wait (void *id)
623 {
624 if (!id)
625 return SANE_STATUS_GOOD;
626 else
627 return sanei_scsi_req_wait (id);
628 }
629
630 static SANE_Status
dev_block_read_start(Mustek_Scanner * s, SANE_Int lines)631 dev_block_read_start (Mustek_Scanner * s, SANE_Int lines)
632 {
633 DBG (4, "dev_block_read_start: entering block for %d lines\n", lines);
634 if (s->hw->flags & MUSTEK_FLAG_N)
635 {
636 SANE_Byte readlines[6];
637
638 memset (readlines, 0, sizeof (readlines));
639 readlines[0] = MUSTEK_SCSI_READ_SCANNED_DATA;
640 readlines[2] = (lines >> 16) & 0xff;
641 readlines[3] = (lines >> 8) & 0xff;
642 readlines[4] = (lines >> 0) & 0xff;
643 return sanei_ab306_cmd (s->fd, readlines, sizeof (readlines), 0, 0);
644 }
645 else if (s->hw->flags & MUSTEK_FLAG_SCSI_PP)
646 {
647 SANE_Byte readlines[6];
648
649 memset (readlines, 0, sizeof (readlines));
650 readlines[0] = MUSTEK_SCSI_READ_SCANNED_DATA;
651 readlines[2] = (lines >> 16) & 0xff;
652 readlines[3] = (lines >> 8) & 0xff;
653 readlines[4] = (lines >> 0) & 0xff;
654 return mustek_scsi_pp_cmd (s->fd, readlines, sizeof (readlines), 0, 0);
655 }
656 else if (s->hw->flags & MUSTEK_FLAG_PARAGON_2)
657 {
658 SANE_Byte buffer[6];
659 size_t len;
660 SANE_Int color;
661 SANE_Status status;
662
663 /* reset line-distance values */
664 if (s->mode & MUSTEK_MODE_COLOR)
665 {
666 for (color = 0; color < 3; ++color)
667 {
668 s->ld.index[color] = -s->ld.dist[color];
669 }
670 s->ld.lmod3 = -1;
671 s->ld.ld_line = 0;
672 }
673
674 /* Get image status (necessary to start new block) */
675 len = sizeof (buffer);
676 status = dev_cmd (s, scsi_get_image_status,
677 sizeof (scsi_get_image_status), buffer, &len);
678 if (status != SANE_STATUS_GOOD)
679 return status;
680
681 /* tell scanner how many lines to scan in one block */
682 memset (buffer, 0, sizeof (buffer));
683 buffer[0] = MUSTEK_SCSI_READ_SCANNED_DATA;
684 buffer[2] = (lines >> 16) & 0xff;
685 buffer[3] = (lines >> 8) & 0xff;
686 buffer[4] = (lines >> 0) & 0xff;
687 buffer[5] = 0x04;
688 return sanei_scsi_cmd (s->fd, buffer, sizeof (buffer), 0, 0);
689 }
690 else
691 return SANE_STATUS_GOOD;
692 }
693
694 static SANE_Status
dev_read_req_enter(Mustek_Scanner * s, SANE_Byte * buf, SANE_Int lines, SANE_Int bpl, size_t * lenp, void **idp, SANE_Int bank, SANE_Byte * command)695 dev_read_req_enter (Mustek_Scanner * s, SANE_Byte * buf, SANE_Int lines,
696 SANE_Int bpl, size_t * lenp, void **idp, SANE_Int bank,
697 SANE_Byte * command)
698 {
699 *lenp = lines * bpl;
700
701 if (s->hw->flags & MUSTEK_FLAG_N)
702 {
703 SANE_Int planes;
704
705 *idp = 0;
706 planes = (s->mode & MUSTEK_MODE_COLOR) ? 3 : 1;
707
708 return sanei_ab306_rdata (s->fd, planes, buf, lines, bpl);
709 }
710 else if (s->hw->flags & MUSTEK_FLAG_SCSI_PP)
711 {
712 SANE_Int planes;
713
714 *idp = 0;
715 planes = (s->mode & MUSTEK_MODE_COLOR) ? 3 : 1;
716
717 return mustek_scsi_pp_rdata (s->fd, planes, buf, lines, bpl);
718 }
719 else
720 {
721 if (s->hw->flags & MUSTEK_FLAG_SE)
722 {
723 if (s->mode & MUSTEK_MODE_COLOR)
724 lines *= 3;
725
726 memset (command, 0, 10);
727 command[0] = MUSTEK_SCSI_READ_DATA;
728 command[6] = bank; /* buffer bank not used ??? */
729 command[7] = (lines >> 8) & 0xff;
730 command[8] = (lines >> 0) & 0xff;
731 return sanei_scsi_req_enter (s->fd, command, 10, buf, lenp, idp);
732 }
733 else if (s->hw->flags & MUSTEK_FLAG_PRO)
734 {
735 memset (command, 0, 6);
736 command[0] = MUSTEK_SCSI_READ_SCANNED_DATA;
737 command[2] = ((lines * bpl) >> 16) & 0xff;
738 command[3] = ((lines * bpl) >> 8) & 0xff;
739 command[4] = ((lines * bpl) >> 0) & 0xff;
740
741 return sanei_scsi_req_enter (s->fd, command, 6, buf, lenp, idp);
742 }
743 else /* Paragon series */
744 {
745 memset (command, 0, 6);
746 command[0] = MUSTEK_SCSI_READ_SCANNED_DATA;
747 command[2] = (lines >> 16) & 0xff;
748 command[3] = (lines >> 8) & 0xff;
749 command[4] = (lines >> 0) & 0xff;
750 return sanei_scsi_req_enter (s->fd, command, 6, buf, lenp, idp);
751 }
752 }
753 }
754
755 static void
dev_close(Mustek_Scanner * s)756 dev_close (Mustek_Scanner * s)
757 {
758 if (s->hw->flags & MUSTEK_FLAG_N)
759 sanei_ab306_close (s->fd);
760 else if (s->hw->flags & MUSTEK_FLAG_SCSI_PP)
761 mustek_scsi_pp_close (s->fd);
762 else
763 sanei_scsi_close (s->fd);
764 }
765
766 static SANE_Status
sense_handler(SANE_Int scsi_fd, SANE_Byte * result, void *arg)767 sense_handler (SANE_Int scsi_fd, SANE_Byte * result, void *arg)
768 {
769 if (!result)
770 {
771 DBG (5, "sense_handler: no sense buffer\n");
772 return SANE_STATUS_IO_ERROR;
773 }
774 if (!arg)
775 DBG (5, "sense_handler: got sense code %02x for fd %d (arg = null)\n",
776 result[0], scsi_fd);
777 else
778 DBG (5, "sense_handler: got sense code %02x for fd %d (arg = %uc)\n",
779 result[0], scsi_fd, *(SANE_Byte *) arg);
780 switch (result[0])
781 {
782 case 0x00:
783 break;
784
785 case 0x82:
786 if (result[1] & 0x80)
787 {
788 DBG (3, "sense_handler: ADF is jammed\n");
789 return SANE_STATUS_JAMMED; /* ADF is jammed */
790 }
791 break;
792
793 case 0x83:
794 if (result[2] & 0x02)
795 {
796 DBG (3, "sense_handler: ADF is out of documents\n");
797 return SANE_STATUS_NO_DOCS; /* ADF out of documents */
798 }
799 break;
800
801 case 0x84:
802 if (result[1] & 0x10)
803 {
804 DBG (3, "sense_handler: transparency adapter cover open\n");
805 return SANE_STATUS_COVER_OPEN; /* open transparency adapter cover */
806 }
807 break;
808
809 default:
810 DBG (1, "sense_handler: got unknown sense code %02x for fd %d\n",
811 result[0], scsi_fd);
812 return SANE_STATUS_IO_ERROR;
813 }
814 return SANE_STATUS_GOOD;
815 }
816
817 static SANE_Status
inquiry(Mustek_Scanner * s)818 inquiry (Mustek_Scanner * s)
819 {
820 SANE_Byte result[INQ_LEN];
821 size_t size;
822 SANE_Status status;
823
824 DBG (5, "inquiry: sending INQUIRY\n");
825 size = sizeof (result);
826
827 memset (result, 0, size);
828
829 status = dev_cmd (s, scsi_inquiry, sizeof (scsi_inquiry), result, &size);
830 if (status != SANE_STATUS_GOOD)
831 return status;
832
833 /* checking ADF status */
834 if (s->hw->flags & MUSTEK_FLAG_ADF)
835 {
836 if (result[63] & (1 << 3))
837 {
838 s->hw->flags |= MUSTEK_FLAG_ADF_READY;
839 DBG (4, "inquiry: ADF ready\n");
840 }
841 else
842 {
843 s->hw->flags &= ~MUSTEK_FLAG_ADF_READY;
844 DBG (4, "inquiry: ADF not ready (out of paper)\n");
845 }
846 }
847 if (!result[0])
848 return SANE_STATUS_DEVICE_BUSY;
849 return SANE_STATUS_GOOD;
850 }
851
852 static SANE_Status
paragon_2_get_adf_status(Mustek_Scanner * s)853 paragon_2_get_adf_status (Mustek_Scanner * s)
854 {
855 SANE_Status status;
856 size_t len;
857 SANE_Byte sense_buffer[4];
858
859 len = sizeof (sense_buffer);
860
861 status = sanei_scsi_cmd (s->fd, scsi_request_sense,
862 sizeof (scsi_request_sense), sense_buffer, &len);
863 if (status != SANE_STATUS_GOOD)
864 {
865 DBG (1, "paragon_2_get_adf_status: %s\n", sane_strstatus (status));
866 return status;
867 }
868 DBG (5, "paragon_2_get_adf_status: sense_buffer: %x %x %x %x\n",
869 sense_buffer[0], sense_buffer[1], sense_buffer[3], sense_buffer[3]);
870
871 if (sense_buffer[0] == 0x00 && sense_buffer[1] == 0x00)
872 return SANE_STATUS_GOOD;
873
874 return SANE_STATUS_NO_DOCS;
875 }
876
877 static SANE_Bool
ta_available_pro(Mustek_Scanner * s)878 ta_available_pro (Mustek_Scanner * s)
879 {
880 SANE_Status status;
881 size_t len;
882 SANE_Byte sense_buffer[4];
883
884 len = sizeof (sense_buffer);
885
886 status = sanei_scsi_cmd (s->fd, scsi_request_sense,
887 sizeof (scsi_request_sense), sense_buffer, &len);
888 if (status != SANE_STATUS_GOOD)
889 {
890 DBG (1, "ta_available_pro: failed: %s\n", sane_strstatus (status));
891 return status;
892 }
893 DBG (5, "ta_available_pro: sense_buffer[2] = %x\n", sense_buffer[2]);
894
895 scsi_unit_wait_ready (s);
896 if (sense_buffer[2] == 0x40)
897 return SANE_TRUE;
898
899 return SANE_FALSE;
900 }
901
902 static SANE_Status
attach(SANE_String_Const devname, Mustek_Device ** devp, SANE_Bool may_wait)903 attach (SANE_String_Const devname, Mustek_Device ** devp, SANE_Bool may_wait)
904 {
905 SANE_Int mustek_scanner, fw_revision;
906 SANE_Byte result[INQ_LEN];
907 SANE_Byte inquiry_byte_list[50], inquiry_text_list[17];
908 SANE_Byte inquiry_byte[5], inquiry_text[5];
909 SANE_Byte *model_name = result + 44;
910 Mustek_Scanner s;
911 Mustek_Device *dev, new_dev;
912 SANE_Status status;
913 size_t size;
914 SANE_String scsi_device_type[] = {
915 "Direct-Access", "Sequential-Access", "Printer", "Processor",
916 "Write-Once", "CD-ROM", "Scanner", "Optical Memory", "Medium Changer",
917 "Communications"
918 };
919 SANE_Byte scsi_vendor[9];
920 SANE_Byte scsi_product[17];
921 SANE_Byte scsi_revision[5];
922 SANE_Byte *pp;
923 SANE_Bool warning = SANE_FALSE;
924 SANE_Int firmware_format = 0;
925 SANE_Int firmware_revision_system = 0;
926
927 if (devp)
928 *devp = 0;
929
930 for (dev = first_dev; dev; dev = dev->next)
931 if (strcmp (dev->sane.name, devname) == 0)
932 {
933 if (devp)
934 *devp = dev;
935 return SANE_STATUS_GOOD;
936 }
937
938 memset (&new_dev, 0, sizeof (new_dev));
939 memset (&s, 0, sizeof (s));
940 s.hw = &new_dev;
941 s.hw->max_buffer_size = 8 * 1024;
942
943 DBG (3, "attach: trying device %s\n", devname);
944
945 status = dev_open (devname, &s, sense_handler);
946 if (status != SANE_STATUS_GOOD)
947 return status;
948
949 if (may_wait || force_wait)
950 dev_wait_ready (&s);
951
952 DBG (5, "attach: sending INQUIRY\n");
953 size = sizeof (result);
954 memset (result, 0, sizeof (result));
955 status = dev_cmd (&s, scsi_inquiry, sizeof (scsi_inquiry), result, &size);
956 if (status != SANE_STATUS_GOOD || size != INQ_LEN)
957 {
958 DBG (1, "attach: inquiry for device %s failed (%s)\n", devname,
959 sane_strstatus (status));
960 dev_close (&s);
961 return status;
962 }
963
964 status = dev_wait_ready (&s);
965 dev_close (&s);
966
967 if (status != SANE_STATUS_GOOD)
968 return status;
969
970 if ((result[0] & 0x1f) != 0x06)
971 {
972 DBG (1, "attach: device %s doesn't look like a scanner at all (%d)\n",
973 devname, result[0] & 0x1f);
974 return SANE_STATUS_INVAL;
975 }
976
977 if (debug_level >= 3)
978 {
979 /* clear spaces and special chars */
980 strncpy ((SANE_String) scsi_vendor, (SANE_String) result + 8, 8);
981 scsi_vendor[8] = '\0';
982 pp = scsi_vendor + 7;
983 while (pp >= scsi_vendor && (*pp == ' ' || *pp >= 127))
984 *pp-- = '\0';
985 strncpy ((SANE_String) scsi_product, (SANE_String) result + 16, 16);
986 scsi_product[16] = '\0';
987 pp = scsi_product + 15;
988 while (pp >= scsi_product && (*pp == ' ' || *pp >= 127))
989 *pp-- = '\0';
990 strncpy ((SANE_String) scsi_revision, (SANE_String) result + 32, 4);
991 scsi_revision[4] = '\0';
992 pp = scsi_revision + 3;
993 while (pp >= scsi_revision && (*pp == ' ' || *pp >= 127))
994 *pp-- = '\0';
995 DBG (3, "attach: SCSI Vendor: `%-8s' Model: `%-16s' Rev.: `%-4s'\n",
996 scsi_vendor, scsi_product, scsi_revision);
997 DBG (3, "attach: SCSI Type: %s; ANSI rev.: %d\n",
998 ((result[0] & 0x1f) < 0x10) ?
999 scsi_device_type[result[0] & 0x1f] : "Unknown", result[2] & 0x03);
1000 DBG (3, "attach: SCSI flags: %s%s%s%s%s%s%s\n",
1001 (result[7] & 0x80) ? "RelAdr " : "",
1002 (result[7] & 0x40) ? "WBus32 " : "",
1003 (result[7] & 0x20) ? "WBus16 " : "",
1004 (result[7] & 0x10) ? "Sync " : "",
1005 (result[7] & 0x08) ? "Linked " : "",
1006 (result[7] & 0x02) ? "CmdQue " : "",
1007 (result[7] & 0x01) ? "SftRe " : "");
1008 }
1009
1010 if (debug_level >= 4)
1011 {
1012 /* print out inquiry */
1013 DBG (4, "attach: inquiry output:\n");
1014 inquiry_byte_list[0] = '\0';
1015 inquiry_text_list[0] = '\0';
1016 for (pp = result; pp < (result + INQ_LEN); pp++)
1017 {
1018 sprintf ((SANE_String) inquiry_text, "%c",
1019 (*pp < 127) && (*pp > 31) ? *pp : '.');
1020 strcat ((SANE_String) inquiry_text_list,
1021 (SANE_String) inquiry_text);
1022 sprintf ((SANE_String) inquiry_byte, " %02x", *pp);
1023 strcat ((SANE_String) inquiry_byte_list,
1024 (SANE_String) inquiry_byte);
1025 if ((pp - result) % 0x10 == 0x0f)
1026 {
1027 DBG (4, "%s %s\n", inquiry_byte_list, inquiry_text_list);
1028 inquiry_byte_list[0] = '\0';
1029 inquiry_text_list[0] = '\0';
1030 }
1031 }
1032 }
1033
1034 /* first check for new firmware format: */
1035 mustek_scanner = (strncmp ((SANE_String) result + 36, "MUSTEK", 6) == 0);
1036 if (mustek_scanner)
1037 {
1038 if (result[43] == 'M')
1039 {
1040 DBG (3, "attach: found Mustek scanner (pro series firmware "
1041 "format)\n");
1042 firmware_format = 2;
1043 model_name = result + 43;
1044 }
1045 else
1046 {
1047 DBG (3, "attach: found Mustek scanner (new firmware format)\n");
1048 firmware_format = 1;
1049 }
1050 }
1051 else
1052 {
1053 /* check for old format: */
1054 mustek_scanner = (strncmp ((SANE_String) result + 8, "MUSTEK", 6) == 0);
1055 if (mustek_scanner)
1056 {
1057 model_name = result + 16;
1058 DBG (3, "attach: found Mustek scanner (old firmware format)\n");
1059 firmware_format = 0;
1060 }
1061 else
1062 {
1063 /* Check for some non-Mustek scanners an print warning */
1064 if (strncmp ((SANE_String) result + 8, "Trust", 5) == 0)
1065 DBG (1, "attach: this is a real Trust scanner. It is not "
1066 " supported by this backend.\n");
1067 if (strncmp ((SANE_String) result + 8, "Aashima", 7) == 0)
1068 DBG (1, "attach: this is an Aashima/Teco scanner. It is not "
1069 " supported by this backend.\n");
1070 if (strncmp ((SANE_String) result + 16, "Flatbed Scanner", 15) == 0
1071 && strncmp ((SANE_String) result + 42, "TECO", 4) == 0)
1072 DBG (1, "attach: this is a Relysis/Teco scanner. It is not "
1073 " supported by this backend.\n");
1074 DBG (1, "attach: device %s doesn't look like a Mustek scanner\n",
1075 devname);
1076 return SANE_STATUS_INVAL;
1077 }
1078 }
1079
1080 /* get firmware revision as BCD number: */
1081 /* General format: x.yz */
1082 /* Newer ScanExpress scanners (ID XC06): Vxyz */
1083 if (result[33] == '.')
1084 {
1085 fw_revision =
1086 (result[32] - '0') << 8 | (result[34] - '0') << 4 | (result[35] -
1087 '0');
1088 firmware_revision_system = 0;
1089 DBG (4, "attach: old firmware revision system\n");
1090 }
1091 else
1092 {
1093 fw_revision =
1094 (result[33] - '0') << 8 | (result[34] - '0') << 4 | (result[35] -
1095 '0');
1096 firmware_revision_system = 1;
1097 DBG (4, "attach: new firmware revision system\n");
1098 }
1099 DBG (3, "attach: firmware revision %d.%02x\n",
1100 fw_revision >> 8, fw_revision & 0xff);
1101
1102 dev = malloc (sizeof (*dev));
1103 if (!dev)
1104 return SANE_STATUS_NO_MEM;
1105
1106 memcpy (dev, &new_dev, sizeof (*dev));
1107
1108 dev->name = strdup (devname);
1109 if (!dev->name)
1110 return SANE_STATUS_NO_MEM;
1111 dev->sane.name = (SANE_String_Const) dev->name;
1112 dev->sane.vendor = "Mustek";
1113 dev->sane.type = "flatbed scanner";
1114
1115 dev->x_range.min = 0;
1116 dev->y_range.min = 0;
1117 dev->x_range.quant = 0;
1118 dev->y_range.quant = 0;
1119 dev->x_trans_range.min = 0;
1120 dev->y_trans_range.min = 0;
1121 /* default to something really small to be on the safe side: */
1122 dev->x_trans_range.max = SANE_FIX (8.0 * MM_PER_INCH);
1123 dev->y_trans_range.max = SANE_FIX (5.0 * MM_PER_INCH);
1124 dev->x_trans_range.quant = 0;
1125 dev->y_trans_range.quant = 0;
1126 dev->dpi_range.min = SANE_FIX (72); /* some scanners don't like low dpi */
1127 dev->dpi_range.quant = SANE_FIX (1);
1128 /* default to 128 kB */
1129 dev->max_buffer_size = 128 * 1024; /* SCSI buffer -> use 64 k per buffer */
1130 dev->max_block_buffer_size = 1024 * 1024 * 1024;
1131 dev->firmware_format = firmware_format;
1132 dev->firmware_revision_system = firmware_revision_system;
1133
1134 DBG (3, "attach: scanner id: %.11s\n", model_name);
1135 if (strncmp ((SANE_String) model_name + 10, "PRO", 3) == 0)
1136 DBG (3, "attach: this is probably a Paragon Pro series scanner\n");
1137 else if (strncmp ((SANE_String) model_name, "MFC", 3) == 0)
1138 DBG (3, "attach: this is probably a Paragon series II scanner\n");
1139 else if (strncmp ((SANE_String) model_name, "M", 1) == 0)
1140 DBG (3,
1141 "attach: this is probably a Paragon series I or 3-pass scanner\n");
1142 else if (strncmp ((SANE_String) model_name, " C", 2) == 0)
1143 DBG (3, "attach: this is probably a ScanExpress series A4 scanner\n");
1144 else if (strncmp ((SANE_String) model_name, " L", 2) == 0)
1145 DBG (3, "attach: this is probably a ScanExpress series A3 scanner\n");
1146 else if (strncmp ((SANE_String) model_name, "XC", 2) == 0)
1147 DBG (3,
1148 "attach: this is probably a ScanExpress Plus series A4 scanner\n");
1149 else
1150 DBG (3, "attach: I am not sure what type of scanner this is\n");
1151
1152 /* Paragon 3-pass series */
1153 if (strncmp ((SANE_String) model_name, "MFS-12000CX", 11) == 0)
1154 {
1155 /* These values were measured and compared to those from the Windows
1156 driver. Tested with a Paragon MFS-12000CX v4.00 */
1157 dev->x_range.min = SANE_FIX (0.0);
1158 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1159 dev->y_range.min = SANE_FIX (0.0);
1160 dev->y_range.max = SANE_FIX (14.00 * MM_PER_INCH);
1161 dev->x_trans_range.min = SANE_FIX (1.0);
1162 dev->y_trans_range.min = SANE_FIX (1.0);
1163 dev->x_trans_range.max = SANE_FIX (205.0);
1164 dev->y_trans_range.max = SANE_FIX (255.0);
1165 dev->dpi_range.max = SANE_FIX (1200);
1166 dev->sane.model = "MFS-12000CX";
1167 }
1168 /* There are two different versions of the MFS-6000CX, one has the model
1169 name "MFS-06000CX", the other one is "MSF-06000CZ" */
1170 else if (strncmp ((SANE_String) model_name, "MFS-06000CX", 11) == 0)
1171 {
1172 /* These values were measured and tested with a Paragon MFS-6000CX
1173 v4.06 */
1174 dev->x_range.min = SANE_FIX (0.0);
1175 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1176 dev->y_range.min = SANE_FIX (0.0);
1177 dev->y_range.max = SANE_FIX (13.86 * MM_PER_INCH);
1178 dev->x_trans_range.min = SANE_FIX (1.0);
1179 dev->y_trans_range.min = SANE_FIX (2.0);
1180 dev->x_trans_range.max = SANE_FIX (203.0);
1181 dev->y_trans_range.max = SANE_FIX (255.0);
1182
1183 dev->dpi_range.max = SANE_FIX (600);
1184 dev->sane.model = "MFS-6000CX";
1185 }
1186 else if (strncmp ((SANE_String) model_name, "MSF-06000CZ", 11) == 0)
1187 {
1188 /* These values were measured and compared to those from the Windows
1189 driver. Tested with a Paragon MFS-6000CX v4.00 */
1190 dev->x_range.min = SANE_FIX (0.0);
1191 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1192 dev->y_range.min = SANE_FIX (0.0);
1193 dev->y_range.max = SANE_FIX (13.85 * MM_PER_INCH);
1194 dev->x_trans_range.min = SANE_FIX (1.0);
1195 dev->y_trans_range.min = SANE_FIX (2.0);
1196 dev->x_trans_range.max = SANE_FIX (205.0);
1197 dev->y_trans_range.max = SANE_FIX (255.0);
1198
1199 dev->dpi_range.max = SANE_FIX (600);
1200 dev->sane.model = "MFS-6000CX";
1201 }
1202
1203 /* Paragon 1-pass 14" series I */
1204
1205 /* I haven't seen a single report for this, but it is mentioned in
1206 the old man page. All reported Paragon 1200SP had a model name
1207 "MFS-12000SP" */
1208 else if (strncmp ((SANE_String) model_name, "MSF-12000SP", 11) == 0)
1209 {
1210 /* These values are not tested and mostly guessed. */
1211 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1212 dev->y_range.max = SANE_FIX (13.85 * MM_PER_INCH);
1213 dev->x_trans_range.min = SANE_FIX (1.0);
1214 dev->y_trans_range.min = SANE_FIX (1.0);
1215 dev->x_trans_range.max = SANE_FIX (200.0);
1216 dev->y_trans_range.max = SANE_FIX (250.0);
1217 dev->dpi_range.max = SANE_FIX (1200);
1218 dev->flags |= MUSTEK_FLAG_LD_NONE;
1219 dev->flags |= MUSTEK_FLAG_PARAGON_1;
1220 dev->flags |= MUSTEK_FLAG_USE_BLOCK;
1221 dev->sane.model = "MFS-12000SP";
1222 warning = SANE_TRUE;
1223 }
1224 /* MFS-8000 SP v 1.x */
1225 else if (strncmp ((SANE_String) model_name, "MSF-08000SP", 11) == 0)
1226 {
1227 /* These values were measured and compared to those from the Windows
1228 driver. Tested with a Paragon MFS-8000SP v1.20 */
1229 dev->x_range.min = SANE_FIX (0.0);
1230 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1231 dev->y_range.min = SANE_FIX (0);
1232 dev->y_range.max = SANE_FIX (355.6);
1233 dev->x_trans_range.min = SANE_FIX (1.0);
1234 dev->y_trans_range.min = SANE_FIX (1.0);
1235 dev->x_trans_range.max = SANE_FIX (205.0);
1236 dev->y_trans_range.max = SANE_FIX (255.0);
1237
1238 dev->dpi_range.max = SANE_FIX (800);
1239 /* At least scanners with firmware 1.20 need a gamma table upload
1240 in color mode, otherwise the image is red */
1241 if (fw_revision == 0x120)
1242 dev->flags |= MUSTEK_FLAG_FORCE_GAMMA;
1243 dev->flags |= MUSTEK_FLAG_PARAGON_1;
1244 dev->sane.model = "MFS-8000SP";
1245 }
1246 /* This model name exists */
1247 else if (strncmp ((SANE_String) model_name, "MSF-06000SP", 11) == 0)
1248 {
1249 /* These values were measured and compared to those from the Windows
1250 driver. Tested with a Paragon MFS-6000SP v3.12 */
1251 dev->x_range.min = SANE_FIX (0.0);
1252 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1253 dev->y_range.min = SANE_FIX (0.0);
1254 dev->y_range.max = SANE_FIX (355.6);
1255 dev->x_trans_range.min = SANE_FIX (1.0);
1256 dev->y_trans_range.min = SANE_FIX (1.0);
1257 dev->x_trans_range.max = SANE_FIX (205.0);
1258 dev->y_trans_range.max = SANE_FIX (255.0);
1259 dev->dpi_range.max = SANE_FIX (600);
1260 /* Looks like at least some versions of this scanner produce black images
1261 in gray and color mode if MUSTEK_FORCE_GAMMA is not set. At least
1262 versions 2.01, 2.02 and 2.10 are reported as having this bug. 3.12
1263 doesn't need this workaround but it doesn't harm either. */
1264 dev->flags |= MUSTEK_FLAG_FORCE_GAMMA;
1265 dev->flags |= MUSTEK_FLAG_PARAGON_1;
1266 dev->sane.model = "MFS-6000SP";
1267 }
1268
1269 /* This one was reported multiple times */
1270 else if (strncmp ((SANE_String) model_name, "MFS-12000SP", 11) == 0)
1271 {
1272 /* These values were measured and compared to those from the Windows
1273 driver. Tested with a Paragon MFS-12000SP v1.02 and v1.00 */
1274 dev->x_range.min = SANE_FIX (0.0);
1275 dev->x_range.max = SANE_FIX (217.0);
1276 dev->y_range.min = SANE_FIX (2.0);
1277 dev->y_range.max = SANE_FIX (352.0);
1278 dev->x_trans_range.min = SANE_FIX (0.0);
1279 dev->y_trans_range.min = SANE_FIX (0.0);
1280 dev->x_trans_range.max = SANE_FIX (205.0);
1281 dev->y_trans_range.max = SANE_FIX (250.0);
1282
1283 dev->dpi_range.max = SANE_FIX (1200);
1284 /* Earlier versions of this source code used MUSTEK_FLAG_LD_MFS
1285 for firmware versions < 1.02 and LD_NONE for the rest. This
1286 didn't work for my scanners. 1.00 doesn't need any LD
1287 correction, 1.02, 1.07 and 1.11 do need normal LD
1288 corrections. Maybe all != 1.00 need normal LD */
1289 dev->flags |= MUSTEK_FLAG_PARAGON_1;
1290 dev->flags |= MUSTEK_FLAG_LD_BLOCK;
1291 dev->flags |= MUSTEK_FLAG_USE_BLOCK;
1292 dev->sane.model = "MFS-12000SP";
1293 }
1294 /* MFS-8000 SP v2.x */
1295 else if (strncmp ((SANE_String) model_name, "MFS-08000SP", 11) == 0)
1296 {
1297 /* These values are tested with a MFS-08000SP v 2.04 */
1298 dev->x_range.min = SANE_FIX (0.0);
1299 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1300 dev->y_range.min = SANE_FIX (0);
1301 dev->y_range.max = SANE_FIX (355.6);
1302 dev->x_trans_range.min = SANE_FIX (1.0);
1303 dev->y_trans_range.min = SANE_FIX (1.0);
1304 dev->x_trans_range.max = SANE_FIX (205.0);
1305 dev->y_trans_range.max = SANE_FIX (255.0);
1306
1307 dev->dpi_range.max = SANE_FIX (800);
1308 /* At least scanners with firmware 1.20 need a gamma table upload
1309 in color mode, otherwise the image is red */
1310 if (fw_revision == 0x120)
1311 dev->flags |= MUSTEK_FLAG_FORCE_GAMMA;
1312 dev->flags |= MUSTEK_FLAG_PARAGON_1;
1313 dev->sane.model = "MFS-8000SP";
1314 }
1315 /* I have never seen one of those */
1316 else if (strncmp ((SANE_String) model_name, "MFS-06000SP", 11) == 0)
1317 {
1318 /* These values are not tested. */
1319 dev->x_range.max = SANE_FIX (8.5 * MM_PER_INCH);
1320 dev->y_range.max = SANE_FIX (13.84 * MM_PER_INCH);
1321 /* copied from MSF-06000SP */
1322 dev->x_trans_range.min = SANE_FIX (1.0);
1323 dev->y_trans_range.min = SANE_FIX (1.0);
1324 dev->x_trans_range.max = SANE_FIX (205.0);
1325 dev->y_trans_range.max = SANE_FIX (255.0);
1326 dev->dpi_range.max = SANE_FIX (600);
1327 dev->flags |= MUSTEK_FLAG_PARAGON_1;
1328 dev->sane.model = "MFS-6000SP";
1329 warning = SANE_TRUE;
1330 }
1331
1332 /* Paragon 1-pass A4 series II */
1333 else if (strncmp ((SANE_String) model_name, "MFC-08000CZ", 11) == 0)
1334 {
1335 /* These values were measured and compared to those from the Windows
1336 driver. Tested with a Paragon 800 II SP v1.06. */
1337 dev->x_range.min = SANE_FIX (1.5);
1338 dev->x_range.max = SANE_FIX (218.0);
1339 dev->y_range.min = SANE_FIX (0.0);
1340 dev->y_range.max = SANE_FIX (293.0);
1341 dev->x_trans_range.min = SANE_FIX (0.0);
1342 dev->y_trans_range.min = SANE_FIX (0.0);
1343 dev->x_trans_range.max = SANE_FIX (205.0);
1344 dev->y_trans_range.max = SANE_FIX (254.0);
1345
1346 dev->dpi_range.max = SANE_FIX (800);
1347 dev->max_block_buffer_size = 2 * 1024 * 1024;
1348
1349 dev->flags |= MUSTEK_FLAG_PARAGON_2;
1350 dev->flags |= MUSTEK_FLAG_LD_BLOCK;
1351 dev->flags |= MUSTEK_FLAG_USE_BLOCK;
1352 dev->sane.model = "800S/800 II SP";
1353 }
1354 else if (strncmp ((SANE_String) model_name, "MFC-06000CZ", 11) == 0)
1355 {
1356 /* These values were measured and compared to those from the
1357 Windows driver. Tested with a Paragon 600 II CD, a Paragon
1358 MFC-600S and a Paragon 600 II N. */
1359 dev->x_range.min = SANE_FIX (0.0);
1360 dev->x_range.max = SANE_FIX (218.0);
1361 dev->y_range.min = SANE_FIX (0.0);
1362 dev->y_range.max = SANE_FIX (293.0);
1363 dev->x_trans_range.min = SANE_FIX (0.0);
1364 dev->y_trans_range.min = SANE_FIX (0.0);
1365 dev->x_trans_range.max = SANE_FIX (201.0);
1366 dev->y_trans_range.max = SANE_FIX (257.0);
1367
1368 dev->dpi_range.max = SANE_FIX (600);
1369 /* This model comes in a non-scsi version, too. It is supplied
1370 with its own parallel-port like adapter, an AB306N. Two
1371 firmware revisions are known: 1.01 and 2.00. Each needs its
1372 own line-distance correction code. */
1373 if (dev->flags & MUSTEK_FLAG_N)
1374 {
1375 if (fw_revision < 0x200)
1376 dev->flags |= MUSTEK_FLAG_LD_N1;
1377 else
1378 dev->flags |= MUSTEK_FLAG_LD_N2;
1379 dev->x_trans_range.min = SANE_FIX (33.0);
1380 dev->y_trans_range.min = SANE_FIX (62.0);
1381 dev->x_trans_range.max = SANE_FIX (183.0);
1382 dev->y_trans_range.max = SANE_FIX (238.0);
1383 dev->max_block_buffer_size = 1024 * 1024 * 1024;
1384 dev->sane.model = "600 II N";
1385 }
1386 else if (dev->flags & MUSTEK_FLAG_SCSI_PP)
1387 {
1388 /* FIXME; experiment with different line distance codes later */
1389 dev->dpi_range.min = SANE_FIX (75.0);
1390 dev->flags |= MUSTEK_FLAG_LD_NONE;
1391 dev->max_block_buffer_size = 2 * 1024 * 1024;
1392 dev->sane.model = "600 II EP";
1393 }
1394 else
1395 {
1396 dev->sane.model = "600S/600 II CD";
1397 dev->flags |= MUSTEK_FLAG_PARAGON_2;
1398 dev->flags |= MUSTEK_FLAG_LD_BLOCK;
1399 dev->flags |= MUSTEK_FLAG_USE_BLOCK;
1400 dev->max_block_buffer_size = 2 * 1024 * 1024;
1401 }
1402 }
1403
1404 /* ScanExpress and ScanMagic series */
1405 else if (strncmp ((SANE_String) model_name, " C03", 4) == 0)
1406 {
1407 /* These values were measured and compared to those from the Windows
1408 driver. Tested with a ScannExpress 6000SP 1.00 */
1409 dev->x_range.max = SANE_FIX (215);
1410 dev->y_range.min = SANE_FIX (0);
1411 dev->y_range.max = SANE_FIX (293);
1412
1413 dev->x_trans_range.min = SANE_FIX (0);
1414 dev->y_trans_range.min = SANE_FIX (0);
1415 dev->x_trans_range.max = SANE_FIX (150.0);
1416 dev->y_trans_range.max = SANE_FIX (175.0);
1417
1418 dev->dpi_range.max = SANE_FIX (600);
1419 dev->dpi_range.min = SANE_FIX (60);
1420 dev->flags |= MUSTEK_FLAG_SE;
1421 /* At least the SE 6000SP with firmware 1.00 limits its
1422 x-resolution to 300 dpi and does *no* interpolation at higher
1423 resolutions. So this has to be done in software. */
1424 dev->flags |= MUSTEK_FLAG_ENLARGE_X;
1425 dev->sane.model = "ScanExpress 6000SP";
1426 }
1427 /* There are two different versions of the ScanExpress 12000SP, one
1428 has the model name " C06", the other one is "XC06". The latter
1429 seems to be used in the newer "Plus" models.
1430 Also there is the Mustek ScanExpress 1200 FS, which looks similar to the
1431 ScanExpress 12000 SP but has an "F" instead of the "V" in the
1432 firmware version.
1433 */
1434 else if (strncmp ((SANE_String) model_name, " C06", 4) == 0)
1435 {
1436 if (result[32] == 'F')
1437 {
1438 /* Mustek ScanExpress 1200 FS. Completely untested. */
1439 dev->x_range.min = SANE_FIX (0);
1440 dev->y_range.min = SANE_FIX (0);
1441 dev->x_range.max = SANE_FIX (215.9);
1442 dev->y_range.max = SANE_FIX (291.2);
1443
1444 dev->x_trans_range.min = SANE_FIX (0);
1445 dev->y_trans_range.min = SANE_FIX (0);
1446 dev->x_trans_range.max = SANE_FIX (150.0);
1447 dev->y_trans_range.max = SANE_FIX (175.0);
1448
1449 dev->dpi_range.max = SANE_FIX (1200);
1450 dev->dpi_range.min = SANE_FIX (60);
1451 dev->flags |= MUSTEK_FLAG_SE;
1452 /* The ScanExpress models limit their x-resolution to 600 dpi
1453 and do *no* interpolation at higher resolutions. So this has
1454 to be done in software. */
1455 dev->flags |= MUSTEK_FLAG_ENLARGE_X;
1456 dev->flags |= MUSTEK_FLAG_COVER_SENSOR;
1457 dev->sane.model = "ScanExpress 12000 FS (untested)";
1458 }
1459 else
1460 {
1461 /* These values were measured and compared to those from the Windows
1462 driver. Tested with a ScaneExpress 12000SP 2.02 and a ScanMagic
1463 9636S v 1.01 */
1464 dev->x_range.min = SANE_FIX (0);
1465 dev->y_range.min = SANE_FIX (0);
1466 dev->x_range.max = SANE_FIX (215.9);
1467 dev->y_range.max = SANE_FIX (291.2);
1468
1469 dev->x_trans_range.min = SANE_FIX (0);
1470 dev->y_trans_range.min = SANE_FIX (0);
1471 dev->x_trans_range.max = SANE_FIX (150.0);
1472 dev->y_trans_range.max = SANE_FIX (175.0);
1473
1474 dev->dpi_range.max = SANE_FIX (1200);
1475 dev->dpi_range.min = SANE_FIX (60);
1476 dev->flags |= MUSTEK_FLAG_SE;
1477 /* The ScanExpress models limit their x-resolution to 600 dpi
1478 and do *no* interpolation at higher resolutions. So this has
1479 to be done in software. */
1480 dev->flags |= MUSTEK_FLAG_ENLARGE_X;
1481 dev->flags |= MUSTEK_FLAG_COVER_SENSOR;
1482 dev->sane.model = "ScanExpress 12000SP";
1483 }
1484 }
1485 else if (strncmp ((SANE_String) model_name, "XC06", 4) == 0)
1486 {
1487 /* These values are tested with a SE 12000 SP Plus v 1.01 */
1488 dev->x_range.max = SANE_FIX (216);
1489 dev->y_range.min = SANE_FIX (0);
1490 dev->y_range.max = SANE_FIX (294.5);
1491
1492 dev->x_trans_range.min = SANE_FIX (0);
1493 dev->y_trans_range.min = SANE_FIX (0);
1494 dev->x_trans_range.max = SANE_FIX (152.0);
1495 dev->y_trans_range.max = SANE_FIX (177.0);
1496
1497 dev->dpi_range.max = SANE_FIX (1200);
1498 dev->dpi_range.min = SANE_FIX (60);
1499
1500 dev->flags |= MUSTEK_FLAG_SE;
1501 dev->flags |= MUSTEK_FLAG_SE_PLUS;
1502 /* The ScanExpress models limit their x-resolution to 600 dpi
1503 and do *no* interpolation at higher resolutions. So this has
1504 to be done in software. */
1505 dev->flags |= MUSTEK_FLAG_ENLARGE_X;
1506 dev->flags |= MUSTEK_FLAG_COVER_SENSOR;
1507 dev->sane.model = "ScanExpress 12000SP Plus";
1508 }
1509 /* ScanExpress A3 SP */
1510 else if (strncmp ((SANE_String) model_name, " L03", 4) == 0)
1511 {
1512 /* These values were measured with a ScannExpress A3 SP 2.00 */
1513 dev->x_range.max = SANE_FIX (297);
1514 dev->y_range.min = SANE_FIX (0);
1515 dev->y_range.max = SANE_FIX (430);
1516
1517 /* TA couldn't be tested due to lack of equipment. So At least
1518 the TA IV (A4 size) is supported */
1519 dev->x_trans_range.min = SANE_FIX (0);
1520 dev->y_trans_range.min = SANE_FIX (0);
1521 dev->x_trans_range.max = SANE_FIX (150.0);
1522 dev->y_trans_range.max = SANE_FIX (175.0);
1523
1524 dev->dpi_range.max = SANE_FIX (600);
1525 dev->dpi_range.min = SANE_FIX (60);
1526 dev->flags |= MUSTEK_FLAG_SE;
1527 /* The ScanExpress models limit their x-resolution to 300 dpi
1528 and do *no* interpolation at higher resolutions. So this has
1529 to be done in software. */
1530 dev->flags |= MUSTEK_FLAG_ENLARGE_X;
1531 dev->flags |= MUSTEK_FLAG_COVER_SENSOR;
1532 dev->sane.model = "ScanExpress A3 SP";
1533 }
1534 /* Paragon 1200 SP Pro */
1535 else if (strncmp ((SANE_String) model_name, "MFS-1200SPPRO", 13) == 0)
1536 {
1537 /* These values were measured with a Paragon 1200 SP Pro v2.01 */
1538 dev->x_range.max = SANE_FIX (8.6 * MM_PER_INCH);
1539 dev->y_range.max = SANE_FIX (13.70 * MM_PER_INCH);
1540 dev->dpi_range.max = SANE_FIX (1200);
1541 dev->sane.model = "1200 SP PRO";
1542 dev->flags |= MUSTEK_FLAG_LD_NONE;
1543 dev->flags |= MUSTEK_FLAG_ENLARGE_X;
1544 }
1545 /* No documentation, but it works: Paragon 1200 A3 PRO */
1546 else if (strncmp ((SANE_String) model_name, "MFS-1200A3PRO", 13) == 0)
1547 {
1548 /* These values were measured and compared to those from the Windows
1549 driver. Tested with a Paragon 1200 A3 Pro v1.10 */
1550 dev->x_range.max = SANE_FIX (11.7 * MM_PER_INCH);
1551 dev->y_range.max = SANE_FIX (424);
1552 dev->dpi_range.max = SANE_FIX (1200);
1553 dev->sane.model = "1200 A3 PRO";
1554 dev->flags |= MUSTEK_FLAG_LD_NONE;
1555 dev->flags |= MUSTEK_FLAG_ENLARGE_X;
1556 }
1557 else
1558 {
1559 DBG (0, "attach: this Mustek scanner (ID: %s) is not supported yet\n",
1560 model_name);
1561 DBG (0, "attach: please set the debug level to 5 and send a debug "
1562 "report\n");
1563 DBG (0, "attach: to henning@meier-geinitz.de (export "
1564 "SANE_DEBUG_MUSTEK=5\n");
1565 DBG (0, "attach: scanimage -L 2>debug.txt). Thank you.\n");
1566 free (dev);
1567 return SANE_STATUS_INVAL;
1568 }
1569
1570 if (dev->flags & MUSTEK_FLAG_SE)
1571 {
1572 DBG (3, "attach: this is a single-pass scanner\n");
1573 if (result[63] & (1 << 6))
1574 {
1575 dev->flags |= MUSTEK_FLAG_TA;
1576 DBG (3, "attach: scanner supports transparency adapter (TA)\n");
1577 }
1578 }
1579 else
1580 {
1581 if (result[57] & (1 << 6))
1582 {
1583 DBG (3, "attach: this is a single-pass scanner\n");
1584 if (dev->flags & MUSTEK_FLAG_LD_NONE)
1585 DBG (4,
1586 "attach: scanner doesn't need line-distance correction\n");
1587 else if (dev->flags & MUSTEK_FLAG_LD_N1)
1588 DBG (4, "attach: scanner has N1 line-distance correction\n");
1589 else if (dev->flags & MUSTEK_FLAG_LD_N2)
1590 DBG (4, "attach: scanner has N2 line-distance correction\n");
1591 else if (dev->flags & MUSTEK_FLAG_LD_BLOCK)
1592 DBG (4, "attach: scanner has block line-distance correction\n");
1593 else
1594 DBG (4, "attach: scanner has normal line-distance correction\n");
1595 }
1596 else
1597 {
1598 dev->flags |= MUSTEK_FLAG_THREE_PASS;
1599 /* three-pass scanners quantize to 0.5% of the maximum resolution: */
1600 dev->dpi_range.quant = dev->dpi_range.max / 200;
1601 dev->dpi_range.min = dev->dpi_range.quant;
1602 DBG (3, "attach: this is a three-pass scanner\n");
1603 }
1604 if (result[57] & (1 << 5))
1605 {
1606 DBG (3, "attach: this is a professional series scanner\n");
1607 dev->flags |= MUSTEK_FLAG_PRO;
1608 status = dev_open (devname, &s, sense_handler);
1609 if (status == SANE_STATUS_GOOD)
1610 {
1611 if (ta_available_pro (&s))
1612 {
1613 dev->flags |= MUSTEK_FLAG_TA;
1614 DBG (3, "attach: found transparency adapter (TA)\n");
1615 }
1616 dev_close (&s);
1617 }
1618 else
1619 {
1620 DBG (1, "attach: couldn't open device: %s\n",
1621 sane_strstatus (status));
1622 return status;
1623 }
1624 }
1625 if (result[63] & (1 << 2))
1626 {
1627 dev->flags |= MUSTEK_FLAG_ADF;
1628 DBG (3, "attach: found automatic document feeder (ADF)\n");
1629 if (result[63] & (1 << 3))
1630 {
1631 dev->flags |= MUSTEK_FLAG_ADF_READY;
1632 DBG (4, "attach: automatic document feeder is ready\n");
1633 }
1634 else
1635 {
1636 DBG (4, "attach: automatic document feeder is out of "
1637 "documents\n");
1638 }
1639 }
1640
1641 if (result[63] & (1 << 6))
1642 {
1643 dev->flags |= MUSTEK_FLAG_TA;
1644 DBG (3, "attach: found transparency adapter (TA)\n");
1645 }
1646 }
1647
1648 if (dev->flags & MUSTEK_FLAG_COVER_SENSOR)
1649 {
1650 if (result[62] & (1 << 0))
1651 DBG (4, "attach: scanner cover is closed\n");
1652 else
1653 DBG (4, "attach: scanner cover is open\n");
1654 }
1655
1656 if (warning == SANE_TRUE)
1657 {
1658 DBG (0,
1659 "WARNING: Your scanner was detected by the SANE Mustek backend, "
1660 "but\n it is not fully tested. It may or may not work. Be "
1661 "careful and read\n the PROBLEMS file in the sane directory. "
1662 "Please set the debug level of this\n backend to maximum "
1663 "(export SANE_DEBUG_MUSTEK=255) and send the output of\n "
1664 "scanimage -L to the SANE mailing list sane-devel@alioth-lists.debian.net. "
1665 "Please include\n the exact model name of your scanner and to "
1666 "which extend it works.\n");
1667 }
1668
1669 DBG (2, "attach: found Mustek %s %s, %s%s%s%s\n",
1670 dev->sane.model, dev->sane.type,
1671 (dev->flags & MUSTEK_FLAG_THREE_PASS) ? "3-pass" : "1-pass",
1672 (dev->flags & MUSTEK_FLAG_ADF) ? ", ADF" : "",
1673 (dev->flags & MUSTEK_FLAG_TA) ? ", TA" : "",
1674 (dev->flags & MUSTEK_FLAG_SE) ? ", SE" : "");
1675
1676 ++num_devices;
1677 dev->next = first_dev;
1678 first_dev = dev;
1679
1680 if (devp)
1681 *devp = dev;
1682 return SANE_STATUS_GOOD;
1683 }
1684
1685 static size_t
max_string_size(const SANE_String_Const strings[])1686 max_string_size (const SANE_String_Const strings[])
1687 {
1688 size_t size, max_size = 0;
1689 SANE_Int i;
1690
1691 for (i = 0; strings[i]; ++i)
1692 {
1693 size = strlen (strings[i]) + 1;
1694 if (size > max_size)
1695 max_size = size;
1696 }
1697 return max_size;
1698 }
1699
1700 static SANE_Status
constrain_value(Mustek_Scanner * s, SANE_Int option, void *value, SANE_Int * info)1701 constrain_value (Mustek_Scanner * s, SANE_Int option, void *value,
1702 SANE_Int * info)
1703 {
1704 SANE_Fixed w, dpi;
1705 SANE_Status status;
1706
1707 if (value)
1708 w = *(SANE_Fixed *) value;
1709 else
1710 w = 0;
1711
1712 if (option == OPT_RESOLUTION)
1713 {
1714 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
1715 {
1716 /* The three pass scanners use a 0.5% of the maximum resolution
1717 increment for resolutions less than or equal to half of the
1718 maximum resolution. The MFS-06000CX uses a 5% of the maximum
1719 resolution increment for larger resolutions. The models
1720 MFS-12000CX and MSF-06000CZ use 1% of the maximum resolution.
1721 We can't represent this easily in SANE, so the constraint is
1722 simply for 0.5% and then we round to the 5% or 1% increments
1723 if necessary. */
1724 SANE_Fixed max_dpi, quant, half_res;
1725
1726 /*w = *(SANE_Word *) value; */
1727 max_dpi = s->hw->dpi_range.max;
1728 half_res = max_dpi / 2;
1729
1730 if (w > half_res)
1731 {
1732 /* quantizize to 1% step */
1733 quant = max_dpi / 100;
1734
1735 dpi = (w + quant / 2) / quant;
1736 dpi *= quant;
1737 if (dpi != w)
1738 {
1739 *(SANE_Word *) value = dpi;
1740 if (info)
1741 *info |= SANE_INFO_INEXACT;
1742 }
1743 }
1744
1745 }
1746 }
1747
1748 status = sanei_constrain_value (s->opt + option, value, info);
1749 if (s->opt[option].type == SANE_TYPE_FIXED)
1750 DBG (5, "constrain_value: %s = %.2f (was %.2f)\n", s->opt[option].name,
1751 SANE_UNFIX (*(SANE_Word *) value), SANE_UNFIX (w));
1752 return status;
1753 }
1754
1755 /* Quantize s->val[OPT_RESOLUTION].w and return the resolution code for the
1756 quantized resolution. Quantization depends on scanner type (single
1757 pass vs. three-pass) and resolution */
1758 static SANE_Int
encode_resolution(Mustek_Scanner * s)1759 encode_resolution (Mustek_Scanner * s)
1760 {
1761 SANE_Fixed max_dpi, dpi;
1762 SANE_Int code, mode = 0;
1763
1764 dpi = s->val[OPT_RESOLUTION].w;
1765
1766 if (!(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
1767 {
1768 code = dpi >> SANE_FIXED_SCALE_SHIFT;
1769 }
1770 else
1771 {
1772 SANE_Fixed quant, half_res;
1773
1774 max_dpi = s->hw->dpi_range.max;
1775 half_res = max_dpi / 2;
1776
1777 if (dpi <= half_res)
1778 {
1779 /* quantizize to 0.5% step */
1780 quant = max_dpi / 200;
1781 }
1782 else
1783 {
1784 /* quantizize to 1% step */
1785 quant = max_dpi / 100;
1786 mode = 0x100; /* indicate 5% or 1% quantization */
1787 }
1788
1789 code = (dpi + quant / 2) / quant;
1790 if (code < 1)
1791 code = 1;
1792
1793 }
1794 DBG (5, "encode_resolution: code = 0x%x (%d); mode = %x\n", code, code,
1795 mode);
1796 return code | mode;
1797 }
1798
1799 static SANE_Int
encode_percentage(Mustek_Scanner * s, double value)1800 encode_percentage (Mustek_Scanner * s, double value)
1801 {
1802 SANE_Int max, code, sign = 0;
1803
1804 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
1805 {
1806 code = (int) ((value / 100.0 * 12) + 12.5);
1807 max = 0x18;
1808 }
1809 else
1810 {
1811 if (value < 0.0)
1812 {
1813 value = -value;
1814 sign = 0x80;
1815 }
1816 code = (int) (value / 100.0 * 127 + 0.5);
1817 code |= sign;
1818 max = 0xff;
1819 }
1820 if (code > max)
1821 code = max;
1822 if (code < 0)
1823 code = 0x00;
1824 return code;
1825 }
1826
1827 /* encode halftone pattern type and size */
1828 static SANE_Status
encode_halftone(Mustek_Scanner * s)1829 encode_halftone (Mustek_Scanner * s)
1830 {
1831 SANE_String selection = s->val[OPT_HALFTONE_DIMENSION].s;
1832 SANE_Int i = 0;
1833
1834 while ((halftone_list[i] != 0) && (strcmp (selection, halftone_list[i]) != 0))
1835 {
1836 i++;
1837 }
1838 if (halftone_list[i] == 0)
1839 return SANE_STATUS_INVAL;
1840
1841 if (i < 0x0c) /* standard pattern */
1842 {
1843 s->custom_halftone_pattern = SANE_FALSE;
1844 s->halftone_pattern_type = i;
1845 }
1846 else /* custom pattern */
1847 {
1848 s->custom_halftone_pattern = SANE_TRUE;
1849 i -= 0x0c;
1850 i = 8 - i;
1851 if (i < 8)
1852 i--;
1853 i = i + (i << 4);
1854 s->halftone_pattern_type = i;
1855 }
1856
1857 DBG (5, "encode_halftone: %s pattern type %x\n",
1858 s->custom_halftone_pattern ? "custom" : "standard",
1859 s->halftone_pattern_type);
1860 return SANE_STATUS_GOOD;
1861 }
1862
1863 /* Paragon series */
1864 static SANE_Status
area_and_windows(Mustek_Scanner * s)1865 area_and_windows (Mustek_Scanner * s)
1866 {
1867 SANE_Byte cmd[117], *cp;
1868 SANE_Int i, offset;
1869
1870 /* setup SCSI command (except length): */
1871 memset (cmd, 0, sizeof (cmd));
1872 cmd[0] = MUSTEK_SCSI_AREA_AND_WINDOWS;
1873
1874 cp = cmd + 6;
1875
1876 /* Some scanners need a larger scanarea for line-distance correction */
1877 offset = 0;
1878 if (((s->hw->flags & MUSTEK_FLAG_LD_N1)
1879 || ((s->hw->flags & MUSTEK_FLAG_LD_BLOCK)
1880 && (s->hw->flags & MUSTEK_FLAG_PARAGON_1)))
1881 && (s->mode & MUSTEK_MODE_COLOR))
1882 offset = MAX_LINE_DIST;
1883
1884 /* fill in frame header: */
1885
1886 if (s->hw->flags & MUSTEK_FLAG_USE_EIGHTS)
1887 {
1888 double eights_per_mm = 8 / MM_PER_INCH;
1889 SANE_Int tlx, tly, brx, bry;
1890 /*
1891 * The MSF-06000CZ seems to lock-up if the pixel-unit is used.
1892 * Using 1/8" works.
1893 * This doesn't seem to be true with the current scheme.
1894 * This code isn't used at the moment. <henning@meier-geinitz.de>
1895 */
1896 *cp++ = ((s->mode & MUSTEK_MODE_LINEART) ? 0x00 : 0x01);
1897
1898 tlx = SANE_UNFIX (s->val[OPT_TL_X].w) * eights_per_mm + 0.5;
1899 tly = SANE_UNFIX (s->val[OPT_TL_Y].w) * eights_per_mm + 0.5;
1900 brx = SANE_UNFIX (s->val[OPT_BR_X].w) * eights_per_mm + 0.5;
1901 bry = SANE_UNFIX (s->val[OPT_BR_Y].w) * eights_per_mm + 0.5;
1902 STORE16L (cp, tlx);
1903 STORE16L (cp, tly);
1904 STORE16L (cp, brx);
1905 STORE16L (cp, bry);
1906 DBG (5, "area_and_windows: tlx=%d (%d mm); tly=%d (%d mm); "
1907 "brx=%d (%d mm); bry=%d (%d mm)\n", tlx,
1908 (int) (tlx / eights_per_mm), tly, (int) (tly / eights_per_mm), brx,
1909 (int) (brx / eights_per_mm), bry, (int) (bry / eights_per_mm));
1910 }
1911 else
1912 {
1913 double pixels_per_mm = SANE_UNFIX (s->hw->dpi_range.max) / MM_PER_INCH;
1914 SANE_Int tlx, tly, brx, bry;
1915
1916 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
1917 /* 3pass scanners use 1/2 of the max resolution as base */
1918 pixels_per_mm /= 2;
1919
1920 /* pixel unit and halftoning: */
1921 *cp++ = 0x8 | ((s->mode & MUSTEK_MODE_LINEART) ? 0x00 : 0x01);
1922
1923 /* fill in scanning area: */
1924 if (strcmp (s->val[OPT_SOURCE].s, "Automatic Document Feeder") == 0)
1925 {
1926 /* must mirror the x coordinates */
1927 brx = SANE_UNFIX (s->hw->x_range.max - s->val[OPT_TL_X].w)
1928 * pixels_per_mm + 0.5;
1929 tlx = SANE_UNFIX (s->hw->x_range.max - s->val[OPT_BR_X].w)
1930 * pixels_per_mm + 0.5;
1931 }
1932 else
1933 {
1934 tlx = SANE_UNFIX (s->val[OPT_TL_X].w) * pixels_per_mm + 0.5;
1935 brx = SANE_UNFIX (s->val[OPT_BR_X].w) * pixels_per_mm + 0.5;
1936
1937 }
1938 tly = SANE_UNFIX (s->val[OPT_TL_Y].w) * pixels_per_mm + 0.5;
1939 bry = SANE_UNFIX (s->val[OPT_BR_Y].w) * pixels_per_mm + 0.5 + offset;
1940 STORE16L (cp, tlx);
1941 STORE16L (cp, tly);
1942 STORE16L (cp, brx);
1943 STORE16L (cp, bry);
1944 DBG (5, "area_and_windows: tlx=%d (%d mm); tly=%d (%d mm); "
1945 "brx=%d (%d mm); bry=%d (%d mm)\n", tlx,
1946 (int) (tlx / pixels_per_mm), tly, (int) (tly / pixels_per_mm), brx,
1947 (int) (brx / pixels_per_mm), bry, (int) (bry / pixels_per_mm));
1948 }
1949
1950 if (s->custom_halftone_pattern)
1951 {
1952 *cp++ = 0x40; /* mark presence of user pattern */
1953 *cp++ = s->halftone_pattern_type; /* set pattern length */
1954 for (i = 0; i < (s->halftone_pattern_type & 0x0f) *
1955 ((s->halftone_pattern_type >> 4) & 0x0f); ++i)
1956 *cp++ = s->val[OPT_HALFTONE_PATTERN].wa[i];
1957 }
1958
1959 cmd[4] = (cp - cmd) - 6;
1960
1961 return dev_cmd (s, cmd, (cp - cmd), 0, 0);
1962 }
1963
1964 /* ScanExpress */
1965 static SANE_Status
set_window_se(Mustek_Scanner * s, SANE_Int lamp)1966 set_window_se (Mustek_Scanner * s, SANE_Int lamp)
1967 {
1968 SANE_Byte cmd[58], *cp;
1969 double pixels_per_mm;
1970 SANE_Int offset;
1971 SANE_Int tlx, tly, width, height;
1972
1973 /* setup SCSI command (except length): */
1974 memset (cmd, 0, sizeof (cmd));
1975 cmd[0] = MUSTEK_SCSI_SET_WINDOW;
1976 cp = cmd + CDB_SIZE (MUSTEK_SCSI_SET_WINDOW); /* skip command block */
1977
1978 if (s->mode & MUSTEK_MODE_COLOR)
1979 {
1980 /* We have to increase the specified resolution to the next */
1981 /* "standard" resolution due to a firmware bug(?) in color mode */
1982 /* It's possible to scan in 36, 75, 100, 150, 200, 250, 300, */
1983 /* 400, 500, 600, 900, 1200 dpi but the speed is only different */
1984 /* with 36, 150, 300, 600, 1200 dpi. */
1985 /* Additionally we must increase the window length slightly to */
1986 /* compensate for different line counts for r/g/b */
1987 const SANE_Int resolution_list[] = { 36, 150, 300, 600, 1200, 0 };
1988 SANE_Int entry = 0;
1989
1990 while (resolution_list[entry] < s->resolution_code)
1991 entry++;
1992 s->ld.peak_res = resolution_list[entry];
1993
1994 offset = MAX_LINE_DIST; /* distance r/b lines */
1995 }
1996 else
1997 {
1998 /* In gray and lineart modes all resolutions are possible */
1999 s->ld.peak_res = s->resolution_code;
2000 offset = 0;
2001 }
2002 DBG (5, "set_window_se: hardware resolution is %d dpi; offset is %d\n",
2003 s->ld.peak_res, offset);
2004
2005 STORE16B (cp, 0); /* window identifier */
2006 STORE16B (cp, s->ld.peak_res);
2007 /* x and y resolution */
2008 STORE16B (cp, 0); /* not used acc. to specs */
2009
2010 pixels_per_mm = SANE_UNFIX (s->hw->dpi_range.max) / MM_PER_INCH;
2011
2012 /* fill in scanning area, begin and length(!) */
2013 if ((strcmp (s->val[OPT_SOURCE].s, "Transparency Adapter") == 0) &&
2014 !(s->hw->flags & MUSTEK_FLAG_TA))
2015 {
2016 /* need to add the start values of the transparency adapter */
2017 tlx = (SANE_UNFIX (s->val[OPT_TL_X].w) + 33.0) * pixels_per_mm + 0.5;
2018 tly = (SANE_UNFIX (s->val[OPT_TL_Y].w) + 60.0) * pixels_per_mm + 0.5;
2019 DBG (5, "set_window_se: added offset for transparency adapter\n");
2020 }
2021 else
2022 {
2023 /* no transparency adapter selected or calculation done in firmware */
2024 tlx = SANE_UNFIX (s->val[OPT_TL_X].w) * pixels_per_mm + 0.5;
2025 tly = SANE_UNFIX (s->val[OPT_TL_Y].w) * pixels_per_mm + 0.5;
2026 }
2027 width = (SANE_UNFIX (s->val[OPT_BR_X].w) - SANE_UNFIX (s->val[OPT_TL_X].w))
2028 * pixels_per_mm + 0.5;
2029 height = (SANE_UNFIX (s->val[OPT_BR_Y].w) - SANE_UNFIX (s->val[OPT_TL_Y].w))
2030 * pixels_per_mm + 0.5 + offset;
2031
2032 DBG (5, "set_window_se: tlx=%d (%d mm); tly=%d (%d mm); width=%d (%d mm); "
2033 "height=%d (%d mm)\n", tlx, (int) (tlx / pixels_per_mm), tly,
2034 (int) (tly / pixels_per_mm), width, (int) (width / pixels_per_mm),
2035 height, (int) (height / pixels_per_mm));
2036
2037
2038 STORE32B (cp, tlx);
2039 STORE32B (cp, tly);
2040 STORE32B (cp, width);
2041 STORE32B (cp, height);
2042
2043 *cp++ = 0x00; /* brightness, not impl. */
2044 *cp++ = 0x80; /* threshold, not impl. */
2045 *cp++ = 0x00; /* contrast, not impl. */
2046
2047 /* Note that 'image composition' has no meaning for the SE series */
2048 /* Mode selection is accomplished solely by bits/pixel (1, 8, 24) */
2049 if (s->mode & MUSTEK_MODE_COLOR)
2050 {
2051 *cp++ = 0x05; /* actually not used! */
2052 *cp++ = 24; /* 24 bits/pixel in color mode */
2053 }
2054 else if (s->mode & MUSTEK_MODE_GRAY)
2055 {
2056 *cp++ = 0x02; /* actually not used! */
2057 *cp++ = 8; /* 8 bits/pixel in gray mode */
2058 }
2059 else
2060 {
2061 *cp++ = 0x00; /* actually not used! */
2062 *cp++ = 1; /* 1 bit/pixel in lineart mode */
2063 }
2064
2065 cp += 14; /* skip reserved bytes */
2066 *cp++ = lamp; /* 0 = normal, 1 = on, 2 = off */
2067
2068 if ((s->hw->flags & MUSTEK_FLAG_TA)
2069 && (strcmp (s->val[OPT_SOURCE].s, "Transparency Adapter") == 0))
2070 *cp++ = 1;
2071 else
2072 *cp++ = 0;
2073 cp += 5; /* skip reserved bytes */
2074
2075 cmd[8] = cp - cmd - CDB_SIZE (MUSTEK_SCSI_SET_WINDOW);
2076 return dev_cmd (s, cmd, (cp - cmd), 0, 0);
2077 }
2078
2079 /* Pro series */
2080 static SANE_Status
set_window_pro(Mustek_Scanner * s)2081 set_window_pro (Mustek_Scanner * s)
2082 {
2083 SANE_Byte cmd[20], *cp;
2084 double pixels_per_mm;
2085
2086 memset (cmd, 0, sizeof (cmd));
2087 cmd[0] = MUSTEK_SCSI_SET_WINDOW;
2088 if (strcmp (s->hw->sane.model, "1200 SP PRO") == 0)
2089 cmd[8] = 0x09;
2090 else
2091 cmd[8] = 0x0a;
2092
2093 cp = cmd + CDB_SIZE (MUSTEK_SCSI_SET_WINDOW); /* skip command block */
2094
2095 *cp++ = 0; /* what's this? */
2096 pixels_per_mm = SANE_UNFIX (s->hw->dpi_range.max) / MM_PER_INCH;
2097
2098 /* The next for 16 bit values are x0, y0, x1, y1 in pixels at max res */
2099 STORE16L (cp, SANE_UNFIX (s->val[OPT_TL_X].w) * pixels_per_mm + 0.5);
2100 STORE16L (cp, SANE_UNFIX (s->val[OPT_TL_Y].w) * pixels_per_mm + 0.5);
2101 STORE16L (cp, SANE_UNFIX (s->val[OPT_BR_X].w) * pixels_per_mm + 0.5);
2102 STORE16L (cp, SANE_UNFIX (s->val[OPT_BR_Y].w) * pixels_per_mm + 0.5);
2103
2104 if (strcmp (s->hw->sane.model, "1200 SP PRO") != 0)
2105 *cp++ = lamp_off_time; /* Only needed for A3 Pro, default: 60 minutes until lamp-off */
2106 DBG (5, "set_window_pro\n");
2107
2108 return dev_cmd (s, cmd, (cp - cmd), 0, 0);
2109 }
2110
2111 /* Pro series calibration */
2112 static SANE_Status
get_calibration_size_pro(Mustek_Scanner * s)2113 get_calibration_size_pro (Mustek_Scanner * s)
2114 {
2115 SANE_Status status;
2116 SANE_Byte cmd[6];
2117 SANE_Byte result[6];
2118 size_t len;
2119
2120 memset (cmd, 0, sizeof (cmd));
2121 memset (result, 0, sizeof (result));
2122 cmd[0] = MUSTEK_SCSI_GET_IMAGE_STATUS;
2123 cmd[4] = 0x06; /* size of result */
2124 cmd[5] = 0x80; /* get back buffer size and number of buffers */
2125 len = sizeof (result);
2126 status = dev_cmd (s, cmd, sizeof (cmd), result, &len);
2127 if (status != SANE_STATUS_GOOD)
2128 return status;
2129
2130 s->hw->cal.bytes = result[1] | (result[2] << 8);
2131 s->hw->cal.lines = result[3] | (result[4] << 8);
2132
2133 DBG (4, "get_calibration_size_pro: bytes=%d, lines=%d\n", s->hw->cal.bytes,
2134 s->hw->cal.lines);
2135 return SANE_STATUS_GOOD;
2136 }
2137
2138 static SANE_Status
get_calibration_lines_pro(Mustek_Scanner * s)2139 get_calibration_lines_pro (Mustek_Scanner * s)
2140 {
2141 SANE_Status status;
2142 SANE_Byte cmd[10];
2143 size_t len;
2144 SANE_Int line;
2145
2146 DBG (2, "get_calibration_lines_pro: please wait for warmup\n");
2147 memset (cmd, 0, sizeof (cmd));
2148 cmd[0] = MUSTEK_SCSI_READ_DATA;
2149 len = s->hw->cal.bytes;
2150 cmd[6] = (len >> 16) & 0xff;
2151 cmd[7] = (len >> 8) & 0xff;
2152 cmd[8] = (len >> 0) & 0xff;
2153
2154 for (line = 0; line < s->hw->cal.lines; line++)
2155 {
2156 status = dev_cmd (s, cmd, CDB_SIZE (MUSTEK_SCSI_READ_DATA),
2157 s->hw->cal.buffer + line * len, &len);
2158
2159 if ((status != SANE_STATUS_GOOD)
2160 || (len != (unsigned int) s->hw->cal.bytes))
2161 {
2162 DBG (1, "get_calibration_lines_pro: read failed\n");
2163 return status;
2164 }
2165 }
2166 DBG (5, "get_calibration_lines_pro finished. Assuming 12 bits per color\n");
2167 return SANE_STATUS_GOOD;
2168 }
2169
2170 static SANE_Status
send_calibration_lines_pro(Mustek_Scanner * s)2171 send_calibration_lines_pro (Mustek_Scanner * s)
2172 {
2173 SANE_Status status;
2174 SANE_Byte *cmd1, *cmd2;
2175 size_t buf_size;
2176 SANE_Word column, line, color;
2177
2178 DBG (5, "send_calibration_lines_pro\n");
2179
2180 buf_size = s->hw->cal.bytes / 2;
2181 cmd1 = (SANE_Byte *) malloc (buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2182 cmd2 = (SANE_Byte *) malloc (buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2183 if (!cmd1 || !cmd2)
2184 {
2185 DBG (1, "send_calibration_lines_pro: failed to malloc %zu bytes for "
2186 "sending lines\n",
2187 buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2188 return SANE_STATUS_NO_MEM;
2189 }
2190 memset (cmd1, 0, CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2191 memset (cmd2, 0, CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2192
2193 cmd1[0] = cmd2[0] = MUSTEK_SCSI_SEND_DATA;
2194 cmd1[6] = cmd2[6] = (buf_size >> 16) & 0xff;
2195 cmd1[7] = cmd2[7] = (buf_size >> 8) & 0xff;
2196 cmd1[8] = cmd2[8] = (buf_size >> 0) & 0xff;
2197 cmd1[9] = 0; /* Least significant 8 bits */
2198 cmd2[9] = 0x80; /* Most significant 2 bits */
2199
2200 for (color = 0; color < 3; color++)
2201 {
2202 for (column = 0; column < s->hw->cal.bytes / 6; column++)
2203 {
2204 SANE_Word calibration_word = 0;
2205 for (line = 0; line < s->hw->cal.lines; line++)
2206 {
2207 calibration_word +=
2208 *(s->hw->cal.buffer + column * 6 + color_seq[color] * 2 + 0)
2209 +
2210 (*(s->hw->cal.buffer + column * 6 + color_seq[color] * 2 + 1)
2211 << 8);
2212 }
2213 if (!calibration_word)
2214 calibration_word = 1;
2215 calibration_word = (1024 * 65536 / calibration_word) - 1024;
2216 if (calibration_word > 1023)
2217 calibration_word = 1023;
2218 *(cmd1 + CDB_SIZE (MUSTEK_SCSI_SEND_DATA) + (buf_size / 3) * color
2219 + column) = calibration_word & 0xff;
2220 *(cmd2 + CDB_SIZE (MUSTEK_SCSI_SEND_DATA) + (buf_size / 3) * color
2221 + column) = (calibration_word >> 8) & 0xff;
2222 }
2223 }
2224
2225 status = dev_cmd (s, cmd1, buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA), 0, 0);
2226 if (status != SANE_STATUS_GOOD)
2227 {
2228 DBG (1, "send_calibration_lines_pro: send failed\n");
2229 return status;
2230 }
2231
2232 status = dev_cmd (s, cmd2, buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA), 0, 0);
2233 if (status != SANE_STATUS_GOOD)
2234 {
2235 DBG (1, "send_calibration_lines_pro: send failed\n");
2236 return status;
2237 }
2238 free (cmd1);
2239 free (cmd2);
2240 return SANE_STATUS_GOOD;
2241 }
2242
2243 static SANE_Status
calibration_pro(Mustek_Scanner * s)2244 calibration_pro (Mustek_Scanner * s)
2245 {
2246 SANE_Status status;
2247
2248 if (s->val[OPT_QUALITY_CAL].w)
2249 DBG (4, "calibration_pro: doing calibration\n");
2250 else
2251 {
2252 DBG (4, "calibration_pro: calibration not necessary\n");
2253 return SANE_STATUS_GOOD;
2254 }
2255
2256 status = get_calibration_size_pro (s);
2257 if (status != SANE_STATUS_GOOD)
2258 return status;
2259
2260 s->hw->cal.buffer = (SANE_Byte *) malloc (s->hw->cal.bytes *
2261 s->hw->cal.lines);
2262 if (!s->hw->cal.buffer)
2263 {
2264 DBG (1, "calibration_pro: failed to malloc %d bytes for buffer\n",
2265 s->hw->cal.bytes * s->hw->cal.lines);
2266 return SANE_STATUS_NO_MEM;
2267 }
2268
2269 status = get_calibration_lines_pro (s);
2270 if (status != SANE_STATUS_GOOD)
2271 return status;
2272
2273 status = send_calibration_lines_pro (s);
2274 if (status != SANE_STATUS_GOOD)
2275 return status;
2276
2277 free (s->hw->cal.buffer);
2278 return SANE_STATUS_GOOD;
2279 }
2280
2281
2282 /* ScanExpress series calibration */
2283 static SANE_Status
get_calibration_lines_se(Mustek_Scanner * s)2284 get_calibration_lines_se (Mustek_Scanner * s)
2285 {
2286 SANE_Status status;
2287 SANE_Byte cmd[10];
2288 size_t len;
2289 SANE_Word lines, bytes_per_color;
2290
2291 if (s->mode == MUSTEK_MODE_COLOR)
2292 {
2293 lines = s->hw->cal.lines * 3;
2294 bytes_per_color = s->hw->cal.bytes / 3;
2295 }
2296 else
2297 {
2298 lines = s->hw->cal.lines;
2299 bytes_per_color = s->hw->cal.bytes;
2300 }
2301
2302 DBG (4, "get_calibration_lines_se: reading %d lines (%d bytes per color)\n",
2303 lines, bytes_per_color);
2304 memset (cmd, 0, sizeof (cmd));
2305 cmd[0] = MUSTEK_SCSI_READ_DATA;
2306 cmd[2] = 1;
2307 cmd[7] = (lines >> 8) & 0xff;
2308 cmd[8] = (lines >> 0) & 0xff;
2309 len = lines * bytes_per_color;
2310 status = dev_cmd (s, cmd, CDB_SIZE (MUSTEK_SCSI_READ_DATA),
2311 s->hw->cal.buffer, &len);
2312 if ((status != SANE_STATUS_GOOD)
2313 || (len != (unsigned int) (lines * bytes_per_color)))
2314 {
2315 DBG (1, "get_calibration_lines_se: read failed\n");
2316 return status;
2317 }
2318 return SANE_STATUS_GOOD;
2319 }
2320
2321 static SANE_Status
send_calibration_lines_se(Mustek_Scanner * s, SANE_Word color)2322 send_calibration_lines_se (Mustek_Scanner * s, SANE_Word color)
2323 {
2324 SANE_Status status;
2325 SANE_Byte *cmd;
2326 size_t buf_size;
2327 SANE_Word column;
2328 SANE_Word bytes_per_color;
2329
2330 if (s->mode == MUSTEK_MODE_COLOR)
2331 {
2332 bytes_per_color = s->hw->cal.bytes / 3;
2333 }
2334 else
2335 {
2336 bytes_per_color = s->hw->cal.bytes;
2337 }
2338
2339 buf_size = bytes_per_color;
2340
2341 DBG (5, "send_calibration_lines_se: %d bytes, color: %d\n",
2342 bytes_per_color, color + 1);
2343
2344 cmd = (SANE_Byte *) malloc (buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2345 if (!cmd)
2346 {
2347 DBG (1, "send_calibration_lines_se: failed to malloc %zu bytes for "
2348 "sending lines\n",
2349 buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2350 return SANE_STATUS_NO_MEM;
2351 }
2352 memset (cmd, 0, CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2353
2354 for (column = 0; column < bytes_per_color; column++)
2355 {
2356 SANE_Word line;
2357 SANE_Word cali_word = 0;
2358 SANE_Int color_seq[] = { 2, 0, 1 };
2359
2360 for (line = 0; line < s->hw->cal.lines; line++)
2361 cali_word += *(s->hw->cal.buffer
2362 + line * bytes_per_color
2363 + bytes_per_color * color_seq[color] + column);
2364 if (!cali_word)
2365 cali_word = 1;
2366 cali_word = 256 * s->hw->cal.lines * 255 / cali_word - 256;
2367 if (cali_word > 255)
2368 cali_word = 255;
2369 *(cmd + CDB_SIZE (MUSTEK_SCSI_SEND_DATA) + column) = cali_word;
2370 }
2371
2372 cmd[0] = MUSTEK_SCSI_SEND_DATA;
2373 cmd[2] = 1;
2374 cmd[6] = color + 1;
2375 cmd[7] = (buf_size >> 8) & 0xff;
2376 cmd[8] = (buf_size >> 0) & 0xff;
2377
2378 status = dev_cmd (s, cmd, buf_size + CDB_SIZE (MUSTEK_SCSI_SEND_DATA), 0, 0);
2379 if (status != SANE_STATUS_GOOD)
2380 {
2381 DBG (1, "send_calibration_lines_se: send failed\n");
2382 return status;
2383 }
2384 free (cmd);
2385 return SANE_STATUS_GOOD;
2386 }
2387
2388 static SANE_Status
calibration_se(Mustek_Scanner * s)2389 calibration_se (Mustek_Scanner * s)
2390 {
2391 SANE_Status status;
2392
2393 if (!s->val[OPT_QUALITY_CAL].w || s->val[OPT_PREVIEW].w
2394 || s->mode == MUSTEK_MODE_LINEART)
2395 return SANE_STATUS_GOOD;
2396
2397 DBG (4, "calibration_se: doing calibration\n");
2398
2399 s->hw->cal.lines = MIN (s->hw->cal.lines,
2400 s->hw->buffer_size / s->hw->cal.bytes);
2401
2402 s->hw->cal.buffer = (SANE_Byte *) malloc (s->hw->cal.bytes
2403 * s->hw->cal.lines);
2404 if (!s->hw->cal.buffer)
2405 {
2406 DBG (1, "calibration_se: failed to malloc %d bytes for buffer\n",
2407 s->hw->cal.bytes * s->hw->cal.lines);
2408 return SANE_STATUS_NO_MEM;
2409 }
2410
2411 status = get_calibration_lines_se (s);
2412 if (status != SANE_STATUS_GOOD)
2413 return status;
2414
2415 if (s->mode == MUSTEK_MODE_GRAY)
2416 status = send_calibration_lines_se (s, 0);
2417 else
2418 {
2419 status = send_calibration_lines_se (s, 0);
2420 status = send_calibration_lines_se (s, 1);
2421 status = send_calibration_lines_se (s, 2);
2422 }
2423 if (status != SANE_STATUS_GOOD)
2424 return status;
2425
2426 free (s->hw->cal.buffer);
2427 return SANE_STATUS_GOOD;
2428 }
2429
2430 /* ScanExpress series */
2431 static SANE_Status
send_gamma_table_se(Mustek_Scanner * s)2432 send_gamma_table_se (Mustek_Scanner * s)
2433 {
2434 SANE_Status status;
2435 SANE_Byte gamma[10 + 4096], *cp;
2436 SANE_Int color, factor, val_a, val_b;
2437 SANE_Int i, j;
2438 # define CLIP(x) ((x) < 0 ? 0 : ((x) > 255 ? 255 : (x)))
2439
2440 memset (gamma, 0, CDB_SIZE (MUSTEK_SCSI_SEND_DATA));
2441
2442 gamma[0] = MUSTEK_SCSI_SEND_DATA;
2443 gamma[2] = 0x03; /* indicates gamma table */
2444
2445 if ((s->mode & MUSTEK_MODE_GRAY) || (s->mode & MUSTEK_MODE_COLOR))
2446 {
2447 if ((size_t) s->hw->gamma_length + CDB_SIZE (MUSTEK_SCSI_SEND_DATA)
2448 > sizeof (gamma))
2449 return SANE_STATUS_NO_MEM;
2450 gamma[7] = (s->hw->gamma_length >> 8) & 0xff;
2451 gamma[8] = (s->hw->gamma_length >> 0) & 0xff;
2452
2453 factor = s->hw->gamma_length / 256;
2454 color = (s->mode & MUSTEK_MODE_COLOR) ? 1 : 0;
2455
2456 do
2457 {
2458 gamma[6] = color;
2459
2460 if (color == 0)
2461 {
2462 val_a = s->gamma_table[0][1];
2463 val_b = s->gamma_table[0][0];
2464 }
2465 else
2466 {
2467 /* compose intensity gamma and color channel gamma: */
2468 val_a = s->gamma_table[0][s->gamma_table[color][1]];
2469 val_b = s->gamma_table[0][s->gamma_table[color][0]];
2470 }
2471 /* Now val_a is extrapolated from [0] and [1] */
2472 val_a = MAX (2 * val_b - val_a, 0);
2473
2474 /* Interpolate first entries from 256 entry table */
2475 cp = gamma + CDB_SIZE (MUSTEK_SCSI_SEND_DATA);
2476 for (j = 0; j < factor; j++)
2477 *cp++ = CLIP (((factor - j) * val_a + j * val_b
2478 + factor / 2) / factor);
2479
2480 for (i = 1; i < 256; i++)
2481 {
2482 if (color == 0)
2483 {
2484 val_a = s->gamma_table[0][i - 1];
2485 val_b = s->gamma_table[0][i];
2486 }
2487 else
2488 {
2489 /* compose intensity gamma and color channel gamma: */
2490 val_a = s->gamma_table[0][s->gamma_table[color][i - 1]];
2491 val_b = s->gamma_table[0][s->gamma_table[color][i]];
2492 }
2493
2494 /* Interpolate next entries from the 256 entry table */
2495 for (j = 0; j < factor; j++)
2496 *cp++ = CLIP (((factor - j) * val_a + j * val_b
2497 + factor / 2) / factor);
2498 }
2499
2500 DBG (5, "send_gamma_table_se: sending table for color %d\n",
2501 gamma[6]);
2502 status = dev_cmd (s, gamma, CDB_SIZE (MUSTEK_SCSI_SEND_DATA)
2503 + s->hw->gamma_length, 0, 0);
2504 ++color;
2505 }
2506 while ((color != 1) & (color < 4) & (status == SANE_STATUS_GOOD));
2507
2508 return status;
2509 }
2510 else
2511 {
2512 /* In lineart mode the threshold is encoded in byte 8 as follows */
2513 /* brightest -> 00 01 02 ... 7F 80 81 82 ... FF <- darkest image */
2514 gamma[6] = 0x04;
2515 gamma[8] = 128 - 127 * SANE_UNFIX (s->val[OPT_BRIGHTNESS].w) / 100.0;
2516
2517 DBG (5, "send_gamma_table_se: sending lineart threshold %2X\n",
2518 gamma[8]);
2519 return dev_cmd (s, gamma, CDB_SIZE (MUSTEK_SCSI_SEND_DATA), 0, 0);
2520 }
2521 }
2522
2523 /* Paragon series */
2524 static SANE_Status
mode_select_paragon(Mustek_Scanner * s, SANE_Int color_code)2525 mode_select_paragon (Mustek_Scanner * s, SANE_Int color_code)
2526 {
2527 SANE_Int speed_code;
2528 SANE_Byte mode[19], *cp;
2529
2530 /* calculate funky speed code: */
2531 for (speed_code = 0; speed_list[speed_code]; ++speed_code)
2532 {
2533 if (strcmp (speed_list[speed_code], s->val[OPT_SPEED].s) == 0)
2534 break;
2535 }
2536 if (speed_code > 4)
2537 speed_code = 4;
2538 else if (speed_code < 0)
2539 speed_code = 0;
2540 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
2541 {
2542 speed_code = 5 - speed_code; /* 1 is fast, 5 is slow */
2543 }
2544 else
2545 {
2546 speed_code = 4 - speed_code; /* 0 is fast, 4 is slow */
2547 }
2548 memset (mode, 0, sizeof (mode));
2549 mode[0] = MUSTEK_SCSI_MODE_SELECT;
2550
2551 /* set command length and resolution code: */
2552 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
2553 {
2554 mode[4] = 0x0b;
2555 mode[7] = s->resolution_code;
2556 }
2557 else
2558 {
2559 mode[4] = 0x0d;
2560 cp = mode + 17;
2561 STORE16L (cp, s->resolution_code);
2562 }
2563 /* set mode byte: */
2564 mode[6] = 0x83 | (color_code << 5);
2565 if (!(s->hw->flags & MUSTEK_FLAG_USE_EIGHTS))
2566 mode[6] |= 0x08;
2567 if (s->custom_halftone_pattern)
2568 mode[6] |= 0x10;
2569 if (s->hw->flags & MUSTEK_FLAG_PARAGON_1)
2570 {
2571 if ((s->mode == MUSTEK_MODE_LINEART)
2572 || (s->mode == MUSTEK_MODE_HALFTONE))
2573 {
2574 mode[8] =
2575 encode_percentage (s, SANE_UNFIX (s->val[OPT_BRIGHTNESS].w));
2576 mode[9] =
2577 encode_percentage (s, SANE_UNFIX (s->val[OPT_CONTRAST].w));
2578 }
2579 else
2580 {
2581 mode[8] = 0x0c;
2582 mode[9] = 0x0c;
2583 }
2584 mode[10] = 2; /* grain */
2585 if (s->val[OPT_PREVIEW].w && s->val[OPT_FAST_PREVIEW].w)
2586 mode[11] = 0x01;
2587 else if ((s->mode == MUSTEK_MODE_COLOR)
2588 || (s->mode == MUSTEK_MODE_HALFTONE))
2589 mode[11] = 0x00; /* speed */
2590 else
2591 mode[11] = 0x02; /* speed */
2592 mode[12] = 0x00; /* shadow param not used by Mustek */
2593 mode[13] = 0xff; /* highlight only used by some scanners */
2594 mode[14] = 0x70; /* paper- */
2595 mode[15] = 0x00; /* length */
2596 mode[16] = 0x53; /* midtone param not used by Mustek */
2597 }
2598 else if (s->hw->flags & MUSTEK_FLAG_PARAGON_2)
2599 {
2600 mode[8] = encode_percentage (s, SANE_UNFIX (s->val[OPT_BRIGHTNESS].w));
2601 mode[9] = encode_percentage (s, SANE_UNFIX (s->val[OPT_CONTRAST].w));
2602 mode[10] = 2; /* grain */
2603 if ((s->mode == MUSTEK_MODE_COLOR) || (s->mode == MUSTEK_MODE_HALFTONE))
2604 mode[11] = 0x00; /* speed */
2605 else
2606 mode[11] = 0x02; /* speed */
2607 mode[12] = 0x00; /* shadow param not used by Mustek */
2608 mode[13] = 0x00; /* highlight param not used by Mustek */
2609 mode[14] = 0x5c; /* paper- */
2610 mode[15] = 0x00; /* length */
2611 mode[16] = 0x41; /* midtone param not used by Mustek */
2612 }
2613 else if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
2614 {
2615 if (s->mode & MUSTEK_MODE_COLOR)
2616 {
2617 mode[8] = encode_percentage
2618 (s, SANE_UNFIX (s->val[OPT_BRIGHTNESS + s->pass + 1].w - 1));
2619 mode[9] = encode_percentage
2620 (s, SANE_UNFIX (s->val[OPT_CONTRAST + s->pass + 1].w - 1));
2621 }
2622 else
2623 {
2624 mode[8] = encode_percentage
2625 (s, SANE_UNFIX (s->val[OPT_BRIGHTNESS].w - 1));
2626 mode[9] = encode_percentage
2627 (s, SANE_UNFIX (s->val[OPT_CONTRAST].w - 1));
2628 }
2629 mode[10] = s->halftone_pattern_type;
2630 mode[11] = speed_code; /* lamp setting not supported yet */
2631 mode[12] = 0; /* shadow param not used by Mustek */
2632 mode[13] = 0; /* highlight param not used by Mustek */
2633 mode[14] = mode[15] = 0; /* paperlength not used by Mustek */
2634 mode[16] = 0; /* midtone param not used by Mustek */
2635 }
2636 else
2637 {
2638 mode[8] = encode_percentage (s, SANE_UNFIX (s->val[OPT_BRIGHTNESS].w));
2639 mode[9] = encode_percentage (s, SANE_UNFIX (s->val[OPT_CONTRAST].w));
2640 mode[10] = s->halftone_pattern_type;
2641 mode[11] = speed_code; /* lamp setting not supported yet */
2642 mode[12] = 0; /* shadow param not used by Mustek */
2643 mode[13] = 0; /* highlight param not used by Mustek */
2644 mode[14] = mode[15] = 0; /* paperlength not used by Mustek */
2645 mode[16] = 0; /* midtone param not used by Mustek */
2646 }
2647
2648 DBG (5, "mode_select: resolution_code=%d (0x%x)\n", s->resolution_code,
2649 s->resolution_code);
2650 return dev_cmd (s, mode, 6 + mode[4], 0, 0);
2651 }
2652
2653 /* Pro series */
2654 static SANE_Status
mode_select_pro(Mustek_Scanner * s)2655 mode_select_pro (Mustek_Scanner * s)
2656 {
2657 SANE_Byte mode[19], *cp;
2658
2659 memset (mode, 0, sizeof (mode));
2660
2661 mode[0] = MUSTEK_SCSI_MODE_SELECT;
2662 mode[4] = 0x0d;
2663
2664 if (s->mode & MUSTEK_MODE_COLOR)
2665 {
2666 if (strcmp (s->val[OPT_BIT_DEPTH].s, "12") == 0)
2667 mode[6] = 0xE0;
2668 else
2669 mode[6] = 0x60;
2670 }
2671 else if (s->mode & MUSTEK_MODE_GRAY)
2672 {
2673 if (s->val[OPT_FAST_GRAY_MODE].w)
2674 mode[6] = 0x20;
2675 else
2676 mode[6] = 0x40;
2677 }
2678 else
2679 mode[6] = 0x00; /* lineart */
2680
2681 mode[7] = 0;
2682 mode[8] = 0;
2683 mode[9] = 0;
2684 mode[10] = 0;
2685 mode[11] = 0x00;
2686 mode[12] = 0x27;
2687 mode[13] = 0xb0;
2688 mode[14] = 0x04;
2689 mode[15] = 0x43;
2690 mode[16] = 0x41;
2691
2692 cp = mode + 17;
2693 STORE16L (cp, s->resolution_code);
2694
2695 DBG (5, "mode_select_pro: resolution_code=%d (0x%x), mode=0x%x\n",
2696 s->resolution_code, s->resolution_code, mode[6]);
2697 return dev_cmd (s, mode, 6 + mode[4], 0, 0);
2698 }
2699
2700 /* Paragon and Pro series. According to Mustek, the only builtin gamma
2701 table is a linear table, so all we support here is user-defined
2702 gamma tables. */
2703 static SANE_Status
gamma_correction(Mustek_Scanner * s, SANE_Int color_code)2704 gamma_correction (Mustek_Scanner * s, SANE_Int color_code)
2705 {
2706 SANE_Int i, j, table = 0, len = 0, bytes_per_channel, num_channels = 1;
2707 SANE_Byte gamma[4096 + 10], val, *cp; /* for Paragon models 3 x 256 is the
2708 maximum. Pro needs 4096 bytes */
2709
2710 if ((s->hw->flags & MUSTEK_FLAG_N)
2711 && ((s->mode & MUSTEK_MODE_LINEART)
2712 || (s->mode & MUSTEK_MODE_HALFTONE)))
2713 {
2714 /* sigh! - the 600 II N needs a (dummy) table download even for
2715 lineart and halftone mode, else it produces a completely
2716 white image. Thank Mustek for their buggy firmware ! */
2717 memset (gamma, 0, sizeof (gamma));
2718 gamma[0] = MUSTEK_SCSI_LOOKUP_TABLE;
2719 gamma[2] = 0x0; /* indicate any preloaded gamma table */
2720 DBG (5, "gamma_correction: sending dummy gamma table\n");
2721 return dev_cmd (s, gamma, 6, 0, 0);
2722 }
2723
2724 if (((s->mode & MUSTEK_MODE_LINEART) || (s->mode & MUSTEK_MODE_HALFTONE))
2725 && !(s->hw->flags & MUSTEK_FLAG_PRO))
2726 {
2727 DBG (5, "gamma_correction: nothing to do in lineart mode -- exiting\n");
2728 return SANE_STATUS_GOOD;
2729 }
2730
2731 if ((!s->val[OPT_CUSTOM_GAMMA].w) && (!(s->hw->flags & MUSTEK_FLAG_PRO)))
2732 {
2733 /* Do we need to upload a gamma table even if the user didn't select
2734 this option? Some scanners need this work around. */
2735 if (!(s->hw->flags & MUSTEK_FLAG_FORCE_GAMMA) ||
2736 !((s->mode & MUSTEK_MODE_COLOR) || (s->mode & MUSTEK_MODE_GRAY)))
2737 {
2738 DBG (5,
2739 "gamma_correction: no custom table selected -- exititing\n");
2740 return SANE_STATUS_GOOD;
2741 }
2742 }
2743
2744 if (s->mode & MUSTEK_MODE_COLOR)
2745 {
2746 table = 1;
2747 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
2748 table += s->pass;
2749 else
2750 {
2751 if ((color_code == MUSTEK_CODE_GRAY)
2752 && !(s->hw->flags & MUSTEK_FLAG_PRO))
2753 num_channels = 3;
2754 else
2755 table = color_code;
2756 }
2757 }
2758 else if (s->hw->flags & MUSTEK_FLAG_N)
2759 {
2760 /* it seems 600 II N (firmware 1.x at least) wants 768 bytes in
2761 * gray mode too */
2762 num_channels = 3;
2763 }
2764
2765 memset (gamma, 0, sizeof (gamma));
2766 gamma[0] = MUSTEK_SCSI_LOOKUP_TABLE;
2767
2768 if (s->hw->flags & MUSTEK_FLAG_PRO)
2769 {
2770 bytes_per_channel = 4096;
2771 len = bytes_per_channel;
2772 if (s->mode == MUSTEK_MODE_COLOR)
2773 {
2774 gamma[9] = (color_code << 6); /* color */
2775 if (strcmp (s->val[OPT_BIT_DEPTH].s, "12") == 0)
2776 gamma[2] = 0x7f; /* medium brightness */
2777 }
2778 else if (s->mode == MUSTEK_MODE_GRAY)
2779 {
2780 gamma[9] = 0x80; /* grayscale */
2781 if (s->val[OPT_FAST_GRAY_MODE].w)
2782 gamma[2] = 0x7f; /* medium brightness */
2783 }
2784 else /* lineart */
2785 {
2786 gamma[2] =
2787 128 - 127 * SANE_UNFIX (s->val[OPT_BRIGHTNESS].w) / 100.0;
2788 gamma[9] = 0x80; /* grayscale/lineart */
2789 DBG (5, "gamma_correction: sending brightness information\n");
2790 }
2791 gamma[7] = (len >> 8) & 0xff; /* big endian! */
2792 gamma[8] = (len >> 0) & 0xff;
2793 }
2794 else
2795 {
2796 bytes_per_channel = 256;
2797 len = num_channels * bytes_per_channel;
2798 gamma[2] = 0x27; /* indicate user-selected gamma table */
2799 if (s->hw->flags & MUSTEK_FLAG_N)
2800 {
2801 /* 600 II N always uses 6-byte cdb */
2802 gamma[3] = (len >> 8) & 0xff; /* big endian! */
2803 gamma[4] = (len >> 0) & 0xff;
2804 /* no way to pass color_code (?) */
2805 }
2806 else
2807 {
2808 gamma[7] = (len >> 8) & 0xff; /* big endian! */
2809 gamma[8] = (len >> 0) & 0xff;
2810 gamma[9] = (color_code << 6);
2811 }
2812 }
2813
2814 if (len > 0)
2815 {
2816 cp = gamma + 10;
2817 for (j = 0; j < num_channels; ++j)
2818 {
2819 for (i = 0; i < bytes_per_channel; ++i)
2820 {
2821 if (s->val[OPT_CUSTOM_GAMMA].w == SANE_TRUE)
2822 val = s->gamma_table[table][i * 256 / bytes_per_channel];
2823 else
2824 val = i * 256 / bytes_per_channel;
2825 if ((s->mode & MUSTEK_MODE_COLOR)
2826 && (s->val[OPT_CUSTOM_GAMMA].w == SANE_TRUE))
2827 /* compose intensity gamma and color channel gamma: */
2828 val = s->gamma_table[0][val];
2829 *cp++ = val;
2830 }
2831 if (!(s->hw->flags & MUSTEK_FLAG_N)
2832 || !(s->mode & MUSTEK_MODE_GRAY))
2833 table++;
2834 }
2835 }
2836 DBG (5, "gamma_correction: sending gamma table of %d bytes\n", len);
2837 return dev_cmd (s, gamma, 10 + len, 0, 0);
2838 }
2839
2840 static SANE_Status
send_gamma_table(Mustek_Scanner * s)2841 send_gamma_table (Mustek_Scanner * s)
2842 {
2843 SANE_Status status;
2844
2845 if (s->one_pass_color_scan)
2846 {
2847 if (s->hw->flags & MUSTEK_FLAG_N)
2848 /* This _should_ work for all one-pass scanners (not just
2849 AB306N scanners), but it doesn't work for my Paragon
2850 600 II SP with firmware rev 1.01. Too bad, since it would
2851 simplify the gamma correction code quite a bit. */
2852 status = gamma_correction (s, MUSTEK_CODE_GRAY);
2853 else
2854 {
2855 status = gamma_correction (s, MUSTEK_CODE_RED);
2856 if (status != SANE_STATUS_GOOD)
2857 return status;
2858
2859 status = gamma_correction (s, MUSTEK_CODE_GREEN);
2860 if (status != SANE_STATUS_GOOD)
2861 return status;
2862
2863 status = gamma_correction (s, MUSTEK_CODE_BLUE);
2864 }
2865 }
2866 else
2867 status = gamma_correction (s, MUSTEK_CODE_GRAY);
2868 return status;
2869 }
2870
2871
2872 /* ScanExpress and Paragon series */
2873 static SANE_Status
start_scan(Mustek_Scanner * s)2874 start_scan (Mustek_Scanner * s)
2875 {
2876 SANE_Byte start[6];
2877 SANE_Status status;
2878
2879 memset (start, 0, sizeof (start));
2880 start[0] = MUSTEK_SCSI_START_STOP;
2881 start[4] = 0x01;
2882
2883 DBG (4, "start_scan\n");
2884 /* ScanExpress and Pro models don't have any variants */
2885 if (!(s->hw->flags & MUSTEK_FLAG_SE) && !(s->hw->flags & MUSTEK_FLAG_PRO))
2886 {
2887 if (s->mode & MUSTEK_MODE_COLOR)
2888 {
2889 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
2890 start[4] |= ((s->pass + 1) << 3);
2891 else
2892 start[4] |= 0x20;
2893 }
2894 /* or in single/multi bit: */
2895 start[4] |= ((s->mode & MUSTEK_MODE_LINEART)
2896 || (s->mode & MUSTEK_MODE_HALFTONE)) ? 0 : (1 << 6);
2897
2898 /* or in expanded resolution bit: */
2899 if (s->val[OPT_RESOLUTION].w > (s->hw->dpi_range.max / 2)
2900 && ((s->hw->flags & MUSTEK_FLAG_THREE_PASS)
2901 || (s->hw->flags & MUSTEK_FLAG_PARAGON_1)
2902 || (s->hw->flags & MUSTEK_FLAG_PARAGON_2)))
2903 start[4] |= 1 << 7;
2904
2905 /* block mode (or whatever) */
2906 if (s->hw->flags & MUSTEK_FLAG_USE_BLOCK)
2907 {
2908 start[5] = 0x08;
2909 DBG (4, "start_scan: using block mode\n");
2910 }
2911 }
2912
2913 status = dev_cmd (s, start, sizeof (start), 0, 0);
2914 if (status != SANE_STATUS_GOOD)
2915 DBG (1, "start_scan returned status %s\n", sane_strstatus (status));
2916 return status;
2917 }
2918
2919 static SANE_Status
do_eof(Mustek_Scanner * s)2920 do_eof (Mustek_Scanner * s)
2921 {
2922 if (s->pipe >= 0)
2923 {
2924 close (s->pipe);
2925 s->pipe = -1;
2926 DBG (5, "do_eof: closing pipe\n");
2927 }
2928 return SANE_STATUS_EOF;
2929 }
2930
2931 static SANE_Status
do_stop(Mustek_Scanner * s)2932 do_stop (Mustek_Scanner * s)
2933 {
2934 SANE_Status status = SANE_STATUS_GOOD;
2935
2936 DBG (5, "do_stop\n");
2937
2938 if (s->cancelled)
2939 status = SANE_STATUS_CANCELLED;
2940
2941 s->scanning = SANE_FALSE;
2942 s->pass = 0;
2943
2944 if (sanei_thread_is_valid (s->reader_pid))
2945 {
2946 SANE_Int exit_status;
2947 struct timeval now;
2948 long int scan_time;
2949 long int scan_size;
2950 SANE_Pid pid;
2951
2952 /* print scanning time */
2953 gettimeofday (&now, 0);
2954 scan_time = now.tv_sec - s->start_time;
2955 if (scan_time < 1)
2956 scan_time = 1;
2957 scan_size = s->hw->bpl * s->hw->lines / 1024;
2958 DBG (2, "Scanning time was %ld seconds, %ld kB/s\n", scan_time,
2959 scan_size / scan_time);
2960
2961 if (s->total_bytes == s->params.lines * s->params.bytes_per_line)
2962 DBG (3, "Scanned %d bytes as expected\n", s->total_bytes);
2963 else if (s->total_bytes < s->params.lines * s->params.bytes_per_line)
2964 DBG (3, "Scanned %d bytes, expected %d bytes\n", s->total_bytes,
2965 s->params.lines * s->params.bytes_per_line);
2966 else
2967 DBG (1, "Warning: Scanned %d bytes, but expected only %d bytes\n",
2968 s->total_bytes, s->params.lines * s->params.bytes_per_line);
2969
2970 /* ensure child knows it's time to stop: */
2971 DBG (5, "do_stop: terminating reader process\n");
2972 sanei_thread_kill (s->reader_pid);
2973
2974 pid = sanei_thread_waitpid (s->reader_pid, &exit_status);
2975 if (!sanei_thread_is_valid (pid))
2976 {
2977 DBG (1,
2978 "do_stop: sanei_thread_waitpid failed, already terminated? (%s)\n",
2979 strerror (errno));
2980 }
2981 else
2982 {
2983 DBG (2, "do_stop: reader process terminated with status %s\n",
2984 sane_strstatus (exit_status));
2985 if (status != SANE_STATUS_CANCELLED
2986 && exit_status != SANE_STATUS_GOOD)
2987 status = exit_status;
2988 }
2989
2990 sanei_thread_invalidate (s->reader_pid);
2991 }
2992
2993 if (s->fd >= 0)
2994 {
2995 if (!sanei_thread_is_forked ())
2996 sanei_scsi_req_flush_all (); /* flush SCSI queue */
2997
2998 if (s->hw->flags & MUSTEK_FLAG_PRO)
2999 {
3000 if (s->total_bytes < s->params.lines * s->params.bytes_per_line)
3001 status = dev_cmd (s, scsi_start_stop, sizeof (scsi_start_stop),
3002 0, 0);
3003 dev_wait_ready (s);
3004 }
3005 else if ((s->hw->flags & MUSTEK_FLAG_PARAGON_1)
3006 || (s->hw->flags & MUSTEK_FLAG_PARAGON_2)
3007 || (s->hw->flags & MUSTEK_FLAG_THREE_PASS))
3008 {
3009 if (s->cancelled &&
3010 (s->total_bytes < s->params.lines * s->params.bytes_per_line))
3011 status = dev_cmd (s, scsi_start_stop, sizeof (scsi_start_stop),
3012 0, 0);
3013 }
3014 else
3015 status = dev_cmd (s, scsi_start_stop, sizeof (scsi_start_stop), 0, 0);
3016
3017 if (force_wait)
3018 {
3019 DBG (5, "do_stop: waiting for scanner to be ready\n");
3020 dev_wait_ready (s);
3021 }
3022
3023 do_eof (s);
3024 DBG (5, "do_stop: closing scanner\n");
3025 dev_close (s);
3026 s->fd = -1;
3027 }
3028
3029 DBG (5, "do_stop: finished\n");
3030 return status;
3031 }
3032
3033 /* Paragon I + II: Determine the CCD's distance between the primary color
3034 lines. */
3035 static SANE_Status
line_distance(Mustek_Scanner * s)3036 line_distance (Mustek_Scanner * s)
3037 {
3038 SANE_Int factor, color, res, peak_res;
3039 SANE_Status status;
3040 SANE_Byte result[5];
3041 size_t len;
3042
3043 memset (result, 0, 5);
3044
3045 res = SANE_UNFIX (s->val[OPT_RESOLUTION].w) + 0.5;
3046 peak_res = SANE_UNFIX (s->hw->dpi_range.max) + 0.5;
3047
3048 s->ld.buf[0] = NULL;
3049
3050 len = sizeof (result);
3051 status = dev_cmd (s, scsi_ccd_distance, sizeof (scsi_ccd_distance),
3052 result, &len);
3053 if (status != SANE_STATUS_GOOD)
3054 return status;
3055
3056 DBG (3, "line_distance: got factor=%d, (r/g/b)=(%d/%d/%d)\n",
3057 result[0] | (result[1] << 8), result[2], result[3], result[4]);
3058
3059 if (s->hw->flags & MUSTEK_FLAG_LD_FIX)
3060 {
3061 result[0] = 0xff;
3062 result[1] = 0xff;
3063 if (s->mode & MUSTEK_MODE_COLOR)
3064 {
3065 if (s->hw->flags & MUSTEK_FLAG_N)
3066 {
3067 /* According to Andreas Czechanowski, the line-distance values
3068 returned for the AB306N scanners are garbage, so we have to
3069 fix things up manually. Not good.
3070 This seems to be true only for firmware 2.00 which is
3071 extremely seldom.. AB306N scanners with firmware 1.01 don't
3072 need this fix. <henning@meier-geinitz.de> */
3073 if (peak_res == 600)
3074 {
3075 if (res < 51)
3076 {
3077 result[0] = 8;
3078 result[1] = 0;
3079 result[2] = 0;
3080 result[3] = 2;
3081 result[4] = 3;
3082 }
3083 else if (res < 75 || (res > 90 && res < 150))
3084 {
3085 /* 51-74 and 91-149 dpi: */
3086 result[0] = 4;
3087 result[1] = 0;
3088 result[2] = 0;
3089 result[3] = 3;
3090 result[4] = 5;
3091 }
3092 else if (res <= 90 || (res >= 150 && res <= 300))
3093 {
3094 /* 75-90 and 150-300 dpi: */
3095 result[0] = 2;
3096 result[1] = 0;
3097 result[2] = 0;
3098 result[3] = 5;
3099 result[4] = 9;
3100 }
3101 else
3102 {
3103 /* 301-600 dpi: */
3104 result[0] = 1;
3105 result[1] = 0;
3106 result[2] = 0;
3107 result[3] = 9;
3108 result[4] = 23;
3109 }
3110 }
3111 else
3112 DBG (1, "don't know how to fix up line-distance for %d dpi\n",
3113 peak_res);
3114 }
3115 else if (!(s->hw->flags & MUSTEK_FLAG_LD_NONE))
3116 {
3117 if (peak_res == 600)
3118 {
3119 if (res < 51)
3120 {
3121 /* 1-50 dpi: */
3122 result[0] = 4;
3123 result[1] = 0;
3124 result[2] = 0;
3125 result[3] = 3;
3126 result[4] = 5;
3127 }
3128 else if (res <= 300)
3129 {
3130 /* 51-300 dpi: */
3131 result[0] = 2;
3132 result[1] = 0;
3133 result[2] = 0;
3134 result[3] = 5;
3135 result[4] = 9;
3136 }
3137 else
3138 {
3139 /*301-600 dpi: */
3140 result[0] = 1;
3141 result[1] = 0;
3142 result[2] = 0;
3143 result[3] = 9;
3144 result[4] = 17;
3145 }
3146 }
3147 else if (peak_res == 800)
3148 {
3149 if (res < 72)
3150 {
3151 /* 1-71 dpi: */
3152 result[0] = 4;
3153 result[1] = 0;
3154 result[2] = 0;
3155 result[3] = 3;
3156 result[4] = 5;
3157 }
3158 else if (res <= 400)
3159 {
3160 /* 72-400 dpi: */
3161 result[0] = 2;
3162 result[1] = 0;
3163 result[2] = 0;
3164 result[3] = 9;
3165 result[4] = 17;
3166 }
3167 else
3168 {
3169 /*401-800 dpi: */
3170 result[0] = 1;
3171 result[1] = 0;
3172 result[2] = 0;
3173 result[3] = 16;
3174 result[4] = 32;
3175 }
3176 }
3177 }
3178 }
3179 DBG (4, "line_distance: fixed up to factor=%d, (r/g/b)=(%d/%d/%d)\n",
3180 result[0] | (result[1] << 8), result[2], result[3], result[4]);
3181 }
3182
3183 factor = result[0] | (result[1] << 8);
3184 if (factor != 0xffff)
3185 {
3186 /* need to do line-distance adjustment ourselves... */
3187
3188 s->ld.max_value = peak_res;
3189
3190 if (factor == 0)
3191 {
3192 if (res <= peak_res / 2)
3193 res *= 2;
3194 }
3195 else
3196 res *= factor;
3197 s->ld.peak_res = res;
3198 for (color = 0; color < 3; ++color)
3199 {
3200 s->ld.dist[color] = result[2 + color];
3201 s->ld.quant[color] = s->ld.max_value;
3202 s->ld.index[color] = -s->ld.dist[color];
3203 }
3204 s->ld.lmod3 = -1;
3205
3206 DBG (4, "line_distance: max_value = %d, peak_res = %d, ld.quant = "
3207 "(%d, %d, %d)\n", s->ld.max_value, s->ld.peak_res, s->ld.quant[0],
3208 s->ld.quant[1], s->ld.quant[2]);
3209 }
3210 else
3211 s->ld.max_value = 0;
3212
3213 return SANE_STATUS_GOOD;
3214 }
3215
3216 /* Paragon + Pro series */
3217 static SANE_Status
get_image_status(Mustek_Scanner * s, SANE_Int * bpl, SANE_Int * lines)3218 get_image_status (Mustek_Scanner * s, SANE_Int * bpl, SANE_Int * lines)
3219 {
3220 SANE_Byte result[6];
3221 SANE_Status status;
3222 size_t len;
3223 SANE_Int busy, offset;
3224 long res, half_res;
3225
3226 memset (result, 0, 6);
3227
3228 /* The 600 II N v1.01 and Paragon 12000SP need a larger scan-area for
3229 line-distance correction in color mode */
3230 offset = 0;
3231 if ((s->hw->flags & MUSTEK_FLAG_LD_N1) && (s->mode & MUSTEK_MODE_COLOR))
3232 offset = s->ld.dist[1];
3233 else if ((s->hw->flags & MUSTEK_FLAG_LD_BLOCK)
3234 && (s->hw->flags & MUSTEK_FLAG_PARAGON_1)
3235 && (s->mode & MUSTEK_MODE_COLOR))
3236 offset = MAX_LINE_DIST * SANE_UNFIX (s->val[OPT_RESOLUTION].w)
3237 / SANE_UNFIX (s->hw->dpi_range.max);
3238
3239 do
3240 {
3241 len = sizeof (result);
3242 status = dev_cmd (s, scsi_get_image_status,
3243 sizeof (scsi_get_image_status), result, &len);
3244 if (status != SANE_STATUS_GOOD)
3245 return status;
3246
3247 busy = result[0];
3248 if (busy)
3249 usleep (100000);
3250
3251 if (!s->scanning) /* ? */
3252 if (!(s->hw->flags & MUSTEK_FLAG_PRO))
3253 return do_stop (s);
3254 }
3255 while (busy);
3256
3257 s->hw->bpl = result[1] | (result[2] << 8);
3258 s->hw->lines = result[3] | (result[4] << 8) | (result[5] << 16);
3259
3260 res = SANE_UNFIX (s->val[OPT_RESOLUTION].w);
3261 half_res = SANE_UNFIX (s->hw->dpi_range.max) / 2;
3262 /* Need to interpolate resolutions > max x-resolution? */
3263 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X) && (res > half_res))
3264 {
3265 *bpl = (s->hw->bpl * res) / half_res / 3;
3266 *bpl *= 3;
3267 DBG (4, "get_image_status: resolution > x-max; enlarge %d bpl to "
3268 "%d bpl\n", s->hw->bpl, *bpl);
3269 }
3270 else
3271 *bpl = s->hw->bpl;
3272
3273 *lines = s->hw->lines - offset;
3274
3275 DBG (3, "get_image_status: bytes_per_line=%d, lines=%d (offset = %d)\n",
3276 *bpl, *lines, offset);
3277 return SANE_STATUS_GOOD;
3278 }
3279
3280 /* ScanExpress models */
3281 static SANE_Status
get_window(Mustek_Scanner * s, SANE_Int * bpl, SANE_Int * lines, SANE_Int * pixels)3282 get_window (Mustek_Scanner * s, SANE_Int * bpl, SANE_Int * lines,
3283 SANE_Int * pixels)
3284 {
3285 SANE_Byte result[48];
3286 SANE_Status status;
3287 size_t len;
3288 SANE_Int color;
3289 long res, half_res;
3290
3291 res = s->resolution_code;
3292 half_res = SANE_UNFIX (s->hw->dpi_range.max) / 2;
3293
3294 DBG (5, "get_window: resolution: %ld dpi (hardware: %d dpi)\n",
3295 res, s->ld.peak_res);
3296
3297 len = sizeof (result);
3298 status = dev_cmd (s, scsi_get_window, sizeof (scsi_get_window), result,
3299 &len);
3300 if (status != SANE_STATUS_GOOD)
3301 return status;
3302
3303 if (!s->scanning)
3304 return do_stop (s);
3305
3306 s->hw->cal.bytes = (result[6] << 24) | (result[7] << 16) |
3307 (result[8] << 8) | (result[9] << 0);
3308 s->hw->cal.lines = (result[10] << 24) | (result[11] << 16) |
3309 (result[12] << 8) | (result[13] << 0);
3310
3311 DBG (4, "get_window: calibration bpl=%d, lines=%d\n",
3312 s->hw->cal.bytes, s->hw->cal.lines);
3313
3314 s->hw->bpl = (result[14] << 24) | (result[15] << 16) |
3315 (result[16] << 8) | result[17];
3316
3317 s->hw->lines = (result[18] << 24) | (result[19] << 16) |
3318 (result[20] << 8) | result[21];
3319
3320 DBG (4, "get_window: scan bpl=%d, lines=%d\n", s->hw->bpl, s->hw->lines);
3321
3322 if ((s->hw->cal.bytes == 0) || (s->hw->cal.lines == 0)
3323 || (s->hw->bpl == 0) || (s->hw->lines == 0))
3324 {
3325 DBG (1, "get_window: oops, none of these values should be 0 "
3326 "-- exiting\n");
3327 return SANE_STATUS_INVAL;
3328 }
3329
3330 s->hw->gamma_length = 1 << result[26];
3331 DBG (4, "get_window: gamma length=%d\n", s->hw->gamma_length);
3332
3333 if (s->mode & MUSTEK_MODE_COLOR)
3334 {
3335 s->ld.buf[0] = NULL;
3336 for (color = 0; color < 3; ++color)
3337 {
3338 s->ld.dist[color] = result[42 + color];
3339 }
3340
3341 DBG (4, "get_window: LD res=%d, (r/g/b)=(%d/%d/%d)\n",
3342 (result[40] << 8) | result[41], s->ld.dist[0],
3343 s->ld.dist[1], s->ld.dist[2]);
3344 s->ld.max_value = (result[40] << 8) | result[41];
3345 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X) && (res > half_res))
3346 {
3347 /* We must interpolate resolutions > max x-resolution */
3348 *bpl = *pixels = (((s->hw->bpl / 3) * res) / half_res) * 3;
3349 }
3350 else
3351 {
3352 /* Scale down the image according to desired resolution */
3353 *bpl = *pixels = (((s->hw->bpl / 3) * res) / s->ld.peak_res) * 3;
3354 }
3355 *lines = (s->hw->lines - s->ld.dist[2]) * res / s->ld.peak_res;
3356 }
3357 else
3358 {
3359 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X) && (res > half_res))
3360 {
3361 /* We must interpolate resolutions > max x-resolution */
3362 *bpl = s->hw->bpl * res / half_res;
3363 }
3364 else
3365 {
3366 *bpl = s->hw->bpl;
3367 }
3368 *lines = s->hw->lines;
3369 }
3370 DBG (4, "get_window: bpl = %d (hardware: %d), lines = %d (hardware: %d)\n",
3371 *bpl, s->hw->bpl, *lines, s->hw->lines);
3372 return SANE_STATUS_GOOD;
3373 }
3374
3375 static SANE_Status
adf_and_backtrack(Mustek_Scanner * s)3376 adf_and_backtrack (Mustek_Scanner * s)
3377 {
3378 SANE_Byte backtrack[6];
3379 SANE_Int code = 0x80;
3380
3381 if (!(s->hw->flags & MUSTEK_FLAG_NO_BACKTRACK))
3382 code |= 0x02; /* enable backtracking */
3383
3384 if (strcmp (s->val[OPT_SOURCE].s, "Automatic Document Feeder") == 0)
3385 code |= 0x01;
3386 else if (strcmp (s->val[OPT_SOURCE].s, "Transparency Adapter") == 0)
3387 code |= 0x04;
3388 memset (backtrack, 0, sizeof (backtrack));
3389 backtrack[0] = MUSTEK_SCSI_ADF_AND_BACKTRACK;
3390 backtrack[4] = code;
3391
3392 DBG (4, "adf_and_backtrack: backtrack: %s; ADF: %s; TA: %s\n",
3393 code & 0x02 ? "yes" : "no", code & 0x01 ? "yes" : "no",
3394 code & 0x04 ? "yes" : "no");
3395 return dev_cmd (s, backtrack, sizeof (backtrack), 0, 0);
3396 }
3397
3398 /* 600 II N firmware 2.x */
3399 static SANE_Int
fix_line_distance_n_2(Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out)3400 fix_line_distance_n_2 (Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl,
3401 SANE_Byte * raw, SANE_Byte * out)
3402 {
3403 SANE_Byte *out_end, *out_ptr, *raw_end = raw + num_lines * bpl;
3404 SANE_Int c, num_saved_lines, line;
3405
3406 if (!s->ld.buf[0])
3407 {
3408 /* This buffer must be big enough to hold maximum line distance
3409 times max_bpl bytes. The maximum line distance for the
3410 Paragon 600 II N scanner is 23, so 40 should be safe. */
3411 DBG (5,
3412 "fix_line_distance_n_2: allocating temp buffer of %d*%d bytes\n",
3413 MAX_LINE_DIST, bpl);
3414 s->ld.buf[0] = malloc (MAX_LINE_DIST * (long) bpl);
3415 if (!s->ld.buf[0])
3416 {
3417 DBG (1,
3418 "fix_line_distance_n_2: failed to malloc temporary buffer\n");
3419 return 0;
3420 }
3421 }
3422
3423 num_saved_lines = s->ld.index[0] - s->ld.index[2];
3424 if (num_saved_lines > 0)
3425 /* restore the previously saved lines: */
3426 memcpy (out, s->ld.buf[0], num_saved_lines * bpl);
3427
3428 while (1)
3429 {
3430 if (++s->ld.lmod3 >= 3)
3431 s->ld.lmod3 = 0;
3432
3433 c = color_seq[s->ld.lmod3];
3434 if (s->ld.index[c] < 0)
3435 ++s->ld.index[c];
3436 else if (s->ld.index[c] < s->params.lines)
3437 {
3438 s->ld.quant[c] += s->ld.peak_res;
3439 if (s->ld.quant[c] > s->ld.max_value)
3440 {
3441 s->ld.quant[c] -= s->ld.max_value;
3442 line = s->ld.index[c]++ - s->ld.ld_line;
3443 out_ptr = out + line * bpl + c;
3444 out_end = out_ptr + bpl;
3445 while (out_ptr != out_end)
3446 {
3447 *out_ptr = *raw++;
3448 out_ptr += 3;
3449 }
3450
3451 if (raw >= raw_end)
3452 {
3453 DBG (3, "fix_line_distance_n_2: lmod3=%d, "
3454 "index=(%d,%d,%d)\n", s->ld.lmod3,
3455 s->ld.index[0], s->ld.index[1], s->ld.index[2]);
3456 num_lines = s->ld.index[2] - s->ld.ld_line;
3457
3458 /* copy away the lines with at least one missing
3459 color component, so that we can interleave them
3460 with new scan data on the next call */
3461 num_saved_lines = s->ld.index[0] - s->ld.index[2];
3462 memcpy (s->ld.buf[0], out + num_lines * bpl,
3463 num_saved_lines * bpl);
3464
3465 /* notice the number of lines we processed */
3466 s->ld.ld_line = s->ld.index[2];
3467 /* return number of complete (r+g+b) lines */
3468 return num_lines;
3469 }
3470 }
3471 }
3472 }
3473 }
3474
3475 /* 600 II N firmware 1.x */
3476 static SANE_Int
fix_line_distance_n_1(Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out)3477 fix_line_distance_n_1 (Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl,
3478 SANE_Byte * raw, SANE_Byte * out)
3479 {
3480 SANE_Byte *out_end, *out_ptr, *raw_end = raw + num_lines * bpl;
3481 SANE_Int c, num_saved_lines, line;
3482
3483 /* For firmware 1.x the scanarea must be soemwhat bigger than needed
3484 because of the linedistance correction */
3485
3486 if (!s->ld.buf[0])
3487 {
3488 /* This buffer must be big enough to hold maximum line distance
3489 times max_bpl bytes. The maximum line distance for the 600 II N
3490 is 23, so 40 is safe. */
3491 DBG (5,
3492 "fix_line_distance_n_1: allocating temp buffer of %d*%d bytes\n",
3493 MAX_LINE_DIST, bpl);
3494 s->ld.buf[0] = malloc (MAX_LINE_DIST * (long) bpl);
3495 if (!s->ld.buf[0])
3496 {
3497 DBG (1,
3498 "fix_line_distance_n_1: failed to malloc temporary buffer\n");
3499 return 0;
3500 }
3501 }
3502 num_saved_lines = s->ld.index[0] - s->ld.index[1];
3503 DBG (5, "fix_line_distance_n_1: got %d lines, %d bpl\n", num_lines, bpl);
3504 DBG (5, "fix_line_distance_n_1: num_saved_lines = %d; peak_res = %d; "
3505 "max_value = %d\n", num_saved_lines, s->ld.peak_res, s->ld.max_value);
3506 if (num_saved_lines > 0)
3507 /* restore the previously saved lines: */
3508 memcpy (out, s->ld.buf[0], num_saved_lines * bpl);
3509
3510 while (1)
3511 {
3512 if (++s->ld.lmod3 >= 3)
3513 s->ld.lmod3 = 0;
3514 c = s->ld.lmod3;
3515 if (s->ld.index[c] < 0)
3516 ++s->ld.index[c];
3517 else
3518 {
3519 s->ld.quant[c] += s->ld.peak_res;
3520 if (s->ld.quant[c] > s->ld.max_value)
3521 {
3522 s->ld.quant[c] -= s->ld.max_value;
3523 line = s->ld.index[c]++ - s->ld.ld_line;
3524 out_ptr = out + line * bpl + c;
3525 out_end = out_ptr + bpl;
3526 while (out_ptr != out_end)
3527 {
3528 *out_ptr = *raw++;
3529 out_ptr += 3;
3530 }
3531 DBG (5, "fix_line_distance_n_1: copied line %d (color %d)\n",
3532 line, c);
3533 }
3534 }
3535 if ((raw >= raw_end) || ((s->ld.index[0] >= s->params.lines) &&
3536 (s->ld.index[1] >= s->params.lines) &&
3537 (s->ld.index[2] >= s->params.lines)))
3538 {
3539 DBG (3, "fix_line_distance_n_1: lmod3=%d, index=(%d,%d,%d)%s\n",
3540 s->ld.lmod3,
3541 s->ld.index[0], s->ld.index[1], s->ld.index[2],
3542 raw >= raw_end ? " raw >= raw_end" : "");
3543 num_lines = s->ld.index[1] - s->ld.ld_line;
3544 if (num_lines < 0)
3545 num_lines = 0;
3546 DBG (4, "fix_line_distance_n_1: lines ready: %d\n", num_lines);
3547
3548 /* copy away the lines with at least one missing
3549 color component, so that we can interleave them
3550 with new scan data on the next call */
3551 num_saved_lines = s->ld.index[0] - s->ld.index[1];
3552 DBG (4, "fix_line_distance_n_1: copied %d lines to "
3553 "ld.buf\n", num_saved_lines);
3554 memcpy (s->ld.buf[0], out + num_lines * bpl, num_saved_lines * bpl);
3555 /* notice the number of lines we processed */
3556 s->ld.ld_line = s->ld.index[1];
3557 if (s->ld.ld_line < 0)
3558 s->ld.ld_line = 0;
3559 /* return number of complete (r+g+b) lines */
3560 return num_lines;
3561 }
3562
3563 }
3564 }
3565
3566 /* For ScanExpress models */
3567 static SANE_Int
fix_line_distance_se(Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out)3568 fix_line_distance_se (Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl,
3569 SANE_Byte * raw, SANE_Byte * out)
3570 {
3571 SANE_Byte *raw_end = raw + num_lines * bpl;
3572 SANE_Byte *out_ptr[3], *ptr;
3573 SANE_Int index[3], lines[3], quant[3], dist[3];
3574 SANE_Int max_value;
3575 SANE_Int color, pixel, res, half_res, scale;
3576 SANE_Int bpc = bpl / 3; /* bytes per color (per line) */
3577 SANE_Bool preview = SANE_FALSE;
3578
3579 res = s->resolution_code;
3580 half_res = SANE_UNFIX (s->hw->dpi_range.max) / 2;
3581 max_value = s->ld.max_value;
3582 if ((s->val[OPT_PREVIEW].w == SANE_TRUE)
3583 && (s->val[OPT_FAST_PREVIEW].w == SANE_TRUE))
3584 {
3585 preview = SANE_TRUE;
3586 /*max_value = 75; */
3587 dist[0] = s->ld.dist[0];
3588 dist[1] = s->ld.dist[1];
3589 dist[2] = s->ld.dist[2];
3590 }
3591 else
3592 {
3593 dist[0] = s->ld.dist[0];
3594 dist[1] = s->ld.dist[1];
3595 dist[2] = s->ld.dist[2];
3596 }
3597
3598 if (!s->ld.buf[0])
3599 {
3600 /* This buffer must be big enough to hold maximum line distance times
3601 3*bpl bytes. The maximum line distance for 1200 dpi is 32 */
3602 DBG (5, "fix_line_distance_se: allocating temp buffer of %d*%d bytes\n",
3603 3 * MAX_LINE_DIST, bpc);
3604 s->ld.buf[0] = malloc (3 * MAX_LINE_DIST * (long) bpc);
3605
3606 if (!s->ld.buf[0])
3607 {
3608 DBG (1,
3609 "fix_line_distance_se: failed to malloc temporary buffer\n");
3610 return 0;
3611 }
3612
3613 /* Note that either s->ld.buf[1] or s->ld.buf[2] is never used. */
3614 s->ld.buf[1] = s->ld.buf[2] =
3615 s->ld.buf[0] + 2 * MAX_LINE_DIST * (long) bpc;
3616
3617 /* Since the blocks don't start necessarily with red note color. */
3618 s->ld.color = 0;
3619
3620 /* The scan area must be longer than desired because of the line
3621 distance. So me must count complete (r+g+b) lines already
3622 submitted to the fronted. */
3623 s->ld.ld_line = s->params.lines;
3624
3625 for (color = 0; color < 3; ++color)
3626 {
3627 s->ld.index[color] = -dist[color];
3628 s->ld.quant[color] = 0;
3629 s->ld.saved[color] = 0;
3630 }
3631 }
3632
3633 num_lines *= 3;
3634 DBG (5, "fix_line_distance_se: start color: %d; %d lines \n",
3635 s->ld.color, num_lines);
3636
3637 /* First scan the lines read and count red, green and blue ones.
3638 Since we will step through the lines a second time we must not
3639 alter any global variables here! */
3640 for (color = 0; color < 3; ++color)
3641 {
3642 index[color] = s->ld.index[color];
3643 lines[color] = s->ld.saved[color];
3644 quant[color] = s->ld.quant[color];
3645 }
3646
3647 color = s->ld.color;
3648 while (num_lines > 0)
3649 {
3650 if (index[color] < 0)
3651 ++index[color];
3652 else
3653 {
3654 quant[color] += res;
3655 if (quant[color] >= max_value)
3656 {
3657 /* This line must be processed, not dropped. */
3658 quant[color] -= max_value;
3659 ++lines[color];
3660 --num_lines;
3661 }
3662 else if (!preview)
3663 --num_lines;
3664
3665 }
3666 if (++color > 2)
3667 color = 0;
3668 }
3669
3670 /* Calculate how many triples of color lines we can output now.
3671 Because the number of available red lines is always greater
3672 than for the other colors we may ignore the red ones here. */
3673 num_lines = MIN (lines[1], lines[2]);
3674
3675 DBG (5, "fix_line_distance_se: saved lines: %d/%d/%d\n", s->ld.saved[0],
3676 s->ld.saved[1], s->ld.saved[2]);
3677 DBG (5, "fix_line_distance_se: available: %d/%d/%d --> triples: %d\n",
3678 lines[0], lines[1], lines[2], num_lines);
3679
3680 lines[0] = lines[1] = lines[2] = num_lines;
3681
3682 /* Output the color lines saved in previous call first.
3683 Note that data is converted in r/g/b interleave on the fly. */
3684 for (color = 0; color < 3; ++color)
3685 {
3686 out_ptr[color] = out + color;
3687 ptr = s->ld.buf[color];
3688 while ((s->ld.saved[color] > 0) && (lines[color] > 0))
3689 {
3690 scale = 0;
3691 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X) && (res > half_res))
3692 {
3693 /* Need to enlarge x-resolution */
3694 SANE_Byte *ptr_start = ptr;
3695 for (pixel = 0; pixel < s->params.pixels_per_line; ++pixel)
3696 {
3697 *out_ptr[color] = *ptr;
3698 out_ptr[color] += 3;
3699 scale += half_res;
3700 if (scale >= half_res)
3701 {
3702 scale -= res;
3703 ++ptr;
3704 }
3705 }
3706 DBG (5, "fix_line_distance_se: got saved line: %d; line: %d; "
3707 "color: %d; raw bytes: %lu; out bytes: %d\n",
3708 s->ld.saved[color], lines[color], color, (u_long) (ptr - ptr_start),
3709 s->params.pixels_per_line);
3710 ptr = ptr_start + bpc;
3711 }
3712 else
3713 {
3714 if (preview)
3715 {
3716 for (pixel = 0; pixel < bpc; ++pixel)
3717 {
3718 *out_ptr[color] = *ptr++;
3719 out_ptr[color] += 3;
3720 }
3721 }
3722 else
3723 {
3724 for (pixel = 0; pixel < bpc; ++pixel)
3725 {
3726 scale += res;
3727 if (scale >= max_value)
3728 {
3729 scale -= max_value;
3730 *out_ptr[color] = *ptr;
3731 out_ptr[color] += 3;
3732 }
3733 ++ptr;
3734 }
3735 }
3736 DBG (5, "fix_line_distance_se: got saved line: %d; line: %d; "
3737 "color: %d\n", s->ld.saved[color], lines[color], color);
3738 }
3739 --(s->ld.saved[color]);
3740 --lines[color];
3741 }
3742 if (s->ld.saved[color] > 0)
3743 memmove (s->ld.buf[color], ptr, s->ld.saved[color] * bpc);
3744 }
3745
3746 while (1)
3747 {
3748 if (s->ld.index[s->ld.color] < 0)
3749 ++(s->ld.index[s->ld.color]);
3750 else
3751 {
3752 s->ld.quant[s->ld.color] += res;
3753 if (s->ld.quant[s->ld.color] >= max_value)
3754 {
3755 /* This line must be processed, not dropped. */
3756 s->ld.quant[s->ld.color] -= max_value;
3757
3758 if (lines[s->ld.color] > 0)
3759 {
3760 /* There's still a line to be output for current color.
3761 Then shuffle current color line to output buffer. */
3762 scale = 0;
3763 /* need to enlarge x-resolution? */
3764 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X)
3765 && (res > half_res))
3766 {
3767 SANE_Byte *raw_start = raw;
3768 for (pixel = 0; pixel < s->params.pixels_per_line;
3769 ++pixel)
3770 {
3771 *out_ptr[s->ld.color] = *raw;
3772 out_ptr[s->ld.color] += 3;
3773 scale += half_res;
3774 if (scale >= half_res)
3775 {
3776 scale -= res;
3777 ++raw;
3778 }
3779
3780 }
3781 DBG (5,
3782 "fix_line_distance_se: got line: %d; color: %d; "
3783 "raw bytes: %lu; out bytes: %d\n",
3784 lines[s->ld.color], s->ld.color, (u_long) (raw - raw_start),
3785 s->params.pixels_per_line);
3786 raw = raw_start + bpc;
3787 }
3788 else
3789 {
3790 if (preview)
3791 {
3792 for (pixel = 0; pixel < bpc; ++pixel)
3793 {
3794 *out_ptr[s->ld.color] = *raw++;
3795 out_ptr[s->ld.color] += 3;
3796 }
3797 }
3798 else
3799 {
3800 for (pixel = 0; pixel < bpc; ++pixel)
3801 {
3802 scale += res;
3803 if (scale >= max_value)
3804 {
3805 scale -= max_value;
3806 *out_ptr[s->ld.color] = *raw;
3807 out_ptr[s->ld.color] += 3;
3808 }
3809 ++raw;
3810 }
3811 }
3812
3813 DBG (5, "fix_line_distance_se: got line: %d; color: "
3814 "%d\n", lines[s->ld.color], s->ld.color);
3815 }
3816 --lines[s->ld.color];
3817 }
3818 else
3819 {
3820 /* At least one component missing, so save this line. */
3821 memcpy (s->ld.buf[s->ld.color] + s->ld.saved[s->ld.color]
3822 * bpc, raw, bpc);
3823 DBG (5, "fix_line_distance_se: saved line %d; color %d\n",
3824 s->ld.saved[s->ld.color], s->ld.color);
3825 ++(s->ld.saved[s->ld.color]);
3826 raw += bpc;
3827 }
3828 }
3829 else
3830 {
3831 if (!preview)
3832 raw += bpc;
3833 DBG (5, "fix_line_distance_se: ignored line; color: %d\n",
3834 s->ld.color);
3835 }
3836
3837 if (raw >= raw_end)
3838 {
3839 /* Reduce num_lines if we encounter excess lines. */
3840 if (num_lines > s->ld.ld_line)
3841 num_lines = s->ld.ld_line;
3842 s->ld.ld_line -= num_lines;
3843
3844 if (++s->ld.color > 2)
3845 s->ld.color = 0;
3846 return num_lines;
3847 }
3848 }
3849 if (++s->ld.color > 2)
3850 s->ld.color = 0;
3851 }
3852 }
3853
3854
3855 /* For Pro models. Not really a linedistance correction (they don't need one)
3856 only enlarging x-res here */
3857 static void
fix_line_distance_pro(Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out)3858 fix_line_distance_pro (Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl,
3859 SANE_Byte * raw, SANE_Byte * out)
3860 {
3861 SANE_Byte *out_addr, *in_addr;
3862 SANE_Int res, half_res, y, x_out, x_in;
3863
3864
3865 res = SANE_UNFIX (s->val[OPT_RESOLUTION].w);
3866 half_res = SANE_UNFIX (s->hw->dpi_range.max) / 2;
3867
3868 DBG (5, "fix_line_distance_pro: res=%d; halfres=%d; num_lines=%d; bpl=%d\n",
3869 res, half_res, num_lines, bpl);
3870
3871 if (strcmp (s->val[OPT_BIT_DEPTH].s, "12") == 0)
3872 {
3873 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X) && (res > half_res))
3874 {
3875 /*12 bit, need to enlarge x-resolution */
3876 DBG (5, "fix_line_distance_pro: res > half_res --> need to "
3877 "enlarge x\n");
3878 if (little_endian ())
3879 for (y = 0; y < num_lines; y++)
3880 {
3881 for (x_out = 0; x_out < s->params.pixels_per_line; x_out++)
3882 {
3883 x_in = x_out * bpl / s->params.bytes_per_line / 2;
3884 x_in *= 2;
3885 out_addr = out + y * s->params.bytes_per_line + x_out * 6;
3886 in_addr = raw + y * bpl + x_in * 6;
3887 *(out_addr) = *(in_addr) << 4;
3888 *(out_addr + 1) = (*(in_addr) >> 4) +
3889 (*(in_addr + 1) << 4);
3890 *(out_addr + 2) = *(in_addr + 2) << 4;
3891 *(out_addr + 3) = (*(in_addr + 2) >> 4) +
3892 (*(in_addr + 3) << 4);
3893 *(out_addr + 4) = *(in_addr + 4) << 4;
3894 *(out_addr + 5) = (*(in_addr + 4) >> 4) +
3895 (*(in_addr + 5) << 4);
3896 }
3897 }
3898 else /* big endian */
3899 for (y = 0; y < num_lines; y++)
3900 {
3901 for (x_out = 0; x_out < s->params.pixels_per_line; x_out++)
3902 {
3903 x_in = x_out * bpl / s->params.bytes_per_line / 2;
3904 out_addr = out + y * s->params.bytes_per_line + x_out * 6;
3905 in_addr = raw + y * bpl + x_in * 6;
3906 *(out_addr) = (*(in_addr) >> 4) + (*(in_addr + 1) << 4);
3907 *(out_addr + 1) = *(in_addr) << 4;
3908 *(out_addr + 2) = (*(in_addr + 2) >> 4) +
3909 (*(in_addr + 3) << 4);
3910 *(out_addr + 3) = *(in_addr + 2) << 4;
3911 *(out_addr + 4) = (*(in_addr + 4) >> 4) +
3912 (*(in_addr + 5) << 4);
3913 *(out_addr + 5) = *(in_addr + 4) << 4;
3914 }
3915 }
3916 }
3917 else /* 12 bit, no need to enlarge x */
3918 {
3919 SANE_Word pixel;
3920
3921 if (little_endian ())
3922 for (pixel = 0; pixel < (num_lines * bpl / 2); pixel++)
3923 {
3924 *(out + pixel * 2) = *(raw + pixel * 2) << 4;
3925 *(out + pixel * 2 + 1) = (*(raw + pixel * 2) >> 4) +
3926 (*(raw + pixel * 2 + 1) << 4);
3927 }
3928 else /* big endian */
3929 for (pixel = 0; pixel < (num_lines * bpl / 2); pixel++)
3930 {
3931 *(out + pixel * 2) = (*(raw + pixel * 2) >> 4) +
3932 (*(raw + pixel * 2 + 1) << 4);
3933 *(out + pixel * 2 + 1) = *(raw + pixel * 2) << 4;
3934 }
3935
3936 }
3937 }
3938 else /* 8 bit */
3939 {
3940 /* need to enlarge x-resolution? */
3941 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X) && (res > half_res))
3942 {
3943 DBG (5, "fix_line_distance_pro: res > half_res --> need to "
3944 "enlarge x\n");
3945
3946 for (y = 0; y < num_lines; y++)
3947 {
3948 for (x_out = 0; x_out < s->params.pixels_per_line; x_out++)
3949 {
3950 x_in = x_out * bpl / s->params.bytes_per_line;
3951 out_addr = out + y * s->params.bytes_per_line + x_out * 3;
3952 in_addr = raw + y * bpl + x_in * 3;
3953 *(out_addr) = *(in_addr);
3954 *(out_addr + 1) = *(in_addr + 1);
3955 *(out_addr + 2) = *(in_addr + 2);
3956 }
3957 }
3958 }
3959 else
3960 memcpy (out, raw, num_lines * bpl);
3961 }
3962 return;
3963 }
3964
3965 /* For MFS-08000SP, MFS-06000SP. MFC-08000CZ, MFC-06000CZ */
3966 static void
fix_line_distance_normal(Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out)3967 fix_line_distance_normal (Mustek_Scanner * s, SANE_Int num_lines,
3968 SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out)
3969 {
3970 SANE_Byte *out_end, *out_ptr, *raw_end = raw + num_lines * bpl;
3971 SANE_Int index[3]; /* index of the next output line for color C */
3972 SANE_Int i, color;
3973
3974 /* Initialize the indices with the line distances that were returned
3975 by the CCD linedistance command or set manually (option
3976 linedistance-fix). We want to skip data for the first OFFSET
3977 rounds, so we initialize the indices to the negative of this
3978 offset. */
3979
3980 DBG (5, "fix_line_distance_normal: %d lines, %d bpl\n", num_lines, bpl);
3981
3982 for (color = 0; color < 3; ++color)
3983 index[color] = -s->ld.dist[color];
3984
3985 while (1)
3986 {
3987 for (i = 0; i < 3; ++i)
3988 {
3989 color = color_seq[i];
3990 if (index[color] < 0)
3991 ++index[color];
3992 else if (index[color] < num_lines)
3993 {
3994 s->ld.quant[color] += s->ld.peak_res;
3995 if (s->ld.quant[color] > s->ld.max_value)
3996 {
3997 s->ld.quant[color] -= s->ld.max_value;
3998 out_ptr = out + index[color] * bpl + color;
3999 out_end = out_ptr + bpl;
4000 while (out_ptr != out_end)
4001 {
4002 *out_ptr = *raw++;
4003 out_ptr += 3;
4004 }
4005 ++index[color];
4006 if (raw >= raw_end)
4007 return;
4008 }
4009 }
4010 }
4011 }
4012 }
4013
4014 /* Paragon series I + II. */
4015 static SANE_Int
fix_line_distance_block(Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out, SANE_Int num_lines_total)4016 fix_line_distance_block (Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl,
4017 SANE_Byte * raw, SANE_Byte * out,
4018 SANE_Int num_lines_total)
4019 {
4020 SANE_Byte *out_end, *out_ptr, *raw_end = raw + num_lines * bpl;
4021 SANE_Int c, num_saved_lines, line, max_index, min_index;
4022
4023 if (!s->ld.buf[0])
4024 {
4025 DBG (5, "fix_line_distance_block: allocating temp buffer of %d*%d "
4026 "bytes\n", MAX_LINE_DIST, bpl);
4027 s->ld.buf[0] = malloc (MAX_LINE_DIST * (long) bpl);
4028 if (!s->ld.buf[0])
4029 {
4030 DBG (1, "fix_line_distance_block: failed to malloc temporary "
4031 "buffer\n");
4032 return 0;
4033 }
4034 }
4035 DBG (5, "fix_line_distance_block: s->ld.index = {%d, %d, %d}, "
4036 "s->ld.lmod3 = %d\n", s->ld.index[0], s->ld.index[1], s->ld.index[2],
4037 s->ld.lmod3);
4038 DBG (5, "fix_line_distance_block: s->ld.quant = {%d, %d, %d}, "
4039 "s->ld.max_value = %d\n", s->ld.quant[0], s->ld.quant[1],
4040 s->ld.quant[2], s->ld.max_value);
4041 DBG (5,
4042 "fix_line_distance_block: s->ld.peak_res = %d, s->ld.ld_line = %d\n",
4043 s->ld.peak_res, s->ld.ld_line);
4044
4045 /* search maximum and minimum index */
4046 max_index = MAX (s->ld.index[0], MAX (s->ld.index[1], s->ld.index[2]));
4047 min_index = MIN (s->ld.index[0], MIN (s->ld.index[1], s->ld.index[2]));
4048 num_saved_lines = max_index - min_index;
4049 if (s->ld.index[0] == 0)
4050 num_saved_lines = 0;
4051 /* restore the previously saved lines: */
4052 memcpy (out, s->ld.buf[0], num_saved_lines * bpl);
4053 DBG (5, "fix_line_distance_block: copied %d lines from "
4054 "ld.buf to buffer (max=%d, min=%d)\n", num_saved_lines, max_index,
4055 min_index);
4056 while (1)
4057 {
4058 if (++s->ld.lmod3 >= 3)
4059 s->ld.lmod3 = 0;
4060
4061 c = color_seq[s->ld.lmod3];
4062 if (s->ld.index[c] < 0)
4063 ++s->ld.index[c];
4064 else if (s->ld.index[c] < num_lines_total)
4065 {
4066 s->ld.quant[c] += s->ld.peak_res;
4067 if (s->ld.quant[c] > s->ld.max_value)
4068 {
4069 s->ld.quant[c] -= s->ld.max_value;
4070 line = s->ld.index[c]++ - s->ld.ld_line;
4071 out_ptr = out + line * bpl + c;
4072 out_end = out_ptr + bpl;
4073 while (out_ptr != out_end)
4074 {
4075 *out_ptr = *raw++;
4076 out_ptr += 3;
4077 }
4078 DBG (5, "fix_line_distance_block: copied line %d (color %d)\n",
4079 line + s->ld.ld_line, c);
4080
4081 max_index = MAX (s->ld.index[0],
4082 MAX (s->ld.index[1], s->ld.index[2]));
4083 min_index = MIN (s->ld.index[0],
4084 MIN (s->ld.index[1], s->ld.index[2]));
4085 if ((raw >= raw_end) || ((min_index >= num_lines_total)))
4086 {
4087 DBG (5, "fix_line_distance_block: got num_lines: %d\n",
4088 num_lines);
4089 num_lines = min_index - s->ld.ld_line;
4090 if (num_lines < 0)
4091 num_lines = 0;
4092 if ((s->total_lines + num_lines) > s->params.lines)
4093 num_lines = s->params.lines - s->total_lines;
4094 s->total_lines += num_lines;
4095
4096 /* copy away the lines with at least one missing
4097 color component, so that we can interleave them
4098 with new scan data on the next call */
4099 num_saved_lines = max_index - min_index;
4100
4101 DBG (5, "fix_line_distance_block: num_saved_lines = %d; "
4102 "num_lines = %d; bpl = %d\n", num_saved_lines,
4103 num_lines, bpl);
4104
4105 memcpy (s->ld.buf[0], out + num_lines * bpl,
4106 num_saved_lines * bpl);
4107
4108 DBG (5, "fix_line_distance_block: copied %d lines to "
4109 "ld.buf\n", num_saved_lines);
4110
4111 /* notice the number of lines we processed */
4112 s->ld.ld_line = min_index;
4113 if (s->ld.ld_line < 0)
4114 s->ld.ld_line = 0;
4115
4116 DBG (4, "fix_line_distance_block: lmod3=%d, "
4117 "index=(%d,%d,%d), line = %d, lines = %d\n",
4118 s->ld.lmod3,
4119 s->ld.index[0], s->ld.index[1], s->ld.index[2],
4120 s->ld.ld_line, num_lines);
4121 /* return number of complete (r+g+b) lines */
4122 return num_lines;
4123 }
4124 }
4125 }
4126 }
4127 }
4128
4129 /* For MFS-1200SP 1.00 and others */
4130 /* No LD correction necessary, just shuffle around data */
4131 static SANE_Int
fix_line_distance_none(Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl, SANE_Byte * raw, SANE_Byte * out)4132 fix_line_distance_none (Mustek_Scanner * s, SANE_Int num_lines, SANE_Int bpl,
4133 SANE_Byte * raw, SANE_Byte * out)
4134 {
4135 SANE_Byte *red_ptr, *grn_ptr, *blu_ptr, *ptr, *ptr_end;
4136 SANE_Word y;
4137
4138 ptr = out;
4139 red_ptr = raw;
4140
4141 DBG (5, "fix_line_distance_none: no ld correction necessary (%d lines)\n",
4142 num_lines);
4143
4144 s->ld.ld_line += num_lines;
4145
4146 if (s->ld.ld_line > s->params.lines)
4147 num_lines -= (s->ld.ld_line - s->params.lines);
4148 if (num_lines < 0)
4149 num_lines = 0;
4150
4151 DBG (5, "fix_line_distance_none: using %d lines (ld_line = %d, "
4152 "s->params.lines = %d)\n", num_lines, s->ld.ld_line, s->params.lines);
4153
4154 for (y = 0; y < num_lines; ++y)
4155 {
4156 grn_ptr = red_ptr + bpl / 3;
4157 blu_ptr = grn_ptr + bpl / 3;
4158 ptr_end = red_ptr + bpl;
4159
4160 while (blu_ptr != ptr_end)
4161 {
4162 *ptr++ = *red_ptr++;
4163 *ptr++ = *grn_ptr++;
4164 *ptr++ = *blu_ptr++;
4165 }
4166 red_ptr = ptr_end;
4167 }
4168 return num_lines;
4169 }
4170
4171
4172 static SANE_Status
init_options(Mustek_Scanner * s)4173 init_options (Mustek_Scanner * s)
4174 {
4175 SANE_Int i, j, gammasize;
4176
4177 memset (s->opt, 0, sizeof (s->opt));
4178 memset (s->val, 0, sizeof (s->val));
4179
4180 for (i = 0; i < NUM_OPTIONS; ++i)
4181 {
4182 s->opt[i].size = sizeof (SANE_Word);
4183 s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
4184 }
4185
4186 s->opt[OPT_NUM_OPTS].name = "";
4187 s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
4188 s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
4189 s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
4190 s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
4191 s->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
4192
4193 /* "Mode" group: */
4194 s->opt[OPT_MODE_GROUP].title = SANE_I18N ("Scan Mode");
4195 s->opt[OPT_MODE_GROUP].desc = "";
4196 s->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
4197 s->opt[OPT_MODE_GROUP].cap = 0;
4198 s->opt[OPT_MODE_GROUP].size = 0;
4199 s->opt[OPT_MODE_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
4200
4201 /* scan mode */
4202 s->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
4203 s->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
4204 s->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
4205 s->opt[OPT_MODE].type = SANE_TYPE_STRING;
4206 s->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
4207 if (s->hw->flags & MUSTEK_FLAG_SE)
4208 {
4209 s->opt[OPT_MODE].size = max_string_size (mode_list_se);
4210 s->opt[OPT_MODE].constraint.string_list = mode_list_se;
4211 s->val[OPT_MODE].s = strdup (mode_list_se[1]);
4212 if (!s->val[OPT_MODE].s)
4213 return SANE_STATUS_NO_MEM;
4214 }
4215 else
4216 {
4217 s->opt[OPT_MODE].size = max_string_size (mode_list_paragon);
4218 s->opt[OPT_MODE].constraint.string_list = mode_list_paragon;
4219 s->val[OPT_MODE].s = strdup (mode_list_paragon[2]);
4220 if (!s->val[OPT_MODE].s)
4221 return SANE_STATUS_NO_MEM;
4222 }
4223
4224 /* fast gray mode (pro models) */
4225 s->opt[OPT_FAST_GRAY_MODE].name = "fast-gray-mode";
4226 s->opt[OPT_FAST_GRAY_MODE].title = SANE_I18N ("Fast gray mode");
4227 s->opt[OPT_FAST_GRAY_MODE].desc = SANE_I18N ("Scan in fast gray mode "
4228 "(lower quality).");
4229 s->opt[OPT_FAST_GRAY_MODE].type = SANE_TYPE_BOOL;
4230 s->val[OPT_FAST_GRAY_MODE].w = SANE_FALSE;
4231 s->opt[OPT_FAST_GRAY_MODE].cap |= SANE_CAP_INACTIVE;
4232 if (s->hw->flags & MUSTEK_FLAG_PRO)
4233 {
4234 /* Only Pro models support fast gray mode */
4235 s->opt[OPT_FAST_GRAY_MODE].cap &= ~SANE_CAP_INACTIVE;
4236 }
4237
4238 /* resolution */
4239 s->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
4240 s->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
4241 s->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
4242 s->opt[OPT_RESOLUTION].type = SANE_TYPE_FIXED;
4243 s->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
4244 s->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_RANGE;
4245 s->opt[OPT_RESOLUTION].constraint.range = &s->hw->dpi_range;
4246 s->val[OPT_RESOLUTION].w = MAX (SANE_FIX (72), s->hw->dpi_range.min);
4247
4248 /* bit depth */
4249 s->opt[OPT_BIT_DEPTH].name = SANE_NAME_BIT_DEPTH;
4250 s->opt[OPT_BIT_DEPTH].title = SANE_TITLE_BIT_DEPTH;
4251 s->opt[OPT_BIT_DEPTH].desc = SANE_DESC_BIT_DEPTH;
4252 s->opt[OPT_BIT_DEPTH].type = SANE_TYPE_STRING;
4253 s->opt[OPT_BIT_DEPTH].constraint_type = SANE_CONSTRAINT_STRING_LIST;
4254 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
4255 s->opt[OPT_BIT_DEPTH].size = max_string_size (bit_depth_list_pro);
4256 s->opt[OPT_BIT_DEPTH].constraint.string_list = bit_depth_list_pro;
4257 s->val[OPT_BIT_DEPTH].s = strdup (bit_depth_list_pro[0]);
4258 if (!s->val[OPT_BIT_DEPTH].s)
4259 return SANE_STATUS_NO_MEM;
4260
4261 /* speed */
4262 s->opt[OPT_SPEED].name = SANE_NAME_SCAN_SPEED;
4263 s->opt[OPT_SPEED].title = SANE_TITLE_SCAN_SPEED;
4264 s->opt[OPT_SPEED].desc = SANE_DESC_SCAN_SPEED;
4265 s->opt[OPT_SPEED].type = SANE_TYPE_STRING;
4266 s->opt[OPT_SPEED].size = max_string_size (speed_list);
4267 s->opt[OPT_SPEED].constraint_type = SANE_CONSTRAINT_STRING_LIST;
4268 s->opt[OPT_SPEED].constraint.string_list = speed_list;
4269 s->val[OPT_SPEED].s = strdup (speed_list[4]);
4270 if (!s->val[OPT_SPEED].s)
4271 return SANE_STATUS_NO_MEM;
4272 if (!(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
4273 {
4274 /* Speed only supported by 3-pass scanners */
4275 s->opt[OPT_SPEED].cap |= SANE_CAP_INACTIVE;
4276 }
4277
4278 /* source */
4279 s->opt[OPT_SOURCE].name = SANE_NAME_SCAN_SOURCE;
4280 s->opt[OPT_SOURCE].title = SANE_TITLE_SCAN_SOURCE;
4281 s->opt[OPT_SOURCE].desc = SANE_DESC_SCAN_SOURCE;
4282 s->opt[OPT_SOURCE].type = SANE_TYPE_STRING;
4283
4284 if ((s->hw->flags & MUSTEK_FLAG_SE) || (s->hw->flags & MUSTEK_FLAG_N)
4285 || (s->hw->flags & MUSTEK_FLAG_TA))
4286 {
4287 s->opt[OPT_SOURCE].size = max_string_size (ta_source_list);
4288 s->opt[OPT_SOURCE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
4289 s->opt[OPT_SOURCE].constraint.string_list = ta_source_list;
4290 s->val[OPT_SOURCE].s = strdup (ta_source_list[0]);
4291 if (!s->val[OPT_SOURCE].s)
4292 return SANE_STATUS_NO_MEM;
4293 }
4294 else if (s->hw->flags & MUSTEK_FLAG_ADF)
4295 {
4296 s->opt[OPT_SOURCE].size = max_string_size (adf_source_list);
4297 s->opt[OPT_SOURCE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
4298 s->opt[OPT_SOURCE].constraint.string_list = adf_source_list;
4299 s->val[OPT_SOURCE].s = strdup (adf_source_list[0]);
4300 if (!s->val[OPT_SOURCE].s)
4301 return SANE_STATUS_NO_MEM;
4302 }
4303 else
4304 {
4305 s->opt[OPT_SOURCE].size = max_string_size (source_list);
4306 s->opt[OPT_SOURCE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
4307 s->opt[OPT_SOURCE].constraint.string_list = source_list;
4308 s->val[OPT_SOURCE].s = strdup (source_list[0]);
4309 s->opt[OPT_SOURCE].cap |= SANE_CAP_INACTIVE;
4310 if (!s->val[OPT_SOURCE].s)
4311 return SANE_STATUS_NO_MEM;
4312 }
4313
4314 /* preview */
4315 s->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
4316 s->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
4317 s->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
4318 s->opt[OPT_PREVIEW].cap = SANE_CAP_SOFT_DETECT | SANE_CAP_SOFT_SELECT;
4319 s->val[OPT_PREVIEW].w = 0;
4320
4321 /* fast preview */
4322 s->opt[OPT_FAST_PREVIEW].name = "fast-preview";
4323 s->opt[OPT_FAST_PREVIEW].title = SANE_I18N ("Fast preview");
4324 s->opt[OPT_FAST_PREVIEW].desc = SANE_I18N ("Request that all previews are "
4325 "done in the fastest (low-quality) mode. This may be a non-color "
4326 "mode or a low resolution mode.");
4327 s->opt[OPT_FAST_PREVIEW].type = SANE_TYPE_BOOL;
4328 s->val[OPT_FAST_PREVIEW].w = SANE_FALSE;
4329
4330 /* lamp off time*/
4331 s->opt[OPT_LAMP_OFF_TIME].name = "lamp-off-time";
4332 s->opt[OPT_LAMP_OFF_TIME].title = SANE_I18N ("Lamp off time (minutes)");
4333 s->opt[OPT_LAMP_OFF_TIME].desc = SANE_I18N ("Set the time (in minutes) after "
4334 "which the lamp is shut off.");
4335 s->opt[OPT_LAMP_OFF_TIME].type = SANE_TYPE_INT;
4336 if (strcmp (s->hw->sane.model, "1200 A3 PRO") != 0)
4337 s->opt[OPT_LAMP_OFF_TIME].cap |= SANE_CAP_INACTIVE;
4338 s->opt[OPT_LAMP_OFF_TIME].constraint_type = SANE_CONSTRAINT_RANGE;
4339 s->opt[OPT_LAMP_OFF_TIME].constraint.range = &u8_range;
4340 s->val[OPT_LAMP_OFF_TIME].w = 60;
4341
4342 /* shut lamp off */
4343 s->opt[OPT_LAMP_OFF_BUTTON].name = "lamp-off";
4344 s->opt[OPT_LAMP_OFF_BUTTON].title = SANE_I18N ("Turn lamp off");
4345 s->opt[OPT_LAMP_OFF_BUTTON].desc = SANE_I18N ("Turns the lamp off immediately.");
4346 s->opt[OPT_LAMP_OFF_BUTTON].type = SANE_TYPE_BUTTON;
4347 s->opt[OPT_LAMP_OFF_BUTTON].cap = SANE_CAP_SOFT_SELECT;
4348 if (strcmp (s->hw->sane.model, "1200 A3 PRO") != 0)
4349 s->opt[OPT_LAMP_OFF_BUTTON].cap |= SANE_CAP_INACTIVE;
4350
4351 /* "Geometry" group: */
4352 s->opt[OPT_GEOMETRY_GROUP].title = SANE_I18N ("Geometry");
4353 s->opt[OPT_GEOMETRY_GROUP].desc = "";
4354 s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
4355 s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
4356 s->opt[OPT_GEOMETRY_GROUP].size = 0;
4357 s->opt[OPT_GEOMETRY_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
4358
4359 /* top-left x */
4360 s->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
4361 s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
4362 s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
4363 s->opt[OPT_TL_X].type = SANE_TYPE_FIXED;
4364 s->opt[OPT_TL_X].unit = SANE_UNIT_MM;
4365 s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
4366 s->opt[OPT_TL_X].constraint.range = &s->hw->x_range;
4367 s->val[OPT_TL_X].w = s->hw->x_range.min;
4368
4369 /* top-left y */
4370 s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
4371 s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
4372 s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
4373 s->opt[OPT_TL_Y].type = SANE_TYPE_FIXED;
4374 s->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
4375 s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
4376 s->opt[OPT_TL_Y].constraint.range = &s->hw->y_range;
4377 s->val[OPT_TL_Y].w = s->hw->y_range.min;
4378
4379 /* bottom-right x */
4380 s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
4381 s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
4382 s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
4383 s->opt[OPT_BR_X].type = SANE_TYPE_FIXED;
4384 s->opt[OPT_BR_X].unit = SANE_UNIT_MM;
4385 s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
4386 s->opt[OPT_BR_X].constraint.range = &s->hw->x_range;
4387 s->val[OPT_BR_X].w = s->hw->x_range.max;
4388
4389 /* bottom-right y */
4390 s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
4391 s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
4392 s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
4393 s->opt[OPT_BR_Y].type = SANE_TYPE_FIXED;
4394 s->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
4395 s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
4396 s->opt[OPT_BR_Y].constraint.range = &s->hw->y_range;
4397 s->val[OPT_BR_Y].w = s->hw->y_range.max;
4398
4399 /* "Enhancement" group: */
4400 s->opt[OPT_ENHANCEMENT_GROUP].title = SANE_I18N ("Enhancement");
4401 s->opt[OPT_ENHANCEMENT_GROUP].desc = "";
4402 s->opt[OPT_ENHANCEMENT_GROUP].type = SANE_TYPE_GROUP;
4403 s->opt[OPT_ENHANCEMENT_GROUP].cap = 0;
4404 s->opt[OPT_ENHANCEMENT_GROUP].size = 0;
4405 s->opt[OPT_ENHANCEMENT_GROUP].constraint_type = SANE_CONSTRAINT_NONE;
4406
4407 /* brightness */
4408 s->opt[OPT_BRIGHTNESS].name = SANE_NAME_BRIGHTNESS;
4409 s->opt[OPT_BRIGHTNESS].title = SANE_TITLE_BRIGHTNESS;
4410 s->opt[OPT_BRIGHTNESS].desc = SANE_DESC_BRIGHTNESS;
4411 s->opt[OPT_BRIGHTNESS].type = SANE_TYPE_FIXED;
4412 s->opt[OPT_BRIGHTNESS].unit = SANE_UNIT_PERCENT;
4413 s->opt[OPT_BRIGHTNESS].constraint_type = SANE_CONSTRAINT_RANGE;
4414 s->opt[OPT_BRIGHTNESS].constraint.range = &percentage_range;
4415 if (!(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
4416 /* 1-pass scanners don't support brightness in multibit mode */
4417 s->opt[OPT_BRIGHTNESS].cap |= SANE_CAP_INACTIVE;
4418 s->val[OPT_BRIGHTNESS].w = 0;
4419
4420 /* brightness red */
4421 s->opt[OPT_BRIGHTNESS_R].name = "brightness-r";
4422 s->opt[OPT_BRIGHTNESS_R].title = SANE_I18N ("Red brightness");
4423 s->opt[OPT_BRIGHTNESS_R].desc = SANE_I18N ("Controls the brightness of "
4424 "the red channel of the "
4425 "acquired image.");
4426 s->opt[OPT_BRIGHTNESS_R].type = SANE_TYPE_FIXED;
4427 s->opt[OPT_BRIGHTNESS_R].unit = SANE_UNIT_PERCENT;
4428 s->opt[OPT_BRIGHTNESS_R].constraint_type = SANE_CONSTRAINT_RANGE;
4429 s->opt[OPT_BRIGHTNESS_R].constraint.range = &percentage_range;
4430 s->opt[OPT_BRIGHTNESS_R].cap |= SANE_CAP_INACTIVE;
4431 s->val[OPT_BRIGHTNESS_R].w = 0;
4432
4433 /* brightness green */
4434 s->opt[OPT_BRIGHTNESS_G].name = "brightness-g";
4435 s->opt[OPT_BRIGHTNESS_G].title = SANE_I18N ("Green brightness");
4436 s->opt[OPT_BRIGHTNESS_G].desc = SANE_I18N ("Controls the brightness of "
4437 "the green channel of the "
4438 "acquired image.");
4439 s->opt[OPT_BRIGHTNESS_G].type = SANE_TYPE_FIXED;
4440 s->opt[OPT_BRIGHTNESS_G].unit = SANE_UNIT_PERCENT;
4441 s->opt[OPT_BRIGHTNESS_G].constraint_type = SANE_CONSTRAINT_RANGE;
4442 s->opt[OPT_BRIGHTNESS_G].constraint.range = &percentage_range;
4443 s->opt[OPT_BRIGHTNESS_G].cap |= SANE_CAP_INACTIVE;
4444 s->val[OPT_BRIGHTNESS_G].w = 0;
4445
4446 /* brightness blue */
4447 s->opt[OPT_BRIGHTNESS_B].name = "brightness-b";
4448 s->opt[OPT_BRIGHTNESS_B].title = SANE_I18N ("Blue brightness");
4449 s->opt[OPT_BRIGHTNESS_B].desc = SANE_I18N ("Controls the brightness of "
4450 "the blue channel of the "
4451 "acquired image.");
4452 s->opt[OPT_BRIGHTNESS_B].type = SANE_TYPE_FIXED;
4453 s->opt[OPT_BRIGHTNESS_B].unit = SANE_UNIT_PERCENT;
4454 s->opt[OPT_BRIGHTNESS_B].constraint_type = SANE_CONSTRAINT_RANGE;
4455 s->opt[OPT_BRIGHTNESS_B].constraint.range = &percentage_range;
4456 s->opt[OPT_BRIGHTNESS_B].cap |= SANE_CAP_INACTIVE;
4457 s->val[OPT_BRIGHTNESS_B].w = 0;
4458
4459 /* contrast */
4460 s->opt[OPT_CONTRAST].name = SANE_NAME_CONTRAST;
4461 s->opt[OPT_CONTRAST].title = SANE_TITLE_CONTRAST;
4462 s->opt[OPT_CONTRAST].desc = SANE_DESC_CONTRAST;
4463 s->opt[OPT_CONTRAST].type = SANE_TYPE_FIXED;
4464 s->opt[OPT_CONTRAST].unit = SANE_UNIT_PERCENT;
4465 s->opt[OPT_CONTRAST].constraint_type = SANE_CONSTRAINT_RANGE;
4466 s->opt[OPT_CONTRAST].constraint.range = &percentage_range;
4467 if (!(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
4468 /* 1-pass scanners don't support contrast in multibit mode */
4469 s->opt[OPT_CONTRAST].cap |= SANE_CAP_INACTIVE;
4470 s->val[OPT_CONTRAST].w = 0;
4471
4472 /* contrast red */
4473 s->opt[OPT_CONTRAST_R].name = "contrast-r";
4474 s->opt[OPT_CONTRAST_R].title = SANE_I18N ("Contrast red channel");
4475 s->opt[OPT_CONTRAST_R].desc = SANE_I18N ("Controls the contrast of "
4476 "the red channel of the "
4477 "acquired image.");
4478 s->opt[OPT_CONTRAST_R].type = SANE_TYPE_FIXED;
4479 s->opt[OPT_CONTRAST_R].unit = SANE_UNIT_PERCENT;
4480 s->opt[OPT_CONTRAST_R].constraint_type = SANE_CONSTRAINT_RANGE;
4481 s->opt[OPT_CONTRAST_R].constraint.range = &percentage_range;
4482 s->opt[OPT_CONTRAST_R].cap |= SANE_CAP_INACTIVE;
4483 s->val[OPT_CONTRAST_R].w = 0;
4484
4485 /* contrast green */
4486 s->opt[OPT_CONTRAST_G].name = "contrast-g";
4487 s->opt[OPT_CONTRAST_G].title = SANE_I18N ("Contrast green channel");
4488 s->opt[OPT_CONTRAST_G].desc = SANE_I18N ("Controls the contrast of "
4489 "the green channel of the "
4490 "acquired image.");
4491 s->opt[OPT_CONTRAST_G].type = SANE_TYPE_FIXED;
4492 s->opt[OPT_CONTRAST_G].unit = SANE_UNIT_PERCENT;
4493 s->opt[OPT_CONTRAST_G].constraint_type = SANE_CONSTRAINT_RANGE;
4494 s->opt[OPT_CONTRAST_G].constraint.range = &percentage_range;
4495 s->opt[OPT_CONTRAST_G].cap |= SANE_CAP_INACTIVE;
4496 s->val[OPT_CONTRAST_G].w = 0;
4497
4498 /* contrast blue */
4499 s->opt[OPT_CONTRAST_B].name = "contrast-b";
4500 s->opt[OPT_CONTRAST_B].title = SANE_I18N ("Contrast blue channel");
4501 s->opt[OPT_CONTRAST_B].desc = SANE_I18N ("Controls the contrast of "
4502 "the blue channel of the "
4503 "acquired image.");
4504 s->opt[OPT_CONTRAST_B].type = SANE_TYPE_FIXED;
4505 s->opt[OPT_CONTRAST_B].unit = SANE_UNIT_PERCENT;
4506 s->opt[OPT_CONTRAST_B].constraint_type = SANE_CONSTRAINT_RANGE;
4507 s->opt[OPT_CONTRAST_B].constraint.range = &percentage_range;
4508 s->opt[OPT_CONTRAST_B].cap |= SANE_CAP_INACTIVE;
4509 s->val[OPT_CONTRAST_B].w = 0;
4510
4511 /* gamma */
4512 gammasize = 256;
4513 for (i = 0; i < 4; ++i)
4514 for (j = 0; j < gammasize; ++j)
4515 s->gamma_table[i][j] = j;
4516
4517 /* custom-gamma table */
4518 s->opt[OPT_CUSTOM_GAMMA].name = SANE_NAME_CUSTOM_GAMMA;
4519 s->opt[OPT_CUSTOM_GAMMA].title = SANE_TITLE_CUSTOM_GAMMA;
4520 s->opt[OPT_CUSTOM_GAMMA].desc = SANE_DESC_CUSTOM_GAMMA;
4521 s->opt[OPT_CUSTOM_GAMMA].type = SANE_TYPE_BOOL;
4522 s->val[OPT_CUSTOM_GAMMA].w = SANE_FALSE;
4523
4524 /* grayscale gamma vector */
4525 s->opt[OPT_GAMMA_VECTOR].name = SANE_NAME_GAMMA_VECTOR;
4526 s->opt[OPT_GAMMA_VECTOR].title = SANE_TITLE_GAMMA_VECTOR;
4527 s->opt[OPT_GAMMA_VECTOR].desc = SANE_DESC_GAMMA_VECTOR;
4528 s->opt[OPT_GAMMA_VECTOR].type = SANE_TYPE_INT;
4529 s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
4530 s->opt[OPT_GAMMA_VECTOR].unit = SANE_UNIT_NONE;
4531 s->opt[OPT_GAMMA_VECTOR].size = 256 * sizeof (SANE_Word);
4532 s->val[OPT_GAMMA_VECTOR].wa = &s->gamma_table[0][0];
4533 s->opt[OPT_GAMMA_VECTOR].constraint_type = SANE_CONSTRAINT_RANGE;
4534 s->opt[OPT_GAMMA_VECTOR].constraint.range = &u8_range;
4535
4536 /* red gamma vector */
4537 s->opt[OPT_GAMMA_VECTOR_R].name = SANE_NAME_GAMMA_VECTOR_R;
4538 s->opt[OPT_GAMMA_VECTOR_R].title = SANE_TITLE_GAMMA_VECTOR_R;
4539 s->opt[OPT_GAMMA_VECTOR_R].desc = SANE_DESC_GAMMA_VECTOR_R;
4540 s->opt[OPT_GAMMA_VECTOR_R].type = SANE_TYPE_INT;
4541 s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
4542 s->opt[OPT_GAMMA_VECTOR_R].unit = SANE_UNIT_NONE;
4543 s->opt[OPT_GAMMA_VECTOR_R].size = 256 * sizeof (SANE_Word);
4544 s->val[OPT_GAMMA_VECTOR_R].wa = &s->gamma_table[1][0];
4545 s->opt[OPT_GAMMA_VECTOR_R].constraint_type = SANE_CONSTRAINT_RANGE;
4546 s->opt[OPT_GAMMA_VECTOR_R].constraint.range = &u8_range;
4547
4548 /* green gamma vector */
4549 s->opt[OPT_GAMMA_VECTOR_G].name = SANE_NAME_GAMMA_VECTOR_G;
4550 s->opt[OPT_GAMMA_VECTOR_G].title = SANE_TITLE_GAMMA_VECTOR_G;
4551 s->opt[OPT_GAMMA_VECTOR_G].desc = SANE_DESC_GAMMA_VECTOR_G;
4552 s->opt[OPT_GAMMA_VECTOR_G].type = SANE_TYPE_INT;
4553 s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
4554 s->opt[OPT_GAMMA_VECTOR_G].unit = SANE_UNIT_NONE;
4555 s->opt[OPT_GAMMA_VECTOR_G].size = 256 * sizeof (SANE_Word);
4556 s->val[OPT_GAMMA_VECTOR_G].wa = &s->gamma_table[2][0];
4557 s->opt[OPT_GAMMA_VECTOR_G].constraint_type = SANE_CONSTRAINT_RANGE;
4558 s->opt[OPT_GAMMA_VECTOR_G].constraint.range = &u8_range;
4559
4560 /* blue gamma vector */
4561 s->opt[OPT_GAMMA_VECTOR_B].name = SANE_NAME_GAMMA_VECTOR_B;
4562 s->opt[OPT_GAMMA_VECTOR_B].title = SANE_TITLE_GAMMA_VECTOR_B;
4563 s->opt[OPT_GAMMA_VECTOR_B].desc = SANE_DESC_GAMMA_VECTOR_B;
4564 s->opt[OPT_GAMMA_VECTOR_B].type = SANE_TYPE_INT;
4565 s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
4566 s->opt[OPT_GAMMA_VECTOR_B].unit = SANE_UNIT_NONE;
4567 s->opt[OPT_GAMMA_VECTOR_B].size = 256 * sizeof (SANE_Word);
4568 s->val[OPT_GAMMA_VECTOR_B].wa = &s->gamma_table[3][0];
4569 s->opt[OPT_GAMMA_VECTOR_B].constraint_type = SANE_CONSTRAINT_RANGE;
4570 s->opt[OPT_GAMMA_VECTOR_B].constraint.range = &u8_range;
4571
4572 /* quality calibration */
4573 s->opt[OPT_QUALITY_CAL].name = SANE_NAME_QUALITY_CAL;
4574 s->opt[OPT_QUALITY_CAL].title = SANE_TITLE_QUALITY_CAL;
4575 s->opt[OPT_QUALITY_CAL].desc = SANE_DESC_QUALITY_CAL;
4576 s->opt[OPT_QUALITY_CAL].type = SANE_TYPE_BOOL;
4577 if (s->hw->flags & MUSTEK_FLAG_PRO)
4578 s->val[OPT_QUALITY_CAL].w = SANE_TRUE;
4579 else
4580 s->val[OPT_QUALITY_CAL].w = SANE_FALSE;
4581 s->opt[OPT_QUALITY_CAL].cap |= SANE_CAP_INACTIVE;
4582 if ((s->hw->flags & MUSTEK_FLAG_PRO)
4583 || (s->hw->flags & MUSTEK_FLAG_SE_PLUS))
4584 {
4585 /* Only Pro and SE Plus models support calibration */
4586 s->opt[OPT_QUALITY_CAL].cap &= ~SANE_CAP_INACTIVE;
4587 }
4588
4589 /* halftone dimension */
4590 s->opt[OPT_HALFTONE_DIMENSION].name = SANE_NAME_HALFTONE_DIMENSION;
4591 s->opt[OPT_HALFTONE_DIMENSION].title = SANE_TITLE_HALFTONE_DIMENSION;
4592 s->opt[OPT_HALFTONE_DIMENSION].desc = SANE_DESC_HALFTONE_DIMENSION;
4593 s->opt[OPT_HALFTONE_DIMENSION].type = SANE_TYPE_STRING;
4594 s->opt[OPT_HALFTONE_DIMENSION].size = max_string_size (halftone_list);
4595 s->opt[OPT_HALFTONE_DIMENSION].constraint_type =
4596 SANE_CONSTRAINT_STRING_LIST;
4597 s->opt[OPT_HALFTONE_DIMENSION].constraint.string_list = halftone_list;
4598 s->val[OPT_HALFTONE_DIMENSION].s = strdup (halftone_list[0]);
4599 if (!s->val[OPT_HALFTONE_DIMENSION].s)
4600 return SANE_STATUS_NO_MEM;
4601 s->opt[OPT_HALFTONE_DIMENSION].cap |= SANE_CAP_INACTIVE;
4602
4603 /* halftone pattern */
4604 s->opt[OPT_HALFTONE_PATTERN].name = SANE_NAME_HALFTONE_PATTERN;
4605 s->opt[OPT_HALFTONE_PATTERN].title = SANE_TITLE_HALFTONE_PATTERN;
4606 s->opt[OPT_HALFTONE_PATTERN].desc = SANE_DESC_HALFTONE_PATTERN;
4607 s->opt[OPT_HALFTONE_PATTERN].type = SANE_TYPE_INT;
4608 s->opt[OPT_HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE;
4609 s->opt[OPT_HALFTONE_PATTERN].constraint_type = SANE_CONSTRAINT_RANGE;
4610 s->opt[OPT_HALFTONE_PATTERN].constraint.range = &u8_range;
4611 s->val[OPT_HALFTONE_PATTERN].wa = s->halftone_pattern;
4612
4613 return SANE_STATUS_GOOD;
4614 }
4615
4616 /* The following three functions execute as a child process. The
4617 reason for using a subprocess is that some (most?) generic SCSI
4618 interfaces block a SCSI request until it has completed. With a
4619 subprocess, we can let it block waiting for the request to finish
4620 while the main process can go about to do more important things
4621 (such as recognizing when the user presses a cancel button).
4622
4623 WARNING: Since this is executed as a subprocess, it's NOT possible
4624 to update any of the variables in the main process (in particular
4625 the scanner state cannot be updated).
4626
4627 NOTE: At least for Linux, it seems that we could get rid of the
4628 subprocess. Linux v2.0 does seem to allow select() on SCSI
4629 descriptors. */
4630
4631 static void
output_data(Mustek_Scanner * s, FILE * fp, SANE_Byte * data, SANE_Int lines_per_buffer, SANE_Int bpl, SANE_Byte * extra)4632 output_data (Mustek_Scanner * s, FILE * fp,
4633 SANE_Byte * data, SANE_Int lines_per_buffer, SANE_Int bpl,
4634 SANE_Byte * extra)
4635 {
4636 SANE_Byte *ptr, *ptr_end;
4637 SANE_Int y, num_lines;
4638
4639 DBG (5, "output_data: data=%p, lpb=%d, bpl=%d, extra=%p\n",
4640 (void *) data, lines_per_buffer, bpl, (void *) extra);
4641
4642 /* convert to pixel-interleaved format: */
4643 if ((s->mode & MUSTEK_MODE_COLOR)
4644 && !(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
4645 {
4646 num_lines = lines_per_buffer;
4647
4648 /* need to correct for distance between r/g/b sensors: */
4649 if (s->hw->flags & MUSTEK_FLAG_PRO)
4650 fix_line_distance_pro (s, num_lines, bpl, data, extra);
4651 else if (s->hw->flags & MUSTEK_FLAG_SE)
4652 {
4653 num_lines = fix_line_distance_se (s, num_lines, bpl, data, extra);
4654 }
4655 else if (s->hw->flags & MUSTEK_FLAG_N)
4656 {
4657 if (s->hw->flags & MUSTEK_FLAG_LD_N2)
4658 num_lines = fix_line_distance_n_2 (s, num_lines, bpl, data,
4659 extra);
4660 else
4661 num_lines = fix_line_distance_n_1 (s, num_lines, bpl, data,
4662 extra);
4663 }
4664 else if ((s->hw->flags & MUSTEK_FLAG_LD_BLOCK)
4665 && (s->ld.max_value != 0))
4666 {
4667 if (s->hw->flags & MUSTEK_FLAG_PARAGON_1)
4668 num_lines = fix_line_distance_block (s, num_lines, bpl, data,
4669 extra, s->hw->lines);
4670 else
4671 num_lines = fix_line_distance_block (s, num_lines, bpl, data,
4672 extra,
4673 s->hw->lines_per_block);
4674 }
4675 else if (!(s->hw->flags & MUSTEK_FLAG_LD_NONE)
4676 && (s->ld.max_value != 0))
4677 fix_line_distance_normal (s, num_lines, bpl, data, extra);
4678 else
4679 num_lines = fix_line_distance_none (s, num_lines, bpl, data, extra);
4680
4681 if (strcmp (s->val[OPT_SOURCE].s, "Automatic Document Feeder") == 0)
4682 {
4683 /* need to revert line direction */
4684 SANE_Int line_number;
4685 SANE_Int byte_number;
4686
4687 DBG (5, "output_data: ADF found, mirroring lines\n");
4688 for (line_number = 0; line_number < num_lines; line_number++)
4689 {
4690 for (byte_number = bpl - 3; byte_number >= 0; byte_number -= 3)
4691 {
4692 fputc (*(extra + line_number * bpl + byte_number), fp);
4693 fputc (*(extra + line_number * bpl + byte_number + 1), fp);
4694 fputc (*(extra + line_number * bpl + byte_number + 2), fp);
4695 }
4696 }
4697 }
4698 else
4699 fwrite (extra, num_lines, s->params.bytes_per_line, fp);
4700 }
4701 else
4702 {
4703 DBG (5, "output_data: write %d lpb; %d bpl\n", lines_per_buffer, bpl);
4704 /* Scale x-resolution above 1/2 of the maximum resolution for
4705 SE and Pro scanners */
4706 if ((s->hw->flags & MUSTEK_FLAG_ENLARGE_X) &&
4707 (s->val[OPT_RESOLUTION].w > (s->hw->dpi_range.max / 2)))
4708 {
4709 SANE_Int x;
4710 SANE_Int half_res = SANE_UNFIX (s->hw->dpi_range.max) / 2;
4711 SANE_Int res = SANE_UNFIX (s->val[OPT_RESOLUTION].w);
4712 SANE_Int res_counter;
4713 SANE_Int enlarged_x;
4714
4715 DBG (5, "output_data: enlarge lines from %d bpl to %d bpl\n",
4716 s->hw->bpl, s->params.bytes_per_line);
4717
4718 for (y = 0; y < lines_per_buffer; y++)
4719 {
4720 SANE_Byte byte = 0;
4721
4722 x = 0;
4723 res_counter = 0;
4724 enlarged_x = 0;
4725
4726 while (enlarged_x < s->params.pixels_per_line)
4727 {
4728 if (s->mode & MUSTEK_MODE_GRAY)
4729 {
4730 fputc (*(data + y * bpl + x), fp);
4731 res_counter += half_res;
4732 if (res_counter >= half_res)
4733 {
4734 res_counter -= res;
4735 x++;
4736 }
4737 enlarged_x++;
4738 }
4739 else /* lineart */
4740 {
4741 /* need to invert image because of funny SANE 1-bit image
4742 polarity */
4743 if (*(data + x / 8 + y * bpl) & (1 << (7 - (x % 8))))
4744 byte |= 1 << (7 - (enlarged_x % 8));
4745
4746 if ((enlarged_x % 8) == 7)
4747 {
4748 fputc (~byte, fp); /* invert image */
4749 byte = 0;
4750 }
4751 res_counter += half_res;
4752 if (res_counter >= half_res)
4753 {
4754 res_counter -= res;
4755 x++;
4756 }
4757 enlarged_x++;
4758 }
4759 }
4760 }
4761 }
4762 else /* lineart, gray or halftone (nothing to scale) */
4763 {
4764 if ((s->mode & MUSTEK_MODE_LINEART)
4765 || (s->mode & MUSTEK_MODE_HALFTONE))
4766 {
4767 /* need to invert image because of funny SANE 1-bit image
4768 polarity */
4769 ptr = data;
4770 ptr_end = ptr + lines_per_buffer * bpl;
4771
4772 if (strcmp (s->val[OPT_SOURCE].s,
4773 "Automatic Document Feeder") == 0)
4774 {
4775 while (ptr != ptr_end)
4776 {
4777 (*ptr) = ~(*ptr);
4778 ptr++;
4779 /* need to revert bit direction */
4780 *ptr = ((*ptr & 0x80) >> 7) + ((*ptr & 0x40) >> 5)
4781 + ((*ptr & 0x20) >> 3) + ((*ptr & 0x10) >> 1)
4782 + ((*ptr & 0x08) << 1) + ((*ptr & 0x04) << 3)
4783 + ((*ptr & 0x02) << 5) + ((*ptr & 0x01) << 7);
4784 }
4785 }
4786 else
4787 while (ptr != ptr_end)
4788 {
4789 (*ptr) = ~(*ptr);
4790 ptr++;
4791 }
4792 }
4793 if (strcmp (s->val[OPT_SOURCE].s, "Automatic Document Feeder") == 0)
4794 {
4795 /* need to revert line direction */
4796 SANE_Int line_number;
4797 SANE_Int byte_number;
4798
4799 DBG (5, "output_data: ADF found, mirroring lines\n");
4800 for (line_number = 0; line_number < lines_per_buffer;
4801 line_number++)
4802 {
4803 for (byte_number = bpl - 1; byte_number >= 0; byte_number--)
4804 {
4805 fputc (*(data + line_number * bpl + byte_number), fp);
4806 }
4807 }
4808 }
4809 else
4810 {
4811 fwrite (data, lines_per_buffer, bpl, fp);
4812 }
4813 }
4814 }
4815 DBG (5, "output_data: end\n");
4816 }
4817
4818 static void
sigterm_handler(int signal)4819 sigterm_handler (int signal)
4820 {
4821 DBG (4,
4822 "sigterm_handler: started, signal is %d, starting sanei_scsi_req_flush_all()\n",
4823 signal);
4824 sanei_scsi_req_flush_all (); /* flush SCSI queue */
4825 DBG (4,
4826 "sigterm_handler: sanei_scsi_req_flush_all() finisheshed, _exiting()\n");
4827 _exit (SANE_STATUS_GOOD);
4828 }
4829
4830
4831 static SANE_Int
reader_process(void *data)4832 reader_process (void *data)
4833 {
4834 Mustek_Scanner *s = (Mustek_Scanner *) data;
4835 SANE_Int lines_per_buffer, bpl;
4836 SANE_Byte *extra = 0, *ptr;
4837 sigset_t sigterm_set;
4838 struct SIGACTION act;
4839 SANE_Status status;
4840 FILE *fp;
4841 int fd = s->reader_fds;
4842 SANE_Int buffernumber = 0;
4843 SANE_Int buffer_count, max_buffers;
4844 struct
4845 {
4846 void *id; /* scsi queue id */
4847 SANE_Byte *data; /* data buffer */
4848 SANE_Byte *command; /* command buffer */
4849 SANE_Int lines; /* # lines in buffer */
4850 size_t num_read; /* # of bytes read (return value) */
4851 SANE_Int bank; /* needed by SE models */
4852 SANE_Bool ready; /* ready to send to application? */
4853 SANE_Bool finished; /* block is finished */
4854 }
4855 bstat[2];
4856
4857 DBG (3, "reader_process: started\n");
4858 if (sanei_thread_is_forked ())
4859 {
4860 DBG (4, "reader_process: using fork ()\n");
4861 close (s->pipe);
4862 s->pipe = -1;
4863 }
4864 else
4865 {
4866 DBG (4, "reader_process: using threads\n");
4867 }
4868
4869 if (sanei_thread_is_forked ())
4870 {
4871 /* ignore SIGTERM while writing SCSI commands */
4872 sigemptyset (&sigterm_set);
4873 sigaddset (&sigterm_set, SIGTERM);
4874
4875 /* call our sigterm handler to clean up ongoing SCSI requests */
4876 memset (&act, 0, sizeof (act));
4877 act.sa_handler = sigterm_handler;
4878 sigaction (SIGTERM, &act, 0);
4879 }
4880
4881 if (disable_double_buffering)
4882 DBG (3, "reader_process: disable_double_buffering is set, this may be "
4883 "slow\n");
4884
4885 fp = fdopen (fd, "w");
4886 if (!fp)
4887 return SANE_STATUS_IO_ERROR;
4888
4889 s->total_lines = 0;
4890 bpl = s->hw->bpl;
4891
4892 /* buffer size is scanner dependent */
4893 lines_per_buffer = s->hw->buffer_size / bpl / 2;
4894
4895 if (strip_height > 0.0)
4896 {
4897 SANE_Int max_lines;
4898 double dpi;
4899
4900 dpi = SANE_UNFIX (s->val[OPT_RESOLUTION].w);
4901 max_lines = (int) (strip_height * dpi + 0.5);
4902
4903 if (lines_per_buffer > max_lines)
4904 {
4905 DBG (2, "reader_process: limiting strip height to %g inches "
4906 "(%d lines)\n", strip_height, max_lines);
4907 lines_per_buffer = max_lines;
4908 }
4909 }
4910
4911 if (!lines_per_buffer)
4912 {
4913 DBG (1, "reader_process: bpl (%d) > SCSI buffer size / 2 (%d)\n",
4914 bpl, s->hw->buffer_size / 2);
4915 return SANE_STATUS_NO_MEM; /* resolution is too high */
4916 }
4917
4918 DBG (4, "reader_process: %d lines per buffer, %d bytes per line, "
4919 "%d bytes per buffer\n", lines_per_buffer, bpl,
4920 lines_per_buffer * bpl);
4921
4922 bstat[0].data = malloc (2 * lines_per_buffer * (long) bpl);
4923 if (!bstat[0].data)
4924 {
4925 DBG (1, "reader_process: failed to malloc %ld bytes for data buffer\n",
4926 lines_per_buffer * (long) bpl);
4927 return SANE_STATUS_NO_MEM;
4928 }
4929 bstat[1].data = bstat[0].data + lines_per_buffer * (long) bpl;
4930
4931 bstat[0].command = malloc (2 * 10);
4932 if (!bstat[0].command)
4933 {
4934 DBG (1,
4935 "reader_process: failed to malloc %d bytes for command buffer\n",
4936 2 * 10);
4937 return SANE_STATUS_NO_MEM;
4938 }
4939 bstat[1].command = bstat[0].command + 10;
4940
4941 /* Touch all pages of the buffer to fool the memory management. */
4942 ptr = bstat[0].data + 2 * lines_per_buffer * (long) bpl - 1;
4943 while (ptr >= bstat[0].data)
4944 {
4945 *ptr = 0x00;
4946 ptr -= 256;
4947 }
4948
4949 if (!(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
4950 {
4951 /* get temporary buffer for line-distance correction and/or bit
4952 expansion. For some scanners more space is needed because the
4953 data must be read in as single big block (cut up into pieces
4954 of lines_per_buffer). This requires that the line distance
4955 correction continues on every call exactly where it stopped
4956 if the image shall be reconstructed without any stripes. */
4957
4958 extra = malloc ((lines_per_buffer + MAX_LINE_DIST)
4959 * (long) s->params.bytes_per_line);
4960 if (!extra)
4961 {
4962 DBG (1, "reader_process: failed to malloc extra buffer\n");
4963 return SANE_STATUS_NO_MEM;
4964 }
4965 }
4966
4967 if (s->hw->flags & MUSTEK_FLAG_N)
4968 {
4969 /* reacquire port access rights (lost because of fork()): */
4970 sanei_ab306_get_io_privilege (s->fd);
4971 }
4972
4973 if ((s->hw->flags & MUSTEK_FLAG_N) || (s->hw->flags & MUSTEK_FLAG_LD_BLOCK))
4974 {
4975 /* reset counter of line number for line-dictance correction */
4976 s->ld.ld_line = 0;
4977 }
4978
4979 max_buffers = s->hw->max_block_buffer_size / (lines_per_buffer * bpl);
4980 if (max_buffers < 1)
4981 {
4982 DBG (1, "reader_process: buffersize > blocksize!\n");
4983 return SANE_STATUS_NO_MEM;
4984 }
4985 DBG (4, "reader_process: limiting block read to %d buffers (%d lines)\n",
4986 max_buffers, MIN (s->hw->lines, (max_buffers * lines_per_buffer)));
4987
4988 while (s->line < s->hw->lines)
4989 {
4990 s->hw->lines_per_block =
4991 MIN (s->hw->lines - s->line, (max_buffers * lines_per_buffer));
4992 status = dev_block_read_start (s, s->hw->lines_per_block);
4993 if (status != SANE_STATUS_GOOD)
4994 return status;
4995
4996 for (buffernumber = 0; buffernumber < 2; buffernumber++)
4997 {
4998 bstat[buffernumber].ready = SANE_FALSE;
4999 bstat[buffernumber].finished = SANE_FALSE;
5000 }
5001 buffer_count = 0;
5002 buffernumber = 0;
5003
5004 while (1)
5005 {
5006 /* omit reading first two buffers (not yet ready) */
5007 if (bstat[buffernumber].ready == SANE_TRUE)
5008 {
5009 DBG (4, "reader_process: buffer %d: waiting for request to be "
5010 "ready\n", buffernumber + 1);
5011 status = dev_req_wait (bstat[buffernumber].id);
5012 if (status == SANE_STATUS_GOOD)
5013 {
5014 DBG (4, "reader_process: buffer %d is ready, wanted %d, "
5015 "got %ld bytes\n", buffernumber + 1,
5016 bstat[buffernumber].lines * bpl,
5017 (long int) bstat[buffernumber].num_read);
5018 }
5019 else
5020 {
5021 DBG (1, "reader_process: failed to read data, status: %s, "
5022 "buffer: %d\n", sane_strstatus (status),
5023 buffernumber + 1);
5024 if (status == SANE_STATUS_NO_MEM)
5025 {
5026 DBG (1,
5027 "Probably the size of the kernel SCSI buffer is "
5028 "too small for the\n selected buffersize "
5029 "in mustek.conf. Either decrease "
5030 "buffersize in\n mustek.conf to e.g. 32, "
5031 "increase SG_BIG_BUF in kernel to 130560, "
5032 "or\n use SANE_SG_BUFFERSIZE variable. "
5033 "See man sane-scsi and README for\n "
5034 "details.\n");
5035 }
5036 return status;
5037 }
5038
5039 DBG (4, "reader_process: buffer %d: sending %ld bytes to "
5040 "output_data\n", buffernumber + 1,
5041 (long int) bstat[buffernumber].num_read);
5042 output_data (s, fp, bstat[buffernumber].data,
5043 bstat[buffernumber].lines, bpl, extra);
5044 if (bstat[buffernumber].finished)
5045 break; /* everything written; exit loop */
5046 }
5047 if (disable_double_buffering)
5048 {
5049 /* Enter only one buffer at once */
5050 if (buffernumber == 1)
5051 buffernumber = 0;
5052 else
5053 buffernumber = 1;
5054 }
5055
5056 /* enter read requests only if data left */
5057 if ((s->line < s->hw->lines) && (buffer_count < max_buffers))
5058 {
5059 if (s->line + lines_per_buffer >= s->hw->lines)
5060 {
5061 /* do the last few lines: */
5062 bstat[buffernumber].lines = s->hw->lines - s->line;
5063 bstat[buffernumber].bank = 0x01;
5064 bstat[buffernumber].finished = SANE_TRUE;
5065 }
5066 else
5067 {
5068 bstat[buffernumber].lines = lines_per_buffer;
5069 bstat[buffernumber].bank = 0x00;
5070 }
5071
5072 if ((buffer_count + 1) >= max_buffers)
5073 bstat[buffernumber].finished = SANE_TRUE;
5074
5075 s->line += bstat[buffernumber].lines;
5076 bstat[buffernumber].ready = SANE_TRUE;
5077
5078 buffer_count++;
5079
5080 DBG (4,
5081 "reader_process: buffer %d: entering read request for %d "
5082 "bytes (buffer %d)\n", buffernumber + 1,
5083 bstat[buffernumber].lines * bpl, buffer_count);
5084 sigprocmask (SIG_BLOCK, &sigterm_set, 0);
5085 status = dev_read_req_enter (s, bstat[buffernumber].data,
5086 bstat[buffernumber].lines, bpl,
5087 &bstat[buffernumber].num_read,
5088 &bstat[buffernumber].id,
5089 bstat[buffernumber].bank,
5090 bstat[buffernumber].command);
5091 sigprocmask (SIG_UNBLOCK, &sigterm_set, 0);
5092
5093
5094 if (status == SANE_STATUS_GOOD)
5095 {
5096 DBG (5, "reader_process: buffer %d: entered (line %d of %d,"
5097 " buffer %d)\n", buffernumber + 1, s->line,
5098 s->hw->lines, buffer_count);
5099 }
5100 else
5101 {
5102 DBG (1, "reader_process: buffer %d: failed to enter read "
5103 "request, status: %s\n", buffernumber + 1,
5104 sane_strstatus (status));
5105 return status;
5106 }
5107 }
5108 if (!disable_double_buffering)
5109 {
5110 if (buffernumber == 1)
5111 buffernumber = 0;
5112 else
5113 buffernumber = 1;
5114 }
5115 /* This is said to fix the scanner hangs that reportedly show on
5116 some MFS-12000SP scanners. */
5117 if (s->mode == 0 && (s->hw->flags & MUSTEK_FLAG_LINEART_FIX))
5118 usleep (200000);
5119 }
5120 }
5121
5122 fclose (fp);
5123 free (bstat[0].data);
5124 if (s->ld.buf[0])
5125 free (s->ld.buf[0]);
5126 s->ld.buf[0] = NULL;
5127 if (extra)
5128 free (extra);
5129 close (fd);
5130 return SANE_STATUS_GOOD;
5131 }
5132
5133 static SANE_Status
attach_one_device(SANE_String_Const devname)5134 attach_one_device (SANE_String_Const devname)
5135 {
5136 Mustek_Device *dev;
5137
5138 attach (devname, &dev, SANE_FALSE);
5139 if (dev)
5140 {
5141 /* Keep track of newly attached devices so we can set options as
5142 necessary. */
5143 if (new_dev_len >= new_dev_alloced)
5144 {
5145 new_dev_alloced += 4;
5146 if (new_dev)
5147 new_dev =
5148 realloc (new_dev, new_dev_alloced * sizeof (new_dev[0]));
5149 else
5150 new_dev = malloc (new_dev_alloced * sizeof (new_dev[0]));
5151 if (!new_dev)
5152 {
5153 DBG (1, "attach_one_device: out of memory\n");
5154 return SANE_STATUS_NO_MEM;
5155 }
5156 }
5157 new_dev[new_dev_len++] = dev;
5158 }
5159 return SANE_STATUS_GOOD;
5160 }
5161
5162 /**************************************************************************/
5163 /* SANE API calls */
5164 /**************************************************************************/
5165
5166 SANE_Status
sane_init(SANE_Int * version_code, SANE_Auth_Callback authorize)5167 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
5168 {
5169 SANE_Char line[PATH_MAX], *word, *end;
5170 SANE_String_Const cp;
5171 SANE_Int linenumber;
5172 FILE *fp;
5173
5174 DBG_INIT ();
5175
5176 sanei_thread_init ();
5177
5178 #ifdef DBG_LEVEL
5179 debug_level = DBG_LEVEL;
5180 #else
5181 debug_level = 0;
5182 #endif
5183
5184 DBG (2, "SANE mustek backend version %d.%d build %d from %s\n", SANE_CURRENT_MAJOR,
5185 SANE_CURRENT_MINOR, BUILD, PACKAGE_STRING);
5186
5187 if (version_code)
5188 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, BUILD);
5189
5190 DBG (5, "sane_init: authorize %s null\n", authorize ? "!=" : "==");
5191
5192 #ifdef HAVE_SANEI_SCSI_OPEN_EXTENDED
5193 DBG (5, "sane_init: using sanei_scsi_open_extended\n");
5194 #else
5195 DBG (5, "sane_init: using sanei_scsi_open with buffer size = %d bytes\n",
5196 sanei_scsi_max_request_size);
5197 #endif
5198
5199 num_devices = 0;
5200 force_wait = SANE_FALSE;
5201 disable_double_buffering = SANE_FALSE;
5202 first_dev = 0;
5203 first_handle = 0;
5204 devlist = 0;
5205 new_dev = 0;
5206 new_dev_len = 0;
5207 new_dev_alloced = 0;
5208
5209 fp = sanei_config_open (MUSTEK_CONFIG_FILE);
5210 if (!fp)
5211 {
5212 /* default to /dev/scanner instead of insisting on config file */
5213 DBG (3, "sane_init: couldn't find config file (%s), trying "
5214 "/dev/scanner directly\n", MUSTEK_CONFIG_FILE);
5215 attach ("/dev/scanner", 0, SANE_FALSE);
5216 return SANE_STATUS_GOOD;
5217 }
5218 linenumber = 0;
5219 DBG (4, "sane_init: reading config file `%s'\n", MUSTEK_CONFIG_FILE);
5220 while (sanei_config_read (line, sizeof (line), fp))
5221 {
5222 word = 0;
5223 linenumber++;
5224
5225 cp = sanei_config_get_string (line, &word);
5226 if (!word || cp == line)
5227 {
5228 DBG (5, "sane_init: config file line %d: ignoring empty line\n",
5229 linenumber);
5230 if (word)
5231 free (word);
5232 continue;
5233 }
5234 if (word[0] == '#')
5235 {
5236 DBG (5, "sane_init: config file line %d: ignoring comment line\n",
5237 linenumber);
5238 free (word);
5239 continue;
5240 }
5241
5242 if (strcmp (word, "option") == 0)
5243 {
5244 free (word);
5245 word = 0;
5246 cp = sanei_config_get_string (cp, &word);
5247 if (!word)
5248 {
5249 DBG (1,
5250 "sane_init: config file line %d: missing quotation mark?\n",
5251 linenumber);
5252 continue;
5253 }
5254
5255 if (strcmp (word, "strip-height") == 0)
5256 {
5257 free (word);
5258 word = 0;
5259 cp = sanei_config_get_string (cp, &word);
5260 if (!word)
5261 {
5262 DBG (1,
5263 "sane_init: config file line %d: missing quotation mark?\n",
5264 linenumber);
5265 continue;
5266 }
5267
5268 errno = 0;
5269 strip_height = strtod (word, &end);
5270 if (end == word)
5271 {
5272 DBG (3, "sane-init: config file line %d: strip-height "
5273 "must have a parameter; using 1 inch\n", linenumber);
5274 strip_height = 1.0;
5275 }
5276 if (errno)
5277 {
5278 DBG (3, "sane-init: config file line %d: strip-height `%s' "
5279 "is invalid (%s); using 1 inch\n", linenumber,
5280 word, strerror (errno));
5281 strip_height = 1.0;
5282 }
5283 else
5284 {
5285 if (strip_height < 0.1)
5286 strip_height = 0.1;
5287 DBG (3, "sane_init: config file line %d: strip-height set "
5288 "to %g inches\n", linenumber, strip_height);
5289 }
5290 if (word)
5291 free (word);
5292 word = 0;
5293 }
5294 else if (strcmp (word, "force-wait") == 0)
5295 {
5296 DBG (3, "sane_init: config file line %d: enabling force-wait\n",
5297 linenumber);
5298 force_wait = SANE_TRUE;
5299 if (word)
5300 free (word);
5301 word = 0;
5302 }
5303 else if (strcmp (word, "disable-double-buffering") == 0)
5304 {
5305 DBG (3, "sane_init: config file line %d: disabling "
5306 "double-buffering\n", linenumber);
5307 disable_double_buffering = SANE_TRUE;
5308 if (word)
5309 free (word);
5310 word = 0;
5311 }
5312 else if (strcmp (word, "legal-size") == 0)
5313 {
5314 if (new_dev_len > 0)
5315 {
5316 /* Check for 12000 LS, no way to find out automatically */
5317 if (strcmp (new_dev[new_dev_len - 1]->sane.model,
5318 "ScanExpress 12000SP") == 0)
5319 {
5320 new_dev[new_dev_len - 1]->x_range.max =
5321 SANE_FIX (220.0);
5322 new_dev[new_dev_len - 1]->y_range.max =
5323 SANE_FIX (360.0);
5324 new_dev[new_dev_len - 1]->sane.model =
5325 "Paragon 1200 LS";
5326 DBG (3,
5327 "sane_init: config file line %d: enabling "
5328 "legal-size for %s\n", linenumber,
5329 new_dev[new_dev_len - 1]->sane.name);
5330 }
5331 else
5332 {
5333 DBG (3, "sane_init: config file line %d: option "
5334 "legal-size ignored, device %s is not a "
5335 "Paragon 1200 LS\n", linenumber,
5336 new_dev[new_dev_len - 1]->sane.name);
5337 }
5338
5339 }
5340 else
5341 {
5342 DBG (3, "sane_init: config file line %d: option "
5343 "legal-size ignored, was set before any device "
5344 "name\n", linenumber);
5345 }
5346 if (word)
5347 free (word);
5348 word = 0;
5349 }
5350 else if (strcmp (word, "linedistance-fix") == 0)
5351 {
5352 if (new_dev_len > 0)
5353 {
5354 new_dev[new_dev_len - 1]->flags |= MUSTEK_FLAG_LD_FIX;
5355 DBG (3, "sane_init: config file line %d: enabling "
5356 "linedistance-fix for %s\n", linenumber,
5357 new_dev[new_dev_len - 1]->sane.name);
5358 }
5359 else
5360 {
5361 DBG (3, "sane_init: config file line %d: option "
5362 "linedistance-fix ignored, was set before any device "
5363 "name\n", linenumber);
5364 }
5365 if (word)
5366 free (word);
5367 word = 0;
5368 }
5369 else if (strcmp (word, "disable-backtracking") == 0)
5370 {
5371 if (new_dev_len > 0)
5372 {
5373 new_dev[new_dev_len - 1]->flags |= MUSTEK_FLAG_NO_BACKTRACK;
5374 DBG (3, "sane_init: config file line %d: disabling "
5375 "backtracking for %s\n", linenumber,
5376 new_dev[new_dev_len - 1]->sane.name);
5377 }
5378 else
5379 {
5380 DBG (3, "sane_init: config file line %d: option "
5381 "disable-backtracking ignored, was set before any "
5382 "device name\n", linenumber);
5383 }
5384 if (word)
5385 free (word);
5386 word = 0;
5387 }
5388 else if (strcmp (word, "lineart-fix") == 0)
5389 {
5390 if (new_dev_len > 0)
5391 {
5392 new_dev[new_dev_len - 1]->flags |= MUSTEK_FLAG_LINEART_FIX;
5393 DBG (3, "sane_init: config file line %d: enabling "
5394 "lineart-fix for %s\n", linenumber,
5395 new_dev[new_dev_len - 1]->sane.name);
5396 }
5397 else
5398 {
5399 DBG (3, "sane_init: config file line %d: option "
5400 "lineart-fix ignored, was set before any device name\n",
5401 linenumber);
5402 }
5403 if (word)
5404 free (word);
5405 word = 0;
5406 }
5407 else if (strcmp (word, "buffersize") == 0)
5408 {
5409 long buffer_size;
5410
5411 free (word);
5412 word = 0;
5413 cp = sanei_config_get_string (cp, &word);
5414 if (!word)
5415 {
5416 DBG (1,
5417 "sane_init: config file line %d: missing quotation mark?\n",
5418 linenumber);
5419 continue;
5420 }
5421
5422 errno = 0;
5423 buffer_size = strtol (word, &end, 0);
5424
5425 if (end == word)
5426 {
5427 DBG (3, "sane-init: config file line %d: buffersize must "
5428 "have a parameter; using default (%d kb)\n",
5429 linenumber, new_dev[new_dev_len - 1]->max_buffer_size);
5430 }
5431 if (errno)
5432 {
5433 DBG (3, "sane-init: config file line %d: buffersize `%s' "
5434 "is invalid (%s); using default (%d kb)\n", linenumber,
5435 word, strerror (errno),
5436 new_dev[new_dev_len - 1]->max_buffer_size);
5437 }
5438 else
5439 {
5440 if (new_dev_len > 0)
5441 {
5442 if (buffer_size < 32.0)
5443 buffer_size = 32.0;
5444 new_dev[new_dev_len - 1]->max_buffer_size =
5445 buffer_size * 1024;
5446 DBG (3,
5447 "sane_init: config file line %d: buffersize set "
5448 "to %ld kb for %s\n", linenumber, buffer_size,
5449 new_dev[new_dev_len - 1]->sane.name);
5450 }
5451 else
5452 {
5453 DBG (3, "sane_init: config file line %d: option "
5454 "buffersize ignored, was set before any device "
5455 "name\n", linenumber);
5456 }
5457 }
5458 if (word)
5459 free (word);
5460 word = 0;
5461 }
5462 else if (strcmp (word, "blocksize") == 0)
5463 {
5464 long block_size;
5465
5466 free (word);
5467 word = 0;
5468 cp = sanei_config_get_string (cp, &word);
5469 if (!word)
5470 {
5471 DBG (1,
5472 "sane_init: config file line %d: missing quotation mark?\n",
5473 linenumber);
5474 continue;
5475 }
5476
5477 errno = 0;
5478 block_size = strtol (word, &end, 0);
5479
5480 if (end == word)
5481 {
5482 DBG (3, "sane-init: config file line %d:: blocksize must "
5483 "have a parameter; using default (1 GB)\n",
5484 linenumber);
5485 }
5486 if (errno)
5487 {
5488 DBG (3, "sane-init: config file line %d: blocksize `%s' "
5489 "is invalid (%s); using default (1 GB)\n", linenumber,
5490 word, strerror (errno));
5491 }
5492 else
5493 {
5494 if (new_dev_len > 0)
5495 {
5496 if (block_size < 256.0)
5497 block_size = 256.0;
5498 new_dev[new_dev_len - 1]->max_block_buffer_size =
5499 block_size * 1024;
5500 DBG (3, "sane_init: config file line %d: blocksize set "
5501 "to %ld kb for %s\n", linenumber, block_size,
5502 new_dev[new_dev_len - 1]->sane.name);
5503 }
5504 else
5505 {
5506 DBG (3, "sane_init: config file line %d: option "
5507 "blocksize ignored, was set before any device "
5508 "name\n", linenumber);
5509 }
5510 }
5511 if (word)
5512 free (word);
5513 word = 0;
5514 }
5515 else
5516 {
5517 DBG (3, "sane_init: config file line %d: ignoring unknown "
5518 "option `%s'\n", linenumber, word);
5519 if (word)
5520 free (word);
5521 word = 0;
5522 }
5523 }
5524 else
5525 {
5526 new_dev_len = 0;
5527 DBG (4, "sane_init: config file line %d: trying to attach `%s'\n",
5528 linenumber, line);
5529 sanei_config_attach_matching_devices (line, attach_one_device);
5530 if (word)
5531 free (word);
5532 word = 0;
5533 }
5534 }
5535
5536 if (new_dev_alloced > 0)
5537 {
5538 new_dev_len = new_dev_alloced = 0;
5539 free (new_dev);
5540 }
5541 fclose (fp);
5542 DBG (5, "sane_init: end\n");
5543 return SANE_STATUS_GOOD;
5544 }
5545
5546 void
sane_exit(void)5547 sane_exit (void)
5548 {
5549 Mustek_Device *dev, *next;
5550
5551 DBG (4, "sane_exit\n");
5552 for (dev = first_dev; dev; dev = next)
5553 {
5554 next = dev->next;
5555 free (dev->name);
5556 free (dev);
5557 }
5558 if (devlist)
5559 free (devlist);
5560 devlist = 0;
5561 first_dev = 0;
5562 sanei_ab306_exit (); /* may have to do some cleanup */
5563 mustek_scsi_pp_exit ();
5564 DBG (5, "sane_exit: finished\n");
5565 }
5566
5567 SANE_Status
sane_get_devices(const SANE_Device *** device_list, SANE_Bool local_only)5568 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
5569 {
5570 Mustek_Device *dev;
5571 SANE_Int i;
5572
5573 DBG (4, "sane_get_devices: %d devices %s\n", num_devices,
5574 local_only ? "(local only)" : "");
5575 if (devlist)
5576 free (devlist);
5577
5578 devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
5579 if (!devlist)
5580 return SANE_STATUS_NO_MEM;
5581
5582 i = 0;
5583 for (dev = first_dev; i < num_devices; dev = dev->next)
5584 devlist[i++] = &dev->sane;
5585 devlist[i++] = 0;
5586
5587 *device_list = devlist;
5588 DBG (5, "sane_get_devices: end\n");
5589 return SANE_STATUS_GOOD;
5590 }
5591
5592 SANE_Status
sane_open(SANE_String_Const devicename, SANE_Handle * handle)5593 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
5594 {
5595 Mustek_Device *dev;
5596 SANE_Status status;
5597 Mustek_Scanner *s;
5598
5599 if (!devicename)
5600 {
5601 DBG (1, "sane_open: devicename is null!\n");
5602 return SANE_STATUS_INVAL;
5603 }
5604 if (!handle)
5605 {
5606 DBG (1, "sane_open: handle is null!\n");
5607 return SANE_STATUS_INVAL;
5608 }
5609 DBG (4, "sane_open: devicename=%s\n", devicename);
5610
5611 if (devicename[0])
5612 {
5613 for (dev = first_dev; dev; dev = dev->next)
5614 if (strcmp (dev->sane.name, devicename) == 0)
5615 break;
5616
5617 if (!dev)
5618 {
5619 status = attach (devicename, &dev, SANE_TRUE);
5620 if (status != SANE_STATUS_GOOD)
5621 return status;
5622 }
5623 }
5624 else
5625 /* empty devicname -> use first device */
5626 dev = first_dev;
5627
5628 if (!dev)
5629 return SANE_STATUS_INVAL;
5630
5631 s = malloc (sizeof (*s));
5632 if (!s)
5633 return SANE_STATUS_NO_MEM;
5634 memset (s, 0, sizeof (*s));
5635 s->fd = -1;
5636 s->pipe = -1;
5637 s->hw = dev;
5638 s->ld.ld_line = 0;
5639 s->halftone_pattern = malloc (8 * 8 * sizeof (SANE_Int));
5640 if (!s->halftone_pattern)
5641 return SANE_STATUS_NO_MEM;
5642 init_options (s);
5643
5644 /* insert newly opened handle into list of open handles: */
5645 s->next = first_handle;
5646 first_handle = s;
5647
5648 *handle = s;
5649 DBG (4, "sane_open: finished (handle=%p)\n", (void *) s);
5650 return SANE_STATUS_GOOD;
5651 }
5652
5653 void
sane_close(SANE_Handle handle)5654 sane_close (SANE_Handle handle)
5655 {
5656 Mustek_Scanner *prev, *s;
5657
5658 DBG (4, "sane_close: handle=%p\n", handle);
5659 /* remove handle from list of open handles: */
5660 prev = 0;
5661 for (s = first_handle; s; s = s->next)
5662 {
5663 if (s == handle)
5664 break;
5665 prev = s;
5666 }
5667 if (!s)
5668 {
5669 DBG (1, "sane_close: invalid handle %p\n", handle);
5670 return; /* oops, not a handle we know about */
5671 }
5672
5673 if (s->scanning)
5674 do_stop (handle);
5675
5676 if (s->ld.buf[0])
5677 free (s->ld.buf[0]);
5678 if (s->val[OPT_MODE].s)
5679 free (s->val[OPT_MODE].s);
5680 if (s->val[OPT_BIT_DEPTH].s)
5681 free (s->val[OPT_BIT_DEPTH].s);
5682 if (s->val[OPT_SPEED].s)
5683 free (s->val[OPT_SPEED].s);
5684 if (s->val[OPT_SOURCE].s)
5685 free (s->val[OPT_SOURCE].s);
5686 if (s->val[OPT_HALFTONE_DIMENSION].s)
5687 free (s->val[OPT_HALFTONE_DIMENSION].s);
5688 if (s->halftone_pattern)
5689 free (s->halftone_pattern);
5690 if (prev)
5691 prev->next = s->next;
5692 else
5693 first_handle = s->next;
5694 free (handle);
5695 handle = 0;
5696 DBG (5, "sane_close: finished\n");
5697 }
5698
5699 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle, SANE_Int option)5700 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
5701 {
5702 Mustek_Scanner *s = handle;
5703
5704 if (((unsigned) option >= NUM_OPTIONS) || (option < 0))
5705 {
5706 DBG (4, "sane_get_option_descriptor: option %d >= NUM_OPTIONS or < 0\n",
5707 option);
5708 return 0;
5709 }
5710 if (!s)
5711 {
5712 DBG (1, "sane_get_option_descriptor: handle is null!\n");
5713 return 0;
5714 }
5715 if (s->opt[option].name && s->opt[option].name[0] != 0)
5716 DBG (5, "sane_get_option_descriptor for option %s (%sactive%s)\n",
5717 s->opt[option].name,
5718 s->opt[option].cap & SANE_CAP_INACTIVE ? "in" : "",
5719 s->opt[option].cap & SANE_CAP_ADVANCED ? ", advanced" : "");
5720 else
5721 DBG (5, "sane_get_option_descriptor for option \"%s\" (%sactive%s)\n",
5722 s->opt[option].title,
5723 s->opt[option].cap & SANE_CAP_INACTIVE ? "in" : "",
5724 s->opt[option].cap & SANE_CAP_ADVANCED ? ", advanced" : "");
5725 return s->opt + option;
5726 }
5727
5728 SANE_Status
sane_control_option(SANE_Handle handle, SANE_Int option, SANE_Action action, void *val, SANE_Int * info)5729 sane_control_option (SANE_Handle handle, SANE_Int option,
5730 SANE_Action action, void *val, SANE_Int * info)
5731 {
5732 Mustek_Scanner *s = handle;
5733 SANE_Status status;
5734 SANE_Word w, cap;
5735
5736 if (((unsigned) option >= NUM_OPTIONS) || (option < 0))
5737 {
5738 DBG (4, "sane_control_option: option %d < 0 or >= NUM_OPTIONS\n",
5739 option);
5740 return SANE_STATUS_INVAL;
5741 }
5742 if (!s)
5743 {
5744 DBG (1, "sane_control_option: handle is null!\n");
5745 return SANE_STATUS_INVAL;
5746 }
5747
5748 if (s->opt[option].type != SANE_TYPE_BUTTON && !val)
5749 {
5750 DBG (1, "sane_control_option: val is null!\n");
5751 return SANE_STATUS_INVAL;
5752 }
5753
5754 if (s->opt[option].name && s->opt[option].name[0] != 0)
5755 DBG (5, "sane_control_option (%s option %s)\n",
5756 action == SANE_ACTION_GET_VALUE ? "get" :
5757 (action == SANE_ACTION_SET_VALUE ? "set" : "unknown action with"),
5758 s->opt[option].name);
5759 else
5760 DBG (5, "sane_control_option (%s option \"%s\")\n",
5761 action == SANE_ACTION_GET_VALUE ? "get" :
5762 (action == SANE_ACTION_SET_VALUE ? "set" : "unknown action with"),
5763 s->opt[option].title);
5764
5765 if (info)
5766 *info = 0;
5767
5768 if (s->scanning)
5769 {
5770 DBG (4, "sane_control_option: don't use while scanning (option %s)\n",
5771 s->opt[option].name);
5772 return SANE_STATUS_DEVICE_BUSY;
5773 }
5774
5775 cap = s->opt[option].cap;
5776
5777 if (!SANE_OPTION_IS_ACTIVE (cap))
5778 {
5779 DBG (4, "sane_control_option: option %s is inactive\n",
5780 s->opt[option].name);
5781 return SANE_STATUS_INVAL;
5782 }
5783
5784 if (action == SANE_ACTION_GET_VALUE)
5785 {
5786 switch (option)
5787 {
5788 /* word options: */
5789 case OPT_PREVIEW:
5790 case OPT_FAST_PREVIEW:
5791 case OPT_RESOLUTION:
5792 case OPT_FAST_GRAY_MODE:
5793 case OPT_TL_X:
5794 case OPT_TL_Y:
5795 case OPT_BR_X:
5796 case OPT_BR_Y:
5797 case OPT_NUM_OPTS:
5798 case OPT_BRIGHTNESS:
5799 case OPT_BRIGHTNESS_R:
5800 case OPT_BRIGHTNESS_G:
5801 case OPT_BRIGHTNESS_B:
5802 case OPT_CONTRAST:
5803 case OPT_CONTRAST_R:
5804 case OPT_CONTRAST_G:
5805 case OPT_CONTRAST_B:
5806 case OPT_CUSTOM_GAMMA:
5807 case OPT_QUALITY_CAL:
5808 case OPT_LAMP_OFF_TIME:
5809 *(SANE_Word *) val = s->val[option].w;
5810 return SANE_STATUS_GOOD;
5811
5812 /* word-array options: */
5813 case OPT_GAMMA_VECTOR:
5814 case OPT_GAMMA_VECTOR_R:
5815 case OPT_GAMMA_VECTOR_G:
5816 case OPT_GAMMA_VECTOR_B:
5817 case OPT_HALFTONE_PATTERN:
5818 memcpy (val, s->val[option].wa, s->opt[option].size);
5819 return SANE_STATUS_GOOD;
5820
5821 /* string options: */
5822 case OPT_SPEED:
5823 case OPT_SOURCE:
5824 case OPT_MODE:
5825 case OPT_BIT_DEPTH:
5826 case OPT_HALFTONE_DIMENSION:
5827 strcpy (val, s->val[option].s);
5828 return SANE_STATUS_GOOD;
5829 }
5830 }
5831 else if (action == SANE_ACTION_SET_VALUE)
5832 {
5833 if (!SANE_OPTION_IS_SETTABLE (cap))
5834 {
5835 DBG (4, "sane_control_option: option %s is not setable\n",
5836 s->opt[option].name);
5837 return SANE_STATUS_INVAL;
5838 }
5839
5840 status = constrain_value (s, option, val, info);
5841 if (status != SANE_STATUS_GOOD)
5842 {
5843 DBG (4, "sane_control_option: constrain_value error (option %s)\n",
5844 s->opt[option].name);
5845 return status;
5846 }
5847
5848 switch (option)
5849 {
5850 case OPT_LAMP_OFF_BUTTON:
5851 {
5852 SANE_Int old_time = lamp_off_time;
5853 SANE_Status status;
5854
5855 status = dev_open (s->hw->sane.name, s, sense_handler);
5856 if (status != SANE_STATUS_GOOD)
5857 return status;
5858 lamp_off_time = 0;
5859 set_window_pro (s);
5860 lamp_off_time = old_time;
5861 dev_close (s);
5862 return SANE_STATUS_GOOD;
5863 }
5864 /* (mostly) side-effect-free word options: */
5865 case OPT_RESOLUTION:
5866 case OPT_TL_X:
5867 case OPT_BR_X:
5868 case OPT_TL_Y:
5869 case OPT_BR_Y:
5870 if (info)
5871 *info |= SANE_INFO_RELOAD_PARAMS;
5872 /* fall through */
5873 case OPT_PREVIEW:
5874 case OPT_FAST_PREVIEW:
5875 case OPT_FAST_GRAY_MODE:
5876 case OPT_BRIGHTNESS:
5877 case OPT_BRIGHTNESS_R:
5878 case OPT_BRIGHTNESS_G:
5879 case OPT_BRIGHTNESS_B:
5880 case OPT_CONTRAST:
5881 case OPT_CONTRAST_R:
5882 case OPT_CONTRAST_G:
5883 case OPT_CONTRAST_B:
5884 case OPT_QUALITY_CAL:
5885 case OPT_LAMP_OFF_TIME:
5886 s->val[option].w = *(SANE_Word *) val;
5887 return SANE_STATUS_GOOD;
5888
5889 /* side-effect-free word-array options: */
5890 case OPT_HALFTONE_PATTERN:
5891 case OPT_GAMMA_VECTOR:
5892 case OPT_GAMMA_VECTOR_R:
5893 case OPT_GAMMA_VECTOR_G:
5894 case OPT_GAMMA_VECTOR_B:
5895 memcpy (s->val[option].wa, val, s->opt[option].size);
5896 return SANE_STATUS_GOOD;
5897
5898 /* side-effect-free single-string options: */
5899 case OPT_SPEED:
5900 if (s->val[option].s)
5901 free (s->val[option].s);
5902 s->val[option].s = strdup (val);
5903 if (!s->val[option].s)
5904 return SANE_STATUS_NO_MEM;
5905
5906 return SANE_STATUS_GOOD;
5907
5908 /* side-effect-free string list options: */
5909 case OPT_BIT_DEPTH:
5910 {
5911 SANE_Char *old_val = s->val[option].s;
5912
5913 if (old_val)
5914 {
5915 if (strcmp (old_val, val) == 0)
5916 return SANE_STATUS_GOOD; /* no change */
5917 free (old_val);
5918 }
5919 s->val[option].s = strdup (val);
5920 if (!s->val[option].s)
5921 return SANE_STATUS_NO_MEM;
5922 return SANE_STATUS_GOOD;
5923 }
5924
5925 /* options with side-effects: */
5926 case OPT_CUSTOM_GAMMA:
5927 w = *(SANE_Word *) val;
5928
5929 if (w == s->val[OPT_CUSTOM_GAMMA].w)
5930 return SANE_STATUS_GOOD; /* no change */
5931
5932 if (info)
5933 *info |= SANE_INFO_RELOAD_OPTIONS;
5934
5935 s->val[OPT_CUSTOM_GAMMA].w = w;
5936 if (w)
5937 {
5938 SANE_String_Const mode = s->val[OPT_MODE].s;
5939
5940 if (strcmp (mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
5941 s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
5942 else if (strcmp (mode, SANE_VALUE_SCAN_MODE_COLOR) == 0)
5943 {
5944 s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
5945 s->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
5946 s->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
5947 s->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
5948 }
5949 else if ((strcmp (mode, SANE_VALUE_SCAN_MODE_LINEART) == 0)
5950 && (s->hw->flags & MUSTEK_FLAG_PRO))
5951 {
5952 s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
5953 }
5954 }
5955 else
5956 {
5957 s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
5958 s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
5959 s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
5960 s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
5961 }
5962 return SANE_STATUS_GOOD;
5963
5964 case OPT_MODE:
5965 {
5966 SANE_Char *old_val = s->val[option].s;
5967 SANE_Int halftoning, binary;
5968
5969 if (old_val)
5970 {
5971 if (strcmp (old_val, val) == 0)
5972 return SANE_STATUS_GOOD; /* no change */
5973 free (old_val);
5974 }
5975 if (info)
5976 *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
5977
5978 s->val[option].s = strdup (val);
5979 if (!s->val[option].s)
5980 return SANE_STATUS_NO_MEM;
5981
5982 s->opt[OPT_BRIGHTNESS].cap |= SANE_CAP_INACTIVE;
5983 s->opt[OPT_BRIGHTNESS_R].cap |= SANE_CAP_INACTIVE;
5984 s->opt[OPT_BRIGHTNESS_G].cap |= SANE_CAP_INACTIVE;
5985 s->opt[OPT_BRIGHTNESS_B].cap |= SANE_CAP_INACTIVE;
5986 s->opt[OPT_CONTRAST].cap |= SANE_CAP_INACTIVE;
5987 s->opt[OPT_CONTRAST_R].cap |= SANE_CAP_INACTIVE;
5988 s->opt[OPT_CONTRAST_G].cap |= SANE_CAP_INACTIVE;
5989 s->opt[OPT_CONTRAST_B].cap |= SANE_CAP_INACTIVE;
5990 s->opt[OPT_CUSTOM_GAMMA].cap |= SANE_CAP_INACTIVE;
5991 s->opt[OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE;
5992 s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
5993 s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
5994 s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
5995 s->opt[OPT_HALFTONE_DIMENSION].cap |= SANE_CAP_INACTIVE;
5996 s->opt[OPT_HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE;
5997
5998 halftoning = strcmp (val, SANE_VALUE_SCAN_MODE_HALFTONE) == 0;
5999 binary = (halftoning || strcmp (val, SANE_VALUE_SCAN_MODE_LINEART) == 0);
6000
6001 if (binary)
6002 {
6003 /* enable brightness/contrast for when in a binary mode */
6004 s->opt[OPT_BRIGHTNESS].cap &= ~SANE_CAP_INACTIVE;
6005 /* The SE and paragon models support only threshold
6006 in lineart */
6007 if (!(s->hw->flags & MUSTEK_FLAG_SE)
6008 && !(s->hw->flags & MUSTEK_FLAG_PRO))
6009 s->opt[OPT_CONTRAST].cap &= ~SANE_CAP_INACTIVE;
6010
6011 if (halftoning)
6012 {
6013 s->opt[OPT_HALFTONE_DIMENSION].cap &= ~SANE_CAP_INACTIVE;
6014 encode_halftone (s);
6015 if (s->custom_halftone_pattern)
6016 {
6017 s->opt[OPT_HALFTONE_PATTERN].cap
6018 &= ~SANE_CAP_INACTIVE;
6019 }
6020 else
6021 {
6022 s->opt[OPT_HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE;
6023 }
6024 }
6025 }
6026 else
6027 {
6028 s->opt[OPT_CUSTOM_GAMMA].cap &= ~SANE_CAP_INACTIVE;
6029 }
6030
6031 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
6032 {
6033 if (strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR) == 0)
6034 {
6035 s->opt[OPT_BRIGHTNESS_R].cap &= ~SANE_CAP_INACTIVE;
6036 s->opt[OPT_BRIGHTNESS_G].cap &= ~SANE_CAP_INACTIVE;
6037 s->opt[OPT_BRIGHTNESS_B].cap &= ~SANE_CAP_INACTIVE;
6038 s->opt[OPT_CONTRAST_R].cap &= ~SANE_CAP_INACTIVE;
6039 s->opt[OPT_CONTRAST_G].cap &= ~SANE_CAP_INACTIVE;
6040 s->opt[OPT_CONTRAST_B].cap &= ~SANE_CAP_INACTIVE;
6041 }
6042 else
6043 {
6044 s->opt[OPT_BRIGHTNESS].cap &= ~SANE_CAP_INACTIVE;
6045 s->opt[OPT_CONTRAST].cap &= ~SANE_CAP_INACTIVE;
6046 }
6047 }
6048 else if (s->hw->flags & MUSTEK_FLAG_PRO)
6049 {
6050 if (strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_GRAY) == 0)
6051 s->opt[OPT_FAST_GRAY_MODE].cap &= ~SANE_CAP_INACTIVE;
6052 else
6053 s->opt[OPT_FAST_GRAY_MODE].cap |= SANE_CAP_INACTIVE;
6054 if (strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR) == 0)
6055 s->opt[OPT_BIT_DEPTH].cap &= ~SANE_CAP_INACTIVE;
6056 else
6057 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
6058 }
6059 else if (s->hw->flags & MUSTEK_FLAG_SE_PLUS)
6060 {
6061 if (strcmp (s->val[OPT_MODE].s, SANE_VALUE_SCAN_MODE_COLOR) == 0)
6062 s->opt[OPT_BIT_DEPTH].cap &= ~SANE_CAP_INACTIVE;
6063 else
6064 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
6065 }
6066
6067 if (s->val[OPT_CUSTOM_GAMMA].w)
6068 {
6069 if (strcmp (val, SANE_VALUE_SCAN_MODE_GRAY) == 0)
6070 s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
6071 else if (strcmp (val, SANE_VALUE_SCAN_MODE_COLOR) == 0)
6072 {
6073 s->opt[OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE;
6074 s->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
6075 s->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
6076 s->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
6077 }
6078 }
6079 return SANE_STATUS_GOOD;
6080 }
6081
6082 case OPT_HALFTONE_DIMENSION:
6083 /* halftone pattern dimension affects halftone pattern option: */
6084 {
6085 if (strcmp (s->val[option].s, (SANE_String) val) == 0)
6086 return SANE_STATUS_GOOD; /* no change */
6087
6088 if (info)
6089 *info |= SANE_INFO_RELOAD_OPTIONS;
6090
6091 s->val[option].s = strdup (val);
6092 if (!s->val[option].s)
6093 return SANE_STATUS_NO_MEM;
6094 encode_halftone (s);
6095 s->opt[OPT_HALFTONE_PATTERN].cap |= SANE_CAP_INACTIVE;
6096 if (s->custom_halftone_pattern)
6097 {
6098 s->opt[OPT_HALFTONE_PATTERN].cap &= ~SANE_CAP_INACTIVE;
6099 /* BUG: The SANE standard does nor allow to change the option
6100 size at run time */
6101 s->opt[OPT_HALFTONE_PATTERN].size =
6102 (s->halftone_pattern_type & 0x0f) * sizeof (SANE_Word);
6103 }
6104 return SANE_STATUS_GOOD;
6105 }
6106
6107 case OPT_SOURCE:
6108 if (info)
6109 *info |= SANE_INFO_RELOAD_OPTIONS;
6110 if (s->val[option].s)
6111 free (s->val[option].s);
6112 s->val[option].s = strdup (val);
6113 if (!s->val[option].s)
6114 return SANE_STATUS_NO_MEM;
6115
6116 if (strcmp (val, "Transparency Adapter") == 0)
6117 {
6118 s->opt[OPT_TL_X].constraint.range = &s->hw->x_trans_range;
6119 s->opt[OPT_TL_Y].constraint.range = &s->hw->y_trans_range;
6120 s->opt[OPT_BR_X].constraint.range = &s->hw->x_trans_range;
6121 s->opt[OPT_BR_Y].constraint.range = &s->hw->y_trans_range;
6122 }
6123 else
6124 {
6125 s->opt[OPT_TL_X].constraint.range = &s->hw->x_range;
6126 s->opt[OPT_TL_Y].constraint.range = &s->hw->y_range;
6127 s->opt[OPT_BR_X].constraint.range = &s->hw->x_range;
6128 s->opt[OPT_BR_Y].constraint.range = &s->hw->y_range;
6129 }
6130 return SANE_STATUS_GOOD;
6131 }
6132 }
6133 DBG (4, "sane_control_option: unknown action for option %s\n",
6134 s->opt[option].name);
6135 return SANE_STATUS_INVAL;
6136 }
6137
6138 SANE_Status
sane_get_parameters(SANE_Handle handle, SANE_Parameters * params)6139 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
6140 {
6141 Mustek_Scanner *s = handle;
6142 SANE_String_Const mode;
6143
6144 if (!s)
6145 {
6146 DBG (1, "sane_get_parameters: handle is null!\n");
6147 return SANE_STATUS_INVAL;
6148 }
6149
6150 if (!s->scanning)
6151 {
6152 double width, height, dpi;
6153
6154 memset (&s->params, 0, sizeof (s->params));
6155
6156 width = SANE_UNFIX (s->val[OPT_BR_X].w - s->val[OPT_TL_X].w);
6157 height = SANE_UNFIX (s->val[OPT_BR_Y].w - s->val[OPT_TL_Y].w);
6158 dpi = SANE_UNFIX (s->val[OPT_RESOLUTION].w);
6159
6160 /* make best-effort guess at what parameters will look like once
6161 scanning starts. */
6162 if (dpi > 0.0 && width > 0.0 && height > 0.0)
6163 {
6164 double dots_per_mm = dpi / MM_PER_INCH;
6165
6166 s->params.pixels_per_line = width * dots_per_mm;
6167 s->params.lines = height * dots_per_mm;
6168 }
6169 encode_halftone (s);
6170 mode = s->val[OPT_MODE].s;
6171 if (strcmp (mode, SANE_VALUE_SCAN_MODE_LINEART) == 0 || strcmp (mode, SANE_VALUE_SCAN_MODE_HALFTONE) == 0)
6172 {
6173 s->params.format = SANE_FRAME_GRAY;
6174 s->params.bytes_per_line = (s->params.pixels_per_line + 7) / 8;
6175 s->params.depth = 1;
6176 }
6177 else if (strcmp (mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
6178 {
6179 s->params.format = SANE_FRAME_GRAY;
6180 s->params.bytes_per_line = s->params.pixels_per_line;
6181 s->params.depth = 8;
6182 }
6183 else
6184 {
6185 /* it's one of the color modes... */
6186
6187 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
6188 {
6189 s->params.format = SANE_FRAME_RED + s->pass;
6190 s->params.bytes_per_line = s->params.pixels_per_line;
6191 s->params.depth = 8;
6192 }
6193 else /* 1-pass */
6194 {
6195 if (strcmp (s->val[OPT_BIT_DEPTH].s, "12") == 0)
6196 {
6197 s->params.bytes_per_line = 6 * s->params.pixels_per_line;
6198 s->params.depth = 16;
6199 }
6200 else
6201 {
6202 s->params.bytes_per_line = 3 * s->params.pixels_per_line;
6203 s->params.depth = 8;
6204 }
6205 s->params.format = SANE_FRAME_RGB;
6206 }
6207 }
6208 }
6209 else if ((s->mode & MUSTEK_MODE_COLOR)
6210 && (s->hw->flags & MUSTEK_FLAG_THREE_PASS))
6211 s->params.format = SANE_FRAME_RED + s->pass;
6212 s->params.last_frame = (s->params.format != SANE_FRAME_RED
6213 && s->params.format != SANE_FRAME_GREEN);
6214 if (params)
6215 *params = s->params;
6216 DBG (4, "sane_get_parameters: frame = %d; last_frame = %s; depth = %d\n",
6217 s->params.format, s->params.last_frame ? "true" : "false",
6218 s->params.depth);
6219 DBG (4, "sane_get_parameters: lines = %d; ppl = %d; bpl = %d\n",
6220 s->params.lines, s->params.pixels_per_line, s->params.bytes_per_line);
6221
6222 return SANE_STATUS_GOOD;
6223 }
6224
6225 SANE_Status
sane_start(SANE_Handle handle)6226 sane_start (SANE_Handle handle)
6227 {
6228 Mustek_Scanner *s = handle;
6229 SANE_Status status;
6230 int fds[2];
6231 struct SIGACTION act;
6232
6233 if (!s)
6234 {
6235 DBG (1, "sane_start: handle is null!\n");
6236 return SANE_STATUS_INVAL;
6237 }
6238
6239 DBG (4, "sane_start\n");
6240 /* First make sure we have a current parameter set. Some of the
6241 parameters will be overwritten below, but that's OK. */
6242 status = sane_get_parameters (s, 0);
6243 if (status != SANE_STATUS_GOOD)
6244 return status;
6245
6246 /* Check for inconsistencies */
6247
6248 if (s->val[OPT_TL_X].w > s->val[OPT_BR_X].w)
6249 {
6250 DBG (0, "sane_start: %s (%.1f mm) is bigger than %s (%.1f mm) "
6251 "-- aborting\n",
6252 s->opt[OPT_TL_X].title, SANE_UNFIX (s->val[OPT_TL_X].w),
6253 s->opt[OPT_BR_X].title, SANE_UNFIX (s->val[OPT_BR_X].w));
6254 return SANE_STATUS_INVAL;
6255 }
6256 if (s->val[OPT_TL_Y].w > s->val[OPT_BR_Y].w)
6257 {
6258 DBG (0, "sane_start: %s (%.1f mm) is bigger than %s (%.1f mm) "
6259 "-- aborting\n",
6260 s->opt[OPT_TL_Y].title, SANE_UNFIX (s->val[OPT_TL_Y].w),
6261 s->opt[OPT_BR_Y].title, SANE_UNFIX (s->val[OPT_BR_Y].w));
6262 return SANE_STATUS_INVAL;
6263 }
6264
6265 s->total_bytes = 0;
6266
6267 if (s->fd < 0)
6268 {
6269 /* this is the first (and maybe only) pass... */
6270 SANE_String_Const mode;
6271 struct timeval start;
6272
6273 /* save start time */
6274 gettimeofday (&start, 0);
6275 s->start_time = start.tv_sec;
6276 /* translate options into s->mode for convenient access: */
6277 mode = s->val[OPT_MODE].s;
6278 if (strcmp (mode, SANE_VALUE_SCAN_MODE_LINEART) == 0)
6279 s->mode = MUSTEK_MODE_LINEART;
6280 else if (strcmp (mode, SANE_VALUE_SCAN_MODE_HALFTONE) == 0)
6281 s->mode = MUSTEK_MODE_HALFTONE;
6282 else if (strcmp (mode, SANE_VALUE_SCAN_MODE_GRAY) == 0)
6283 s->mode = MUSTEK_MODE_GRAY;
6284 else if (strcmp (mode, SANE_VALUE_SCAN_MODE_COLOR) == 0)
6285 s->mode = MUSTEK_MODE_COLOR;
6286
6287 /* scanner dependent specials */
6288 s->one_pass_color_scan = SANE_FALSE;
6289 if ((s->mode & MUSTEK_MODE_COLOR)
6290 && !(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
6291 {
6292 s->one_pass_color_scan = SANE_TRUE;
6293 }
6294
6295 s->resolution_code = encode_resolution (s);
6296
6297 if (s->val[OPT_PREVIEW].w && s->val[OPT_FAST_PREVIEW].w)
6298 {
6299 if (s->hw->flags & MUSTEK_FLAG_THREE_PASS)
6300 {
6301 if (s->mode & MUSTEK_MODE_COLOR)
6302 {
6303 /* Force gray-scale mode when previewing. */
6304 s->mode = MUSTEK_MODE_GRAY;
6305 s->params.format = SANE_FRAME_GRAY;
6306 s->params.bytes_per_line = s->params.pixels_per_line;
6307 s->params.last_frame = SANE_TRUE;
6308 }
6309 }
6310 else if (s->hw->flags & MUSTEK_FLAG_SE)
6311 {
6312 /* use 36 dpi color in any case */
6313 s->mode = MUSTEK_MODE_COLOR;
6314 s->params.format = SANE_FRAME_RGB;
6315 s->params.depth = 8;
6316 s->one_pass_color_scan = SANE_TRUE;
6317 s->resolution_code = 36;
6318 }
6319 else if (s->hw->flags & MUSTEK_FLAG_PARAGON_1)
6320 {
6321 /* use 36 dpi */
6322 s->resolution_code = 36;
6323 }
6324 else if (s->hw->flags & MUSTEK_FLAG_PRO)
6325 {
6326 /* use 30 dpi color mode */
6327 s->mode = MUSTEK_MODE_COLOR;
6328 s->params.format = SANE_FRAME_RGB;
6329 s->params.depth = 8;
6330 s->one_pass_color_scan = SANE_TRUE;
6331 s->resolution_code = 30;
6332 }
6333 DBG (4, "sane_start: use fast preview (res=%d dpi)\n",
6334 s->resolution_code);
6335 }
6336
6337 status = dev_open (s->hw->sane.name, s, sense_handler);
6338 if (status != SANE_STATUS_GOOD)
6339 return status;
6340 }
6341
6342 status = dev_wait_ready (s);
6343 if (status != SANE_STATUS_GOOD)
6344 {
6345 DBG (1, "sane_start: wait_ready() failed: %s\n",
6346 sane_strstatus (status));
6347 goto stop_scanner_and_return;
6348 }
6349
6350 if (!(s->hw->flags & MUSTEK_FLAG_SCSI_PP))
6351 {
6352 /* SCSI-over-parallel port doesn't seem to like being inquired here */
6353 status = inquiry (s);
6354 if (status != SANE_STATUS_GOOD)
6355 {
6356 DBG (1, "sane_start: inquiry command failed: %s\n",
6357 sane_strstatus (status));
6358 goto stop_scanner_and_return;
6359 }
6360 }
6361
6362 if ((strcmp (s->val[OPT_SOURCE].s, "Automatic Document Feeder") == 0) &&
6363 !(s->hw->flags & MUSTEK_FLAG_ADF_READY))
6364 {
6365 DBG (2, "sane_start: automatic document feeder is out of documents\n");
6366 status = SANE_STATUS_NO_DOCS;
6367 goto stop_scanner_and_return;
6368 }
6369
6370 if (s->hw->flags & MUSTEK_FLAG_SE)
6371 {
6372 status = set_window_se (s, 0);
6373 if (status != SANE_STATUS_GOOD)
6374 {
6375 DBG (1, "sane_start: set window command failed: %s\n",
6376 sane_strstatus (status));
6377 goto stop_scanner_and_return;
6378 }
6379
6380 s->scanning = SANE_TRUE;
6381 s->cancelled = SANE_FALSE;
6382
6383 dev_wait_ready (s);
6384
6385 status = get_window (s, &s->params.bytes_per_line,
6386 &s->params.lines, &s->params.pixels_per_line);
6387 if (status != SANE_STATUS_GOOD)
6388 {
6389 DBG (1, "sane_start: get window command failed: %s\n",
6390 sane_strstatus (status));
6391 goto stop_scanner_and_return;
6392 }
6393
6394 status = calibration_se (s);
6395 if (status != SANE_STATUS_GOOD)
6396 goto stop_scanner_and_return;
6397
6398 status = start_scan (s);
6399 if (status != SANE_STATUS_GOOD)
6400 goto stop_scanner_and_return;
6401
6402 status = send_gamma_table_se (s);
6403 if (status != SANE_STATUS_GOOD)
6404 goto stop_scanner_and_return;
6405 }
6406
6407 else if (s->hw->flags & MUSTEK_FLAG_PRO)
6408 {
6409
6410 status = dev_wait_ready (s);
6411 if (status != SANE_STATUS_GOOD)
6412 goto stop_scanner_and_return;
6413
6414 status = set_window_pro (s);
6415 if (status != SANE_STATUS_GOOD)
6416 goto stop_scanner_and_return;
6417
6418 s->scanning = SANE_TRUE;
6419 s->cancelled = SANE_FALSE;
6420
6421 status = adf_and_backtrack (s);
6422 if (status != SANE_STATUS_GOOD)
6423 goto stop_scanner_and_return;
6424
6425 status = mode_select_pro (s);
6426 if (status != SANE_STATUS_GOOD)
6427 goto stop_scanner_and_return;
6428
6429 status = scsi_sense_wait_ready (s);
6430 if (status != SANE_STATUS_GOOD)
6431 goto stop_scanner_and_return;
6432
6433 status = calibration_pro (s);
6434 if (status != SANE_STATUS_GOOD)
6435 goto stop_scanner_and_return;
6436
6437 status = send_gamma_table (s);
6438 if (status != SANE_STATUS_GOOD)
6439 goto stop_scanner_and_return;
6440
6441 status = start_scan (s);
6442 if (status != SANE_STATUS_GOOD)
6443 goto stop_scanner_and_return;
6444
6445 status = get_image_status (s, &s->params.bytes_per_line,
6446 &s->params.lines);
6447 if (status != SANE_STATUS_GOOD)
6448 goto stop_scanner_and_return;
6449
6450 status = scsi_sense_wait_ready (s);
6451 if (status != SANE_STATUS_GOOD)
6452 goto stop_scanner_and_return;
6453 }
6454
6455 else /* Paragon series */
6456 {
6457 status = area_and_windows (s);
6458 if (status != SANE_STATUS_GOOD)
6459 {
6460 DBG (1, "sane_start: set scan area command failed: %s\n",
6461 sane_strstatus (status));
6462 goto stop_scanner_and_return;
6463 }
6464
6465 status = adf_and_backtrack (s);
6466 if (status != SANE_STATUS_GOOD)
6467 goto stop_scanner_and_return;
6468
6469 if (s->one_pass_color_scan)
6470 {
6471 status = mode_select_paragon (s, MUSTEK_CODE_RED);
6472 if (status != SANE_STATUS_GOOD)
6473 goto stop_scanner_and_return;
6474
6475 status = mode_select_paragon (s, MUSTEK_CODE_GREEN);
6476 if (status != SANE_STATUS_GOOD)
6477 goto stop_scanner_and_return;
6478
6479 status = mode_select_paragon (s, MUSTEK_CODE_BLUE);
6480 }
6481 else
6482 status = mode_select_paragon (s, MUSTEK_CODE_GRAY);
6483
6484 if (status != SANE_STATUS_GOOD)
6485 goto stop_scanner_and_return;
6486
6487 s->scanning = SANE_TRUE;
6488 s->cancelled = SANE_FALSE;
6489
6490 status = send_gamma_table (s);
6491 if (status != SANE_STATUS_GOOD)
6492 goto stop_scanner_and_return;
6493
6494 status = start_scan (s);
6495 if (status != SANE_STATUS_GOOD)
6496 goto stop_scanner_and_return;
6497
6498 if (!(s->hw->flags & MUSTEK_FLAG_SCSI_PP))
6499 {
6500 /* This second gamma table download upsets the SCSI-over-parallel models */
6501 status = send_gamma_table (s);
6502 if (status != SANE_STATUS_GOOD)
6503 goto stop_scanner_and_return;
6504 }
6505
6506 s->ld.max_value = 0;
6507 if (!(s->hw->flags & MUSTEK_FLAG_THREE_PASS))
6508 {
6509 status = line_distance (s);
6510 if (status != SANE_STATUS_GOOD)
6511 goto stop_scanner_and_return;
6512 }
6513
6514 status = get_image_status (s, &s->params.bytes_per_line,
6515 &s->params.lines);
6516 if (status != SANE_STATUS_GOOD)
6517 goto stop_scanner_and_return;
6518
6519 if ((strcmp (s->val[OPT_SOURCE].s, "Automatic Document Feeder") == 0)
6520 && (s->hw->flags & MUSTEK_FLAG_PARAGON_2))
6521 {
6522 status = paragon_2_get_adf_status (s);
6523 if (status != SANE_STATUS_GOOD)
6524 goto stop_scanner_and_return;
6525 }
6526 }
6527
6528 s->params.pixels_per_line = s->params.bytes_per_line;
6529 if (s->one_pass_color_scan)
6530 {
6531 if (strcmp (s->val[OPT_BIT_DEPTH].s, "12") == 0)
6532 s->params.pixels_per_line /= 6;
6533 else
6534 s->params.pixels_per_line /= 3;
6535 }
6536 else if ((s->mode & MUSTEK_MODE_LINEART)
6537 || (s->mode & MUSTEK_MODE_HALFTONE))
6538 s->params.pixels_per_line *= 8;
6539
6540 s->line = 0;
6541
6542 /* don't call any SIGTERM or SIGCHLD handlers
6543 this is to stop xsane and other frontends from calling
6544 its quit handlers */
6545 memset (&act, 0, sizeof (act));
6546 sigaction (SIGTERM, &act, 0);
6547 sigaction (SIGCHLD, &act, 0);
6548
6549 if (pipe (fds) < 0)
6550 return SANE_STATUS_IO_ERROR;
6551
6552 s->reader_fds = fds[1];
6553
6554 /* create reader routine as new process or thread */
6555 s->reader_pid = sanei_thread_begin (reader_process, (void *) s);
6556
6557 if (!sanei_thread_is_valid (s->reader_pid))
6558 {
6559 DBG (1, "sane_start: sanei_thread_begin failed (%s)\n",
6560 strerror (errno));
6561 return SANE_STATUS_NO_MEM;
6562 }
6563
6564 if (sanei_thread_is_forked ())
6565 {
6566 close (s->reader_fds);
6567 s->reader_fds = -1;
6568 }
6569
6570 s->pipe = fds[0];
6571
6572 return SANE_STATUS_GOOD;
6573
6574 stop_scanner_and_return:
6575 do_stop (s);
6576 return status;
6577 }
6578
6579 SANE_Status
sane_read(SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len, SANE_Int * len)6580 sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
6581 SANE_Int * len)
6582 {
6583 Mustek_Scanner *s = handle;
6584 SANE_Status status;
6585 ssize_t nread;
6586
6587
6588 if (!s)
6589 {
6590 DBG (1, "sane_read: handle is null!\n");
6591 return SANE_STATUS_INVAL;
6592 }
6593
6594 if (!buf)
6595 {
6596 DBG (1, "sane_read: buf is null!\n");
6597 return SANE_STATUS_INVAL;
6598 }
6599
6600 if (!len)
6601 {
6602 DBG (1, "sane_read: len is null!\n");
6603 return SANE_STATUS_INVAL;
6604 }
6605
6606 DBG (5, "sane_read\n");
6607 *len = 0;
6608
6609 if (s->cancelled)
6610 {
6611 DBG (4, "sane_read: scan was cancelled\n");
6612 return SANE_STATUS_CANCELLED;
6613 }
6614
6615 if (!s->scanning)
6616 {
6617 DBG (3, "sane_read: must call sane_start before sane_read\n");
6618 return SANE_STATUS_INVAL;
6619 }
6620
6621 while (*len < max_len)
6622 {
6623 nread = read (s->pipe, buf + *len, max_len - *len);
6624
6625 if (s->cancelled)
6626 {
6627 DBG (4, "sane_read: scan was cancelled\n");
6628 *len = 0;
6629 return SANE_STATUS_CANCELLED;
6630 }
6631
6632 if (nread < 0)
6633 {
6634 if (errno == EAGAIN)
6635 {
6636 if (*len == 0)
6637 DBG (5, "sane_read: no more data at the moment--try again\n");
6638 else
6639 DBG (5, "sane_read: read buffer of %d bytes "
6640 "(%d bytes total)\n", *len, s->total_bytes);
6641 return SANE_STATUS_GOOD;
6642 }
6643 else
6644 {
6645 DBG (1, "sane_read: IO error\n");
6646 do_stop (s);
6647 *len = 0;
6648 return SANE_STATUS_IO_ERROR;
6649 }
6650 }
6651
6652 *len += nread;
6653 s->total_bytes += nread;
6654
6655 if (nread == 0)
6656 {
6657 if (*len == 0)
6658 {
6659 if (!(s->hw->flags & MUSTEK_FLAG_THREE_PASS)
6660 || !(s->mode & MUSTEK_MODE_COLOR) || ++s->pass >= 3)
6661 {
6662 DBG (5, "sane_read: pipe was closed ... calling do_stop\n");
6663 status = do_stop (s);
6664 if (status != SANE_STATUS_CANCELLED
6665 && status != SANE_STATUS_GOOD)
6666 return status; /* something went wrong */
6667 }
6668 else /* 3pass color first or second pass */
6669 {
6670 DBG (5,
6671 "sane_read: pipe was closed ... finishing pass %d\n",
6672 s->pass);
6673 }
6674
6675 return do_eof (s);
6676 }
6677 else
6678 {
6679 DBG (5, "sane_read: read last buffer of %d bytes "
6680 "(%d bytes total)\n", *len, s->total_bytes);
6681 return SANE_STATUS_GOOD;
6682 }
6683 }
6684 }
6685 DBG (5, "sane_read: read full buffer of %d bytes (%d total bytes)\n",
6686 *len, s->total_bytes);
6687 return SANE_STATUS_GOOD;
6688 }
6689
6690 void
sane_cancel(SANE_Handle handle)6691 sane_cancel (SANE_Handle handle)
6692 {
6693 Mustek_Scanner *s = handle;
6694
6695 if (!s)
6696 {
6697 DBG (1, "sane_cancel: handle is null!\n");
6698 return;
6699 }
6700
6701 DBG (4, "sane_cancel\n");
6702 if (s->scanning)
6703 {
6704 s->cancelled = SANE_TRUE;
6705 do_stop (handle);
6706 }
6707 DBG (5, "sane_cancel: finished\n");
6708 }
6709
6710 SANE_Status
sane_set_io_mode(SANE_Handle handle, SANE_Bool non_blocking)6711 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
6712 {
6713 Mustek_Scanner *s = handle;
6714
6715 if (!s)
6716 {
6717 DBG (1, "sane_set_io_mode: handle is null!\n");
6718 return SANE_STATUS_INVAL;
6719 }
6720
6721 DBG (4, "sane_set_io_mode: %s\n",
6722 non_blocking ? "non-blocking" : "blocking");
6723
6724 if (!s->scanning)
6725 {
6726 DBG (1, "sane_set_io_mode: call sane_start before sane_set_io_mode");
6727 return SANE_STATUS_INVAL;
6728 }
6729
6730 if (fcntl (s->pipe, F_SETFL, non_blocking ? O_NONBLOCK : 0) < 0)
6731 {
6732 DBG (1, "sane_set_io_mode: can't set io mode");
6733 return SANE_STATUS_IO_ERROR;
6734 }
6735
6736 return SANE_STATUS_GOOD;
6737 }
6738
6739 SANE_Status
sane_get_select_fd(SANE_Handle handle, SANE_Int * fd)6740 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
6741 {
6742 Mustek_Scanner *s = handle;
6743
6744 if (!s)
6745 {
6746 DBG (1, "sane_get_select_fd: handle is null!\n");
6747 return SANE_STATUS_INVAL;
6748 }
6749 if (!fd)
6750 {
6751 DBG (1, "sane_get_select_fd: fd is null!\n");
6752 return SANE_STATUS_INVAL;
6753 }
6754
6755 DBG (4, "sane_get_select_fd\n");
6756 if (!s->scanning)
6757 return SANE_STATUS_INVAL;
6758
6759 *fd = s->pipe;
6760 return SANE_STATUS_GOOD;
6761 }
6762
6763 #include "mustek_scsi_pp.c"
6764