summaryrefslogtreecommitdiff
path: root/backend/rts8891_low.c
blob: 1b95765d7813bda7fba71133387d12c3e9a27c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/* sane - Scanner Access Now Easy.

   Copyright (C) 2007-2013 stef.dev@free.fr

   This file is part of the SANE package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.
*/

/* this file contains all the low level functions needed for the higher level
 * functions of the sane standard. They are put there to keep files smaller
 * and separate functions with different goals.
 */

#include "../include/sane/config.h"
#include "../include/sane/sane.h"
#include "../include/sane/sanei_backend.h"
#include "../include/sane/sanei_usb.h"

#include <stdio.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include "rts8891_low.h"

#define RTS8891_BUILD           30
#define RTS8891_MAX_REGISTERS	244

/* init rts8891 library */
static void
rts8891_low_init (void)
{
  DBG_INIT ();
  DBG (DBG_info, "RTS8891 low-level  functions, version %d.%d-%d\n",
       SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, RTS8891_BUILD);
}


/****************************************************************/
/*                 ASIC specific functions                      */
/****************************************************************/

/* write all registers, taking care of the special 0xaa value which
 * must be escaped with a zero
 */
static SANE_Status
rts8891_write_all (SANE_Int devnum, SANE_Byte * regs, SANE_Int count)
{
  SANE_Status status = SANE_STATUS_GOOD;
  SANE_Byte local_regs[RTS8891_MAX_REGISTERS];
  size_t size = 0;
  SANE_Byte buffer[260];
  unsigned int i, j;
  char message[256 * 5];

  if (DBG_LEVEL > DBG_io)
    {
      for (i = 0; i < (unsigned int) count; i++)
	{
	  if (i != 0xb3)
	    sprintf (message + 5 * i, "0x%02x ", regs[i]);
	  else
	    sprintf (message + 5 * i, "---- ");
	}
      DBG (DBG_io, "rts8891_write_all : write_all(0x00,%d)=%s\n", count,
	   message);
    }

  /* copy register set and escaping 0xaa values                     */
  /* b0, b1 abd b3 values may be scribled, but that isn't important */
  /* since they are read-only registers                             */
  j = 0;
  for (i = 0; i < 0xb3; i++)
    {
      local_regs[j] = regs[i];
      if (local_regs[j] == 0xaa && i < 0xb3)
	{
	  j++;
	  local_regs[j] = 0x00;
	}
      j++;
    }
  buffer[0] = 0x88;
  buffer[1] = 0;
  buffer[2] = 0x00;
  buffer[3] = 0xb3;
  for (i = 0; i < j; i++)
    buffer[i + 4] = local_regs[i];
  /* the USB block is size + 4 bytes of header long */
  size = j + 4;
  if (sanei_usb_write_bulk (devnum, buffer, &size) != SANE_STATUS_GOOD)
    {
      DBG (DBG_error,
	   "rts88xx_write_all : write registers part 1 failed ...\n");
      return SANE_STATUS_IO_ERROR;
    }

  size = count - 0xb4;		/*  we need to subtract one reg since b3 won't be written */
  buffer[0] = 0x88;
  buffer[1] = 0xb4;
  buffer[2] = 0x00;
  buffer[3] = size;
  for (i = 0; i < size; i++)
    buffer[i + 4] = regs[0xb4 + i];
  /* the USB block is size + 4 bytes of header long */
  size += 4;
  if (sanei_usb_write_bulk (devnum, buffer, &size) != SANE_STATUS_GOOD)
    {
      DBG (DBG_error,
	   "rts88xx_write_all : write registers part 2 failed ...\n");
      return SANE_STATUS_IO_ERROR;
    }
  return status;
}


/* this functions "commits" pending scan command */
static SANE_Status
rts8891_commit (SANE_Int devnum, SANE_Byte value)
{
  SANE_Status status;
  SANE_Byte reg;

  reg = value;
  sanei_rts88xx_write_reg (devnum, 0xd3, &reg);
  sanei_rts88xx_cancel (devnum);
  sanei_rts88xx_write_control (devnum, 0x08);
  status = sanei_rts88xx_write_control (devnum, 0x08);
  return status;
}

