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