1 #ifndef SP15C_H
2 #define SP15C_H
3 
4 /* sane - Scanner Access Now Easy.
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    This file implements a SANE backend for Fujitsu ScanParner 15c
43    flatbed/ADF scanners.  It was derived from the COOLSCAN driver.
44    Written by Randolph Bentson <bentson@holmsjoen.com> */
45 
46 /* ------------------------------------------------------------------------- */
47 /*
48  * Revision 1.8  2008/05/15 12:50:24  ellert-guest
49  * Fix for bug #306751: sanei-thread with pthreads on 64 bit
50  *
51  * Revision 1.7  2005-09-19 19:57:48  fzago-guest
52  * Replaced __unused__ with __sane_unused__ to avoid a namespace conflict.
53  *
54  * Revision 1.6  2004/11/13 19:53:04  fzago-guest
55  * Fixes some warnings.
56  *
57  * Revision 1.5  2004/05/23 17:28:56  hmg-guest
58  * Use sanei_thread instead of fork() in the unmaintained backends.
59  * Patches from Mattias Ellert (bugs: 300635, 300634, 300633, 300629).
60  *
61  * Revision 1.4  2003/12/27 17:48:38  hmg-guest
62  * Silenced some compilation warnings.
63  *
64  * Revision 1.3  2000/08/12 15:09:42  pere
65  * Merge devel (v1.0.3) into head branch.
66  *
67  * Revision 1.1.2.3  2000/03/14 17:47:14  abel
68  * new version of the Sharp backend added.
69  *
70  * Revision 1.1.2.2  2000/01/26 03:51:50  pere
71  * Updated backends sp15c (v1.12) and m3096g (v1.11).
72  *
73  * Revision 1.7  2000/01/05 05:22:26  bentson
74  * indent to barfable GNU style
75  *
76  * Revision 1.6  1999/12/03 20:57:13  bentson
77  * add MEDIA CHECK command
78  *
79  * Revision 1.5  1999/11/24 15:55:56  bentson
80  * remove some debug stuff; rename function
81  *
82  * Revision 1.4  1999/11/23 18:54:26  bentson
83  * tidy up function types for constraint checking
84  *
85  * Revision 1.3  1999/11/23 06:41:54  bentson
86  * add debug flag to interface
87  *
88  * Revision 1.2  1999/11/22 18:15:20  bentson
89  * more work on color support
90  *
91  * Revision 1.1  1999/11/19 15:09:08  bentson
92  * cribbed from m3096g
93  *
94  */
95 
96 static const SANE_Device **devlist = NULL;
97 static int num_devices;
98 static struct sp15c *first_dev;
99 
100 enum sp15c_Option
101   {
102     OPT_NUM_OPTS = 0,
103 
104     OPT_MODE_GROUP,
105     OPT_SOURCE,
106     OPT_MODE,
107     OPT_TYPE,
108     OPT_X_RES,
109     OPT_Y_RES,
110     OPT_PRESCAN,
111     OPT_PREVIEW_RES,
112 
113     OPT_GEOMETRY_GROUP,
114     OPT_TL_X,			/* in mm/2^16 */
115     OPT_TL_Y,			/* in mm/2^16 */
116     OPT_BR_X,			/* in mm/2^16 */
117     OPT_BR_Y,			/* in mm/2^16 */
118 
119     OPT_ENHANCEMENT_GROUP,
120     OPT_AVERAGING,
121     OPT_BRIGHTNESS,
122     OPT_THRESHOLD,
123 
124     OPT_ADVANCED_GROUP,
125     OPT_PREVIEW,
126 
127     /* must come last: */
128     NUM_OPTIONS
129   };
130 
131 struct sp15c
132   {
133     struct sp15c *next;
134 
135     SANE_Option_Descriptor opt[NUM_OPTIONS];
136     SANE_Device sane;
137 
138     char vendor[9];
139     char product[17];
140     char version[5];
141 
142     char *devicename;		/* name of the scanner device */
143     int sfd;			/* output file descriptor, scanner device */
144     int pipe;
145     int reader_pipe;
146 
147     int scanning;		/* "in progress" flag */
148     int autofeeder;		/* detected */
149     int use_adf;		/* requested */
150     SANE_Pid reader_pid;	/* child is running */
151     int prescan;		/* ??? */
152 
153 /***** terms for "set window" command *****/
154     int x_res;			/* resolution in */
155     int y_res;			/* pixels/inch */
156     int tl_x;			/* top left position, */
157     int tl_y;			/* in inch/1200 units */
158     int br_x;			/* bottom right position, */
159     int br_y;			/* in inch/1200 units */
160 
161     int brightness;
162     int threshold;
163     int contrast;
164     int composition;
165     int bitsperpixel;		/* at the scanner interface */
166     int halftone;
167     int rif;
168     int bitorder;
169     int compress_type;
170     int compress_arg;
171     int vendor_id_code;
172     int outline;
173     int emphasis;
174     int auto_sep;
175     int mirroring;
176     int var_rate_dyn_thresh;
177     int white_level_follow;
178     int subwindow_list;
179     int paper_size;
180     int paper_width_X;
181     int paper_length_Y;
182 /***** end of "set window" terms *****/
183 
184     /* buffer used for scsi-transfer */
185     unsigned char *buffer;
186     unsigned int row_bufsize;
187 
188   };
189 
190 /* ------------------------------------------------------------------------- */
191 
192 #define length_quant SANE_UNFIX(SANE_FIX(MM_PER_INCH / 1200.0))
193 #define mmToIlu(mm) ((mm) / length_quant)
194 #define iluToMm(ilu) ((ilu) * length_quant)
195 #define SP15C_CONFIG_FILE "sp15c.conf"
196 
197 /* ------------------------------------------------------------------------- */
198 
199 SANE_Status
200 sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize);
201 
202 SANE_Status
203 sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only);
204 
205 SANE_Status
206 sane_open (SANE_String_Const name, SANE_Handle * handle);
207 
208 SANE_Status
209 sane_set_io_mode (SANE_Handle h, SANE_Bool non_blocking);
210 
211 SANE_Status
212 sane_get_select_fd (SANE_Handle h, SANE_Int * fdp);
213 
214 const SANE_Option_Descriptor *
215   sane_get_option_descriptor (SANE_Handle handle, SANE_Int option);
216 
217 SANE_Status
218 sane_control_option (SANE_Handle handle, SANE_Int option,
219 		     SANE_Action action, void *val, SANE_Int * info);
220 
221 SANE_Status
222 sane_start (SANE_Handle handle);
223 
224 SANE_Status
225 sane_get_parameters (SANE_Handle handle, SANE_Parameters * params);
226 
227 SANE_Status
228 sane_read (SANE_Handle handle, SANE_Byte * buf,
229 	   SANE_Int max_len, SANE_Int * len);
230 
231 void
232   sane_cancel (SANE_Handle h);
233 
234 void
235   sane_close (SANE_Handle h);
236 
237 void
238   sane_exit (void);
239 
240 /* ------------------------------------------------------------------------- */
241 
242 static SANE_Status
243   attach_scanner (const char *devicename, struct sp15c **devp);
244 
245 static SANE_Status
246   sense_handler (int scsi_fd, u_char * result, void *arg);
247 
248 static int
249   request_sense_parse (u_char * sensed_data);
250 
251 static SANE_Status
252   sp15c_identify_scanner (struct sp15c *s);
253 
254 static SANE_Status
255   sp15c_do_inquiry (struct sp15c *s);
256 
257 static SANE_Status
258   do_scsi_cmd (int fd, unsigned char *cmd, int cmd_len, unsigned char *out, size_t out_len);
259 
260 static void
261   hexdump (int level, char *comment, unsigned char *p, int l);
262 
263 static SANE_Status
264   init_options (struct sp15c *scanner);
265 
266 static int
267   sp15c_check_values (struct sp15c *s);
268 
269 static int
270   sp15c_grab_scanner (struct sp15c *s);
271 
272 static int
273   sp15c_free_scanner (struct sp15c *s);
274 
275 static int
276   wait_scanner (struct sp15c *s);
277 
278 static int __sane_unused__
279   sp15c_object_position (struct sp15c *s);
280 
281 static SANE_Status
282   do_cancel (struct sp15c *scanner);
283 
284 static void
285   swap_res (struct sp15c *s);
286 
287 static int __sane_unused__
288   sp15c_object_discharge (struct sp15c *s);
289 
290 static int
291   sp15c_set_window_param (struct sp15c *s, int prescan);
292 
293 static size_t
294   max_string_size (const SANE_String_Const strings[]);
295 
296 static int
297   sp15c_start_scan (struct sp15c *s);
298 
299 static int
300   reader_process (void *scanner);
301 
302 static SANE_Status
303   do_eof (struct sp15c *scanner);
304 
305 static int
306   pixels_per_line (struct sp15c *s);
307 
308 static int
309   lines_per_scan (struct sp15c *s);
310 
311 static int
312   bytes_per_line (struct sp15c *s);
313 
314 static void
315   sp15c_trim_rowbufsize (struct sp15c *s);
316 
317 static int
318   sp15c_read_data_block (struct sp15c *s, unsigned int length);
319 
320 static SANE_Status
321   attach_one (const char *name);
322 
323 static void
324   adjust_width (struct sp15c *s, SANE_Int * info);
325 
326 static SANE_Status
327   apply_constraints (struct sp15c *s, SANE_Int opt,
328 		     SANE_Int * target, SANE_Word * info);
329 
330 static int
331   sp15c_media_check (struct sp15c *s);
332 
333 #endif /* SP15C_H */
334