2013-07-14 15:55:35 -04:00
|
|
|
/*
|
2013-07-18 23:00:58 -04:00
|
|
|
* Sony UP-DR150 Photo Printer CUPS backend -- libusb-1.0 version
|
2013-07-14 15:55:35 -04:00
|
|
|
*
|
2014-01-23 16:07:25 -05:00
|
|
|
* (c) 2013-2014 Solomon Peachy <pizza@shaftnet.org>
|
2013-07-14 15:55:35 -04:00
|
|
|
*
|
|
|
|
* The latest version of this program can be found at:
|
2014-01-13 05:41:48 -05:00
|
|
|
*
|
2013-08-20 20:10:21 -04:00
|
|
|
* http://git.shaftnet.org/cgit/selphy_print.git
|
2014-01-13 05:41:48 -05:00
|
|
|
*
|
2013-07-14 15:55:35 -04:00
|
|
|
* 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; either version 3 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* [http://www.gnu.org/licenses/gpl-3.0.html]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
#include "backend_common.h"
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
/* Private data stucture */
|
|
|
|
struct updr150_ctx {
|
2013-07-14 15:55:35 -04:00
|
|
|
struct libusb_device_handle *dev;
|
2013-07-18 08:46:44 -04:00
|
|
|
uint8_t endp_up;
|
|
|
|
uint8_t endp_down;
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
uint8_t *databuf;
|
|
|
|
int datalen;
|
|
|
|
};
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-19 09:23:53 -04:00
|
|
|
static void* updr150_init(void)
|
2013-07-18 08:46:44 -04:00
|
|
|
{
|
|
|
|
struct updr150_ctx *ctx = malloc(sizeof(struct updr150_ctx));
|
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
|
|
|
memset(ctx, 0, sizeof(struct updr150_ctx));
|
2013-07-19 09:23:53 -04:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void updr150_attach(void *vctx, struct libusb_device_handle *dev,
|
|
|
|
uint8_t endp_up, uint8_t endp_down, uint8_t jobid)
|
|
|
|
{
|
|
|
|
struct updr150_ctx *ctx = vctx;
|
|
|
|
|
2013-11-23 19:51:55 -05:00
|
|
|
UNUSED(jobid);
|
|
|
|
|
2013-07-19 09:23:53 -04:00
|
|
|
ctx->dev = dev;
|
2013-07-18 08:46:44 -04:00
|
|
|
ctx->endp_up = endp_up;
|
|
|
|
ctx->endp_down = endp_down;
|
|
|
|
}
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
static void updr150_teardown(void *vctx) {
|
|
|
|
struct updr150_ctx *ctx = vctx;
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
if (!ctx)
|
|
|
|
return;
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
if (ctx->databuf)
|
|
|
|
free(ctx->databuf);
|
|
|
|
free(ctx);
|
|
|
|
}
|
2013-07-17 19:47:47 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
#define MAX_PRINTJOB_LEN 16736455
|
|
|
|
static int updr150_read_parse(void *vctx, int data_fd) {
|
|
|
|
struct updr150_ctx *ctx = vctx;
|
2014-01-19 19:53:45 -05:00
|
|
|
int i, len, run = 1;
|
|
|
|
uint32_t *ptr;
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
if (!ctx)
|
|
|
|
return 1;
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2014-01-21 20:34:00 -05:00
|
|
|
if (ctx->databuf) {
|
2014-01-19 19:53:45 -05:00
|
|
|
free(ctx->databuf);
|
2014-01-21 20:34:00 -05:00
|
|
|
ctx->databuf = NULL;
|
|
|
|
}
|
2014-01-19 19:53:45 -05:00
|
|
|
|
|
|
|
ctx->datalen = 0;
|
2013-07-18 08:46:44 -04:00
|
|
|
ctx->databuf = malloc(MAX_PRINTJOB_LEN);
|
|
|
|
if (!ctx->databuf) {
|
2013-07-14 15:55:35 -04:00
|
|
|
ERROR("Memory allocation failure!\n");
|
2013-07-18 08:46:44 -04:00
|
|
|
return 2;
|
2013-07-14 15:55:35 -04:00
|
|
|
}
|
|
|
|
|
2014-01-19 19:53:45 -05:00
|
|
|
while(run) {
|
|
|
|
int keep = 0;
|
|
|
|
i = read(data_fd, ctx->databuf + ctx->datalen, 4);
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
2014-01-20 19:41:52 -05:00
|
|
|
if (i == 0)
|
|
|
|
break;
|
2014-01-19 19:53:45 -05:00
|
|
|
|
|
|
|
ptr = (uint32_t *) ctx->databuf + ctx->datalen;
|
|
|
|
len = le32_to_cpu(*ptr);
|
|
|
|
|
|
|
|
/* Filter out chunks we don't send to the printer */
|
|
|
|
switch (len) {
|
|
|
|
case 0xffffff6a:
|
|
|
|
case 0xfffffffc:
|
|
|
|
case 0xfffffffb:
|
|
|
|
case 0xfffffff4:
|
|
|
|
case 0xffffffed:
|
|
|
|
case 0xfffffff9:
|
|
|
|
case 0xfffffff8:
|
|
|
|
case 0xffffffec:
|
|
|
|
case 0xffffffeb:
|
|
|
|
case 0xfffffffa:
|
|
|
|
case 0xfffffff3:
|
|
|
|
len = 0;
|
|
|
|
if(dyesub_debug)
|
|
|
|
DEBUG("Block ID '%x' (len %d)\n", *ptr, len);
|
|
|
|
break;
|
|
|
|
case 0xffffffef:
|
|
|
|
case 0xfffffff5:
|
|
|
|
len = 4;
|
|
|
|
if(dyesub_debug)
|
|
|
|
DEBUG("Block ID '%x' (len %d)\n", *ptr, len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (len & 0xff000000) {
|
|
|
|
ERROR("Unknown block ID '%x', aborting!\n", *ptr);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* Only keep these chunks */
|
|
|
|
if(dyesub_debug)
|
|
|
|
DEBUG("Data chunk (len %d)\n", len);
|
|
|
|
keep = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (keep)
|
|
|
|
ctx->datalen += sizeof(uint32_t);
|
|
|
|
|
|
|
|
/* Last block is the plane data, and is HUGE.. */
|
|
|
|
if (len > 4096)
|
|
|
|
run = 0;
|
|
|
|
|
|
|
|
/* Read in the data chunk */
|
|
|
|
while(len > 0) {
|
|
|
|
i = read(data_fd, ctx->databuf + ctx->datalen, len);
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (keep)
|
|
|
|
ctx->datalen += i;
|
|
|
|
len -= i;
|
|
|
|
}
|
2013-07-14 15:55:35 -04:00
|
|
|
}
|
2014-01-20 19:41:52 -05:00
|
|
|
if (!ctx->datalen)
|
|
|
|
return 1;
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
static int updr150_main_loop(void *vctx, int copies) {
|
|
|
|
struct updr150_ctx *ctx = vctx;
|
2014-01-19 19:53:45 -05:00
|
|
|
int i = 0, ret;
|
2013-07-14 15:55:35 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
if (!ctx)
|
|
|
|
return 1;
|
2013-07-14 15:55:35 -04:00
|
|
|
|
|
|
|
top:
|
2014-01-19 19:53:45 -05:00
|
|
|
while (i < ctx->datalen) {
|
|
|
|
uint32_t *ptr = (uint32_t *) ctx->databuf + i;
|
|
|
|
uint32_t len = le32_to_cpu(*ptr);
|
|
|
|
|
|
|
|
i += sizeof(uint32_t);
|
|
|
|
|
|
|
|
if (dyesub_debug)
|
|
|
|
DEBUG("Sending %d bytes to printer\n", len);
|
|
|
|
if ((ret = send_data(ctx->dev, ctx->endp_down,
|
|
|
|
ctx->databuf + i, len)))
|
|
|
|
return ret;
|
|
|
|
i += len;
|
2013-07-14 15:55:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up */
|
|
|
|
if (terminate)
|
|
|
|
copies = 1;
|
|
|
|
|
2014-01-22 09:10:34 -05:00
|
|
|
INFO("Print complete (%d copies remaining)\n", copies - 1);
|
2013-07-14 15:55:35 -04:00
|
|
|
|
|
|
|
if (copies && --copies) {
|
|
|
|
goto top;
|
|
|
|
}
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
return 0;
|
2013-07-14 15:55:35 -04:00
|
|
|
}
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
/* Exported */
|
2013-07-18 23:39:36 -04:00
|
|
|
#define USB_VID_SONY 0x054C
|
|
|
|
#define USB_PID_SONY_UPDR150 0x01E8
|
2014-01-22 08:45:41 -05:00
|
|
|
#define USB_PID_SONY_UPDR200 0x035F
|
2013-07-18 23:39:36 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
struct dyesub_backend updr150_backend = {
|
|
|
|
.name = "Sony UP-DR150",
|
2014-01-22 08:45:41 -05:00
|
|
|
.version = "0.12",
|
2013-07-18 12:45:35 -04:00
|
|
|
.uri_prefix = "sonyupdr150",
|
2014-01-19 19:53:45 -05:00
|
|
|
.multipage_capable = 1,
|
2013-07-18 08:46:44 -04:00
|
|
|
.init = updr150_init,
|
2013-07-19 09:23:53 -04:00
|
|
|
.attach = updr150_attach,
|
2013-07-18 08:46:44 -04:00
|
|
|
.teardown = updr150_teardown,
|
|
|
|
.read_parse = updr150_read_parse,
|
|
|
|
.main_loop = updr150_main_loop,
|
2013-07-18 23:39:36 -04:00
|
|
|
.devices = {
|
|
|
|
{ USB_VID_SONY, USB_PID_SONY_UPDR150, P_SONY_UPDR150, ""},
|
2014-01-22 08:45:41 -05:00
|
|
|
{ USB_VID_SONY, USB_PID_SONY_UPDR200, P_SONY_UPDR150, ""},
|
2013-07-18 23:39:36 -04:00
|
|
|
{ 0, 0, 0, ""}
|
|
|
|
}
|
2013-07-18 08:46:44 -04:00
|
|
|
};
|
|
|
|
|
2013-10-16 21:46:53 -04:00
|
|
|
/* Sony UP-DR150/UP-DR200 Spool file format
|
2013-07-14 15:55:35 -04:00
|
|
|
|
|
|
|
The spool file is a series of 4-byte commands, followed by optional
|
|
|
|
arguments. The purpose of the commands is unknown, but they presumably
|
|
|
|
instruct the driver to perform certain things.
|
|
|
|
|
2014-01-17 23:21:23 -05:00
|
|
|
If you treat these 4 bytes as a 32-bit little-endian number, if the most significant
|
|
|
|
four bits are bits are non-zero, the value is is to be interpreted as a driver
|
|
|
|
command. If the most significant bits are zero, the value signifies that the following
|
|
|
|
N bytes of data should be sent to the printer as-is.
|
|
|
|
|
|
|
|
Known driver "commands":
|
2013-07-14 15:55:35 -04:00
|
|
|
|
|
|
|
6a ff ff ff
|
|
|
|
fc ff ff ff
|
|
|
|
fb ff ff ff
|
|
|
|
f4 ff ff ff
|
|
|
|
ed ff ff ff
|
|
|
|
f9 ff ff ff
|
|
|
|
f8 ff ff ff
|
|
|
|
ec ff ff ff
|
|
|
|
eb ff ff ff
|
|
|
|
fa ff ff ff
|
|
|
|
f3 ff ff ff
|
|
|
|
ef ff ff ff XX 00 00 00 # XX == print size (0x01/0x02/0x03/0x04)
|
|
|
|
f5 ff ff ff YY 00 00 00 # YY == ??? (seen 0x01)
|
|
|
|
|
|
|
|
All printer commands start with 0x1b, and are at least 7 bytes long.
|
|
|
|
|
|
|
|
************************************************************************
|
|
|
|
|
|
|
|
The data stream sent to the printer consists of all the commands in the
|
|
|
|
spool file, plus a couple other ones that generate responses. It is
|
|
|
|
unknown if those additional commands are necessary. This is a typical
|
|
|
|
sequence:
|
|
|
|
|
2013-10-16 21:46:53 -04:00
|
|
|
[[ Sniff start of a UP-DR150 ]]
|
2013-07-14 15:55:35 -04:00
|
|
|
|
|
|
|
<- 1b e0 00 00 00 0f 00
|
|
|
|
-> 0e 00 00 00 00 00 00 00 00 04 a8 08 0a a4 00
|
|
|
|
|
|
|
|
<- 1b 16 00 00 00 00 00
|
|
|
|
-> "reset" ??
|
|
|
|
|
|
|
|
[[ begin job ]]
|
|
|
|
|
|
|
|
<- 1b ef 00 00 00 06 00
|
|
|
|
-> 05 00 00 00 00 22
|
|
|
|
|
|
|
|
<- 1b e5 00 00 00 08 00 ** In spool file
|
|
|
|
<- 00 00 00 00 00 00 01 00
|
|
|
|
|
|
|
|
<- 1b c1 00 02 06 00 00
|
|
|
|
-> 02 02 00 03 00 00
|
|
|
|
|
|
|
|
<- 1b ee 00 00 00 02 00 ** In spool file
|
|
|
|
<- 00 01
|
|
|
|
|
|
|
|
<- 1b 15 00 00 00 0d 00 ** In spool file
|
|
|
|
<- 00 00 00 00 07 00 00 00 00 08 00 0a a4
|
|
|
|
|
|
|
|
<- 1b 03 00 00 00 13 00
|
|
|
|
-> 70 00 00 00 00 00 00 0b 00 00 00 00 00 00 00 00
|
|
|
|
00 00 00
|
|
|
|
|
|
|
|
<- 1b e1 00 00 00 0b 00 ** In spool file
|
|
|
|
<- 00 80 00 00 00 00 00 08 00 0a a4
|
|
|
|
|
|
|
|
<- 1b 03 00 00 00 13 00
|
|
|
|
-> 70 00 00 00 00 00 00 0b 00 00 00 00 00 00 00 00
|
|
|
|
00 00 00
|
|
|
|
|
|
|
|
<- 1b ea 00 00 00 00 00 ff 60 00 00 ** In spool file
|
|
|
|
<- [[ 0x0060ff00 bytes of data ]]
|
|
|
|
|
|
|
|
<- 1b e0 00 00 00 0f 00
|
|
|
|
-> 0e 00 00 00 00 00 00 00 04 a8 08 00 0a a4 00
|
|
|
|
|
|
|
|
<- 1b 0a 00 00 00 00 00 ** In spool file
|
|
|
|
<- 1b 17 00 00 00 00 00 ** In spool file
|
|
|
|
|
|
|
|
[[fin]]
|
|
|
|
|
|
|
|
*/
|