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_dnpds40.c

3902 lines
99 KiB

/*
* Citizen / DNP Photo Printer CUPS backend -- libusb-1.0 version
*
* (c) 2013-2021 Solomon Peachy <pizza@shaftnet.org>
*
* Development of this backend was sponsored by:
*
* Marco Di Antonio and [ ilgruppodigitale.com ]
* LiveLink Technology [ www.livelinktechnology.net ]
* A generous benefactor who wishes to remain anonymous
*
* 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 DNP_ONLY
//#define CITIZEN_ONLY
/* Enables caching of last print type to speed up
job pipelining. Without this we always have to
assume the worst */
//#define STATE_DIR "/tmp"
#define BACKEND dnpds40_backend
#include "backend_common.h"
#include <time.h>
/* Private data structure */
struct dnpds40_printjob {
struct dyesub_job_common common;
uint8_t *databuf;
int datalen;
uint32_t dpi;
int matte;
int cutter;
uint32_t multicut;
int fullcut;
int printspeed;
int can_rewind;
int buf_needed;
int cut_paper;
};
#define MFG_DNP 0
#define MFG_CITIZEN 1
#define MFG_MITSUBISHI 2
#define MFG_OTHER 3
struct dnpds40_ctx {
struct dyesub_connection *conn;
int mfg; /* see MFG_* */
/* Version and whatnot */
char *serno;
char *version;
int ver_major;
int ver_minor;
/* State */
uint32_t media;
uint32_t media_subtype;
char media_text[32];
uint32_t duplex_media;
int duplex_media_status;
uint16_t media_count_new;
uint32_t last_multicut;
int last_matte;
int partialmatte;
int media_sticker;
int mediaoffset;
int correct_count;
int needs_mlot;
struct marker marker[2];
int marker_count;
/* Printer capabilities */
uint32_t native_width;
uint32_t max_height;
int supports_600dpi;
int supports_6x9;
int supports_2x6;
int supports_3x5x2;
int supports_a4x6;
int supports_matte;
int supports_finematte;
int supports_luster;
int supports_advmatte;
int supports_fullcut;
int supports_rewind;
int supports_standby;
int supports_keepmode;
int supports_6x4_5;
int supports_mqty_default;
int supports_iserial;
int supports_6x6;
int supports_5x5;
int supports_counterp;
int supports_adv_fullcut;
int supports_mediaoffset;
int supports_ctrld_ext;
int supports_media_ext;
int supports_printspeed;
int supports_lowspeed;
int supports_highdensity;
int supports_systime;
int supports_mediaclassrfid;
int supports_gamma;
};
struct dnpds40_cmd {
uint8_t esc; /* Fixed at ascii ESC, aka 0x1B */
uint8_t p; /* Fixed at ascii 'P' aka 0x50 */
uint8_t arg1[6];
uint8_t arg2[16];
uint8_t arg3[8]; /* Decimal value of arg4's length, or empty */
uint8_t arg4[0]; /* Extra payload if arg3 is non-empty
Doesn't have to be sent in the same URB */
/* All unused elements are set to 0x20 (ie ascii space) */
};
#define MULTICUT_5x3_5 1
#define MULTICUT_6x4 2
#define MULTICUT_5x7 3
#define MULTICUT_6x8 4
#define MULTICUT_6x9 5
#define MULTICUT_8x10 6
#define MULTICUT_8x12 7
#define MULTICUT_8x4 8
#define MULTICUT_8x5 9
#define MULTICUT_8x6 10
#define MULTICUT_8x8 11
#define MULTICUT_6x4X2 12
#define MULTICUT_8x4X2 13
#define MULTICUT_8x5X2 14
#define MULTICUT_8x6X2 15
#define MULTICUT_8x5_8x4 16
#define MULTICUT_8x6_8x4 17
#define MULTICUT_8x6_8x5 18
#define MULTICUT_8x8_8x4 19
#define MULTICUT_8x4X3 20
#define MULTICUT_8xA4LEN 21
#define MULTICUT_5x3_5X2 22
#define MULTICUT_6x6 27
#define MULTICUT_5x5 29
#define MULTICUT_6x4_5 30
#define MULTICUT_6x4_5X2 31
#define MULTICUT_8x7 32
#define MULTICUT_8x9 33
#define MULTICUT_A5 34
#define MULTICUT_A5X2 35
#define MULTICUT_A4x4 36
#define MULTICUT_A4x5 37
#define MULTICUT_A4x6 38
#define MULTICUT_A4x8 39
#define MULTICUT_A4x10 40
#define MULTICUT_A4 41
#define MULTICUT_A4x5X2 43
#define MULTICUT_4x4 47
#define MULTICUT_4x6 48
#define MULTICUT_4x8 49
#define MULTICUT_4_5x4_5 50
#define MULTICUT_4_5x6 51
#define MULTICUT_4_5x8 52
#define MULTICUT_S_SIMPLEX 100
#define MULTICUT_S_FRONT 200
#define MULTICUT_S_BACK 300
#define MULTICUT_S_8x10 6
#define MULTICUT_S_8x12 7
#define MULTICUT_S_8x4 8
#define MULTICUT_S_8x5 9
#define MULTICUT_S_8x6 10
#define MULTICUT_S_8x8 11
#define MULTICUT_S_8x4X2 13
#define MULTICUT_S_8x5X2 14
#define MULTICUT_S_8x6X2 15
#define MULTICUT_S_8x10_5 25
#define MULTICUT_S_8x10_75 26
#define MULTICUT_S_8x4X3 28 // different than roll type.
#ifndef min
#define min(__x, __y) ((__x) < (__y)) ? __x : __y
#endif
/* Legacy spool file support */
static int legacy_cw01_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
static int legacy_dnp_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
static int legacy_dnp620_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
static int legacy_dnp820_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
static int legacy_qw410_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
static void dnpds40_cleanup_job(const void *vjob);
static int dnpds40_query_markers(void *vctx, struct marker **markers, int *count);
#define JOB_EQUIV(__x) if (job1->__x != job2->__x) goto done
/* NOTE: Does _not_ free the input jobs */
static void *dnp_combine_jobs(const void *vjob1,
const void *vjob2)
{
const struct dnpds40_printjob *job1 = vjob1;
const struct dnpds40_printjob *job2 = vjob2;
struct dnpds40_printjob *newjob = NULL;
uint32_t new_multicut;
uint16_t new_w, new_h;
int32_t gap_bytes;
/* Sanity check */
if (!job1 || !job2)
goto done;
/* Make sure pertinent paremeters are the same */
JOB_EQUIV(dpi);
JOB_EQUIV(matte);
JOB_EQUIV(cutter);
JOB_EQUIV(fullcut);
JOB_EQUIV(multicut); // TODO: Support fancier modes for 8" models (eg 8x4+8x6, etc)
JOB_EQUIV(datalen); // <-- cheating a little?
// JOV_EQUIV(printspeed); <-- does it matter?
/* Any fancy cutter action means we pass */
if (job1->fullcut || job1->cutter > 120)
goto done;
/* Partial matte is no bueno */
if (job1->matte > 100 ||
job2->matte > 100)
goto done;
/* Make sure we can combine these two prints */
switch (job1->multicut) {
case MULTICUT_5x3_5:
new_multicut = MULTICUT_5x3_5X2;
new_w = 1920;
new_h = 2176;
gap_bytes = 0;
break;
case MULTICUT_6x4:
if (job1->cutter == 120) {
new_multicut = MULTICUT_6x8;
new_h = 2436;
gap_bytes = -44; /* Chop out the middle 44 rows */
} else {
new_multicut = MULTICUT_6x4X2;
new_h = 2498;
gap_bytes = 18;
}
new_w = 1920;
break;
case MULTICUT_6x4_5:
new_multicut = MULTICUT_6x4_5X2;
new_w = 1920;
new_h = 2802;
gap_bytes = 30;
break;
case MULTICUT_8x4:
new_multicut = MULTICUT_8x4X2;
new_w = 2560;
new_h = 2502;
gap_bytes = 30;
break;
case MULTICUT_8x5:
new_multicut = MULTICUT_8x5X2;
new_w = 2560;
new_h = 3102;
gap_bytes = 30;
break;
case MULTICUT_A4x5:
new_multicut = MULTICUT_A4x5X2;
new_w = 2560;
new_h = 3102;
gap_bytes = 30;
break;
case MULTICUT_A5:
new_multicut = MULTICUT_A5X2;
new_w = 2560;
new_h = 3598;
gap_bytes = 30;
break;
case MULTICUT_8x6:
new_multicut = MULTICUT_8x6X2;
new_w = 2560;
new_h = 3702;
gap_bytes = 30;
break;
default:
/* Everything else is NOT handled */
goto done;
}
gap_bytes *= new_w;
if (job1->dpi == 600) {
gap_bytes *= 2;
new_h *= 2;
}
DEBUG("Combining jobs to save media\n");
/* Okay, it's kosher to proceed */
newjob = malloc(sizeof(*newjob));
if (!newjob) {
ERROR("Memory allocation failure!\n");
goto done;
}
memcpy(newjob, job1, sizeof(*newjob));
newjob->databuf = malloc(((new_w*new_h+1024+54+10))*3+1024 + abs(gap_bytes));
newjob->datalen = 0;
newjob->multicut = new_multicut;
newjob->can_rewind = 0;
newjob->common.can_combine = 0;
if (!newjob->databuf) {
dnpds40_cleanup_job(newjob);
newjob = NULL;
ERROR("Memory allocation failure!\n");
goto done;
}
/* Copy data blocks from job1 */
uint8_t *ptr, *ptr2;
char buf[9];
ptr = job1->databuf;
while(ptr && ptr < (job1->databuf + job1->datalen)) {
int i;
buf[8] = 0;
memcpy(buf, ptr + 24, 8);
i = atoi(buf) + 32;
memcpy(newjob->databuf + newjob->datalen, ptr, i);
/* If we're on a plane data block... */
if (!memcmp("PLANE", newjob->databuf + newjob->datalen + 9, 5)) {
long planelen = (new_w * new_h) + 1088;
uint32_t newlen;
/* Fix up length in command */
snprintf(buf, sizeof(buf), "%08ld", planelen);
memcpy(newjob->databuf + newjob->datalen + 24, buf, 8);
/* Alter BMP header */
newlen = cpu_to_le32(planelen);
memcpy(newjob->databuf + newjob->datalen + 32 + 2, &newlen, 4);
/* alter DIB header */
newlen = cpu_to_le32(new_h);
memcpy(newjob->databuf + newjob->datalen + 32 + 22, &newlen, 4);
if (gap_bytes > 0) {
/* Insert gap/padding after first image */
memset(newjob->databuf + newjob->datalen + i, 0xff, gap_bytes);
newjob->datalen += gap_bytes;
} else {
// uint8_t *ptrA = newjob->databuf + newjob->datalen + 1088;
// /* Back off by 1/2 the gap */
// memmove(ptrA, ptrA - (gap_bytes / 2), (i - 1088) + gap_bytes/2);
/* And chop the end off by half the gap */
newjob->datalen += gap_bytes / 2;
}
/* Locate job2's PLANE -- Assume it's in the same place! */
ptr2 = job2->databuf + (ptr - job1->databuf);
/* Copy over job2's image data */
memcpy(newjob->databuf + newjob->datalen + i,
ptr2 + 32 + 1088, i - 32 - 1088);
if (gap_bytes < 0) {
uint8_t *ptrA = newjob->databuf + newjob->datalen + i;
/* Back off by 1/2 the gap */
memmove(ptrA, ptrA - (gap_bytes / 2), (i - 1088) + gap_bytes/2);
/* And chop the end off by half the gap */
newjob->datalen += gap_bytes / 2;
}
newjob->datalen += i - 32 - 1088; /* add in job2 length */
}
newjob->datalen += i;
ptr += i;
}
done:
return newjob;
}
#undef JOB_EQUIV
static void dnpds40_build_cmd(struct dnpds40_cmd *cmd, const char *arg1, const char *arg2, uint32_t arg3_len)
{
memset(cmd, 0x20, sizeof(*cmd));
cmd->esc = 0x1b;
cmd->p = 0x50;
memcpy(cmd->arg1, arg1, min(strlen(arg1), sizeof(cmd->arg1)));
memcpy(cmd->arg2, arg2, min(strlen(arg2), sizeof(cmd->arg2)));
if (arg3_len) {
char buf[11]; /* Extra padding to shut up GCC 10 */
snprintf(buf, sizeof(buf), "%08u", arg3_len);
memcpy(cmd->arg3, buf, 8);
}
}
static void dnpds40_cleanup_string(char *start, int len)
{
char *ptr = strchr(start, 0x0d);
if (ptr && (ptr - start < len)) {
*ptr = 0x00; /* If there is a <CR>, terminate there */
len = ptr - start;
} else {
start[--len] = 0x00; /* force null-termination */
}
/* Trim trailing spaces */
while (len && start[len-1] == ' ') {
start[--len] = 0;
}
}
static const char *dnpds40_printer_type(int type, int mfg)
{
switch(type) {
case P_DNP_DS40: return mfg == MFG_CITIZEN? "CX" : "DS40";
case P_DNP_DS80: return mfg == MFG_CITIZEN? "CW" : (mfg == MFG_MITSUBISHI ? "CP3800" : "DS80");
case P_DNP_DS80D: return "DS80DX";
case P_DNP_DSRX1: return mfg == MFG_CITIZEN ? "CY" : "DSRX1";
case P_DNP_DS620: return mfg == MFG_CITIZEN ? "CX-02" : "DS620";
case P_DNP_DS820: return mfg == MFG_CITIZEN ? "CX-02W" : "DS820";
case P_DNP_QW410: return mfg == MFG_CITIZEN ? "CZ-01" : "QW410";
case P_CITIZEN_CW01: return "CW01";
case P_CITIZEN_OP900II: return "CW-02 / OP900ii";
default: break;
}
return "Unknown";
}
static const char *dnpds40_media_types(int media, int sticker)
{
switch (media) {
case 100: return "UNKNOWN100"; // seen in driver dumps
case 110: return "UNKNOWN110"; // seen in driver dumps
case 150: return "4x6 (PC)";
case 151: return "4x8";
case 160: return "4.5x6";
case 161: return "4.5x8";
case 200: return sticker ? "5x3.5 (L): Sticker" : "5x3.5 (L)";
case 210: return sticker ? "5x7 (2L) Sticker" : "5x7 (2L)";
case 300: return sticker ? "6x4 (PC) Sticker" : "6x4 (PC)";
case 310: return sticker ? "6x8 (A5) Sticker" : "6x8 (A5)";
case 400: return sticker ? "6x9 (A5W) Sticker" : "6x9 (A5W)";
case 500: return "8x10";
case 510: return "8x12";
case 600: return "A4";
default:
break;
}
return "Unknown";
}
static const char *dnpds620_media_extension_code(int media)
{
switch (media) {
case 0: return "Normal Paper";
case 1: return "Sticker Paper";
case 99: return "Unknown Paper";
default:
break;
}
return "Unknown";
}
static const char *rfid_media_subtypes(int media)
{
switch (media) {
case 1: return "SD";
case 2: return "PD";
case 3: return "PP";
default:
break;
}
return "Unknown";
}
static const char *dnpds80_duplex_media_types(int media)
{
switch (media) {
case 100: return "8x10.75";
case 200: return "8x12";
default:
break;
}
return "Unknown";
}
#define DUPLEX_UNIT_PAPER_NONE 0
#define DUPLEX_UNIT_PAPER_PROTECTIVE 1
#define DUPLEX_UNIT_PAPER_PRESENT 2
static const char *dnpds80_duplex_paper_status(int media)
{
switch (media) {
case DUPLEX_UNIT_PAPER_NONE: return "No Paper";
case DUPLEX_UNIT_PAPER_PROTECTIVE: return "Protective Sheet";
case DUPLEX_UNIT_PAPER_PRESENT: return "Cut Paper Present";
default:
return "Unknown";
}
}
static const char *dnpds80_duplex_statuses(int status)
{
switch (status) {
case 5000: return "No Error";
case 5500: return "Duplex Unit Not Connected";
case 5017: return "Paper Jam: Supply Sensor On";
case 5018: return "Paper Jam: Supply Sensor Off";
case 5019: return "Paper Jam: Slot Sensor On";
case 5020: return "Paper Jam: Slot Sensor Off";
case 5021: return "Paper Jam: Pass Sensor On";
case 5022: return "Paper Jam: Pass Sensor Off";
case 5023: return "Paper Jam: Shell Sensor 1 On";
case 5024: return "Paper Jam: Shell Sensor 1 Off";
case 5025: return "Paper Jam: Shell Sensor 2 On";
case 5026: return "Paper Jam: Shell Sensor 2 Off";
case 5027: return "Paper Jam: Eject Sensor On";
case 5028: return "Paper Jam: Eject Sensor Off";
case 5029: return "Paper Jam: Slot FG Sensor";
case 5030: return "Paper Jam: Shell FG Sensor";
case 5033: return "Paper Supply Sensor Off";
case 5034: return "Printer Feed Slot Sensor Off";
case 5035: return "Pinch Pass Sensor Off";
case 5036: return "Shell Pass Sensor 1 Off";
case 5037: return "Shell Pass Sensor 2 Off";
case 5038: return "Eject Sensor Off";
case 5049: return "Capstan Drive Control Error";
case 5065: return "Shell Roller Error";
case 5081: return "Pinch Open Error";
case 5082: return "Pinch Close Error";
case 5083: return "Pinch Init Error";
case 5084: return "Pinch Position Error";
case 5097: return "Pass Guide Supply Error";
case 5098: return "Pass Guide Shell Error";
case 5099: return "Pass Guide Eject Error";
case 5100: return "Pass Guide Init Error";
case 5101: return "Pass Guide Position Error";
case 5113: return "Side Guide Home Error";
case 5114: return "Side Guide Position Error";
case 5115: return "Side Guide Init Error";
case 5129: return "Act Guide Home Error";
case 5145: return "Shell Rotate Home Error";
case 5146: return "Shell Rotate Rev Error";
case 5161: return "Paper Feed Lever Down Error";
case 5162: return "Paper Feed Lever Lock Error";
case 5163: return "Paper Feed Lever Up Error";
case 5177: return "Cutter Home Error";
case 5178: return "Cutter Away Error";
case 5179: return "Cutter Init Error";
case 5180: return "Cutter Position Error";
case 5193: return "Paper Tray Removed";
case 5209: return "Cover Opened";
case 5241: return "System Error";
default:
break;
}
return "Unknown Duplexer Error";
}
static const char *dnpds40_statuses(int status)
{
if (status >= 5000 && status <= 5999)
return dnpds80_duplex_statuses(status);
switch (status) {
case 0: return "Idle";
case 1: return "Printing";
case 500: return "Cooling Print Head";
case 510: return "Cooling Paper Motor";
case 900: return "Standby Mode";
case 1000: return "Cover Open";
case 1010: return "No Scrap Box";
case 1100: return "Paper End";
case 1200: return "Ribbon End";
case 1300: return "Paper Jam";
case 1400: return "Ribbon Error";
case 1500: return "Paper Definition Error";
case 1600: return "Data Error";
case 2000: return "Head Voltage Error";
case 2010: return "USB Power Supply Error";
case 2100: return "Head Position Error";
case 2200: return "Power Supply Fan Error";
case 2300: return "Cutter Error";
case 2400: return "Pinch Roller Error";
case 2500: return "Abnormal Head Temperature";
case 2600: return "Abnormal Media Temperature";
case 2610: return "Abnormal Paper Motor Temperature";
case 2700: return "Ribbon Tension Error";
case 2800: return "RF-ID Module Error";
case 2900: return "RS422 Communiation Error";
case 3000: return "System Error";
case 9999: return "Communication Failure"; /* Special */
default:
break;
}
return "Unknown Error";
}
static int dnpds40_do_cmd(struct dnpds40_ctx *ctx,
struct dnpds40_cmd *cmd,
uint8_t *data, int len)
{
int ret;
if ((ret = send_data(ctx->conn,
(uint8_t*)cmd, sizeof(*cmd))))
return ret;
if (data && len)
if ((ret = send_data(ctx->conn,
data, len)))
return ret;
return CUPS_BACKEND_OK;
}
static uint8_t *dnpds40_resp_cmd2(struct dnpds40_ctx *ctx,
struct dnpds40_cmd *cmd,
int *len,
uint8_t *buf, uint32_t buf_len)
{
char tmp[9];
uint8_t *respbuf;
int ret, i, num = 0;
memset(tmp, 0, sizeof(tmp));
if ((ret = dnpds40_do_cmd(ctx, cmd, buf, buf_len)))
return NULL;
/* Read in the response header */
ret = read_data(ctx->conn,
(uint8_t*)tmp, 8, &num);
if (ret < 0)
return NULL;
if (num != 8) {
ERROR("Short read! (%d/%d)\n", num, 8);
return NULL;
}
i = atoi(tmp); /* Length of payload in bytes, possibly padded */
respbuf = malloc(i + 1);
if (!respbuf) {
ERROR("Memory allocation failure (%d bytes)!\n", i);
return NULL;
}
respbuf[i] = 0; /* Explicitly null-pad */
/* Read in the actual response */
ret = read_data(ctx->conn,
respbuf, i, &num);
if (ret < 0) {
free(respbuf);
return NULL;
}
if (num != i) {
ERROR("Short read! (%d/%d)\n", num, i);
free(respbuf);
return NULL;
}
*len = num;
return respbuf;
}
#define dnpds40_resp_cmd(__ctx, __cmd, __len) dnpds40_resp_cmd2(__ctx, __cmd, __len, NULL, 0)
static int dnpds40_query_serno(struct dyesub_connection *conn, char *buf, int buf_len)
{
struct dnpds40_cmd cmd;
uint8_t *resp;
int len = 0;
struct dnpds40_ctx ctx = {
.conn = conn,
};
/* Get Serial Number */
dnpds40_build_cmd