1 /*
2  * SANE backend for Xerox Phaser 3200MFP et al.
3  * Copyright 2008-2016 ABC <abc@telekom.ru>
4  *
5  * Network Scanners Support
6  * Copyright 2010 Alexander Kuznetsov <acca(at)cpan.org>
7  *
8  * Color scanning on Samsung M2870 model and Xerox Cognac 3215 & 3225
9  * models by Laxmeesh Onkar Markod <m.laxmeesh@samsung.com>
10  *
11  * This program is licensed under GPL + SANE exception.
12  * More info at http://www.sane-project.org/license.html
13  */
14 
15 #ifndef xerox_mfp_h
16 #define xerox_mfp_h
17 
18 #ifdef __GNUC__
19 #define UNUSED(x) x __attribute__((unused))
20 #else
21 #define UNUSED(x) x
22 #endif
23 
24 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
25 
26 #define UNCONST(ptr) ((void *)(long)(ptr))
27 
28 #define PNT_PER_MM	(1200. / MM_PER_INCH)
29 
30 #define PADDING_SIZE	16
31 
32 #define SWAP_Word(x, y) { SANE_Word z = x; x = y; y = z; }
33 
34 enum options {
35     OPT_NUMOPTIONS,
36     OPT_GROUP_STD,
37     OPT_RESOLUTION,	/* dpi*/
38     OPT_MODE,		/* color */
39     OPT_THRESHOLD,	/* brightness */
40     OPT_SOURCE,		/* affects max window size */
41     OPT_JPEG,
42     OPT_GROUP_GEO,
43     OPT_SCAN_TL_X,	/* for (OPT_SCAN_TL_X to OPT_SCAN_BR_Y) */
44     OPT_SCAN_TL_Y,
45     OPT_SCAN_BR_X,
46     OPT_SCAN_BR_Y,
47     NUM_OPTIONS
48 };
49 
50 typedef struct transport transport;
51 
52 struct device {
53     struct device *next;
54     SANE_Device sane;
55     int dn;			/* usb file descriptor */
56     SANE_Byte res[1024];		/* buffer for responses */
57     size_t reslen;		/* response len */
58     SANE_Option_Descriptor opt[NUM_OPTIONS];
59     Option_Value val[NUM_OPTIONS];
60     SANE_Parameters para;
61     SANE_Bool non_blocking;
62     int scanning;			/* scanning is started */
63     int cancel;			/* cancel flag */
64     int state;			/* current state */
65     int reserved;			/* CMD_RESERVE_UNIT */
66     int reading;			/* READ_IMAGE is sent */
67 
68     SANE_Byte *data;		/* postprocessing cyclic buffer 64k */
69     int datalen;			/* how data in buffer */
70     int dataoff;			/* offset of data */
71     int dataindex;		/* sequental number */
72 #define DATAMASK 0xffff		/* mask of data buffer */
73 #define DATASIZE (DATAMASK + 1)	/* size of data buffer */
74     /* 64K will be enough to hold whole line of 2400 dpi of 23cm */
75 #define DATATAIL(dev) ((dev->dataoff + dev->datalen) & DATAMASK)
76 #define DATAROOM(dev) dataroom(dev)
77 
78 #define POST_DATASIZE 0xFFFFFF	/* 16777215 bytes */
79     SANE_Byte *decData;		/* static buffer of POST_DATASIZE bytes */
80     int decDataSize;
81     int currentDecDataIndex;
82     /* data from CMD_INQUIRY: */
83     int resolutions;		/* supported resolution bitmask */
84     int compositions;		/* supported image compositions bitmask */
85     int max_len;			/* effective max len for current doc source */
86     int max_win_width;
87     int max_win_len;
88     int max_len_adf;
89     int max_len_fb;
90     int line_order;		/* if need post processing */
91     SANE_Word dpi_list[30];	/* allowed resolutions */
92     int doc_loaded;
93 
94     SANE_Range win_x_range;
95     SANE_Range win_y_range;
96 
97     /* CMD_SET_WINDOW parameters we set: */
98     int win_width;		/* in 1200dpi points */
99     int win_len;
100     double win_off_x;		/* in inches (byte.byte) */
101     double win_off_y;
102     int resolution;		/* dpi indexed values */
103     int composition;		/* MODE_ */
104     int doc_source;		/* document source */
105     int threshold;		/* brightness */
106     int compressionTypes;
107     SANE_Bool compressionEnabled;
108 
109     /* CMD_READ data. It is per block only, image could be in many blocks */
110     int blocklen;			/* image data block len (padding incl.) */
111     int vertical;			/* lines in block (padded) */
112     int horizontal;		/* b/w: bytes, gray/color: pixels (padded) */
113     int final_block;
114     int pixels_per_line;
115     int bytes_per_line;
116     int ulines;			/* up to this block including */
117     int y_off;			/* up to this block excluding*/
118     int blocks;
119 
120     /* stat */
121     int total_img_size;		/* predicted image size */
122     int total_out_size;		/* total we sent to user */
123     int total_data_size;		/* total of what scanner sent us */
124 
125     /* transport to use */
126     transport *io;
127 };
128 
129 
130 /*	Transport abstract layer	*/
131 struct transport {
132     char *ttype;
133 
134     int (*dev_request)(struct device *dev,
135                        SANE_Byte *cmd, size_t cmdlen,
136                        SANE_Byte *resp, size_t *resplen);
137     SANE_Status(*dev_open)(struct device *dev);
138     void (*dev_close)(struct device *dev);
139     SANE_Status(*configure_device)(const char *devname, SANE_Status(*cb)(SANE_String_Const devname));
140 };
141 
142 /*	USB transport	*/
143 int		usb_dev_request(struct device *dev, SANE_Byte *cmd, size_t cmdlen, SANE_Byte *resp, size_t *resplen);
144 SANE_Status	usb_dev_open(struct device *dev);
145 void		usb_dev_close(struct device *dev);
146 SANE_Status	usb_configure_device(const char *devname, SANE_Status(*cb)(SANE_String_Const devname));
147 
148 /*	TCP unicast	*/
149 int		tcp_dev_request(struct device *dev, SANE_Byte *cmd, size_t cmdlen, SANE_Byte *resp, size_t *resplen);
150 SANE_Status	tcp_dev_open(struct device *dev);
151 void		tcp_dev_close(struct device *dev);
152 SANE_Status	tcp_configure_device(const char *devname, SANE_Status(*cb)(SANE_String_Const devname));
153 
154 /* device wants transfer buffer to be multiple of 512 */
155 #define USB_BLOCK_SIZE 512
156 #define USB_BLOCK_MASK ~(USB_BLOCK_SIZE - 1)
157 
dataroom(struct device *dev)158 static inline int dataroom(struct device *dev)
159 {
160     int tail = DATATAIL(dev);
161     if (tail < dev->dataoff)
162         return dev->dataoff - tail;
163     else if (dev->datalen == DATASIZE) {
164         return 0;
165     } else
166         return DATASIZE - tail;
167 }
168 
169 /*	Functions from original xerox_mfp.c, used in -usb.c and -tcp.c	*/
170 SANE_Status ret_cancel(struct device *dev, SANE_Status ret);
171 
172 /* a la SCSI commands. */ /* request len, response len, exception */
173 #define CMD_ABORT		0x06	/*  4, 32 */
174 #define CMD_INQUIRY		0x12	/*  4, 70 */
175 #define CMD_RESERVE_UNIT	0x16	/*  4, 32 */
176 #define CMD_RELEASE_UNIT	0x17	/*  4, 32 */
177 #define CMD_SET_WINDOW		0x24	/* 25, 32, specified req len is 22 */
178 #define CMD_READ		0x28	/*  4, 32 */
179 #define CMD_READ_IMAGE		0x29	/*  4, var + padding[16] */
180 #define CMD_OBJECT_POSITION	0x31	/*  4, 32 */
181 
182 /* Packet Headers */
183 #define REQ_CODE_A		0x1b
184 #define REQ_CODE_B		0xa8
185 #define RES_CODE		0xa8
186 
187 /* Status Codes, going into dev->state */
188 #define STATUS_GOOD		0x00
189 #define STATUS_CHECK		0x02	/* MSG_SCANNER_STATE */
190 #define STATUS_CANCEL		0x04
191 #define STATUS_BUSY		0x08
192 
193 /* Message Code */
194 #define MSG_NO_MESSAGE		0x00
195 #define MSG_PRODUCT_INFO	0x10	/* CMD_INQUIRY */
196 #define MSG_SCANNER_STATE	0x20	/* CMD_RESERVE_UNIT, and
197 					   CMD_READ, CMD_SET_WINDOW, CMD_OBJECT_POSITION */
198 #define MSG_SCANNING_PARAM	0x30	/* CMD_SET_WINDOW */
199 #define MSG_PREVIEW_PARAM	0x31	/* CMD_SET_WINDOW */
200 #define MSG_LINK_BLOCK		0x80	/* CMD_READ */
201 #define MSG_END_BLOCK		0x81	/* CMD_READ */
202 
203 /* Scanner State Bits (if MSG_SCANNER_STATE if STATUS_CHECK) */
204 #define STATE_NO_ERROR		0x001
205 #define STATE_COMMAND_ERROR	0x002
206 #define STATE_UNSUPPORTED	0x004
207 #define STATE_RESET		0x008
208 #define STATE_NO_DOCUMENT	0x010
209 #define STATE_DOCUMENT_JAM	0x020
210 #define STATE_COVER_OPEN	0x040
211 #define STATE_WARMING		0x080
212 #define STATE_LOCKING		0x100
213 #define STATE_INVALID_AREA	0x200
214 #define STATE_RESOURCE_BUSY	0x400
215 
216 /* Image Composition */
217 #define MODE_LINEART		0x00
218 #define MODE_HALFTONE		0x01
219 #define MODE_GRAY8		0x03
220 #define MODE_RGB24		0x05
221 
222 /* Document Source */
223 #define DOC_ADF			0x20
224 #define DOC_FLATBED		0x40
225 #define DOC_AUTO		0x80
226 
227 #endif	/* xerox_mfp_h	*/
228