1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2002 Frank Zago (sane at zago dot net)
4
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
42 /* Commands supported by the scanner. */
43 #define SCSI_TEST_UNIT_READY 0x00
44 #define SCSI_REQUEST_SENSE 0x03
45 #define SCSI_VENDOR_09 0x09
46 #define SCSI_VENDOR_0E 0x0E
47 #define SCSI_INQUIRY 0x12
48 #define SCSI_MODE_SELECT 0x15
49 #define SCSI_SCAN 0x1b
50 #define SCSI_SET_WINDOW 0x24
51 #define SCSI_SEND_10 0x2a
52 #define SCSI_READ_10 0x28
53 #define SCSI_OBJECT_POSITION 0x31
54 #define SCSI_GET_DATA_BUFFER_STATUS 0x34
55
56
57 typedef struct
58 {
59 unsigned char data[16];
60 int len;
61 }
62 CDB;
63
64
65 /* Set a specific bit depending on a boolean.
66 * MKSCSI_BIT(TRUE, 3) will generate 0x08. */
67 #define MKSCSI_BIT(bit, pos) ((bit)? 1<<(pos): 0)
68
69 /* Set a value in a range of bits.
70 * MKSCSI_I2B(5, 3, 5) will generate 0x28 */
71 #define MKSCSI_I2B(bits, pos_b, pos_e) ((bits) << (pos_b) & ((1<<((pos_e)-(pos_b)+1))-1))
72
73 /* Store an integer in 2, 3 or 4 byte in an array. */
74 #define Ito16(val, buf) { \
75 ((unsigned char *)buf)[0] = ((val) >> 8) & 0xff; \
76 ((unsigned char *)buf)[1] = ((val) >> 0) & 0xff; \
77 }
78
79 #define Ito24(val, buf) { \
80 ((unsigned char *)buf)[0] = ((val) >> 16) & 0xff; \
81 ((unsigned char *)buf)[1] = ((val) >> 8) & 0xff; \
82 ((unsigned char *)buf)[2] = ((val) >> 0) & 0xff; \
83 }
84
85 #define Ito32(val, buf) { \
86 ((unsigned char *)buf)[0] = ((val) >> 24) & 0xff; \
87 ((unsigned char *)buf)[1] = ((val) >> 16) & 0xff; \
88 ((unsigned char *)buf)[2] = ((val) >> 8) & 0xff; \
89 ((unsigned char *)buf)[3] = ((val) >> 0) & 0xff; \
90 }
91
92 #define MKSCSI_GET_DATA_BUFFER_STATUS(cdb, wait, buflen) \
93 cdb.data[0] = SCSI_GET_DATA_BUFFER_STATUS; \
94 cdb.data[1] = MKSCSI_BIT(wait, 0); \
95 cdb.data[2] = 0; \
96 cdb.data[3] = 0; \
97 cdb.data[4] = 0; \
98 cdb.data[5] = 0; \
99 cdb.data[6] = 0; \
100 cdb.data[7] = (((buflen) >> 8) & 0xff); \
101 cdb.data[8] = (((buflen) >> 0) & 0xff); \
102 cdb.data[9] = 0; \
103 cdb.len = 10;
104
105 #define MKSCSI_INQUIRY(cdb, buflen) \
106 cdb.data[0] = SCSI_INQUIRY; \
107 cdb.data[1] = 0; \
108 cdb.data[2] = 0; \
109 cdb.data[3] = 0; \
110 cdb.data[4] = buflen; \
111 cdb.data[5] = 0; \
112 cdb.len = 6;
113
114 #define MKSCSI_MODE_SELECT(cdb, pf, sp, buflen) \
115 cdb.data[0] = SCSI_MODE_SELECT; \
116 cdb.data[1] = MKSCSI_BIT(pf, 4) | MKSCSI_BIT(sp, 0); \
117 cdb.data[2] = 0; \
118 cdb.data[3] = 0; \
119 cdb.data[4] = buflen; \
120 cdb.data[5] = 0; \
121 cdb.len = 6;
122
123 #define MKSCSI_OBJECT_POSITION(cdb, position) \
124 cdb.data[0] = SCSI_OBJECT_POSITION; \
125 cdb.data[1] = 0; \
126 cdb.data[2] = (((position) >> 16) & 0xff); \
127 cdb.data[3] = (((position) >> 8) & 0xff); \
128 cdb.data[4] = (((position) >> 0) & 0xff); \
129 cdb.data[5] = 0; \
130 cdb.data[6] = 0; \
131 cdb.data[7] = 0; \
132 cdb.data[8] = 0; \
133 cdb.data[9] = 0; \
134 cdb.len = 10;
135
136 #define MKSCSI_SET_WINDOW(cdb, buflen) \
137 cdb.data[0] = SCSI_SET_WINDOW; \
138 cdb.data[1] = 0; \
139 cdb.data[2] = 0; \
140 cdb.data[3] = 0; \
141 cdb.data[4] = 0; \
142 cdb.data[5] = 0; \
143 cdb.data[6] = (((buflen) >> 16) & 0xff); \
144 cdb.data[7] = (((buflen) >> 8) & 0xff); \
145 cdb.data[8] = (((buflen) >> 0) & 0xff); \
146 cdb.data[9] = 0; \
147 cdb.len = 10;
148
149 #define MKSCSI_READ_10(cdb, dtc, dtq, buflen) \
150 cdb.data[0] = SCSI_READ_10; \
151 cdb.data[1] = 0; \
152 cdb.data[2] = (dtc); \
153 cdb.data[3] = 0; \
154 cdb.data[4] = (((dtq) >> 8) & 0xff); \
155 cdb.data[5] = (((dtq) >> 0) & 0xff); \
156 cdb.data[6] = (((buflen) >> 16) & 0xff); \
157 cdb.data[7] = (((buflen) >> 8) & 0xff); \
158 cdb.data[8] = (((buflen) >> 0) & 0xff); \
159 cdb.data[9] = 0; \
160 cdb.len = 10;
161
162 #define MKSCSI_REQUEST_SENSE(cdb, buflen) \
163 cdb.data[0] = SCSI_REQUEST_SENSE; \
164 cdb.data[1] = 0; \
165 cdb.data[2] = 0; \
166 cdb.data[3] = 0; \
167 cdb.data[4] = (buflen); \
168 cdb.data[5] = 0; \
169 cdb.len = 6;
170
171 #define MKSCSI_SCAN(cdb) \
172 cdb.data[0] = SCSI_SCAN; \
173 cdb.data[1] = 0; \
174 cdb.data[2] = 0; \
175 cdb.data[3] = 0; \
176 cdb.data[4] = 0; \
177 cdb.data[5] = 0; \
178 cdb.len = 6;
179
180 #define MKSCSI_SEND_10(cdb, dtc, dtq, buflen) \
181 cdb.data[0] = SCSI_SEND_10; \
182 cdb.data[1] = 0; \
183 cdb.data[2] = (dtc); \
184 cdb.data[3] = 0; \
185 cdb.data[4] = (((dtq) >> 8) & 0xff); \
186 cdb.data[5] = (((dtq) >> 0) & 0xff); \
187 cdb.data[6] = (((buflen) >> 16) & 0xff); \
188 cdb.data[7] = (((buflen) >> 8) & 0xff); \
189 cdb.data[8] = (((buflen) >> 0) & 0xff); \
190 cdb.data[9] = 0; \
191 cdb.len = 10;
192
193 #define MKSCSI_TEST_UNIT_READY(cdb) \
194 cdb.data[0] = SCSI_TEST_UNIT_READY; \
195 cdb.data[1] = 0; \
196 cdb.data[2] = 0; \
197 cdb.data[3] = 0; \
198 cdb.data[4] = 0; \
199 cdb.data[5] = 0; \
200 cdb.len = 6;
201
202 /*--------------------------------------------------------------------------*/
203
204 static inline int
getbitfield(unsigned char * pageaddr,int mask,int shift)205 getbitfield (unsigned char *pageaddr, int mask, int shift)
206 {
207 return ((*pageaddr >> shift) & mask);
208 }
209
210 /* defines for request sense return block */
211 #define get_RS_information_valid(b) getbitfield(b + 0x00, 1, 7)
212 #define get_RS_error_code(b) getbitfield(b + 0x00, 0x7f, 0)
213 #define get_RS_filemark(b) getbitfield(b + 0x02, 1, 7)
214 #define get_RS_EOM(b) getbitfield(b + 0x02, 1, 6)
215 #define get_RS_ILI(b) getbitfield(b + 0x02, 1, 5)
216 #define get_RS_sense_key(b) getbitfield(b + 0x02, 0x0f, 0)
217 #define get_RS_information(b) getnbyte(b+0x03, 4)
218 #define get_RS_additional_length(b) b[0x07]
219 #define get_RS_ASC(b) b[0x0c]
220 #define get_RS_ASCQ(b) b[0x0d]
221 #define get_RS_SKSV(b) getbitfield(b+0x0f,1,7)
222
223 /*--------------------------------------------------------------------------*/
224
225 #define mmToIlu(mm) (((mm) * 300) / MM_PER_INCH)
226 #define iluToMm(ilu) (((ilu) * MM_PER_INCH) / 300)
227
228 /*--------------------------------------------------------------------------*/
229
230 #define GAMMA_LENGTH 0x400 /* number of value per color */
231
232 /*--------------------------------------------------------------------------*/
233
234 enum Teco_Option
235 {
236 OPT_NUM_OPTS = 0,
237
238 OPT_MODE_GROUP,
239 OPT_MODE, /* scanner modes */
240 OPT_RESOLUTION, /* X and Y resolution */
241
242 OPT_GEOMETRY_GROUP,
243 OPT_TL_X, /* upper left X */
244 OPT_TL_Y, /* upper left Y */
245 OPT_BR_X, /* bottom right X */
246 OPT_BR_Y, /* bottom right Y */
247
248 OPT_ENHANCEMENT_GROUP,
249 OPT_CUSTOM_GAMMA, /* Use the custom gamma tables */
250 OPT_GAMMA_VECTOR_R, /* Custom Red gamma table */
251 OPT_GAMMA_VECTOR_G, /* Custom Green Gamma table */
252 OPT_GAMMA_VECTOR_B, /* Custom Blue Gamma table */
253 OPT_GAMMA_VECTOR_GRAY, /* Custom Grayscale Gamma table */
254 OPT_THRESHOLD, /* Threshold */
255 OPT_DITHER,
256 OPT_PREVIEW,
257
258 /* must come last: */
259 OPT_NUM_OPTIONS
260 };
261
262 /*--------------------------------------------------------------------------*/
263
264 /*
265 * Scanner supported by this backend.
266 */
267 struct scanners_supported
268 {
269 int scsi_type;
270 char scsi_teco_name[12]; /* real name of the scanner */
271 enum
272 {
273 TECO_VM3552
274 }
275 tecoref;
276 char *real_vendor; /* brand on the box */
277 char *real_product; /* name on the box */
278
279 SANE_Range res_range;
280
281 int x_resolution_max; /* maximum X dpi */
282 int y_resolution_max; /* maximum Y dpi */
283 };
284
285 /*--------------------------------------------------------------------------*/
286
287 #define BLACK_WHITE_STR SANE_VALUE_SCAN_MODE_LINEART
288 #define GRAY_STR SANE_VALUE_SCAN_MODE_GRAY
289 #define COLOR_STR SANE_VALUE_SCAN_MODE_COLOR
290
291 /*--------------------------------------------------------------------------*/
292
293 /* Define a scanner occurrence. */
294 typedef struct Teco_Scanner
295 {
296 struct Teco_Scanner *next;
297 SANE_Device sane;
298
299 char *devicename;
300 int sfd; /* device handle */
301
302 /* Infos from inquiry. */
303 char scsi_type;
304 char scsi_vendor[9];
305 char scsi_product[17];
306 char scsi_version[5];
307 char scsi_teco_name[12]; /* real name of the scanner */
308
309 /* SCSI handling */
310 size_t buffer_size; /* size of the buffer */
311 SANE_Byte *buffer; /* for SCSI transfer. */
312
313 /* Scanner infos. */
314 const struct scanners_supported *def; /* default options for that scanner */
315
316 /* Scanning handling. */
317 int scanning; /* TRUE if a scan is running. */
318 int x_resolution; /* X resolution in DPI */
319 int y_resolution; /* Y resolution in DPI */
320 int x_tl; /* X top left */
321 int y_tl; /* Y top left */
322 int x_br; /* X bottom right */
323 int y_br; /* Y bottom right */
324 int width; /* width of the scan area in mm */
325 int length; /* length of the scan area in mm */
326
327 enum
328 {
329 TECO_BW,
330 TECO_GRAYSCALE,
331 TECO_COLOR
332 }
333 scan_mode;
334
335 int depth; /* depth per color */
336
337 size_t bytes_left; /* number of bytes left to give to the backend */
338
339 size_t real_bytes_left; /* number of bytes left the scanner will return. */
340 size_t bytes_per_raster; /* bytes per raster. In B&W and Gray,
341 that the same as
342 param.bytes_per_lines. In Color,
343 it's a third.
344 */
345
346 SANE_Byte *image; /* keep the raw image here */
347 size_t image_size; /* allocated size of image */
348 size_t image_begin; /* first significant byte in image */
349 size_t image_end; /* first free byte in image */
350
351 int does_color_shift; /* in color mode only, do we need to
352 * apply the color shifting algorithm?
353 * It is necessary for the VM3552
354 * without a RAM extension. With the
355 * RAM extension, the scanner does
356 * it. */
357
358 int color_shift; /* for color scan: number of lines to
359 * shift the colors. The higher the
360 * resolution, the higher this
361 * number. */
362
363 int raster_size; /* size of a raster */
364 int raster_num; /* for color scan, current raster read */
365 int raster_real; /* real number of raster in the
366 * scan. This is necessary since I
367 * don't know how to reliably compute
368 * the number of lines */
369 int raster_ahead; /* max size of the incomplete lines */
370 int line; /* current line of the scan */
371
372 SANE_Parameters params;
373
374 /* Options */
375 SANE_Option_Descriptor opt[OPT_NUM_OPTIONS];
376 Option_Value val[OPT_NUM_OPTIONS];
377
378 /* Gamma table. 1 array per colour. */
379 SANE_Word gamma_GRAY[GAMMA_LENGTH];
380 SANE_Word gamma_R[GAMMA_LENGTH];
381 SANE_Word gamma_G[GAMMA_LENGTH];
382 SANE_Word gamma_B[GAMMA_LENGTH];
383 }
384 Teco_Scanner;
385
386 /*--------------------------------------------------------------------------*/
387
388 /* Debug levels.
389 * Should be common to all backends. */
390
391 #define DBG_error0 0
392 #define DBG_error 1
393 #define DBG_sense 2
394 #define DBG_warning 3
395 #define DBG_inquiry 4
396 #define DBG_info 5
397 #define DBG_info2 6
398 #define DBG_proc 7
399 #define DBG_read 8
400 #define DBG_sane_init 10
401 #define DBG_sane_proc 11
402 #define DBG_sane_info 12
403 #define DBG_sane_option 13
404
405 /*--------------------------------------------------------------------------*/
406
407 /* 32 bits from an array to an integer (eg ntohl). */
408 #define B32TOI(buf) \
409 ((((unsigned char *)buf)[0] << 24) | \
410 (((unsigned char *)buf)[1] << 16) | \
411 (((unsigned char *)buf)[2] << 8) | \
412 (((unsigned char *)buf)[3] << 0))
413
414 #define B24TOI(buf) \
415 ((((unsigned char *)buf)[0] << 16) | \
416 (((unsigned char *)buf)[1] << 8) | \
417 (((unsigned char *)buf)[2] << 0))
418
419 #define B16TOI(buf) \
420 ((((unsigned char *)buf)[0] << 8) | \
421 (((unsigned char *)buf)[1] << 0))
422