summaryrefslogtreecommitdiff
path: root/sanei/sanei_lm983x.c
blob: 5b41985c2b9586fc5d1da209465e28efabbfa80d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* sane - Scanner Access Now Easy.

   based on sources acquired from Plustek Inc.
   Copyright (C) 2002-2004 Gerhard Jaeger <gerhard@gjaeger.de>

   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.

   Interface files for the LM9831/2/3 chip,
   a chip used in many USB scanners.

 */

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

#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define BACKEND_NAME sanei_lm983x   /**< the name of this module for dbg  */

#include "../include/sane/sane.h"
#include "../include/sane/sanei_debug.h"
#include "../include/sane/sanei_usb.h"
#include "../include/sane/sanei_lm983x.h"

/***************************** some definitions ******************************/

#define _MIN(a,b) ((a) < (b) ? (a) : (b))
#define _MAX(a,b) ((a) > (b) ? (a) : (b))

#define _CMD_BYTE_CNT      4           /**< header for LM983x transfers      */
#define _MAX_RETRY         20          /**< number of tries for reset        */
#define _LM9831_MAX_REG    0x7f        /**< number of LM983x bytes           */
#define _MAX_TRANSFER_SIZE 60          /**< max. number of bytes to transfer */

/******************************* the functions *******************************/

void
sanei_lm983x_init( void )
{
	DBG_INIT();
}

SANE_Status
sanei_lm983x_write_byte( SANE_Int fd, SANE_Byte reg, SANE_Byte value )
{
	return sanei_lm983x_write( fd, reg, &value, 1, SANE_FALSE );
}

SANE_Status
sanei_lm983x_write( SANE_Int fd, SANE_Byte reg,
                    SANE_Byte *buffer, SANE_Word len, SANE_Bool increment )
{
	size_t      size;
	SANE_Byte   command_buffer[_MAX_TRANSFER_SIZE + _CMD_BYTE_CNT];
	SANE_Status result;
	SANE_Word   bytes, max_len;

	DBG( 15, "sanei_lm983x_write: fd=%d, reg=%d, len=%d, increment=%d\n", fd,
	         reg, len, increment);
	
	if( reg > _LM9831_MAX_REG ) {
		DBG( 1, "sanei_lm983x_write: register out of range (%u>%u)\n",
				 reg, _LM9831_MAX_REG );
		return SANE_STATUS_INVAL;
	}
	
	for( bytes = 0; len > 0; ) {

		max_len = _MIN( len, _MAX_TRANSFER_SIZE );

		command_buffer[0] = 0;                      /* write              */
		command_buffer[1] = reg;                    /* LM983x register    */
		
		if( increment == SANE_TRUE ) {
			command_buffer[0] += 0x02;              /* increase reg?      */
			command_buffer[1] += bytes;
		}

		command_buffer[2] = (max_len >> 8) & 0xff;  /* bytes to write MSB */
		command_buffer[3] = max_len & 0xff;         /* bytes to write LSB */

		memcpy( command_buffer + _CMD_BYTE_CNT, buffer + bytes, max_len );

		size   = (max_len + _CMD_BYTE_CNT);
		result = sanei_usb_write_bulk( fd, command_buffer, &size );
		
		if( SANE_STATUS_GOOD != result )
			return result;

		if( size != (size_t)(max_len + _CMD_BYTE_CNT)) {
			DBG( 2, "sanei_lm983x_write: short write (%d/%d)\n",
			     result, max_len + _CMD_BYTE_CNT);

			if( size < _CMD_BYTE_CNT ) {
				DBG( 1, "sanei_lm983x_write: couldn't even send command\n" );
				return SANE_STATUS_IO_ERROR;
			}
			DBG( 1, "sanei_lm983x_write: trying again\n" );
		}
		len   -= (size - _CMD_BYTE_CNT);
		bytes += (size - _CMD_BYTE_CNT);
	}
	DBG( 15, "sanei_lm983x_write: succeeded\n" );
	return SANE_STATUS_GOOD;
}

