1141cc406Sopenharmony_ci/*
2141cc406Sopenharmony_ci * SANE backend for Xerox Phaser 3200MFP et al.
3141cc406Sopenharmony_ci * Copyright 2008-2016 ABC <abc@telekom.ru>
4141cc406Sopenharmony_ci *
5141cc406Sopenharmony_ci * Network Scanners Support
6141cc406Sopenharmony_ci * Copyright 2010 Alexander Kuznetsov <acca(at)cpan.org>
7141cc406Sopenharmony_ci *
8141cc406Sopenharmony_ci * Color scanning on Samsung M2870 model and Xerox Cognac 3215 & 3225
9141cc406Sopenharmony_ci * models by Laxmeesh Onkar Markod <m.laxmeesh@samsung.com>
10141cc406Sopenharmony_ci *
11141cc406Sopenharmony_ci * This program is licensed under GPL + SANE exception.
12141cc406Sopenharmony_ci * More info at http://www.sane-project.org/license.html
13141cc406Sopenharmony_ci */
14141cc406Sopenharmony_ci
15141cc406Sopenharmony_ci#ifndef xerox_mfp_h
16141cc406Sopenharmony_ci#define xerox_mfp_h
17141cc406Sopenharmony_ci
18141cc406Sopenharmony_ci#ifdef __GNUC__
19141cc406Sopenharmony_ci#define UNUSED(x) x __attribute__((unused))
20141cc406Sopenharmony_ci#else
21141cc406Sopenharmony_ci#define UNUSED(x) x
22141cc406Sopenharmony_ci#endif
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci#define MIN(a,b) (((a) < (b)) ? (a) : (b))
25141cc406Sopenharmony_ci
26141cc406Sopenharmony_ci#define UNCONST(ptr) ((void *)(long)(ptr))
27141cc406Sopenharmony_ci
28141cc406Sopenharmony_ci#define PNT_PER_MM	(1200. / MM_PER_INCH)
29141cc406Sopenharmony_ci
30141cc406Sopenharmony_ci#define PADDING_SIZE	16
31141cc406Sopenharmony_ci
32141cc406Sopenharmony_ci#define SWAP_Word(x, y) { SANE_Word z = x; x = y; y = z; }
33141cc406Sopenharmony_ci
34141cc406Sopenharmony_cienum options {
35141cc406Sopenharmony_ci    OPT_NUMOPTIONS,
36141cc406Sopenharmony_ci    OPT_GROUP_STD,
37141cc406Sopenharmony_ci    OPT_RESOLUTION,	/* dpi*/
38141cc406Sopenharmony_ci    OPT_MODE,		/* color */
39141cc406Sopenharmony_ci    OPT_THRESHOLD,	/* brightness */
40141cc406Sopenharmony_ci    OPT_SOURCE,		/* affects max window size */
41141cc406Sopenharmony_ci    OPT_JPEG,
42141cc406Sopenharmony_ci    OPT_GROUP_GEO,
43141cc406Sopenharmony_ci    OPT_SCAN_TL_X,	/* for (OPT_SCAN_TL_X to OPT_SCAN_BR_Y) */
44141cc406Sopenharmony_ci    OPT_SCAN_TL_Y,
45141cc406Sopenharmony_ci    OPT_SCAN_BR_X,
46141cc406Sopenharmony_ci    OPT_SCAN_BR_Y,
47141cc406Sopenharmony_ci    NUM_OPTIONS
48141cc406Sopenharmony_ci};
49141cc406Sopenharmony_ci
50141cc406Sopenharmony_citypedef struct transport transport;
51141cc406Sopenharmony_ci
52141cc406Sopenharmony_cistruct device {
53141cc406Sopenharmony_ci    struct device *next;
54141cc406Sopenharmony_ci    SANE_Device sane;
55141cc406Sopenharmony_ci    int dn;			/* usb file descriptor */
56141cc406Sopenharmony_ci    SANE_Byte res[1024];		/* buffer for responses */
57141cc406Sopenharmony_ci    size_t reslen;		/* response len */
58141cc406Sopenharmony_ci    SANE_Option_Descriptor opt[NUM_OPTIONS];
59141cc406Sopenharmony_ci    Option_Value val[NUM_OPTIONS];
60141cc406Sopenharmony_ci    SANE_Parameters para;
61141cc406Sopenharmony_ci    SANE_Bool non_blocking;
62141cc406Sopenharmony_ci    int scanning;			/* scanning is started */
63141cc406Sopenharmony_ci    int cancel;			/* cancel flag */
64141cc406Sopenharmony_ci    int state;			/* current state */
65141cc406Sopenharmony_ci    int reserved;			/* CMD_RESERVE_UNIT */
66141cc406Sopenharmony_ci    int reading;			/* READ_IMAGE is sent */
67141cc406Sopenharmony_ci
68141cc406Sopenharmony_ci    SANE_Byte *data;		/* postprocessing cyclic buffer 64k */
69141cc406Sopenharmony_ci    int datalen;			/* how data in buffer */
70141cc406Sopenharmony_ci    int dataoff;			/* offset of data */
71141cc406Sopenharmony_ci    int dataindex;		/* sequental number */
72141cc406Sopenharmony_ci#define DATAMASK 0xffff		/* mask of data buffer */
73141cc406Sopenharmony_ci#define DATASIZE (DATAMASK + 1)	/* size of data buffer */
74141cc406Sopenharmony_ci    /* 64K will be enough to hold whole line of 2400 dpi of 23cm */
75141cc406Sopenharmony_ci#define DATATAIL(dev) ((dev->dataoff + dev->datalen) & DATAMASK)
76141cc406Sopenharmony_ci#define DATAROOM(dev) dataroom(dev)
77141cc406Sopenharmony_ci
78141cc406Sopenharmony_ci#define POST_DATASIZE 0xFFFFFF	/* 16777215 bytes */
79141cc406Sopenharmony_ci    SANE_Byte *decData;		/* static buffer of POST_DATASIZE bytes */
80141cc406Sopenharmony_ci    int decDataSize;
81141cc406Sopenharmony_ci    int currentDecDataIndex;
82141cc406Sopenharmony_ci    /* data from CMD_INQUIRY: */
83141cc406Sopenharmony_ci    int resolutions;		/* supported resolution bitmask */
84141cc406Sopenharmony_ci    int compositions;		/* supported image compositions bitmask */
85141cc406Sopenharmony_ci    int max_len;			/* effective max len for current doc source */
86141cc406Sopenharmony_ci    int max_win_width;
87141cc406Sopenharmony_ci    int max_win_len;
88141cc406Sopenharmony_ci    int max_len_adf;
89141cc406Sopenharmony_ci    int max_len_fb;
90141cc406Sopenharmony_ci    int line_order;		/* if need post processing */
91141cc406Sopenharmony_ci    SANE_Word dpi_list[30];	/* allowed resolutions */
92141cc406Sopenharmony_ci    int doc_loaded;
93141cc406Sopenharmony_ci
94141cc406Sopenharmony_ci    SANE_Range win_x_range;
95141cc406Sopenharmony_ci    SANE_Range win_y_range;
96141cc406Sopenharmony_ci
97141cc406Sopenharmony_ci    /* CMD_SET_WINDOW parameters we set: */
98141cc406Sopenharmony_ci    int win_width;		/* in 1200dpi points */
99141cc406Sopenharmony_ci    int win_len;
100141cc406Sopenharmony_ci    double win_off_x;		/* in inches (byte.byte) */
101141cc406Sopenharmony_ci    double win_off_y;
102141cc406Sopenharmony_ci    int resolution;		/* dpi indexed values */
103141cc406Sopenharmony_ci    int composition;		/* MODE_ */
104141cc406Sopenharmony_ci    int doc_source;		/* document source */
105141cc406Sopenharmony_ci    int threshold;		/* brightness */
106141cc406Sopenharmony_ci    int compressionTypes;
107141cc406Sopenharmony_ci    SANE_Bool compressionEnabled;
108141cc406Sopenharmony_ci
109141cc406Sopenharmony_ci    /* CMD_READ data. It is per block only, image could be in many blocks */
110141cc406Sopenharmony_ci    int blocklen;			/* image data block len (padding incl.) */
111141cc406Sopenharmony_ci    int vertical;			/* lines in block (padded) */
112141cc406Sopenharmony_ci    int horizontal;		/* b/w: bytes, gray/color: pixels (padded) */
113141cc406Sopenharmony_ci    int final_block;
114141cc406Sopenharmony_ci    int pixels_per_line;
115141cc406Sopenharmony_ci    int bytes_per_line;
116141cc406Sopenharmony_ci    int ulines;			/* up to this block including */
117141cc406Sopenharmony_ci    int y_off;			/* up to this block excluding*/
118141cc406Sopenharmony_ci    int blocks;
119141cc406Sopenharmony_ci
120141cc406Sopenharmony_ci    /* stat */
121141cc406Sopenharmony_ci    int total_img_size;		/* predicted image size */
122141cc406Sopenharmony_ci    int total_out_size;		/* total we sent to user */
123141cc406Sopenharmony_ci    int total_data_size;		/* total of what scanner sent us */
124141cc406Sopenharmony_ci
125141cc406Sopenharmony_ci    /* transport to use */
126141cc406Sopenharmony_ci    transport *io;
127141cc406Sopenharmony_ci};
128141cc406Sopenharmony_ci
129141cc406Sopenharmony_ci
130141cc406Sopenharmony_ci/*	Transport abstract layer	*/
131141cc406Sopenharmony_cistruct transport {
132141cc406Sopenharmony_ci    char *ttype;
133141cc406Sopenharmony_ci
134141cc406Sopenharmony_ci    int (*dev_request)(struct device *dev,
135141cc406Sopenharmony_ci                       SANE_Byte *cmd, size_t cmdlen,
136141cc406Sopenharmony_ci                       SANE_Byte *resp, size_t *resplen);
137141cc406Sopenharmony_ci    SANE_Status(*dev_open)(struct device *dev);
138141cc406Sopenharmony_ci    void (*dev_close)(struct device *dev);
139141cc406Sopenharmony_ci    SANE_Status(*configure_device)(const char *devname, SANE_Status(*cb)(SANE_String_Const devname));
140141cc406Sopenharmony_ci};
141141cc406Sopenharmony_ci
142141cc406Sopenharmony_ci/*	USB transport	*/
143141cc406Sopenharmony_ciint		usb_dev_request(struct device *dev, SANE_Byte *cmd, size_t cmdlen, SANE_Byte *resp, size_t *resplen);
144141cc406Sopenharmony_ciSANE_Status	usb_dev_open(struct device *dev);
145141cc406Sopenharmony_civoid		usb_dev_close(struct device *dev);
146141cc406Sopenharmony_ciSANE_Status	usb_configure_device(const char *devname, SANE_Status(*cb)(SANE_String_Const devname));
147141cc406Sopenharmony_ci
148141cc406Sopenharmony_ci/*	TCP unicast	*/
149141cc406Sopenharmony_ciint		tcp_dev_request(struct device *dev, SANE_Byte *cmd, size_t cmdlen, SANE_Byte *resp, size_t *resplen);
150141cc406Sopenharmony_ciSANE_Status	tcp_dev_open(struct device *dev);
151141cc406Sopenharmony_civoid		tcp_dev_close(struct device *dev);
152141cc406Sopenharmony_ciSANE_Status	tcp_configure_device(const char *devname, SANE_Status(*cb)(SANE_String_Const devname));
153141cc406Sopenharmony_ci
154141cc406Sopenharmony_ci/* device wants transfer buffer to be multiple of 512 */
155141cc406Sopenharmony_ci#define USB_BLOCK_SIZE 512
156141cc406Sopenharmony_ci#define USB_BLOCK_MASK ~(USB_BLOCK_SIZE - 1)
157141cc406Sopenharmony_ci
158141cc406Sopenharmony_cistatic inline int dataroom(struct device *dev)
159141cc406Sopenharmony_ci{
160141cc406Sopenharmony_ci    int tail = DATATAIL(dev);
161141cc406Sopenharmony_ci    if (tail < dev->dataoff)
162141cc406Sopenharmony_ci        return dev->dataoff - tail;
163141cc406Sopenharmony_ci    else if (dev->datalen == DATASIZE) {
164141cc406Sopenharmony_ci        return 0;
165141cc406Sopenharmony_ci    } else
166141cc406Sopenharmony_ci        return DATASIZE - tail;
167141cc406Sopenharmony_ci}
168141cc406Sopenharmony_ci
169141cc406Sopenharmony_ci/*	Functions from original xerox_mfp.c, used in -usb.c and -tcp.c	*/
170141cc406Sopenharmony_ciSANE_Status ret_cancel(struct device *dev, SANE_Status ret);
171141cc406Sopenharmony_ci
172141cc406Sopenharmony_ci/* a la SCSI commands. */ /* request len, response len, exception */
173141cc406Sopenharmony_ci#define CMD_ABORT		0x06	/*  4, 32 */
174141cc406Sopenharmony_ci#define CMD_INQUIRY		0x12	/*  4, 70 */
175141cc406Sopenharmony_ci#define CMD_RESERVE_UNIT	0x16	/*  4, 32 */
176141cc406Sopenharmony_ci#define CMD_RELEASE_UNIT	0x17	/*  4, 32 */
177141cc406Sopenharmony_ci#define CMD_SET_WINDOW		0x24	/* 25, 32, specified req len is 22 */
178141cc406Sopenharmony_ci#define CMD_READ		0x28	/*  4, 32 */
179141cc406Sopenharmony_ci#define CMD_READ_IMAGE		0x29	/*  4, var + padding[16] */
180141cc406Sopenharmony_ci#define CMD_OBJECT_POSITION	0x31	/*  4, 32 */
181141cc406Sopenharmony_ci
182141cc406Sopenharmony_ci/* Packet Headers */
183141cc406Sopenharmony_ci#define REQ_CODE_A		0x1b
184141cc406Sopenharmony_ci#define REQ_CODE_B		0xa8
185141cc406Sopenharmony_ci#define RES_CODE		0xa8
186141cc406Sopenharmony_ci
187141cc406Sopenharmony_ci/* Status Codes, going into dev->state */
188141cc406Sopenharmony_ci#define STATUS_GOOD		0x00
189141cc406Sopenharmony_ci#define STATUS_CHECK		0x02	/* MSG_SCANNER_STATE */
190141cc406Sopenharmony_ci#define STATUS_CANCEL		0x04
191141cc406Sopenharmony_ci#define STATUS_BUSY		0x08
192141cc406Sopenharmony_ci
193141cc406Sopenharmony_ci/* Message Code */
194141cc406Sopenharmony_ci#define MSG_NO_MESSAGE		0x00
195141cc406Sopenharmony_ci#define MSG_PRODUCT_INFO	0x10	/* CMD_INQUIRY */
196141cc406Sopenharmony_ci#define MSG_SCANNER_STATE	0x20	/* CMD_RESERVE_UNIT, and
197141cc406Sopenharmony_ci					   CMD_READ, CMD_SET_WINDOW, CMD_OBJECT_POSITION */
198141cc406Sopenharmony_ci#define MSG_SCANNING_PARAM	0x30	/* CMD_SET_WINDOW */
199141cc406Sopenharmony_ci#define MSG_PREVIEW_PARAM	0x31	/* CMD_SET_WINDOW */
200141cc406Sopenharmony_ci#define MSG_LINK_BLOCK		0x80	/* CMD_READ */
201141cc406Sopenharmony_ci#define MSG_END_BLOCK		0x81	/* CMD_READ */
202141cc406Sopenharmony_ci
203141cc406Sopenharmony_ci/* Scanner State Bits (if MSG_SCANNER_STATE if STATUS_CHECK) */
204141cc406Sopenharmony_ci#define STATE_NO_ERROR		0x001
205141cc406Sopenharmony_ci#define STATE_COMMAND_ERROR	0x002
206141cc406Sopenharmony_ci#define STATE_UNSUPPORTED	0x004
207141cc406Sopenharmony_ci#define STATE_RESET		0x008
208141cc406Sopenharmony_ci#define STATE_NO_DOCUMENT	0x010
209141cc406Sopenharmony_ci#define STATE_DOCUMENT_JAM	0x020
210141cc406Sopenharmony_ci#define STATE_COVER_OPEN	0x040
211141cc406Sopenharmony_ci#define STATE_WARMING		0x080
212141cc406Sopenharmony_ci#define STATE_LOCKING		0x100
213141cc406Sopenharmony_ci#define STATE_INVALID_AREA	0x200
214141cc406Sopenharmony_ci#define STATE_RESOURCE_BUSY	0x400
215141cc406Sopenharmony_ci
216141cc406Sopenharmony_ci/* Image Composition */
217141cc406Sopenharmony_ci#define MODE_LINEART		0x00
218141cc406Sopenharmony_ci#define MODE_HALFTONE		0x01
219141cc406Sopenharmony_ci#define MODE_GRAY8		0x03
220141cc406Sopenharmony_ci#define MODE_RGB24		0x05
221141cc406Sopenharmony_ci
222141cc406Sopenharmony_ci/* Document Source */
223141cc406Sopenharmony_ci#define DOC_ADF			0x20
224141cc406Sopenharmony_ci#define DOC_FLATBED		0x40
225141cc406Sopenharmony_ci#define DOC_AUTO		0x80
226141cc406Sopenharmony_ci
227141cc406Sopenharmony_ci#endif	/* xerox_mfp_h	*/
228