Debian Codegraph @

src/cups/backend_magicard.c

raw symbols SCIP
1 /*
2 * Magicard card printer family CUPS backend
3 *
4 * (c) 2017-2025 Solomon Peachy <[email protected]>
5 *
6 * The latest version of this program can be found at:
7 *
8 * https://git.shaftnet.org/gitea/slp/selphy_print.git
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 *
23 * SPDX-License-Identifier: GPL-2.0+
24 *
25 */
26
27 #define BACKEND magicard_backend
28
29 #include "backend_common.h"
30
31 #include <time.h>
32
33 /* Gamma tables computed with this perl program:
34
35 my $input_bpp = 8;
36 my $output_bpp = 6;
37 my $gamma = 1/1.8; # or 1/2.2 or whatever.
38
39 my $i;
40
41 for (my $i = 0 ; $i < (2 ** $input_bpp) ; $i++) {
42 my $linear = $i / (2 ** $input_bpp);
43 my $gc = ($linear ** $gamma) * (2 ** $output_bpp);
44 $gc = int($gc);
45 print "$gc, ";
46 }
47
48 */
49
50 static uint8_t gammas[2][256] = {
51 /* Gamma = 2.2 */
52 {
53 0, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 15, 16, 17,
54 17, 18, 18, 19, 19, 20, 20, 20, 21, 21, 22, 22, 23, 23, 23,
55 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28,
56 29, 29, 29, 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, 32, 32,
57 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35, 35, 36, 36,
58 36, 36, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39,
59 39, 39, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 42, 42, 42,
60 42, 42, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45,
61 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47,
62 47, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 50,
63 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 52, 52,
64 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54,
65 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56,
66 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58,
67 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60,
68 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, 61, 61, 62,
69 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63,
70 },
71 /* Gamma = 1.8 */
72 {
73 0, 2, 4, 5, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12,
74 13, 13, 14, 14, 15, 15, 15, 16, 16, 17, 17, 17, 18, 18, 19,
75 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24,
76 24, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 28, 28,
77 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 32,
78 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35,
79 35, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 38, 38, 38, 38,
80 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41,
81 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 44, 44, 44, 44,
82 44, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47,
83 47, 47, 47, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49,
84 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 52, 52, 52,
85 52, 52, 52, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54,
86 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 56, 57, 57,
87 57, 57, 57, 57, 57, 58, 58, 58, 58, 58, 58, 58, 59, 59, 59,
88 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61,
89 61, 61, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63,
90 }
91 };
92
93 struct magicard_printjob {
94 struct dyesub_job_common common;
95
96 uint8_t *databuf;
97 int datalen;
98
99 int hdr_len;
100 };
101
102 /* Private data structure */
103 struct magicard_ctx {
104 struct dyesub_connection *conn;
105 struct marker marker;
106 };
107
108 struct magicard_cmd_header {
109 uint8_t guard[9]; /* 0x05 */
110 uint8_t guard2[1]; /* 0x01 */
111 uint8_t cmd[4]; /* 'REQ,' */
112 uint8_t subcmd[4]; /* '???,' */
113 uint8_t arg[4]; /* '???,' */
114 uint8_t footer[2]; /* 0x1c 0x03 */
115 };
116
117 struct magicard_cmd_simple_header {
118 uint8_t guard[9]; /* 0x05 */
119 uint8_t guard2[1]; /* 0x01 */
120 uint8_t cmd[]; /* '???' */
121 // uint8_t footer[2]; /* 0x1c 0x03 */
122 };
123
124 struct magicard_resp_header {
125 uint8_t guard[1]; /* 0x01 */
126 uint8_t subcmd_arg[7]; /* '???,???' */
127 uint8_t data[]; /* freeform resp */
128 // uint8_t term[2]; /* 0x2c 0x03 terminates! */
129 };
130
131 struct magicard_requests {
132 const char *key;
133 const char *desc;
134 uint8_t type;
135 };
136
137 enum {
138 TYPE_UNKNOWN = 0,
139 TYPE_STRING,
140 TYPE_STRINGINT,
141 TYPE_STRINGINT_HEX,
142 TYPE_IPADDR,
143 TYPE_YESNO,
144 TYPE_MODEL,
145 };
146
147 /* Data definitions */
148 static struct magicard_requests magicard_sta_requests[] = {
149 { "MSR", "Printer Serial Number", TYPE_STRING },
150 { "PSR", "Print Head Serial Number", TYPE_STRING },
151 { "BSR", "PCB Serial Number", TYPE_STRING },
152 { "VRS", "Firmware Version", TYPE_STRING },
153 { "FDC", "Head Density", TYPE_STRINGINT }, /* 25 per step */
154 { "FSP", "Image Start", TYPE_STRINGINT }, /* 8 steps per pixel */
155 { "FEP", "Image End", TYPE_STRINGINT }, /* 8 steps per pixel */
156 { "FSS", "Ramp Adjust", TYPE_STRINGINT },
157 { "FPP", "Head Position", TYPE_STRINGINT }, /* L-R alignment */
158 { "MDL", "Model", TYPE_MODEL }, /* 0 == Standard. Others? */
159 { "PID", "USB PID", TYPE_STRINGINT_HEX }, /* ASCII integer, but needs to be shown as hex */
160 { "VID", "USB VID", TYPE_STRINGINT_HEX }, /* ASCII integer, but needs to be shown as hex */
161 { "USN", "USB Serial Number", TYPE_STRING },
162 { "UPN", "USB Manufacturer", TYPE_STRING },
163 { "MAC", "Ethernet MAC Address", TYPE_STRING },
164 { "DYN", "Dynamic Address", TYPE_YESNO }, /* 1 == yes, 0 == no */
165 { "IPA", "IP Address", TYPE_IPADDR }, /* ASCII signed integer */
166 { "SNM", "IP Netmask", TYPE_IPADDR }, /* ASCII signed integer */
167 { "GWY", "IP Gateway", TYPE_IPADDR }, /* ASCII signed integer */
168
169 { "TCQ", "Total Cards Printed", TYPE_STRINGINT },
170 { "TCP", "Prints on Head", TYPE_STRINGINT },
171 { "TCN", "Cleaning Cycles", TYPE_STRINGINT },
172 { "CCQ", "Cards Since Last Cleaning", TYPE_STRINGINT },
173 { "TPQ", "Total Panels Printed", TYPE_STRINGINT },
174 { "CCP", "Cards between Cleaning Prompts", TYPE_STRINGINT },
175 { "CPQ", "Panels Since Last Cleaning", TYPE_STRINGINT },
176 { "DFR", "Panels Remaining", TYPE_STRINGINT }, // cook somehow?
177 { "CLP", "Cleaning Prompt", TYPE_STRING },
178
179 // CRQ: OFF ?? Cleaning overdue?
180 // CHK: checksum of fw? (8 chars, hex?)
181 // TES: ??? signed int? IP addr?
182 // RAMP: ??? hangs.
183
184 { NULL, NULL, 0 }
185 };
186
187 // Sensors: CAM1 CAM2 TACHO FLIP DYE BARCODE LID FRONT REAR BUTTON TEMP ON OFF
188 // Languages: ENG ITA POR FRA DEU ESP SCH
189
190 /* Helper functions */
191 static int magicard_build_cmd(uint8_t *buf,
192 const char *cmd, const char *subcmd, const char *arg)
193 {
194 struct magicard_cmd_header *hdr = (struct magicard_cmd_header *) buf;
195
196 memset(hdr->guard, 0x05, sizeof(hdr->guard));
197 hdr->guard2[0] = 0x01;
198 memcpy(hdr->cmd, cmd, 3);
199 hdr->cmd[3] = ',';
200 memcpy(hdr->subcmd, subcmd, 3);
201 hdr->subcmd[3] = ',';
202 memcpy(hdr->arg, arg, 3);
203 hdr->arg[3] = ',';
204 hdr->footer[0] = 0x1c;
205 hdr->footer[1] = 0x03;
206
207 return sizeof(*hdr);
208 }
209
210 static int magicard_build_cmd_simple(uint8_t *buf,
211 const char *cmd)
212 {
213 struct magicard_cmd_simple_header *hdr = (struct magicard_cmd_simple_header *) buf;
214 int len = strlen(cmd);
215
216 memset(hdr->guard, 0x05, sizeof(hdr->guard));
217 hdr->guard2[0] = 0x01;
218 strncpy((char*)hdr->cmd, cmd, len);
219 hdr->cmd[len] = 0x1c;
220 hdr->cmd[len+1] = 0x03;
221
222 return (sizeof(*hdr) + len + 2);
223 }
224
225
226 static uint8_t * magicard_parse_resp(uint8_t *buf, uint16_t len, uint16_t *resplen)
227 {
228 struct magicard_resp_header *hdr = (struct magicard_resp_header *) buf;
229
230 *resplen = len - sizeof(hdr->guard) - sizeof(hdr->subcmd_arg) - 2;
231
232 return hdr->data;
233 }
234
235 static int magicard_query_sensors(struct magicard_ctx *ctx)
236 {
237 int ret = 0;
238 int i;
239 uint8_t buf[256];
240 char buf2[24];
241
242 for (i = 1 ; ; i++) {
243 int num = 0;
244
245 snprintf(buf2, sizeof(buf2), "SNR%d", i);
246 ret = magicard_build_cmd_simple(buf, buf2);
247
248 if ((ret = send_data(ctx->conn,
249 buf, ret)))
250 return ret;
251
252 memset(buf, 0, sizeof(buf));
253
254 ret = read_data(ctx->conn,
255 buf, sizeof(buf), &num);
256
257 if (ret < 0)
258 return ret;
259
260 if (!memcmp(buf, "END", 3))
261 break;
262
263 buf[num] = 0;
264 INFO("%s\n", buf);
265 }
266 return CUPS_BACKEND_OK;
267 }
268
269 static int magicard_selftest_card(struct magicard_ctx *ctx)
270 {
271 int ret = 0;
272 uint8_t buf[256];
273
274 ret = magicard_build_cmd_simple(buf, "TST,");
275
276 ret = send_data(ctx->conn,
277 buf, ret);
278 return ret;
279 }
280
281 static int magicard_reset(struct magicard_ctx *ctx)
282 {
283 int ret = 0;
284 uint8_t buf[256];
285
286 ret = magicard_build_cmd_simple(buf, "RST,");
287
288 ret = send_data(ctx->conn,
289 buf, ret);
290 return ret;
291 }
292
293 static int magicard_eject(struct magicard_ctx *ctx)
294 {
295 int ret = 0;
296 uint8_t buf[256];
297
298 ret = magicard_build_cmd_simple(buf, "EJT,");
299
300 ret = send_data(ctx->conn,
301 buf, ret);
302 return ret;
303 }
304
305 static int magicard_query_printer(struct magicard_ctx *ctx)
306 {
307 int ret = 0;
308 int i;
309 uint8_t buf[256];
310 char buf2[24];
311
312 for (i = 1 ; ; i++) {
313 int num = 0;
314
315 snprintf(buf2, sizeof(buf2), "QPR%d", i);
316 ret = magicard_build_cmd_simple(buf, buf2);
317
318 if ((ret = send_data(ctx->conn,
319 buf, ret)))
320 return ret;
321
322 memset(buf, 0, sizeof(buf));
323
324 ret = read_data(ctx->conn,
325 buf, sizeof(buf), &num);
326
327 if (ret < 0)
328 return ret;
329
330 if (!memcmp(buf, "END", 3))
331 break;
332
333 buf[num] = 0;
334 INFO("%s\n", buf);
335 }
336 return CUPS_BACKEND_OK;
337 }
338
339 static int magicard_query_status(struct magicard_ctx *ctx)
340 {
341 int ret = 0;
342 int i;
343 uint8_t buf[256];
344
345 for (i = 0 ; ; i++) {
346 uint16_t resplen = 0;
347 uint8_t *resp;
348 int num = 0;
349
350 if (magicard_sta_requests[i].key == NULL)
351 break;
352
353 ret = magicard_build_cmd(buf, "REQ", "STA",
354 magicard_sta_requests[i].key);
355
356 if ((ret = send_data(ctx->conn,
357 buf, ret)))
358 return ret;
359
360 memset(buf, 0, sizeof(buf));
361
362 ret = read_data(ctx->conn,
363 buf, sizeof(buf), &num);
364
365 if (ret < 0)
366 return ret;
367
368 resp = magicard_parse_resp(buf, num, &resplen);
369 resp[resplen] = 0;
370 switch(magicard_sta_requests[i].type) {
371 case TYPE_IPADDR: {
372 int32_t ipaddr;
373 uint8_t *addr = (uint8_t *) &ipaddr;
374 ipaddr = atoi((char*)resp);
375 INFO("%s:\t%d.%d.%d.%d\n",
376 magicard_sta_requests[i].desc,
377 addr[3], addr[2], addr[1], addr[0]);
378 break;
379 }
380 case TYPE_YESNO: {
381 int val = atoi((char*)resp);
382 INFO("%s:\t%s\n",
383 magicard_sta_requests[i].desc,
384 val? "Yes" : "No");
385 break;
386 }
387 case TYPE_MODEL: {
388 int val = atoi((char*)resp);
389 INFO("%s:\t%s\n",
390 magicard_sta_requests[i].desc,
391 val == 0? "Standard" : "Unknown");
392 break;
393 }
394 case TYPE_STRINGINT_HEX: {
395 int val = atoi((char*)resp);
396 INFO("%s:\t%X\n",
397 magicard_sta_requests[i].desc,
398 val);
399 break;
400 }
401 case TYPE_STRINGINT:
402 // treat differently?
403 case TYPE_STRING:
404 case TYPE_UNKNOWN:
405 default:
406 INFO("%s:\t%s\n",
407 magicard_sta_requests[i].desc,
408 resp);
409 }
410 }
411
412 return ret;
413 }
414
415 /* Main driver */
416 static void* magicard_init(void)
417 {
418 struct magicard_ctx *ctx = malloc(sizeof(struct magicard_ctx));
419 if (!ctx) {
420 ERROR("Memory Allocation Failure!\n");
421 return NULL;
422 }
423 memset(ctx, 0, sizeof(struct magicard_ctx));
424 return ctx;
425 }
426
427 static int magicard_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid)
428 {
429 struct magicard_ctx *ctx = vctx;
430
431 UNUSED(jobid);
432
433 ctx->conn = conn;
434
435 ctx->marker.color = "#00FFFF#FF00FF#FFFF00"; // XXX YMCK too!
436 ctx->marker.name = "Unknown"; // LC1/LC3/LC6/LC8
437 ctx->marker.numtype = -1;
438 ctx->marker.levelmax = CUPS_MARKER_UNAVAILABLE;
439 ctx->marker.levelnow = CUPS_MARKER_UNKNOWN;
440
441 return CUPS_BACKEND_OK;
442 }
443
444 static void magicard_cleanup_job(const void *vjob)
445 {
446 const struct magicard_printjob *job = vjob;
447
448 if (job->databuf)
449 free(job->databuf);
450
451 free((void*)job);
452 }
453
454 static void downscale_and_extract(int gamma, uint32_t pixels,
455 uint8_t *y_i, uint8_t *m_i, uint8_t *c_i,
456 uint8_t *y_o, uint8_t *m_o, uint8_t *c_o, uint8_t *k_o)
457 {
458 uint32_t i;
459
460 for (i = 0 ; i < pixels; i++)
461 {
462 uint8_t y, m, c;
463 uint8_t k = 0;
464 uint32_t j;
465 uint32_t row;
466 uint32_t col;
467 uint32_t b_offset;
468 uint8_t b_shift;
469
470 /* Downscale color planes from 8bpp -> 6bpp; */
471 if (gamma) {
472 if (gamma > 2)
473 gamma = 2;
474 gamma--;
475 y = gammas[gamma][*y_i++];
476 m = gammas[gamma][*m_i++];
477 c = gammas[gamma][*c_i++];
478 } else {
479 y = *y_i++ >> 2;
480 m = *m_i++ >> 2;
481 c = *c_i++ >> 2;
482 }
483
484 /* Extract "true black" from ymc data, if enabled */
485 if (k_o && y == 0x3f && m == 0x3f && c == 0x3f) {
486 k = 1;
487 y = m = c = 0;
488 }
489
490 /* Compute row number and offsets */
491 row = i / 672;
492 col = i - (row * 672);
493 b_offset = col / 8;
494 b_shift = 7 - (col - (b_offset * 8));
495
496 /* Now, for each row, break it down into sub-chunks */
497 for (j = 0 ; j < 6 ; j++) {
498 if (b_shift == 7) {
499 y_o[row * 504 + j * 84 + b_offset] = 0;
500 m_o[row * 504 + j * 84 + b_offset] = 0;
501 c_o[row * 504 + j * 84 + b_offset] = 0;
502 }
503 if (y & (1 << j))
504 y_o[row * 504 + j * 84 + b_offset] |= (1 << b_shift);
505 if (m & (1 << j))
506 m_o[row * 504 + j * 84 + b_offset] |= (1 << b_shift);
507 if (c & (1 << j))
508 c_o[row * 504 + j * 84 + b_offset] |= (1 << b_shift);
509 }
510
511 /* And resin black, if enabled */
512 if (k_o) {
513 if (b_shift == 7) {
514 k_o[row * 84 + b_offset] = 0;
515 }
516 if (k)
517 k_o[row * 84 + b_offset] |= (1 << b_shift);
518 }
519 }
520 }
521
522 #define MAX_HEADERS_LEN 2048
523 #define MAX_PRINTJOB_LEN (1016*672*4) + MAX_HEADERS_LEN /* 1016*672 * 4color */
524 #define INITIAL_BUF_LEN 1024
525 static int magicard_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
526 struct magicard_ctx *ctx = vctx;
527 uint8_t initial_buf[INITIAL_BUF_LEN + 1];
528 uint32_t buf_offset = 0;
529 int i;
530
531 uint8_t *in_y, *in_m, *in_c;
532 uint8_t *out_y, *out_m, *out_c, *out_k;
533 uint32_t len_y = 0, len_m = 0, len_c = 0, len_k = 0;
534 int gamma = 0;
535
536 uint8_t x_gp_8bpp;
537 uint8_t x_gp_rk;
538 uint8_t k_only;
539
540 struct magicard_printjob *job = NULL;
541
542 if (!ctx)
543 return CUPS_BACKEND_FAILED;
544
545 job = malloc(sizeof(*job));
546 if (!job) {
547 ERROR("Memory allocation failure!\n");
548 return CUPS_BACKEND_RETRY_CURRENT;
549 }
550 memset(job, 0, sizeof(*job));
551 job->common.jobsize = sizeof(*job);
552 job->common.copies = copies;
553
554 /* Read in the first chunk */
555 i = read(data_fd, initial_buf, INITIAL_BUF_LEN);
556 if (i < 0) {
557 magicard_cleanup_job(job);
558 return i;
559 } else if (i == 0) {
560 magicard_cleanup_job(job);
561 return CUPS_BACKEND_CANCEL; /* Ie no data, we're done */
562 } else if (i < INITIAL_BUF_LEN) {
563 magicard_cleanup_job(job);
564 return CUPS_BACKEND_CANCEL;
565 }
566
567 /* Basic Sanity Check */
568 if (initial_buf[0] != 0x05 ||
569 initial_buf[64] != 0x01 ||
570 initial_buf[65] != 0x2c) {
571 ERROR("Unrecognized header data format @%d!\n", job->datalen);
572 magicard_cleanup_job(job);
573 return CUPS_BACKEND_CANCEL;
574 }
575
576 initial_buf[INITIAL_BUF_LEN] = 0;
577
578 /* We can start allocating! */
579 if (job->databuf) {
580 free(job->databuf);
581 job->databuf = NULL;
582 }
583 job->datalen = 0;
584 job->databuf = malloc(MAX_PRINTJOB_LEN);
585 if (!job->databuf) {
586 ERROR("Memory allocation failure!\n");
587 magicard_cleanup_job(job);
588 return CUPS_BACKEND_RETRY_CURRENT;
589 }
590
591 /* Copy over initial header */
592 memcpy(job->databuf + job->datalen, initial_buf + buf_offset, 65);
593 job->datalen += 65;
594 buf_offset += 65;
595
596 /* Start parsing headers */
597 x_gp_8bpp = x_gp_rk = k_only = job->hdr_len = 0;
598
599 char *ptr;
600 ptr = strtok((char*)initial_buf + ++buf_offset, ",\x1c");
601 while (ptr
602 && ((ptr - (char*)initial_buf) < INITIAL_BUF_LEN)
603 && ((ptr - (char*)initial_buf) + strnlen(ptr, INITIAL_BUF_LEN) < INITIAL_BUF_LEN)
604 && *ptr != 0x1c) {
605 if (!strcmp("X-GP-8", ptr)) {
606 x_gp_8bpp = 1;
607 } else if (!strncmp("TDT", ptr, 3)) {
608 /* Strip out the timestamp, replace it with one from the backend */
609 } else if (!strncmp("IMF", ptr,3)) {
610 /* Strip out the image format, replace it with backend */
611 // } else if (!strncmp("ESS", ptr, 3)) {
612 // /* Strip out copies */
613 // XXX implement me properly.
614 copies = 1;
615 } else if (!strcmp("X-GP-RK", ptr)) {
616 x_gp_rk = 1;
617 } else if (!strncmp("ICC", ptr,3)) {
618 /* Gamma curve is not handled by printer,
619 strip it out and use it! */
620 gamma = atoi(ptr + 3);
621 } else if (!strncmp("SZ", ptr, 2)) {
622 if (ptr[2] == 'B') {
623 len_y = atoi(ptr + 3);
624 } else if (ptr[2] == 'G') {
625 len_m = atoi(ptr + 3);
626 } else if (ptr[2] == 'R') {
627 len_c = atoi(ptr + 3);
628 } else if (ptr[2] == 'K') {
629 len_k = atoi(ptr + 3);
630 }
631 } else {
632 /* Safety valve */
633 if (strlen(ptr) + job->datalen > MAX_HEADERS_LEN) {
634 ERROR("headers too long, bogus job!\n");
635 magicard_cleanup_job(job);
636 return CUPS_BACKEND_CANCEL;
637 }
638
639 /* Everything else goes in */
640 job->datalen += sprintf((char*)job->databuf + job->datalen, ",%s", ptr);
641 }
642
643 /* Keep going */
644 buf_offset += strlen(ptr) + 1;
645 /* Peek ahead to see if this is it */
646 if (initial_buf[buf_offset + 1] == 0x1c)
647 break;
648 /* Otherwise continue to the next token */
649 ptr = strtok(NULL, ",\x1c");
650 }
651
652 /* Sanity checks */
653 if (!len_y || !len_m || !len_c) {
654 ERROR("Plane lengths missing? %u/%u/%u!\n", len_y, len_m, len_c);
655 magicard_cleanup_job(job);
656 return CUPS_BACKEND_CANCEL;
657 }
658 if (len_y != len_m || len_y != len_c) {
659 ERROR("Inconsistent data plane lengths! %u/%u/%u!\n", len_y, len_m, len_c);
660 magicard_cleanup_job(job);
661 return CUPS_BACKEND_CANCEL;
662 }
663 if (x_gp_rk && len_k) {
664 ERROR("Data stream already has a K layer!\n");
665 magicard_cleanup_job(job);
666 return CUPS_BACKEND_CANCEL;
667 }
668
669 /* Generate a timestamp */
670 job->datalen += sprintf((char*)job->databuf + job->datalen, ",TDT%08X", (uint32_t) time(NULL));
671
672 /* Generate image format tag */
673 if (k_only == 1) {
674 job->datalen += sprintf((char*)job->databuf + job->datalen, ",IMFK");
675 } else if (x_gp_rk || len_k) {
676 /* We're adding K, so make this BGRK */
677 job->datalen += sprintf((char*)job->databuf + job->datalen, ",IMFBGRK");
678 } else {
679 /* Just BGR */
680 job->datalen += sprintf((char*)job->databuf + job->datalen, ",IMFBGR");
681 }
682
683 /* Insert SZB/G/R/K length descriptors */
684 if (x_gp_8bpp) {
685 if (k_only == 1) {
686 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZK%u", len_c / 8);
687 } else {
688 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZB%u", len_y * 6 / 8);
689 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZG%u", len_m * 6 / 8);
690 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZR%u", len_c * 6 / 8);
691 /* Add in a SZK length indication if requested */
692 if (x_gp_rk == 1) {
693 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZK%u", len_c / 8);
694 }
695 }
696 } else {
697 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZB%u", len_y);
698 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZG%u", len_m);
699 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZR%u", len_c);
700 /* Add in a SZK length indication if requested */
701 if (len_k) {
702 job->datalen += sprintf((char*)job->databuf + job->datalen, ",SZK%u", len_k);
703 }
704 }
705
706 /* Terminate command stream */
707 job->databuf[job->datalen++] = 0x1c;
708
709 /* Let's figure out how long the image data stream is supposed to be. */
710 uint32_t remain;
711 if (k_only) {
712 remain = len_k + 3;
713 } else {
714 remain = len_y + len_m + len_c + 3 * 3;
715 if (len_k)
716 remain += len_k + 3;
717 }
718 /* Offset the stuff we already read in. */
719 remain -= INITIAL_BUF_LEN - buf_offset;
720 remain++; /* Add in a byte for the end of job marker. This is our final value. */
721
722 /* This is how much of the initial buffer is the header length. */
723 job->hdr_len = job->datalen;
724
725 if (job->datalen + remain > MAX_PRINTJOB_LEN) {
726 ERROR("Buffer overflow when parsing printjob! (%d+%d)\n",
727 job->datalen, remain);
728 magicard_cleanup_job(job);
729 return CUPS_BACKEND_CANCEL;
730 }
731
732 if (x_gp_8bpp) {
733 uint32_t srcbuf_offset = INITIAL_BUF_LEN - buf_offset;
734 uint8_t *srcbuf = malloc(MAX_PRINTJOB_LEN);
735 if (!srcbuf) {
736 magicard_cleanup_job(job);
737 ERROR("Memory allocation failure!\n");
738 return CUPS_BACKEND_RETRY_CURRENT;
739 }
740
741 memcpy(srcbuf, initial_buf + buf_offset, srcbuf_offset);
742
743 /* Finish loading the data */
744 while (remain > 0) {
745 i = read(data_fd, srcbuf + srcbuf_offset, remain);
746 if (i < 0) {
747 ERROR("Data Read Error: %d (%u) @%u)\n", i, remain, srcbuf_offset);
748 magicard_cleanup_job(job);
749 free(srcbuf);
750 return i;
751 }
752 if (i == 0) {
753 ERROR("Short read! (%d/%u)\n", i, remain);
754 magicard_cleanup_job(job);
755 free(srcbuf);
756 return CUPS_BACKEND_CANCEL;
757 }
758 srcbuf_offset += i;
759 remain -= i;
760 }
761
762 // XXX handle conversion of K-only jobs. if needed.
763
764 /* set up source pointers */
765 in_y = srcbuf;
766 in_m = in_y + len_y + 3;
767 in_c = in_m + len_m + 3;
768
769 /* Set up destination pointers */
770 out_y = job->databuf + job->datalen;
771 out_m = out_y + (len_y * 6 / 8) + 3;
772 out_c = out_m + (len_m * 6 / 8) + 3;
773 out_k = out_c + (len_c * 6 / 8) + 3;
774
775 /* Termination of each plane */
776 memcpy(out_m - 3, in_y + len_y, 3);
777 memcpy(out_c - 3, in_m + len_m, 3);
778 memcpy(out_k - 3, in_c + len_c, 3);
779
780 if (!x_gp_rk)
781 out_k = NULL;
782
783 INFO("Converting image data to printer's native format %s\n", x_gp_rk ? "and extracting K channel" : "");
784
785 downscale_and_extract(gamma, len_y, in_y, in_m, in_c,
786 out_y, out_m, out_c, out_k);
787
788 /* Pad out the length appropriately. */
789 job->datalen += ((len_c * 6 / 8) + 3) * 3;
790
791 /* If there's a K plane, compute length.. */
792 if (out_k) {
793 job->datalen += (len_c / 8);
794 job->databuf[job->datalen++] = 0x1c;
795 job->databuf[job->datalen++] = 0x4b;
796 job->databuf[job->datalen++] = 0x3a;
797 }
798
799 /* Terminate the entire stream */
800 job->databuf[job->datalen++] = 0x03;
801
802 free(srcbuf);
803 } else {
804 uint32_t srcbuf_offset = INITIAL_BUF_LEN - buf_offset;
805 memcpy(job->databuf + job->datalen, initial_buf + buf_offset, srcbuf_offset);
806 job->datalen += srcbuf_offset;
807
808 /* Finish loading the data */
809 while (remain > 0) {
810 i = read(data_fd, job->databuf + job->datalen, remain);
811 if (i < 0) {
812 ERROR("Data Read Error: %d (%u) @%d)\n", i, remain, job->datalen);
813 magicard_cleanup_job(job);
814 return i;
815 }
816 if (i == 0) {
817 magicard_cleanup_job(job);
818 ERROR("Short read! (%d/%u)\n", i, remain);
819 return CUPS_BACKEND_CANCEL;
820 }
821 job->datalen += i;
822 remain -= i;
823 }
824 }
825
826 *vjob = job;
827
828 return CUPS_BACKEND_OK;
829 }
830
831 static int magicard_main_loop(void *vctx, const void *vjob, int wait_for_return) {
832 struct magicard_ctx *ctx = vctx;
833 int ret;
834 int copies;
835 (void)wait_for_return;
836
837 const struct magicard_printjob *job = vjob;
838
839 // XXX printer handles copy generation..
840 // but it's a numeric parameter. Bleh.
841 if (!ctx)
842 return CUPS_BACKEND_FAILED;
843 if (!job)
844 return CUPS_BACKEND_FAILED;
845
846 copies = job->common.copies;
847 top:
848 if ((ret = send_data(ctx->conn,
849 job->databuf, job->hdr_len)))
850 return CUPS_BACKEND_FAILED;
851
852 if ((ret = send_data(ctx->conn,
853 job->databuf + job->hdr_len, job->datalen - job->hdr_len)))
854 return CUPS_BACKEND_FAILED;
855
856 /* Clean up */
857 if (terminate)
858 copies = 1;
859
860 INFO("Print complete (%d copies remaining)\n", copies - 1);
861
862 if (copies && --copies) {
863 goto top;
864 }
865
866 return CUPS_BACKEND_OK;
867 }
868
869 static void magicard_cmdline(void)
870 {
871 DEBUG("\t\t[ -s ] # Query status\n");
872 DEBUG("\t\t[ -q ] # Query information\n");
873 DEBUG("\t\t[ -I ] # Query printer sensors\n");
874 DEBUG("\t\t[ -E ] # Eject card\n");
875 DEBUG("\t\t[ -T ] # Print self-test card\n");
876 DEBUG("\t\t[ -R ] # Reset printer\n");
877 }
878
879 static int magicard_cmdline_arg(void *vctx, int argc, char **argv)
880 {
881 struct magicard_ctx *ctx = vctx;
882 int i, j = 0;
883
884 if (!ctx)
885 return -1;
886
887 while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "sqEIRT")) >= 0) {
888 switch(i) {
889 GETOPT_PROCESS_GLOBAL
890 case 's':
891 j = magicard_query_status(ctx);
892 break;
893 case 'q':
894 j = magicard_query_printer(ctx);
895 break;
896 case 'E':
897 j = magicard_eject(ctx);
898 break;
899 case 'I':
900 j = magicard_query_sensors(ctx);
901 break;
902 case 'R':
903 j = magicard_reset(ctx);
904 break;
905 case 'T':
906 j = magicard_selftest_card(ctx);
907 break;
908 }
909
910 if (j) return j;
911 }
912
913 return CUPS_BACKEND_OK;
914 }
915
916 static int magicard_query_markers(void *vctx, struct marker **markers, int *count)
917 {
918 struct magicard_ctx *ctx = vctx;
919
920 *markers = &ctx->marker;
921 *count = 1;
922
923 return CUPS_BACKEND_OK;
924 }
925
926 static const char *magicard_prefixes[] = {
927 "magicard", // Family name
928 // backwards compatibility
929 "tango2e", "enduro", "enduroplus",
930 NULL
931 };
932
933 static const struct device_id magicard_devices[] = {
934 { 0x0c1f, 0x1800, P_MAGICARD, NULL, "magicard-tango2e"},
935 // { 0x0c1f, 0x1800, P_MAGICARD, NULL, "magicard-rio2e"},
936 { 0x0c1f, 0x4800, P_MAGICARD, NULL, "magicard-enduro"}, // ??
937 { 0x0c1f, 0x880a, P_MAGICARD, NULL, "magicard-enduroplus"}, // ??
938 { 0x0c1f, 0xFFFF, P_MAGICARD, NULL, "magicard"},
939 { 0, 0, 0, NULL, NULL}
940 };
941
942 const struct dyesub_backend magicard_backend = {
943 .name = "Magicard family",
944 .version = "0.21",
945 .uri_prefixes = magicard_prefixes,
946 .devices = magicard_devices,
947 .cmdline_arg = magicard_cmdline_arg,
948 .cmdline_usage = magicard_cmdline,
949 .init = magicard_init,
950 .attach = magicard_attach,
951 .cleanup_job = magicard_cleanup_job,
952 .read_parse = magicard_read_parse,
953 .main_loop = magicard_main_loop,
954 .query_markers = magicard_query_markers,
955 };
956
957 /* Magicard family Spool file format (Tango2e/Rio2e/AvalonE family)
958
959 This one was rather fun to figure out.
960
961 * Job starts with a sequence of 64 '0x05'
962 * Command sequence starts with 0x01
963 * Commands are textual and comma-separated.
964 * Most are passed through ignored, except for:
965 * SZB, SZG, SZR, SZK -- indicate length of respective data plane
966 * IMF -- Image format (BGR/BGRK/K)
967 * X-GP-8 -- Tells backend to convert from Gutenprint's 8bpp data
968 * X-GP-RK -- Tells backend to extract K channel from color data
969 * Command sequence ends with 0x1c
970 * Image plane data follows, in the order of the SZ# entries
971 * Plane lengths are specified by the SZ# entry.
972 * Color planes are actually Y/M/C rather than B/G/R!
973 * Each plane terminates with 0x1c __ 0x3a, where __ is 0x42, 0x47, 0x52,
974 and 0x4b for B/G/R/K respectively. Terminator is _not_ part of length.
975 * Image data is 6bpp for B/G/R and 1bpp for K, 672*1016 pixels
976 * Organized in a series of 84-byte rows.
977 * Byte data is LSB first.
978 * Each row is a single stripe of a single bit of a pixel, so
979 color data is b0b0b0b0.. b1b1b1b1.. .. b5b5b5b5.
980 * Job ends with 0x03
981
982 ** ** ** ** ** **
983
984 Firmware updates:
985
986 0x05 (x9) 0x01 REQ,FRM###### 0x1c
987
988 Where ###### is the length of the firmware image.
989
990 Then send over 64 bytes at a time until it's done.
991
992 Then send 0x03 to mark end of job.
993
994 Follow it with:
995
996 0x01 STA,CHK########, 0x03 (8-digit checksum?)
997
998 0x05 (x9) 0x01 REQ,UPG, 0x1c 0x03
999
1000 ** ** ** ** ** **
1001
1002 Known commands seen in print jobs:
1003
1004 BAC%s Backside format (CKO, KO, C, CO, K) -- Only used with Duplex.
1005 CKI%s Custom Holokote (ON or OFF)
1006 CPW%s Color power level (0-100, default 50)
1007 DPX%s Duplex (ON or OFF)
1008 EOI%d Card alignment end (0-100, default 50)
1009 ESS%d Number of copies (1-?)
1010 HGT%d Image Height (always seems to be 1016)
1011 HKM%06X Holokote hole. bitwise number, each bit corresponds to an area.
1012 HKT%d Holokote type (1 is "ultra secure, 2 is "interlocking rings", etc)
1013 HPH%s Holopatch (ON or OFF)
1014 IMF%s Image Data Format (BGR, BGRK, K)
1015 KPW%s Black power level (0-100, default 50)
1016 LAN%s Printer display lanaguage (ENG, ITA, POR, FRA, DEU, ESP, SCH)
1017 LC%d Force media type (LC1, LC3, LC6, LC8 for YMCKO/MONO/KO/YMCKOK)
1018 NCT%d,%d,%d,%d Overcoat hole
1019 OPW%s Overcoat power level (0-100, default 50)
1020 OVR%s Overcoat (ON or OFF)
1021 PAG%d Page number (always 1, except 2 if printing duplex backside)
1022 PAT%d Holopatch area (0-24)
1023 REJ%s Reject faulty cards (ON or OFF)
1024 SOI%d Card alignment start (0-100, default 50)
1025 SLW%s Colorsure (ON or OFF)
1026 SZB%d Blue data length
1027 SZG%d Green data length
1028 SZK%d Black data length
1029 SZR%d Red data length
1030 TDT%08X Driver-supplied timestamp of print job.
1031 USF%s Holokote (ON or OFF)
1032 VER%s Inform the printer of the driver version (seems to be ignored)
1033 WID%d Image Width (always seems to be 642)
1034
1035 Mag-stripe encoding:
1036
1037 MAG%d Magstripe position (1, 2, or 3)
1038 BPI%d Bits per Inch (75 or 210)
1039 MPC%d Character encoding (5 or 7)
1040 COE%s 'H'igh or 'L'ow coercivity
1041
1042 Unknown commands seen in print jobs:
1043
1044 DDD%s ? (only seen '50') -- Could it be K alignment?
1045 KEE ?
1046 NNN%s ? (Seen 'OFF')
1047 NOC%d ? (Seen '1') (Seems to start a job)
1048 PCT%d,%d,%d,%d ? Print area, seems fixed @ 0,0, 1025, 641)
1049 RT2 ?
1050 TRO%d ? (Seen '0', appears with Holokote)
1051 XCO%d ? X start offset (always seems to be 0)
1052 YCO%d ? Y start offset (always seems to be 0)
1053
1054 Unknown commands: (Seen in firmware guts)
1055
1056 AAA
1057 AMS
1058 BBB%d Numeric parameter
1059 CLR
1060 FBF
1061 FTC
1062 HFD%s String parameter
1063 IPM
1064 KKK
1065 LBL
1066 LLL
1067 LRC
1068 MGV%s "ON" or "OFF" but no idea
1069 MMM
1070 PAR
1071 RDM
1072 SNR
1073 SSP
1074
1075 Unknown commands unique to Tango +L (ie w/ Laminator support)
1076
1077 FRN
1078 LAM
1079 LAM_DLY
1080 LAM_SPD
1081 LAM_LEN
1082 LAM_END
1083 LAM_STA
1084 LAM_DEG
1085 LAM_FLM
1086 LAM_KBD
1087 LAM_MOD
1088
1089 Commands consumed by backend:
1090
1091 ICC%d Gamma curve (0, 1, 2) -- off, 2.2, or 1.8 respectively.
1092 X-GP-8 Raw data is 8bpp. needs to be converted.
1093 X-GP-RK Extract K channel from color data.
1094
1095 Open questions:
1096
1097 * How to query/read magstripe
1098 * How to set IP address (etc)
1099 * How to set other parameters
1100
1101 "Simple Commands" (REQ,....,)
1102
1103 RST Reset printer
1104 TST Generate self-test page
1105 EJT Eject card
1106
1107 Other "Simple commands" referenced in Rio Pro/Enduro+ docs
1108
1109 DEALERSERVICE%s ON/OFF (enter/exit dealer service mode)
1110 CAM Reset print head cam position
1111 CHP%s UP/DOWN Feed card into smart encoder
1112 CLN Cleaning cycle
1113 DYE Re-init dye film
1114 ENC Test encoding cycle
1115 FEED%d 0/1,+ 0/1, load card into standby, >1 feed N cards.
1116 FLIP Flip card in printer
1117 FRN%s ON/OFF -- Film saving
1118 HEAD%s UP/DOWN -- Raise or lower print head.
1119 RAMP%d 0-100 Density ramp, 50 default
1120 SET Saves settings into NVDATA
1121 STN Re-init Holokote
1122 SNS Soak cycle, test all sensors
1123 SHW%s CAM, TACHO, FLIP, DYE, LID, FRONT, MID, READ, BUTTON1, BUTTON2,
1124 SMART, TEMP, ON, OFF
1125 LNG%d 0/1/2/3/4/5 == ENG/POR/FRE/GER/SPA/ITA
1126 RUN%s CAM, FEED, DYE, MAIN, FLIPPER, FLIPROLL, FAN, PANEL, POUT, CAL, LCD,
1127 OFF
1128 FLM%s Y/M/C/K/O Align ribbon at corresponding panel
1129 FCL Init dye calibration routine
1130 FCL###### Set dye color to ###### (RGB hex)
1131
1132 */