1 /** @file u12-io.c
2  *  @brief The I/O functions to the U12 backend stuff.
3  *
4  * Copyright (c) 2003-2004 Gerhard Jaeger <gerhard@gjaeger.de>
5  * GeneSys Logic I/O stuff derived from canon630u-common.c which has
6  * been written by Nathan Rutman <nathan@gordian.com>
7  *
8  * History:
9  * - 0.01 - initial version
10  * - 0.02 - changed u12io_GetFifoLength() behaviour
11  *        - added delays to reset function
12  * .
13  * <hr>
14  * This file is part of the SANE package.
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation; either version 2 of the
19  * License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
28  *
29  * As a special exception, the authors of SANE give permission for
30  * additional uses of the libraries contained in this release of SANE.
31  *
32  * The exception is that, if you link a SANE library with other files
33  * to produce an executable, this does not by itself cause the
34  * resulting executable to be covered by the GNU General Public
35  * License.  Your use of that executable is in no way restricted on
36  * account of linking the SANE library code into it.
37  *
38  * This exception does not, however, invalidate any other reasons why
39  * the executable file might be covered by the GNU General Public
40  * License.
41  *
42  * If you submit changes to SANE to the maintainers to be included in
43  * a subsequent release, you agree by submitting the changes that
44  * those changes may be distributed with this exception intact.
45  *
46  * If you write modifications of your own for SANE, it is your choice
47  * whether to permit this exception to apply to your modifications.
48  * If you do not wish that, delete this exception notice.
49  * <hr>
50  */
51 
52 /** Format:
53  * cacheLen[0]  =  ASIC-ID
54  * cacheLen[1]  =  SCANSTATE ?
55  * cacheLen[2]  =  REG-STATUS ?
56  * cacheLen[3]  =  ??
57  * cacheLen[4]  =  FIFO-LEN (RED)   HiByte  LW
58  * cacheLen[5]  =  FIFO-LEN (RED)   LoByte  LW
59  * cacheLen[6]  =  FIFO-LEN (RED)   LoByte  HW
60  * cacheLen[7]  =  FIFO-LEN (GREEN) HiByte  LW
61  * cacheLen[8]  =  FIFO-LEN (GREEN) LoByte  LW
62  * cacheLen[9]  =  FIFO-LEN (GREEN) LoByte  HW
63  * cacheLen[10] =  FIFO-LEN (BLUE)  HiByte  LW
64  * cacheLen[11] =  FIFO-LEN (BLUE)  LoByte  LW
65  * cacheLen[12] =  FIFO-LEN (BLUE)  LoByte  HW
66  */
67 static SANE_Byte cacheLen[13];
68 
69 /** This function is used to detect a cancel condition,
70  * our ESC key is the SIGUSR1 signal. It is sent by the backend when the
71  * cancel button has been pressed
72  *
73  * @param - none
74  * @return the function returns SANE_TRUE if a cancel condition has been
75  *  detected, if not, it returns SANE_FALSE
76  */
u12io_IsEscPressed(void)77 static SANE_Bool u12io_IsEscPressed( void )
78 {
79 	sigset_t sigs;
80 
81 	sigpending( &sigs );
82 	if( sigismember( &sigs, SIGUSR1 )) {
83 		DBG( _DBG_INFO, "SIGUSR1 is pending --> Cancel detected\n" );
84 		return SANE_TRUE;
85 	}
86 
87 	return SANE_FALSE;
88 }
89 
90 /** fall asleep for some micro-seconds...
91  */
u12io_udelay(unsigned long usec)92 static void u12io_udelay( unsigned long usec )
93 {
94 	struct timeval now, deadline;
95 
96 	if( usec == 0 )
97 		return;
98 
99 	gettimeofday( &deadline, NULL );
100 	deadline.tv_usec += usec;
101 	deadline.tv_sec  += deadline.tv_usec / 1000000;
102 	deadline.tv_usec %= 1000000;
103 
104 	do {
105 		gettimeofday( &now, NULL );
106 	} while ((now.tv_sec < deadline.tv_sec) ||
107 		(now.tv_sec == deadline.tv_sec && now.tv_usec < deadline.tv_usec));
108 }
109 
110 /** Initializes a timer.
111  * @param timer - pointer to the timer to start
112  * @param us    - timeout value in micro-seconds
113  */
u12io_StartTimer(TimerDef * timer,unsigned long us)114 static void u12io_StartTimer( TimerDef *timer , unsigned long us )
115 {
116 	struct timeval start_time;
117 
118 	gettimeofday( &start_time, NULL );
119 	*timer = start_time.tv_sec * 1e6 + start_time.tv_usec + us;
120 }
121 
122 /** Checks if a timer has been expired or not.
123  * @param timer - pointer to the timer to check
124  * @return Function returns  SANE_TRUE when the timer has been expired,
125  *         otherwise SANE_FALSE
126  */
u12io_CheckTimer(TimerDef * timer)127 static SANE_Bool u12io_CheckTimer( TimerDef *timer )
128 {
129 	struct timeval current_time;
130 
131 	gettimeofday(&current_time, NULL);
132 
133 	if((current_time.tv_sec * 1e6 + current_time.tv_usec) > *timer )
134 		return SANE_TRUE;
135 
136 	return SANE_FALSE;
137 }
138 
139 /* GL640 communication functions for Genesys Logic GL640USB
140  * USB-IEEE1284 parallel port bridge
141  */
142 
143 /* Assign status and verify a good return code */
144 #define CHK(A) {if( (status = A) != SANE_STATUS_GOOD ) { \
145                  DBG( _DBG_ERROR, "Failure on line of %s: %d\n", __FILE__, \
146                       __LINE__ ); return A; }}
147 
148 /** Register codes for the bridge.  These are NOT the registers for the ASIC
149  *  on the other side of the bridge.
150  */
151 typedef enum
152 {
153 	GL640_BULK_SETUP     = 0x82,
154 	GL640_EPP_ADDR       = 0x83,
155 	GL640_EPP_DATA_READ  = 0x84,
156 	GL640_EPP_DATA_WRITE = 0x85,
157 	GL640_SPP_STATUS     = 0x86,
158 	GL640_SPP_CONTROL    = 0x87,
159 	GL640_SPP_DATA       = 0x88,
160 	GL640_GPIO_OE        = 0x89,
161 	GL640_GPIO_READ      = 0x8a,
162 	GL640_GPIO_WRITE     = 0x8b
163 } GL640_Request;
164 
165 /** for setting up bulk transfers */
166 static SANE_Byte bulk_setup_data[] = { 0, 0x11, 0, 0, 0, 0, 0, 0 };
167 
168 /** Write to the usb-parallel port bridge.
169  */
170 static SANE_Status
gl640WriteControl(int fd,GL640_Request req,u_char * data,unsigned int size)171 gl640WriteControl(int fd, GL640_Request req, u_char * data, unsigned int size)
172 {
173 	SANE_Status status;
174 
175 	status = sanei_usb_control_msg( fd,
176 				  /* rqttype */ USB_TYPE_VENDOR |
177 				  USB_RECIP_DEVICE | USB_DIR_OUT /*0x40 */ ,
178 				  /* rqt */ (size > 1) ? 0x04 : 0x0C,
179 				  /* val */ (SANE_Int) req,
180 				  /* ind */ 0,
181 				  /* len */ size,
182 				  /* dat */ data);
183 
184 	if( status != SANE_STATUS_GOOD ) {
185 		DBG( _DBG_ERROR, "gl640WriteControl error\n");
186 	}
187 	return status;
188 }
189 
190 /** Read from the usb-parallel port bridge.
191  */
192 static SANE_Status
gl640ReadControl(int fd,GL640_Request req,u_char * data,unsigned int size)193 gl640ReadControl( int fd, GL640_Request req, u_char *data, unsigned int size )
194 {
195 	SANE_Status status;
196 
197 	status = sanei_usb_control_msg( fd,
198 				  /* rqttype */ USB_TYPE_VENDOR |
199 				  USB_RECIP_DEVICE | USB_DIR_IN /*0xc0 */ ,
200 				  /* rqt */ (size > 1) ? 0x04 : 0x0C,
201 				  /* val */ (SANE_Int) req,
202 				  /* ind */ 0,
203 				  /* len */ size,
204 				  /* dat */ data);
205 
206 	if( status != SANE_STATUS_GOOD ) {
207 		DBG( _DBG_ERROR, "gl640ReadControl error\n");
208 	}
209 	return status;
210 }
211 
212 /** Wrappers to write a single byte to the bridge */
213 static inline SANE_Status
gl640WriteReq(int fd,GL640_Request req,u_char data)214 gl640WriteReq( int fd, GL640_Request req, u_char data )
215 {
216 	return gl640WriteControl( fd, req, &data, 1);
217 }
218 
219 /** Wrappers to read a single byte from the bridge */
220 static inline SANE_Status
gl640ReadReq(int fd,GL640_Request req,u_char * data)221 gl640ReadReq( int fd, GL640_Request req, u_char *data )
222 {
223 	return gl640ReadControl( fd, req, data, 1 );
224 }
225 
226 /** Write USB bulk data
227  * setup is an apparently scanner-specific sequence:
228  * {(0=read, 1=write), 0x00, 0x00, 0x00, sizelo, sizehi, 0x00, 0x00}
229  * setup[1] = 0x11 --> data to register
230  * setup[1] = 0x01 --> data to scanner memory
231  */
232 static SANE_Status
gl640WriteBulk(int fd,u_char * setup,u_char * data,size_t size)233 gl640WriteBulk( int fd, u_char *setup, u_char *data, size_t size )
234 {
235 	SANE_Status status;
236 
237 	setup[0] = 1;
238 	setup[4] = (size) & 0xFF;
239 	setup[5] = (size >> 8) & 0xFF;
240 	setup[6] = 0;
241 
242 	CHK (gl640WriteControl (fd, GL640_BULK_SETUP, setup, 8));
243 
244 	status = sanei_usb_write_bulk (fd, data, &size);
245 	if( status != SANE_STATUS_GOOD ) {
246 		DBG( _DBG_ERROR, "gl640WriteBulk error\n");
247 	}
248 	return status;
249 }
250 
251 /** Read USB bulk data
252  * setup is an apparently scanner-specific sequence:
253  * {(0=read, 1=write), 0x00, 0x00, 0x00, sizelo, sizehi, 0x00, 0x00}
254  * setup[1] = 0x00 --> data from scanner memory
255  * setup[1] = 0x0c --> data from scanner fifo?
256  */
257 static SANE_Status
gl640ReadBulk(int fd,u_char * setup,u_char * data,size_t size,int mod)258 gl640ReadBulk( int fd, u_char *setup, u_char *data, size_t size, int mod )
259 {
260 	SANE_Byte  *len_info;
261 	size_t      complete, current, toget;
262 	SANE_Status status;
263 
264 	setup[0] = 0;
265 	setup[4] = (size) & 0xFF;
266 	setup[5] = (size >> 8) & 0xFF;
267 	setup[6] = mod;
268 
269 	CHK (gl640WriteControl (fd, GL640_BULK_SETUP, setup, 8));
270 
271 	len_info = NULL;
272 	toget    = size;
273 	if( mod ) {
274 		toget   *= mod;
275 		len_info = data + toget;
276 		toget   += 13;
277 	}
278 
279 	for( complete = 0; complete < toget; ) {
280 
281 		current = toget - complete;
282 		status = sanei_usb_read_bulk( fd, data, &current );
283 		if( status != SANE_STATUS_GOOD ) {
284 			DBG( _DBG_ERROR, "gl640ReadBulk error\n");
285 			break;
286 		}
287 		data     += current;
288 		complete += current;
289 	}
290 	if( len_info ) {
291 		memcpy( cacheLen, len_info, 13 );
292 	}
293 	return status;
294 }
295 
296 /* now the functions to access PP registers */
297 
298 /** read the contents of the status register */
299 static SANE_Byte
inb_status(int fd)300 inb_status( int fd )
301 {
302 	u_char data = 0xff;
303 
304 	gl640ReadReq( fd, GL640_SPP_STATUS, &data );
305 	return data;
306 }
307 
308 /** write a byte to the SPP data port */
309 static SANE_Status
outb_data(int fd,u_char data)310 outb_data( int fd, u_char data )
311 {
312 	return gl640WriteReq( fd, GL640_SPP_DATA, data);
313 }
314 
315 /** write to the parport control register */
316 static SANE_Status
outb_ctrl(int fd,u_char data)317 outb_ctrl( int fd, u_char data )
318 {
319 	return gl640WriteReq( fd, GL640_SPP_CONTROL, data);
320 }
321 
322 /************************* ASIC access stuff *********************************/
323 
324 /** write a register number to the ASIC
325  */
u12io_RegisterToScanner(U12_Device * dev,SANE_Byte reg)326 static void u12io_RegisterToScanner( U12_Device *dev, SANE_Byte reg )
327 {
328 	if( dev->mode == _PP_MODE_EPP ) {
329 
330 		gl640WriteReq( dev->fd, GL640_EPP_ADDR, reg );
331 
332 	} else {
333 
334 		/* write register number to read from to SPP data-port
335 		 */
336 		outb_data( dev->fd, reg );
337 
338 		/* signal that to the ASIC */
339 		outb_ctrl( dev->fd, _CTRL_SIGNAL_REGWRITE );
340 		_DODELAY(20);
341 		outb_ctrl( dev->fd, _CTRL_END_REGWRITE );
342 	}
343 }
344 
345 /** as the name says, we switch to SPP mode
346  */
u12io_SwitchToSPPMode(U12_Device * dev)347 static void u12io_SwitchToSPPMode( U12_Device *dev )
348 {
349 	dev->mode = _PP_MODE_SPP;
350 	outb_ctrl( dev->fd, _CTRL_GENSIGNAL );
351 }
352 
353 /** as the name says, we switch to SPP mode
354  */
u12io_SwitchToEPPMode(U12_Device * dev)355 static void u12io_SwitchToEPPMode( U12_Device *dev )
356 {
357 	u12io_RegisterToScanner( dev, REG_EPPENABLE );
358 	dev->mode = _PP_MODE_EPP;
359 }
360 
361 /** read data from SPP status port
362  */
u12io_DataFromSPP(U12_Device * dev)363 static SANE_Byte u12io_DataFromSPP( U12_Device *dev )
364 {
365 	SANE_Byte data, tmp;
366 
367 	/* read low nibble */
368 	tmp = inb_status( dev->fd );
369 
370 	outb_ctrl( dev->fd, (_CTRL_GENSIGNAL + _CTRL_STROBE));
371 
372 	/* read high nibble */
373 	data  = inb_status( dev->fd );
374 	data &= 0xf0;
375 
376 	/* combine with low nibble */
377 	data |= (tmp >> 4);
378 	return data;
379 }
380 
381 /** Read the content of specific ASIC register
382  */
u12io_DataFromRegister(U12_Device * dev,SANE_Byte reg)383 static SANE_Byte u12io_DataFromRegister( U12_Device *dev, SANE_Byte reg )
384 {
385 	SANE_Byte val;
386 
387 	if( dev->mode == _PP_MODE_EPP ) {
388 		gl640WriteReq( dev->fd, GL640_EPP_ADDR, reg );
389 		gl640ReadReq ( dev->fd, GL640_EPP_DATA_READ, &val );
390 	} else {
391 
392 		u12io_RegisterToScanner( dev, reg );
393 		val = u12io_DataFromSPP( dev );
394 	}
395 	return val;
396 }
397 
398 /**
399  */
u12io_CloseScanPath(U12_Device * dev)400 static void u12io_CloseScanPath( U12_Device *dev )
401 {
402 	DBG( _DBG_INFO, "u12io_CloseScanPath()\n" );
403 /* FIXME: Probably not needed */
404 #if 0
405 	u12io_RegisterToScanner( dev, 0xff );
406 #endif
407 	u12io_RegisterToScanner( dev, REG_SWITCHBUS );
408 
409 	dev->mode = _PP_MODE_SPP;
410 }
411 
412 /** try to connect to scanner
413  */
u12io_OpenScanPath(U12_Device * dev)414 static SANE_Bool u12io_OpenScanPath( U12_Device *dev )
415 {
416 	u_char tmp;
417 
418 	DBG( _DBG_INFO, "u12io_OpenScanPath()\n" );
419 
420 	u12io_SwitchToSPPMode( dev );
421 
422 	outb_data( dev->fd, _ID_TO_PRINTER );
423 	_DODELAY(20);
424 
425 	outb_data( dev->fd, _ID1ST );
426 	_DODELAY(5);
427 
428 	outb_data( dev->fd, _ID2ND );
429 	_DODELAY(5);
430 
431 	outb_data( dev->fd, _ID3RD );
432 	_DODELAY(5);
433 
434 	outb_data( dev->fd, _ID4TH );
435 	_DODELAY(5);
436 
437 	tmp = u12io_DataFromRegister( dev, REG_ASICID );
438 	if( ASIC_ID == tmp ) {
439 		u12io_SwitchToEPPMode( dev );
440 		return SANE_TRUE;
441 	}
442 
443 	DBG( _DBG_ERROR, "u12io_OpenScanPath() failed!\n" );
444 	return SANE_FALSE;
445 }
446 
447 /** Write data to asic (SPP mode only)
448  */
u12io_DataToScanner(U12_Device * dev,SANE_Byte bValue)449 static void u12io_DataToScanner( U12_Device *dev , SANE_Byte bValue )
450 {
451 	if( dev->mode != _PP_MODE_SPP ) {
452 		DBG( _DBG_ERROR, "u12io_DataToScanner() in wrong mode!\n" );
453 		return;
454 	}
455 
456     /* output data */
457 	outb_data( dev->fd, bValue );
458 
459 	/* notify asic there is data */
460 	outb_ctrl( dev->fd, _CTRL_SIGNAL_DATAWRITE );
461 
462 	/* end write cycle */
463 	outb_ctrl( dev->fd, _CTRL_END_DATAWRITE );
464 }
465 
466 /** Write data to specific ASIC's register
467  */
u12io_DataToRegister(U12_Device * dev,SANE_Byte reg,SANE_Byte data)468 static SANE_Status u12io_DataToRegister( U12_Device *dev,
469                                          SANE_Byte reg, SANE_Byte data )
470 {
471 	SANE_Status status;
472 	SANE_Byte   buf[2];
473 
474 	if( dev->mode == _PP_MODE_EPP ) {
475 
476 		buf[0] = reg;
477 		buf[1] = data;
478 
479 		bulk_setup_data[1] = 0x11;
480 		CHK( gl640WriteBulk ( dev->fd, bulk_setup_data, buf, 2 ));
481 
482 	} else {
483 
484 		u12io_RegisterToScanner( dev, reg );
485 		u12io_DataToScanner( dev, data );
486 	}
487 	return SANE_STATUS_GOOD;
488 }
489 
490 /** Write data-buffer to specific ASIC's register
491  *  The format in the buffer is
492  *  reg(0),val(0),reg(1),val(1),..., reg(len-1),val(len-1)
493  */
u12io_DataToRegs(U12_Device * dev,SANE_Byte * buf,int len)494 static SANE_Status u12io_DataToRegs( U12_Device *dev, SANE_Byte *buf, int len )
495 {
496 	SANE_Status status;
497 
498 	if( dev->mode != _PP_MODE_EPP ) {
499 		DBG( _DBG_ERROR, "u12io_DataToRegs() in wrong mode!\n" );
500 		return SANE_STATUS_IO_ERROR;
501 	}
502 
503 	bulk_setup_data[1] = 0x11;
504 	CHK( gl640WriteBulk ( dev->fd, bulk_setup_data, buf, len*2 ));
505 	return SANE_STATUS_GOOD;
506 }
507 
508 /** write data to the DAC
509  */
510 static void
u12io_DataRegisterToDAC(U12_Device * dev,SANE_Byte reg,SANE_Byte val)511 u12io_DataRegisterToDAC( U12_Device *dev, SANE_Byte reg, SANE_Byte val )
512 {
513 	SANE_Byte buf[6];
514 
515 	buf[0] = REG_ADCADDR;
516 	buf[1] = reg;
517 	buf[2] = REG_ADCDATA;
518 	buf[3] = val;
519 	buf[4] = REG_ADCSERIALOUT;
520  	buf[5] = val;
521 
522 	u12io_DataToRegs( dev, buf, 3 );
523 }
524 
525 /** write data block to scanner
526  */
u12io_MoveDataToScanner(U12_Device * dev,SANE_Byte * buf,int len)527 static SANE_Status u12io_MoveDataToScanner( U12_Device *dev,
528                                             SANE_Byte *buf, int len )
529 {
530 	SANE_Status status;
531 
532 /*	u12io_RegisterToScanner( dev, REG_INITDATAFIFO ); */
533 	u12io_RegisterToScanner( dev, REG_WRITEDATAMODE );
534 
535 	bulk_setup_data[1] = 0x01;
536 	CHK( gl640WriteBulk( dev->fd, bulk_setup_data, buf, len ));
537 	bulk_setup_data[1] = 0x11;
538 
539 	return SANE_STATUS_GOOD;
540 }
541 
u12io_ReadData(U12_Device * dev,SANE_Byte * buf,int len)542 static SANE_Status u12io_ReadData( U12_Device *dev, SANE_Byte *buf, int len )
543 {
544 	SANE_Status status;
545 
546 	u12io_DataToRegister( dev, REG_MODECONTROL, dev->regs.RD_ModeControl );
547 /*	u12io_RegisterToScanner( dev, REG_INITDATAFIFO ); */
548 	u12io_RegisterToScanner( dev, REG_READDATAMODE );
549 
550 	bulk_setup_data[1] = 0x00;
551 	CHK (gl640ReadBulk( dev->fd, bulk_setup_data, buf, len, 0 ));
552 	bulk_setup_data[1] = 0x11;
553 
554 	return SANE_STATUS_GOOD;
555 }
556 
557 /** perform a SW reset of ASIC P98003
558  */
u12io_SoftwareReset(U12_Device * dev)559 static void u12io_SoftwareReset( U12_Device *dev )
560 {
561 	DBG( _DBG_INFO, "Device reset (%i)!!!\n", dev->fd );
562 
563 	u12io_DataToRegister( dev, REG_TESTMODE, _SW_TESTMODE );
564 
565 	outb_data( dev->fd, _ID_TO_PRINTER );
566 	_DODELAY(20);
567 
568 	outb_data( dev->fd, _RESET1ST );
569 	_DODELAY(5);
570 	outb_data( dev->fd, _RESET2ND );
571 	_DODELAY(5);
572 	outb_data( dev->fd, _RESET3RD );
573 	_DODELAY(5);
574 	outb_data( dev->fd, _RESET4TH );
575 	_DODELAY(250);
576 }
577 
578 /**
579  */
u12io_IsConnected(U12_Device * dev)580 static SANE_Bool u12io_IsConnected( U12_Device *dev )
581 {
582 	int       c, mode;
583 	SANE_Byte tmp, rb[6];
584 
585 	DBG( _DBG_INFO, "u12io_IsConnected()\n" );
586 	tmp = inb_status( dev->fd );
587 	DBG( _DBG_INFO, "* tmp1 = 0x%02x\n", tmp );
588 
589 	gl640WriteReq( dev->fd, GL640_EPP_ADDR, REG_ASICID );
590 	gl640ReadReq ( dev->fd, GL640_EPP_DATA_READ, &tmp );
591 	DBG( _DBG_INFO, "* REG_ASICID = 0x%02x\n", tmp );
592 
593 	if( tmp != ASIC_ID ) {
594 
595 		DBG( _DBG_INFO, "* Scanner is NOT connected!\n" );
596 
597 		tmp = inb_status( dev->fd );
598 		DBG( _DBG_INFO, "* tmp2 = 0x%02x\n", tmp );
599 
600 		gl640WriteReq( dev->fd, GL640_EPP_ADDR, REG_ASICID );
601 		gl640ReadReq ( dev->fd, GL640_EPP_DATA_READ, &tmp );
602 		DBG( _DBG_INFO, "* REG_ASICID = 0x%02x\n", tmp );
603 
604 		if( tmp == 0x02 ) {
605 
606 			mode = dev->mode;
607 			dev->mode = _PP_MODE_EPP;
608 			u12io_DataToRegister( dev, REG_ADCADDR, 0x01 );
609 			u12io_DataToRegister( dev, REG_ADCDATA, 0x00 );
610 			u12io_DataToRegister( dev, REG_ADCSERIALOUT, 0x00 );
611 
612 			c = 0;
613 			_SET_REG( rb, c, REG_MODECONTROL, 0x19 );
614 			_SET_REG( rb, c, REG_STEPCONTROL, 0xff );
615 			_SET_REG( rb, c, REG_MOTOR0CONTROL, 0 );
616 			u12io_DataToRegs( dev, rb, c );
617 			dev->mode = mode ;
618 		}
619 		return SANE_FALSE;
620 	}
621 
622 	u12io_SwitchToEPPMode( dev );
623 	DBG( _DBG_INFO, "* Scanner is connected!\n" );
624 	return SANE_TRUE;
625 }
626 
627 /**
628  */
u12io_GetExtendedStatus(U12_Device * dev)629 static SANE_Byte u12io_GetExtendedStatus( U12_Device *dev )
630 {
631 	SANE_Byte b;
632 
633 	b = u12io_DataFromRegister( dev, REG_STATUS2 );
634 	if( b == 0xff )
635 		return 0;
636 	return b;
637 }
638 
639 /**
640  */
u12io_ReadMonoData(U12_Device * dev,SANE_Byte * buf,u_long len)641 static SANE_Status u12io_ReadMonoData( U12_Device *dev, SANE_Byte *buf, u_long len )
642 {
643 	SANE_Status status;
644 
645 	bulk_setup_data[1] = 0x0c;
646 	bulk_setup_data[2] = ((dev->regs.RD_ModeControl >> 3) + 1);
647 
648 	CHK (gl640ReadBulk( dev->fd, bulk_setup_data, buf, len, 1 ));
649 	bulk_setup_data[1] = 0x11;
650 	bulk_setup_data[2] = 0;
651 
652 	return SANE_STATUS_GOOD;
653 }
654 
655 /**
656  */
657 static SANE_Status
u12io_ReadColorData(U12_Device * dev,SANE_Byte * buf,u_long len)658 u12io_ReadColorData( U12_Device *dev, SANE_Byte *buf, u_long len )
659 {
660 	SANE_Status status;
661 
662 	bulk_setup_data[1] = 0x0c;
663 
664 	CHK (gl640ReadBulk( dev->fd, bulk_setup_data, buf, len, 3 ));
665 	bulk_setup_data[1] = 0x11;
666 	return SANE_STATUS_GOOD;
667 }
668 
669 /** read the recent state count
670  */
u12io_GetScanState(U12_Device * dev)671 static SANE_Byte u12io_GetScanState( U12_Device *dev )
672 {
673 	if( cacheLen[0] == 0x83 ) {
674 		DBG( _DBG_READ, "u12io_GetScanState(cached) = 0x%02x\n", cacheLen[1] );
675 		return cacheLen[1];
676     }
677 	return u12io_DataFromRegister( dev, REG_GETSCANSTATE );
678 }
679 
680 /** download a scanstate-table
681  */
u12io_DownloadScanStates(U12_Device * dev)682 static SANE_Status u12io_DownloadScanStates( U12_Device *dev )
683 {
684 	SANE_Status status;
685 	TimerDef    timer;
686 
687 	u12io_RegisterToScanner( dev, REG_SCANSTATECONTROL );
688 
689 	bulk_setup_data[1] = 0x01;
690 	CHK( gl640WriteBulk( dev->fd, bulk_setup_data,
691 	                     dev->scanStates, _SCANSTATE_BYTES ));
692 	bulk_setup_data[1] = 0x11;
693 
694 /* FIXME: refreshState probably always FALSE */
695 	if( dev->scan.refreshState ) {
696 
697 		u12io_RegisterToScanner( dev, REG_REFRESHSCANSTATE );
698 
699 		u12io_StartTimer( &timer, (_SECOND/2));
700 		do {
701 
702 			if (!( u12io_GetScanState( dev ) & _SCANSTATE_STOP))
703 				break;
704 		}
705 		while( !u12io_CheckTimer(&timer));
706 	}
707 	return SANE_STATUS_GOOD;
708 }
709 
710 /** - initializes the scan states
711  *  - sets all necessary registers
712  * FIXME: first copy to buffer, then use u12io_DataToRegs()
713  */
u12io_PutOnAllRegisters(U12_Device * dev)714 static void u12io_PutOnAllRegisters( U12_Device *dev )
715 {
716 	SANE_Byte *val, reg;
717 	SANE_Byte *rb, buf[100];
718 	int        c;
719 
720 	/* setup scan states */
721 	u12io_DownloadScanStates( dev );
722 
723 	c  = 0;
724 	rb = buf;
725 
726 	*(rb++) = REG_MODECONTROL;
727 	*(rb++) = dev->regs.RD_ModeControl;
728 	c++;
729 	*(rb++) = REG_STEPCONTROL;
730 	*(rb++) = dev->regs.RD_StepControl;
731 	c++;
732 	*(rb++) = REG_MOTOR0CONTROL;
733 	*(rb++) = dev->regs.RD_Motor0Control;
734 	c++;
735 	*(rb++) = REG_LINECONTROL;
736 	*(rb++) = dev->regs.RD_LineControl;
737 	c++;
738 	*(rb++) = REG_XSTEPTIME;
739 	*(rb++) = dev->regs.RD_XStepTime;
740 	c++;
741 	*(rb++) = REG_MODELCONTROL;
742 	*(rb++) =  dev->regs.RD_ModelControl;
743 	c++;
744 	/* the 1st register to write */
745 	val = (SANE_Byte*)&dev->regs.RD_Dpi;
746 
747 	/* 0x21 - 0x28 */
748 	for( reg = REG_DPILO; reg <= REG_THRESHOLDHI; reg++, val++ ) {
749 		*(rb++) = reg;
750 		*(rb++) = *val;
751 		c++;
752 	}
753 
754 	u12io_DataToRegs( dev, buf, c );
755 
756 	u12io_RegisterToScanner( dev, REG_INITDATAFIFO );
757 	u12io_DataToRegister( dev, REG_MODECONTROL, _ModeScan );
758 }
759 
760 /**
761  */
u12io_ResetFifoLen(void)762 static void u12io_ResetFifoLen( void )
763 {
764 	memset( cacheLen, 0, 13 );
765 }
766 
767 /**
768  */
u12io_GetFifoLength(U12_Device * dev)769 static u_long u12io_GetFifoLength( U12_Device *dev )
770 {
771 	SANE_Status status;
772 	size_t      toget;
773 	SANE_Byte   data[64];
774 	u_long      len, len_r, len_g, len_b;
775 
776 	if( cacheLen[0] == 0x83 ) {
777 
778 		DBG( _DBG_READ, "Using cached FIFO len\n" );
779 		memcpy( data, cacheLen, 13 );
780 		u12io_ResetFifoLen();
781 
782 	} else {
783 
784 		memset( bulk_setup_data, 0, 8 );
785 		bulk_setup_data[1] = 0x0c;
786 
787 		CHK (gl640WriteControl(dev->fd, GL640_BULK_SETUP, bulk_setup_data, 8));
788 
789 		toget = 13;
790 		status = sanei_usb_read_bulk( dev->fd, data, &toget );
791 		if( status != SANE_STATUS_GOOD ) {
792 			DBG( _DBG_ERROR, "ReadBulk error\n");
793 			return SANE_FALSE;
794 		}
795 		bulk_setup_data[1] = 0x11;
796 
797 		memcpy( cacheLen, data, 13 );
798 	}
799 	len_r = (u_long)data[5]  * 256 + (u_long)data[4];
800 	len_g = (u_long)data[8]  * 256 + (u_long)data[7];
801 	len_b = (u_long)data[11] * 256 + (u_long)data[10];
802 
803 	if( dev->DataInf.wPhyDataType < COLOR_TRUE24 ) {
804 		len = len_g;
805 	} else {
806 
807 		len = len_r;
808 		if( len_g < len )
809 			len = len_g;
810 		if( len_b < len )
811 			len = len_b;
812 	}
813 
814 	DBG( _DBG_READ, "FIFO-LEN: %lu %lu %lu = %lu\n", len_r, len_g, len_b, len );
815 	return len;
816 }
817 
818 /**
819  */
820 static SANE_Bool
u12io_ReadOneShadingLine(U12_Device * dev,SANE_Byte * buf,u_long len)821 u12io_ReadOneShadingLine( U12_Device *dev, SANE_Byte *buf, u_long len )
822 {
823 	TimerDef    timer;
824 	SANE_Status status;
825 
826 	DBG( _DBG_READ, "u12io_ReadOneShadingLine()\n" );
827 	u12io_StartTimer( &timer, _SECOND );
828 
829 	dev->scan.bFifoSelect = REG_GFIFOOFFSET;
830 
831 	do {
832 		u12io_ResetFifoLen();
833 		if( u12io_GetFifoLength( dev ) >= dev->regs.RD_Pixels ) {
834 
835 			status = u12io_ReadColorData( dev, buf, len );
836 			if( status != SANE_STATUS_GOOD ) {
837 				DBG( _DBG_ERROR, "ReadColorData error\n");
838 				return SANE_FALSE;
839 			}
840 			DBG( _DBG_READ, "* done\n" );
841 			return SANE_TRUE;
842 		}
843 	} while( !u12io_CheckTimer( &timer ));
844 
845 	DBG( _DBG_ERROR, "u12io_ReadOneShadingLine() failed!\n" );
846 	return SANE_FALSE;
847 }
848 
849 /* END U12-IO.C .............................................................*/
850