selphy_print/backend_sonyupd.c

1577 lines
38 KiB
C
Raw Permalink Normal View History

/*
2024-01-05 12:09:41 -05:00
* Sony UP-D series Photo Printer CUPS backend
*
* (c) 2013-2024 Solomon Peachy <pizza@shaftnet.org>
*
* The latest version of this program can be found at:
*
* https://git.shaftnet.org/gitea/slp/selphy_print.git
*
* 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, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0+
*
*/
#define BACKEND sonyupd_backend
#include "backend_common.h"
/* Printer status
--> 1b e0 00 00 00 00 XX 00 [[ XX is 0xe on UPD895, 0xf on others ]]
<-- this struct
*/
struct sony_updsts {
2019-04-06 17:38:28 -04:00
uint8_t len; /* 0x0d/0x0e (ie number of bytes AFTER this one) */
uint8_t zero1; /* 0x00 */
uint8_t printing; /* UPD_PRINTING_* */
uint8_t remain; /* Number of remaining pages */
2020-11-28 10:39:28 -05:00
uint8_t sts0; /* UPD_STS0_* */
uint8_t sts1; /* UPD_STS1_* */
2020-11-28 10:39:28 -05:00
uint8_t sts2; /* UPD_STS2_* */
uint8_t sts3; /* UPD_STS3_* */
uint8_t ribbon; /* 0x04 = R206/6x8 or C48/4x8 */
uint8_t paper; /* 0x38 = EMPTY, 0xa8/0x90 = loaded */
uint16_t max_cols; /* BE */
uint16_t max_rows; /* BE */
uint8_t percent; /* 0-99, if job is printing (UP-D89x) */
} __attribute__((packed));
struct sony_prints {
uint8_t zero[4];
uint16_t remain; /* BE, remaining prints on media */
} __attribute((packed));
#define UPD_PRINTING_BW 0xe0 /* UPD-895/897 only */
#define UPD_PRINTING_Y 0x40
#define UPD_PRINTING_M 0x80
#define UPD_PRINTING_C 0xc0
#define UPD_PRINTING_O 0x20
#define UPD_PRINTING_IDLE 0x00
2020-11-28 10:39:28 -05:00
/* Confirmed on UP-DR200 */
#define UPD_STS0_OK 0x00
#define UPD_STS0_NORIBBON 0x10
#define UPD_STS0_NOPAPER 0x20
#define UPD_STS0_DOOROPEN 0x40
/* Confirmed on UP-D89x */
#define UPD_STS1_IDLE 0x00
#define UPD_STS1_DOOROPEN 0x08
#define UPD_STS1_NOPAPER 0x40
#define UPD_STS1_PRINTING 0x80
2020-11-29 09:42:05 -05:00
#define UPD_STS1_PRINTING2 0xC0
/* Confirmed on UP-D711 */
#define UPD7_STS0_IDLE 0x00
#define UPD7_STS0_NOPAPER 0x08
#define UPD7_STS0_DOOROPEN 0x40
//#define UPD7_STS0_PRINTING 0x80
//#define UPD7_STS0_PRINTING2 0xC0
#define UPD_RIBBON_R206 0x04
#define UPD_RIBBON_C48 0x04
#define NDC_RIBBON_D2T 0x02
/* Private data structures */
struct upd_printjob {
struct dyesub_job_common common;
2020-11-29 09:42:05 -05:00
uint8_t *databuf;
int datalen;
uint16_t rows;
uint16_t cols;
uint32_t imglen;
};
struct upd_ctx {
struct dyesub_connection *conn;
int native_bpp;
struct sony_updsts stsbuf;
struct sony_prints printbuf;
struct marker marker;
};
2020-11-29 09:42:05 -05:00
static const char *upd_ribbons(int type, uint8_t code)
{
if (type == P_SONY_UPD895 || type == P_SONY_UPD897) {
return "UP-110 Roll";
} else if (type == P_SONY_UPD711) {
return "UPP-84 Roll";
} else if (type == P_SONY_UPCR10) {
if (code == UPD_RIBBON_C48)
return "2UPC-C48 (4x8)";
} else if (type == P_SONY_UPDR150) {
/* DR200/DR150 */
if (code == UPD_RIBBON_R206)
return "R206 (8x6)";
} else if (type == P_NDC) {
if (code == NDC_RIBBON_D2T) {
return "RK-D2T (4x6)";
}
}
return "Unknown";
}
static int sonyupd_media_maxes(uint8_t type, uint8_t media)
{
if (type == P_SONY_UPDR150) {
if (media == UPD_RIBBON_R206)
return 350;
else
return 700; // XXX guess until we have more codes?
// XXX also differs for DR200 vs DR150?
} else if (type == P_SONY_UPCR10) {
if (media == UPD_RIBBON_C48)
return 150;
return 200; // XXX guess until we have more codes.
} else if (type == P_NDC) {
2023-09-11 16:40:55 -04:00
if (media == NDC_RIBBON_D2T) {
return 600;
}
}
return CUPS_MARKER_UNAVAILABLE;
}
2019-04-16 09:53:54 -04:00
// UP-DR200
// 2UPC-R203 3.5x5 (770)
// 2UPC-R204 4x6 (700)
// 2UPC-R205 5x7 (400)
// 2UPC-R206 6x8 (350)
// UP-DR150
// 2UPC-R153 (610)
// 2UPC-R154 (550)
// 2UPC-R155 (335)
// 2UPC-R156 (295)
// UP-CR10L & UP-CX1
2020-11-28 10:39:28 -05:00
// 2UPC-C13 (344)
2020-11-28 10:39:28 -05:00
// 2UPC-C14 (200)
// 2UPC-C15 (172)
// 2UPC-C48 (150)
2020-11-28 10:39:28 -05:00
2019-04-16 09:53:54 -04:00
// print order: ->YMCO->
// current prints (power on)
// total prints (lifetime)
// f/w version
static const char* upd895_statuses(uint8_t code)
{
switch (code) {
case UPD_STS1_IDLE:
return "Idle";
case UPD_STS1_DOOROPEN:
return "Door open";
case UPD_STS1_NOPAPER:
return "No paper";
case UPD_STS1_PRINTING:
2020-11-29 09:42:05 -05:00
case UPD_STS1_PRINTING2:
return "Printing";
default:
return "Unknown";
}
}
static const char* upd711_statuses(uint8_t code)
{
switch (code) {
case UPD7_STS0_IDLE:
return "Idle";
case UPD7_STS0_DOOROPEN:
return "Door open";
case UPD7_STS0_NOPAPER:
return "No paper";
#if 0
case UPD7_STS0_PRINTING:
case UPD7_STS0_PRINTING2:
return "Printing";
#endif
default:
return "Unknown";
}
}
2020-11-28 10:39:28 -05:00
static const char* updr200_statuses(uint8_t code)
{
switch (code) {
case UPD_STS0_OK:
return "OK";
case UPD_STS0_DOOROPEN:
return "Door open";
case UPD_STS0_NOPAPER:
return "No paper";
case UPD_STS0_NORIBBON:
return "No ribbon";
default:
return "Unknown";
}
}
/* Now for the code */
static int sony_get_status(struct upd_ctx *ctx, struct sony_updsts *buf)
{
int ret, num = 0;
uint8_t query[7] = { 0x1b, 0xe0, 0, 0, 0, 0x0f, 0 };
if (ctx->conn->type == P_SONY_UPD895 ||
ctx->conn->type == P_NDC)
query[5] = 0x0e;
if ((ret = send_data(ctx->conn,
query, sizeof(query))))
return CUPS_BACKEND_FAILED;
ret = read_data(ctx->conn, (uint8_t*) buf, sizeof(*buf),
&num);
if (ret < 0)
return CUPS_BACKEND_FAILED;
#if 0
if (ctx->conn->type == P_SONY_UPD895 && ret != 14)
return CUPS_BACKEND_FAILED;
else if (ret != 15)
return CUPS_BACKEND_FAILED;
#endif
buf->max_cols = be16_to_cpu(buf->max_cols);
buf->max_rows = be16_to_cpu(buf->max_rows);
return CUPS_BACKEND_OK;
}
static int sony_get_prints(struct upd_ctx *ctx, struct sony_prints *buf)
{
int ret, num = 0;
uint8_t query[7] = { 0x1b, 0xef, 0, 0, 0, 0x06, 0 };
if ((ret = send_data(ctx->conn,
query, sizeof(query))))
return CUPS_BACKEND_FAILED;
ret = read_data(ctx->conn, (uint8_t*) buf, sizeof(*buf),
&num);
if (ret < 0)
return CUPS_BACKEND_FAILED;
buf->remain = be16_to_cpu(buf->remain);
2019-03-21 19:40:58 -04:00
return CUPS_BACKEND_OK;
}
static void* upd_init(void)
{
struct upd_ctx *ctx = malloc(sizeof(struct upd_ctx));
if (!ctx) {
ERROR("Memory Allocation Failure!\n");
return NULL;
}
memset(ctx, 0, sizeof(struct upd_ctx));
return ctx;
}
static int upd_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid)
{
struct upd_ctx *ctx = vctx;
UNUSED(jobid);
ctx->conn = conn;
if (ctx->conn->type == P_SONY_UPD895 || ctx->conn->type == P_SONY_UPD897 || ctx->conn->type == P_SONY_UPD711) {
ctx->marker.color = "#000000"; /* Ie black! */
ctx->native_bpp = 1;
} else {
ctx->marker.color = "#00FFFF#FF00FF#FFFF00";
ctx->native_bpp = 3;
}
if (test_mode < TEST_MODE_NOATTACH) {
int ret;
if ((ret = sony_get_status(ctx, &ctx->stsbuf))) {
return ret;
}
if (ctx->native_bpp == 1 && (ret = sony_get_prints(ctx, &ctx->printbuf))) {
return ret;
}
}
if (test_mode >= TEST_MODE_NOATTACH && getenv("MEDIA_CODE")) {
ctx->marker.numtype = strtol(getenv("MEDIA_CODE"), NULL, 16);
} else {
ctx->marker.numtype = ctx->stsbuf.ribbon;
}
2020-11-29 09:42:05 -05:00
ctx->marker.name = upd_ribbons(ctx->conn->type, ctx->stsbuf.ribbon);
if (test_mode >= TEST_MODE_NOATTACH || ctx->native_bpp == 1) {
ctx->marker.levelmax = CUPS_MARKER_UNAVAILABLE;
ctx->marker.levelnow = CUPS_MARKER_UNKNOWN;
} else {
ctx->marker.levelmax = sonyupd_media_maxes(ctx->conn->type, ctx->stsbuf.ribbon);
ctx->marker.levelnow = ctx->printbuf.remain;
}
return CUPS_BACKEND_OK;
}
static void upd_cleanup_job(const void *vjob)
{
const struct upd_printjob *job = vjob;
if (job->databuf)
free(job->databuf);
free((void*)job);
}
//#define MAX_PRINTJOB_LEN (2048*2764*3 + 2048) /* Sony */
#define MAX_PRINTJOB_LEN (2444*3644*3 + 2048) /* DPB and ASK series */
static int ndc_read_parse(struct upd_ctx *ctx, struct upd_printjob *job, int data_fd, uint32_t *copies_offset)
{
int run = 1;
uint32_t param_offset = 0;
(void)ctx;
while(run) {
int i;
int remain = 0;
uint32_t cmdlen;
uint8_t cmdbuf[11];
/* Read the ESC and command */
i = read(data_fd, cmdbuf, 2);
if (i < 0) {
return CUPS_BACKEND_CANCEL;
}
if (i == 0)
break;
if (cmdbuf[0] != 0x1b) {
ERROR("Unexpected data in stream! (%02x)\n", cmdbuf[0]);
return CUPS_BACKEND_CANCEL;
}
/* Work out command specifics */
if (cmdbuf[1] == 0xea) { // Data transfer
cmdlen = 11;
} else { // everything else
cmdlen = 7;
}
if (job->datalen + cmdlen > MAX_PRINTJOB_LEN) {
ERROR("Buffer overflow when parsing printjob! (%d+%d)\n",
job->datalen, remain);
return CUPS_BACKEND_CANCEL;
}
/* Some special casing */
if (cmdbuf[1] == 0x0a)
run = 0;
else if (cmdbuf[1] == 0xee)
*copies_offset = job->datalen + 7 + sizeof(uint32_t)*2;
else if (cmdbuf[1] == 0xe1)
param_offset = job->datalen + 14 + sizeof(uint32_t)*2;
/* Read remainder of cmd */
i = read(data_fd, cmdbuf + 2, cmdlen - 2);
if (i != (int)(cmdlen - 2)) {
ERROR("Unexpected read length! (%d)\n", i);
return CUPS_BACKEND_CANCEL;
}
/* Move cmdbuf into job buffer */
memcpy(job->databuf + job->datalen, &cmdlen, sizeof(uint32_t));
job->datalen += sizeof(uint32_t);
memcpy(job->databuf + job->datalen, cmdbuf, cmdlen);
job->datalen += cmdlen;
/* Work out data transfer length */
if (cmdbuf[1] == 0xea) { /* Image data transfer */
uint32_t datalen;
memcpy(&datalen, cmdbuf + 6, sizeof(uint32_t));
datalen = be32_to_cpu(datalen);
memcpy(job->databuf + job->datalen, &datalen, sizeof(uint32_t));
job->datalen += sizeof(uint32_t);
remain = datalen;
job->imglen = datalen;
} else { // everything else
uint32_t datalen;
uint16_t len;
memcpy(&len, cmdbuf + 4, sizeof(uint16_t));
len = be16_to_cpu(len);
if (len) {
datalen = len;
memcpy(job->databuf + job->datalen, &datalen, sizeof(uint32_t));
job->datalen += sizeof(uint32_t);
}
remain = len;
}
if(dyesub_debug)
DEBUG("Data block (len %d)\n", remain);
/* Make sure we're not too large */
if (job->datalen + remain > MAX_PRINTJOB_LEN) {
ERROR("Buffer overflow when parsing printjob! (%d+%d)\n",
job->datalen, remain);
return CUPS_BACKEND_CANCEL;
}
/* Read in the data chunk */
while (remain > 0) {
i = read(data_fd, job->databuf + job->datalen, remain);
if (i < 0) {
return CUPS_BACKEND_CANCEL;
}
if (i == 0)
break;
remain -= i;
job->datalen += i;
}
}
/* Parse some other stuff */
if (param_offset) {
memcpy(&job->cols, job->databuf + param_offset, sizeof(uint16_t));
memcpy(&job->rows, job->databuf + param_offset + 2, sizeof(uint16_t));
job->cols = be16_to_cpu(job->cols);
job->rows = be16_to_cpu(job->rows);
}
return CUPS_BACKEND_OK;
}
static int sony_read_parse(struct upd_ctx *ctx, struct upd_printjob *job, int data_fd, uint32_t *copies_offset) {
int len, run = 1;
uint32_t param_offset = 0;
uint32_t data_offset = 0;
while(run) {
int i;
int keep = 0;
i = read(data_fd, job->databuf + job->datalen, 4);
if (i < 0) {
return CUPS_BACKEND_CANCEL;
}
if (i == 0)
break;
memcpy(&len, job->databuf + job->datalen, sizeof(len));
len = le32_to_cpu(len);
/* Filter out chunks we don't send to the printer */
if (len & 0xf0000000) {
switch (len) {
case 0xfffffff3:
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 0);
len = 0;
if (ctx->conn->type == P_SONY_UPDR150)
run = 0;
break;
case 0xfffffff7:
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 0);
len = 0;
if (ctx->conn->type == P_SONY_UPCR10)
run = 0;
break;
case 0xfffffff8: // 895
case 0xfffffff4: // 897/711
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 0);
len = 0;
if (ctx->conn->type == P_SONY_UPD895 || ctx->conn->type == P_SONY_UPD897 || ctx->conn->type == P_SONY_UPD711)
run = 0;
break;
case 0xfffffff5:
if (ctx->conn->type == P_SONY_UPD711) {
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 0);
len = 0;
} else {
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 4);
len = 4;
}
break;
case 0xffffff97:
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 12);
len = 12;
break;
case 0xffffffef:
if (ctx->conn->type == P_SONY_UPD895 || ctx->conn->type == P_SONY_UPD897) {
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 0);
len = 0;
} else {
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 4);
len = 4;
}
break;
case 0xffffffeb:
case 0xffffffee:
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 4);
len = 4;
break;
case 0xffffffec:
if (ctx->conn->type == P_SONY_UPD897 || ctx->conn->type == P_SONY_UPD711) {
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 4);
len = 4;
} else {
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 0);
len = 0;
}
break;
default:
if(dyesub_debug)
DEBUG("Block ID '%08x' (len %d)\n", len, 0);
len = 0;
break;
}
} else {
/* Only keep these chunks */
if(dyesub_debug)
DEBUG("Data block (len %d)\n", len);
if (len > 0)
keep = 1;
}
if (keep)
job->datalen += sizeof(uint32_t);
/* Make sure we're not too large */
if (job->datalen + len > MAX_PRINTJOB_LEN) {
ERROR("Buffer overflow when parsing printjob! (%d+%d)\n",
job->datalen, len);
return CUPS_BACKEND_CANCEL;
}
/* Read in the data chunk */
while(len > 0) {
i = read(data_fd, job->databuf + job->datalen, len);
if (i < 0) {
return CUPS_BACKEND_CANCEL;
}
if (i == 0)
break;
/* Work out offset of copies command */
if (job->databuf[job->datalen] == 0x1b) {
int offset = 0;
if (i == 7)
offset = 4;
switch (job->databuf[job->datalen + 1]) {
2019-03-19 16:25:28 -04:00
case 0x15: /* Print dimensions */
param_offset = job->datalen + 16 + offset;
break;
// XXX case 0xc0:
// for param 03, take the value at offset 4 -- for (eg) 4x6 on 8x6 media, needs to be set to 0x02
case 0xee:
*copies_offset = job->datalen + 7 + offset;
break;
2019-03-19 16:25:28 -04:00
case 0xe1: /* Image dimensions */
param_offset = job->datalen + 14 + offset;
break;
case 0xea:
data_offset = job->datalen + 6 + offset;
break;
default:
break;
}
}
if (keep)
job->datalen += i;
len -= i;
}
}
if (!job->datalen) {
return CUPS_BACKEND_CANCEL;
}
/* Parse some other stuff */
if (param_offset) {
memcpy(&job->cols, job->databuf + param_offset, sizeof(uint16_t));
memcpy(&job->rows, job->databuf + param_offset + 2, sizeof(uint16_t));
job->cols = be16_to_cpu(job->cols);
job->rows = be16_to_cpu(job->rows);
}
if (data_offset) {
memcpy(&job->imglen, job->databuf + data_offset, sizeof(uint32_t));
job->imglen = be32_to_cpu(job->imglen);
}
return CUPS_BACKEND_OK;
}
static int upd_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
struct upd_ctx *ctx = vctx;
struct upd_printjob *job = NULL;
uint32_t copies_offset = 0;
int rval = CUPS_BACKEND_OK;
if (!ctx)
return CUPS_BACKEND_FAILED;
job = malloc(sizeof(*job));
if (!job) {
ERROR("Memory allocation failure!\n");
return CUPS_BACKEND_RETRY_CURRENT;
}
memset(job, 0, sizeof(*job));
job->common.jobsize = sizeof(*job);
job->common.copies = copies;
job->datalen = 0;
job->databuf = malloc(MAX_PRINTJOB_LEN);
if (!job->databuf) {
ERROR("Memory allocation failure!\n");
rval = CUPS_BACKEND_RETRY_CURRENT;
goto done;
}
if (ctx->conn->type == P_NDC) {
rval = ndc_read_parse(ctx, job, data_fd, &copies_offset);
} else {
rval = sony_read_parse(ctx, job, data_fd, &copies_offset);
}
/* Some models specify copies in the print job */
if (copies_offset) {
uint16_t tmp;
memcpy(&tmp, job->databuf + copies_offset, sizeof(tmp));
tmp = be16_to_cpu(tmp);
if (tmp < copies) { /* Use whichever one is larger */
tmp = cpu_to_be16(copies);
memcpy(job->databuf + copies_offset, &tmp, sizeof(tmp));
}
job->common.copies = 1;
}
if (!job->datalen) {
rval = CUPS_BACKEND_CANCEL;
goto done;
}
/* Sanity check job parameters */
if (job->imglen != (uint32_t)(job->rows * job->cols * ctx->native_bpp))
{
ERROR("Job data length mismatch (%u vs %d)!\n",
job->imglen, job->rows * job->cols * ctx->native_bpp);
rval = CUPS_BACKEND_CANCEL;
goto done;
}
#if 0
{
FILE *fp = fopen("/tmp/foo.prn", "wb");
fwrite(job->databuf, job->datalen, 1, fp);
fclose(fp);
}
#endif
done:
if (rval) {
upd_cleanup_job(job);
} else {
*vjob = job;
}
return rval;
}
static int upd_main_loop(void *vctx, const void *vjob, int wait_for_return) {