| 1 | /* |
| 2 | * Mitsubishi P93D/P95D Monochrome Thermal Photo Printer CUPS backend |
| 3 | * |
| 4 | * (c) 2016-2025 Solomon Peachy <[email protected]> |
| 5 | * |
| 6 | * Development of this backend was sponsored by: |
| 7 | * |
| 8 | * A benefactor who wishes to remain anonymous |
| 9 | * |
| 10 | * The latest version of this program can be found at: |
| 11 | * |
| 12 | * https://git.shaftnet.org/gitea/slp/selphy_print.git |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify it |
| 15 | * under the terms of the GNU General Public License as published by the Free |
| 16 | * Software Foundation; either version 2 of the License, or (at your option) |
| 17 | * any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, but |
| 20 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 21 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 22 | * for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 26 | * |
| 27 | * SPDX-License-Identifier: GPL-2.0+ |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #define BACKEND mitsup95d_backend |
| 32 | |
| 33 | #include "backend_common.h" |
| 34 | |
| 35 | /* Private data structure */ |
| 36 | struct mitsup95d_printjob { |
| 37 | struct dyesub_job_common common; |
| 38 | |
| 39 | uint8_t *databuf; |
| 40 | uint32_t datalen; |
| 41 | |
| 42 | uint8_t hdr[2]; // 1b 51 |
| 43 | uint8_t hdr1[50]; // 1b 57 20 2e ... |
| 44 | uint8_t hdr2[50]; // 1b 57 21 2e ... |
| 45 | uint8_t hdr3[50]; // 1b 57 22 2e ... |
| 46 | uint8_t hdr4[42]; // 1b 58 ... |
| 47 | int hdr4_len; // 36 (P95) or 42 (P93) |
| 48 | |
| 49 | uint8_t plane[12]; // 1b 5a 74 00 ... |
| 50 | |
| 51 | uint8_t mem_clr[4]; // 1b 5a 43 00 |
| 52 | int mem_clr_present; |
| 53 | |
| 54 | uint8_t ftr[2]; |
| 55 | }; |
| 56 | |
| 57 | struct mitsup95d_ctx { |
| 58 | struct dyesub_connection *conn; |
| 59 | |
| 60 | char serno[STR_LEN_MAX + 1]; |
| 61 | |
| 62 | struct marker marker; |
| 63 | }; |
| 64 | |
| 65 | #define QUERYRESP_SIZE_MAX 9 |
| 66 | |
| 67 | static const char *mitsup93d_errors(uint8_t code) |
| 68 | { |
| 69 | switch (code) { |
| 70 | case 0x6f: return "Door Open"; |
| 71 | case 0x50: return "No Paper"; |
| 72 | default: return "Unknown Error"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static const char *mitsup95d_errors(uint8_t code) |
| 77 | { |
| 78 | switch (code & 0xf) { |
| 79 | case 3: return "Door Open"; |
| 80 | case 4: return "No Paper"; |
| 81 | default: return "Unknown Error"; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static void *mitsup95d_init(void) |
| 86 | { |
| 87 | struct mitsup95d_ctx *ctx = malloc(sizeof(struct mitsup95d_ctx)); |
| 88 | if (!ctx) { |
| 89 | ERROR("Memory Allocation Failure!\n"); |
| 90 | return NULL; |
| 91 | } |
| 92 | memset(ctx, 0, sizeof(struct mitsup95d_ctx)); |
| 93 | |
| 94 | return ctx; |
| 95 | } |
| 96 | |
| 97 | static int mitsup95d_get_status(struct mitsup95d_ctx *ctx, uint8_t *resp) |
| 98 | { |
| 99 | uint8_t querycmd[4] = { 0x1b, 0x72, 0x00, 0x00 }; |
| 100 | int ret; |
| 101 | int num; |
| 102 | |
| 103 | /* P93D is ... special. Windows switches to this halfway through |
| 104 | but it seems be okay to use it everywhere */ |
| 105 | if (ctx->conn->type == P_MITSU_P93D) { |
| 106 | querycmd[2] = 0x03; |
| 107 | } |
| 108 | |
| 109 | /* Query Status to sanity-check job */ |
| 110 | if ((ret = send_data(ctx->conn, |
| 111 | querycmd, sizeof(querycmd)))) |
| 112 | return CUPS_BACKEND_FAILED; |
| 113 | ret = read_data(ctx->conn, |
| 114 | resp, QUERYRESP_SIZE_MAX, &num); |
| 115 | |
| 116 | if (ret < 0) |
| 117 | return CUPS_BACKEND_FAILED; |
| 118 | if (ctx->conn->type == P_MITSU_P95D && num != 9) { |
| 119 | return CUPS_BACKEND_FAILED; |
| 120 | } else if (ctx->conn->type == P_MITSU_P93D && num != 8) { |
| 121 | return CUPS_BACKEND_FAILED; |
| 122 | } |
| 123 | return CUPS_BACKEND_OK; |
| 124 | } |
| 125 | |
| 126 | static int mitsup95d_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid) |
| 127 | { |
| 128 | struct mitsup95d_ctx *ctx = vctx; |
| 129 | |
| 130 | UNUSED(jobid); |
| 131 | |
| 132 | ctx->conn = conn; |
| 133 | |
| 134 | ctx->marker.color = "#000000"; /* Ie black! */ |
| 135 | ctx->marker.name = "Unknown"; |
| 136 | ctx->marker.numtype = -1; |
| 137 | ctx->marker.levelmax = CUPS_MARKER_UNAVAILABLE; |
| 138 | ctx->marker.levelnow = CUPS_MARKER_UNKNOWN; |
| 139 | |
| 140 | if (test_mode >= TEST_MODE_NOATTACH) |
| 141 | goto done; |
| 142 | |
| 143 | /* Query serial number */ |
| 144 | { |
| 145 | struct libusb_device_descriptor desc; |
| 146 | struct libusb_device *udev; |
| 147 | |
| 148 | udev = libusb_get_device(ctx->conn->dev); |
| 149 | libusb_get_device_descriptor(udev, &desc); |
| 150 | |
| 151 | if (!desc.iSerialNumber) { |
| 152 | WARNING("Printer configured for iSerial mode U0, so no serial number is reported.\n"); |
| 153 | } else { |
| 154 | libusb_get_string_descriptor_ascii(ctx->conn->dev, desc.iSerialNumber, (uint8_t*)ctx->serno, STR_LEN_MAX); |
| 155 | |
| 156 | if (strstr(ctx->serno, "000000")) { |
| 157 | WARNING("Printer configured for iSerial mode U2, reporting a fixed serial number of 000000\n"); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | done: |
| 163 | return CUPS_BACKEND_OK; |
| 164 | } |
| 165 | |
| 166 | static void mitsup95d_cleanup_job(const void *vjob) |
| 167 | { |
| 168 | const struct mitsup95d_printjob *job = vjob; |
| 169 | |
| 170 | if (job->databuf) |
| 171 | free(job->databuf); |
| 172 | |
| 173 | free((void*)job); |
| 174 | } |
| 175 | |
| 176 | static int mitsup95d_read_parse(void *vctx, const void **vjob, int data_fd, int copies) { |
| 177 | struct mitsup95d_ctx *ctx = vctx; |
| 178 | uint8_t buf[2]; /* Enough to read in any header */ |
| 179 | uint8_t tmphdr[50]; |
| 180 | uint8_t *ptr; |
| 181 | int i; |
| 182 | int remain; |
| 183 | int ptr_offset; |
| 184 | |
| 185 | struct mitsup95d_printjob *job = NULL; |
| 186 | |
| 187 | if (!ctx) |
| 188 | return CUPS_BACKEND_FAILED; |
| 189 | |
| 190 | job = malloc(sizeof(*job)); |
| 191 | if (!job) { |
| 192 | ERROR("Memory allocation failure!\n"); |
| 193 | return CUPS_BACKEND_RETRY_CURRENT; |
| 194 | } |
| 195 | memset(job, 0, sizeof(*job)); |
| 196 | job->common.jobsize = sizeof(*job); |
| 197 | |
| 198 | job->mem_clr_present = 0; |
| 199 | |
| 200 | top: |
| 201 | i = read(data_fd, buf, sizeof(buf)); |
| 202 | |
| 203 | if (i == 0) { |
| 204 | mitsup95d_cleanup_job(job); |
| 205 | return CUPS_BACKEND_CANCEL; |
| 206 | } |
| 207 | if (i < 0) { |
| 208 | mitsup95d_cleanup_job(job); |
| 209 | return CUPS_BACKEND_CANCEL; |
| 210 | } |
| 211 | if (buf[0] != 0x1b) { |
| 212 | ERROR("malformed data stream\n"); |
| 213 | mitsup95d_cleanup_job(job); |
| 214 | return CUPS_BACKEND_CANCEL; |
| 215 | } |
| 216 | |
| 217 | switch (buf[1]) { |
| 218 | case 0x50: /* Footer */ |
| 219 | remain = 2; |
| 220 | ptr = job->ftr; |
| 221 | break; |
| 222 | case 0x51: /* Job Header */ |
| 223 | remain = 2; |
| 224 | ptr = job->hdr; |
| 225 | break; |
| 226 | case 0x57: /* Geeneral headers */ |
| 227 | remain = sizeof(tmphdr); |
| 228 | ptr = tmphdr; |
| 229 | break; |
| 230 | case 0x58: /* User Comment */ |
| 231 | if (ctx->conn->type == P_MITSU_P93D) |
| 232 | job->hdr4_len = 42; |
| 233 | else |
| 234 | job->hdr4_len = 36; |
| 235 | remain = job->hdr4_len; |
| 236 | ptr = job->hdr4; |
| 237 | break; |
| 238 | case 0x5a: /* Plane header OR printer reset */ |
| 239 | // reset memory: 1b 5a 43 ... [len 04] |
| 240 | // plane header: 1b 5a 74 ... [len 12] |
| 241 | // Read in the minimum length, and clean it up later */ |
| 242 | ptr = tmphdr; |
| 243 | remain = 4; |
| 244 | break; |
| 245 | default: |
| 246 | ERROR("Unrecognized command! (%02x %02x)\n", buf[0], buf[1]); |
| 247 | mitsup95d_cleanup_job(job); |
| 248 | return CUPS_BACKEND_CANCEL; |
| 249 | } |
| 250 | |
| 251 | memcpy(ptr, buf, sizeof(buf)); |
| 252 | remain -= sizeof(buf); |
| 253 | ptr_offset = sizeof(buf); |
| 254 | |
| 255 | while (remain) { |
| 256 | i = read(data_fd, ptr + ptr_offset, remain); |
| 257 | if (i == 0) { |
| 258 | mitsup95d_cleanup_job(job); |
| 259 | return CUPS_BACKEND_CANCEL; |
| 260 | } |
| 261 | if (i < 0) { |
| 262 | mitsup95d_cleanup_job(job); |
| 263 | return CUPS_BACKEND_CANCEL; |
| 264 | } |
| 265 | remain -= i; |
| 266 | ptr_offset += i; |
| 267 | |
| 268 | /* Handle the ambiguous 0x5a block */ |
| 269 | if (buf[1] == 0x5a && remain == 0) { |
| 270 | if (tmphdr[2] == 0x74) { /* plane header */ |
| 271 | ptr = job->plane; |
| 272 | remain = 12 - ptr_offset; /* Finish reading */ |
| 273 | } else if (tmphdr[2] == 0x43) { /* reset memory */ |
| 274 | ptr = job->mem_clr; |
| 275 | job->mem_clr_present = 1; |
| 276 | remain = 4 - ptr_offset; |
| 277 | } |
| 278 | memcpy(ptr, tmphdr, ptr_offset); |
| 279 | buf[1] = 0xff; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if (ptr == tmphdr) { |
| 284 | if (tmphdr[3] != 46) { |
| 285 | ERROR("Unexpected header chunk: %02x %02x %02x %02x\n", |
| 286 | tmphdr[0], tmphdr[1], tmphdr[2], tmphdr[3]); |
| 287 | mitsup95d_cleanup_job(job); |
| 288 | return CUPS_BACKEND_CANCEL; |
| 289 | } |
| 290 | switch (tmphdr[2]) { |
| 291 | case 0x20: |
| 292 | ptr = job->hdr1; |
| 293 | break; |
| 294 | case 0x21: |
| 295 | ptr = job->hdr2; |
| 296 | break; |
| 297 | case 0x22: |
| 298 | ptr = job->hdr3; |
| 299 | break; |
| 300 | default: |
| 301 | WARNING("Unexpected header chunk: %02x %02x %02x %02x\n", |
| 302 | tmphdr[0], tmphdr[1], tmphdr[2], tmphdr[3]); |
| 303 | } |
| 304 | memcpy(ptr, tmphdr, sizeof(tmphdr)); |
| 305 | } else if (ptr == job->plane) { |
| 306 | uint16_t rows = job->plane[10] << 8 | job->plane[11]; |
| 307 | uint16_t cols = job->plane[8] << 8 | job->plane[9]; |
| 308 | |
| 309 | remain = rows * cols; |
| 310 | |
| 311 | /* Allocate buffer for the payload */ |
| 312 | job->datalen = 0; |
| 313 | job->databuf = malloc(remain); |
| 314 | if (!job->databuf) { |
| 315 | ERROR("Memory allocation failure!\n"); |
| 316 | mitsup95d_cleanup_job(job); |
| 317 | return CUPS_BACKEND_RETRY_CURRENT; |
| 318 | } |
| 319 | |
| 320 | /* Read it in */ |
| 321 | while (remain) { |
| 322 | i = read(data_fd, job->databuf + job->datalen, remain); |
| 323 | if (i == 0) { |
| 324 | mitsup95d_cleanup_job(job); |
| 325 | return CUPS_BACKEND_CANCEL; |
| 326 | } |
| 327 | if (i < 0) { |
| 328 | mitsup95d_cleanup_job(job); |
| 329 | return CUPS_BACKEND_CANCEL; |
| 330 | } |
| 331 | remain -= i; |
| 332 | job->datalen += i; |
| 333 | } |
| 334 | } else if (ptr == job->ftr) { |
| 335 | |
| 336 | /* Update unknown header field to match sniffs */ |
| 337 | if (ctx->conn->type == P_MITSU_P95D) { |
| 338 | if (job->hdr1[18] == 0x00) |
| 339 | job->hdr1[18] = 0x01; |
| 340 | } |
| 341 | |
| 342 | /* Update printjob header to reflect number of requested copies */ |
| 343 | if (job->hdr2[13] != 0xff) { |
| 344 | /* Use larger of our copy counts */ |
| 345 | if (copies > job->hdr2[13]) |
| 346 | job->hdr2[13] = copies; |
| 347 | else |
| 348 | copies = job->hdr2[13]; |
| 349 | } |
| 350 | job->common.copies = copies; |
| 351 | |
| 352 | *vjob = job; |
| 353 | return CUPS_BACKEND_OK; |
| 354 | } |
| 355 | |
| 356 | goto top; |
| 357 | } |
| 358 | |
| 359 | static int mitsup95d_main_loop(void *vctx, const void *vjob, int wait_for_return) { |
| 360 | struct mitsup95d_ctx *ctx = vctx; |
| 361 | uint8_t queryresp[QUERYRESP_SIZE_MAX]; |
| 362 | int ret; |
| 363 | |
| 364 | const struct mitsup95d_printjob *job = vjob; |
| 365 | |
| 366 | if (!ctx) |
| 367 | return CUPS_BACKEND_FAILED; |
| 368 | if (!job) |
| 369 | return CUPS_BACKEND_FAILED; |
| 370 | |
| 371 | |
| 372 | INFO("Waiting for printer idle\n"); |
| 373 | |
| 374 | /* Query Status to make sure printer is idle */ |
| 375 | do { |
| 376 | ret = mitsup95d_get_status(ctx, queryresp); |
| 377 | if (ret) |
| 378 | return ret; |
| 379 | |
| 380 | if (ctx->conn->type == P_MITSU_P95D) { |
| 381 | if (queryresp[6] & 0x40) { |
| 382 | INFO("Printer Status: %s (%02x)\n", mitsup95d_errors(queryresp[6]), queryresp[6]); |
| 383 | return CUPS_BACKEND_STOP; |
| 384 | } |
| 385 | if (queryresp[5] == 0x00) |
| 386 | break; |
| 387 | } else { |
| 388 | if (queryresp[6] == 0x45) { |
| 389 | ERROR("Printer error %02x\n", queryresp[7]); |
| 390 | return CUPS_BACKEND_STOP; |
| 391 | } |
| 392 | if (queryresp[6] == 0x30) |
| 393 | break; |
| 394 | } |
| 395 | |
| 396 | sleep(1); |
| 397 | } while (1); |
| 398 | |
| 399 | INFO("Sending print job\n"); |
| 400 | |
| 401 | /* Send over Memory Clear, if present */ |
| 402 | if (job->mem_clr_present) { |
| 403 | if ((ret = send_data(ctx->conn, |
| 404 | job->mem_clr, sizeof(job->mem_clr)))) |
| 405 | return CUPS_BACKEND_FAILED; |
| 406 | } |
| 407 | |
| 408 | /* Send Job Start */ |
| 409 | if ((ret = send_data(ctx->conn, |
| 410 | job->hdr, sizeof(job->hdr)))) |
| 411 | return CUPS_BACKEND_FAILED; |
| 412 | |
| 413 | /* Send over headers */ |
| 414 | if ((ret = send_data(ctx->conn, |
| 415 | job->hdr1, sizeof(job->hdr1)))) |
| 416 | return CUPS_BACKEND_FAILED; |
| 417 | if ((ret = send_data(ctx->conn, |
| 418 | job->hdr2, sizeof(job->hdr2)))) |
| 419 | return CUPS_BACKEND_FAILED; |
| 420 | if ((ret = send_data(ctx->conn, |
| 421 | job->hdr3, sizeof(job->hdr3)))) |
| 422 | return CUPS_BACKEND_FAILED; |
| 423 | if ((ret = send_data(ctx->conn, |
| 424 | job->hdr4, job->hdr4_len))) |
| 425 | return CUPS_BACKEND_FAILED; |
| 426 | |
| 427 | /* Send plane header and image data */ |
| 428 | if ((ret = send_data(ctx->conn, |
| 429 | job->plane, sizeof(job->plane)))) |
| 430 | return CUPS_BACKEND_FAILED; |
| 431 | if ((ret = send_data(ctx->conn, |
| 432 | job->databuf, job->datalen))) |
| 433 | return CUPS_BACKEND_FAILED; |
| 434 | |
| 435 | /* Query Status to sanity-check job */ |
| 436 | ret = mitsup95d_get_status(ctx, queryresp); |
| 437 | if (ret) |
| 438 | return ret; |
| 439 | |
| 440 | if (ctx->conn->type == P_MITSU_P95D) { |
| 441 | if (queryresp[6] & 0x40) { |
| 442 | INFO("Printer Status: %s (%02x)\n", mitsup95d_errors(queryresp[6]), queryresp[6]); |
| 443 | return CUPS_BACKEND_STOP; |
| 444 | } |
| 445 | if (queryresp[5] != 0x00) { |
| 446 | ERROR("Printer not ready (%02x)!\n", queryresp[5]); |
| 447 | return CUPS_BACKEND_CANCEL; |
| 448 | } |
| 449 | } else { |
| 450 | if (queryresp[6] == 0x45) { |
| 451 | INFO("Printer Status: %s (%02x)\n", mitsup93d_errors(queryresp[7]), queryresp[7]); |
| 452 | return CUPS_BACKEND_STOP; |
| 453 | } |
| 454 | if (queryresp[6] != 0x30) { |
| 455 | ERROR("Printer not ready (%02x)!\n", queryresp[6]); |
| 456 | return CUPS_BACKEND_CANCEL; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* Send over Footer */ |
| 461 | if ((ret = send_data(ctx->conn, |
| 462 | job->ftr, sizeof(job->ftr)))) |
| 463 | return CUPS_BACKEND_FAILED; |
| 464 | |
| 465 | INFO("Waiting for completion\n"); |
| 466 | |
| 467 | /* Query status until we're done.. */ |
| 468 | do { |
| 469 | sleep(1); |
| 470 | |
| 471 | /* Query Status */ |
| 472 | ret = mitsup95d_get_status(ctx, queryresp); |
| 473 | if (ret) |
| 474 | return ret; |
| 475 | |
| 476 | if (ctx->conn->type == P_MITSU_P95D) { |
| 477 | if (queryresp[6] & 0x40) { |
| 478 | INFO("Printer Status: %s (%02x)\n", mitsup95d_errors(queryresp[6]), queryresp[6]); |
| 479 | return CUPS_BACKEND_STOP; |
| 480 | } |
| 481 | if (queryresp[5] == 0x00) |
| 482 | break; |
| 483 | |
| 484 | if (queryresp[7] > 0) { |
| 485 | if (!wait_for_return) { |
| 486 | INFO("Fast return mode enabled.\n"); |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | } else { |
| 491 | if (queryresp[6] == 0x45) { |
| 492 | INFO("Printer Status: %s (%02x)\n", mitsup93d_errors(queryresp[7]), queryresp[7]); |
| 493 | return CUPS_BACKEND_STOP; |
| 494 | } |
| 495 | if (queryresp[6] == 0x30) |
| 496 | break; |
| 497 | if (queryresp[6] == 0x43 && queryresp[7] > 0) { |
| 498 | if (!wait_for_return) { |
| 499 | INFO("Fast return mode enabled.\n"); |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } while(1); |
| 505 | |
| 506 | INFO("Print complete\n"); |
| 507 | return CUPS_BACKEND_OK; |
| 508 | } |
| 509 | |
| 510 | static int mitsup95d_dump_status(struct mitsup95d_ctx *ctx) |
| 511 | { |
| 512 | uint8_t queryresp[QUERYRESP_SIZE_MAX]; |
| 513 | int ret; |
| 514 | |
| 515 | ret = mitsup95d_get_status(ctx, queryresp); |
| 516 | if (ret) |
| 517 | return ret; |
| 518 | if (!ctx->serno[0]) |
| 519 | INFO("iSerial mode: Disasbled (U0)\n"); |
| 520 | else if (strstr(ctx->serno, "000000")) |
| 521 | INFO("iSerial Mode: Force 000000 (U2)\n"); |
| 522 | else { |
| 523 | INFO("iSerial Mode: Enabled (U1)\n"); |
| 524 | INFO("Serial Number: %s\n", ctx->serno); |
| 525 | } |
| 526 | |
| 527 | if (ctx->conn->type == P_MITSU_P95D) { |
| 528 | if (queryresp[6] & 0x40) { |
| 529 | INFO("Printer Status: %s (%02x)\n", mitsup95d_errors(queryresp[6]), queryresp[6]); |
| 530 | } else if (queryresp[5] == 0x00) { |
| 531 | INFO("Printer Status: Idle\n"); |
| 532 | } else if (queryresp[5] == 0x02 && queryresp[7] > 0) { |
| 533 | INFO("Printer Status: Printing (%d) copies remaining\n", queryresp[7]); |
| 534 | } |
| 535 | } else { |
| 536 | if (queryresp[6] == 0x45) { |
| 537 | INFO("Printer Status: %s (%02x)\n", mitsup93d_errors(queryresp[7]), queryresp[7]); |
| 538 | } else if (queryresp[6] == 0x30) { |
| 539 | INFO("Printer Status: Idle\n"); |
| 540 | } else if (queryresp[6] == 0x43 && queryresp[7] > 0) { |
| 541 | INFO("Printer Status: Printing (%d) copies remaining\n", queryresp[7]); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return CUPS_BACKEND_OK; |
| 546 | } |
| 547 | |
| 548 | static void mitsup95d_cmdline(void) |
| 549 | { |
| 550 | DEBUG("\t\t[ -s ] # Query status\n"); |
| 551 | } |
| 552 | |
| 553 | static int mitsup95d_cmdline_arg(void *vctx, int argc, char **argv) |
| 554 | { |
| 555 | struct mitsup95d_ctx *ctx = vctx; |
| 556 | int i, j = 0; |
| 557 | |
| 558 | if (!ctx) |
| 559 | return -1; |
| 560 | |
| 561 | while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "s")) >= 0) { |
| 562 | switch(i) { |
| 563 | GETOPT_PROCESS_GLOBAL |
| 564 | case 's': |
| 565 | j = mitsup95d_dump_status(ctx); |
| 566 | break; |
| 567 | default: |
| 568 | break; /* Ignore completely */ |
| 569 | } |
| 570 | |
| 571 | if (j) return j; |
| 572 | } |
| 573 | |
| 574 | return CUPS_BACKEND_OK; |
| 575 | } |
| 576 | |
| 577 | static int mitsup95d_query_markers(void *vctx, struct marker **markers, int *count) |
| 578 | { |
| 579 | struct mitsup95d_ctx *ctx = vctx; |
| 580 | uint8_t queryresp[QUERYRESP_SIZE_MAX]; |
| 581 | |
| 582 | if (mitsup95d_get_status(ctx, queryresp)) |
| 583 | return CUPS_BACKEND_FAILED; |
| 584 | |
| 585 | ctx->marker.levelnow = CUPS_MARKER_UNKNOWN_OK; |
| 586 | |
| 587 | if (ctx->conn->type == P_MITSU_P95D) { |
| 588 | if (queryresp[6] & 0x40) { |
| 589 | ctx->marker.levelnow = 0; |
| 590 | } |
| 591 | } else { |
| 592 | if (queryresp[6] == 0x45) { |
| 593 | ctx->marker.levelnow = 0; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | *markers = &ctx->marker; |
| 598 | *count = 1; |
| 599 | |
| 600 | return CUPS_BACKEND_OK; |
| 601 | } |
| 602 | |
| 603 | static const char *mitsup95d_prefixes[] = { |
| 604 | "mitsup9x", // Family driver name |
| 605 | // backwards compatibility |
| 606 | "mitsup95d", "mitsup93d", |
| 607 | NULL |
| 608 | }; |
| 609 | |
| 610 | static const struct device_id mitsup95d_devices[] = { |
| 611 | { 0x06d3, 0x0398, P_MITSU_P93D, NULL, "mitsubishi-p93d"}, |
| 612 | { 0x06d3, 0x3b10, P_MITSU_P95D, NULL, "mitsubishi-p95d"}, |
| 613 | { 0, 0, 0, NULL, NULL} |
| 614 | }; |
| 615 | |
| 616 | /* Exported */ |
| 617 | const struct dyesub_backend mitsup95d_backend = { |
| 618 | .name = "Mitsubishi P93D/P95D", |
| 619 | .version = "0.17", |
| 620 | .uri_prefixes = mitsup95d_prefixes, |
| 621 | .devices = mitsup95d_devices, |
| 622 | .cmdline_arg = mitsup95d_cmdline_arg, |
| 623 | .cmdline_usage = mitsup95d_cmdline, |
| 624 | .init = mitsup95d_init, |
| 625 | .attach = mitsup95d_attach, |
| 626 | .cleanup_job = mitsup95d_cleanup_job, |
| 627 | .read_parse = mitsup95d_read_parse, |
| 628 | .main_loop = mitsup95d_main_loop, |
| 629 | .query_markers = mitsup95d_query_markers, |
| 630 | }; |
| 631 | |
| 632 | /***************************************************** |
| 633 | |
| 634 | Mitsubishi P93D/P95D Spool Format |
| 635 | |
| 636 | ...All fields are BIG ENDIAN. |
| 637 | |
| 638 | MEMORY_CLEAR (optional) |
| 639 | |
| 640 | 1b 5a 43 00 |
| 641 | |
| 642 | JOB_HDR |
| 643 | |
| 644 | 1b 51 |
| 645 | |
| 646 | PRINT_SETUP |
| 647 | |
| 648 | 1b 57 20 2e 00 0a 00 ZZ 00 00 00 00 00 00 CC CC |
| 649 | RR RR XX 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 650 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 651 | 00 00 |
| 652 | |
| 653 | XX == 01 seen in sniffs, 00 seen in dumps. Unknown purpose. |
| 654 | ZZ == 00 on P93D, 02 on P95D |
| 655 | |
| 656 | CC CC = columns, RR RR = rows (print dimensions) |
| 657 | |
| 658 | PRINT_OPTIONS |
| 659 | |
| 660 | P95: |
| 661 | |
| 662 | 1b 57 21 2e 00 4a aa 00 20 TT 00 00 64 NN 00 MM |
| 663 | [[ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 664 | 00 ]] 00 00 00 02 00 00 00 00 00 00 00 00 00 00 |
| 665 | 00 XY |
| 666 | |
| 667 | P93: |
| 668 | |
| 669 | 1b 57 21 2e 00 4a aa 00 00 TT 00 00 00 NN 00 MM |
| 670 | [[ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 671 | 00 ]] 00 00 00 02 00 00 00 00 00 00 00 00 00 00 |
| 672 | 00 XY |
| 673 | |
| 674 | NN = copies |
| 675 | 1..200 |
| 676 | 0xff (continuous print) |
| 677 | MM = comment type |
| 678 | 00 = None |
| 679 | 01 = Printer Setting |
| 680 | 02 = Date |
| 681 | 03 = DateTime |
| 682 | [[ .. ]] = actual comment (18 bytes), see below. |
| 683 | |
| 684 | TT = media type |
| 685 | |
| 686 | P95D: |
| 687 | |
| 688 | 00 = Standard |
| 689 | 01 = High Density |
| 690 | 02 = High Glossy |
| 691 | 03 = High Glossy (K95HG) |
| 692 | |
| 693 | P93D: |
| 694 | |
| 695 | 00 = High Density |
| 696 | 01 = High Glossy |
| 697 | 02 = Standard |
| 698 | |
| 699 | X = media cut length (P95D ONLY. P93 is 0) |
| 700 | 4..8 (mm) |
| 701 | Y = flags |
| 702 | 0x04 = Paper save |
| 703 | 0x03 = Buzzer (3 = high, 2 = low, 0 = off) |
| 704 | |
| 705 | GAMMA (P95) |
| 706 | |
| 707 | 1b 57 22 2e 00 15 TT 00 00 00 00 00 LL BB CC 00 |
| 708 | [[ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 709 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 710 | 00 ]] |
| 711 | |
| 712 | LL = Gamma table |
| 713 | 00 = Printer Setting |
| 714 | 01..05 Gamma table 1..5 |
| 715 | 10 = Use LUT |
| 716 | BB = Brightness (signed 8-bit) |
| 717 | CC = Contrast (signed 8-bit) |
| 718 | TT = Table present |
| 719 | 00 = No |
| 720 | 01 = Yes |
| 721 | [[ .. ]] = Gamma table, loaded from LUT on disk. (skip first 16 bytes) |
| 722 | |
| 723 | GAMMA (P93) |
| 724 | |
| 725 | 1b 57 22 2e 00 d5 00 00 00 00 00 00 SS 00 LL 00 |
| 726 | BB 00 CC 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 727 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
| 728 | 00 00 |
| 729 | |
| 730 | SS = Sharpening (0 = low, 1 = normal, 2 = high) |
| 731 | LL = Gamma table |
| 732 | 00..04 Gamma table 1..5 |
| 733 | BB = Brightness (signed 8-bit) |
| 734 | CC = Contrast (signed 8-bit) |
| 735 | |
| 736 | USER_COMMENT (P95) |
| 737 | |
| 738 | 1b 58 [[ 20 20 20 20 20 20 20 20 20 20 20 20 20 |
| 739 | 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |
| 740 | 20 20 20 ]] |
| 741 | |
| 742 | [[ .. ]] = Actual comment. 34 bytes payload, 0x20 -> 0x7e |
| 743 | (Null terminated?) |
| 744 | |
| 745 | USER_COMMENT (P93) |
| 746 | |
| 747 | 1b 58 [[ 20 20 20 20 20 20 20 20 20 20 20 20 20 |
| 748 | 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |
| 749 | 20 20 20 20 20 20 20 20 20 20 ]] |
| 750 | |
| 751 | [[ .. ]] = Actual comment. 40 bytes payload, 0x20 -> 0x7e |
| 752 | (Null terminated?) |
| 753 | |
| 754 | IMAGE_DATA |
| 755 | |
| 756 | 1b 5a 74 00 00 00 YY YY CC CC RR RR |
| 757 | [[ .. data ... ]] |
| 758 | |
| 759 | CC CC = columns |
| 760 | RR CC = rows |
| 761 | YY YY = row offset |
| 762 | |
| 763 | Followed by C*R bytes of monochrome data, 0xff = white, 0x00 = black |
| 764 | |
| 765 | PRINT_START |
| 766 | |
| 767 | 1b 50 |
| 768 | |
| 769 | ********************************* |
| 770 | |
| 771 | P95D Printer Comms: |
| 772 | |
| 773 | STATUS query |
| 774 | |
| 775 | -> 1b 72 00 00 |
| 776 | <- e4 72 00 00 04 XX ZZ YY 00 |
| 777 | |
| 778 | YY == remaining copies |
| 779 | XX == Status? |
| 780 | 00 == Idle |
| 781 | 02 == Printing |
| 782 | ZZ == Error! |
| 783 | 00 == None |
| 784 | 43 == Door open |
| 785 | 44 == No Paper |
| 786 | 4? == "Button" |
| 787 | 4? == "Gear Lock" |
| 788 | 4? == Head Up |
| 789 | ^ |
| 790 | \--- 0x40 appears to be a flag that indicates error. |
| 791 | |
| 792 | P93D Printer Comms: |
| 793 | |
| 794 | STATUS query |
| 795 | |
| 796 | -> 1b 72 0? 00 |
| 797 | <- e4 72 0? 00 03 XX YY ZZ |
| 798 | |
| 799 | ? could be 0x00 or 0x03. Seen both. |
| 800 | |
| 801 | Seen: 30 30 30 |
| 802 | 30 43 01 <- 1 copies remaining |
| 803 | 30 43 00 <- 0 copies remaining |
| 804 | ^^ |
| 805 | \-- 30 == idle, 43 == printing |
| 806 | |
| 807 | 30 45 6f <- door open |
| 808 | 30 45 50 <- no paper |
| 809 | |
| 810 | 45 == error? |
| 811 | **************************** |
| 812 | |
| 813 | UNKNOWNS: |
| 814 | |
| 815 | * How multiple images are stacked for printing on a single page |
| 816 | (col offset too? write four, then tell PRINT?) |
| 817 | * How to adjust P95D printer sharpness? |
| 818 | * What "custom gamma" table does to spool file? |
| 819 | |
| 820 | */ |