/* sane - Scanner Access Now Easy.

   Copyright (C) 2008 -2008 Gerard Klaver (gerard at gkall dot hobby dot nl)
                            Paul Wise
   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: lexmark2.h,v 1.1 2008/04/28 09:30:46 gerard Exp $
*/

/* Commands supported by the scanner. */

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

/* Store an integer in 2, 3 or 4 byte in an array. */
#define Ito16(val, buf) { \
 ((unsigned char *)buf)[0] = ((val) >> 8) & 0xff; \
 ((unsigned char *)buf)[1] = ((val) >> 0) & 0xff; \
}

#define Ito24(val, buf) { \
 ((unsigned char *)buf)[0] = ((val) >> 16) & 0xff; \
 ((unsigned char *)buf)[1] = ((val) >>  8) & 0xff; \
 ((unsigned char *)buf)[2] = ((val) >>  0) & 0xff; \
}

#define Ito32(val, buf) { \
 ((unsigned char *)buf)[0] = ((val) >> 24) & 0xff; \
 ((unsigned char *)buf)[1] = ((val) >> 16) & 0xff; \
 ((unsigned char *)buf)[2] = ((val) >>  8) & 0xff; \
 ((unsigned char *)buf)[3] = ((val) >>  0) & 0xff; \
}

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

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;

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

#define MM_PER_INCH     25.4
#define mmToIlu(mm) (((mm) * dev->hw->x_resolution_max) / MM_PER_INCH)
#define iluToMm(ilu) (((ilu) * MM_PER_INCH) / dev->hw->x_resolution_max)

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

/* Black magic for color adjustment. Used only for Lexmark2 */
struct dpi_color_adjust
{
  int resolution_y;		/* y resolution  */
  int resolution_x;		/* x 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 Lexmark2_Option
{
  /* Must come first */
  OPT_NUM_OPTS = 0,
  
  OPT_MODE_GROUP,
  OPT_MODE,			/* scanner modes */
  OPT_RESOLUTION,		/* X and Y resolution */
  OPT_BRIGHTNESS,               /* brightness   */
  OPT_CONTRAST,                 /* contrast     */

  OPT_GEOMETRY_GROUP,
  OPT_TL_X,			/* upper left X */
  OPT_TL_Y,			/* upper left Y */
  OPT_BR_X,			/* bottom right X */
  OPT_BR_Y,			/* bottom right Y */

  OPT_ENHANCEMENT_GROUP,
  OPT_THRESHOLD,		/* Threshold */
  

  OPT_DITHER,
  OPT_FILTER_COLOR,		/* which color to filter */

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


  SANE_Range res_range;

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

  int cal_length;		/* size of a calibration line in pixels */
  int cal_lines;		/* number of calibration lines to read */
  int cal_col_len;		/* number of byte to code one color */
  int cal_algo;			/* default algo to use to compute calibration line */

  /* Minimum and maximum width and length supported. */
  SANE_Range x_range;
  SANE_Range y_range;

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

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

/* Define a scanner occurence. */
typedef struct Lexmark2_Scanner
{
  struct Lexmark2_Scanner *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 scanner_hardware *hw;	/* default options for that scanner */

  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 x_tl;			/* X top left */
  int y_tl;			/* Y top left */
  int x_br;			/* X bottom right */
  int y_br;			/* Y bottom right */
  int width;			/* width of the scan area in mm */
  int length;			/* length of the scan area in mm */
  int depth;			/* depth per color */

  enum
  {
	  LEXMARK2_LINEART,
	  LEXMARK2_GRAY,
	  LEXMARK2_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 scanner 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;

  size_t bytes_per_raster;	/* bytes per raster. In B&W and Gray,
				   that the same as
				   param.bytes_per_lines. In Color,
				   it's a third.
				 */
  
  int raster_size;		/* size of a raster */
  int raster_num;		/* for color scan, current raster read */
  int raster_real;		/* real number of raster in the
				   * scan. This is necessary since I
				   * don't know how to reliably compute
				   * the number of lines */
  int raster_ahead;		/* max size of the incomplete lines */
  int line;			/* current line of the scan */

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


/* USB int messages handling    */
  size_t size11;
  size_t size15;
  SANE_Byte *buffer11;
  SANE_Byte *buffer15;

  SANE_Parameters params;

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

}
Lexmark2_Scanner;

/* Commands for the LEXMARK2. */
typedef enum
{
	LEXMARK2_BULK_WRITE = 0x02,
	LEXMARK2_BULK_READ = 0x82,
	LEXMARK2_INT_READ = 0x83
}
Lexmark2_Request;



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

/* Data for the lexmark2 URB 102 */
const SANE_Byte lexmark2_init_str1[] = { 
    0xf1, 0xff, 0x24, 0x00, 0x29, 0x0a, 0x48, 0x49, 0x2c, 0xa8, 0x2b, 0x2b, 0x7a, 0x8e, 0x5f, 0x31,
    0x40, 0xeb, 0x0c, 0x30, 0x04, 0x00, 0x00 
    
};

/* Data for the lexmark2 URB 103 */
const SANE_Byte lexmark2_init_str2[] = {
    0x10, 0x12, 0x70, 0x72, 0x50, 0x52, 0x70, 0x73, 0xff, 0xff
};

/* Data for the lexmark2 URB 104 */
const SANE_Byte lexmark2_init_str3[] = {
    0x36, 0x2b, 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))
