xref: /third_party/backends/backend/mustek.h (revision 141cc406)
1/* sane - Scanner Access Now Easy.
2   Copyright (C) 1996, 1997 David Mosberger-Tang, 1998 Andreas Bolsch for
3   extension to ScanExpress models version 0.5,
4   2000 - 2005 Henning Meier-Geinitz.
5   This file is part of the SANE package.
6
7   This program is free software; you can redistribute it and/or
8   modify it under the terms of the GNU General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20   As a special exception, the authors of SANE give permission for
21   additional uses of the libraries contained in this release of SANE.
22
23   The exception is that, if you link a SANE library with other files
24   to produce an executable, this does not by itself cause the
25   resulting executable to be covered by the GNU General Public
26   License.  Your use of that executable is in no way restricted on
27   account of linking the SANE library code into it.
28
29   This exception does not, however, invalidate any other reasons why
30   the executable file might be covered by the GNU General Public
31   License.
32
33   If you submit changes to SANE to the maintainers to be included in
34   a subsequent release, you agree by submitting the changes that
35   those changes may be distributed with this exception intact.
36
37   If you write modifications of your own for SANE, it is your choice
38   whether to permit this exception to apply to your modifications.
39   If you do not wish that, delete this exception notice.
40
41   This file implements a SANE backend for Mustek and some Trust flatbed
42   scanners with SCSI or proprietary interface.  */
43
44#ifndef mustek_h
45#define mustek_h
46
47#include "../include/sane/config.h"
48#include <sys/types.h>
49
50/* Some constants */
51#define INQ_LEN	0x60		/* Length of SCSI inquiry */
52#ifndef PATH_MAX
53# define PATH_MAX 1024
54#endif
55#define MUSTEK_CONFIG_FILE "mustek.conf"
56
57#define MAX_WAITING_TIME 60	/* How long to wait for scanner to become ready */
58#define MAX_LINE_DIST 40	/* Extra lines needed for LD correction */
59
60/* Flag values */
61/* Scanner types */
62#define MUSTEK_FLAG_THREE_PASS	 (1 << 0)	/* three pass scanner */
63#define MUSTEK_FLAG_PARAGON_1    (1 << 1)	/* Paragon series I scanner */
64#define MUSTEK_FLAG_PARAGON_2    (1 << 2)	/* Paragon series II(A4) scanner */
65#define MUSTEK_FLAG_SE		 (1 << 3)	/* ScanExpress scanner */
66#define MUSTEK_FLAG_SE_PLUS    	 (1 << 4)	/* ScanExpress Plus scanner */
67#define MUSTEK_FLAG_PRO          (1 << 5)	/* Professional series scanner */
68#define MUSTEK_FLAG_N		 (1 << 6)	/* N-type scanner (non SCSI) */
69#define MUSTEK_FLAG_SCSI_PP      (1 << 22)	/* SCSI over parallel (e.g. 600 II EP) */
70/* Additional equipment */
71#define MUSTEK_FLAG_ADF		 (1 << 7)	/* automatic document feeder */
72#define MUSTEK_FLAG_ADF_READY	 (1 << 8)	/* paper present */
73#define MUSTEK_FLAG_TA		 (1 << 9)	/* transparency adapter */
74/* Line-distance correction */
75#define MUSTEK_FLAG_LD_NONE	 (1 << 10)	/* no line-distance corr */
76#define MUSTEK_FLAG_LD_BLOCK     (1 << 11)	/* blockwise LD corr */
77#define MUSTEK_FLAG_LD_N1	 (1 << 12)	/* LD corr for N-type v1 */
78#define MUSTEK_FLAG_LD_N2	 (1 << 13)	/* LD corr for N-type v2 */
79/* Manual fixes */
80#define MUSTEK_FLAG_LD_FIX	 (1 << 14)	/* need line-distance fix? */
81#define MUSTEK_FLAG_LINEART_FIX	 (1 << 15)	/* lineart fix/hack */
82#define MUSTEK_FLAG_USE_EIGHTS	 (1 << 16)	/* use 1/8" lengths */
83#define MUSTEK_FLAG_FORCE_GAMMA  (1 << 17)	/* force gamma table upload */
84#define MUSTEK_FLAG_ENLARGE_X    (1 << 18)	/* need to enlarge x-res */
85#define MUSTEK_FLAG_COVER_SENSOR (1 << 19)	/* scanner can detect open cover */
86#define MUSTEK_FLAG_USE_BLOCK	 (1 << 20)	/* use blockmode */
87#define MUSTEK_FLAG_LEGAL_SIZE	 (1 << 21)	/* scanner has legal size */
88#define MUSTEK_FLAG_NO_BACKTRACK (1 << 21)	/* scanner has legal size */
89
90/* Source values: */
91#define MUSTEK_SOURCE_FLATBED	0
92#define MUSTEK_SOURCE_ADF	1
93#define MUSTEK_SOURCE_TA	2
94
95/* Mode values: */
96#define MUSTEK_MODE_LINEART	(1 << 0)	/* grayscale 1 bit / pixel */
97#define MUSTEK_MODE_GRAY	(1 << 1)	/* grayscale 8 bits / pixel */
98#define MUSTEK_MODE_COLOR	(1 << 2)	/* color 24 bits / pixel */
99#define MUSTEK_MODE_HALFTONE	(1 << 3)	/* use dithering */
100
101/* Color band codes: */
102#define MUSTEK_CODE_GRAY	0
103#define MUSTEK_CODE_RED		1
104#define MUSTEK_CODE_GREEN	2
105#define MUSTEK_CODE_BLUE	3
106
107/* SCSI commands that the Mustek scanners understand (or not): */
108#define MUSTEK_SCSI_TEST_UNIT_READY	0x00
109#define MUSTEK_SCSI_REQUEST_SENSE       0x03
110#define MUSTEK_SCSI_AREA_AND_WINDOWS	0x04
111#define MUSTEK_SCSI_READ_SCANNED_DATA	0x08
112#define MUSTEK_SCSI_GET_IMAGE_STATUS	0x0f
113#define MUSTEK_SCSI_ADF_AND_BACKTRACK	0x10
114#define MUSTEK_SCSI_CCD_DISTANCE	0x11
115#define MUSTEK_SCSI_INQUIRY		0x12
116#define MUSTEK_SCSI_MODE_SELECT 	0x15
117#define MUSTEK_SCSI_START_STOP		0x1b
118#define MUSTEK_SCSI_SET_WINDOW		0x24
119#define MUSTEK_SCSI_GET_WINDOW		0x25
120#define MUSTEK_SCSI_READ_DATA		0x28
121#define MUSTEK_SCSI_SEND_DATA		0x2a
122#define MUSTEK_SCSI_LOOKUP_TABLE	0x55
123
124/* Convenience macros */
125#if defined(MIN)
126#undef MIN
127#endif
128#if defined(MAX)
129#undef MAX
130#endif
131#define MIN(a,b)	((a) < (b) ? (a) : (b))
132#define MAX(a,b)	((a) > (b) ? (a) : (b))
133/* Copy values to memory ('L' = little endian, 'B' = big endian */
134#define STORE16L(cp,v)				\
135do {						\
136    int value = (v);				\
137						\
138    *(cp)++ = (value >> 0) & 0xff;		\
139    *(cp)++ = (value >> 8) & 0xff;		\
140} while (0)
141#define STORE16B(cp,v)				\
142do {						\
143    int value = (v);				\
144						\
145    *(cp)++ = (value >> 8) & 0xff;		\
146    *(cp)++ = (value >> 0) & 0xff;		\
147} while (0)
148#define STORE32B(cp,v)				\
149do {						\
150    long int value = (v);			\
151						\
152    *(cp)++ = (value >> 24) & 0xff;		\
153    *(cp)++ = (value >> 16) & 0xff;		\
154    *(cp)++ = (value >>  8) & 0xff;		\
155    *(cp)++ = (value >>  0) & 0xff;		\
156} while (0)
157
158/* declarations */
159enum Mustek_Option
160{
161  OPT_NUM_OPTS = 0,
162
163  OPT_MODE_GROUP,
164  OPT_MODE,
165  OPT_FAST_GRAY_MODE,
166  OPT_RESOLUTION,
167  OPT_BIT_DEPTH,
168  OPT_SPEED,
169  OPT_SOURCE,
170  OPT_PREVIEW,
171  OPT_FAST_PREVIEW,
172  OPT_LAMP_OFF_TIME,
173  OPT_LAMP_OFF_BUTTON,
174  OPT_GEOMETRY_GROUP,
175  OPT_TL_X,			/* top-left x */
176  OPT_TL_Y,			/* top-left y */
177  OPT_BR_X,			/* bottom-right x */
178  OPT_BR_Y,			/* bottom-right y */
179
180  OPT_ENHANCEMENT_GROUP,
181  OPT_BRIGHTNESS,
182  OPT_BRIGHTNESS_R,
183  OPT_BRIGHTNESS_G,
184  OPT_BRIGHTNESS_B,
185  OPT_CONTRAST,
186  OPT_CONTRAST_R,
187  OPT_CONTRAST_G,
188  OPT_CONTRAST_B,
189  OPT_CUSTOM_GAMMA,		/* use custom gamma tables? */
190  /* The gamma vectors MUST appear in the order gray, red, green,
191     blue.  */
192  OPT_GAMMA_VECTOR,
193  OPT_GAMMA_VECTOR_R,
194  OPT_GAMMA_VECTOR_G,
195  OPT_GAMMA_VECTOR_B,
196  OPT_QUALITY_CAL,
197  OPT_HALFTONE_DIMENSION,
198  OPT_HALFTONE_PATTERN,
199
200  /* must come last: */
201  NUM_OPTIONS
202};
203
204typedef struct Mustek_Device
205{
206  struct Mustek_Device *next;
207  SANE_String name;
208  SANE_Device sane;
209  SANE_Range dpi_range;
210  SANE_Range x_range;
211  SANE_Range y_range;
212  /* scan area when transparency adapter is used: */
213  SANE_Range x_trans_range;
214  SANE_Range y_trans_range;
215  SANE_Word flags;
216  /* length of gamma table, probably always <= 4096 for the SE */
217  SANE_Int gamma_length;
218  /* values actually used by scanner, not necessarily the desired! */
219  SANE_Int bpl, lines;
220  /* what is needed for calibration (ScanExpress and Pro series) */
221  struct
222  {
223    SANE_Int bytes;
224    SANE_Int lines;
225    SANE_Byte *buffer;
226    SANE_Word *line_buffer[3];
227  }
228  cal;
229  /* current and maximum buffer size used by the backend */
230  /* the buffer sent to the scanner is actually half of this size */
231  SANE_Int buffer_size;
232  SANE_Int max_buffer_size;
233  /* maximum size scanned in one block and corresponding lines */
234  SANE_Int max_block_buffer_size;
235  SANE_Int lines_per_block;
236  SANE_Byte *block_buffer;
237
238  /* firmware format: 0 = old, MUSTEK at pos 8; 1 = new, MUSTEK at
239     pos 36 */
240  SANE_Int firmware_format;
241  /* firmware revision system: 0 = old, x.yz; 1 = new, Vxyz */
242  SANE_Int firmware_revision_system;
243}
244Mustek_Device;
245
246typedef struct Mustek_Scanner
247{
248  /* all the state needed to define a scan request: */
249  struct Mustek_Scanner *next;
250
251  SANE_Option_Descriptor opt[NUM_OPTIONS];
252  Option_Value val[NUM_OPTIONS];
253  SANE_Int gamma_table[4][256];
254  SANE_Int *halftone_pattern;
255  SANE_Bool custom_halftone_pattern;
256  SANE_Int halftone_pattern_type;
257
258  SANE_Bool scanning;
259  SANE_Bool cancelled;
260  SANE_Int pass;		/* pass number */
261  SANE_Int line;		/* current line number */
262  SANE_Parameters params;
263
264  /* Parsed option values and variables that are valid only during
265     actual scanning: */
266  SANE_Word mode;
267  SANE_Bool one_pass_color_scan;
268  SANE_Int resolution_code;
269  int fd;			/* SCSI filedescriptor */
270  SANE_Pid reader_pid;		/* process id of reader */
271  int reader_fds;		/* OS/2: pipe write handler for reader */
272  int pipe;			/* pipe to reader process */
273  long start_time;		/* at this time the scan started */
274  SANE_Word total_bytes;	/* bytes transmitted by sane_read */
275  SANE_Word total_lines;	/* lines transmitted to sane_read pipe */
276
277  /* scanner dependent/low-level state: */
278  Mustek_Device *hw;
279
280  /* line-distance correction related state: */
281  struct
282  {
283    SANE_Int color;		/* first color appearing in read data */
284    SANE_Int max_value;
285    SANE_Int peak_res;
286    SANE_Int dist[3];		/* line distance */
287    SANE_Int index[3];		/* index for R/G/B color assignment */
288    SANE_Int quant[3];		/* for resolution correction */
289    SANE_Int saved[3];		/* number of saved color lines */
290    /* these are used for SE, MFS and N line-distance correction: */
291    SANE_Byte *buf[3];
292    /* these are used for N line-distance correction only: */
293    SANE_Int ld_line;		/* line # currently processed in
294				   ld-correction */
295    SANE_Int lmod3;		/* line # modulo 3 */
296  }
297  ld;
298}
299Mustek_Scanner;
300
301#endif /* mustek_h */
302