/* this functions reads button status */
static SANE_Status
rts8891_read_buttons (SANE_Int devnum, SANE_Int * mask)
{
  SANE_Status status = SANE_STATUS_GOOD;
  SANE_Byte reg;

  /* check CONTROL_REG */
  sanei_rts88xx_read_reg (devnum, CONTROL_REG, &reg);

  /* read 'base' button status */
  sanei_rts88xx_read_reg (devnum, 0x25, &reg);
  DBG (DBG_io, "rts8891_read_buttons: r25=0x%02x\n", reg);
  *mask |= reg;

  /* read 'extended' button status */
  sanei_rts88xx_read_reg (devnum, 0x1a, &reg);
  DBG (DBG_io, "rts8891_read_buttons: r1a=0x%02x\n", reg);
  *mask |= reg << 8;

  /* clear register r25 */
  reg = 0x00;
  sanei_rts88xx_write_reg (devnum, 0x25, &reg);

  /* clear register r1a */
  sanei_rts88xx_read_reg (devnum, 0x1a, &reg);
  reg = 0x00;
  status = sanei_rts88xx_write_reg (devnum, 0x1a, &reg);

  DBG (DBG_info, "rts8891_read_buttons: mask=0x%04x\n", *mask);
  return status;
}

/*
 * Does a simple scan based on the given register set, returning data in a
 * preallocated buffer of the claimed size.
 * sanei_rts88xx_data_count cannot be made reliable, when the announced data
 * amount is read, it may no be ready, leading to errors. To work around
 * it, we read data count one more time before reading.
 */
static SANE_Status
rts8891_simple_scan (SANE_Int devnum, SANE_Byte * regs, int regcount,
		     SANE_Int format, SANE_Word total, unsigned char *image)
{
  SANE_Word count, read, len, dummy;
  SANE_Status status = SANE_STATUS_GOOD;
  SANE_Byte control;

  rts8891_write_all (devnum, regs, regcount);
  rts8891_commit (devnum, format);

  read = 0;
  count = 0;
  while (count == 0)
    {
      status = sanei_rts88xx_data_count (devnum, &count);
      if (status != SANE_STATUS_GOOD)
	{
	  DBG (DBG_error, "simple_scan: failed to wait for data\n");
	  return status;
	}
      if (count == 0)
	{
	  status = sanei_rts88xx_read_reg (devnum, CONTROL_REG, &control);
	  if (((control & 0x08) == 0) || (status != SANE_STATUS_GOOD))
	    {
	      DBG (DBG_error, "simple_scan: failed to wait for data\n");
	      return SANE_STATUS_IO_ERROR;
	    }
	}
    }

  /* data reading */
  read = 0;
  while ((read < total) && (count != 0 || (control & 0x08) == 0x08))
    {
      /* sync ? */
      status = sanei_rts88xx_data_count (devnum, &dummy);

      /* read */
      if (count > 0)
	{
	  len = count;
	  /* read even size unless last chunk */
	  if ((len & 1) && (read + len < total))
	    {
	      len++;
	    }
	  if (len > RTS88XX_MAX_XFER_SIZE)
	    {
	      len = RTS88XX_MAX_XFER_SIZE;
	    }
	  if (len > 0)
	    {
	      status = sanei_rts88xx_read_data (devnum, &len, image + read);
	      if (status != SANE_STATUS_GOOD)
		{
		  DBG (DBG_error,
		       "simple_scan: failed to read from scanner\n");
		  return status;
		}
	      read += len;
	    }
	}

      /* don't try to read data count if we have enough data */
      if (read < total)
	{
	  status = sanei_rts88xx_data_count (devnum, &count);
	}
      else
	{
	  count = 0;
	}
      if (count == 0)
	{
	  sanei_rts88xx_read_reg (devnum, CONTROL_REG, &control);
	}
    }

  /* sanity check */
  if (read < total)
    {
      DBG (DBG_io2, "simple_scan: ERROR, %d bytes missing ... \n",
	   total - read);
    }

  /* wait for the motor to stop */
  do
    {
      sanei_rts88xx_read_reg (devnum, CONTROL_REG, &control);
    }
  while ((control & 0x08) == 0x08);

  return status;
}

 /**
  * set the data format. Is part of the commit sequence. Then returned
  * value is the value used in d3 register for a scan.
  */
