• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2 
3    based on sources acquired from Plustek Inc.
4    Copyright (C) 2002-2004 Gerhard Jaeger <gerhard@gjaeger.de>
5 
6    This file is part of the SANE package.
7 
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 
21    As a special exception, the authors of SANE give permission for
22    additional uses of the libraries contained in this release of SANE.
23 
24    The exception is that, if you link a SANE library with other files
25    to produce an executable, this does not by itself cause the
26    resulting executable to be covered by the GNU General Public
27    License.  Your use of that executable is in no way restricted on
28    account of linking the SANE library code into it.
29 
30    This exception does not, however, invalidate any other reasons why
31    the executable file might be covered by the GNU General Public
32    License.
33 
34    If you submit changes to SANE to the maintainers to be included in
35    a subsequent release, you agree by submitting the changes that
36    those changes may be distributed with this exception intact.
37 
38    If you write modifications of your own for SANE, it is your choice
39    whether to permit this exception to apply to your modifications.
40    If you do not wish that, delete this exception notice.
41 
42    Interface files for the LM9831/2/3 chip,
43    a chip used in many USB scanners.
44 
45  */
46 
47 #include "../include/sane/config.h"
48 
49 #include <sys/types.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <errno.h>
54 
55 #define BACKEND_NAME sanei_lm983x   /**< the name of this module for dbg  */
56 
57 #include "../include/sane/sane.h"
58 #include "../include/sane/sanei_debug.h"
59 #include "../include/sane/sanei_usb.h"
60 #include "../include/sane/sanei_lm983x.h"
61 
62 /***************************** some definitions ******************************/
63 
64 #define _MIN(a,b) ((a) < (b) ? (a) : (b))
65 #define _MAX(a,b) ((a) > (b) ? (a) : (b))
66 
67 #define _CMD_BYTE_CNT      4           /**< header for LM983x transfers      */
68 #define _MAX_RETRY         20          /**< number of tries for reset        */
69 #define _LM9831_MAX_REG    0x7f        /**< number of LM983x bytes           */
70 #define _MAX_TRANSFER_SIZE 60          /**< max. number of bytes to transfer */
71 
72 /******************************* the functions *******************************/
73 
74 void
sanei_lm983x_init(void)75 sanei_lm983x_init( void )
76 {
77 	DBG_INIT();
78 }
79 
80 SANE_Status
sanei_lm983x_write_byte(SANE_Int fd,SANE_Byte reg,SANE_Byte value)81 sanei_lm983x_write_byte( SANE_Int fd, SANE_Byte reg, SANE_Byte value )
82 {
83 	return sanei_lm983x_write( fd, reg, &value, 1, SANE_FALSE );
84 }
85 
86 SANE_Status
sanei_lm983x_write(SANE_Int fd,SANE_Byte reg,SANE_Byte * buffer,SANE_Word len,SANE_Bool increment)87 sanei_lm983x_write( SANE_Int fd, SANE_Byte reg,
88                     SANE_Byte *buffer, SANE_Word len, SANE_Bool increment )
89 {
90 	size_t      size;
91 	SANE_Byte   command_buffer[_MAX_TRANSFER_SIZE + _CMD_BYTE_CNT];
92 	SANE_Status result;
93 	SANE_Word   bytes, max_len;
94 
95 	DBG( 15, "sanei_lm983x_write: fd=%d, reg=%d, len=%d, increment=%d\n", fd,
96 	         reg, len, increment);
97 
98 	if( reg > _LM9831_MAX_REG ) {
99 		DBG( 1, "sanei_lm983x_write: register out of range (%u>%u)\n",
100 				 reg, _LM9831_MAX_REG );
101 		return SANE_STATUS_INVAL;
102 	}
103 
104 	for( bytes = 0; len > 0; ) {
105 
106 		max_len = _MIN( len, _MAX_TRANSFER_SIZE );
107 
108 		command_buffer[0] = 0;                      /* write              */
109 		command_buffer[1] = reg;                    /* LM983x register    */
110 
111 		if( increment == SANE_TRUE ) {
112 			command_buffer[0] += 0x02;              /* increase reg?      */
113 			command_buffer[1] += bytes;
114 		}
115 
116 		command_buffer[2] = (max_len >> 8) & 0xff;  /* bytes to write MSB */
117 		command_buffer[3] = max_len & 0xff;         /* bytes to write LSB */
118 
119 		memcpy( command_buffer + _CMD_BYTE_CNT, buffer + bytes, max_len );
120 
121 		size   = (max_len + _CMD_BYTE_CNT);
122 		result = sanei_usb_write_bulk( fd, command_buffer, &size );
123 
124 		if( SANE_STATUS_GOOD != result )
125 			return result;
126 
127 		if( size != (size_t)(max_len + _CMD_BYTE_CNT)) {
128 			DBG( 2, "sanei_lm983x_write: short write (%d/%d)\n",
129 			     result, max_len + _CMD_BYTE_CNT);
130 
131 			if( size < _CMD_BYTE_CNT ) {
132 				DBG( 1, "sanei_lm983x_write: couldn't even send command\n" );
133 				return SANE_STATUS_IO_ERROR;
134 			}
135 			DBG( 1, "sanei_lm983x_write: trying again\n" );
136 		}
137 		len   -= (size - _CMD_BYTE_CNT);
138 		bytes += (size - _CMD_BYTE_CNT);
139 	}
140 	DBG( 15, "sanei_lm983x_write: succeeded\n" );
141 	return SANE_STATUS_GOOD;
142 }
143 
144 SANE_Status
sanei_lm983x_read(SANE_Int fd,SANE_Byte reg,SANE_Byte * buffer,SANE_Word len,SANE_Bool increment)145 sanei_lm983x_read( SANE_Int fd, SANE_Byte reg,
146                    SANE_Byte *buffer, SANE_Word len, SANE_Bool increment )
147 {
148 	size_t      size;
149 	SANE_Byte   command_buffer[_CMD_BYTE_CNT];
150 	SANE_Status result;
151 	SANE_Word   bytes, max_len, read_bytes;
152 
153 	DBG( 15, "sanei_lm983x_read: fd=%d, reg=%d, len=%d, increment=%d\n", fd,
154 	         reg, len, increment );
155 	if( reg > _LM9831_MAX_REG ) {
156 		DBG( 1, "sanei_lm983x_read: register out of range (%u>%u)\n",
157 		        reg, _LM9831_MAX_REG );
158 		return SANE_STATUS_INVAL;
159 	}
160 
161 	for( bytes = 0; len > 0; ) {
162 
163 		max_len = _MIN(len, 0xFFFF );
164 		command_buffer[0] = 1;                 /* read */
165 		command_buffer[1] = reg;               /* LM9831 register */
166 
167 		if( increment ) {
168 			command_buffer[0] += 0x02;
169 			command_buffer[1] += bytes;
170 		}
171 
172 		command_buffer[2] = (max_len >> 8) & 0xff; /* bytes to read MSB */
173 		command_buffer[3] = max_len & 0xff;        /* bytes to read LSB */
174 
175 		DBG( 15, "sanei_lm983x_read: writing command: "
176 			"%02x %02x %02x %02x\n", command_buffer[0], command_buffer[1],
177 			                         command_buffer[2], command_buffer[3]);
178 
179 		size   = _CMD_BYTE_CNT;
180 		result = sanei_usb_write_bulk( fd, command_buffer, &size );
181 
182 		if( SANE_STATUS_GOOD != result )
183 			return result;
184 
185 		if( size != _CMD_BYTE_CNT) {
186 			DBG( 1, "sanei_lm983x_read: short write while writing command "
187 			        "(%d/_CMD_BYTE_CNT)\n", result);
188 			return SANE_STATUS_IO_ERROR;
189 		}
190 
191 		read_bytes = 0;
192 		do {
193 
194 			size = (max_len - read_bytes);
195 
196 			result = sanei_usb_read_bulk( fd, (buffer + bytes + read_bytes), &size );
197 
198 			if( SANE_STATUS_GOOD != result )
199 				return result;
200 
201 			read_bytes += size;
202 			DBG( 15, "sanei_lm983x_read: read %lu bytes\n", (u_long) size );
203 
204 			if( read_bytes != max_len ) {
205 				DBG( 2, "sanei_lm983x_read: short read (%d/%d)\n",
206 				                                 result, max_len );
207 				/* wait a little bit before retrying */
208 				usleep( 10000 );
209 				DBG( 2, "sanei_lm983x_read: trying again\n" );
210 			}
211 		} while( read_bytes < max_len );
212 
213 		bytes += (max_len);
214 		len   -= (max_len);
215 	}
216 	DBG( 15, "sanei_lm983x_read: succeeded\n" );
217 	return SANE_STATUS_GOOD;
218 }
219 
sanei_lm983x_reset(SANE_Int fd)220 SANE_Bool sanei_lm983x_reset( SANE_Int fd )
221 {
222 	SANE_Status res;
223 	SANE_Byte   tmp;
224 	SANE_Int    i;
225 
226 	DBG( 15, "sanei_lm983x_reset()\n" );
227 
228 	for( i = 0; i < _MAX_RETRY; i++ ) {
229 
230 		/* Read the command register and check that the reset bit is not set
231 		 * If it is set, clear it and return false to indicate that
232 		 * the bit has only now been cleared
233 		 *
234 		 * Write the command bytes for a register read
235 		 * without increment
236 		 */
237 		if( SANE_STATUS_GOOD != sanei_lm983x_read_byte( fd, 0x07, &tmp ))
238 			continue;
239 
240 		if( tmp & 0x20 ) {
241 
242 			res = sanei_lm983x_write_byte( fd, 0x07, 0 );
243 
244 			/* We will attempt to reset it but we really don't do
245 			 * anything if this fails
246 			 */
247 			if( res == SANE_STATUS_GOOD ) {
248 				DBG( 15, "Resetting the LM983x already done\n" );
249 				return SANE_TRUE;
250 			}
251 		} else {
252 
253 			res = sanei_lm983x_write_byte( fd, 0x07, 0x20 );
254 			if( res == SANE_STATUS_GOOD ) {
255 				DBG( 15, "Resetting the LM983x done\n" );
256 				return SANE_TRUE;
257 			}
258 		}
259 	}
260 	return SANE_FALSE;
261 }
262 
263 /* END sanei_lm983x.c .......................................................*/
264