Niash backend files, initial checkin.

merge-requests/1/head
Gerhard Jaeger 2004-08-03 07:42:08 +00:00
rodzic 75858b7e9b
commit 727dd4ab9a
6 zmienionych plików z 3121 dodań i 0 usunięć

1113
backend/niash.c 100644

Plik diff jest za duży Load Diff

1421
backend/niash_core.c 100644

Plik diff jest za duży Load Diff

Wyświetl plik

@ -0,0 +1,134 @@
/*
Copyright (C) 2001 Bertrik Sikken (bertrik@zonnet.nl)
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.
$Id$
*/
/*
Core NIASH chip functions.
*/
#ifndef _NIASH_CORE_H_
#define _NIASH_CORE_H_
#include <unistd.h>
#include "niash_types.h"
#include "niash_xfer.h" /* for EScannerModel */
#define HP3300C_RIGHT 330
#define HP3300C_TOP 452
#define HP3300C_BOTTOM (HP3300C_TOP + 14200UL)
#define HW_PIXELS 5300 /* number of pixels supported by hardware */
#define HW_DPI 600 /* horizontal resolution of hardware */
#define HW_LPI 1200 /* vertical resolution of hardware */
#define BYTES_PER_PIXEL 3
typedef struct
{
int iXferHandle; /* handle used for data transfer to HW */
int iTopLeftX; /* in mm */
int iTopLeftY; /* in mm */
int iSensorSkew; /* in units of 1/1200 inch */
int iSkipLines; /* lines of garbage to skip */
bool fReg07; /* NIASH00019 */
bool fGamma16; /* if TRUE, gamma entries are 16 bit */
int iExpTime;
bool iReversedHead; /* Head is reversed */
int iBufferSize; /* Size of internal scan buffer */
EScannerModel eModel;
} THWParams;
typedef struct
{
int iDpi; /* horizontal resolution */
int iLpi; /* vertical resolution */
int iTop; /* in HW coordinates */
int iLeft; /* in HW coordinates */
int iWidth; /* pixels */
int iHeight; /* lines */
int iBottom;
int fCalib; /* if TRUE, disable backtracking? */
} TScanParams;
typedef struct
{
unsigned char *pabXferBuf; /* transfer buffer */
int iCurLine; /* current line in the transfer buffer */
int iBytesPerLine; /* unsigned chars in one scan line */
int iLinesPerXferBuf; /* number of lines held in the transfer buffer */
int iLinesLeft; /* transfer (down) counter for pabXFerBuf */
int iSaneBytesPerLine; /* how many unsigned chars to be read by SANE per line */
int iScaleDownDpi; /* factors used to emulate lower resolutions */
int iScaleDownLpi; /* than those offered by hardware */
int iSkipLines; /* line to skip at the start of scan */
int iWidth; /* number of pixels expected by SANE */
unsigned char *pabCircBuf; /* circular buffer */
int iLinesPerCircBuf; /* lines held in the circular buffer */
int iRedLine, iGrnLine, /* start indices for the color information */
iBluLine; /* in the circular buffer */
unsigned char *pabLineBuf; /* buffer used to pass data to SANE */
int iBytesLeft; /* number of buffer line transfers */
} TDataPipe;
STATIC int NiashOpen (THWParams * pHWParams, const char *pszName);
STATIC void NiashClose (THWParams * pHWParams);
/* more sof. method that also returns the values of the white (RGB) value */
STATIC bool SimpleCalibExt (THWParams * pHWPar, unsigned char *pabCalibTable,
unsigned char *pabCalWhite);
STATIC bool GetLamp (THWParams * pHWParams, bool * pfLampIsOn);
STATIC bool SetLamp (THWParams * pHWParams, bool fLampOn);
STATIC bool InitScan (TScanParams * pParams, THWParams * pHWParams);
STATIC void FinishScan (THWParams * pHWParams);
STATIC void CalcGamma (unsigned char *pabTable, double Gamma);
STATIC void WriteGammaCalibTable (unsigned char *pabGammaR,
unsigned char *pabGammaG,
unsigned char *pabGammaB,
unsigned char *pabCalibTable, int iGain,
int iOffset, THWParams * pHWPar);
/* set -1 for iHeight to disable all checks on buffer transfers */
/* iWidth is in pixels of SANE */
/* iHeight is lines in scanner resolution */
STATIC void CircBufferInit (int iHandle, TDataPipe * p,
int iWidth, int iHeight,
int iMisAlignment, bool iReversedHead,
int iScaleDownDpi, int iScaleDownLpi);
/* returns false, when trying to read after end of buffer */
STATIC bool CircBufferGetLine (int iHandle, TDataPipe * p,
unsigned char *pabLine, bool iReversedHead);
STATIC void CircBufferExit (TDataPipe * p);
#ifndef WITH_NIASH
void DumpHex (unsigned char *pabData, int iLen, int iWidth);
void ScanLines (FILE * pFile, TScanParams * pParams, THWParams * pHWParams);
bool SimpleCalib (THWParams * pHWPar, unsigned char *pabCalibTable);
#endif /* NO WITH_NIASH */
#endif /* _NIASH_CORE_H_ */