static SANE_Int
rts8891_data_format (SANE_Int dpi, int sensor)
{
  SANE_Byte reg = 0x00;

  /* it seems that lower nibble is a divisor */
  if (sensor == SENSOR_TYPE_BARE || sensor == SENSOR_TYPE_XPA)
    {
      switch (dpi)
	{
	case 75:
	  reg = 0x02;
	  break;
	case 150:
	  if (sensor == SENSOR_TYPE_BARE)
	    reg = 0x0e;
	  else
	    reg = 0x0b;
	  break;
	case 300:
	  reg = 0x17;
	  break;
	case 600:
	  if (sensor == SENSOR_TYPE_BARE)
	    reg = 0x02;
	  else
	    reg = 0x0e;
	  break;
	case 1200:
	  if (sensor == SENSOR_TYPE_BARE)
	    reg = 0x17;
	  else
	    reg = 0x05;
	  break;
	}
    }
  if (sensor == SENSOR_TYPE_4400 || sensor == SENSOR_TYPE_4400_BARE)
    {
      switch (dpi)
	{
	case 75:
	  reg = 0x02;
	  break;
	case 150:
	  if (sensor == SENSOR_TYPE_4400)
	    reg = 0x0b;
	  else
	    reg = 0x17;
	  break;
	case 300:
	  reg = 0x17;
	  break;
	case 600:
	  if (sensor == SENSOR_TYPE_4400)
	    reg = 0x0e;
	  else
	    reg = 0x02;
	  break;
	case 1200:
	  if (sensor == SENSOR_TYPE_4400)
	    reg = 0x05;
	  else
	    reg = 0x17;
	  break;
	}
    }
  return reg;
}

/**
 * set up default values for a 75xdpi, 150 ydpi scan
 */
static void
rts8891_set_default_regs (SANE_Byte * scanner_regs)
{
  SANE_Byte default_75[RTS8891_MAX_REGISTERS] =
    { 0xe5, 0x41, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x0a, 0x0a, 0x0a, 0x70,
    0x00, 0x00, 0x00, 0x00, 0x28, 0x3f, 0xff, 0x20, 0xf8, 0x28, 0x07, 0x00,
    0xff, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
    0x00, 0x3a, 0xf2, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x8c,
    0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe1, 0x14, 0x18, 0x15, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0xff, 0x3f, 0x80, 0x68, 0x00, 0x00,
    0x00, 0x00, 0x01, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc,
    0x27, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
    0x0e, 0x00, 0x00, 0xf0, 0xff, 0xf5, 0xf7, 0xea, 0x0b, 0x03, 0x05, 0x86,
    0x1b, 0x30, 0xf6, 0xa2, 0x27, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00
  };
  unsigned int i;
  for (i = 0; i < RTS8891_MAX_REGISTERS; i++)
    scanner_regs[i] = default_75[i];
}

