Debian Codegraph @

src/cups/backend_dnpds40.c

raw symbols SCIP
1 /*
2 * Citizen / DNP Photo Printer CUPS backend
3 *
4 * (c) 2013-2025 Solomon Peachy <[email protected]>
5 *
6 * Development of this backend was sponsored by:
7 *
8 * Marco Di Antonio and [ ilgruppodigitale.com ]
9 * LiveLink Technology [ www.livelinktechnology.net ]
10 * A generous benefactor who wishes to remain anonymous
11 *
12 * The latest version of this program can be found at:
13 *
14 * https://git.shaftnet.org/gitea/slp/selphy_print.git
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <https://www.gnu.org/licenses/>.
28 *
29 * SPDX-License-Identifier: GPL-2.0+
30 *
31 */
32
33 //#define DNP_ONLY
34 //#define CITIZEN_ONLY
35
36 /* Enables caching of last print type to speed up
37 job pipelining. Without this we always have to
38 assume the worst */
39 //#define STATE_DIR "/tmp"
40
41 #define BACKEND dnpds40_backend
42
43 #include "backend_common.h"
44
45 #include <time.h>
46
47 /* Private data structure */
48 struct dnpds40_printjob {
49 struct dyesub_job_common common;
50
51 uint8_t *databuf;
52 int datalen;
53
54 uint32_t dpi;
55 int matte;
56 int cutter;
57 uint32_t multicut;
58 int fullcut;
59 int printspeed;
60 int can_rewind;
61 int buffcntrl;
62
63 int rows;
64 int is_pano;
65
66 int buf_needed;
67 int cut_paper;
68 };
69
70 #define MAX_PRINTJOB_LEN (((ctx->native_width*ctx->max_height+1024+54+10))*3+1024) /* Worst-case, YMC */
71 #define MAX_PANOPRINTJOB_LEN ((((ctx->native_width*ctx->max_height+1024+54+10))*3+1024)*3) /* Worst-case, YMC */
72
73 #define MFG_DNP 0
74 #define MFG_CITIZEN 1
75 #define MFG_MITSUBISHI 2
76 #define MFG_FUJIFILM 3
77 #define MFG_OTHER 4
78
79 //#define USE_PANODATA_FILES
80
81 struct dnpds40_ctx {
82 struct dyesub_connection *conn;
83
84 int mfg; /* see MFG_* */
85
86 /* Version and whatnot */
87 char *serno;
88 char *version;
89 int ver_major;
90 int ver_minor;
91
92 int flash_mode;
93
94 /* State */
95 uint32_t media;
96 uint32_t media_subtype;
97
98 char media_text[32];
99 uint32_t duplex_media;
100 int duplex_media_status;
101 uint16_t media_count_new;
102
103 uint32_t last_multicut;
104 int last_matte;
105 int partialmatte;
106
107 int media_sticker;
108 int mediaoffset;
109 int correct_count;
110 int needs_mlot;
111 int pano;
112
113 struct marker marker[2];
114 int marker_count;
115
116 /* Printer capabilities */
117 uint32_t native_width;
118 uint32_t max_height;
119 int supports_600dpi;
120 int supports_6x9;
121 int supports_2x6;
122 int supports_3x5x2;
123 int supports_a4x6;
124 int supports_45_34;
125 int supports_matte;
126 int supports_finematte;
127 int supports_luster;
128 int supports_advmatte;
129 int supports_fullcut;
130 int supports_rewind;
131 int supports_standby;
132 int supports_keepmode;
133 int supports_6x4_5;
134 int supports_mqty_default;
135 int supports_iserial; /* 0 == no, 1 == configurable, 2 == always enabled */
136 int supports_6x6;
137 int supports_5x5;
138 int supports_counterp;
139 int supports_adv_fullcut;
140 int supports_mediaoffset;
141 int supports_ctrld_ext;
142 int supports_media_ext;
143 int supports_printspeed;
144 int supports_lowspeed;
145 int supports_highdensity;
146 int supports_systime;
147 int supports_mediaclassrfid;
148 int supports_gamma;
149 int supports_pano;
150 int supports_contpano;
151 };
152
153 struct dnpds40_cmd {
154 uint8_t esc; /* Fixed at ascii ESC, aka 0x1B */
155 uint8_t p; /* Fixed at ascii 'P' aka 0x50 */
156 uint8_t arg1[6];
157 uint8_t arg2[16];
158 uint8_t arg3[8]; /* Decimal value of arg4's length, or empty */
159 uint8_t arg4[]; /* Extra payload if arg3 is non-empty
160 Doesn't have to be sent in the same URB */
161
162 /* All unused elements are set to 0x20 (ie ascii space) */
163 };
164
165 #define MULTICUT_5x3_5 1
166 #define MULTICUT_6x4 2
167 #define MULTICUT_5x7 3
168 #define MULTICUT_6x8 4
169 #define MULTICUT_6x9 5
170 #define MULTICUT_8x10 6
171 #define MULTICUT_8x12 7
172 #define MULTICUT_8x4 8
173 #define MULTICUT_8x5 9
174 #define MULTICUT_8x6 10
175 #define MULTICUT_8x8 11
176 #define MULTICUT_6x4X2 12
177 #define MULTICUT_8x4X2 13
178 #define MULTICUT_8x5X2 14
179 #define MULTICUT_8x6X2 15
180 #define MULTICUT_8x5_8x4 16
181 #define MULTICUT_8x6_8x4 17
182 #define MULTICUT_8x6_8x5 18
183 #define MULTICUT_8x8_8x4 19
184 #define MULTICUT_8x4X3 20
185 #define MULTICUT_8xA4LEN 21
186 #define MULTICUT_5x3_5X2 22
187 #define MULTICUT_6x6 27
188 #define MULTICUT_5x5 29
189 #define MULTICUT_6x4_5 30
190 #define MULTICUT_6x4_5X2 31
191 #define MULTICUT_8x7 32
192 #define MULTICUT_8x9 33
193 #define MULTICUT_A5 34
194 #define MULTICUT_A5X2 35
195 #define MULTICUT_A4x4 36
196 #define MULTICUT_A4x5 37
197 #define MULTICUT_A4x6 38
198 #define MULTICUT_A4x8 39
199 #define MULTICUT_A4x10 40
200 #define MULTICUT_A4 41
201 #define MULTICUT_A4x5X2 43
202
203 #define MULTICUT_4x4 47
204 #define MULTICUT_4x6 48
205 #define MULTICUT_4x8 49
206 #define MULTICUT_4_5x4_5 50
207 #define MULTICUT_4_5x6 51
208 #define MULTICUT_4_5x8 52
209
210 #define MULTICUT_4x3 53
211 #define MULTICUT_4x4_5 54
212 #define MULTICUT_4_5x3 55
213 // #define MULTICUT_??? 56 // XXX WTF is missing?
214 #define MULTICUT_4_5x4 57
215
216 #if 0
217 // XXX do these exist? Or are they just the larger print cut in two?
218 // #define MULTICUT_4x3X2
219 // #define MULTICUT_4x4X2
220 // #define MULTICUT_4_5x3X2
221 // #define MULTICUT_4_5x4X2
222 #endif
223
224 #define MULTICUT_S_SIMPLEX 100
225 #define MULTICUT_S_FRONT 200
226 #define MULTICUT_S_BACK 300
227
228 #define MULTICUT_S_8x10 6
229 #define MULTICUT_S_8x12 7
230 #define MULTICUT_S_8x4 8
231 #define MULTICUT_S_8x5 9
232 #define MULTICUT_S_8x6 10
233 #define MULTICUT_S_8x8 11
234 #define MULTICUT_S_8x4X2 13
235 #define MULTICUT_S_8x5X2 14
236 #define MULTICUT_S_8x6X2 15
237 #define MULTICUT_S_8x10_5 25
238 #define MULTICUT_S_8x10_75 26
239 #define MULTICUT_S_8x4X3 28 // different than roll type.
240
241 /* Special panorama sizes */
242 #define MULTICUT_PANO_6x14 9000
243 #define MULTICUT_PANO_6x20 9001
244 #define MULTICUT_PANO_8x18 9010
245 #define MULTICUT_PANO_8x26 9011
246 #define MULTICUT_PANO_8x22 9012
247 #define MULTICUT_PANO_8x32 9013
248 //#define MULTICUT_PANO_A4x21 9014
249 //#define MULTICUT_PANO_A4x31 9015
250
251 #define MULTICUT_PANO_6x16 9020
252 #define MULTICUT_PANO_6x24 9021
253 #define MULTICUT_PANO_8x20 9030
254 #define MULTICUT_PANO_8x30 9031
255 #define MULTICUT_PANO_8x24 9032
256 #define MULTICUT_PANO_8x36 9033
257 #define MULTICUT_PANO_A4x2 9034
258 #define MULTICUT_PANO_A4x3 9035
259
260 #ifndef min
261 #define min(__x, __y) ((__x) < (__y)) ? __x : __y
262 #endif
263
264 /* Legacy spool file support */
265 static int legacy_cw01_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
266 static int legacy_dnp_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
267 static int legacy_dnp620_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
268 static int legacy_dnp820_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
269 static int legacy_qw410_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data);
270
271 static void dnpds40_cleanup_job(const void *vjob);
272 static int dnpds40_query_markers(void *vctx, struct marker **markers, int *count);
273
274 /* Panorama crap */
275 #ifdef USE_PANODATA_FILES
276 #define PANODATA_DS620 "LUTData_0010.csv"
277 #define PANODATA_DS820 "LUTData820_0010.csv"
278
279 static struct dnp_panodata *dnp_read_panodata(const char *filename)
280 {
281 struct dnp_panodata *pano = NULL;
282
283 FILE *f;
284 char buf[4096];
285 int line;
286 char *ptr;
287
288 const char *delim = " ,\t\n";
289
290 if (!filename)
291 return NULL;
292 pano = malloc(sizeof(struct dnp_panodata));
293 if (!pano)
294 return NULL;
295
296 pano->elements = 0;
297
298 snprintf(buf, sizeof(buf), "%s/%s", corrtable_path, filename);
299
300 f = fopen(buf, "r");
301 if (!f)
302 goto done_free;
303
304 /* Skip the first four lines */
305 for (line = 0; line < DNP_PANO_MAXROWS ; line++) {
306 if (fgets(buf, sizeof(buf), f) == NULL)
307 break;
308
309 ptr = strtok(buf, delim);
310 if (!ptr)
311 goto abort;
312
313 if (strcmp("REC", ptr))
314 continue;
315
316 ptr = strtok(NULL, delim);
317 if (!ptr)
318 goto abort;
319 pano->rows[pano->elements].start_row = strtol(ptr, NULL, 10);
320
321 ptr = strtok(NULL, delim);
322 if (!ptr)
323 goto abort;
324 pano->rows[pano->elements].rhYMC[0] = strtod(ptr,NULL);
325
326 ptr = strtok(NULL, delim);
327 if (!ptr)
328 goto abort;
329 pano->rows[pano->elements].rhYMC[1] = strtod(ptr,NULL);
330
331 ptr = strtok(NULL, delim);
332 if (!ptr)
333 goto abort;
334 pano->rows[pano->elements].rhYMC[2] = strtod(ptr,NULL);
335
336 ptr = strtok(NULL, delim);
337 if (!ptr)
338 goto abort;
339 pano->rows[pano->elements].lhYMC[0] = strtod(ptr,NULL);
340
341 ptr = strtok(NULL, delim);
342 if (!ptr)
343 goto abort;
344 pano->rows[pano->elements].lhYMC[1] = strtod(ptr,NULL);
345
346 ptr = strtok(NULL, delim);
347 if (!ptr)
348 goto abort;
349 pano->rows[pano->elements].lhYMC[2] = strtod(ptr,NULL);
350
351 pano->elements++;
352 }
353 if (pano->elements < 20)
354 goto abort;
355
356 fclose(f);
357
358 #if 0
359 f = fopen("/tmp/panodata.h", "w");
360 fprintf(f, "static struct dnp_panodata panodata = {\n");
361 fprintf(f, "\t.elements = %d,\n", pano->elements);
362 for (int i = 0 ; i < pano->elements ; i++) {
363 fprintf(f, "\t.rows[%2u] = { %4u, { %1.10f, %1.10f, %1.10f }, { %1.10f, %1.10f, %1.10f} },\n",
364 i,
365 pano->rows[i].start_row,
366 pano->rows[i].rhYMC[0],
367 pano->rows[i].rhYMC[1],
368 pano->rows[i].rhYMC[2],
369 pano->rows[i].lhYMC[0],
370 pano->rows[i].lhYMC[1],
371 pano->rows[i].lhYMC[2]);
372 }
373 fprintf(f, "};\n");
374 fclose(f);
375 exit(1);
376 #endif
377
378 return pano;
379
380 abort:
381 fclose(f);
382
383 done_free:
384 free(pano);
385 return NULL;
386 }
387
388 void dnp_free_panodata (struct dnp_panodata *pano)
389 {
390 if (pano)
391 free(pano);
392 }
393 #else /* !USE_PANODATA_FILES */
394
395 #include "backend_panodata.h"
396
397 #define dnp_read_panodata(__fname) &panodata
398 #define dnp_free_panodata(__pano)
399
400 #endif
401
402 #define PROCESS_PIXEL(__corr) \
403 outdata[r * cols + c] = 255 - ((255 - (double)indata[r * cols + c]) * (__corr))
404
405 static void dnp_applypano_plane(const struct dnp_panodata *pano,
406 const uint8_t *indata, uint8_t *outdata,
407 uint16_t rows, const uint16_t cols, const uint16_t pad_rows,
408 const uint16_t dpi, int overlap, const int plane,
409 const int rh, const int lh)
410 {
411 uint16_t r, c;
412
413 /* Fill the start margin with white */
414 if (pad_rows) {
415 memset(outdata, 0xff, pad_rows * cols);
416 outdata += pad_rows * cols;
417 }
418
419 for (r = 0 ; r < rows; r++) {
420 const struct panodata_row *lhc = pano ? &pano->rows[0] : NULL;
421 const struct panodata_row *rhc = pano ? &pano->rows[pano->elements - 1] : NULL;
422
423 if (rh && r < overlap) {
424 /* Row is in RH overlap portion of panel */
425 int i;
426 int row = (overlap - r);
427 if (dpi == 600)
428 row /= 2;
429 for (i = 0 ; i < pano->elements-1 ; i++) {
430 if (row >= pano->rows[i].start_row && row < pano->rows[i+1].start_row)
431 break;
432 }
433 rhc = &pano->rows[i];
434 } else if (lh && (rows -r) < overlap) {
435 /* Row is in LH overlap portion of panel */
436 int i;
437 int row = (rows -r);
438 if (dpi == 600)
439 row /= 2;
440
441 for (i = 0 ; i < pano->elements-1 ; i++) {
442 if (row >= pano->rows[i].start_row && row < pano->rows[i+1].start_row)
443 break;
444 }
445 lhc = &pano->rows[i];
446 } else {
447 /* No processing on row, pass through as-is */
448 memcpy(&outdata[r * cols], &indata[r * cols], cols);
449 continue;
450 }
451
452 if (rh && rhc && r < overlap) {
453 /* Fade RH row */
454 if (plane == 'Y')
455 for (c = 0 ; c < cols ; c++) {
456 PROCESS_PIXEL(rhc->rhYMC[0]);
457 }
458 else if (plane == 'M') {
459 for (c = 0 ; c < cols ; c++) {
460 PROCESS_PIXEL(rhc->rhYMC[1]);
461 }
462 } else {
463 for (c = 0 ; c < cols ; c++) {
464 PROCESS_PIXEL(rhc->rhYMC[2]);
465 }
466 }
467 } else if (lh && lhc && (rows -r) < overlap) {
468 /* Fade LH row */
469 if (plane == 'Y')
470 for (c = 0 ; c < cols ; c++) {
471 PROCESS_PIXEL(lhc->lhYMC[0]);
472 }
473 else if (plane == 'M') {
474 for (c = 0 ; c < cols ; c++) {
475 PROCESS_PIXEL(lhc->lhYMC[1]);
476 }
477 } else {
478 for (c = 0 ; c < cols ; c++) {
479 PROCESS_PIXEL(lhc->lhYMC[2]);
480 }
481 }
482 }
483 }
484
485 /* Fill the tail margin with white */
486 if (pad_rows)
487 memset(&outdata[rows * cols], 0xff, pad_rows * cols);
488 }
489
490 static int dnp_panorama_splitjob(struct dnpds40_ctx *ctx,
491 const struct dnpds40_printjob *injob,
492 struct dnpds40_printjob **newjobs)
493 {
494 struct dnp_panodata *pano = NULL;
495 int overlap = injob->dpi * 2;
496
497 uint16_t out_rows = 0;
498 uint16_t in_rows = 0;
499 uint8_t num_panels = 0;
500 uint32_t new_multicut = 0;
501 uint16_t pad_rows = 0;
502
503 in_rows = injob->dpi == 600 ? injob->rows / 2 : injob->rows;
504 if (ctx->conn->type == P_DNP_DS620) {
505 out_rows = 2436;
506 new_multicut = MULTICUT_6x8;
507
508 if (in_rows == 4236) {
509 num_panels = 2;
510 pad_rows = 18;
511 } else if (in_rows == 6036) {
512 num_panels = 3;
513 pad_rows = 18;
514 } else if (in_rows == 2436*2) {
515 num_panels = 2;
516 overlap = 0;
517 } else if (in_rows == 2436*3) {
518 num_panels = 3;
519 overlap = 0;
520 } else {
521 ERROR("Invalid panorama size (%d rows)\n", injob->rows);
522 goto bail;
523 }
524 } else if (ctx->conn->type == P_DNP_DS820) {
525 if (in_rows == 5436) {
526 new_multicut = ctx->media == 600 ? MULTICUT_A4x10 : MULTICUT_8x10;
527 out_rows = 3036;
528 pad_rows = 18;
529 num_panels = 2;
530 } else if (in_rows == 7836) {
531 new_multicut = ctx->media == 600 ? MULTICUT_A4x10 : MULTICUT_8x10;
532 out_rows = 3036;
533 pad_rows = 18;
534 num_panels = 3;
535 } else if (in_rows == 3036*2) {
536 new_multicut = ctx->media == 600 ? MULTICUT_A4x10 : MULTICUT_8x10;
537 num_panels = 2;
538 out_rows = 3036;
539 overlap = 0;
540 } else if (in_rows == 3036*3) {
541 new_multicut = ctx->media == 600 ? MULTICUT_A4x10 : MULTICUT_8x10;
542 num_panels = 3;
543 out_rows = 3036;
544 overlap = 0;
545 } else if (in_rows == 6452) {
546 new_multicut = ctx->media == 600 ? MULTICUT_A4 : MULTICUT_8xA4LEN;
547 num_panels = 2;
548 pad_rows = 18;
549 out_rows = 3544;
550 } else if (in_rows == 9360) {
551 new_multicut = ctx->media == 600 ? MULTICUT_A4 : MULTICUT_8xA4LEN;
552 num_panels = 3;
553 pad_rows = 18;
554 out_rows = 3544;
555 } else if (in_rows == 3544*2) {
556 new_multicut = ctx->media == 600 ? MULTICUT_A4 : MULTICUT_8xA4LEN;
557 num_panels = 2;
558 out_rows = 3544;
559 overlap = 0;
560 } else if (in_rows == 3544*3) {
561 new_multicut = ctx->media == 600 ? MULTICUT_A4 : MULTICUT_8xA4LEN;
562 num_panels = 3;
563 out_rows = 3544;
564 overlap = 0;
565 } else if (in_rows == 6636) {
566 new_multicut = MULTICUT_8x12;
567 num_panels = 2;
568 pad_rows = 18;
569 out_rows = 3636;
570 } else if (in_rows == 9636) {
571 new_multicut = MULTICUT_8x12;
572 num_panels = 3;
573 pad_rows = 18;
574 out_rows = 3636;
575 } else if (in_rows == 3636*2) {
576 new_multicut = MULTICUT_8x12;
577 num_panels = 2;
578 out_rows = 3636;
579 overlap = 0;
580 } else if (in_rows == 3636*3) {
581 new_multicut = MULTICUT_8x12;
582 num_panels = 3;
583 out_rows = 3636;
584 overlap = 0;
585 } else {
586 ERROR("Invalid panorama size (%d rows)\n", injob->rows);
587 goto bail;
588 }
589 }
590
591 /* Double everything up for >300dpi */
592 if (injob->dpi == 600) {
593 out_rows *= 2;
594 overlap *= 2;
595 pad_rows *= 2;
596 }
597
598 if (overlap) {
599 #ifdef USE_PANODATA_FILES
600 const char *filename = NULL;
601
602 if (ctx->conn->type == P_DNP_DS620)
603 filename = PANODATA_DS620;
604 else if (ctx->conn->type == P_DNP_DS820)
605 filename = PANODATA_DS820;
606
607 if (!filename || !(pano = dnp_read_panodata(filename))) {
608 ERROR("Failure to load panorama LUT file (%s)\n", filename);
609 goto bail;
610 }
611 #else
612 pano = dnp_read_panodata(NULL);
613 #endif
614
615 DEBUG("Splitting continuous panorama job... (%d panels of %d+%d rows, %d overlap)\n", num_panels, out_rows - 2*pad_rows, 2*pad_rows, overlap);
616 } else {
617 DEBUG("Splitting discrete panel panorama job...(%d panels of %d rows)\n", num_panels, out_rows);
618 }
619
620 for (int panel = 0 ; panel < num_panels ; panel++) {
621 int lh = (panel < (num_panels -1));
622 int rh = (panel > 0);
623
624 /* Create new job */
625 newjobs[panel] = malloc(sizeof(struct dnpds40_printjob));
626 if (!newjobs[panel]) {
627 ERROR("Memory allocation failure");
628 return CUPS_BACKEND_RETRY_CURRENT;
629 }
630 struct dnpds40_printjob *newjob = newjobs[panel];
631 memcpy(newjob, injob, sizeof(struct dnpds40_printjob));
632
633 newjob->common.copies = 1;
634 newjob->is_pano = lh; /* All but last */
635 newjob->databuf = malloc(MAX_PRINTJOB_LEN);
636 if (!newjob->databuf) {
637 dnpds40_cleanup_job(newjob);
638 newjobs[panel] = NULL;
639 ERROR("Memory allocation failure!\n");
640 return CUPS_BACKEND_RETRY_CURRENT;
641 }
642 newjob->datalen = 0;
643
644 newjob->multicut = new_multicut;
645 newjob->rows = out_rows;
646
647 uint32_t plane_len = out_rows * ctx->native_width + 1088;
648
649 /* Copy data blocks from injob */
650 uint8_t *ptr = injob->databuf;
651 while(ptr && ptr < (injob->databuf + injob->datalen)) {
652 uint32_t i;
653 int copy = 1;
654 if(!memcmp("IMAGE YPLANE", ptr +2, 12) ||
655 !memcmp("IMAGE MPLANE", ptr +2, 12) ||
656 !memcmp("IMAGE CPLANE", ptr +2, 12) ) {
657
658 if (ptr[8] == 'Y') {
659 /* Insert CONT_PANORAMA _before_ the image data */
660 int cont = (panel == num_panels-1) ? 0 : 1;
661 int lap = overlap;
662 if (injob->dpi == 600)
663 lap /= 2;
664 lap /= 3; /* Convert pixels into inches * 100 */
665
666 if (overlap) {
667 /* Continuous panorama */
668 newjob->datalen += sprintf((char*)newjob->databuf + newjob->datalen, "\033PCNTRL CONT_PANORAMA 00000008%04u%04u", cont, lap);
669 } else {
670 /* Discrete panel */
671 newjob->cutter = 1000;
672 }
673 }
674
675 uint8_t *bmp_hdr;
676 DEBUG("Panel %d plane %c\n", panel, ptr[8]);
677 newjob->datalen += sprintf((char*)newjob->databuf + newjob->datalen,
678 "\033PIMAGE %cPLANE %08u", ptr[8], plane_len);
679
680 /* Copy over old bitmap header*/
681 memcpy(newjob->databuf + newjob->datalen, ptr + 32, 1088);
682 bmp_hdr = newjob->databuf + newjob->datalen;
683
684 /* Mangle BMP header for new size and row count */
685 i = cpu_to_le32(plane_len);
686 memcpy(bmp_hdr + 2, &i, sizeof(i));
687 i = cpu_to_le32(out_rows);
688 memcpy(bmp_hdr + 22, &i, sizeof(i));
689
690 /* Split panorama */
691 dnp_applypano_plane(pano,
692 ptr + 32 + 1088 + (pad_rows * ctx->native_width) + (panel * (out_rows - pad_rows*2 - overlap) * ctx->native_width),
693 newjob->databuf + newjob->datalen + 1088,
694 out_rows - pad_rows*2, ctx->native_width, pad_rows, injob->dpi, overlap, ptr[8], rh, lh);
695 newjob->datalen += plane_len;
696
697 /* Don't copy anything else from source */
698 copy = 0;
699 } else if(!memcmp("CNTRL QTY", ptr + 2, 9)) {
700 /* Ignore copy count */
701 copy = 0;
702 } else if(!memcmp("CNTRL CUTTER", ptr + 2, 12) ||
703 !memcmp("CNTRL FULL_CUTTER_SET", ptr + 2, 21)) {
704 /* Ignore all cutter commands */
705 copy = 0;
706 }
707
708 /* Work out how much to copy from source, and increment source pointer */
709 char buf[10];
710 buf[8] = 0;
711 memcpy(buf, ptr + 24, 8);
712 i = atoi(buf) + 32;
713 if (copy) {
714 memcpy(newjob->databuf + newjob->datalen, ptr, i);
715 newjob->datalen += i;
716 }
717 ptr += i;
718 }
719 #if 0
720 {
721 char buf[32];
722 sprintf(buf, "/tmp/dump%d.raw", panel);
723 FILE *f = fopen(buf, "wb");
724 fwrite(newjob->databuf, newjob->datalen, 1, f);
725 fclose(f);
726 }
727 #endif
728 }
729
730 dnp_free_panodata(pano);
731 return CUPS_BACKEND_OK;
732
733 bail:
734 dnp_free_panodata(pano);
735 return CUPS_BACKEND_FAILED;
736 }
737
738 #define JOB_EQUIV(__x) if (job1->__x != job2->__x) goto done
739
740 /* NOTE: Does _not_ free the input jobs */
741 static void *dnp_combine_jobs(const void *vjob1,
742 const void *vjob2)
743 {
744 const struct dnpds40_printjob *job1 = vjob1;
745 const struct dnpds40_printjob *job2 = vjob2;
746 struct dnpds40_printjob *newjob = NULL;
747 uint32_t new_multicut;
748 uint16_t new_w, new_h;
749 int32_t gap_bytes;
750
751 /* Sanity check */
752 if (!job1 || !job2)
753 goto done;
754
755 /* Make sure pertinent paremeters are the same */
756 JOB_EQUIV(dpi);
757 JOB_EQUIV(matte);
758 JOB_EQUIV(cutter);
759 JOB_EQUIV(fullcut);
760 JOB_EQUIV(multicut); // TODO: Support fancier modes for 8" models (eg 8x4+8x6, etc)
761 JOB_EQUIV(datalen); // <-- cheating a little?
762 // JOV_EQUIV(printspeed); <-- does it matter?
763
764 /* Any fancy cutter action means we pass */
765 if (job1->fullcut || job1->cutter > 120)
766 goto done;
767 /* Partial matte is no bueno */
768 if (job1->matte > 100 ||
769 job2->matte > 100)
770 goto done;
771
772 /* Make sure we can combine these two prints */
773 switch (job1->multicut) {
774 case MULTICUT_5x3_5:
775 new_multicut = MULTICUT_5x3_5X2;
776 new_w = 1920;
777 new_h = 2176;
778 gap_bytes = 0;
779 break;
780 case MULTICUT_6x4:
781 if (job1->cutter == 120) {
782 new_multicut = MULTICUT_6x8;
783 new_h = 2436;
784 gap_bytes = -44; /* Chop out the middle 44 rows */
785 } else {
786 new_multicut = MULTICUT_6x4X2;
787 new_h = 2498;
788 gap_bytes = 18;
789 }
790 new_w = 1920;
791 break;
792 case MULTICUT_6x4_5:
793 new_multicut = MULTICUT_6x4_5X2;
794 new_w = 1920;
795 new_h = 2802;
796 gap_bytes = 30;
797 break;
798 case MULTICUT_8x4:
799 new_multicut = MULTICUT_8x4X2;
800 new_w = 2560;
801 new_h = 2502;
802 gap_bytes = 30;
803 break;
804 case MULTICUT_8x5:
805 new_multicut = MULTICUT_8x5X2;
806 new_w = 2560;
807 new_h = 3102;
808 gap_bytes = 30;
809 break;
810 case MULTICUT_A4x5:
811 new_multicut = MULTICUT_A4x5X2;
812 new_w = 2560;
813 new_h = 3102;
814 gap_bytes = 30;
815 break;
816 case MULTICUT_A5:
817 new_multicut = MULTICUT_A5X2;
818 new_w = 2560;
819 new_h = 3598;
820 gap_bytes = 30;
821 break;
822 case MULTICUT_8x6:
823 new_multicut = MULTICUT_8x6X2;
824 new_w = 2560;
825 new_h = 3702;
826 gap_bytes = 30;
827 break;
828 #if 0 // XXX Do these printers handle automatic multicut?
829 // if not, implement using FULL_CUTTER_CONTROL!
830 case MULTICUT_4x3:
831 new_multicut = MULTICUT_4x6;
832 new_w = 1408;
833 new_h = 1836;
834 gap_bytes = 36;
835 break;
836 case MULTICUT_4x4:
837 new_multicut = MULTICUT_4x8;
838 new_w = 1408;
839 new_h = 2436;
840 gap_bytes = 36;
841 break;
842 case MULTICUT_4_5x3:
843 new_multicut = MULTICUT_4_5x6;
844 new_w = 1408;
845 new_h = 1836;
846 gap_bytes = 36;
847 break;
848 case MULTICUT_4_5x4:
849 new_multicut = MULTICUT_4_5x8;
850 new_w = 1408;
851 new_h = 2436;
852 gap_bytes = 36;
853 break;
854 #endif
855 default:
856 /* Everything else is NOT handled */
857 goto done;
858 }
859 gap_bytes *= new_w;
860 if (job1->dpi == 600) {
861 gap_bytes *= 2;
862 new_h *= 2;
863 }
864
865 DEBUG("Combining jobs to save media\n");
866
867 /* Okay, it's kosher to proceed */
868
869 newjob = malloc(sizeof(*newjob));
870 if (!newjob) {
871 ERROR("Memory allocation failure!\n");
872 goto done;
873 }
874 memcpy(newjob, job1, sizeof(*newjob));
875
876 newjob->databuf = malloc(((new_w*new_h+1024+54+10))*3+1024 + abs(gap_bytes));
877 newjob->datalen = 0;
878 newjob->multicut = new_multicut;
879 newjob->can_rewind = 0;
880 newjob->common.can_combine = 0;
881 if (!newjob->databuf) {
882 dnpds40_cleanup_job(newjob);
883 newjob = NULL;
884 ERROR("Memory allocation failure!\n");
885 goto done;
886 }
887
888 /* Copy data blocks from job1 */
889 uint8_t *ptr, *ptr2;
890 char buf[10];
891 ptr = job1->databuf;
892 while(ptr && ptr < (job1->databuf + job1->datalen)) {
893 int i;
894 buf[8] = 0;
895 memcpy(buf, ptr + 24, 8);
896 i = atoi(buf) + 32;
897 memcpy(newjob->databuf + newjob->datalen, ptr, i);
898
899 /* If we're on a plane data block... */
900 if (!memcmp("PLANE", newjob->databuf + newjob->datalen + 9, 5)) {
901 long planelen = (new_w * new_h) + 1088;
902 uint32_t newlen;
903
904 /* Fix up length in command */
905 snprintf(buf, sizeof(buf), "%08lu", planelen);
906 memcpy(newjob->databuf + newjob->datalen + 24, buf, 8);
907
908 /* Alter BMP header */
909 newlen = cpu_to_le32(planelen);
910 memcpy(newjob->databuf + newjob->datalen + 32 + 2, &newlen, 4);
911
912 /* alter DIB header */
913 newlen = cpu_to_le32(new_h);
914 memcpy(newjob->databuf + newjob->datalen + 32 + 22, &newlen, 4);
915
916 if (gap_bytes > 0) {
917 /* Insert gap/padding after first image */
918 memset(newjob->databuf + newjob->datalen + i, 0xff, gap_bytes);
919 newjob->datalen += gap_bytes;
920 } else {
921 // uint8_t *ptrA = newjob->databuf + newjob->datalen + 1088;
922 // /* Back off by 1/2 the gap */
923 // memmove(ptrA, ptrA - (gap_bytes / 2), (i - 1088) + gap_bytes/2);
924 /* And chop the end off by half the gap */
925 newjob->datalen += gap_bytes / 2;
926 }
927
928 /* Locate job2's PLANE -- Assume it's in the same place! */
929 ptr2 = job2->databuf + (ptr - job1->databuf);
930
931 /* Copy over job2's image data */
932 memcpy(newjob->databuf + newjob->datalen + i,
933 ptr2 + 32 + 1088, i - 32 - 1088);
934
935 if (gap_bytes < 0) {
936 uint8_t *ptrA = newjob->databuf + newjob->datalen + i;
937 /* Back off by 1/2 the gap */
938 memmove(ptrA, ptrA - (gap_bytes / 2), (i - 1088) + gap_bytes/2);
939 /* And chop the end off by half the gap */
940 newjob->datalen += gap_bytes / 2;
941 }
942 newjob->datalen += i - 32 - 1088; /* add in job2 length */
943 }
944
945 newjob->datalen += i;
946 ptr += i;
947 }
948
949 done:
950 return newjob;
951 }
952
953 #undef JOB_EQUIV
954
955 static void dnpds40_build_cmd(struct dnpds40_cmd *cmd, const char *arg1, const char *arg2, uint32_t arg3_len)
956 {
957 memset(cmd, 0x20, sizeof(*cmd));
958 cmd->esc = 0x1b;
959 cmd->p = 0x50;
960 memcpy(cmd->arg1, arg1, min(strlen(arg1), sizeof(cmd->arg1)));
961 memcpy(cmd->arg2, arg2, min(strlen(arg2), sizeof(cmd->arg2)));
962 if (arg3_len) {
963 char buf[11]; /* Extra padding to shut up GCC 10 */
964 snprintf(buf, sizeof(buf), "%08u", arg3_len);
965 memcpy(cmd->arg3, buf, 8);
966 }
967 }
968
969 static void dnpds40_cleanup_string(char *start, int len)
970 {
971 char *ptr = strchr(start, 0x0d);
972
973 if (ptr && (ptr - start < len)) {
974 *ptr = 0x00; /* If there is a <CR>, terminate there */
975 len = ptr - start;
976 } else {
977 start[--len] = 0x00; /* force null-termination */
978 }
979
980 /* Trim trailing spaces */
981 while (len && start[len-1] == ' ') {
982 start[--len] = 0;
983 }
984 }
985
986 static const char *dnpds40_printer_type(int type, int mfg)
987 {
988 switch(type) {
989 case P_DNP_DS40: return mfg == MFG_CITIZEN? "CX" : "DS40";
990 case P_DNP_DS80: return mfg == MFG_CITIZEN? "CW" : (mfg == MFG_MITSUBISHI ? "CP3800" : "DS80");
991 case P_DNP_DS80D: return "DS80DX";
992 case P_DNP_DSRX1: return mfg == MFG_CITIZEN ? "CY" : "DSRX1";
993 case P_DNP_DS620: return mfg == MFG_CITIZEN ? "CX-02" : (mfg == MFG_FUJIFILM ? "ASK400" : "DS620");
994 case P_DNP_DS820: return mfg == MFG_CITIZEN ? "CX-02W" : "DS820";
995 case P_DNP_QW410: return mfg == MFG_CITIZEN ? "CZ-01" : "QW410";
996 case P_CITIZEN_CW01: return "CW01";
997 case P_CITIZEN_OP900II: return "CW-02 / OP900ii";
998 default: break;
999 }
1000 return "Unknown";
1001 }
1002
1003 static const char *dnpds40_media_types(int media, int sticker)
1004 {
1005 switch (media) {
1006 case 100: return "UNKNOWN100"; // seen in driver dumps
1007 case 110: return "UNKNOWN110"; // seen in driver dumps
1008 case 150: return "4x6 (PC)";
1009 case 151: return "4x8";
1010 case 160: return "4.5x6";
1011 case 161: return "4.5x8";
1012 case 200: return sticker ? "5x3.5 (L): Sticker" : "5x3.5 (L)";
1013 case 210: return sticker ? "5x7 (2L) Sticker" : "5x7 (2L)";
1014 case 300: return sticker ? "6x4 (PC) Sticker" : "6x4 (PC)";
1015 case 310: return sticker ? "6x8 (A5) Sticker" : "6x8 (A5)";
1016 case 400: return sticker ? "6x9 (A5W) Sticker" : "6x9 (A5W)";
1017 case 500: return "8x10";
1018 case 510: return "8x12";
1019 case 600: return "A4";
1020 default:
1021 break;
1022 }
1023
1024 return "Unknown";
1025 }
1026
1027 static const char *dnpds620_media_extension_code(int media)
1028 {
1029 switch (media) {
1030 case 0: return "Normal Paper";
1031 case 1: return "Sticker Paper";
1032 case 99: return "Unknown Paper";
1033 default:
1034 break;
1035 }
1036
1037 return "Unknown";
1038 }
1039
1040 static const char *rfid_media_subtypes(int media)
1041 {
1042 switch (media) {
1043 case 1: return "SD";
1044 case 2: return "PD";
1045 case 3: return "PP";
1046 default:
1047 break;
1048 }
1049
1050 return "Unknown";
1051 }
1052
1053 static const char *dnpds80_duplex_media_types(int media)
1054 {
1055 switch (media) {
1056 case 100: return "8x10.75";
1057 case 200: return "8x12";
1058 default:
1059 break;
1060 }
1061
1062 return "Unknown";
1063 }
1064
1065 #define DUPLEX_UNIT_PAPER_NONE 0
1066 #define DUPLEX_UNIT_PAPER_PROTECTIVE 1
1067 #define DUPLEX_UNIT_PAPER_PRESENT 2
1068
1069 static const char *dnpds80_duplex_paper_status(int media)
1070 {
1071 switch (media) {
1072 case DUPLEX_UNIT_PAPER_NONE: return "No Paper";
1073 case DUPLEX_UNIT_PAPER_PROTECTIVE: return "Protective Sheet";
1074 case DUPLEX_UNIT_PAPER_PRESENT: return "Cut Paper Present";
1075 default:
1076 return "Unknown";
1077 }
1078 }
1079
1080 static const char *dnpds80_duplex_statuses(int status)
1081 {
1082 switch (status) {
1083 case 5000: return "No Error";
1084
1085 case 5500: return "Duplex Unit Not Connected";
1086
1087 case 5017: return "Paper Jam: Supply Sensor On";
1088 case 5018: return "Paper Jam: Supply Sensor Off";
1089 case 5019: return "Paper Jam: Slot Sensor On";
1090 case 5020: return "Paper Jam: Slot Sensor Off";
1091 case 5021: return "Paper Jam: Pass Sensor On";
1092 case 5022: return "Paper Jam: Pass Sensor Off";
1093 case 5023: return "Paper Jam: Shell Sensor 1 On";
1094 case 5024: return "Paper Jam: Shell Sensor 1 Off";
1095 case 5025: return "Paper Jam: Shell Sensor 2 On";
1096 case 5026: return "Paper Jam: Shell Sensor 2 Off";
1097 case 5027: return "Paper Jam: Eject Sensor On";
1098 case 5028: return "Paper Jam: Eject Sensor Off";
1099 case 5029: return "Paper Jam: Slot FG Sensor";
1100 case 5030: return "Paper Jam: Shell FG Sensor";
1101
1102 case 5033: return "Paper Supply Sensor Off";
1103 case 5034: return "Printer Feed Slot Sensor Off";
1104 case 5035: return "Pinch Pass Sensor Off";
1105 case 5036: return "Shell Pass Sensor 1 Off";
1106 case 5037: return "Shell Pass Sensor 2 Off";
1107 case 5038: return "Eject Sensor Off";
1108
1109 case 5049: return "Capstan Drive Control Error";
1110 case 5065: return "Shell Roller Error";
1111
1112 case 5081: return "Pinch Open Error";
1113 case 5082: return "Pinch Close Error";
1114 case 5083: return "Pinch Init Error";
1115 case 5084: return "Pinch Position Error";
1116
1117 case 5097: return "Pass Guide Supply Error";
1118 case 5098: return "Pass Guide Shell Error";
1119 case 5099: return "Pass Guide Eject Error";
1120 case 5100: return "Pass Guide Init Error";
1121 case 5101: return "Pass Guide Position Error";
1122
1123 case 5113: return "Side Guide Home Error";
1124 case 5114: return "Side Guide Position Error";
1125 case 5115: return "Side Guide Init Error";
1126
1127 case 5129: return "Act Guide Home Error";
1128
1129 case 5145: return "Shell Rotate Home Error";
1130 case 5146: return "Shell Rotate Rev Error";
1131
1132 case 5161: return "Paper Feed Lever Down Error";
1133 case 5162: return "Paper Feed Lever Lock Error";
1134 case 5163: return "Paper Feed Lever Up Error";
1135
1136 case 5177: return "Cutter Home Error";
1137 case 5178: return "Cutter Away Error";
1138 case 5179: return "Cutter Init Error";
1139 case 5180: return "Cutter Position Error";
1140
1141 case 5193: return "Paper Tray Removed";
1142 case 5209: return "Cover Opened";
1143 case 5241: return "System Error";
1144
1145 default:
1146 break;
1147 }
1148
1149 return "Unknown Duplexer Error";
1150 }
1151
1152 static const char *dnpds40_statuses(int status)
1153 {
1154 if (status >= 5000 && status <= 5999)
1155 return dnpds80_duplex_statuses(status);
1156
1157 switch (status) {
1158 case 0: return "Idle";
1159 case 1: return "Printing";
1160 case 500: return "Cooling Print Head";
1161 case 510: return "Cooling Paper Motor";
1162 case 900: return "Standby Mode";
1163 case 1000: return "Cover Open";
1164 case 1010: return "No Scrap Box";
1165 case 1100: return "Paper End";
1166 case 1200: return "Ribbon End";
1167 case 1300: return "Paper Jam";
1168 case 1400: return "Ribbon Error";
1169 case 1500: return "Paper Definition Error";
1170 case 1600: return "Data Error";
1171 case 2000: return "Head Voltage Error";
1172 case 2010: return "USB Power Supply Error";
1173 case 2100: return "Head Position Error";
1174 case 2200: return "Power Supply Fan Error";
1175 case 2300: return "Cutter Error";
1176 case 2400: return "Pinch Roller Error";
1177 case 2500: return "Abnormal Head Temperature";
1178 case 2600: return "Abnormal Media Temperature";
1179 case 2610: return "Abnormal Paper Motor Temperature";
1180 case 2700: return "Ribbon Tension Error";
1181 case 2800: return "RF-ID Module Error";
1182 case 2900: return "RS422 Communiation Error";
1183 case 3000: return "System Error";
1184 case 9999: return "Communication Failure"; /* Special */
1185 default:
1186 break;
1187 }
1188
1189 return "Unknown Error";
1190 }
1191
1192 static const char *dnpds40_panorama_status(char *resp)
1193 {
1194 /* 00000 == okay to print
1195 1xxxx == not okay to print
1196 xxxx1 == head temp high
1197 xxx2x == media temp low
1198 xx1xx == humidity too high
1199 */
1200
1201 if (resp[4] == '0')
1202 return "Yes";
1203 if (resp[0] == '1')
1204 return "No, High Head temp";
1205 if (resp[1] == '2')
1206 return "No, Low media temp";
1207 if (resp[2] == '1')
1208 return "No, High humidity";
1209
1210 return "No, Unknown reason";
1211 }
1212
1213 static int dnpds40_do_cmd(struct dnpds40_ctx *ctx,
1214 const struct dnpds40_cmd *cmd,
1215 const uint8_t *data, int len)
1216 {
1217 int ret;
1218
1219 if ((ret = send_data(ctx->conn,
1220 (uint8_t*)cmd, sizeof(*cmd))))
1221 return ret;
1222
1223 if (data && len)
1224 if ((ret = send_data(ctx->conn,
1225 data, len)))
1226 return ret;
1227
1228 return CUPS_BACKEND_OK;
1229 }
1230
1231 static uint8_t *dnpds40_resp_cmd2(struct dnpds40_ctx *ctx,
1232 struct dnpds40_cmd *cmd,
1233 int *len,
1234 uint8_t *buf, uint32_t buf_len)
1235 {
1236 char tmp[9];
1237 uint8_t *respbuf;
1238
1239 int ret, i, num = 0;
1240
1241 memset(tmp, 0, sizeof(tmp));
1242
1243 if ((ret = dnpds40_do_cmd(ctx, cmd, buf, buf_len)))
1244 return NULL;
1245
1246 /* Read in the response header */
1247 ret = read_data(ctx->conn,
1248 (uint8_t*)tmp, 8, &num);
1249 if (ret < 0)
1250 return NULL;
1251
1252 if (num != 8) {
1253 ERROR("Short read! (%d/%d)\n", num, 8);
1254 return NULL;
1255 }
1256
1257 i = atoi(tmp); /* Length of payload in bytes, possibly padded */
1258 respbuf = malloc(i + 1);
1259 if (!respbuf) {
1260 ERROR("Memory allocation failure (%d bytes)!\n", i);
1261 return NULL;
1262 }
1263 respbuf[i] = 0; /* Explicitly null-pad */
1264
1265 /* Read in the actual response */
1266 ret = read_data(ctx->conn,
1267 respbuf, i, &num);
1268 if (ret < 0) {
1269 free(respbuf);
1270 return NULL;
1271 }
1272
1273 if (num != i) {
1274 ERROR("Short read! (%d/%d)\n", num, i);
1275 free(respbuf);
1276 return NULL;
1277 }
1278
1279 *len = num;
1280 return respbuf;
1281 }
1282
1283 #define dnpds40_resp_cmd(__ctx, __cmd, __len) dnpds40_resp_cmd2(__ctx, __cmd, __len, NULL, 0)
1284
1285 static int dnpds40_query_serno(struct dyesub_connection *conn, char *buf, int buf_len)
1286 {
1287 struct dnpds40_cmd cmd;
1288 uint8_t *resp;
1289 int len = 0;
1290
1291 struct dnpds40_ctx ctx = {
1292 .conn = conn,
1293 };
1294
1295 /* Get Serial Number */
1296 dnpds40_build_cmd(&cmd, "INFO", "SERIAL_NUMBER", 0);
1297
1298 resp = dnpds40_resp_cmd(&ctx, &cmd, &len);
1299 if (!resp)
1300 return CUPS_BACKEND_FAILED;
1301
1302 dnpds40_cleanup_string((char*)resp, len);
1303
1304 strncpy(buf, (char*)resp, buf_len);
1305 buf[buf_len-1] = 0;
1306
1307 free(resp);
1308
1309 return CUPS_BACKEND_OK;
1310 }
1311
1312 static void *dnpds40_init(void)
1313 {
1314 struct dnpds40_ctx *ctx = malloc(sizeof(struct dnpds40_ctx));
1315 if (!ctx) {
1316 ERROR("Memory allocation failure (%d bytes)!\n", (int)sizeof(struct dnpds40_ctx));
1317 return NULL;
1318 }
1319 memset(ctx, 0, sizeof(struct dnpds40_ctx));
1320
1321 return ctx;
1322 }
1323
1324 #define FW_VER_CHECK(__major, __minor) \
1325 ((ctx->ver_major > (__major)) || \
1326 (ctx->ver_major == (__major) && ctx->ver_minor >= (__minor)))
1327
1328 static int dnpds40_query_mqty(struct dnpds40_ctx *ctx)
1329 {
1330 struct dnpds40_cmd cmd;
1331 uint8_t *resp;
1332 int len = 0, count;
1333
1334 /* Get Media remaining */
1335 dnpds40_build_cmd(&cmd, "INFO", "MQTY", 0);
1336
1337 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1338 if (!resp)
1339 return -1;
1340
1341 dnpds40_cleanup_string((char*)resp, len);
1342
1343 count = atoi((char*)resp+4);
1344 free(resp);
1345
1346 if (count) {
1347 /* Old-sk00l models report one less than they should */
1348 if (!ctx->correct_count)
1349 count++;
1350
1351 count -= ctx->mediaoffset;
1352 if (count < 0) /* Just in case */
1353 count = 0;
1354 }
1355
1356 return count;
1357 }
1358
1359 static int dnpds80dx_query_paper(struct dnpds40_ctx *ctx)
1360 {
1361 struct dnpds40_cmd cmd;
1362 uint8_t *resp;
1363 int len = 0;
1364
1365 /* Query Duplex Media Info */
1366 dnpds40_build_cmd(&cmd, "INFO", "UNIT_CUT_PAPER", 0);
1367
1368 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1369 if (resp) {
1370 char tmp[5];
1371 char status;
1372
1373 dnpds40_cleanup_string((char*)resp, len);
1374
1375 memcpy(tmp, resp + 4, 4);
1376 status = tmp[3];
1377 tmp[3] = '0';
1378 tmp[4] = 0;
1379
1380 ctx->duplex_media = atoi(tmp);
1381
1382 tmp[0] = tmp[1] = tmp[2] = '0';
1383 tmp[3] = status;
1384 ctx->duplex_media_status = atoi(tmp);
1385
1386 free(resp);
1387 } else {
1388 return CUPS_BACKEND_FAILED;
1389 }
1390
1391 return CUPS_BACKEND_OK;
1392 }
1393
1394 static int dnpds40_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid)
1395 {
1396 struct dnpds40_ctx *ctx = vctx;
1397
1398 UNUSED(jobid);
1399
1400 ctx->conn = conn;
1401
1402 /* Nearly all models support 600dpi */
1403 ctx->supports_600dpi = 1;
1404
1405 if (test_mode < TEST_MODE_NOATTACH) {
1406 struct dnpds40_cmd cmd;
1407 uint8_t *resp;
1408 int len = 0;
1409
1410 /* Get Firmware Version */
1411 dnpds40_build_cmd(&cmd, "INFO", "FVER", 0);
1412
1413 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1414 if (resp) {
1415 char *ptr;
1416 dnpds40_cleanup_string((char*)resp, len);
1417 ctx->version = strdup((char*) resp);
1418
1419 /* See if we're in FW update mode */
1420 if (strstr(ctx->version, "RW")) {
1421 ctx->flash_mode = 1;
1422 ctx->ver_major = 0;
1423 ctx->ver_minor = 0;
1424 } else {
1425 /* Parse version */
1426 /* ptr = */ strtok((char*)resp, " .");
1427 ptr = strtok(NULL, ".");
1428 ctx->ver_major = atoi(ptr);
1429 ptr = strtok(NULL, ".");
1430 ctx->ver_minor = atoi(ptr);
1431 }
1432
1433 free(resp);
1434 } else {
1435 return CUPS_BACKEND_FAILED;
1436 }
1437
1438 /* Figure out actual Manufacturer */
1439 {
1440 struct libusb_device_descriptor desc;
1441 struct libusb_device *udev;
1442
1443 udev = libusb_get_device(ctx->conn->dev);
1444 libusb_get_device_descriptor(udev, &desc);
1445
1446 char buf[STR_LEN_MAX + 1];
1447 buf[0] = 0;
1448 buf[STR_LEN_MAX] = 0;
1449 libusb_get_string_descriptor_ascii(ctx->conn->dev, desc.iManufacturer, (unsigned char*)buf, STR_LEN_MAX);
1450
1451 if (!strncmp(buf, "Dai", 3)) /* "Dai Nippon Printing" */
1452 ctx->mfg = MFG_DNP;
1453 else if (!strncmp(buf, "CIT", 3)) /* "CITIZEN SYSTEMS" */
1454 ctx->mfg = MFG_CITIZEN;
1455 else if (!strncmp(buf, "M", 1)) /* "Mitsubishi" */
1456 ctx->mfg = MFG_MITSUBISHI;
1457 else if (!strncmp(buf, "F", 1)) /* "Fujifilm" */
1458 ctx->mfg = MFG_FUJIFILM;
1459 else
1460 ctx->mfg = MFG_OTHER;
1461
1462 #ifdef DNP_ONLY /* Only allow DNP printers to work. */
1463 if (ctx->mfg != MFG_DNP)
1464 return CUPS_BACKEND_FAILED;
1465 #endif
1466 #ifdef CITIZEN_ONLY /* Only allow CITIZEN printers to work. */
1467 if (ctx->mfg != MFG_CITIZEN)
1468 return CUPS_BACKEND_FAILED
1469 #endif
1470 }
1471 } else {
1472 ctx->ver_major = 3;
1473 ctx->ver_minor = 0;
1474 ctx->version = strdup("UNKNOWN");
1475 }
1476
1477 /* Per-printer options */
1478 switch (ctx->conn->type) {
1479 case P_DNP_DS40:
1480 ctx->native_width = 1920;
1481 ctx->max_height = 5480;
1482 ctx->supports_6x9 = 1;
1483 if (FW_VER_CHECK(1,04))
1484 ctx->supports_counterp = 1;
1485 if (FW_VER_CHECK(1,30))
1486 ctx->supports_matte = 1;
1487 if (FW_VER_CHECK(1,40))
1488 ctx->supports_2x6 = 1;
1489 if (FW_VER_CHECK(1,50))
1490 ctx->supports_3x5x2 = 1;
1491 if (FW_VER_CHECK(1,60))
1492 ctx->supports_fullcut = ctx->supports_6x6 = 1; // No 5x5!
1493 if (FW_VER_CHECK(1,70))
1494 /* Always reports iSerial, not configurable */
1495 ctx->supports_iserial = 2;
1496 break;
1497 case P_DNP_DS80:
1498 case P_DNP_DS80D:
1499 ctx->native_width = 2560;
1500 ctx->max_height = 7536;
1501 if (FW_VER_CHECK(1,02))
1502 ctx->supports_counterp = 1;
1503 if (FW_VER_CHECK(1,30))
1504 ctx->supports_matte = 1;
1505 if (FW_VER_CHECK(1,42))
1506 /* Always reports iSerial, not configurable */
1507 ctx->supports_iserial = 2;
1508 break;
1509 case P_DNP_DSRX1:
1510 ctx->native_width = 1920;
1511 ctx->max_height = 5480;
1512 ctx->supports_counterp = 1;
1513 ctx->supports_matte = 1;
1514 if (FW_VER_CHECK(1,10))
1515 ctx->supports_2x6 = ctx->supports_mqty_default = 1;
1516 if (FW_VER_CHECK(1,20))
1517 ctx->supports_3x5x2 = 1;
1518 if (FW_VER_CHECK(2,00)) { /* AKA RX1HS */
1519 ctx->needs_mlot = 1;
1520 ctx->supports_mediaoffset = 1;
1521 ctx->supports_iserial = 1;
1522 }
1523 if (FW_VER_CHECK(2,06)) {
1524 ctx->supports_5x5 = ctx->supports_6x6 = 1;
1525 }
1526 break;
1527 case P_CITIZEN_OP900II:
1528 ctx->native_width = 1920;
1529 ctx->max_height = 5480;
1530 ctx->supports_counterp = 1;
1531 ctx->supports_matte = 1;
1532 ctx->supports_mqty_default = 1;
1533 ctx->supports_6x9 = 1;
1534 if (FW_VER_CHECK(1,11))
1535 ctx->supports_2x6 = 1;
1536 break;
1537 case P_CITIZEN_CW01:
1538 ctx->native_width = 2048;
1539 ctx->max_height = 5480;
1540 ctx->supports_6x9 = 1;
1541 break;
1542 case P_DNP_DS620:
1543 ctx->native_width = 1920;
1544 ctx->max_height = 5604;
1545 ctx->correct_count = 1;
1546 ctx->supports_counterp = 1;
1547 ctx->supports_matte = 1;
1548 ctx->supports_2x6 = 1;
1549 ctx->supports_fullcut = 1;
1550 ctx->supports_mqty_default = 1;
1551 ctx->supports_standby = 1;
1552 ctx->supports_keepmode = 1;
1553 ctx->supports_iserial = 1;
1554 ctx->supports_6x6 = 1;
1555 ctx->supports_5x5 = 1;
1556 ctx->supports_lowspeed = 1;
1557 ctx->supports_3x5x2 = 1;
1558
1559 if (ctx->mfg == MFG_CITIZEN) { /* Citizen and DNP firmware diverge */
1560 ctx->supports_adv_fullcut = 1;
1561 ctx->supports_advmatte = 1;
1562 ctx->supports_luster = 1;
1563 ctx->supports_rewind = 1;
1564 ctx->supports_pano = 1; // XXX what version?
1565
1566 if (FW_VER_CHECK(1,01)) {
1567 ctx->supports_finematte = 1;
1568 ctx->supports_contpano = 1; // XXX what version?
1569 }
1570 if (FW_VER_CHECK(1,10))
1571 ctx->supports_6x9 = ctx->supports_6x4_5 = 1;
1572
1573 } else if (ctx->mfg == MFG_DNP) {
1574 if (strchr(ctx->version, 'A'))
1575 ctx->supports_rewind = 0;
1576 else
1577 ctx->supports_rewind = 1;
1578
1579 if (FW_VER_CHECK(1,10))
1580 ctx->supports_6x9 = ctx->supports_6x4_5 = 1;
1581 if (FW_VER_CHECK(1,20))
1582 ctx->supports_adv_fullcut = ctx->supports_advmatte = ctx->supports_pano = 1;
1583 if (FW_VER_CHECK(1,30))
1584 ctx->supports_luster = 1;
1585 if (FW_VER_CHECK(1,32))
1586 ctx->supports_contpano = 1;
1587 if (FW_VER_CHECK(1,33))
1588 ctx->supports_media_ext = 1;
1589 if (FW_VER_CHECK(1,52))
1590 ctx->supports_finematte = 1;
1591 } else if (ctx->mfg == MFG_FUJIFILM) {
1592 ctx->supports_finematte = 1;
1593 ctx->supports_luster = 1;
1594 ctx->supports_media_ext = 1;
1595 ctx->supports_6x4_5 = 1;
1596 ctx->supports_6x9 = 1;
1597 ctx->supports_rewind = 1;
1598 // XXX Need to confirm these
1599 ctx->supports_adv_fullcut = 1;
1600 ctx->supports_advmatte = 1;
1601 }
1602 break;
1603 case P_DNP_DS820:
1604 ctx->native_width = 2560;
1605 ctx->max_height = 7536;
1606 ctx->correct_count = 1;
1607 ctx->supports_counterp = 1;
1608 ctx->supports_matte = 1;
1609 ctx->supports_fullcut = 1;
1610 ctx->supports_mqty_default = 1;
1611 ctx->supports_standby = 1;
1612 ctx->supports_keepmode = 1;
1613 ctx->supports_iserial = 1;
1614 ctx->supports_adv_fullcut = 1;
1615 ctx->supports_advmatte = 1;
1616 ctx->supports_luster = 1;
1617 ctx->supports_finematte = 1;
1618 ctx->supports_printspeed = 1;
1619 ctx->supports_lowspeed = 1;
1620 ctx->supports_highdensity = 1;
1621 ctx->supports_ctrld_ext = 1;
1622 ctx->supports_mediaoffset = 1;
1623 ctx->supports_mediaclassrfid = 1;
1624 ctx->supports_gamma = 1;
1625 ctx->supports_pano = 1;
1626 ctx->supports_contpano = 1;
1627
1628 if (ctx->mfg == MFG_CITIZEN) { /* Citizen and DNP firmware diverge */
1629 ctx->supports_rewind = 1;
1630 ctx->supports_a4x6 = 1; // XXX need to confirm this!
1631 } else {
1632 if (strchr(ctx->version, 'A'))
1633 ctx->supports_rewind = 0;
1634 else
1635 ctx->supports_rewind = 1;
1636
1637 if (FW_VER_CHECK(1,06))
1638 ctx->supports_a4x6 = 1;
1639 }
1640 break;
1641 case P_DNP_QW410:
1642 ctx->native_width = 1408;
1643 ctx->max_height = 2436;
1644 ctx->correct_count = 1;
1645 ctx->supports_600dpi = 0;
1646 ctx->supports_counterp = 1;
1647 ctx->supports_matte = 1;
1648 ctx->supports_fullcut = 1;
1649 ctx->supports_mqty_default = 1;
1650 ctx->supports_keepmode = 1;
1651 ctx->supports_iserial = 1;
1652 ctx->supports_adv_fullcut = 1;
1653 ctx->supports_advmatte = 1;
1654 ctx->supports_printspeed = 1;
1655 ctx->supports_lowspeed = 1;
1656 ctx->supports_ctrld_ext = 1;
1657 ctx->supports_mediaoffset = 1;
1658 ctx->supports_mediaclassrfid = 1;
1659 ctx->supports_systime = 1;
1660 if (FW_VER_CHECK(1,10))
1661 ctx->supports_45_34 = 1;
1662 break;
1663 default:
1664 ERROR("Unknown printer type %d\n", ctx->conn->type);
1665 return CUPS_BACKEND_FAILED;
1666 }
1667
1668 if (test_mode < TEST_MODE_NOATTACH) {
1669 struct dnpds40_cmd cmd;
1670 uint8_t *resp;
1671 int len = 0;
1672
1673 if (ctx->flash_mode) {
1674 goto skip_queries;
1675 }
1676
1677 /* Get Serial Number */
1678 dnpds40_build_cmd(&cmd, "INFO", "SERIAL_NUMBER", 0);
1679
1680 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1681 if (resp) {
1682 dnpds40_cleanup_string((char*)resp, len);
1683 ctx->serno = (char*) resp;
1684 /* Do NOT free resp! */
1685 } else {
1686 return CUPS_BACKEND_FAILED;
1687 }
1688
1689 /* Query Media Info */
1690 dnpds40_build_cmd(&cmd, "INFO", "MEDIA", 0);
1691
1692 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1693 if (resp) {
1694 char tmp[4];
1695
1696 dnpds40_cleanup_string((char*)resp, len);
1697
1698 memcpy(tmp, resp + 4, 3);
1699 tmp[3] = 0;
1700
1701 ctx->media = atoi(tmp);
1702
1703 if (ctx->conn->type != P_DNP_QW410) {
1704 /* Subtract out the "mark" type */
1705 if (ctx->media & 1)
1706 ctx->media--;
1707 }
1708 free(resp);
1709 } else {
1710 return CUPS_BACKEND_FAILED;
1711 }
1712
1713 /* Try to figure out media subtype */
1714 if (ctx->supports_mediaclassrfid) {
1715 dnpds40_build_cmd(&cmd, "INFO", "MEDIA_CLASS_RFID", 0);
1716 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1717 if (!resp)
1718 return CUPS_BACKEND_FAILED;
1719
1720 dnpds40_cleanup_string((char*)resp, len);
1721 ctx->media_subtype = atoi((char*)resp);
1722 free(resp);
1723 }
1724
1725 /* And sticker */
1726 if (ctx->supports_media_ext) {
1727 int type;
1728 dnpds40_build_cmd(&cmd, "INFO", "MEDIA_EXT_CODE", 0);
1729 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1730 if (!resp)
1731 return CUPS_BACKEND_FAILED;
1732
1733 dnpds40_cleanup_string((char*)resp, len);
1734 type = atoi((char*)resp+7);
1735 ctx->media_sticker = (type == 1);
1736 free(resp);
1737 }
1738
1739 if (ctx->conn->type == P_DNP_DS80D) {
1740 if (dnpds80dx_query_paper(ctx))
1741 return CUPS_BACKEND_FAILED;
1742 }
1743 } else {
1744 switch(ctx->conn->type) {
1745 case P_DNP_DS80D:
1746 ctx->duplex_media = 200;
1747 /* Intentional fallthrough */
1748 case P_DNP_DS80:
1749 case P_DNP_DS820:
1750 ctx->media = 510; /* 8x12 */
1751 break;
1752 case P_DNP_DSRX1:
1753 ctx->media = 310; /* 6x8 */
1754 break;
1755 default:
1756 ctx->media = 400; /* 6x9 */
1757 break;
1758 }
1759
1760 if (getenv("MEDIA_CODE"))
1761 ctx->media = strtol(getenv("MEDIA_CODE"), NULL, 10);
1762 }
1763
1764 ctx->last_matte = -1;
1765 #ifdef STATE_DIR
1766 /* Check our current job's lamination vs previous job. */
1767 {
1768 /* Load last matte status from file */
1769 char buf[64];
1770 FILE *f;
1771 snprintf(buf, sizeof(buf), STATE_DIR "/%s-last", ctx->serno);
1772 f = fopen(buf, "r");
1773 if (f) {
1774 fscanf(f, "%d", &ctx->last_matte);
1775 fclose(f);
1776 }
1777 }
1778 #endif
1779 if (test_mode < TEST_MODE_NOATTACH && ctx->supports_mediaoffset) {
1780 /* Get Media Offset */
1781 struct dnpds40_cmd cmd;
1782 uint8_t *resp;
1783 int len = 0;
1784
1785 dnpds40_build_cmd(&cmd, "INFO", "MEDIA_OFFSET", 0);
1786 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1787 if (resp) {
1788 ctx->mediaoffset = atoi((char*)resp+4);
1789 free(resp);
1790 } else {
1791 return CUPS_BACKEND_FAILED;
1792 }
1793 } else if (!ctx->correct_count) {
1794 ctx->mediaoffset = 50;
1795 }
1796
1797 if (test_mode < TEST_MODE_NOATTACH && ctx->supports_mqty_default) {
1798 struct dnpds40_cmd cmd;
1799 uint8_t *resp;
1800 int len = 0;
1801
1802 dnpds40_build_cmd(&cmd, "INFO", "MQTY_DEFAULT", 0);
1803
1804 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
1805 if (resp) {
1806 dnpds40_cleanup_string((char*)resp, len);
1807 ctx->media_count_new = atoi((char*)resp+4);
1808 free(resp);
1809 ctx->media_count_new -= ctx->mediaoffset;
1810 } else {
1811 return CUPS_BACKEND_FAILED;
1812 }
1813 } else {
1814 /* Look it up for legacy models & FW */
1815 switch (ctx->conn->type) {
1816 case P_DNP_DS40:
1817 switch (ctx->media) {
1818 case 200: // L
1819 ctx->media_count_new = 460;
1820 break;
1821 case 210: // 2L
1822 ctx->media_count_new = 230;
1823 break;
1824 case 300: // PC
1825 ctx->media_count_new = 400;
1826 break;
1827 case 310: // A5
1828 ctx->media_count_new = 200;
1829 break;
1830 case 400: // A5W
1831 ctx->media_count_new = 180;
1832 break;
1833 default:
1834 ctx->media_count_new = 0;
1835 break;
1836 }
1837 break;
1838 case P_DNP_DSRX1:
1839 switch (ctx->media) {
1840 case 210: // 2L
1841 ctx->media_count_new = 350;
1842 break;
1843 case 300: // PC
1844 ctx->media_count_new = 700;
1845 break;
1846 case 310: // A5
1847 ctx->media_count_new = 350;
1848 break;
1849 default:
1850 ctx->media_count_new = 0;
1851 break;
1852 }
1853 break;
1854 case P_CITIZEN_OP900II:
1855 switch (ctx->media) {
1856 case 210: // 2L
1857 ctx->media_count_new = 350;
1858 break;
1859 case 300: // PC
1860 ctx->media_count_new = 600;
1861 break;
1862 case 310: // A5
1863 ctx->media_count_new = 300;
1864 break;
1865 case 400: // A5W
1866 ctx->media_count_new = 280;
1867 break;
1868 default:
1869 ctx->media_count_new = 0;
1870 break;
1871 }
1872 break;
1873 case P_CITIZEN_CW01:
1874 switch (ctx->media) {
1875 case 300: // PC
1876 ctx->media_count_new = 600;
1877 break;
1878 case 350: // 2L
1879 ctx->media_count_new = 230;
1880 break;
1881 case 400: // A5W
1882 ctx->media_count_new = 280;
1883 break;
1884 default:
1885 ctx->media_count_new = 0;
1886 break;
1887 }
1888 break;
1889 case P_DNP_DS80:
1890 case P_DNP_DS80D:
1891 switch (ctx->media) {
1892 case 500: // 8x10
1893 ctx->media_count_new = 130;
1894 break;
1895 case 510: // 8x12
1896 ctx->media_count_new = 110;
1897 break;
1898 default:
1899 ctx->media_count_new = 0;
1900 break;
1901 }
1902 break;
1903 case P_DNP_DS620:
1904 switch (ctx->media) {
1905 case 200: // L
1906 ctx->media_count_new = 420;
1907 break;
1908 case 210: // 2L
1909 ctx->media_count_new = 230;
1910 break;
1911 case 300: // PC
1912 ctx->media_count_new = 400;
1913 break;
1914 case 310: // A5
1915 ctx->media_count_new = 200;
1916 break;
1917 case 400: // A5W
1918 ctx->media_count_new = 180;
1919 break;
1920 default:
1921 ctx->media_count_new = 0;
1922 break;
1923 }
1924 break;
1925 case P_DNP_DS820:
1926 ctx->media_subtype = 1;
1927 switch (ctx->media) {
1928 case 500: // 8x10
1929 ctx->media_count_new = 260; // ???
1930 break;
1931 case 510: // 8x12
1932 ctx->media_count_new = 220; // ???
1933 break;
1934 case 600: // A4
1935 ctx->media_count_new = 220; // ???
1936 break;
1937 default:
1938 ctx->media_count_new = 0;
1939 break;
1940 }
1941 break;
1942 case P_DNP_QW410:
1943 switch (ctx->media) {
1944 case 150: // 4x6
1945 case 160: // 4.5x6
1946 ctx->media_count_new = 150;
1947 break;
1948 case 151: // 4x8
1949 case 161: // 4.5x8
1950 ctx->media_count_new = 110;
1951 break;
1952 default:
1953 ctx->media_count_new = 0;
1954 break;
1955 }
1956 break;
1957 default:
1958 ctx->media_count_new = 0;
1959 break;
1960 }
1961 }
1962
1963 if (test_mode < TEST_MODE_NOATTACH && ctx->supports_systime) {
1964 /* Set Printer Time */
1965 struct dnpds40_cmd cmd;
1966 char buf[16];
1967 struct tm *tm;
1968
1969 time_t now = time(NULL);
1970 tm = localtime(&now);
1971 strftime(buf, sizeof(buf), "%Y%m%d%H%M%S\r", tm); /* YYYYMMDDHHMMSS\n\0 */
1972 dnpds40_build_cmd(&cmd, "CNTRL", "SET_SYS_TIME", 0);
1973 if ((dnpds40_do_cmd(ctx, &cmd, (uint8_t*) buf, sizeof(buf))) != 0)
1974 return CUPS_BACKEND_FAILED;
1975 }
1976
1977 skip_queries:
1978 /* Fill out marker message */
1979 if (ctx->supports_mediaclassrfid) {
1980 snprintf(ctx->media_text, sizeof(ctx->media_text),
1981 "%s %s", dnpds40_media_types(ctx->media, ctx->media_sticker),
1982 rfid_media_subtypes(ctx->media_subtype));
1983 } else {
1984 snprintf(ctx->media_text, sizeof(ctx->media_text),
1985 "%s", dnpds40_media_types(ctx->media, ctx->media_sticker));
1986 }
1987
1988 /* Fill out marker structure */
1989 ctx->marker[0].color = "#00FFFF#FF00FF#FFFF00";
1990 ctx->marker[0].name = ctx->media_text;
1991 ctx->marker[0].numtype = ctx->media;
1992 ctx->marker[0].levelmax = ctx->media_count_new;
1993 ctx->marker[0].levelnow = CUPS_MARKER_UNKNOWN;
1994 ctx->marker_count = 1;
1995
1996 if (ctx->conn->type == P_DNP_DS80D) {
1997 ctx->marker[1].color = "#00FFFF#FF00FF#FFFF00";
1998 ctx->marker[1].name = dnpds80_duplex_media_types(ctx->duplex_media);
1999 ctx->marker[1].numtype = ctx->duplex_media;
2000 ctx->marker[1].levelmax = ctx->marker[0].levelmax/2;
2001 ctx->marker[1].levelnow = CUPS_MARKER_UNKNOWN;
2002 ctx->marker_count++;
2003 }
2004
2005 return CUPS_BACKEND_OK;
2006 }
2007
2008 static void dnpds40_cleanup_job(const void *vjob) {
2009 const struct dnpds40_printjob *job = vjob;
2010
2011 if (job->databuf)
2012 free(job->databuf);
2013
2014 free((void*)job);
2015 }
2016
2017 static void dnpds40_teardown(void *vctx) {
2018 struct dnpds40_ctx *ctx = vctx;
2019
2020 if (!ctx)
2021 return;
2022
2023 if (test_mode < TEST_MODE_NOATTACH && ctx->conn->type == P_DNP_DS80D) {
2024 struct dnpds40_cmd cmd;
2025
2026 /* Check to see if last print was the front side
2027 of a duplex job, and if so, cancel things so we're done */
2028 if (ctx->last_multicut >= 200 &&
2029 ctx->last_multicut < 300) {
2030 dnpds40_build_cmd(&cmd, "CNTRL", "DUPLEX_CANCEL", 0);
2031 if ((dnpds40_do_cmd(ctx, &cmd, NULL, 0)) != 0)
2032 return;
2033 }
2034 }
2035
2036 if (ctx->serno)
2037 free(ctx->serno);
2038 if (ctx->version)
2039 free(ctx->version);
2040 free(ctx);
2041 }
2042
2043 static int dnpds40_query_status(struct dnpds40_ctx *ctx)
2044 {
2045 struct dnpds40_cmd cmd;
2046 uint8_t *resp;
2047 int count, len;
2048
2049 if (test_mode >= TEST_MODE_NOATTACH)
2050 return 9999;
2051
2052 /* Generate command */
2053 dnpds40_build_cmd(&cmd, "STATUS", "", 0);
2054
2055 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
2056 if (!resp)
2057 return 9999;
2058
2059 dnpds40_cleanup_string((char*)resp, len);
2060 count = atoi((char*)resp);
2061 free(resp);
2062
2063 return count;
2064 }
2065
2066 static int dnpds40_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
2067 struct dnpds40_ctx *ctx = vctx;
2068 int run = 1;
2069 char buf[9] = { 0 };
2070
2071 struct dnpds40_printjob *job = NULL;
2072
2073 if (!ctx)
2074 return CUPS_BACKEND_FAILED;
2075
2076 job = malloc(sizeof(*job));
2077 if (!job) {
2078 ERROR("Memory allocation failure!\n");
2079 return CUPS_BACKEND_RETRY_CURRENT;
2080 }
2081 memset(job, 0, sizeof(*job));
2082 job->common.jobsize = sizeof(*job);
2083 job->printspeed = -1;
2084 job->buffcntrl = -1;
2085
2086 /* There's no way to figure out the total job length in advance, we
2087 have to parse the stream until we get to the image plane data,
2088 and even then the stream can contain arbitrary commands later.
2089
2090 So instead, we allocate a buffer of the maximum possible length,
2091 then parse the incoming stream until we hit the START command at
2092 the end of the job.
2093 */
2094
2095 job->databuf = malloc(MAX_PANOPRINTJOB_LEN);
2096 if (!job->databuf) {
2097 dnpds40_cleanup_job(job);
2098 ERROR("Memory allocation failure!\n");
2099 return CUPS_BACKEND_RETRY_CURRENT;
2100 }
2101
2102 while (run) {
2103 int i, j;
2104 uint32_t remain;
2105
2106 /* Make sure we won't overflow... */
2107 if (job->datalen + sizeof(struct dnpds40_cmd) > MAX_PANOPRINTJOB_LEN) {
2108 ERROR("Buffer overflow when parsing printjob! (%d+%lu)\n",
2109 job->datalen, sizeof(struct dnpds40_cmd));
2110 dnpds40_cleanup_job(job);
2111 return CUPS_BACKEND_CANCEL;
2112 }
2113
2114 /* Read in command header */
2115 i = read(data_fd, job->databuf + job->datalen,
2116 sizeof(struct dnpds40_cmd));
2117 if (i < 0) {
2118 dnpds40_cleanup_job(job);
2119 return i;
2120 } else if (i == 0) {
2121 break;
2122 } else if (i < (int) sizeof(struct dnpds40_cmd)) {
2123 int r = i;
2124 i = read(data_fd, job->databuf + job->datalen + r, sizeof(struct dnpds40_cmd) - r);
2125 if (i < 0) {
2126 dnpds40_cleanup_job(job);
2127 return i;
2128 } else if (i == 0) {
2129 break;
2130 } else if (i < (int)(sizeof(struct dnpds40_cmd) - r)) {
2131 ERROR("Double short read (%d + %d vs %d)\n", r, i, (int)sizeof(struct dnpds40_cmd));
2132 dnpds40_cleanup_job(job);
2133 return CUPS_BACKEND_CANCEL;
2134 }
2135 }
2136
2137 /* Special case handling for beginning of job */
2138 if (job->datalen == 0) {
2139 /* See if job lacks the standard ESC-P start sequence */
2140 if (job->databuf[job->datalen + 0] != 0x1b ||
2141 job->databuf[job->datalen + 1] != 0x50) {
2142 switch(ctx->conn->type) {
2143 case P_DNP_QW410:
2144 i = legacy_qw410_read_parse(job, data_fd, i);
2145 break;
2146 case P_CITIZEN_CW01:
2147 i = legacy_cw01_read_parse(job, data_fd, i);
2148 break;
2149 case P_DNP_DS620:
2150 i = legacy_dnp620_read_parse(job, data_fd, i);
2151 break;
2152 case P_DNP_DS820:
2153 i = legacy_dnp820_read_parse(job, data_fd, i);
2154 break;
2155 case P_DNP_DSRX1:
2156 case P_DNP_DS40:
2157 case P_DNP_DS80:
2158 case P_DNP_DS80D:
2159 default:
2160 i = legacy_dnp_read_parse(job, data_fd, i);
2161 break;
2162 }
2163
2164 if (i == CUPS_BACKEND_OK) {
2165 goto parsed;
2166 }
2167 dnpds40_cleanup_job(job);
2168 return i;
2169 }
2170 }
2171
2172 /* Parse out length of data chunk, if any */
2173 memcpy(buf, job->databuf + job->datalen + 24, 8);
2174 j = atoi(buf);
2175
2176 /* Read in data chunk as quickly as possible */
2177 remain = j;
2178
2179 /* Make sure we won't overflow... */
2180 if (job->datalen + remain > MAX_PANOPRINTJOB_LEN) {
2181 ERROR("Buffer overflow when parsing printjob! (%d+%d)\n",
2182 job->datalen, remain);
2183 dnpds40_cleanup_job(job);
2184 return CUPS_BACKEND_CANCEL;
2185 }
2186
2187 while (remain > 0) {
2188 i = read(data_fd, job->databuf + job->datalen + sizeof(struct dnpds40_cmd),
2189 remain);
2190 if (i < 0) {
2191 ERROR("Data Read Error: %d (%d/%d @%d/%d)\n", i, remain, j, job->datalen,MAX_PANOPRINTJOB_LEN);
2192 dnpds40_cleanup_job(job);
2193 return i;
2194 }
2195 if (i == 0) {
2196 dnpds40_cleanup_job(job);
2197 return 1;
2198 }
2199 job->datalen += i;
2200 remain -= i;
2201 }
2202 job->datalen -= j; /* Back it off */
2203
2204 /* Check for some offsets */
2205 if(!memcmp("CNTRL QTY", job->databuf + job->datalen+2, 9)) {
2206 memcpy(buf, job->databuf + job->datalen + 32, 8);
2207 job->common.copies = atoi(buf);
2208 continue;
2209 }
2210 if(!memcmp("CNTRL CUTTER", job->databuf + job->datalen+2, 12)) {
2211 memcpy(buf, job->databuf + job->datalen + 32, 8);
2212 job->cutter = atoi(buf);
2213 /* We'll insert it ourselves later */
2214
2215 if (job->cutter == 1000)
2216 job->is_pano = 1;
2217
2218 continue;
2219 }
2220 if(!memcmp("CNTRL BUFFCNTRL", job->databuf + job->datalen+2, 15)) {
2221 memcpy(buf, job->databuf + job->datalen + 32, 8);
2222 job->buffcntrl = atoi(buf);
2223 continue;
2224 }
2225 if(!memcmp("CNTRL OVERCOAT", job->databuf + job->datalen+2, 14)) {
2226 if (ctx->supports_matte) {
2227 memcpy(buf, job->databuf + job->datalen + 32, 8);
2228 job->matte = atoi(buf);
2229 } else {
2230 WARNING("Printer FW does not support matte prints, using glossy mode\n");
2231 }
2232
2233 if (job->matte && ctx->media_sticker) {
2234 WARNING("Sticker media, forcing glossy mode\n");
2235 job->matte = 0;
2236 }
2237
2238 /* We'll insert our own later, if appropriate */
2239 continue;
2240 }
2241 if(!memcmp("IMAGE MULTICUT", job->databuf + job->datalen+2, 14)) {
2242 memcpy(buf, job->databuf + job->datalen + 32, 8);
2243 job->multicut = atoi(buf);
2244 /* Backend automatically handles rewind support, so
2245 ignore application requests to use it. */
2246 if (job->multicut > 400 && job->multicut < 1000)
2247 job->multicut -= 400;
2248
2249 /* We'll insert this ourselves later. */
2250 continue;
2251 }
2252 if(!memcmp("CNTRL FULL_CUTTER_SET", job->databuf + job->datalen+2, 21)) {
2253 if (!ctx->supports_fullcut) {
2254 WARNING("Printer FW does not support full cutter control!\n");
2255 continue;
2256 }
2257
2258 if (ctx->conn->type == P_DNP_DS820) {
2259 if (j != 24) {
2260 WARNING("Full cutter argument length incorrect, ignoring!\n");
2261 continue;
2262 }
2263 } else if (j != 16) {
2264 WARNING("Full cutter argument length incorrect, ignoring!\n");
2265 continue;
2266 } else if (!ctx->supports_adv_fullcut) {
2267 if (job->databuf[job->datalen + 32 + 12] != '0' ||
2268 job->databuf[job->datalen + 32 + 13] != '0' ||
2269 job->databuf[job->datalen + 32 + 14] != '0') {
2270 WARNING("Full cutter scrap setting not supported on this firmware, ignoring!\n");
2271 continue;
2272 }
2273 }
2274 // XXX enforce cut counts/sizes?
2275
2276 job->fullcut = 1;
2277 }
2278 if(!memcmp("IMAGE YPLANE", job->databuf + job->datalen + 2, 12)) {
2279 uint32_t y_ppm; /* Pixels Per Meter */
2280
2281 /* Validate vertical resolution */
2282 memcpy(&y_ppm, job->databuf + job->datalen + 32 + 42, sizeof(y_ppm));
2283 y_ppm = le32_to_cpu(y_ppm);
2284
2285 switch (y_ppm) {
2286 case 11808:
2287 job->dpi = 300;
2288 break;
2289 case 13146:
2290 job->dpi = 334;
2291 break;
2292 case 23615:
2293 case 23616:
2294 job->dpi = 600;
2295 break;
2296 default:
2297 ERROR("Unrecognized printjob resolution (%u ppm)\n", y_ppm);
2298 dnpds40_cleanup_job(job);
2299 return CUPS_BACKEND_CANCEL;
2300 }
2301
2302 /* Validate horizontal size */
2303 memcpy(&y_ppm, job->databuf + job->datalen + 32 + 18, sizeof(y_ppm));
2304 y_ppm = le32_to_cpu(y_ppm);
2305 if (y_ppm != ctx->native_width) {
2306 ERROR("Incorrect horizontal resolution (%u), aborting!\n", y_ppm);
2307 dnpds40_cleanup_job(job);
2308 return CUPS_BACKEND_CANCEL;
2309 }
2310
2311 /* Capture number of rows */
2312 memcpy(&y_ppm, job->databuf + job->datalen + 32 + 22, sizeof(y_ppm));
2313 job->rows = le32_to_cpu(y_ppm);
2314 }
2315 if(!memcmp("CNTRL PRINTSPEED", job->databuf + job->datalen + 2, 16)) {
2316 if (!ctx->supports_printspeed) {
2317 WARNING("Printer does not support PRINTSPEED\n");
2318 continue;
2319 }
2320 memcpy(buf, job->databuf + job->datalen + 32, 8);
2321 job->printspeed = atoi(buf) / 10;
2322
2323 /* We'll insert this ourselves later. */
2324 continue;
2325 }
2326 if(!memcmp("CNTRL CONT_PANORAMA", job->databuf + job->datalen + 2, 18)) {
2327 if (!ctx->supports_contpano) {
2328 ERROR("Printer does not support Continuous Panorama, aborting!\n");
2329 continue;
2330 }
2331 memcpy(buf, job->databuf + job->datalen + 32 + 4, 4);
2332 job->is_pano = atoi(buf);
2333 }
2334
2335 /* This is the last block.. */
2336 if(!memcmp("CNTRL START", job->databuf + job->datalen + 2, 11))
2337 run = 0;
2338
2339 /* Add in the size of this chunk */
2340 job->datalen += sizeof(struct dnpds40_cmd) + j;
2341 }
2342
2343 /* Figure out the number of buffers we need. */
2344 job->buf_needed = 1;
2345
2346 if (job->dpi == 600) {
2347 switch(ctx->conn->type) {
2348 case P_DNP_DS620:
2349 if (job->multicut == MULTICUT_6x9 ||
2350 job->multicut == MULTICUT_6x4_5X2)
2351 job->buf_needed = 2;
2352 break;
2353 case P_DNP_DS80: /* DS80/CX-W */
2354 if (job->matte && (job->multicut == MULTICUT_8xA4LEN ||
2355 job->multicut == MULTICUT_8x4X3 ||
2356 job->multicut == MULTICUT_8x8_8x4 ||
2357 job->multicut == MULTICUT_8x6X2 ||
2358 job->multicut == MULTICUT_8x12))
2359 job->buf_needed = 2;
2360 break;
2361 case P_DNP_DS80D:
2362 if (job->matte) {
2363 int mcut = job->multicut;
2364
2365 if (mcut > MULTICUT_S_BACK)
2366 mcut -= MULTICUT_S_BACK;
2367 else if (mcut > MULTICUT_S_FRONT)
2368 mcut -= MULTICUT_S_FRONT;
2369
2370 if (mcut == MULTICUT_8xA4LEN ||
2371 mcut == MULTICUT_8x4X3 ||
2372 mcut == MULTICUT_8x8_8x4 ||
2373 mcut == MULTICUT_8x6X2 ||
2374 mcut == MULTICUT_8x12)
2375 job->buf_needed = 2;
2376
2377 if (mcut == MULTICUT_S_8x12 ||
2378 mcut == MULTICUT_S_8x6X2 ||
2379 mcut == MULTICUT_S_8x4X3)
2380 job->buf_needed = 2;
2381 }
2382 break;
2383 case P_DNP_DS820:
2384 // Nothing; all sizes only need 1 buffer
2385 break;
2386 case P_CITIZEN_CW01:
2387 job->buf_needed = 2;
2388 break;
2389 default: /* DS40/CX/RX1/CY/everything else */
2390 if (job->matte) {
2391 if (job->multicut == MULTICUT_6x8 ||
2392 job->multicut == MULTICUT_6x9 ||
2393 job->multicut == MULTICUT_6x4X2 ||
2394 job->multicut == MULTICUT_5x7 ||
2395 job->multicut == MULTICUT_5x3_5X2)
2396 job->buf_needed = 2;
2397
2398 } else {
2399 if (job->multicut == MULTICUT_6x8 ||
2400 job->multicut == MULTICUT_6x9 ||
2401 job->multicut == MULTICUT_6x4X2)
2402 job->buf_needed = 1;
2403 }
2404 break;
2405 }
2406 }
2407
2408 if (job->multicut >= MULTICUT_PANO_6x14)
2409 {
2410 int rval = dnp_panorama_splitjob(ctx, job, (struct dnpds40_printjob**)vjob);
2411
2412 /* Clean up original job regardless */
2413 dnpds40_cleanup_job(job);
2414 if (rval)
2415 return rval;
2416
2417 /* And continue validating everything based on the 1st job */
2418 job = (struct dnpds40_printjob*) *vjob;
2419 }
2420
2421 parsed:
2422 /* If we have no data.. don't bother */
2423 if (!job->datalen) {
2424 dnpds40_cleanup_job(job);
2425 return CUPS_BACKEND_CANCEL;
2426 }
2427
2428 /* Use larger of the copy arguments */
2429 if (!job->is_pano && job->common.copies < copies)
2430 job->common.copies = copies;
2431
2432 /* Make sure advanced matte modes are supported */
2433 if (job->matte > 100) {
2434 if (!ctx->supports_advmatte) {
2435 ERROR("Printer does not support advanced matte modes, aborting!\n");
2436 return CUPS_BACKEND_CANCEL;
2437 }
2438 if (ctx->partialmatte == 1) {
2439 ctx->partialmatte = 2;
2440 } else {
2441 INFO("Partial matte lamination layer!\n");
2442 ctx->partialmatte = 1;
2443 }
2444 } else {
2445 if (ctx->partialmatte == 1)
2446 ctx->partialmatte = 2;
2447 }
2448
2449 /* Sanity check matte mode */
2450 if ((job->matte == 21 || job->matte == 121) && !ctx->supports_finematte) {
2451 WARNING("Printer FW does not support Fine Matte mode, downgrading to normal matte\n");
2452 job->matte = 1;
2453 } else if ((job->matte == 22 || job->matte == 122) && !ctx->supports_luster) {
2454 WARNING("Printer FW does not support Luster mode, downgrading to normal matte\n");
2455 job->matte = 1;
2456 } else if (job->matte > 1 && !ctx->supports_advmatte) {
2457 WARNING("Printer FW does not support advanced matte modes, downgrading to normal matte\n");
2458 job->matte = 1;
2459 }
2460
2461 /* QW410 only supports 0 and 3, supposedly. */
2462 if (ctx->conn->type == P_DNP_QW410 && job->printspeed > 1) {
2463 job->printspeed = 3;
2464 }
2465
2466 /* Pick a sane default value for printspeed if not specified */
2467 if (job->printspeed == -1 || job->printspeed > 3)
2468 {
2469 if (job->dpi == 600)
2470 job->printspeed = 1;
2471 else
2472 job->printspeed = 0;
2473 }
2474
2475 if (job->dpi == 600 && !ctx->supports_600dpi) {
2476 ERROR("Printer does not support 600dpi!\n");
2477 dnpds40_cleanup_job(job);
2478 return CUPS_BACKEND_CANCEL;
2479 }
2480
2481 /* And sanity-check whatever value is there */
2482 if (job->printspeed == 0 && job->dpi == 600) {
2483 job->printspeed = 1;
2484 } else if (job->printspeed >= 1 && job->dpi == 300) {
2485 job->printspeed = 0;
2486 }
2487
2488 /* Make sure MULTICUT is sane, most validation needs this */
2489 if (!job->multicut && ctx->conn->type != P_CITIZEN_CW01) {
2490 WARNING("Missing or illegal MULTICUT command!\n");
2491 if (job->dpi == 300)
2492 job->buf_needed = 1;
2493 else
2494 job->buf_needed = 2;
2495
2496 goto skip_checks;
2497 }
2498
2499 /* Only DS80D supports Cut Paper types */
2500 if (job->multicut > 100) {
2501 if ( ctx->conn->type == P_DNP_DS80D) {
2502 job->cut_paper = 1;
2503 } else {
2504 ERROR("Only DS80D supports cut-paper sizes!\n");
2505 dnpds40_cleanup_job(job);
2506 return CUPS_BACKEND_CANCEL;
2507 }
2508 }
2509
2510 if (job->dpi == 334 && ctx->conn->type != P_CITIZEN_CW01)
2511 {
2512 ERROR("Illegal resolution (%u) for printer!\n", job->dpi);
2513 dnpds40_cleanup_job(job);
2514 return CUPS_BACKEND_CANCEL;
2515 }
2516
2517 /* Sanity-check type vs loaded media */
2518 if (job->multicut == 0)
2519 goto skip_multicut;
2520
2521 /* Extra sanity checks */
2522 if (ctx->media == 0) {
2523 int status = dnpds40_query_status(ctx);
2524 if (status > 1000) {
2525 ERROR("User-Recoverable Printer Error: %d => %s, halting queue!\n", status, dnpds40_statuses(status));
2526 return CUPS_BACKEND_STOP;
2527 }
2528 if (status > 2000) {
2529 ERROR("Fatal Printer Hardware Error: %d => %s, halting queue!\n", status, dnpds40_statuses(status));
2530 return CUPS_BACKEND_STOP;
2531 }
2532 }
2533
2534 if (job->multicut < 100) {
2535 switch(ctx->media) {
2536 case 150: // 4x6, QW410
2537 if(job->multicut != MULTICUT_4x3 &&
2538 job->multicut != MULTICUT_4x4 &&
2539 job->multicut != MULTICUT_4x4_5 &&
2540 // XXX job->multicut != MULTICUT_4x3X2 &&
2541 job->multicut != MULTICUT_4x6) {
2542 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2543 dnpds40_cleanup_job(job);
2544 return CUPS_BACKEND_CANCEL;
2545 }
2546 break;
2547 case 151: // 4x8, QW410
2548 if (job->multicut != MULTICUT_4x3 &&
2549 job->multicut != MULTICUT_4x4 &&
2550 job->multicut != MULTICUT_4x4_5 &&
2551 job->multicut != MULTICUT_4x6 &&
2552 // XXX job->multicut != MULTICUT_4x3X2 &&
2553 // XXX job->multicut != MULTICUT_4x4X2 &&
2554 job->multicut != MULTICUT_4x8) {
2555 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2556 dnpds40_cleanup_job(job);
2557 return CUPS_BACKEND_CANCEL;
2558 }
2559 break;
2560 case 160: // 4.5x6, QW410
2561 if (job->multicut != MULTICUT_4_5x3 &&
2562 job->multicut != MULTICUT_4_5x4 &&
2563 job->multicut != MULTICUT_4_5x4_5 &&
2564 // XXX job->multicut != MULTICUT_4_5x3X2 &&
2565 job->multicut != MULTICUT_4_5x6) {
2566 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2567 dnpds40_cleanup_job(job);
2568 return CUPS_BACKEND_CANCEL;
2569 }
2570 break;
2571 case 161: // 4.5x8, QW410
2572 if (job->multicut != MULTICUT_4_5x3 &&
2573 job->multicut != MULTICUT_4_5x4 &&
2574 job->multicut != MULTICUT_4_5x4_5 &&
2575 job->multicut != MULTICUT_4_5x6 &&
2576 // XXX job->multicut != MULTICUT_4_5x4X2 &&
2577 // XXX job->multicut != MULTICUT_4_5x3X2 &&
2578 job->multicut != MULTICUT_4_5x8) {
2579 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2580 dnpds40_cleanup_job(job);
2581 return CUPS_BACKEND_CANCEL;
2582 }
2583 break;
2584 case 200: //"5x3.5 (L)"
2585 if (job->multicut != MULTICUT_5x3_5) {
2586 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2587 dnpds40_cleanup_job(job);
2588 return CUPS_BACKEND_CANCEL;
2589 }
2590 break;
2591 case 210: //"5x7 (2L)"
2592 if (job->multicut != MULTICUT_5x3_5 && job->multicut != MULTICUT_5x7 &&
2593 job->multicut != MULTICUT_5x3_5X2 && job->multicut != MULTICUT_5x5) {
2594 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2595 dnpds40_cleanup_job(job);
2596 return CUPS_BACKEND_CANCEL;
2597 }
2598 /* Only 3.5x5 on 7x5 media can be rewound */
2599 if (job->multicut == MULTICUT_5x3_5)
2600 job->can_rewind = 1;
2601 break;
2602 case 300: //"6x4 (PC)"
2603 if (job->multicut != MULTICUT_6x4) {
2604 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2605 dnpds40_cleanup_job(job);
2606 return CUPS_BACKEND_CANCEL;
2607 }
2608 break;
2609 case 310: //"6x8 (A5)"
2610 if (job->multicut != MULTICUT_6x4 && job->multicut != MULTICUT_6x8 &&
2611 job->multicut != MULTICUT_6x4X2 &&
2612 job->multicut != MULTICUT_6x6 && job->multicut != 30) {
2613 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2614 dnpds40_cleanup_job(job);
2615 return CUPS_BACKEND_CANCEL;
2616 }
2617 /* Only 6x4 on 6x8 media can be rewound */
2618 if (job->multicut == MULTICUT_6x4)
2619 job->can_rewind = 1;
2620 break;
2621 case 400: //"6x9 (A5W)"
2622 if (job->multicut != MULTICUT_6x4 && job->multicut != MULTICUT_6x8 &&
2623 job->multicut != MULTICUT_6x9 && job->multicut != MULTICUT_6x4X2 &&
2624 job->multicut != MULTICUT_6x6 &&
2625 job->multicut != MULTICUT_6x4_5 && job->multicut != MULTICUT_6x4_5X2) {
2626 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2627 dnpds40_cleanup_job(job);
2628 return CUPS_BACKEND_CANCEL;
2629 }
2630 /* Only 6x4 or 6x4.5 on 6x9 media can be rewound */
2631 if (job->multicut == MULTICUT_6x4 || job->multicut == MULTICUT_6x4_5)
2632 job->can_rewind = 1;
2633 break;
2634 case 500: //"8x10"
2635 if (ctx->conn->type == P_DNP_DS820 &&
2636 (job->multicut == MULTICUT_8x7 || job->multicut == MULTICUT_8x9)) {
2637 /* These are okay */
2638 } else if (job->multicut < MULTICUT_8x10 || job->multicut == MULTICUT_8x12 ||
2639 job->multicut == MULTICUT_8x6X2 || job->multicut >= MULTICUT_8x6_8x5 ) {
2640 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2641 dnpds40_cleanup_job(job);
2642 return CUPS_BACKEND_CANCEL;
2643 }
2644
2645 /* 8x4, 8x5 can be rewound */
2646 if (job->multicut == MULTICUT_8x4 ||
2647 job->multicut == MULTICUT_8x5)
2648 job->can_rewind = 1;
2649 break;
2650 case 510: //"8x12"
2651 if (job->multicut < MULTICUT_8x10 || (job->multicut > MULTICUT_8xA4LEN && !(job->multicut == MULTICUT_8x7 || job->multicut == MULTICUT_8x9))) {
2652 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2653 dnpds40_cleanup_job(job);
2654 return CUPS_BACKEND_CANCEL;
2655 }
2656
2657 /* 8x4, 8x5, 8x6 can be rewound */
2658 if (job->multicut == MULTICUT_8x4 ||
2659 job->multicut == MULTICUT_8x5 ||
2660 job->multicut == MULTICUT_8x6)
2661 job->can_rewind = 1;
2662 break;
2663 case 600: //"A4"
2664 if (job->multicut < MULTICUT_A5 || job->multicut > MULTICUT_A4x5X2) {
2665 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->media, job->multicut);
2666 dnpds40_cleanup_job(job);
2667 return CUPS_BACKEND_CANCEL;
2668 }
2669 /* Only A4x4 and A5 can be rewound */
2670 // XXX we can fake more with fancy multicuts...
2671 if (job->multicut == MULTICUT_A4x5 ||
2672 job->multicut == MULTICUT_A5)
2673 job->can_rewind = 1;
2674 break;
2675 default:
2676 ERROR("Unknown media (%u vs %u)!\n", ctx->media, job->multicut);
2677 dnpds40_cleanup_job(job);
2678 return CUPS_BACKEND_CANCEL;
2679 }
2680 } else if (job->multicut < 400) {
2681 int mcut = job->multicut;
2682
2683 switch(ctx->duplex_media) {
2684 case 100: //"8x10.75"
2685 if (mcut > MULTICUT_S_BACK)
2686 mcut -= MULTICUT_S_BACK;
2687 else if (mcut > MULTICUT_S_FRONT)
2688 mcut -= MULTICUT_S_FRONT;
2689
2690 if (mcut == MULTICUT_S_8x12 ||
2691 mcut == MULTICUT_S_8x6X2 ||
2692 mcut == MULTICUT_S_8x4X3) {
2693 ERROR("Incorrect media for job loaded (%u vs %u)\n", ctx->duplex_media, job->multicut);
2694 dnpds40_cleanup_job(job);
2695 return CUPS_BACKEND_CANCEL;
2696 }
2697 break;
2698 case 200: //"8x12"
2699 /* Everything is legal */
2700 break;
2701 default:
2702 ERROR("Unknown duplexer media (%u vs %u)!\n", ctx->duplex_media, job->multicut);
2703 dnpds40_cleanup_job(job);
2704 return CUPS_BACKEND_CANCEL;
2705 }
2706 } else {
2707 ERROR("Multicut value out of range! (%u)\n", job->multicut);
2708 dnpds40_cleanup_job(job);
2709 return CUPS_BACKEND_CANCEL;
2710 }
2711
2712 /* Additional santity checks, make sure printer support exists */
2713 if (!ctx->supports_6x6 && job->multicut == MULTICUT_6x6) {
2714 ERROR("Printer does not support 6x6 prints, aborting!\n");
2715 dnpds40_cleanup_job(job);
2716 return CUPS_BACKEND_CANCEL;
2717 }
2718
2719 if (!ctx->supports_5x5 && job->multicut == MULTICUT_5x5) {
2720 ERROR("Printer does not support 5x5 prints, aborting!\n");
2721 dnpds40_cleanup_job(job);
2722 return CUPS_BACKEND_CANCEL;
2723 }
2724
2725 if ((job->multicut == MULTICUT_6x4_5 || job->multicut == MULTICUT_6x4_5X2) &&
2726 !ctx->supports_6x4_5) {
2727 ERROR("Printer does not support 6x4.5 prints, aborting!\n");
2728 dnpds40_cleanup_job(job);
2729 return CUPS_BACKEND_CANCEL;
2730 }
2731 #if 0 // XXX
2732 if (!ctx->supports_45_34 &&
2733 (job->multicut == MULTICUT_45_3 || job->multicut == MULTICUT_45_4 || job->multicut == MULTICUT_45_4)) {
2734 ERROR("Printer does not support 4.5x3, 4.5x4, or 4x4.5 prints, aborting!\n");
2735 dnpds40_cleanup_job(job);
2736 return CUPS_BACKEND_CANCEL;
2737 }
2738 #endif
2739
2740 if (job->multicut == MULTICUT_6x9 && !ctx->supports_6x9) {
2741 ERROR("Printer does not support 6x9 prints, aborting!\n");
2742 dnpds40_cleanup_job(job);
2743 return CUPS_BACKEND_CANCEL;
2744 }
2745
2746 if (job->multicut == MULTICUT_5x3_5X2 && !ctx->supports_3x5x2) {
2747 ERROR("Printer does not support 3.5x5*2 prints, aborting!\n");
2748 dnpds40_cleanup_job(job);
2749 return CUPS_BACKEND_CANCEL;
2750 }
2751
2752 if (!ctx->supports_a4x6 && job->multicut == MULTICUT_A4x6) {
2753 ERROR("Printer does not support A4x6 prints, aborting!\n");
2754 dnpds40_cleanup_job(job);
2755 return CUPS_BACKEND_CANCEL;
2756 }
2757
2758 skip_multicut:
2759
2760 if (job->fullcut && !ctx->supports_adv_fullcut &&
2761 job->multicut != MULTICUT_6x8) {
2762 ERROR("Printer does not support full control on sizes other than 6x8, aborting!\n");
2763 dnpds40_cleanup_job(job);
2764 return CUPS_BACKEND_CANCEL;
2765 }
2766
2767 if (job->fullcut && job->cutter) {
2768 WARNING("Cannot simultaneously use FULL_CUTTER and CUTTER, using the former\n");
2769 job->cutter = 0;
2770 }
2771
2772 /* Panorama sanity checking */
2773 if (job->is_pano) {
2774 if (!ctx->supports_pano) {
2775 ERROR("Printer does not support panorama printing, aborting!\n");
2776 dnpds40_cleanup_job(job);
2777 return CUPS_BACKEND_CANCEL;
2778 }
2779
2780 if (ctx->conn->type == P_DNP_DS620) {
2781 if (job->multicut != MULTICUT_6x8) { // XXX what about 6x9?
2782 ERROR("Panorama mode is only supported with 6x8 prints, aborting!\n");
2783 dnpds40_cleanup_job(job);
2784 return CUPS_BACKEND_CANCEL;
2785 }
2786 } else if (ctx->conn->type == P_DNP_DS820) {
2787 if (job->cutter) { /* Panel panorama */
2788 if (job->multicut != MULTICUT_8x10 &&
2789 job->multicut != MULTICUT_8xA4LEN &&
2790 job->multicut != MULTICUT_8x12 &&
2791 job->multicut != MULTICUT_A4x10 &&
2792 job->multicut != MULTICUT_A4) {
2793 ERROR("Discrete Panorama mode is only supported with 8x10, 8x12, or A4 print sizes, aborting!\n");
2794 dnpds40_cleanup_job(job);
2795 return CUPS_BACKEND_CANCEL;
2796 }
2797 } else {
2798 if (ctx->media == 600) {
2799 ERROR("Continuous panorama mode is not supported on A4 media!\n");
2800 dnpds40_cleanup_job(job);
2801 return CUPS_BACKEND_CANCEL;
2802 }
2803 if (job->multicut != MULTICUT_8x10 &&
2804 job->multicut != MULTICUT_8x12) {
2805 ERROR("Continious Panorama mode is only supported with 8x10 or 8x12 print sizes, aborting!\n");
2806 dnpds40_cleanup_job(job);
2807 return CUPS_BACKEND_CANCEL;
2808
2809 }
2810 }
2811 }
2812
2813 if (job->fullcut) {
2814 ERROR("Cannot use full cutter control in panorama mode!\n");
2815 dnpds40_cleanup_job(job);
2816 return CUPS_BACKEND_CANCEL;
2817 }
2818 }
2819
2820 if (job->cutter == 120) {
2821 if (job->multicut == MULTICUT_6x4 || job->multicut == MULTICUT_6x8) {
2822 if (!ctx->supports_2x6) {
2823 ERROR("Printer does not support 2x6 prints, aborting!\n");
2824 dnpds40_cleanup_job(job);
2825 return CUPS_BACKEND_CANCEL;
2826 }
2827 } else {
2828 ERROR("Printer only supports legacy 2-inch cuts on 4x6 or 8x6 jobs!\n");
2829 dnpds40_cleanup_job(job);
2830 return CUPS_BACKEND_CANCEL;
2831 }
2832 }
2833
2834 skip_checks:
2835 DEBUG("job->dpi %u matte %d mcut %u cutter %d/%d, bufs %d spd %d\n",
2836 job->dpi, job->matte, job->multicut, job->cutter, job->fullcut, job->buf_needed, job->printspeed);
2837
2838 job->common.can_combine = job->can_rewind; /* Any rewindable size can be stacked */
2839
2840 *vjob = job;
2841
2842 return CUPS_BACKEND_OK;
2843 }
2844
2845 static int dnpds40_main_loop(void *vctx, const void *vjob, int wait_on_return) {
2846 struct dnpds40_ctx *ctx = vctx;
2847 int ret;
2848 struct dnpds40_cmd cmd;
2849 uint8_t *resp;
2850 int len = 0;
2851 uint8_t *ptr;
2852 char buf[9];
2853 int status;
2854 int buf_needed;
2855 int multicut;
2856 int count = 0;
2857 int manual_copies = 0;
2858 int copies;
2859
2860 const struct dnpds40_printjob *job = vjob;
2861
2862 if (!ctx)
2863 return CUPS_BACKEND_FAILED;
2864 if (!job)
2865 return CUPS_BACKEND_FAILED;
2866
2867 buf_needed = job->buf_needed;
2868 multicut = job->multicut;
2869 copies = job->common.copies;
2870
2871 /* If we switch major overcoat modes, we need both buffers */
2872 if (!!job->matte != ctx->last_matte)
2873 buf_needed = 2;
2874
2875 if (job->cutter == 120) {
2876 /* Work around firmware bug on DS40 where if we run out
2877 of media, we can't resume the job without losing the
2878 cutter setting. */
2879 // XXX add version test? what about other printers?
2880 manual_copies = 1;
2881 }
2882
2883 /* Partial matte operation:
2884 - Two buffers to start
2885 - One buffer to complete
2886 */
2887 if (ctx->partialmatte == 1) {
2888 copies = 1; /* Only send the partial matte layer over once */
2889 buf_needed = 2;
2890 } else if (ctx->partialmatte == 2) {
2891 buf_needed = 1;
2892 }
2893
2894 /* Panorama sanity checking */
2895 if (ctx->pano && copies > 1) {
2896 WARNING("Cannot generate multiple copies in panorama mode!\n");
2897 copies = 1;
2898 }
2899
2900 /* RX1HS requires HS media, but the only way to tell is that the
2901 HS media reports a lot code, while the non-HS media does not. */
2902 if (ctx->needs_mlot) {
2903 /* Get Media Lot */
2904 dnpds40_build_cmd(&cmd, "INFO", "MLOT", 0);
2905
2906 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
2907 if (!resp)
2908 return CUPS_BACKEND_FAILED;
2909
2910 dnpds40_cleanup_string((char*)resp, len);
2911
2912 len = strlen((char*)resp);
2913 free(resp);
2914 if (!len) {
2915 ERROR("Media does not report a valid lot number (non-HS media in RX1HS?)\n");
2916 return CUPS_BACKEND_STOP;
2917 }
2918 }
2919
2920 top:
2921
2922 /* Query status */
2923 status = dnpds40_query_status(ctx);
2924
2925 /* Figure out what's going on */
2926 switch(status) {
2927 case 0: /* Idle; we can continue! */
2928 case 1: /* Printing */
2929 {
2930 int bufs;
2931
2932 /* Query buffer state */
2933 dnpds40_build_cmd(&cmd, "INFO", "FREE_PBUFFER", 0);
2934 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
2935
2936 if (!resp)
2937 return CUPS_BACKEND_FAILED;
2938
2939 dnpds40_cleanup_string((char*)resp, len);
2940 /* Check to see if we have sufficient buffers */
2941 bufs = atoi(((char*)resp)+3);
2942 free(resp);
2943 if (bufs < buf_needed) {
2944 INFO("Insufficient printer buffers (%d vs %d), retrying...\n", bufs, buf_needed);
2945 sleep(1);
2946 goto top;
2947 }
2948 break;
2949 }
2950 case 500: /* Cooling print head */
2951 case 510: /* Cooling paper motor */
2952 INFO("Printer cooling down...\n");
2953 sleep(1);
2954 goto top;
2955 case 900:
2956 INFO("Waking printer up from standby...\n");
2957 break;
2958 case 1000: /* Cover open */
2959 case 1010: /* No Scrap Box */
2960 case 1100: /* Paper End */
2961 case 1200: /* Ribbon End */
2962 case 1300: /* Paper Jam */
2963 case 1400: /* Ribbon Error */
2964 ERROR("Printer not ready: %s, please correct...\n", dnpds40_statuses(status));
2965 return CUPS_BACKEND_STOP;
2966 case 1500: /* Paper definition error */
2967 ERROR("Paper definition error, aborting job\n");
2968 return CUPS_BACKEND_CANCEL;
2969 case 1600: /* Data error */
2970 ERROR("Data error, aborting job\n");
2971 return CUPS_BACKEND_CANCEL;
2972 default:
2973 ERROR("Fatal Printer Error: %d => %s, halting queue!\n", status, dnpds40_statuses(status));
2974 return CUPS_BACKEND_STOP;
2975 }
2976
2977 /* Figure out remaining native prints */
2978 if (dnpds40_query_markers(ctx, NULL, NULL))
2979 return CUPS_BACKEND_FAILED;
2980 if (ctx->marker[0].levelnow < 0)
2981 return CUPS_BACKEND_FAILED;
2982 dump_markers(ctx->marker, ctx->marker_count, 0);
2983
2984 // For logic below.
2985 count = ctx->marker[0].levelnow;
2986 if (job->cut_paper && count > ctx->marker[1].levelnow)
2987 count = ctx->marker[1].levelnow;
2988
2989 /* See if we can rewind to save media */
2990 if (job->can_rewind && ctx->supports_rewind) {
2991 /* Tell printer to use rewind */
2992 multicut += 400;
2993
2994 /* Get Media remaining */
2995 dnpds40_build_cmd(&cmd, "INFO", "RQTY", 0);
2996
2997 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
2998 if (!resp)
2999 return CUPS_BACKEND_FAILED;
3000
3001 dnpds40_cleanup_string((char*)resp, len);
3002 count = atoi((char*)resp+4);
3003 free(resp);
3004 }
3005
3006 if (ctx->conn->type == P_CITIZEN_CW01) {
3007 /* Get Vertical resolution */
3008 dnpds40_build_cmd(&cmd, "INFO", "RESOLUTION_V", 0);
3009
3010 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3011 if (!resp)
3012 return CUPS_BACKEND_FAILED;
3013
3014 dnpds40_cleanup_string((char*)resp, len);
3015
3016 #if 0 // TODO Fix 600dpi support on CW01
3017 // have to read the last DPI, and send the correct CWD over?
3018 if (ctx->dpi == 600 && strcmp("RV0334", *char*)resp) {
3019 ERROR("600DPI prints not yet supported, need 600DPI CWD load\n");
3020 return CUPS_BACKEND_CANCEL;
3021 }
3022 #endif
3023 free(resp);
3024 }
3025
3026 /* Verify we have sufficient media for prints */
3027 #if 0 // disabled this to allow error to be reported on the printer panel
3028 if (count < 1) {
3029 ERROR("Printer out of media, please correct!\n");
3030 return CUPS_BACKEND_STOP;
3031 }
3032 #endif
3033
3034 if (count < copies) {
3035 WARNING("Printer does not have sufficient remaining media (%d) to complete job (%d)\n", copies, count);
3036 }
3037
3038 /* Ensure we can complete panorama */
3039 if (ctx->pano && (count < 3)) {
3040 ERROR("Printer does not have sufficent media for panorama job, aborting job!\n");
3041 return CUPS_BACKEND_CANCEL;
3042 }
3043
3044 /* If we're printing a panorama, query state before _first_ page */
3045 if (job->is_pano && ctx->pano == 0) {
3046 int i;
3047 dnpds40_build_cmd(&cmd, "INFO", "PANORAMA_PRINT", 0);
3048
3049 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3050 if (!resp)
3051 return CUPS_BACKEND_FAILED;
3052
3053 dnpds40_cleanup_string((char*)resp, len);
3054 i = atoi((char*)resp);
3055 if (i != 0) {
3056 INFO("Printer Not ready for panorama printing, waiting.. (%d = %s)\n", i, dnpds40_panorama_status((char*)resp));
3057 free(resp);
3058 sleep(1);
3059 goto top;
3060 }
3061 free(resp);
3062 }
3063 if (job->is_pano)
3064 ctx->pano++;
3065
3066 /* Work around a bug in older gutenprint releases. */
3067 if (ctx->last_multicut >= 200 && ctx->last_multicut < 300 &&
3068 multicut >= 200 && multicut < 300) {
3069 WARNING("Bogus multicut value for duplex page, correcting\n");
3070 multicut += 100;
3071 }
3072
3073 /* Store our last multicut state */
3074 ctx->last_multicut = multicut;
3075
3076 /* For continuous panorama jobs we only want to send this stuff on the FIRST page */
3077 if (ctx->pano > 1 && !job->cutter)
3078 goto skip_pageparams;
3079
3080 /* Tell printer how many copies to make */
3081 snprintf(buf, sizeof(buf), "%07d\r", manual_copies ? 1 : copies);
3082 dnpds40_build_cmd(&cmd, "CNTRL", "QTY", 8);
3083 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, 8)))
3084 return CUPS_BACKEND_FAILED;
3085
3086 if (!manual_copies)
3087 copies = 1;
3088
3089 /* Job resumption on correctable errors */
3090 {
3091 int en = job->buffcntrl;
3092 if (!ctx->supports_matte)
3093 en = 0; /* Disable if the printer doesn't support matte overcoat */
3094 else if (ctx->pano)
3095 en = 0; /* Disable recovery for any pano operation */
3096 else if (ctx->conn->type == P_DNP_DS80D && multicut >= 100)
3097 en = 0; /* DS80D does not support this for sheet media */
3098 else if (en == -1)
3099 en = 1;
3100
3101 if (en) {
3102 snprintf(buf, sizeof(buf), "%08d", en);
3103 dnpds40_build_cmd(&cmd, "CNTRL", "BUFFCNTRL", 8);
3104 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, 8)))
3105 return CUPS_BACKEND_FAILED;
3106 }
3107 }
3108
3109 #if 0
3110 /* Tell printer to not retain anything */
3111 snprintf(buf, sizeof(buf), "%08d", 0);
3112 dnpds40_build_cmd(&cmd, "CNTRL", "RETENTION", 8);
3113 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, 8)))
3114 return CUPS_BACKEND_FAILED;
3115 #endif
3116
3117 /* Set overcoat parameters if appropriate */
3118 if (ctx->supports_matte && ctx->partialmatte != 2) {
3119 snprintf(buf, sizeof(buf), "%08d", job->matte);
3120 dnpds40_build_cmd(&cmd, "CNTRL", "OVERCOAT", 8);
3121 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, 8)))
3122 return CUPS_BACKEND_FAILED;
3123 }
3124
3125 /* Program in the cutter setting */
3126 if (job->cutter) {
3127 snprintf(buf, sizeof(buf), "%08d", job->cutter);
3128 dnpds40_build_cmd(&cmd, "CNTRL", "CUTTER", 8);
3129 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, 8)))
3130 return CUPS_BACKEND_FAILED;
3131 }
3132
3133 /* Send over the printspeed if appropriate */
3134 if (ctx->supports_printspeed) {
3135 snprintf(buf, sizeof(buf), "%08d", job->printspeed * 10);
3136 dnpds40_build_cmd(&cmd, "CNTRL", "PRINTSPEED", 8);
3137 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, 8)))
3138 return CUPS_BACKEND_FAILED;
3139 }
3140
3141 /* Program in the multicut setting, if one exists */
3142 /* Do _NOT_ send for continuous panoramas! */
3143 if ((!job->is_pano || job->cutter) && multicut) {
3144 snprintf(buf, sizeof(buf), "%08d", multicut);
3145 dnpds40_build_cmd(&cmd, "IMAGE", "MULTICUT", 8);
3146 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, 8)))
3147 return CUPS_BACKEND_FAILED;
3148 }
3149
3150 skip_pageparams:
3151
3152 /* Finally, send the stream over as individual data chunks */
3153 ptr = job->databuf;
3154 while(ptr && ptr < (job->databuf + job->datalen)) {
3155 int i;
3156 buf[8] = 0;
3157 memcpy(buf, ptr + 24, 8);
3158 i = atoi(buf) + 32;
3159
3160 if ((ret = send_data(ctx->conn,
3161 ptr, i)))
3162 return CUPS_BACKEND_FAILED;
3163
3164 ptr += i;
3165 }
3166 sleep(1); /* Give things a moment */
3167
3168 /* Reset panorama state if we've sent the last panel in the set */
3169 if (!job->is_pano && ctx->pano)
3170 ctx->pano = 0;
3171
3172 /* Reset partial matte state if necessary */
3173 if (ctx->partialmatte == 2)
3174 ctx->partialmatte = 0;
3175
3176 if (!wait_on_return && !manual_copies) {
3177 INFO("Fast return mode enabled.\n");
3178 } else if (!ctx->pano && !ctx->partialmatte) {
3179 INFO("Waiting for job to complete...\n");
3180 int started = 0;
3181
3182 while (1) {
3183 /* Query status */
3184 status = dnpds40_query_status(ctx);
3185
3186 /* If we're idle or there's an error..*/
3187 if (status == 0 && started)
3188 break;
3189 if (status)
3190 started = 1;
3191 if (status >= 1000) {
3192 ERROR("Printer encountered error: %d -> %s\n", status, dnpds40_statuses(status));
3193 /* Note: We are *not* returning a backend error code here as we don't want to
3194 stop the queue unnecessarily. If another job is submitted and the error is
3195 still present, the queue will halt at that time */
3196 break;
3197 }
3198 sleep(1);
3199 }
3200
3201 /* Figure out remaining native prints */
3202 if (dnpds40_query_markers(ctx, NULL, NULL))
3203 return CUPS_BACKEND_FAILED;
3204 dump_markers(ctx->marker, ctx->marker_count, 0);
3205 }
3206
3207 /* Clean up */
3208 if (terminate)
3209 copies = 1;
3210
3211 INFO("Print complete (%d copies remaining)\n", copies - 1);
3212
3213 if (copies && --copies) {
3214 /* No need to wait on buffers due to matte switching */
3215 buf_needed = job->buf_needed;
3216 goto top;
3217 }
3218
3219 /* Finally, account for overcoat mode of last print */
3220 ctx->last_matte = !!job->matte;
3221 #ifdef STATE_DIR
3222 {
3223 /* Store last matte status into file */
3224 char buf[64];
3225 FILE *f;
3226 snprintf(buf, sizeof(buf), STATE_DIR "/%s-last", ctx->serno);
3227 f = fopen(buf, "w");
3228 if (f) {
3229 fprintf(f, "%08d", ctx->last_matte);
3230 fclose(f);
3231 }
3232 }
3233 #endif
3234
3235 return CUPS_BACKEND_OK;
3236 }
3237
3238 static int dnpds40_get_sensors(struct dnpds40_ctx *ctx)
3239 {
3240 struct dnpds40_cmd cmd;
3241 uint8_t *resp;
3242 int len = 0;
3243 char *tok;
3244
3245 /* Get Sensor Info */
3246 dnpds40_build_cmd(&cmd, "INFO", "SENSOR", 0);
3247
3248 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3249 if (!resp)
3250 return CUPS_BACKEND_FAILED;
3251
3252 dnpds40_cleanup_string((char*)resp, len);
3253
3254 tok = strtok((char*)resp, "; -");
3255 do {
3256 char *val = strtok(NULL, "; -");
3257
3258 if (!strcmp("HDT", tok)) {
3259 INFO("Head Temperature : %s\n", val);
3260 } else if (!strcmp("MDT", tok)) {
3261 INFO("Media Temperature : %s\n", val);
3262 } else if (!strcmp("PMK", tok)) {
3263 INFO("Paper Mark : %s\n", val);
3264 } else if (!strcmp("RML", tok)) {
3265 INFO("Ribbon Mark Left : %s\n", val);
3266 } else if (!strcmp("RMC", tok)) {
3267 INFO("Ribbon Mark Right : %s\n", val);
3268 } else if (!strcmp("RMR", tok)) {
3269 INFO("Ribbon Mark Center : %s\n", val);
3270 } else if (!strcmp("PSZ", tok)) {
3271 INFO("Paper Size : %s\n", val);
3272 } else if (!strcmp("PNT", tok)) {
3273 INFO("Paper Notch : %s\n", val);
3274 } else if (!strcmp("PJM", tok)) {
3275 INFO("Paper Jam : %s\n", val);
3276 } else if (!strcmp("PED", tok)) {
3277 INFO("Paper End : %s\n", val);
3278 } else if (!strcmp("PET", tok)) {
3279 INFO("Paper Empty : %s\n", val);
3280 } else if (!strcmp("HDV", tok)) {
3281 INFO("Head Voltage : %s\n", val);
3282 } else if (!strcmp("HMD", tok)) {
3283 INFO("Humidity : %s\n", val);
3284 } else if (!strcmp("RP1", tok)) {
3285 INFO("Roll Paper End 1 : %s\n", val);
3286 } else if (!strcmp("RP2", tok)) {
3287 INFO("Roll Paper End 2 : %s\n", val);
3288 } else if (!strcmp("CSR", tok)) {
3289 INFO("Color Sensor Red : %s\n", val);
3290 } else if (!strcmp("CSG", tok)) {
3291 INFO("Color Sensor Green : %s\n", val);
3292 } else if (!strcmp("CSB", tok)) {
3293 INFO("Color Sensor Blue : %s\n", val);
3294 } else if (!strcmp("DC5", tok)) {
3295 INFO("USB Supply Voltage : %s\n", val);
3296 } else {
3297 INFO("Unknown Sensor: '%s' '%s'\n",
3298 tok, val);
3299 }
3300 } while ((tok = strtok(NULL, "; -")) != NULL);
3301
3302 free(resp);
3303
3304 return CUPS_BACKEND_OK;
3305 }
3306
3307 static int dnpds40_get_info(struct dnpds40_ctx *ctx)
3308 {
3309 struct dnpds40_cmd cmd;
3310 uint8_t *resp;
3311 int len = 0;
3312 int cwd_extra = 0;
3313 int cwd_index = 1;
3314 uint8_t cwd_buf[5];
3315
3316 INFO("Model: %s\n", dnpds40_printer_type(ctx->conn->type, ctx->mfg));
3317
3318 /* Serial number already queried */
3319 INFO("Serial Number: %s\n", ctx->serno);
3320
3321 /* Firmware version already queried */
3322 INFO("Firmware Version: %s\n", ctx->version);
3323
3324 /* Figure out Duplexer */
3325 if (ctx->conn->type == P_DNP_DS80D) {
3326 dnpds40_build_cmd(&cmd, "INFO", "UNIT_FVER", 0);
3327
3328 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3329 if (!resp)
3330 return CUPS_BACKEND_FAILED;
3331
3332 dnpds40_cleanup_string((char*)resp, len);
3333
3334 INFO("Duplexer Version: %s\n", resp);
3335
3336 free(resp);
3337 }
3338
3339 if (ctx->conn->type == P_CITIZEN_CW01) {
3340 /* Get Horizonal resolution */
3341 dnpds40_build_cmd(&cmd, "INFO", "RESOLUTION_H", 0);
3342
3343 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3344 if (!resp)
3345 return CUPS_BACKEND_FAILED;
3346
3347 dnpds40_cleanup_string((char*)resp, len);
3348
3349 INFO("Horizontal Resolution: %s dpi\n", (char*)resp + 3);
3350
3351 free(resp);
3352
3353 /* Get Vertical resolution */
3354 dnpds40_build_cmd(&cmd, "INFO", "RESOLUTION_V", 0);
3355
3356 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3357 if (!resp)
3358 return CUPS_BACKEND_FAILED;
3359
3360 dnpds40_cleanup_string((char*)resp, len);
3361
3362 INFO("Vertical Resolution: %s dpi\n", (char*)resp + 3);
3363
3364 free(resp);
3365
3366 /* Get Color Control Data Version */
3367 dnpds40_build_cmd(&cmd, "TBL_RD", "Version", 0);
3368
3369 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3370 if (!resp)
3371 return CUPS_BACKEND_FAILED;
3372
3373 dnpds40_cleanup_string((char*)resp, len);
3374
3375 INFO("Color Data Version: %s ", (char*)resp);
3376
3377 free(resp);
3378
3379 /* Get Color Control Data Checksum */
3380 dnpds40_build_cmd(&cmd, "MNT_RD", "CTRLD_CHKSUM", 0);
3381
3382 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3383 if (!resp)
3384 return CUPS_BACKEND_FAILED;
3385
3386 dnpds40_cleanup_string((char*)resp, len);
3387
3388 DEBUG2("(%s)\n", (char*)resp);
3389
3390 free(resp);
3391 }
3392
3393 /* Get Media Color offset */
3394 dnpds40_build_cmd(&cmd, "INFO", "MCOLOR", 0);
3395
3396 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3397 if (!resp)
3398 return CUPS_BACKEND_FAILED;
3399
3400 dnpds40_cleanup_string((char*)resp, len);
3401
3402 INFO("Media Color Offset: Y %u M %u C %u L %u\n", *(resp+2), *(resp+3),
3403 *(resp+4), *(resp+5));
3404
3405 free(resp);
3406
3407 /* Get Media Class */
3408 dnpds40_build_cmd(&cmd, "INFO", "MEDIA_CLASS", 0);
3409
3410 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3411 if (!resp)
3412 return CUPS_BACKEND_FAILED;
3413
3414 dnpds40_cleanup_string((char*)resp, len);
3415
3416 INFO("Media Class: %d\n", atoi((char*)resp + 4));
3417
3418 free(resp);
3419
3420 /* Get Media Lot */
3421 dnpds40_build_cmd(&cmd, "INFO", "MLOT", 0);
3422
3423 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3424 if (!resp)
3425 return CUPS_BACKEND_FAILED;
3426
3427 dnpds40_cleanup_string((char*)resp, len);
3428
3429 INFO("Media Lot Code: %s\n", (char*)resp+2);
3430 free(resp);
3431
3432 /* Get Media ID Set (?) */
3433 dnpds40_build_cmd(&cmd, "MNT_RD", "MEDIA_ID_SET", 0);
3434
3435 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3436 if (!resp)
3437 return CUPS_BACKEND_FAILED;
3438
3439 dnpds40_cleanup_string((char*)resp, len);
3440
3441 INFO("Media ID: %d\n", atoi((char*)resp+4));
3442
3443 free(resp);
3444
3445 if (ctx->conn->type == P_CITIZEN_CW01)
3446 goto skip;
3447
3448 /* Get Ribbon ID code (?) */
3449 dnpds40_build_cmd(&cmd, "MNT_RD", "RIBBON_ID_CODE", 0);
3450
3451 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3452 if (!resp)
3453 return CUPS_BACKEND_FAILED;
3454
3455 dnpds40_cleanup_string((char*)resp, len);
3456
3457 INFO("Ribbon ID: %s\n", (char*)resp);
3458
3459 free(resp);
3460
3461 CWD_TOP:
3462 /* Figure out color data and checksums */
3463 if (ctx->supports_ctrld_ext) {
3464 cwd_extra = 4;
3465 snprintf((char*)cwd_buf, sizeof(cwd_buf), "%04d", cwd_index);
3466 } else {
3467 cwd_extra = 0;
3468 cwd_buf[0] = 0;
3469 }
3470
3471 /* 300 DPI */
3472 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD300_Version", cwd_extra);
3473
3474 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3475 if (!resp)
3476 return CUPS_BACKEND_FAILED;
3477
3478 dnpds40_cleanup_string((char*)resp, len);
3479
3480 INFO("300 DPI Color Data (%s): %s ", rfid_media_subtypes(cwd_index), (char*)resp);
3481
3482 free(resp);
3483
3484 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD300_Checksum", cwd_extra);
3485
3486 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3487 if (!resp)
3488 return CUPS_BACKEND_FAILED;
3489
3490 dnpds40_cleanup_string((char*)resp, len);
3491
3492 DEBUG2("(%s)\n", (char*)resp);
3493
3494 free(resp);
3495
3496 if (ctx->supports_600dpi) {
3497 /* 600 DPI */
3498 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD600_Version", cwd_extra);
3499
3500 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3501 if (!resp)
3502 return CUPS_BACKEND_FAILED;
3503
3504 dnpds40_cleanup_string((char*)resp, len);
3505
3506 INFO("600 DPI Color Data (%s): %s ", rfid_media_subtypes(cwd_index), (char*)resp);
3507
3508 free(resp);
3509
3510 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD600_Checksum", cwd_extra);
3511
3512 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3513 if (!resp)
3514 return CUPS_BACKEND_FAILED;
3515
3516 dnpds40_cleanup_string((char*)resp, len);
3517
3518 DEBUG2("(%s)\n", (char*)resp);
3519
3520 free(resp);
3521 }
3522
3523 if (ctx->supports_lowspeed) {
3524 /* "Low Speed" */
3525 if (ctx->supports_600dpi) {
3526 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD610_Version", cwd_extra);
3527 } else {
3528 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD310_Version", cwd_extra);
3529 }
3530
3531 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3532 if (!resp)
3533 return CUPS_BACKEND_FAILED;
3534
3535 dnpds40_cleanup_string((char*)resp, len);
3536
3537 INFO("Low Speed Color Data (%s): %s ", rfid_media_subtypes(cwd_index), (char*)resp);
3538
3539 free(resp);
3540
3541 if (ctx->supports_600dpi) {
3542 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD610_Checksum", cwd_extra);
3543 } else {
3544 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD310_Checksum", cwd_extra);
3545 }
3546
3547 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3548 if (!resp)
3549 return CUPS_BACKEND_FAILED;
3550
3551 dnpds40_cleanup_string((char*)resp, len);
3552
3553 DEBUG2("(%s)\n", (char*)resp);
3554
3555 free(resp);
3556 }
3557 if (ctx->supports_highdensity) {
3558 /* "High Density" */
3559 if (ctx->supports_600dpi) {
3560 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD620_Version", cwd_extra);
3561 } else {
3562 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD320_Version", cwd_extra);
3563 }
3564
3565 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3566 if (!resp)
3567 return CUPS_BACKEND_FAILED;
3568
3569 dnpds40_cleanup_string((char*)resp, len);
3570
3571 INFO("High Density Color Data (%s): %s ", rfid_media_subtypes(cwd_index), (char*)resp);
3572
3573 free(resp);
3574
3575 if (ctx->supports_600dpi) {
3576 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD620_Checksum", cwd_extra);
3577 } else {
3578 dnpds40_build_cmd(&cmd, "TBL_RD", "CWD320_Checksum", cwd_extra);
3579 }
3580
3581 resp = dnpds40_resp_cmd2(ctx, &cmd, &len, cwd_buf, cwd_extra);
3582 if (!resp)
3583 return CUPS_BACKEND_FAILED;
3584
3585 dnpds40_cleanup_string((char*)resp, len);
3586
3587 DEBUG2("(%s)\n", (char*)resp);
3588
3589 free(resp);
3590 }
3591
3592 if (cwd_extra && cwd_index == 1) {
3593 if (ctx->conn->type == P_DNP_QW410)
3594 cwd_index = 2;
3595 else
3596 cwd_index = 3;
3597 goto CWD_TOP;
3598 }
3599
3600 if (ctx->supports_gamma) {
3601 /* "Low Speed" */
3602 dnpds40_build_cmd(&cmd, "TBL_RD", "CTRLD_GAMMA16", 0);
3603
3604 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3605 if (!resp)
3606 return CUPS_BACKEND_FAILED;
3607
3608 dnpds40_cleanup_string((char*)resp, len);
3609
3610 INFO("Gamma Correction Data Checksum: %s\n", (char*)resp);
3611
3612 free(resp);
3613 }
3614
3615 if (ctx->supports_standby) {
3616 /* Get Standby stuff */
3617 int i;
3618
3619 dnpds40_build_cmd(&cmd, "MNT_RD", "STANDBY_TIME", 0);
3620
3621 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3622 if (!resp)
3623 return CUPS_BACKEND_FAILED;
3624
3625 dnpds40_cleanup_string((char*)resp, len);
3626 i = atoi((char*)resp);
3627
3628 INFO("Standby Transition time: %d minutes\n", i);
3629
3630 free(resp);
3631 }
3632
3633 if (ctx->supports_keepmode) {
3634 /* Get Media End Keep */
3635 int i;
3636
3637 dnpds40_build_cmd(&cmd, "MNT_RD", "END_KEEP_MODE", 0);
3638
3639 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3640 if (!resp)
3641 return CUPS_BACKEND_FAILED;
3642
3643 dnpds40_cleanup_string((char*)resp, len);
3644 i = atoi((char*)resp);
3645 INFO("Media End kept across power cycles: %s\n",
3646 i ? "Yes" : "No");
3647
3648 free(resp);
3649 }
3650
3651 if (ctx->supports_pano) {
3652 dnpds40_build_cmd(&cmd, "INFO", "PANORAMA_PRINT", 0);
3653
3654 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3655 if (!resp)
3656 return CUPS_BACKEND_FAILED;
3657
3658 dnpds40_cleanup_string((char*)resp, len);
3659 INFO("Printer is ready to print panorama: %s\n",
3660 dnpds40_panorama_status((char*)resp));
3661
3662 free(resp);
3663 }
3664
3665 if (ctx->supports_iserial == 2) {
3666 INFO("Report Serial Number in USB descriptor: Yes\n");
3667 } else if (ctx->supports_iserial == 1) {
3668 int i;
3669 /* Get USB serial descriptor status */
3670 dnpds40_build_cmd(&cmd, "MNT_RD", "USB_ISERI_SET", 0);
3671
3672 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3673 if (!resp)
3674 return CUPS_BACKEND_FAILED;
3675
3676 dnpds40_cleanup_string((char*)resp, len);
3677 i = atoi((char*)resp);
3678
3679 INFO("Report Serial Number in USB descriptor: %s\n",
3680 i ? "Yes" : "No");
3681
3682 free(resp);
3683 }
3684
3685 skip:
3686 return CUPS_BACKEND_OK;
3687 }
3688
3689 static int dnpds40_get_status(struct dnpds40_ctx *ctx)
3690 {
3691 struct dnpds40_cmd cmd;
3692 uint8_t *resp;
3693 int len = 0;
3694 int count;
3695
3696 /* Generate command */
3697 count = dnpds40_query_status(ctx);
3698
3699 INFO("Printer Status: %s (%d)\n", dnpds40_statuses(count), count);
3700
3701 /* Figure out Duplexer */
3702 if (ctx->conn->type == P_DNP_DS80D) {
3703 dnpds40_build_cmd(&cmd, "INFO", "UNIT_STATUS", 0);
3704
3705 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3706 if (!resp)
3707 return CUPS_BACKEND_FAILED;
3708
3709 dnpds40_cleanup_string((char*)resp, len);
3710 count = atoi((char*)resp);
3711
3712 INFO("Duplexer Status: %s\n", dnpds80_duplex_statuses(count));
3713 INFO("Duplexer Media Status: %s\n", dnpds80_duplex_paper_status(ctx->duplex_media_status));
3714
3715 free(resp);
3716 }
3717
3718 /* Get remaining print quantity */
3719 dnpds40_build_cmd(&cmd, "INFO", "PQTY", 0);
3720
3721 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3722 if (!resp)
3723 return CUPS_BACKEND_FAILED;
3724
3725 dnpds40_cleanup_string((char*)resp, len);
3726
3727 INFO("Prints remaining in job: %d\n", atoi((char*)resp + 4));
3728
3729 free(resp);
3730
3731 /* Generate command */
3732 dnpds40_build_cmd(&cmd, "INFO", "FREE_PBUFFER", 0);
3733
3734 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3735 if (!resp)
3736 return CUPS_BACKEND_FAILED;
3737
3738 dnpds40_cleanup_string((char*)resp, len);
3739
3740 INFO("Free Buffers: %d\n", atoi((char*)resp + 3));
3741 free(resp);
3742
3743 /* Report media */
3744 INFO("Media Type: %s\n", dnpds40_media_types(ctx->media, ctx->media_sticker));
3745
3746 if (ctx->supports_media_ext) {
3747 int type;
3748 dnpds40_build_cmd(&cmd, "INFO", "MEDIA_EXT_CODE", 0);
3749 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3750 if (!resp)
3751 return CUPS_BACKEND_FAILED;
3752
3753 dnpds40_cleanup_string((char*)resp, len);
3754 type = atoi((char*)resp+7);
3755 ctx->media_sticker = (type == 1);
3756 INFO("Media Code: %s\n", dnpds620_media_extension_code(type));
3757 free(resp);
3758 }
3759
3760 /* Try to figure out media subtype */
3761 if (ctx->supports_mediaclassrfid) {
3762 INFO("Media Subtype: %s\n", rfid_media_subtypes(ctx->media_subtype));
3763 }
3764
3765 /* Report Cut Media */
3766 if (ctx->conn->type == P_DNP_DS80D) {
3767 INFO("Duplex Media Type: %s\n", dnpds80_duplex_media_types(ctx->duplex_media));
3768 INFO("Duplexer Media Status: %s\n", dnpds80_duplex_paper_status(ctx->duplex_media_status));
3769 }
3770
3771 if (ctx->media_count_new)
3772 INFO("Native Prints Available on New Media: %u\n", ctx->media_count_new);
3773
3774 /* Get Media remaining */
3775 count = dnpds40_query_mqty(ctx);
3776 if (count < 0)
3777 return CUPS_BACKEND_FAILED;
3778
3779 INFO("Native Prints Remaining on Media: %d\n", count);
3780
3781 if (ctx->supports_rewind) {
3782 /* Get Media remaining */
3783 dnpds40_build_cmd(&cmd, "INFO", "RQTY", 0);
3784
3785 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3786 if (!resp)
3787 return CUPS_BACKEND_FAILED;
3788
3789 dnpds40_cleanup_string((char*)resp, len);
3790
3791 count = atoi((char*)resp+4);
3792 free(resp);
3793 } else {
3794 // Do nothing, re-use native print count.
3795 }
3796 INFO("Half-Size Prints Remaining on Media: %d\n", count);
3797
3798 return CUPS_BACKEND_OK;
3799 }
3800
3801 static int dnpds40_get_counters(struct dnpds40_ctx *ctx)
3802 {
3803 struct dnpds40_cmd cmd;
3804 uint8_t *resp;
3805 int len = 0;
3806
3807 /* Generate command */
3808 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_LIFE", 0);
3809
3810 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3811 if (!resp)
3812 return CUPS_BACKEND_FAILED;
3813
3814 dnpds40_cleanup_string((char*)resp, len);
3815
3816 INFO("Lifetime Counter: %d\n", atoi((char*)resp+2));
3817
3818 free(resp);
3819
3820 if (ctx->conn->type == P_DNP_DS620 ||
3821 ctx->conn->type == P_DNP_DS820) {
3822 /* Generate command */
3823 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_HEAD", 0);
3824
3825 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3826 if (!resp)
3827 return CUPS_BACKEND_FAILED;
3828
3829 dnpds40_cleanup_string((char*)resp, len);
3830
3831 INFO("Head Counter: %d\n", atoi((char*)resp+2));
3832
3833 free(resp);
3834 }
3835
3836 /* Generate command */
3837 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_A", 0);
3838
3839 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3840 if (!resp)
3841 return CUPS_BACKEND_FAILED;
3842
3843 dnpds40_cleanup_string((char*)resp, len);
3844
3845 INFO("A Counter: %d\n", atoi((char*)resp+2));
3846
3847 free(resp);
3848
3849 /* Generate command */
3850 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_B", 0);
3851
3852 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3853 if (!resp)
3854 return CUPS_BACKEND_FAILED;
3855
3856 dnpds40_cleanup_string((char*)resp, len);
3857
3858 INFO("B Counter: %d\n", atoi((char*)resp+2));
3859
3860 free(resp);
3861
3862 if (ctx->supports_counterp) {
3863 /* Generate command */
3864 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_P", 0);
3865
3866 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3867 if (!resp)
3868 return CUPS_BACKEND_FAILED;
3869
3870 dnpds40_cleanup_string((char*)resp, len);
3871
3872 INFO("P Counter: %d\n", atoi((char*)resp+2));
3873
3874 free(resp);
3875 }
3876
3877 if (ctx->supports_matte) {
3878 /* Generate command */
3879 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_M", 0);
3880
3881 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3882 if (!resp)
3883 return CUPS_BACKEND_FAILED;
3884
3885 dnpds40_cleanup_string((char*)resp, len);
3886
3887 INFO("M Counter: %d\n", atoi((char*)resp+2));
3888
3889 free(resp);
3890
3891 /* Generate command */
3892 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_MATTE", 0);
3893
3894 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3895 if (!resp)
3896 return CUPS_BACKEND_FAILED;
3897
3898 dnpds40_cleanup_string((char*)resp, len);
3899
3900 INFO("Matte Counter: %d\n", atoi((char*)resp+4));
3901
3902 free(resp);
3903 }
3904
3905 if (ctx->conn->type == P_DNP_DS80D) {
3906 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_DUPLEX", 0);
3907
3908 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
3909 if (!resp)
3910 return CUPS_BACKEND_FAILED;
3911
3912 dnpds40_cleanup_string((char*)resp, len);
3913
3914 INFO("Duplexer Counter: %d\n", atoi((char*)resp));
3915
3916 free(resp);
3917 }
3918
3919 return CUPS_BACKEND_OK;
3920 }
3921
3922 static int dnpds40_clear_counter(struct dnpds40_ctx *ctx, char counter)
3923 {
3924 struct dnpds40_cmd cmd;
3925 char msg[4];
3926 int ret;
3927
3928 /* Generate command */
3929 dnpds40_build_cmd(&cmd, "MNT_WT", "COUNTER_CLEAR", 4);
3930 msg[0] = 'C';
3931 msg[1] = counter;
3932 msg[2] = 0x0d; /* ie carriage return, ASCII '\r' */
3933 msg[3] = 0x00;
3934
3935 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)msg, 4)))
3936 return ret;
3937
3938 return CUPS_BACKEND_OK;
3939 }
3940
3941 static int dnpds40_cancel_job(struct dnpds40_ctx *ctx)
3942 {
3943 struct dnpds40_cmd cmd;
3944 int ret;
3945
3946 /* Generate command */
3947 dnpds40_build_cmd(&cmd, "CNTRL", "CANCEL", 0);
3948
3949 if ((ret = dnpds40_do_cmd(ctx, &cmd, NULL, 0)))
3950 return ret;
3951
3952 return CUPS_BACKEND_OK;
3953 }
3954
3955 static int dnpds40_reset_printer(struct dnpds40_ctx *ctx)
3956 {
3957 struct dnpds40_cmd cmd;
3958 int ret;
3959
3960 /* Generate command */
3961 dnpds40_build_cmd(&cmd, "CNTRL", "PRINTER_RESET", 0);
3962
3963 if ((ret = dnpds40_do_cmd(ctx, &cmd, NULL, 0)))
3964 return ret;
3965
3966 return CUPS_BACKEND_OK;
3967 }
3968
3969 static int dnpds620_standby_mode(struct dnpds40_ctx *ctx, int delay)
3970 {
3971 struct dnpds40_cmd cmd;
3972 char msg[9];
3973 int ret;
3974
3975 /* Generate command */
3976 dnpds40_build_cmd(&cmd, "MNT_WT", "STANDBY_TIME", 8);
3977 snprintf(msg, sizeof(msg), "%08d", delay);
3978
3979 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)msg, 8)))
3980 return ret;
3981
3982 return CUPS_BACKEND_OK;
3983 }
3984
3985 static int dnpds620_media_keep_mode(struct dnpds40_ctx *ctx, int delay)
3986 {
3987 struct dnpds40_cmd cmd;
3988 char msg[9];
3989 int ret;
3990
3991 /* Generate command */
3992 dnpds40_build_cmd(&cmd, "MNT_WT", "END_KEEP_MODE", 4);
3993 snprintf(msg, sizeof(msg), "%02d\r", delay);
3994
3995 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)msg, 4)))
3996 return ret;
3997
3998 return CUPS_BACKEND_OK;
3999 }
4000
4001 static int dnpds620_iserial_mode(struct dnpds40_ctx *ctx, int enable)
4002 {
4003 struct dnpds40_cmd cmd;
4004 char msg[9];
4005 int ret;
4006
4007 /* Generate command */
4008 dnpds40_build_cmd(&cmd, "MNT_WT", "USB_ISERI_SET", 4);
4009 snprintf(msg, sizeof(msg), "%02d\r", enable);
4010
4011 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)msg, 8)))
4012 return ret;
4013
4014 return CUPS_BACKEND_OK;
4015 }
4016
4017 static int dnpds40_set_counter_p(struct dnpds40_ctx *ctx, const char *arg)
4018 {
4019 struct dnpds40_cmd cmd;
4020 char msg[9];
4021 int i = atoi(arg);
4022 int ret;
4023
4024 /* Generate command */
4025 dnpds40_build_cmd(&cmd, "MNT_WT", "COUNTERP_SET", 8);
4026 snprintf(msg, sizeof(msg), "%08d", i);
4027
4028 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)msg, 8)))
4029 return ret;
4030
4031 return CUPS_BACKEND_OK;
4032 }
4033
4034 static int dnpds40_write_serial(struct dnpds40_ctx *ctx, const char *arg)
4035 {
4036 struct dnpds40_cmd cmd;
4037 char buf[32];
4038 int ret;
4039 memset(buf, ' ', sizeof(buf));
4040 memcpy(buf, arg, strnlen(arg, 32));
4041 dnpds40_build_cmd(&cmd, "MNT_WT", "PRINTER_ID_SET", sizeof(buf));
4042 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*)buf, sizeof(buf)))) {
4043 ERROR("Serial number write failed!");
4044 return -1;
4045 }
4046 INFO("Serial number updated!\n");
4047
4048 return CUPS_BACKEND_OK;
4049 }
4050
4051 static int dnpds40_erase_cwd(struct dnpds40_ctx *ctx)
4052 {
4053 struct dnpds40_cmd cmd;
4054 int ret;
4055 dnpds40_build_cmd(&cmd, "TBL_CL", "", 0);
4056 if ((ret = dnpds40_do_cmd(ctx, &cmd, NULL, 0))) {
4057 ERROR("CWD Erase failed!");
4058 return -1;
4059 }
4060 INFO("CWD data erased\n");
4061
4062 return CUPS_BACKEND_OK;
4063 }
4064
4065 static int dnpds40_write_cwd(struct dnpds40_ctx *ctx, char *filename)
4066 {
4067 struct dnpds40_cmd cmd;
4068 uint8_t *buf;
4069 int len = strlen(filename);
4070 int ret;
4071 struct stat st;
4072 len = (len + 3) / 4 * 4; /* round up to 32-bit boundary */
4073 buf = malloc(len);
4074 if (!buf) {
4075 ERROR("Memory allocation failure!");
4076 return -1;
4077 }
4078
4079 /* Make sure file exits and is readable */
4080 if ((ret = stat(filename, &st))) {
4081 ERROR("Failed to stat file! (%s)\n", filename);
4082 free(buf);
4083 return -1;
4084 }
4085
4086 /* Truncate filename and tell printer about it */
4087 memset(buf, 0, len);
4088 if (strchr(filename, '/'))
4089 strcpy((char*)buf, strrchr(filename, '/') + 1);
4090 else
4091 strcpy((char*)buf, filename);
4092 dnpds40_build_cmd(&cmd, "TBL_WT", "Version", len);
4093 if ((ret = dnpds40_do_cmd(ctx, &cmd, buf, len))) {
4094 ERROR("CWD write failed!");
4095 free(buf);
4096 return -1;
4097 }
4098 free(buf);
4099
4100 /* Allocate buffer and read in CWD data */
4101 len = (st.st_size + 3) / 4 * 4;
4102 buf = malloc(len);
4103 if (!buf) {
4104 ERROR("Memory allocation failure!\n");
4105 return -1;
4106 }
4107
4108 /* Read in file */
4109 if ((ret = dyesub_read_file(filename, buf, st.st_size, NULL))) {
4110 ERROR("Failed to read '%s' (%d)\n", filename, ret);
4111 free(buf);
4112 return -1;
4113 }
4114
4115 /* Dump it all over to the printer! */
4116 dnpds40_build_cmd(&cmd, "TBL_WT", "CTRLD_UPDATE_CW", len);
4117 if ((ret = dnpds40_do_cmd(ctx, &cmd, buf, len))) {
4118 ERROR("CWD Data transfer failed!\n");
4119 free(buf);
4120 return -1;
4121 }
4122 INFO("CWD file '%s' written\n", filename);
4123
4124 free(buf);
4125 return CUPS_BACKEND_OK;
4126 }
4127
4128 static int dnpds40_write_flash(struct dnpds40_ctx *ctx, char *filename)
4129 {
4130 struct dnpds40_cmd cmd;
4131 uint8_t *buf;
4132 int len;
4133 int ret;
4134 struct stat st;
4135
4136 if ((ret = stat(filename, &st))) {
4137 ERROR("Failed to stat file! (%s)\n", filename);
4138 return -1;
4139 }
4140
4141 len = (st.st_size + 3) / 4 * 4;
4142 buf = malloc(len);
4143 if (!buf) {
4144 ERROR("Memory allocation failure!\n");
4145 return -1;
4146 }
4147
4148 /* Read in file */
4149 if ((ret = dyesub_read_file(filename, buf, st.st_size, NULL))) {
4150 ERROR("Failed to read '%s' (%d)\n", filename, ret);
4151 free(buf);
4152 return -1;
4153 }
4154
4155 if (ctx->flash_mode)
4156 goto skip_entry;
4157
4158 /* This triggers a detachment and re-enumeration on the bus */
4159 dnpds40_build_cmd(&cmd, "FW_UPD", "FLASH_REWRITE", 0);
4160 if ((ret = dnpds40_do_cmd(ctx, &cmd, NULL, 0))) {
4161 ERROR("Flash rewrite mode failed!\n");
4162 free(buf);
4163 return -1;
4164 }
4165
4166 // Note: This changes the FW_VER string as follows:
4167 // 'DS-RX1_RW_2.00' vs 'DS-RX1 02.07'
4168 // note the 'RW' presence.
4169
4170 INFO("Waiting for printer to re-enumerate...\n");
4171 libusb_close(ctx->conn->dev);
4172
4173 /* Have libusb re-enumerate and attach to the printer after a short delay */
4174 sleep(5);
4175
4176 ctx->conn->dev = libusb_open_device_with_vid_pid(NULL, ctx->conn->usb_vid, ctx->conn->usb_pid);
4177 if (ctx->conn->dev == NULL) {
4178 ERROR("Can't re-attach to printer. Repeat command, or power-cycle printer to reset.\n");
4179 free(buf);
4180 exit(1);
4181 }
4182
4183 // (find altset, new endpoints?)
4184
4185 /* Detach kernel driver */
4186 if (libusb_kernel_driver_active(ctx->conn->dev, ctx->conn->iface))
4187 libusb_detach_kernel_driver(ctx->conn->dev, ctx->conn->iface);
4188
4189 /* Claim interface */
4190 if (backend_claim_interface(ctx->conn->dev, ctx->conn->iface, NUM_CLAIM_ATTEMPTS)) {
4191 ERROR("Can't re-claim interface. Power-cycle printer to reset.\n");
4192 free(buf);
4193 exit(2);
4194 }
4195
4196 skip_entry:
4197 INFO("Writing Printer Flash. DO NOT POWER OFF!\n");
4198
4199 dnpds40_build_cmd(&cmd, "FW_UPD", "FLASH_PROGRAM", len);
4200 if ((ret = dnpds40_do_cmd(ctx, &cmd, buf, len))) {
4201 ERROR("Flash data transfer failed!\n");
4202 free(buf);
4203 exit(3);
4204 }
4205 sleep(5);
4206 INFO("Flash write complete! DO NOT POWER OFF until printer flashes all lights and resets!\n");
4207 INFO("You will likely need to erase CWD data and load each CWD file\n");
4208
4209 free(buf);
4210 exit(0);
4211 }
4212
4213 static int dnpds40_write_duplexflash(struct dnpds40_ctx *ctx, char *filename)
4214 {
4215 struct dnpds40_cmd cmd;
4216 uint8_t *buf;
4217
4218 int len;
4219 int ret;
4220 struct stat st;
4221 char msg[9];
4222
4223 if (ctx->conn->type != P_DNP_DS80D) {
4224 ERROR("Only DS80DX supports updating duplexer firmware!\n");
4225 return -1;
4226 }
4227
4228 if ((ret = stat(filename, &st))) {
4229 ERROR("Failed to stat file! (%s)\n", filename);
4230 return -1;
4231 }
4232
4233 len = (st.st_size + 3) / 4 * 4;
4234 buf = malloc(len);
4235 if (!buf) {
4236 ERROR("Memory allocation failure!\n");
4237 return -1;
4238 }
4239
4240 /* Read in file */
4241 if ((ret = dyesub_read_file(filename, buf, st.st_size, NULL))) {
4242 ERROR("Failed to read '%s' (%d)\n", filename, ret);
4243 free(buf);
4244 return -1;
4245 }
4246
4247 snprintf(msg, sizeof(msg), "%08d", 0); // 0 = 38.4, 1 = 57.6, 2 = 115.2, 3 = 230.4
4248 dnpds40_build_cmd(&cmd, "FW_UPD", "DUNIT_REWRITE", 8);
4249 if ((ret = dnpds40_do_cmd(ctx, &cmd, (uint8_t*) msg, 8))) {
4250 ERROR("Flash rewrite mode failed!\n");
4251 free(buf);
4252 return -1;
4253 }
4254 sleep(2);
4255 // XXXX re-enumerate printer here..?
4256
4257 dnpds40_build_cmd(&cmd, "FW_UPD", "FLASH_PROGRAM", len);
4258 if ((ret = dnpds40_do_cmd(ctx, &cmd, buf, len))) {
4259 ERROR("Flash data transfer failed!\n");
4260 free(buf);
4261 return -1;
4262 }
4263
4264 free(buf);
4265 return CUPS_BACKEND_OK;
4266 }
4267
4268 static int dnpds40_write_gamma(struct dnpds40_ctx *ctx, char *filename)
4269 {
4270 struct dnpds40_cmd cmd;
4271 uint8_t *buf;
4272 int len = strlen(filename);
4273 int ret;
4274 struct stat st;
4275
4276 if (!ctx->supports_gamma) {
4277 ERROR("Printer does not support updating gamma tables\n");
4278 return -1;
4279 }
4280
4281 if ((ret = stat(filename, &st))) {
4282 ERROR("Failed to stat file! (%s)\n", filename);
4283 return -1;
4284 }
4285
4286 if (len != 2064) {
4287 ERROR("Invalid gamma table!\n");
4288 return -1;
4289 }
4290
4291 buf = malloc(len);
4292 if (!buf) {
4293 ERROR("Memory allocation failure!\n");
4294 return -1;
4295 }
4296
4297 /* Read in file */
4298 if ((ret = dyesub_read_file(filename, buf, st.st_size, NULL))) {
4299 ERROR("Failed to read '%s' (%d)\n", filename, ret);
4300 free(buf);
4301 return -1;
4302 }
4303
4304 dnpds40_build_cmd(&cmd, "TBL_WT", "CTRLD_CWE_RESET", 0);
4305 if ((ret = dnpds40_do_cmd(ctx, &cmd, NULL, 0))) {
4306 ERROR("Gamma table reset failed!\n");
4307 free(buf);
4308 return -1;
4309 }
4310
4311 dnpds40_build_cmd(&cmd, "TBL_WT", "CTRLD_GAMMA16", len);
4312 if ((ret = dnpds40_do_cmd(ctx, &cmd, buf, len))) {
4313 ERROR("Flash data transfer failed!\n");
4314 free(buf);
4315 return -1;
4316 }
4317 INFO("Gamma file '%s' written\n", filename);
4318
4319 free(buf);
4320 return CUPS_BACKEND_OK;
4321 }
4322
4323 static void dnpds40_cmdline(void)
4324 {
4325 DEBUG("\t\t[ -i ] # Query printer info\n");
4326 DEBUG("\t\t[ -I ] # Query sensor info\n");
4327 DEBUG("\t\t[ -k num ] # Set standby time (1-99 minutes, 0 disables)\n");
4328 DEBUG("\t\t[ -K num ] # Keep Media Status Across Power Cycles (1 on, 0 off)\n");
4329 DEBUG("\t\t[ -n ] # Query counters\n");
4330 DEBUG("\t\t[ -N A|B|M ] # Clear counter A/B/M\n");
4331 DEBUG("\t\t[ -p num ] # Set counter P\n");
4332 DEBUG("\t\t[ -R ] # Reset printer\n");
4333 DEBUG("\t\t[ -s ] # Query status\n");
4334 DEBUG("\t\t[ -x num ] # Set USB iSerialNumber Reporting (1 on, 0 off)\n");
4335 DEBUG("\t\t[ -X ] # Cancel current print job\n");
4336 #if 0
4337 DEBUG("\t\t[ -S serno ] # Overwrite serial number (DANGEROUS!!!)\n");
4338 DEBUG("\t\t[ -E ] # Erase CWD data (DANGEROUS!!!)\n");
4339 DEBUG("\t\t[ -C filename ] # Write CWD data (DANGEROUS!!!)\n");
4340 DEBUG("\t\t[ -g filename ] # Write Gamma Table (DANGEROUS!!!)\n");
4341 DEBUG("\t\t[ -F filename ] # Write Flash (*VERY* DANGEROUS!!!)\n");
4342 DEBUG("\t\t[ -H filename ] # Write Duplexer Flash (*VERY* DANGEROUS!!!)\n");
4343 #endif
4344 }
4345
4346 static int dnpds40_cmdline_arg(void *vctx, int argc, char **argv)
4347 {
4348 struct dnpds40_ctx *ctx = vctx;
4349 int i, j = 0;
4350
4351 if (!ctx)
4352 return -1;
4353
4354 while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "iIk:K:nN:p:Rsx:XEC:F:g:H:S:")) >= 0) {
4355 switch(i) {
4356 GETOPT_PROCESS_GLOBAL
4357 case 'i':
4358 j = dnpds40_get_info(ctx);
4359 break;
4360 case 'I':
4361 j = dnpds40_get_sensors(ctx);
4362 break;
4363 case 'k': {
4364 int sleeptime = atoi(optarg);
4365 if (!ctx->supports_standby) {
4366 ERROR("Printer does not support standby\n");
4367 j = -1;
4368 break;
4369 }
4370 if (sleeptime < 0 || sleeptime > 99) {
4371 ERROR("Value out of range (0-99)\n");
4372 j = -1;
4373 break;
4374 }
4375 j = dnpds620_standby_mode(ctx, sleeptime);
4376 break;
4377 }
4378 case 'K': {
4379 int keep = atoi(optarg);
4380 if (!ctx->supports_standby) {
4381 ERROR("Printer does not support media keep mode\n");
4382 j = -1;
4383 break;
4384 }
4385 if (keep < 0 || keep > 1) {
4386 ERROR("Value out of range (0-1)\n");
4387 j = -1;
4388 break;
4389 }
4390 j = dnpds620_media_keep_mode(ctx, keep);
4391 break;
4392 }
4393 case 'n':
4394 j = dnpds40_get_counters(ctx);
4395 break;
4396 case 'N':
4397 if (optarg[0] != 'A' &&
4398 optarg[0] != 'B' &&
4399 optarg[0] != 'M')
4400 return CUPS_BACKEND_FAILED;
4401 if (optarg[0] == 'M' && !ctx->supports_matte) {
4402 ERROR("Printer FW does not support matte functions, please update!\n");
4403 return CUPS_BACKEND_FAILED;
4404 }
4405 j = dnpds40_clear_counter(ctx, optarg[0]);
4406 break;
4407 case 'p':
4408 if (!ctx->supports_counterp) {
4409 ERROR("Printer FW dows not support P counter!\n");
4410 return CUPS_BACKEND_FAILED;
4411 }
4412 j = dnpds40_set_counter_p(ctx, optarg);
4413 break;
4414 case 'R': {
4415 j = dnpds40_reset_printer(ctx);
4416 break;
4417 }
4418 case 's': {
4419 j = dnpds40_get_status(ctx);
4420 break;
4421 }
4422 case 'x': {
4423 int enable = atoi(optarg);
4424 if (ctx->supports_iserial != 1) {
4425 ERROR("Printer does not support configuring iSerialNumber reporting!\n");
4426 j = -1;
4427 break;
4428 }
4429 if (enable < 0 || enable > 1) {
4430 ERROR("Value out of range (0-1)\n");
4431 j = -1;
4432 break;
4433 }
4434 j = dnpds620_iserial_mode(ctx, enable);
4435 break;
4436 }
4437 case 'X': {
4438 j = dnpds40_cancel_job(ctx);
4439 break;
4440 }
4441 case 'S': {
4442 j = dnpds40_write_serial(ctx, optarg);
4443 break;
4444 }
4445 case 'E': {
4446 j = dnpds40_erase_cwd(ctx);
4447 break;
4448 }
4449 case 'C': {
4450 j = dnpds40_write_cwd(ctx, optarg);
4451 break;
4452 }
4453 case 'F': {
4454 j = dnpds40_write_flash(ctx, optarg);
4455 break;
4456 }
4457 case 'g': {
4458 j = dnpds40_write_gamma(ctx, optarg);
4459 break;
4460 }
4461 case 'H': {
4462 j = dnpds40_write_duplexflash(ctx, optarg);
4463 break;
4464 }
4465 default:
4466 break; /* Ignore completely */
4467 }
4468
4469 if (j) return j;
4470 }
4471
4472 return CUPS_BACKEND_OK;
4473 }
4474
4475 static int dnpds40_query_markers(void *vctx, struct marker **markers, int *count)
4476 {
4477 struct dnpds40_ctx *ctx = vctx;
4478
4479 if (markers)
4480 *markers = ctx->marker;
4481 if (count)
4482 *count = ctx->marker_count;
4483
4484 ctx->marker[0].levelnow = dnpds40_query_mqty(ctx);
4485
4486 if (ctx->marker[0].levelnow < 0)
4487 return CUPS_BACKEND_FAILED;
4488
4489 if (ctx->conn->type == P_DNP_DS80D) {
4490 if (dnpds80dx_query_paper(ctx))
4491 return CUPS_BACKEND_FAILED;
4492 switch (ctx->duplex_media_status) {
4493 case DUPLEX_UNIT_PAPER_NONE:
4494 ctx->marker[1].levelnow = 0;
4495 break;
4496 case DUPLEX_UNIT_PAPER_PROTECTIVE:
4497 ctx->marker[1].levelnow = CUPS_MARKER_UNAVAILABLE;
4498 break;
4499 case DUPLEX_UNIT_PAPER_PRESENT:
4500 ctx->marker[1].levelnow = CUPS_MARKER_UNKNOWN_OK;
4501 break;
4502 }
4503 }
4504
4505 return CUPS_BACKEND_OK;
4506 }
4507
4508 static int dnp_query_stats(void *vctx, struct printerstats *stats)
4509 {
4510 struct dnpds40_cmd cmd;
4511 struct dnpds40_ctx *ctx = vctx;
4512 uint8_t *resp;
4513 int len = 0;
4514
4515 /* Update marker info */
4516 if (dnpds40_query_markers(ctx, NULL, NULL))
4517 return CUPS_BACKEND_FAILED;
4518
4519 switch (ctx->mfg)
4520 {
4521 case MFG_DNP: stats->mfg = "Dai Nippon Printing"; break;
4522 case MFG_CITIZEN: stats->mfg = "Citizen Systems"; break;
4523 case MFG_MITSUBISHI: stats->mfg = "Mitsubishi" ; break;
4524 case MFG_FUJIFILM: stats->mfg = "Fujifilm" ; break;
4525 default: stats->mfg = "Unknown" ; break;
4526 }
4527
4528 stats->model = dnpds40_printer_type(ctx->conn->type, ctx->mfg);
4529 stats->serial = ctx->serno;
4530 stats->fwver = ctx->version; // XXX duplexer version?
4531
4532 stats->decks = ctx->conn->type == P_DNP_DS80D ? 2: 1;
4533 stats->mediatype[0] = ctx->marker[0].name;
4534 stats->levelmax[0] = ctx->marker[0].levelmax;
4535 stats->levelnow[0] = ctx->marker[0].levelnow;
4536 stats->name[0] = "Roll";
4537
4538 /* Query status */
4539 stats->status[0] = strdup(dnpds40_statuses(dnpds40_query_status(ctx)));
4540
4541 /* Query lifetime counter */
4542 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_LIFE", 0);
4543
4544 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
4545 if (!resp)
4546 return CUPS_BACKEND_FAILED;
4547
4548 dnpds40_cleanup_string((char*)resp, len);
4549
4550 stats->cnt_life[0] = atoi((char*)resp+2);
4551 free(resp);
4552
4553 if (ctx->conn->type == P_DNP_DS80D) {
4554 stats->name[0] = "Sheet";
4555 stats->mediatype[1] = ctx->marker[1].name;
4556 stats->levelmax[1] = ctx->marker[1].levelmax;
4557 stats->levelnow[1] = ctx->marker[1].levelnow;
4558 stats->status[1] = strdup(dnpds80_duplex_paper_status(ctx->duplex_media_status));
4559
4560 dnpds40_build_cmd(&cmd, "MNT_RD", "COUNTER_DUPLEX", 0);
4561
4562 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
4563 if (!resp)
4564 return CUPS_BACKEND_FAILED;
4565
4566 dnpds40_cleanup_string((char*)resp, len);
4567
4568 stats->cnt_life[1] = atoi((char*)resp);
4569
4570 free(resp);
4571 }
4572
4573 return CUPS_BACKEND_OK;
4574 }
4575
4576 static int dnp_job_polarity(void *vctx)
4577 {
4578 struct dnpds40_ctx *ctx = vctx;
4579 struct dnpds40_cmd cmd;
4580 uint8_t *resp;
4581 int count;
4582 int len = 0;
4583
4584 if (test_mode >= TEST_MODE_NOATTACH)
4585 return 0;
4586
4587 if (!ctx->supports_rewind)
4588 return 0;
4589
4590 /* Get Media remaining */
4591 dnpds40_build_cmd(&cmd, "INFO", "RQTY", 0);
4592
4593 resp = dnpds40_resp_cmd(ctx, &cmd, &len);
4594 if (!resp)
4595 return 0;
4596
4597 dnpds40_cleanup_string((char*)resp, len);
4598 count = atoi((char*)resp+4);
4599 free(resp);
4600
4601 return (count & 1);
4602 }
4603
4604 static const char *dnpds40_prefixes[] = {
4605 "dnp_citizen", "dnpds40", // Family names, do *not* nuke.
4606 // backwards compatibility
4607 "dnpds80", "dnpds80dx", "dnpds620", "dnpds820", "dnprx1",
4608 "citizencw01", "citizencw02", "citizencx02",
4609 NULL
4610 };
4611
4612 static const struct device_id dnpcitizen_devices[] = {
4613 { 0x1343, 0x0003, P_DNP_DS40, NULL, "dnp-ds40"},
4614 { 0x1343, 0x0003, P_DNP_DS40, NULL, "citizen-cx"}, /* Duplicate */
4615 { 0x1343, 0x0004, P_DNP_DS80, NULL, "dnp-ds80"},
4616 { 0x1343, 0x0004, P_DNP_DS80, NULL, "citizen-cx-w"}, /* Duplicate */
4617 { 0x1343, 0x0004, P_DNP_DS80, NULL, "mitsubishi-cp3800dw"}, /* Duplicate */
4618 { 0x1343, 0x0008, P_DNP_DS80D, NULL, "dnp-ds80dx"},
4619 { 0x1343, 0x0005, P_DNP_DSRX1, NULL, "dnp-dsrx1"},
4620 { 0x1343, 0x0005, P_DNP_DSRX1, NULL, "citizen-cy"}, /* Duplicate */
4621 { 0x1343, 0x0005, P_DNP_DSRX1, NULL, "citizen-cy-02"}, /* Duplicate */
4622 { 0x1452, 0x8b01, P_DNP_DS620, NULL, "dnp-ds620"},
4623 { 0x1452, 0x9001, P_DNP_DS820, NULL, "dnp-ds820"},
4624 { 0x1452, 0x9201, P_DNP_QW410, NULL, "dnp-qw410"},
4625 { 0x1343, 0x0002, P_CITIZEN_CW01, NULL, "citizen-cw-01"},
4626 { 0x1343, 0x0002, P_CITIZEN_CW01, NULL, "citizen-op900"}, /* Duplicate */
4627 { 0x1343, 0x0006, P_CITIZEN_OP900II, NULL, "citizen-cw-02"},
4628 { 0x1343, 0x0006, P_CITIZEN_OP900II, NULL, "citizen-op900ii"}, /* Duplicate */
4629 { 0x1343, 0x000a, P_DNP_DS620, NULL, "citizen-cx-02"},
4630 // { 0x1343, 0xXXXX, P_DNP_DS620, NULL, "citizen-cx-02s"},
4631 { 0x1343, 0x000b, P_DNP_DS820, NULL, "citizen-cx-02w"},
4632 { 0x1343, 0x000c, P_DNP_QW410, NULL, "citizen-cz-01"},
4633 // { 0x04cb, 0xXXXX, P_DNP_DS620, NULL, "fujifilm-ask-400"},
4634 { 0, 0, 0, NULL, NULL}
4635 };
4636
4637 const struct dyesub_backend dnpds40_backend = {
4638 .name = "DNP DS-series / Citizen C-series",
4639 .version = "0.158",
4640 .uri_prefixes = dnpds40_prefixes,
4641 .devices = dnpcitizen_devices,
4642 .cmdline_usage = dnpds40_cmdline,
4643 .cmdline_arg = dnpds40_cmdline_arg,
4644 .init = dnpds40_init,
4645 .attach = dnpds40_attach,
4646 .teardown = dnpds40_teardown,
4647 .read_parse = dnpds40_read_parse,
4648 .cleanup_job = dnpds40_cleanup_job,
4649 .main_loop = dnpds40_main_loop,
4650 .query_serno = dnpds40_query_serno,
4651 .query_markers = dnpds40_query_markers,
4652 .query_stats = dnp_query_stats,
4653 .combine_jobs = dnp_combine_jobs,
4654 .job_polarity = dnp_job_polarity,
4655 };
4656
4657 /* Windows spool file support */
4658 static int legacy_spool_helper(struct dnpds40_printjob *job, int data_fd,
4659 int read_data, int hdrlen, uint32_t plane_len,
4660 int parse_dpi)
4661 {
4662 uint8_t *buf;
4663 uint8_t bmp_hdr[14];
4664 int i, remain;
4665 uint32_t j;
4666
4667 /* Allocate a temp processing buffer */
4668 remain = plane_len * 3;
4669 buf = malloc(remain);
4670 if (!buf) {
4671 ERROR("Memory allocation failure!\n");
4672 return CUPS_BACKEND_RETRY_CURRENT;
4673 }
4674
4675 /* Copy over the post-jobhdr crap into our processing buffer */
4676 j = read_data - hdrlen;
4677 memcpy(buf, job->databuf + hdrlen, j);
4678 remain -= j;
4679
4680 /* Read in the remaining spool data */
4681 while (remain) {
4682 i = read(data_fd, buf + j, remain);
4683
4684 if (i < 0) {
4685 free(buf);
4686 return i;
4687 }
4688
4689 remain -= i;
4690 j += i;
4691 }
4692
4693 if (parse_dpi) {
4694 /* Parse out Y DPI */
4695 memcpy(&j, buf + 28, sizeof(j));
4696 j = le32_to_cpu(j);
4697 switch(j) {
4698 case 11808:
4699 job->dpi = 300;
4700 break;
4701 case 23615:
4702 case 23616:
4703 job->dpi = 600;
4704 break;
4705 default:
4706 ERROR("Unrecognized printjob resolution (%u ppm)\n", j);
4707 free(buf);
4708 return CUPS_BACKEND_CANCEL;
4709 }
4710 }
4711
4712 /* Generate bitmap file header (same for all planes) */
4713 j = cpu_to_le32(plane_len + 24);
4714 memset(bmp_hdr, 0, sizeof(bmp_hdr));
4715 bmp_hdr[0] = 0x42;
4716 bmp_hdr[1] = 0x4d;
4717 memcpy(bmp_hdr + 2, &j, sizeof(j));
4718 bmp_hdr[10] = 0x40;
4719 bmp_hdr[11] = 0x04;
4720
4721 /* Set up planes */
4722 j = 0;
4723
4724 /* Y plane */
4725 job->datalen += sprintf((char*)job->databuf + job->datalen,
4726 "\033PIMAGE YPLANE %08u", plane_len + 24);
4727 memcpy(job->databuf + job->datalen, bmp_hdr, sizeof(bmp_hdr));
4728 job->datalen += sizeof(bmp_hdr);
4729 memcpy(job->databuf + job->datalen, buf + j, plane_len);
4730 job->datalen += plane_len;
4731 j += plane_len;
4732 memset(job->databuf + job->datalen, 0, 10);
4733 job->datalen += 10;
4734
4735 /* M plane */
4736 job->datalen += sprintf((char*)job->databuf + job->datalen,
4737 "\033PIMAGE MPLANE %08u", plane_len + 24);
4738 memcpy(job->databuf + job->datalen, bmp_hdr, sizeof(bmp_hdr));
4739 job->datalen += sizeof(bmp_hdr);
4740 memcpy(job->databuf + job->datalen, buf + j, plane_len);
4741 job->datalen += plane_len;
4742 j += plane_len;
4743 memset(job->databuf + job->datalen, 0, 10);
4744 job->datalen += 10;
4745
4746 /* C plane */
4747 job->datalen += sprintf((char*)job->databuf + job->datalen,
4748 "\033PIMAGE CPLANE %08u", plane_len + 24);
4749 memcpy(job->databuf + job->datalen, bmp_hdr, sizeof(bmp_hdr));
4750 job->datalen += sizeof(bmp_hdr);
4751 memcpy(job->databuf + job->datalen, buf + j, plane_len);
4752 job->datalen += plane_len;
4753 j += plane_len;
4754 memset(job->databuf + job->datalen, 0, 10);
4755 job->datalen += 10;
4756
4757 /* Start */
4758 job->datalen += sprintf((char*)job->databuf + job->datalen,
4759 "\033PCNTRL START ");
4760
4761 /* We're done */
4762 free(buf);
4763
4764 return CUPS_BACKEND_OK;
4765 }
4766
4767 struct cw01_spool_hdr {
4768 uint8_t type; /* TYPE_??? */
4769 uint8_t res; /* DPI_??? */
4770 uint8_t copies; /* number of prints */
4771 uint8_t null0;
4772 uint32_t plane_len; /* LE */
4773 uint8_t null1[4];
4774 } __attribute__((packed));
4775
4776 #define DPI_334 0
4777 #define DPI_600 1
4778
4779 #define TYPE_DSC 0
4780 #define TYPE_L 1
4781 #define TYPE_PC 2
4782 #define TYPE_2DSC 3
4783 #define TYPE_3L 4
4784 #define TYPE_A5 5
4785 #define TYPE_A6 6
4786
4787 static int legacy_cw01_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data)
4788 {
4789 struct cw01_spool_hdr hdr;
4790 uint32_t plane_len;
4791
4792 /* get original header out of structure */
4793 memcpy(&hdr, job->databuf + job->datalen, sizeof(hdr));
4794
4795 /* Early parsing and sanity checking */
4796 plane_len = le32_to_cpu(hdr.plane_len);
4797
4798 if (hdr.type > TYPE_A6 ||
4799 hdr.res > DPI_600 ||
4800 hdr.null1[0] || hdr.null1[1] || hdr.null1[2] || hdr.null1[3]) {
4801 ERROR("Unrecognized header data format @%d!\n", job->datalen);
4802 return CUPS_BACKEND_CANCEL;
4803 }
4804 job->dpi = (hdr.res == DPI_600) ? 600 : 334;
4805 job->cutter = 0;
4806
4807 /* Use job's copies */
4808 job->common.copies = hdr.copies;
4809
4810 return legacy_spool_helper(job, data_fd, read_data,
4811 sizeof(hdr), plane_len, 0);
4812 }
4813
4814 struct rx1_spool_hdr {
4815 uint8_t type; /* equals MULTICUT_?? - 1 */
4816 uint8_t null0;
4817 uint8_t copies; /* number of copies, always fixed at 01.. */
4818 uint8_t null1;
4819 uint32_t plane_len; /* LE */
4820 uint8_t flags; /* combination of FLAG_?? */
4821 uint8_t null2[3];
4822 } __attribute__((packed));
4823
4824 #define FLAG_MATTE 0x02
4825 #define FLAG_NORETRY 0x08
4826 #define FLAG_2INCH 0x10
4827
4828 static int legacy_dnp_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data)
4829 {
4830 struct rx1_spool_hdr hdr;
4831 uint32_t plane_len;
4832
4833 /* get original header out of structure */
4834 memcpy(&hdr, job->databuf + job->datalen, sizeof(hdr));
4835
4836 /* Early parsing and sanity checking */
4837 plane_len = le32_to_cpu(hdr.plane_len);
4838
4839 if (hdr.type >= MULTICUT_A4x5X2 ||
4840 hdr.null2[0] || hdr.null2[1] || hdr.null2[2]) {
4841 ERROR("Unrecognized header data format @%d!\n", job->datalen);
4842 return CUPS_BACKEND_CANCEL;
4843 }
4844
4845 /* Use job's copies */
4846 job->common.copies = hdr.copies;
4847
4848 /* Don't bother with FW version checks for legacy stuff */
4849 job->multicut = hdr.type + 1;
4850 job->matte = (hdr.flags & FLAG_MATTE) ? 1 : 0;
4851 job->cutter = (hdr.flags & FLAG_2INCH) ? 120 : 0;
4852
4853 return legacy_spool_helper(job, data_fd, read_data,
4854 sizeof(hdr), plane_len, 1);
4855 }
4856
4857 struct ds620_spool_hdr {
4858 uint8_t type; /* MULTICUT_?? -1, but >0x90 is a flag for rewind */
4859 uint8_t copies; /* Always fixed at 01..*/
4860 uint8_t null0[2];
4861 uint8_t quality; /* 0x02 is HQ, 0x00 is HS. Equivalent to DPI. */
4862 uint8_t unk[3]; /* Always 00 01 00 */
4863 uint32_t plane_len; /* LE */
4864 uint8_t flags; /* FLAG_?? */
4865 uint8_t null1[3];
4866 } __attribute__((packed));
4867
4868 #define FLAG_LUSTER 0x04
4869 #define FLAG_FINEMATTE 0x06
4870
4871 static int legacy_dnp620_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data)
4872 {
4873 struct ds620_spool_hdr hdr;
4874 uint32_t plane_len;
4875
4876 /* get original header out of structure */
4877 memcpy(&hdr, job->databuf + job->datalen, sizeof(hdr));
4878
4879 /* Early parsing and sanity checking */
4880 plane_len = le32_to_cpu(hdr.plane_len);
4881
4882 /* Used to signify rewind request. Backend handles automatically. */
4883 if (hdr.type > 0x90)
4884 hdr.type -= 0x90;
4885
4886 if (hdr.type > MULTICUT_A4x5X2 ||
4887 hdr.null1[0] || hdr.null1[1] || hdr.null1[2]) {
4888 ERROR("Unrecognized header data format @%d!\n", job->datalen);
4889 return CUPS_BACKEND_CANCEL;
4890 }
4891
4892 /* Use job's copies */
4893 job->common.copies = hdr.copies;
4894
4895 /* Don't bother with FW version checks for legacy stuff */
4896 job->multicut = hdr.type + 1;
4897 if ((hdr.flags & FLAG_FINEMATTE) == FLAG_FINEMATTE)
4898 job->matte = 21;
4899 else if (hdr.flags & FLAG_LUSTER)
4900 job->matte = 22;
4901 else if (hdr.flags & FLAG_MATTE)
4902 job->matte = 1;
4903
4904 job->cutter = (hdr.flags & FLAG_2INCH) ? 120 : 0;
4905
4906 return legacy_spool_helper(job, data_fd, read_data,
4907 sizeof(hdr), plane_len, 1);
4908 }
4909
4910 #define FLAG_820_HD 0x80
4911 #define FLAG_820_RETRY 0x20
4912 #define FLAG_820_LUSTER 0x06
4913 #define FLAG_820_FMATTE 0x04
4914 #define FLAG_820_MATTE 0x02
4915
4916 static int legacy_dnp820_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data)
4917 {
4918 struct ds620_spool_hdr hdr;
4919 uint32_t plane_len;
4920
4921 /* get original header out of structure */
4922 memcpy(&hdr, job->databuf + job->datalen, sizeof(hdr));
4923
4924 /* Early parsing and sanity checking */
4925 plane_len = le32_to_cpu(hdr.plane_len);
4926
4927 /* Used to signify rewind request. Backend handles automatically. */
4928 if (hdr.type > 0x90)
4929 hdr.type -= 0x90;
4930
4931 if (hdr.type > MULTICUT_A4x5X2 ||
4932 hdr.null1[0] || hdr.null1[1] || hdr.null1[2]) {
4933 ERROR("Unrecognized header data format @%d!\n", job->datalen);
4934 return CUPS_BACKEND_CANCEL;
4935 }
4936
4937 /* Use job's copies */
4938 job->common.copies = hdr.copies;
4939
4940 /* Don't bother with FW version checks for legacy stuff */
4941 job->multicut = hdr.type + 1;
4942 if ((hdr.flags & FLAG_820_FMATTE) == FLAG_820_FMATTE)
4943 job->matte = 21;
4944 else if ((hdr.flags & FLAG_820_LUSTER) == FLAG_820_LUSTER)
4945 job->matte = 22;
4946 else if ((hdr.flags & FLAG_820_MATTE) == FLAG_820_MATTE)
4947 job->matte = 1;
4948
4949 if (hdr.flags & FLAG_820_HD)
4950 job->printspeed = 3;
4951
4952 return legacy_spool_helper(job, data_fd, read_data,
4953 sizeof(hdr), plane_len, 1);
4954 }
4955
4956 struct qw410_spool_hdr {
4957 uint8_t type; /* MULTICUT_?? -1 */
4958 uint8_t null0[5];
4959 uint16_t copies; /* LE, 01 or bigger */
4960
4961 uint32_t plane_len; /* LE */
4962 uint8_t matte;
4963 uint8_t null1[3];
4964
4965 uint8_t unk; /* always 01 */
4966 uint8_t null2[3];
4967 uint8_t cut2;
4968 uint8_t null3[3];
4969
4970 uint8_t hd;
4971 uint8_t null4[3];
4972 uint8_t dc;
4973 uint8_t null5[3];
4974 } __attribute__((packed));
4975
4976 static int legacy_qw410_read_parse(struct dnpds40_printjob *job, int data_fd, int read_data)
4977 {
4978 struct qw410_spool_hdr hdr;
4979 uint32_t plane_len;
4980
4981 /* get original header out of structure */
4982 memcpy(&hdr, job->databuf + job->datalen, sizeof(hdr));
4983
4984 /* Early parsing and sanity checking */
4985 plane_len = le32_to_cpu(hdr.plane_len);
4986
4987 if (hdr.type < MULTICUT_4x4 || hdr.type > MULTICUT_4_5x4 ||
4988 hdr.null0[0] || hdr.null0[1] || hdr.null0[2]) {
4989 ERROR("Unrecognized header data format @%d!\n", job->datalen);
4990 return CUPS_BACKEND_CANCEL;
4991 }
4992
4993 /* Don't bother with FW version checks for legacy stuff */
4994 job->multicut = hdr.type + 1;
4995 job->matte = hdr.matte;
4996 job->cutter = (hdr.cut2) ? 120 : 0;
4997 job->printspeed = (hdr.hd) ? 3 : 0;
4998 // XXX decurl = hdr.dc (Decurl: 2 = auto, 1 = on, 0 = off)
4999
5000 return legacy_spool_helper(job, data_fd, read_data,
5001 sizeof(hdr), plane_len, 1);
5002 }
5003
5004 /*
5005
5006 Basic spool file format for CW01
5007
5008 TT RR NN 00 XX XX XX XX 00 00 00 00 <- FILE header.
5009
5010 NN : copies (0x01 or more)
5011 RR : resolution; 0 == 334 dpi, 1 == 600dpi
5012 TT : type 0x02 == 4x6, 0x01 == 5x3.5, 0x06 = 6x9
5013 XX XX XX XX : plane length (LE)
5014 plane length * 3 + 12 == file length.
5015
5016 Followed by three planes, each with this 40b header:
5017
5018 28 00 00 00 00 08 00 00 RR RR 00 00 01 00 08 00
5019 00 00 00 00 00 00 00 00 5a 33 00 00 YY YY 00 00
5020 00 01 00 00 00 00 00 00
5021
5022 RR RR : rows in LE format
5023 YY YY : 0x335a (334dpi) or 0x5c40 (600dpi)
5024
5025 Followed by 1024 bytes of color tables:
5026
5027 ff ff ff 00 ... 00 00 00 00
5028
5029 1024+40 = 1064 bytes of header per plane.
5030
5031 Always have 2048 columns of data.
5032
5033 followed by (2048 * rows) bytes of data.
5034
5035 */