1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2000 Mustek.
4    Originally maintained by Tom Wang <tom.wang@mustek.com.tw>
5 
6    Copyright (C) 2001, 2002 by Henning Meier-Geinitz.
7 
8    This file is part of the SANE package.
9 
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of the
13    License, or (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <https://www.gnu.org/licenses/>.
22 
23    As a special exception, the authors of SANE give permission for
24    additional uses of the libraries contained in this release of SANE.
25 
26    The exception is that, if you link a SANE library with other files
27    to produce an executable, this does not by itself cause the
28    resulting executable to be covered by the GNU General Public
29    License.  Your use of that executable is in no way restricted on
30    account of linking the SANE library code into it.
31 
32    This exception does not, however, invalidate any other reasons why
33    the executable file might be covered by the GNU General Public
34    License.
35 
36    If you submit changes to SANE to the maintainers to be included in
37    a subsequent release, you agree by submitting the changes that
38    those changes may be distributed with this exception intact.
39 
40    If you write modifications of your own for SANE, it is your choice
41    whether to permit this exception to apply to your modifications.
42    If you do not wish that, delete this exception notice.
43 
44    This file implements a SANE backend for Mustek 1200UB and similar
45    USB flatbed scanners.  */
46 
47 #ifndef mustek_usb_low_h
48 #define mustek_usb_low_h
49 
50 #include "../include/sane/sane.h"
51 
52 
53 /* ---------------------------------- macros ------------------------------ */
54 
55 
56 /* calculate the minimum/maximum values */
57 #if defined(MIN)
58 #undef MIN
59 #endif
60 #if defined(MAX)
61 #undef MAX
62 #endif
63 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
64 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
65 /* return the lower/upper 8 bits of a 16 bit word */
66 #define HIBYTE(w) ((SANE_Byte)(((SANE_Word)(w) >> 8) & 0xFF))
67 #define LOBYTE(w) ((SANE_Byte)(w))
68 /* RIE: return if error */
69 #define RIE(function) do {status = function; if (status != SANE_STATUS_GOOD) \
70                         return status;} while (SANE_FALSE)
71 
72 
73 /* ---------------------------------- types ------------------------------- */
74 
75 
76 typedef enum Mustek_Type
77 {
78   MT_UNKNOWN = 0,
79   MT_1200USB,
80   MT_1200UB,
81   MT_1200CU,
82   MT_1200CU_PLUS,
83   MT_600CU,
84   MT_600USB
85 }
86 Mustek_Type;
87 
88 typedef enum Sensor_Type
89 {
90   ST_NONE = 0,
91   ST_INI = 1,
92   ST_INI_DARK = 2,
93   ST_CANON300 = 3,
94   ST_CANON600 = 4,
95   ST_TOSHIBA600 = 5,
96   ST_CANON300600 = 6,
97   ST_NEC600 = 7
98 }
99 Sensor_Type;
100 
101 typedef enum Motor_Type
102 {
103   MT_NONE = 0,
104   MT_600 = 1,
105   MT_1200 = 2
106 }
107 Motor_Type;
108 
109 struct ma1017;
110 
111 typedef struct ma1017
112 {
113   int fd;
114 
115   SANE_Bool is_opened;
116   SANE_Bool is_rowing;
117 
118   /* A2 */
119   SANE_Byte append;
120   SANE_Byte test_sram;
121   SANE_Byte fix_pattern;
122   /* A4 */
123   SANE_Byte select;
124   SANE_Byte frontend;
125   /* A6 */
126   SANE_Byte rgb_sel_pin;
127   SANE_Byte asic_io_pins;
128   /* A7 */
129   SANE_Byte timing;
130   SANE_Byte sram_bank;
131   /* A8 */
132   SANE_Byte dummy_msb;
133   SANE_Byte ccd_width_msb;
134   SANE_Byte cmt_table_length;
135   /* A9 */
136   SANE_Byte cmt_second_pos;
137   /* A10 + A8ID5 */
138   SANE_Word ccd_width;
139   /* A11 + A8ID6 */
140   SANE_Word dummy;
141   /* A12 + A13 */
142   SANE_Word byte_width;
143   /* A14 + A30W */
144   SANE_Word loop_count;
145   /* A15 */
146   SANE_Byte motor_enable;
147   SANE_Byte motor_movement;
148   SANE_Byte motor_direction;
149   SANE_Byte motor_signal;
150   SANE_Byte motor_home;
151   /* A16 */
152   SANE_Byte pixel_depth;
153   SANE_Byte image_invert;
154   SANE_Byte optical_600;
155   SANE_Byte sample_way;
156   /* A17 + A18 + A19 */
157   SANE_Byte red_ref;
158   SANE_Byte green_ref;
159   SANE_Byte blue_ref;
160   /* A20 + A21 + A22 */
161   SANE_Byte red_pd;
162   SANE_Byte green_pd;
163   SANE_Byte blue_pd;
164   /* A23 */
165   SANE_Byte a23;
166   /* A24 */
167   SANE_Byte fy1_delay;
168   SANE_Byte special_ad;
169   /* A27 */
170   SANE_Byte sclk;
171   SANE_Byte sen;
172   SANE_Byte serial_length;
173 
174   /* Use for Rowing */
175     SANE_Status (*get_row) (struct ma1017 * chip, SANE_Byte * row,
176 			    SANE_Word * lines_left);
177 
178   SANE_Word cmt_table_length_word;
179   SANE_Word cmt_second_pos_word;
180   SANE_Word row_size;
181   SANE_Word soft_resample;
182   SANE_Word total_lines;
183   SANE_Word lines_left;
184   SANE_Bool is_transfer_table[32];
185   Sensor_Type sensor;
186   Motor_Type motor;
187   Mustek_Type scanner_type;
188   SANE_Word max_block_size;
189 
190   SANE_Word total_read_urbs;
191   SANE_Word total_write_urbs;
192 }
193 ma1017;
194 
195 typedef enum Channel
196 {
197   CH_NONE = 0,
198   CH_RED = 1,
199   CH_GREEN = 2,
200   CH_BLUE = 3
201 }
202 Channel;
203 
204 typedef enum Banksize
205 {
206   BS_NONE = 0,
207   BS_4K = 1,
208   BS_8K = 2,
209   BS_16K = 3
210 }
211 Banksize;
212 
213 typedef enum Pixeldepth
214 {
215   PD_NONE = 0,
216   PD_1BIT = 1,
217   PD_4BIT = 2,
218   PD_8BIT = 3,
219   PD_12BIT = 4
220 }
221 Pixeldepth;
222 
223 typedef enum Sampleway
224 {
225   SW_NONE = 0,
226   SW_P1P6 = 1,
227   SW_P2P6 = 2,
228   SW_P3P6 = 3,
229   SW_P4P6 = 4,
230   SW_P5P6 = 5,
231   SW_P6P6 = 6
232 }
233 Sampleway;
234 
235 /* ------------------------- function declarations ------------------------ */
236 
237 static SANE_Status usb_low_init (ma1017 ** chip);
238 
239 static SANE_Status usb_low_exit (ma1017 * chip);
240 
241 /* Register read and write functions */
242 /* A0 ~ A1 */
243 static SANE_Status
244 usb_low_set_cmt_table (ma1017 * chip, SANE_Int index, Channel channel,
245 		       SANE_Bool is_move_motor, SANE_Bool is_transfer);
246 
247 /* A2 */
248 static SANE_Status usb_low_get_a2 (ma1017 * chip, SANE_Byte * value);
249 
250 static SANE_Status usb_low_start_cmt_table (ma1017 * chip);
251 
252 static SANE_Status usb_low_stop_cmt_table (ma1017 * chip);
253 
254 static SANE_Status
255 usb_low_set_test_sram_mode (ma1017 * chip, SANE_Bool is_test);
256 
257 static SANE_Status usb_low_set_fix_pattern (ma1017 * chip, SANE_Bool is_fix);
258 
259 /* A3 */
260 static SANE_Status usb_low_adjust_timing (ma1017 * chip, SANE_Byte data);
261 
262 /* A4 */
263 static SANE_Status usb_low_get_a4 (ma1017 * chip, SANE_Byte * value);
264 
265 static SANE_Status usb_low_select_timing (ma1017 * chip, SANE_Byte data);
266 
267 static SANE_Status
268 usb_low_turn_frontend_mode (ma1017 * chip, SANE_Bool is_on);
269 
270 /* A6 */
271 static SANE_Status usb_low_get_a6 (ma1017 * chip, SANE_Byte * value);
272 
273 static SANE_Status usb_low_set_asic_io_pins (ma1017 * chip, SANE_Byte data);
274 
275 static SANE_Status usb_low_set_rgb_sel_pins (ma1017 * chip, SANE_Byte data);
276 
277 /* A7 */
278 static SANE_Status usb_low_get_a7 (ma1017 * chip, SANE_Byte * value);
279 
280 static SANE_Status usb_low_set_timing (ma1017 * chip, SANE_Byte data);
281 
282 static SANE_Status usb_low_set_sram_bank (ma1017 * chip, Banksize banksize);
283 
284 /* A8 */
285 static SANE_Status usb_low_get_a8 (ma1017 * chip, SANE_Byte * value);
286 
287 static SANE_Status
288 usb_low_set_cmt_table_length (ma1017 * chip, SANE_Byte table_length);
289 
290 /* A9 */
291 static SANE_Status usb_low_get_a9 (ma1017 * chip, SANE_Byte * value);
292 
293 static SANE_Status
294 usb_low_set_cmt_second_position (ma1017 * chip, SANE_Byte position);
295 
296 /* A10 + A8ID5 */
297 static SANE_Status usb_low_get_a10 (ma1017 * chip, SANE_Byte * value);
298 
299 static SANE_Status usb_low_set_ccd_width (ma1017 * chip, SANE_Word ccd_width);
300 
301 /* A11 + A8ID6 */
302 static SANE_Status usb_low_get_a11 (ma1017 * chip, SANE_Byte * value);
303 
304 static SANE_Status usb_low_set_dummy (ma1017 * chip, SANE_Word dummy);
305 
306 /* A12 + A13 */
307 static SANE_Status usb_low_get_a12 (ma1017 * chip, SANE_Byte * value);
308 
309 static SANE_Status usb_low_get_a13 (ma1017 * chip, SANE_Byte * value);
310 
311 static SANE_Status
312 usb_low_set_image_byte_width (ma1017 * chip, SANE_Word row_size);
313 
314 static SANE_Status
315 usb_low_set_soft_resample (ma1017 * chip, SANE_Word soft_resample);
316 
317 /* A14 + A30W */
318 static SANE_Status
319 usb_low_set_cmt_loop_count (ma1017 * chip, SANE_Word loop_count);
320 
321 /* A15 */
322 static SANE_Status usb_low_get_a15 (ma1017 * chip, SANE_Byte * value);
323 
324 static SANE_Status usb_low_enable_motor (ma1017 * chip, SANE_Bool is_enable);
325 
326 static SANE_Status
327 usb_low_set_motor_movement (ma1017 * chip, SANE_Bool is_full_step,
328 			    SANE_Bool is_double_phase, SANE_Bool is_two_step);
329 
330 static SANE_Status usb_low_set_motor_signal (ma1017 * chip, SANE_Byte signal);
331 
332 static SANE_Status
333 usb_low_set_motor_direction (ma1017 * chip, SANE_Bool is_backward);
334 
335 static SANE_Status
336 usb_low_move_motor_home (ma1017 * chip, SANE_Bool is_home,
337 			 SANE_Bool is_backward);
338 
339 /* A16 */
340 static SANE_Status usb_low_get_a16 (ma1017 * chip, SANE_Byte * value);
341 
342 static SANE_Status
343 usb_low_set_image_dpi (ma1017 * chip, SANE_Bool is_optical600,
344 		       Sampleway sampleway);
345 
346 static SANE_Status
347 usb_low_set_pixel_depth (ma1017 * chip, Pixeldepth pixeldepth);
348 
349 static SANE_Status usb_low_invert_image (ma1017 * chip, SANE_Bool is_invert);
350 
351 /* A17 + A18 + A19 */
352 static SANE_Status usb_low_get_a17 (ma1017 * chip, SANE_Byte * value);
353 
354 static SANE_Status usb_low_get_a18 (ma1017 * chip, SANE_Byte * value);
355 
356 static SANE_Status usb_low_get_a19 (ma1017 * chip, SANE_Byte * value);
357 
358 static SANE_Status usb_low_set_red_ref (ma1017 * chip, SANE_Byte red_ref);
359 
360 static SANE_Status usb_low_set_green_ref (ma1017 * chip, SANE_Byte green_ref);
361 
362 static SANE_Status usb_low_set_blue_ref (ma1017 * chip, SANE_Byte blue_ref);
363 
364 /* A20 + A21 + A22 */
365 static SANE_Status usb_low_get_a20 (ma1017 * chip, SANE_Byte * value);
366 
367 static SANE_Status usb_low_get_a21 (ma1017 * chip, SANE_Byte * value);
368 
369 static SANE_Status usb_low_get_a22 (ma1017 * chip, SANE_Byte * value);
370 
371 static SANE_Status usb_low_set_red_pd (ma1017 * chip, SANE_Byte red_pd);
372 
373 static SANE_Status usb_low_set_green_pd (ma1017 * chip, SANE_Byte green_pd);
374 
375 static SANE_Status usb_low_set_blue_pd (ma1017 * chip, SANE_Byte blue_pd);
376 
377 /* A23 */
378 static SANE_Status usb_low_get_a23 (ma1017 * chip, SANE_Byte * value);
379 
380 static SANE_Status
381 usb_low_turn_peripheral_power (ma1017 * chip, SANE_Bool is_on);
382 
383 static SANE_Status usb_low_turn_lamp_power (ma1017 * chip, SANE_Bool is_on);
384 
385 static SANE_Status usb_low_set_io_3 (ma1017 * chip, SANE_Bool is_high);
386 
387 static SANE_Status
388 usb_low_set_led_light_all (ma1017 * chip, SANE_Bool is_light_all);
389 
390 /* A24 */
391 static SANE_Status usb_low_get_a24 (ma1017 * chip, SANE_Byte * value);
392 
393 static SANE_Status usb_low_set_ad_timing (ma1017 * chip, SANE_Byte pattern);
394 
395 /* A25 + A26 */
396 static SANE_Status usb_low_set_serial_byte1 (ma1017 * chip, SANE_Byte data);
397 
398 static SANE_Status usb_low_set_serial_byte2 (ma1017 * chip, SANE_Byte data);
399 
400 /* A27 */
401 static SANE_Status usb_low_get_a27 (ma1017 * chip, SANE_Byte * value);
402 
403 static SANE_Status usb_low_set_serial_format (ma1017 * chip, SANE_Byte data);
404 
405 /* A31 */
406 static SANE_Status usb_low_get_home_sensor (ma1017 * chip);
407 
408 /* Special Mode */
409 static SANE_Status usb_low_start_rowing (ma1017 * chip);
410 
411 static SANE_Status usb_low_stop_rowing (ma1017 * chip);
412 
413 static SANE_Status usb_low_wait_rowing_stop (ma1017 * chip);
414 
415 /* Global functions */
416 static SANE_Status usb_low_read_all_registers (ma1017 * chip);
417 
418 static SANE_Status
419 usb_low_get_row (ma1017 * chip, SANE_Byte * data, SANE_Word * lines_left);
420 
421 static SANE_Status
422 usb_low_get_row_direct (ma1017 * chip, SANE_Byte * data,
423 			SANE_Word * lines_left);
424 
425 static SANE_Status
426 usb_low_get_row_resample (ma1017 * chip, SANE_Byte * data,
427 			  SANE_Word * lines_left);
428 
429 /* Direct access */
430 static SANE_Status usb_low_wait_rowing (ma1017 * chip);
431 
432 static SANE_Status
433 usb_low_read_rows (ma1017 * chip, SANE_Byte * data, SANE_Word byte_count);
434 
435 static SANE_Status
436 usb_low_write_reg (ma1017 * chip, SANE_Byte reg_no, SANE_Byte data);
437 
438 static SANE_Status
439 usb_low_read_reg (ma1017 * chip, SANE_Byte reg_no, SANE_Byte * data);
440 
441 static SANE_Status
442 usb_low_identify_scanner (SANE_Int fd, Mustek_Type * scanner_type);
443 
444 static SANE_Status usb_low_open (ma1017 * chip, const char *devname);
445 
446 static SANE_Status usb_low_close (ma1017 * chip);
447 
448 #endif /* defined mustek_usb_low_h */
449