• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2 
3    Copyright (C) 2007-2013 stef.dev@free.fr
4 
5    This file is part of the SANE package.
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 
20    As a special exception, the authors of SANE give permission for
21    additional uses of the libraries contained in this release of SANE.
22 
23    The exception is that, if you link a SANE library with other files
24    to produce an executable, this does not by itself cause the
25    resulting executable to be covered by the GNU General Public
26    License.  Your use of that executable is in no way restricted on
27    account of linking the SANE library code into it.
28 
29    This exception does not, however, invalidate any other reasons why
30    the executable file might be covered by the GNU General Public
31    License.
32 
33    If you submit changes to SANE to the maintainers to be included in
34    a subsequent release, you agree by submitting the changes that
35    those changes may be distributed with this exception intact.
36 
37    If you write modifications of your own for SANE, it is your choice
38    whether to permit this exception to apply to your modifications.
39    If you do not wish that, delete this exception notice.
40 */
41 
42 /* this file contains all the low level functions needed for the higher level
43  * functions of the sane standard. They are put there to keep files smaller
44  * and separate functions with different goals.
45  */
46 
47 #include "../include/sane/config.h"
48 #include "../include/sane/sane.h"
49 #include "../include/sane/sanei_backend.h"
50 #include "../include/sane/sanei_usb.h"
51 
52 #include <stdio.h>
53 #ifdef HAVE_SYS_TIME_H
54 #include <sys/time.h>
55 #endif
56 #include "rts8891_low.h"
57 
58 #define RTS8891_BUILD           30
59 #define RTS8891_MAX_REGISTERS	244
60 
61 /* init rts8891 library */
62 static void
rts8891_low_init(void)63 rts8891_low_init (void)
64 {
65   DBG_INIT ();
66   DBG (DBG_info, "RTS8891 low-level  functions, version %d.%d-%d\n",
67        SANE_CURRENT_MAJOR, SANE_CURRENT_MINOR, RTS8891_BUILD);
68 }
69 
70 
71 /****************************************************************/
72 /*                 ASIC specific functions                      */
73 /****************************************************************/
74 
75 /* write all registers, taking care of the special 0xaa value which
76  * must be escaped with a zero
77  */
78 static SANE_Status
rts8891_write_all(SANE_Int devnum,SANE_Byte * regs,SANE_Int count)79 rts8891_write_all (SANE_Int devnum, SANE_Byte * regs, SANE_Int count)
80 {
81   SANE_Status status = SANE_STATUS_GOOD;
82   SANE_Byte local_regs[RTS8891_MAX_REGISTERS];
83   size_t size = 0;
84   SANE_Byte buffer[260];
85   unsigned int i, j;
86   char message[256 * 5];
87 
88   if (DBG_LEVEL > DBG_io)
89     {
90       for (i = 0; i < (unsigned int) count; i++)
91 	{
92 	  if (i != 0xb3)
93 	    sprintf (message + 5 * i, "0x%02x ", regs[i]);
94 	  else
95 	    sprintf (message + 5 * i, "---- ");
96 	}
97       DBG (DBG_io, "rts8891_write_all : write_all(0x00,%d)=%s\n", count,
98 	   message);
99     }
100 
101   /* copy register set and escaping 0xaa values                     */
102   /* b0, b1 abd b3 values may be scribled, but that isn't important */
103   /* since they are read-only registers                             */
104   j = 0;
105   for (i = 0; i < 0xb3; i++)
106     {
107       local_regs[j] = regs[i];
108       if (local_regs[j] == 0xaa && i < 0xb3)
109 	{
110 	  j++;
111 	  local_regs[j] = 0x00;
112 	}
113       j++;
114     }
115   buffer[0] = 0x88;
116   buffer[1] = 0;
117   buffer[2] = 0x00;
118   buffer[3] = 0xb3;
119   for (i = 0; i < j; i++)
120     buffer[i + 4] = local_regs[i];
121   /* the USB block is size + 4 bytes of header long */
122   size = j + 4;
123   if (sanei_usb_write_bulk (devnum, buffer, &size) != SANE_STATUS_GOOD)
124     {
125       DBG (DBG_error,
126 	   "rts88xx_write_all : write registers part 1 failed ...\n");
127       return SANE_STATUS_IO_ERROR;
128     }
129 
130   size = count - 0xb4;		/*  we need to subtract one reg since b3 won't be written */
131   buffer[0] = 0x88;
132   buffer[1] = 0xb4;
133   buffer[2] = 0x00;
134   buffer[3] = size;
135   for (i = 0; i < size; i++)
136     buffer[i + 4] = regs[0xb4 + i];
137   /* the USB block is size + 4 bytes of header long */
138   size += 4;
139   if (sanei_usb_write_bulk (devnum, buffer, &size) != SANE_STATUS_GOOD)
140     {
141       DBG (DBG_error,
142 	   "rts88xx_write_all : write registers part 2 failed ...\n");
143       return SANE_STATUS_IO_ERROR;
144     }
145   return status;
146 }
147 
148 
149 /* this functions "commits" pending scan command */
150 static SANE_Status
rts8891_commit(SANE_Int devnum,SANE_Byte value)151 rts8891_commit (SANE_Int devnum, SANE_Byte value)
152 {
153   SANE_Status status;
154   SANE_Byte reg;
155 
156   reg = value;
157   sanei_rts88xx_write_reg (devnum, 0xd3, &reg);
158   sanei_rts88xx_cancel (devnum);
159   sanei_rts88xx_write_control (devnum, 0x08);
160   status = sanei_rts88xx_write_control (devnum, 0x08);
161   return status;
162 }
163 
164 /* this functions reads button status */
165 static SANE_Status
rts8891_read_buttons(SANE_Int devnum,SANE_Int * mask)166 rts8891_read_buttons (SANE_Int devnum, SANE_Int * mask)
167 {
168   SANE_Status status = SANE_STATUS_GOOD;
169   SANE_Byte reg;
170 
171   /* check CONTROL_REG */
172   sanei_rts88xx_read_reg (devnum, CONTROL_REG, &reg);
173 
174   /* read 'base' button status */
175   sanei_rts88xx_read_reg (devnum, 0x25, &reg);
176   DBG (DBG_io, "rts8891_read_buttons: r25=0x%02x\n", reg);
177   *mask |= reg;
178 
179   /* read 'extended' button status */
180   sanei_rts88xx_read_reg (devnum, 0x1a, &reg);
181   DBG (DBG_io, "rts8891_read_buttons: r1a=0x%02x\n", reg);
182   *mask |= reg << 8;
183 
184   /* clear register r25 */
185   reg = 0x00;
186   sanei_rts88xx_write_reg (devnum, 0x25, &reg);
187 
188   /* clear register r1a */
189   sanei_rts88xx_read_reg (devnum, 0x1a, &reg);
190   reg = 0x00;
191   status = sanei_rts88xx_write_reg (devnum, 0x1a, &reg);
192 
193   DBG (DBG_info, "rts8891_read_buttons: mask=0x%04x\n", *mask);
194   return status;
195 }
196 
197 /*
198  * Does a simple scan based on the given register set, returning data in a
199  * preallocated buffer of the claimed size.
200  * sanei_rts88xx_data_count cannot be made reliable, when the announced data
201  * amount is read, it may no be ready, leading to errors. To work around
202  * it, we read data count one more time before reading.
203  */
204 static SANE_Status
rts8891_simple_scan(SANE_Int devnum,SANE_Byte * regs,int regcount,SANE_Int format,SANE_Word total,unsigned char * image)205 rts8891_simple_scan (SANE_Int devnum, SANE_Byte * regs, int regcount,
206 		     SANE_Int format, SANE_Word total, unsigned char *image)
207 {
208   SANE_Word count, read, len, dummy;
209   SANE_Status status = SANE_STATUS_GOOD;
210   SANE_Byte control;
211 
212   rts8891_write_all (devnum, regs, regcount);
213   rts8891_commit (devnum, format);
214 
215   read = 0;
216   count = 0;
217   while (count == 0)
218     {
219       status = sanei_rts88xx_data_count (devnum, &count);
220       if (status != SANE_STATUS_GOOD)
221 	{
222 	  DBG (DBG_error, "simple_scan: failed to wait for data\n");
223 	  return status;
224 	}
225       if (count == 0)
226 	{
227 	  status = sanei_rts88xx_read_reg (devnum, CONTROL_REG, &control);
228 	  if (((control & 0x08) == 0) || (status != SANE_STATUS_GOOD))
229 	    {
230 	      DBG (DBG_error, "simple_scan: failed to wait for data\n");
231 	      return SANE_STATUS_IO_ERROR;
232 	    }
233 	}
234     }
235 
236   /* data reading */
237   read = 0;
238   while ((read < total) && (count != 0 || (control & 0x08) == 0x08))
239     {
240       /* sync ? */
241       status = sanei_rts88xx_data_count (devnum, &dummy);
242 
243       /* read */
244       if (count > 0)
245 	{
246 	  len = count;
247 	  /* read even size unless last chunk */
248 	  if ((len & 1) && (read + len < total))
249 	    {
250 	      len++;
251 	    }
252 	  if (len > RTS88XX_MAX_XFER_SIZE)
253 	    {
254 	      len = RTS88XX_MAX_XFER_SIZE;
255 	    }
256 	  if (len > 0)
257 	    {
258 	      status = sanei_rts88xx_read_data (devnum, &len, image + read);
259 	      if (status != SANE_STATUS_GOOD)
260 		{
261 		  DBG (DBG_error,
262 		       "simple_scan: failed to read from scanner\n");
263 		  return status;
264 		}
265 	      read += len;
266 	    }
267 	}
268 
269       /* don't try to read data count if we have enough data */
270       if (read < total)
271 	{
272 	  status = sanei_rts88xx_data_count (devnum, &count);
273 	}
274       else
275 	{
276 	  count = 0;
277 	}
278       if (count == 0)
279 	{
280 	  sanei_rts88xx_read_reg (devnum, CONTROL_REG, &control);
281 	}
282     }
283 
284   /* sanity check */
285   if (read < total)
286     {
287       DBG (DBG_io2, "simple_scan: ERROR, %d bytes missing ... \n",
288 	   total - read);
289     }
290 
291   /* wait for the motor to stop */
292   do
293     {
294       sanei_rts88xx_read_reg (devnum, CONTROL_REG, &control);
295     }
296   while ((control & 0x08) == 0x08);
297 
298   return status;
299 }
300 
301  /**
302   * set the data format. Is part of the commit sequence. Then returned
303   * value is the value used in d3 register for a scan.
304   */
305 static SANE_Int
rts8891_data_format(SANE_Int dpi,int sensor)306 rts8891_data_format (SANE_Int dpi, int sensor)
307 {
308   SANE_Byte reg = 0x00;
309 
310   /* it seems that lower nibble is a divisor */
311   if (sensor == SENSOR_TYPE_BARE || sensor == SENSOR_TYPE_XPA)
312     {
313       switch (dpi)
314 	{
315 	case 75:
316 	  reg = 0x02;
317 	  break;
318 	case 150:
319 	  if (sensor == SENSOR_TYPE_BARE)
320 	    reg = 0x0e;
321 	  else
322 	    reg = 0x0b;
323 	  break;
324 	case 300:
325 	  reg = 0x17;
326 	  break;
327 	case 600:
328 	  if (sensor == SENSOR_TYPE_BARE)
329 	    reg = 0x02;
330 	  else
331 	    reg = 0x0e;
332 	  break;
333 	case 1200:
334 	  if (sensor == SENSOR_TYPE_BARE)
335 	    reg = 0x17;
336 	  else
337 	    reg = 0x05;
338 	  break;
339 	}
340     }
341   if (sensor == SENSOR_TYPE_4400 || sensor == SENSOR_TYPE_4400_BARE)
342     {
343       switch (dpi)
344 	{
345 	case 75:
346 	  reg = 0x02;
347 	  break;
348 	case 150:
349 	  if (sensor == SENSOR_TYPE_4400)
350 	    reg = 0x0b;
351 	  else
352 	    reg = 0x17;
353 	  break;
354 	case 300:
355 	  reg = 0x17;
356 	  break;
357 	case 600:
358 	  if (sensor == SENSOR_TYPE_4400)
359 	    reg = 0x0e;
360 	  else
361 	    reg = 0x02;
362 	  break;
363 	case 1200:
364 	  if (sensor == SENSOR_TYPE_4400)
365 	    reg = 0x05;
366 	  else
367 	    reg = 0x17;
368 	  break;
369 	}
370     }
371   return reg;
372 }
373 
374 /**
375  * set up default values for a 75xdpi, 150 ydpi scan
376  */
377 static void
rts8891_set_default_regs(SANE_Byte * scanner_regs)378 rts8891_set_default_regs (SANE_Byte * scanner_regs)
379 {
380   SANE_Byte default_75[RTS8891_MAX_REGISTERS] =
381     { 0xe5, 0x41, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x0a, 0x0a, 0x0a, 0x70,
382     0x00, 0x00, 0x00, 0x00, 0x28, 0x3f, 0xff, 0x20, 0xf8, 0x28, 0x07, 0x00,
383     0xff, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
384     0x00, 0x3a, 0xf2, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
385     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386     0x00, 0x00, 0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
387     0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x8c,
388     0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
390     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
391     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe1, 0x14, 0x18, 0x15, 0x00,
393     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
394     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
395     0x00, 0xff, 0x3f, 0x80, 0x68, 0x00, 0x00,
396     0x00, 0x00, 0x01, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
397     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc,
398     0x27, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
399     0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
400     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00,
401     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
402     0x0e, 0x00, 0x00, 0xf0, 0xff, 0xf5, 0xf7, 0xea, 0x0b, 0x03, 0x05, 0x86,
403     0x1b, 0x30, 0xf6, 0xa2, 0x27, 0x00, 0x00,
404     0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
405     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406     0x00, 0x00, 0x00, 0x00
407   };
408   unsigned int i;
409   for (i = 0; i < RTS8891_MAX_REGISTERS; i++)
410     scanner_regs[i] = default_75[i];
411 }
412 
413 static SANE_Status
rts8891_move(struct Rts8891_Device * device,SANE_Byte * regs,SANE_Int distance,SANE_Bool forward)414 rts8891_move (struct Rts8891_Device *device, SANE_Byte * regs,
415 	      SANE_Int distance, SANE_Bool forward)
416 {
417   SANE_Status status = SANE_STATUS_GOOD;
418   SANE_Byte regs10, regs11;
419 
420   DBG (DBG_proc, "rts8891_move: start\n");
421   DBG (DBG_io, "rts8891_move: %d lines %s, sensor=%d\n", distance,
422        forward == SANE_TRUE ? "forward" : "backward", device->sensor);
423 
424   /* prepare scan */
425   rts8891_set_default_regs (regs);
426   if (device->sensor != SENSOR_TYPE_4400
427       && device->sensor != SENSOR_TYPE_4400_BARE)
428     {
429       regs10 = 0x20;
430       regs11 = 0x28;
431     }
432   else
433     {
434       regs10 = 0x10;
435       regs11 = 0x2a;
436     }
437 
438   regs[0x32] = 0x80;
439   regs[0x33] = 0x81;
440   regs[0x35] = 0x10;
441   regs[0x36] = 0x24;
442   regs[0x39] = 0x02;
443   regs[0x3a] = 0x0e;
444 
445   regs[0x64] = 0x01;
446   regs[0x65] = 0x20;
447 
448   regs[0x79] = 0x20;
449   regs[0x7a] = 0x01;
450 
451   regs[0x80] = 0x32;
452   regs[0x82] = 0x33;
453   regs[0x85] = 0x46;
454   regs[0x86] = 0x0b;
455   regs[0x87] = 0x8c;
456   regs[0x88] = 0x10;
457   regs[0x89] = 0xb2;
458   regs[0x8d] = 0x3b;
459   regs[0x8e] = 0x60;
460 
461   regs[0x90] = 0x1c;
462   regs[0xb2] = 0x16;		/* 0x10 : stop when at home, 0x04: no data */
463   regs[0xc0] = 0x00;
464   regs[0xc1] = 0x00;
465   regs[0xc3] = 0x00;
466   regs[0xc4] = 0x00;
467   regs[0xc5] = 0x00;
468   regs[0xc6] = 0x00;
469   regs[0xc7] = 0x00;
470   regs[0xc8] = 0x00;
471   regs[0xca] = 0x00;
472   regs[0xcd] = 0x00;
473   regs[0xce] = 0x00;
474   regs[0xcf] = 0x00;
475   regs[0xd0] = 0x00;
476   regs[0xd1] = 0x00;
477   regs[0xd2] = 0x00;
478   regs[0xd3] = 0x00;
479   regs[0xd4] = 0x00;
480   regs[0xd6] = 0x6b;
481   regs[0xd7] = 0x00;
482   regs[0xd8] = 0x00;
483   regs[0xd9] = 0xad;
484   regs[0xda] = 0xa7;
485   regs[0xe2] = 0x17;
486   regs[0xe3] = 0x0d;
487   regs[0xe4] = 0x06;
488   regs[0xe5] = 0xf9;
489   regs[0xe7] = 0x53;
490   regs[0xe8] = 0x02;
491   regs[0xe9] = 0x02;
492 
493   /* hp4400 sensors */
494   if (device->sensor == SENSOR_TYPE_4400
495       || device->sensor == SENSOR_TYPE_4400_BARE)
496     {
497       regs[0x13] = 0x39;	/* 0x20 */
498       regs[0x14] = 0xf0;	/* 0xf8 */
499       regs[0x15] = 0x29;	/* 0x28 */
500       regs[0x16] = 0x0f;	/* 0x07 */
501       regs[0x17] = 0x10;	/* 0x00 */
502       regs[0x23] = 0x00;	/* 0xff */
503       regs[0x35] = 0x29;	/* 0x10 */
504       regs[0x36] = 0x21;	/* 0x24 */
505       regs[0x39] = 0x00;	/* 0x02 */
506       regs[0x80] = 0xb0;	/* 0x32 */
507       regs[0x82] = 0xb1;	/* 0x33 */
508       regs[0xe2] = 0x0b;	/* 0x17 */
509       regs[0xe5] = 0xf3;	/* 0xf9 */
510       regs[0xe6] = 0x01;	/* 0x00 */
511     }
512 
513   /* disable CCD */
514   regs[0] = 0xf5;
515 
516   sanei_rts88xx_set_status (device->devnum, regs, regs10, regs11);
517   sanei_rts88xx_set_scan_area (regs, distance, distance + 1, 100, 200);
518   sanei_rts88xx_set_gain (regs, 16, 16, 16);
519   sanei_rts88xx_set_offset (regs, 127, 127, 127);
520 
521   /* forward/backward */
522   /* 0x2c is forward, 0x24 backward */
523   if (forward == SANE_TRUE)
524     {				/* forward */
525       regs[0x36] = regs[0x36] | 0x08;
526     }
527   else
528     {				/* backward */
529       regs[0x36] = regs[0x36] & 0xf7;
530     }
531 
532   /* write registers values */
533   status = rts8891_write_all (device->devnum, regs, RTS8891_MAX_REGISTERS);
534 
535   /* commit it */
536   rts8891_commit (device->devnum, 0x00);
537 
538   return status;
539 }
540 
541  /**
542   * wait for the scanning head to reach home position
543   */
544 static SANE_Status
rts8891_wait_for_home(struct Rts8891_Device * device,SANE_Byte * regs)545 rts8891_wait_for_home (struct Rts8891_Device *device, SANE_Byte * regs)
546 {
547   SANE_Status status = SANE_STATUS_GOOD;
548   SANE_Byte motor, sensor, reg;
549 
550   DBG (DBG_proc, "rts8891_wait_for_home: start\n");
551 
552   /* wait for controller home bit to raise, no timeout */
553   /* at each loop we check that motor is on, then that the sensor bit it cleared */
554   do
555     {
556       sanei_rts88xx_read_reg (device->devnum, CONTROL_REG, &motor);
557       sanei_rts88xx_read_reg (device->devnum, CONTROLER_REG, &sensor);
558     }
559   while ((motor & 0x08) && ((sensor & 0x02) == 0));
560 
561   /* flag that device has finished parking */
562   device->parking=SANE_FALSE;
563 
564   /* check for error */
565   if (((motor & 0x08) == 0x00) && ((sensor & 0x02) == 0))
566     {
567       DBG (DBG_error,
568 	   "rts8891_wait_for_home: error, motor stopped before head parked\n");
569       status = SANE_STATUS_INVAL;
570     }
571 
572   /* re-enable CCD */
573   regs[0] = regs[0] & 0xef;
574 
575   sanei_rts88xx_cancel (device->devnum);
576 
577   /* reset ? so we don't need to read data */
578   reg = 0;
579   /* b7: movement on/off, b3-b0 : movement divisor */
580   sanei_rts88xx_write_reg (device->devnum, 0x33, &reg);
581   sanei_rts88xx_write_reg (device->devnum, 0x33, &reg);
582   /* movement direction */
583   sanei_rts88xx_write_reg (device->devnum, 0x36, &reg);
584   sanei_rts88xx_cancel (device->devnum);
585 
586   DBG (DBG_proc, "rts8891_wait_for_home: end\n");
587   return status;
588 }
589 
590  /**
591   * move the head backward by a huge line number then poll home sensor until
592   * head has get back home. We have our own copy of the registers to avoid
593   * messing scanner status
594   */
595 static SANE_Status
rts8891_park(struct Rts8891_Device * device,SANE_Byte * regs,SANE_Bool wait)596 rts8891_park (struct Rts8891_Device *device, SANE_Byte *regs, SANE_Bool wait)
597 {
598   SANE_Status status = SANE_STATUS_GOOD;
599 
600   DBG (DBG_proc, "rts8891_park: start\n");
601 
602   device->parking=SANE_TRUE;
603   rts8891_move (device, regs, 8000, SANE_FALSE);
604 
605   if(wait==SANE_TRUE)
606     {
607       status = rts8891_wait_for_home (device,regs);
608     }
609 
610   DBG (DBG_proc, "rts8891_park: end\n");
611   return status;
612 }
613 
614 /* reads data from scanner.
615  * First we wait for some data to be available and then loop reading
616  * from scanner until the required amount is reached.
617  * We handle non blocking I/O by returning immediately (with SANE_STATUS_BUSY)
618  * if there is no data available from scanner. But once read is started,
619  * all the required amount is read. Once wait for data succeeded, we still poll
620  * for data in order no to read it too fast, but we don' take care of non blocking
621  * mode since we cope with it on first data wait.
622  */
623 static SANE_Status
read_data(struct Rts8891_Session * session,SANE_Byte * dest,SANE_Int length)624 read_data (struct Rts8891_Session *session, SANE_Byte * dest, SANE_Int length)
625 {
626   SANE_Status status = SANE_STATUS_GOOD;
627   SANE_Int count, read, len, dummy;
628   struct Rts8891_Device *dev = session->dev;
629   static FILE *raw = NULL;	/* for debugging purpose we need it static */
630   SANE_Byte control = 0x08;
631   unsigned char buffer[RTS88XX_MAX_XFER_SIZE];
632 
633   DBG (DBG_proc, "read_data: start\n");
634   DBG (DBG_proc, "read_data: requiring %d bytes\n", length);
635 
636   /* wait for data being available and handle non blocking mode */
637   /* only when data reading hasn't produce any data yet */
638   if (dev->read == 0)
639     {
640       do
641 	{
642 	  status = sanei_rts88xx_data_count (dev->devnum, &count);
643 	  if (status != SANE_STATUS_GOOD)
644 	    {
645 	      DBG (DBG_error, "read_data: failed to wait for data\n");
646 	      return status;
647 	    }
648 	  if (count == 0)
649 	    {
650 	      sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
651 	      if ((control & 0x08) == 0 && (count == 0))
652 		{
653 		  DBG (DBG_error,
654 		       "read_data: scanner stopped being busy before data are available\n");
655 		  return SANE_STATUS_IO_ERROR;
656 		}
657 	    }
658 
659 	  /* in case there is no data, we return BUSY since this mean    */
660 	  /* that scanning head hasn't reach is position and data hasn't */
661 	  /* come yet */
662 	  if (session->non_blocking && count == 0)
663 	    {
664 
665 	      dev->regs[LAMP_REG] = 0x8d;
666 	      sanei_rts88xx_write_reg (dev->devnum, LAMP_REG,
667 				       &(dev->regs[LAMP_REG]));
668 	      DBG (DBG_io, "read_data: no data available\n");
669 	      DBG (DBG_proc, "read_data: end\n");
670 	      return SANE_STATUS_DEVICE_BUSY;
671 	    }
672 	}
673       while (count == 0);
674     }
675   else
676     {				/* start of read for a new block */
677       status = sanei_rts88xx_data_count (dev->devnum, &count);
678       if (status != SANE_STATUS_GOOD)
679 	{
680 	  DBG (DBG_error, "read_data: failed to wait for data\n");
681 	  return status;
682 	}
683       if (count == 0)
684 	{
685 	  sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
686 	  if ((control & 0x08) == 0 && (count == 0))
687 	    {
688 	      DBG (DBG_error,
689 		   "read_data: scanner stopped being busy before data are available\n");
690 	      return SANE_STATUS_IO_ERROR;
691 	    }
692 	}
693     }
694 
695   /* fill scanned data buffer */
696   read = 0;
697 
698   /* now loop reading data until we have the amount requested */
699   /* we also take care of not reading too much data           */
700   while (read < length && dev->read < dev->to_read
701 	 && ((control & 0x08) == 0x08))
702     {
703       /* used to sync */
704       if (dev->read == 0)
705 	{
706 	  status = sanei_rts88xx_data_count (dev->devnum, &dummy);
707 	  if (status != SANE_STATUS_GOOD)
708 	    {
709 	      DBG (DBG_error, "read_data: failed to read data count\n");
710 	      return status;
711 	    }
712 	}
713 
714       /* if there is data to read, read it */
715       if (count > 0)
716 	{
717 	  len = count;
718 
719 	  if (len > RTS88XX_MAX_XFER_SIZE)
720 	    {
721 	      len = RTS88XX_MAX_XFER_SIZE;
722 	    }
723 
724 	  /* we only read even size blocks of data */
725 	  if (len & 1)
726 	    {
727 	      DBG (DBG_io, "read_data: round to next even number\n");
728 	      len++;
729 	    }
730 
731 	  if (len > length - read)
732 	    {
733 	      len = length - read;
734 	    }
735 
736 	  status = sanei_rts88xx_read_data (dev->devnum, &len, dest + read);
737 	  if (status != SANE_STATUS_GOOD)
738 	    {
739 	      DBG (DBG_error, "read_data: failed to read from scanner\n");
740 	      return status;
741 	    }
742 
743 	  /* raw data tracing */
744 	  if (DBG_LEVEL >= DBG_io2)
745 	    {
746 	      /* open a new file only when no data scanned */
747 	      if (dev->read == 0)
748 		{
749 		  raw = fopen ("raw_data.pnm", "wb");
750 		  if (raw != NULL)
751 		    {
752 		      /* PNM header */
753 		      fprintf (raw, "P%c\n%d %d\n255\n",
754 			       session->params.format ==
755 			       SANE_FRAME_RGB
756 			       || session->emulated_gray ==
757 			       SANE_TRUE ? '6' : '5', dev->pixels,
758 			       dev->lines);
759 		    }
760 		}
761 	      if (raw != NULL)
762 		{
763 		  fwrite (dest + read, 1, len, raw);
764 		}
765 	    }
766 
767 	  /* move pointer and counter */
768 	  read += len;
769 	  dev->read += len;
770 	  DBG (DBG_io2, "read_data: %d/%d\n", dev->read, dev->to_read);
771 	}
772 
773       /* in fast scan mode, read data count
774        * in slow scan, head moves by the amount of data read */
775       status = sanei_rts88xx_data_count (dev->devnum, &count);
776       if (status != SANE_STATUS_GOOD)
777 	{
778 	  DBG (DBG_error, "read_data: failed to read data count\n");
779 	  return status;
780 	}
781 
782       /* if no data, check if still scanning */
783       if (count == 0 && dev->read < dev->to_read)
784 	{
785 	  sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
786 	  if ((control & 0x08) == 0x00)
787 	    {
788 	      DBG (DBG_error,
789 		   "read_data: scanner stopped being busy before data are available\n");
790 	      return SANE_STATUS_IO_ERROR;
791 	    }
792 	}
793     }
794 
795   /* end of physical reads */
796   if (dev->read >= dev->to_read)
797     {
798       /* check there is no more data in case of a bug */
799       sanei_rts88xx_data_count (dev->devnum, &count);
800       if (count > 0)
801 	{
802 	  DBG (DBG_warn,
803 	       "read_data: %d bytes are still available from scanner\n",
804 	       count);
805 
806 	  /* flush left-over data */
807 	  while (count > 0)
808 	    {
809 	      len = count;
810 	      if (len > RTS88XX_MAX_XFER_SIZE)
811 		{
812 		  len = RTS88XX_MAX_XFER_SIZE;
813 		}
814 
815 	      /* we only read even size blocks of data */
816 	      if (len & 1)
817 		{
818 		  len++;
819 		}
820 	      sanei_rts88xx_read_data (dev->devnum, &len, buffer);
821 	      sanei_rts88xx_data_count (dev->devnum, &count);
822 	    }
823 	}
824 
825       /* wait for motor to stop at the end of the scan */
826       do
827 	{
828 	  sanei_rts88xx_read_reg (dev->devnum, CONTROL_REG, &control);
829 	}
830       while ((control & 0x08) != 0);
831 
832       /* close log file if needed */
833       if (DBG_LEVEL >= DBG_io2)
834 	{
835 	  if (raw != NULL)
836 	    {
837 	      fclose (raw);
838 	      raw = NULL;
839 	    }
840 	}
841     }
842 
843   DBG (DBG_io, "read_data: read %d bytes from scanner\n", length);
844   DBG (DBG_proc, "read_data: end\n");
845   return status;
846 }
847 
848   /**
849    * set scanner idle before leaving xxx_quiet()
850 write_reg(0x33,1)=0x00
851 write_reg(0x33,1)=0x00
852 write_reg(0x36,1)=0x00
853 prepare();
854 ------
855 write_reg(LAMP_REG,1)=0x80
856 write_reg(LAMP_REG,1)=0xad
857 read_reg(0x14,2)=0xf8 0x28
858 write_reg(0x14,2)=0x78 0x28
859 get_status()=0x20 0x3f
860 read_reg(0xb1,1)=0x00
861 read_control()=0x00
862 reset_lamp()=(0x20,0x3f)
863 write_reg(LAMP_REG,1)=0x8d
864 write_reg(LAMP_REG,1)=0xad
865    */
866 
867 /* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */
868