Debian Codegraph @

src/cups/backend_shinkos1245.c

raw symbols SCIP
1 /*
2 * Shinko/Sinfonia CHC-S1245 CUPS backend
3 *
4 * (c) 2015-2024 Solomon Peachy <[email protected]>
5 *
6 * Low-level documentation was provided by Sinfonia, Inc. Thank you!
7 *
8 * The latest version of this program can be found at:
9 *
10 * https://git.shaftnet.org/gitea/slp/selphy_print.git
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses/>.
24 *
25 * SPDX-License-Identifier: GPL-2.0+
26 *
27 */
28
29 #define BACKEND shinkos1245_backend
30
31 #include "backend_common.h"
32 #include "backend_sinfonia.h"
33
34 /* Printer data structures */
35 struct shinkos1245_cmd_hdr {
36 uint8_t prefix; /* 0x03 */
37 uint8_t hdr[4]; /* 0x1b 0x43 0x48 0x43 */
38 } __attribute__((packed));
39
40 /* Get Printer ID */
41 struct shinkos1245_cmd_getid {
42 struct shinkos1245_cmd_hdr hdr;
43 uint8_t cmd[1]; /* 0x12 */
44 uint8_t pad[11];
45 } __attribute__((packed));
46
47 struct shinkos1245_resp_getid {
48 uint8_t id; /* 0x00 */
49 uint8_t data[23]; /* padded with 0x20 (space) */
50 uint8_t reserved[8]; // XXX actual serial number?
51 } __attribute__((packed));
52
53 /* Set Printer ID -- Returns Status */
54 struct shinkos1245_cmd_setid {
55 struct shinkos1245_cmd_hdr hdr;
56 uint8_t cmd[2]; /* 0x0a 0x22 */
57 uint8_t id; /* 0x00 */
58 uint8_t data[23]; /* pad with 0x20 (space) */
59 } __attribute__((packed));
60
61 /* Print -- Returns Status */
62 struct shinkos1245_cmd_print {
63 struct shinkos1245_cmd_hdr hdr;
64 uint8_t cmd[2]; /* 0x0a 0x00 */
65 uint8_t id; /* 1-255 */
66 uint16_t count; /* # Copies in BCD, 1-9999 */
67 uint16_t columns; /* Fixed at 2446 */
68 uint16_t rows;
69 uint8_t media; /* Fixed at 0x10 */
70 uint8_t mode; /* dust removal and lamination mode */
71 uint8_t combo; /* aka "print method" in the spool file */
72 } __attribute__((packed));
73
74 /* Get Status */
75 struct shinkos1245_cmd_getstatus {
76 struct shinkos1245_cmd_hdr hdr;
77 uint8_t cmd[1]; /* 0x03 */
78 uint8_t pad[10];
79 } __attribute__((packed));
80
81 struct shinkos1245_resp_status {
82 uint8_t code;
83 uint8_t print_status;
84 struct {
85 uint8_t status1;
86 uint32_t status2; /* BE */
87 uint8_t error;
88 } __attribute__((packed)) state;
89 struct {
90 uint32_t lifetime; /* BE */
91 uint32_t maint; /* BE */
92 uint32_t media; /* BE */
93 uint32_t cutter; /* BE */
94 uint8_t reserved;
95 uint8_t ver_boot;
96 uint8_t ver_ctrl;
97 uint8_t control_flag; // 0x00 == epson, 0x01 == cypress
98 } __attribute__((packed)) counters;
99 struct {
100 uint16_t main_boot;
101 uint16_t main_control;
102 uint16_t dsp_boot;
103 uint16_t dsp_control;
104 } __attribute__((packed)) versions;
105 struct {
106 uint8_t bank1_id;
107 uint8_t bank2_id;
108 uint16_t bank1_remain; /* BE */
109 uint16_t bank1_complete; /* BE */
110 uint16_t bank1_spec; /* BE */
111 uint16_t bank2_remain; /* BE */
112 uint16_t bank2_complete; /* BE */
113 uint16_t bank2_spec; /* BE */
114 } __attribute__((packed)) counters2;
115 uint8_t curve_status;
116 } __attribute__((packed));
117
118 /* Query media info */
119 struct shinkos1245_cmd_getmedia {
120 struct shinkos1245_cmd_hdr hdr;
121 uint8_t cmd[1]; /* 0x1a/0x2a/0x3a for A/B/C */
122 uint8_t pad[10];
123 } __attribute__((packed));
124
125 #define NUM_MEDIAS 5 /* Maximum per message */
126
127 struct shinkos1245_resp_media {
128 uint8_t code;
129 uint8_t reserved[6];
130 uint8_t count; /* 1-5? */
131 struct sinfonia_mediainfo_item data[NUM_MEDIAS];
132 } __attribute__((packed));
133
134
135 enum {
136 PRINT_TYPE_STANDARD = 0x00,
137 PRINT_TYPE_8x5_2up = 0x01,
138 PRINT_TYPE_8x4_2up = 0x02,
139 PRINT_TYPE_8x6_8x4 = 0x03,
140 PRINT_TYPE_8x5 = 0x04,
141 PRINT_TYPE_8x4 = 0x05,
142 PRINT_TYPE_8x6 = 0x06,
143 PRINT_TYPE_8x6_2up = 0x07,
144 PRINT_TYPE_8x4_3up = 0x08,
145 PRINT_TYPE_8x8 = 0x09,
146 };
147
148 /* Cancel Job -- returns Status */
149 struct shinkos1245_cmd_canceljob {
150 struct shinkos1245_cmd_hdr hdr;
151 uint8_t cmd[1]; /* 0x13 */
152 uint8_t id; /* 1-255 */
153 uint8_t pad[9];
154 } __attribute__((packed));
155
156 /* Reset printer -- returns Status */
157 struct shinkos1245_cmd_reset {
158 struct shinkos1245_cmd_hdr hdr;
159 uint8_t cmd[1]; /* 0xc0 */
160 uint8_t pad[10];
161 } __attribute__((packed));
162
163 /* Tone curve manipulation -- returns Status */
164 struct shinkos1245_cmd_tone {
165 struct shinkos1245_cmd_hdr hdr;
166 uint8_t cmd[1]; /* 0xc0 */
167 uint8_t tone[4]; /* 0x54 0x4f 0x4e 0x45 */
168 uint8_t cmd2[1]; /* 0x72/0x77/0x65/0x20 for read/write/end/data */
169 union {
170 struct {
171 uint8_t tone_table;
172 uint8_t param_table;
173 uint8_t pad[3];
174 } read_write;
175 struct {
176 uint8_t pad[5];
177 } end_data;
178 } data;
179 } __attribute__((packed));
180
181 #define TONE_CURVE_DATA_BLOCK_SIZE 64
182
183 /* Query Model information */
184 struct shinkos1245_cmd_getmodel {
185 struct shinkos1245_cmd_hdr hdr;
186 uint8_t cmd[1]; /* 0x02 */
187 uint8_t pad[10];
188 } __attribute__((packed));
189
190 struct shinkos1245_resp_getmodel {
191 uint8_t vendor_id[4];
192 uint8_t product_id[4];
193 uint8_t strings[40];
194 } __attribute__((packed));
195
196
197 /* Query and Set Matte info, returns a Matte response */
198 struct shinkos1245_cmd_getmatte {
199 struct shinkos1245_cmd_hdr hdr;
200 uint8_t cmd[1]; /* 0x20 */
201 uint8_t mode; /* Fixed at 0x00 */
202 uint8_t pad[9];
203 } __attribute__((packed));
204
205 struct shinkos1245_cmd_setmatte {
206 struct shinkos1245_cmd_hdr hdr;
207 uint8_t cmd[1]; /* 0x21 */
208 uint8_t mode; /* Fixed at 0x00 */
209 int8_t level; /* -25->+25 */
210 uint8_t pad[8];
211 } __attribute__((packed));
212
213 struct shinkos1245_resp_matte {
214 uint8_t code;
215 uint8_t mode;
216 int8_t level;
217 uint8_t reserved[4];
218 } __attribute__((packed));
219
220 #define MATTE_MODE_MATTE 0x00
221 #define MAX_MEDIA_ITEMS 15
222
223 /* Private data structure */
224 struct shinkos1245_ctx {
225 struct dyesub_connection *conn;
226
227 uint8_t jobid;
228
229 struct sinfonia_mediainfo_item medias[MAX_MEDIA_ITEMS];
230 int num_medias;
231 int media_8x12;
232
233 char serial[32];
234 char fwver[32];
235
236 struct marker marker;
237
238 int tonecurve;
239 };
240
241 enum {
242 S_IDLE = 0,
243 S_PRINTER_READY_CMD,
244 S_PRINTER_SENT_DATA,
245 S_FINISHED,
246 };
247
248
249 /* Basic printer I/O stuffs */
250 static void shinkos1245_fill_hdr(struct shinkos1245_cmd_hdr *hdr)
251 {
252 hdr->prefix = 0x03;
253 hdr->hdr[0] = 0x1b;
254 hdr->hdr[1] = 0x43;
255 hdr->hdr[2] = 0x48;
256 hdr->hdr[3] = 0x43;
257 }
258
259 static int shinkos1245_do_cmd(struct shinkos1245_ctx *ctx,
260 const void *cmd, int cmd_len,
261 void *resp, int resp_len,
262 int *actual_len)
263 {
264 int ret;
265
266 /* Write command */
267 if ((ret = send_data(ctx->conn,
268 cmd, cmd_len)))
269 return (ret < 0) ? ret : -99;
270
271 /* Read response */
272 ret = read_data(ctx->conn,
273 resp, resp_len, actual_len);
274 if (ret < 0)
275 return ret;
276 if (*actual_len < resp_len) {
277 ERROR("Short read! (%d/%d))\n", *actual_len, resp_len);
278 return -99;
279 }
280
281 return ret;
282 }
283
284 static int shinkos1245_get_status(struct shinkos1245_ctx *ctx,
285 struct shinkos1245_resp_status *resp)
286 {
287 struct shinkos1245_cmd_getstatus cmd;
288 int ret, num;
289
290 shinkos1245_fill_hdr(&cmd.hdr);
291 cmd.cmd[0] = 0x03;
292 memset(cmd.pad, 0, sizeof(cmd.pad));
293
294 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
295 resp, sizeof(*resp), &num);
296 if (ret < 0) {
297 ERROR("Failed to execute GET_STATUS command\n");
298 return ret;
299 }
300 if (resp->code != CMD_CODE_OK) {
301 ERROR("Bad return code on GET_STATUS (%02x)\n",
302 resp->code);
303 return -99;
304 }
305
306 /* Byteswap important stuff */
307 resp->state.status2 = be32_to_cpu(resp->state.status2);
308
309 return CUPS_BACKEND_OK;
310 }
311
312 static int shinkos1245_get_media(struct shinkos1245_ctx *ctx)
313 {
314 struct shinkos1245_cmd_getmedia cmd;
315 struct shinkos1245_resp_media resp;
316 int i, j;
317 int ret = 0, num;
318
319 shinkos1245_fill_hdr(&cmd.hdr);
320 memset(cmd.pad, 0, sizeof(cmd.pad));
321 ctx->media_8x12 = 0;
322 for (i = 1 ; i <= 3 ; i++) {
323 cmd.cmd[0] = 0x0a | (i << 4);
324
325 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
326 &resp, sizeof(resp), &num);
327 if (ret < 0) {
328 ERROR("Failed to execute GET_MEDIA command\n");
329 return ret;
330 }
331 if (resp.code != CMD_CODE_OK) {
332 ERROR("Bad return code on GET_MEDIA (%02x)\n",
333 resp.code);
334 return -99;
335 }
336
337 /* Store media info */
338 for (j = 0; j < NUM_MEDIAS && ctx->num_medias < resp.count ; j++) {
339 ctx->medias[ctx->num_medias].code = resp.data[j].code;
340 ctx->medias[ctx->num_medias].columns = be16_to_cpu(resp.data[j].columns);
341 ctx->medias[ctx->num_medias].rows = be16_to_cpu(resp.data[j].rows);
342 ctx->medias[ctx->num_medias].type = resp.data[j].type;
343 ctx->medias[ctx->num_medias].method = resp.data[j].method;
344 ctx->num_medias++;
345
346 if (ctx->medias[i].rows >= 3636)
347 ctx->media_8x12 = 1;
348 }
349
350 /* Once we've parsed them all.. we're done */
351 if (ctx->num_medias == resp.count)
352 break;
353 }
354 return ret;
355 }
356
357 static int shinkos1245_get_printerid(struct shinkos1245_ctx *ctx,
358 struct shinkos1245_resp_getid *resp)
359 {
360 struct shinkos1245_cmd_getid cmd;
361 int ret, num;
362
363 shinkos1245_fill_hdr(&cmd.hdr);
364 cmd.cmd[0] = 0x12;
365 memset(cmd.pad, 0, sizeof(cmd.pad));
366
367 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
368 resp, sizeof(*resp), &num);
369 if (ret < 0) {
370 ERROR("Failed to execute GET_PRINTERID command\n");
371 return ret;
372 }
373
374 return CUPS_BACKEND_OK;
375 }
376
377 static int shinkos1245_set_printerid(struct shinkos1245_ctx *ctx,
378 char *id)
379 {
380 struct shinkos1245_cmd_setid cmd;
381 struct shinkos1245_resp_status sts;
382
383 int ret, num;
384 int i;
385
386 shinkos1245_fill_hdr(&cmd.hdr);
387 cmd.cmd[0] = 0x0a;
388 cmd.cmd[1] = 0x22;
389
390 for (i = 0 ; i < (int)sizeof(cmd.data) ; i++) {
391 if (*id)
392 cmd.data[i] = (uint8_t) *id++;
393 else
394 cmd.data[i] = ' ';
395 }
396 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
397 &sts, sizeof(sts), &num);
398 if (ret < 0) {
399 ERROR("Failed to execute SET_PRINTERID command\n");
400 return ret;
401 }
402 if (sts.code != CMD_CODE_OK) {
403 ERROR("Bad return code on SET_PRINTERID command\n");
404 return -99;
405 }
406 return CUPS_BACKEND_OK;
407 }
408
409 static int shinkos1245_canceljob(struct shinkos1245_ctx *ctx,
410 int id)
411 {
412 struct shinkos1245_cmd_canceljob cmd;
413 struct shinkos1245_resp_status sts;
414
415 int ret, num;
416
417 shinkos1245_fill_hdr(&cmd.hdr);
418 cmd.cmd[0] = 0x13;
419 cmd.id = id;
420
421 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
422 &sts, sizeof(sts), &num);
423 if (ret < 0) {
424 ERROR("Failed to execute CANCELJOB command\n");
425 return ret;
426 }
427 if (sts.code != CMD_CODE_OK) {
428 ERROR("Bad return code on CANCELJOB command\n");
429 return -99;
430 }
431 return CUPS_BACKEND_OK;
432 }
433
434 static int shinkos1245_reset(struct shinkos1245_ctx *ctx)
435 {
436 struct shinkos1245_cmd_reset cmd;
437 struct shinkos1245_resp_status sts;
438
439 int ret, num;
440
441 shinkos1245_fill_hdr(&cmd.hdr);
442 cmd.cmd[0] = 0xc0;
443
444 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
445 &sts, sizeof(sts), &num);
446 if (ret < 0) {
447 ERROR("Failed to execute RESET command\n");
448 return ret;
449 }
450 if (sts.code != CMD_CODE_OK) {
451 ERROR("Bad return code on RESET command\n");
452 return -99;
453 }
454 return CUPS_BACKEND_OK;
455 }
456
457
458 static int shinkos1245_set_matte(struct shinkos1245_ctx *ctx,
459 int intensity)
460 {
461 struct shinkos1245_cmd_setmatte cmd;
462 struct shinkos1245_resp_matte sts;
463
464 int ret, num;
465
466 shinkos1245_fill_hdr(&cmd.hdr);
467 cmd.cmd[0] = 0x21;
468 cmd.mode = MATTE_MODE_MATTE;
469 cmd.level = intensity;
470
471 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
472 &sts, sizeof(sts), &num);
473 if (ret < 0) {
474 ERROR("Failed to execute SET_MATTE command\n");
475 return ret;
476 }
477 if (sts.code == CMD_CODE_OK)
478 return CUPS_BACKEND_OK;
479 if (sts.code == CMD_CODE_BAD)
480 return 1;
481
482 ERROR("Bad return code (%02x) on SET_MATTE command\n", sts.code);
483 return -99;
484 }
485
486 static int shinkos1245_get_matte(struct shinkos1245_ctx *ctx,
487 int *intensity)
488 {
489 struct shinkos1245_cmd_getmatte cmd;
490 struct shinkos1245_resp_matte sts;
491
492 int ret, num;
493
494 shinkos1245_fill_hdr(&cmd.hdr);
495 cmd.cmd[0] = 0x20;
496 cmd.mode = MATTE_MODE_MATTE;
497
498 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
499 &sts, sizeof(sts), &num);
500 if (ret < 0) {
501 ERROR("Failed to execute GET_MATTE command\n");
502 return ret;
503 }
504 if (sts.code != CMD_CODE_OK) {
505 ERROR("Bad return code (%02x) on GET_MATTE command\n", sts.code);
506 return -99;
507 }
508 *intensity = sts.level;
509
510 return CUPS_BACKEND_OK;
511 }
512
513 static const char* shinkos1245_tonecurves(int type, int table)
514 {
515 switch (type) {
516 case TONE_TABLE_STANDARD:
517 switch (table) {
518 case PARAM_TABLE_STANDARD:
519 return "Standard/Standard";
520 case PARAM_TABLE_FINE:
521 return "Standard/Fine";
522 default:
523 return "Standard/Unknown";
524 }
525 case TONE_TABLE_USER:
526 switch (table) {
527 case PARAM_TABLE_STANDARD:
528 return "User/Standard";
529 case PARAM_TABLE_FINE:
530 return "User/Fine";
531 default:
532 return "User/Unknown";
533 }
534 case TONE_TABLE_CURRENT:
535 switch (table) {
536 case PARAM_TABLE_STANDARD:
537 return "Current/Standard";
538 case PARAM_TABLE_FINE:
539 return "Current/Fine";
540 default:
541 return "Current/Unknown";
542 }
543 default:
544 return "Unknown";
545 }
546 }
547
548 static void shinkos1245_dump_status(struct shinkos1245_ctx *ctx,
549 struct shinkos1245_resp_status *sts)
550 {
551 const char *detail;
552 switch (sts->print_status) {
553 case STATUS_PRINTING:
554 detail = "Printing";
555 break;
556 case STATUS_IDLE:
557 detail = "Idle";
558 break;
559 default:
560 detail = "Unknown";
561 break;
562 }
563 INFO("Printer Status: %s\n", detail);
564
565 /* Byteswap */
566 INFO("Printer State: %s # %02x %08x %02x\n",
567 sinfonia_1x45_status_str(sts->state.status1, sts->state.status2, sts->state.error),
568 sts->state.status1, sts->state.status2, sts->state.error);
569 INFO("Counters:\n");
570 INFO("\tLifetime : %u\n", be32_to_cpu(sts->counters.lifetime));
571 INFO("\tThermal Head : %u\n", be32_to_cpu(sts->counters.maint));
572 INFO("\tMedia : %u\n", be32_to_cpu(sts->counters.media));
573 INFO("\tRemaining : %u\n", ctx->marker.levelmax - be32_to_cpu(sts->counters.media));
574 INFO("\tCutter : %u\n", be32_to_cpu(sts->counters.cutter));
575 INFO("Versions:\n");
576 INFO("\tUSB Boot : %u\n", sts->counters.ver_boot);
577 INFO("\tUSB Control : %u\n", sts->counters.ver_ctrl);
578 INFO("\tMain Boot : %u\n", be16_to_cpu(sts->versions.main_boot));
579 INFO("\tMain Control: %u\n", be16_to_cpu(sts->versions.main_control));
580 INFO("\tDSP Boot : %u\n", be16_to_cpu(sts->versions.dsp_boot));
581 INFO("\tDSP Control : %u\n", be16_to_cpu(sts->versions.dsp_control));
582
583 // INFO("USB TypeFlag: %02x\n", sts->counters.control_flag);
584
585 INFO("Bank 1 ID: %u\n", sts->counters2.bank1_id);
586 INFO("\tPrints: %d/%d complete\n",
587 be16_to_cpu(sts->counters2.bank1_complete),
588 be16_to_cpu(sts->counters2.bank1_spec));
589 INFO("Bank 2 ID: %u\n", sts->counters2.bank2_id);
590 INFO("\tPrints: %d/%d complete\n",
591 be16_to_cpu(sts->counters2.bank2_complete),
592 be16_to_cpu(sts->counters2.bank2_spec));
593
594 switch (sts->curve_status) {
595 case CURVE_TABLE_STATUS_INITIAL:
596 detail = "Initial/Default";
597 break;
598 case CURVE_TABLE_STATUS_USERSET:
599 detail = "User Stored";
600 break;
601 case CURVE_TABLE_STATUS_CURRENT:
602 detail = "Current";
603 break;
604 default:
605 detail = "Unknown";
606 break;
607 }
608 INFO("Tone Curve Status: %s\n", detail);
609 }
610
611 static void shinkos1245_dump_media(struct sinfonia_mediainfo_item *medias,
612 int media_8x12,
613 int count)
614 {
615 int i;
616
617 INFO("Loaded media type: %s\n", media_8x12 ? "8x12" : "8x10");
618 INFO("Supported print sizes: %d\n", count);
619
620 for (i = 0 ; i < count ; i++) {
621 INFO("\t %02d: %04u*%04u (%02x/%02u)\n",
622 i,
623 medias[i].columns,
624 medias[i].rows,
625 medias[i].type,
626 medias[i].method);
627 }
628 }
629
630 static int get_tonecurve(struct shinkos1245_ctx *ctx, int type, int table, char *fname)
631 {
632 int ret = 0, num, remaining;
633 uint8_t *data, *ptr;
634
635 struct shinkos1245_cmd_tone cmd;
636 struct shinkos1245_resp_status resp;
637
638 INFO("Dump %s Tone Curve to '%s'\n", shinkos1245_tonecurves(type, table), fname);
639
640 /* Issue a tone_read_start */
641 shinkos1245_fill_hdr(&cmd.hdr);
642 cmd.cmd[0] = 0x0c;
643 cmd.tone[0] = 0x54;
644 cmd.tone[1] = 0x4f;
645 cmd.tone[2] = 0x4e;
646 cmd.tone[3] = 0x45;
647 cmd.cmd2[0] = 0x72;
648 cmd.data.read_write.tone_table = type;
649 cmd.data.read_write.param_table = table;
650
651 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
652 &resp, sizeof(resp), &num);
653
654 if (ret < 0) {
655 ERROR("Failed to execute TONE_READ command\n");
656 return ret;
657 }
658 if (resp.code != CMD_CODE_OK) {
659 ERROR("Bad return code on TONE_READ (%02x)\n",
660 resp.code);
661 return -99;
662 }
663
664 /* Get the data out */
665 remaining = TONE_CURVE_SIZE;
666 data = malloc(remaining);
667 if (!data) {
668 ERROR("Memory Allocation Failure!\n");
669 return -11;
670 }
671 ptr = data;
672
673 while(remaining) {
674 /* Issue a tone_data message */
675 cmd.cmd2[0] = 0x20;
676
677 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
678 &resp, sizeof(resp), &num);
679
680 if (ret < 0) {
681 ERROR("Failed to execute TONE_DATA command\n");
682 goto done;
683 }
684 if (resp.code != CMD_CODE_OK) {
685 ERROR("Bad return code on TONE_DATA (%02x)\n",
686 resp.code);
687 ret = -99;
688 goto done;
689 }
690
691 /* And read back 64-bytes of data */
692 ret = read_data(ctx->conn,
693 ptr, TONE_CURVE_DATA_BLOCK_SIZE, &num);
694 if (num != TONE_CURVE_DATA_BLOCK_SIZE) {
695 ret = -99;
696 goto done;
697 }
698 if (ret < 0)
699 goto done;
700 ptr += num;
701 remaining -= num;
702 }
703
704 /* Issue a tone_end */
705 cmd.cmd2[0] = 0x65;
706 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
707 &resp, sizeof(resp), &num);
708
709 if (ret < 0) {
710 ERROR("Failed to execute TONE_END command\n");
711 goto done;
712 }
713 if (resp.code != CMD_CODE_OK) {
714 ERROR("Bad return code on TONE_END (%02x)\n",
715 resp.code);
716 ret = -99;
717 goto done;
718 }
719
720 /* Open file and write it out */
721 {
722 int tc_fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
723 if (tc_fd < 0) {
724 ret = tc_fd;
725 goto done;
726 }
727
728 ret = write(tc_fd, data, TONE_CURVE_SIZE);
729 if (ret < 0)
730 goto done;
731 close(tc_fd);
732 }
733
734 done:
735 free(data);
736
737 return ret;
738 }
739
740 static int set_tonecurve(struct shinkos1245_ctx *ctx, int type, int table, char *fname)
741 {
742 int ret = 0, num, remaining;
743 uint8_t *data, *ptr;
744
745 struct shinkos1245_cmd_tone cmd;
746 struct shinkos1245_resp_status resp;
747
748 INFO("Read %d/%d Tone Curve from '%s'\n", type, table, fname);
749
750 /* Allocate space */
751 remaining = TONE_CURVE_SIZE;
752 data = malloc(remaining);
753 if (!data) {
754 ERROR("Memory Allocation Failure!\n");
755 return -11;
756 }
757 ptr = data;
758
759
760 /* Read in file */
761 if ((ret = dyesub_read_file(fname, data, TONE_CURVE_SIZE, NULL))) {
762 ERROR("Failed to read Tone Curve file\n");
763 goto done;
764 }
765
766 /* Issue a tone_write_start */
767 shinkos1245_fill_hdr(&cmd.hdr);
768 cmd.cmd[0] = 0x0c;
769 cmd.tone[0] = 0x54;
770 cmd.tone[1] = 0x4f;
771 cmd.tone[2] = 0x4e;
772 cmd.tone[3] = 0x45;
773 cmd.cmd2[0] = 0x77;
774 cmd.data.read_write.tone_table = type;
775 cmd.data.read_write.param_table = table;
776
777 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
778 &resp, sizeof(resp), &num);
779
780 if (ret < 0) {
781 ERROR("Failed to execute TONE_WRITE command\n");
782 goto done;
783 }
784 if (resp.code != CMD_CODE_OK) {
785 ERROR("Bad return code on TONE_WRITE (%02x)\n",
786 resp.code);
787 ret = -99;
788 goto done;
789 }
790
791 while(remaining) {
792 /* Issue a tone_data message */
793 cmd.cmd2[0] = 0x20;
794
795 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
796 &resp, sizeof(resp), &num);
797
798 if (ret < 0) {
799 ERROR("Failed to execute TONE_DATA command\n");
800 goto done;
801 }
802 if (resp.code != CMD_CODE_OK) {
803 ERROR("Bad return code on TONE_DATA (%02x)\n",
804 resp.code);
805 ret = -99;
806 goto done;
807 }
808
809 /* Write 64-bytes of data */
810 ret = send_data(ctx->conn,
811 ptr, TONE_CURVE_DATA_BLOCK_SIZE);
812 if (ret < 0)
813 goto done;
814 ptr += num;
815 remaining -= num;
816 }
817
818 /* Issue a tone_end */
819 cmd.cmd2[0] = 0x65;
820 ret = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
821 &resp, sizeof(resp), &num);
822
823 if (ret < 0) {
824 ERROR("Failed to execute TONE_END command\n");
825 goto done;
826 }
827 if (resp.code != CMD_CODE_OK) {
828 ERROR("Bad return code on TONE_END (%02x)\n",
829 resp.code);
830 ret = -99;
831 goto done;
832 }
833
834 done:
835 free(data);
836
837 return ret;
838 }
839
840
841 /* Driver API */
842
843 static void shinkos1245_cmdline(void)
844 {
845 DEBUG("\t\t[ -m ] # Query media\n");
846 DEBUG("\t\t[ -s ] # Query status\n");
847 DEBUG("\t\t[ -u ] # Query user string\n");
848 DEBUG("\t\t[ -U sometext ] # Set user string\n");
849 DEBUG("\t\t[ -R ] # Reset printer\n");
850 DEBUG("\t\t[ -X jobid ] # Abort a printjob\n");
851 DEBUG("\t\t[ -F ] # Tone curve refers to FINE mode\n");
852 DEBUG("\t\t[ -c filename ] # Get user/NV tone curve\n");
853 DEBUG("\t\t[ -C filename ] # Set user/NV tone curve\n");
854 DEBUG("\t\t[ -l filename ] # Get current tone curve\n");
855 DEBUG("\t\t[ -L filename ] # Set current tone curve\n");
856 }
857
858 static int shinkos1245_cmdline_arg(void *vctx, int argc, char **argv)
859 {
860 struct shinkos1245_ctx *ctx = vctx;
861 int i, j = 0;
862
863 if (!ctx)
864 return -1;
865
866 while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "c:C:l:L:FmRsuU:X:")) >= 0) {
867 switch(i) {
868 GETOPT_PROCESS_GLOBAL
869 case 'F':
870 ctx->tonecurve = PARAM_TABLE_FINE;
871 break;
872 case 'c':
873 j = get_tonecurve(ctx, TONE_TABLE_USER, ctx->tonecurve, optarg);
874 break;
875 case 'C':
876 j = set_tonecurve(ctx, TONE_TABLE_USER, ctx->tonecurve, optarg);
877 break;
878 case 'l':
879 j = get_tonecurve(ctx, TONE_TABLE_CURRENT, ctx->tonecurve, optarg);
880 break;
881 case 'L':
882 j = set_tonecurve(ctx, TONE_TABLE_CURRENT, ctx->tonecurve, optarg);
883 break;
884 case 'm':
885 j = shinkos1245_get_media(ctx);
886 if (!j)
887 shinkos1245_dump_media(ctx->medias, ctx->media_8x12, ctx->num_medias);
888 break;
889 case 'R':
890 j = shinkos1245_reset(ctx);
891 break;
892 case 's': {
893 struct shinkos1245_resp_status sts;
894 j = shinkos1245_get_status(ctx, &sts);
895 if (!j)
896 shinkos1245_dump_status(ctx, &sts);
897 break;
898 }
899 case 'u': {
900 struct shinkos1245_resp_getid resp;
901 j = shinkos1245_get_printerid(ctx, &resp);
902 if (!j) {
903 char buffer[sizeof(resp.data)+1];
904 memcpy(buffer, resp.data, sizeof(resp.data));
905 buffer[sizeof(resp.data)] = 0;
906 INFO("Printer ID: %02x '%s'\n", resp.id, buffer);
907 }
908 break;
909 }
910 case 'U':
911 j = shinkos1245_set_printerid(ctx, optarg);
912 break;
913 case 'X':
914 j = shinkos1245_canceljob(ctx, atoi(optarg));
915 break;
916 default:
917 break; /* Ignore completely */
918 }
919
920 if (j) return j;
921 }
922
923 return CUPS_BACKEND_OK;
924 }
925
926 static void *shinkos1245_init(void)
927 {
928 struct shinkos1245_ctx *ctx = malloc(sizeof(struct shinkos1245_ctx));
929 if (!ctx) {
930 ERROR("Memory Allocation Failure!\n");
931 return NULL;
932 }
933 memset(ctx, 0, sizeof(struct shinkos1245_ctx));
934
935 ctx->tonecurve = PARAM_TABLE_STANDARD;
936
937 return ctx;
938 }
939
940 static int shinkos1245_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid)
941 {
942 struct shinkos1245_ctx *ctx = vctx;
943
944 ctx->conn = conn;
945
946 /* Ensure jobid is sane */
947 ctx->jobid = jobid & 0x7f;
948 if (!ctx->jobid)
949 ctx->jobid++;
950
951 if (test_mode < TEST_MODE_NOATTACH) {
952 /* Query Media */
953 if (shinkos1245_get_media(ctx))
954 return CUPS_BACKEND_FAILED;
955 if (!ctx->num_medias) {
956 ERROR("Media Query Error\n");
957 return CUPS_BACKEND_FAILED;
958 }
959 } else {
960 int media_code = 1;
961 if (getenv("MEDIA_CODE"))
962 media_code = strtol(getenv("MEDIA_CODE"), NULL, 16);
963
964 ctx->media_8x12 = media_code;
965 ctx->num_medias = 0;
966 }
967 ctx->marker.color = "#00FFFF#FF00FF#FFFF00";
968 ctx->marker.name = ctx->media_8x12 ? "8x12" : "8x10";
969 ctx->marker.numtype = ctx->media_8x12;
970 ctx->marker.levelmax = ctx->media_8x12 ? 230 : 280;
971 ctx->marker.levelnow = CUPS_MARKER_UNKNOWN;
972
973 return CUPS_BACKEND_OK;
974 }
975
976 static int shinkos1245_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
977 struct shinkos1245_ctx *ctx = vctx;
978 int ret;
979
980 struct sinfonia_printjob *job = NULL;
981
982 if (!ctx)
983 return CUPS_BACKEND_FAILED;
984
985 job = malloc(sizeof(*job));
986 if (!job) {
987 ERROR("Memory allocation failure!\n");
988 return CUPS_BACKEND_RETRY_CURRENT;
989 }
990 memset(job, 0, sizeof(*job));
991 job->common.jobsize = sizeof(*job);
992
993 /* Common read/parse code */
994 ret = sinfonia_read_parse(data_fd, 1245, job);
995 if (ret) {
996 free(job);
997 return ret;
998 }
999
1000 /* Use larger of our copy counts */
1001 if (job->common.copies < copies)
1002 job->common.copies = copies;
1003
1004 *vjob = job;
1005 return CUPS_BACKEND_OK;
1006 }
1007
1008 static int shinkos1245_main_loop(void *vctx, const void *vjob, int wait_for_return) {
1009 struct shinkos1245_ctx *ctx = vctx;
1010 int i, num, last_state = -1, state = S_IDLE;
1011 struct shinkos1245_resp_status status1, status2;
1012 int copies;
1013
1014 const struct sinfonia_printjob *job = vjob;
1015
1016 if (!ctx)
1017 return CUPS_BACKEND_FAILED;
1018 if (!job)
1019 return CUPS_BACKEND_FAILED;
1020
1021 copies = job->common.copies;
1022
1023 /* Make sure print size is supported */
1024 for (i = 0 ; i < ctx->num_medias ; i++) {
1025 if (job->jp.media == ctx->medias[i].code &&
1026 job->jp.method == ctx->medias[i].method &&
1027 job->jp.rows == ctx->medias[i].rows &&
1028 job->jp.columns == ctx->medias[i].columns)
1029 break;
1030 }
1031 if (i == ctx->num_medias) {
1032 ERROR("Unsupported print type\n");
1033 return CUPS_BACKEND_HOLD;
1034 }
1035
1036 /* Fix max print count. */
1037 if (copies > 9999) // XXX test against remaining media?
1038 copies = 9999;
1039
1040 top:
1041 if (state != last_state) {
1042 if (dyesub_debug)
1043 DEBUG("last_state %d new %d\n", last_state, state);
1044 }
1045
1046 /* Send status query */
1047 i = shinkos1245_get_status(ctx, &status1);
1048 if (i < 0)
1049 return CUPS_BACKEND_FAILED;
1050
1051 if (memcmp(&status1, &status2, sizeof(status1))) {
1052 memcpy(&status2, &status1, sizeof(status1));
1053 // status changed.
1054 } else if (state == last_state) {
1055 sleep(1);
1056 goto top;
1057 }
1058
1059 /* Make sure we're not in an error state */
1060 if (status1.state.status1 == STATE_STATUS1_ERROR)
1061 goto printer_error;
1062
1063 last_state = state;
1064
1065 fflush(logger);
1066
1067 switch (state) {
1068 case S_IDLE:
1069 if (status1.state.status1 == STATE_STATUS1_STANDBY) {
1070 state = S_PRINTER_READY_CMD;
1071 break;
1072 }
1073
1074 #if 0 // XXX is this necessary
1075 if (status1.state.status1 == STATE_STATUS1_WAIT) {
1076 INFO("Printer busy: %s\n",
1077 sinfonia_1x45_status_str(status1.state.status1, status1.state.status2, status1.state.error));
1078 break;
1079 }
1080 #endif
1081 /* If the printer is "busy" check to see if there's any
1082 open memory banks so we can queue the next print */
1083
1084 /* make sure we're not colliding with an existing
1085 jobid */
1086 while (ctx->jobid == status1.counters2.bank1_id ||
1087 ctx->jobid == status1.counters2.bank2_id) {
1088 ctx->jobid++;
1089 ctx->jobid &= 0x7f;
1090 if (!ctx->jobid)
1091 ctx->jobid++;
1092 }
1093
1094 if (!status1.counters2.bank1_remain ||
1095 !status1.counters2.bank2_remain) {
1096 state = S_PRINTER_READY_CMD;
1097 break;
1098 }
1099 break;
1100 case S_PRINTER_READY_CMD: {
1101 struct shinkos1245_cmd_print cmd;
1102
1103 /* Set matte intensity */
1104 if (job->jp.mattedepth != 0x7fffffff) {
1105 int current = -1;
1106 i = shinkos1245_get_matte(ctx, &current);
1107 if (i < 0)
1108 goto printer_error2;
1109 if (current != job->jp.mattedepth) {
1110 i = shinkos1245_set_matte(ctx, job->jp.mattedepth);
1111 if (i < 0)
1112 goto printer_error2;
1113 if (i > 0) {
1114 INFO("Can't set matte intensity when printing in progress...\n");
1115 state = S_IDLE;
1116 sleep(1);
1117 break;
1118 }
1119 }
1120 }
1121
1122 INFO("Sending print job (internal id %u)\n", ctx->jobid);
1123
1124 shinkos1245_fill_hdr(&cmd.hdr);
1125 cmd.cmd[0] = 0x0a;
1126 cmd.cmd[1] = 0x00;
1127
1128 cmd.id = ctx->jobid;
1129 cmd.count = uint16_to_packed_bcd(copies);
1130 cmd.columns = cpu_to_be16(job->jp.columns);
1131 cmd.rows = cpu_to_be16(job->jp.rows);
1132 cmd.media = job->jp.media;
1133 cmd.mode = (job->jp.oc_mode & 0x3f) || ((job->jp.dust & 0x3) << 6);
1134 cmd.combo = job->jp.method;
1135
1136 /* Issue print command */
1137 i = shinkos1245_do_cmd(ctx, &cmd, sizeof(cmd),
1138 &status1, sizeof(status1),
1139 &num);
1140 status1.state.status2 = be32_to_cpu(status1.state.status2);
1141 if (i < 0)
1142 goto printer_error;
1143
1144 /* Check for buffer full state, and wait if we're full */
1145 if (status1.code != CMD_CODE_OK) {
1146 if (status1.print_status == STATUS_PRINTING) {
1147 sleep(1);
1148 break;
1149 } else {
1150 goto printer_error;
1151 }
1152 }
1153
1154 /* Check for error states */
1155 if (status1.state.status1 == STATE_STATUS1_ERROR)
1156 goto printer_error;
1157
1158 /* Send over data */
1159 INFO("Sending image data to printer\n");
1160 if ((i = send_data(ctx->conn,
1161 job->databuf, job->datalen)))
1162 return CUPS_BACKEND_FAILED;
1163
1164 INFO("Waiting for printer to acknowledge completion\n");
1165 sleep(1);
1166 state = S_PRINTER_SENT_DATA;
1167 break;
1168 }
1169 case S_PRINTER_SENT_DATA:
1170 if (!wait_for_return) {
1171 INFO("Fast return mode enabled.\n");
1172 state = S_FINISHED;
1173 }
1174 /* Check for completion */
1175 if (status1.print_status == STATUS_IDLE)
1176 state = S_FINISHED;
1177
1178 break;
1179 default:
1180 break;
1181 }
1182
1183 if (state != S_FINISHED)
1184 goto top;
1185
1186 INFO("Print complete\n");
1187
1188 return CUPS_BACKEND_OK;
1189
1190 printer_error:
1191 ERROR("Printer Error: %s # %02x %08x %02x\n",
1192 sinfonia_1x45_status_str(status1.state.status1, status1.state.status2, status1.state.error),
1193 status1.state.status1, status1.state.status2, status1.state.error);
1194 printer_error2:
1195 return CUPS_BACKEND_FAILED;
1196 }
1197
1198 static int shinkos1245_query_serno(struct dyesub_connection *conn, char *buf, int buf_len)
1199 {
1200 struct shinkos1245_resp_getid resp;
1201 int i;
1202
1203 struct shinkos1245_ctx ctx = {
1204 .conn = conn,
1205 };
1206
1207 i = shinkos1245_get_printerid(&ctx, &resp);
1208 if (i < 0)
1209 return CUPS_BACKEND_FAILED;
1210
1211 for (i = 0 ; i < (int) sizeof(resp.data) && i < buf_len; i++) {
1212 buf[i] = resp.data[i];
1213 }
1214
1215 /* Ensure null-termination */
1216 if (i < buf_len)
1217 buf[i] = 0;
1218 else
1219 buf[buf_len-1] = 0;
1220
1221 return CUPS_BACKEND_OK;
1222 }
1223
1224 static int shinkos1245_query_markers(void *vctx, struct marker **markers, int *count)
1225 {
1226 struct shinkos1245_ctx *ctx = vctx;
1227 struct shinkos1245_resp_status status;
1228
1229 /* Query status */
1230 if (shinkos1245_get_status(ctx, &status))
1231 return CUPS_BACKEND_FAILED;
1232
1233 ctx->marker.levelnow = ctx->marker.levelmax - be32_to_cpu(status.counters.media);
1234
1235 if (markers) *markers = &ctx->marker;
1236 if (markers) *count = 1;
1237
1238 return CUPS_BACKEND_OK;
1239 }
1240
1241 static int shinkos1245_query_stats(void *vctx, struct printerstats *stats)
1242 {
1243 struct shinkos1245_ctx *ctx = vctx;
1244 struct shinkos1245_resp_status status;
1245
1246 if (shinkos1245_query_markers(ctx, NULL, NULL))
1247 return CUPS_BACKEND_FAILED;
1248
1249 if (shinkos1245_get_status(ctx, &status))
1250 return CUPS_BACKEND_FAILED;
1251
1252 stats->mfg = "Sinfonia";
1253 stats->model = "E1 / S1245";
1254
1255 if (shinkos1245_query_serno(ctx->conn,
1256 ctx->serial, sizeof(ctx->serial)))
1257 return CUPS_BACKEND_FAILED;
1258
1259 stats->serial = ctx->serial;
1260
1261 snprintf(ctx->fwver, sizeof(ctx->fwver)-1,
1262 "%d / %d", be16_to_cpu(status.versions.main_control),
1263 be16_to_cpu(status.versions.dsp_control));
1264 stats->fwver = ctx->fwver;
1265
1266 stats->decks = 1;
1267 stats->mediatype[0] = ctx->marker.name;
1268 stats->levelmax[0] = ctx->marker.levelmax;
1269 stats->levelnow[0] = ctx->marker.levelnow;
1270 stats->name[0] = "Roll";
1271 stats->status[0] = strdup(sinfonia_1x45_status_str(status.state.status1, status.state.status2, status.state.error));
1272 stats->cnt_life[0] = be32_to_cpu(status.counters.lifetime);
1273
1274 return CUPS_BACKEND_OK;
1275 }
1276
1277 static const char *shinkos1245_prefixes[] = {
1278 "shinkos1245", /* Family Name */
1279 NULL
1280 };
1281
1282 static const struct device_id shinkos1245_devices[] = {
1283 { 0x10ce, 0x0007, P_SHINKO_S1245, NULL, "shinko-chcs1245"},
1284 { 0x10ce, 0x0007, P_SHINKO_S1245, NULL, "sinfonia-chcs1245"}, /* Duplicate */
1285 { 0, 0, 0, NULL, NULL}
1286 };
1287
1288 const struct dyesub_backend shinkos1245_backend = {
1289 .name = "Shinko/Sinfonia CHC-S1245/E1",
1290 .version = "0.35" " (lib " LIBSINFONIA_VER ")",
1291 .uri_prefixes = shinkos1245_prefixes,
1292 .devices = shinkos1245_devices,
1293 .cmdline_usage = shinkos1245_cmdline,
1294 .cmdline_arg = shinkos1245_cmdline_arg,
1295 .init = shinkos1245_init,
1296 .attach = shinkos1245_attach,
1297 .cleanup_job = sinfonia_cleanup_job,
1298 .read_parse = shinkos1245_read_parse,
1299 .main_loop = shinkos1245_main_loop,
1300 .query_serno = shinkos1245_query_serno,
1301 .query_markers = shinkos1245_query_markers,
1302 .query_stats = shinkos1245_query_stats,
1303 };
1304
1305 /* CHC-S1245 data format
1306
1307 Spool file consists of an 116-byte header, followed by RGB-packed data,
1308 followed by a 4-byte footer. Header appears to consist of a series of
1309 4-byte Little Endian words.
1310
1311 10 00 00 00 MM MM 00 00 00 00 00 00 01 00 00 00 MM == Model (ie 1245d)
1312 64 00 00 00 00 00 00 00 TT 00 00 00 00 00 00 00 TT == Media Size (0x10 fixed)
1313 MM 00 00 00 PP 00 00 00 00 00 00 00 ZZ ZZ ZZ ZZ MM = Print Method (aka cut control), PP = Default/Glossy/Matte (0x01/0x03/0x05), ZZ == matte intensity (0x7fffffff for glossy, else 0x00000000 +- 25 for matte)
1314 VV 00 00 00 WW WW 00 00 HH HH 00 00 XX XX 00 00 VV == dust; 0x00 default, 0x01 off, 0x02 on, XX == Copies
1315 00 00 00 00 00 00 00 00 00 00 00 00 ce ff ff ff
1316 00 00 00 00 ce ff ff ff QQ QQ 00 00 ce ff ff ff QQ == DPI, ie 300.
1317 00 00 00 00 ce ff ff ff 00 00 00 00 00 00 00 00
1318 00 00 00 00
1319
1320 [[Packed RGB payload of WW*HH*3 bytes]]
1321
1322 04 03 02 01 [[ footer ]]
1323
1324
1325 */