/* sane - Scanner Access Now Easy.

   Copyright (C) 2004 -2004 Gerard Klaver (gerard at gkall dot hobby dot nl)
   The teco2 and gl646 backend (Frank Zago) are used as a template for 
   this backend

   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. 
*/

/* 
	$Id: dvc323.h v 0.01 2004/01/16$
*/

/* Commands supported by the vidcam. */

typedef struct
{
  unsigned char data[16];
  int len;
}
CDB;

/*--------------------------------------------------------------------------*/

static inline int
getbitfield (unsigned char *pageaddr, int mask, int shift)
{
  return ((*pageaddr >> shift) & mask);
}


/*--------------------------------------------------------------------------*/

#include <stdio.h>

#define LIBUSB_TIMEOUT					1000 /* ms */

typedef unsigned char byte;

/*--------------------------------------------------------------------------*/

/* Black magic for color adjustment. Used only for ICM532B */
struct dpi_color_adjust
{
  int resolution_x;		/* x-resolution  */
  int resolution_y;             /* y-resolution  */

  int z3_color_0;		/* 0, 1 or 2 */
  int z3_color_1;		/* idem */
  int z3_color_2;		/* idem */

  int factor_x;

  int color_shift;		/* color plane shift in pixel. If a
				   * negative shift seems necessary, set
				   * factor_x to 1 */
};

/*--------------------------------------------------------------------------*/

enum Dvc323_Option
{
  /* Must come first */
  OPT_NUM_OPTS = 0,
  
  OPT_MODE_GROUP,
  OPT_MODE,			/* vidcam modes */
  OPT_RESOLUTION,		/* X and Y resolution */
  OPT_BRIGHTNESS,               /* brightness   */
  OPT_CONTRAST,                 /* contrast     */
  OPT_HUE,			/* hue          */
  OPT_SATURATION,               /* saturation   */ 

  OPT_ENHANCEMENT_GROUP,
  OPT_THRESHOLD,		/* Threshold */
  
  OPT_BLACK_LEVEL,              /* black level correction */
  OPT_WHITE_LEVEL_R,		/*white level red correction */
  OPT_WHITE_LEVEL_G,		/*white level green correction */
  OPT_WHITE_LEVEL_B,		/*white level blue correction */

  OPT_PREVIEW,

  /* must come last: */
  OPT_NUM_OPTIONS
};

/*--------------------------------------------------------------------------*/

/* 
 * Scanner supported by this backend. 
 */
struct vidcam_hardware
{
  /* USB stuff */
  SANE_Word vendor;
  SANE_Word product;
  SANE_Word class;
  
  /* Readable names */
  const char *vendor_name;		/* brand on the box */
  const char *product_name;		/* name on the box */

  int x_resolution_max;		/* maximum X dpi */
  int y_resolution_max;		/* maximum Y dpi */

  /* Resolutions supported in color mode. */
  const struct dpi_color_adjust *color_adjust;
};


#define COLOR_RAW_STR			SANE_I18N("Color raw")
#define COLOR_8_STR			SANE_I18N("Color 8 bit")
#define COLOR_5COMP_STR                 SANE_I18N("Color 5x compresion")
#define COLOR_STR			SANE_I18N("Color")
/*--------------------------------------------------------------------------*/

/* Define a vidcam occurence. */
typedef struct Dvc323_Vidcam
{
  struct Dvc323_Vidcam *next;
  SANE_Device sane;

  char *devicename;
  SANE_Int fd;			/* device handle */
  SANE_Int n_pack;
  SANE_Int p_size;

  /* USB handling */
  size_t buffer_size;		/* size of the buffer */
  SANE_Byte *buffer;		/* for USB transfer. */

  /* Scanner infos. */
  const struct vidcam_hardware *hw;	/* default options for that vidcam */

  SANE_Word *resolutions_list;

  /* Scanning handling. */
  int scanning;			/* TRUE if a scan is running. */
  int x_resolution;		/* X resolution in DPI */
  int y_resolution;		/* Y resolution in DPI */
  int depth;			/* depth per color */
  SANE_Parameters s_params;
  enum
  {
	  DVC323_LINEART,
	  DVC323_GRAY,
	  DVC323_COLOR_RAW,
	  DVC323_COLOR_8,
	  DVC323_COLOR_5COMP,
	  DVC323_COLOR
  }
  scan_mode;
  
  size_t bytes_left;		/* number of bytes left to give to the backend */

  size_t real_bytes_left;	/* number of bytes left the vidcam will return. */

  SANE_Byte *image;		/* keep the raw image here */
  size_t image_size;		/* allocated size of image */
  size_t image_begin;		/* first significant byte in image */
  size_t image_end;		/* first free byte in image */

  const struct dpi_color_adjust *color_adjust;

/* USB control messages handling */
  size_t windoww_size;		/* size of window write */
  size_t windowr_size;		/* size of window read  */
  SANE_Byte *windoww;		/* for window write     */
  SANE_Byte *windowr;		/* for window read      */
  
  SANE_Parameters params;

  /* Options */
  SANE_Option_Descriptor opt[OPT_NUM_OPTIONS];
  Option_Value val[OPT_NUM_OPTIONS];

}
Dvc323_Vidcam;

/* Commands for the DVC323. */
typedef enum
{
	DVC323_ISO_READ = 0x83
}
Dvc323_Request;



/*--------------------------------------------------------------------------*/

/* Data for the dvc323 */
const SANE_Byte dvc323_init_str1[] = {
    0xf0, 0xff, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02,
    0xe0, 0x01, 0x02, 0x04, 0x04, 0x02, 0x08, 0x08, 0x46, 0x00, 0x24, 0x00, 0x00, 0x00, 0x03, 0x00,
    0x20, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x68, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const SANE_Byte dvc323_init_str2[] = {
    0x1f, 0x2f, 0x3f, 0x4f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02,
    0xe0, 0x01, 0x01, 0x02, 0x01, 0x01, 0x02, 0x02, 0x46, 0x00, 0xd2, 0x00, 0x00, 0x00, 0x03, 0x00,
    0x20, 0x01, 0x01, 0x00, 0x01, 0x07, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const SANE_Byte dvc323_init_str3[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00

};
const SANE_Byte dvc323_init_str4[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00

};

/*--------------------------------------------------------------------------*/

/* Debug levels. 
 * Should be common to all backends. */

#define DBG_error0  0
#define DBG_error   1
#define DBG_sense   2
#define DBG_warning 3
#define DBG_inquiry 4
#define DBG_info    5
#define DBG_info2   6
#define DBG_proc    7
#define DBG_read    8
#define DBG_sane_init   10
#define DBG_sane_proc   11
#define DBG_sane_info   12
#define DBG_sane_option 13

/*--------------------------------------------------------------------------*/

/* 32 bits from an array to an integer (eg ntohl). */
#define B32TOI(buf) \
	((((unsigned char *)buf)[0] << 24) | \
	 (((unsigned char *)buf)[1] << 16) | \
	 (((unsigned char *)buf)[2] <<  8) |  \
	 (((unsigned char *)buf)[3] <<  0))

#define B16TOI(buf) \
	((((unsigned char *)buf)[0] <<  8) | \
	 (((unsigned char *)buf)[1] <<  0))
