1 #ifndef EPJITSU_H
2 #define EPJITSU_H
3 
4 /*
5  * Part of SANE - Scanner Access Now Easy.
6  * Please see opening comment in epjitsu.c
7  */
8 
9 /* -------------------------------------------------------------------------
10  * This option list has to contain all options for all scanners supported by
11  * this driver. If a certain scanner cannot handle a certain option, there's
12  * still the possibility to say so, later.
13  */
14 enum scanner_Option
15 {
16   OPT_NUM_OPTS = 0,
17 
18   OPT_MODE_GROUP,
19   OPT_SOURCE,   /*adffront/adfback/adfduplex/fb*/
20   OPT_MODE,     /*mono/gray/color*/
21   OPT_RES,
22 
23   OPT_GEOMETRY_GROUP,
24   OPT_TL_X,
25   OPT_TL_Y,
26   OPT_BR_X,
27   OPT_BR_Y,
28   OPT_PAGE_WIDTH,
29   OPT_PAGE_HEIGHT,
30 
31   OPT_ENHANCEMENT_GROUP,
32   OPT_BRIGHTNESS,
33   OPT_CONTRAST,
34   OPT_GAMMA,
35   OPT_THRESHOLD,
36   OPT_THRESHOLD_CURVE,
37 
38   OPT_SENSOR_GROUP,
39   OPT_SCAN_SW,
40   OPT_HOPPER,
41   OPT_TOP,
42   OPT_ADF_OPEN,
43   OPT_SLEEP,
44 
45   /* must come last: */
46   NUM_OPTIONS
47 };
48 
49 #define FIRMWARE_LENGTH 0x10000
50 #define MAX_IMG_PASS 0x10000
51 #define MAX_IMG_BLOCK 0x80000
52 
53 struct image {
54   int width_pix;
55   int width_bytes;
56   int height;
57   int pages;
58   int mode;
59   int x_res;
60   int y_res;
61   int x_start_offset;
62   int x_offset_bytes;
63   int y_skip_offset;
64   unsigned char * buffer;
65 };
66 
67 struct transfer {
68   int plane_width;   /* in RGB pixels */
69   int plane_stride;  /* in bytes */
70   int line_stride;   /* in bytes */
71 
72   int total_bytes;
73   int rx_bytes;
74   int done;
75   int mode;
76   int x_res;
77   int y_res;
78 
79   unsigned char * raw_data;
80   struct image * image;
81 };
82 
83 struct page {
84   int bytes_total;
85   int bytes_scanned;
86   int bytes_read;
87   int lines_rx;   /* received from scanner */
88   int lines_pass; /* passed thru from scanner to user (might be smaller than tx for 225dpi) */
89   int lines_tx;   /* transmitted to user */
90   int done;
91   struct image *image;
92 };
93 
94 struct scanner
95 {
96   /* --------------------------------------------------------------------- */
97   /* immutable values which are set during init of scanner.                */
98   struct scanner *next;
99 
100   int missing;
101 
102   int model;
103   int usb_power;
104 
105   int has_fb;
106   int has_adf;
107   int has_adf_duplex;
108 
109   int min_res;
110   int max_res;
111 
112   float white_factor[3];
113   int adf_height_padding;
114 
115   /* the scan size in 1/1200th inches, NOT basic_units or sane units */
116   int max_x;
117   int max_y;
118   int min_x;
119   int min_y;
120 
121   /* --------------------------------------------------------------------- */
122   /* immutable values which are set during inquiry probing of the scanner. */
123   SANE_Device sane; /*contains: name, vendor, model, type*/
124 
125   /* --------------------------------------------------------------------- */
126   /* changeable SANE_Option structs provide our interface to frontend.     */
127 
128   /* long array of option structs */
129   SANE_Option_Descriptor opt[NUM_OPTIONS];
130 
131   /* --------------------------------------------------------------------- */
132   /* some options require lists of strings or numbers, we keep them here   */
133   /* instead of in global vars so that they can differ for each scanner    */
134 
135   /*mode group, room for lineart, gray, color, null */
136   SANE_String_Const source_list[5];
137   SANE_String_Const mode_list[4];
138   SANE_Range res_range;
139 
140   /*geometry group*/
141   SANE_Range tl_x_range;
142   SANE_Range tl_y_range;
143   SANE_Range br_x_range;
144   SANE_Range br_y_range;
145   SANE_Range paper_x_range;
146   SANE_Range paper_y_range;
147 
148   /*enhancement group*/
149   SANE_Range brightness_range;
150   SANE_Range contrast_range;
151   SANE_Range gamma_range;
152   SANE_Range threshold_range;
153   SANE_Range threshold_curve_range;
154 
155   /* --------------------------------------------------------------------- */
156   /* changeable vars to hold user input. modified by SANE_Options above    */
157 
158   /*mode group*/
159   int source;         /* adf or fb */
160   int mode;           /* color,lineart,etc */
161   int resolution;     /* dpi */
162 
163   /*geometry group*/
164   /* The desired size of the scan, all in 1/1200 inch */
165   int tl_x;
166   int tl_y;
167   int br_x;
168   int br_y;
169   int page_width;
170   int page_height;
171 
172   /*enhancement group*/
173   int brightness;
174   int contrast;
175   int gamma;
176   int threshold;
177   int threshold_curve;
178 
179   int height;         /* may run out on adf */
180 
181   /* --------------------------------------------------------------------- */
182   /* values which are set by user parameter changes, scanner specific      */
183   unsigned char * setWindowCoarseCal;   /* sent before coarse cal */
184   size_t setWindowCoarseCalLen;
185 
186   unsigned char * setWindowFineCal;   /* sent before fine cal */
187   size_t setWindowFineCalLen;
188 
189   unsigned char * setWindowSendCal;   /* sent before send cal */
190   size_t setWindowSendCalLen;
191 
192   unsigned char * sendCal1Header; /* part of 1b c3 command */
193   size_t sendCal1HeaderLen;
194 
195   unsigned char * sendCal2Header; /* part of 1b c4 command */
196   size_t sendCal2HeaderLen;
197 
198   unsigned char * setWindowScan;  /* sent before scan */
199   size_t setWindowScanLen;
200 
201   /* --------------------------------------------------------------------- */
202   /* values which are set by scanning functions to keep track of pages, etc */
203   int started;
204   int side;
205 
206   /* holds temp buffers for getting 16 lines of cal data */
207   struct transfer cal_image;
208   struct image coarsecal;
209   struct image darkcal;
210   struct image lightcal;
211 
212   /* holds temp buffer for building calibration data */
213   struct transfer cal_data;
214   struct image sendcal;
215 
216   /* scanner transmits more data per line than requested */
217   /* due to padding and/or duplex interlacing */
218   /* the scan struct holds these larger numbers, but image buffer is unused */
219   struct {
220       int done;
221       int mode;
222       int x_res;
223       int y_res;
224       int height;
225       int rx_bytes;
226       int width_bytes;
227       int total_bytes;
228   } fullscan;
229 
230   /* The page structs contain data about the progress as the application reads */
231   /* data from the front/back image buffers via the sane_read() function */
232   struct page pages[2];
233 
234   /* scanner transmits data in blocks, up to 512k */
235   /* but always ends on a scanline. */
236   /* the block struct holds the most recent buffer */
237   struct transfer block_xfr;
238   struct image    block_img;
239 
240   /* temporary buffers used by dynamic threshold code */
241   struct image  dt;
242   unsigned char dt_lut[256];
243 
244   /* final-sized front image, always used */
245   struct image front;
246 
247   /* final-sized back image, only used during duplex/backside */
248   struct image back;
249 
250   /* --------------------------------------------------------------------- */
251   /* values used by the command and data sending function                  */
252   int fd;                       /* The scanner device file descriptor.     */
253 
254   /* --------------------------------------------------------------------- */
255   /* values which are used by the get hardware status command              */
256   time_t last_ghs;
257 
258   int hw_scan_sw;
259   int hw_hopper;
260   int hw_top;
261   int hw_adf_open;
262   int hw_sleep;
263 };
264 
265 #define MODEL_NONE (1<<0)
266 #define MODEL_S300 (1<<1)
267 #define MODEL_FI60F (1<<2)
268 #define MODEL_S1100 (1<<3)
269 #define MODEL_S1300i (1<<4)
270 #define MODEL_FI65F (1<<5)
271 
272 #define USB_COMMAND_TIME   10000
273 #define USB_DATA_TIME      10000
274 
275 #define SIDE_FRONT 0
276 #define SIDE_BACK 1
277 
278 #define SOURCE_FLATBED 0
279 #define SOURCE_ADF_FRONT 1
280 #define SOURCE_ADF_BACK 2
281 #define SOURCE_ADF_DUPLEX 3
282 
283 #define MODE_COLOR 0
284 #define MODE_GRAYSCALE 1
285 #define MODE_LINEART 2
286 
287 #define WINDOW_COARSECAL 0
288 #define WINDOW_FINECAL 1
289 #define WINDOW_SENDCAL 2
290 #define WINDOW_SCAN 3
291 
292 #define EPJITSU_PAPER_INGEST 1
293 #define EPJITSU_PAPER_EJECT 0
294 
295 /* ------------------------------------------------------------------------- */
296 
297 #define MM_PER_UNIT_UNFIX SANE_UNFIX(SANE_FIX(MM_PER_INCH / 1200.0))
298 #define MM_PER_UNIT_FIX SANE_FIX(SANE_UNFIX(SANE_FIX(MM_PER_INCH / 1200.0)))
299 
300 #define SCANNER_UNIT_TO_FIXED_MM(number) SANE_FIX((number) * MM_PER_UNIT_UNFIX)
301 #define FIXED_MM_TO_SCANNER_UNIT(number) SANE_UNFIX(number) / MM_PER_UNIT_UNFIX
302 
303 #define PIX_TO_SCANNER_UNIT(number, dpi) SANE_UNFIX(SANE_FIX((number) * 1200 / dpi ))
304 #define SCANNER_UNIT_TO_PIX(number, dpi) SANE_UNFIX(SANE_FIX((number) * dpi / 1200 ))
305 
306 #define CONFIG_FILE "epjitsu.conf"
307 
308 #ifndef PATH_MAX
309 #  define PATH_MAX 1024
310 #endif
311 
312 /* ------------------------------------------------------------------------- */
313 
314 SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize);
315 
316 SANE_Status sane_get_devices (const SANE_Device *** device_list,
317                               SANE_Bool local_only);
318 
319 SANE_Status sane_open (SANE_String_Const name, SANE_Handle * handle);
320 
321 SANE_Status sane_set_io_mode (SANE_Handle h, SANE_Bool non_blocking);
322 
323 SANE_Status sane_get_select_fd (SANE_Handle h, SANE_Int * fdp);
324 
325 const SANE_Option_Descriptor * sane_get_option_descriptor (SANE_Handle handle,
326                                                           SANE_Int option);
327 
328 SANE_Status sane_control_option (SANE_Handle handle, SANE_Int option,
329                                  SANE_Action action, void *val,
330                                  SANE_Int * info);
331 
332 SANE_Status sane_start (SANE_Handle handle);
333 
334 SANE_Status sane_get_parameters (SANE_Handle handle,
335                                  SANE_Parameters * params);
336 
337 SANE_Status sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
338                        SANE_Int * len);
339 
340 void sane_cancel (SANE_Handle h);
341 
342 void sane_close (SANE_Handle h);
343 
344 void sane_exit (void);
345 
346 /* ------------------------------------------------------------------------- */
347 
348 static SANE_Status attach_one (const char *devicename);
349 static SANE_Status connect_fd (struct scanner *s);
350 static SANE_Status disconnect_fd (struct scanner *s);
351 
352 static SANE_Status
353 do_cmd(struct scanner *s, int shortTime,
354  unsigned char * cmdBuff, size_t cmdLen,
355  unsigned char * outBuff, size_t outLen,
356  unsigned char * inBuff, size_t * inLen
357 );
358 
359 /*
360 static SANE_Status load_calibration (struct scanner *s);
361 static SANE_Status read_from_scanner_gray(struct scanner *s);
362 */
363 
364 /* commands */
365 static SANE_Status load_fw(struct scanner *s);
366 static SANE_Status get_ident(struct scanner *s);
367 
368 static SANE_Status change_params(struct scanner *s);
369 
370 static SANE_Status destroy(struct scanner *s);
371 static SANE_Status teardown_buffers(struct scanner *s);
372 static SANE_Status setup_buffers(struct scanner *s);
373 
374 static SANE_Status object_position(struct scanner *s, int ingest);
375 static SANE_Status six5 (struct scanner *s);
376 static SANE_Status coarsecal(struct scanner *s);
377 static SANE_Status finecal(struct scanner *s);
378 static SANE_Status send_lut(struct scanner *s);
379 static SANE_Status lamp(struct scanner *s, unsigned char set);
380 static SANE_Status set_window(struct scanner *s, int window);
381 static SANE_Status scan(struct scanner *s);
382 
383 static SANE_Status read_from_scanner(struct scanner *s, struct transfer *tp);
384 static SANE_Status descramble_raw_gray(struct scanner *s, struct transfer * tp);
385 static SANE_Status descramble_raw(struct scanner *s, struct transfer * tp);
386 static SANE_Status copy_block_to_page(struct scanner *s, int side);
387 static SANE_Status binarize_line(struct scanner *s, unsigned char *lineOut, int width);
388 
389 static SANE_Status get_hardware_status (struct scanner *s);
390 
391 static SANE_Status load_lut (unsigned char * lut, int in_bits, int out_bits,
392   int out_min, int out_max, int slope, int offset);
393 
394 static int get_page_width (struct scanner *s);
395 static int get_page_height (struct scanner *s);
396 static unsigned char get_stat(struct scanner *s);
397 
398 /* utils */
399 static void update_transfer_totals(struct transfer * t);
400 static void hexdump (int level, char *comment, unsigned char *p, int l);
401 static size_t maxStringSize (const SANE_String_Const strings[]);
402 
403 #endif /* EPJITSU_H */
404