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 - 2004 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 #include <unistd.h>
48 
49 #include "../include/sane/sane.h"
50 #include "../include/sane/sanei_usb.h"
51 #include "mustek_usb_low.h"
52 
53 
54 SANE_Status
usb_low_init(ma1017 ** chip_address)55 usb_low_init (ma1017 ** chip_address)
56 {
57   SANE_Int i;
58   ma1017 *chip;
59 
60   DBG (7, "usb_low_init: start\n");
61   if (!chip_address)
62     return SANE_STATUS_INVAL;
63 
64   chip = (ma1017 *) malloc (sizeof (ma1017));
65 
66   if (!chip)
67     {
68       DBG (3, "usb_low_init: couldn't malloc %ld bytes for chip\n",
69 	   (long int) sizeof (ma1017));
70       *chip_address = 0;
71       return SANE_STATUS_NO_MEM;
72     }
73   *chip_address = chip;
74 
75   /* io */
76   chip->is_rowing = SANE_FALSE;
77   chip->is_opened = SANE_FALSE;
78   chip->fd = -1;
79 
80   /* Construction/Destruction */
81   chip->is_opened = SANE_FALSE;
82   chip->is_rowing = SANE_FALSE;
83 
84   /* A2 */
85   chip->append = 0x00;
86   chip->test_sram = 0x00;
87   chip->fix_pattern = 0x00;
88   /* A4 */
89   chip->select = 0x00;
90   chip->frontend = 0x00;
91   /* A6 */
92   chip->rgb_sel_pin = 0x02;
93   chip->asic_io_pins = 0x9c;
94   /* A7 */
95   chip->timing = 0xe8;
96   chip->sram_bank = 0x02;
97   /* A8 */
98   chip->dummy_msb = 0x00;
99   chip->ccd_width_msb = 0x00;
100   chip->cmt_table_length = 0x00;
101   /* A9 */
102   chip->cmt_second_pos = 0x00;
103   /* A10 + A8ID5 */
104   chip->ccd_width = 0x0c80;
105   /* A11 + A8ID6 */
106   chip->dummy = 0x0020;
107   /* A12 + A13 */
108   chip->byte_width = 0x09f6;
109   /* A14 + A30W */
110   chip->loop_count = 0x0db5;
111   /* A15 */
112   chip->motor_enable = 0x00;
113   chip->motor_movement = 0x60;
114   chip->motor_direction = 0x10;
115   chip->motor_signal = 0x00;
116   chip->motor_home = 0x00;
117   /* A16 */
118   chip->pixel_depth = 0x00;
119   chip->image_invert = 0x00;
120   chip->optical_600 = 0x00;
121   chip->sample_way = 0x06;
122   /* A17 + A18 + A19 */
123   chip->red_ref = 0xff;
124   chip->green_ref = 0xff;
125   chip->blue_ref = 0xff;
126   /* A20 + A21 + A22 */
127   chip->red_pd = 0x00;
128   chip->green_pd = 0x00;
129   chip->blue_pd = 0x00;
130   /* A23 */
131   chip->a23 = 0x80;
132   /* A24 */
133   chip->fy1_delay = 0x00;
134   chip->special_ad = 0x00;
135   /* A27 */
136   chip->sclk = 0x00;
137   chip->sen = 0x00;
138   chip->serial_length = 0x10;
139 
140   /* Use for Rowing */
141   chip->get_row = NULL;
142 
143   chip->cmt_table_length_word = 0x00000000;
144   chip->cmt_second_pos_word = 0x00000000;
145   chip->row_size = 0x00;
146   chip->soft_resample = 0x01;
147   chip->total_lines = 0x00;
148   chip->lines_left = 0x00;
149   for (i = 0; i < 32; i++)
150     chip->is_transfer_table[i] = SANE_FALSE;
151   chip->sensor = ST_CANON600;
152   chip->motor = MT_1200;
153 
154   chip->total_read_urbs = 0;
155   chip->total_write_urbs = 0;
156   DBG (7, "usb_low_init: exit\n");
157   return SANE_STATUS_GOOD;
158 }
159 
160 SANE_Status
usb_low_exit(ma1017 * chip)161 usb_low_exit (ma1017 * chip)
162 {
163   DBG (7, "usb_low_exit: chip = %p\n", (void *) chip);
164   if (chip)
165     {
166       if (chip->fd >= 0 && chip->is_opened)
167 	usb_low_close (chip);
168       DBG (7, "usb_low_exit: freeing chip\n");
169       free (chip);
170     }
171   DBG (5, "usb_low_exit: read %d URBs, wrote %d URBs\n",
172        chip->total_read_urbs, chip->total_write_urbs);
173   DBG (7, "usb_low_exit: exit\n");
174   return SANE_STATUS_GOOD;
175 }
176 
177 /* A0 ~ A1 */
178 SANE_Status
usb_low_set_cmt_table(ma1017 * chip,SANE_Int index,Channel channel,SANE_Bool is_move_motor,SANE_Bool is_transfer)179 usb_low_set_cmt_table (ma1017 * chip, SANE_Int index, Channel channel,
180 		       SANE_Bool is_move_motor, SANE_Bool is_transfer)
181 {
182   SANE_Byte pattern = ((SANE_Byte) index) << 4;
183   SANE_Byte reg_no = 0;
184   SANE_Status status;
185 
186   DBG (7, "usb_low_set_cmt_table: start\n");
187 
188   if (!chip->is_opened)
189     {
190       DBG (3, "usb_low_set_cmt_table: not opened yet\n");
191       return SANE_STATUS_INVAL;
192     }
193   if (chip->is_rowing)
194     {
195       DBG (3, "usb_low_set_cmt_table: stop rowing first\n");
196       return SANE_STATUS_INVAL;
197     }
198   if ((unsigned int) index > 31)
199     {
200       DBG (7, "usb_low_set_cmt_table: CMT index (%d) exceed 31", index);
201       return SANE_STATUS_INVAL;
202     }
203 
204   switch (channel)
205     {
206     case CH_RED:
207       pattern |= 0x04;
208       break;
209     case CH_GREEN:
210       pattern |= 0x08;
211       break;
212     case CH_BLUE:
213       pattern |= 0x0c;
214       break;
215     default:
216       break;
217     }
218   if (is_move_motor)
219     pattern |= 0x02;
220   if (is_transfer)
221     pattern |= 0x01;
222   if (index > 15)
223     reg_no++;
224 
225   RIE (usb_low_write_reg (chip, reg_no, pattern));
226 
227   chip->is_transfer_table[index] = is_transfer;
228 
229   DBG (7, "usb_low_set_cmt_table: exit\n");
230 
231   return SANE_STATUS_GOOD;
232 }
233 
234 /* A2 */
235 SANE_Status
usb_low_get_a2(ma1017 * chip,SANE_Byte * value)236 usb_low_get_a2 (ma1017 * chip, SANE_Byte * value)
237 {
238   SANE_Byte pattern;
239   SANE_Status status;
240 
241   DBG (7, "usb_low_get_a2: start\n");
242   if (!chip->is_opened)
243     {
244       DBG (3, "usb_low_get_a2: not opened yet\n");
245       return SANE_STATUS_INVAL;
246     }
247   if (chip->is_rowing)
248     {
249       DBG (3, "usb_low_get_a2: stop rowing first\n");
250       return SANE_STATUS_INVAL;
251     }
252   RIE (usb_low_read_reg (chip, 2, &pattern));
253 
254   chip->append = pattern & 0x10;
255   chip->test_sram = pattern & 0x20;
256   chip->fix_pattern = pattern & 0x80;
257   if (value)
258     *value = pattern;
259   DBG (7, "usb_low_get_a2: exit, value =%d\n", pattern);
260   return SANE_STATUS_GOOD;
261 }
262 
263 SANE_Status
usb_low_start_cmt_table(ma1017 * chip)264 usb_low_start_cmt_table (ma1017 * chip)
265 {
266   SANE_Byte data_field[2];
267   SANE_Status status;
268   size_t n;
269 
270   DBG (7, "usb_low_start_cmt_table: start\n");
271 
272   data_field[0] = 0x02 | chip->append | chip->test_sram | chip->fix_pattern;
273   data_field[1] = 2;
274 
275   if (!chip->is_opened)
276     {
277       DBG (3, "usb_low_start_cmt_table: not opened yet\n");
278       return SANE_STATUS_INVAL;
279     }
280   if (chip->is_rowing)
281     {
282       DBG (7, "usb_low_start_cmt_table: Already Rowing\n");
283       return SANE_STATUS_INVAL;
284     }
285 
286   data_field[1] |= 0x60;
287   n = 2;
288   status = sanei_usb_write_bulk (chip->fd, data_field, &n);
289   if (status != SANE_STATUS_GOOD || n != 2)
290     {
291       DBG (3, "usb_low_start_cmt_table: can't write, wanted 2 bytes, "
292 	   "wrote %lu bytes\n", (unsigned long int) n);
293       return SANE_STATUS_IO_ERROR;
294     }
295   chip->total_write_urbs++;
296   chip->is_rowing = SANE_TRUE;
297   DBG (7, "usb_low_start_cmt_table: exit\n");
298   return SANE_STATUS_GOOD;
299 }
300 
301 SANE_Status
usb_low_stop_cmt_table(ma1017 * chip)302 usb_low_stop_cmt_table (ma1017 * chip)
303 {
304   SANE_Byte data_field[2];
305   SANE_Byte read_byte;
306   size_t n;
307   SANE_Status status;
308 
309   DBG (7, "usb_low_stop_cmt_table: start\n");
310 
311   if (!chip->is_opened)
312     {
313       DBG (3, "usb_low_stop_cmt_table: not opened yet\n");
314       return SANE_STATUS_INVAL;
315     }
316   if (!chip->is_rowing)
317     {
318       DBG (7, "usb_low_stop_cmt_table: Not Rowing yet\n");
319       return SANE_STATUS_INVAL;
320     }
321 
322   data_field[0] = 0x01 | chip->append | chip->test_sram | chip->fix_pattern;
323   data_field[1] = 2;
324   data_field[1] |= 0x80;
325   n = 2;
326   status = sanei_usb_write_bulk (chip->fd, data_field, &n);
327   if (status != SANE_STATUS_GOOD || n != 2)
328     {
329       DBG (3, "usb_low_stop_cmt_table: couldn't write, wanted 2 bytes, wrote "
330 	   "%lu bytes\n", (unsigned long int) n);
331       return SANE_STATUS_IO_ERROR;
332     }
333   chip->total_write_urbs++;
334   n = 1;
335   status = sanei_usb_read_bulk (chip->fd, &read_byte, &n);
336   if (status != SANE_STATUS_GOOD || n != 1)
337     {
338       DBG (3, "usb_low_stop_cmt_table: couldn't read, wanted 1 byte, got %lu "
339 	   "bytes\n", (unsigned long int) n);
340       return SANE_STATUS_IO_ERROR;
341     }
342   chip->total_read_urbs++;
343   chip->is_rowing = SANE_FALSE;
344 
345   DBG (7, "usb_low_stop_cmt_table: exit\n");
346 
347   return SANE_STATUS_GOOD;
348 }
349 
350 SANE_Status
usb_low_set_test_sram_mode(ma1017 * chip,SANE_Bool is_test)351 usb_low_set_test_sram_mode (ma1017 * chip, SANE_Bool is_test)
352 {
353   SANE_Byte data, reg_no;
354   SANE_Status status;
355 
356   DBG (7, "usb_low_set_test_sram_mode: start\n");
357 
358   data = chip->append | chip->test_sram | chip->fix_pattern;
359   reg_no = 2;
360 
361   if (!chip->is_opened)
362     {
363       DBG (3, "usb_low_set_test_sram_mode: not opened yet\n");
364       return SANE_STATUS_INVAL;
365     }
366   if (chip->is_rowing)
367     {
368       DBG (3, "usb_low_set_test_sram_mode: stop rowing first\n");
369       return SANE_STATUS_INVAL;
370     }
371 
372   if (is_test)
373     chip->test_sram = 0x20;
374   else
375     chip->test_sram = 0x00;
376 
377   RIE (usb_low_write_reg (chip, reg_no, data));
378 
379   DBG (7, "usb_low_set_test_sram_mode: exit\n");
380   return SANE_STATUS_GOOD;
381 }
382 
383 SANE_Status
usb_low_set_fix_pattern(ma1017 * chip,SANE_Bool is_fix)384 usb_low_set_fix_pattern (ma1017 * chip, SANE_Bool is_fix)
385 {
386   SANE_Byte data, reg_no;
387   SANE_Status status;
388 
389   DBG (7, "usb_low_set_fix_pattern: start\n");
390 
391   data = chip->append | chip->test_sram | chip->fix_pattern;
392   reg_no = 2;
393 
394   if (!chip->is_opened)
395     {
396       DBG (3, "usb_low_set_fix_pattern: not opened yet\n");
397       return SANE_STATUS_INVAL;
398     }
399   if (chip->is_rowing)
400     {
401       DBG (3, "usb_low_set_fix_pattern: stop rowing first\n");
402       return SANE_STATUS_INVAL;
403     }
404 
405   if (is_fix)
406     chip->fix_pattern = 0x80;
407   else
408     chip->fix_pattern = 0x00;
409 
410   RIE (usb_low_write_reg (chip, reg_no, data));
411 
412   DBG (7, "usb_low_set_fix_pattern: exit\n");
413   return SANE_STATUS_GOOD;
414 }
415 
416 SANE_Status
usb_low_adjust_timing(ma1017 * chip,SANE_Byte data)417 usb_low_adjust_timing (ma1017 * chip, SANE_Byte data)
418 {
419   SANE_Status status;
420   SANE_Byte reg_no;
421 
422   DBG (7, "usb_low_adjust_timing: start\n");
423 
424   reg_no = 3;
425 
426   if (!chip->is_opened)
427     {
428       DBG (3, "usb_low_adjust_timing: not opened yet\n");
429       return SANE_STATUS_INVAL;
430     }
431   if (chip->is_rowing)
432     {
433       DBG (3, "usb_low_adjust_timing: stop rowing first\n");
434       return SANE_STATUS_INVAL;
435     }
436 
437   RIE (usb_low_write_reg (chip, reg_no, data));
438 
439   DBG (7, "usb_low_adjust_timing: exit\n");
440 
441   return SANE_STATUS_GOOD;
442 }
443 
444 /* A4 */
445 SANE_Status
usb_low_get_a4(ma1017 * chip,SANE_Byte * value)446 usb_low_get_a4 (ma1017 * chip, SANE_Byte * value)
447 {
448   SANE_Status status;
449   SANE_Byte pattern;
450 
451   DBG (7, "usb_low_get_a4: start\n");
452   if (!chip->is_opened)
453     {
454       DBG (3, "usb_low_get_a4: not opened yet\n");
455       return SANE_STATUS_INVAL;
456     }
457   if (chip->is_rowing)
458     {
459       DBG (3, "usb_low_get_a4: stop rowing first\n");
460       return SANE_STATUS_INVAL;
461     }
462 
463   RIE (usb_low_read_reg (chip, 4, &pattern));
464 
465   chip->select = pattern & 0xfe;
466   chip->frontend = pattern & 0x01;
467 
468   if (value)
469     *value = pattern;
470 
471   DBG (7, "usb_low_get_a4: exit, value=%d\n", pattern);
472 
473   return SANE_STATUS_GOOD;
474 }
475 
476 SANE_Status
usb_low_select_timing(ma1017 * chip,SANE_Byte data)477 usb_low_select_timing (ma1017 * chip, SANE_Byte data)
478 {
479   SANE_Status status;
480   SANE_Byte reg_no;
481 
482   DBG (7, "usb_low_select_timing: start\n");
483 
484   reg_no = 4;
485 
486   if (!chip->is_opened)
487     {
488       DBG (3, "usb_low_select_timing: not opened yet\n");
489       return SANE_STATUS_INVAL;
490     }
491   if (chip->is_rowing)
492     {
493       DBG (3, "usb_low_select_timing: stop rowing first\n");
494       return SANE_STATUS_INVAL;
495     }
496 
497   chip->select = data & 0xfe;
498   chip->frontend = data & 0x01;
499 
500   RIE (usb_low_write_reg (chip, reg_no, data));
501 
502   DBG (7, "usb_low_select_timing: exit\n");
503   return SANE_STATUS_GOOD;
504 }
505 
506 SANE_Status
usb_low_turn_frontend_mode(ma1017 * chip,SANE_Bool is_on)507 usb_low_turn_frontend_mode (ma1017 * chip, SANE_Bool is_on)
508 {
509   SANE_Status status;
510   SANE_Byte data, reg_no;
511 
512   DBG (7, "usb_low_turn_frontend_mode: start\n");
513   if (!chip->is_opened)
514     {
515       DBG (3, "usb_low_turn_frontend_mode: not opened yet\n");
516       return SANE_STATUS_INVAL;
517     }
518   if (chip->is_rowing)
519     {
520       DBG (3, "usb_low_turn_frontend_mode: stop rowing first\n");
521       return SANE_STATUS_INVAL;
522     }
523 
524   if (is_on)
525     chip->frontend = 0x01;
526   else
527     chip->frontend = 0x00;
528 
529   data = chip->select | chip->frontend;
530   reg_no = 4;
531 
532   RIE (usb_low_write_reg (chip, reg_no, data));
533 
534   DBG (7, "usb_low_turn_frontend_mode: exit\n");
535   return SANE_STATUS_GOOD;
536 }
537 
538 /* A6 */
539 SANE_Status
usb_low_get_a6(ma1017 * chip,SANE_Byte * value)540 usb_low_get_a6 (ma1017 * chip, SANE_Byte * value)
541 {
542   SANE_Byte pattern;
543   SANE_Status status;
544 
545   DBG (7, "usb_low_get_a6: start\n");
546 
547   if (!chip->is_opened)
548     {
549       DBG (3, "usb_low_get_a6: not opened yet\n");
550       return SANE_STATUS_INVAL;
551     }
552   if (chip->is_rowing)
553     {
554       DBG (3, "usb_low_get_a6: stop rowing first\n");
555       return SANE_STATUS_INVAL;
556     }
557   RIE (usb_low_read_reg (chip, 6, &pattern));
558 
559   chip->asic_io_pins = pattern & 0xdc;
560   chip->rgb_sel_pin = pattern & 0x03;
561 
562   if (value)
563     *value = pattern;
564 
565   DBG (7, "usb_low_get_a6: exit\n");
566 
567   return SANE_STATUS_GOOD;
568 }
569 
570 SANE_Status
usb_low_set_asic_io_pins(ma1017 * chip,SANE_Byte data)571 usb_low_set_asic_io_pins (ma1017 * chip, SANE_Byte data)
572 {
573   SANE_Status status;
574   SANE_Byte reg_no;
575 
576   DBG (7, "usb_low_set_asic_io_pins: start\n");
577 
578   if (!chip->is_opened)
579     {
580       DBG (3, "usb_low_set_asic_io_pins: not opened yet\n");
581       return SANE_STATUS_INVAL;
582     }
583   if (chip->is_rowing)
584     {
585       DBG (3, "usb_low_set_asic_io_pins: stop rowing first\n");
586       return SANE_STATUS_INVAL;
587     }
588 
589   chip->asic_io_pins = data & 0xdc;
590 
591   data = chip->asic_io_pins | chip->rgb_sel_pin;
592   reg_no = 6;
593 
594   RIE (usb_low_write_reg (chip, reg_no, data));
595 
596   DBG (7, "usb_low_set_asic_io_pins: exit\n");
597   return SANE_STATUS_GOOD;
598 }
599 
600 SANE_Status
usb_low_set_rgb_sel_pins(ma1017 * chip,SANE_Byte data)601 usb_low_set_rgb_sel_pins (ma1017 * chip, SANE_Byte data)
602 {
603   SANE_Status status;
604   SANE_Byte reg_no;
605 
606   DBG (7, "usb_low_set_rgb_sel_pins: start\n");
607 
608   if (!chip->is_opened)
609     {
610       DBG (3, "usb_low_set_rgb_sel_pins: not opened yet\n");
611       return SANE_STATUS_INVAL;
612     }
613   if (chip->is_rowing)
614     {
615       DBG (3, "usb_low_set_rgb_sel_pins: stop rowing first\n");
616       return SANE_STATUS_INVAL;
617     }
618   chip->rgb_sel_pin = data & 0x03;
619   data = chip->asic_io_pins | chip->rgb_sel_pin;
620   reg_no = 6;
621 
622   RIE (usb_low_write_reg (chip, reg_no, data));
623 
624   DBG (7, "usb_low_set_rgb_sel_pins: exit\n");
625   return SANE_STATUS_GOOD;	/* was false? */
626 }
627 
628 /* A7 */
629 SANE_Status
usb_low_get_a7(ma1017 * chip,SANE_Byte * value)630 usb_low_get_a7 (ma1017 * chip, SANE_Byte * value)
631 {
632   SANE_Byte pattern;
633   SANE_Status status;
634 
635   DBG (7, "usb_low_get_a7: start\n");
636   if (!chip->is_opened)
637     {
638       DBG (3, "usb_low_get_a7: not opened yet\n");
639       return SANE_STATUS_INVAL;
640     }
641   if (chip->is_rowing)
642     {
643       DBG (3, "usb_low_get_a7: stop rowing first\n");
644       return SANE_STATUS_INVAL;
645     }
646   RIE (usb_low_read_reg (chip, 7, &pattern));
647 
648   if (value)
649     *value = pattern;
650 
651   chip->timing = pattern & 0xfc;
652   chip->sram_bank = pattern & 0x03;
653 
654   DBG (7, "usb_low_get_a7: exit\n");
655 
656   return SANE_STATUS_GOOD;
657 }
658 
659 SANE_Status
usb_low_set_timing(ma1017 * chip,SANE_Byte data)660 usb_low_set_timing (ma1017 * chip, SANE_Byte data)
661 {
662   SANE_Status status;
663   SANE_Byte reg_no;
664 
665   DBG (7, "usb_low_set_timing: start\n");
666   if (!chip->is_opened)
667     {
668       DBG (3, "usb_low_set_timing: not opened yet\n");
669       return SANE_STATUS_INVAL;
670     }
671   if (chip->is_rowing)
672     {
673       DBG (3, "usb_low_set_timing: stop rowing first\n");
674       return SANE_STATUS_INVAL;
675     }
676 
677   chip->timing = data & 0xfc;
678   data = chip->timing | chip->sram_bank;
679   reg_no = 7;
680 
681   RIE (usb_low_write_reg (chip, reg_no, data));
682 
683   DBG (7, "usb_low_set_timing: exit\n");
684   return SANE_STATUS_GOOD;
685 }
686 
687 SANE_Status
usb_low_set_sram_bank(ma1017 * chip,Banksize banksize)688 usb_low_set_sram_bank (ma1017 * chip, Banksize banksize)
689 {
690   SANE_Status status;
691   SANE_Byte data, reg_no;
692 
693   DBG (7, "usb_low_set_sram_bank: start\n");
694   if (!chip->is_opened)
695     {
696       DBG (3, "usb_low_set_sram_bank: not opened yet\n");
697       return SANE_STATUS_INVAL;
698     }
699   if (chip->is_rowing)
700     {
701       DBG (3, "usb_low_set_sram_bank: stop rowing first\n");
702       return SANE_STATUS_INVAL;
703     }
704 
705   switch (banksize)
706     {
707     case BS_4K:
708       chip->sram_bank = 0x00;
709       break;
710     case BS_8K:
711       chip->sram_bank = 0x01;
712       break;
713     case BS_16K:
714       chip->sram_bank = 0x02;
715       break;
716     default:
717       DBG (3, "usb_low_set_sram_bank: bsBankSize error\n");
718       return SANE_STATUS_INVAL;
719       break;
720     }
721   data = chip->timing | chip->sram_bank;
722   reg_no = 7;
723 
724   RIE (usb_low_write_reg (chip, reg_no, data));
725 
726   DBG (7, "usb_low_set_sram_bank: exit\n");
727   return SANE_STATUS_GOOD;
728 }
729 
730 /* A8 */
731 SANE_Status
usb_low_get_a8(ma1017 * chip,SANE_Byte * value)732 usb_low_get_a8 (ma1017 * chip, SANE_Byte * value)
733 {
734   SANE_Byte pattern;
735   SANE_Status status;
736 
737   DBG (7, "usb_low_get_a8: start\n");
738   if (!chip->is_opened)
739     {
740       DBG (3, "usb_low_get_a8: not opened yet\n");
741       return SANE_STATUS_INVAL;
742     }
743   if (chip->is_rowing)
744     {
745       DBG (3, "usb_low_get_a8: stop rowing first\n");
746       return SANE_STATUS_INVAL;
747     }
748 
749   RIE (usb_low_read_reg (chip, 8, &pattern));
750 
751   chip->dummy_msb = pattern & 0x40;
752   chip->ccd_width_msb = pattern & 0x20;
753   chip->cmt_table_length = pattern & 0x1f;
754   chip->ccd_width =
755     ((chip->ccd_width / 32) & 0x00ff) * 32 +
756     ((chip->ccd_width_msb == 0) ? 0 : 0x0100 * 32);
757   chip->dummy =
758     ((chip->dummy / 32) & 0x00ff) * 32 +
759     ((chip->dummy_msb == 0) ? 0 : 0x0100 * 32);
760 
761   if (value)
762     *value = pattern;
763 
764   DBG (7, "usb_low_get_a8: exit\n");
765   return SANE_STATUS_GOOD;
766 }
767 
768 SANE_Status
usb_low_set_cmt_table_length(ma1017 * chip,SANE_Byte table_length)769 usb_low_set_cmt_table_length (ma1017 * chip, SANE_Byte table_length)
770 {
771   SANE_Status status;
772   SANE_Byte data, reg_no;
773 
774   DBG (7, "usb_low_set_cmt_table_length: start\n");
775   if (!chip->is_opened)
776     {
777       DBG (3, "usb_low_set_cmt_table_length: not opened yet\n");
778       return SANE_STATUS_INVAL;
779     }
780   if (chip->is_rowing)
781     {
782       DBG (3, "usb_low_set_cmt_table_length: stop rowing first\n");
783       return SANE_STATUS_INVAL;
784     }
785   if (table_length > 32)
786     {
787       DBG (3, "usb_low_set_cmt_table_length: length %d exceeds 32\n",
788 	   (int) table_length);
789       return SANE_STATUS_INVAL;
790     }
791   if (table_length == 0)
792     {
793       DBG (3, "usb_low_set_cmt_table_length: length is 0\n");
794       return SANE_STATUS_INVAL;
795     }
796 
797   chip->cmt_table_length = table_length - 1;
798   chip->cmt_table_length_word = (SANE_Word) table_length;
799   data = chip->cmt_table_length | chip->ccd_width_msb | chip->dummy_msb;
800   reg_no = 8;
801 
802   RIE (usb_low_write_reg (chip, reg_no, data));
803 
804   DBG (7, "usb_low_set_cmt_table_length: exit\n");
805   return SANE_STATUS_GOOD;
806 }
807 
808 /* A9 */
809 SANE_Status
usb_low_get_a9(ma1017 * chip,SANE_Byte * value)810 usb_low_get_a9 (ma1017 * chip, SANE_Byte * value)
811 {
812   SANE_Byte pattern;
813   SANE_Status status;
814 
815   DBG (7, "usb_low_get_a9: start\n");
816 
817   if (!chip->is_opened)
818     {
819       DBG (3, "usb_low_get_a9: not opened yet\n");
820       return SANE_STATUS_INVAL;
821     }
822   if (chip->is_rowing)
823     {
824       DBG (3, "usb_low_get_a9: stop rowing first\n");
825       return SANE_STATUS_INVAL;
826     }
827   RIE (usb_low_read_reg (chip, 9, &pattern));
828 
829   chip->cmt_second_pos = pattern & 0x1f;
830 
831   if (value)
832     *value = pattern;
833 
834   DBG (7, "usb_low_get_a9: exit\n");
835   return SANE_STATUS_GOOD;
836 }
837 
838 SANE_Status
usb_low_set_cmt_second_position(ma1017 * chip,SANE_Byte position)839 usb_low_set_cmt_second_position (ma1017 * chip, SANE_Byte position)
840 {
841   SANE_Byte data, reg_no;
842   SANE_Status status;
843 
844   DBG (7, "usb_low_set_cmt_second_position: start\n");
845 
846   if (!chip->is_opened)
847     {
848       DBG (3, "usb_low_set_cmt_second_position: not opened yet\n");
849       return SANE_STATUS_INVAL;
850     }
851   if (chip->is_rowing)
852     {
853       DBG (3, "usb_low_set_cmt_second_position: stop rowing first\n");
854       return SANE_STATUS_INVAL;
855     }
856   if (position > 31)
857     {
858       DBG (3, "usb_low_set_cmt_second_position: length: %d exceeds 31\n",
859 	   (int) position);
860       return SANE_STATUS_INVAL;
861     }
862 
863   chip->cmt_second_pos = position;
864   chip->cmt_second_pos_word = (SANE_Word) (position);
865   data = chip->cmt_second_pos;
866   reg_no = 9;
867 
868   RIE (usb_low_write_reg (chip, reg_no, data));
869 
870   DBG (7, "usb_low_set_cmt_second_position: exit\n");
871 
872   return SANE_STATUS_GOOD;
873 }
874 
875 /* A10 + A8ID5 */
876 SANE_Status
usb_low_get_a10(ma1017 * chip,SANE_Byte * value)877 usb_low_get_a10 (ma1017 * chip, SANE_Byte * value)
878 {
879   SANE_Byte pattern;
880   SANE_Status status;
881 
882   DBG (7, "usb_low_get_a10: start\n");
883 
884   if (!chip->is_opened)
885     {
886       DBG (3, "usb_low_get_a10: not opened yet\n");
887       return SANE_STATUS_INVAL;
888     }
889   if (chip->is_rowing)
890     {
891       DBG (3, "usb_low_get_a10: stop rowing first\n");
892       return SANE_STATUS_INVAL;
893     }
894 
895   RIE (usb_low_read_reg (chip, 10, &pattern));
896 
897   chip->ccd_width =
898     ((SANE_Word) (pattern)) * 32 +
899     ((chip->ccd_width_msb == 0) ? 0 : 0x0100 * 32);
900 
901   if (value)
902     *value = pattern;
903 
904   DBG (7, "usb_low_get_a10: exit\n");
905 
906   return SANE_STATUS_GOOD;
907 }
908 
909 SANE_Status
usb_low_set_ccd_width(ma1017 * chip,SANE_Word ccd_width)910 usb_low_set_ccd_width (ma1017 * chip, SANE_Word ccd_width)
911 {
912   SANE_Status status;
913   SANE_Byte data, reg_no;
914 
915   DBG (7, "usb_low_set_ccd_width: start\n");
916 
917   if (!chip->is_opened)
918     {
919       DBG (3, "usb_low_set_ccd_width: not opened yet\n");
920       return SANE_STATUS_INVAL;
921     }
922   if (chip->is_rowing)
923     {
924       DBG (3, "usb_low_set_ccd_width: stop rowing first\n");
925       return SANE_STATUS_INVAL;
926     }
927   if (ccd_width / 32 > 0x01ff)
928     {
929       DBG (3, "usb_low_set_ccd_width: width %d too high\n", (int) ccd_width);
930       return SANE_STATUS_INVAL;
931     }
932 
933   chip->ccd_width = ccd_width;
934   ccd_width /= 32;
935   if (HIBYTE (ccd_width) == 0x01)
936     chip->ccd_width_msb = 0x20;
937   else
938     chip->ccd_width_msb = 0x00;
939 
940   data = chip->cmt_table_length | chip->ccd_width_msb | chip->dummy_msb;
941   reg_no = 8;
942   RIE (usb_low_write_reg (chip, reg_no, data));
943 
944   data = LOBYTE (ccd_width);
945   reg_no = 10;
946   RIE (usb_low_write_reg (chip, reg_no, data));
947 
948   DBG (7, "usb_low_set_ccd_width: exit\n");
949   return SANE_STATUS_GOOD;
950 }
951 
952 /* A11 + A8ID6 */
953 SANE_Status
usb_low_get_a11(ma1017 * chip,SANE_Byte * value)954 usb_low_get_a11 (ma1017 * chip, SANE_Byte * value)
955 {
956   SANE_Byte pattern;
957   SANE_Status status;
958 
959   DBG (7, "usb_low_get_a11: start\n");
960 
961   if (!chip->is_opened)
962     {
963       DBG (3, "usb_low_get_a11: not opened yet\n");
964       return SANE_STATUS_INVAL;
965     }
966   if (chip->is_rowing)
967     {
968       DBG (3, "usb_low_get_a11: stop rowing first\n");
969       return SANE_STATUS_INVAL;
970     }
971 
972   RIE (usb_low_read_reg (chip, 11, &pattern));
973 
974   chip->dummy =
975     ((SANE_Word) (pattern)) * 32 + ((chip->dummy_msb == 0) ? 0 : 0x0100 * 32);
976 
977   if (value)
978     *value = pattern;
979 
980   DBG (7, "usb_low_get_a11: exit\n");
981   return SANE_STATUS_GOOD;
982 }
983 
984 SANE_Status
usb_low_set_dummy(ma1017 * chip,SANE_Word dummy)985 usb_low_set_dummy (ma1017 * chip, SANE_Word dummy)
986 {
987   SANE_Status status;
988   SANE_Byte data, reg_no;
989 
990   DBG (7, "usb_low_set_dummy: start\n");
991 
992   if (!chip->is_opened)
993     {
994       DBG (3, "usb_low_set_dummy: not opened yet\n");
995       return SANE_STATUS_INVAL;
996     }
997   if (chip->is_rowing)
998     {
999       DBG (3, "usb_low_set_dummy: stop rowing first\n");
1000       return SANE_STATUS_INVAL;
1001     }
1002   if (dummy / 32 > 0x01ff)
1003     {
1004       DBG (7, "usb_low_set_dummy: width %d exceeded\n", (int) dummy);
1005       return SANE_STATUS_INVAL;
1006     }
1007 
1008   chip->dummy = dummy;
1009   dummy /= 32;
1010   dummy++;
1011   if (HIBYTE (dummy) == 0x01)
1012     chip->dummy_msb = 0x40;
1013   else
1014     chip->dummy_msb = 0x00;
1015   data = chip->cmt_table_length | chip->ccd_width_msb | chip->dummy_msb;
1016   reg_no = 8;
1017   RIE (usb_low_write_reg (chip, reg_no, data));
1018 
1019   data = LOBYTE (dummy);
1020   reg_no = 11;
1021   RIE (usb_low_write_reg (chip, reg_no, data));
1022 
1023   DBG (7, "usb_low_set_dummy: exit\n");
1024   return SANE_STATUS_GOOD;
1025 }
1026 
1027 /* A12 + A13 */
1028 SANE_Status
usb_low_get_a12(ma1017 * chip,SANE_Byte * value)1029 usb_low_get_a12 (ma1017 * chip, SANE_Byte * value)
1030 {
1031   SANE_Byte pattern;
1032   SANE_Status status;
1033 
1034   DBG (7, "usb_low_get_a12: start\n");
1035 
1036   if (!chip->is_opened)
1037     {
1038       DBG (3, "usb_low_get_a12: not opened yet\n");
1039       return SANE_STATUS_INVAL;
1040     }
1041   if (chip->is_rowing)
1042     {
1043       DBG (3, "usb_low_get_a12: stop rowing first\n");
1044       return SANE_STATUS_INVAL;
1045     }
1046 
1047   RIE (usb_low_read_reg (chip, 12, &pattern));
1048 
1049   chip->byte_width = (chip->byte_width & 0x3f00) + ((SANE_Word) pattern);
1050   chip->soft_resample = (chip->soft_resample == 0) ? 1 : chip->soft_resample;
1051   chip->get_row =
1052     (chip->soft_resample == 1)
1053     ? &usb_low_get_row_direct : &usb_low_get_row_resample;
1054   chip->row_size = chip->byte_width / chip->soft_resample;
1055 
1056   if (value)
1057     *value = pattern;
1058 
1059   DBG (7, "usb_low_get_a12: exit\n");
1060   return SANE_STATUS_GOOD;
1061 }
1062 
1063 SANE_Status
usb_low_get_a13(ma1017 * chip,SANE_Byte * value)1064 usb_low_get_a13 (ma1017 * chip, SANE_Byte * value)
1065 {
1066   SANE_Byte pattern;
1067   SANE_Status status;
1068 
1069   DBG (7, "usb_low_get_a13: start\n");
1070 
1071   if (!chip->is_opened)
1072     {
1073       DBG (3, "usb_low_get_a13: not opened yet\n");
1074       return SANE_STATUS_INVAL;
1075     }
1076   if (chip->is_rowing)
1077     {
1078       DBG (3, "usb_low_get_a13: stop rowing first\n");
1079       return SANE_STATUS_INVAL;
1080     }
1081 
1082   RIE (usb_low_read_reg (chip, 13, &pattern));
1083 
1084   chip->byte_width =
1085     (chip->byte_width & 0x00ff) + (((SANE_Word) (pattern & 0x3f)) << 8);
1086   chip->soft_resample = (chip->soft_resample == 0) ? 1 : chip->soft_resample;
1087   chip->get_row =
1088     (chip->soft_resample ==
1089      1) ? &usb_low_get_row_direct : &usb_low_get_row_resample;
1090   chip->row_size = chip->byte_width / chip->soft_resample;
1091 
1092   if (value)
1093     *value = pattern;
1094 
1095   DBG (7, "usb_low_get_a13: exit\n");
1096   return SANE_STATUS_GOOD;
1097 }
1098 
1099 SANE_Status
usb_low_set_image_byte_width(ma1017 * chip,SANE_Word row_size)1100 usb_low_set_image_byte_width (ma1017 * chip, SANE_Word row_size)
1101 {
1102   SANE_Byte data, reg_no;
1103   SANE_Status status;
1104 
1105   DBG (7, "usb_low_set_image_byte_width: start\n");
1106 
1107   if (!chip->is_opened)
1108     {
1109       DBG (3, "usb_low_set_image_byte_width: not opened yet\n");
1110       return SANE_STATUS_INVAL;
1111     }
1112   if (chip->is_rowing)
1113     {
1114       DBG (3, "usb_low_set_image_byte_width: stop rowing first\n");
1115       return SANE_STATUS_INVAL;
1116     }
1117 
1118   chip->row_size = row_size;
1119   chip->soft_resample = (chip->soft_resample == 0) ? 1 : chip->soft_resample;
1120   chip->get_row = (chip->soft_resample == 1) ? &usb_low_get_row_direct
1121     : &usb_low_get_row_resample;
1122   chip->byte_width = chip->row_size * chip->soft_resample;
1123   if (chip->byte_width > 0x3fff)
1124     {
1125       DBG (3, "usb_low_set_image_byte_width: width %d exceeded\n",
1126 	   (int) chip->byte_width);
1127       return SANE_STATUS_INVAL;
1128     }
1129 
1130   data = LOBYTE (chip->byte_width);
1131   reg_no = 12;
1132   RIE (usb_low_write_reg (chip, reg_no, data));
1133 
1134   data = HIBYTE (chip->byte_width);
1135   reg_no = 13;
1136   RIE (usb_low_write_reg (chip, reg_no, data));
1137 
1138   DBG (7, "usb_low_set_image_byte_width: exit\n");
1139 
1140   return SANE_STATUS_GOOD;
1141 }
1142 
1143 SANE_Status
usb_low_set_soft_resample(ma1017 * chip,SANE_Word soft_resample)1144 usb_low_set_soft_resample (ma1017 * chip, SANE_Word soft_resample)
1145 {
1146   SANE_Byte data, reg_no;
1147   SANE_Status status;
1148 
1149   DBG (7, "usb_low_set_soft_resample: start\n");
1150 
1151   if (!chip->is_opened)
1152     {
1153       DBG (3, "usb_low_set_soft_resample: not opened yet\n");
1154       return SANE_STATUS_INVAL;
1155     }
1156   if (chip->is_rowing)
1157     {
1158       DBG (3, "usb_low_set_soft_resample: stop rowing first\n");
1159       return SANE_STATUS_INVAL;
1160     }
1161   if (soft_resample == 0x00)
1162     {
1163       DBG (3, "usb_low_set_soft_resample: soft_resample==0\n");
1164       return SANE_STATUS_INVAL;
1165     }
1166 
1167   chip->soft_resample = soft_resample;
1168   chip->get_row = (chip->soft_resample == 1) ? &usb_low_get_row_direct
1169     : &usb_low_get_row_resample;
1170   chip->byte_width = chip->row_size * chip->soft_resample;
1171   if (chip->byte_width > 0x3fff)
1172     {
1173       DBG (3, "usb_low_set_soft_resample: width %d exceeded",
1174 	   (int) chip->byte_width);
1175       return SANE_STATUS_INVAL;
1176     }
1177 
1178   data = LOBYTE (chip->byte_width);
1179   reg_no = 12;
1180   RIE (usb_low_write_reg (chip, reg_no, data));
1181 
1182   data = HIBYTE (chip->byte_width);
1183   reg_no = 13;
1184   RIE (usb_low_write_reg (chip, reg_no, data));
1185 
1186   DBG (7, "usb_low_set_soft_resample: exit\n");
1187   return SANE_STATUS_GOOD;
1188 }
1189 
1190 /* A14 + A30W */
1191 SANE_Status
usb_low_set_cmt_loop_count(ma1017 * chip,SANE_Word loop_count)1192 usb_low_set_cmt_loop_count (ma1017 * chip, SANE_Word loop_count)
1193 {
1194   SANE_Byte data, reg_no;
1195   SANE_Status status;
1196 
1197   DBG (7, "usb_low_set_cmt_loop_count: start\n");
1198 
1199   if (!chip->is_opened)
1200     {
1201       DBG (3, "usb_low_set_cmt_loop_count: not opened yet\n");
1202       return SANE_STATUS_INVAL;
1203     }
1204   if (chip->is_rowing)
1205     {
1206       DBG (3, "usb_low_set_cmt_loop_count: stop rowing first\n");
1207       return SANE_STATUS_INVAL;
1208     }
1209 
1210   chip->loop_count = loop_count;
1211 
1212   data = LOBYTE (loop_count);
1213   reg_no = 14;
1214   RIE (usb_low_write_reg (chip, reg_no, data));
1215 
1216   data = HIBYTE (loop_count);
1217   reg_no = 30;
1218   RIE (usb_low_write_reg (chip, reg_no, data));
1219 
1220   DBG (7, "usb_low_set_cmt_loop_count: exit\n");
1221   return SANE_STATUS_GOOD;
1222 }
1223 
1224 /* A15 */
1225 SANE_Status
usb_low_get_a15(ma1017 * chip,SANE_Byte * value)1226 usb_low_get_a15 (ma1017 * chip, SANE_Byte * value)
1227 {
1228   SANE_Byte pattern;
1229   SANE_Status status;
1230 
1231   DBG (7, "usb_low_get_a15: start\n");
1232 
1233   if (!chip->is_opened)
1234     {
1235       DBG (3, "usb_low_get_a15: not opened yet\n");
1236       return SANE_STATUS_INVAL;
1237     }
1238   if (chip->is_rowing)
1239     {
1240       DBG (3, "usb_low_get_a15: stop rowing first\n");
1241       return SANE_STATUS_INVAL;
1242     }
1243 
1244   RIE (usb_low_read_reg (chip, 15, &pattern));
1245 
1246   chip->motor_enable = pattern & 0x80;
1247   chip->motor_movement = pattern & 0x68;
1248   chip->motor_direction = pattern & 10;
1249   chip->motor_signal = pattern & 0x06;
1250   chip->motor_home = pattern & 0x01;
1251 
1252   if (value)
1253     *value = pattern;
1254 
1255   DBG (7, "usb_low_get_a15: exit\n");
1256   return SANE_STATUS_GOOD;
1257 }
1258 
1259 SANE_Status
usb_low_enable_motor(ma1017 * chip,SANE_Bool is_enable)1260 usb_low_enable_motor (ma1017 * chip, SANE_Bool is_enable)
1261 {
1262   SANE_Byte data, reg_no;
1263   SANE_Status status;
1264 
1265   DBG (7, "usb_low_enable_motor: start\n");
1266   if (!chip->is_opened)
1267     {
1268       DBG (3, "usb_low_enable_motor: not opened yet\n");
1269       return SANE_STATUS_INVAL;
1270     }
1271   if (chip->is_rowing)
1272     {
1273       DBG (3, "usb_low_enable_motor: stop rowing first\n");
1274       return SANE_STATUS_INVAL;
1275     }
1276 
1277   chip->motor_enable = 0x00;
1278   if (is_enable)
1279     chip->motor_enable |= 0x80;
1280   data = chip->motor_enable | chip->motor_movement
1281     | chip->motor_direction | chip->motor_signal | chip->motor_home;
1282   reg_no = 15;
1283   RIE (usb_low_write_reg (chip, reg_no, data));
1284 
1285   DBG (7, "usb_low_enable_motor: exit\n");
1286   return SANE_STATUS_GOOD;
1287 }
1288 
1289 SANE_Status
usb_low_set_motor_movement(ma1017 * chip,SANE_Bool is_full_step,SANE_Bool is_double_phase,SANE_Bool is_two_step)1290 usb_low_set_motor_movement (ma1017 * chip, SANE_Bool is_full_step,
1291 			    SANE_Bool is_double_phase, SANE_Bool is_two_step)
1292 {
1293   SANE_Byte data, reg_no;
1294   SANE_Status status;
1295 
1296   DBG (7, "usb_low_set_motor_movement: start\n");
1297   if (!chip->is_opened)
1298     {
1299       DBG (3, "usb_low_set_motor_movement: not opened yet\n");
1300       return SANE_STATUS_INVAL;
1301     }
1302   if (chip->is_rowing)
1303     {
1304       DBG (3, "usb_low_set_motor_movement: stop rowing first\n");
1305       return SANE_STATUS_INVAL;
1306     }
1307 
1308   chip->motor_movement = 0x00;
1309   if (is_full_step)
1310     chip->motor_movement |= 0x40;
1311   if (is_double_phase)
1312     chip->motor_movement |= 0x20;
1313   if (is_two_step)
1314     chip->motor_movement |= 0x08;
1315   data = chip->motor_enable | chip->motor_movement
1316     | chip->motor_direction | chip->motor_signal | chip->motor_home;
1317   reg_no = 15;
1318   RIE (usb_low_write_reg (chip, reg_no, data));
1319 
1320   DBG (7, "usb_low_set_motor_movement:  exit\n");
1321   return SANE_STATUS_GOOD;
1322 }
1323 
1324 SANE_Status
usb_low_set_motor_direction(ma1017 * chip,SANE_Bool is_backward)1325 usb_low_set_motor_direction (ma1017 * chip, SANE_Bool is_backward)
1326 {
1327   SANE_Byte data, reg_no;
1328   SANE_Status status;
1329 
1330   DBG (7, "usb_low_set_motor_direction: start\n");
1331 
1332   if (!chip->is_opened)
1333     {
1334       DBG (3, "usb_low_set_motor_direction: not opened yet\n");
1335       return SANE_STATUS_INVAL;
1336     }
1337   if (chip->is_rowing)
1338     {
1339       DBG (3, "usb_low_set_motor_direction: stop rowing first\n");
1340       return SANE_STATUS_INVAL;
1341     }
1342 
1343   chip->motor_direction = 0x00;
1344   if (is_backward)
1345     chip->motor_direction |= 0x10;
1346   data = chip->motor_enable | chip->motor_movement
1347     | chip->motor_direction | chip->motor_signal | chip->motor_home;
1348   reg_no = 15;
1349   RIE (usb_low_write_reg (chip, reg_no, data));
1350 
1351   DBG (7, "usb_low_set_motor_direction: exit\n");
1352   return SANE_STATUS_GOOD;
1353 }
1354 
1355 SANE_Status
usb_low_set_motor_signal(ma1017 * chip,SANE_Byte signal)1356 usb_low_set_motor_signal (ma1017 * chip, SANE_Byte signal)
1357 {
1358   SANE_Byte data, reg_no;
1359   SANE_Status status;
1360 
1361   DBG (7, "usb_low_set_motor_signal: start\n");
1362 
1363   if (!chip->is_opened)
1364     {
1365       DBG (3, "usb_low_set_motor_signal: not opened yet\n");
1366       return SANE_STATUS_INVAL;
1367     }
1368   if (chip->is_rowing)
1369     {
1370       DBG (3, "usb_low_set_motor_signal: stop rowing first\n");
1371       return SANE_STATUS_INVAL;
1372     }
1373 
1374   chip->motor_signal = signal & 0x06;
1375   data = chip->motor_enable | chip->motor_movement
1376     | chip->motor_direction | chip->motor_signal | chip->motor_home;
1377   reg_no = 15;
1378   RIE (usb_low_write_reg (chip, reg_no, data));
1379 
1380   DBG (7, "usb_low_set_motor_signal: exit\n");
1381   return SANE_STATUS_GOOD;
1382 }
1383 
1384 SANE_Status
usb_low_move_motor_home(ma1017 * chip,SANE_Bool is_home,SANE_Bool is_backward)1385 usb_low_move_motor_home (ma1017 * chip, SANE_Bool is_home,
1386 			 SANE_Bool is_backward)
1387 {
1388   SANE_Byte data, reg_no;
1389   SANE_Status status;
1390 
1391   DBG (7, "usb_low_move_motor_home: start\n");
1392 
1393   if (!chip->is_opened)
1394     {
1395       DBG (3, "usb_low_move_motor_home: not opened yet\n");
1396       return SANE_STATUS_INVAL;
1397     }
1398   if (chip->is_rowing)
1399     {
1400       DBG (3, "usb_low_move_motor_home: stop rowing first\n");
1401       return SANE_STATUS_INVAL;
1402     }
1403 
1404   chip->motor_enable = 0x00;
1405   chip->motor_direction = 0x00;
1406   chip->motor_home = 0x00;
1407   if (is_backward)
1408     chip->motor_direction |= 0x10;
1409   if (is_home)
1410     {
1411       chip->motor_enable |= 0x80;
1412       chip->motor_home |= 0x01;
1413     }
1414   data = chip->motor_enable | chip->motor_movement
1415     | chip->motor_direction | chip->motor_signal | chip->motor_home;
1416   reg_no = 15;
1417   RIE (usb_low_write_reg (chip, reg_no, data));
1418 
1419   DBG (7, "usb_low_move_motor_home: exit\n");
1420   return SANE_STATUS_GOOD;
1421 }
1422 
1423 /* A16 */
1424 SANE_Status
usb_low_get_a16(ma1017 * chip,SANE_Byte * value)1425 usb_low_get_a16 (ma1017 * chip, SANE_Byte * value)
1426 {
1427   SANE_Byte pattern;
1428   SANE_Status status;
1429 
1430   DBG (7, "usb_low_get_a16: start\n");
1431 
1432   if (!chip->is_opened)
1433     {
1434       DBG (3, "usb_low_get_a16: not opened yet\n");
1435       return SANE_STATUS_INVAL;
1436     }
1437   if (chip->is_rowing)
1438     {
1439       DBG (3, "usb_low_get_a16: stop rowing first\n");
1440       return SANE_STATUS_INVAL;
1441     }
1442 
1443   RIE (usb_low_read_reg (chip, 16, &pattern));
1444 
1445   chip->pixel_depth = pattern & 0xe0;
1446   chip->image_invert = pattern & 0x10;
1447   chip->optical_600 = pattern & 0x08;
1448   chip->sample_way = pattern & 0x07;
1449 
1450   if (value)
1451     *value = pattern;
1452 
1453   DBG (7, "usb_low_get_a16: exit\n");
1454   return SANE_STATUS_GOOD;
1455 }
1456 
1457 SANE_Status
usb_low_set_image_dpi(ma1017 * chip,SANE_Bool is_optical600,Sampleway sampleway)1458 usb_low_set_image_dpi (ma1017 * chip, SANE_Bool is_optical600,
1459 		       Sampleway sampleway)
1460 {
1461   SANE_Byte data, reg_no;
1462   SANE_Status status;
1463 
1464   DBG (7, "usb_low_set_image_dpi: start\n");
1465 
1466   if (!chip->is_opened)
1467     {
1468       DBG (3, "usb_low_set_image_dpi: not opened yet\n");
1469       return SANE_STATUS_INVAL;
1470     }
1471   if (chip->is_rowing)
1472     {
1473       DBG (3, "usb_low_set_image_dpi: stop rowing first\n");
1474       return SANE_STATUS_INVAL;
1475     }
1476 
1477   chip->optical_600 = 0x00;
1478   chip->sample_way = 0x00;
1479   if (is_optical600)
1480     chip->optical_600 |= 0x08;
1481   switch (sampleway)
1482     {
1483     case SW_P1P6:
1484       chip->sample_way = 0x01;
1485       break;
1486     case SW_P2P6:
1487       chip->sample_way = 0x02;
1488       break;
1489     case SW_P3P6:
1490       chip->sample_way = 0x03;
1491       break;
1492     case SW_P4P6:
1493       chip->sample_way = 0x04;
1494       break;
1495     case SW_P5P6:
1496       chip->sample_way = 0x05;
1497       break;
1498     case SW_P6P6:
1499       chip->sample_way = 0x06;
1500       break;
1501     default:
1502       DBG (3, "usb_low_set_image_dpi: swsample_way error\n");
1503       return SANE_STATUS_INVAL;
1504       break;
1505     }
1506   data = chip->pixel_depth | chip->image_invert | chip->optical_600
1507     | chip->sample_way;
1508   reg_no = 16;
1509   RIE (usb_low_write_reg (chip, reg_no, data));
1510 
1511   DBG (7, "usb_low_set_image_dpi: exit\n");
1512   return SANE_STATUS_GOOD;
1513 }
1514 
1515 SANE_Status
usb_low_set_pixel_depth(ma1017 * chip,Pixeldepth pixeldepth)1516 usb_low_set_pixel_depth (ma1017 * chip, Pixeldepth pixeldepth)
1517 {
1518   SANE_Byte data, reg_no;
1519   SANE_Status status;
1520 
1521   DBG (7, "usb_low_set_pixel_depth: start\n");
1522   if (!chip->is_opened)
1523     {
1524       DBG (3, "usb_low_set_pixel_depth: not opened yet\n");
1525       return SANE_STATUS_INVAL;
1526     }
1527   if (chip->is_rowing)
1528     {
1529       DBG (3, "usb_low_set_pixel_depth: stop rowing first\n");
1530       return SANE_STATUS_INVAL;
1531     }
1532 
1533   chip->pixel_depth = 0x00;
1534   switch (pixeldepth)
1535     {
1536     case PD_1BIT:
1537       chip->pixel_depth = 0x80;
1538       break;
1539     case PD_4BIT:
1540       chip->pixel_depth = 0xc0;
1541       break;
1542     case PD_8BIT:
1543       chip->pixel_depth = 0x00;
1544       break;
1545     case PD_12BIT:
1546       chip->pixel_depth = 0x20;
1547       break;
1548     default:
1549       DBG (3, "usb_low_set_pixel_depth: pdPixelDepth error\n");
1550       return SANE_STATUS_INVAL;
1551     }
1552   data = chip->pixel_depth | chip->image_invert | chip->optical_600
1553     | chip->sample_way;
1554   reg_no = 16;
1555   RIE (usb_low_write_reg (chip, reg_no, data));
1556 
1557   DBG (7, "usb_low_SetPixelDeepth: exit\n");
1558   return SANE_STATUS_GOOD;
1559 }
1560 
1561 SANE_Status
usb_low_invert_image(ma1017 * chip,SANE_Bool is_invert)1562 usb_low_invert_image (ma1017 * chip, SANE_Bool is_invert)
1563 {
1564   SANE_Byte data, reg_no;
1565   SANE_Status status;
1566 
1567   DBG (7, "usb_low_invert_image: start\n");
1568   if (!chip->is_opened)
1569     {
1570       DBG (3, "usb_low_invert_image: not opened yet\n");
1571       return SANE_STATUS_INVAL;
1572     }
1573   if (chip->is_rowing)
1574     {
1575       DBG (3, "usb_low_invert_image: stop rowing first\n");
1576       return SANE_STATUS_INVAL;
1577     }
1578 
1579   chip->image_invert = 0x00;
1580   if (is_invert)
1581     chip->image_invert |= 0x10;
1582   data = chip->pixel_depth | chip->image_invert | chip->optical_600
1583     | chip->sample_way;
1584   reg_no = 16;
1585   RIE (usb_low_write_reg (chip, reg_no, data));
1586 
1587   DBG (7, "usb_low_invert_image: exit\n");
1588   return SANE_STATUS_GOOD;
1589 }
1590 
1591 /* A17 + A18 + A19 */
1592 SANE_Status
usb_low_get_a17(ma1017 * chip,SANE_Byte * value)1593 usb_low_get_a17 (ma1017 * chip, SANE_Byte * value)
1594 {
1595   SANE_Byte pattern;
1596   SANE_Status status;
1597 
1598   DBG (7, "usb_low_get_a17: start\n");
1599   if (!chip->is_opened)
1600     {
1601       DBG (3, "usb_low_get_a17: not opened yet\n");
1602       return SANE_STATUS_INVAL;
1603     }
1604   if (chip->is_rowing)
1605     {
1606       DBG (3, "usb_low_get_a17: stop rowing first\n");
1607       return SANE_STATUS_INVAL;
1608     }
1609 
1610   RIE (usb_low_read_reg (chip, 17, &pattern));
1611 
1612   chip->red_ref = pattern;
1613 
1614   if (value)
1615     *value = pattern;
1616 
1617   DBG (7, "usb_low_get_a17: exit\n");
1618   return SANE_STATUS_GOOD;
1619 }
1620 
1621 SANE_Status
usb_low_get_a18(ma1017 * chip,SANE_Byte * value)1622 usb_low_get_a18 (ma1017 * chip, SANE_Byte * value)
1623 {
1624   SANE_Byte pattern;
1625   SANE_Status status;
1626 
1627   DBG (7, "usb_low_get_a18: start\n");
1628   if (!chip->is_opened)
1629     {
1630       DBG (3, "usb_low_get_a18: not opened yet\n");
1631       return SANE_STATUS_INVAL;
1632     }
1633   if (chip->is_rowing)
1634     {
1635       DBG (3, "usb_low_get_a18: stop rowing first\n");
1636       return SANE_STATUS_INVAL;
1637     }
1638 
1639   RIE (usb_low_read_reg (chip, 18, &pattern));
1640 
1641   chip->green_ref = pattern;
1642 
1643   if (value)
1644     *value = pattern;
1645 
1646   DBG (7, "usb_low_get_a18: exit\n");
1647   return SANE_STATUS_GOOD;
1648 }
1649 
1650 SANE_Status
usb_low_get_a19(ma1017 * chip,SANE_Byte * value)1651 usb_low_get_a19 (ma1017 * chip, SANE_Byte * value)
1652 {
1653   SANE_Byte pattern;
1654   SANE_Status status;
1655 
1656   DBG (7, "usb_low_get_a19: start\n");
1657   if (!chip->is_opened)
1658     {
1659       DBG (3, "usb_low_get_a19: not opened yet\n");
1660       return SANE_STATUS_INVAL;
1661     }
1662   if (chip->is_rowing)
1663     {
1664       DBG (3, "usb_low_get_a19:stop rowing first\n");
1665       return SANE_STATUS_INVAL;
1666     }
1667 
1668   RIE (usb_low_read_reg (chip, 19, &pattern));
1669 
1670   chip->blue_ref = pattern;
1671 
1672   if (value)
1673     *value = pattern;
1674 
1675   DBG (7, "usb_low_get_a19: exit\n");
1676   return SANE_STATUS_GOOD;
1677 }
1678 
1679 SANE_Status
usb_low_set_red_ref(ma1017 * chip,SANE_Byte red_ref)1680 usb_low_set_red_ref (ma1017 * chip, SANE_Byte red_ref)
1681 {
1682   SANE_Byte data, reg_no;
1683   SANE_Status status;
1684 
1685   DBG (7, "usb_low_set_red_ref: start\n");
1686 
1687   if (!chip->is_opened)
1688     {
1689       DBG (3, "usb_low_set_red_ref: not opened yet\n");
1690       return SANE_STATUS_INVAL;
1691     }
1692   if (chip->is_rowing)
1693     {
1694       DBG (3, "usb_low_set_red_ref: stop rowing first\n");
1695       return SANE_STATUS_INVAL;
1696     }
1697 
1698   chip->red_ref = red_ref;
1699   data = red_ref;
1700   reg_no = 17;
1701   RIE (usb_low_write_reg (chip, reg_no, data));
1702 
1703   DBG (7, "usb_low_set_red_ref: stop\n");
1704   return SANE_STATUS_GOOD;
1705 }
1706 
1707 SANE_Status
usb_low_set_green_ref(ma1017 * chip,SANE_Byte green_ref)1708 usb_low_set_green_ref (ma1017 * chip, SANE_Byte green_ref)
1709 {
1710   SANE_Byte data, reg_no;
1711   SANE_Status status;
1712 
1713   DBG (7, "usb_low_set_green_ref: start\n");
1714 
1715 
1716   if (!chip->is_opened)
1717     {
1718       DBG (3, "usb_low_set_green_ref: not opened yet\n");
1719       return SANE_STATUS_INVAL;
1720     }
1721   if (chip->is_rowing)
1722     {
1723       DBG (3, "usb_low_set_green_ref: stop rowing first\n");
1724       return SANE_STATUS_INVAL;
1725     }
1726 
1727   chip->green_ref = green_ref;
1728 
1729   data = green_ref;
1730   reg_no = 18;
1731   RIE (usb_low_write_reg (chip, reg_no, data));
1732 
1733   DBG (7, "usb_low_set_green_ref: exit\n");
1734   return SANE_STATUS_GOOD;
1735 }
1736 
1737 SANE_Status
usb_low_set_blue_ref(ma1017 * chip,SANE_Byte blue_ref)1738 usb_low_set_blue_ref (ma1017 * chip, SANE_Byte blue_ref)
1739 {
1740   SANE_Byte data, reg_no;
1741   SANE_Status status;
1742 
1743   DBG (7, "usb_low_set_blue_ref: start\n");
1744 
1745   if (!chip->is_opened)
1746     {
1747       DBG (3, "usb_low_set_blue_ref: not opened yet\n");
1748       return SANE_STATUS_INVAL;
1749     }
1750   if (chip->is_rowing)
1751     {
1752       DBG (3, "usb_low_set_blue_ref: stop rowing first\n");
1753       return SANE_STATUS_INVAL;
1754     }
1755 
1756   chip->blue_ref = blue_ref;
1757 
1758   data = blue_ref;
1759   reg_no = 19;
1760   RIE (usb_low_write_reg (chip, reg_no, data));
1761 
1762   DBG (7, "usb_low_set_blue_ref: stop\n");
1763   return SANE_STATUS_GOOD;
1764 }
1765 
1766 /* A20 + A21 + A22 */
1767 SANE_Status
usb_low_get_a20(ma1017 * chip,SANE_Byte * value)1768 usb_low_get_a20 (ma1017 * chip, SANE_Byte * value)
1769 {
1770   SANE_Byte pattern;
1771   SANE_Status status;
1772 
1773   DBG (7, "usb_low_get_a20: start\n");
1774 
1775   if (!chip->is_opened)
1776     {
1777       DBG (3, "usb_low_get_a20: not opened yet\n");
1778       return SANE_STATUS_INVAL;
1779     }
1780   if (chip->is_rowing)
1781     {
1782       DBG (3, "usb_low_get_a20: stop rowing first\n");
1783       return SANE_STATUS_INVAL;
1784     }
1785   RIE (usb_low_read_reg (chip, 20, &pattern));
1786 
1787   chip->red_pd = pattern;
1788 
1789   if (value)
1790     *value = pattern;
1791 
1792   DBG (7, "usb_low_get_a20: stop\n");
1793   return SANE_STATUS_GOOD;
1794 }
1795 
1796 SANE_Status
usb_low_get_a21(ma1017 * chip,SANE_Byte * value)1797 usb_low_get_a21 (ma1017 * chip, SANE_Byte * value)
1798 {
1799   SANE_Byte pattern;
1800   SANE_Status status;
1801 
1802   DBG (7, "usb_low_get_a21: start\n");
1803 
1804   if (!chip->is_opened)
1805     {
1806       DBG (3, "usb_low_get_a21: not opened yet\n");
1807       return SANE_STATUS_INVAL;
1808     }
1809   if (chip->is_rowing)
1810     {
1811       DBG (3, "usb_low_get_a21: stop rowing first\n");
1812       return SANE_STATUS_INVAL;
1813     }
1814 
1815   RIE (usb_low_read_reg (chip, 21, &pattern));
1816 
1817   chip->green_pd = pattern;
1818 
1819   if (value)
1820     *value = pattern;
1821 
1822   DBG (7, "usb_low_get_a21: exit\n");
1823   return SANE_STATUS_GOOD;
1824 }
1825 
1826 SANE_Status
usb_low_get_a22(ma1017 * chip,SANE_Byte * value)1827 usb_low_get_a22 (ma1017 * chip, SANE_Byte * value)
1828 {
1829   SANE_Byte pattern;
1830   SANE_Status status;
1831 
1832   DBG (7, "usb_low_get_a22: start\n");
1833 
1834   if (!chip->is_opened)
1835     {
1836       DBG (3, "usb_low_get_a22: not opened yet\n");
1837       return SANE_STATUS_INVAL;
1838     }
1839   if (chip->is_rowing)
1840     {
1841       DBG (3, "usb_low_get_a22: stop rowing first\n");
1842       return SANE_STATUS_INVAL;
1843     }
1844 
1845   RIE (usb_low_read_reg (chip, 22, &pattern));
1846 
1847   chip->blue_pd = pattern;
1848 
1849   if (value)
1850     *value = pattern;
1851 
1852   DBG (7, "usb_low_get_a22: exit\n");
1853   return SANE_STATUS_GOOD;
1854 }
1855 
1856 SANE_Status
usb_low_set_red_pd(ma1017 * chip,SANE_Byte red_pd)1857 usb_low_set_red_pd (ma1017 * chip, SANE_Byte red_pd)
1858 {
1859   SANE_Byte data, reg_no;
1860   SANE_Status status;
1861 
1862   DBG (7, "usb_low_set_red_pd: start\n");
1863 
1864   if (!chip->is_opened)
1865     {
1866       DBG (3, "usb_low_set_red_pd: not opened yet\n");
1867       return SANE_STATUS_INVAL;
1868     }
1869   if (chip->is_rowing)
1870     {
1871       DBG (3, "usb_low_set_red_pd: stop rowing first\n");
1872       return SANE_STATUS_INVAL;
1873     }
1874 
1875   chip->red_pd = red_pd;
1876 
1877   data = chip->red_pd;
1878   reg_no = 20;
1879   RIE (usb_low_write_reg (chip, reg_no, data));
1880 
1881   DBG (7, "usb_low_set_red_pd: exit\n");
1882   return SANE_STATUS_GOOD;
1883 }
1884 
1885 SANE_Status
usb_low_set_green_pd(ma1017 * chip,SANE_Byte green_pd)1886 usb_low_set_green_pd (ma1017 * chip, SANE_Byte green_pd)
1887 {
1888   SANE_Byte data, reg_no;
1889   SANE_Status status;
1890 
1891   DBG (7, "usb_low_set_green_pd: start\n");
1892 
1893   if (!chip->is_opened)
1894     {
1895       DBG (3, "usb_low_set_green_pd: not opened yet\n");
1896       return SANE_STATUS_INVAL;
1897     }
1898   if (chip->is_rowing)
1899     {
1900       DBG (3, "usb_low_set_green_pd: stop rowing first\n");
1901       return SANE_STATUS_INVAL;
1902     }
1903 
1904   chip->green_pd = green_pd;
1905   data = chip->green_pd;
1906   reg_no = 21;
1907   RIE (usb_low_write_reg (chip, reg_no, data));
1908 
1909   DBG (7, "usb_low_set_green_pd: exit\n");
1910   return SANE_STATUS_GOOD;
1911 }
1912 
1913 SANE_Status
usb_low_set_blue_pd(ma1017 * chip,SANE_Byte blue_pd)1914 usb_low_set_blue_pd (ma1017 * chip, SANE_Byte blue_pd)
1915 {
1916   SANE_Byte data, reg_no;
1917   SANE_Status status;
1918 
1919   DBG (7, "usb_low_set_blue_pd: start\n");
1920 
1921   if (!chip->is_opened)
1922     {
1923       DBG (3, "usb_low_set_blue_pd: not opened yet\n");
1924       return SANE_STATUS_INVAL;
1925     }
1926   if (chip->is_rowing)
1927     {
1928       DBG (3, "usb_low_set_blue_pd: stop rowing first\n");
1929       return SANE_STATUS_INVAL;
1930     }
1931 
1932   chip->blue_pd = blue_pd;
1933 
1934   data = chip->blue_pd;
1935   reg_no = 22;
1936   RIE (usb_low_write_reg (chip, reg_no, data));
1937 
1938   DBG (7, "usb_low_set_blue_pd: exit\n");
1939   return SANE_STATUS_GOOD;
1940 }
1941 
1942 /* A23 */
1943 SANE_Status
usb_low_get_a23(ma1017 * chip,SANE_Byte * value)1944 usb_low_get_a23 (ma1017 * chip, SANE_Byte * value)
1945 {
1946   SANE_Byte pattern;
1947   SANE_Status status;
1948 
1949   DBG (7, "usb_low_get_a23: start\n");
1950   if (!chip->is_opened)
1951     {
1952       DBG (3, "usb_low_get_a23: not opened yet\n");
1953       return SANE_STATUS_INVAL;
1954     }
1955   if (chip->is_rowing)
1956     {
1957       DBG (3, "usb_low_get_a23: stop rowing first\n");
1958       return SANE_STATUS_INVAL;
1959     }
1960 
1961   RIE (usb_low_read_reg (chip, 23, &pattern));
1962 
1963   chip->a23 = pattern;
1964 
1965   if (value)
1966     *value = pattern;
1967 
1968   DBG (7, "usb_low_get_a23: exit\n");
1969   return SANE_STATUS_GOOD;
1970 }
1971 
1972 SANE_Status
usb_low_turn_peripheral_power(ma1017 * chip,SANE_Bool is_on)1973 usb_low_turn_peripheral_power (ma1017 * chip, SANE_Bool is_on)
1974 {
1975   SANE_Byte data, reg_no;
1976   SANE_Status status;
1977 
1978   DBG (7, "usb_low_turn_peripheral_power: start\n");
1979 
1980   if (!chip->is_opened)
1981     {
1982       DBG (3, "usb_low_turn_peripheral_power: not opened yet\n");
1983       return SANE_STATUS_INVAL;
1984     }
1985   if (chip->is_rowing)
1986     {
1987       DBG (3, "usb_low_turn_peripheral_power: stop rowing first\n");
1988       return SANE_STATUS_INVAL;
1989     }
1990 
1991   chip->a23 &= 0x7f;
1992   if (is_on)
1993     chip->a23 |= 0x80;
1994   data = chip->a23;
1995   reg_no = 23;
1996   RIE (usb_low_write_reg (chip, reg_no, data));
1997 
1998   DBG (7, "usb_low_turn_peripheral_power: exit\n");
1999   return SANE_STATUS_GOOD;
2000 }
2001 
2002 SANE_Status
usb_low_turn_lamp_power(ma1017 * chip,SANE_Bool is_on)2003 usb_low_turn_lamp_power (ma1017 * chip, SANE_Bool is_on)
2004 {
2005   SANE_Byte data, reg_no;
2006   SANE_Status status;
2007 
2008   DBG (7, "usb_low_turn_lamp_power: start\n");
2009 
2010   if (!chip->is_opened)
2011     {
2012       DBG (3, "usb_low_turn_lamp_power: not opened yet\n");
2013       return SANE_STATUS_INVAL;
2014     }
2015   if (chip->is_rowing)
2016     {
2017       DBG (3, "usb_low_turn_lamp_power: stop rowing first\n");
2018       return SANE_STATUS_INVAL;
2019     }
2020 
2021   chip->a23 &= 0xbf;
2022   if (is_on)
2023     chip->a23 |= 0x40;
2024 
2025   data = chip->a23;
2026   reg_no = 23;
2027   RIE (usb_low_write_reg (chip, reg_no, data));
2028 
2029   DBG (7, "usb_low_turn_lamp_power: exit\n");
2030   return SANE_STATUS_GOOD;
2031 }
2032 
2033 SANE_Status
usb_low_set_io_3(ma1017 * chip,SANE_Bool is_high)2034 usb_low_set_io_3 (ma1017 * chip, SANE_Bool is_high)
2035 {
2036   SANE_Byte data, reg_no;
2037   SANE_Status status;
2038 
2039   DBG (7, "usb_low_set_io_3: start\n");
2040 
2041   if (!chip->is_opened)
2042     {
2043       DBG (3, "usb_low_set_io_3: not opened yet\n");
2044       return SANE_STATUS_INVAL;
2045     }
2046   if (chip->is_rowing)
2047     {
2048       DBG (3, "usb_low_set_io_3: stop rowing first\n");
2049       return SANE_STATUS_INVAL;
2050     }
2051 
2052   chip->a23 &= 0xf7;
2053   if (is_high)
2054     chip->a23 |= 0x08;
2055 
2056   data = chip->a23;
2057   reg_no = 23;
2058   RIE (usb_low_write_reg (chip, reg_no, data));
2059 
2060   DBG (7, "usb_low_set_io_3: exit\n");
2061   return SANE_STATUS_GOOD;
2062 }
2063 
2064 SANE_Status
usb_low_set_led_light_all(ma1017 * chip,SANE_Bool is_light_all)2065 usb_low_set_led_light_all (ma1017 * chip, SANE_Bool is_light_all)
2066 {
2067   SANE_Byte data, reg_no;
2068   SANE_Status status;
2069 
2070   DBG (7, "usb_low_set_led_light_all: start\n");
2071 
2072   if (!chip->is_opened)
2073     {
2074       DBG (3, "usb_low_set_led_light_all: not opened yet\n");
2075       return SANE_STATUS_INVAL;
2076     }
2077   if (chip->is_rowing)
2078     {
2079       DBG (3, "usb_low_set_led_light_all: stop rowing first\n");
2080       return SANE_STATUS_INVAL;
2081     }
2082 
2083   chip->a23 &= 0xfe;
2084   if (is_light_all)
2085     chip->a23 |= 0x01;
2086 
2087   data = chip->a23;
2088   reg_no = 23;
2089   RIE (usb_low_write_reg (chip, reg_no, data));
2090 
2091   DBG (7, "usb_low_set_led_light_all: exit\n");
2092   return SANE_STATUS_GOOD;
2093 }
2094 
2095 /* A24 */
2096 SANE_Status
usb_low_get_a24(ma1017 * chip,SANE_Byte * value)2097 usb_low_get_a24 (ma1017 * chip, SANE_Byte * value)
2098 {
2099   SANE_Byte pattern;
2100   SANE_Status status;
2101 
2102   DBG (7, "usb_low_get_a24: start\n");
2103 
2104   if (!chip->is_opened)
2105     {
2106       DBG (3, "usb_low_get_a24: not opened yet\n");
2107       return SANE_STATUS_INVAL;
2108     }
2109   if (chip->is_rowing)
2110     {
2111       DBG (3, "usb_low_get_a24: stop rowing first\n");
2112       return SANE_STATUS_INVAL;
2113     }
2114 
2115   RIE (usb_low_read_reg (chip, 24, &pattern));
2116 
2117   chip->fy1_delay = pattern & 0x01;
2118   chip->special_ad = pattern & 0x02;
2119 
2120   if (value)
2121     *value = pattern;
2122 
2123   DBG (7, "usb_low_get_a24: exit\n");
2124   return SANE_STATUS_GOOD;
2125 }
2126 
2127 SANE_Status
usb_low_set_ad_timing(ma1017 * chip,SANE_Byte data)2128 usb_low_set_ad_timing (ma1017 * chip, SANE_Byte data)
2129 {
2130   SANE_Byte reg_no;
2131   SANE_Status status;
2132 
2133   DBG (7, "usb_low_set_ad_timing: start\n");
2134 
2135   if (!chip->is_opened)
2136     {
2137       DBG (3, "usb_low_set_ad_timing: not opened yet\n");
2138       return SANE_STATUS_INVAL;
2139     }
2140   if (chip->is_rowing)
2141     {
2142       DBG (3, "usb_low_set_ad_timing: stop rowing first\n");
2143       return SANE_STATUS_INVAL;
2144     }
2145 
2146   chip->fy1_delay = data & 0x01;
2147   chip->special_ad = data & 0x02;
2148 
2149   data = chip->special_ad | chip->fy1_delay;
2150   reg_no = 24;
2151   RIE (usb_low_write_reg (chip, reg_no, data));
2152 
2153   DBG (7, "usb_low_set_ad_timing: exit\n");
2154   return SANE_STATUS_GOOD;
2155 }
2156 
2157 /* A25 + A26 */
2158 SANE_Status
usb_low_set_serial_byte1(ma1017 * chip,SANE_Byte data)2159 usb_low_set_serial_byte1 (ma1017 * chip, SANE_Byte data)
2160 {
2161   SANE_Byte reg_no;
2162   SANE_Status status;
2163 
2164   DBG (7, "usb_low_set_serial_byte1: start\n");
2165 
2166   if (!chip->is_opened)
2167     {
2168       DBG (3, "usb_low_set_serial_byte1: not opened\n");
2169       return SANE_STATUS_INVAL;
2170     }
2171   if (chip->is_rowing)
2172     {
2173       DBG (3, "usb_low_set_serial_byte1: stop rowing first\n");
2174       return SANE_STATUS_INVAL;
2175     }
2176 
2177   reg_no = 25;
2178   RIE (usb_low_write_reg (chip, reg_no, data));
2179 
2180   DBG (7, "usb_low_set_serial_byte1: exit\n");
2181   return SANE_STATUS_GOOD;
2182 }
2183 
2184 SANE_Status
usb_low_set_serial_byte2(ma1017 * chip,SANE_Byte data)2185 usb_low_set_serial_byte2 (ma1017 * chip, SANE_Byte data)
2186 {
2187   SANE_Byte reg_no;
2188   SANE_Status status;
2189 
2190   DBG (7, "usb_low_set_serial_byte2: start\n");
2191 
2192   if (!chip->is_opened)
2193     {
2194       DBG (3, "usb_low_set_serial_byte2: not opened yet\n");
2195       return SANE_STATUS_INVAL;
2196     }
2197   if (chip->is_rowing)
2198     {
2199       DBG (3, "usb_low_set_serial_byte2: stop rowing first\n");
2200       return SANE_STATUS_INVAL;
2201     }
2202 
2203   reg_no = 26;
2204   RIE (usb_low_write_reg (chip, reg_no, data));
2205 
2206   DBG (7, "usb_low_set_serial_byte2: exit\n");
2207   return SANE_STATUS_GOOD;
2208 }
2209 
2210 /* A27 */
2211 SANE_Status
usb_low_get_a27(ma1017 * chip,SANE_Byte * value)2212 usb_low_get_a27 (ma1017 * chip, SANE_Byte * value)
2213 {
2214   SANE_Byte pattern;
2215   SANE_Status status;
2216 
2217   DBG (7, "usb_low_get_a27: start\n");
2218 
2219   if (!chip->is_opened)
2220     {
2221       DBG (3, "usb_low_get_a27: not opened yet\n");
2222       return SANE_STATUS_INVAL;
2223     }
2224   if (chip->is_rowing)
2225     {
2226       DBG (3, "usb_low_get_a27: stop rowing first\n");
2227       return SANE_STATUS_INVAL;
2228     }
2229 
2230   RIE (usb_low_read_reg (chip, 27, &pattern));
2231 
2232   chip->sclk = pattern & 0x80;
2233   chip->sen = pattern & 0x40;
2234   chip->serial_length = pattern & 0x1f;
2235 
2236   if (value)
2237     *value = pattern;
2238 
2239   DBG (7, "usb_low_get_a27: exit\n");
2240   return SANE_STATUS_GOOD;
2241 }
2242 
2243 SANE_Status
usb_low_set_serial_format(ma1017 * chip,SANE_Byte data)2244 usb_low_set_serial_format (ma1017 * chip, SANE_Byte data)
2245 {
2246   SANE_Byte reg_no;
2247   SANE_Status status;
2248 
2249   DBG (7, "usb_low_set_serial_format: start\n");
2250 
2251   if (!chip->is_opened)
2252     {
2253       DBG (3, "usb_low_set_serial_format: not opened yet\n");
2254       return SANE_STATUS_INVAL;
2255     }
2256   if (chip->is_rowing)
2257     {
2258       DBG (3, "usb_low_set_serial_format: stop rowing first\n");
2259       return SANE_STATUS_INVAL;
2260     }
2261 
2262 
2263   chip->sclk = data & 0x80;
2264   chip->sen = data & 0x40;
2265   chip->serial_length = data & 0x1f;
2266 
2267   reg_no = 27;
2268   RIE (usb_low_write_reg (chip, reg_no, data));
2269 
2270   DBG (7, "usb_low_set_serial_format: exit\n");
2271   return SANE_STATUS_GOOD;
2272 }
2273 
2274 SANE_Status
usb_low_get_home_sensor(ma1017 * chip)2275 usb_low_get_home_sensor (ma1017 * chip)
2276 {
2277   SANE_Byte data;
2278   SANE_Status status;
2279 
2280   DBG (7, "usb_low_get_home_sensor: start\n");
2281 
2282   if (!chip->is_opened)
2283     {
2284       DBG (3, "usb_low_get_home_sensor: not opened yet\n");
2285       return SANE_STATUS_INVAL;
2286     }
2287   if (chip->is_rowing)
2288     {
2289       DBG (3, "usb_low_get_home_sensor: stop rowing first\n");
2290       return SANE_STATUS_INVAL;
2291     }
2292 
2293   RIE (usb_low_read_reg (chip, 31, &data));
2294 
2295   DBG (7, "usb_low_get_home_sensor: exit\n");
2296   if ((data & 0x80) != 0)
2297     return SANE_STATUS_GOOD;
2298   else
2299     return SANE_STATUS_IO_ERROR;
2300 }
2301 
2302 /* Special Mode */
2303 SANE_Status
usb_low_start_rowing(ma1017 * chip)2304 usb_low_start_rowing (ma1017 * chip)
2305 {
2306   SANE_Word line_of_first = 0;
2307   SANE_Word line_of_second = 0;
2308   SANE_Int i;
2309   SANE_Status status;
2310 
2311   DBG (7, "usb_low_start_rowing: start\n");
2312 
2313   if (chip->loop_count == 0)
2314     {
2315       DBG (3, "usb_low_start_rowing loop_count hasn't been set yet\n");
2316       return SANE_STATUS_INVAL;
2317     }
2318   if (chip->cmt_table_length_word == 0)
2319     {
2320       DBG (3, "usb_low_start_rowing: cmt_table_length_word hasn't been set "
2321 	   "yet\n");
2322       return SANE_STATUS_INVAL;
2323     }
2324   if (chip->cmt_table_length_word <= chip->cmt_second_pos_word)
2325     {
2326       DBG (3, "usb_low_start_rowing: cmt_second_pos_word cannot be larger "
2327 	   "than cmt_table_length_word\n");
2328       return SANE_STATUS_INVAL;
2329     }
2330 
2331   for (i = 0; i < (int) chip->cmt_second_pos_word; i++)
2332     {
2333       if (chip->is_transfer_table[i])
2334 	line_of_first++;
2335     }
2336   for (; i < (int) chip->cmt_table_length_word; i++)
2337     {
2338       if (chip->is_transfer_table[i])
2339 	{
2340 	  line_of_first++;
2341 	  line_of_second++;
2342 	}
2343     }
2344 
2345   chip->total_lines =
2346     ((SANE_Word) (chip->loop_count - 1)) * line_of_second + line_of_first;
2347   chip->lines_left = chip->total_lines;
2348 
2349   RIE (usb_low_start_cmt_table (chip));
2350 
2351   DBG (7, "usb_low_start_rowing: exit\n");
2352   return SANE_STATUS_GOOD;
2353 }
2354 
2355 SANE_Status
usb_low_stop_rowing(ma1017 * chip)2356 usb_low_stop_rowing (ma1017 * chip)
2357 {
2358   SANE_Status status;
2359 
2360   DBG (7, "usb_low_stop_rowing: start\n");
2361 
2362   RIE (usb_low_stop_cmt_table (chip));
2363 
2364   DBG (7, "usb_low_stop_rowing: exit\n");
2365   return SANE_STATUS_GOOD;
2366 
2367 }
2368 
2369 SANE_Status
usb_low_wait_rowing_stop(ma1017 * chip)2370 usb_low_wait_rowing_stop (ma1017 * chip)
2371 {
2372   SANE_Status status;
2373 
2374   DBG (7, "usb_low_wait_rowing_stop: start\n");
2375   if (chip->total_lines != 0)
2376     {
2377       DBG (3, "usb_low_wait_rowing_stop: total_lines must be 0\n");
2378       return SANE_STATUS_INVAL;
2379     }
2380 
2381   RIE (usb_low_wait_rowing (chip));
2382   chip->is_rowing = SANE_FALSE;
2383   DBG (7, "usb_low_wait_rowing_stop: exit\n");
2384   return SANE_STATUS_GOOD;
2385 }
2386 
2387 SANE_Status
usb_low_read_all_registers(ma1017 * chip)2388 usb_low_read_all_registers (ma1017 * chip)
2389 {
2390   SANE_Status status;
2391 
2392   DBG (7, "usb_low_read_all_registers: start\n");
2393 
2394   RIE (usb_low_get_a2 (chip, 0));
2395   RIE (usb_low_get_a4 (chip, 0));
2396   RIE (usb_low_get_a6 (chip, 0));
2397   RIE (usb_low_get_a7 (chip, 0));
2398   RIE (usb_low_get_a8 (chip, 0));
2399   RIE (usb_low_get_a9 (chip, 0));
2400   RIE (usb_low_get_a10 (chip, 0));
2401   RIE (usb_low_get_a11 (chip, 0));
2402   RIE (usb_low_get_a12 (chip, 0));
2403   RIE (usb_low_get_a13 (chip, 0));
2404   RIE (usb_low_get_a15 (chip, 0));
2405   RIE (usb_low_get_a16 (chip, 0));
2406   RIE (usb_low_get_a17 (chip, 0));
2407   RIE (usb_low_get_a18 (chip, 0));
2408   RIE (usb_low_get_a19 (chip, 0));
2409   RIE (usb_low_get_a20 (chip, 0));
2410   RIE (usb_low_get_a21 (chip, 0));
2411   RIE (usb_low_get_a22 (chip, 0));
2412   RIE (usb_low_get_a23 (chip, 0));
2413   RIE (usb_low_get_a24 (chip, 0));
2414   RIE (usb_low_get_a27 (chip, 0));
2415 
2416   return SANE_STATUS_GOOD;
2417   DBG (7, "usb_low_read_all_registers: exit\n");
2418 }
2419 
2420 SANE_Status
usb_low_get_row(ma1017 * chip,SANE_Byte * data,SANE_Word * lines_left)2421 usb_low_get_row (ma1017 * chip, SANE_Byte * data, SANE_Word * lines_left)
2422 {
2423   SANE_Status status;
2424 
2425   DBG (7, "usb_low_get_row: start\n");
2426   RIE ((*chip->get_row) (chip, data, lines_left));
2427   DBG (7, "usb_low_get_row: exit\n");
2428   return SANE_STATUS_GOOD;;
2429 }
2430 
2431 SANE_Status
usb_low_get_row_direct(ma1017 * chip,SANE_Byte * data,SANE_Word * lines_left)2432 usb_low_get_row_direct (ma1017 * chip, SANE_Byte * data,
2433 			SANE_Word * lines_left)
2434 {
2435   SANE_Status status;
2436 
2437   DBG (7, "usb_low_get_row_direct: start\n");
2438   if (chip->lines_left == 0)
2439     {
2440       DBG (3, "usb_low_get_row_direct: lines_left == 0\n");
2441       return SANE_STATUS_INVAL;
2442     }
2443 
2444   if (chip->lines_left <= 1)
2445     {
2446       RIE (usb_low_read_rows (chip, data, chip->byte_width));
2447       RIE (usb_low_wait_rowing (chip));
2448 
2449       chip->lines_left = 0x00;
2450       chip->is_rowing = SANE_FALSE;
2451       *lines_left = 0;
2452     }
2453   else
2454     {
2455       RIE (usb_low_read_rows (chip, data, chip->byte_width));
2456       chip->lines_left--;
2457       *lines_left = chip->lines_left;
2458     }
2459   DBG (7, "usb_low_get_row_direct: exit\n");
2460   return SANE_STATUS_GOOD;
2461 }
2462 
2463 SANE_Status
usb_low_get_row_resample(ma1017 * chip,SANE_Byte * data,SANE_Word * lines_left)2464 usb_low_get_row_resample (ma1017 * chip, SANE_Byte * data,
2465 			  SANE_Word * lines_left)
2466 {
2467   static SANE_Byte resample_buffer[8 * 1024];
2468   SANE_Word *pixel_temp;
2469   SANE_Word i;
2470   SANE_Word j;
2471   SANE_Word k;
2472   SANE_Status status;
2473 
2474   DBG (7, "usb_low_get_row_resample: start\n");
2475 
2476   if (chip->lines_left == 0)
2477     {
2478       DBG (3, "usb_low_get_row_resample: lines_left == 0\n");
2479       return SANE_STATUS_INVAL;
2480     }
2481 
2482   if (chip->lines_left <= 1)
2483     {
2484       RIE (usb_low_read_rows (chip, resample_buffer, chip->byte_width));
2485 
2486       if ((chip->sensor == ST_CANON600) && (chip->pixel_depth == 0x20))
2487 	{
2488 	  pixel_temp = (SANE_Word *) malloc (6 * 1024 * sizeof (SANE_Word));
2489 	  if (!pixel_temp)
2490 	    return SANE_STATUS_NO_MEM;
2491 
2492 	  j = 0;
2493 	  for (i = 0; i < chip->byte_width; i += 3)
2494 	    {
2495 	      pixel_temp[j] = (SANE_Word) resample_buffer[i];
2496 	      pixel_temp[j] |= ((SANE_Word)
2497 				(resample_buffer[i + 1] & 0xf0)) << 4;
2498 	      j++;
2499 	      pixel_temp[j] = ((SANE_Word)
2500 			       (resample_buffer[i + 1] & 0x0f)) << 8;
2501 	      pixel_temp[j] |= (SANE_Word) resample_buffer[i + 2];
2502 	      j++;
2503 	    }
2504 
2505 	  k = 0;
2506 	  for (i = 0; i < j; i += chip->soft_resample * 2)
2507 	    {
2508 	      data[k] = (SANE_Byte) (pixel_temp[i] & 0x00ff);
2509 	      k++;
2510 	      data[k] = (SANE_Byte) ((pixel_temp[i] & 0x0f00) >> 4);
2511 	      data[k] |= (SANE_Byte) ((pixel_temp[i + 2] & 0x0f00) >> 8);
2512 	      k++;
2513 	      data[k] = (SANE_Byte) (pixel_temp[i + 2] & 0x00ff);
2514 	      k++;
2515 	    }
2516 	  free (pixel_temp);
2517 	}
2518       else			/* fixme ? */
2519 	{
2520 	  for (i = 0; i < chip->byte_width; i += chip->soft_resample)
2521 	    *(data++) = resample_buffer[i];
2522 	}
2523       RIE (usb_low_wait_rowing (chip));
2524 
2525       chip->lines_left = 0x00;
2526       chip->is_rowing = SANE_FALSE;
2527       *lines_left = 0;
2528     }
2529   else
2530     {
2531       RIE (usb_low_read_rows (chip, resample_buffer, chip->byte_width));
2532 
2533       if ((chip->sensor == ST_CANON600) && (chip->pixel_depth == 0x20))
2534 	{
2535 	  pixel_temp = (SANE_Word *) malloc (6 * 1024 * sizeof (SANE_Word));
2536 	  if (!pixel_temp)
2537 	    return SANE_STATUS_NO_MEM;
2538 
2539 	  j = 0;
2540 	  for (i = 0; i < chip->byte_width; i += 3)
2541 	    {
2542 	      pixel_temp[j] = (SANE_Word) resample_buffer[i];
2543 	      pixel_temp[j] |=
2544 		((SANE_Word) (resample_buffer[i + 1] & 0xf0)) << 4;
2545 	      j++;
2546 	      pixel_temp[j] =
2547 		((SANE_Word) (resample_buffer[i + 1] & 0x0f)) << 8;
2548 	      pixel_temp[j] |= (SANE_Word) resample_buffer[i + 2];
2549 	      j++;
2550 	    }
2551 
2552 	  k = 0;
2553 	  for (i = 0; i < j; i += chip->soft_resample * 2)
2554 	    {
2555 	      data[k] = (SANE_Byte) (pixel_temp[i] & 0x00ff);
2556 	      k++;
2557 	      data[k] = (SANE_Byte) ((pixel_temp[i] & 0x0f00) >> 4);
2558 	      data[k] |= (SANE_Byte) ((pixel_temp[i + 2] & 0x0f00) >> 8);
2559 	      k++;
2560 	      data[k] = (SANE_Byte) (pixel_temp[i + 2] & 0x00ff);
2561 	      k++;
2562 	    }
2563 	  free (pixel_temp);
2564 	}
2565       else			/* fixme? */
2566 	{
2567 	  for (i = 0; i < chip->byte_width; i += chip->soft_resample)
2568 	    *(data++) = resample_buffer[i];
2569 	}
2570       chip->lines_left--;
2571       *lines_left = chip->lines_left;
2572     }
2573   DBG (7, "usb_low_get_row_resample: exit\n");
2574   return SANE_STATUS_GOOD;
2575 }
2576 
2577 SANE_Status
usb_low_wait_rowing(ma1017 * chip)2578 usb_low_wait_rowing (ma1017 * chip)
2579 {
2580   SANE_Byte read_byte;
2581   size_t n;
2582   SANE_Status status;
2583 
2584   DBG (7, "usb_low_wait_rowing: start\n");
2585 
2586   if (!chip->is_opened)
2587     {
2588       DBG (3, "usb_low_wait_rowing: open first\n");
2589       return SANE_STATUS_INVAL;
2590     }
2591   if (!chip->is_rowing)
2592     {
2593       DBG (3, "usb_low_wait_rowing: not rowing\n");
2594       return SANE_STATUS_INVAL;
2595     }
2596   n = 1;
2597   status = sanei_usb_read_bulk (chip->fd, (SANE_Byte *) & read_byte, &n);
2598   if (status != SANE_STATUS_GOOD || n != 1)
2599     {
2600       DBG (3, "usb_low_wait_rowing: couldn't read: %s\n",
2601 	   sane_strstatus (status));
2602       return SANE_STATUS_IO_ERROR;
2603     }
2604   chip->total_read_urbs++;
2605   chip->is_rowing = SANE_FALSE;
2606   DBG (7, "usb_low_wait_rowing: exit\n");
2607   return SANE_STATUS_GOOD;
2608 }
2609 
2610 SANE_Status
usb_low_read_rows(ma1017 * chip,SANE_Byte * data,SANE_Word byte_count)2611 usb_low_read_rows (ma1017 * chip, SANE_Byte * data, SANE_Word byte_count)
2612 {
2613   size_t n, bytes_total;
2614   SANE_Status status;
2615 
2616   DBG (7, "usb_low_read_rows: start\n");
2617   if (!(chip->is_opened))
2618     {
2619       DBG (3, "usb_low_read_rows: is_opened==SANE_FALSE\n");
2620       return SANE_STATUS_INVAL;
2621     }
2622   if (!(chip->is_rowing))
2623     {
2624       DBG (3, "usb_low_read_rows: is_rowing==SANE_FALSE\n");
2625       return SANE_STATUS_INVAL;
2626     }
2627 
2628   n = MIN (byte_count, chip->max_block_size);
2629   bytes_total = 0;
2630 
2631   while ((SANE_Word) bytes_total < byte_count)
2632     {
2633       status =
2634 	sanei_usb_read_bulk (chip->fd, (SANE_Byte *) (data + bytes_total),
2635 			     &n);
2636       if (status != SANE_STATUS_GOOD)
2637 	{
2638 	  DBG (7, "usb_low_read_rows: problems during read: %s -- exiting\n",
2639 	       sane_strstatus (status));
2640 	  return status;
2641 	}
2642       /* Count the number of URBs. This is a bit tricky, as we are reading
2643 	 bigger chunks here but the scanner can only handle 64 bytes at once. */
2644       chip->total_read_urbs += ((n +  63) / 64);
2645       bytes_total += n;
2646       if ((SANE_Word) bytes_total != byte_count)
2647 	{
2648 	  DBG (7, "usb_low_read_rows:  wanted %d, got %d "
2649 	       "bytes (%d in total) -- retrying\n", byte_count, (SANE_Word) n,
2650 	       (SANE_Word) bytes_total);
2651 	}
2652       n = MIN ((byte_count - (SANE_Word) bytes_total), chip->max_block_size);
2653     }
2654 
2655   DBG (7, "usb_low_read_rows: exit, read %d bytes\n",
2656        (SANE_Word) bytes_total);
2657   return SANE_STATUS_GOOD;
2658 }
2659 
2660 
2661 SANE_Status
usb_low_write_reg(ma1017 * chip,SANE_Byte reg_no,SANE_Byte data)2662 usb_low_write_reg (ma1017 * chip, SANE_Byte reg_no, SANE_Byte data)
2663 {
2664   size_t n;
2665   SANE_Status status;
2666   SANE_Byte data_field[2];
2667 
2668   data_field[0] = data;
2669   data_field[1] = reg_no;
2670 
2671   if (!chip->is_opened)
2672     {
2673       DBG (3, "usb_low_write_reg: open first\n");
2674       return SANE_STATUS_INVAL;
2675     }
2676   if (chip->is_rowing)
2677     {
2678       DBG (3, "usb_low_write_reg: rowing, stop first\n");
2679       return SANE_STATUS_INVAL;
2680     }
2681   if (reg_no > 0x20)
2682     {
2683       DBG (3, "usb_low_write_reg: reg_no out of range\n");
2684       return SANE_STATUS_INVAL;
2685     }
2686   n = 2;
2687   status = sanei_usb_write_bulk (chip->fd, data_field, &n);
2688   if (status != SANE_STATUS_GOOD || n != 2)
2689     {
2690       DBG (3, "usb_low_write_reg: couldn't write, tried to write %d, "
2691 	   "wrote %lu: %s\n", 2, (unsigned long int) n,
2692 	   sane_strstatus (status));
2693       return SANE_STATUS_IO_ERROR;
2694     }
2695   chip->total_write_urbs++;
2696   DBG (7, "usb_low_write_reg: reg: 0x%02x, value: 0x%02x\n", reg_no, data);
2697   return SANE_STATUS_GOOD;
2698 }
2699 
2700 SANE_Status
usb_low_read_reg(ma1017 * chip,SANE_Byte reg_no,SANE_Byte * data)2701 usb_low_read_reg (ma1017 * chip, SANE_Byte reg_no, SANE_Byte * data)
2702 {
2703   SANE_Byte data_field[2];
2704   SANE_Byte read_byte;
2705   size_t n;
2706   SANE_Status status;
2707 
2708   data_field[0] = 0x00;
2709   data_field[1] = reg_no | 0x20;
2710 
2711   if (!chip->is_opened)
2712     {
2713       DBG (3, "usb_low_read_reg: open first\n");
2714       return SANE_STATUS_INVAL;
2715     }
2716   if (chip->is_rowing)
2717     {
2718       DBG (3, "usb_low_read_reg: rowing, stop first\n");
2719       return SANE_STATUS_INVAL;
2720     }
2721   if (reg_no > 0x20)
2722     {
2723       DBG (3, "usb_low_read_reg: reg_no out of range\n");
2724       return SANE_STATUS_INVAL;
2725     }
2726   n = 2;
2727   DBG (5, "usb_low_read_reg: trying to write %lu bytes\n", (unsigned long int) n);
2728 
2729   status = sanei_usb_write_bulk (chip->fd, data_field, &n);
2730   if (status != SANE_STATUS_GOOD || n != 2)
2731     {
2732       DBG (3, "usb_low_read_reg: couldn't write, tried to write %d, "
2733 	   "wrote %lu: %s\n", 2, (unsigned long int) n,
2734 	   sane_strstatus (status));
2735       return SANE_STATUS_IO_ERROR;
2736     }
2737   chip->total_write_urbs++;
2738   n = 1;
2739   DBG (5, "usb_low_read_reg: trying to read %lu bytes\n", (unsigned long int) n);
2740   status = sanei_usb_read_bulk (chip->fd, (SANE_Byte *) & read_byte, &n);
2741   if (status != SANE_STATUS_GOOD || n != 1)
2742     {
2743       DBG (3, "usb_low_read_reg: couldn't read, tried to read %lu, "
2744 	   "read %lu: %s\n", (unsigned long int) 1,
2745 	   (unsigned long int) n, sane_strstatus (status));
2746       return SANE_STATUS_IO_ERROR;
2747     }
2748   chip->total_read_urbs++;
2749   if (data)
2750     *data = read_byte;
2751   DBG (7, "usb_low_read_reg: Reg: 0x%02x, Value: 0x%02x\n",
2752        reg_no, read_byte);
2753   return SANE_STATUS_GOOD;
2754 }
2755 
2756 SANE_Status
usb_low_identify_scanner(SANE_Int fd,Mustek_Type * scanner_type)2757 usb_low_identify_scanner (SANE_Int fd, Mustek_Type * scanner_type)
2758 {
2759   SANE_Status status;
2760   SANE_Word devvendor, devproduct;
2761   Mustek_Type devtype;
2762 
2763   DBG (7, "usb_low_identify_scanner: start\n");
2764 
2765   status = sanei_usb_get_vendor_product (fd, &devvendor, &devproduct);
2766   devtype = MT_UNKNOWN;
2767   if (status == SANE_STATUS_GOOD)
2768     {
2769       if (devvendor == 0x055f)
2770 	{
2771 	  switch (devproduct)
2772 	    {
2773 	    case 0x0001:
2774 	      devtype = MT_1200CU;
2775 	      break;
2776 	    case 0x0002:
2777 	      devtype = MT_600CU;
2778 	      break;
2779 	    case 0x0003:
2780 	      devtype = MT_1200USB;
2781 	      break;
2782 	    case 0x0006:
2783 	      devtype = MT_1200UB;
2784 	      break;
2785 	    case 0x0008:
2786 	      devtype = MT_1200CU_PLUS;
2787 	      break;
2788 	    case 0x0873:
2789 	      devtype = MT_600USB;
2790 	      break;
2791 	    default:
2792 	      if (scanner_type)
2793 		*scanner_type = devtype;
2794 	      DBG (3, "usb_low_identify_scanner: unknown product id: "
2795 		   "0x%04x\n", devproduct);
2796 	      return SANE_STATUS_INVAL;
2797 	      break;
2798 	    }
2799 	}
2800       else
2801 	{
2802 	  if (scanner_type)
2803 	    *scanner_type = devtype;
2804 	  DBG (3, "usb_low_identify_scanner: unknown vendor id: 0x%04d\n",
2805 	       devvendor);
2806 	  return SANE_STATUS_INVAL;
2807 	}
2808     }
2809   if (scanner_type)
2810     *scanner_type = devtype;
2811   DBG (7, "usb_low_identify_scanner: exit\n");
2812   return SANE_STATUS_GOOD;
2813 }
2814 
2815 SANE_Status
usb_low_open(ma1017 * chip,SANE_String_Const devname)2816 usb_low_open (ma1017 * chip, SANE_String_Const devname)
2817 {
2818   SANE_Status status;
2819   Mustek_Type scanner_type;
2820 
2821   DBG (7, "usb_low_open: start: chip = %p\n", (void *) chip);
2822 
2823   if (chip->is_rowing)
2824     {
2825       DBG (3, "usb_low_open: already rowing\n");
2826       return SANE_STATUS_INVAL;
2827     }
2828   if (chip->is_opened)
2829     {
2830       DBG (3, "usb_low_open: already opened\n");
2831       return SANE_STATUS_INVAL;
2832     }
2833 
2834   status = sanei_usb_open ((SANE_String_Const) devname, &chip->fd);
2835 
2836   if (status == SANE_STATUS_GOOD)
2837     {
2838       DBG (7, "usb_low_open: device %s successfully opened\n", devname);
2839       chip->is_opened = SANE_TRUE;
2840       /* Try to get vendor and device ids */
2841       DBG (7, "usb_low_open: trying to identify device `%s'\n", devname);
2842       status = usb_low_identify_scanner (chip->fd, &scanner_type);
2843       if (status != SANE_STATUS_GOOD)
2844 	{
2845 	  DBG (3, "usb_low_open: device `%s' doesn't look like a supported "
2846 	       "scanner\n", devname);
2847 	  sanei_usb_close (chip->fd);
2848 	  return status;
2849 	}
2850       else
2851 	{
2852 	  if (scanner_type == MT_UNKNOWN)
2853 	    {
2854 	      DBG (3, "usb_low_open: device `%s' can't be identified\n",
2855 		   devname);
2856 	    }
2857 	  else if (scanner_type != chip->scanner_type)
2858 	    {
2859 	      DBG (3, "usb_low_open: device `%s' is supported but"
2860 		   "it's not the same as at the start\n", devname);
2861 	      return SANE_STATUS_INVAL;
2862 	    }
2863 	}
2864     }
2865   else
2866     {
2867       DBG (1, "usb_low_open: device %s couldn't be opened: %s\n",
2868 	   devname, sane_strstatus (status));
2869       return status;
2870     }
2871 
2872   chip->is_opened = SANE_TRUE;
2873 
2874   RIE (usb_low_read_all_registers (chip));
2875 
2876   DBG (7, "usb_low_open: exit, type is %d\n", scanner_type);
2877   return SANE_STATUS_GOOD;
2878 }
2879 
2880 SANE_Status
usb_low_close(ma1017 * chip)2881 usb_low_close (ma1017 * chip)
2882 {
2883   DBG (7, "usb_low_close: start, chip=%p\n", (void *) chip);
2884   if (!chip->is_opened)
2885     {
2886       DBG (3, "usb_low_close: already close or never opened\n");
2887       return SANE_STATUS_INVAL;
2888     }
2889 
2890   if (chip->fd >= 0)
2891     {
2892       SANE_Byte dummy;
2893 
2894       if (chip->is_rowing)
2895 	usb_low_stop_rowing (chip);
2896       /* Now make sure that both the number of written and
2897 	 read URBs is even. Use some dummy writes/reads. That's to avoid
2898 	 a nasty bug in the MA 1017 chipset that causes timeouts when
2899 	 the number of URBs is odd (toggle bug). */
2900       if ((chip->total_read_urbs % 2) == 1)
2901 	usb_low_get_a4 (chip, &dummy);
2902       if ((chip->total_write_urbs % 2) == 1)
2903 	usb_low_set_fix_pattern (chip, SANE_FALSE);
2904       sanei_usb_close (chip->fd);
2905       chip->fd = -1;
2906     }
2907   chip->is_opened = SANE_FALSE;
2908   chip->is_rowing = SANE_FALSE;
2909 
2910   DBG (7, "usb_low_close: exit\n");
2911   return SANE_STATUS_GOOD;
2912 }
2913