1 /*
2 epson.c - SANE library for Epson flatbed scanners.
3
4 Based on Kazuhiro Sasayama previous
5 Work on epson.[ch] file from the SANE package.
6
7 Original code taken from sane-0.71
8 Copyright (C) 1997 Hypercore Software Design, Ltd.
9
10 modifications
11 Copyright (C) 1998-1999 Christian Bucher <bucher@vernetzt.at>
12 Copyright (C) 1998-1999 Kling & Hautzinger GmbH
13 Copyright (C) 1999 Norihiko Sawa <sawa@yb3.so-net.ne.jp>
14 Copyright (C) 2000 Mike Porter <mike@udel.edu> (mjp)
15 Copyright (C) 2003 EPSON KOWA Corporation
16 Copyright (C) 1999-2005 Karl Heinz Kremer <khk@khk.net>
17 Copyright (C) 2006 Claus Boje <claus@egehuset.dk>
18 */
19
20 #define SANE_EPSON_VERSION "SANE Epson Backend v0.2.47 - 2006-08-21"
21 #define SANE_EPSON_BUILD 247
22
23 /*
24 This file is part of the SANE package.
25
26 This program is free software; you can redistribute it and/or
27 modify it under the terms of the GNU General Public License as
28 published by the Free Software Foundation; either version 2 of the
29 License, or (at your option) any later version.
30
31 This program is distributed in the hope that it will be useful, but
32 WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 General Public License for more details.
35
36 You should have received a copy of the GNU General Public License
37 along with this program. If not, see <https://www.gnu.org/licenses/>.
38
39 As a special exception, the authors of SANE give permission for
40 additional uses of the libraries contained in this release of SANE.
41
42 The exception is that, if you link a SANE library with other files
43 to produce an executable, this does not by itself cause the
44 resulting executable to be covered by the GNU General Public
45 License. Your use of that executable is in no way restricted on
46 account of linking the SANE library code into it.
47
48 This exception does not, however, invalidate any other reasons why
49 the executable file might be covered by the GNU General Public
50 License.
51
52 If you submit changes to SANE to the maintainers to be included in
53 a subsequent release, you agree by submitting the changes that
54 those changes may be distributed with this exception intact.
55
56 If you write modifications of your own for SANE, it is your choice
57 whether to permit this exception to apply to your modifications.
58 If you do not wish that, delete this exception notice. */
59
60 /*
61 2006-08-21 Fix buffer overflow error (submitted by Johannes Meixner)
62 2006-06-11 Applied patch from Henning. Fixed a number of compiler warnings
63 2006-03-12 Added support for perfetion 4990 photo 4800 dpi
64 2005-01-09 "flaming hack to get USB scanners working without timeouts under linux"
65 submitted by "Steve" (in comment to bug #300830)
66 2004-12-18 Added USB IDs for CX-4600 and CX-3650
67 2004-10-16 Added USB ID for Expression 10000XL
68 2004-05-08 Disable feed() for Perfection1640
69 2004-02-08 Reformat all source code with "indent -bli0"
70 2004-02-01 Added D7 function level as copy of D1 for CX-6400
71 Added IDs for CX-6400 and Perfection 4870
72 2003-10-27 Replaced DBG(0, ... with DBG(1, ...
73 2003-09-12 Increment only once in loop to find USB scanners
74 Fix rounding problem when determining number of lines to scan
75 2003-08-21 Removed '//' comments - again ...
76 Added EPSON Kowa copyright message
77 2003-08-15 Added support for GT-30000, with support for the ADF in simplex mode
78 Borrowed some code from the EPSON Kowa IScan version of the backend
79 Use sanei_scsi_cmd2() to send commands. This makes this backend
80 useable for SBP-2 under FreeBSD
81 2003-05-11 Initialize OPT_LIMIT_RESOLUTION before first call to filter_resolution_list()
82 Fix memory problem in get_identity_information(). Both problems were
83 reported to the Debian bug database.
84 2003-03-26 Fixed two warnings reported by der Mouse
85 2003-02-16 Code cleanup, use more descriptive variable names.
86 2003-02-15 Move sanei_usb_init() to sane_init(). Thanks to Ron Cemer
87 for providing the patch.
88 2003-02-15 Fix problem with "usb <vendor> <product> syntax in config file
89 2002-12-28 Added advanced option to display only short resolution list for
90 displays that can not show the complete list.
91 2002-11-23 Fixed problem with dropout color.
92 2002-11-03 Full libusb support.
93 2002-10-05 Fixed problem with incorrect response to sane_get_parameters()
94 in certain situations.
95 2002-09-01 USB scanners are now using libsane-usb functions
96 2002-08-17 Fixed typo in variable name.
97 Fixed IEEE-1394 problem with Perfection-2450.
98 Fixed problem with older B3 level SCSI scanners that do
99 not support the extended status request.
100 2002-04-22 Declare close_scanner() and open_scanner() before they
101 are used.
102 2002-04-13 Check if scanner needs to be opened for the reset call.
103 (Thanks to Thomas Wenrich for pointing this out)
104 Added product IDs for Perfection 1650 and 2450
105 2002-01-18 Recognize GT-xxxx type scanners also when using the SCSI
106 or IEEE-1394 interface
107 2002-01-06 Disable TEST_IOCTL again, which was enabled by accident. Also
108 protect the ioctl portion with an #ifdef __linux__
109 2002-01-05 Version 0.2.17
110 Check for and set s->fd to -1 when device is closed.
111 Removed black gamma table - only use RGB even for grayscale
112 2002-01-01 Do not call access() for OS/2 systems
113 2001-11-13 Version 0.2.16
114 Do not call access() for parallel port scanners.
115 2001-11-11 Version 0.2.15
116 Fixed "wait-for-button" functionality, accidentally merged back wrong
117 version after code freeze.
118 Corrected "need-strange-reorder" recognition.
119 Added IOCTL support to header file.
120 2001-11-10 Version 0.2.14
121 Added "wait-for-button" functionality
122 2001-10-30 I18N patches (Stefan Roellin)
123 2001-10-28 Fixed bug with 1650 recognition
124 2001-06-09 Version 0.2.09
125 Changed debug level for sense handler from 0 to 2
126 2001-05-25 Version 0.2.07
127 Allow more than 8 bit color depth even for preview mode
128 since Xsane can handle this. Some code cleanup.
129 2001-05-24 Removed ancient code that was used to determine the resolution
130 back when the backend still had a slider for the resolution
131 selection.
132 2001-05-22 Version 0.2.06
133 Added sense_handler to support the GT-8000 scanner. Thanks to Matthias Trute
134 for figuring out the details.
135 Also added experimental code to use USB scanner probing. Need kernel patch
136 for this.
137 2001-05-19 Version 0.2.05
138 fixed the year in the recent change log entries - I now that it's
139 2001...
140 Finally fixed the TPU problem with B4 level scanners
141 2001-05-13 Version 0.2.04
142 Removed check for '\n' before end of line
143 Free memory malloced in sane_get_devices() in sane_exit() again
144 2001-04-22 Version 0.2.03
145 Check first if the scanner does support the set film type
146 and set focus position before the GUI elements are displayed.
147 This caused problems with older (B4 level) scanners when a TPU
148 was connected.
149 2001-03-31 Version 0.2.02
150 2001-03-17 Next attempt to get the reported number of lines correct
151 for the "color shuffling" part.
152 Added more comments.
153 2000-12-25 Version 0.2.01
154 Fixed problem with bilevel scanning with Perfection610: The
155 line count has to be an even number with this scanner.
156 Several initialization fixes regarding bit depth selection.
157 This version goes back into the CVS repository, the 1.0.4
158 release is out and therefore the code freeze is over.
159 Some general cleanup, added more comments.
160 2000-12-09 Version 0.2.00
161 Cleaned up printing of gamma table data. 16 elements
162 are now printed in one line without the [epson] in
163 between the values. Values are only printed for
164 Debug levels >= 10.
165 2000-12-04 We've introduced the concept of inverting images
166 when scanning from a TPU. This is fine, but
167 the user supplied gamma tables no longer work.
168 This is because the data a frontend is going
169 to compute a gamma table for is not what the
170 scanner actually sent. So, we have to back into
171 the proper gamma table. I think this works. See
172 set_gamma_table. (mjp)
173 2000-12-03 added the 12/14/16 bit support again.
174 2000-12-03 Version 0.1.38
175 removed changes regarding 12/14 bit support because
176 of SANE feature freeze for 1.0.4. The D1 fix for
177 reading the values from the scanner instead of using
178 hardcoded values and the fix for the off-by-one error
179 in the reorder routine are still in the code base.
180 Also force reload after change of scan mode.
181 The full backend can be downloaded from my web site at
182 http://www.freecolormanagement.com/sane
183 2000-12-03 Fixed off-by-one error in color reordering function.
184 2000-12-02 Read information about optical resolution and line
185 distance from scanner instead of hardcoded values.
186 Add support for color depth > 8 bits per channel.
187 2000-11-23 Display "Set Focus" control only for scanners that
188 can actually handle the command.
189 2000-11-19 Added support for the "set focus position" command,
190 this is necessary for the Expression1600.
191 2000-07-28 Changed #include <...> to #include "..." for the
192 sane/... include files.
193 2000-07-26 Fixed problem with Perfection610: The variable
194 s->color_shuffle_line was never correctly initialized
195 2000-06-28 When closing the scanner device the data that's
196 still in the scanner, waiting to be transferred
197 is flushed. This fixes the problem with scanimage -T
198 2000-06-13 Invert image when scanning negative with TPU,
199 Show film type only when TPU is selected
200 2000-06-13 Initialize optical_res to 0 (Dave Hill)
201 2000-06-07 Fix in sane_close() - found by Henning Meier-Geinitz
202 2000-06-01 Threshold should only be active when scan depth
203 is 1 and halftoning is off. (mjp)
204 2000-05-28 Turned on scanner based color correction.
205 Dependencies between many options are now
206 being enforced. For instance, auto area seg
207 (AAS) should only be on when scan depth == 1.
208 Added some routines to active and deactivate
209 options. Routines report if option changed.
210 Help prevent extraneous option reloads. Split
211 sane_control_option in getvalue and setvalue.
212 Further split up setvalue into several different
213 routines. (mjp)
214 2000-05-21 In sane_close use close_scanner instead of just the
215 SCSI close function.
216 2000-05-20 ... finally fixed the problem with the 610
217 Added resolution_list to Epson_Device structure in
218 epson.h - this fixes a bug that caused problems when
219 more than one EPSON scanner was connected.
220 2000-05-13 Fixed the color problem with the Perfection 610. The few
221 lines with "garbage" at the beginning of the scan are not
222 yet removed.
223 2000-05-06 Added support for multiple EPSON scanners. At this time
224 this may not be bug free, but it's a start and it seems
225 to work well with just one scanner.
226 2000-04-06 Did some cleanup on the gamma correction part. The user
227 defined table is now initialized to gamma=1, the gamma
228 handling is also no longer depending on platform specific
229 tables (handled instead by pointers to the actual tables)
230 2000-03-27 Disable request for push button status
231 2000-03-22 Removed free() calls to static strings to remove
232 compile warnings. These were introduced to apparently
233 fix an OS/2 bug. It now turned out that they are not
234 necessary. The real fix was in the repository for a
235 long time (2000-01-25).
236 2000-03-19 Fixed problem with A4 level devices - they use the
237 line mode instead of the block mode. The routine to
238 handle this was screwed up pretty bad. Now I have
239 a solid version that handles all variations of line
240 mode (automatically deals with the order the color
241 lines are sent).
242 2000-03-06 Fixed occasional crash after warm up when the "in warmup
243 state" went away in between doing ESC G and getting the
244 extended status message.
245 2000-03-02 Code cleanup, disabled ZOOM until I have time to
246 deal with all the side effects.
247 2000-03-01 More D1 fixes. In the future I have to come up with
248 a more elegant solution to distinguish between different
249 function levels. The level > n does not work anymore with
250 D1.
251 Added support for "set threshold" and "set zoom".
252 2000-02-23 First stab at level D1 support, also added a test
253 for valid "set halftone" command to enable OPT_HALFTONE
254 2000-02-21 Check for "warming up" in after sane_start. This is
255 IMHO a horrible hack, but that's the only way without
256 a major redesign that will work. (KHK)
257 2000-02-20 Added some cleanup on error conditions in attach()
258 Use new sanei_config_read() instead of fgets() for
259 compatibility with OS/2 (Yuri Dario)
260 2000-02-19 Changed some "int" to "size_t" types
261 Removed "Preview Resolution"
262 Implemented resolution list as WORD_LIST instead of
263 a RANGE (KHK)
264 2000-02-11 Default scan source is always "Flatbed", regardless
265 of installed options. Corrected some typos. (KHK)
266 2000-02-03 Gamma curves now coupled with gamma correction menu.
267 Only when "User defined" is selected are the curves
268 selected. (Dave Hill)
269 Renamed "Contrast" to "Gamma Correction" (KHK)
270 2000-02-02 "Brown Paper Bag Release" Put the USB fix finally
271 into the CVS repository.
272 2000-02-01 Fixed problem with USB scanner not being recognized
273 because of the changes to attach a few days ago. (KHK)
274 2000-01-29 fixed core dump with xscanimage by moving the gamma
275 curves to the standard interface (no longer advanced)
276 Removed pragma pack() from source code to make it
277 easier to compile on non-gcc compilers (KHK)
278 2000-01-26 fixed problem with resolution selection when using the
279 resolution list in xsane (KHK)
280 2000-01-25 moved the section where the device name is assigned
281 in attach. This avoids the core dump of frontend
282 applications when no scanner is found (Dave Hill)
283 2000-01-24 reorganization of SCSI related "helper" functions
284 started support for user defined color correction -
285 this is not yet available via the UI (Christian Bucher)
286 2000-01-24 Removed C++ style comments '//' (KHK)
287 */
288
289
290 /* #define TEST_IOCTL */
291
292 /* DON'T CHANGE THE NEXT LINE ! */
293 /* #undef FORCE_COLOR_SHUFFLE */
294
295
296 #ifdef _AIX
297 #include <lalloca.h> /* MUST come first for AIX! */
298 #endif
299
300 /* --------------------- SANE INTERNATIONALISATION ------------------ */
301
302 #ifndef SANE_I18N
303 #define SANE_I18N(text) (text)
304 #endif
305
306 #include "../include/sane/config.h"
307
308 #include <lalloca.h>
309
310 #include <limits.h>
311 #include <stdio.h>
312 #include <string.h>
313 #include <stdlib.h>
314 #include <ctype.h>
315 #include <fcntl.h>
316 #include <unistd.h>
317 #include <errno.h>
318
319 #include "../include/sane/sane.h"
320 #include "../include/sane/saneopts.h"
321 #include "../include/sane/sanei_scsi.h"
322
323 #include "../include/sane/sanei_usb.h"
324
325 #include "../include/sane/sanei_pio.h"
326
327 #define BACKEND_NAME epson
328 #include "../include/sane/sanei_backend.h"
329
330 #include "../include/sane/sanei_config.h"
331
332 #include "epson.h"
333 #include "epson_scsi.h"
334 #include "epson_usb.h"
335
336 #define EPSON_CONFIG_FILE "epson.conf"
337
338 #ifndef PATH_MAX
339 #define PATH_MAX (1024)
340 #endif
341
342 #define walloc(x) (x *)malloc(sizeof(x))
343 #define walloca(x) (x *)alloca(sizeof(x))
344
345 #ifndef XtNumber
346 #define XtNumber(x) ( sizeof(x)/ sizeof(x[0]) )
347 #define XtOffset(p_type,field) ((size_t)&(((p_type)NULL)->field))
348 #define XtOffsetOf(s_type,field) XtOffset(s_type*,field)
349 #endif
350
351 #define NUM_OF_HEX_ELEMENTS (16) /* number of hex numbers per line for data dump */
352 #define DEVICE_NAME_LEN (16) /* length of device name in extended status */
353
354 /* NOTE: you can find these codes with "man ascii". */
355 #define STX 0x02
356 #define ACK 0x06
357 #define NAK 0x15
358 #define CAN 0x18
359 #define ESC 0x1B
360 #define PF 0x19
361
362 #define S_ACK "\006"
363 #define S_CAN "\030"
364
365 #define STATUS_FER 0x80 /* fatal error */
366 #define STATUS_AREA_END 0x20 /* area end */
367 #define STATUS_OPTION 0x10 /* option installed */
368
369 #define EXT_STATUS_FER 0x80 /* fatal error */
370 #define EXT_STATUS_FBF 0x40 /* flat bed scanner */
371 #define EXT_STATUS_WU 0x02 /* warming up */
372 #define EXT_STATUS_PB 0x01 /* scanner has a push button */
373
374 #define EXT_STATUS_IST 0x80 /* option detected */
375 #define EXT_STATUS_EN 0x40 /* option enabled */
376 #define EXT_STATUS_ERR 0x20 /* other error */
377 #define EXT_STATUS_PE 0x08 /* no paper */
378 #define EXT_STATUS_PJ 0x04 /* paper jam */
379 #define EXT_STATUS_OPN 0x02 /* cover open */
380
381 #define EPSON_LEVEL_A1 0
382 #define EPSON_LEVEL_A2 1
383 #define EPSON_LEVEL_B1 2
384 #define EPSON_LEVEL_B2 3
385 #define EPSON_LEVEL_B3 4
386 #define EPSON_LEVEL_B4 5
387 #define EPSON_LEVEL_B5 6
388 #define EPSON_LEVEL_B6 7
389 #define EPSON_LEVEL_B7 8
390 #define EPSON_LEVEL_B8 9
391 #define EPSON_LEVEL_F5 10
392 #define EPSON_LEVEL_D1 11
393 #define EPSON_LEVEL_D7 12
394 #define EPSON_LEVEL_D8 13
395
396 /* there is also a function level "A5", which I'm ignoring here until somebody can
397 convince me that this is still needed. The A5 level was for the GT-300, which
398 was (is) a monochrome only scanner. So if somebody really wants to use this
399 scanner with SANE get in touch with me and we can work something out - khk */
400
401 #define EPSON_LEVEL_DEFAULT EPSON_LEVEL_B3
402
403 static EpsonCmdRec epson_cmd[] = {
404 /*
405 * request identity
406 * | request identity2
407 * | | request status
408 * | | | request condition
409 * | | | | set color mode
410 * | | | | | start scanning
411 * | | | | | | set data format
412 * | | | | | | | set resolution
413 * | | | | | | | | set zoom
414 * | | | | | | | | | set scan area
415 * | | | | | | | | | | set brightness
416 * | | | | | | | | | | | set gamma
417 * | | | | | | | | | | | | set halftoning
418 * | | | | | | | | | | | | | set color correction
419 * | | | | | | | | | | | | | | initialize scanner
420 * | | | | | | | | | | | | | | | set speed
421 * | | | | | | | | | | | | | | | | set lcount
422 * | | | | | | | | | | | | | | | | | mirror image
423 * | | | | | | | | | | | | | | | | | | set gamma table
424 * | | | | | | | | | | | | | | | | | | | set outline emphasis
425 * | | | | | | | | | | | | | | | | | | | | set dither
426 * | | | | | | | | | | | | | | | | | | | | | set color correction coefficients
427 * | | | | | | | | | | | | | | | | | | | | | | request extension status
428 * | | | | | | | | | | | | | | | | | | | | | | | control an extension
429 * | | | | | | | | | | | | | | | | | | | | | | | | forward feed / eject
430 * | | | | | | | | | | | | | | | | | | | | | | | | | feed
431 * | | | | | | | | | | | | | | | | | | | | | | | | | | request push button status
432 * | | | | | | | | | | | | | | | | | | | | | | | | | | | control auto area segmentation
433 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | set film type
434 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set exposure time
435 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set bay
436 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set threshold
437 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | set focus position
438 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | request focus position
439 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
440 * | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
441 */
442 {"A1", 'I', 0, 'F','S', 0, 'G', 0, 'R', 0, 'A', 0, {0,0,0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
443 {"A2", 'I', 0, 'F','S', 0, 'G','D','R','H','A','L',{-3,3,0}, 'Z','B', 0, '@', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
444 {"B1", 'I', 0, 'F','S','C','G','D','R', 0, 'A', 0, {0,0,0}, 0, 'B', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
445 {"B2", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-3,3,0}, 'Z','B', 0, '@', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
446 {"B3", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-3,3,0}, 'Z','B','M','@', 0, 0, 0, 0, 0, 0, 'm','f','e', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
447 {"B4", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-3,3,0}, 'Z','B','M','@','g','d', 0, 'z','Q','b','m','f','e', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
448 {"B5", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-3,3,0}, 'Z','B','M','@','g','d','K','z','Q','b','m','f','e', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
449 {"B6", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-3,3,0}, 'Z','B','M','@','g','d','K','z','Q','b','m','f','e', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
450 {"B7", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-4,3,0}, 'Z','B','M','@','g','d','K','z','Q','b','m','f','e','\f', 0, '!','s','N', 0, 0, 't', 0, 0},
451 {"B8", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-4,3,0}, 'Z','B','M','@','g','d','K','z','Q','b','m','f','e','\f', 0x19,'!','s','N', 0, 0, 0, 'p','q'},
452 {"F5", 'I', 0, 'F','S','C','G','D','R','H','A','L',{-3,3,0}, 'Z', 0, 'M','@','g','d','K','z','Q', 0, 'm','f','e','\f', 0, 0, 0, 'N','T','P', 0, 0, 0},
453 {"D1", 'I','i','F', 0, 'C','G','D','R', 0, 'A', 0, {0,0,0}, 'Z', 0, 0, '@','g','d', 0, 'z', 0, 0, 0, 'f', 0, 0, 0, '!', 0, 0, 0, 0, 0, 0, 0},
454 {"D7", 'I', 0, 'F', 0, 'C','G','D','R', 0, 'A', 0, {0,0,0}, 'Z', 0, 0, '@','g','d', 0, 'z', 0, 0, 0, 'f', 0, 0, 0, '!', 0, 0, 0, 0, 0, 0, 0},
455 {"D8", 'I','i','F', 0, 'C','G','D','R', 0, 'A', 0, {0,0,0}, 'Z', 0, 0, '@','g','d', 0, 'z', 0, 0, 0, 'f','e', 0, 0, '!', 0, 0, 0, 0, 0, 0, 0},
456 };
457
458
459
460 /*
461 * Definition of the mode_param struct, that is used to
462 * specify the valid parameters for the different scan modes.
463 *
464 * The depth variable gets updated when the bit depth is modified.
465 */
466
467 struct mode_param
468 {
469 int color;
470 int mode_flags;
471 int dropout_mask;
472 int depth;
473 };
474
475 static struct mode_param mode_params[] = {
476 {0, 0x00, 0x30, 1},
477 {0, 0x00, 0x30, 8},
478 {1, 0x02, 0x00, 8}
479 };
480
481 static const SANE_String_Const mode_list[] = {
482 SANE_VALUE_SCAN_MODE_LINEART,
483 SANE_VALUE_SCAN_MODE_GRAY,
484 SANE_VALUE_SCAN_MODE_COLOR,
485 NULL
486 };
487
488 static const SANE_String_Const adf_mode_list[] = {
489 SANE_I18N ("Simplex"),
490 SANE_I18N ("Duplex"),
491 NULL
492 };
493
494
495 /*
496 * Define the different scan sources:
497 */
498
499 #define FBF_STR SANE_I18N("Flatbed")
500 #define TPU_STR SANE_I18N("Transparency Unit")
501 #define ADF_STR SANE_I18N("Automatic Document Feeder")
502
503 /*
504 * source list need one dummy entry (save device settings is crashing).
505 * NOTE: no const - this list gets created while exploring the capabilities
506 * of the scanner.
507 */
508
509 static SANE_String_Const source_list[] = {
510 FBF_STR,
511 NULL,
512 NULL,
513 NULL
514 };
515
516 /* some defines to make handling the TPU easier: */
517 #define FILM_TYPE_POSITIVE (0)
518 #define FILM_TYPE_NEGATIVE (1)
519
520 static const SANE_String_Const film_list[] = {
521 SANE_I18N ("Positive Film"),
522 SANE_I18N ("Negative Film"),
523 NULL
524 };
525
526 static const SANE_String_Const focus_list[] = {
527 SANE_I18N ("Focus on glass"),
528 SANE_I18N ("Focus 2.5mm above glass"),
529 NULL
530 };
531
532 /*
533 * TODO: add some missing const.
534 */
535
536 #define HALFTONE_NONE 0x01
537 #define HALFTONE_TET 0x03
538
539 static int halftone_params[] = {
540 HALFTONE_NONE,
541 0x00,
542 0x10,
543 0x20,
544 0x80,
545 0x90,
546 0xa0,
547 0xb0,
548 HALFTONE_TET,
549 0xc0,
550 0xd0
551 };
552
553 static const SANE_String_Const halftone_list[] = {
554 SANE_I18N ("None"),
555 SANE_I18N ("Halftone A (Hard Tone)"),
556 SANE_I18N ("Halftone B (Soft Tone)"),
557 SANE_I18N ("Halftone C (Net Screen)"),
558 NULL
559 };
560
561 static const SANE_String_Const halftone_list_4[] = {
562 SANE_I18N ("None"),
563 SANE_I18N ("Halftone A (Hard Tone)"),
564 SANE_I18N ("Halftone B (Soft Tone)"),
565 SANE_I18N ("Halftone C (Net Screen)"),
566 SANE_I18N ("Dither A (4x4 Bayer)"),
567 SANE_I18N ("Dither B (4x4 Spiral)"),
568 SANE_I18N ("Dither C (4x4 Net Screen)"),
569 SANE_I18N ("Dither D (8x4 Net Screen)"),
570 NULL
571 };
572
573 static const SANE_String_Const halftone_list_7[] = {
574 SANE_I18N ("None"),
575 SANE_I18N ("Halftone A (Hard Tone)"),
576 SANE_I18N ("Halftone B (Soft Tone)"),
577 SANE_I18N ("Halftone C (Net Screen)"),
578 SANE_I18N ("Dither A (4x4 Bayer)"),
579 SANE_I18N ("Dither B (4x4 Spiral)"),
580 SANE_I18N ("Dither C (4x4 Net Screen)"),
581 SANE_I18N ("Dither D (8x4 Net Screen)"),
582 SANE_I18N ("Text Enhanced Technology"),
583 SANE_I18N ("Download pattern A"),
584 SANE_I18N ("Download pattern B"),
585 NULL
586 };
587
588 static int dropout_params[] = {
589 0x00, /* none */
590 0x10, /* red */
591 0x20, /* green */
592 0x30 /* blue */
593 };
594
595 static const SANE_String_Const dropout_list[] = {
596 SANE_I18N ("None"),
597 SANE_I18N ("Red"),
598 SANE_I18N ("Green"),
599 SANE_I18N ("Blue"),
600 NULL
601 };
602
603 /*
604 * Color correction:
605 * One array for the actual parameters that get sent to the scanner (color_params[]),
606 * one array for the strings that get displayed in the user interface (color_list[])
607 * and one array to mark the user defined color correction (dolor_userdefined[]).
608 */
609
610 static int color_params[] = {
611 0x00,
612 0x01,
613 0x10,
614 0x20,
615 0x40,
616 0x80
617 };
618
619 static SANE_Bool color_userdefined[] = {
620 SANE_FALSE,
621 SANE_TRUE,
622 SANE_FALSE,
623 SANE_FALSE,
624 SANE_FALSE,
625 SANE_FALSE
626 };
627
628 static const SANE_String_Const color_list[] = {
629 SANE_I18N ("No Correction"),
630 SANE_I18N ("User defined"),
631 SANE_I18N ("Impact-dot printers"),
632 SANE_I18N ("Thermal printers"),
633 SANE_I18N ("Ink-jet printers"),
634 SANE_I18N ("CRT monitors"),
635 NULL
636 };
637
638 /*
639 * Gamma correction:
640 * The A and B level scanners work differently than the D level scanners, therefore
641 * I define two different sets of arrays, plus one set of variables that get set to
642 * the actually used params and list arrays at runtime.
643 */
644
645 static int gamma_params_ab[] = {
646 0x01,
647 0x03,
648 0x00,
649 0x10,
650 0x20
651 };
652
653 static const SANE_String_Const gamma_list_ab[] = {
654 SANE_I18N ("Default"),
655 SANE_I18N ("User defined"),
656 SANE_I18N ("High density printing"),
657 SANE_I18N ("Low density printing"),
658 SANE_I18N ("High contrast printing"),
659 NULL
660 };
661
662 static SANE_Bool gamma_userdefined_ab[] = {
663 SANE_FALSE,
664 SANE_TRUE,
665 SANE_FALSE,
666 SANE_FALSE,
667 SANE_FALSE,
668 };
669
670 static int gamma_params_d[] = {
671 0x03,
672 0x04
673 };
674
675 static const SANE_String_Const gamma_list_d[] = {
676 SANE_I18N ("User defined (Gamma=1.0)"),
677 SANE_I18N ("User defined (Gamma=1.8)"),
678 NULL
679 };
680
681 static SANE_Bool gamma_userdefined_d[] = {
682 SANE_TRUE,
683 SANE_TRUE
684 };
685
686 static SANE_Bool *gamma_userdefined;
687 static int *gamma_params;
688
689 /* flaming hack to get USB scanners
690 working without timeouts under linux */
691 /* (cribbed from fujitsu.c) */
692 static unsigned int r_cmd_count = 0;
693 static unsigned int w_cmd_count = 0;
694
695
696
697
698 /* Bay list:
699 * this is used for the FilmScan
700 */
701
702 static const SANE_String_Const bay_list[] = {
703 " 1 ",
704 " 2 ",
705 " 3 ",
706 " 4 ",
707 " 5 ",
708 " 6 ",
709 NULL
710 };
711
712 /*
713 * minimum, maximum, quantization.
714 */
715
716 static const SANE_Range u8_range = { 0, 255, 0 };
717 static const SANE_Range s8_range = { -127, 127, 0 };
718 static const SANE_Range zoom_range = { 50, 200, 0 };
719
720 /*
721 * The "switch_params" are used for several boolean choices
722 */
723 static int switch_params[] = {
724 0,
725 1
726 };
727
728 #define mirror_params switch_params
729 #define speed_params switch_params
730 #define film_params switch_params
731
732 static const SANE_Range outline_emphasis_range = { -2, 2, 0 };
733
734 /* static const SANE_Range gamma_range = { -2, 2, 0 }; */
735
736 struct qf_param
737 {
738 SANE_Word tl_x;
739 SANE_Word tl_y;
740 SANE_Word br_x;
741 SANE_Word br_y;
742 };
743
744 /* gcc don't like to overwrite const field */
745 static /*const */ struct qf_param qf_params[] = {
746 {0, 0, SANE_FIX (120.0), SANE_FIX (120.0)},
747 {0, 0, SANE_FIX (148.5), SANE_FIX (210.0)},
748 {0, 0, SANE_FIX (210.0), SANE_FIX (148.5)},
749 {0, 0, SANE_FIX (215.9), SANE_FIX (279.4)}, /* 8.5" x 11" */
750 {0, 0, SANE_FIX (210.0), SANE_FIX (297.0)},
751 {0, 0, 0, 0}
752 };
753
754 static const SANE_String_Const qf_list[] = {
755 SANE_I18N ("CD"),
756 SANE_I18N ("A5 portrait"),
757 SANE_I18N ("A5 landscape"),
758 SANE_I18N ("Letter"),
759 SANE_I18N ("A4"),
760 SANE_I18N ("Max"),
761 NULL
762 };
763
764
765 static SANE_Word *bitDepthList = NULL;
766
767
768
769 /*
770 * List of pointers to devices - will be dynamically allocated depending
771 * on the number of devices found.
772 */
773 static const SANE_Device **devlist = 0;
774
775
776 /*
777 * Some utility functions
778 */
779
780 static size_t
max_string_size(const SANE_String_Const strings[])781 max_string_size (const SANE_String_Const strings[])
782 {
783 size_t size, max_size = 0;
784 int i;
785
786 for (i = 0; strings[i]; i++)
787 {
788 size = strlen (strings[i]) + 1;
789 if (size > max_size)
790 max_size = size;
791 }
792 return max_size;
793 }
794
795 typedef struct
796 {
797 u_char code;
798 u_char status;
799 u_char count1;
800 u_char count2;
801 u_char buf[1];
802
803 } EpsonHdrRec, *EpsonHdr;
804
805 typedef struct
806 {
807 u_char code;
808 u_char status;
809 u_char count1;
810 u_char count2;
811
812 u_char type;
813 u_char level;
814
815 u_char buf[1];
816
817 } EpsonIdentRec, *EpsonIdent;
818
819 typedef union
820 {
821 EpsonHdrRec hdr;
822 EpsonIdentRec ident;
823 } EpsonHdrUnionRec, *EpsonHdrUnion;
824
825
826 typedef struct
827 {
828 u_char code;
829 u_char status;
830 u_short count;
831
832 u_char buf[1];
833
834 } EpsonParameterRec, *EpsonParameter;
835
836 typedef struct
837 {
838 u_char code;
839 u_char status;
840
841 u_char buf[4];
842
843 } EpsonDataRec, *EpsonData;
844
845 /*
846 *
847 *
848 */
849
850 static EpsonHdrUnion command (Epson_Scanner * s, u_char * cmd, size_t cmd_size,
851 SANE_Status * status);
852 static SANE_Status get_identity_information (SANE_Handle handle);
853 static SANE_Status get_identity2_information (SANE_Handle handle);
854 static int send (Epson_Scanner * s, void *buf, size_t buf_size,
855 SANE_Status * status);
856 static ssize_t receive (Epson_Scanner * s, void *buf, ssize_t buf_size,
857 SANE_Status * status);
858 static SANE_Status color_shuffle (SANE_Handle handle, int *new_length);
859 static SANE_Status request_focus_position (SANE_Handle handle,
860 u_char * position);
861 static SANE_Bool request_push_button_status (SANE_Handle handle,
862 SANE_Bool * theButtonStatus);
863 static void activateOption (Epson_Scanner * s, SANE_Int option,
864 SANE_Bool * change);
865 static void deactivateOption (Epson_Scanner * s, SANE_Int option,
866 SANE_Bool * change);
867 static void setOptionState (Epson_Scanner * s, SANE_Bool state,
868 SANE_Int option, SANE_Bool * change);
869 static void close_scanner (Epson_Scanner * s);
870 static SANE_Status open_scanner (Epson_Scanner * s);
871 SANE_Status sane_auto_eject (Epson_Scanner * s);
872 static SANE_Status attach_one_usb (SANE_String_Const devname);
873 static void filter_resolution_list (Epson_Scanner * s);
874 static void get_size (char c1, char c2, double *w, double *h);
875 static void scan_finish (Epson_Scanner * s);
876
877 /*
878 *
879 *
880 */
881
882 static int
send(Epson_Scanner * s, void *buf, size_t buf_size, SANE_Status * status)883 send (Epson_Scanner * s, void *buf, size_t buf_size, SANE_Status * status)
884 {
885 DBG (3, "send buf, size = %lu\n", (u_long) buf_size);
886
887 #if 1
888 {
889 unsigned int k;
890 const u_char *s = buf;
891
892 for (k = 0; k < buf_size; k++)
893 {
894 DBG (125, "buf[%d] %02x %c\n", k, s[k], isprint (s[k]) ? s[k] : '.');
895 }
896 }
897 #endif
898
899 if (s->hw->connection == SANE_EPSON_SCSI)
900 {
901 return sanei_epson_scsi_write (s->fd, buf, buf_size, status);
902 }
903 else if (s->hw->connection == SANE_EPSON_PIO)
904 {
905 size_t n;
906
907 if (buf_size == (n = sanei_pio_write (s->fd, buf, buf_size)))
908 *status = SANE_STATUS_GOOD;
909 else
910 *status = SANE_STATUS_INVAL;
911
912 return n;
913 }
914 else if (s->hw->connection == SANE_EPSON_USB)
915 {
916 size_t n;
917 n = buf_size;
918 *status = sanei_usb_write_bulk (s->fd, buf, &n);
919 w_cmd_count++;
920 DBG (5, "w_cmd_count = %d\n",w_cmd_count);
921 DBG (5, "r_cmd_count = %d\n",r_cmd_count);
922
923 return n;
924 }
925
926 return SANE_STATUS_INVAL;
927 /* never reached */
928 }
929
930 /*
931 *
932 *
933 */
934
935 static ssize_t
receive(Epson_Scanner * s, void *buf, ssize_t buf_size, SANE_Status * status)936 receive (Epson_Scanner * s, void *buf, ssize_t buf_size, SANE_Status * status)
937 {
938 ssize_t n = 0;
939
940 if (s->hw->connection == SANE_EPSON_SCSI)
941 {
942 n = sanei_epson_scsi_read (s->fd, buf, buf_size, status);
943 }
944 else if (s->hw->connection == SANE_EPSON_PIO)
945 {
946 if (buf_size == (n = sanei_pio_read (s->fd, buf, (size_t) buf_size)))
947 *status = SANE_STATUS_GOOD;
948 else
949 *status = SANE_STATUS_INVAL;
950 }
951 else if (s->hw->connection == SANE_EPSON_USB)
952 {
953 /* !!! only report an error if we don't read anything */
954 n = buf_size; /* buf_size gets overwritten */
955 *status = sanei_usb_read_bulk (s->fd, (SANE_Byte *) buf, (size_t *) & n);
956 r_cmd_count += (n+63)/64; /* add # of packets, rounding up */
957 DBG (5, "w_cmd_count = %d\n",w_cmd_count);
958 DBG (5, "r_cmd_count = %d\n",r_cmd_count);
959
960 if (n > 0)
961 *status = SANE_STATUS_GOOD;
962 }
963
964 DBG (7, "receive buf, expected = %lu, got = %ld\n", (u_long) buf_size, (long) n);
965
966 #if 1
967 if (n > 0)
968 {
969 int k;
970 const u_char *s = buf;
971
972 for (k = 0; k < n; k++)
973 {
974 DBG (127, "buf[%d] %02x %c\n", k, s[k], isprint (s[k]) ? s[k] : '.');
975 }
976 }
977 #else
978 {
979 int i;
980 ssize_t k;
981 ssize_t hex_start = 0;
982 const u_char *s = buf;
983 char hex_str[NUM_OF_HEX_ELEMENTS * 3 + 1];
984 char tmp_str[NUM_OF_HEX_ELEMENTS * 3 + 1];
985 char ascii_str[NUM_OF_HEX_ELEMENTS * 2 + 1];
986
987 hex_str[0] = '\0';
988 ascii_str[0] = '\0';
989
990 for (k = 0; k < buf_size; k++)
991 {
992 /* write out the data in lines of 16 bytes */
993 /* add the next hex value to the hex string */
994 sprintf (tmp_str, "%s %02x", hex_str, s[k]);
995 strcpy (hex_str, tmp_str);
996
997 /* add the character to the ascii string */
998 sprintf (tmp_str, "%s %c", ascii_str, isprint (s[k]) ? s[k] : '.');
999 strcpy (ascii_str, tmp_str);
1000
1001 if ((k % (NUM_OF_HEX_ELEMENTS)) == 0)
1002 {
1003 if (k != 0) /* don't do this the first time */
1004 {
1005 for (i = strlen (hex_str); i < (NUM_OF_HEX_ELEMENTS * 3); i++)
1006 {
1007 hex_str[i] = ' ';
1008 }
1009 hex_str[NUM_OF_HEX_ELEMENTS + 1] = '\0';
1010
1011 DBG (125, "recv buf[%05d]: %s %s\n", hex_start, hex_str,
1012 ascii_str);
1013 hex_start = k;
1014 hex_str[0] = '\0';
1015 ascii_str[0] = '\0';
1016 }
1017 }
1018 }
1019
1020 for (i = strlen (hex_str); i < NUM_OF_HEX_ELEMENTS * 3; i++)
1021 {
1022 hex_str[i] = ' ';
1023 }
1024 hex_str[NUM_OF_HEX_ELEMENTS + 1] = '\0';
1025
1026 DBG (125, "recv buf[%05d]: %s %s\n", hex_start, hex_str, ascii_str);
1027 }
1028 #endif
1029
1030 return n;
1031 }
1032
1033 /*
1034 *
1035 *
1036 */
1037
1038 static SANE_Status
expect_ack(Epson_Scanner * s)1039 expect_ack (Epson_Scanner * s)
1040 {
1041 u_char result[1];
1042 size_t len;
1043 SANE_Status status;
1044
1045 len = sizeof (result);
1046
1047 receive (s, result, len, &status);
1048
1049 if (SANE_STATUS_GOOD != status)
1050 return status;
1051
1052 if (ACK != result[0])
1053 return SANE_STATUS_INVAL;
1054
1055 return SANE_STATUS_GOOD;
1056 }
1057
1058 /*
1059 *
1060 *
1061 */
1062
1063 static SANE_Status
set_cmd(Epson_Scanner * s, u_char cmd, int val)1064 set_cmd (Epson_Scanner * s, u_char cmd, int val)
1065 {
1066 SANE_Status status;
1067 u_char params[2];
1068
1069 if (!cmd)
1070 return SANE_STATUS_UNSUPPORTED;
1071
1072 params[0] = ESC;
1073 params[1] = cmd;
1074
1075 send (s, params, 2, &status);
1076 if (SANE_STATUS_GOOD != (status = expect_ack (s)))
1077 return status;
1078
1079 params[0] = val;
1080 send (s, params, 1, &status);
1081 status = expect_ack (s);
1082
1083 return status;
1084 }
1085
1086 /* A little helper function to correct the extended status reply
1087 gotten from scanners with known buggy firmware.
1088 */
1089 static void
fix_up_extended_status_reply(const char *model, u_char * buf)1090 fix_up_extended_status_reply (const char *model, u_char * buf)
1091 {
1092
1093 if (0 == strncmp (model, "ES-9000H", strlen ("ES-9000H"))
1094 || 0 == strncmp (model, "GT-30000", strlen ("GT-30000")))
1095 {
1096 DBG (1, "Fixing up buggy ADF max scan dimensions.\n");
1097 buf[2] = 0xB0;
1098 buf[3] = 0x6D;
1099 buf[4] = 0x60;
1100 buf[5] = 0x9F;
1101 }
1102 }
1103
1104 static void
print_params(const SANE_Parameters params)1105 print_params (const SANE_Parameters params)
1106 {
1107 DBG (5, "params.format = %d\n", params.format);
1108 DBG (5, "params.last_frame = %d\n", params.last_frame);
1109 DBG (5, "params.bytes_per_line = %d\n", params.bytes_per_line);
1110 DBG (5, "params.pixels_per_line = %d\n", params.pixels_per_line);
1111 DBG (5, "params.lines = %d\n", params.lines);
1112 DBG (5, "params.depth = %d\n", params.depth);
1113 }
1114
1115
1116 /*
1117 *
1118 *
1119 */
1120
1121 #define set_focus_position(s,v) set_cmd( s,(s)->hw->cmd->set_focus_position,v)
1122 #define set_color_mode(s,v) set_cmd( s,(s)->hw->cmd->set_color_mode,v)
1123 #define set_data_format(s,v) set_cmd( s,(s)->hw->cmd->set_data_format, v)
1124 #define set_halftoning(s,v) set_cmd( s,(s)->hw->cmd->set_halftoning, v)
1125 #define set_gamma(s,v) set_cmd( s,(s)->hw->cmd->set_gamma, v)
1126 #define set_color_correction(s,v) set_cmd( s,(s)->hw->cmd->set_color_correction, v)
1127 #define set_lcount(s,v) set_cmd( s,(s)->hw->cmd->set_lcount, v)
1128 #define set_bright(s,v) set_cmd( s,(s)->hw->cmd->set_bright, v)
1129 #define mirror_image(s,v) set_cmd( s,(s)->hw->cmd->mirror_image, v)
1130 #define set_speed(s,v) set_cmd( s,(s)->hw->cmd->set_speed, v)
1131 #define set_outline_emphasis(s,v) set_cmd( s,(s)->hw->cmd->set_outline_emphasis, v)
1132 #define control_auto_area_segmentation(s,v) set_cmd( s,(s)->hw->cmd->control_auto_area_segmentation, v)
1133 #define set_film_type(s,v) set_cmd( s,(s)->hw->cmd->set_film_type, v)
1134 #define set_exposure_time(s,v) set_cmd( s,(s)->hw->cmd->set_exposure_time, v)
1135 #define set_bay(s,v) set_cmd( s,(s)->hw->cmd->set_bay, v)
1136 #define set_threshold(s,v) set_cmd( s,(s)->hw->cmd->set_threshold, v)
1137 #define control_extension(s,v) set_cmd( s,(s)->hw->cmd->control_an_extension, v)
1138
1139 /*#define (s,v) set_cmd( s,(s)->hw->cmd->, v) */
1140
1141 static SANE_Status
set_zoom(Epson_Scanner * s, int x_zoom, int y_zoom)1142 set_zoom (Epson_Scanner * s, int x_zoom, int y_zoom)
1143 {
1144 SANE_Status status;
1145 u_char cmd[2];
1146 u_char params[2];
1147
1148 if (!s->hw->cmd->set_zoom)
1149 return SANE_STATUS_GOOD;
1150
1151 cmd[0] = ESC;
1152 cmd[1] = s->hw->cmd->set_zoom;
1153
1154 send (s, cmd, 2, &status);
1155 status = expect_ack (s);
1156
1157 if (status != SANE_STATUS_GOOD)
1158 return status;
1159
1160 params[0] = x_zoom;
1161 params[1] = y_zoom;
1162
1163 send (s, params, 2, &status);
1164 status = expect_ack (s);
1165
1166 return status;
1167 }
1168
1169
1170 static SANE_Status
set_resolution(Epson_Scanner * s, int xres, int yres)1171 set_resolution (Epson_Scanner * s, int xres, int yres)
1172 {
1173 SANE_Status status;
1174 u_char params[4];
1175
1176 if (!s->hw->cmd->set_resolution)
1177 return SANE_STATUS_GOOD;
1178
1179 params[0] = ESC;
1180 params[1] = s->hw->cmd->set_resolution;
1181
1182 send (s, params, 2, &status);
1183 status = expect_ack (s);
1184
1185 if (status != SANE_STATUS_GOOD)
1186 return status;
1187
1188 params[0] = xres;
1189 params[1] = xres >> 8;
1190 params[2] = yres;
1191 params[3] = yres >> 8;
1192
1193 send (s, params, 4, &status);
1194 status = expect_ack (s);
1195
1196 return status;
1197 }
1198
1199 /*
1200 * set_scan_area()
1201 *
1202 * Sends the "set scan area" command to the scanner with the currently selected
1203 * scan area. This scan area is already corrected for "color shuffling" if
1204 * necessary.
1205 */
1206 static SANE_Status
set_scan_area(Epson_Scanner * s, int x, int y, int width, int height)1207 set_scan_area (Epson_Scanner * s, int x, int y, int width, int height)
1208 {
1209 SANE_Status status;
1210 u_char params[8];
1211
1212 DBG (1, "set_scan_area: %p %d %d %d %d\n", (void *) s, x, y, width, height);
1213
1214 if (!s->hw->cmd->set_scan_area)
1215 {
1216 DBG (1, "set_scan_area not supported\n");
1217 return SANE_STATUS_GOOD;
1218 }
1219
1220 /* verify the scan area */
1221 if (x < 0 || y < 0 || width <= 0 || height <= 0)
1222 return SANE_STATUS_INVAL;
1223
1224 params[0] = ESC;
1225 params[1] = s->hw->cmd->set_scan_area;
1226
1227 send (s, params, 2, &status);
1228 status = expect_ack (s);
1229 if (status != SANE_STATUS_GOOD)
1230 return status;
1231
1232 params[0] = x;
1233 params[1] = x >> 8;
1234 params[2] = y;
1235 params[3] = y >> 8;
1236 params[4] = width;
1237 params[5] = width >> 8;
1238 params[6] = height;
1239 params[7] = height >> 8;
1240
1241 send (s, params, 8, &status);
1242 status = expect_ack (s);
1243
1244 return status;
1245 }
1246
1247 /*
1248 * set_color_correction_coefficients()
1249 *
1250 * Sends the "set color correction coefficients" command with the currently selected
1251 * parameters to the scanner.
1252 */
1253
1254 static SANE_Status
set_color_correction_coefficients(Epson_Scanner * s)1255 set_color_correction_coefficients (Epson_Scanner * s)
1256 {
1257 SANE_Status status;
1258 u_char cmd = s->hw->cmd->set_color_correction_coefficients;
1259 u_char params[2];
1260 const int length = 9;
1261 signed char cct[9];
1262
1263 DBG (1, "set_color_correction_coefficients: starting.\n");
1264 if (!cmd)
1265 return SANE_STATUS_UNSUPPORTED;
1266
1267 params[0] = ESC;
1268 params[1] = cmd;
1269
1270 send (s, params, 2, &status);
1271 if (SANE_STATUS_GOOD != (status = expect_ack (s)))
1272 return status;
1273
1274 cct[0] = s->val[OPT_CCT_1].w;
1275 cct[1] = s->val[OPT_CCT_2].w;
1276 cct[2] = s->val[OPT_CCT_3].w;
1277 cct[3] = s->val[OPT_CCT_4].w;
1278 cct[4] = s->val[OPT_CCT_5].w;
1279 cct[5] = s->val[OPT_CCT_6].w;
1280 cct[6] = s->val[OPT_CCT_7].w;
1281 cct[7] = s->val[OPT_CCT_8].w;
1282 cct[8] = s->val[OPT_CCT_9].w;
1283
1284 DBG (1, "set_color_correction_coefficients: %d,%d,%d %d,%d,%d %d,%d,%d.\n",
1285 cct[0], cct[1], cct[2], cct[3],
1286 cct[4], cct[5], cct[6], cct[7], cct[8]);
1287
1288 send (s, cct, length, &status);
1289 status = expect_ack (s);
1290 DBG (1, "set_color_correction_coefficients: ending=%d.\n", status);
1291
1292 return status;
1293 }
1294
1295 /*
1296 *
1297 *
1298 */
1299
1300 static SANE_Status
set_gamma_table(Epson_Scanner * s)1301 set_gamma_table (Epson_Scanner * s)
1302 {
1303
1304 SANE_Status status;
1305 u_char cmd = s->hw->cmd->set_gamma_table;
1306 u_char params[2];
1307 const int length = 257;
1308 u_char gamma[257];
1309 int n;
1310 int table;
1311 /* static const char gamma_cmds[] = { 'M', 'R', 'G', 'B' }; */
1312 static const char gamma_cmds[] = { 'R', 'G', 'B' };
1313
1314
1315 DBG (1, "set_gamma_table: starting.\n");
1316 if (!cmd)
1317 return SANE_STATUS_UNSUPPORTED;
1318
1319 params[0] = ESC;
1320 params[1] = cmd;
1321
1322 /*
1323 * Print the gamma tables before sending them to the scanner.
1324 */
1325
1326 if (DBG_LEVEL > 0)
1327 {
1328 int c, i, j;
1329
1330 DBG (1, "set_gamma_table()\n");
1331 for (c = 0; c < 3; c++)
1332 {
1333 for (i = 0; i < 256; i += 16)
1334 {
1335 char gammaValues[16 * 3 + 1], newValue[4];
1336
1337 gammaValues[0] = '\0';
1338
1339 for (j = 0; j < 16; j++)
1340 {
1341 sprintf (newValue, " %02x", s->gamma_table[c][i + j]);
1342 strcat (gammaValues, newValue);
1343 }
1344
1345 DBG (10, "Gamma Table[%d][%d] %s\n", c, i, gammaValues);
1346 }
1347 }
1348 }
1349
1350
1351 /*
1352 * TODO: &status in send makes no sense like that.
1353 */
1354
1355 /*
1356 * When handling inverted images, we must also invert the user
1357 * supplied gamma function. This is *not* just 255-gamma -
1358 * this gives a negative image.
1359 */
1360
1361 for (table = 0; table < 3; table++)
1362 {
1363 gamma[0] = gamma_cmds[table];
1364 if (s->invert_image)
1365 {
1366 for (n = 0; n < 256; ++n)
1367 {
1368 gamma[n + 1] = 255 - s->gamma_table[table][255 - n];
1369 }
1370 }
1371 else
1372 {
1373 for (n = 0; n < 256; ++n)
1374 {
1375 gamma[n + 1] = s->gamma_table[table][n];
1376 }
1377 }
1378
1379 send (s, params, 2, &status);
1380 if (SANE_STATUS_GOOD != (status = expect_ack (s)))
1381 return status;
1382
1383 send (s, gamma, length, &status);
1384 if (SANE_STATUS_GOOD != (status = expect_ack (s)))
1385 return status;
1386
1387 }
1388
1389 DBG (1, "set_gamma_table: complete = %d.\n", status);
1390
1391 return status;
1392 }
1393
1394
1395
1396 void
get_size(char c1, char c2, double *w, double *h)1397 get_size (char c1, char c2, double *w, double *h)
1398 {
1399 int ind;
1400 unsigned char flag;
1401
1402 double wsizetbl[] = {
1403 11.60, /* A3V */
1404 11.00, /* WLT */
1405 10.12, /* B4V */
1406 8.50, /* LGV */
1407 8.27, /* A4V */
1408 11.69, /* A4H */
1409 8.50, /* LTV */
1410 11.00, /* LTH */
1411 7.17, /* B5V */
1412 10.12, /* B5H */
1413 5.83, /* A5V */
1414 8.27, /* A5H */
1415 7.25, /* EXV */
1416 10.50, /* EXH */
1417 11.69, /* unknown */
1418 11.69, /* unknown */
1419 11.69, /* unknown */
1420 };
1421 double hsizetbl[] = {
1422 16.54, /* A3V */
1423 17.00, /* WLT */
1424 14.33, /* B4V */
1425 14.00, /* LGV */
1426 11.69, /* A4V */
1427 8.27, /* A4H */
1428 11.00, /* LTV */
1429 8.50, /* LTH */
1430 10.12, /* B5V */
1431 7.17, /* B5H */
1432 8.27, /* A5V */
1433 5.83, /* A5H */
1434 10.50, /* EXV */
1435 7.25, /* EXH */
1436 17.00, /* unknown */
1437 17.00, /* unknown */
1438 17.00, /* unknown */
1439 };
1440
1441 flag = c1;
1442 for (ind = 0; ind < 8; ind++)
1443 {
1444 if (flag & 0x80)
1445 goto DetectSize;
1446 flag = flag << 1;
1447 }
1448 flag = c2;
1449 for (; ind < 16; ind++)
1450 {
1451 if (flag & 0x80)
1452 goto DetectSize;
1453 flag = flag << 1;
1454 }
1455
1456 DetectSize:
1457
1458 *w = wsizetbl[ind];
1459 *h = hsizetbl[ind];
1460
1461 DBG (10, "detected width: %f\n", *w);
1462 DBG (10, "detected height: %f\n", *h);
1463 }
1464
1465
1466 /*
1467 * check_ext_status()
1468 *
1469 * Requests the extended status flag from the scanner. The "warming up" condition
1470 * is reported as a warning (only visible if debug level is set to 10 or greater) -
1471 * every other condition is reported as an error.
1472 *
1473 * This function only gets called when we are dealing with a scanner that supports the
1474 * "warming up" code, so it's not a problem for B3 level scanners, that don't handle
1475 * request extended status commands.
1476 */
1477
1478 static SANE_Status
check_ext_status(Epson_Scanner * s, int *max_x, int *max_y)1479 check_ext_status (Epson_Scanner * s, int *max_x, int *max_y)
1480 {
1481 SANE_Status status;
1482 u_char cmd = s->hw->cmd->request_extended_status;
1483 u_char params[2];
1484 u_char *buf;
1485 EpsonHdr head;
1486
1487 *max_x = 0;
1488 *max_y = 0;
1489
1490 if (cmd == 0)
1491 return SANE_STATUS_UNSUPPORTED;
1492
1493 params[0] = ESC;
1494 params[1] = cmd;
1495
1496 head = (EpsonHdr) command (s, params, 2, &status);
1497 if (NULL == head)
1498 {
1499 DBG (1, "Extended status flag request failed\n");
1500 return status;
1501 }
1502
1503 buf = &head->buf[0];
1504
1505 if (buf[0] & EXT_STATUS_WU)
1506 {
1507 DBG (10, "option: warming up\n");
1508 status = SANE_STATUS_DEVICE_BUSY;
1509 }
1510
1511 if (buf[0] & EXT_STATUS_FER)
1512 {
1513 DBG (1, "option: fatal error\n");
1514 status = SANE_STATUS_INVAL;
1515 }
1516
1517 if (s->hw->ADF && s->hw->use_extension && s->hw->cmd->feed)
1518 {
1519 fix_up_extended_status_reply (s->hw->sane.model, buf);
1520
1521 *max_x = buf[3] << 8 | buf[2];
1522 *max_y = buf[5] << 8 | buf[4];
1523
1524 if (0 == strcmp ("ES-9000H", s->hw->sane.model)
1525 || 0 == strcmp ("GT-30000", s->hw->sane.model))
1526 {
1527 /* set size of current sheet, but don't clobber zoom
1528 settings (which should always be smaller than the
1529 detected sheet size) */
1530 double w, h;
1531 get_size (buf[16], buf[17], &w, &h);
1532 w = SANE_FIX (w * MM_PER_INCH);
1533 h = SANE_FIX (h * MM_PER_INCH);
1534 if (w < s->val[OPT_BR_X].w)
1535 s->val[OPT_BR_X].w = w;
1536 if (h < s->val[OPT_BR_Y].w)
1537 s->val[OPT_BR_Y].w = h;
1538 }
1539 }
1540
1541
1542 if (buf[1] & EXT_STATUS_ERR)
1543 {
1544 DBG (1, "ADF: other error\n");
1545 status = SANE_STATUS_INVAL;
1546 }
1547
1548 if (buf[1] & EXT_STATUS_PE)
1549 {
1550 DBG (1, "ADF: no paper\n");
1551 status = SANE_STATUS_NO_DOCS;
1552 return status;
1553 }
1554
1555 if (buf[1] & EXT_STATUS_PJ)
1556 {
1557 DBG (1, "ADF: paper jam\n");
1558 status = SANE_STATUS_JAMMED;
1559 }
1560
1561 if (buf[1] & EXT_STATUS_OPN)
1562 {
1563 DBG (1, "ADF: cover open\n");
1564 status = SANE_STATUS_COVER_OPEN;
1565 }
1566
1567 if (buf[6] & EXT_STATUS_ERR)
1568 {
1569 DBG (1, "TPU: other error\n");
1570 status = SANE_STATUS_INVAL;
1571 }
1572
1573 /* return the max. scan area for the ADF */
1574 if (buf[6] & EXT_STATUS_IST)
1575 {
1576 *max_x = buf[8] << 8 | buf[7];
1577 *max_y = buf[10] << 8 | buf[9];
1578 }
1579
1580 /* return the max. scan area for the flatbed */
1581 if (s->hw->devtype == 3 && s->hw->use_extension == 0)
1582 {
1583 double w, h;
1584 get_size (buf[18], buf[19], &w, &h);
1585 *max_x = (int) (w * s->hw->dpi_range.max);
1586 *max_y = (int) (h * s->hw->dpi_range.max);
1587 }
1588
1589 free (head);
1590
1591 return status;
1592 }
1593
1594 /*
1595 * reset()
1596 *
1597 * Send the "initialize scanner" command to the device and reset it.
1598 *
1599 */
1600
1601 static SANE_Status
reset(Epson_Scanner * s)1602 reset (Epson_Scanner * s)
1603 {
1604 SANE_Status status;
1605 u_char param[2];
1606 SANE_Bool needToClose = SANE_FALSE;
1607
1608 DBG (5, "reset()\n");
1609
1610 if (!s->hw->cmd->initialize_scanner)
1611 return SANE_STATUS_GOOD;
1612
1613 param[0] = ESC;
1614 param[1] = s->hw->cmd->initialize_scanner;
1615
1616 if (s->fd == -1)
1617 {
1618 needToClose = SANE_TRUE;
1619 DBG (5, "reset calling open_scanner\n");
1620 if ((status = open_scanner (s)) != SANE_STATUS_GOOD)
1621 return status;
1622 }
1623
1624 send (s, param, 2, &status);
1625 status = expect_ack (s);
1626
1627 if (needToClose)
1628 {
1629 close_scanner (s);
1630 }
1631
1632 return status;
1633 }
1634
1635
1636 /*
1637 * close_scanner()
1638 *
1639 * Close the open scanner. Depending on the connection method, a different
1640 * close function is called.
1641 */
1642
1643 static void
close_scanner(Epson_Scanner * s)1644 close_scanner (Epson_Scanner * s)
1645 {
1646 DBG (5, "close_scanner(fd = %d)\n", s->fd);
1647
1648 if (s->fd == -1)
1649 return;
1650
1651 if (r_cmd_count % 2)
1652 {
1653 /* send a request_status. This toggles w_cmd_count and r_cmd_count */
1654 u_char param[3];
1655 u_char result[5];
1656 SANE_Status status;
1657
1658 param[0] = ESC;
1659 param[1] = s->hw->cmd->request_status;
1660 param[2]='\0';
1661 send(s,param,2,&status);
1662 receive(s,result,4,&status);
1663 }
1664
1665
1666 DBG (5, "w_cmd_count = %d\n",w_cmd_count);
1667 DBG (5, "r_cmd_count = %d\n",r_cmd_count);
1668
1669 if (w_cmd_count % 2)
1670 {
1671 int junk1,junk2;
1672
1673 /* check extended status. This toggles w_cmd_count%2 only */
1674 check_ext_status (s,&junk1,&junk2);
1675 }
1676
1677 DBG (5, "w_cmd_count = %d\n",w_cmd_count);
1678 DBG (5, "r_cmd_count = %d\n",r_cmd_count);
1679
1680
1681 if (s->hw->connection == SANE_EPSON_SCSI)
1682 {
1683 sanei_scsi_close (s->fd);
1684 }
1685 else if (s->hw->connection == SANE_EPSON_PIO)
1686 {
1687 sanei_pio_close (s->fd);
1688 }
1689 else if (s->hw->connection == SANE_EPSON_USB)
1690 {
1691 sanei_usb_close (s->fd);
1692 }
1693
1694 s->fd = -1;
1695 return;
1696 }
1697
1698 /*
1699 * open_scanner()
1700 *
1701 * Open the scanner device. Depending on the connection method,
1702 * different open functions are called.
1703 */
1704
1705 static SANE_Status
open_scanner(Epson_Scanner * s)1706 open_scanner (Epson_Scanner * s)
1707 {
1708 SANE_Status status = 0;
1709
1710 DBG (5, "open_scanner()\n");
1711
1712 if (s->fd != -1)
1713 {
1714 DBG (5, "scanner is already open: fd = %d\n", s->fd);
1715 return SANE_STATUS_GOOD; /* no need to open the scanner */
1716 }
1717
1718 /* don't do this for OS2: */
1719 #ifndef HAVE_OS2_H
1720 #if 0
1721 /* test the device name */
1722 if ((s->hw->connection != SANE_EPSON_PIO)
1723 && (access (s->hw->sane.name, R_OK | W_OK) != 0))
1724 {
1725 DBG (1, "sane_start: access(%s, R_OK | W_OK) failed\n", s->hw->sane.name);
1726 return SANE_STATUS_ACCESS_DENIED;
1727 }
1728 #endif
1729 #endif
1730
1731
1732 if (s->hw->connection == SANE_EPSON_SCSI)
1733 {
1734 status = sanei_scsi_open (s->hw->sane.name, &s->fd,
1735 sanei_epson_scsi_sense_handler, NULL);
1736 if (SANE_STATUS_GOOD != status)
1737 {
1738 DBG (1, "sane_start: %s open failed: %s\n", s->hw->sane.name,
1739 sane_strstatus (status));
1740 return status;
1741 }
1742 }
1743 else if (s->hw->connection == SANE_EPSON_PIO)
1744 {
1745 status = sanei_pio_open (s->hw->sane.name, &s->fd);
1746 if (SANE_STATUS_GOOD != status)
1747 {
1748 DBG (1, "sane_start: %s open failed: %s\n", s->hw->sane.name,
1749 sane_strstatus (status));
1750 return status;
1751 }
1752 }
1753 else if (s->hw->connection == SANE_EPSON_USB)
1754 {
1755 status = sanei_usb_open (s->hw->sane.name, &s->fd);
1756
1757 return status;
1758 }
1759
1760 return SANE_STATUS_GOOD;
1761 }
1762
1763
1764 /*
1765 * feed ( )
1766 */
1767
1768 static SANE_Status
feed(Epson_Scanner * s)1769 feed (Epson_Scanner * s)
1770 {
1771 SANE_Status status;
1772 u_char params[2];
1773 u_char cmd = s->hw->cmd->feed;
1774
1775 DBG (5, "feed()\n");
1776
1777 if (!cmd)
1778 {
1779 DBG (5, "feed() is not supported\n");
1780 return SANE_STATUS_UNSUPPORTED;
1781 }
1782
1783 params[0] = cmd;
1784
1785 send (s, params, 1, &status);
1786
1787 if (SANE_STATUS_GOOD != (status = expect_ack (s)))
1788 {
1789 close_scanner (s);
1790 return status;
1791 }
1792
1793 return status;
1794 }
1795
1796
1797 /*
1798 * eject()
1799 *
1800 * Eject the current page from the ADF. The scanner is opened prior to
1801 * sending the command and closed afterwards.
1802 *
1803 */
1804
1805 static SANE_Status
eject(Epson_Scanner * s)1806 eject (Epson_Scanner * s)
1807 {
1808 SANE_Status status;
1809 u_char params[2];
1810 u_char cmd = s->hw->cmd->eject;
1811 SANE_Bool needToClose = SANE_FALSE;
1812
1813 DBG (5, "eject()\n");
1814
1815 if (!cmd)
1816 return SANE_STATUS_UNSUPPORTED;
1817
1818 if (s->fd == -1)
1819 {
1820 needToClose = SANE_TRUE;
1821 if (SANE_STATUS_GOOD != (status = open_scanner (s)))
1822 return status;
1823 }
1824
1825 params[0] = cmd;
1826
1827 send (s, params, 1, &status);
1828
1829 if (SANE_STATUS_GOOD != (status = expect_ack (s)))
1830 {
1831 close_scanner (s);
1832 return status;
1833 }
1834
1835 if (needToClose)
1836 close_scanner (s);
1837
1838 return status;
1839 }
1840
1841 /*
1842 *
1843 *
1844 */
1845
1846 static int num_devices = 0; /* number of EPSON scanners attached to backend */
1847 static Epson_Device *first_dev = NULL; /* first EPSON scanner in list */
1848 static Epson_Scanner *first_handle = NULL;
1849
1850
1851 static EpsonHdrUnion
command(Epson_Scanner * s, u_char * cmd, size_t cmd_size, SANE_Status * status)1852 command (Epson_Scanner * s, u_char * cmd, size_t cmd_size,
1853 SANE_Status * status)
1854 {
1855 EpsonHdrUnion hdrunion, hdrunion_bak;
1856 EpsonHdr head;
1857 u_char *buf;
1858 int count;
1859
1860 if (NULL == (hdrunion = walloc (EpsonHdrUnionRec)))
1861 {
1862 DBG (1, "out of memory (line %d)\n", __LINE__);
1863 *status = SANE_STATUS_NO_MEM;
1864 return (EpsonHdrUnion) 0;
1865 }
1866
1867 head = &(hdrunion->hdr);
1868
1869 send (s, cmd, cmd_size, status);
1870
1871 if (SANE_STATUS_GOOD != *status)
1872 {
1873 /* this is necessary for the GT-8000. I don't know why, but
1874 it seems to fix the problem. It should not have any
1875 ill effects on other scanners. */
1876 *status = SANE_STATUS_GOOD;
1877 send (s, cmd, cmd_size, status);
1878 if (SANE_STATUS_GOOD != *status)
1879 return (EpsonHdrUnion) 0;
1880 }
1881
1882 buf = (u_char *) head;
1883
1884 if (s->hw->connection == SANE_EPSON_SCSI)
1885 {
1886 receive (s, buf, 4, status);
1887 buf += 4;
1888 }
1889 else if (s->hw->connection == SANE_EPSON_USB)
1890 {
1891 int bytes_read;
1892 bytes_read = receive (s, buf, 4, status);
1893 buf += bytes_read;
1894 }
1895 else
1896 {
1897 receive (s, buf, 1, status);
1898 buf += 1;
1899 }
1900
1901 if (SANE_STATUS_GOOD != *status)
1902 return (EpsonHdrUnion) 0;
1903
1904 DBG (4, "code %02x\n", (int) head->code);
1905
1906 switch (head->code)
1907 {
1908
1909 case NAK:
1910 /* fall through */
1911 /* !!! is this really sufficient to report an error ? */
1912 case ACK:
1913 break; /* no need to read any more data after ACK or NAK */
1914
1915 case STX:
1916 if (s->hw->connection == SANE_EPSON_SCSI)
1917 {
1918 /* nope */
1919 }
1920 else if (s->hw->connection == SANE_EPSON_USB)
1921 {
1922 /* we've already read the complete data */
1923 }
1924 else
1925 {
1926 receive (s, buf, 3, status);
1927 /* buf += 3; */
1928 }
1929
1930 if (SANE_STATUS_GOOD != *status)
1931 return (EpsonHdrUnion) 0;
1932
1933 DBG (4, "status %02x\n", (int) head->status);
1934
1935 count = head->count2 * 255 + head->count1;
1936 DBG (4, "count %d\n", count);
1937
1938 hdrunion_bak = hdrunion;
1939 if (NULL == (hdrunion = realloc (hdrunion,
1940 sizeof (EpsonHdrUnionRec) + count)))
1941 {
1942 free(hdrunion_bak);
1943 DBG (1, "out of memory (line %d)\n", __LINE__);
1944 *status = SANE_STATUS_NO_MEM;
1945 return (EpsonHdrUnion) 0;
1946 }
1947
1948 head = &(hdrunion->hdr);
1949
1950 buf = head->buf;
1951 receive (s, buf, count, status);
1952
1953 if (SANE_STATUS_GOOD != *status)
1954 return (EpsonHdrUnion) 0;
1955
1956 break;
1957
1958 default:
1959 if (0 == head->code)
1960 DBG (1, "Incompatible printer port (probably bi/directional)\n");
1961 else if (cmd[cmd_size - 1] == head->code)
1962 DBG (1, "Incompatible printer port (probably not bi/directional)\n");
1963
1964 DBG (2, "Illegal response of scanner for command: %02x\n", head->code);
1965 break;
1966 }
1967
1968 return hdrunion;
1969 }
1970
1971
1972 /*
1973 * static SANE_Status attach()
1974 *
1975 * Attach one device with name *dev_name to the backend.
1976 */
1977
1978 static SANE_Status
attach(const char *dev_name, Epson_Device * *devp, int type)1979 attach (const char *dev_name, Epson_Device * *devp, int type)
1980 {
1981 SANE_Status status;
1982 Epson_Scanner *s = walloca (Epson_Scanner);
1983 char *str;
1984 struct Epson_Device *dev;
1985 SANE_String_Const *source_list_add = source_list;
1986 int port;
1987
1988 DBG (1, "%s\n", SANE_EPSON_VERSION);
1989
1990 DBG (5, "attach(%s, %d)\n", dev_name, type);
1991
1992 for (dev = first_dev; dev; dev = dev->next)
1993 {
1994 if (strcmp (dev->sane.name, dev_name) == 0)
1995 {
1996 if (devp)
1997 {
1998 *devp = dev;
1999 }
2000 return SANE_STATUS_GOOD;
2001 }
2002 }
2003
2004 dev = malloc (sizeof (*dev));
2005 if (!dev)
2006 {
2007 DBG (1, "out of memory (line %d)\n", __LINE__);
2008 return SANE_STATUS_NO_MEM;
2009 }
2010
2011 /* check for PIO devices */
2012 /* can we convert the device name to an integer? This is only possible
2013 with PIO devices */
2014 port = atoi (dev_name);
2015 if (port != 0)
2016 {
2017 type = SANE_EPSON_PIO;
2018 }
2019
2020 if (strncmp
2021 (dev_name, SANE_EPSON_CONFIG_PIO, strlen (SANE_EPSON_CONFIG_PIO)) == 0)
2022 {
2023 /* we have a match for the PIO string and adjust the device name */
2024 dev_name += strlen (SANE_EPSON_CONFIG_PIO);
2025 dev_name = sanei_config_skip_whitespace (dev_name);
2026 type = SANE_EPSON_PIO;
2027 }
2028
2029
2030 /*
2031 * set dummy values.
2032 */
2033
2034 s->hw = dev;
2035 s->hw->sane.name = NULL;
2036 s->hw->sane.type = "flatbed scanner";
2037 s->hw->sane.vendor = "Epson";
2038 s->hw->sane.model = NULL;
2039 s->hw->optical_res = 0; /* just to have it initialized */
2040 s->hw->color_shuffle = SANE_FALSE;
2041 s->hw->extension = SANE_FALSE;
2042 s->hw->use_extension = SANE_FALSE;
2043
2044 s->hw->need_color_reorder = SANE_FALSE;
2045 s->hw->need_double_vertical = SANE_FALSE;
2046
2047 s->hw->cmd = &epson_cmd[EPSON_LEVEL_DEFAULT]; /* default function level */
2048 s->hw->connection = type;
2049
2050 DBG (3, "attach: opening %s\n", dev_name);
2051
2052 s->hw->last_res = 0;
2053 s->hw->last_res_preview = 0; /* set resolution to safe values */
2054
2055 /*
2056 * decide if interface is USB, SCSI or parallel.
2057 */
2058
2059 /*
2060 * if interface is SCSI do an inquiry.
2061 */
2062
2063 if (s->hw->connection == SANE_EPSON_SCSI)
2064 {
2065 u_char buf[INQUIRY_BUF_SIZE + 1];
2066 size_t buf_size = INQUIRY_BUF_SIZE;
2067
2068 status =
2069 sanei_scsi_open (dev_name, &s->fd, sanei_epson_scsi_sense_handler,
2070 NULL);
2071 if (SANE_STATUS_GOOD != status)
2072 {
2073 DBG (1, "attach: open failed: %s\n", sane_strstatus (status));
2074 return status;
2075 }
2076 DBG (3, "attach: sending INQUIRY\n");
2077
2078 status = sanei_epson_scsi_inquiry (s->fd, 0, buf, &buf_size);
2079 if (SANE_STATUS_GOOD != status)
2080 {
2081 DBG (1, "attach: inquiry failed: %s\n", sane_strstatus (status));
2082 close_scanner (s);
2083 return status;
2084 }
2085
2086 buf[INQUIRY_BUF_SIZE] = 0;
2087 DBG (1, ">%s<\n", buf + 8);
2088
2089 /*
2090 * For USB and PIO scanners this will be done later, once
2091 * we have communication established with the device.
2092 */
2093
2094 if (buf[0] != TYPE_PROCESSOR
2095 || strncmp ((char *) (buf + 8), "EPSON", 5) != 0
2096 || (strncmp ((char *) buf + 16, "SCANNER ", 8) != 0
2097 && strncmp ((char *) buf + 14, "SCANNER ", 8) != 0
2098 && strncmp ((char *) buf + 14, "Perfection", 10) != 0
2099 && strncmp ((char *) buf + 16, "Perfection", 10) != 0
2100 && strncmp ((char *) buf + 16, "Expression", 10) != 0
2101 && strncmp ((char *) buf + 16, "GT", 2) != 0))
2102 {
2103 DBG (1, "attach: device doesn't look like an EPSON scanner\n");
2104 close_scanner (s);
2105 return SANE_STATUS_INVAL;
2106 }
2107 }
2108 /* use the SANEI functions to handle a PIO device */
2109 else if (s->hw->connection == SANE_EPSON_PIO)
2110 {
2111 if (SANE_STATUS_GOOD != (status = sanei_pio_open (dev_name, &s->fd)))
2112 {
2113 DBG (1, "Cannot open %s as a parallel-port device: %s\n",
2114 dev_name, sane_strstatus (status));
2115 return status;
2116 }
2117 }
2118 /* use the SANEI functions to handle a USB device */
2119 else if (s->hw->connection == SANE_EPSON_USB)
2120 {
2121 SANE_Word vendor;
2122 SANE_Word product;
2123 SANE_Bool isLibUSB;
2124
2125 isLibUSB = (strncmp (dev_name, "libusb:", strlen ("libusb:")) == 0);
2126
2127 if ((!isLibUSB) && (strlen (dev_name) == 0))
2128 {
2129 int i;
2130 int numIds;
2131
2132 numIds = sanei_epson_getNumberOfUSBProductIds ();
2133
2134 for (i = 0; i < numIds; i++)
2135 {
2136 product = sanei_epson_usb_product_ids[i];
2137 vendor = 0x4b8;
2138
2139 status = sanei_usb_find_devices (vendor, product, attach_one_usb);
2140 }
2141 return SANE_STATUS_INVAL; /* return - the attach_one_usb()
2142 will take care of this */
2143 }
2144
2145 status = sanei_usb_open (dev_name, &s->fd);
2146
2147 if (SANE_STATUS_GOOD != status)
2148 {
2149 return status;
2150 }
2151
2152 /* if the sanei_usb_get_vendor_product call is not supported,
2153 then we just ignore this and rely on the user to config
2154 the correct device.
2155 */
2156
2157 if (sanei_usb_get_vendor_product (s->fd, &vendor, &product) ==
2158 SANE_STATUS_GOOD)
2159 {
2160 int i; /* loop variable */
2161 int numIds;
2162 SANE_Bool is_valid;
2163
2164 /* check the vendor ID to see if we are dealing with an EPSON device */
2165 if (vendor != SANE_EPSON_VENDOR_ID)
2166 {
2167 /* this is not a supported vendor ID */
2168 DBG (1,
2169 "The device at %s is not manufactured by EPSON (vendor id=0x%x)\n",
2170 dev_name, vendor);
2171 sanei_usb_close (s->fd);
2172 s->fd = -1;
2173 return SANE_STATUS_INVAL;
2174 }
2175
2176 numIds = sanei_epson_getNumberOfUSBProductIds ();
2177 is_valid = SANE_FALSE;
2178 i = 0;
2179
2180 /* check all known product IDs to verify that we know
2181 about the device */
2182 while (i != numIds && !is_valid)
2183 {
2184 if (product == sanei_epson_usb_product_ids[i])
2185 is_valid = SANE_TRUE;
2186
2187 i++;
2188 }
2189
2190 if (is_valid == SANE_FALSE)
2191 {
2192 DBG (1,
2193 "The device at %s is not a supported EPSON scanner (product id=0x%x)\n",
2194 dev_name, product);
2195 sanei_usb_close (s->fd);
2196 s->fd = -1;
2197 return SANE_STATUS_INVAL;
2198 }
2199 DBG (1, "Found valid EPSON scanner: 0x%x/0x%x (vendorID/productID)\n",
2200 vendor, product);
2201 }
2202 else
2203 {
2204 DBG (1,
2205 "Cannot use IOCTL interface to verify that device is a scanner - will continue\n");
2206 }
2207 }
2208
2209 /*
2210 * Initialize the scanner (ESC @).
2211 */
2212 reset (s);
2213
2214
2215
2216 /*
2217 * Identification Request (ESC I).
2218 */
2219 if (s->hw->cmd->request_identity != 0)
2220 {
2221 status = get_identity_information (s);
2222 if (status != SANE_STATUS_GOOD)
2223 return status;
2224 } /* request identity */
2225
2226
2227 /*
2228 * Check for "Request Identity 2" command. If this command is available
2229 * get the information from the scanner and store it in dev
2230 */
2231
2232 if (s->hw->cmd->request_identity2 != 0)
2233 {
2234 status = get_identity2_information (s);
2235 if (status != SANE_STATUS_GOOD)
2236 return status;
2237 } /* request identity 2 */
2238
2239
2240 /*
2241 * Check for the max. supported color depth and assign
2242 * the values to the bitDepthList.
2243 */
2244
2245 bitDepthList = malloc (sizeof (SANE_Word) * 4);
2246 if (bitDepthList == NULL)
2247 {
2248 DBG (1, "out of memory (line %d)\n", __LINE__);
2249 return SANE_STATUS_NO_MEM;
2250 }
2251
2252 bitDepthList[0] = 1; /* we start with one element in the list */
2253 bitDepthList[1] = 8; /* 8bit is the default */
2254
2255 if (set_data_format (s, 16) == SANE_STATUS_GOOD)
2256 {
2257 s->hw->maxDepth = 16;
2258
2259 bitDepthList[0]++;
2260 bitDepthList[bitDepthList[0]] = 16;
2261
2262 }
2263 else if (set_data_format (s, 14) == SANE_STATUS_GOOD)
2264 {
2265 s->hw->maxDepth = 14;
2266
2267 bitDepthList[0]++;
2268 bitDepthList[bitDepthList[0]] = 14;
2269 }
2270 else if (set_data_format (s, 12) == SANE_STATUS_GOOD)
2271 {
2272 s->hw->maxDepth = 12;
2273
2274 bitDepthList[0]++;
2275 bitDepthList[bitDepthList[0]] = 12;
2276 }
2277 else
2278 {
2279 s->hw->maxDepth = 8;
2280
2281 /* the default depth is already in the list */
2282 }
2283
2284 DBG (1, "Max. supported color depth = %d\n", s->hw->maxDepth);
2285
2286
2287 /*
2288 * Check for "request focus position" command. If this command is
2289 * supported, then the scanner does also support the "set focus
2290 * position" command.
2291 */
2292
2293 if (request_focus_position (s, &s->currentFocusPosition) ==
2294 SANE_STATUS_GOOD)
2295 {
2296 DBG (1, "Enabling 'Set Focus' support\n");
2297 s->hw->focusSupport = SANE_TRUE;
2298 s->opt[OPT_FOCUS].cap &= ~SANE_CAP_INACTIVE;
2299
2300 /* reflect the current focus position in the GUI */
2301 if (s->currentFocusPosition < 0x4C)
2302 {
2303 /* focus on glass */
2304 s->val[OPT_FOCUS].w = 0;
2305 }
2306 else
2307 {
2308 /* focus 2.5mm above glass */
2309 s->val[OPT_FOCUS].w = 1;
2310 }
2311
2312 }
2313 else
2314 {
2315 DBG (1, "Disabling 'Set Focus' support\n");
2316 s->hw->focusSupport = SANE_FALSE;
2317 s->opt[OPT_FOCUS].cap |= SANE_CAP_INACTIVE;
2318 s->val[OPT_FOCUS].w = 0; /* on glass - just in case */
2319 }
2320
2321
2322
2323 /*
2324 * Set defaults for no extension.
2325 */
2326
2327 dev->x_range = &dev->fbf_x_range;
2328 dev->y_range = &dev->fbf_y_range;
2329
2330 /*
2331 * Correct for a firmware bug in some Perfection 1650 scanners:
2332 * Firmware version 1.08 reports only half the vertical scan area, we have
2333 * to double the number. To find out if we have to do this, we just compare
2334 * is the vertical range is smaller than the horizontal range.
2335 */
2336
2337 if ((dev->x_range->max - dev->x_range->min) >
2338 (dev->y_range->max - dev->y_range->min))
2339 {
2340 dev->y_range->max += (dev->y_range->max - dev->y_range->min);
2341 dev->need_double_vertical = SANE_TRUE;
2342 dev->need_color_reorder = SANE_TRUE;
2343 }
2344
2345
2346 /*
2347 * Extended status flag request (ESC f).
2348 * this also requests the scanner device name from the scanner
2349 */
2350 /*
2351 * because we are also using the device name from this command,
2352 * we have to run this block even if the scanner does not report
2353 * an extension. The extensions are only reported if the ADF or
2354 * the TPU are actually detected.
2355 */
2356 if (s->hw->cmd->request_extended_status != 0)
2357 {
2358 u_char *buf;
2359 u_char params[2];
2360 EpsonHdr head;
2361
2362 params[0] = ESC;
2363 params[1] = s->hw->cmd->request_extended_status;
2364
2365 if (NULL == (head = (EpsonHdr) command (s, params, 2, &status)))
2366 {
2367 DBG (1, "Extended status flag request failed\n");
2368 dev->sane.model = strdup ("Unknown model");
2369 *source_list_add++ = FBF_STR;
2370 }
2371 else
2372 {
2373 buf = &head->buf[0];
2374
2375 /*
2376 * Add the flatbed option to the source list
2377 */
2378
2379 *source_list_add++ = FBF_STR;
2380
2381 s->hw->devtype = buf[11] >> 6;
2382
2383 /*
2384 * Get the device name and copy it to dev->sane.model.
2385 * The device name starts at buf[0x1A] and is up to 16 bytes long
2386 * We are overwriting whatever was set previously!
2387 */
2388 {
2389 char device_name[DEVICE_NAME_LEN + 1];
2390 char *end_ptr;
2391 int len;
2392
2393 /* make sure that the end of string is marked */
2394 device_name[DEVICE_NAME_LEN] = '\0';
2395
2396 /* copy the string to an area where we can work with it */
2397 memcpy (device_name, buf + 0x1A, DEVICE_NAME_LEN);
2398 end_ptr = strchr (device_name, ' ');
2399 if (end_ptr != NULL)
2400 {
2401 *end_ptr = '\0';
2402 }
2403
2404 len = strlen (device_name);
2405
2406 str = malloc (len + 1);
2407 str[len] = '\0';
2408
2409 dev->sane.model = (char *) memcpy (str, device_name, len);
2410 }
2411 /*
2412 * ADF
2413 */
2414
2415 if (dev->extension && (buf[1] & EXT_STATUS_IST))
2416 {
2417 DBG (1, "ADF detected\n");
2418
2419 /* the GT-30000 does not report the ADF scan area */
2420 if ((strcmp (dev->sane.model, "GT-30000") == 0) ||
2421 (strcmp (dev->sane.model, "ES-9000H") == 0))
2422 {
2423 fix_up_extended_status_reply ((const char *) buf + 26, buf);
2424
2425 dev->duplexSupport = (buf[0] & 0x10) != 0;
2426 if (dev->duplexSupport)
2427 {
2428 DBG (1, "Found DUPLEX ADF\n");
2429 }
2430
2431
2432
2433
2434 }
2435
2436 if (buf[1] & EXT_STATUS_EN)
2437 {
2438 DBG (1, "ADF is enabled\n");
2439 dev->x_range = &dev->adf_x_range;
2440 dev->y_range = &dev->adf_y_range;
2441 }
2442
2443 dev->adf_x_range.min = 0;
2444 dev->adf_x_range.max =
2445 SANE_FIX ((buf[3] << 8 | buf[2]) * 25.4 / dev->dpi_range.max);
2446 dev->adf_x_range.quant = 0;
2447
2448 dev->adf_max_x = buf[3] << 8 | buf[2];
2449
2450 dev->adf_y_range.min = 0;
2451 dev->adf_y_range.max =
2452 SANE_FIX ((buf[5] << 8 | buf[4]) * 25.4 / dev->dpi_range.max);
2453 dev->adf_y_range.quant = 0;
2454
2455 dev->adf_max_y = buf[5] << 8 | buf[4];
2456
2457 DBG (5, "adf tlx %f tly %f brx %f bry %f [mm]\n",
2458 SANE_UNFIX (dev->adf_x_range.min),
2459 SANE_UNFIX (dev->adf_y_range.min),
2460 SANE_UNFIX (dev->adf_x_range.max),
2461 SANE_UNFIX (dev->adf_y_range.max));
2462
2463 *source_list_add++ = ADF_STR;
2464
2465 dev->ADF = SANE_TRUE;
2466 }
2467
2468
2469 /*
2470 * TPU
2471 */
2472
2473 if (dev->extension && (buf[6] & EXT_STATUS_IST))
2474 {
2475 DBG (1, "TPU detected\n");
2476
2477 if (buf[6] & EXT_STATUS_EN)
2478 {
2479 DBG (1, "TPU is enabled\n");
2480 dev->x_range = &dev->tpu_x_range;
2481 dev->y_range = &dev->tpu_y_range;
2482 }
2483
2484 dev->tpu_x_range.min = 0;
2485 dev->tpu_x_range.max =
2486 SANE_FIX ((buf[8] << 8 | buf[7]) * 25.4 / dev->dpi_range.max);
2487 dev->tpu_x_range.quant = 0;
2488
2489 dev->tpu_y_range.min = 0;
2490 dev->tpu_y_range.max =
2491 SANE_FIX ((buf[10] << 8 | buf[9]) * 25.4 / dev->dpi_range.max);
2492 dev->tpu_y_range.quant = 0;
2493
2494 /*
2495 * Check for Perfection 4990 photo/GT-X800 scanner.
2496 * This scanner only report 3200 dpi back.
2497 * The scanner physically supports 4800 dpi.
2498 * This is simulated here...
2499 * Further details read:
2500 * EPSON Programming guide for EPSON Color Image Scanner Perfection 4990
2501 */
2502 if (strncmp((char *) buf + 0x1A,"GT-X800",7) == 0)
2503 {
2504 dev->tpu_x_range.max = (dev->tpu_x_range.max/32)*48;
2505 dev->tpu_y_range.max = (dev->tpu_y_range.max/32)*48;
2506 DBG (5, "dpi_range.max %x \n", dev->dpi_range.max);
2507 }
2508
2509 DBG (5, "tpu tlx %f tly %f brx %f bry %f [mm]\n",
2510 SANE_UNFIX (dev->tpu_x_range.min),
2511 SANE_UNFIX (dev->tpu_y_range.min),
2512 SANE_UNFIX (dev->tpu_x_range.max),
2513 SANE_UNFIX (dev->tpu_y_range.max));
2514
2515 *source_list_add++ = TPU_STR;
2516
2517 dev->TPU = SANE_TRUE;
2518 }
2519
2520 /*
2521 * Get the device name and copy it to dev->sane.model.
2522 * The device name starts at buf[0x1A] and is up to 16 bytes long
2523 * We are overwriting whatever was set previously!
2524 */
2525 {
2526 char device_name[DEVICE_NAME_LEN + 1];
2527 char *end_ptr;
2528 int len;
2529
2530 /* make sure that the end of string is marked */
2531 device_name[DEVICE_NAME_LEN] = '\0';
2532
2533 /* copy the string to an area where we can work with it */
2534 memcpy (device_name, buf + 0x1A, DEVICE_NAME_LEN);
2535 end_ptr = strchr (device_name, ' ');
2536 if (end_ptr != NULL)
2537 {
2538 *end_ptr = '\0';
2539 }
2540
2541 len = strlen (device_name);
2542
2543 str = malloc (len + 1);
2544 str[len] = '\0';
2545
2546 /* finally copy the device name to the structure */
2547 dev->sane.model = (char *) memcpy (str, device_name, len);
2548 }
2549 }
2550 }
2551 else /* command is not known */
2552 {
2553 dev->sane.model = strdup ("EPSON Scanner");
2554 }
2555
2556 *source_list_add = NULL; /* add end marker to source list */
2557
2558 DBG (1, "scanner model: %s\n", dev->sane.model);
2559
2560 /* establish defaults */
2561 s->hw->need_reset_on_source_change = SANE_FALSE;
2562
2563 if (strcmp ("ES-9000H", dev->sane.model) == 0 ||
2564 strcmp ("GT-30000", dev->sane.model) == 0)
2565 {
2566 s->hw->cmd->set_focus_position = 0;
2567 s->hw->cmd->feed = 0x19;
2568 }
2569 else if (strcmp ("GT-8200", dev->sane.model) == 0 ||
2570 strcmp ("Perfection1650", dev->sane.model) == 0 ||
2571 strcmp ("Perfection1640", dev->sane.model) == 0 ||
2572 strcmp ("GT-8700", dev->sane.model) == 0)
2573 {
2574 s->hw->cmd->feed = 0;
2575 s->hw->cmd->set_focus_position = 0;
2576 s->hw->need_reset_on_source_change = SANE_TRUE;
2577 }
2578
2579
2580 /*
2581 * Set values for quick format "max" entry.
2582 */
2583
2584 qf_params[XtNumber (qf_params) - 1].tl_x = dev->x_range->min;
2585 qf_params[XtNumber (qf_params) - 1].tl_y = dev->y_range->min;
2586 qf_params[XtNumber (qf_params) - 1].br_x = dev->x_range->max;
2587 qf_params[XtNumber (qf_params) - 1].br_y = dev->y_range->max;
2588
2589
2590 /*
2591 * Now we can finally set the device name:
2592 */
2593 str = malloc (strlen (dev_name) + 1);
2594 dev->sane.name = strcpy (str, dev_name);
2595
2596 close_scanner (s);
2597
2598 /*
2599 * we are done with this one, prepare for the next scanner:
2600 */
2601
2602 ++num_devices;
2603 dev->next = first_dev;
2604 first_dev = dev;
2605
2606 if (devp)
2607 {
2608 *devp = dev;
2609 }
2610
2611 return SANE_STATUS_GOOD;
2612 }
2613
2614
2615
2616 /*
2617 * attach_one()
2618 *
2619 * Part of the SANE API: Attaches the scanner with the device name in *dev.
2620 */
2621
2622 static SANE_Status
attach_one(const char *dev)2623 attach_one (const char *dev)
2624 {
2625 DBG (5, "attach_one(%s)\n", dev);
2626
2627 return attach (dev, 0, SANE_EPSON_SCSI);
2628 }
2629
2630 SANE_Status
attach_one_usb(SANE_String_Const devname)2631 attach_one_usb (SANE_String_Const devname)
2632 {
2633 int len = strlen (devname);
2634 char *attach_string;
2635
2636 DBG (5, "attach_one_usb(%s)\n", devname);
2637
2638 attach_string = alloca (len + 5);
2639 if (attach_string == NULL)
2640 return SANE_STATUS_NO_MEM;
2641
2642 return attach (devname, 0, SANE_EPSON_USB);
2643 }
2644
2645 /*
2646 * sane_init()
2647 *
2648 *
2649 */
2650 SANE_Status
sane_init(SANE_Int * version_code, SANE_Auth_Callback authorize)2651 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
2652 {
2653 size_t len;
2654 FILE *fp;
2655
2656 (void) authorize; /* get rid of compiler warning */
2657
2658 /* sanei_authorization(devicename, STRINGIFY(BACKEND_NAME), auth_callback); */
2659
2660 DBG_INIT ();
2661 #if defined PACKAGE && defined VERSION
2662 DBG (2, "sane_init: " PACKAGE " " VERSION "\n");
2663 #endif
2664
2665 if (version_code != NULL)
2666 *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, SANE_EPSON_BUILD);
2667
2668 sanei_usb_init ();
2669
2670 /* default to /dev/scanner instead of insisting on config file */
2671 if ((fp = sanei_config_open (EPSON_CONFIG_FILE)))
2672 {
2673 char line[PATH_MAX];
2674
2675 while (sanei_config_read (line, sizeof (line), fp))
2676 {
2677 int vendor, product;
2678
2679 DBG (4, "sane_init, >%s<\n", line);
2680 if (line[0] == '#') /* ignore line comments */
2681 continue;
2682 len = strlen (line);
2683 if (!len)
2684 continue; /* ignore empty lines */
2685
2686 if (sscanf (line, "usb %i %i", &vendor, &product) == 2)
2687 {
2688 int numIds;
2689
2690 /* add the vendor and product IDs to the list of
2691 known devices before we call the attach function */
2692 numIds = sanei_epson_getNumberOfUSBProductIds ();
2693 if (vendor != 0x4b8)
2694 continue; /* this is not an EPSON device */
2695
2696 sanei_epson_usb_product_ids[numIds - 1] = product;
2697 sanei_usb_attach_matching_devices (line, attach_one_usb);
2698 }
2699 else if (strncmp (line, "usb", 3) == 0)
2700 {
2701 const char *dev_name;
2702 /* remove the "usb" sub string */
2703 dev_name = sanei_config_skip_whitespace (line + 3);
2704 attach_one_usb (dev_name);
2705 }
2706 else
2707 {
2708 sanei_config_attach_matching_devices (line, attach_one);
2709 }
2710 }
2711 fclose (fp);
2712 }
2713
2714 /* read the option section and assign the connection type to the
2715 scanner structure - which we don't have at this time. So I have
2716 to come up with something :-) */
2717
2718 return SANE_STATUS_GOOD;
2719 }
2720
2721 /*
2722 * void sane_exit(void)
2723 *
2724 * Clean up the list of attached scanners.
2725 */
2726
2727 void
sane_exit(void)2728 sane_exit (void)
2729 {
2730 Epson_Device *dev, *next;
2731
2732 for (dev = first_dev; dev; dev = next)
2733 {
2734 next = dev->next;
2735 free ((void *) dev->sane.name);
2736 free ((void *) dev->sane.model);
2737 free (dev);
2738 }
2739
2740 free (devlist);
2741 }
2742
2743 /*
2744 *
2745 *
2746 */
2747
2748 SANE_Status
sane_get_devices(const SANE_Device * **device_list, SANE_Bool local_only)2749 sane_get_devices (const SANE_Device * **device_list, SANE_Bool local_only)
2750 {
2751 Epson_Device *dev;
2752 int i;
2753
2754 DBG (5, "sane_get_devices()\n");
2755
2756 (void) local_only; /* just to get rid of the compiler warning */
2757
2758 if (devlist)
2759 {
2760 free (devlist);
2761 }
2762
2763 devlist = malloc ((num_devices + 1) * sizeof (devlist[0]));
2764 if (!devlist)
2765 {
2766 DBG (1, "out of memory (line %d)\n", __LINE__);
2767 return SANE_STATUS_NO_MEM;
2768 }
2769
2770 i = 0;
2771
2772 for (dev = first_dev; i < num_devices; dev = dev->next)
2773 {
2774 devlist[i++] = &dev->sane;
2775 }
2776
2777 devlist[i++] = 0;
2778
2779 *device_list = devlist;
2780
2781 return SANE_STATUS_GOOD;
2782 }
2783
2784 /*
2785 *
2786 *
2787 */
2788
2789 static SANE_Status
init_options(Epson_Scanner * s)2790 init_options (Epson_Scanner * s)
2791 {
2792 int i;
2793 SANE_Bool dummy;
2794
2795 DBG (5, "init_options()\n");
2796
2797 for (i = 0; i < NUM_OPTIONS; ++i)
2798 {
2799 s->opt[i].size = sizeof (SANE_Word);
2800 s->opt[i].cap = SANE_CAP_SOFT_SELECT | SANE_CAP_SOFT_DETECT;
2801 }
2802
2803 s->opt[OPT_NUM_OPTS].title = SANE_TITLE_NUM_OPTIONS;
2804 s->opt[OPT_NUM_OPTS].desc = SANE_DESC_NUM_OPTIONS;
2805 s->opt[OPT_NUM_OPTS].type = SANE_TYPE_INT;
2806 s->opt[OPT_NUM_OPTS].cap = SANE_CAP_SOFT_DETECT;
2807 s->val[OPT_NUM_OPTS].w = NUM_OPTIONS;
2808
2809 /* "Scan Mode" group: */
2810
2811 s->opt[OPT_MODE_GROUP].title = SANE_I18N ("Scan Mode");
2812 s->opt[OPT_MODE_GROUP].desc = "";
2813 s->opt[OPT_MODE_GROUP].type = SANE_TYPE_GROUP;
2814 s->opt[OPT_MODE_GROUP].cap = 0;
2815
2816 /* scan mode */
2817 s->opt[OPT_MODE].name = SANE_NAME_SCAN_MODE;
2818 s->opt[OPT_MODE].title = SANE_TITLE_SCAN_MODE;
2819 s->opt[OPT_MODE].desc = SANE_DESC_SCAN_MODE;
2820 s->opt[OPT_MODE].type = SANE_TYPE_STRING;
2821 s->opt[OPT_MODE].size = max_string_size (mode_list);
2822 s->opt[OPT_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2823 s->opt[OPT_MODE].constraint.string_list = mode_list;
2824 s->val[OPT_MODE].w = 0; /* Binary */
2825
2826 /* bit depth */
2827 s->opt[OPT_BIT_DEPTH].name = SANE_NAME_BIT_DEPTH;
2828 s->opt[OPT_BIT_DEPTH].title = SANE_TITLE_BIT_DEPTH;
2829 s->opt[OPT_BIT_DEPTH].desc = SANE_DESC_BIT_DEPTH;
2830 s->opt[OPT_BIT_DEPTH].type = SANE_TYPE_INT;
2831 s->opt[OPT_BIT_DEPTH].unit = SANE_UNIT_NONE;
2832 s->opt[OPT_BIT_DEPTH].constraint_type = SANE_CONSTRAINT_WORD_LIST;
2833 s->opt[OPT_BIT_DEPTH].constraint.word_list = bitDepthList;
2834 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
2835 s->val[OPT_BIT_DEPTH].w = bitDepthList[1]; /* the first "real" element is the default */
2836
2837 if (bitDepthList[0] == 1) /* only one element in the list -> hide the option */
2838 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
2839
2840 /* halftone */
2841 s->opt[OPT_HALFTONE].name = SANE_NAME_HALFTONE;
2842 s->opt[OPT_HALFTONE].title = SANE_TITLE_HALFTONE;
2843 s->opt[OPT_HALFTONE].desc = SANE_I18N ("Selects the halftone.");
2844
2845 s->opt[OPT_HALFTONE].type = SANE_TYPE_STRING;
2846 s->opt[OPT_HALFTONE].size = max_string_size (halftone_list_7);
2847 s->opt[OPT_HALFTONE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2848
2849 if (s->hw->level >= 7)
2850 s->opt[OPT_HALFTONE].constraint.string_list = halftone_list_7;
2851 else if (s->hw->level >= 4)
2852 s->opt[OPT_HALFTONE].constraint.string_list = halftone_list_4;
2853 else
2854 s->opt[OPT_HALFTONE].constraint.string_list = halftone_list;
2855
2856 s->val[OPT_HALFTONE].w = 1; /* Halftone A */
2857
2858 if (!s->hw->cmd->set_halftoning)
2859 {
2860 s->opt[OPT_HALFTONE].cap |= SANE_CAP_INACTIVE;
2861 }
2862
2863 /* dropout */
2864 s->opt[OPT_DROPOUT].name = "dropout";
2865 s->opt[OPT_DROPOUT].title = SANE_I18N ("Dropout");
2866 s->opt[OPT_DROPOUT].desc = SANE_I18N ("Selects the dropout.");
2867
2868 s->opt[OPT_DROPOUT].type = SANE_TYPE_STRING;
2869 s->opt[OPT_DROPOUT].size = max_string_size (dropout_list);
2870 s->opt[OPT_DROPOUT].cap |= SANE_CAP_ADVANCED;
2871 s->opt[OPT_DROPOUT].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2872 s->opt[OPT_DROPOUT].constraint.string_list = dropout_list;
2873 s->val[OPT_DROPOUT].w = 0; /* None */
2874
2875 /* brightness */
2876 s->opt[OPT_BRIGHTNESS].name = SANE_NAME_BRIGHTNESS;
2877 s->opt[OPT_BRIGHTNESS].title = SANE_TITLE_BRIGHTNESS;
2878 s->opt[OPT_BRIGHTNESS].desc = SANE_I18N ("Selects the brightness.");
2879
2880 s->opt[OPT_BRIGHTNESS].type = SANE_TYPE_INT;
2881 s->opt[OPT_BRIGHTNESS].unit = SANE_UNIT_NONE;
2882 s->opt[OPT_BRIGHTNESS].constraint_type = SANE_CONSTRAINT_RANGE;
2883 s->opt[OPT_BRIGHTNESS].constraint.range = &s->hw->cmd->bright_range;
2884 s->val[OPT_BRIGHTNESS].w = 0; /* Normal */
2885
2886 if (!s->hw->cmd->set_bright)
2887 {
2888 s->opt[OPT_BRIGHTNESS].cap |= SANE_CAP_INACTIVE;
2889 }
2890
2891 /* sharpness */
2892 s->opt[OPT_SHARPNESS].name = "sharpness";
2893 s->opt[OPT_SHARPNESS].title = SANE_I18N ("Sharpness");
2894 s->opt[OPT_SHARPNESS].desc = "";
2895
2896 s->opt[OPT_SHARPNESS].type = SANE_TYPE_INT;
2897 s->opt[OPT_SHARPNESS].unit = SANE_UNIT_NONE;
2898 s->opt[OPT_SHARPNESS].constraint_type = SANE_CONSTRAINT_RANGE;
2899 s->opt[OPT_SHARPNESS].constraint.range = &outline_emphasis_range;
2900 s->val[OPT_SHARPNESS].w = 0; /* Normal */
2901
2902 if (!s->hw->cmd->set_outline_emphasis)
2903 {
2904 s->opt[OPT_SHARPNESS].cap |= SANE_CAP_INACTIVE;
2905 }
2906
2907
2908 /* gamma */
2909 s->opt[OPT_GAMMA_CORRECTION].name = SANE_NAME_GAMMA_CORRECTION;
2910 s->opt[OPT_GAMMA_CORRECTION].title = SANE_TITLE_GAMMA_CORRECTION;
2911 s->opt[OPT_GAMMA_CORRECTION].desc = SANE_DESC_GAMMA_CORRECTION;
2912
2913 s->opt[OPT_GAMMA_CORRECTION].type = SANE_TYPE_STRING;
2914 s->opt[OPT_GAMMA_CORRECTION].constraint_type = SANE_CONSTRAINT_STRING_LIST;
2915 /*
2916 * special handling for D1 function level - at this time I'm not
2917 * testing for D1, I'm just assuming that all D level scanners will
2918 * behave the same way. This has to be confirmed with the next D-level
2919 * scanner
2920 */
2921 if (s->hw->cmd->level[0] == 'D')
2922 {
2923 s->opt[OPT_GAMMA_CORRECTION].size = max_string_size (gamma_list_d);
2924 s->opt[OPT_GAMMA_CORRECTION].constraint.string_list = gamma_list_d;
2925 s->val[OPT_GAMMA_CORRECTION].w = 1; /* Default */
2926 gamma_userdefined = gamma_userdefined_d;
2927 gamma_params = gamma_params_d;
2928 }
2929 else
2930 {
2931 s->opt[OPT_GAMMA_CORRECTION].size = max_string_size (gamma_list_ab);
2932 s->opt[OPT_GAMMA_CORRECTION].constraint.string_list = gamma_list_ab;
2933 s->val[OPT_GAMMA_CORRECTION].w = 0; /* Default */
2934 gamma_userdefined = gamma_userdefined_ab;
2935 gamma_params = gamma_params_ab;
2936 }
2937
2938 if (!s->hw->cmd->set_gamma)
2939 {
2940 s->opt[OPT_GAMMA_CORRECTION].cap |= SANE_CAP_INACTIVE;
2941 }
2942
2943
2944 /* gamma vector */
2945 /*
2946 s->opt[ OPT_GAMMA_VECTOR].name = SANE_NAME_GAMMA_VECTOR;
2947 s->opt[ OPT_GAMMA_VECTOR].title = SANE_TITLE_GAMMA_VECTOR;
2948 s->opt[ OPT_GAMMA_VECTOR].desc = SANE_DESC_GAMMA_VECTOR;
2949
2950 s->opt[ OPT_GAMMA_VECTOR].type = SANE_TYPE_INT;
2951 s->opt[ OPT_GAMMA_VECTOR].unit = SANE_UNIT_NONE;
2952 s->opt[ OPT_GAMMA_VECTOR].size = 256 * sizeof (SANE_Word);
2953 s->opt[ OPT_GAMMA_VECTOR].constraint_type = SANE_CONSTRAINT_RANGE;
2954 s->opt[ OPT_GAMMA_VECTOR].constraint.range = &u8_range;
2955 s->val[ OPT_GAMMA_VECTOR].wa = &s->gamma_table [ 0] [ 0];
2956 */
2957
2958
2959 /* red gamma vector */
2960 s->opt[OPT_GAMMA_VECTOR_R].name = SANE_NAME_GAMMA_VECTOR_R;
2961 s->opt[OPT_GAMMA_VECTOR_R].title = SANE_TITLE_GAMMA_VECTOR_R;
2962 s->opt[OPT_GAMMA_VECTOR_R].desc = SANE_DESC_GAMMA_VECTOR_R;
2963
2964 s->opt[OPT_GAMMA_VECTOR_R].type = SANE_TYPE_INT;
2965 s->opt[OPT_GAMMA_VECTOR_R].unit = SANE_UNIT_NONE;
2966 s->opt[OPT_GAMMA_VECTOR_R].size = 256 * sizeof (SANE_Word);
2967 s->opt[OPT_GAMMA_VECTOR_R].constraint_type = SANE_CONSTRAINT_RANGE;
2968 s->opt[OPT_GAMMA_VECTOR_R].constraint.range = &u8_range;
2969 s->val[OPT_GAMMA_VECTOR_R].wa = &s->gamma_table[0][0];
2970
2971
2972 /* green gamma vector */
2973 s->opt[OPT_GAMMA_VECTOR_G].name = SANE_NAME_GAMMA_VECTOR_G;
2974 s->opt[OPT_GAMMA_VECTOR_G].title = SANE_TITLE_GAMMA_VECTOR_G;
2975 s->opt[OPT_GAMMA_VECTOR_G].desc = SANE_DESC_GAMMA_VECTOR_G;
2976
2977 s->opt[OPT_GAMMA_VECTOR_G].type = SANE_TYPE_INT;
2978 s->opt[OPT_GAMMA_VECTOR_G].unit = SANE_UNIT_NONE;
2979 s->opt[OPT_GAMMA_VECTOR_G].size = 256 * sizeof (SANE_Word);
2980 s->opt[OPT_GAMMA_VECTOR_G].constraint_type = SANE_CONSTRAINT_RANGE;
2981 s->opt[OPT_GAMMA_VECTOR_G].constraint.range = &u8_range;
2982 s->val[OPT_GAMMA_VECTOR_G].wa = &s->gamma_table[1][0];
2983
2984
2985 /* red gamma vector */
2986 s->opt[OPT_GAMMA_VECTOR_B].name = SANE_NAME_GAMMA_VECTOR_B;
2987 s->opt[OPT_GAMMA_VECTOR_B].title = SANE_TITLE_GAMMA_VECTOR_B;
2988 s->opt[OPT_GAMMA_VECTOR_B].desc = SANE_DESC_GAMMA_VECTOR_B;
2989
2990 s->opt[OPT_GAMMA_VECTOR_B].type = SANE_TYPE_INT;
2991 s->opt[OPT_GAMMA_VECTOR_B].unit = SANE_UNIT_NONE;
2992 s->opt[OPT_GAMMA_VECTOR_B].size = 256 * sizeof (SANE_Word);
2993 s->opt[OPT_GAMMA_VECTOR_B].constraint_type = SANE_CONSTRAINT_RANGE;
2994 s->opt[OPT_GAMMA_VECTOR_B].constraint.range = &u8_range;
2995 s->val[OPT_GAMMA_VECTOR_B].wa = &s->gamma_table[2][0];
2996
2997 if (s->hw->cmd->set_gamma_table &&
2998 gamma_userdefined[s->val[OPT_GAMMA_CORRECTION].w] == SANE_TRUE)
2999 {
3000 /* s->opt[ OPT_GAMMA_VECTOR].cap &= ~SANE_CAP_INACTIVE; */
3001 s->opt[OPT_GAMMA_VECTOR_R].cap &= ~SANE_CAP_INACTIVE;
3002 s->opt[OPT_GAMMA_VECTOR_G].cap &= ~SANE_CAP_INACTIVE;
3003 s->opt[OPT_GAMMA_VECTOR_B].cap &= ~SANE_CAP_INACTIVE;
3004 }
3005 else
3006 {
3007 /* s->opt[ OPT_GAMMA_VECTOR].cap |= SANE_CAP_INACTIVE; */
3008 s->opt[OPT_GAMMA_VECTOR_R].cap |= SANE_CAP_INACTIVE;
3009 s->opt[OPT_GAMMA_VECTOR_G].cap |= SANE_CAP_INACTIVE;
3010 s->opt[OPT_GAMMA_VECTOR_B].cap |= SANE_CAP_INACTIVE;
3011 }
3012
3013 /* initialize the Gamma tables */
3014 memset (&s->gamma_table[0], 0, 256 * sizeof (SANE_Word));
3015 memset (&s->gamma_table[1], 0, 256 * sizeof (SANE_Word));
3016 memset (&s->gamma_table[2], 0, 256 * sizeof (SANE_Word));
3017 /* memset(&s->gamma_table[3], 0, 256 * sizeof(SANE_Word)); */
3018 for (i = 0; i < 256; i++)
3019 {
3020 s->gamma_table[0][i] = i;
3021 s->gamma_table[1][i] = i;
3022 s->gamma_table[2][i] = i;
3023 /* s->gamma_table[3][i] = i; */
3024 }
3025
3026
3027 /* color correction */
3028 s->opt[OPT_COLOR_CORRECTION].name = "color-correction";
3029 s->opt[OPT_COLOR_CORRECTION].title = SANE_I18N ("Color correction");
3030 s->opt[OPT_COLOR_CORRECTION].desc =
3031 SANE_I18N
3032 ("Sets the color correction table for the selected output device.");
3033
3034 s->opt[OPT_COLOR_CORRECTION].type = SANE_TYPE_STRING;
3035 s->opt[OPT_COLOR_CORRECTION].size = 32;
3036 s->opt[OPT_COLOR_CORRECTION].cap |= SANE_CAP_ADVANCED;
3037 s->opt[OPT_COLOR_CORRECTION].constraint_type = SANE_CONSTRAINT_STRING_LIST;
3038 s->opt[OPT_COLOR_CORRECTION].constraint.string_list = color_list;
3039 s->val[OPT_COLOR_CORRECTION].w = 5; /* scanner default: CRT monitors */
3040
3041 if (!s->hw->cmd->set_color_correction)
3042 {
3043 s->opt[OPT_COLOR_CORRECTION].cap |= SANE_CAP_INACTIVE;
3044 }
3045
3046 /* resolution */
3047 s->opt[OPT_RESOLUTION].name = SANE_NAME_SCAN_RESOLUTION;
3048 s->opt[OPT_RESOLUTION].title = SANE_TITLE_SCAN_RESOLUTION;
3049 s->opt[OPT_RESOLUTION].desc = SANE_DESC_SCAN_RESOLUTION;
3050
3051 s->opt[OPT_RESOLUTION].type = SANE_TYPE_INT;
3052 s->opt[OPT_RESOLUTION].unit = SANE_UNIT_DPI;
3053 s->opt[OPT_RESOLUTION].constraint_type = SANE_CONSTRAINT_WORD_LIST;
3054 s->opt[OPT_RESOLUTION].constraint.word_list = s->hw->resolution_list;
3055 s->val[OPT_RESOLUTION].w = s->hw->dpi_range.min;
3056
3057 /* threshold */
3058 s->opt[OPT_THRESHOLD].name = SANE_NAME_THRESHOLD;
3059 s->opt[OPT_THRESHOLD].title = SANE_TITLE_THRESHOLD;
3060 s->opt[OPT_THRESHOLD].desc = SANE_DESC_THRESHOLD;
3061
3062 s->opt[OPT_THRESHOLD].type = SANE_TYPE_INT;
3063 s->opt[OPT_THRESHOLD].unit = SANE_UNIT_NONE;
3064 s->opt[OPT_THRESHOLD].constraint_type = SANE_CONSTRAINT_RANGE;
3065 s->opt[OPT_THRESHOLD].constraint.range = &u8_range;
3066 s->val[OPT_THRESHOLD].w = 0x80;
3067
3068 if (!s->hw->cmd->set_threshold)
3069 {
3070 s->opt[OPT_THRESHOLD].cap |= SANE_CAP_INACTIVE;
3071 }
3072
3073 s->opt[OPT_CCT_GROUP].title = SANE_I18N ("Color correction coefficients");
3074 s->opt[OPT_CCT_GROUP].desc = SANE_I18N ("Matrix multiplication of RGB");
3075 s->opt[OPT_CCT_GROUP].type = SANE_TYPE_GROUP;
3076 s->opt[OPT_CCT_GROUP].cap = SANE_CAP_ADVANCED;
3077
3078
3079 /* color correction coefficients */
3080 s->opt[OPT_CCT_1].name = "cct-1";
3081 s->opt[OPT_CCT_2].name = "cct-2";
3082 s->opt[OPT_CCT_3].name = "cct-3";
3083 s->opt[OPT_CCT_4].name = "cct-4";
3084 s->opt[OPT_CCT_5].name = "cct-5";
3085 s->opt[OPT_CCT_6].name = "cct-6";
3086 s->opt[OPT_CCT_7].name = "cct-7";
3087 s->opt[OPT_CCT_8].name = "cct-8";
3088 s->opt[OPT_CCT_9].name = "cct-9";
3089
3090 s->opt[OPT_CCT_1].title = SANE_I18N ("Green");
3091 s->opt[OPT_CCT_2].title = SANE_I18N ("Shift green to red");
3092 s->opt[OPT_CCT_3].title = SANE_I18N ("Shift green to blue");
3093 s->opt[OPT_CCT_4].title = SANE_I18N ("Shift red to green");
3094 s->opt[OPT_CCT_5].title = SANE_I18N ("Red");
3095 s->opt[OPT_CCT_6].title = SANE_I18N ("Shift red to blue");
3096 s->opt[OPT_CCT_7].title = SANE_I18N ("Shift blue to green");
3097 s->opt[OPT_CCT_8].title = SANE_I18N ("Shift blue to red");
3098 s->opt[OPT_CCT_9].title = SANE_I18N ("Blue");
3099
3100 s->opt[OPT_CCT_1].desc = SANE_I18N ("Controls green level");
3101 s->opt[OPT_CCT_2].desc = SANE_I18N ("Adds to red based on green level");
3102 s->opt[OPT_CCT_3].desc = SANE_I18N ("Adds to blue based on green level");
3103 s->opt[OPT_CCT_4].desc = SANE_I18N ("Adds to green based on red level");
3104 s->opt[OPT_CCT_5].desc = SANE_I18N ("Controls red level");
3105 s->opt[OPT_CCT_6].desc = SANE_I18N ("Adds to blue based on red level");
3106 s->opt[OPT_CCT_7].desc = SANE_I18N ("Adds to green based on blue level");
3107 s->opt[OPT_CCT_8].desc = SANE_I18N ("Adds to red based on blue level");
3108 s->opt[OPT_CCT_9].desc = SANE_I18N ("Controls blue level");
3109
3110 s->opt[OPT_CCT_1].type = SANE_TYPE_INT;
3111 s->opt[OPT_CCT_2].type = SANE_TYPE_INT;
3112 s->opt[OPT_CCT_3].type = SANE_TYPE_INT;
3113 s->opt[OPT_CCT_4].type = SANE_TYPE_INT;
3114 s->opt[OPT_CCT_5].type = SANE_TYPE_INT;
3115 s->opt[OPT_CCT_6].type = SANE_TYPE_INT;
3116 s->opt[OPT_CCT_7].type = SANE_TYPE_INT;
3117 s->opt[OPT_CCT_8].type = SANE_TYPE_INT;
3118 s->opt[OPT_CCT_9].type = SANE_TYPE_INT;
3119
3120 s->opt[OPT_CCT_1].cap |= SANE_CAP_ADVANCED;
3121 s->opt[OPT_CCT_2].cap |= SANE_CAP_ADVANCED;
3122 s->opt[OPT_CCT_3].cap |= SANE_CAP_ADVANCED;
3123 s->opt[OPT_CCT_4].cap |= SANE_CAP_ADVANCED;
3124 s->opt[OPT_CCT_5].cap |= SANE_CAP_ADVANCED;
3125 s->opt[OPT_CCT_6].cap |= SANE_CAP_ADVANCED;
3126 s->opt[OPT_CCT_7].cap |= SANE_CAP_ADVANCED;
3127 s->opt[OPT_CCT_8].cap |= SANE_CAP_ADVANCED;
3128 s->opt[OPT_CCT_9].cap |= SANE_CAP_ADVANCED;
3129
3130 s->opt[OPT_CCT_1].cap |= SANE_CAP_INACTIVE;
3131 s->opt[OPT_CCT_2].cap |= SANE_CAP_INACTIVE;
3132 s->opt[OPT_CCT_3].cap |= SANE_CAP_INACTIVE;
3133 s->opt[OPT_CCT_4].cap |= SANE_CAP_INACTIVE;
3134 s->opt[OPT_CCT_5].cap |= SANE_CAP_INACTIVE;
3135 s->opt[OPT_CCT_6].cap |= SANE_CAP_INACTIVE;
3136 s->opt[OPT_CCT_7].cap |= SANE_CAP_INACTIVE;
3137 s->opt[OPT_CCT_8].cap |= SANE_CAP_INACTIVE;
3138 s->opt[OPT_CCT_9].cap |= SANE_CAP_INACTIVE;
3139
3140 s->opt[OPT_CCT_1].unit = SANE_UNIT_NONE;
3141 s->opt[OPT_CCT_2].unit = SANE_UNIT_NONE;
3142 s->opt[OPT_CCT_3].unit = SANE_UNIT_NONE;
3143 s->opt[OPT_CCT_4].unit = SANE_UNIT_NONE;
3144 s->opt[OPT_CCT_5].unit = SANE_UNIT_NONE;
3145 s->opt[OPT_CCT_6].unit = SANE_UNIT_NONE;
3146 s->opt[OPT_CCT_7].unit = SANE_UNIT_NONE;
3147 s->opt[OPT_CCT_8].unit = SANE_UNIT_NONE;
3148 s->opt[OPT_CCT_9].unit = SANE_UNIT_NONE;
3149
3150 s->opt[OPT_CCT_1].constraint_type = SANE_CONSTRAINT_RANGE;
3151 s->opt[OPT_CCT_2].constraint_type = SANE_CONSTRAINT_RANGE;
3152 s->opt[OPT_CCT_3].constraint_type = SANE_CONSTRAINT_RANGE;
3153 s->opt[OPT_CCT_4].constraint_type = SANE_CONSTRAINT_RANGE;
3154 s->opt[OPT_CCT_5].constraint_type = SANE_CONSTRAINT_RANGE;
3155 s->opt[OPT_CCT_6].constraint_type = SANE_CONSTRAINT_RANGE;
3156 s->opt[OPT_CCT_7].constraint_type = SANE_CONSTRAINT_RANGE;
3157 s->opt[OPT_CCT_8].constraint_type = SANE_CONSTRAINT_RANGE;
3158 s->opt[OPT_CCT_9].constraint_type = SANE_CONSTRAINT_RANGE;
3159
3160 s->opt[OPT_CCT_1].constraint.range = &s8_range;
3161 s->opt[OPT_CCT_2].constraint.range = &s8_range;
3162 s->opt[OPT_CCT_3].constraint.range = &s8_range;
3163 s->opt[OPT_CCT_4].constraint.range = &s8_range;
3164 s->opt[OPT_CCT_5].constraint.range = &s8_range;
3165 s->opt[OPT_CCT_6].constraint.range = &s8_range;
3166 s->opt[OPT_CCT_7].constraint.range = &s8_range;
3167 s->opt[OPT_CCT_8].constraint.range = &s8_range;
3168 s->opt[OPT_CCT_9].constraint.range = &s8_range;
3169
3170 s->val[OPT_CCT_1].w = 32;
3171 s->val[OPT_CCT_2].w = 0;
3172 s->val[OPT_CCT_3].w = 0;
3173 s->val[OPT_CCT_4].w = 0;
3174 s->val[OPT_CCT_5].w = 32;
3175 s->val[OPT_CCT_6].w = 0;
3176 s->val[OPT_CCT_7].w = 0;
3177 s->val[OPT_CCT_8].w = 0;
3178 s->val[OPT_CCT_9].w = 32;
3179
3180 if (!s->hw->cmd->set_color_correction_coefficients)
3181 {
3182 s->opt[OPT_CCT_1].cap |= SANE_CAP_INACTIVE;
3183 s->opt[OPT_CCT_2].cap |= SANE_CAP_INACTIVE;
3184 s->opt[OPT_CCT_3].cap |= SANE_CAP_INACTIVE;
3185 s->opt[OPT_CCT_4].cap |= SANE_CAP_INACTIVE;
3186 s->opt[OPT_CCT_5].cap |= SANE_CAP_INACTIVE;
3187 s->opt[OPT_CCT_6].cap |= SANE_CAP_INACTIVE;
3188 s->opt[OPT_CCT_7].cap |= SANE_CAP_INACTIVE;
3189 s->opt[OPT_CCT_8].cap |= SANE_CAP_INACTIVE;
3190 s->opt[OPT_CCT_9].cap |= SANE_CAP_INACTIVE;
3191 }
3192
3193
3194 /* "Advanced" group: */
3195 s->opt[OPT_ADVANCED_GROUP].title = SANE_I18N ("Advanced");
3196 s->opt[OPT_ADVANCED_GROUP].desc = "";
3197 s->opt[OPT_ADVANCED_GROUP].type = SANE_TYPE_GROUP;
3198 s->opt[OPT_ADVANCED_GROUP].cap = SANE_CAP_ADVANCED;
3199
3200
3201 /* mirror */
3202 s->opt[OPT_MIRROR].name = "mirror";
3203 s->opt[OPT_MIRROR].title = SANE_I18N ("Mirror image");
3204 s->opt[OPT_MIRROR].desc = SANE_I18N ("Mirror the image.");
3205
3206 s->opt[OPT_MIRROR].type = SANE_TYPE_BOOL;
3207 s->val[OPT_MIRROR].w = SANE_FALSE;
3208
3209 if (!s->hw->cmd->mirror_image)
3210 {
3211 s->opt[OPT_MIRROR].cap |= SANE_CAP_INACTIVE;
3212 }
3213
3214
3215 /* speed */
3216 s->opt[OPT_SPEED].name = SANE_NAME_SCAN_SPEED;
3217 s->opt[OPT_SPEED].title = SANE_TITLE_SCAN_SPEED;
3218 s->opt[OPT_SPEED].desc = SANE_DESC_SCAN_SPEED;
3219
3220 s->opt[OPT_SPEED].type = SANE_TYPE_BOOL;
3221 s->val[OPT_SPEED].w = SANE_FALSE;
3222
3223 if (!s->hw->cmd->set_speed)
3224 {
3225 s->opt[OPT_SPEED].cap |= SANE_CAP_INACTIVE;
3226 }
3227
3228 /* preview speed */
3229 s->opt[OPT_PREVIEW_SPEED].name = "preview-speed";
3230 s->opt[OPT_PREVIEW_SPEED].title = SANE_I18N ("Fast preview");
3231 s->opt[OPT_PREVIEW_SPEED].desc = "";
3232
3233 s->opt[OPT_PREVIEW_SPEED].type = SANE_TYPE_BOOL;
3234 s->val[OPT_PREVIEW_SPEED].w = SANE_FALSE;
3235
3236 if (!s->hw->cmd->set_speed)
3237 {
3238 s->opt[OPT_PREVIEW_SPEED].cap |= SANE_CAP_INACTIVE;
3239 }
3240
3241 /* auto area segmentation */
3242 s->opt[OPT_AAS].name = "auto-area-segmentation";
3243 s->opt[OPT_AAS].title = SANE_I18N ("Auto area segmentation");
3244 s->opt[OPT_AAS].desc = "";
3245
3246 s->opt[OPT_AAS].type = SANE_TYPE_BOOL;
3247 s->val[OPT_AAS].w = SANE_TRUE;
3248
3249 if (!s->hw->cmd->control_auto_area_segmentation)
3250 {
3251 s->opt[OPT_AAS].cap |= SANE_CAP_INACTIVE;
3252 }
3253
3254 /* limit resolution list */
3255 s->opt[OPT_LIMIT_RESOLUTION].name = "short-resolution";
3256 s->opt[OPT_LIMIT_RESOLUTION].title = SANE_I18N ("Short resolution list");
3257 s->opt[OPT_LIMIT_RESOLUTION].desc =
3258 SANE_I18N ("Display short resolution list");
3259 s->opt[OPT_LIMIT_RESOLUTION].type = SANE_TYPE_BOOL;
3260 s->val[OPT_LIMIT_RESOLUTION].w = SANE_FALSE;
3261
3262
3263 /* zoom */
3264 s->opt[OPT_ZOOM].name = "zoom";
3265 s->opt[OPT_ZOOM].title = SANE_I18N ("Zoom");
3266 s->opt[OPT_ZOOM].desc =
3267 SANE_I18N ("Defines the zoom factor the scanner will use");
3268
3269 s->opt[OPT_ZOOM].type = SANE_TYPE_INT;
3270 s->opt[OPT_ZOOM].unit = SANE_UNIT_NONE;
3271 s->opt[OPT_ZOOM].constraint_type = SANE_CONSTRAINT_RANGE;
3272 s->opt[OPT_ZOOM].constraint.range = &zoom_range;
3273 s->val[OPT_ZOOM].w = 100;
3274
3275 /* if( ! s->hw->cmd->set_zoom) */
3276 {
3277 s->opt[OPT_ZOOM].cap |= SANE_CAP_INACTIVE;
3278 }
3279
3280
3281 /* "Preview settings" group: */
3282 s->opt[OPT_PREVIEW_GROUP].title = SANE_TITLE_PREVIEW;
3283 s->opt[OPT_PREVIEW_GROUP].desc = "";
3284 s->opt[OPT_PREVIEW_GROUP].type = SANE_TYPE_GROUP;
3285 s->opt[OPT_PREVIEW_GROUP].cap = SANE_CAP_ADVANCED;
3286
3287 /* preview */
3288 s->opt[OPT_PREVIEW].name = SANE_NAME_PREVIEW;
3289 s->opt[OPT_PREVIEW].title = SANE_TITLE_PREVIEW;
3290 s->opt[OPT_PREVIEW].desc = SANE_DESC_PREVIEW;
3291
3292 s->opt[OPT_PREVIEW].type = SANE_TYPE_BOOL;
3293 s->val[OPT_PREVIEW].w = SANE_FALSE;
3294
3295 /* "Geometry" group: */
3296 s->opt[OPT_GEOMETRY_GROUP].title = SANE_I18N ("Geometry");
3297 s->opt[OPT_GEOMETRY_GROUP].desc = "";
3298 s->opt[OPT_GEOMETRY_GROUP].type = SANE_TYPE_GROUP;
3299 s->opt[OPT_GEOMETRY_GROUP].cap = SANE_CAP_ADVANCED;
3300
3301 /* top-left x */
3302 s->opt[OPT_TL_X].name = SANE_NAME_SCAN_TL_X;
3303 s->opt[OPT_TL_X].title = SANE_TITLE_SCAN_TL_X;
3304 s->opt[OPT_TL_X].desc = SANE_DESC_SCAN_TL_X;
3305
3306 s->opt[OPT_TL_X].type = SANE_TYPE_FIXED;
3307 s->opt[OPT_TL_X].unit = SANE_UNIT_MM;
3308 s->opt[OPT_TL_X].constraint_type = SANE_CONSTRAINT_RANGE;
3309 s->opt[OPT_TL_X].constraint.range = s->hw->x_range;
3310 s->val[OPT_TL_X].w = 0;
3311
3312 /* top-left y */
3313 s->opt[OPT_TL_Y].name = SANE_NAME_SCAN_TL_Y;
3314 s->opt[OPT_TL_Y].title = SANE_TITLE_SCAN_TL_Y;
3315 s->opt[OPT_TL_Y].desc = SANE_DESC_SCAN_TL_Y;
3316
3317 s->opt[OPT_TL_Y].type = SANE_TYPE_FIXED;
3318 s->opt[OPT_TL_Y].unit = SANE_UNIT_MM;
3319 s->opt[OPT_TL_Y].constraint_type = SANE_CONSTRAINT_RANGE;
3320 s->opt[OPT_TL_Y].constraint.range = s->hw->y_range;
3321 s->val[OPT_TL_Y].w = 0;
3322
3323 /* bottom-right x */
3324 s->opt[OPT_BR_X].name = SANE_NAME_SCAN_BR_X;
3325 s->opt[OPT_BR_X].title = SANE_TITLE_SCAN_BR_X;
3326 s->opt[OPT_BR_X].desc = SANE_DESC_SCAN_BR_X;
3327
3328 s->opt[OPT_BR_X].type = SANE_TYPE_FIXED;
3329 s->opt[OPT_BR_X].unit = SANE_UNIT_MM;
3330 s->opt[OPT_BR_X].constraint_type = SANE_CONSTRAINT_RANGE;
3331 s->opt[OPT_BR_X].constraint.range = s->hw->x_range;
3332 s->val[OPT_BR_X].w = s->hw->x_range->max;
3333
3334 /* bottom-right y */
3335 s->opt[OPT_BR_Y].name = SANE_NAME_SCAN_BR_Y;
3336 s->opt[OPT_BR_Y].title = SANE_TITLE_SCAN_BR_Y;
3337 s->opt[OPT_BR_Y].desc = SANE_DESC_SCAN_BR_Y;
3338
3339 s->opt[OPT_BR_Y].type = SANE_TYPE_FIXED;
3340 s->opt[OPT_BR_Y].unit = SANE_UNIT_MM;
3341 s->opt[OPT_BR_Y].constraint_type = SANE_CONSTRAINT_RANGE;
3342 s->opt[OPT_BR_Y].constraint.range = s->hw->y_range;
3343 s->val[OPT_BR_Y].w = s->hw->y_range->max;
3344
3345 /* Quick format */
3346 s->opt[OPT_QUICK_FORMAT].name = "quick-format";
3347 s->opt[OPT_QUICK_FORMAT].title = SANE_I18N ("Quick format");
3348 s->opt[OPT_QUICK_FORMAT].desc = "";
3349
3350 s->opt[OPT_QUICK_FORMAT].type = SANE_TYPE_STRING;
3351 s->opt[OPT_QUICK_FORMAT].size = max_string_size (qf_list);
3352 s->opt[OPT_QUICK_FORMAT].cap |= SANE_CAP_ADVANCED;
3353 s->opt[OPT_QUICK_FORMAT].constraint_type = SANE_CONSTRAINT_STRING_LIST;
3354 s->opt[OPT_QUICK_FORMAT].constraint.string_list = qf_list;
3355 s->val[OPT_QUICK_FORMAT].w = XtNumber (qf_params) - 1; /* max */
3356
3357 /* "Optional equipment" group: */
3358 s->opt[OPT_EQU_GROUP].title = SANE_I18N ("Optional equipment");
3359 s->opt[OPT_EQU_GROUP].desc = "";
3360 s->opt[OPT_EQU_GROUP].type = SANE_TYPE_GROUP;
3361 s->opt[OPT_EQU_GROUP].cap = SANE_CAP_ADVANCED;
3362
3363
3364 /* source */
3365 s->opt[OPT_SOURCE].name = SANE_NAME_SCAN_SOURCE;
3366 s->opt[OPT_SOURCE].title = SANE_TITLE_SCAN_SOURCE;
3367 s->opt[OPT_SOURCE].desc = SANE_DESC_SCAN_SOURCE;
3368
3369 s->opt[OPT_SOURCE].type = SANE_TYPE_STRING;
3370 s->opt[OPT_SOURCE].size = max_string_size (source_list);
3371
3372 s->opt[OPT_SOURCE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
3373 s->opt[OPT_SOURCE].constraint.string_list = source_list;
3374
3375 if (!s->hw->extension)
3376 {
3377 s->opt[OPT_SOURCE].cap |= SANE_CAP_INACTIVE;
3378 }
3379 s->val[OPT_SOURCE].w = 0; /* always use Flatbed as default */
3380
3381
3382 /* film type */
3383 s->opt[OPT_FILM_TYPE].name = "film-type";
3384 s->opt[OPT_FILM_TYPE].title = SANE_I18N ("Film type");
3385 s->opt[OPT_FILM_TYPE].desc = "";
3386
3387 s->opt[OPT_FILM_TYPE].type = SANE_TYPE_STRING;
3388 s->opt[OPT_FILM_TYPE].size = max_string_size (film_list);
3389
3390 s->opt[OPT_FILM_TYPE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
3391 s->opt[OPT_FILM_TYPE].constraint.string_list = film_list;
3392
3393 s->val[OPT_FILM_TYPE].w = 0;
3394
3395 deactivateOption (s, OPT_FILM_TYPE, &dummy); /* default is inactive */
3396
3397 /* focus position */
3398 s->opt[OPT_FOCUS].name = SANE_EPSON_FOCUS_NAME;
3399 s->opt[OPT_FOCUS].title = SANE_EPSON_FOCUS_TITLE;
3400 s->opt[OPT_FOCUS].desc = SANE_EPSON_FOCUS_DESC;
3401 s->opt[OPT_FOCUS].type = SANE_TYPE_STRING;
3402 s->opt[OPT_FOCUS].size = max_string_size (focus_list);
3403 s->opt[OPT_FOCUS].constraint_type = SANE_CONSTRAINT_STRING_LIST;
3404 s->opt[OPT_FOCUS].constraint.string_list = focus_list;
3405 s->val[OPT_FOCUS].w = 0;
3406
3407 s->opt[OPT_FOCUS].cap |= SANE_CAP_ADVANCED;
3408 if (s->hw->focusSupport == SANE_TRUE)
3409 {
3410 s->opt[OPT_FOCUS].cap &= ~SANE_CAP_INACTIVE;
3411 }
3412 else
3413 {
3414 s->opt[OPT_FOCUS].cap |= SANE_CAP_INACTIVE;
3415 }
3416
3417 #if 0
3418 if ((!s->hw->TPU) && (!s->hw->cmd->set_bay))
3419 { /* Hack: Using set_bay to indicate. */
3420 SANE_Bool dummy;
3421 deactivateOption (s, OPT_FILM_TYPE, &dummy);
3422
3423 }
3424 #endif
3425
3426
3427 /* forward feed / eject */
3428 s->opt[OPT_EJECT].name = "eject";
3429 s->opt[OPT_EJECT].title = SANE_I18N ("Eject");
3430 s->opt[OPT_EJECT].desc = SANE_I18N ("Eject the sheet in the ADF");
3431
3432 s->opt[OPT_EJECT].type = SANE_TYPE_BUTTON;
3433
3434 if ((!s->hw->ADF) && (!s->hw->cmd->set_bay))
3435 { /* Hack: Using set_bay to indicate. */
3436 s->opt[OPT_EJECT].cap |= SANE_CAP_INACTIVE;
3437 }
3438
3439
3440 /* auto forward feed / eject */
3441 s->opt[OPT_AUTO_EJECT].name = "auto-eject";
3442 s->opt[OPT_AUTO_EJECT].title = SANE_I18N ("Auto eject");
3443 s->opt[OPT_AUTO_EJECT].desc = SANE_I18N ("Eject document after scanning");
3444
3445 s->opt[OPT_AUTO_EJECT].type = SANE_TYPE_BOOL;
3446 s->val[OPT_AUTO_EJECT].w = SANE_FALSE;
3447
3448 if (!s->hw->ADF)
3449 {
3450 s->opt[OPT_AUTO_EJECT].cap |= SANE_CAP_INACTIVE;
3451 }
3452
3453
3454 s->opt[OPT_ADF_MODE].name = "adf_mode";
3455 s->opt[OPT_ADF_MODE].title = SANE_I18N ("ADF Mode");
3456 s->opt[OPT_ADF_MODE].desc =
3457 SANE_I18N ("Selects the ADF mode (simplex/duplex)");
3458 s->opt[OPT_ADF_MODE].type = SANE_TYPE_STRING;
3459 s->opt[OPT_ADF_MODE].size = max_string_size (adf_mode_list);
3460 s->opt[OPT_ADF_MODE].constraint_type = SANE_CONSTRAINT_STRING_LIST;
3461 s->opt[OPT_ADF_MODE].constraint.string_list = adf_mode_list;
3462 s->val[OPT_ADF_MODE].w = 0; /* simplex */
3463
3464 if ((!s->hw->ADF) || (s->hw->duplexSupport == SANE_FALSE))
3465 {
3466 s->opt[OPT_ADF_MODE].cap |= SANE_CAP_INACTIVE;
3467 }
3468
3469 /* select bay */
3470 s->opt[OPT_BAY].name = "bay";
3471 s->opt[OPT_BAY].title = SANE_I18N ("Bay");
3472 s->opt[OPT_BAY].desc = SANE_I18N ("Select bay to scan");
3473
3474 s->opt[OPT_BAY].type = SANE_TYPE_STRING;
3475 s->opt[OPT_BAY].size = max_string_size (bay_list);
3476 s->opt[OPT_BAY].constraint_type = SANE_CONSTRAINT_STRING_LIST;
3477 s->opt[OPT_BAY].constraint.string_list = bay_list;
3478 s->val[OPT_BAY].w = 0; /* Bay 1 */
3479
3480 if (!s->hw->cmd->set_bay)
3481 {
3482 s->opt[OPT_BAY].cap |= SANE_CAP_INACTIVE;
3483 }
3484
3485
3486 s->opt[OPT_WAIT_FOR_BUTTON].name = SANE_EPSON_WAIT_FOR_BUTTON_NAME;
3487 s->opt[OPT_WAIT_FOR_BUTTON].title = SANE_EPSON_WAIT_FOR_BUTTON_TITLE;
3488 s->opt[OPT_WAIT_FOR_BUTTON].desc = SANE_EPSON_WAIT_FOR_BUTTON_DESC;
3489
3490 s->opt[OPT_WAIT_FOR_BUTTON].type = SANE_TYPE_BOOL;
3491 s->opt[OPT_WAIT_FOR_BUTTON].unit = SANE_UNIT_NONE;
3492 s->opt[OPT_WAIT_FOR_BUTTON].constraint_type = SANE_CONSTRAINT_NONE;
3493 s->opt[OPT_WAIT_FOR_BUTTON].constraint.range = NULL;
3494 s->opt[OPT_WAIT_FOR_BUTTON].cap |= SANE_CAP_ADVANCED;
3495
3496 if (!s->hw->cmd->request_push_button_status)
3497 {
3498 s->opt[OPT_WAIT_FOR_BUTTON].cap |= SANE_CAP_INACTIVE;
3499 }
3500
3501
3502 return SANE_STATUS_GOOD;
3503 }
3504
3505 /*
3506 *
3507 *
3508 */
3509
3510 SANE_Status
sane_open(SANE_String_Const devicename, SANE_Handle * handle)3511 sane_open (SANE_String_Const devicename, SANE_Handle * handle)
3512 {
3513 Epson_Device *dev;
3514 Epson_Scanner *s;
3515
3516 DBG (5, "sane_open(%s)\n", devicename);
3517
3518 /* search for device */
3519 if (devicename[0])
3520 {
3521 for (dev = first_dev; dev; dev = dev->next)
3522 {
3523 if (strcmp (dev->sane.name, devicename) == 0)
3524 {
3525 break;
3526 }
3527 }
3528
3529 if (!dev)
3530 {
3531 #if 0
3532 status = attach (devicename, &dev, SANE_EPSON_);
3533 if (status != SANE_STATUS_GOOD)
3534 {
3535 return status;
3536 }
3537 #endif
3538 DBG (1, "Error opening the device");
3539 return SANE_STATUS_INVAL;
3540 }
3541 }
3542 else
3543 {
3544 dev = first_dev;
3545 }
3546
3547 if (!dev)
3548 {
3549 return SANE_STATUS_INVAL;
3550 }
3551
3552 s = calloc (sizeof (Epson_Scanner), 1);
3553 if (!s)
3554 {
3555 DBG (1, "out of memory (line %d)\n", __LINE__);
3556 return SANE_STATUS_NO_MEM;
3557 }
3558
3559 s->fd = -1;
3560 s->hw = dev;
3561
3562 init_options (s);
3563
3564 /* insert newly opened handle into list of open handles */
3565 s->next = first_handle;
3566 first_handle = s;
3567
3568 *handle = (SANE_Handle) s;
3569
3570 open_scanner (s);
3571
3572 return SANE_STATUS_GOOD;
3573 }
3574
3575 /*
3576 *
3577 *
3578 */
3579
3580 void
sane_close(SANE_Handle handle)3581 sane_close (SANE_Handle handle)
3582 {
3583 Epson_Scanner *s, *prev;
3584
3585 /*
3586 * Test if there is still data pending from
3587 * the scanner. If so, then do a cancel
3588 */
3589
3590 s = (Epson_Scanner *) handle;
3591
3592 /* remove handle from list of open handles */
3593 prev = 0;
3594 for (s = first_handle; s; s = s->next)
3595 {
3596 if (s == handle)
3597 break;
3598 prev = s;
3599 }
3600
3601 if (!s)
3602 {
3603 DBG (1, "close: invalid handle (0x%p)\n", handle);
3604 return;
3605 }
3606
3607 if (prev)
3608 prev->next = s->next;
3609 else
3610 first_handle = s->next;
3611
3612 if (s->fd != -1)
3613 close_scanner (s);
3614
3615 free (s);
3616 }
3617
3618 /*
3619 *
3620 *
3621 */
3622
3623 const SANE_Option_Descriptor *
sane_get_option_descriptor(SANE_Handle handle, SANE_Int option)3624 sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
3625 {
3626 Epson_Scanner *s = (Epson_Scanner *) handle;
3627
3628 if (option < 0 || option >= NUM_OPTIONS)
3629 return NULL;
3630
3631 return (s->opt + option);
3632 }
3633
3634 /*
3635 *
3636 *
3637 */
3638
3639 static const SANE_String_Const *
search_string_list(const SANE_String_Const * list, SANE_String value)3640 search_string_list (const SANE_String_Const * list, SANE_String value)
3641 {
3642 while (*list != NULL && strcmp (value, *list) != 0)
3643 {
3644 ++list;
3645 }
3646
3647 return ((*list == NULL) ? NULL : list);
3648 }
3649
3650 /*
3651 *
3652 *
3653 */
3654
3655 /*
3656 Activate, deactivate an option. Subroutines so we can add
3657 debugging info if we want. The change flag is set to TRUE
3658 if we changed an option. If we did not change an option,
3659 then the value of the changed flag is not modified.
3660 */
3661
3662 static void
activateOption(Epson_Scanner * s, SANE_Int option, SANE_Bool * change)3663 activateOption (Epson_Scanner * s, SANE_Int option, SANE_Bool * change)
3664 {
3665 if (!SANE_OPTION_IS_ACTIVE (s->opt[option].cap))
3666 {
3667 s->opt[option].cap &= ~SANE_CAP_INACTIVE;
3668 *change = SANE_TRUE;
3669 }
3670 }
3671
3672 static void
deactivateOption(Epson_Scanner * s, SANE_Int option, SANE_Bool * change)3673 deactivateOption (Epson_Scanner * s, SANE_Int option, SANE_Bool * change)
3674 {
3675 if (SANE_OPTION_IS_ACTIVE (s->opt[option].cap))
3676 {
3677 s->opt[option].cap |= SANE_CAP_INACTIVE;
3678 *change = SANE_TRUE;
3679 }
3680 }
3681
3682 static void
setOptionState(Epson_Scanner * s, SANE_Bool state, SANE_Int option, SANE_Bool * change)3683 setOptionState (Epson_Scanner * s, SANE_Bool state,
3684 SANE_Int option, SANE_Bool * change)
3685 {
3686 if (state)
3687 {
3688 activateOption (s, option, change);
3689 }
3690 else
3691 {
3692 deactivateOption (s, option, change);
3693 }
3694 }
3695
3696 /**
3697 End of activateOption, deactivateOption, setOptionState.
3698 **/
3699
3700 static SANE_Status
getvalue(SANE_Handle handle, SANE_Int option, void *value)3701 getvalue (SANE_Handle handle, SANE_Int option, void *value)
3702 {
3703 Epson_Scanner *s = (Epson_Scanner *) handle;
3704 SANE_Option_Descriptor *sopt = &(s->opt[option]);
3705 Option_Value *sval = &(s->val[option]);
3706
3707 switch (option)
3708 {
3709 /* case OPT_GAMMA_VECTOR: */
3710 case OPT_GAMMA_VECTOR_R:
3711 case OPT_GAMMA_VECTOR_G:
3712 case OPT_GAMMA_VECTOR_B:
3713 memcpy (value, sval->wa, sopt->size);
3714 break;
3715
3716 case OPT_NUM_OPTS:
3717 case OPT_RESOLUTION:
3718 case OPT_TL_X:
3719 case OPT_TL_Y:
3720 case OPT_BR_X:
3721 case OPT_BR_Y:
3722 case OPT_MIRROR:
3723 case OPT_SPEED:
3724 case OPT_PREVIEW_SPEED:
3725 case OPT_AAS:
3726 case OPT_PREVIEW:
3727 case OPT_BRIGHTNESS:
3728 case OPT_SHARPNESS:
3729 case OPT_AUTO_EJECT:
3730 case OPT_CCT_1:
3731 case OPT_CCT_2:
3732 case OPT_CCT_3:
3733 case OPT_CCT_4:
3734 case OPT_CCT_5:
3735 case OPT_CCT_6:
3736 case OPT_CCT_7:
3737 case OPT_CCT_8:
3738 case OPT_CCT_9:
3739 case OPT_THRESHOLD:
3740 case OPT_ZOOM:
3741 case OPT_BIT_DEPTH:
3742 case OPT_WAIT_FOR_BUTTON:
3743 case OPT_LIMIT_RESOLUTION:
3744 *((SANE_Word *) value) = sval->w;
3745 break;
3746 case OPT_MODE:
3747 case OPT_ADF_MODE:
3748 case OPT_HALFTONE:
3749 case OPT_DROPOUT:
3750 case OPT_QUICK_FORMAT:
3751 case OPT_SOURCE:
3752 case OPT_FILM_TYPE:
3753 case OPT_GAMMA_CORRECTION:
3754 case OPT_COLOR_CORRECTION:
3755 case OPT_BAY:
3756 case OPT_FOCUS:
3757 strcpy ((char *) value, sopt->constraint.string_list[sval->w]);
3758 break;
3759 #if 0
3760 case OPT_MODEL:
3761 strcpy (value, sval->s);
3762 break;
3763 #endif
3764
3765
3766 default:
3767 return SANE_STATUS_INVAL;
3768
3769 }
3770
3771 return SANE_STATUS_GOOD;
3772 }
3773
3774
3775 /**
3776 End of getvalue.
3777 **/
3778
3779
3780 static void
handle_depth_halftone(Epson_Scanner * s, SANE_Bool * reload)3781 handle_depth_halftone (Epson_Scanner * s, SANE_Bool * reload)
3782 /*
3783 This routine handles common options between OPT_MODE and
3784 OPT_HALFTONE. These options are TET (a HALFTONE mode), AAS
3785 - auto area segmentation, and threshold. Apparently AAS
3786 is some method to differentiate between text and photos.
3787 Or something like that.
3788
3789 AAS is available when the scan color depth is 1 and the
3790 halftone method is not TET.
3791
3792 Threshold is available when halftone is NONE, and depth is 1.
3793 */
3794 {
3795 int hti = s->val[OPT_HALFTONE].w;
3796 int mdi = s->val[OPT_MODE].w;
3797 SANE_Bool aas = SANE_FALSE;
3798 SANE_Bool thresh = SANE_FALSE;
3799
3800 if (!s->hw->cmd->control_auto_area_segmentation)
3801 return;
3802
3803 if (mode_params[mdi].depth == 1)
3804 {
3805 if (halftone_params[hti] != HALFTONE_TET)
3806 {
3807 aas = SANE_TRUE;
3808 }
3809 if (halftone_params[hti] == HALFTONE_NONE)
3810 {
3811 thresh = SANE_TRUE;
3812 }
3813 }
3814 setOptionState (s, aas, OPT_AAS, reload);
3815 setOptionState (s, thresh, OPT_THRESHOLD, reload);
3816 }
3817
3818 /**
3819 End of handle_depth_halftone.
3820 **/
3821
3822
3823 static void
handle_source(Epson_Scanner * s, SANE_Int optindex, char *value)3824 handle_source (Epson_Scanner * s, SANE_Int optindex, char *value)
3825 /*
3826 Handles setting the source (flatbed, transparency adapter (TPU),
3827 or auto document feeder (ADF)).
3828
3829 For newer scanners it also sets the focus according to the
3830 glass / TPU settings.
3831 */
3832 {
3833 int force_max = SANE_FALSE;
3834 SANE_Bool dummy;
3835
3836 /* reset the scanner when we are changing the source setting -
3837 this is necessary for the Perfection 1650 */
3838 if (s->hw->need_reset_on_source_change)
3839 reset (s);
3840
3841 s->focusOnGlass = SANE_TRUE; /* this is the default */
3842
3843 if (s->val[OPT_SOURCE].w == optindex)
3844 return;
3845
3846 s->val[OPT_SOURCE].w = optindex;
3847
3848 if (s->val[OPT_TL_X].w == s->hw->x_range->min
3849 && s->val[OPT_TL_Y].w == s->hw->y_range->min
3850 && s->val[OPT_BR_X].w == s->hw->x_range->max
3851 && s->val[OPT_BR_Y].w == s->hw->y_range->max)
3852 {
3853 force_max = SANE_TRUE;
3854 }
3855 if (strcmp (ADF_STR, value) == 0)
3856 {
3857 s->hw->x_range = &s->hw->adf_x_range;
3858 s->hw->y_range = &s->hw->adf_y_range;
3859 s->hw->use_extension = SANE_TRUE;
3860 /* disable film type option */
3861 deactivateOption (s, OPT_FILM_TYPE, &dummy);
3862 s->val[OPT_FOCUS].w = 0;
3863 if (s->hw->duplexSupport)
3864 {
3865 activateOption (s, OPT_ADF_MODE, &dummy);
3866 }
3867 else
3868 {
3869 deactivateOption (s, OPT_ADF_MODE, &dummy);
3870 s->val[OPT_ADF_MODE].w = 0;
3871 }
3872 }
3873 else if (strcmp (TPU_STR, value) == 0)
3874 {
3875 s->hw->x_range = &s->hw->tpu_x_range;
3876 s->hw->y_range = &s->hw->tpu_y_range;
3877 s->hw->use_extension = SANE_TRUE;
3878 /* enable film type option only if the scanner supports it */
3879 if (s->hw->cmd->set_film_type != 0)
3880 {
3881 activateOption (s, OPT_FILM_TYPE, &dummy);
3882 }
3883 else
3884 {
3885 deactivateOption (s, OPT_FILM_TYPE, &dummy);
3886 }
3887 /* enable focus position if the scanner supports it */
3888 if (s->hw->cmd->set_focus_position != 0)
3889 {
3890 s->val[OPT_FOCUS].w = 1;
3891 s->focusOnGlass = SANE_FALSE;
3892 }
3893 deactivateOption (s, OPT_ADF_MODE, &dummy);
3894 deactivateOption (s, OPT_EJECT, &dummy);
3895 deactivateOption (s, OPT_AUTO_EJECT, &dummy);
3896 }
3897 else /* neither ADF nor TPU active */
3898 {
3899 s->hw->x_range = &s->hw->fbf_x_range;
3900 s->hw->y_range = &s->hw->fbf_y_range;
3901 s->hw->use_extension = SANE_FALSE;
3902 /* disable film type option */
3903 deactivateOption (s, OPT_FILM_TYPE, &dummy);
3904 s->val[OPT_FOCUS].w = 0;
3905 deactivateOption (s, OPT_ADF_MODE, &dummy);
3906 }
3907
3908 qf_params[XtNumber (qf_params) - 1].tl_x = s->hw->x_range->min;
3909 qf_params[XtNumber (qf_params) - 1].tl_y = s->hw->y_range->min;
3910 qf_params[XtNumber (qf_params) - 1].br_x = s->hw->x_range->max;
3911 qf_params[XtNumber (qf_params) - 1].br_y = s->hw->y_range->max;
3912
3913 s->opt[OPT_BR_X].constraint.range = s->hw->x_range;
3914 s->opt[OPT_BR_Y].constraint.range = s->hw->y_range;
3915
3916 if (s->val[OPT_TL_X].w < s->hw->x_range->min || force_max)
3917 s->val[OPT_TL_X].w = s->hw->x_range->min;
3918
3919 if (s->val[OPT_TL_Y].w < s->hw->y_range->min || force_max)
3920 s->val[OPT_TL_Y].w = s->hw->y_range->min;
3921
3922 if (s->val[OPT_BR_X].w > s->hw->x_range->max || force_max)
3923 s->val[OPT_BR_X].w = s->hw->x_range->max;
3924
3925 if (s->val[OPT_BR_Y].w > s->hw->y_range->max || force_max)
3926 s->val[OPT_BR_Y].w = s->hw->y_range->max;
3927
3928 setOptionState (s, s->hw->ADF && s->hw->use_extension,
3929 OPT_AUTO_EJECT, &dummy);
3930 setOptionState (s, s->hw->ADF && s->hw->use_extension, OPT_EJECT, &dummy);
3931
3932 #if 0
3933 BAY is part of the filmscan device.We are not sure
3934 if we are really going to support this device in this
3935 code.Is there an online manual for it ?
3936 setOptionState (s, s->hw->ADF && s->hw->use_extension, OPT_BAY, &reload);
3937 #endif
3938 }
3939
3940 /**
3941 End of handle_source.
3942 **/
3943
3944 static SANE_Status
setvalue(SANE_Handle handle, SANE_Int option, void *value, SANE_Int * info)3945 setvalue (SANE_Handle handle, SANE_Int option, void *value, SANE_Int * info)
3946 {
3947 Epson_Scanner *s = (Epson_Scanner *) handle;
3948 SANE_Option_Descriptor *sopt = &(s->opt[option]);
3949 Option_Value *sval = &(s->val[option]);
3950
3951 SANE_Status status;
3952 const SANE_String_Const *optval;
3953 int optindex;
3954 SANE_Bool reload = SANE_FALSE;
3955
3956 DBG (5, "setvalue(option = %d, value = %p)\n", option, value);
3957
3958 status = sanei_constrain_value (sopt, value, info);
3959
3960 if (status != SANE_STATUS_GOOD)
3961 return status;
3962
3963 s->option_has_changed = SANE_TRUE;
3964
3965 optval = NULL;
3966 optindex = 0;
3967
3968 if (sopt->constraint_type == SANE_CONSTRAINT_STRING_LIST)
3969 {
3970 optval = search_string_list (sopt->constraint.string_list,
3971 (char *) value);
3972
3973 if (optval == NULL)
3974 return SANE_STATUS_INVAL;
3975 optindex = optval - sopt->constraint.string_list;
3976 }
3977
3978 switch (option)
3979 {
3980 /* case OPT_GAMMA_VECTOR: */
3981 case OPT_GAMMA_VECTOR_R:
3982 case OPT_GAMMA_VECTOR_G:
3983 case OPT_GAMMA_VECTOR_B:
3984 memcpy (sval->wa, value, sopt->size); /* Word arrays */
3985 break;
3986
3987 case OPT_CCT_1:
3988 case OPT_CCT_2:
3989 case OPT_CCT_3:
3990 case OPT_CCT_4:
3991 case OPT_CCT_5:
3992 case OPT_CCT_6:
3993 case OPT_CCT_7:
3994 case OPT_CCT_8:
3995 case OPT_CCT_9:
3996 sval->w = *((SANE_Word *) value); /* Simple values */
3997 break;
3998
3999 case OPT_DROPOUT:
4000 case OPT_FILM_TYPE:
4001 case OPT_BAY:
4002 case OPT_FOCUS:
4003 sval->w = optindex; /* Simple lists */
4004 break;
4005
4006 case OPT_EJECT:
4007 /* return eject( s ); */
4008 eject (s);
4009 break;
4010
4011 case OPT_RESOLUTION:
4012 sval->w = *((SANE_Word *) value);
4013 reload = SANE_TRUE;
4014 break;
4015
4016 case OPT_TL_X:
4017 case OPT_TL_Y:
4018 case OPT_BR_X:
4019 case OPT_BR_Y:
4020 sval->w = *((SANE_Word *) value);
4021 DBG (1, "set = %f\n", SANE_UNFIX (sval->w));
4022 if (NULL != info)
4023 *info |= SANE_INFO_RELOAD_PARAMS;
4024 break;
4025
4026 case OPT_SOURCE:
4027 handle_source (s, optindex, (char *) value);
4028 reload = SANE_TRUE;
4029 break;
4030
4031 case OPT_MODE:
4032 {
4033 SANE_Bool isColor = mode_params[optindex].color;
4034 SANE_Bool userDefined =
4035 color_userdefined[s->val[OPT_COLOR_CORRECTION].w];
4036
4037 sval->w = optindex;
4038
4039 if (s->hw->cmd->set_halftoning != 0)
4040 {
4041 setOptionState (s, mode_params[optindex].depth == 1,
4042 OPT_HALFTONE, &reload);
4043 }
4044
4045 setOptionState (s, !isColor, OPT_DROPOUT, &reload);
4046 if (s->hw->cmd->set_color_correction)
4047 {
4048 setOptionState (s, isColor, OPT_COLOR_CORRECTION, &reload);
4049 }
4050 if (s->hw->cmd->set_color_correction_coefficients)
4051 {
4052 setOptionState (s, isColor && userDefined, OPT_CCT_1, &reload);
4053 setOptionState (s, isColor && userDefined, OPT_CCT_2, &reload);
4054 setOptionState (s, isColor && userDefined, OPT_CCT_3, &reload);
4055 setOptionState (s, isColor && userDefined, OPT_CCT_4, &reload);
4056 setOptionState (s, isColor && userDefined, OPT_CCT_5, &reload);
4057 setOptionState (s, isColor && userDefined, OPT_CCT_6, &reload);
4058 setOptionState (s, isColor && userDefined, OPT_CCT_7, &reload);
4059 setOptionState (s, isColor && userDefined, OPT_CCT_8, &reload);
4060 setOptionState (s, isColor && userDefined, OPT_CCT_9, &reload);
4061 }
4062
4063 /* if binary, then disable the bit depth selection */
4064 if (optindex == 0)
4065 {
4066 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
4067 }
4068 else
4069 {
4070 if (bitDepthList[0] == 1)
4071 s->opt[OPT_BIT_DEPTH].cap |= SANE_CAP_INACTIVE;
4072 else
4073 {
4074 s->opt[OPT_BIT_DEPTH].cap &= ~SANE_CAP_INACTIVE;
4075 s->val[OPT_BIT_DEPTH].w = mode_params[optindex].depth;
4076 }
4077 }
4078
4079 handle_depth_halftone (s, &reload);
4080 reload = SANE_TRUE;
4081
4082 break;
4083 }
4084
4085 case OPT_ADF_MODE:
4086 sval->w = optindex;
4087 break;
4088
4089 case OPT_BIT_DEPTH:
4090 sval->w = *((SANE_Word *) value);
4091 mode_params[s->val[OPT_MODE].w].depth = sval->w;
4092 reload = SANE_TRUE;
4093 break;
4094
4095 case OPT_HALFTONE:
4096 sval->w = optindex;
4097 handle_depth_halftone (s, &reload);
4098 break;
4099
4100 case OPT_COLOR_CORRECTION:
4101 {
4102 SANE_Bool f = color_userdefined[optindex];
4103
4104 sval->w = optindex;
4105 setOptionState (s, f, OPT_CCT_1, &reload);
4106 setOptionState (s, f, OPT_CCT_2, &reload);
4107 setOptionState (s, f, OPT_CCT_3, &reload);
4108 setOptionState (s, f, OPT_CCT_4, &reload);
4109 setOptionState (s, f, OPT_CCT_5, &reload);
4110 setOptionState (s, f, OPT_CCT_6, &reload);
4111 setOptionState (s, f, OPT_CCT_7, &reload);
4112 setOptionState (s, f, OPT_CCT_8, &reload);
4113 setOptionState (s, f, OPT_CCT_9, &reload);
4114
4115 break;
4116 }
4117
4118 case OPT_GAMMA_CORRECTION:
4119 {
4120 SANE_Bool f = gamma_userdefined[optindex];
4121
4122 sval->w = optindex;
4123 /* setOptionState(s, f, OPT_GAMMA_VECTOR, &reload ); */
4124 setOptionState (s, f, OPT_GAMMA_VECTOR_R, &reload);
4125 setOptionState (s, f, OPT_GAMMA_VECTOR_G, &reload);
4126 setOptionState (s, f, OPT_GAMMA_VECTOR_B, &reload);
4127 setOptionState (s, !f, OPT_BRIGHTNESS, &reload); /* Note... */
4128
4129 break;
4130 }
4131
4132 case OPT_MIRROR:
4133 case OPT_SPEED:
4134 case OPT_PREVIEW_SPEED:
4135 case OPT_AAS:
4136 case OPT_PREVIEW: /* needed? */
4137 case OPT_BRIGHTNESS:
4138 case OPT_SHARPNESS:
4139 case OPT_AUTO_EJECT:
4140 case OPT_THRESHOLD:
4141 case OPT_ZOOM:
4142 case OPT_WAIT_FOR_BUTTON:
4143 sval->w = *((SANE_Word *) value);
4144 break;
4145
4146 case OPT_LIMIT_RESOLUTION:
4147 sval->w = *((SANE_Word *) value);
4148 filter_resolution_list (s);
4149 reload = SANE_TRUE;
4150 break;
4151
4152 case OPT_QUICK_FORMAT:
4153 sval->w = optindex;
4154
4155 s->val[OPT_TL_X].w = qf_params[sval->w].tl_x;
4156 s->val[OPT_TL_Y].w = qf_params[sval->w].tl_y;
4157 s->val[OPT_BR_X].w = qf_params[sval->w].br_x;
4158 s->val[OPT_BR_Y].w = qf_params[sval->w].br_y;
4159
4160 if (s->val[OPT_TL_X].w < s->hw->x_range->min)
4161 s->val[OPT_TL_X].w = s->hw->x_range->min;
4162
4163 if (s->val[OPT_TL_Y].w < s->hw->y_range->min)
4164 s->val[OPT_TL_Y].w = s->hw->y_range->min;
4165
4166 if (s->val[OPT_BR_X].w > s->hw->x_range->max)
4167 s->val[OPT_BR_X].w = s->hw->x_range->max;
4168
4169 if (s->val[OPT_BR_Y].w > s->hw->y_range->max)
4170 s->val[OPT_BR_Y].w = s->hw->y_range->max;
4171
4172 reload = SANE_TRUE;
4173 break;
4174
4175 default:
4176 return SANE_STATUS_INVAL;
4177 }
4178
4179 if (reload && info != NULL)
4180 {
4181 *info |= SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS;
4182 }
4183
4184 return SANE_STATUS_GOOD;
4185 }
4186
4187 /**
4188 End of setvalue.
4189 **/
4190
4191 SANE_Status
sane_control_option(SANE_Handle handle, SANE_Int option, SANE_Action action, void *value, SANE_Int * info)4192 sane_control_option (SANE_Handle handle,
4193 SANE_Int option,
4194 SANE_Action action, void *value, SANE_Int * info)
4195 {
4196 if (option < 0 || option >= NUM_OPTIONS)
4197 return SANE_STATUS_INVAL;
4198
4199 if (info != NULL)
4200 *info = 0;
4201
4202 switch (action)
4203 {
4204 case SANE_ACTION_GET_VALUE:
4205 return (getvalue (handle, option, value));
4206
4207 case SANE_ACTION_SET_VALUE:
4208 return (setvalue (handle, option, value, info));
4209 default:
4210 return SANE_STATUS_INVAL;
4211 }
4212
4213 return SANE_STATUS_GOOD;
4214 }
4215
4216 /*
4217 * sane_get_parameters()
4218 *
4219 * This function is part of the SANE API and gets called when the front end
4220 * requests information aobut the scan configuration (e.g. color depth, mode,
4221 * bytes and pixels per line, number of lines. This information is returned
4222 * in the SANE_Parameters structure.
4223 *
4224 * Once a scan was started, this routine has to report the correct values, if
4225 * it is called before the scan is actually started, the values are based on
4226 * the current settings.
4227 *
4228 */
4229 SANE_Status
sane_get_parameters(SANE_Handle handle, SANE_Parameters * params)4230 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
4231 {
4232 Epson_Scanner *s = (Epson_Scanner *) handle;
4233 int ndpi;
4234 int bytes_per_pixel;
4235
4236 DBG (5, "sane_get_parameters()\n");
4237
4238 /*
4239 * If sane_start was already called, then just retrieve the parameters
4240 * from the scanner data structure
4241 */
4242
4243 if (!s->eof && s->ptr != NULL)
4244 {
4245 DBG (5, "Returning saved params structure\n");
4246 if (params != NULL)
4247 {
4248 DBG(1, "Restoring parameters from saved parameters\n");
4249 *params = s->params;
4250 }
4251
4252 DBG (3, "Preview = %d\n", s->val[OPT_PREVIEW].w);
4253 DBG (3, "Resolution = %d\n", s->val[OPT_RESOLUTION].w);
4254
4255 DBG (1, "get para %p %p tlx %f tly %f brx %f bry %f [mm]\n", (void *) s,
4256 (void *) s->val, SANE_UNFIX (s->val[OPT_TL_X].w),
4257 SANE_UNFIX (s->val[OPT_TL_Y].w), SANE_UNFIX (s->val[OPT_BR_X].w),
4258 SANE_UNFIX (s->val[OPT_BR_Y].w));
4259
4260 print_params (s->params);
4261
4262 return SANE_STATUS_GOOD;
4263 }
4264
4265 /* otherwise initialize the params structure and gather the data */
4266
4267 memset (&s->params, 0, sizeof (SANE_Parameters));
4268
4269 ndpi = s->val[OPT_RESOLUTION].w;
4270
4271 s->params.pixels_per_line =
4272 SANE_UNFIX (s->val[OPT_BR_X].w - s->val[OPT_TL_X].w) / 25.4 * ndpi + 0.5;
4273 s->params.lines =
4274 SANE_UNFIX (s->val[OPT_BR_Y].w - s->val[OPT_TL_Y].w) / 25.4 * ndpi + 0.5;
4275
4276 /*
4277 * Make sure that the number of lines is correct for color shuffling:
4278 * The shuffling algorithm produces 2xline_distance lines at the
4279 * beginning and the same amount at the end of the scan that are not
4280 * useable. If s->params.lines gets negative, 0 lines are reported
4281 * back to the frontend.
4282 */
4283 if (s->hw->color_shuffle)
4284 {
4285 s->params.lines -= 4 * s->line_distance;
4286 if (s->params.lines < 0)
4287 {
4288 s->params.lines = 0;
4289 }
4290 DBG (1, "Adjusted params.lines for color_shuffle by %d to %d\n",
4291 4 * s->line_distance, s->params.lines);
4292 }
4293
4294 DBG (3, "Preview = %d\n", s->val[OPT_PREVIEW].w);
4295 DBG (3, "Resolution = %d\n", s->val[OPT_RESOLUTION].w);
4296
4297 DBG (1, "get para %p %p tlx %f tly %f brx %f bry %f [mm]\n", (void *) s,
4298 (void *) s->val, SANE_UNFIX (s->val[OPT_TL_X].w),
4299 SANE_UNFIX (s->val[OPT_TL_Y].w), SANE_UNFIX (s->val[OPT_BR_X].w),
4300 SANE_UNFIX (s->val[OPT_BR_Y].w));
4301
4302
4303 /*
4304 * Calculate bytes_per_pixel and bytes_per_line for
4305 * any color depths.
4306 *
4307 * The default color depth is stored in mode_params.depth:
4308 */
4309
4310 if (mode_params[s->val[OPT_MODE].w].depth == 1)
4311 {
4312 s->params.depth = 1;
4313 }
4314 else
4315 {
4316 s->params.depth = s->val[OPT_BIT_DEPTH].w;
4317 }
4318
4319 if (s->params.depth > 8)
4320 {
4321 s->params.depth = 16; /*
4322 * The frontends can only handle 8 or 16 bits
4323 * for gray or color - so if it's more than 8,
4324 * it gets automatically set to 16. This works
4325 * as long as EPSON does not come out with a
4326 * scanner that can handle more than 16 bits
4327 * per color channel.
4328 */
4329
4330 }
4331
4332 bytes_per_pixel = s->params.depth / 8; /* this works because it can only be set to 1, 8 or 16 */
4333 if (s->params.depth % 8) /* just in case ... */
4334 {
4335 bytes_per_pixel++;
4336 }
4337
4338 /* pixels_per_line is rounded to the next 8bit boundary */
4339 s->params.pixels_per_line = s->params.pixels_per_line & ~7;
4340
4341 s->params.last_frame = SANE_TRUE;
4342
4343 if (mode_params[s->val[OPT_MODE].w].color)
4344 {
4345 s->params.format = SANE_FRAME_RGB;
4346 s->params.bytes_per_line =
4347 3 * s->params.pixels_per_line * bytes_per_pixel;
4348 }
4349 else
4350 {
4351 s->params.format = SANE_FRAME_GRAY;
4352 s->params.bytes_per_line =
4353 s->params.pixels_per_line * s->params.depth / 8;
4354 }
4355
4356 if (NULL != params)
4357 *params = s->params;
4358
4359 print_params (s->params);
4360
4361 return SANE_STATUS_GOOD;
4362 }
4363
4364 /*
4365 * sane_start()
4366 *
4367 * This function is part of the SANE API and gets called from the front end to
4368 * start the scan process.
4369 *
4370 */
4371
4372 SANE_Status
sane_start(SANE_Handle handle)4373 sane_start (SANE_Handle handle)
4374 {
4375 Epson_Scanner *s = (Epson_Scanner *) handle;
4376 SANE_Status status;
4377 SANE_Bool button_status;
4378 const struct mode_param *mparam;
4379 u_char params[4];
4380 int ndpi;
4381 int left, top;
4382 int lcount;
4383 int i, j; /* loop counter */
4384
4385 DBG (5, "sane_start()\n");
4386
4387 open_scanner (s);
4388
4389 /*
4390 * There is some undocumented special behavior with the TPU enable/disable.
4391 * TPU power ESC e status
4392 * on 0 NAK
4393 * on 1 ACK
4394 * off 0 ACK
4395 * off 1 NAK
4396 *
4397 * It makes no sense to scan with TPU powered on and source flatbed, because
4398 * light will come from both sides.
4399 */
4400
4401 if (s->hw->extension)
4402 {
4403 int max_x, max_y;
4404
4405 int extensionCtrl;
4406 extensionCtrl = (s->hw->use_extension ? 1 : 0);
4407 if (s->hw->use_extension && (s->val[OPT_ADF_MODE].w == 1))
4408 extensionCtrl = 2;
4409
4410 status = control_extension (s, extensionCtrl);
4411
4412 if (SANE_STATUS_GOOD != status)
4413 {
4414 DBG (1, "You may have to power %s your TPU\n",
4415 s->hw->use_extension ? "on" : "off");
4416
4417 DBG (1, "Also you may have to restart the Sane frontend.\n");
4418 close_scanner (s);
4419 return status;
4420 }
4421
4422 if (s->hw->cmd->request_extended_status != 0)
4423 {
4424 status = check_ext_status (s, &max_x, &max_y);
4425
4426 if (SANE_STATUS_GOOD != status && SANE_STATUS_DEVICE_BUSY != status)
4427 {
4428 close_scanner (s);
4429 return status;
4430 }
4431 }
4432
4433 if (s->hw->ADF && s->hw->use_extension && s->hw->cmd->feed)
4434 {
4435 status = feed (s);
4436 if (SANE_STATUS_GOOD != status)
4437 {
4438 close_scanner (s);
4439 return status;
4440 }
4441
4442 check_ext_status (s, &max_x, &max_y);
4443 s->hw->adf_max_x = max_x;
4444 s->hw->adf_max_y = max_y;
4445 }
4446
4447
4448 /*
4449 * set the focus position according to the extension used:
4450 * if the TPU is selected, then focus 2.5mm above the glass,
4451 * otherwise focus on the glass. Scanners that don't support
4452 * this feature, will just ignore these calls.
4453 */
4454
4455 if (s->hw->focusSupport == SANE_TRUE)
4456 {
4457 if (s->val[OPT_FOCUS].w == 0)
4458 {
4459 DBG (1, "Setting focus to glass surface\n");
4460 set_focus_position (s, 0x40);
4461 }
4462 else
4463 {
4464 DBG (1, "Setting focus to 2.5mm above glass\n");
4465 set_focus_position (s, 0x59);
4466 }
4467 }
4468 }
4469
4470 /* use the flatbed size for the max. scansize for the GT-30000
4471 and similar scanners if the ADF is not enabled */
4472 if (s->hw->devtype == 3 && s->hw->use_extension == 0)
4473 {
4474 int max_x, max_y;
4475
4476 status = check_ext_status (s, &max_x, &max_y);
4477 if (SANE_STATUS_GOOD != status && SANE_STATUS_DEVICE_BUSY != status)
4478 {
4479 close_scanner (s);
4480 return status;
4481 }
4482
4483 s->hw->fbf_max_x = max_x;
4484 s->hw->fbf_max_y = max_y;
4485 }
4486
4487
4488 mparam = mode_params + s->val[OPT_MODE].w;
4489 DBG (1, "sane_start: Setting data format to %d bits\n", mparam->depth);
4490 status = set_data_format (s, mparam->depth);
4491
4492 if (SANE_STATUS_GOOD != status)
4493 {
4494 DBG (1, "sane_start: set_data_format failed: %s\n",
4495 sane_strstatus (status));
4496 return status;
4497 }
4498
4499 /*
4500 * The byte sequence mode was introduced in B5, for B[34] we need line sequence mode
4501 */
4502
4503 if ((s->hw->cmd->level[0] == 'D' ||
4504 (s->hw->cmd->level[0] == 'B' && s->hw->level >= 5)) &&
4505 mparam->mode_flags == 0x02)
4506 {
4507 status = set_color_mode (s, 0x13);
4508 }
4509 else
4510 {
4511 status = set_color_mode (s, mparam->mode_flags | (mparam->dropout_mask
4512 & dropout_params[s->
4513 val
4514 [OPT_DROPOUT].
4515 w]));
4516 }
4517
4518 if (SANE_STATUS_GOOD != status)
4519 {
4520 DBG (1, "sane_start: set_color_mode failed: %s\n",
4521 sane_strstatus (status));
4522 return status;
4523 }
4524
4525 if (s->hw->cmd->set_halftoning &&
4526 SANE_OPTION_IS_ACTIVE (s->opt[OPT_HALFTONE].cap))
4527 {
4528 status = set_halftoning (s, halftone_params[s->val[OPT_HALFTONE].w]);
4529
4530 if (SANE_STATUS_GOOD != status)
4531 {
4532 DBG (1, "sane_start: set_halftoning failed: %s\n",
4533 sane_strstatus (status));
4534 return status;
4535 }
4536 }
4537
4538
4539 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_BRIGHTNESS].cap))
4540 {
4541 status = set_bright (s, s->val[OPT_BRIGHTNESS].w);
4542
4543 if (SANE_STATUS_GOOD != status)
4544 {
4545 DBG (1, "sane_start: set_bright failed: %s\n", sane_strstatus (status));
4546 return status;
4547 }
4548 }
4549
4550 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_MIRROR].cap))
4551 {
4552 status = mirror_image (s, mirror_params[s->val[OPT_MIRROR].w]);
4553
4554 if (SANE_STATUS_GOOD != status)
4555 {
4556 DBG (1, "sane_start: mirror_image failed: %s\n",
4557 sane_strstatus (status));
4558 return status;
4559 }
4560
4561 }
4562
4563 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_SPEED].cap))
4564 {
4565
4566 if (s->val[OPT_PREVIEW].w)
4567 status = set_speed (s, speed_params[s->val[OPT_PREVIEW_SPEED].w]);
4568 else
4569 status = set_speed (s, speed_params[s->val[OPT_SPEED].w]);
4570
4571 if (SANE_STATUS_GOOD != status)
4572 {
4573 DBG (1, "sane_start: set_speed failed: %s\n", sane_strstatus (status));
4574 return status;
4575 }
4576
4577 }
4578
4579 /*
4580 * use of speed_params is ok here since they are false and true.
4581 * NOTE: I think I should throw that "params" stuff as long w is already the value.
4582 */
4583
4584 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_AAS].cap))
4585 {
4586 status = control_auto_area_segmentation (s,
4587 speed_params[s->val[OPT_AAS].w]);
4588
4589 if (SANE_STATUS_GOOD != status)
4590 {
4591 DBG (1, "sane_start: control_auto_area_segmentation failed: %s\n",
4592 sane_strstatus (status));
4593 return status;
4594 }
4595 }
4596
4597 s->invert_image = SANE_FALSE; /* default: to not inverting the image */
4598
4599 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_FILM_TYPE].cap))
4600 {
4601 s->invert_image = (s->val[OPT_FILM_TYPE].w == FILM_TYPE_NEGATIVE);
4602 status = set_film_type (s, film_params[s->val[OPT_FILM_TYPE].w]);
4603 if (SANE_STATUS_GOOD != status)
4604 {
4605 DBG (1, "sane_start: set_film_type failed: %s\n",
4606 sane_strstatus (status));
4607 return status;
4608 }
4609 }
4610
4611 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_BAY].cap))
4612 {
4613 status = set_bay (s, s->val[OPT_BAY].w);
4614
4615 if (SANE_STATUS_GOOD != status)
4616 {
4617 DBG (1, "sane_start: set_bay: %s\n", sane_strstatus (status));
4618 return status;
4619 }
4620 }
4621
4622 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_SHARPNESS].cap))
4623 {
4624
4625 status = set_outline_emphasis (s, s->val[OPT_SHARPNESS].w);
4626
4627 if (SANE_STATUS_GOOD != status)
4628 {
4629 DBG (1, "sane_start: set_outline_emphasis failed: %s\n",
4630 sane_strstatus (status));
4631 return status;
4632 }
4633 }
4634
4635 if (s->hw->cmd->set_gamma &&
4636 SANE_OPTION_IS_ACTIVE (s->opt[OPT_GAMMA_CORRECTION].cap))
4637 {
4638 int val;
4639 if (s->hw->cmd->level[0] == 'D')
4640 {
4641 /*
4642 * The D1 level has only the two user defined gamma
4643 * settings.
4644 */
4645 val = gamma_params[s->val[OPT_GAMMA_CORRECTION].w];
4646 }
4647 else
4648 {
4649 val = gamma_params[s->val[OPT_GAMMA_CORRECTION].w];
4650
4651 /*
4652 * If "Default" is selected then determine the actual value
4653 * to send to the scanner: If bilevel mode, just send the
4654 * value from the table (0x01), for grayscale or color mode
4655 * add one and send 0x02.
4656 */
4657 /* if( s->val[ OPT_GAMMA_CORRECTION].w <= 1) { */
4658 if (s->val[OPT_GAMMA_CORRECTION].w == 0)
4659 {
4660 val += mparam->depth == 1 ? 0 : 1;
4661 }
4662 }
4663
4664 DBG (1, "sane_start: set_gamma( s, 0x%x ).\n", val);
4665 status = set_gamma (s, val);
4666
4667 if (SANE_STATUS_GOOD != status)
4668 {
4669 DBG (1, "sane_start: set_gamma failed: %s\n", sane_strstatus (status));
4670 return status;
4671 }
4672 }
4673
4674 if (s->hw->cmd->set_gamma_table &&
4675 gamma_userdefined[s->val[OPT_GAMMA_CORRECTION].w])
4676 { /* user defined. */
4677 status = set_gamma_table (s);
4678
4679 if (SANE_STATUS_GOOD != status)
4680 {
4681 DBG (1, "sane_start: set_gamma_table failed: %s\n",
4682 sane_strstatus (status));
4683 return status;
4684 }
4685 }
4686
4687 /*
4688 * TODO: think about if SANE_OPTION_IS_ACTIVE is a good criteria to send commands.
4689 */
4690
4691 if (SANE_OPTION_IS_ACTIVE (s->opt[OPT_COLOR_CORRECTION].cap))
4692 {
4693 int val = color_params[s->val[OPT_COLOR_CORRECTION].w];
4694
4695 DBG (1, "sane_start: set_color_correction( s, 0x%x )\n", val);
4696 status = set_color_correction (s, val);
4697
4698 if (SANE_STATUS_GOOD != status)
4699 {
4700 DBG (1, "sane_start: set_color_correction failed: %s\n",
4701 sane_strstatus (status));
4702 return status;
4703 }
4704 }
4705 if (1 == s->val[OPT_COLOR_CORRECTION].w)
4706 { /* user defined. */
4707 status = set_color_correction_coefficients (s);
4708
4709 if (SANE_STATUS_GOOD != status)
4710 {
4711 DBG (1, "sane_start: set_color_correction_coefficients failed: %s\n",
4712 sane_strstatus (status));
4713 return status;
4714 }
4715 }
4716
4717
4718 if (s->hw->cmd->set_threshold != 0
4719 && SANE_OPTION_IS_ACTIVE (s->opt[OPT_THRESHOLD].cap))
4720 {
4721 status = set_threshold (s, s->val[OPT_THRESHOLD].w);
4722
4723 if (SANE_STATUS_GOOD != status)
4724 {
4725 DBG (1, "sane_start: set_threshold(%d) failed: %s\n",
4726 s->val[OPT_THRESHOLD].w, sane_strstatus (status));
4727 return status;
4728 }
4729 }
4730
4731 ndpi = s->val[OPT_RESOLUTION].w;
4732
4733 status = set_resolution (s, ndpi, ndpi);
4734
4735 if (SANE_STATUS_GOOD != status)
4736 {
4737 DBG (1, "sane_start: set_resolution(%d, %d) failed: %s\n",
4738 ndpi, ndpi, sane_strstatus (status));
4739 return status;
4740 }
4741
4742 status = sane_get_parameters (handle, NULL);
4743
4744 if (status != SANE_STATUS_GOOD)
4745 return status;
4746
4747 /* set the zoom */
4748 if (s->hw->cmd->set_zoom != 0
4749 && SANE_OPTION_IS_ACTIVE (s->opt[OPT_ZOOM].cap))
4750 {
4751 status = set_zoom (s, s->val[OPT_ZOOM].w, s->val[OPT_ZOOM].w);
4752 if (status != SANE_STATUS_GOOD)
4753 {
4754 DBG (1, "sane_start: set_zoom(%d) failed: %s\n",
4755 s->val[OPT_ZOOM].w, sane_strstatus (status));
4756 return status;
4757 }
4758 }
4759
4760
4761 /*
4762 * Now s->params is initialized.
4763 */
4764
4765
4766 /*
4767 * If WAIT_FOR_BUTTON is active, then do just that: Wait until the button is
4768 * pressed. If the button was already pressed, then we will get the button
4769 * Pressed event right away.
4770 */
4771
4772 if (s->val[OPT_WAIT_FOR_BUTTON].w == SANE_TRUE)
4773 {
4774 s->hw->wait_for_button = SANE_TRUE;
4775
4776 while (s->hw->wait_for_button == SANE_TRUE)
4777 {
4778 if (s->canceling == SANE_TRUE)
4779 {
4780 s->hw->wait_for_button = SANE_FALSE;
4781 }
4782 /* get the button status from the scanner */
4783 else if (request_push_button_status (s, &button_status) ==
4784 SANE_STATUS_GOOD)
4785 {
4786 if (button_status == SANE_TRUE)
4787 {
4788 s->hw->wait_for_button = SANE_FALSE;
4789 }
4790 else
4791 {
4792 sleep (1);
4793 }
4794 }
4795 else
4796 {
4797 /* we ran into an error condition, just continue */
4798 s->hw->wait_for_button = SANE_FALSE;
4799 }
4800 }
4801 }
4802
4803
4804 /*
4805 * in file:frontend/preview.c
4806 *
4807 * The preview strategy is as follows:
4808 *
4809 * 1) A preview always acquires an image that covers the entire
4810 * scan surface. This is necessary so the user can see not
4811 * only what is, but also what isn't selected.
4812 */
4813
4814 left = SANE_UNFIX (s->val[OPT_TL_X].w) / 25.4 * ndpi + 0.5;
4815 top = SANE_UNFIX (s->val[OPT_TL_Y].w) / 25.4 * ndpi + 0.5;
4816
4817 /*
4818 * Calculate correction for line_distance in D1 scanner:
4819 * Start line_distance lines earlier and add line_distance lines at the end
4820 *
4821 * Because the actual line_distance is not yet calculated we have to do this
4822 * first.
4823 */
4824
4825 s->hw->color_shuffle = SANE_FALSE;
4826 s->current_output_line = 0;
4827 s->lines_written = 0;
4828 s->color_shuffle_line = 0;
4829
4830 if ((s->hw->optical_res != 0) && (mparam->depth == 8)
4831 && (mparam->mode_flags != 0))
4832 {
4833 s->line_distance = s->hw->max_line_distance * ndpi / s->hw->optical_res;
4834 if (s->line_distance != 0)
4835 {
4836 s->hw->color_shuffle = SANE_TRUE;
4837 }
4838 else
4839 s->hw->color_shuffle = SANE_FALSE;
4840 }
4841
4842 /*
4843 * for debugging purposes:
4844 */
4845 #ifdef FORCE_COLOR_SHUFFLE
4846 DBG (1, "Test mode: FORCE_COLOR_SHUFFLE = TRUE\n");
4847 s->hw->color_shuffle = SANE_TRUE;
4848 #endif
4849
4850
4851 /*
4852 * Modify the scan area: If the scanner requires color shuffling, then we try to
4853 * scan more lines to compensate for the lines that will be removed from the scan
4854 * due to the color shuffling algorithm.
4855 * At this time we add two times the line distance to the number of scan lines if
4856 * this is possible - if not, then we try to calculate the number of additional
4857 * lines according to the selected scan area.
4858 */
4859 if (s->hw->color_shuffle == SANE_TRUE)
4860 {
4861
4862 /* start the scan 2*line_distance earlier */
4863 top -= 2 * s->line_distance;
4864 if (top < 0)
4865 {
4866 top = 0;
4867 }
4868
4869 /* scan 4*line_distance lines more */
4870 s->params.lines += 4 * s->line_distance;
4871 }
4872
4873 /*
4874 * If (top + s->params.lines) is larger than the max scan area, reset
4875 * the number of scan lines:
4876 */
4877 if (SANE_UNFIX (s->val[OPT_BR_Y].w) / 25.4 * ndpi < (s->params.lines + top))
4878 {
4879 s->params.lines = ((int) SANE_UNFIX (s->val[OPT_BR_Y].w) /
4880 25.4 * ndpi + 0.5) - top;
4881 }
4882
4883
4884 status =
4885 set_scan_area (s, left, top, s->params.pixels_per_line, s->params.lines);
4886
4887 if (SANE_STATUS_GOOD != status)
4888 {
4889 DBG (1, "sane_start: set_scan_area failed: %s\n",
4890 sane_strstatus (status));
4891 return status;
4892 }
4893
4894 s->block = SANE_FALSE;
4895 lcount = 1;
4896
4897 /*
4898 * The set line count commands needs to be sent for certain scanners in
4899 * color mode. The D1 level requires it, we are however only testing for
4900 * 'D' and not for the actual numeric level.
4901 */
4902
4903 if (((s->hw->cmd->level[0] == 'B') &&
4904 ((s->hw->level >= 5) || ((s->hw->level >= 4) &&
4905 (!mode_params[s->val[OPT_MODE].w].color))))
4906 || (s->hw->cmd->level[0] == 'D'))
4907 {
4908 s->block = SANE_TRUE;
4909 lcount = sanei_scsi_max_request_size / s->params.bytes_per_line;
4910
4911 if (lcount >= 255)
4912 {
4913 lcount = 255;
4914 }
4915
4916 if (s->hw->TPU && s->hw->use_extension && lcount > 32)
4917 {
4918 lcount = 32;
4919 }
4920
4921
4922 /*
4923 * The D1 series of scanners only allow an even line number
4924 * for bi-level scanning. If a bit depth of 1 is selected, then
4925 * make sure the next lower even number is selected.
4926 */
4927 if (s->hw->cmd->level[0] == 'D')
4928 {
4929 if (lcount % 2)
4930 {
4931 lcount -= 1;
4932 }
4933 }
4934
4935 if (lcount == 0)
4936 {
4937 DBG (1, "out of memory (line %d)\n", __LINE__);
4938 return SANE_STATUS_NO_MEM;
4939 }
4940
4941 status = set_lcount (s, lcount);
4942
4943 if (SANE_STATUS_GOOD != status)
4944 {
4945 DBG (1, "sane_start: set_lcount(%d) failed: %s\n",
4946 lcount, sane_strstatus (status));
4947 return status;
4948 }
4949 }
4950
4951 if (s->hw->cmd->request_extended_status != 0
4952 && SANE_TRUE == s->hw->extension)
4953 {
4954 u_char result[4];
4955 u_char *buf;
4956 size_t len;
4957
4958 params[0] = ESC;
4959 params[1] = s->hw->cmd->request_extended_status;
4960
4961 send (s, params, 2, &status); /* send ESC f (request extended status) */
4962
4963 if (SANE_STATUS_GOOD == status)
4964 {
4965 len = 4; /* receive header */
4966
4967 receive (s, result, len, &status);
4968 if (SANE_STATUS_GOOD != status)
4969 return status;
4970
4971 len = result[3] << 8 | result[2];
4972 buf = alloca (len);
4973
4974 receive (s, buf, len, &status); /* receive actual status data */
4975
4976 if (buf[0] & 0x80)
4977 {
4978 close_scanner (s);
4979 return SANE_STATUS_INVAL;
4980 }
4981 }
4982 else
4983 {
4984 DBG (1, "Extended status flag request failed\n");
4985 }
4986 }
4987
4988 /*
4989 * for debug purpose
4990 * check scanner conditions
4991 */
4992 #if 1
4993 if (s->hw->cmd->request_condition != 0)
4994 {
4995 u_char result[4];
4996 u_char *buf;
4997 size_t len;
4998
4999 params[0] = ESC;
5000 params[1] = s->hw->cmd->request_condition;
5001
5002 send (s, params, 2, &status); /* send request condition */
5003
5004 if (SANE_STATUS_GOOD != status)
5005 return status;
5006
5007 len = 4;
5008 receive (s, result, len, &status);
5009
5010 if (SANE_STATUS_GOOD != status)
5011 return status;
5012
5013 len = result[3] << 8 | result[2];
5014 buf = alloca (len);
5015 receive (s, buf, len, &status);
5016
5017 if (SANE_STATUS_GOOD != status)
5018 return status;
5019
5020 #if 0
5021 DBG (10, "SANE_START: length=%d\n", len);
5022 for (i = 1; i <= len; i++)
5023 {
5024 DBG (10, "SANE_START: %d: %c\n", i, buf[i - 1]);
5025 }
5026 #endif
5027
5028 DBG (5, "SANE_START: Color: %d\n", (int) buf[1]);
5029 DBG (5, "SANE_START: Resolution (x, y): (%d, %d)\n",
5030 (int) (buf[4] << 8 | buf[3]), (int) (buf[6] << 8 | buf[5]));
5031 DBG (5,
5032 "SANE_START: Scan area(pixels) (x0, y0), (x1, y1): (%d, %d), (%d, %d)\n",
5033 (int) (buf[9] << 8 | buf[8]), (int) (buf[11] << 8 | buf[10]),
5034 (int) (buf[13] << 8 | buf[12]), (int) (buf[15] << 8 | buf[14]));
5035 DBG (5, "SANE_START: Data format: %d\n", (int) buf[17]);
5036 DBG (5, "SANE_START: Halftone: %d\n", (int) buf[19]);
5037 DBG (5, "SANE_START: Brightness: %d\n", (int) buf[21]);
5038 DBG (5, "SANE_START: Gamma: %d\n", (int) buf[23]);
5039 DBG (5, "SANE_START: Zoom (x, y): (%d, %d)\n", (int) buf[26],
5040 (int) buf[25]);
5041 DBG (5, "SANE_START: Color correction: %d\n", (int) buf[28]);
5042 DBG (5, "SANE_START: Sharpness control: %d\n", (int) buf[30]);
5043 DBG (5, "SANE_START: Scanning mode: %d\n", (int) buf[32]);
5044 DBG (5, "SANE_START: Mirroring: %d\n", (int) buf[34]);
5045 DBG (5, "SANE_START: Auto area segmentation: %d\n", (int) buf[36]);
5046 DBG (5, "SANE_START: Threshold: %d\n", (int) buf[38]);
5047 DBG (5, "SANE_START: Line counter: %d\n", (int) buf[40]);
5048 DBG (5, "SANE_START: Option unit control: %d\n", (int) buf[42]);
5049 DBG (5, "SANE_START: Film type: %d\n", (int) buf[44]);
5050 }
5051 #endif
5052
5053
5054 /* set the retry count to 0 */
5055 s->retry_count = 0;
5056
5057 if (s->hw->color_shuffle == SANE_TRUE)
5058 {
5059
5060 /* initialize the line buffers */
5061 for (i = 0; i < s->line_distance * 2 + 1; i++)
5062 {
5063 if (s->line_buffer[i] != NULL)
5064 free (s->line_buffer[i]);
5065
5066 s->line_buffer[i] = malloc (s->params.bytes_per_line);
5067 if (s->line_buffer[i] == NULL)
5068 {
5069 /* free the memory we've malloced so far */
5070 for (j = 0; j < i; j++)
5071 {
5072 free (s->line_buffer[j]);
5073 s->line_buffer[j] = NULL;
5074 }
5075 DBG (1, "out of memory (line %d)\n", __LINE__);
5076 return SANE_STATUS_NO_MEM;
5077 }
5078 }
5079 }
5080
5081 params[0] = ESC;
5082 params[1] = s->hw->cmd->start_scanning;
5083
5084 send (s, params, 2, &status);
5085
5086 if (SANE_STATUS_GOOD != status)
5087 {
5088 DBG (1, "sane_start: start failed: %s\n", sane_strstatus (status));
5089 return status;
5090 }
5091
5092 s->eof = SANE_FALSE;
5093 s->buf = realloc (s->buf, lcount * s->params.bytes_per_line);
5094 s->ptr = s->end = s->buf;
5095 s->canceling = SANE_FALSE;
5096
5097 return SANE_STATUS_GOOD;
5098 } /* sane_start */
5099
5100 /*
5101 *
5102 * TODO: clean up the eject and direct cmd mess.
5103 */
5104
5105 SANE_Status
sane_auto_eject(Epson_Scanner * s)5106 sane_auto_eject (Epson_Scanner * s)
5107 {
5108
5109 DBG (5, "sane_auto_eject()\n");
5110
5111 if (s->hw->ADF && s->hw->use_extension && s->val[OPT_AUTO_EJECT].w)
5112 { /* sequence! */
5113 SANE_Status status;
5114
5115 u_char params[1];
5116 u_char cmd = s->hw->cmd->eject;
5117
5118 if (!cmd)
5119 return SANE_STATUS_UNSUPPORTED;
5120
5121 params[0] = cmd;
5122
5123 send (s, params, 1, &status);
5124
5125 if (SANE_STATUS_GOOD != (status = expect_ack (s)))
5126 {
5127 return status;
5128 }
5129 }
5130
5131 return SANE_STATUS_GOOD;
5132 }
5133
5134 /*
5135 *
5136 *
5137 */
5138
5139 static SANE_Status
read_data_block(Epson_Scanner * s, EpsonDataRec * result)5140 read_data_block (Epson_Scanner * s, EpsonDataRec * result)
5141 {
5142 SANE_Status status;
5143 u_char param[3];
5144
5145 receive (s, result, s->block ? 6 : 4, &status);
5146
5147 if (SANE_STATUS_GOOD != status)
5148 return status;
5149
5150 if (STX != result->code)
5151 {
5152 DBG (1, "code %02x\n", (int) result->code);
5153 DBG (1, "error, expected STX\n");
5154
5155 return SANE_STATUS_INVAL;
5156 }
5157
5158 if (result->status & STATUS_FER)
5159 {
5160 int dummy_x, dummy_y;
5161
5162 DBG (1, "fatal error - Status = %02x\n", result->status);
5163
5164 status = check_ext_status (s, &dummy_x, &dummy_y);
5165
5166 /*
5167 * Hack Alert!!!
5168 * If the status is SANE_STATUS_DEVICE_BUSY then we need to
5169 * re-issue the command again. We can assume that the command that
5170 * caused this problem was ESC G, so in a loop with a sleep 1 we
5171 * are testing this over and over and over again, until the lamp
5172 * "thinks" it is ready.
5173 *
5174 * TODO: Store the last command and execute what was actually used
5175 * as the last command. For all situations this error may occur
5176 * ESC G is very very likely to be the command in question, but
5177 * we better make sure that this is the case.
5178 *
5179 */
5180
5181 /*
5182 * let's safe some stack space: If this is not the first go around,
5183 * then just return the status and let the loop handle this - otherwise
5184 * we would run this function recursively.
5185 */
5186
5187 if ((status == SANE_STATUS_DEVICE_BUSY && s->retry_count > 0) ||
5188 (status == SANE_STATUS_GOOD && s->retry_count > 0))
5189 {
5190 return SANE_STATUS_DEVICE_BUSY; /* return busy even if we just read OK
5191 so that the following loop can end
5192 gracefully */
5193 }
5194
5195 while (status == SANE_STATUS_DEVICE_BUSY)
5196 {
5197 if (s->retry_count > SANE_EPSON_MAX_RETRIES)
5198 {
5199 DBG (1, "Max retry count exceeded (%d)\n", s->retry_count);
5200 return SANE_STATUS_INVAL;
5201 }
5202
5203 sleep (1); /* wait one second for the next attempt */
5204
5205 DBG (1, "retrying ESC G - %d\n", ++(s->retry_count));
5206
5207 param[0] = ESC;
5208 param[1] = s->hw->cmd->start_scanning;
5209
5210 send (s, param, 2, &status);
5211
5212 if (SANE_STATUS_GOOD != status)
5213 {
5214 DBG (1, "read_data_block: start failed: %s\n",
5215 sane_strstatus (status));
5216 return status;
5217 }
5218
5219 status = read_data_block (s, result);
5220 }
5221
5222 }
5223
5224 return status;
5225 }
5226
5227 /*
5228 *
5229 *
5230 */
5231
5232
5233 void
scan_finish(Epson_Scanner * s)5234 scan_finish (Epson_Scanner * s)
5235 {
5236 SANE_Status status;
5237 int i, x, y;
5238
5239 DBG (5, "scan_finish()\n");
5240
5241 free (s->buf);
5242 s->buf = NULL;
5243
5244 status = check_ext_status (s, &x, &y);
5245
5246 if (SANE_STATUS_NO_DOCS == status && s->hw->ADF && s->hw->use_extension)
5247 sane_auto_eject (s);
5248
5249 for (i = 0; i < s->line_distance; i++)
5250 {
5251 if (s->line_buffer[i] != NULL)
5252 {
5253 free (s->line_buffer[i]);
5254 s->line_buffer[i] = NULL;
5255 }
5256 }
5257 }
5258
5259 #define GET_COLOR(x) ((x.status>>2) & 0x03)
5260
5261 SANE_Status
sane_read(SANE_Handle handle, SANE_Byte * data, SANE_Int max_length, SANE_Int * length)5262 sane_read (SANE_Handle handle, SANE_Byte * data, SANE_Int max_length,
5263 SANE_Int * length)
5264 {
5265 Epson_Scanner *s = (Epson_Scanner *) handle;
5266 SANE_Status status;
5267 int index = 0;
5268 SANE_Bool reorder = SANE_FALSE;
5269 SANE_Bool needStrangeReorder = SANE_FALSE;
5270
5271 START_READ:
5272 DBG (5, "sane_read: begin\n");
5273
5274 if (s->ptr == s->end)
5275 {
5276 EpsonDataRec result;
5277 size_t buf_len;
5278
5279 if ((s->fd != -1) && s->eof)
5280 {
5281 if (s->hw->color_shuffle)
5282 {
5283 DBG (1, "Written %d lines after color shuffle\n", s->lines_written);
5284 DBG (1, "Lines requested: %d\n", s->params.lines);
5285 }
5286
5287 *length = 0;
5288 scan_finish (s);
5289
5290 return SANE_STATUS_EOF;
5291 }
5292
5293
5294 DBG (5, "sane_read: begin scan1\n");
5295
5296 if (SANE_STATUS_GOOD != (status = read_data_block (s, &result)))
5297 {
5298 *length = 0;
5299 scan_finish (s);
5300 return status;
5301 }
5302
5303 buf_len = result.buf[1] << 8 | result.buf[0];
5304
5305 DBG (5, "sane_read: buf len = %lu\n", (u_long) buf_len);
5306
5307 if (s->block)
5308 {
5309 buf_len *= (result.buf[3] << 8 | result.buf[2]);
5310 DBG (5, "sane_read: buf len (adjusted) = %lu\n", (u_long) buf_len);
5311 }
5312
5313 if (!s->block && SANE_FRAME_RGB == s->params.format)
5314 {
5315 /*
5316 * Read color data in line mode
5317 */
5318
5319
5320 /*
5321 * read the first color line - the number of bytes to read
5322 * is already known (from last call to read_data_block()
5323 * We determine where to write the line from the color information
5324 * in the data block. At the end we want the order RGB, but the
5325 * way the data is delivered does not guarantee this - actually it's
5326 * most likely that the order is GRB if it's not RGB!
5327 */
5328 switch (GET_COLOR (result))
5329 {
5330 case 1:
5331 index = 1;
5332 break;
5333 case 2:
5334 index = 0;
5335 break;
5336 case 3:
5337 index = 2;
5338 break;
5339 }
5340
5341 receive (s, s->buf + index * s->params.pixels_per_line, buf_len,
5342 &status);
5343
5344 if (SANE_STATUS_GOOD != status)
5345 return status;
5346 /*
5347 * send the ACK signal to the scanner in order to make
5348 * it ready for the next data block.
5349 */
5350 send (s, S_ACK, 1, &status);
5351
5352 /*
5353 * ... and request the next data block
5354 */
5355 if (SANE_STATUS_GOOD != (status = read_data_block (s, &result)))
5356 return status;
5357
5358 buf_len = result.buf[1] << 8 | result.buf[0];
5359 /*
5360 * this should never happen, because we are already in
5361 * line mode, but it does not hurt to check ...
5362 */
5363 if (s->block)
5364 buf_len *= (result.buf[3] << 8 | result.buf[2]);
5365
5366 DBG (5, "sane_read: buf len2 = %lu\n", (u_long) buf_len);
5367
5368 switch (GET_COLOR (result))
5369 {
5370 case 1:
5371 index = 1;
5372 break;
5373 case 2:
5374 index = 0;
5375 break;
5376 case 3:
5377 index = 2;
5378 break;
5379 }
5380
5381 receive (s, s->buf + index * s->params.pixels_per_line, buf_len,
5382 &status);
5383
5384 if (SANE_STATUS_GOOD != status)
5385 {
5386 scan_finish (s);
5387 *length = 0;
5388 return status;
5389 }
5390
5391 send (s, S_ACK, 1, &status);
5392
5393 /*
5394 * ... and the last data block
5395 */
5396 if (SANE_STATUS_GOOD != (status = read_data_block (s, &result)))
5397 {
5398 *length = 0;
5399 scan_finish (s);
5400 return status;
5401 }
5402
5403 buf_len = result.buf[1] << 8 | result.buf[0];
5404
5405 if (s->block)
5406 buf_len *= (result.buf[3] << 8 | result.buf[2]);
5407
5408 DBG (5, "sane_read: buf len3 = %lu\n", (u_long) buf_len);
5409
5410 switch (GET_COLOR (result))
5411 {
5412 case 1:
5413 index = 1;
5414 break;
5415 case 2:
5416 index = 0;
5417 break;
5418 case 3:
5419 index = 2;
5420 break;
5421 }
5422
5423 receive (s, s->buf + index * s->params.pixels_per_line, buf_len,
5424 &status);
5425
5426 if (SANE_STATUS_GOOD != status)
5427 {
5428 *length = 0;
5429 scan_finish (s);
5430 return status;
5431 }
5432 }
5433 else
5434 {
5435 /*
5436 * Read data in block mode
5437 */
5438
5439 /* do we have to reorder the data ? */
5440 if (GET_COLOR (result) == 0x01)
5441 {
5442 reorder = SANE_TRUE;
5443 }
5444
5445 receive (s, s->buf, buf_len, &status);
5446
5447 if (SANE_STATUS_GOOD != status)
5448 {
5449 *length = 0;
5450 scan_finish (s);
5451 return status;
5452 }
5453 }
5454
5455 if (result.status & STATUS_AREA_END)
5456 {
5457 s->eof = SANE_TRUE;
5458 }
5459 else
5460 {
5461 if (s->canceling)
5462 {
5463 send (s, S_CAN, 1, &status);
5464 expect_ack (s);
5465
5466 *length = 0;
5467
5468 scan_finish (s);
5469
5470 return SANE_STATUS_CANCELLED;
5471 }
5472 else
5473 send (s, S_ACK, 1, &status);
5474 }
5475
5476 s->end = s->buf + buf_len;
5477 s->ptr = s->buf;
5478
5479 /*
5480 * if we have to re-order the color components (GRB->RGB) we
5481 * are doing this here:
5482 */
5483
5484 /*
5485 * Some scaners (e.g. the Perfection 1640 and GT-2200) seem
5486 * to have the R and G channels swapped.
5487 * The GT-8700 is the Asian version of the Perfection1640.
5488 * If the scanner name is one of these, and the scan mode is
5489 * RGB then swap the colors.
5490 */
5491
5492 needStrangeReorder =
5493 (strstr (s->hw->sane.model, "GT-2200") ||
5494 ((strstr (s->hw->sane.model, "1640") &&
5495 strstr (s->hw->sane.model, "Perfection")) ||
5496 strstr (s->hw->sane.model, "GT-8700"))) &&
5497 s->params.format == SANE_FRAME_RGB;
5498
5499 /*
5500 * Certain Perfection 1650 also need this re-ordering of the two
5501 * color channels. These scanners are identified by the problem
5502 * with the half vertical scanning area. When we corrected this,
5503 * we also set the variable s->hw->need_color_reorder
5504 */
5505 if (s->hw->need_color_reorder)
5506 {
5507 needStrangeReorder = SANE_TRUE;
5508 }
5509
5510 if (needStrangeReorder)
5511 reorder = SANE_FALSE; /* reordering once is enough */
5512
5513 if (s->params.format != SANE_FRAME_RGB)
5514 reorder = SANE_FALSE; /* don't reorder for BW or gray */
5515
5516 if (reorder)
5517 {
5518 SANE_Byte *ptr;
5519
5520 ptr = s->buf;
5521 while (ptr < s->end)
5522 {
5523 if (s->params.depth > 8)
5524 {
5525 SANE_Byte tmp;
5526
5527 /* R->G G->R */
5528 tmp = ptr[0];
5529 ptr[0] = ptr[2]; /* first Byte G */
5530 ptr[2] = tmp; /* first Byte R */
5531
5532 tmp = ptr[1];
5533 ptr[1] = ptr[3]; /* second Byte G */
5534 ptr[3] = tmp; /* second Byte R */
5535
5536 ptr += 6; /* go to next pixel */
5537 }
5538 else
5539 {
5540 /* R->G G->R */
5541 SANE_Byte tmp;
5542
5543 tmp = ptr[0];
5544 ptr[0] = ptr[1]; /* G */
5545 ptr[1] = tmp; /* R */
5546 /* B stays the same */
5547 ptr += 3; /* go to next pixel */
5548 }
5549 }
5550 }
5551
5552 /*
5553 * Do the color_shuffle if everything else is correct - at this time
5554 * most of the stuff is hardcoded for the Perfection 610
5555 */
5556
5557 if (s->hw->color_shuffle)
5558 {
5559 int new_length = 0;
5560
5561 status = color_shuffle (s, &new_length);
5562
5563 /*
5564 * If no bytes are returned, check if the scanner is already done, if so,
5565 * we'll probably just return, but if there is more data to process get
5566 * the next batch.
5567 */
5568
5569 if (new_length == 0 && s->end != s->ptr)
5570 {
5571 goto START_READ;
5572 }
5573
5574 s->end = s->buf + new_length;
5575 s->ptr = s->buf;
5576
5577 }
5578
5579
5580 DBG (5, "sane_read: begin scan2\n");
5581 }
5582
5583
5584
5585 /*
5586 * copy the image data to the data memory area
5587 */
5588
5589 if (!s->block && SANE_FRAME_RGB == s->params.format)
5590 {
5591
5592 max_length /= 3;
5593
5594 if (max_length > s->end - s->ptr)
5595 max_length = s->end - s->ptr;
5596
5597 *length = 3 * max_length;
5598
5599 if (s->invert_image == SANE_TRUE)
5600 {
5601 while (max_length-- != 0)
5602 {
5603 /* invert the three values */
5604 *data++ = (u_char) ~ (s->ptr[0]);
5605 *data++ = (u_char) ~ (s->ptr[s->params.pixels_per_line]);
5606 *data++ = (u_char) ~ (s->ptr[2 * s->params.pixels_per_line]);
5607 ++s->ptr;
5608 }
5609 }
5610 else
5611 {
5612 while (max_length-- != 0)
5613 {
5614 *data++ = s->ptr[0];
5615 *data++ = s->ptr[s->params.pixels_per_line];
5616 *data++ = s->ptr[2 * s->params.pixels_per_line];
5617 ++s->ptr;
5618 }
5619 }
5620 }
5621 else
5622 {
5623 if (max_length > s->end - s->ptr)
5624 max_length = s->end - s->ptr;
5625
5626 *length = max_length;
5627
5628 if (1 == s->params.depth)
5629 {
5630 if (s->invert_image == SANE_TRUE)
5631 {
5632 while (max_length-- != 0)
5633 *data++ = *s->ptr++;
5634 }
5635 else
5636 {
5637 while (max_length-- != 0)
5638 *data++ = ~*s->ptr++;
5639 }
5640 }
5641 else
5642 {
5643
5644 if (s->invert_image == SANE_TRUE)
5645 {
5646 int i;
5647
5648 for (i = 0; i < max_length; i++)
5649 {
5650 data[i] = (u_char) ~ (s->ptr[i]);
5651 }
5652 }
5653 else
5654 {
5655 memcpy (data, s->ptr, max_length);
5656 }
5657 s->ptr += max_length;
5658 }
5659 }
5660
5661 DBG (5, "sane_read: end\n");
5662
5663 return SANE_STATUS_GOOD;
5664 }
5665
5666
5667 static SANE_Status
color_shuffle(SANE_Handle handle, int *new_length)5668 color_shuffle (SANE_Handle handle, int *new_length)
5669 {
5670 Epson_Scanner *s = (Epson_Scanner *) handle;
5671 SANE_Byte *buf = s->buf;
5672 int length = s->end - s->buf;
5673
5674 if (s->hw->color_shuffle == SANE_TRUE)
5675 {
5676 SANE_Byte *data_ptr; /* ptr to data to process */
5677 SANE_Byte *data_end; /* ptr to end of processed data */
5678 SANE_Byte *out_data_ptr; /* ptr to memory when writing data */
5679 int i; /* loop counter */
5680
5681 /*
5682 * It looks like we are dealing with a scanner that has an odd way
5683 * of dealing with colors... The red and blue scan lines are shifted
5684 * up or down by a certain number of lines relative to the green line.
5685 */
5686 DBG (5, "sane_read: color_shuffle\n");
5687
5688
5689 /*
5690 * Initialize the variables we are going to use for the
5691 * copying of the data. data_ptr is the pointer to
5692 * the currently worked on scan line. data_end is the
5693 * end of the data area as calculated from adding *length
5694 * to the start of data.
5695 * out_data_ptr is used when writing out the processed data
5696 * and always points to the beginning of the next line to
5697 * write.
5698 */
5699
5700 data_ptr = out_data_ptr = buf;
5701 data_end = data_ptr + length;
5702
5703 /*
5704 * The image data is in *buf, we know that the buffer contains s->end - s->buf ( = length)
5705 * bytes of data. The width of one line is in s->params.bytes_per_line
5706 */
5707
5708 /*
5709 * The buffer area is supposed to have a number of full scan
5710 * lines, let's test if this is the case.
5711 */
5712
5713 if (length % s->params.bytes_per_line != 0)
5714 {
5715 DBG (1, "ERROR in size of buffer: %d / %d\n",
5716 length, s->params.bytes_per_line);
5717 return SANE_STATUS_INVAL;
5718 }
5719
5720 while (data_ptr < data_end)
5721 {
5722 SANE_Byte *source_ptr, *dest_ptr;
5723 int loop;
5724
5725 /* copy the green information into the current line */
5726
5727 source_ptr = data_ptr + 1;
5728 dest_ptr = s->line_buffer[s->color_shuffle_line] + 1;
5729
5730 for (i = 0; i < s->params.bytes_per_line / 3; i++)
5731 {
5732 *dest_ptr = *source_ptr;
5733 dest_ptr += 3;
5734 source_ptr += 3;
5735 }
5736
5737 /* copy the red information n lines back */
5738
5739 if (s->color_shuffle_line >= s->line_distance)
5740 {
5741 source_ptr = data_ptr + 2;
5742 dest_ptr =
5743 s->line_buffer[s->color_shuffle_line - s->line_distance] + 2;
5744
5745 /* while (source_ptr < s->line_buffer[s->color_shuffle_line] + s->params.bytes_per_line) */
5746 for (loop = 0; loop < s->params.bytes_per_line / 3; loop++)
5747
5748 {
5749 *dest_ptr = *source_ptr;
5750 dest_ptr += 3;
5751 source_ptr += 3;
5752 }
5753 }
5754
5755 /* copy the blue information n lines forward */
5756
5757 source_ptr = data_ptr;
5758 dest_ptr = s->line_buffer[s->color_shuffle_line + s->line_distance];
5759
5760 /* while (source_ptr < s->line_buffer[s->color_shuffle_line] + s->params.bytes_per_line) */
5761 for (loop = 0; loop < s->params.bytes_per_line / 3; loop++)
5762 {
5763 *dest_ptr = *source_ptr;
5764 dest_ptr += 3;
5765 source_ptr += 3;
5766 }
5767
5768 data_ptr += s->params.bytes_per_line;
5769
5770 if (s->color_shuffle_line == s->line_distance)
5771 {
5772 /*
5773 * we just finished the line in line_buffer[0] - write it to the
5774 * output buffer and continue.
5775 */
5776
5777
5778 /*
5779 * The output buffer is still "buf", but because we are
5780 * only overwriting from the beginning of the memory area
5781 * we are not interfering with the "still to shuffle" data
5782 * in the same area.
5783 */
5784
5785 /*
5786 * Strip the first and last n lines and limit to
5787 */
5788 if ((s->current_output_line >= s->line_distance) &&
5789 (s->current_output_line < s->params.lines + s->line_distance))
5790 {
5791 memcpy (out_data_ptr, s->line_buffer[0], s->params.bytes_per_line);
5792 out_data_ptr += s->params.bytes_per_line;
5793
5794 s->lines_written++;
5795 }
5796
5797 s->current_output_line++;
5798
5799
5800 /*
5801 * Now remove the 0-entry and move all other
5802 * lines up by one. There are 2*line_distance + 1
5803 * buffers, * therefore the loop has to run from 0
5804 * to * 2*line_distance, and because we want to
5805 * copy every n+1st entry to n the loop runs
5806 * from - to 2*line_distance-1!
5807 */
5808
5809 free (s->line_buffer[0]);
5810
5811 for (i = 0; i < s->line_distance * 2; i++)
5812 {
5813 s->line_buffer[i] = s->line_buffer[i + 1];
5814 }
5815
5816 /*
5817 * and create one new buffer at the end
5818 */
5819
5820 s->line_buffer[s->line_distance * 2] =
5821 malloc (s->params.bytes_per_line);
5822 if (s->line_buffer[s->line_distance * 2] == NULL)
5823 {
5824 int i;
5825 for (i = 0; i < s->line_distance * 2; i++)
5826 {
5827 free (s->line_buffer[i]);
5828 s->line_buffer[i] = NULL;
5829 }
5830 DBG (1, "out of memory (line %d)\n", __LINE__);
5831 return SANE_STATUS_NO_MEM;
5832 }
5833 }
5834 else
5835 {
5836 s->color_shuffle_line++; /* increase the buffer number */
5837 }
5838 }
5839
5840 /*
5841 * At this time we've used up all the new data from the scanner, some of
5842 * it is still in the line_buffers, but we are ready to return some of it
5843 * to the front end software. To do so we have to adjust the size of the
5844 * data area and the *new_length variable.
5845 */
5846
5847 *new_length = out_data_ptr - buf;
5848 }
5849
5850 return SANE_STATUS_GOOD;
5851
5852 }
5853
5854
5855
5856
5857 /*
5858 * static SANE_Status get_identity_information ( SANE_Handle handle)
5859 *
5860 * Request Identity information from scanner and fill in information
5861 * into dev and/or scanner structures.
5862 */
5863 static SANE_Status
get_identity_information(SANE_Handle handle)5864 get_identity_information (SANE_Handle handle)
5865 {
5866 Epson_Scanner *s = (Epson_Scanner *) handle;
5867 Epson_Device *dev = s->hw;
5868 EpsonIdent ident;
5869 u_char param[3];
5870 SANE_Status status;
5871 u_char *buf;
5872
5873 DBG (5, "get_identity_information()\n");
5874
5875 if (!s->hw->cmd->request_identity)
5876 return SANE_STATUS_INVAL;
5877
5878
5879 param[0] = ESC;
5880 param[1] = s->hw->cmd->request_identity;
5881 param[2] = '\0';
5882
5883 if (NULL == (ident = (EpsonIdent) command (s, param, 2, &status)))
5884 {
5885 DBG (1, "ident failed\n");
5886 return SANE_STATUS_INVAL;
5887 }
5888
5889 DBG (1, "type %3c 0x%02x\n", ident->type, ident->type);
5890 DBG (1, "level %3c 0x%02x\n", ident->level, ident->level);
5891
5892 {
5893 char *force = getenv ("SANE_EPSON_CMD_LVL");
5894
5895 if (force)
5896 {
5897 ident->type = force[0];
5898 ident->level = force[1];
5899
5900 DBG (1, "type %3c 0x%02x\n", ident->type, ident->type);
5901 DBG (1, "level %3c 0x%02x\n", ident->level, ident->level);
5902
5903 DBG (1, "forced\n");
5904 }
5905 }
5906
5907 /*
5908 * check if option equipment is installed.
5909 */
5910
5911 if (ident->status & STATUS_OPTION)
5912 {
5913 DBG (1, "option equipment is installed\n");
5914 dev->extension = SANE_TRUE;
5915 }
5916 else
5917 {
5918 DBG (1, "no option equipment installed\n");
5919 dev->extension = SANE_FALSE;
5920 }
5921
5922 dev->TPU = SANE_FALSE;
5923 dev->ADF = SANE_FALSE;
5924
5925 /*
5926 * set command type and level.
5927 */
5928
5929 {
5930 int n;
5931
5932 for (n = 0; n < NELEMS (epson_cmd); n++)
5933 {
5934 char type_level[3];
5935 sprintf(type_level, "%c%c", ident->type, ident->level);
5936 if (!strncmp (type_level, epson_cmd[n].level, 2))
5937 break;
5938 }
5939
5940 if (n < NELEMS (epson_cmd))
5941 {
5942 dev->cmd = &epson_cmd[n];
5943 }
5944 else
5945 {
5946 dev->cmd = &epson_cmd[EPSON_LEVEL_DEFAULT];
5947 DBG (1, "Unknown type %c or level %c, using %s\n",
5948 ident->type, ident->level, dev->cmd->level);
5949 }
5950
5951 s->hw->level = dev->cmd->level[1] - '0';
5952 } /* set command type and level */
5953
5954 /*
5955 * Setting available resolutions and xy ranges for sane frontend.
5956 */
5957
5958 s->hw->res_list_size = 0;
5959 s->hw->res_list =
5960 (SANE_Int *) calloc (s->hw->res_list_size, sizeof (SANE_Int));
5961
5962 if (NULL == s->hw->res_list)
5963 {
5964 DBG (1, "out of memory (line %d)\n", __LINE__);
5965 return SANE_STATUS_NO_MEM;
5966 }
5967
5968 {
5969 int n, k;
5970 int x = 0, y = 0;
5971 int count = ident->count2 * 255 + ident->count1;
5972
5973 /* we need to correct for the difference in size between
5974 the EpsonIdentRec and the EpsonHdrRec */
5975 int correction = sizeof (EpsonIdentRec) - sizeof (EpsonHdrRec);
5976 for (n = (count - correction), buf = ident->buf; n; n -= k, buf += k)
5977 {
5978 switch (*buf)
5979 {
5980 case 'R':
5981 {
5982 int val = buf[2] << 8 | buf[1];
5983
5984 s->hw->res_list_size++;
5985 s->hw->res_list =
5986 (SANE_Int *) realloc (s->hw->res_list,
5987 s->hw->res_list_size * sizeof (SANE_Int));
5988
5989 if (NULL == s->hw->res_list)
5990 {
5991 DBG (1, "out of memory (line %d)\n", __LINE__);
5992 return SANE_STATUS_NO_MEM;
5993 }
5994
5995 s->hw->res_list[s->hw->res_list_size - 1] = (SANE_Int) val;
5996
5997 DBG (1, "resolution (dpi): %d\n", val);
5998 k = 3;
5999 continue;
6000 }
6001 case 'A':
6002 {
6003 x = buf[2] << 8 | buf[1];
6004 y = buf[4] << 8 | buf[3];
6005
6006 DBG (1, "maximum scan area: x %d y %d\n", x, y);
6007 k = 5;
6008
6009 /*
6010 * Check for Perfection 4990 photo/GT-X800 scanner.
6011 * This scanner only report 3200 dpi back.
6012 * The scanner physically supports 4800 dpi.
6013 * This is simulated here...
6014 * Further details read:
6015 * EPSON Programming guide for EPSON Color Image Scanner Perfection 4990
6016 */
6017 if (s->hw->cmd->request_extended_status != 0)
6018 {
6019 u_char *buf;
6020 u_char params[2];
6021 EpsonHdr head;
6022
6023 params[0] = ESC;
6024 params[1] = s->hw->cmd->request_extended_status;
6025
6026 if (NULL != (head = (EpsonHdr) command (s, params, 2, &status)))
6027 {
6028 buf = &head->buf[0x1A];
6029 DBG (1, "product name %x %x %x %x %x %x %x %x \n", buf[0], buf[1],buf[2],buf[3],buf[4], buf[5],buf[6], buf[7] );
6030 if (strncmp((char *) buf,"GT-X800",7) == 0)
6031 {
6032 int val = 0x12 << 8 | 0xC0;
6033
6034 s->hw->res_list_size++;
6035 s->hw->res_list =
6036 (SANE_Int *) realloc (s->hw->res_list,
6037 s->hw->res_list_size * sizeof (SANE_Int));
6038
6039 if (NULL == s->hw->res_list)
6040 {
6041 DBG (1, "out of memory (line %d)\n", __LINE__);
6042 return SANE_STATUS_NO_MEM;
6043 }
6044
6045 s->hw->res_list[s->hw->res_list_size - 1] = (SANE_Int) val;
6046 x = (x/32)*48;
6047 y = (y/32)*48;
6048
6049 DBG (1, "resolution (dpi): %d\n", val);
6050 DBG (1, "maximum scan area GT-X800: x %d y %d\n", x, y);
6051 }
6052 }
6053 }
6054
6055 continue;
6056 }
6057 default:
6058 break;
6059 } /* case */
6060
6061 break;
6062 } /* for */
6063
6064 dev->dpi_range.min = s->hw->res_list[0];
6065 dev->dpi_range.max = s->hw->res_list[s->hw->res_list_size - 1];
6066 dev->dpi_range.quant = 0;
6067
6068 dev->fbf_x_range.min = 0;
6069 dev->fbf_x_range.max = SANE_FIX (x * 25.4 / dev->dpi_range.max);
6070 dev->fbf_x_range.quant = 0;
6071
6072 dev->fbf_y_range.min = 0;
6073 dev->fbf_y_range.max = SANE_FIX (y * 25.4 / dev->dpi_range.max);
6074 dev->fbf_y_range.quant = 0;
6075
6076 DBG (5, "fbf tlx %f tly %f brx %f bry %f [mm]\n",
6077 SANE_UNFIX (dev->fbf_x_range.min), SANE_UNFIX (dev->fbf_y_range.min),
6078 SANE_UNFIX (dev->fbf_x_range.max),
6079 SANE_UNFIX (dev->fbf_y_range.max));
6080
6081 }
6082
6083 /*
6084 * Copy the resolution list to the resolution_list array so that the frontend can
6085 * display the correct values
6086 */
6087
6088 s->hw->resolution_list =
6089 malloc ((s->hw->res_list_size + 1) * sizeof (SANE_Word));
6090
6091 if (s->hw->resolution_list == NULL)
6092 {
6093 DBG (1, "out of memory (line %d)\n", __LINE__);
6094 return SANE_STATUS_NO_MEM;
6095 }
6096 *(s->hw->resolution_list) = s->hw->res_list_size;
6097 memcpy (&(s->hw->resolution_list[1]), s->hw->res_list,
6098 s->hw->res_list_size * sizeof (SANE_Word));
6099
6100 /* filter the resolution list */
6101 /* the option is not yet initialized, for now just set it to false */
6102 s->val[OPT_LIMIT_RESOLUTION].w = SANE_FALSE;
6103 filter_resolution_list (s);
6104
6105 return SANE_STATUS_GOOD;
6106
6107 } /* request identity */
6108
6109
6110 /*
6111 * static SANE_Status get_identity2_information ( SANE_Handle handle)
6112 *
6113 * Request Identity2 information from scanner and fill in information
6114 * into dev and/or scanner structures.
6115 */
6116 static SANE_Status
get_identity2_information(SANE_Handle handle)6117 get_identity2_information (SANE_Handle handle)
6118 {
6119 Epson_Scanner *s = (Epson_Scanner *) handle;
6120 SANE_Status status;
6121 int len;
6122 u_char param[3];
6123 u_char result[4];
6124 u_char *buf;
6125
6126 DBG (5, "get_identity2_information()\n");
6127
6128 if (s->hw->cmd->request_identity2 == 0)
6129 return SANE_STATUS_UNSUPPORTED;
6130
6131 param[0] = ESC;
6132 param[1] = s->hw->cmd->request_identity2;
6133 param[2] = '\0';
6134
6135 send (s, param, 2, &status);
6136
6137 if (SANE_STATUS_GOOD != status)
6138 return status;
6139
6140 len = 4; /* receive header */
6141
6142 receive (s, result, len, &status);
6143 if (SANE_STATUS_GOOD != status)
6144 return status;
6145
6146 len = result[3] << 8 | result[2];
6147 buf = alloca (len);
6148
6149 receive (s, buf, len, &status); /* receive actual status data */
6150
6151 /* the first two bytes of the buffer contain the optical resolution */
6152 s->hw->optical_res = buf[1] << 8 | buf[0];
6153
6154 /*
6155 * the 4th and 5th byte contain the line distance. Both values have to
6156 * be identical, otherwise this software can not handle this scanner.
6157 */
6158 if (buf[4] != buf[5])
6159 {
6160 close_scanner (s);
6161 return SANE_STATUS_INVAL;
6162 }
6163 s->hw->max_line_distance = buf[4];
6164
6165 return SANE_STATUS_GOOD;
6166 }
6167
6168 /*
6169 * void sane_cancel(SANE_Handle handle)
6170 *
6171 * Set the cancel flag to true. The next time the backend requests data
6172 * from the scanner the CAN message will be sent.
6173 */
6174
6175 void
sane_cancel(SANE_Handle handle)6176 sane_cancel (SANE_Handle handle)
6177 {
6178 Epson_Scanner *s = (Epson_Scanner *) handle;
6179
6180 /*
6181 * If the s->ptr pointer is not NULL, then a scan operation
6182 * was started and if s->eof is FALSE, it was not finished.
6183 */
6184
6185 if (s->buf != NULL)
6186 {
6187 u_char *dummy;
6188 int len;
6189
6190 /* malloc one line */
6191 dummy = malloc (s->params.bytes_per_line);
6192 if (dummy == NULL)
6193 {
6194 DBG (1, "Out of memory\n");
6195 return;
6196 }
6197 else
6198 {
6199
6200 /* there is still data to read from the scanner */
6201
6202 s->canceling = SANE_TRUE;
6203
6204 while (!s->eof &&
6205 SANE_STATUS_CANCELLED != sane_read (s, dummy,
6206 s->params.bytes_per_line,
6207 &len))
6208 {
6209 /* empty body, the while condition does the processing */
6210 }
6211 free (dummy);
6212 }
6213
6214 }
6215 }
6216
6217
6218 static SANE_Status
request_focus_position(SANE_Handle handle, u_char * position)6219 request_focus_position (SANE_Handle handle, u_char * position)
6220 {
6221 Epson_Scanner *s = (Epson_Scanner *) handle;
6222 SANE_Status status;
6223 int len;
6224 u_char param[3];
6225 u_char result[4];
6226 u_char *buf;
6227
6228 DBG (5, "request_focus_position()\n");
6229
6230 if (s->hw->cmd->request_focus_position == 0)
6231 return SANE_STATUS_UNSUPPORTED;
6232
6233 param[0] = ESC;
6234 param[1] = s->hw->cmd->request_focus_position;
6235 param[2] = '\0';
6236
6237 send (s, param, 2, &status);
6238
6239 if (SANE_STATUS_GOOD != status)
6240 return status;
6241
6242 len = 4; /* receive header */
6243
6244 receive (s, result, len, &status);
6245 if (SANE_STATUS_GOOD != status)
6246 return status;
6247
6248 len = result[3] << 8 | result[2];
6249 buf = alloca (len);
6250
6251 receive (s, buf, len, &status); /* receive actual status data */
6252
6253 *position = buf[1];
6254 DBG (1, "Focus position = 0x%x\n", buf[1]);
6255
6256 return SANE_STATUS_GOOD;
6257 }
6258
6259
6260 /*
6261 * Request the push button status
6262 * returns SANE_TRUE if the button was pressed
6263 * and SANE_FALSE if the button was not pressed
6264 * it also returns SANE_TRUE in case of an error.
6265 * This is necessary so that a process that waits for
6266 * the button does not block indefinitely.
6267 */
6268 static SANE_Bool
request_push_button_status(SANE_Handle handle, SANE_Bool * theButtonStatus)6269 request_push_button_status (SANE_Handle handle, SANE_Bool * theButtonStatus)
6270 {
6271 Epson_Scanner *s = (Epson_Scanner *) handle;
6272 SANE_Status status;
6273 int len;
6274 u_char param[3];
6275 u_char result[4];
6276 u_char *buf;
6277
6278 DBG (5, "request_push_button_status()\n");
6279
6280 if (s->hw->cmd->request_push_button_status == 0)
6281 {
6282 DBG (1, "push button status unsupported\n");
6283 return SANE_STATUS_UNSUPPORTED;
6284 }
6285
6286 param[0] = ESC;
6287 param[1] = s->hw->cmd->request_push_button_status;
6288 param[2] = '\0';
6289
6290 send (s, param, 2, &status);
6291
6292 if (SANE_STATUS_GOOD != status)
6293 {
6294 DBG (1, "error sending command\n");
6295 return status;
6296 }
6297
6298 len = 4; /* receive header */
6299
6300 receive (s, result, len, &status);
6301 if (SANE_STATUS_GOOD != status)
6302 return status;
6303
6304 len = result[3] << 8 | result[2]; /* this should be 1 for scanners with one button */
6305 buf = alloca (len);
6306
6307 receive (s, buf, len, &status); /* receive actual status data */
6308
6309 DBG (1, "Push button status = %d\n", buf[0] & 0x01);
6310 *theButtonStatus = ((buf[0] & 0x01) != 0);
6311
6312 return (SANE_STATUS_GOOD);
6313 }
6314
6315
6316
6317 static void
filter_resolution_list(Epson_Scanner * s)6318 filter_resolution_list (Epson_Scanner * s)
6319 {
6320 /* re-create the list */
6321
6322 if (s->val[OPT_LIMIT_RESOLUTION].w == SANE_TRUE)
6323 {
6324 /* copy the short list */
6325
6326 /* filter out all values that are not 300 or 400 dpi based */
6327 int i;
6328
6329 int new_size = 0;
6330 SANE_Bool is_correct_resolution = SANE_FALSE;
6331
6332 for (i = 1; i <= s->hw->res_list_size; i++)
6333 {
6334 SANE_Word res;
6335 res = s->hw->res_list[i];
6336 if ((res < 100) || (0 == (res % 300)) || (0 == (res % 400)))
6337 {
6338 /* add the value */
6339 new_size++;
6340
6341 s->hw->resolution_list[new_size] = s->hw->res_list[i];
6342
6343 /* check for a valid current resolution */
6344 if (res == s->val[OPT_RESOLUTION].w)
6345 {
6346 is_correct_resolution = SANE_TRUE;
6347 }
6348 }
6349 }
6350 s->hw->resolution_list[0] = new_size;
6351
6352 if (is_correct_resolution == SANE_FALSE)
6353 {
6354 for (i = 1; i <= new_size; i++)
6355 {
6356 if (s->val[OPT_RESOLUTION].w < s->hw->resolution_list[i])
6357 {
6358 s->val[OPT_RESOLUTION].w = s->hw->resolution_list[i];
6359 i = new_size + 1;
6360 }
6361 }
6362 }
6363
6364 }
6365 else
6366 {
6367 /* copy the full list */
6368 s->hw->resolution_list[0] = s->hw->res_list_size;
6369 memcpy (&(s->hw->resolution_list[1]), s->hw->res_list,
6370 s->hw->res_list_size * sizeof (SANE_Word));
6371 }
6372 }
6373
6374 /**********************************************************************************/
6375
6376 /*
6377 * SANE_Status sane_set_io_mode()
6378 *
6379 * not supported - for asynchronous I/O
6380 */
6381
6382 SANE_Status
sane_set_io_mode(SANE_Handle handle, SANE_Bool non_blocking)6383 sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
6384 {
6385 /* get rid of compiler warning */
6386 (void) handle;
6387 (void) non_blocking;
6388
6389 return SANE_STATUS_UNSUPPORTED;
6390 }
6391
6392 /*
6393 * SANE_Status sane_get_select_fd()
6394 *
6395 * not supported - for asynchronous I/O
6396 */
6397
6398 SANE_Status
sane_get_select_fd(SANE_Handle handle, SANE_Int * fd)6399 sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
6400 {
6401 /* get rid of compiler warnings */
6402 (void) handle;
6403 (void) fd;
6404
6405 return SANE_STATUS_UNSUPPORTED;
6406 }
6407
6408 /*
6409 vim:ts=2:sw=2:cindent:
6410 */
6411