• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Sceptre S1200 scanner. */
42 #define SCSI_TEST_UNIT_READY			0x00
43 #define SCSI_GET_STATUS					0x02
44 #define SCSI_INQUIRY					0x12
45 #define SCSI_MODE_SELECT				0x15
46 #define SCSI_MODE_SENSE					0x1a
47 #define SCSI_SCAN						0x1b
48 #define SCSI_RECEIVE_DIAG				0x1c
49 #define SCSI_SEND_DIAG					0x1D
50 #define SCSI_SET_WINDOW					0x24
51 #define SCSI_READ_10					0x28
52 #define SCSI_REQUEST_SENSE				0x03
53 #define SCSI_SEND_10					0x2a
54 #define SCSI_GET_DATA_BUFFER_STATUS		0x34
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_TEST_UNIT_READY(cdb) \
92 	cdb.data[0] = SCSI_TEST_UNIT_READY; \
93 	cdb.data[1] = 0; \
94 	cdb.data[2] = 0; \
95 	cdb.data[3] = 0; \
96 	cdb.data[4] = 0; \
97 	cdb.data[5] = 0; \
98 	cdb.len = 6;
99 
100 #define MKSCSI_GET_STATUS(cdb, buflen) \
101 	cdb.data[0] = SCSI_GET_STATUS; \
102 	cdb.data[1] = 0; \
103 	cdb.data[2] = 0; \
104 	cdb.data[3] = 0; \
105 	cdb.data[4] = buflen; \
106 	cdb.data[5] = 0; \
107 	cdb.len = 6;
108 
109 #define MKSCSI_INQUIRY(cdb, buflen) \
110 	cdb.data[0] = SCSI_INQUIRY; \
111 	cdb.data[1] = 0; \
112 	cdb.data[2] = 0; \
113 	cdb.data[3] = 0; \
114 	cdb.data[4] = buflen; \
115 	cdb.data[5] = 0; \
116 	cdb.len = 6;
117 
118 #define MKSCSI_MODE_SELECT(cdb, pf, sp, buflen) \
119 	cdb.data[0] = SCSI_MODE_SELECT; \
120 	cdb.data[1] = MKSCSI_BIT(pf, 4) | MKSCSI_BIT(sp, 0); \
121 	cdb.data[2] = 0; \
122 	cdb.data[3] = 0; \
123 	cdb.data[4] = buflen; \
124 	cdb.data[5] = 0; \
125 	cdb.len = 6;
126 
127 #define MKSCSI_MODE_SENSE(cdb, pc, page_code, buflen) \
128 	cdb.data[0] = SCSI_MODE_SENSE; \
129 	cdb.data[1] = 0; \
130 	cdb.data[2] = MKSCSI_I2B(pc, 6, 7) | MKSCSI_I2B(page_code, 0, 5); \
131 	cdb.data[3] = 0; \
132 	cdb.data[4] = buflen; \
133 	cdb.data[5] = 0; \
134 	cdb.len = 6;
135 
136 #define MKSCSI_SCAN(cdb) \
137 	cdb.data[0] = SCSI_SCAN; \
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.len = 6;
144 
145 #define MKSCSI_RECEIVE_DIAG(cdb, pc, buflen) \
146 	cdb.data[0] = SCSI_RECEIVE_DIAG; \
147 	cdb.data[1] = 0; \
148 	cdb.data[2] = pc; \
149 	cdb.data[3] = (((buflen) >> 8) & 0xff); \
150 	cdb.data[4] = (((buflen) >> 0) & 0xff); \
151 	cdb.data[5] = 0; \
152 	cdb.len = 6;
153 
154 #define MKSCSI_SEND_DIAG(cdb, buflen) \
155 	cdb.data[0] = SCSI_SEND_DIAG; \
156 	cdb.data[1] = 0; \
157 	cdb.data[2] = 0; \
158 	cdb.data[3] = (((buflen) >> 8) & 0xff); \
159 	cdb.data[4] = (((buflen) >> 0) & 0xff); \
160 	cdb.data[5] = 0; \
161 	cdb.len = 6;
162 
163 #define MKSCSI_SET_WINDOW(cdb, buflen) \
164 	cdb.data[0] = SCSI_SET_WINDOW; \
165 	cdb.data[1] = 0; \
166 	cdb.data[2] = 0; \
167 	cdb.data[3] = 0; \
168 	cdb.data[4] = 0; \
169 	cdb.data[5] = 0; \
170 	cdb.data[6] = (((buflen) >> 16) & 0xff); \
171 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
172 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
173 	cdb.data[9] = 0; \
174 	cdb.len = 10;
175 
176 #define MKSCSI_READ_10(cdb, dtc, dtq, buflen) \
177 	cdb.data[0] = SCSI_READ_10; \
178 	cdb.data[1] = 0; \
179 	cdb.data[2] = (dtc); \
180 	cdb.data[3] = 0; \
181 	cdb.data[4] = (((dtq) >> 8) & 0xff); \
182 	cdb.data[5] = (((dtq) >> 0) & 0xff); \
183 	cdb.data[6] = (((buflen) >> 16) & 0xff); \
184 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
185 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
186 	cdb.data[9] = 0; \
187 	cdb.len = 10;
188 
189 #define MKSCSI_REQUEST_SENSE(cdb, buflen) \
190 	cdb.data[0] = SCSI_REQUEST_SENSE; \
191 	cdb.data[1] = 0; \
192 	cdb.data[2] = 0; \
193 	cdb.data[3] = 0; \
194 	cdb.data[4] = (buflen); \
195 	cdb.data[5] = 0; \
196 	cdb.len = 6;
197 
198 #define MKSCSI_SEND_10(cdb, dtc, dtq, buflen) \
199 	cdb.data[0] = SCSI_SEND_10; \
200 	cdb.data[1] = 0; \
201 	cdb.data[2] = (dtc); \
202 	cdb.data[3] = 0; \
203 	cdb.data[4] = (((dtq) >> 8) & 0xff); \
204 	cdb.data[5] = (((dtq) >> 0) & 0xff); \
205 	cdb.data[6] = (((buflen) >> 16) & 0xff); \
206 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
207 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
208 	cdb.data[9] = 0; \
209 	cdb.len = 10;
210 
211 #define MKSCSI_GET_DATA_BUFFER_STATUS(cdb, wait, buflen) \
212 	cdb.data[0] = SCSI_GET_DATA_BUFFER_STATUS; \
213 	cdb.data[1] = MKSCSI_BIT(wait, 0); \
214 	cdb.data[2] = 0; \
215 	cdb.data[3] = 0; \
216 	cdb.data[4] = 0; \
217 	cdb.data[5] = 0; \
218 	cdb.data[6] = 0; \
219 	cdb.data[7] = (((buflen) >>  8) & 0xff); \
220 	cdb.data[8] = (((buflen) >>  0) & 0xff); \
221 	cdb.data[9] = 0; \
222 	cdb.len = 10;
223 
224 /*--------------------------------------------------------------------------*/
225 
226 #define length_quant SANE_UNFIX(SANE_FIX(MM_PER_INCH / 600))
227 #define mmToIlu(mm) ((mm) / length_quant)
228 #define iluToMm(ilu) ((ilu) * length_quant)
229 
230 /*--------------------------------------------------------------------------*/
231 
232 #define GAMMA_LENGTH 0x100	/* number of value per color */
233 
234 /*--------------------------------------------------------------------------*/
235 
236 enum Sceptre_Option
237 {
238   OPT_NUM_OPTS = 0,
239 
240   OPT_MODE_GROUP,
241   OPT_MODE,			/* scanner modes */
242   OPT_RESOLUTION,		/* X and Y resolution */
243 
244   OPT_GEOMETRY_GROUP,
245   OPT_TL_X,			/* upper left X */
246   OPT_TL_Y,			/* upper left Y */
247   OPT_BR_X,			/* bottom right X */
248   OPT_BR_Y,			/* bottom right Y */
249 
250   OPT_ENHANCEMENT_GROUP,
251   OPT_CUSTOM_GAMMA,		/* Use the custom gamma tables */
252   OPT_GAMMA_VECTOR_R,		/* Custom Red gamma table */
253   OPT_GAMMA_VECTOR_G,		/* Custom Green Gamma table */
254   OPT_GAMMA_VECTOR_B,		/* Custom Blue Gamma table */
255   OPT_THRESHOLD,		/* Threshold */
256   OPT_HALFTONE_PATTERN,		/* Halftone pattern (1 to 4) */
257 
258   OPT_PREVIEW,			/* preview mode */
259 
260   /* must come last: */
261   OPT_NUM_OPTIONS
262 };
263 
264 /*--------------------------------------------------------------------------*/
265 
266 /*
267  * Scanner supported by this backend.
268  */
269 struct scanners_supported
270 {
271   /* From scsi inquiry */
272   int scsi_type;
273   char scsi_vendor[9];
274   char scsi_product[17];
275 
276   char *real_vendor;
277   char *real_product;
278 };
279 
280 /*--------------------------------------------------------------------------*/
281 
282 #define LINEART_STR			SANE_VALUE_SCAN_MODE_LINEART
283 #define HALFTONE_STR		        SANE_VALUE_SCAN_MODE_HALFTONE
284 #define GRAY_STR			SANE_VALUE_SCAN_MODE_GRAY
285 #define COLOR_STR			SANE_VALUE_SCAN_MODE_COLOR
286 
287 /*--------------------------------------------------------------------------*/
288 
289 /* Define a scanner occurrence. */
290 typedef struct Sceptre_Scanner
291 {
292   struct Sceptre_Scanner *next;
293   SANE_Device sane;
294 
295   char *devicename;
296   int sfd;			/* device handle */
297 
298   /* Infos from inquiry. */
299   char scsi_type;
300   char scsi_vendor[9];
301   char scsi_product[17];
302   char scsi_version[5];
303 
304   /* Scanner infos. */
305   SANE_Range x_range;
306   SANE_Range y_range;
307   SANE_Range resolution_range;
308   int scnum;			/* index of that scanner in
309 				   * scanners_supported */
310 
311   /* SCSI handling */
312   SANE_Byte *buffer;		/* for SCSI transfer. */
313   size_t buffer_size;		/* allocated size of buffer */
314 
315   /* Scanning handling. */
316   int scanning;			/* TRUE is a scan is running. */
317   int resolution;		/* scan resolution */
318   int x_tl;			/* X top left */
319   int y_tl;			/* Y top left */
320   int x_br;			/* X bottom right */
321   int y_br;			/* Y bottom right */
322   int width;			/* width of the scan area in mm */
323   int length;			/* length of the scan area in mm */
324 
325   enum
326   {
327     SCEPTRE_LINEART,
328     SCEPTRE_HALFTONE,
329     SCEPTRE_GRAYSCALE,
330     SCEPTRE_COLOR
331   }
332   scan_mode;
333 
334   int depth;			/* depth per color */
335   int halftone_param;		/* haltone number, valid for SCEPTRE_HALFTONE */
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 
341   SANE_Byte *image;		/* keep the raw image here */
342   size_t image_size;		/* allocated size of image */
343   size_t image_begin;		/* first significant byte in image */
344   size_t image_end;		/* first free byte in image */
345 
346   int color_shift;		/* for color scan: number of lines to
347 				   * shift the colors. The higher the
348 				   * resolution, the higher this
349 				   * number. */
350 
351   int raster_size;		/* size of a raster */
352   int raster_num;		/* for colour scan, current raster read */
353   int raster_real;		/* real number of raster in the
354 				   * scan. This is necessary since I
355 				   * don't know how to reliably compute
356 				   * the number of lines */
357 
358   int raster_ahead;		/* max size of the incomplete lines */
359 
360   int line;			/* current line of the scan */
361 
362 
363   SANE_Parameters params;
364 
365   /* Options */
366   SANE_Option_Descriptor opt[OPT_NUM_OPTIONS];
367   Option_Value val[OPT_NUM_OPTIONS];
368 
369   /* Gamma table. 1 array per colour. */
370   SANE_Word gamma_R[GAMMA_LENGTH];
371   SANE_Word gamma_G[GAMMA_LENGTH];
372   SANE_Word gamma_B[GAMMA_LENGTH];
373 
374 }
375 Sceptre_Scanner;
376 
377 /*--------------------------------------------------------------------------*/
378 
379 /* Debug levels.
380  * Should be common to all backends. */
381 
382 #define DBG_error0  0
383 #define DBG_error   1
384 #define DBG_sense   2
385 #define DBG_warning 3
386 #define DBG_inquiry 4
387 #define DBG_info    5
388 #define DBG_info2   6
389 #define DBG_proc    7
390 #define DBG_read    8
391 #define DBG_sane_init   10
392 #define DBG_sane_proc   11
393 #define DBG_sane_info   12
394 #define DBG_sane_option 13
395 
396 /*--------------------------------------------------------------------------*/
397 
398 /* 32 bits from an array to an integer (eg ntohl). */
399 #define B32TOI(buf) \
400 	((((unsigned char *)buf)[0] << 24) | \
401 	 (((unsigned char *)buf)[1] << 16) | \
402 	 (((unsigned char *)buf)[2] <<  8) |  \
403 	 (((unsigned char *)buf)[3] <<  0))
404 
405 /* 16 bits from an array to an integer (eg ntohs). */
406 #define B16TOI(buf) \
407 	((((unsigned char *)buf)[0] <<  8) | \
408 	 (((unsigned char *)buf)[1] <<  0))
409