SANE_Status
sanei_lm983x_read( SANE_Int fd, SANE_Byte reg,
                   SANE_Byte *buffer, SANE_Word len, SANE_Bool increment )
{
	size_t      size;
	SANE_Byte   command_buffer[_CMD_BYTE_CNT];
	SANE_Status result;
	SANE_Word   bytes, max_len, read_bytes;

	DBG( 15, "sanei_lm983x_read: fd=%d, reg=%d, len=%d, increment=%d\n", fd,
	         reg, len, increment );
	if( reg > _LM9831_MAX_REG ) {
		DBG( 1, "sanei_lm983x_read: register out of range (%u>%u)\n",
		        reg, _LM9831_MAX_REG );
		return SANE_STATUS_INVAL;
	}

	for( bytes = 0; len > 0; ) {

		max_len = _MIN(len, 0xFFFF );
		command_buffer[0] = 1;                 /* read */
		command_buffer[1] = reg;               /* LM9831 register */

		if( increment ) {
			command_buffer[0] += 0x02;
			command_buffer[1] += bytes;
		}

		command_buffer[2] = (max_len >> 8) & 0xff; /* bytes to read MSB */
		command_buffer[3] = max_len & 0xff;        /* bytes to read LSB */

		DBG( 15, "sanei_lm983x_read: writing command: "
			"%02x %02x %02x %02x\n", command_buffer[0], command_buffer[1],
			                         command_buffer[2], command_buffer[3]);

		size   = _CMD_BYTE_CNT;
		result = sanei_usb_write_bulk( fd, command_buffer, &size );

		if( SANE_STATUS_GOOD != result )
			return result;
		
		if( size != _CMD_BYTE_CNT) {
			DBG( 1, "sanei_lm983x_read: short write while writing command "
			        "(%d/_CMD_BYTE_CNT)\n", result);
			return SANE_STATUS_IO_ERROR;
		}

		read_bytes = 0;
		do {

			size = (max_len - read_bytes);

			result = sanei_usb_read_bulk( fd, (buffer + bytes + read_bytes), &size );

			if( SANE_STATUS_GOOD != result )
				return result;

			read_bytes += size;
			DBG( 15, "sanei_lm983x_read: read %lu bytes\n", (u_long) size );

			if( read_bytes != max_len ) {
				DBG( 2, "sanei_lm983x_read: short read (%d/%d)\n",
				                                 result, max_len );
				/* wait a little bit before retrying */
				usleep( 10000 );
				DBG( 2, "sanei_lm983x_read: trying again\n" );
			}
		} while( read_bytes < max_len );

		bytes += (max_len);
		len   -= (max_len);
	}
	DBG( 15, "sanei_lm983x_read: succeeded\n" );
	return SANE_STATUS_GOOD;
}

SANE_Bool sanei_lm983x_reset( SANE_Int fd )
{
	SANE_Status res;
	SANE_Byte   tmp;
	SANE_Int    i;

	DBG( 15, "sanei_lm983x_reset()\n" );

	for( i = 0; i < _MAX_RETRY; i++ ) {
		
		/* Read the command register and check that the reset bit is not set
		 * If it is set, clear it and return false to indicate that
		 * the bit has only now been cleared
		 *
		 * Write the command bytes for a register read
		 * without increment
		 */
		if( SANE_STATUS_GOOD != sanei_lm983x_read_byte( fd, 0x07, &tmp ))
			continue;

		if( tmp & 0x20 ) {

			res = sanei_lm983x_write_byte( fd, 0x07, 0 );

			/* We will attempt to reset it but we really don't do
			 * anything if this fails
			 */
			if( res == SANE_STATUS_GOOD ) {
				DBG( 15, "Resetting the LM983x already done\n" );
				return SANE_TRUE;
			}
		} else {

			res = sanei_lm983x_write_byte( fd, 0x07, 0x20 );
			if( res == SANE_STATUS_GOOD ) {
				DBG( 15, "Resetting the LM983x done\n" );
				return SANE_TRUE;
			}
		}
	}
	return SANE_FALSE;
}

/* END sanei_lm983x.c .......................................................*/