sane-project-backends/sanei/sanei_lm983x.c

266 wiersze
7.9 KiB
C
Czysty Zwykły widok Historia

2002-01-10 19:36:06 +00:00
/* sane - Scanner Access Now Easy.
based on sources acquired from Plustek Inc.
2004-07-16 11:49:16 +00:00
Copyright (C) 2002-2004 Gerhard Jaeger <gerhard@gjaeger.de>
2002-01-10 19:36:06 +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.
Interface files for the LM9831/2/3 chip,
a chip used in many USB scanners.
*/
#include "../include/sane/config.h"
#include <sys/types.h>
2002-01-10 19:36:06 +00:00
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
2004-07-16 11:49:16 +00:00
#define BACKEND_NAME sanei_lm983x /**< the name of this module for dbg */
2002-01-10 19:36:06 +00:00
#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 ******************************/
2004-07-16 11:49:16 +00:00
#define _MIN(a,b) ((a) < (b) ? (a) : (b))
#define _MAX(a,b) ((a) > (b) ? (a) : (b))
2002-01-10 19:36:06 +00:00
2004-07-16 11:49:16 +00:00
#define _CMD_BYTE_CNT 4 /**< header for LM983x transfers */
2002-01-12 14:00:58 +00:00
#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 */
2002-01-10 19:36:06 +00:00
/******************************* the functions *******************************/
2002-01-12 14:00:58 +00:00
void
sanei_lm983x_init( void )
{
DBG_INIT();
}
2002-01-10 19:36:06 +00:00
SANE_Status
sanei_lm983x_write_byte( SANE_Int fd, SANE_Byte reg, SANE_Byte value )
{
2004-07-16 11:49:16 +00:00
return sanei_lm983x_write( fd, reg, &value, 1, SANE_FALSE );
2002-01-10 19:36:06 +00:00
}
SANE_Status
sanei_lm983x_write( SANE_Int fd, SANE_Byte reg,
2004-07-16 11:49:16 +00:00
SANE_Byte *buffer, SANE_Word len, SANE_Bool increment )
2002-01-10 19:36:06 +00:00
{
2004-07-16 11:49:16 +00:00
size_t size;
SANE_Byte command_buffer[_MAX_TRANSFER_SIZE + _CMD_BYTE_CNT];
SANE_Status result;
SANE_Word bytes, max_len;
2002-01-10 19:36:06 +00:00
2004-07-16 11:49:16 +00:00
DBG( 15, "sanei_lm983x_write: fd=%d, reg=%d, len=%d, increment=%d\n", fd,
2002-01-10 19:36:06 +00:00
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;
2004-07-16 11:49:16 +00:00
}
2002-01-10 19:36:06 +00:00
for( bytes = 0; len > 0; ) {
max_len = _MIN( len, _MAX_TRANSFER_SIZE );
2004-07-16 11:49:16 +00:00
command_buffer[0] = 0; /* write */
command_buffer[1] = reg; /* LM983x register */
2002-01-10 19:36:06 +00:00
if( increment == SANE_TRUE ) {
2004-07-16 11:49:16 +00:00
command_buffer[0] += 0x02; /* increase reg? */
2002-01-10 19:36:06 +00:00
command_buffer[1] += bytes;
}
2004-07-16 11:49:16 +00:00
command_buffer[2] = (max_len >> 8) & 0xff; /* bytes to write MSB */
command_buffer[3] = max_len & 0xff; /* bytes to write LSB */
2002-01-10 19:36:06 +00:00
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;
2004-07-16 11:49:16 +00:00
if( size != (size_t)(max_len + _CMD_BYTE_CNT)) {
2002-01-10 19:36:06 +00:00
DBG( 2, "sanei_lm983x_write: short write (%d/%d)\n",
2004-07-16 11:49:16 +00:00
result, max_len + _CMD_BYTE_CNT);
2002-01-10 19:36:06 +00:00
if( size < _CMD_BYTE_CNT ) {
2004-07-16 11:49:16 +00:00
DBG( 1, "sanei_lm983x_write: couldn't even send command\n" );
return SANE_STATUS_IO_ERROR;
}
2002-01-10 19:36:06 +00:00
DBG( 1, "sanei_lm983x_write: trying again\n" );
}
len -= (size - _CMD_BYTE_CNT);
bytes += (size - _CMD_BYTE_CNT);
2004-07-16 11:49:16 +00:00
}
2002-01-10 19:36:06 +00:00
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 )
{
2004-07-16 11:49:16 +00:00
size_t size;
SANE_Byte command_buffer[_CMD_BYTE_CNT];
SANE_Status result;
SANE_Word bytes, max_len, read_bytes;
2002-01-10 19:36:06 +00:00
2004-07-16 11:49:16 +00:00
DBG( 15, "sanei_lm983x_read: fd=%d, reg=%d, len=%d, increment=%d\n", fd,
reg, len, increment );
2002-01-10 19:36:06 +00:00
if( reg > _LM9831_MAX_REG ) {
DBG( 1, "sanei_lm983x_read: register out of range (%u>%u)\n",
2004-07-16 11:49:16 +00:00
reg, _LM9831_MAX_REG );
2002-01-10 19:36:06 +00:00
return SANE_STATUS_INVAL;
2004-07-16 11:49:16 +00:00
}
2002-01-10 19:36:06 +00:00
for( bytes = 0; len > 0; ) {
max_len = _MIN(len, 0xFFFF );
command_buffer[0] = 1; /* read */
command_buffer[1] = reg; /* LM9831 register */
2004-07-16 11:49:16 +00:00
2002-01-10 19:36:06 +00:00
if( increment ) {
command_buffer[0] += 0x02;
command_buffer[1] += bytes;
}
2004-07-16 11:49:16 +00:00
2002-01-10 19:36:06 +00:00
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: "
2004-07-16 11:49:16 +00:00
"%02x %02x %02x %02x\n", command_buffer[0], command_buffer[1],
command_buffer[2], command_buffer[3]);
2002-01-10 19:36:06 +00:00
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 "
2004-07-16 11:49:16 +00:00
"(%d/_CMD_BYTE_CNT)\n", result);
2002-01-10 19:36:06 +00:00
return SANE_STATUS_IO_ERROR;
}
read_bytes = 0;
do {
2004-07-16 11:49:16 +00:00
2002-01-10 19:36:06 +00:00
size = (max_len - read_bytes);
2004-07-16 11:49:16 +00:00
2002-01-10 19:36:06 +00:00
result = sanei_usb_read_bulk( fd, (buffer + bytes + read_bytes), &size );
if( SANE_STATUS_GOOD != result )
return result;
2004-07-16 11:49:16 +00:00
read_bytes += size;
2004-10-03 17:34:36 +00:00
DBG( 15, "sanei_lm983x_read: read %lu bytes\n", (u_long) size );
2004-07-16 11:49:16 +00:00
if( read_bytes != max_len ) {
2002-01-10 19:36:06 +00:00
DBG( 2, "sanei_lm983x_read: short read (%d/%d)\n",
result, max_len );
/* wait a little bit before retrying */
2004-07-16 11:49:16 +00:00
usleep( 10000 );
DBG( 2, "sanei_lm983x_read: trying again\n" );
}
2002-01-10 19:36:06 +00:00
} while( read_bytes < max_len );
2004-07-16 11:49:16 +00:00
bytes += (max_len);
len -= (max_len);
2002-01-10 19:36:06 +00:00
}
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;
2002-01-10 19:36:06 +00:00
2004-07-16 11:49:16 +00:00
DBG( 15, "sanei_lm983x_reset()\n" );
2002-01-10 19:36:06 +00:00
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;
2002-01-10 19:36:06 +00:00
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;
2004-07-16 11:49:16 +00:00
}
} else {
res = sanei_lm983x_write_byte( fd, 0x07, 0x20 );
if( res == SANE_STATUS_GOOD ) {
DBG( 15, "Resetting the LM983x done\n" );
return SANE_TRUE;
2004-07-16 11:49:16 +00:00
}
}
2002-01-10 19:36:06 +00:00
}
return SANE_FALSE;
}
2003-10-13 07:16:54 +00:00
/* END sanei_lm983x.c .......................................................*/