summaryrefslogtreecommitdiff
path: root/backend/plustek-usbio.c
blob: cae0e0dea7945d70c13ad700865de639d648fc76 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*.............................................................................
 * Project : SANE library for Plustek flatbed scanners.
 *.............................................................................
 */

/** @file plustek-usbio.c
 *  @brief Some I/O stuff.
 *
 * Based on sources acquired from Plustek Inc.<br>
 * Copyright (C) 2001-2007 Gerhard Jaeger <gerhard@gjaeger.de>
 *
 * 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
 *        - added dimension stuff to dumpPic
 * - 0.46 - disabled reset prior to the detection of Merlin
 * - 0.47 - no changes
 * - 0.48 - cleanup
 * - 0.49 - no changes
 * - 0.50 - usbio_DetectLM983x() now returns error if register 
 *          could not be red
 *        - usbio_ResetLM983x() checks for reg7 value before writing
 * - 0.51 - allow dumpRegs to be called without valid fd
 * - 0.52 - no changes
 * .
 * <hr>
 * 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.
 * <hr>
 */
 
#include "../include/sane/sanei_usb.h"
#include "../include/sane/sanei_lm983x.h"

#define _UIO(func)                            \
    {                                         \
        SANE_Status status;                   \
        status = func;                        \
        if (status != SANE_STATUS_GOOD) {     \
            DBG( _DBG_ERROR, "UIO error\n" ); \
            return SANE_FALSE;                \
        }                                     \
    }

#define usbio_ReadReg(fd, reg, value) \
  sanei_lm983x_read (fd, reg, value, 1, 0)

typedef struct {

	u_char depth;
	u_long x;
	u_long y;
} PicDef, *pPicDef;

static PicDef dPix;

/**
 */
static void dumpPic( char* name, SANE_Byte *buffer, u_long len, int is_gray )
{
	u_short type;
	FILE   *fp;

	if( DBG_LEVEL < _DBG_DPIC )
		return;

	if( NULL == buffer ) {

		DBG( _DBG_DPIC, "Creating file '%s'\n", name );

		fp = fopen( name, "w+b" );

		if( NULL != fp ) {

			if( 0 != dPix.x ) {

				if (is_gray)
					type = 5;
				else
					type = 6;

				DBG( _DBG_DPIC, "> X=%lu, Y=%lu, depth=%u\n",
				                 dPix.x, dPix.y, dPix.depth );
				if( dPix.depth > 8 )
					fprintf( fp, "P%u\n%lu %lu\n65535\n", type, dPix.x, dPix.y);
    			else
					fprintf( fp, "P%u\n%lu %lu\n255\n", type, dPix.x, dPix.y);
			}
    	}
	} else {
		fp = fopen( name, "a+b" );
	}

	if( NULL == fp ) {
		DBG( _DBG_DPIC, "Can not open file '%s'\n", name );
		return;
	}

	fwrite( buffer, 1, len, fp );
	fclose( fp );
}

/**
 */
static void dumpPicInit( ScanParam *sd, char* name )
{
	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;

	if( sd->bDataType == SCANDATATYPE_Color )
		dumpPic(name, NULL, 0, 0);
	else
		dumpPic(name, NULL, 0, 1);
}

/**
 * dump the LM983x registers
 */
