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