1 /* sane - Scanner Access Now Easy.
2 Copyright (C) 2002 Frank Zago (sane at zago dot net)
3
4 This file is part of the SANE package.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19 As a special exception, the authors of SANE give permission for
20 additional uses of the libraries contained in this release of SANE.
21
22 The exception is that, if you link a SANE library with other files
23 to produce an executable, this does not by itself cause the
24 resulting executable to be covered by the GNU General Public
25 License. Your use of that executable is in no way restricted on
26 account of linking the SANE library code into it.
27
28 This exception does not, however, invalidate any other reasons why
29 the executable file might be covered by the GNU General Public
30 License.
31
32 If you submit changes to SANE to the maintainers to be included in
33 a subsequent release, you agree by submitting the changes that
34 those changes may be distributed with this exception intact.
35
36 If you write modifications of your own for SANE, it is your choice
37 whether to permit this exception to apply to your modifications.
38 If you do not wish that, delete this exception notice.
39 */
40
41 /* Commands supported by the KV-SS 25 scanner. */
42 #define SCSI_TEST_UNIT_READY 0x00
43 #define SCSI_INQUIRY 0x12
44 #define SCSI_SET_WINDOW 0x24
45 #define SCSI_READ_10 0x28
46 #define SCSI_REQUEST_SENSE 0x03
47
48 typedef struct
49 {
50 unsigned char data[16];
51 int len;
52 }
53 CDB;
54
55
56 /* Set a specific bit depending on a boolean.
57 * MKSCSI_BIT(TRUE, 3) will generate 0x08. */
58 #define MKSCSI_BIT(bit, pos) ((bit)? 1<<(pos): 0)
59
60 /* Set a value in a range of bits.
61 * MKSCSI_I2B(5, 3, 5) will generate 0x28 */
62 #define MKSCSI_I2B(bits, pos_b, pos_e) ((bits) << (pos_b) & ((1<<((pos_e)-(pos_b)+1))-1))
63
64 /* Store an integer in 2, 3 or 4 byte in an array. */
65 #define Ito16(val, buf) { \
66 ((unsigned char *)buf)[0] = ((val) >> 8) & 0xff; \
67 ((unsigned char *)buf)[1] = ((val) >> 0) & 0xff; \
68 }
69
70 #define Ito24(val, buf) { \
71 ((unsigned char *)buf)[0] = ((val) >> 16) & 0xff; \
72 ((unsigned char *)buf)[1] = ((val) >> 8) & 0xff; \
73 ((unsigned char *)buf)[2] = ((val) >> 0) & 0xff; \
74 }
75
76 #define Ito32(val, buf) { \
77 ((unsigned char *)buf)[0] = ((val) >> 24) & 0xff; \
78 ((unsigned char *)buf)[1] = ((val) >> 16) & 0xff; \
79 ((unsigned char *)buf)[2] = ((val) >> 8) & 0xff; \
80 ((unsigned char *)buf)[3] = ((val) >> 0) & 0xff; \
81 }
82
83 #define MKSCSI_TEST_UNIT_READY(cdb) \
84 cdb.data[0] = SCSI_TEST_UNIT_READY; \
85 cdb.data[1] = 0; \
86 cdb.data[2] = 0; \
87 cdb.data[3] = 0; \
88 cdb.data[4] = 0; \
89 cdb.data[5] = 0; \
90 cdb.len = 6;
91
92 #define MKSCSI_INQUIRY(cdb, buflen) \
93 cdb.data[0] = SCSI_INQUIRY; \
94 cdb.data[1] = 0; \
95 cdb.data[2] = 0; \
96 cdb.data[3] = 0; \
97 cdb.data[4] = buflen; \
98 cdb.data[5] = 0; \
99 cdb.len = 6;
100
101 #define MKSCSI_SET_WINDOW(cdb, buflen) \
102 cdb.data[0] = SCSI_SET_WINDOW; \
103 cdb.data[1] = 0; \
104 cdb.data[2] = 0; \
105 cdb.data[3] = 0; \
106 cdb.data[4] = 0; \
107 cdb.data[5] = 0; \
108 cdb.data[6] = (((buflen) >> 16) & 0xff); \
109 cdb.data[7] = (((buflen) >> 8) & 0xff); \
110 cdb.data[8] = (((buflen) >> 0) & 0xff); \
111 cdb.data[9] = 0; \
112 cdb.len = 10;
113
114 #define MKSCSI_READ_10(cdb, dtc, dtq, buflen) \
115 cdb.data[0] = SCSI_READ_10; \
116 cdb.data[1] = 0; \
117 cdb.data[2] = (dtc); \
118 cdb.data[3] = 0; \
119 cdb.data[4] = (((dtq) >> 8) & 0xff); \
120 cdb.data[5] = (((dtq) >> 0) & 0xff); \
121 cdb.data[6] = (((buflen) >> 16) & 0xff); \
122 cdb.data[7] = (((buflen) >> 8) & 0xff); \
123 cdb.data[8] = (((buflen) >> 0) & 0xff); \
124 cdb.data[9] = 0; \
125 cdb.len = 10;
126
127 #define MKSCSI_REQUEST_SENSE(cdb, buflen) \
128 cdb.data[0] = SCSI_REQUEST_SENSE; \
129 cdb.data[1] = 0; \
130 cdb.data[2] = 0; \
131 cdb.data[3] = 0; \
132 cdb.data[4] = (buflen); \
133 cdb.data[5] = 0; \
134 cdb.len = 6;
135
136 /*--------------------------------------------------------------------------*/
137
138 static inline int
getbitfield(unsigned char * pageaddr,int mask,int shift)139 getbitfield (unsigned char *pageaddr, int mask, int shift)
140 {
141 return ((*pageaddr >> shift) & mask);
142 }
143
144 /* defines for request sense return block */
145 #define get_RS_information_valid(b) getbitfield(b + 0x00, 1, 7)
146 #define get_RS_error_code(b) getbitfield(b + 0x00, 0x7f, 0)
147 #define get_RS_filemark(b) getbitfield(b + 0x02, 1, 7)
148 #define get_RS_EOM(b) getbitfield(b + 0x02, 1, 6)
149 #define get_RS_ILI(b) getbitfield(b + 0x02, 1, 5)
150 #define get_RS_sense_key(b) getbitfield(b + 0x02, 0x0f, 0)
151 #define get_RS_information(b) getnbyte(b+0x03, 4)
152 #define get_RS_additional_length(b) b[0x07]
153 #define get_RS_ASC(b) b[0x0c]
154 #define get_RS_ASCQ(b) b[0x0d]
155 #define get_RS_SKSV(b) getbitfield(b+0x0f,1,7)
156
157 /*--------------------------------------------------------------------------*/
158
159 #define mmToIlu(mm) (((mm) * 1200) / MM_PER_INCH)
160 #define iluToMm(ilu) (((ilu) * MM_PER_INCH) / 1200)
161
162 #define PAGE_FRONT 0x00
163 #define PAGE_BACK 0x80
164
165 /*--------------------------------------------------------------------------*/
166
167 enum Matsushita_Option
168 {
169 OPT_NUM_OPTS = 0,
170
171 OPT_MODE_GROUP,
172 OPT_MODE, /* scanner modes */
173 OPT_RESOLUTION, /* X and Y resolution */
174 OPT_DUPLEX, /* Duplex mode */
175 OPT_FEEDER_MODE, /* Feeding mode */
176
177 OPT_GEOMETRY_GROUP,
178 OPT_PAPER_SIZE, /* Paper size */
179 OPT_TL_X, /* upper left X */
180 OPT_TL_Y, /* upper left Y */
181 OPT_BR_X, /* bottom right X */
182 OPT_BR_Y, /* bottom right Y */
183
184 OPT_ENHANCEMENT_GROUP,
185 OPT_BRIGHTNESS, /* Brightness */
186 OPT_CONTRAST, /* Contrast */
187 OPT_AUTOMATIC_THRESHOLD, /* Automatic threshold */
188 OPT_HALFTONE_PATTERN, /* Halftone pattern */
189 OPT_AUTOMATIC_SEPARATION, /* Automatic separation */
190 OPT_WHITE_LEVEL, /* White level */
191 OPT_NOISE_REDUCTION, /* Noise reduction */
192 OPT_IMAGE_EMPHASIS, /* Image emphasis */
193 OPT_GAMMA, /* Gamma */
194
195
196 /* must come last: */
197 OPT_NUM_OPTIONS
198 };
199
200 /*--------------------------------------------------------------------------*/
201
202 #define BLACK_WHITE_STR SANE_VALUE_SCAN_MODE_LINEART
203 #define GRAY4_STR SANE_I18N("Grayscale 4 bits")
204 #define GRAY8_STR SANE_I18N("Grayscale 8 bits")
205
206 /*--------------------------------------------------------------------------*/
207
208 #define SANE_NAME_DUPLEX "duplex"
209 #define SANE_NAME_PAPER_SIZE "paper-size"
210 #define SANE_NAME_AUTOSEP "autoseparation"
211
212 #define SANE_TITLE_DUPLEX SANE_I18N("Duplex")
213 #define SANE_TITLE_PAPER_SIZE SANE_I18N("Paper size")
214 #define SANE_TITLE_AUTOSEP SANE_I18N("Automatic separation")
215
216 #define SANE_DESC_DUPLEX \
217 SANE_I18N("Enable Duplex (Dual-Sided) Scanning")
218 #define SANE_DESC_PAPER_SIZE \
219 SANE_I18N("Physical size of the paper in the ADF");
220 #define SANE_DESC_AUTOSEP \
221 SANE_I18N("Automatic separation")
222
223 /*--------------------------------------------------------------------------*/
224
225 /* Differences between the scanners.
226 * The scsi_* fields are used to lookup the correcte entry. */
227 struct scanners_supported
228 {
229 int scsi_type;
230 char scsi_vendor[9];
231 char scsi_product[17];
232
233 SANE_Range x_range;
234 SANE_Range y_range;
235 SANE_Range brightness_range;
236 SANE_Range contrast_range;
237
238 SANE_String_Const *scan_mode_list; /* array of scan modes */
239 const SANE_Word *resolutions_list; /* array of available resolutions */
240 const SANE_Word *resolutions_round; /* rounding values for each resolutions */
241
242 SANE_String_Const *image_emphasis_list; /* list of image emphasis options */
243 const int *image_emphasis_val; /* list of image emphasis values */
244
245 /* Scanner capabilities. */
246 int cap; /* bit field */
247 #define MAT_CAP_DUPLEX 0x00000002 /* can do duplex */
248 #define MAT_CAP_CONTRAST 0x00000004 /* have contrast */
249 #define MAT_CAP_AUTOMATIC_THRESHOLD 0x00000008
250 #define MAT_CAP_WHITE_LEVEL 0x00000010
251 #define MAT_CAP_GAMMA 0x00000020
252 #define MAT_CAP_NOISE_REDUCTION 0x00000040
253 #define MAT_CAP_PAPER_DETECT 0x00000080
254 #define MAT_CAP_MIRROR_IMAGE 0x00000100
255 #define MAT_CAP_DETECT_DOUBLE_FEED 0x00000200
256 #define MAT_CAP_MANUAL_FEED 0x00000400
257 };
258
259 struct paper_sizes
260 {
261 SANE_String_Const name; /* name of the paper */
262 int width;
263 int length;
264 };
265
266 /*--------------------------------------------------------------------------*/
267
268 /* Define a scanner occurrence. */
269 typedef struct Matsushita_Scanner
270 {
271 struct Matsushita_Scanner *next;
272 SANE_Device sane;
273
274 char *devicename;
275 int sfd; /* device handle */
276
277 /* Infos from inquiry. */
278 char scsi_type;
279 char scsi_vendor[9];
280 char scsi_product[17];
281 char scsi_version[5];
282
283 /* Scanner infos. */
284 int scnum; /* index of that scanner in
285 * scanners_supported */
286
287 SANE_String_Const *paper_sizes_list; /* names of supported papers */
288 int *paper_sizes_val; /* indirection into paper_sizes[] */
289
290 /* SCSI handling */
291 size_t buffer_size; /* size of the buffer */
292 SANE_Byte *buffer; /* for SCSI transfer. */
293
294 /* Scanning handling. */
295 int scanning; /* TRUE if a scan is running. */
296 int resolution; /* resolution in DPI, for both X and Y */
297 int x_tl; /* X top left */
298 int y_tl; /* Y top left */
299 int x_br; /* X bottom right */
300 int y_br; /* Y bottom right */
301 int width; /* width of the scan area in mm */
302 int length; /* length of the scan area in mm */
303
304 enum
305 {
306 MATSUSHITA_BW,
307 MATSUSHITA_HALFTONE,
308 MATSUSHITA_GRAYSCALE
309 }
310 scan_mode;
311
312 int depth; /* depth per color */
313 int halftone_pattern; /* haltone number, valid for MATSUSHITA_HALFTONE */
314
315 size_t bytes_left; /* number of bytes left to give to the backend */
316
317 size_t real_bytes_left; /* number of bytes left the scanner will return. */
318
319 SANE_Parameters params;
320
321 int page_side; /* 0=front, 1=back */
322 int page_num; /* current number of the page */
323
324 /* For Grayscale 4 bits only */
325 SANE_Byte *image; /* keep the current image there */
326 size_t image_size; /* allocated size of image */
327 size_t image_begin; /* first significant byte in image */
328 size_t image_end; /* first free byte in image */
329
330
331 /* Options */
332 SANE_Option_Descriptor opt[OPT_NUM_OPTIONS];
333 Option_Value val[OPT_NUM_OPTIONS];
334 }
335 Matsushita_Scanner;
336
337 /*--------------------------------------------------------------------------*/
338
339 /* Debug levels.
340 * Should be common to all backends. */
341
342 #define DBG_error0 0
343 #define DBG_error 1
344 #define DBG_sense 2
345 #define DBG_warning 3
346 #define DBG_inquiry 4
347 #define DBG_info 5
348 #define DBG_info2 6
349 #define DBG_proc 7
350 #define DBG_read 8
351 #define DBG_sane_init 10
352 #define DBG_sane_proc 11
353 #define DBG_sane_info 12
354 #define DBG_sane_option 13
355
356 /*--------------------------------------------------------------------------*/
357
358 /* 32 bits from an array to an integer (eg ntohl). */
359 #define B32TOI(buf) \
360 ((((unsigned char *)buf)[0] << 24) | \
361 (((unsigned char *)buf)[1] << 16) | \
362 (((unsigned char *)buf)[2] << 8) | \
363 (((unsigned char *)buf)[3] << 0))
364