Wyświetl plik

@ -0,0 +1,31 @@
/*
Defines my types
$Id$
*/
#ifndef _NIASH_TYPES_H_
#define _NIASH_TYPES_H_
#ifndef byte
typedef unsigned char byte;
#endif
#ifndef word
typedef int word;
#endif
#ifndef bool
typedef int bool;
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#endif /* _NIASH_TYPES_H_ */

Wyświetl plik

@ -0,0 +1,326 @@
/*
Copyright (C) 2001 Bertrik Sikken (bertrik@zonnet.nl)
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.
$Id$
*/
/*
Provides a simple interface to read and write data from the scanner,
without any knowledge whether it's a parallel or USB scanner
*/
#include <stdio.h> /* printf */
#include <errno.h> /* better error reports */
#include <string.h> /* better error reports */
#include "niash_xfer.h"
#include "../include/sane/sanei_usb.h"
/* list of supported models */
STATIC TScannerModel ScannerModels[] = {
{"Hewlett-Packard", "ScanJet 3300C", 0x3F0, 0x205, eHp3300c}
,
{"Hewlett-Packard", "ScanJet 3400C", 0x3F0, 0x405, eHp3400c}
,
{"Hewlett-Packard", "ScanJet 4300C", 0x3F0, 0x305, eHp4300c}
,
{"Agfa", "Snapscan Touch", 0x6BD, 0x100, eAgfaTouch}
,
/* last entry all zeros */
{0, 0, 0, 0, 0}
};
static TFnReportDevice *_pfnReportDevice;
static TScannerModel *_pModel;
/*
MatchUsbDevice
==============
Matches a given USB vendor and product id against a list of
supported scanners.
IN iVendor USB vendor ID
iProduct USB product ID
OUT *ppModel Pointer to TScannerModel structure
Returns TRUE if a matching USB scanner was found
*/
STATIC bool
MatchUsbDevice (int iVendor, int iProduct, TScannerModel ** ppModel)
{
TScannerModel *pModels = ScannerModels;
DBG (DBG_MSG, "Matching USB device 0x%04X-0x%04X ... ", iVendor, iProduct);
while (pModels->pszName != NULL)
{
if ((pModels->iVendor == iVendor) && (pModels->iProduct == iProduct))
{
DBG (DBG_MSG, "found %s %s\n", pModels->pszVendor,
pModels->pszName);
*ppModel = pModels;
return TRUE;
}
/* next model to match */
pModels++;
}
DBG (DBG_MSG, "nothing found\n");
return FALSE;
}
/************************************************************************
Public functions for the SANE compilation
************************************************************************/
/* callback for sanei_usb_attach_matching_devices */
static SANE_Status
_AttachUsb (SANE_String_Const devname)
{
DBG (DBG_MSG, "_AttachUsb: found %s\n", devname);
_pfnReportDevice (_pModel, (const char *) devname);
return SANE_STATUS_GOOD;
}
/*
NiashXferInit
===============
Initialises all registered data transfer modules, which causes
them to report any devices found through the pfnReport callback.
IN pfnReport Function to call to report a transfer device
*/
static void
NiashXferInit (TFnReportDevice * pfnReport)
{
TScannerModel *pModels = ScannerModels;
sanei_usb_init ();
_pfnReportDevice = pfnReport;
/* loop over all scanner models */
while (pModels->pszName != NULL)
{
DBG (DBG_MSG, "Looking for %s...\n", pModels->pszName);
_pModel = pModels;
if (sanei_usb_find_devices ((SANE_Int) pModels->iVendor,
(SANE_Int) pModels->iProduct,
_AttachUsb) != SANE_STATUS_GOOD)
{
DBG (DBG_ERR, "Error invoking sanei_usb_find_devices");
break;
}
pModels++;
}
}
static int
NiashXferOpen (const char *pszName, EScannerModel * peModel)
{
SANE_Status status;
SANE_Word vendor, product;
int fd;
TScannerModel *pModel;
DBG (DBG_MSG, "Trying to open %s...\n", pszName);
status = sanei_usb_open (pszName, &fd);
if (status != SANE_STATUS_GOOD)
{
return -1;
}
status = sanei_usb_get_vendor_product (fd, &vendor, &product);
if (status == SANE_STATUS_GOOD)
{
MatchUsbDevice (vendor, product, &pModel);
*peModel = pModel->eModel;
}
DBG (DBG_MSG, "handle = %d\n", (int) fd);
return fd;
}
static void
NiashXferClose (int iHandle)
{
/* close usb device */
if (iHandle != -1)
{
sanei_usb_close (iHandle);
}
}
static void
parusb_write_reg (int fd, unsigned char bReg, unsigned char bValue)
{
sanei_usb_control_msg (fd,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0x0C, bReg, 0, 1, &bValue);
}
static void
parusb_read_reg (int fd, unsigned char bReg, unsigned char *pbValue)
{
sanei_usb_control_msg (fd,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0x0C, bReg, 0, 1, pbValue);
}
static void
NiashWriteReg (int iHandle, unsigned char bReg, unsigned char bData)
{
if (iHandle < 0)
{
DBG (DBG_MSG, "Invalid handle %d\n", iHandle);
return;
}
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, EPP_ADDR, bReg);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, EPP_DATA_WRITE, bData);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
}
static void
NiashReadReg (int iHandle, unsigned char bReg, unsigned char *pbData)
{
if (iHandle < 0)
{
return;
}
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, EPP_ADDR, bReg);
parusb_write_reg (iHandle, SPP_CONTROL, 0x34);
parusb_read_reg (iHandle, EPP_DATA_READ, pbData);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
}
static void
NiashWriteBulk (int iHandle, unsigned char *pabBuf, int iSize)
{
/* byte abSetup[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
HP3400 probably needs 0x01, 0x01 */
byte abSetup[8] = { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
size_t size;
if (iHandle < 0)
{
return;
}
/* select scanner register 0x24 */
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, EPP_ADDR, 0x24);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
/* tell scanner that a bulk transfer follows */
abSetup[4] = (iSize) & 0xFF;
abSetup[5] = (iSize >> 8) & 0xFF;
sanei_usb_control_msg (iHandle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0x04, USB_SETUP, 0, 8, abSetup);
/* do the bulk write */
size = iSize;
if (sanei_usb_write_bulk (iHandle, pabBuf, &size) != SANE_STATUS_GOOD)
{
DBG (DBG_ERR, "ERROR: Bulk write failed\n");
}
}
static void
NiashReadBulk (int iHandle, unsigned char *pabBuf, int iSize)
{
byte abSetup[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
size_t size;
if (iHandle < 0)
{
return;
}
/* select scanner register 0x24 */
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, EPP_ADDR, 0x24);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
/* tell scanner that a bulk transfer follows */
abSetup[4] = (iSize) & 0xFF;
abSetup[5] = (iSize >> 8) & 0xFF;
sanei_usb_control_msg (iHandle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0x04, USB_SETUP, 0, 8, abSetup);
/* do the bulk read */
size = iSize;
if (sanei_usb_read_bulk (iHandle, pabBuf, &size) != SANE_STATUS_GOOD)
{
DBG (DBG_ERR, "ERROR: Bulk read failed\n");
}
}
static void
NiashWakeup (int iHandle)
{
unsigned char abMagic[] = { 0xA0, 0xA8, 0x50, 0x58, 0x90, 0x98, 0xC0, 0xC8,
0x90, 0x98, 0xE0, 0xE8
};
int i;
if (iHandle < 0)
{
return;
}
/* write magic startup sequence */
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
for (i = 0; i < (int) sizeof (abMagic); i++)
{
parusb_write_reg (iHandle, SPP_DATA, abMagic[i]);
}
/* write 0x04 to scanner register 0x00 the hard way */
parusb_write_reg (iHandle, SPP_DATA, 0x00);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
parusb_write_reg (iHandle, SPP_CONTROL, 0x1D);
parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, SPP_DATA, 0x04);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
parusb_write_reg (iHandle, SPP_CONTROL, 0x17);
parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
}