static SANE_Status
rts8891_move (struct Rts8891_Device *device, SANE_Byte * regs,
	      SANE_Int distance, SANE_Bool forward)
{
  SANE_Status status = SANE_STATUS_GOOD;
  SANE_Byte regs10, regs11;

  DBG (DBG_proc, "rts8891_move: start\n");
  DBG (DBG_io, "rts8891_move: %d lines %s, sensor=%d\n", distance,
       forward == SANE_TRUE ? "forward" : "backward", device->sensor);

  /* prepare scan */
  rts8891_set_default_regs (regs);
  if (device->sensor != SENSOR_TYPE_4400
      && device->sensor != SENSOR_TYPE_4400_BARE)
    {
      regs10 = 0x20;
      regs11 = 0x28;
    }
  else
    {
      regs10 = 0x10;
      regs11 = 0x2a;
    }

  regs[0x32] = 0x80;
  regs[0x33] = 0x81;
  regs[0x35] = 0x10;
  regs[0x36] = 0x24;
  regs[0x39] = 0x02;
  regs[0x3a] = 0x0e;

  regs[0x64] = 0x01;
  regs[0x65] = 0x20;

  regs[0x79] = 0x20;
  regs[0x7a] = 0x01;

  regs[0x80] = 0x32;
  regs[0x82] = 0x33;
  regs[0x85] = 0x46;
  regs[0x86] = 0x0b;
  regs[0x87] = 0x8c;
  regs[0x88] = 0x10;
  regs[0x89] = 0xb2;
  regs[0x8d] = 0x3b;
  regs[0x8e] = 0x60;

  regs[0x90] = 0x1c;
  regs[0xb2] = 0x16;		/* 0x10 : stop when at home, 0x04: no data */
  regs[0xc0] = 0x00;
  regs[0xc1] = 0x00;
  regs[0xc3] = 0x00;
  regs[0xc4] = 0x00;
  regs[0xc5] = 0x00;
  regs[0xc6] = 0x00;
  regs[0xc7] = 0x00;
  regs[0xc8] = 0x00;
  regs[0xca] = 0x00;
  regs[0xcd] = 0x00;
  regs[0xce] = 0x00;
  regs[0xcf] = 0x00;
  regs[0xd0] = 0x00;
  regs[0xd1] = 0x00;
  regs[0xd2] = 0x00;
  regs[0xd3] = 0x00;
  regs[0xd4] = 0x00;
  regs[0xd6] = 0x6b;
  regs[0xd7] = 0x00;
  regs[0xd8] = 0x00;
  regs[0xd9] = 0xad;
  regs[0xda] = 0xa7;
  regs[0xe2] = 0x17;
  regs[0xe3] = 0x0d;
  regs[0xe4] = 0x06;
  regs[0xe5] = 0xf9;
  regs[0xe7] = 0x53;
  regs[0xe8] = 0x02;
  regs[0xe9] = 0x02;

  /* hp4400 sensors */
  if (device->sensor == SENSOR_TYPE_4400
      || device->sensor == SENSOR_TYPE_4400_BARE)
    {
      regs[0x13] = 0x39;	/* 0x20 */
      regs[0x14] = 0xf0;	/* 0xf8 */
      regs[0x15] = 0x29;	/* 0x28 */
      regs[0x16] = 0x0f;	/* 0x07 */
      regs[0x17] = 0x10;	/* 0x00 */
      regs[0x23] = 0x00;	/* 0xff */
      regs[0x35] = 0x29;	/* 0x10 */
      regs[0x36] = 0x21;	/* 0x24 */
      regs[0x39] = 0x00;	/* 0x02 */
      regs[0x80] = 0xb0;	/* 0x32 */
      regs[0x82] = 0xb1;	/* 0x33 */
      regs[0xe2] = 0x0b;	/* 0x17 */
      regs[0xe5] = 0xf3;	/* 0xf9 */
      regs[0xe6] = 0x01;	/* 0x00 */
    }

  /* disable CCD */
  regs[0] = 0xf5;

  sanei_rts88xx_set_status (device->devnum, regs, regs10, regs11);
  sanei_rts88xx_set_scan_area (regs, distance, distance + 1, 100, 200);
  sanei_rts88xx_set_gain (regs, 16, 16, 16);
  sanei_rts88xx_set_offset (regs, 127, 127, 127);

  /* forward/backward */
  /* 0x2c is forward, 0x24 backward */
  if (forward == SANE_TRUE)
    {				/* forward */
      regs[0x36] = regs[0x36] | 0x08;
    }
  else
    {				/* backward */
      regs[0x36] = regs[0x36] & 0xf7;
    }

  /* write registers values */
  status = rts8891_write_all (device->devnum, regs, RTS8891_MAX_REGISTERS);

  /* commit it */
  rts8891_commit (device->devnum, 0x00);

  return status;
}

 /**
  * wait for the scanning head to reach home position
  */
static SANE_Status
rts8891_wait_for_home (struct Rts8891_Device *device, SANE_Byte * regs)
{
  SANE_Status status = SANE_STATUS_GOOD;
  SANE_Byte motor, sensor, reg;

  DBG (DBG_proc, "rts8891_wait_for_home: start\n");

  /* wait for controller home bit to raise, no timeout */
  /* at each loop we check that motor is on, then that the sensor bit it cleared */
  do
    {
      sanei_rts88xx_read_reg (device->devnum, CONTROL_REG, &motor);
      sanei_rts88xx_read_reg (device->devnum, CONTROLER_REG, &sensor);
    }
  while ((motor & 0x08) && ((sensor & 0x02) == 0));

  /* flag that device has finished parking */
  device->parking=SANE_FALSE;

  /* check for error */
  if (((motor & 0x08) == 0x00) && ((sensor & 0x02) == 0))
    {
      DBG (DBG_error,
	   "rts8891_wait_for_home: error, motor stopped before head parked\n");
      status = SANE_STATUS_INVAL;
    }

  /* re-enable CCD */
  regs[0] = regs[0] & 0xef;

  sanei_rts88xx_cancel (device->devnum);

  /* reset ? so we don't need to read data */
  reg = 0;
  /* b7: movement on/off, b3-b0 : movement divisor */
  sanei_rts88xx_write_reg (device->devnum, 0x33, &reg);
  sanei_rts88xx_write_reg (device->devnum, 0x33, &reg);
  /* movement direction */
  sanei_rts88xx_write_reg (device->devnum, 0x36, &reg);
  sanei_rts88xx_cancel (device->devnum);

  DBG (DBG_proc, "rts8891_wait_for_home: end\n");
  return status;
}

 /**
  * move the head backward by a huge line number then poll home sensor until
  * head has get back home. We have our own copy of the registers to avoid
  * messing scanner status
  */
