1 #ifndef __KVS20XX_H
2 #define __KVS20XX_H
3 
4 /*
5    Copyright (C) 2008, Panasonic Russia Ltd.
6    Copyright (C) 2010, m. allan noah
7 */
8 /*
9    Panasonic KV-S20xx USB-SCSI scanners.
10 */
11 
12 #include <sys/param.h>
13 
14 #undef  BACKEND_NAME
15 #define BACKEND_NAME kvs20xx
16 
17 #define DBG_ERR  1
18 #define DBG_WARN 2
19 #define DBG_MSG  3
20 #define DBG_INFO 4
21 #define DBG_DBG  5
22 
23 #define PANASONIC_ID 	0x04da
24 #define KV_S2025C 	0xdeadbeef
25 #define KV_S2045C 	0xdeadbeee
26 #define KV_S2026C 	0x1000
27 #define KV_S2046C 	0x1001
28 #define KV_S2048C 	0x1009
29 #define KV_S2028C 	0x100a
30 #define USB	1
31 #define SCSI	2
32 #define MAX_READ_DATA_SIZE 0x10000
33 #define BULK_HEADER_SIZE	12
34 
35 typedef unsigned char u8;
36 typedef unsigned u32;
37 typedef unsigned short u16;
38 
39 #define SIDE_FRONT      0x00
40 #define SIDE_BACK       0x80
41 
42 /* options */
43 typedef enum
44 {
45   NUM_OPTS = 0,
46 
47   /* General options */
48   MODE_GROUP,
49   MODE,				/* scanner modes */
50   RESOLUTION,			/* X and Y resolution */
51 
52   DUPLEX,			/* Duplex mode */
53   FEEDER_MODE,			/* Feeder mode, fixed to Continuous */
54   LENGTHCTL,			/* Length control mode */
55   MANUALFEED,			/* Manual feed mode */
56   FEED_TIMEOUT,			/* Feed timeout */
57   DBLFEED,			/* Double feed detection mode */
58   FIT_TO_PAGE,			/* Scanner shrinks image to fit scanned page */
59 
60   /* Geometry group */
61   GEOMETRY_GROUP,
62   PAPER_SIZE,			/* Paper size */
63   LANDSCAPE,			/* true if landscape */
64   TL_X,				/* upper left X */
65   TL_Y,				/* upper left Y */
66   BR_X,				/* bottom right X */
67   BR_Y,				/* bottom right Y */
68 
69   ADVANCED_GROUP,
70   BRIGHTNESS,			/* Brightness */
71   CONTRAST,			/* Contrast */
72   THRESHOLD,			/* Binary threshold */
73   IMAGE_EMPHASIS,		/* Image emphasis */
74   GAMMA_CORRECTION,		/* Gamma correction */
75   LAMP,				/* Lamp -- color drop out */
76   /* must come last: */
77   NUM_OPTIONS
78 } KV_OPTION;
79 
80 #ifndef SANE_OPTION
81 typedef union
82 {
83   SANE_Bool b;		/**< bool */
84   SANE_Word w;		/**< word */
85   SANE_Word *wa;	/**< word array */
86   SANE_String s;	/**< string */
87 }
88 Option_Value;
89 #define SANE_OPTION 1
90 #endif
91 
92 struct scanner
93 {
94   unsigned id;
95   int scanning;
96   int page;
97   int side;
98   int bus;
99   SANE_Int file;
100   SANE_Option_Descriptor opt[NUM_OPTIONS];
101   Option_Value val[NUM_OPTIONS];
102   SANE_Parameters params;
103   u8 *buffer;
104   u8 *data;
105   unsigned side_size;
106   unsigned read;
107   unsigned dummy_size;
108   unsigned saved_dummy_size;
109 };
110 
111 struct window
112 {
113   u8 reserved[6];
114   u16 window_descriptor_block_length;
115 
116   u8 window_identifier;
117   u8 reserved2;
118   u16 x_resolution;
119   u16 y_resolution;
120   u32 upper_left_x;
121   u32 upper_left_y;
122   u32 width;
123   u32 length;
124   u8 brightness;
125   u8 threshold;
126   u8 contrast;
127   u8 image_composition;
128   u8 bit_per_pixel;
129   u16 halftone_pattern;
130   u8 reserved3;
131   u16 bit_ordering;
132   u8 compression_type;
133   u8 compression_argument;
134   u8 reserved4[6];
135 
136   u8 vendor_unique_identifier;
137   u8 nobuf_fstspeed_dfstop;
138   u8 mirror_image;
139   u8 image_emphasis;
140   u8 gamma_correction;
141   u8 mcd_lamp_dfeed_sens;
142   u8 reserved5;
143   u8 document_size;
144   u32 document_width;
145   u32 document_length;
146   u8 ahead_deskew_dfeed_scan_area_fspeed_rshad;
147   u8 continuous_scanning_pages;
148   u8 automatic_threshold_mode;
149   u8 automatic_separation_mode;
150   u8 standard_white_level_mode;
151   u8 b_wnr_noise_reduction;
152   u8 mfeed_toppos_btmpos_dsepa_hsepa_dcont_rstkr;
153   u8 stop_mode;
154 } __attribute__((__packed__));
155 
156 void kvs20xx_init_options (struct scanner *);
157 void kvs20xx_init_window (struct scanner *s, struct window *wnd, int wnd_id);
158 
159 static inline u16
swap_bytes16(u16 x)160 swap_bytes16 (u16 x)
161 {
162   return x << 8 | x >> 8;
163 }
164 static inline u32
swap_bytes32(u32 x)165 swap_bytes32 (u32 x)
166 {
167   return x << 24 | x >> 24 |
168     (x & (u32) 0x0000ff00UL) << 8 | (x & (u32) 0x00ff0000UL) >> 8;
169 }
170 
171 static inline void
copy16(u8 * p,u16 x)172 copy16 (u8 * p, u16 x)
173 {
174   memcpy (p, (u8 *) &x, sizeof (x));
175 }
176 
177 #if __BYTE_ORDER == __BIG_ENDIAN
178 static inline void
set24(u8 * p,u32 x)179 set24 (u8 * p, u32 x)
180 {
181   p[2] = x >> 16;
182   p[1] = x >> 8;
183   p[0] = x >> 0;
184 }
185 
186 #define cpu2be16(x) (x)
187 #define cpu2be32(x) (x)
188 #define cpu2le16(x) swap_bytes16(x)
189 #define cpu2le32(x) swap_bytes32(x)
190 #define le2cpu16(x) swap_bytes16(x)
191 #define le2cpu32(x) swap_bytes32(x)
192 #define be2cpu16(x) (x)
193 #define be2cpu32(x) (x)
194 #define BIT_ORDERING 0
195 #elif __BYTE_ORDER == __LITTLE_ENDIAN
196 static inline void
set24(u8 * p,u32 x)197 set24 (u8 * p, u32 x)
198 {
199   p[0] = x >> 16;
200   p[1] = x >> 8;
201   p[2] = x >> 0;
202 }
203 
204 #define cpu2le16(x) (x)
205 #define cpu2le32(x) (x)
206 #define cpu2be16(x) swap_bytes16(x)
207 #define cpu2be32(x) swap_bytes32(x)
208 #define le2cpu16(x) (x)
209 #define le2cpu32(x) (x)
210 #define be2cpu16(x) swap_bytes16(x)
211 #define be2cpu32(x) swap_bytes32(x)
212 #define BIT_ORDERING 1
213 #else
214 #error __BYTE_ORDER not defined
215 #endif
216 
217 #endif /*__KVS20XX_H*/
218