| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | /*.............................................................................
 | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  |  * Project : SANE library for Plustek flatbed scanners. | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  *............................................................................. | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @file plustek-usbio.c
 | 
					
						
							|  |  |  |  *  @brief Some I/O stuff. | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  |  * Based on sources acquired from Plustek Inc.<br> | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  |  * Copyright (C) 2001-2006 Gerhard Jaeger <gerhard@gjaeger.de> | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  |  * History: | 
					
						
							|  |  |  |  * History: | 
					
						
							|  |  |  |  * - 0.40 - starting version of the USB support | 
					
						
							|  |  |  |  * - 0.41 - moved some functions to a sane library (sanei_lm983x.c) | 
					
						
							|  |  |  |  * - 0.42 - no changes | 
					
						
							|  |  |  |  * - 0.43 - no changes | 
					
						
							|  |  |  |  * - 0.44 - added dump registers and dumpPic functions | 
					
						
							|  |  |  |  *        - beautyfied output of ASIC detection | 
					
						
							|  |  |  |  * - 0.45 - fixed dumpRegs | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  |  *        - added dimension stuff to dumpPic | 
					
						
							| 
									
										
										
										
											2003-09-23 07:10:53 +00:00
										 |  |  |  * - 0.46 - disabled reset prior to the detection of Merlin | 
					
						
							| 
									
										
										
										
											2003-12-18 13:24:26 +00:00
										 |  |  |  * - 0.47 - no changes | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  |  * - 0.48 - cleanup | 
					
						
							| 
									
										
										
										
											2004-11-19 12:38:46 +00:00
										 |  |  |  * - 0.49 - no changes | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  |  * - 0.50 - usbio_DetectLM983x() now returns error if register  | 
					
						
							|  |  |  |  *          could not be red | 
					
						
							|  |  |  |  *        - usbio_ResetLM983x() checks for reg7 value before writing | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  |  * - 0.51 - allow dumpRegs to be called without valid fd | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  |  * . | 
					
						
							|  |  |  |  * <hr> | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  * 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, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, | 
					
						
							|  |  |  |  * MA 02111-1307, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  |  * <hr> | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  |   | 
					
						
							| 
									
										
										
										
											2002-01-10 19:39:32 +00:00
										 |  |  | #include "sane/sanei_usb.h"
 | 
					
						
							|  |  |  | #include "sane/sanei_lm983x.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | #define _UIO(func)                            \
 | 
					
						
							|  |  |  |     {                                         \ | 
					
						
							|  |  |  |         SANE_Status status;                   \ | 
					
						
							|  |  |  |         status = func;                        \ | 
					
						
							|  |  |  |         if (status != SANE_STATUS_GOOD) {     \ | 
					
						
							|  |  |  |             DBG( _DBG_ERROR, "UIO error\n" ); \ | 
					
						
							|  |  |  |             return SANE_FALSE;                \ | 
					
						
							|  |  |  |         }                                     \ | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define usbio_ReadReg(fd, reg, value) \
 | 
					
						
							| 
									
										
										
										
											2002-01-10 19:39:32 +00:00
										 |  |  |   sanei_lm983x_read (fd, reg, value, 1, 0) | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	u_char depth; | 
					
						
							|  |  |  | 	u_long x; | 
					
						
							|  |  |  | 	u_long y; | 
					
						
							|  |  |  | } PicDef, *pPicDef; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PicDef dPix; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void dumpPic( char* name, SANE_Byte *buffer, u_long len ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	FILE *fp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 	if( DBG_LEVEL < _DBG_DPIC ) | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  | 	if( NULL == buffer ) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 		DBG( _DBG_DPIC, "Creating file '%s'\n", name ); | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		fp = fopen( name, "w+b" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if( NULL != fp ) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if( 0 != dPix.x ) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 				DBG( _DBG_DPIC, "> X=%lu, Y=%lu, depth=%u\n", | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 				                 dPix.x, dPix.y, dPix.depth ); | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  | 				if( dPix.depth > 8 ) | 
					
						
							|  |  |  | 					fprintf( fp, "P6\n%lu %lu\n65535\n", dPix.x, dPix.y ); | 
					
						
							|  |  |  |     			else | 
					
						
							|  |  |  | 					fprintf( fp, "P6\n%lu %lu\n255\n", dPix.x, dPix.y ); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  |     	} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		fp = fopen( name, "a+b" ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if( NULL == fp ) { | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 		DBG( _DBG_DPIC, "Can not open file '%s'\n", name ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fwrite( buffer, 1, len, fp ); | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 	fclose( fp ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | static void dumpPicInit( ScanParam *sd, char* name ) | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	dPix.x = sd->Size.dwPhyBytes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( sd->bDataType == SCANDATATYPE_Color ) | 
					
						
							|  |  |  | 		dPix.x /= 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( sd->bBitDepth > 8 ) | 
					
						
							|  |  |  | 		dPix.x /= 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dPix.y	   = sd->Size.dwLines; | 
					
						
							|  |  |  | 	dPix.depth = sd->bBitDepth; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dumpPic( name, NULL, 0 ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * dump the LM983x registers | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void dumpregs( int fd, SANE_Byte *cmp ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char      buf[256], b2[10]; | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 	SANE_Byte regs[0x80]; | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 	int       i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 	if( DBG_LEVEL < _DBG_DREGS ) | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf[0] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 	if( fd >= 0 ) { | 
					
						
							|  |  |  | 		usbio_ReadReg(fd, 0x01, ®s[0x01]); | 
					
						
							|  |  |  | 		usbio_ReadReg(fd, 0x02, ®s[0x02]); | 
					
						
							|  |  |  | 		usbio_ReadReg(fd, 0x03, ®s[0x03]); | 
					
						
							|  |  |  | 		usbio_ReadReg(fd, 0x04, ®s[0x04]); | 
					
						
							|  |  |  | 		usbio_ReadReg(fd, 0x07, ®s[0x07]); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 		sanei_lm983x_read( fd, 0x08, ®s[0x8], 0x80-0x8, SANE_TRUE ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 		for( i = 0x0; i < 0x80; i++ ) { | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 			if((i%16) ==0 ) { | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 				if( buf[0] ) | 
					
						
							|  |  |  | 					DBG( _DBG_DREGS, "%s\n", buf ); | 
					
						
							|  |  |  | 				sprintf( buf, "0x%02x:", i ); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 			if((i%8)==0) | 
					
						
							|  |  |  | 				strcat( buf, " "); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 			/* the dataport read returns with "0 Bytes read", of course. */ | 
					
						
							|  |  |  | 			if((i == 0) || (i == 5) || (i == 6)) | 
					
						
							|  |  |  | 				strcat( buf, "XX "); | 
					
						
							|  |  |  | 			else { | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 				sprintf( b2, "%02x ", regs[i]); | 
					
						
							|  |  |  | 				strcat( buf, b2 ); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-05-28 11:25:53 +00:00
										 |  |  | 		DBG( _DBG_DREGS, "%s\n", buf ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( cmp ) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-10 13:32:11 +00:00
										 |  |  | 		buf[0] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 		DBG( _DBG_DREGS, "Internal setting:\n" ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 		for( i = 0x0; i < 0x80; i++ ) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 			if((i%16) ==0 ) { | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if( buf[0] ) | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 					DBG( _DBG_DREGS, "%s\n", buf ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 				sprintf( buf, "0x%02x:", i ); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if((i%8)==0) | 
					
						
							|  |  |  | 				strcat( buf, " "); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 			if((i == 0) || (i == 5) || (i == 6)) | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 				strcat( buf, "XX "); | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				sprintf( b2, "%02x ", cmp[i]); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 				strcat( buf, b2 ); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 		DBG( _DBG_DREGS, "%s\n", buf ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * function to read the contents of a LM983x register and regarding some | 
					
						
							|  |  |  |  * extra stuff, like flushing register 2 when writing register 0x58, etc | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  |  * @param handle - | 
					
						
							|  |  |  |  * @param reg    - | 
					
						
							|  |  |  |  * @param value  - | 
					
						
							|  |  |  |  * @return | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2002-04-14 13:38:04 +00:00
										 |  |  | static SANE_Bool usbio_WriteReg( SANE_Int handle, | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  |                                  SANE_Byte reg, SANE_Byte value ) | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | 	int       i; | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 	SANE_Byte data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | 	/* retry loop... */ | 
					
						
							|  |  |  | 	for( i = 0; i < 100; i++ ) { | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-15 17:19:32 +00:00
										 |  |  | 		sanei_lm983x_write_byte( handle, reg, value ); | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 		/* Flush register 0x02 when register 0x58 is written */ | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 		if( 0x58 == reg ) { | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | 			_UIO( usbio_ReadReg( handle, 2, &data )); | 
					
						
							|  |  |  | 			_UIO( usbio_ReadReg( handle, 2, &data )); | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if( reg != 7 ) | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | 			return SANE_TRUE; | 
					
						
							| 
									
										
										
										
											2004-07-02 13:09:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | 		/* verify register 7 */ | 
					
						
							|  |  |  | 		_UIO( usbio_ReadReg( handle, 7, &data )); | 
					
						
							|  |  |  | 		if( data == value ) { | 
					
						
							|  |  |  | 			return SANE_TRUE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-20 16:08:36 +00:00
										 |  |  | 	return SANE_FALSE; | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | /** try and read register 0x69 from a LM983x to find out which version we have.
 | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | static SANE_Status usbio_DetectLM983x( SANE_Int fd, SANE_Byte *version ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 	char        buf[256]; | 
					
						
							|  |  |  | 	SANE_Byte   value; | 
					
						
							|  |  |  | 	SANE_Status res; | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	DBG( _DBG_INFO, "usbio_DetectLM983x\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 	res = usbio_ReadReg(fd, 0x69, &value); | 
					
						
							|  |  |  | 	if( res != SANE_STATUS_GOOD ) { | 
					
						
							|  |  |  | 		DBG( _DBG_ERROR, " * could not read version register!\n"); | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	value &= 7; | 
					
						
							|  |  |  | 	if (version) | 
					
						
							|  |  |  | 		*version = value; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 	res = SANE_STATUS_GOOD; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sprintf( buf, "usbio_DetectLM983x: found " ); | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	switch((SANE_Int)value ) { | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 		case 4:	 strcat( buf, "LM9832/3" ); break; | 
					
						
							|  |  |  | 		case 3:	 strcat( buf, "LM9831" );   break; | 
					
						
							|  |  |  | 		case 2:	 strcat( buf, "LM9830 --> unsupported!!!" ); | 
					
						
							|  |  |  | 				 res =  SANE_STATUS_INVAL; | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 				 break; | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 		default: DBG( _DBG_INFO, "Unknown chip v%d", value ); | 
					
						
							|  |  |  | 				 res = SANE_STATUS_INVAL; | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 				 break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 	DBG( _DBG_INFO, "%s\n", buf ); | 
					
						
							| 
									
										
										
										
											2002-09-28 11:31:53 +00:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | /** well, this is more or less a reset function, for LM9831 based devices
 | 
					
						
							|  |  |  |  * we issue a real reset command, while for LM9832/3 based devices, checking | 
					
						
							|  |  |  |  * and resetting register 7 will be enough... | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | static SANE_Status usbio_ResetLM983x( Plustek_Device *dev ) | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	SANE_Byte value; | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 	HWDef    *hw = &dev->usbDev.HwSetting; | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-10 15:53:13 +00:00
										 |  |  | 	if( _LM9831 == hw->chip ) { | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 		DBG( _DBG_INFO," * resetting LM9831 device!\n"); | 
					
						
							| 
									
										
										
										
											2002-01-10 19:39:32 +00:00
										 |  |  | 		_UIO( sanei_lm983x_write_byte( dev->fd, 0x07, 0)); | 
					
						
							|  |  |  | 		_UIO( sanei_lm983x_write_byte( dev->fd, 0x07,0x20)); | 
					
						
							|  |  |  | 		_UIO( sanei_lm983x_write_byte( dev->fd, 0x07, 0)); | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 		_UIO( usbio_ReadReg( dev->fd, 0x07, &value)); | 
					
						
							|  |  |  | 		if (value != 0) { | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  |  			DBG( _DBG_ERROR, "usbio_ResetLM983x: reset was not " | 
					
						
							|  |  |  | 			                 "successful, status=%d\n", value ); | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 			return SANE_STATUS_INVAL; | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2005-08-15 14:46:16 +00:00
										 |  |  | 		_UIO( usbio_ReadReg( dev->fd, 0x07, &value)); | 
					
						
							|  |  |  | 		if (value != 0 ) { | 
					
						
							|  |  |  | 			DBG( _DBG_INFO," * setting device to idle state!\n"); | 
					
						
							|  |  |  | 			_UIO( sanei_lm983x_write_byte( dev->fd, 0x07, 0)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-12-29 15:34:03 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return SANE_STATUS_GOOD; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* END PLUSTEK-USBIO.C ......................................................*/ |