Wyświetl plik

@ -0,0 +1,96 @@
/*
Copyright (C) 2001 Bertrik Sikken (bertrik@zonnet.nl)
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.
$Id$
*/
/*
Provides a simple interface to read and write data from the scanner,
without any knowledge whether it's a parallel or USB scanner
*/
#ifndef _NIASH_XFER_H_
#define _NIASH_XFER_H_
#include <stdio.h> /* for FILE * */
#include "niash_types.h"
/* register codes for the USB - IEEE1284 bridge */
#define USB_SETUP 0x82
#define EPP_ADDR 0x83
#define EPP_DATA_READ 0x84
#define EPP_DATA_WRITE 0x85
#define SPP_STATUS 0x86
#define SPP_CONTROL 0x87
#define SPP_DATA 0x88
typedef enum
{
eUnknownModel = 0,
eHp3300c,
eHp3400c,
eHp4300c,
eAgfaTouch
} EScannerModel;
typedef struct
{
char *pszVendor;
char *pszName;
int iVendor;
int iProduct;
EScannerModel eModel;
} TScannerModel;
typedef int (TFnReportDevice) (TScannerModel * pModel,
const char *pszDeviceName);
/* Creates our own DBG definitions, externs are define in main.c*/
#ifndef WITH_NIASH
#define DBG fprintf
extern FILE *DBG_MSG;
extern FILE *DBG_ERR;
extern FILE *BG_ASSERT;
#endif /* NO WITH_NIASH */
/* we do not make data prototypes */
#ifndef WITH_NIASH
/* list of supported models, the actual list is in niash_xfer.c */
extern TScannerModel ScannerModels[];
#endif /* NO WITH_NIASH */
STATIC void NiashXferInit (TFnReportDevice * pfnReport);
STATIC int NiashXferOpen (const char *pszName, EScannerModel * peModel);
STATIC void NiashXferClose (int iXferHandle);
STATIC void NiashWriteReg (int iXferHandle, unsigned char bReg,
unsigned char bData);
STATIC void NiashReadReg (int iXferHandle, unsigned char bReg,
unsigned char *pbData);
STATIC void NiashWriteBulk (int iXferHandle, unsigned char *pabBuf,
int iSize);
STATIC void NiashReadBulk (int iXferHandle, unsigned char *pabBuf, int iSize);
STATIC void NiashWakeup (int iXferHandle);
STATIC bool MatchUsbDevice (int iVendor, int iProduct,
TScannerModel ** ppeModel);
#endif /* _NIASH_XFER_H_ */