• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2002 Frank Zago (sane at zago dot net)
4    Copyright (C) 2003-2005 Gerard Klaver (gerard at gkall dot hobby dot nl)
5 
6    This file is part of the SANE package.
7 
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 
21    As a special exception, the authors of SANE give permission for
22    additional uses of the libraries contained in this release of SANE.
23 
24    The exception is that, if you link a SANE library with other files
25    to produce an executable, this does not by itself cause the
26    resulting executable to be covered by the GNU General Public
27    License.  Your use of that executable is in no way restricted on
28    account of linking the SANE library code into it.
29 
30    This exception does not, however, invalidate any other reasons why
31    the executable file might be covered by the GNU General Public
32    License.
33 
34    If you submit changes to SANE to the maintainers to be included in
35    a subsequent release, you agree by submitting the changes that
36    those changes may be distributed with this exception intact.
37 
38    If you write modifications of your own for SANE, it is your choice
39    whether to permit this exception to apply to your modifications.
40    If you do not wish that, delete this exception notice.
41 */
42 
43 /* Commands supported by the scanner. */
44 #define SCSI_TEST_UNIT_READY			0x00
45 #define SCSI_REQUEST_SENSE			0x03
46 #define SCSI_VENDOR_06				0x06
47 #define SCSI_VENDOR_09				0x09
48 #define SCSI_VENDOR_0C				0x0C
49 #define SCSI_VENDOR_0E				0x0E
50 #define SCSI_INQUIRY				0x12
51 #define SCSI_SCAN				0x1b
52 #define SCSI_VENDOR_1C				0x1C
53 #define SCSI_SET_WINDOW				0x24
54 #define SCSI_SEND_10				0x2a
55 #define SCSI_READ_10				0x28
56 #define SCSI_OBJECT_POSITION			0x31
57 #define SCSI_GET_DATA_BUFFER_STATUS		0x34
58 
59 typedef struct
60 {
61   unsigned char data[16];
62   int len;
63 }
64 CDB;
65 
66 
67 /* Set a specific bit depending on a boolean.
68  *   MKSCSI_BIT(TRUE, 3) will generate 0x08. */
69 #define MKSCSI_BIT(bit, pos) ((bit)? 1<<(pos): 0)
70 
71 /* Set a value in a range of bits.
72  *   MKSCSI_I2B(5, 3, 5) will generate 0x28 */
73 #define MKSCSI_I2B(bits, pos_b, pos_e) ((bits) << (pos_b) & ((1<<((pos_e)-(pos_b)+1))-1))
74 
75 /* Store an integer in 2, 3 or 4 byte in an array. */
76 #define Ito16(val, buf) { \
77  ((unsigned char *)buf)[0] = ((val) >> 8) & 0xff; \
78  ((unsigned char *)buf)[1] = ((val) >> 0) & 0xff; \
79 }
80 
81 #define Ito24(val, buf) { \
82  ((unsigned char *)buf)[0] = ((val) >> 16) & 0xff; \
83  ((unsigned char *)buf)[1] = ((val) >>  8) & 0xff; \
84  ((unsigned char *)buf)[2] = ((val) >>  0) & 0xff; \
85 }
86 
87 #define Ito32(val, buf) { \
88  ((unsigned char *)buf)[0] = ((val) >> 24) & 0xff; \
89  ((unsigned char *)buf)[1] = ((val) >> 16) & 0xff; \
90  ((unsigned char *)buf)[2] = ((val) >>  8) & 0xff; \
91  ((unsigned char *)buf)[3] = ((val) >>  0) & 0xff; \
92 }
93 
94 #define MKSCSI_GET_DATA_BUFFER_STATUS(cdb, wait, buflen) \
95 	cdb.data[0] = SCSI_GET_DATA_BUFFER_STATUS; \
96 	cdb.data[1] = MKSCSI_BIT(wait, 0); \
97 	cdb.data[2] = 0; \
98 	cdb.data[3] = 0; \
99 	cdb.data[4] = 0; \
100 	cdb.data[5] = 0; \
101 	cdb.data[6] = 0; \
102 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
103 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
104 	cdb.data[9] = 0; \
105 	cdb.len = 10;
106 
107 #define MKSCSI_INQUIRY(cdb, buflen) \
108 	cdb.data[0] = SCSI_INQUIRY; \
109 	cdb.data[1] = 0; \
110 	cdb.data[2] = 0; \
111 	cdb.data[3] = 0; \
112 	cdb.data[4] = buflen; \
113 	cdb.data[5] = 0; \
114 	cdb.len = 6;
115 
116 #define MKSCSI_MODE_SELECT(cdb, pf, sp, buflen) \
117 	cdb.data[0] = SCSI_MODE_SELECT; \
118 	cdb.data[1] = MKSCSI_BIT(pf, 4) | MKSCSI_BIT(sp, 0); \
119 	cdb.data[2] = 0; \
120 	cdb.data[3] = 0; \
121 	cdb.data[4] = buflen; \
122 	cdb.data[5] = 0; \
123 	cdb.len = 6;
124 
125 #define MKSCSI_OBJECT_POSITION(cdb, position) \
126     cdb.data[0] = SCSI_OBJECT_POSITION; \
127     cdb.data[1] = 0; \
128     cdb.data[2] = (((position) >> 16) & 0xff); \
129     cdb.data[3] = (((position) >>  8) & 0xff); \
130     cdb.data[4] = (((position) >>  0) & 0xff); \
131     cdb.data[5] = 0; \
132     cdb.data[6] = 0; \
133     cdb.data[7] = 0; \
134     cdb.data[8] = 0; \
135     cdb.data[9] = 0; \
136     cdb.len = 10;
137 
138 #define MKSCSI_SET_WINDOW(cdb, buflen) \
139 	cdb.data[0] = SCSI_SET_WINDOW; \
140 	cdb.data[1] = 0; \
141 	cdb.data[2] = 0; \
142 	cdb.data[3] = 0; \
143 	cdb.data[4] = 0; \
144 	cdb.data[5] = 0; \
145 	cdb.data[6] = (((buflen) >> 16) & 0xff); \
146 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
147 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
148 	cdb.data[9] = 0; \
149 	cdb.len = 10;
150 
151 #define MKSCSI_READ_10(cdb, dtc, dtq, buflen) \
152 	cdb.data[0] = SCSI_READ_10; \
153 	cdb.data[1] = 0; \
154 	cdb.data[2] = (dtc); \
155 	cdb.data[3] = 0; \
156 	cdb.data[4] = (((dtq) >> 8) & 0xff); \
157 	cdb.data[5] = (((dtq) >> 0) & 0xff); \
158 	cdb.data[6] = (((buflen) >> 16) & 0xff); \
159 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
160 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
161 	cdb.data[9] = 0; \
162 	cdb.len = 10;
163 
164 #define MKSCSI_REQUEST_SENSE(cdb, buflen) \
165 	cdb.data[0] = SCSI_REQUEST_SENSE; \
166 	cdb.data[1] = 0; \
167 	cdb.data[2] = 0; \
168 	cdb.data[3] = 0; \
169 	cdb.data[4] = (buflen); \
170 	cdb.data[5] = 0; \
171 	cdb.len = 6;
172 
173 #define MKSCSI_SCAN(cdb) \
174 	cdb.data[0] = SCSI_SCAN; \
175 	cdb.data[1] = 0; \
176 	cdb.data[2] = 0; \
177 	cdb.data[3] = 0; \
178 	cdb.data[4] = 0; \
179 	cdb.data[5] = 0; \
180 	cdb.len = 6;
181 
182 #define MKSCSI_SEND_10(cdb, dtc, dtq, buflen) \
183 	cdb.data[0] = SCSI_SEND_10; \
184 	cdb.data[1] = 0; \
185 	cdb.data[2] = (dtc); \
186 	cdb.data[3] = 0; \
187 	cdb.data[4] = (((dtq) >> 8) & 0xff); \
188 	cdb.data[5] = (((dtq) >> 0) & 0xff); \
189 	cdb.data[6] = (((buflen) >> 16) & 0xff); \
190 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
191 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
192 	cdb.data[9] = 0; \
193 	cdb.len = 10;
194 
195 #define MKSCSI_TEST_UNIT_READY(cdb) \
196 	cdb.data[0] = SCSI_TEST_UNIT_READY; \
197 	cdb.data[1] = 0; \
198 	cdb.data[2] = 0; \
199 	cdb.data[3] = 0; \
200 	cdb.data[4] = 0; \
201 	cdb.data[5] = 0; \
202 	cdb.len = 6;
203 
204 #define MKSCSI_VENDOR_SPEC(cdb, command, length) { \
205     assert(length == 6 || length == 10 || length == 12 || length == 16); \
206     memset(cdb.data, 0, length); \
207 	cdb.data[0] = command; \
208 	cdb.len = length; \
209 }
210 
211 /*--------------------------------------------------------------------------*/
212 
213 static inline int
getbitfield(unsigned char * pageaddr,int mask,int shift)214 getbitfield (unsigned char *pageaddr, int mask, int shift)
215 {
216   return ((*pageaddr >> shift) & mask);
217 }
218 
219 /* defines for request sense return block */
220 #define get_RS_information_valid(b)       getbitfield(b + 0x00, 1, 7)
221 #define get_RS_error_code(b)              getbitfield(b + 0x00, 0x7f, 0)
222 #define get_RS_filemark(b)                getbitfield(b + 0x02, 1, 7)
223 #define get_RS_EOM(b)                     getbitfield(b + 0x02, 1, 6)
224 #define get_RS_ILI(b)                     getbitfield(b + 0x02, 1, 5)
225 #define get_RS_sense_key(b)               getbitfield(b + 0x02, 0x0f, 0)
226 #define get_RS_information(b)             getnbyte(b+0x03, 4)
227 #define get_RS_additional_length(b)       b[0x07]
228 #define get_RS_ASC(b)                     b[0x0c]
229 #define get_RS_ASCQ(b)                    b[0x0d]
230 #define get_RS_SKSV(b)                    getbitfield(b+0x0f,1,7)
231 
232 /*--------------------------------------------------------------------------*/
233 
234 #define mmToIlu(mm) (((mm) * dev->def->x_resolution_max) / MM_PER_INCH)
235 #define iluToMm(ilu) (((ilu) * MM_PER_INCH) / dev->def->x_resolution_max)
236 
237 /*--------------------------------------------------------------------------*/
238 
239 #define GAMMA_LENGTH 0x400	/* number of value per color */
240 
241 /*--------------------------------------------------------------------------*/
242 
243 /* Black magic for color adjustment. Used only for VM3575. */
244 struct dpi_color_adjust
245 {
246   int resolution;		/* in dpi. 0 means all resolution supported. */
247 
248 #if 0
249   int z1_color_0;		/* 0, 1 or 2 */
250   int z1_color_1;		/* idem */
251   int z1_color_2;		/* idem */
252 #endif
253 
254   int z3_color_0;		/* 0, 1 or 2 */
255   int z3_color_1;		/* idem */
256   int z3_color_2;		/* idem */
257 
258   int factor_x;
259 
260   int color_shift;		/* color plane shift in pixel. If a
261 				   * negative shift seems necessary, set
262 				   * factor_x to 1 */
263 };
264 
265 /*--------------------------------------------------------------------------*/
266 
267 enum Teco_Option
268 {
269   /* Must come first */
270   OPT_NUM_OPTS = 0,
271 
272   OPT_MODE_GROUP,
273   OPT_MODE,			/* scanner modes */
274   OPT_RESOLUTION,		/* X and Y resolution */
275 
276   OPT_GEOMETRY_GROUP,
277   OPT_TL_X,			/* upper left X */
278   OPT_TL_Y,			/* upper left Y */
279   OPT_BR_X,			/* bottom right X */
280   OPT_BR_Y,			/* bottom right Y */
281 
282   OPT_ENHANCEMENT_GROUP,
283   OPT_CUSTOM_GAMMA,		/* Use the custom gamma tables */
284   OPT_GAMMA_VECTOR_R,		/* Custom Red gamma table */
285   OPT_GAMMA_VECTOR_G,		/* Custom Green Gamma table */
286   OPT_GAMMA_VECTOR_B,		/* Custom Blue Gamma table */
287   OPT_GAMMA_VECTOR_GRAY,	/* Custom Grayscale Gamma table */
288 
289   OPT_DITHER,
290   OPT_FILTER_COLOR,		/* which color to filter */
291   OPT_THRESHOLD,		/* Threshold */
292   OPT_WHITE_LEVEL_R,		/* white level correction RED */
293   OPT_WHITE_LEVEL_G,            /* white level correction GREEN */
294   OPT_WHITE_LEVEL_B,            /* white level correction BLUE */
295 
296   OPT_PREVIEW,
297 
298   /* must come last: */
299   OPT_NUM_OPTIONS
300 };
301 
302 /*--------------------------------------------------------------------------*/
303 
304 /*
305  * Scanner supported by this backend.
306  */
307 struct scanners_supported
308 {
309   int scsi_type;
310   char scsi_teco_name[12];	/* real name of the scanner */
311   enum
312   {
313     TECO_VM3564,
314     TECO_VM356A,
315     TECO_VM3575,
316     TECO_VM6575,
317     TECO_VM656A,
318     TECO_VM6586
319   }
320   tecoref;
321   char *real_vendor;		/* brand on the box */
322   char *real_product;		/* name on the box */
323 
324   SANE_Range res_range;
325 
326   int x_resolution_max;		/* maximum X dpi */
327   int y_resolution_max;		/* maximum Y dpi */
328 
329   int cal_length;		/* size of a calibration line in pixels */
330   int cal_lines;		/* number of calibration lines to read */
331   int cal_col_len;		/* number of byte to code one color */
332   int cal_algo;			/* default algo to use to compute calibration line */
333 
334   /* Minimum and maximum width and length supported. */
335   SANE_Range x_range;
336   SANE_Range y_range;
337 
338   /* Resolutions supported in color mode. */
339   const struct dpi_color_adjust *color_adjust;
340 };
341 
342 /*--------------------------------------------------------------------------*/
343 
344 /* Define a scanner occurrence. */
345 typedef struct Teco_Scanner
346 {
347   struct Teco_Scanner *next;
348   SANE_Device sane;
349 
350   char *devicename;
351   int sfd;			/* device handle */
352 
353   /* Infos from inquiry. */
354   char scsi_type;
355   char scsi_vendor[9];
356   char scsi_product[17];
357   char scsi_version[5];
358   char scsi_teco_name[12];	/* real name of the scanner */
359 
360   /* SCSI handling */
361   size_t buffer_size;		/* size of the buffer */
362   SANE_Byte *buffer;		/* for SCSI transfer. */
363 
364   /* Scanner infos. */
365   const struct scanners_supported *def;	/* default options for that scanner */
366 
367   SANE_Word *resolutions_list;
368 
369   /* Scanning handling. */
370   int scanning;			/* TRUE if a scan is running. */
371   int x_resolution;		/* X resolution in DPI */
372   int y_resolution;		/* Y resolution in DPI */
373   int x_tl;			/* X top left */
374   int y_tl;			/* Y top left */
375   int x_br;			/* X bottom right */
376   int y_br;			/* Y bottom right */
377   int width;			/* width of the scan area in mm */
378   int length;			/* length of the scan area in mm */
379   int depth;			/* depth per color */
380 
381   enum
382   {
383     TECO_BW,
384     TECO_GRAYSCALE,
385     TECO_COLOR
386   }
387   scan_mode;
388 
389   size_t bytes_left;		/* number of bytes left to give to the backend */
390 
391   size_t real_bytes_left;	/* number of bytes left the scanner will return. */
392 
393   SANE_Byte *image;		/* keep the raw image here */
394   size_t image_size;		/* allocated size of image */
395   size_t image_begin;		/* first significant byte in image */
396   size_t image_end;		/* first free byte in image */
397 
398   const struct dpi_color_adjust *color_adjust;
399 
400   size_t bytes_per_raster;	/* bytes per raster. In B&W and Gray,
401 				   that the same as
402 				   param.bytes_per_lines. In Color,
403 				   it's a third.
404 				 */
405 
406   int raster_size;		/* size of a raster */
407   int raster_num;		/* for color scan, current raster read */
408   int raster_real;		/* real number of raster in the
409 				   * scan. This is necessary since I
410 				   * don't know how to reliably compute
411 				   * the number of lines */
412   int raster_ahead;		/* max size of the incomplete lines */
413   int line;			/* current line of the scan */
414 
415   SANE_Parameters params;
416 
417   /* Options */
418   SANE_Option_Descriptor opt[OPT_NUM_OPTIONS];
419   Option_Value val[OPT_NUM_OPTIONS];
420 
421   /* Gamma table. 1 array per color. */
422   SANE_Word gamma_GRAY[GAMMA_LENGTH];
423   SANE_Word gamma_R[GAMMA_LENGTH];
424   SANE_Word gamma_G[GAMMA_LENGTH];
425   SANE_Word gamma_B[GAMMA_LENGTH];
426 }
427 Teco_Scanner;
428 
429 /*--------------------------------------------------------------------------*/
430 
431 /* Debug levels.
432  * Should be common to all backends. */
433 
434 #define DBG_error0  0
435 #define DBG_error   1
436 #define DBG_sense   2
437 #define DBG_warning 3
438 #define DBG_inquiry 4
439 #define DBG_info    5
440 #define DBG_info2   6
441 #define DBG_proc    7
442 #define DBG_read    8
443 #define DBG_sane_init   10
444 #define DBG_sane_proc   11
445 #define DBG_sane_info   12
446 #define DBG_sane_option 13
447 
448 /*--------------------------------------------------------------------------*/
449 
450 /* 32 bits from an array to an integer (eg ntohl). */
451 #define B32TOI(buf) \
452 	((((unsigned char *)buf)[0] << 24) | \
453 	 (((unsigned char *)buf)[1] << 16) | \
454 	 (((unsigned char *)buf)[2] <<  8) |  \
455 	 (((unsigned char *)buf)[3] <<  0))
456 
457 #define B16TOI(buf) \
458 	((((unsigned char *)buf)[0] <<  8) | \
459 	 (((unsigned char *)buf)[1] <<  0))
460