1 /*
2 * I/O routines for Epson scanners
3 *
4 * Based on Kazuhiro Sasayama previous
5 * Work on epson.[ch] file from the SANE package.
6 * Please see those files for original copyrights.
7 *
8 * Copyright (C) 2006 Tower Technologies
9 * Author: Alessandro Zummo <a.zummo@towertech.it>
10 *
11 * This file is part of the SANE package.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation, version 2.
16 */
17
18 #define DEBUG_DECLARE_ONLY
19
20 #include "sane/config.h"
21
22 #include <ctype.h>
23
24 #include "epson2.h"
25 #include "epson2-io.h"
26
27 #include "sane/sanei_scsi.h"
28 #include "sane/sanei_usb.h"
29 #include "sane/sanei_pio.h"
30 #include "sane/sanei_tcp.h"
31
32 #include "epson2_scsi.h"
33 #include "epson_usb.h"
34 #include "epson2_net.h"
35
36 #include "byteorder.h"
37
38 /* flaming hack to get USB scanners
39 * working without timeouts under linux
40 * (cribbed from fujitsu.c)
41 */
42 unsigned int r_cmd_count = 0;
43 unsigned int w_cmd_count = 0;
44
45 int
e2_send(Epson_Scanner * s, void *buf, size_t buf_size, size_t reply_len, SANE_Status * status)46 e2_send(Epson_Scanner * s, void *buf, size_t buf_size, size_t reply_len,
47 SANE_Status * status)
48 {
49 DBG(15, "%s: size = %lu, reply = %lu\n",
50 __func__, (u_long) buf_size, (u_long) reply_len);
51
52 if (buf_size == 2) {
53 char *cmd = buf;
54
55 switch (cmd[0]) {
56 case ESC:
57 DBG(9, "%s: ESC %c\n", __func__, cmd[1]);
58 break;
59
60 case FS:
61 DBG(9, "%s: FS %c\n", __func__, cmd[1]);
62 break;
63 }
64 }
65
66 if (DBG_LEVEL >= 125) {
67 unsigned int k;
68 const unsigned char *s = buf;
69
70 for (k = 0; k < buf_size; k++) {
71 DBG(125, "buf[%d] %02x %c\n", k, s[k],
72 isprint(s[k]) ? s[k] : '.');
73 }
74 }
75
76 if (s->hw->connection == SANE_EPSON_NET) {
77 if (reply_len == 0) {
78 DBG(0,
79 "Cannot send this command to a networked scanner\n");
80 *status = SANE_STATUS_INVAL;
81 return 0; /* nothing actually sent */
82 }
83 return sanei_epson_net_write(s, 0x2000, buf, buf_size,
84 reply_len, status);
85 } else if (s->hw->connection == SANE_EPSON_SCSI) {
86 return sanei_epson2_scsi_write(s->fd, buf, buf_size, status);
87 } else if (s->hw->connection == SANE_EPSON_PIO) {
88 size_t n;
89
90 if (buf_size == (n = sanei_pio_write(s->fd, buf, buf_size)))
91 *status = SANE_STATUS_GOOD;
92 else
93 *status = SANE_STATUS_INVAL;
94
95 return n;
96
97 } else if (s->hw->connection == SANE_EPSON_USB) {
98 size_t n;
99 n = buf_size;
100 *status = sanei_usb_write_bulk(s->fd, buf, &n);
101 w_cmd_count++;
102 DBG(20, "%s: cmd count, r = %d, w = %d\n",
103 __func__, r_cmd_count, w_cmd_count);
104
105 return n;
106 }
107
108 *status = SANE_STATUS_INVAL;
109 return 0;
110 /* never reached */
111 }
112
113 ssize_t
e2_recv(Epson_Scanner *s, void *buf, ssize_t buf_size, SANE_Status *status)114 e2_recv(Epson_Scanner *s, void *buf, ssize_t buf_size,
115 SANE_Status *status)
116 {
117 ssize_t n = buf_size; /* network interface needs to read header back even data is 0.*/
118
119 DBG(15, "%s: size = %ld, buf = %p\n", __func__, (long) buf_size, buf);
120
121 *status = SANE_STATUS_GOOD;
122 if (s->hw->connection == SANE_EPSON_NET) {
123 n = sanei_epson_net_read(s, buf, buf_size, status);
124 } else if (s->hw->connection == SANE_EPSON_SCSI) {
125 if (buf_size)
126 n = sanei_epson2_scsi_read(s->fd, buf, buf_size, status);
127 } else if (s->hw->connection == SANE_EPSON_PIO) {
128 if (buf_size) {
129 if (buf_size ==
130 (n = sanei_pio_read(s->fd, buf, (size_t) buf_size)))
131 *status = SANE_STATUS_GOOD;
132 else
133 *status = SANE_STATUS_INVAL;
134 }
135 } else if (s->hw->connection == SANE_EPSON_USB) {
136 /* !!! only report an error if we don't read anything */
137 if (n) {
138 *status =
139 sanei_usb_read_bulk(s->fd, (SANE_Byte *) buf,
140 (size_t *) & n);
141 r_cmd_count += (n + 63) / 64; /* add # of packets, rounding up */
142 DBG(20, "%s: cmd count, r = %d, w = %d\n",
143 __func__, r_cmd_count, w_cmd_count);
144
145 if (n > 0)
146 *status = SANE_STATUS_GOOD;
147 }
148 }
149
150 if (n < buf_size) {
151 DBG(1, "%s: expected = %lu, got = %ld, canceling: %d\n", __func__,
152 (u_long) buf_size, (long) n, s->canceling);
153
154 *status = SANE_STATUS_IO_ERROR;
155 }
156
157 /* dump buffer if appropriate */
158 if (DBG_LEVEL >= 127 && n > 0) {
159 int k;
160 const unsigned char *s = buf;
161
162 for (k = 0; k < n; k++)
163 DBG(127, "buf[%d] %02x %c\n", k, s[k],
164 isprint(s[k]) ? s[k] : '.');
165 }
166
167 return n;
168 }
169
170 /* Simple function to exchange a fixed amount of
171 * data with the scanner
172 */
173
174 SANE_Status
e2_txrx(Epson_Scanner * s, unsigned char *txbuf, size_t txlen, unsigned char *rxbuf, size_t rxlen)175 e2_txrx(Epson_Scanner * s, unsigned char *txbuf, size_t txlen,
176 unsigned char *rxbuf, size_t rxlen)
177 {
178 SANE_Status status;
179 size_t done;
180
181 done = e2_send(s, txbuf, txlen, rxlen, &status);
182 if (status != SANE_STATUS_GOOD) {
183 DBG(1, "%s: tx err, %s\n", __func__, sane_strstatus(status));
184 return status;
185 }
186 if (done != txlen) {
187 DBG(1, "%s: tx err, short write\n", __func__);
188 return SANE_STATUS_IO_ERROR;
189 }
190
191 e2_recv(s, rxbuf, rxlen, &status);
192 if (status != SANE_STATUS_GOOD) {
193 DBG(1, "%s: rx err, %s\n", __func__, sane_strstatus(status));
194 }
195 DBG(1, "%s: eds_recv status, %s\n", __func__, sane_strstatus(status));
196 return status;
197 }
198
199 /* This function should be used to send codes that only requires the scanner
200 * to give back an ACK or a NAK.
201 */
202 SANE_Status
e2_cmd_simple(Epson_Scanner * s, void *buf, size_t buf_size)203 e2_cmd_simple(Epson_Scanner * s, void *buf, size_t buf_size)
204 {
205 unsigned char result;
206 SANE_Status status;
207
208 DBG(12, "%s: size = %lu\n", __func__, (u_long) buf_size);
209
210 status = e2_txrx(s, buf, buf_size, &result, 1);
211 if (status != SANE_STATUS_GOOD) {
212 DBG(1, "%s: failed, %s\n", __func__, sane_strstatus(status));
213 return status;
214 }
215
216 if (result == ACK)
217 return SANE_STATUS_GOOD;
218
219 if (result == NAK) {
220 DBG(3, "%s: NAK\n", __func__);
221 return SANE_STATUS_INVAL;
222 }
223
224 DBG(1, "%s: result is neither ACK nor NAK but 0x%02x\n", __func__,
225 result);
226
227 return SANE_STATUS_GOOD;
228 }
229
230 /* receives a 4 or 6 bytes information block from the scanner*/
231 SANE_Status
e2_recv_info_block(Epson_Scanner * s, unsigned char *scanner_status, size_t info_size, size_t * payload_size)232 e2_recv_info_block(Epson_Scanner * s, unsigned char *scanner_status,
233 size_t info_size, size_t * payload_size)
234 {
235 SANE_Status status;
236 unsigned char info[6];
237
238 if (s->hw->connection == SANE_EPSON_PIO)
239 e2_recv(s, info, 1, &status);
240 else
241 e2_recv(s, info, info_size, &status);
242
243 if (status != SANE_STATUS_GOOD)
244 return status;
245
246 /* check for explicit NAK */
247 if (info[0] == NAK) {
248 DBG(1, "%s: command not supported\n", __func__);
249 return SANE_STATUS_UNSUPPORTED;
250 }
251
252 /* check the first byte: if it's not STX, bail out */
253 if (info[0] != STX) {
254 DBG(1, "%s: expecting STX, got %02X\n", __func__, info[0]);
255 return SANE_STATUS_INVAL;
256 }
257
258 /* if connection is PIO read the remaining bytes. */
259 if (s->hw->connection == SANE_EPSON_PIO) {
260 e2_recv(s, &info[1], info_size - 1, &status);
261 if (status != SANE_STATUS_GOOD)
262 return status;
263 }
264
265 if (scanner_status)
266 *scanner_status = info[1];
267
268 if (payload_size) {
269 *payload_size = le16atoh(&info[2]);
270
271 if (info_size == 6)
272 *payload_size *= le16atoh(&info[4]);
273
274 DBG(14, "%s: payload length: %lu\n", __func__,
275 (u_long) *payload_size);
276 }
277
278 return SANE_STATUS_GOOD;
279 }
280
281 /* This function can be called for commands that
282 * will be answered by the scanner with an info block of 4 bytes
283 * and a variable payload. The payload is passed back to the caller
284 * in **buf. The caller must free it if != NULL,
285 * even if the status != SANE_STATUS_GOOD.
286 */
287
288 SANE_Status
e2_cmd_info_block(SANE_Handle handle, unsigned char *params, unsigned char params_len, size_t reply_len, unsigned char **buf, size_t * buf_len)289 e2_cmd_info_block(SANE_Handle handle, unsigned char *params,
290 unsigned char params_len, size_t reply_len,
291 unsigned char **buf, size_t * buf_len)
292 {
293 SANE_Status status;
294 Epson_Scanner *s = (Epson_Scanner *) handle;
295 size_t len;
296
297 DBG(13, "%s, params len = %d, reply len = %lu, buf = %p\n",
298 __func__, params_len, (u_long) reply_len, (void *) buf);
299
300 if (buf == NULL)
301 return SANE_STATUS_INVAL;
302
303 /* initialize */
304 *buf = NULL;
305
306 /* send command, we expect the info block + reply_len back */
307 e2_send(s, params, params_len,
308 reply_len ? reply_len + 4 : 0, &status);
309
310 if (status != SANE_STATUS_GOOD)
311 goto end;
312
313 status = e2_recv_info_block(s, NULL, 4, &len);
314 if (status != SANE_STATUS_GOOD)
315 goto end;
316
317 /* do we need to provide the length of the payload? */
318 if (buf_len)
319 *buf_len = len;
320
321 /* no payload, stop here */
322 if (len == 0)
323 goto end;
324
325 /* if a reply_len has been specified and the actual
326 * length differs, throw a warning
327 */
328 if (reply_len && (len != reply_len)) {
329 DBG(1, "%s: mismatched len - expected %lu, got %lu\n",
330 __func__, (u_long) reply_len, (u_long) len);
331 }
332
333 /* allocate and receive the payload */
334 *buf = malloc(len);
335
336 if (*buf) {
337 memset(*buf, 0x00, len);
338 e2_recv(s, *buf, len, &status); /* receive actual data */
339 } else
340 status = SANE_STATUS_NO_MEM;
341 end:
342
343 if (status != SANE_STATUS_GOOD) {
344 DBG(1, "%s: failed, %s\n", __func__, sane_strstatus(status));
345
346 if (*buf) {
347 free(*buf);
348 *buf = NULL;
349 }
350 }
351
352 return status;
353 }
354
355
356 /* This is used for ESC commands with a single byte parameter. Scanner
357 * will answer with ACK/NAK.
358 */
359 SANE_Status
e2_esc_cmd(Epson_Scanner * s, unsigned char cmd, unsigned char val)360 e2_esc_cmd(Epson_Scanner * s, unsigned char cmd, unsigned char val)
361 {
362 SANE_Status status;
363 unsigned char params[2];
364
365 DBG(8, "%s: cmd = 0x%02x, val = %d\n", __func__, cmd, val);
366 if (!cmd)
367 return SANE_STATUS_UNSUPPORTED;
368
369 params[0] = ESC;
370 params[1] = cmd;
371
372 status = e2_cmd_simple(s, params, 2);
373 if (status != SANE_STATUS_GOOD)
374 return status;
375
376 params[0] = val;
377
378 return e2_cmd_simple(s, params, 1);
379 }
380
381 /* Send an ACK to the scanner */
382
383 SANE_Status
e2_ack(Epson_Scanner * s)384 e2_ack(Epson_Scanner * s)
385 {
386 SANE_Status status;
387 e2_send(s, S_ACK, 1, 0, &status);
388 return status;
389 }
390
391 SANE_Status
e2_ack_next(Epson_Scanner * s, size_t reply_len)392 e2_ack_next(Epson_Scanner * s, size_t reply_len)
393 {
394 SANE_Status status;
395 e2_send(s, S_ACK, 1, reply_len, &status);
396 return status;
397 }
398
399 SANE_Status
e2_cancel(Epson_Scanner * s)400 e2_cancel(Epson_Scanner * s)
401 {
402 DBG(1, "%s\n", __func__);
403 return e2_cmd_simple(s, S_CAN, 1);
404 }
405