Gutenprint + CUPS backends for Dye Sublimation printers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
selphy_print/backend_mitsu9550.c

1970 lines
52 KiB

<
/*
* Mitsubishi CP-9xxx Photo Printer Family CUPS backend
*
* (c) 2014-2021 Solomon Peachy <pizza@shaftnet.org>
*
* The latest version of this program can be found at:
*
* https://git.shaftnet.org/cgit/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 mitsu9550_backend
#include "backend_common.h"
#include "backend_mitsu.h"
#define MITSU_M98xx_LAMINATE_FILE "M98MATTE.raw"
#define MITSU_M98xx_DATATABLE_FILE "M98TABLE.dat"
#define MITSU_M98xx_LUT_FILE "M98XXL01.lut"
#define MITSU_CP30D_LUT_FILE "CP30LT_1.lut"
#define LAMINATE_STRIDE 1868
/* Spool file structures */
/* Print parameters1 */
struct mitsu9550_hdr1 {
uint8_t cmd[4]; /* 1b 57 20 2e */
uint8_t unk[10]; /* 00 0a 10 00 [...] */
uint16_t cols; /* BE */
uint16_t rows; /* BE */
uint8_t matte; /* CP9810/9820 only. 01 for matte, 00 glossy */
uint8_t null[31];
} __attribute__((packed));
/* Print parameters2 */
struct mitsu9550_hdr2 {
uint8_t cmd[4]; /* 1b 57 21 2e */
uint8_t unk[24]; /* 00 80 00 22 08 03 [...] */
uint16_t copies; /* BE, 1-680 */
uint8_t null[2];
uint8_t cut; /* 00 == normal, 83 == 2x6*2 */
uint8_t unkb[5];
uint8_t mode; /* 00 == fine, 80 == superfine */
uint8_t unkc[11]; /* 00 [...] 00 01 ; note [7][8][9] are cp98xx extensions for sharpness/reversed/lut */
} __attribute__((packed));
/* Fine Deep selection (9550 only) */
struct mitsu9550_hdr3 {
uint8_t cmd[4]; /* 1b 57 22 2e */
uint8_t unk[7]; /* 00 40 00 [...] */
uint8_t mode2; /* 00 == normal, 01 == finedeep */
uint8_t null[38];
} __attribute__((packed));
/* Error policy? */
struct mitsu9550_hdr4 {
uint8_t cmd[4]; /* 1b 57 26 2e */
uint8_t unk[46]; /* 00 70 00 00 00 00 00 00 01 01 00 [...] */
} __attribute__((packed));
/* Data plane header */
struct mitsu9550_plane {
uint8_t cmd[4]; /* 1b 5a 54 XX */ /* XX == 0x10 if 16bpp, 0x00 for 8bpp */
uint16_t col_offset; /* BE, normally 0, where we start dumping data */
uint16_t row_offset; /* BE, normally 0, where we start dumping data */
uint16_t cols; /* BE */
uint16_t rows; /* BE */
} __attribute__((packed));
/* Command header */
struct mitsu9550_cmd {
uint8_t cmd[4];
} __attribute__((packed));
/* Private data structure */
struct mitsu9550_printjob {
struct dyesub_job_common common;
uint8_t *databuf;
uint32_t datalen;
uint16_t rows;
uint16_t cols;
uint32_t plane_len;
int is_raw;
/* Parse headers separately */
struct mitsu9550_hdr1 hdr1;
int hdr1_present;
struct mitsu9550_hdr2 hdr2;
int hdr2_present;
struct mitsu9550_hdr3 hdr3;
int hdr3_present;
struct mitsu9550_hdr4 hdr4;
int hdr4_present;
};
struct mitsu9550_ctx {
struct dyesub_connection *conn;
int is_s;
int is_98xx;
int need_lib;
int footer_len;
const char *lut_fname;
char fwver[7]; /* 6 + null */
struct marker marker;
/* CP98xx stuff */
struct mitsu_lib lib;
const struct mitsu98xx_data *m98xxdata;
};
/* Printer data structures */
#define CP9XXX_STS_x20 0x20
#define CP9XXX_STS_x21 0x21 /* struct mitsu9550_status2 */
#define CP9XXX_STS_x22 0x22
#define CP9XXX_STS_FWVER 0x23
#define CP9XXX_STS_MEDIA 0x24 /* struct mitsu9550_media */
#define CP9XXX_STS_x26 0x26
#define CP9XXX_STS_x30 0x30 /* struct mitsu9550_status */
#define CP9XXX_STS_x32 0x32 /* struct mitsucp30_status */
struct mitsu9550_media {
uint8_t hdr[2]; /* 24 2e */
uint8_t unk[12];
uint8_t type;
uint8_t unka[11];
uint16_t unkb; /* 0d 00 (CP9810), 00 00 (others?) */
uint16_t max; /* BE, prints per media */
uint16_t remain2; /* BE, prints remaining (CP30)*/
uint16_t remain; /* BE, prints remaining (Everything else) */
uint8_t unkc[14];
} __attribute__((packed));
struct mitsu9550_status {
uint8_t hdr[2]; /* 30 2e */
uint8_t null[4];
uint8_t sts1; // MM
uint8_t nullb[1];
uint16_t copies; // BE, NN
uint8_t sts2; // ZZ (9600 only?)
uint8_t nullc[5];
uint8_t sts3; // QQ
uint8_t sts4; // RR
uint8_t sts5; // SS
uint8_t nulld[25];
uint8_t sts6; // TT
uint8_t sts7; // UU
uint8_t nulle[2];
} __attribute__((packed));
struct mitsucp30_status {
uint8_t hdr[2]; /* 32 2e */
uint8_t zero[2];
uint16_t serno[13]; /* UTF16, BE */
uint8_t sts; /* CP30_STS_* */
uint8_t sts2;
uint8_t zerob;
uint16_t err; /* CP30_ERR_* */
uint8_t zeroc[2];
uint8_t remain; /* on media */
uint8_t zerod[9];
} __attribute__((packed));
#define CP30_STS_IDLE 0x00
#define CP30_STS_PRINT 0x20
#define CP30_STS_PRINT_A 0x10 //Load
#define CP30_STS_PRINT_B 0x20 //Y
#define CP30_STS_PRINT_C 0x30 //M
#define CP30_STS_PRINT_D 0x40 //C
#define CP30_STS_PRINT_E 0x60 //Eject?
#define CP30_ERR_OK 0x0000
#define CP30_ERR_NOPC 0x0303
#define CP30_ERR_NORIBBON 0x0101
#define CP30_ERR_TRAYFULL 0x1404
struct mitsu9550_status2 {
uint8_t hdr[2]; /* 21 2e */
uint8_t unk[40];
uint16_t remain; /* BE, media remaining */
uint8_t unkb[4]; /* 0a 00 00 01 */
} __attribute__((packed));
static int mitsu9550_main_loop(void *vctx, const void *vjob, int wait_for_return);
static const char *cp30_errors(uint16_t err)
{
switch(err){
case CP30_ERR_OK: return "None";
case CP30_ERR_NOPC: return "No Paper Cassette";
case CP30_ERR_NORIBBON: return "No Ribbon Loaded";
case CP30_ERR_TRAYFULL: return "Output Tray Full";
default: return "Unknown";
}
}
static const char *cp30_media_types(uint16_t remain)
{
switch(remain) {
case 80: return "CK30S";
case 50: return "CK30L";
default: return "Unknown";
}
}
#define CMDBUF_LEN 64
#define READBACK_LEN 128
#define QUERY_STATUS_I \
struct mitsu9550_status *sts = (struct mitsu9550_status*) rdbuf; \
struct mitsucp30_status *sts30 = (struct mitsucp30_status*) rdbuf; \
struct mitsu9550_media *media = (struct mitsu9550_media *) rdbuf; \
uint16_t donor; \
/* media */ \
ret = mitsu9550_get_status(ctx, rdbuf, CP9XXX_STS_MEDIA); \
if (ret < 0) \
return CUPS_BACKEND_FAILED; \
\
if (ctx->conn->type == P_MITSU_CP30D) { \
donor = be16_to_cpu(media->remain2); \
} else { \
donor = be16_to_cpu(media->remain); \
} \
if (donor != ctx->marker.levelnow) { \
ctx->marker.levelnow = donor; \
dump_markers(&ctx->marker, 1, 0); \
} \
/* Sanity-check media response */ \
if ((media->remain == 0 && media->remain2 == 0) || media->max == 0) { \
ERROR("Printer out of media!\n"); \
return CUPS_BACKEND_STOP; \
} \
#define QUERY_STATUS_II \
if (ctx->conn->type != P_MITSU_CP30D) { \
/* struct mitsu9550_status2 *sts2 = (struct mitsu9550_status2*) rdbuf; */ \
ret = mitsu9550_get_status(ctx, rdbuf, CP9XXX_STS_x21); \
if (ret < 0) \
return CUPS_BACKEND_FAILED; \
/* XXX validate status2 ? */ \
}
#define QUERY_STATUS_III \
/* Check for known errors */ \
if (sts->sts2 != 0) { \
ERROR("Printer cover open!\n"); \
return CUPS_BACKEND_STOP; \
}
#define QUERY_STATUS_IIIB \
/* Check for known errors */ \
if (sts30->err != CP30_ERR_OK) { \
ERROR("%s (%04x)!\n", cp30_errors(be16_to_cpu(sts30->err)), be16_to_cpu(sts30->err)); \
return CUPS_BACKEND_STOP; \
}
#define QUERY_STATUS_IV \
if (ctx->conn->type == P_MITSU_CP30D) { \
ret = mitsu9550_get_status(ctx, rdbuf, CP9XXX_STS_x32); \
if (ret < 0) \
return CUPS_BACKEND_FAILED; \
\
if (sts30->sts != CP30_STS_IDLE) { \
sleep(1); \
goto top; \
} \
QUERY_STATUS_IIIB; \
} else { \
ret = mitsu9550_get_status(ctx, rdbuf, CP9XXX_STS_x30); \
if (ret < 0) \
return CUPS_BACKEND_FAILED; \
\
/* Make sure we're idle */ \
if (sts->sts5 != 0) { \
sleep(1); \
goto top; \
} \
QUERY_STATUS_III; \
} \
static int mitsu98xx_fillmatte(struct mitsu9550_printjob *job)
{
int ret;
/* Fill in the lamination plane header */
struct mitsu9550_plane *matte = (struct mitsu9550_plane *)(job->databuf + job->datalen);
matte->cmd[0] = 0x1b;
matte->cmd[1] = 0x5a;
matte->cmd[2] = 0x54;
matte->cmd[3] = 0x10;
matte->row_offset = 0;
matte->col_offset = 0;
matte->cols = cpu_to_be16(job->hdr1.cols);
matte->rows = cpu_to_be16(job->hdr1.rows);
job->datalen += sizeof(struct mitsu9550_plane);
ret = mitsu_readlamdata(MITSU_M98xx_LAMINATE_FILE, LAMINATE_STRIDE,
job->databuf, &job->datalen,
job->rows, job->cols, 2);
if (ret)
return ret;
/* Fill in the lamination plane footer */
job->databuf[job->datalen++] = 0x1b;
job->databuf[job->datalen++] = 0x50;
job->databuf[job->datalen++] = 0x56;
job->databuf[job->datalen++] = 0x00;
return CUPS_BACKEND_OK;
}
static int mitsu9550_get_status(struct mitsu9550_ctx *ctx, uint8_t *resp, int type);
static const char *mitsu9550_media_types(uint8_t type, uint8_t is_s);
static void *mitsu9550_init(void)
{
struct mitsu9550_ctx *ctx = malloc(sizeof(struct mitsu9550_ctx));
if (!ctx) {
ERROR("Memory Allocation Failure!\n");
return NULL;
}
memset(ctx, 0, sizeof(struct mitsu9550_ctx));
return ctx;
}
static int mitsu9550_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid)
{
struct mitsu9550_ctx *ctx = vctx;
struct mitsu9550_media media;
UNUSED(jobid);
ctx->conn = conn;
if (ctx->conn->type == P_MITSU_9550S ||
ctx->conn->type == P_MITSU_9800S)
ctx->is_s = 1;
if (ctx->conn->type == P_MITSU_9800 ||
ctx->conn->type == P_MITSU_9800S ||
ctx->conn->type == P_MITSU_9810) {
ctx->is_98xx = 1;
ctx->need_lib = 1;
ctx->lut_fname = MITSU_M98xx_LUT_FILE;
}
if (ctx->conn->type == P_MITSU_CP30D) {
ctx->need_lib = 1;
}
if (ctx->need_lib) {
#if defined(WITH_DYNAMIC)
/* Attempt to open the library */
if (mitsu_loadlib(&ctx->lib, ctx->conn->type))
#endif
WARNING("Dynamic library support not loaded, will be unable to print.");
}
if (ctx->conn->type == P_MITSU_CP30D) {
ctx->footer_len = 6;
ctx->lut_fname = MITSU_CP30D_LUT_FILE;
} else {
ctx->footer_len = 4;
}
if (test_mode < TEST_MODE_NOATTACH) {
uint8_t buf[48];
if (mitsu9550_get_status(ctx, (uint8_t*) &media, CP9XXX_STS_MEDIA))
return CUPS_BACKEND_FAILED;
/* Get FW Version */
if (mitsu9550_get_status(ctx, buf, CP9XXX_STS_FWVER))
return CUPS_BACKEND_FAILED;
memcpy(ctx->fwver, &buf[6], 6);
ctx->fwver[6] = 0;
// XXX get serial number too?
} else {
int media_code = 0x2;
if (getenv("MEDIA_CODE"))
media_code = atoi(getenv("MEDIA_CODE")) & 0xf;
media.max = cpu_to_be16(400);
media.remain = cpu_to_be16(330);
media.remain2 = cpu_to_be16(330);
media.type = media_code;
ctx->fwver[0] = 0;
}
ctx->marker.color = "#00FFFF#FF00FF#FFFF00";
ctx->marker.numtype = media.type;
ctx->marker.levelmax = be16_to_cpu(media.max);
if (ctx->conn->type == P_MITSU_CP30D) {
ctx->marker.name = cp30_media_types(be16_to_cpu(media.max));
ctx->marker.levelnow = be16_to_cpu(media.remain2);
} else {
ctx->marker.name = mitsu9550_media_types(media.type, ctx->is_s);
ctx->marker.levelnow = be16_to_cpu(media.remain);
}
return CUPS_BACKEND_OK;
}
static void mitsu9550_cleanup_job(const void *vjob)
{
const struct mitsu9550_printjob *job = vjob;
if (job->databuf)
free(job->databuf);
free((void*)job);
}
static void mitsu9550_teardown(void *vctx) {
struct mitsu9550_ctx *ctx = vctx;
if (!ctx)
return;
if (ctx->m98xxdata)
ctx->lib.CP98xx_DestroyData(ctx->m98xxdata);
mitsu_destroylib(&ctx->lib);
free(ctx);
}
static int mitsu9550_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
struct mitsu9550_ctx *ctx = vctx;
uint8_t buf[sizeof(struct mitsu9550_hdr1)];
int remain, i;
uint32_t planelen = 0;
struct mitsu9550_printjob *job = NULL;
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->is_raw = 1;
job->common.jobsize = sizeof(*job);
job->common.copies = copies;
top:
/* Read in initial header */
remain = sizeof(buf);
while (remain > 0) {
i = read(data_fd, buf + sizeof(buf) - remain, remain);
if (i == 0) {
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
if (i < 0) {
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
remain -= i;
}
/* Sanity check */
if (buf[0] != 0x1b || buf[1] != 0x57 || buf[3] != 0x2e) {
if (!job->hdr1_present || !job->hdr2_present) {
ERROR("Unrecognized data format (%02x%02x%02x%02x)!\n",
buf[0], buf[1], buf[2], buf[3]);
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
} else if (buf[0] == 0x1b &&
buf[1] == 0x5a &&
buf[2] == 0x54) {
/* We're in the data portion now */
if (buf[3] == 0x10)
planelen *= 2;
else if (ctx->is_98xx && buf[3] == 0x80)
job->is_raw = 0;
goto hdr_done;
} else {
ERROR("Unrecognized data block (%02x%02x%02x%02x)!\n",
buf[0], buf[1], buf[2], buf[3]);
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
}
switch(buf[2]) {
case 0x20: /* header 1 */
memcpy(&job->hdr1, buf, sizeof(job->hdr1));
job->hdr1_present = 1;
/* Work out printjob size */
job->rows = be16_to_cpu(job->hdr1.rows);
job->cols = be16_to_cpu(job->hdr1.cols);
planelen = job->rows * job->cols;
break;
case 0x21: /* header 2 */
memcpy(&job->hdr2, buf, sizeof(job->hdr2));
job->hdr2_present = 1;
break;
case 0x22: /* header 3 */
memcpy(&job->hdr3, buf, sizeof(job->hdr3));
job->hdr3_present = 1;
break;
case 0x26: /* header 4 */
memcpy(&job->hdr4, buf, sizeof(job->hdr4));
job->hdr4_present = 1;
break;
default:
ERROR("Unrecognized header format (%02x)!\n", buf[2]);
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
/* Read in the next chunk */
goto top;
hdr_done:
/* Read in CP98xx data tables if necessary */
if (ctx->is_98xx && !job->is_raw && !ctx->m98xxdata) {
char full[2048];
if (!ctx->lib.dl_handle) {
ERROR("!!! Image Processing Library not found, aborting!\n");
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
snprintf(full, sizeof(full), "%s/%s", corrtable_path, MITSU_M98xx_DATATABLE_FILE);
DEBUG("Reading in 98xx data from disk\n");
ctx->m98xxdata = ctx->lib.CP98xx_GetData(full);
if (!ctx->m98xxdata) {
ERROR("Unable to read 98xx data table file '%s'\n", full);
}
}
if (job->is_raw) {
/* We have three planes + headers and the final terminator to read */
remain = 3 * (planelen + sizeof(struct mitsu9550_plane)) + ctx->footer_len;
} else {
/* We have one plane + header and the final terminator to read */
remain = planelen * 3 + sizeof(struct mitsu9550_plane) + ctx->footer_len;
}
/* Mitsu9600 windows spool uses more, smaller blocks, but plane data is the same */
if (ctx->conn->type == P_MITSU_9600) {
remain += 128 * sizeof(struct mitsu9550_plane); /* 39 extra seen on 4x6" */
}
/* 9550S/9800S doesn't typically sent over hdr4! */
if (ctx->conn->type == P_MITSU_9550S ||
ctx->conn->type == P_MITSU_9800S) {
/* XXX Has to do with error policy, but not sure what.
Mitsu9550-S/9800-S will set this based on a command,
but it's not part of the standard job spool */
job->hdr4_present = 0;
}
/* Disable matte if the printer doesn't support it */
if (job->hdr1.matte) {
if (ctx->conn->type != P_MITSU_9810) {
WARNING("Matte not supported on this printer, disabling\n");
job->hdr1.matte = 0;
} else if (job->is_raw) {
remain += planelen + sizeof(struct mitsu9550_plane) + sizeof(struct mitsu9550_cmd);
}
}
/* Allocate buffer for the payload */
job->datalen = 0;
job->databuf = malloc(remain);
if (!job->databuf) {
ERROR("Memory allocation failure!\n");
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_RETRY_CURRENT;
}
/* Load up the data blocks.*/
while(1) {
/* Note that 'buf' needs to be already filled here! */
struct mitsu9550_plane *plane = (struct mitsu9550_plane *)buf;
/* Sanity check header... */
if (plane->cmd[0] != 0x1b ||
plane->cmd[1] != 0x5a ||
plane->cmd[2] != 0x54) {
ERROR("Unrecognized data read @%d (%02x%02x%02x%02x)!\n",
job->datalen,
plane->cmd[0], plane->cmd[1], plane->cmd[2], plane->cmd[3]);
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
/* Work out the length of this block */
planelen = be16_to_cpu(plane->rows) * be16_to_cpu(plane->cols);
if (plane->cmd[3] == 0x10)
planelen *= 2;
if (plane->cmd[3] == 0x80)
planelen *= 3;
/* Copy plane header into buffer */
memcpy(job->databuf + job->datalen, buf, sizeof(buf));
job->datalen += sizeof(buf);
planelen -= sizeof(buf) - sizeof(struct mitsu9550_plane);
/* Read in the spool data */
while(planelen > 0) {
i = read(data_fd, job->databuf + job->datalen, planelen);
if (i == 0) {
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
if (i < 0) {
mitsu9550_cleanup_job(job);
return CUPS_BACKEND_CANCEL;
}
job->datalen += i;
planelen -= i;