2006-12-02 14:47:21 +00:00
|
|
|
/*
|
|
|
|
* epson2_net.c - SANE library for Epson scanners.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tower Technologies
|
|
|
|
* Author: Alessandro Zummo <a.zummo@towertech.it>
|
|
|
|
*
|
|
|
|
* 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, version 2.
|
|
|
|
*/
|
|
|
|
|
2009-06-01 14:18:52 +00:00
|
|
|
#define DEBUG_DECLARE_ONLY
|
2006-12-02 14:47:21 +00:00
|
|
|
|
2009-06-01 14:18:52 +00:00
|
|
|
#include "sane/config.h"
|
2009-01-23 03:50:22 +00:00
|
|
|
|
2009-06-01 14:18:52 +00:00
|
|
|
#include "sane/sane.h"
|
|
|
|
#include "sane/saneopts.h"
|
|
|
|
#include "sane/sanei_tcp.h"
|
|
|
|
#include "sane/sanei_config.h"
|
|
|
|
#include "sane/sanei_backend.h"
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
#include "epson2.h"
|
|
|
|
#include "epson2_net.h"
|
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
#include "byteorder.h"
|
|
|
|
|
2009-06-01 14:18:52 +00:00
|
|
|
#include "sane/sanei_debug.h"
|
2006-12-02 14:47:21 +00:00
|
|
|
|
2014-03-13 21:57:17 +00:00
|
|
|
static int
|
|
|
|
sanei_epson_net_read_raw(Epson_Scanner *s, unsigned char *buf, ssize_t wanted,
|
|
|
|
SANE_Status *status)
|
2006-12-18 00:13:09 +00:00
|
|
|
{
|
2014-03-13 21:57:17 +00:00
|
|
|
int ready, read = -1;
|
|
|
|
fd_set readable;
|
|
|
|
struct timeval tv;
|
2006-12-18 00:13:09 +00:00
|
|
|
|
2014-03-13 21:57:17 +00:00
|
|
|
tv.tv_sec = 10;
|
|
|
|
tv.tv_usec = 0;
|
2006-12-18 00:13:09 +00:00
|
|
|
|
2014-03-13 21:57:17 +00:00
|
|
|
FD_ZERO(&readable);
|
|
|
|
FD_SET(s->fd, &readable);
|
2006-12-18 00:13:09 +00:00
|
|
|
|
2014-03-13 21:57:17 +00:00
|
|
|
ready = select(s->fd + 1, &readable, NULL, NULL, &tv);
|
|
|
|
if (ready > 0) {
|
|
|
|
read = sanei_tcp_read(s->fd, buf, wanted);
|
|
|
|
} else {
|
|
|
|
DBG(15, "%s: select failed: %d\n", __func__, ready);
|
2006-12-18 00:13:09 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 21:57:17 +00:00
|
|
|
*status = SANE_STATUS_GOOD;
|
|
|
|
|
|
|
|
if (read < wanted) {
|
2006-12-18 00:13:09 +00:00
|
|
|
*status = SANE_STATUS_IO_ERROR;
|
2014-03-13 21:57:17 +00:00
|
|
|
}
|
2006-12-18 00:13:09 +00:00
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
int
|
2010-06-23 16:21:35 +00:00
|
|
|
sanei_epson_net_read(Epson_Scanner *s, unsigned char *buf, ssize_t wanted,
|
2006-12-02 14:47:21 +00:00
|
|
|
SANE_Status * status)
|
|
|
|
{
|
2010-06-23 16:21:35 +00:00
|
|
|
ssize_t size;
|
2009-06-16 14:24:43 +00:00
|
|
|
ssize_t read = 0;
|
2006-12-02 14:47:21 +00:00
|
|
|
unsigned char header[12];
|
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
/* read from buffer, if available */
|
|
|
|
if (s->netptr != s->netbuf) {
|
2009-06-03 16:52:59 +00:00
|
|
|
DBG(23, "reading %lu from buffer at %p, %lu available\n",
|
2008-02-03 10:34:20 +00:00
|
|
|
(u_long) wanted, s->netptr, (u_long) s->netlen);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
memcpy(buf, s->netptr, wanted);
|
|
|
|
read = wanted;
|
|
|
|
|
|
|
|
s->netlen -= wanted;
|
|
|
|
|
|
|
|
if (s->netlen == 0) {
|
2009-06-03 16:52:59 +00:00
|
|
|
DBG(23, "%s: freeing %p\n", __func__, s->netbuf);
|
2006-12-18 00:13:09 +00:00
|
|
|
free(s->netbuf);
|
|
|
|
s->netbuf = s->netptr = NULL;
|
|
|
|
s->netlen = 0;
|
|
|
|
}
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
/* receive net header */
|
2014-03-25 13:51:14 +00:00
|
|
|
size = sanei_epson_net_read_raw(s, header, 12, status);
|
2009-06-16 14:24:43 +00:00
|
|
|
if (size != 12) {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
if (header[0] != 'I' || header[1] != 'S') {
|
|
|
|
DBG(1, "header mismatch: %02X %02x\n", header[0], header[1]);
|
2006-12-18 00:13:09 +00:00
|
|
|
*status = SANE_STATUS_IO_ERROR;
|
|
|
|
return 0;
|
2006-12-02 14:47:21 +00:00
|
|
|
}
|
2009-06-01 14:18:52 +00:00
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
size = be32atoh(&header[6]);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
2009-06-03 16:52:59 +00:00
|
|
|
DBG(23, "%s: wanted = %lu, available = %lu\n", __FUNCTION__,
|
2008-02-03 10:34:20 +00:00
|
|
|
(u_long) wanted, (u_long) size);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
*status = SANE_STATUS_GOOD;
|
|
|
|
|
|
|
|
if (size == wanted) {
|
2009-06-01 14:18:52 +00:00
|
|
|
|
|
|
|
DBG(15, "%s: full read\n", __func__);
|
2014-03-13 21:57:17 +00:00
|
|
|
|
|
|
|
read = sanei_epson_net_read_raw(s, buf, size, status);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
if (s->netbuf) {
|
2006-12-02 14:47:21 +00:00
|
|
|
free(s->netbuf);
|
2006-12-18 00:13:09 +00:00
|
|
|
s->netbuf = NULL;
|
|
|
|
s->netlen = 0;
|
|
|
|
}
|
2009-06-16 14:24:43 +00:00
|
|
|
|
|
|
|
if (read < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-01 14:18:52 +00:00
|
|
|
|
2009-06-17 15:36:49 +00:00
|
|
|
/* } else if (wanted < size && s->netlen == size) { */
|
|
|
|
} else {
|
2009-06-03 16:52:59 +00:00
|
|
|
DBG(23, "%s: partial read\n", __func__);
|
2006-12-18 00:13:09 +00:00
|
|
|
|
2014-03-25 13:51:14 +00:00
|
|
|
read = sanei_epson_net_read_raw(s, s->netbuf, size, status);
|
2009-06-17 15:36:49 +00:00
|
|
|
if (read != size) {
|
2009-06-16 14:24:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
s->netlen = size - wanted;
|
|
|
|
s->netptr += wanted;
|
|
|
|
read = wanted;
|
|
|
|
|
2009-06-03 16:52:59 +00:00
|
|
|
DBG(23, "0,4 %02x %02x\n", s->netbuf[0], s->netbuf[4]);
|
|
|
|
DBG(23, "storing %lu to buffer at %p, next read at %p, %lu bytes left\n",
|
2008-02-03 10:34:20 +00:00
|
|
|
(u_long) size, s->netbuf, s->netptr, (u_long) s->netlen);
|
2006-12-18 00:13:09 +00:00
|
|
|
|
|
|
|
memcpy(buf, s->netbuf, wanted);
|
2006-12-02 14:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
|
2006-12-02 14:47:21 +00:00
|
|
|
int
|
|
|
|
sanei_epson_net_write(Epson_Scanner *s, unsigned int cmd, const unsigned char *buf,
|
|
|
|
size_t buf_size, size_t reply_len, SANE_Status *status)
|
|
|
|
{
|
|
|
|
unsigned char *h1, *h2, *payload;
|
|
|
|
unsigned char *packet = malloc(12 + 8 + buf_size);
|
|
|
|
|
|
|
|
/* XXX check allocation failure */
|
|
|
|
|
|
|
|
h1 = packet;
|
|
|
|
h2 = packet + 12;
|
|
|
|
payload = packet + 12 + 8;
|
|
|
|
|
|
|
|
if (reply_len) {
|
|
|
|
s->netbuf = s->netptr = malloc(reply_len);
|
|
|
|
s->netlen = reply_len;
|
2009-06-01 14:18:52 +00:00
|
|
|
DBG(24, "allocated %lu bytes at %p\n",
|
2008-02-03 10:34:20 +00:00
|
|
|
(u_long) reply_len, s->netbuf);
|
2006-12-02 14:47:21 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 14:18:52 +00:00
|
|
|
DBG(24, "%s: cmd = %04x, buf = %p, buf_size = %lu, reply_len = %lu\n",
|
2008-02-03 10:34:20 +00:00
|
|
|
__FUNCTION__, cmd, buf, (u_long) buf_size, (u_long) reply_len);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
memset(h1, 0x00, 12);
|
|
|
|
memset(h2, 0x00, 8);
|
|
|
|
|
|
|
|
h1[0] = 'I';
|
|
|
|
h1[1] = 'S';
|
|
|
|
|
|
|
|
h1[2] = cmd >> 8;
|
|
|
|
h1[3] = cmd;
|
|
|
|
|
|
|
|
h1[4] = 0x00;
|
|
|
|
h1[5] = 0x0C; /* Don't know what's that */
|
|
|
|
|
2009-06-01 14:18:52 +00:00
|
|
|
DBG(24, "H1[0]: %02x %02x %02x %02x\n", h1[0], h1[1], h1[2], h1[3]);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
if((cmd >> 8) == 0x20) {
|
2006-12-18 00:13:09 +00:00
|
|
|
htobe32a(&h1[6], buf_size + 8);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
htobe32a(&h2[0], buf_size);
|
|
|
|
htobe32a(&h2[4], reply_len);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
2009-06-01 14:18:52 +00:00
|
|
|
DBG(24, "H1[6]: %02x %02x %02x %02x (%lu)\n", h1[6], h1[7], h1[8], h1[9], (u_long) (buf_size + 8));
|
|
|
|
DBG(24, "H2[0]: %02x %02x %02x %02x (%lu)\n", h2[0], h2[1], h2[2], h2[3], (u_long) buf_size);
|
|
|
|
DBG(24, "H2[4]: %02x %02x %02x %02x (%lu)\n", h2[4], h2[5], h2[6], h2[7], (u_long) reply_len);
|
2006-12-02 14:47:21 +00:00
|
|
|
}
|
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
if ((cmd >> 8) == 0x20 && (buf_size || reply_len)) {
|
|
|
|
if (buf_size)
|
|
|
|
memcpy(payload, buf, buf_size);
|
|
|
|
|
2006-12-02 14:47:21 +00:00
|
|
|
sanei_tcp_write(s->fd, packet, 12 + 8 + buf_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sanei_tcp_write(s->fd, packet, 12);
|
|
|
|
|
|
|
|
free(packet);
|
|
|
|
|
|
|
|
*status = SANE_STATUS_GOOD;
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
SANE_Status
|
|
|
|
sanei_epson_net_lock(struct Epson_Scanner *s)
|
|
|
|
{
|
|
|
|
SANE_Status status;
|
|
|
|
unsigned char buf[1];
|
|
|
|
|
2006-12-18 00:13:09 +00:00
|
|
|
DBG(1, "%s\n", __func__);
|
|
|
|
|
2006-12-02 14:47:21 +00:00
|
|
|
sanei_epson_net_write(s, 0x2100, NULL, 0, 0, &status);
|
|
|
|
sanei_epson_net_read(s, buf, 1, &status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
SANE_Status
|
|
|
|
sanei_epson_net_unlock(struct Epson_Scanner *s)
|
|
|
|
{
|
|
|
|
SANE_Status status;
|
2006-12-18 00:13:09 +00:00
|
|
|
|
|
|
|
DBG(1, "%s\n", __func__);
|
2006-12-02 14:47:21 +00:00
|
|
|
|
|
|
|
sanei_epson_net_write(s, 0x2101, NULL, 0, 0, &status);
|
2006-12-18 00:13:09 +00:00
|
|
|
/* sanei_epson_net_read(s, buf, 1, &status); */
|
2006-12-02 14:47:21 +00:00
|
|
|
return status;
|
|
|
|
}
|