static void dumpregs( int fd, SANE_Byte *cmp )
{
	char      buf[256], b2[10];
	SANE_Byte regs[0x80];
	int       i;

	if( DBG_LEVEL < _DBG_DREGS )
		return;

	buf[0] = '\0';

	if( fd >= 0 ) {
		usbio_ReadReg(fd, 0x01, &regs[0x01]);
		usbio_ReadReg(fd, 0x02, &regs[0x02]);
		usbio_ReadReg(fd, 0x03, &regs[0x03]);
		usbio_ReadReg(fd, 0x04, &regs[0x04]);
		usbio_ReadReg(fd, 0x07, &regs[0x07]);

		sanei_lm983x_read( fd, 0x08, &regs[0x8], 0x80-0x8, SANE_TRUE );

		for( i = 0x0; i < 0x80; i++ ) {

			if((i%16) ==0 ) {

				if( buf[0] )
					DBG( _DBG_DREGS, "%s\n", buf );
				sprintf( buf, "0x%02x:", i );
			}

			if((i%8)==0)
				strcat( buf, " ");

			/* the dataport read returns with "0 Bytes read", of course. */
			if((i == 0) || (i == 5) || (i == 6))
				strcat( buf, "XX ");
			else {

				sprintf( b2, "%02x ", regs[i]);
				strcat( buf, b2 );
			}
		}
		DBG( _DBG_DREGS, "%s\n", buf );
	}

	if( cmp ) {

		buf[0] = '\0';

		DBG( _DBG_DREGS, "Internal setting:\n" );
		for( i = 0x0; i < 0x80; i++ ) {

			if((i%16) ==0 ) {

				if( buf[0] )
					DBG( _DBG_DREGS, "%s\n", buf );
				sprintf( buf, "0x%02x:", i );
			}

			if((i%8)==0)
				strcat( buf, " ");

			if((i == 0) || (i == 5) || (i == 6))
				strcat( buf, "XX ");
			else {
				sprintf( b2, "%02x ", cmp[i]);
				strcat( buf, b2 );
			}
		}
		DBG( _DBG_DREGS, "%s\n", buf );
	}
}

/**
 * function to read the contents of a LM983x register and regarding some
 * extra stuff, like flushing register 2 when writing register 0x58, etc
 *
 * @param handle -
 * @param reg    -
 * @param value  -
 * @return
 */
static SANE_Bool usbio_WriteReg( SANE_Int handle,
                                 SANE_Byte reg, SANE_Byte value )
{
	int       i;
	SANE_Byte data;

	/* retry loop... */
	for( i = 0; i < 100; i++ ) {

		sanei_lm983x_write_byte( handle, reg, value );
	
		/* Flush register 0x02 when register 0x58 is written */
		if( 0x58 == reg ) {
			_UIO( usbio_ReadReg( handle, 2, &data ));
			_UIO( usbio_ReadReg( handle, 2, &data ));
		}

		if( reg != 7 )
			return SANE_TRUE;

		/* verify register 7 */
		_UIO( usbio_ReadReg( handle, 7, &data ));
		if( data == value ) {
			return SANE_TRUE;
		}
	}

	return SANE_FALSE;
}

/** try and read register 0x69 from a LM983x to find out which version we have.
 */
static SANE_Status usbio_DetectLM983x( SANE_Int fd, SANE_Byte *version )
{
	char        buf[256];
	SANE_Byte   value;
	SANE_Status res;

	DBG( _DBG_INFO, "usbio_DetectLM983x\n");

	res = usbio_ReadReg(fd, 0x69, &value);
	if( res != SANE_STATUS_GOOD ) {
		DBG( _DBG_ERROR, " * could not read version register!\n");
		return res;
	}

	value &= 7;
	if (version)
		*version = value;

	res = SANE_STATUS_GOOD;

	sprintf( buf, "usbio_DetectLM983x: found " );

	switch((SANE_Int)value ) {
		
		case 4:	 strcat( buf, "LM9832/3" ); break;
		case 3:	 strcat( buf, "LM9831" );   break;
		case 2:	 strcat( buf, "LM9830 --> unsupported!!!" );
				 res =  SANE_STATUS_INVAL;
				 break;
		default: DBG( _DBG_INFO, "Unknown chip v%d", value );
				 res = SANE_STATUS_INVAL;
				 break;
	}

	DBG( _DBG_INFO, "%s\n", buf );
	return res;
}

/** 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...
 */
static SANE_Status usbio_ResetLM983x( Plustek_Device *dev )
{
	SANE_Byte value;
	HWDef    *hw = &dev->usbDev.HwSetting;

	if( _LM9831 == hw->chip ) {

		DBG( _DBG_INFO," * resetting LM9831 device!\n");
		_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));
		_UIO( usbio_ReadReg( dev->fd, 0x07, &value));
		if (value != 0) {
 			DBG( _DBG_ERROR, "usbio_ResetLM983x: reset was not "
			                 "successful, status=%d\n", value );
			return SANE_STATUS_INVAL;
		}

	} else {
		_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));
		}
	}
	return SANE_STATUS_GOOD;
}

/* END PLUSTEK-USBIO.C ......................................................*/