• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __KVS40XX_H
2 #define __KVS40XX_H
3 
4 /*
5    Copyright (C) 2009, Panasonic Russia Ltd.
6 */
7 /*
8    Panasonic KV-S40xx USB-SCSI scanner driver.
9 */
10 
11 #include "../include/sane/config.h"
12 #include <semaphore.h>
13 #ifdef HAVE_SYS_TYPES_H
14 #include <sys/types.h>
15 #endif
16 
17 #undef  BACKEND_NAME
18 #define BACKEND_NAME kvs40xx
19 
20 #define DBG_ERR  1
21 #define DBG_WARN 2
22 #define DBG_MSG  3
23 #define DBG_INFO 4
24 #define DBG_DBG  5
25 
26 #define PANASONIC_ID 	0x04da
27 #define KV_S4085C 	0x100c
28 #define KV_S4065C 	0x100d
29 #define KV_S7075C 	0x100e
30 
31 #define KV_S4085CL 	(KV_S4085C|0x10000)
32 #define KV_S4085CW 	(KV_S4085C|0x20000)
33 #define KV_S4065CL 	(KV_S4065C|0x10000)
34 #define KV_S4065CW 	(KV_S4065C|0x20000)
35 
36 #define USB	1
37 #define SCSI	2
38 #define BULK_HEADER_SIZE	12
39 #define MAX_READ_DATA_SIZE	(0x10000-0x100)
40 #define BUF_SIZE MAX_READ_DATA_SIZE
41 
42 #define INCORRECT_LENGTH 0xfafafafa
43 
44 typedef unsigned char u8;
45 typedef unsigned u32;
46 typedef unsigned short u16;
47 
48 #define SIDE_FRONT      0x00
49 #define SIDE_BACK       0x80
50 
51 /* options */
52 typedef enum
53 {
54   NUM_OPTS = 0,
55 
56   /* General options */
57   MODE_GROUP,
58   MODE,				/* scanner modes */
59   RESOLUTION,			/* X and Y resolution */
60   SOURCE,
61 
62   DUPLEX,			/* Duplex mode */
63   FEEDER_MODE,			/* Feeder mode, fixed to Continuous */
64   LENGTHCTL,			/* Length control mode */
65   LONG_PAPER,
66   MANUALFEED,			/* Manual feed mode */
67   FEED_TIMEOUT,			/* Feed timeout */
68   DBLFEED,			/* Double feed detection mode */
69   DFEED_SENCE,
70   DFSTOP,
71   DFEED_L,
72   DFEED_C,
73   DFEED_R,
74   STAPELED_DOC,			/* Detect stapled document */
75   FIT_TO_PAGE,			/* Scanner shrinks image to fit scanned page */
76 
77   /* Geometry group */
78   GEOMETRY_GROUP,
79   PAPER_SIZE,			/* Paper size */
80   LANDSCAPE,			/* true if landscape */
81   TL_X,				/* upper left X */
82   TL_Y,				/* upper left Y */
83   BR_X,				/* bottom right X */
84   BR_Y,				/* bottom right Y */
85 
86   ADVANCED_GROUP,
87   BRIGHTNESS,			/* Brightness */
88   CONTRAST,			/* Contrast */
89   THRESHOLD,			/* Binary threshold */
90   AUTOMATIC_THRESHOLD,
91   WHITE_LEVEL,
92   NOISE_REDUCTION,
93   INVERSE,			/* Monochrome reversing */
94   IMAGE_EMPHASIS,		/* Image emphasis */
95   GAMMA_CORRECTION,		/* Gamma correction */
96   LAMP,				/* Lamp -- color drop out */
97   RED_CHROMA,
98   BLUE_CHROMA,
99   HALFTONE_PATTERN,		/* Halftone pattern */
100   COMPRESSION,			/* JPEG Compression */
101   COMPRESSION_PAR,		/* Compression parameter */
102   DESKEW,
103   STOP_SKEW,
104   CROP,
105   MIRROR,
106   BTMPOS,
107   TOPPOS,
108 
109   /* must come last: */
110   NUM_OPTIONS
111 } KV_OPTION;
112 
113 
114 struct buf
115 {
116   u8 **buf;
117   volatile int head;
118   volatile int tail;
119   volatile unsigned size;
120   volatile int sem;
121   volatile SANE_Status st;
122   pthread_mutex_t mu;
123   pthread_cond_t cond;
124 };
125 
126 struct scanner
127 {
128   char name[128];
129   unsigned id;
130   volatile int scanning;
131   int page;
132   int side;
133   int bus;
134   SANE_Int file;
135   SANE_Option_Descriptor opt[NUM_OPTIONS];
136   Option_Value val[NUM_OPTIONS];
137   SANE_Parameters params;
138   u8 *buffer;
139   struct buf buf[2];
140   u8 *data;
141   unsigned side_size;
142   unsigned read;
143   pthread_t thread;
144 };
145 
146 struct window
147 {
148   u8 reserved[6];
149   u8 window_descriptor_block_length[2];
150 
151   u8 window_identifier;
152   u8 reserved2;
153   u8 x_resolution[2];
154   u8 y_resolution[2];
155   u8 upper_left_x[4];
156   u8 upper_left_y[4];
157   u8 width[4];
158   u8 length[4];
159   u8 brightness;
160   u8 threshold;
161   u8 contrast;
162   u8 image_composition;
163   u8 bit_per_pixel;
164   u8 halftone_pattern[2];
165   u8 rif_padding;		/*RIF*/
166   u8 bit_ordering[2];
167   u8 compression_type;
168   u8 compression_argument;
169   u8 reserved4[6];
170 
171   u8 vendor_unique_identifier;
172   u8 nobuf_fstspeed_dfstop;
173   u8 mirror_image;
174   u8 image_emphasis;
175   u8 gamma_correction;
176   u8 mcd_lamp_dfeed_sens;
177   u8 reserved5;			/*rmoir*/
178   u8 document_size;
179   u8 document_width[4];
180   u8 document_length[4];
181   u8 ahead_deskew_dfeed_scan_area_fspeed_rshad;
182   u8 continuous_scanning_pages;
183   u8 automatic_threshold_mode;
184   u8 automatic_separation_mode;
185   u8 standard_white_level_mode;
186   u8 b_wnr_noise_reduction;
187   u8 mfeed_toppos_btmpos_dsepa_hsepa_dcont_rstkr;
188   u8 stop_mode;
189   u8 red_chroma;
190   u8 blue_chroma;
191 };
192 
193 struct support_info
194 {
195   /*TODO: */
196   unsigned char data[32];
197 };
198 
199 void kvs40xx_init_options (struct scanner *);
200 SANE_Status kvs40xx_test_unit_ready (struct scanner *s);
201 SANE_Status kvs40xx_set_timeout (struct scanner *s, int timeout);
202 void kvs40xx_init_window (struct scanner *s, struct window *wnd, int wnd_id);
203 SANE_Status kvs40xx_set_window (struct scanner *s, int wnd_id);
204 SANE_Status kvs40xx_reset_window (struct scanner *s);
205 SANE_Status kvs40xx_read_picture_element (struct scanner *s, unsigned side,
206 					  SANE_Parameters * p);
207 SANE_Status read_support_info (struct scanner *s, struct support_info *inf);
208 SANE_Status kvs40xx_read_image_data (struct scanner *s, unsigned page,
209 				     unsigned side, void *buf,
210 				     unsigned max_size, unsigned *size);
211 SANE_Status kvs40xx_document_exist (struct scanner *s);
212 SANE_Status get_buffer_status (struct scanner *s, unsigned *data_avalible);
213 SANE_Status kvs40xx_scan (struct scanner *s);
214 SANE_Status kvs40xx_sense_handler (int fd, u_char * sense_buffer, void *arg);
215 SANE_Status stop_adf (struct scanner *s);
216 SANE_Status hopper_down (struct scanner *s);
217 SANE_Status inquiry (struct scanner *s, char *id);
218 
219 static inline u16
swap_bytes16(u16 x)220 swap_bytes16 (u16 x)
221 {
222   return x << 8 | x >> 8;
223 }
224 static inline u32
swap_bytes32(u32 x)225 swap_bytes32 (u32 x)
226 {
227   return x << 24 | x >> 24 |
228     (x & (u32) 0x0000ff00UL) << 8 | (x & (u32) 0x00ff0000UL) >> 8;
229 }
230 
231 static inline void
copy16(u8 * p,u16 x)232 copy16 (u8 * p, u16 x)
233 {
234   memcpy (p, (u8 *) &x, sizeof (x));
235 }
236 
237 static inline void
copy32(u8 * p,u32 x)238 copy32 (u8 * p, u32 x)
239 {
240   memcpy (p, (u8 *) &x, sizeof (x));
241 }
242 
243 #if WORDS_BIGENDIAN
244 static inline void
set24(u8 * p,u32 x)245 set24 (u8 * p, u32 x)
246 {
247   p[2] = x >> 16;
248   p[1] = x >> 8;
249   p[0] = x >> 0;
250 }
251 
252 #define cpu2be16(x) (x)
253 #define cpu2be32(x) (x)
254 #define cpu2le16(x) swap_bytes16(x)
255 #define cpu2le32(x) swap_bytes32(x)
256 #define le2cpu16(x) swap_bytes16(x)
257 #define le2cpu32(x) swap_bytes32(x)
258 #define be2cpu16(x) (x)
259 #define be2cpu32(x) (x)
260 #define BIT_ORDERING 0
261 #elif __BYTE_ORDER == __LITTLE_ENDIAN
262 static inline void
set24(u8 * p,u32 x)263 set24 (u8 * p, u32 x)
264 {
265   p[0] = x >> 16;
266   p[1] = x >> 8;
267   p[2] = x >> 0;
268 }
269 
270 #define cpu2le16(x) (x)
271 #define cpu2le32(x) (x)
272 #define cpu2be16(x) swap_bytes16(x)
273 #define cpu2be32(x) swap_bytes32(x)
274 #define le2cpu16(x) (x)
275 #define le2cpu32(x) (x)
276 #define be2cpu16(x) swap_bytes16(x)
277 #define be2cpu32(x) swap_bytes32(x)
278 #define BIT_ORDERING 1
279 #else
280 #error __BYTE_ORDER not defined
281 #endif
282 
283 
284 static inline u32
get24(u8 * p)285 get24 (u8 * p)
286 {
287   u32 x = (((u32) p[0]) << 16) | (((u32) p[1]) << 8) | (((u32) p[0]) << 0);
288   return x;
289 }
290 #endif /*__KVS40XX_H*/
291