static SANE_Status
rts8891_park (struct Rts8891_Device *device, SANE_Byte *regs, SANE_Bool wait)
{
  SANE_Status status = SANE_STATUS_GOOD;

  DBG (DBG_proc, "rts8891_park: start\n");

  device->parking=SANE_TRUE;
  rts8891_move (device, regs, 8000, SANE_FALSE);

  if(wait==SANE_TRUE)
    {
      status = rts8891_wait_for_home (device,regs);
    }

  DBG (DBG_proc, "rts8891_park: end\n");
  return status;
}

/* reads data from scanner.
 * First we wait for some data to be available and then loop reading
 * from scanner until the required amount is reached.
 * We handle non blocking I/O by returning immediately (with SANE_STATUS_BUSY)
 * if there is no data available from scanner. But once read is started,
 * all the required amount is read. Once wait for data succeeded, we still poll
 * for data in order no to read it too fast, but we don' take care of non blocking
 * mode since we cope with it on first data wait.
 */
static SANE_Status
read_data (struct Rts8891_Session *session, SANE_Byte * dest, SANE_Int length)
{
  SANE_Status status = SANE_STATUS_GOOD;
  SANE_Int count, read, len, dummy;
  struct Rts8891_Device *dev = session->dev;
  static FILE *raw = NULL;	/* for debugging purpose we need it static */
  SANE_Byte control = 0x08;
  unsigned char buffer[RTS88XX_MAX_XFER_SIZE];

  DBG (DBG_proc, "read_data: start\n");
  DBG (DBG_proc, "read_data: requiring %d bytes\n", length);

  /* wait for data being available and handle non blocking mode */
  /* only when data reading hasn't produce any data yet */
  if (dev->read == 0)
    {
      do
	{
	  status = sanei_rts88xx_data_count (dev->devnum, &count);
	  if (status != SANE_STATUS_GOOD)
	    {
	      DBG (DBG_error, "read_data: failed to wait for data\n");
	      return status;
	    }
	  if (count == 0)
	    {
	      sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
	      if ((control & 0x08) == 0 && (count == 0))
		{
		  DBG (DBG_error,
		       "read_data: scanner stopped being busy before data are available\n");
		  return SANE_STATUS_IO_ERROR;
		}
	    }

	  /* in case there is no data, we return BUSY since this mean    */
	  /* that scanning head hasn't reach is position and data hasn't */
	  /* come yet */
	  if (session->non_blocking && count == 0)
	    {

	      dev->regs[LAMP_REG] = 0x8d;
	      sanei_rts88xx_write_reg (dev->devnum, LAMP_REG,
				       &(dev->regs[LAMP_REG]));
	      DBG (DBG_io, "read_data: no data available\n");
	      DBG (DBG_proc, "read_data: end\n");
	      return SANE_STATUS_DEVICE_BUSY;
	    }
	}
      while (count == 0);
    }
  else
    {				/* start of read for a new block */
      status = sanei_rts88xx_data_count (dev->devnum, &count);
      if (status != SANE_STATUS_GOOD)
	{
	  DBG (DBG_error, "read_data: failed to wait for data\n");
	  return status;
	}
      if (count == 0)
	{
	  sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
	  if ((control & 0x08) == 0 && (count == 0))
	    {
	      DBG (DBG_error,
		   "read_data: scanner stopped being busy before data are available\n");
	      return SANE_STATUS_IO_ERROR;
	    }
	}
    }

  /* fill scanned data buffer */
  read = 0;

  /* now loop reading data until we have the amount requested */
  /* we also take care of not reading too much data           */
  while (read < length && dev->read < dev->to_read
	 && ((control & 0x08) == 0x08))
    {
      /* used to sync */
      if (dev->read == 0)
	{
	  status = sanei_rts88xx_data_count (dev->devnum, &dummy);
	  if (status != SANE_STATUS_GOOD)
	    {
	      DBG (DBG_error, "read_data: failed to read data count\n");
	      return status;
	    }
	}

      /* if there is data to read, read it */
      if (count > 0)
	{
	  len = count;

	  if (len > RTS88XX_MAX_XFER_SIZE)
	    {
	      len = RTS88XX_MAX_XFER_SIZE;
	    }

	  /* we only read even size blocks of data */
	  if (len & 1)
	    {
	      DBG (DBG_io, "read_data: round to next even number\n");
	      len++;
	    }

	  if (len > length - read)
	    {
	      len = length - read;
	    }

	  status = sanei_rts88xx_read_data (dev->devnum, &len, dest + read);
	  if (status != SANE_STATUS_GOOD)
	    {
	      DBG (DBG_error, "read_data: failed to read from scanner\n");
	      return status;
	    }

	  /* raw data tracing */
	  if (DBG_LEVEL >= DBG_io2)
	    {
	      /* open a new file only when no data scanned */
	      if (dev->read == 0)
		{
		  raw = fopen ("raw_data.pnm", "wb");
		  if (raw != NULL)
		    {
		      /* PNM header */
		      fprintf (raw, "P%c\n%d %d\n255\n",
			       session->params.format ==
			       SANE_FRAME_RGB
			       || session->emulated_gray ==
			       SANE_TRUE ? '6' : '5', dev->pixels,
			       dev->lines);
		    }
		}
	      if (raw != NULL)
		{
		  fwrite (dest + read, 1, len, raw);
		}
	    }

	  /* move pointer and counter */
	  read += len;
	  dev->read += len;
	  DBG (DBG_io2, "read_data: %d/%d\n", dev->read, dev->to_read);
	}

      /* in fast scan mode, read data count
       * in slow scan, head moves by the amount of data read */
      status = sanei_rts88xx_data_count (dev->devnum, &count);
      if (status != SANE_STATUS_GOOD)
	{
	  DBG (DBG_error, "read_data: failed to read data count\n");
	  return status;
	}

      /* if no data, check if still scanning */
      if (count == 0 && dev->read < dev->to_read)
	{
	  sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
	  if ((control & 0x08) == 0x00)
	    {
	      DBG (DBG_error,
		   "read_data: scanner stopped being busy before data are available\n");
	      return SANE_STATUS_IO_ERROR;
	    }
	}
    }

  /* end of physical reads */
  if (dev->read >= dev->to_read)
    {
      /* check there is no more data in case of a bug */
      sanei_rts88xx_data_count (dev->devnum, &count);
      if (count > 0)
	{
	  DBG (DBG_warn,
	       "read_data: %d bytes are still available from scanner\n",
	       count);

	  /* flush left-over data */
	  while (count > 0)
	    {
	      len = count;
	      if (len > RTS88XX_MAX_XFER_SIZE)
		{
		  len = RTS88XX_MAX_XFER_SIZE;
		}

	      /* we only read even size blocks of data */
	      if (len & 1)
		{
		  len++;
		}
	      sanei_rts88xx_read_data (dev->devnum, &len, buffer);
	      sanei_rts88xx_data_count (dev->devnum, &count);
	    }
	}

      /* wait for motor to stop at the end of the scan */
      do
	{
	  sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
	}
      while ((control & 0x08) != 0);

      /* close log file if needed */
      if (DBG_LEVEL >= DBG_io2)
	{
	  if (raw != NULL)
	    {
	      fclose (raw);
	      raw = NULL;
	    }
	}
    }

  DBG (DBG_io, "read_data: read %d bytes from scanner\n", length);
  DBG (DBG_proc, "read_data: end\n");
  return status;
}

  /**
   * set scanner idle before leaving xxx_quiet()
write_reg(0x33,1)=0x00
write_reg(0x33,1)=0x00
write_reg(0x36,1)=0x00
prepare();
------
write_reg(LAMP_REG,1)=0x80
write_reg(LAMP_REG,1)=0xad
read_reg(0x14,2)=0xf8 0x28
write_reg(0x14,2)=0x78 0x28
get_status()=0x20 0x3f
read_reg(0xb1,1)=0x00
read_control()=0x00
reset_lamp()=(0x20,0x3f)
write_reg(LAMP_REG,1)=0x8d
write_reg(LAMP_REG,1)=0xad
   */

/* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */