1 /*
2  * epson2.h - SANE library for Epson scanners.
3  *
4  * Based on Kazuhiro Sasayama previous
5  * Work on epson.[ch] file from the SANE package.
6  * Please see those files for original copyrights.
7  *
8  * Copyright (C) 2006 Tower Technologies
9  * Author: Alessandro Zummo <a.zummo@towertech.it>
10  *
11  * This file is part of the SANE package.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation, version 2.
16  */
17 
18 #ifndef epson2_h
19 #define epson2_h
20 
21 #undef BACKEND_NAME
22 #define BACKEND_NAME epson2
23 #define DEBUG_NOT_STATIC
24 
25 #define mode_params epson2_mode_params
26 #define source_list epson2_source_list
27 
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
30 #endif
31 
32 #ifdef HAVE_STDDEF_H
33 #include <stddef.h>
34 #endif
35 
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 
40 #ifdef NEED_SYS_TYPES_H
41 #include <sys/types.h>
42 #endif
43 
44 #include <string.h> /* for memset and memcpy */
45 #include <stdio.h>
46 
47 #include "sane/sane.h"
48 #include "sane/sanei_backend.h"
49 #include "sane/sanei_debug.h"
50 
51 #define EPSON2_CONFIG_FILE "epson2.conf"
52 
53 #ifndef PATH_MAX
54 #define PATH_MAX (1024)
55 #endif
56 
57 #ifndef XtNumber
58 #define XtNumber(x)  (sizeof(x) / sizeof(x[0]))
59 #define XtOffset(p_type, field)  ((size_t)&(((p_type)NULL)->field))
60 #define XtOffsetOf(s_type, field)  XtOffset(s_type*, field)
61 #endif
62 
63 #define NUM_OF_HEX_ELEMENTS (16)        /* number of hex numbers per line for data dump */
64 #define DEVICE_NAME_LEN (16)    /* length of device name in extended status */
65 
66 
67 /* string constants for GUI elements that are not defined SANE-wide */
68 
69 #define SANE_NAME_GAMMA_CORRECTION "gamma-correction"
70 #define SANE_TITLE_GAMMA_CORRECTION SANE_I18N("Gamma Correction")
71 #define SANE_DESC_GAMMA_CORRECTION SANE_I18N("Selects the gamma correction value from a list of pre-defined devices or the user defined table, which can be downloaded to the scanner")
72 
73 #define SANE_EPSON_FOCUS_NAME "focus-position"
74 #define SANE_EPSON_FOCUS_TITLE SANE_I18N("Focus Position")
75 #define SANE_EPSON_FOCUS_DESC SANE_I18N("Sets the focus position to either the glass or 2.5mm above the glass")
76 #define SANE_EPSON_WAIT_FOR_BUTTON_NAME "wait-for-button"
77 #define SANE_EPSON_WAIT_FOR_BUTTON_TITLE SANE_I18N("Wait for Button")
78 #define SANE_EPSON_WAIT_FOR_BUTTON_DESC SANE_I18N("After sending the scan command, wait until the button on the scanner is pressed to actually start the scan process.");
79 
80 /* misc constants */
81 
82 #define LINES_SHUFFLE_MAX	17	/* 2 x 8 lines plus 1 */
83 #define SANE_EPSON_MAX_RETRIES	14	/* warmup max retry */
84 #define CMD_SIZE_EXT_STATUS	42
85 
86 #define FOCUS_ON_GLASS 64
87 #define FOCUS_ABOVE_25MM (64 + 25)
88 
89 /* NOTE: you can find these codes with "man ascii". */
90 #define STX	0x02
91 #define ACK	0x06
92 #define NAK	0x15
93 #define CAN	0x18
94 #define ESC	0x1B
95 #define PF	0x19
96 #define FS	0x1C
97 
98 #define	S_ACK	"\006"
99 #define	S_CAN	"\030"
100 
101 /* status bits */
102 
103 #define STATUS_FER		0x80	/* fatal error */
104 #define STATUS_NOT_READY	0x40	/* scanner is in use on another interface */
105 #define STATUS_AREA_END		0x20	/* area end */
106 #define STATUS_OPTION		0x10	/* option installed */
107 #define STATUS_EXT_COMMANDS	0x02	/* scanners supports extended commands */
108 #define STATUS_RESERVED		0x01	/* this should be always 0 */
109 
110 #define EXT_STATUS_FER		0x80	/* fatal error */
111 #define EXT_STATUS_FBF		0x40	/* flat bed scanner */
112 #define EXT_STATUS_ADFT		0x20	/* page type ADF */
113 #define EXT_STATUS_ADFS		0x10	/* ADF is duplex capable */
114 #define EXT_STATUS_ADFO		0x08	/* ADF loads from the first sheet (page type only) */
115 #define EXT_STATUS_LID		0x04	/* lid is open */
116 #define EXT_STATUS_WU		0x02	/* warming up */
117 #define EXT_STATUS_PB		0x01	/* scanner has a push button */
118 
119 #define EXT_STATUS_IST		0x80	/* option detected */
120 #define EXT_STATUS_EN		0x40	/* option enabled */
121 #define EXT_STATUS_ERR		0x20	/* other error */
122 #define EXT_STATUS_PE		0x08	/* no paper */
123 #define EXT_STATUS_PJ		0x04	/* paper jam */
124 #define EXT_STATUS_OPN		0x02	/* cover open */
125 
126 #define EXT_IDTY_CAP1_DLF	0x80
127 #define EXT_IDTY_CAP1_NOTFBF	0x40	/* not a flat bed scanner */
128 #define EXT_IDTY_CAP1_ADFT	0x20	/* page type ADF ? */
129 #define EXT_IDTY_CAP1_ADFS	0x10	/* ADF is duplex capable */
130 #define EXT_IDTY_CAP1_ADFO	0x08	/* ADF loads from the first sheet (page type only) */
131 #define EXT_IDTY_CAP1_LID	0x04	/* lid type option ? */
132 #define EXT_IDTY_CAP1_TPIR	0x02	/* TPU with infrared */
133 #define EXT_IDTY_CAP1_PB	0x01	/* scanner has a push button */
134 
135 #define EXT_IDTY_CAP2_AFF	0x04	/* auto form feed */
136 #define EXT_IDTY_CAP2_DFD	0x08	/* double feed detection */
137 #define EXT_IDTY_CAP2_ADFAS	0x10	/* ADF with auto scan support */
138 
139 #define FSF_STATUS_MAIN_FER	0x80	/* system error */
140 #define FSF_STATUS_MAIN_NR	0x40	/* not ready */
141 #define FSF_STATUS_MAIN_WU	0x02	/* warming up */
142 #define FSF_STATUS_MAIN_CWU	0x01	/* warm up can be cancelled (?) */
143 
144 #define FSF_STATUS_ADF_IST	0x80	/* installed */
145 #define FSF_STATUS_ADF_EN	0x40	/* enabled */
146 #define FSF_STATUS_ADF_ERR	0x20	/* system error */
147 #define FSF_STATUS_ADF_PE	0x08	/* paper empty */
148 #define FSF_STATUS_ADF_PJ	0x04	/* paper jam */
149 #define FSF_STATUS_ADF_OPN	0x02	/* cover open */
150 #define FSF_STATUS_ADF_PAG	0x01	/* duplex */
151 
152 #define FSF_STATUS_TPU_IST	0x80	/* installed */
153 #define FSF_STATUS_TPU_EN	0x40	/* enabled */
154 #define FSF_STATUS_TPU_ERR	0x20	/* system error */
155 #define FSF_STATUS_TPU_OPN	0x02	/* cover open */
156 
157 #define FSF_STATUS_MAIN2_ERR	0x20	/* system error */
158 #define FSF_STATUS_MAIN2_PE	0x08	/* paper empty */
159 #define FSF_STATUS_MAIN2_PJ	0x04	/* paper jam */
160 #define FSF_STATUS_MAIN2_OPN	0x02	/* cover open */
161 
162 #define FSG_STATUS_FER		0x80
163 #define FSG_STATUS_NOT_READY	0x40	/* in use via other interface */
164 #define FSG_STATUS_CANCEL_REQ	0x10	/* cancel request from scanner */
165 
166 #define EPSON_LEVEL_A1		 0
167 #define EPSON_LEVEL_A2		 1
168 #define EPSON_LEVEL_B1		 2
169 #define	EPSON_LEVEL_B2		 3
170 #define	EPSON_LEVEL_B3		 4
171 #define	EPSON_LEVEL_B4		 5
172 #define	EPSON_LEVEL_B5		 6
173 #define	EPSON_LEVEL_B6		 7
174 #define	EPSON_LEVEL_B7		 8
175 #define	EPSON_LEVEL_B8		 9
176 #define	EPSON_LEVEL_F5		10
177 #define EPSON_LEVEL_D1		11
178 #define EPSON_LEVEL_D7		12
179 #define EPSON_LEVEL_D8		13
180 
181 /* there is also a function level "A5", which I'm ignoring here until somebody can
182  * convince me that this is still needed. The A5 level was for the GT-300, which
183  * was (is) a monochrome only scanner. So if somebody really wants to use this
184  * scanner with SANE get in touch with me and we can work something out - khk
185  */
186 
187 #define	EPSON_LEVEL_DEFAULT EPSON_LEVEL_B3
188 
189 struct EpsonCmd
190 {
191         char *level;
192 
193         unsigned char request_identity;
194 	unsigned char request_identity2; /* new request identity level Dx */
195 	unsigned char request_status;
196 	unsigned char request_condition;
197 	unsigned char set_color_mode;
198 	unsigned char start_scanning;
199 	unsigned char set_data_format;
200 	unsigned char set_resolution;
201 	unsigned char set_zoom;
202 	unsigned char set_scan_area;
203 	unsigned char set_bright;
204 	SANE_Range bright_range;
205 	unsigned char set_gamma;
206 	unsigned char set_halftoning;
207 	unsigned char set_color_correction;
208 	unsigned char initialize_scanner;
209 	unsigned char set_speed;	/* B4 and later */
210 	unsigned char set_lcount;
211 	unsigned char mirror_image;	/* B5 and later */
212 	unsigned char set_gamma_table;	/* B4 and later */
213 	unsigned char set_outline_emphasis; /* B4 and later */
214 	unsigned char set_dither;	/* B4 and later */
215 	unsigned char set_color_correction_coefficients; /* B3 and later */
216 	unsigned char request_extended_status;	/* get extended status from scanner */
217 	unsigned char control_an_extension;	/* for extension control */
218 	unsigned char eject;			/* for extension control */
219 	unsigned char feed;
220 	unsigned char request_push_button_status;
221 	unsigned char control_auto_area_segmentation;
222 	unsigned char set_film_type;		/* for extension control */
223 	unsigned char set_exposure_time;	/* F5 only */
224 	unsigned char set_bay;			/* F5 only */
225 	unsigned char set_threshold;
226 	unsigned char set_focus_position;	/* B8 only */
227 	unsigned char request_focus_position;	/* B8 only */
228 	unsigned char request_extended_identity;
229 	unsigned char request_scanner_status;
230 };
231 
232 enum {
233 	OPT_NUM_OPTS = 0,
234 	OPT_MODE_GROUP,
235 	OPT_MODE,
236 	OPT_BIT_DEPTH,
237 	OPT_HALFTONE,
238 	OPT_DROPOUT,
239 	OPT_BRIGHTNESS,
240 	OPT_SHARPNESS,
241 	OPT_GAMMA_CORRECTION,
242 	OPT_COLOR_CORRECTION,
243 	OPT_RESOLUTION,
244 	OPT_THRESHOLD,
245 	OPT_ADVANCED_GROUP,
246 	OPT_MIRROR,
247 	OPT_AAS,
248 	OPT_GAMMA_VECTOR_R,
249 	OPT_GAMMA_VECTOR_G,
250 	OPT_GAMMA_VECTOR_B,
251 	OPT_WAIT_FOR_BUTTON,
252 	OPT_CCT_GROUP,
253 	OPT_CCT_MODE,
254 	OPT_CCT_PROFILE,
255 	OPT_PREVIEW_GROUP,
256 	OPT_PREVIEW,
257 	OPT_GEOMETRY_GROUP,
258 	OPT_TL_X,
259 	OPT_TL_Y,
260 	OPT_BR_X,
261 	OPT_BR_Y,
262 	OPT_FOCUS_GROUP,
263 	OPT_AUTOFOCUS,
264 	OPT_FOCUS_POS,
265 	OPT_EQU_GROUP,
266 	OPT_SOURCE,
267 	OPT_AUTO_EJECT,
268 	OPT_FILM_TYPE,
269 	OPT_BAY,
270 	OPT_EJECT,
271 	OPT_ADF_MODE,
272 	NUM_OPTIONS
273 };
274 
275 typedef enum
276 {	/* hardware connection to the scanner */
277 	SANE_EPSON_NODEV,	/* default, no HW specified yet */
278 	SANE_EPSON_SCSI,	/* SCSI interface */
279 	SANE_EPSON_PIO,		/* parallel interface */
280 	SANE_EPSON_USB,		/* USB interface */
281 	SANE_EPSON_NET		/* network interface */
282 } Epson_Connection_Type;
283 
284 struct epson_profile
285 {
286 	unsigned int	model;
287 	double		cct[4][9];
288 };
289 
290 enum {
291 	CCTP_REFLECTIVE = 0, CCTP_COLORNEG,
292 	CCTP_MONONEG, CCTP_COLORPOS
293 };
294 
295 struct epson_profile_map
296 {
297         char		*name;
298         unsigned int	id;
299 };
300 
301 extern const struct epson_profile epson_cct_profiles[];
302 extern const struct epson_profile_map epson_cct_models[];
303 
304 /* hardware description */
305 
306 struct Epson_Device
307 {
308 	struct Epson_Device *next;
309 
310 	char *name;
311 	char *model;
312 
313 	unsigned int model_id;
314 
315 	SANE_Device sane;
316 	SANE_Int level;
317 	SANE_Range dpi_range;
318 
319 	SANE_Range *x_range;	        /* x range w/out extension */
320 	SANE_Range *y_range;	        /* y range w/out extension */
321 
322 	SANE_Range fbf_x_range;	        /* flattbed x range */
323 	SANE_Range fbf_y_range;	        /* flattbed y range */
324 	SANE_Range adf_x_range;	        /* autom. document feeder x range */
325 	SANE_Range adf_y_range;	        /* autom. document feeder y range */
326 	SANE_Range tpu_x_range;	        /* transparency unit x range */
327 	SANE_Range tpu_y_range;	        /* transparency unit y range */
328 	SANE_Range tpu2_x_range;	/* transparency unit 2 x range */
329 	SANE_Range tpu2_y_range;	/* transparency unit 2 y range */
330 
331 	Epson_Connection_Type connection;
332 
333 	SANE_Int *res_list;		/* list of resolutions */
334 	SANE_Int res_list_size;		/* number of entries in this list */
335 	SANE_Int last_res;		/* last selected resolution */
336 	SANE_Int last_res_preview;	/* last selected preview resolution */
337 
338 	SANE_Word *resolution_list;	/* for display purposes we store a second copy */
339 
340 	SANE_Bool extension;		/* extension is installed */
341 	SANE_Int use_extension;		/* use the installed extension */
342 	SANE_Bool TPU;			/* TPU is installed */
343 	SANE_Bool TPU2;			/* TPU2 is installed */
344 	SANE_Bool ADF;			/* ADF is installed */
345 	SANE_Bool duplex;		/* does the ADF handle duplex scanning */
346 	SANE_Bool focusSupport;		/* does this scanner have support for "set focus position" ? */
347 	SANE_Bool color_shuffle;	/* does this scanner need color shuffling */
348 
349 	SANE_Int maxDepth;		/* max. color depth */
350 	SANE_Int *depth_list;
351 
352 	SANE_Int optical_res;		/* optical resolution */
353 	SANE_Int max_line_distance;
354 
355 	SANE_Bool need_double_vertical;
356 	SANE_Bool need_color_reorder;
357 	SANE_Bool need_reset_on_source_change;
358 
359 	SANE_Bool wait_for_button;	/* do we have to wait until the scanner button is pressed? */
360 
361 	SANE_Bool extended_commands;
362 
363 	struct EpsonCmd *cmd;
364 	const struct epson_profile *cct_profile;
365 };
366 
367 typedef struct Epson_Device Epson_Device;
368 
369 /* an instance of a scanner */
370 
371 struct Epson_Scanner
372 {
373 	struct Epson_Scanner *next;
374 	struct Epson_Device *hw;
375 
376 	int fd;
377 
378 	SANE_Option_Descriptor opt[NUM_OPTIONS];
379 	Option_Value val[NUM_OPTIONS];
380 	SANE_Parameters params;
381 
382 	SANE_Bool block;
383 	SANE_Bool eof;
384 	SANE_Byte *buf, *end, *ptr;
385 	SANE_Bool canceling;
386 	SANE_Word gamma_table[3][256];
387 	SANE_Word cct_table[9];
388 	SANE_Int retry_count;
389 
390 	/* buffer lines for color shuffling */
391 	SANE_Byte *line_buffer[LINES_SHUFFLE_MAX];
392 	SANE_Int color_shuffle_line;	/* current line number for color shuffling */
393 	SANE_Int line_distance;		/* current line distance */
394 	SANE_Int current_output_line;	/* line counter when color shuffling */
395 	SANE_Int lines_written;		/* debug variable */
396 
397 	SANE_Int left, top, lcount;
398 	SANE_Byte currentFocusPosition;
399 
400 	/* network buffers */
401 	unsigned char *netbuf, *netptr;
402 	size_t netlen;
403 
404 	/* extended image data handshaking */
405 	SANE_Int ext_block_len;
406 	SANE_Int ext_last_len;
407 	SANE_Int ext_blocks;
408 	SANE_Int ext_counter;
409 };
410 
411 typedef struct Epson_Scanner Epson_Scanner;
412 
413 struct mode_param
414 {
415 	int color;
416 	int flags;
417 	int dropout_mask;
418 	int depth;
419 };
420 
421 enum {
422 	MODE_BINARY, MODE_GRAY, MODE_COLOR, MODE_INFRARED
423 };
424 
425 #endif
426