Debian Codegraph @

src/cups/backend_kodak6800.c

raw symbols SCIP
1 /*
2 * Kodak 6800/6850 Photo Printer CUPS backend
3 *
4 * (c) 2013-2024 Solomon Peachy <[email protected]>
5 *
6 * Development of this backend was sponsored by:
7 *
8 * LiveLink Technology [ www.livelinktechnology.net ]
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 kodak6800_backend
32
33 #include "backend_common.h"
34 #include "backend_sinfonia.h"
35
36 /* File header */
37 struct kodak6800_hdr {
38 uint8_t hdr[7]; /* Always 03 1b 43 48 43 0a 00 */
39 uint8_t jobid; /* Non-zero */
40 uint16_t copies; /* BE, in BCD format (1-9999) */
41 uint16_t columns; /* BE */
42 uint16_t rows; /* BE */
43 uint8_t size; /* media size; 0x06 for 6x8, 0x00 for 6x4, 0x07 for 5x7 */
44 uint8_t laminate; /* 0x01 to laminate, 0x00 for not */
45 uint8_t method; /* 0x00 or 0x01 (for 4x6 on 6x8 media), 0x21 for 2x6, 0x23 for 3x6 */
46 } __attribute__((packed));
47
48 struct kodak68x0_status_readback {
49 uint8_t hdr; /* Always 01 */
50 uint8_t status; /* STATUS_* */
51 uint8_t status1; /* STATUS1_* */
52 uint32_t status2; /* STATUS2_* */
53 uint8_t errcode; /* Error ## */
54 uint32_t lifetime; /* Lifetime Prints (BE) */
55 uint32_t maint; /* Maint Prints (BE) */
56 uint32_t media; /* Media Prints (6850), Unknown (6800) (BE) */
57 uint32_t cutter; /* Cutter Actuations (BE) */
58 uint8_t nullB[2];
59 uint8_t errtype; /* seen 0x00 or 0xd0 */
60 uint8_t donor; /* Percentage, 0-100 */
61 uint16_t main_boot; /* Always 003 */
62 uint16_t main_fw; /* seen 6xx/8xx (6850) and 2xx/3xx/4xx (6800) */
63 uint16_t dsp_boot; /* Always 001 */
64 uint16_t dsp_fw; /* Seen 5xx (6850) and 1xx (6800) */
65 uint8_t b1_jobid;
66 uint8_t b2_jobid;
67 uint16_t b1_remain; /* Remaining prints in job */
68 uint16_t b1_complete; /* Completed prints in job */
69 uint16_t b1_total; /* Total prints in job */
70 uint16_t b2_remain; /* Remaining prints in job */
71 uint16_t b2_complete; /* Completed prints in job */
72 uint16_t b2_total; /* Total prints in job */
73 uint8_t curve_status; /* Always seems to be 0x00 */
74 } __attribute__((packed));
75
76 #define MAX_MEDIAS 16
77
78 struct kodak68x0_media_readback {
79 uint8_t hdr; /* Always 0x01 */
80 uint8_t type; /* Media code, KODAK68x0_MEDIA_xxx */
81 uint8_t null[5];
82 uint8_t count; /* Always 0x04 (6800) or 0x06 (6850)? */
83 struct sinfonia_mediainfo_item sizes[];
84 } __attribute__((packed));
85
86 #define CMDBUF_LEN 17
87
88 /* Private data structure */
89 struct kodak6800_ctx {
90 struct dyesub_connection *conn;
91
92 int supports_sub4x6;
93
94 uint8_t jobid;
95
96 struct sinfonia_mediainfo_item sizes[MAX_MEDIAS];
97 uint8_t media_count;
98 uint8_t media_type;
99
100 struct kodak68x0_status_readback sts;
101
102 char serial[32];
103 char fwver[32];
104
105 struct marker marker;
106 };
107
108 /* Baseline commands */
109 static int kodak6800_do_cmd(struct kodak6800_ctx *ctx,
110 const void *cmd, int cmd_len,
111 void *resp, int resp_len,
112 int *actual_len)
113 {
114 int ret;
115
116 /* Write command */
117 if ((ret = send_data(ctx->conn,
118 cmd, cmd_len)))
119 return (ret < 0) ? ret : -99;
120
121 /* Read response */
122 ret = read_data(ctx->conn,
123 resp, resp_len, actual_len);
124 if (ret < 0)
125 return ret;
126
127 return CUPS_BACKEND_OK;
128 }
129
130 static void kodak68x0_dump_mediainfo(struct sinfonia_mediainfo_item *sizes,
131 uint8_t media_count, uint8_t media_type)
132 {
133 int i;
134
135 if (media_type == KODAK6_MEDIA_NONE) {
136 INFO("No Media Loaded\n");
137 return;
138 }
139 kodak6_dumpmediacommon(media_type);
140
141 INFO("Legal print sizes:\n");
142 for (i = 0 ; i < media_count ; i++) {
143 INFO("\t%d: %dx%d (%02x)\n", i,
144 sizes[i].columns,
145 sizes[i].rows,
146 sizes[i].method);
147 }
148 INFO("\n");
149 }
150
151 static void kodak6800_fillhdr(uint8_t *req, uint8_t cmd)
152 {
153 req[0] = 0x03; /* CMD type */
154 req[1] = 0x1b; /* ESC */
155 req[2] = 0x43; /* C */
156 req[3] = 0x48; /* H */
157 req[4] = 0x43; /* C */
158 req[5] = cmd; /* Commmand code */
159 }
160
161 #define MAX_MEDIA_LEN (sizeof(struct kodak68x0_media_readback) + MAX_MEDIAS * sizeof(struct sinfonia_mediainfo_item))
162
163 static int kodak6800_get_mediainfo(struct kodak6800_ctx *ctx)
164 {
165 struct kodak68x0_media_readback *media;
166 uint8_t req[16];
167 int ret, num, i, j;
168
169 memset(req, 0, sizeof(req));
170 media = malloc(MAX_MEDIA_LEN);
171 if (!media) {
172 ERROR("Memory allocation failure!\n");
173 return CUPS_BACKEND_RETRY_CURRENT;
174 }
175
176 for (j = 0 ; j < 2 ; j ++) {
177 memset(media, 0, sizeof(*media));
178 kodak6800_fillhdr(req, 0x1a);
179 req[6] = j;
180
181 /* Issue command and get response */
182 if ((ret = kodak6800_do_cmd(ctx, req, sizeof(req),
183 media, MAX_MEDIA_LEN,
184 &num))) {
185 free(media);
186 return ret;
187 }
188
189 /* Validate proper response */
190 if (media->hdr != CMD_CODE_OK ||
191 media->null[0] != 0x00) {
192 ERROR("Unexpected response from media query!\n");
193 free(media);
194 return CUPS_BACKEND_STOP;
195 }
196 ctx->media_type = media->type;
197
198 for (i = 0; i < media->count ; i++) {
199 memcpy(&ctx->sizes[ctx->media_count], &media->sizes[i], sizeof(struct sinfonia_mediainfo_item));
200 ctx->sizes[ctx->media_count].rows = be16_to_cpu(ctx->sizes[ctx->media_count].rows);
201 ctx->sizes[ctx->media_count].columns = be16_to_cpu(ctx->sizes[ctx->media_count].columns);
202 ctx->media_count++;
203 }
204 if (i < 6)
205 break;
206 }
207
208 free(media);
209 return CUPS_BACKEND_OK;
210 }
211
212 static int kodak68x0_canceljob(struct kodak6800_ctx *ctx,
213 int id)
214 {
215 uint8_t req[16];
216 int ret, num;
217
218 memset(req, 0, sizeof(req));
219 kodak6800_fillhdr(req, 0x13);
220 req[6] = id;
221
222 /* Issue command and get response */
223 if ((ret = kodak6800_do_cmd(ctx, req, sizeof(req),
224 &ctx->sts, sizeof(ctx->sts),
225 &num)))
226 return ret;
227
228 /* Validate proper response */
229 if (ctx->sts.hdr != CMD_CODE_OK) {
230 ERROR("Unexpected response from job cancel!\n");
231 return -99;
232 }
233
234 return CUPS_BACKEND_OK;
235 }
236
237 static int kodak68x0_reset(struct kodak6800_ctx *ctx)
238 {
239 uint8_t req[16];
240 int ret, num;
241
242 memset(req, 0, sizeof(req));
243 kodak6800_fillhdr(req, 0xc0);
244
245 /* Issue command and get response */
246 if ((ret = kodak6800_do_cmd(ctx, req, sizeof(req),
247 &ctx->sts, sizeof(ctx->sts),
248 &num)))
249 return ret;
250
251 /* Validate proper response */
252 if (ctx->sts.hdr != CMD_CODE_OK) {
253 ERROR("Unexpected response from job cancel!\n");
254 return -99;
255 }
256
257 return CUPS_BACKEND_OK;
258 }
259
260 static void kodak68x0_dump_status(struct kodak6800_ctx *ctx, struct kodak68x0_status_readback *status)
261 {
262 const char *detail;
263
264 switch (status->status) {
265 case STATUS_PRINTING:
266 detail = "Printing";
267 break;
268 case STATUS_IDLE:
269 detail = "Idle";
270 break;
271 default:
272 detail = "Unknown";
273 break;
274 }
275 INFO("Printer Status : %s\n", detail);
276
277 INFO("Printer State : %s # %02x %08x %02x\n",
278 sinfonia_1x45_status_str(status->status1, status->status2, status->errcode),
279 status->status1, status->status2, status->errcode);
280
281 INFO("Bank 1 ID: %u\n", status->b1_jobid);
282 INFO("\tPrints: %d/%d complete\n",
283 be16_to_cpu(status->b1_complete), be16_to_cpu(status->b1_total));
284 INFO("Bank 2 ID: %u\n", status->b2_jobid);
285 INFO("\tPrints: %d/%d complete\n",
286 be16_to_cpu(status->b2_complete), be16_to_cpu(status->b2_total));
287
288 switch (status->curve_status) {
289 case CURVE_TABLE_STATUS_INITIAL:
290 detail = "Initial/Default";
291 break;
292 case CURVE_TABLE_STATUS_USERSET:
293 detail = "User Stored";
294 break;
295 case CURVE_TABLE_STATUS_CURRENT:
296 detail = "Current";
297 break;
298 default:
299 detail = "Unknown";
300 break;
301 }
302 INFO("Tone Curve Status: %s\n", detail);
303
304 INFO("Counters:\n");
305 INFO("\tLifetime : %u\n", be32_to_cpu(status->lifetime));
306 INFO("\tThermal Head : %u\n", be32_to_cpu(status->maint));
307 INFO("\tCutter : %u\n", be32_to_cpu(status->cutter));
308
309 if (ctx->conn->type == P_KODAK_6850) {
310 int max = kodak6_mediamax(ctx->media_type);
311
312 INFO("\tMedia : %u\n", be32_to_cpu(status->media));
313
314 if (max) {
315 INFO("\t Remaining : %u\n", max - be32_to_cpu(status->media));
316 } else {
317 INFO("\t Remaining : Unknown\n");
318 }
319 }
320 INFO("Main FW version : %d\n", be16_to_cpu(status->main_fw));
321 INFO("DSP FW version : %d\n", be16_to_cpu(status->dsp_fw));
322 INFO("Donor : %u%%\n", status->donor);
323 INFO("\n");
324 }
325
326 static int kodak6800_get_status(struct kodak6800_ctx *ctx,
327 struct kodak68x0_status_readback *status)
328 {
329 uint8_t req[16];
330 int ret, num;
331
332 memset(req, 0, sizeof(req));
333 memset(status, 0, sizeof(*status));
334
335 kodak6800_fillhdr(req, 0x03);
336
337 /* Issue command and get response */
338 if ((ret = kodak6800_do_cmd(ctx, req, sizeof(req),
339 status, sizeof(*status),
340 &num)))
341 return ret;
342
343 /* Validate proper response */
344 if (status->hdr != CMD_CODE_OK) {
345 ERROR("Unexpected response from status query!\n");
346 return -99;
347 }
348
349 /* Byteswap important stuff */
350 status->status2 = be32_to_cpu(status->status2);
351
352 return CUPS_BACKEND_OK;
353 }
354
355 static int kodak6800_get_tonecurve(struct kodak6800_ctx *ctx, uint8_t curve, char *fname)
356 {
357 uint8_t cmdbuf[16];
358 uint8_t respbuf[64];
359 int ret, num = 0;
360 int i;
361
362 uint16_t *data = malloc(TONE_CURVE_SIZE * sizeof(uint16_t));
363 if (!data) {
364 ERROR("Memory Allocation Failure\n");
365 return -1;
366 }
367
368 INFO("Dump Tone Curve to '%s'\n", fname);
369
370 /* Initial Request */
371 kodak6800_fillhdr(cmdbuf, 0x0c);
372 cmdbuf[6] = 0x54;
373 cmdbuf[7] = 0x4f;
374 cmdbuf[8] = 0x4e;
375 cmdbuf[9] = 0x45;
376 cmdbuf[10] = 0x72;
377 cmdbuf[11] = curve;
378 cmdbuf[12] = PARAM_TABLE_NONE;
379 cmdbuf[13] = 0x00;
380 cmdbuf[14] = 0x00;
381 cmdbuf[15] = 0x00;
382
383 respbuf[0] = 0xff;
384 /* Issue command and get response */
385 if ((ret = kodak6800_do_cmd(ctx, cmdbuf, sizeof(cmdbuf),
386 respbuf, sizeof(respbuf),
387 &num)))
388
389 /* Validate proper response */
390 if (respbuf[0] != CMD_CODE_OK) {
391 ERROR("Unexpected response from tonecurve query!\n");
392 ret = -99;
393 goto done;
394 }
395
396 /* Then we can poll the data */
397 kodak6800_fillhdr(cmdbuf, 0x0c);
398 cmdbuf[6] = 0x54;
399 cmdbuf[7] = 0x4f;
400 cmdbuf[8] = 0x4e;
401 cmdbuf[9] = 0x45;
402 cmdbuf[10] = 0x20;
403 for (i = 0 ; i < 24 ; i++) {
404 /* Issue command and get response */
405 if ((ret = kodak6800_do_cmd(ctx, cmdbuf, sizeof(cmdbuf),
406 respbuf, sizeof(respbuf),
407 &num)))
408 goto done;
409
410 if (num != 64) {
411 ERROR("Short read! (%d/%d)\n", num, 51);
412 ret = 4;
413 goto done;
414 }
415
416 /* Copy into buffer */
417 memcpy(((uint8_t*)data)+i*64, respbuf, 64);
418 }
419
420 /* Open file and write it out */
421 {
422 int tc_fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
423 if (tc_fd < 0) {
424 ret = 4;
425 goto done;
426 }
427
428 for (i = 0 ; i < TONE_CURVE_SIZE; i++) {
429 /* Byteswap appropriately */
430 data[i] = cpu_to_be16(le16_to_cpu(data[i]));
431 ret = write(tc_fd, &data[i], sizeof(uint16_t));
432 }
433 close(tc_fd);
434 }
435
436 done:
437 /* We're done */
438 free(data);
439
440 return ret;
441 }
442
443 static int kodak6800_set_tonecurve(struct kodak6800_ctx *ctx, uint8_t curve, char *fname)
444 {
445 uint8_t cmdbuf[64];
446 uint8_t respbuf[64];
447 int ret, num = 0;
448 int remain;
449
450 uint16_t *data = malloc(TONE_CURVE_SIZE * sizeof(uint16_t));
451 uint8_t *ptr;
452
453 if (!data) {
454 ERROR("Memory Allocation Failure\n");
455 return -1;
456 }
457
458 INFO("Set Tone Curve from '%s'\n", fname);
459
460 /* Read in file */
461 if ((ret = dyesub_read_file(fname, data, TONE_CURVE_SIZE * sizeof(uint16_t), NULL))) {
462 ERROR("Failed to read Tone Curve file\n");
463 goto done;
464 }
465
466 /* Byteswap data to printer's format */
467 for (ret = 0; ret < TONE_CURVE_SIZE ; ret++) {
468 data[ret] = cpu_to_le16(be16_to_cpu(data[ret]));
469 }
470
471 /* Initial Request */
472 kodak6800_fillhdr(cmdbuf, 0x0c);
473 cmdbuf[6] = 0x54;
474 cmdbuf[7] = 0x4f;
475 cmdbuf[8] = 0x4e;
476 cmdbuf[9] = 0x45;
477 cmdbuf[10] = 0x77;
478 cmdbuf[11] = curve;
479 cmdbuf[12] = PARAM_TABLE_NONE;
480 cmdbuf[13] = 0x00;
481 cmdbuf[14] = 0x00;
482 cmdbuf[15] = 0x00;
483
484 /* Issue command and get response */
485 if ((ret = kodak6800_do_cmd(ctx, cmdbuf, sizeof(cmdbuf),
486 respbuf, sizeof(respbuf),
487 &num)))
488
489 /* Validate proper response */
490 if (num != 51) {
491 ERROR("Short read! (%d/%d)\n", num, 51);
492 ret = 4;
493 goto done;
494 }
495
496 if (respbuf[0] != CMD_CODE_OK) {
497 ERROR("Unexpected response from tonecurve set!\n");
498 ret = -99;
499 goto done;
500 }
501
502 ptr = (uint8_t*) data;
503 remain = TONE_CURVE_SIZE * sizeof(uint16_t);
504 while (remain > 0) {
505 int count = remain > 63 ? 63 : remain;
506
507 cmdbuf[0] = 0x03;
508 memcpy(cmdbuf+1, ptr, count);
509
510 remain -= count;
511 ptr += count;
512
513 /* Issue command and get response */
514 if ((ret = kodak6800_do_cmd(ctx, cmdbuf, count + 1,
515 respbuf, sizeof(respbuf),
516 &num)))
517
518 if (num != 51) {
519 ERROR("Short read! (%d/%d)\n", num, 51);
520 ret = 4;
521 goto done;
522 }
523 if (respbuf[0] != CMD_CODE_OK) {
524 ERROR("Unexpected response from tonecurve set!\n");
525 ret = -99;
526 goto done;
527 }
528 };
529
530 done:
531 /* We're done */
532 free(data);
533 return ret;
534 }
535
536 static int kodak6800_query_serno(struct dyesub_connection *conn, char *buf, int buf_len)
537 {
538 struct kodak6800_ctx ctx = {
539 .conn = conn,
540 };
541
542 int ret;
543 int num;
544
545 uint8_t resp[33];
546 uint8_t req[16];
547
548 memset(req, 0, sizeof(req));
549 memset(resp, 0, sizeof(resp));
550
551 kodak6800_fillhdr(req, 0x12);
552
553 /* Issue command and get response */
554 if ((ret = kodak6800_do_cmd(&ctx, req, sizeof(req),
555 resp, sizeof(resp),
556 &num)))
557 return ret;
558
559 if (num != 32) {
560 ERROR("Short read! (%d/%d)\n", num, 32);
561 return -2;
562 }
563
564 strncpy(buf, (char*)resp+24, buf_len);
565 buf[buf_len-1] = 0;
566
567 return CUPS_BACKEND_OK;
568 }
569
570 static int kodak6850_send_unk(struct kodak6800_ctx *ctx)
571 {
572 uint8_t cmdbuf[16];
573 uint8_t rdbuf[64];
574 int ret = 0, num = 0;
575
576 memset(cmdbuf, 0, sizeof(cmdbuf));
577 kodak6800_fillhdr(cmdbuf, 0x4c);
578
579 /* Issue command and get response */
580 if ((ret = kodak6800_do_cmd(ctx, cmdbuf, sizeof(cmdbuf),
581 rdbuf, sizeof(rdbuf),
582 &num)))
583 return -1;
584
585 if (num != 51) {
586 ERROR("Short read! (%d/%d)\n", num, 51);
587 return CUPS_BACKEND_FAILED;
588 }
589
590 if (rdbuf[0] != CMD_CODE_OK ||
591 rdbuf[2] != 0x43) {
592 ERROR("Unexpected response from printer init!\n");
593 return CUPS_BACKEND_FAILED;
594 }
595
596 return ret;
597 }
598
599 static void kodak6800_cmdline(void)
600 {
601 DEBUG("\t\t[ -c filename ] # Get user/NV tone curve\n");
602 DEBUG("\t\t[ -C filename ] # Set user/NV tone curve\n");
603 DEBUG("\t\t[ -l filename ] # Get current tone curve\n");
604 DEBUG("\t\t[ -L filename ] # Set current tone curve\n");
605 DEBUG("\t\t[ -m ] # Query media\n");
606 DEBUG("\t\t[ -s ] # Query status\n");
607 DEBUG("\t\t[ -R ] # Reset printer\n");
608 DEBUG("\t\t[ -X jobid ] # Cancel Job\n");
609 }
610
611 static int kodak6800_cmdline_arg(void *vctx, int argc, char **argv)
612 {
613 struct kodak6800_ctx *ctx = vctx;
614 int i, j = 0;
615
616 if (!ctx)
617 return -1;
618
619 while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "C:c:L:l:mRsX:")) >= 0) {
620 switch(i) {
621 GETOPT_PROCESS_GLOBAL
622 case 'c':
623 j = kodak6800_get_tonecurve(ctx, TONE_TABLE_USER, optarg);
624 break;
625 case 'C':
626 j = kodak6800_set_tonecurve(ctx, TONE_TABLE_USER, optarg);
627 break;
628 case 'l':
629 j = kodak6800_get_tonecurve(ctx, TONE_TABLE_CURRENT, optarg);
630 break;
631 case 'L':
632 j = kodak6800_set_tonecurve(ctx, TONE_TABLE_CURRENT, optarg);
633 break;
634 case 'm':
635 kodak68x0_dump_mediainfo(ctx->sizes, ctx->media_count, ctx->media_type);
636 break;
637 case 'R':
638 kodak68x0_reset(ctx);
639 break;
640 case 's': {
641 j = kodak6800_get_status(ctx, &ctx->sts);
642 if (!j)
643 kodak68x0_dump_status(ctx, &ctx->sts);
644 break;
645 }
646 case 'X':
647 j = kodak68x0_canceljob(ctx, atoi(optarg));
648 break;
649 default:
650 break; /* Ignore completely */
651 }
652
653 if (j) return j;
654 }
655
656 return CUPS_BACKEND_OK;
657 }
658
659 static void *kodak6800_init(void)
660 {
661 struct kodak6800_ctx *ctx = malloc(sizeof(struct kodak6800_ctx));
662 if (!ctx) {
663 ERROR("Memory Allocation Failure\n");
664 return NULL;
665 }
666 memset(ctx, 0, sizeof(struct kodak6800_ctx));
667
668 return ctx;
669 }
670
671 static int kodak6800_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid)
672 {
673 struct kodak6800_ctx *ctx = vctx;
674
675 ctx->conn = conn;
676
677 /* Ensure jobid is sane */
678 ctx->jobid = jobid & 0x7f;
679 if (!ctx->jobid)
680 ctx->jobid++;
681
682 if (test_mode < TEST_MODE_NOATTACH) {
683 /* Query printer status */
684 if (kodak6800_get_status(ctx, &ctx->sts)) {
685 ERROR("Can't query status\n");
686 return CUPS_BACKEND_FAILED;
687 }
688 uint16_t fw = be16_to_cpu(ctx->sts.main_fw);
689 if (ctx->conn->type == P_KODAK_6850) {
690 if ((fw >= 878) ||
691 (fw < 800 && fw >= 678)) {
692 ctx->supports_sub4x6 = 1;
693 } else {
694 WARNING("Printer FW out of date, recommend updating for current media and features\n");
695 }
696 } else {
697 if ((fw >= 459) ||
698 (fw < 400 && fw >= 359) ||
699 (fw < 300 && fw >= 259)) {
700 ctx->supports_sub4x6 = 1;
701 } else {
702 WARNING("Printer FW out of date, recommend updating for current media and features\n");
703 }
704 }
705
706 /* Query media info */
707 if (kodak6800_get_mediainfo(ctx)) {
708 ERROR("Can't query media\n");
709 return CUPS_BACKEND_FAILED;
710 }
711 } else {
712 int media_code = KODAK6_MEDIA_6TR2;
713 if (getenv("MEDIA_CODE"))
714 media_code = strtol(getenv("MEDIA_CODE"), NULL, 16);
715
716 ctx->media_type = media_code;
717 ctx->supports_sub4x6 = 1;
718 }
719
720 ctx->marker.color = "#00FFFF#FF00FF#FFFF00";
721 ctx->marker.name = kodak6_mediatypes(ctx->media_type);
722 ctx->marker.numtype = ctx->media_type;
723 ctx->marker.levelmax = 100; /* Ie percentage */
724 ctx->marker.levelnow = CUPS_MARKER_UNKNOWN;
725
726 return CUPS_BACKEND_OK;
727 }
728
729 static int kodak6800_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
730 struct kodak6800_ctx *ctx = vctx;
731 int ret;
732
733 struct kodak6800_hdr hdr;
734 struct sinfonia_printjob *job = NULL;
735
736 if (!ctx)
737 return CUPS_BACKEND_FAILED;
738
739 job = malloc(sizeof(*job));
740 if (!job) {
741 ERROR("Memory allocation failure!\n");
742 return CUPS_BACKEND_RETRY_CURRENT;
743 }
744 memset(job, 0, sizeof(*job));
745
746 /* Read in then validate header */
747 ret = read(data_fd, &hdr, sizeof(hdr));
748 if (ret < 0 || ret != sizeof(hdr)) {
749 if (ret == 0) {
750 sinfonia_cleanup_job(job);
751 return CUPS_BACKEND_CANCEL;
752 }
753 ERROR("Read failed (%d/%d/%d)\n",
754 ret, 0, (int)sizeof(hdr));
755 perror("ERROR: Read failed");
756 sinfonia_cleanup_job(job);
757 return CUPS_BACKEND_CANCEL;
758 }
759 if (hdr.hdr[0] != 0x03 ||
760 hdr.hdr[1] != 0x1b ||
761 hdr.hdr[2] != 0x43 ||
762 hdr.hdr[3] != 0x48 ||
763 hdr.hdr[4] != 0x43) {
764 ERROR("Unrecognized data format!\n");
765 sinfonia_cleanup_job(job);
766 return CUPS_BACKEND_CANCEL;
767 }
768
769 uint16_t rows = be16_to_cpu(hdr.rows);
770 uint16_t cols = be16_to_cpu(hdr.columns);
771 if (rows != 1240 && rows != 2434 && rows != 2140 && !ctx->supports_sub4x6) {
772 ERROR("Printer Firmware does not support non-4x6/8x6/5x7 prints, please upgrade!\n");
773 sinfonia_cleanup_job(job);
774 return CUPS_BACKEND_CANCEL;
775 }
776
777 job->datalen = rows * cols * 3;
778 job->databuf = malloc(job->datalen);
779 if (!job->databuf) {
780 ERROR("Memory allocation failure!\n");
781 sinfonia_cleanup_job(job);
782 return CUPS_BACKEND_RETRY_CURRENT;
783 }
784
785 /* Windows driver only sends 634 rows of data, work around */
786 if (rows == 636 && hdr.size == 6 && hdr.method == 0) {
787 rows = 634;
788 job->datalen -= 1844*2*3;
789 }
790
791 /* Read in the spool data */
792 {
793 int remain = job->datalen;
794 uint8_t *ptr = job->databuf;
795 do {
796 ret = read(data_fd, ptr, remain);
797 if (ret < 0) {
798 ERROR("Read failed (%d/%d/%d)\n",
799 ret, remain, job->datalen);
800 perror("ERROR: Read failed");
801 sinfonia_cleanup_job(job);
802 return CUPS_BACKEND_CANCEL;
803 }
804 ptr += ret;
805 remain -= ret;
806 } while (remain);
807 }
808
809 /* Undo the Windows workaround... */
810 if (rows == 634) {
811 rows = 636;
812 job->datalen += 1844*2*3;
813 }
814
815 /* Perform some header re-jiggery */
816 if (hdr.size == 0) {
817 if (cols == 1844)
818 hdr.size = 6;
819 else if (cols == 1548)
820 hdr.size = 7;
821 }
822 if (hdr.method == 0) {
823 if (rows == 636) {
824 hdr.method = 0x21;
825 } else if (rows == 936) {
826 hdr.method = 0x23;
827 } else if (rows == 1240) {
828 hdr.method = 0x01;
829 } else if (rows == 1282) {
830 hdr.method = 0x20;
831 } else if (rows == 1882) {
832 hdr.method = 0x22;
833 } else if (rows == 2490) {
834 hdr.method = 0x2;
835 }
836 }
837
838 hdr.copies = be16_to_cpu(hdr.copies);
839 hdr.copies = packed_bcd_to_uint32((char*)&hdr.copies, 2);
840 /* Use larger of our copy counts */
841 if (hdr.copies > copies)
842 copies = hdr.copies;
843
844 /* Fill out job structure */
845 job->common.copies = copies;
846 job->jp.rows = rows;
847 job->jp.columns = cols;
848 job->jp.media = hdr.size;
849 job->jp.oc_mode = hdr.laminate;
850 job->jp.method = hdr.method;
851
852 *vjob = job;
853
854 return CUPS_BACKEND_OK;
855 }
856
857 static int kodak6800_main_loop(void *vctx, const void *vjob, int wait_for_return) {
858 struct kodak6800_ctx *ctx = vctx;
859
860 int num, ret;
861 int copies;
862
863 const struct sinfonia_printjob *job = vjob;
864
865 if (!ctx)
866 return CUPS_BACKEND_FAILED;
867 if (!job)
868 return CUPS_BACKEND_FAILED;
869
870 copies = job->common.copies;
871
872 /* Validate against supported media list */
873 for (num = 0 ; num < ctx->media_count; num++) {
874 if (ctx->sizes[num].rows == job->jp.rows &&
875 ctx->sizes[num].columns == job->jp.columns &&
876 ctx->sizes[num].method == job->jp.method)
877 break;
878 }
879 if (num == ctx->media_count) {
880 ERROR("Print size unsupported by media!\n");
881 return CUPS_BACKEND_HOLD;
882 }
883
884 INFO("Waiting for printer idle\n");
885
886 while(1) {
887 if (kodak6800_get_status(ctx, &ctx->sts))
888 return CUPS_BACKEND_FAILED;
889
890 if (ctx->marker.levelnow != ctx->sts.donor) {
891 ctx->marker.levelnow = ctx->sts.donor;
892 dump_markers(&ctx->marker, 1, 0);
893 }
894
895 if (ctx->sts.status1 == STATE_STATUS1_ERROR) {
896 INFO("Printer State: %s # %02x %08x %02x\n",
897 sinfonia_1x45_status_str(ctx->sts.status1, ctx->sts.status2, ctx->sts.errcode),
898 ctx->sts.status1, ctx->sts.status2, ctx->sts.errcode);
899 return CUPS_BACKEND_FAILED;
900 }
901
902 /* make sure we're not colliding with an existing
903 jobid */
904 while (ctx->jobid == ctx->sts.b1_jobid ||
905 ctx->jobid == ctx->sts.b2_jobid) {
906 ctx->jobid++;
907 ctx->jobid &= 0x7f;
908 if (!ctx->jobid)
909 ctx->jobid++;
910 }
911
912 /* See if we have an open bank */
913 if (!ctx->sts.b1_remain ||
914 !ctx->sts.b2_remain)
915 break;
916
917 sleep(1);
918 }
919
920 /* This command is unknown, sort of a secondary status query */
921 if (ctx->conn->type == P_KODAK_6850) {
922 ret = kodak6850_send_unk(ctx);
923 if (ret)
924 return ret;
925 }
926
927 /* Fix max print count. */
928 if (copies > 9999)
929 copies = 9999;
930
931 /* Fill out printjob header */
932 struct kodak6800_hdr hdr;
933 kodak6800_fillhdr(hdr.hdr, 0x0a);
934 hdr.hdr[6] = 0x00;
935 hdr.jobid = ctx->jobid;
936 hdr.copies = uint16_to_packed_bcd(copies);
937 hdr.columns = cpu_to_be16(job->jp.columns);
938 hdr.rows = cpu_to_be16(job->jp.rows);
939 hdr.size = job->jp.media;
940 hdr.laminate = job->jp.oc_mode;
941 hdr.method = job->jp.method;
942
943 INFO("Sending Print Job (internal id %u)\n", ctx->jobid);
944 if ((ret = kodak6800_do_cmd(ctx, (uint8_t*) &hdr, sizeof(hdr),
945 &ctx->sts, sizeof(ctx->sts),
946 &num)))
947 return ret;
948
949 if (ctx->sts.hdr != CMD_CODE_OK) {
950 ERROR("Unexpected response from print command!\n");
951 return CUPS_BACKEND_FAILED;
952 }
953
954 // sleep(1); // Appears to be necessary for reliability
955 INFO("Sending image data\n");
956 if ((send_data(ctx->conn,
957 job->databuf, job->datalen)) != 0)
958 return CUPS_BACKEND_FAILED;
959
960 INFO("Waiting for printer to acknowledge completion\n");
961 do {
962 sleep(1);
963 if (kodak6800_get_status(ctx, &ctx->sts))
964 return CUPS_BACKEND_FAILED;
965
966 if (ctx->marker.levelnow != ctx->sts.donor) {
967 ctx->marker.levelnow = ctx->sts.donor;
968 dump_markers(&ctx->marker, 1, 0);
969 }
970
971 if (ctx->sts.status1 == STATE_STATUS1_ERROR) {
972 INFO("Printer State: %s # %02x %08x %02x\n",
973 sinfonia_1x45_status_str(ctx->sts.status1, ctx->sts.status2, ctx->sts.errcode),
974 ctx->sts.status1, ctx->sts.status2, ctx->sts.errcode);
975 return CUPS_BACKEND_FAILED;
976 }
977
978 /* If all prints are complete, we're done! */
979 if (ctx->sts.b1_jobid == hdr.jobid && ctx->sts.b1_complete == ctx->sts.b1_total)
980 break;
981 if (ctx->sts.b2_jobid == hdr.jobid && ctx->sts.b2_complete == ctx->sts.b2_total)
982 break;
983
984 if (!wait_for_return) {
985 INFO("Fast return mode enabled.\n");
986 break;
987 }
988
989 } while (1);
990
991 INFO("Print complete\n");
992
993 return CUPS_BACKEND_OK;
994 }
995
996 static int kodak6800_query_markers(void *vctx, struct marker **markers, int *count)
997 {
998 struct kodak6800_ctx *ctx = vctx;
999
1000 /* Query printer status */
1001 if (kodak6800_get_status(ctx, &ctx->sts))
1002 return CUPS_BACKEND_FAILED;
1003
1004 ctx->marker.levelnow = ctx->sts.donor;
1005
1006 if (markers) *markers = &ctx->marker;
1007 if (count) *count = 1;
1008
1009 return CUPS_BACKEND_OK;
1010 }
1011
1012 static int kodak6800_query_stats(void *vctx, struct printerstats *stats)
1013 {
1014 struct kodak6800_ctx *ctx = vctx;
1015
1016 if (kodak6800_query_markers(ctx, NULL, NULL))
1017 return CUPS_BACKEND_FAILED;
1018
1019 switch (ctx->conn->type) {
1020 case P_KODAK_6800:
1021 stats->mfg = "Kodak";
1022 stats->model = "6800";
1023 break;
1024 case P_KODAK_6850:
1025 stats->mfg = "Kodak";
1026 stats->model = "6850";
1027 break;
1028 default:
1029 stats->mfg = "Unknown";
1030 stats->model = "Unknown";
1031 break;
1032 }
1033
1034 if (kodak6800_query_serno(ctx->conn,
1035 ctx->serial, sizeof(ctx->serial)))
1036 return CUPS_BACKEND_FAILED;
1037
1038 stats->serial = ctx->serial;
1039
1040 snprintf(ctx->fwver, sizeof(ctx->fwver)-1,
1041 "%d / %d", be16_to_cpu(ctx->sts.main_fw),
1042 be16_to_cpu(ctx->sts.dsp_fw));
1043 stats->fwver = ctx->fwver;
1044
1045 stats->decks = 1;
1046 stats->mediatype[0] = ctx->marker.name;
1047 stats->levelmax[0] = ctx->marker.levelmax;
1048 stats->levelnow[0] = ctx->marker.levelnow;
1049 stats->name[0] = "Roll";
1050 stats->status[0] = strdup(sinfonia_1x45_status_str(ctx->sts.status1, ctx->sts.status2, ctx->sts.errcode));
1051 stats->cnt_life[0] = be32_to_cpu(ctx->sts.lifetime);
1052
1053 return CUPS_BACKEND_OK;
1054 }
1055
1056 static const char *kodak6800_prefixes[] = {
1057 "kodak68x0", // Family driver, do not nuke.
1058 // Backwards-compatibility
1059 "kodak6800", "kodak6850",
1060 NULL
1061 };
1062
1063 static const struct device_id kodak6800_devices[] = {
1064 { 0x040a, 0x4021, P_KODAK_6800, "Kodak", "kodak-6800"}, /* Shinko CHC-S1145-5A */
1065 { 0x040a, 0x402b, P_KODAK_6850, "Kodak", "kodak-6850"}, /* Shinko CHC-S1145-5B */
1066 { 0, 0, 0, NULL, NULL}
1067 };
1068
1069 /* Exported */
1070 const struct dyesub_backend kodak6800_backend = {
1071 .name = "Kodak 6800/6850",
1072 .version = "0.81" " (lib " LIBSINFONIA_VER ")",
1073 .uri_prefixes = kodak6800_prefixes,
1074 .cmdline_usage = kodak6800_cmdline,
1075 .cmdline_arg = kodak6800_cmdline_arg,
1076 .init = kodak6800_init,
1077 .attach = kodak6800_attach,
1078 .cleanup_job = sinfonia_cleanup_job,
1079 .read_parse = kodak6800_read_parse,
1080 .main_loop = kodak6800_main_loop,
1081 .query_serno = kodak6800_query_serno,
1082 .query_markers = kodak6800_query_markers,
1083 .query_stats = kodak6800_query_stats,
1084 .devices = kodak6800_devices,
1085 };
1086
1087 /* Kodak 6800/6850 data format
1088
1089 Spool file consists of 17-byte header followed by plane-interleaved BGR data.
1090 Native printer resolution is 1844 pixels per row, and 1240 or 2434 rows.
1091
1092 6850 Adds support for 5x7, with 1548 pixels per row and 2140 columns.
1093
1094 All fields are BIG ENDIAN unless otherwise specified.
1095
1096 Header:
1097
1098 03 1b 43 48 43 0a 00 Fixed header
1099 II Job ID (1-255)
1100 NN NN Number of copies in BCD form (0001->9999)
1101 WW WW Number of columns (Fixed at 1844 on 6800)
1102 HH HH Number of rows.
1103 SS Print size -- 0x00 (4x6) 0x06 (8x6) 0x07 (5x7 on 6850)
1104 LL Laminate mode -- 0x00 (off) or 0x01 (on)
1105 UU Print mode -- 0x00 (normal) or 0x01 (4x6 on 8x6) 0x21 (2x6) 0x23 (3x6)
1106
1107 ************************************************************************
1108
1109 This command is unique to the 6850:
1110
1111 -> 03 1b 43 48 43 4c 00 00 00 00 00 00 00 00 00 00 [???]
1112 <- [51 octets]
1113
1114 01 01 43 48 43 4c 00 00 00 00 00 00 00 00 00 00 <-- Everything after this
1115 00 00 01 29 00 00 3b 0a 00 00 00 0e 00 03 02 90 line is the same as
1116 00 01 02 1d 03 00 00 00 00 01 00 01 00 00 00 00 the "status" resp.
1117 00 00 00
1118
1119 01 00 43 48 43 4c 00 00 00 00 00 00 00 00 00 00
1120 00 00 00 01 00 00 b7 d3 00 00 00 5c 00 03 02 8c
1121 00 01 02 1c 00 00 00 00 00 01 00 01 00 00 00 00
1122 00 00 00
1123
1124 An additional command that's also unknown
1125
1126 -> 03 1b 43 48 43 4d 01 00 00 00 00 00 00 00 00 00
1127 <- 01 02 01 00 00 00 00 00 00 00 5d ca 00 00 5d ca
1128 00 00 00 15 00 00 b8 f8 00 00 00 40 00 03 02 a6
1129 00 01 02 31 1e 00 00 00 00 01 00 01 00 00 00 00
1130 00 00 00
1131
1132 One more note for the 6850. These sizes have been seen:
1133
1134 1844x2434, method 0x03
1135 1844x2490, method 0x05
1136 1844x2222, method 0x00
1137
1138 */