1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 2007 Jeremy Johnson
3    This file is part of a SANE backend for Ricoh IS450
4    and IS420 family of HS2P Scanners using the SCSI controller.
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 #ifndef HS2P_H
43 #define HS2P_H 1
44 
45 #include <sys/types.h>
46 #include "../include/sane/config.h"
47 
48 #include "hs2p-scsi.h"
49 #include "hs2p-saneopts.h"
50 
51 #define HS2P_CONFIG_FILE "hs2p.conf"
52 
53 #define DBG_error0       0
54 #define DBG_error        1
55 #define DBG_sense        2
56 #define DBG_warning      3
57 #define DBG_inquiry      4
58 #define DBG_info         5
59 #define DBG_info2        6
60 #define DBG_proc         7
61 #define DBG_read         8
62 #define DBG_sane_init   10
63 #define DBG_sane_proc   11
64 #define DBG_sane_info   12
65 #define DBG_sane_option 13
66 
67 typedef struct
68 {
69   const char *mfg;
70   const char *model;
71 } HS2P_HWEntry;
72 
73 enum CONNECTION_TYPES
74 { CONNECTION_SCSI = 0, CONNECTION_USB };
75 
76 enum media
77 { FLATBED = 0x00, SIMPLEX, DUPLEX };
78 
79 
80 typedef struct data
81 {
82   size_t bufsize;
83   /* 00H IMAGE */
84   /* 01H RESERVED */
85   /* 02H Halftone Mask  */
86   SANE_Byte gamma[256];		/* 03H Gamma Function */
87   /* 04H - 7FH Reserved */
88   SANE_Byte endorser[19];	/* 80H Endorser */
89   SANE_Byte size;		/* 81H startpos(4bits) + width(4bits) */
90   /* 82H Reserved */
91   /* 83H Reserved (Vendor Unique) */
92   SANE_Byte nlines[5];		/* 84H Page Length */
93   MAINTENANCE_DATA maintenance;	/* 85H */
94   SANE_Byte adf_status;		/* 86H */
95   /* 87H Reserved (Skew Data) */
96   /* 88H-91H Reserved (Vendor Unique) */
97   /* 92H Reserved (Scanner Extension I/O Access) */
98   /* 93H Reserved (Vendor Unique) */
99   /* 94H-FFH Reserved (Vendor Unique) */
100 } HS2P_DATA;
101 
102 typedef struct
103 {
104   SANE_Range xres_range;
105   SANE_Range yres_range;
106   SANE_Range x_range;
107   SANE_Range y_range;
108 
109   SANE_Int window_width;
110   SANE_Int window_height;
111 
112   SANE_Range brightness_range;
113   SANE_Range contrast_range;
114   SANE_Range threshold_range;
115 
116   char inquiry_data[256];
117 
118   SANE_Byte max_win_sections;	/* Number of supported window subsections
119 				   IS450 supports max of 4 sections
120 				   IS420 supports max of 6 sections
121 				 */
122 
123   /* Defaults */
124   SANE_Int default_res;
125   SANE_Int default_xres;
126   SANE_Int default_yres;
127   SANE_Int default_imagecomposition;	/* [lineart], halftone, grayscale, color */
128   SANE_Int default_media;	/* [flatbed], simplex, duplex */
129   SANE_Int default_paper_size;	/* [letter], legal, ledger, ... */
130   SANE_Int default_brightness;
131   SANE_Int default_contrast;
132   SANE_Int default_gamma;	/* Normal, Soft, Sharp, Linear, User */
133   SANE_Bool default_adf;
134   SANE_Bool default_duplex;
135   /*
136      SANE_Bool default_border;
137      SANE_Bool default_batch;
138      SANE_Bool default_deskew;
139      SANE_Bool default_check_adf;
140      SANE_Int  default_timeout_adf;
141      SANE_Int  default_timeout_manual;
142      SANE_Bool default_control_panel;
143    */
144 
145   /* Mode Page Parameters */
146   MP_CXN cxn;			/* hdr + Connection Parameters */
147 
148   SANE_Int bmu;
149   SANE_Int mud;
150   SANE_Int white_balance;	/* 00H Relative, 01H Absolute; power on default is relative */
151   /* Lamp Timer not supported */
152   SANE_Int adf_control;		/* 00H None, 01H Book, 01H Simplex, 02H Duplex */
153   SANE_Int adf_mode_control;	/* bit2: prefeed mode invalid: "0" : valid "1" */
154   /* Medium Wait Timer not supported */
155   SANE_Int endorser_control;	/* Default Off when power on */
156   SANE_Char endorser_string[20];
157   SANE_Bool scan_wait_mode;	/* wait for operator panel start button to be pressed */
158   SANE_Bool service_mode;	/* power on default self_diagnostics 00H; 01H optical_adjustment */
159 
160   /* standard information: EVPD bit is 0 */
161   SANE_Byte devtype;		/* devtype[6]="scanner" */
162   SANE_Char vendor[9];		/* model name 8+1 */
163   SANE_Char product[17];	/* product name 16+1 */
164   SANE_Char revision[5];	/* revision 4+1 */
165 
166   /* VPD information: EVPD bit is 1, Page Code=C0H */
167   /* adf_id: 0: No ADF
168    *         1: Single-sided ADF
169    *         2: Double-sided ADF
170    *         3: ARDF (Reverse double-sided ADF)
171    *         4: Reserved
172    */
173 
174   SANE_Bool hasADF;		/* If YES; can either be one of Simplex,Duplex,ARDF */
175   SANE_Bool hasSimplex;
176   SANE_Bool hasDuplex;
177   SANE_Bool hasARDF;
178 
179   SANE_Bool hasEndorser;
180 
181   SANE_Bool hasIPU;
182   SANE_Bool hasXBD;
183 
184   /* VPD Image Composition */
185   SANE_Bool supports_lineart;
186   SANE_Bool supports_dithering;
187   SANE_Bool supports_errordiffusion;
188   SANE_Bool supports_color;
189   SANE_Bool supports_4bitgray;
190   SANE_Bool supports_8bitgray;
191 
192   /* VPD Image Data Processing ACE (supported for IS420) */
193   SANE_Bool supports_whiteframing;
194   SANE_Bool supports_blackframing;
195   SANE_Bool supports_edgeextraction;
196   SANE_Bool supports_noiseremoval;	/* supported for IS450 if IPU installed */
197   SANE_Bool supports_smoothing;	/* supported for IS450 if IPU installed */
198   SANE_Bool supports_linebolding;
199 
200   /* VPD Compression (not supported for IS450) */
201   SANE_Bool supports_MH;
202   SANE_Bool supports_MR;
203   SANE_Bool supports_MMR;
204   SANE_Bool supports_MHB;
205 
206   /* VPD Marker Recognition (not supported for IS450) */
207   SANE_Bool supports_markerrecognition;
208 
209   /* VPD Size Recognition (supported for IS450 if IPU installed) */
210   SANE_Bool supports_sizerecognition;
211 
212   /* VPD X Maximum Output Pixel: IS450:4960   IS420:4880 */
213   SANE_Int xmaxoutputpixels;
214 
215   /* jis information VPD IDENTIFIER Page Code F0H */
216   SANE_Int resBasicX;		/* basic X resolution */
217   SANE_Int resBasicY;		/* basic Y resolution */
218   SANE_Int resXstep;		/* resolution step in main scan direction */
219   SANE_Int resYstep;		/* resolution step in sub scan direction */
220   SANE_Int resMaxX;		/* maximum X resolution */
221   SANE_Int resMaxY;		/* maximum Y resolution */
222   SANE_Int resMinX;		/* minimum X resolution */
223   SANE_Int resMinY;		/* minimum Y resolution */
224   SANE_Int resStdList[16 + 1];	/* list of available standard resolutions (first slot is the length) */
225   SANE_Int winWidth;		/* length of window (in BasicX res DPI) */
226   SANE_Int winHeight;		/* height of window (in BasicY res DPI) */
227   /* jis.functions duplicates vpd.imagecomposition lineart/dither/grayscale */
228   SANE_Bool overflow_support;
229   SANE_Bool lineart_support;
230   SANE_Bool dither_support;
231   SANE_Bool grayscale_support;
232 
233 } HS2P_Info;
234 
235 typedef struct HS2P_Device
236 {
237   struct HS2P_Device *next;
238   /*
239    * struct with pointers to device/vendor/model names, and a type value
240    * used to inform sane frontend about the device
241    */
242   SANE_Device sane;
243   HS2P_Info info;
244   SENSE_DATA sense_data;
245 } HS2P_Device;
246 
247 #define GAMMA_LENGTH 256
248 typedef struct HS2P_Scanner
249 {
250   /* all the state needed to define a scan request: */
251   struct HS2P_Scanner *next;	/* linked list for housekeeping */
252   int fd;			/* SCSI filedescriptor */
253 
254   /* --------------------------------------------------------------------- */
255   /* immutable values which are set during reading of config file.         */
256   int buffer_size;		/* for sanei_open */
257   int connection;		/* hardware interface type */
258 
259 
260   /* SANE option descriptors and values */
261   SANE_Option_Descriptor opt[NUM_OPTIONS];
262   Option_Value val[NUM_OPTIONS];
263   SANE_Parameters params;	/* SANE image parameters */
264   /* additional values that don't fit into Option_Value representation */
265   SANE_Word gamma_table[GAMMA_LENGTH];	/* Custom Gray Gamma Table */
266 
267   /* state information - not options */
268 
269   /* scanner dependent/low-level state: */
270   HS2P_Device *hw;
271 
272   SANE_Int bmu;			/* Basic Measurement Unit       */
273   SANE_Int mud;			/* Measurement Unit Divisor     */
274   SANE_Byte image_composition;	/* LINEART, HALFTONE, GRAYSCALE */
275   SANE_Byte bpp;		/* 1,4,6,or 8 Bits Per Pixel    */
276 
277 
278   u_long InvalidBytes;
279   size_t bytes_to_read;
280   SANE_Bool cancelled;
281   /*SANE_Bool backpage; */
282   SANE_Bool scanning;
283   SANE_Bool another_side;
284   SANE_Bool EOM;
285 
286   HS2P_DATA data;
287 } HS2P_Scanner;
288 
289 static const SANE_Range u8_range = {
290   0,				/* minimum */
291   255,				/* maximum */
292   0				/* quantization */
293 };
294 static const SANE_Range u16_range = {
295   0,				/* minimum */
296   65535,			/* maximum */
297   0				/* quantization */
298 };
299 
300 #define SM_LINEART        SANE_VALUE_SCAN_MODE_LINEART
301 #define SM_HALFTONE       SANE_VALUE_SCAN_MODE_HALFTONE
302 #define SM_DITHER         "Dither"
303 #define SM_ERRORDIFFUSION "Error Diffusion"
304 #define SM_COLOR          SANE_VALUE_SCAN_MODE_COLOR
305 #define SM_4BITGRAY       "4-Bit Gray"
306 #define SM_6BITGRAY       "6-Bit Gray"
307 #define SM_8BITGRAY       "8-Bit Gray"
308 static SANE_String scan_mode_list[9];
309 enum
310 { FB, ADF };
311 static SANE_String_Const scan_source_list[] = {
312   "FB",				/* Flatbed */
313   "ADF",			/* Automatic Document Feeder */
314   NULL
315 };
316 static SANE_String compression_list[6];	/* "none", "g31d MH", "g32d MR", "g42d MMR", "MH byte boundary", NULL} */
317 
318 typedef struct
319 {
320   SANE_String name;
321   double width, length;		/* paper dimensions in mm */
322 } HS2P_Paper;
323 /* list of support paper sizes */
324 /* 'custom' MUST be item 0; otherwise a width or length of 0 indicates
325  * the maximum value supported by the scanner
326  */
327 static const HS2P_Paper paper_sizes[] = {	/* Name, Width, Height in mm */
328   {"Custom", 0.0, 0.0},
329   {"Letter", 215.9, 279.4},
330   {"Legal", 215.9, 355.6},
331   {"Ledger", 279.4, 431.8},
332   {"A3", 297, 420},
333   {"A4", 210, 297},
334   {"A4R", 297, 210},
335   {"A5", 148.5, 210},
336   {"A5R", 210, 148.5},
337   {"A6", 105, 148.5},
338   {"B4", 250, 353},
339   {"B5", 182, 257},
340   {"Full", 0.0, 0.0},
341 };
342 
343 #define PORTRAIT "Portrait"
344 #define LANDSCAPE "Landscape"
345 static SANE_String_Const orientation_list[] = {
346   PORTRAIT,
347   LANDSCAPE,
348   NULL				/* sentinel */
349 };
350 
351 /* MUST be kept in sync with paper_sizes */
352 static SANE_String_Const paper_list[] = {
353   "Custom",
354   "Letter",
355   "Legal",
356   "Ledger",
357   "A3",
358   "A4", "A4R",
359   "A5", "A5R",
360   "A6",
361   "B4",
362   "B5",
363   "Full",
364   NULL				/* (not the same as "") sentinel */
365 };
366 
367 #if 0
368 static /* inline */ int _is_host_little_endian (void);
369 static /* inline */ int
370 _is_host_little_endian ()
371 {
372   SANE_Int val = 255;
373   unsigned char *firstbyte = (unsigned char *) &val;
374 
375   return (*firstbyte == 255) ? SANE_TRUE : SANE_FALSE;
376 }
377 #endif
378 
379 static /* inline */ void
_lto2b(u_long val,SANE_Byte * bytes)380 _lto2b (u_long val, SANE_Byte * bytes)
381 {
382   bytes[0] = (val >> 8) & 0xff;
383   bytes[1] = val & 0xff;
384 }
385 
386 static /* inline */ void
_lto3b(u_long val,SANE_Byte * bytes)387 _lto3b (u_long val, SANE_Byte * bytes)
388 {
389   bytes[0] = (val >> 16) & 0xff;
390   bytes[1] = (val >> 8) & 0xff;
391   bytes[2] = val & 0xff;
392 }
393 
394 static /* inline */ void
_lto4b(u_long val,SANE_Byte * bytes)395 _lto4b (u_long val, SANE_Byte * bytes)
396 {
397   bytes[0] = (val >> 24) & 0xff;
398   bytes[1] = (val >> 16) & 0xff;
399   bytes[2] = (val >> 8) & 0xff;
400   bytes[3] = val & 0xff;
401 }
402 
403 static /* inline */ u_long
_2btol(SANE_Byte * bytes)404 _2btol (SANE_Byte * bytes)
405 {
406   u_long rv;
407 
408   rv = (bytes[0] << 8) | bytes[1];
409 
410   return rv;
411 }
412 
413 static /* inline */ u_long
_4btol(SANE_Byte * bytes)414 _4btol (SANE_Byte * bytes)
415 {
416   u_long rv;
417 
418   rv = (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
419 
420   return rv;
421 }
422 
423 /*
424 static inline SANE_Int
425 _2btol(SANE_Byte *bytes)
426 {
427   SANE_Int rv;
428 
429   rv = (bytes[0] << 8) | bytes[1];
430   return (rv);
431 }
432 */
433 static inline SANE_Int
_3btol(SANE_Byte * bytes)434 _3btol (SANE_Byte * bytes)
435 {
436   SANE_Int rv;
437 
438   rv = (bytes[0] << 16) | (bytes[1] << 8) | bytes[2];
439   return (rv);
440 }
441 
442 /*
443 static inline SANE_Int
444 _4btol(SANE_Byte *bytes)
445 {
446         SANE_Int rv;
447 
448         rv = (bytes[0] << 24) |
449              (bytes[1] << 16) |
450              (bytes[2] << 8) |
451              bytes[3];
452         return (rv);
453 }
454 */
455 enum adf_ret_bytes
456 { ADF_SELECTION = 2, ADF_MODE_CONTROL, MEDIUM_WAIT_TIMER };
457 
458 #define get_paddingtype_id(s)  (get_list_index( paddingtype_list, (char *)(s) ))
459 #define get_paddingtype_val(i) (paddingtype[ get_paddingtype_id( (i) ) ].val)
460 #define get_paddingtype_strndx(v) (get_val_id_strndx(&paddingtype[0], NELEMS(paddingtype), (v)))
461 
462 #define get_halftone_code_id(s)  (get_list_index( halftone_code, (char *)(s) ))
463 #define get_halftone_code_val(i) (halftone[get_halftone_code_id( (i) ) ].val)
464 
465 #define get_halftone_pattern_id(s)  (get_list_index( halftone_pattern_list, (char *)(s) ))
466 #define get_halftone_pattern_val(i) (halftone[get_halftone_pattern_id( (i) ) ].val)
467 
468 #define get_auto_binarization_id(s)  (get_list_index( auto_binarization_list, (char *)(s) ))
469 #define get_auto_binarization_val(i) (auto_binarization[ get_auto_binarization_id( (i) ) ].val)
470 
471 #define get_auto_separation_id(s)  (get_list_index( auto_separation_list, (char *)(s) ))
472 #define get_auto_separation_val(i) (auto_separation[ get_auto_separation_id( (i) ) ].val)
473 
474 #define get_noisematrix_id(s)    (get_list_index( noisematrix_list, (char *)(s) ))
475 #define get_noisematrix_val(i)   (noisematrix[ get_noisematrix_id( (i) ) ].val)
476 
477 #define get_grayfilter_id(s)    (get_list_index( grayfilter_list, (char *)(s) ))
478 #define get_grayfilter_val(i)   (grayfilter[ get_grayfilter_id( (i) ) ].val)
479 
480 #define get_paper_id(s)       (get_list_index( paper_list, (char *)(s) ))
481 #define get_compression_id(s) (get_list_index( (const char **)compression_list, (char *)(s) ))
482 #define get_scan_source_id(s) (get_list_index( (const char **)scan_source_list, (char *)(s) ))
483 
484 #define reserve_unit(fd) (unit_cmd((fd),HS2P_SCSI_RESERVE_UNIT))
485 #define release_unit(fd) (unit_cmd((fd),HS2P_SCSI_RELEASE_UNIT))
486 
487 #define GET SANE_TRUE
488 #define SET SANE_FALSE
489 
490 #define get_endorser_control(fd,val)  (endorser_control( (fd), (val), GET ))
491 #define set_endorser_control(fd,val)  (endorser_control( (fd), (val), SET ))
492 
493 #define get_connection_parameters(fd,parm)  (connection_parameters( (fd), (parm), GET ))
494 #define set_connection_parameters(fd,parm)  (connection_parameters( (fd), (parm), SET ))
495 
496 #define get_adf_control(fd, a, b, c)      (adf_control( (fd), GET, (a), (b), (c) ))
497 #define set_adf_control(fd, a, b, c)      (adf_control( (fd), SET, (a), (b), (c) ))
498 
499 #define RELATIVE_WHITE 0x00
500 #define ABSOLUTE_WHITE 0x01
501 #define get_white_balance(fd,val)  (white_balance( (fd), (val), GET ))
502 #define set_white_balance(fd,val)  (white_balance( (fd), (val), SET ))
503 
504 #define get_scan_wait_mode(fd)      (scan_wait_mode( (fd),     0, GET ))
505 #define set_scan_wait_mode(fd,val)  (scan_wait_mode( (fd), (val), SET ))
506 
507 #define get_service_mode(fd)      (service_mode( (fd),     0, GET ))
508 #define set_service_mode(fd,val)  (service_mode( (fd), (val), SET ))
509 
510 #define isset_ILI(sd) ( ((sd).sense_key & 0x20) != 0)
511 #define isset_EOM(sd) ( ((sd).sense_key & 0x40) != 0)
512 
513 
514 #endif /* HS2P_H */
515