Debian Codegraph @

src/cups/backend_mitsu70x.c

raw symbols SCIP
1 /*
2 * Mitsubishi CP-D70/D707 Photo Printer CUPS backend
3 *
4 * (c) 2013-2025 Solomon Peachy <[email protected]>
5 *
6 * The latest version of this program can be found at:
7 *
8 * https://git.shaftnet.org/gitea/slp/selphy_print.git
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 *
23 * SPDX-License-Identifier: GPL-2.0+
24 *
25 */
26
27 #define BACKEND mitsu70x_backend
28
29 #include "backend_common.h"
30 #include "backend_mitsu.h"
31
32 /* Width of the laminate data file */
33 #define LAMINATE_STRIDE 1864
34
35 /* Max size of data chunk sent over */
36 #define CHUNK_LEN (256*1024)
37
38 /* Private data structure */
39 struct mitsu70x_printjob {
40 struct dyesub_job_common common;
41
42 uint8_t *databuf;
43 uint32_t datalen;
44
45 uint8_t *spoolbuf;
46 int spoolbuflen;
47
48 uint16_t rows;
49 uint16_t cols;
50 uint32_t planelen;
51 uint32_t matte;
52 int raw_format;
53
54 int decks_exact[2]; /* Media is exact match */
55 int decks_ok[2]; /* Media can be used */
56
57 /* These are used only for the image processing */
58 int sharpen; /* ie mhdr.sharpen - 1 */
59 int reverse;
60
61 const char *laminatefname;
62 const char *lutfname;
63 const char *cpcfname;
64 const char *ecpcfname;
65 };
66
67 struct mitsu70x_ctx {
68 struct dyesub_connection *conn;
69
70 int is_s;
71
72 uint16_t jobid;
73
74 struct marker marker[2];
75 uint8_t medias[2];
76 uint8_t media_subtypes[2];
77
78 uint16_t last_l;
79 uint16_t last_u;
80 int num_decks;
81
82 char serno[7]; /* 6+null */
83 char fwver[7]; /* 6+null */
84
85 struct mitsu_lib lib;
86
87 const char *last_cpcfname;
88 const char *last_ecpcfname;
89
90 struct BandImage output;
91 };
92
93 /* Printer data structures */
94 struct mitsu70x_jobstatus {
95 uint8_t hdr[4]; /* E4 56 31 30 */
96 uint16_t jobid; /* BE */
97 uint16_t mecha_no; /* BE */
98 uint8_t job_status[4];
99 uint8_t memory;
100 uint8_t power;
101 uint8_t mecha_status[2];
102 uint8_t temperature;
103 uint8_t error_status[3];
104 uint8_t mecha_status_up[2];
105 uint8_t temperature_up;
106 uint8_t error_status_up[3];
107 } __attribute__((packed));
108
109 struct mitsu70x_job {
110 uint16_t id; /* BE */
111 uint8_t status[4];
112 } __attribute__((packed));
113
114 #define NUM_JOBS 170
115
116 struct mitsu70x_jobs {
117 uint8_t hdr[4]; /* E4 56 31 31 */
118 struct mitsu70x_job jobs[NUM_JOBS];
119 } __attribute__((packed));
120
121 #define MECHA_STATUS_INIT 0x80
122 #define MECHA_STATUS_FEED 0x50
123 #define MECHA_STATUS_LOAD 0x40
124 #define MECHA_STATUS_LOAD2 0x30
125 #define MECHA_STATUS_PRINT 0x20
126 #define MECHA_STATUS_IDLE 0x00
127
128 #define JOB_STATUS0_NONE 0x00
129 #define JOB_STATUS0_DATA 0x10
130 #define JOB_STATUS0_QUEUE 0x20
131 #define JOB_STATUS0_PRINT 0x50
132 #define JOB_STATUS0_ASSIGN 0x70 // XXX undefined.
133 #define JOB_STATUS0_END 0x80
134
135 #define JOB_STATUS1_PRINT_MEDIALOAD 0x10
136 #define JOB_STATUS1_PRINT_PRE_Y 0x20
137 #define JOB_STATUS1_PRINT_Y 0x30
138 #define JOB_STATUS1_PRINT_PRE_M 0x40
139 #define JOB_STATUS1_PRINT_M 0x50
140 #define JOB_STATUS1_PRINT_PRE_C 0x60
141 #define JOB_STATUS1_PRINT_C 0x70
142 #define JOB_STATUS1_PRINT_PRE_OC 0x80
143 #define JOB_STATUS1_PRINT_OC 0x90
144 #define JOB_STATUS1_PRINT_EJECT 0xA0
145
146 #define JOB_STATUS1_END_OK 0x00
147 #define JOB_STATUS1_END_MECHA 0x10 // 0x10...0x7f
148 #define JOB_STATUS1_END_HEADER 0x80
149 #define JOB_STATUS1_END_PRINT 0x90
150 #define JOB_STATUS1_END_INTERRUPT 0xA0
151
152 #define JOB_STATUS2_END_HEADER_ERROR 0x00
153 #define JOB_STATUS2_END_HEADER_MEMORY 0x10
154 #define JOB_STATUS2_END_PRINT_MEDIA 0x00
155 #define JOB_STATUS2_END_PRINT_PREVERR 0x10
156 #define JOB_STATUS2_END_INT_TIMEOUT 0x00
157 #define JOB_STATUS2_END_INT_CANCEL 0x10
158 #define JOB_STATUS2_END_INT_DISCON 0x20
159
160 /* Error codes */
161 #define ERROR_STATUS0_NOSTRIPBIN 0x01
162 #define ERROR_STATUS0_NORIBBON 0x02
163 #define ERROR_STATUS0_NOPAPER 0x03
164 #define ERROR_STATUS0_MEDIAMISMATCH 0x04
165 #define ERROR_STATUS0_RIBBONCNTEND 0x05
166 #define ERROR_STATUS0_BADRIBBON 0x06
167 #define ERROR_STATUS0_BADJOBPARAM 0x07
168 #define ERROR_STATUS0_PAPEREND 0x08
169 #define ERROR_STATUS0_RIBBONEND 0x09
170 #define ERROR_STATUS0_DOOROPEN_IDLE 0x0A
171 #define ERROR_STATUS0_DOOROPEN_PRNT 0x0B
172 #define ERROR_STATUS0_POWEROFF 0x0C // Powered off during printing..?
173 #define ERROR_STATUS0_NOMCOP 0x0D
174 #define ERROR_STATUS0_RIBBONSKIP1 0x0E
175 #define ERROR_STATUS0_RIBBONSKIP2 0x0F
176 #define ERROR_STATUS0_RIBBONJAM 0x10
177 #define ERROR_STATUS0_RIBBON_OTHER 0x11 // 0x11->0x1F
178 #define ERROR_STATUS0_PAPER_JAM 0x20 // 0x20->0x2F
179 #define ERROR_STATUS0_MECHANICAL 0x30 // 0x30->0x39
180 #define ERROR_STATUS0_RFID 0x3A
181 #define ERROR_STATUS0_FLASH 0x3B
182 #define ERROR_STATUS0_EEPROM 0x3C
183 #define ERROR_STATUS0_PREHEAT 0x3D
184 #define ERROR_STATUS0_MDASTATE 0x3E
185 #define ERROR_STATUS0_PSUFANLOCKED 0x3F
186 #define ERROR_STATUS0_OTHERS 0x40 // 0x40..?
187
188 /* Error classifications */
189 #define ERROR_STATUS1_PAPER 0x01
190 #define ERROR_STATUS1_RIBBON 0x02
191 #define ERROR_STATUS1_SETTING 0x03
192 #define ERROR_STATUS1_OPEN 0x05
193 #define ERROR_STATUS1_NOSTRIPBIN 0x06
194 #define ERROR_STATUS1_PAPERJAM 0x07
195 #define ERROR_STATUS1_RIBBONSYS 0x08
196 #define ERROR_STATUS1_MECHANICAL 0x09
197 #define ERROR_STATUS1_ELECTRICAL 0x0A
198 #define ERROR_STATUS1_FIRMWARE 0x0E
199 #define ERROR_STATUS1_OTHER 0x0F
200
201 /* Error recovery conditions */
202 #define ERROR_STATUS2_AUTO 0x00
203 #define ERROR_STATUS2_RELOAD_PAPER 0x01
204 #define ERROR_STATUS2_RELOAD_RIBBON 0x02
205 #define ERROR_STATUS2_CHANGE_BOTH 0x03
206 #define ERROR_STATUS2_CHANGE_ONE 0x04
207 #define ERROR_STATUS2_CLOSEUNIT 0x05
208 #define ERROR_STATUS2_ATTACHSTRIPBIN 0x06
209 #define ERROR_STATUS2_CLEARJAM 0x07
210 #define ERROR_STATUS2_CHECKRIBBON 0x08
211 #define ERROR_STATUS2_OPENCLOSEUNIT 0x0A
212 #define ERROR_STATUS2_POWEROFF 0x0F
213
214 struct mitsu70x_status_deck {
215 uint8_t mecha_status[2];
216 uint8_t temperature; /* D70/D80 family only, K60 no? */
217 uint8_t error_status[3];
218 uint8_t rsvd_a[3]; /* K60 [1] == temperature? */
219 uint8_t lifetime_prints[4];
220 uint8_t rsvd_b[3]; /* K60 [3] == ?? */
221 uint8_t media_brand;
222 uint8_t media_type;
223 uint8_t media_subtype; /* K60 only? */
224 uint8_t rsvd_c[1];
225 int16_t capacity; /* media capacity */
226 int16_t remain; /* media remaining */
227 uint8_t rsvd_d[2];
228 uint8_t unknown_ctr[4]; /* lifetime + 10 (EK305), lifetime+41 (D80), in BCD! */
229 uint8_t rsvd_e[2]; // Unknown
230 uint16_t rsvd_f[16]; /* all 80 00 */
231 } __attribute__((packed));
232
233 struct mitsu70x_status_ver {
234 char ver[6];
235 uint16_t checksum; /* Presumably BE */
236 } __attribute__((packed));
237
238 struct mitsu70x_printerstatus_resp {
239 uint8_t hdr[4]; /* E4 56 32 30 */
240 uint8_t memory;
241 uint8_t power;
242 uint8_t unk[20];
243 uint8_t sleeptime; /* In minutes, 0-60 */
244 uint8_t iserial; /* 0x00 for Enabled, 0x80 for Disabled */
245 uint8_t unk_b[5]; // [4] == 0x44 on D70x, 0x13 on D80, 0x02 on EK305.
246 uint8_t dual_deck; /* 0x80 for dual-deck D707, 0x00 otherwise */
247 uint8_t unk_c[2]; // always 00 00 ??
248 uint8_t subtype; /* 0x5f on D70x/K60/D80, 0x5e on D70xS/K60-S, 0x01 on EK305 */
249 uint8_t unk_d[3]; // [1:2] == 0x??bd on D70x, 0x0487 on EK305, 0x04d7 on D80
250 int16_t model[6]; /* LE, UTF-16 */
251 int16_t serno[6]; /* LE, UTF-16 */
252 struct mitsu70x_status_ver vers[7]; // components are 'MLRTF'
253 uint8_t null[2];
254 uint8_t user_serno[6]; /* XXX Supposedly. Don't know how to set it! */
255 struct mitsu70x_status_deck lower;
256 struct mitsu70x_status_deck upper;
257 } __attribute__((packed));
258
259 struct mitsu70x_memorystatus_resp {
260 uint8_t hdr[3]; /* E4 56 33 */
261 uint8_t memory;
262 uint8_t size;
263 uint8_t rsvd;
264 } __attribute__((packed));
265
266 struct mitsu70x_calinfo_resp { /* Interpretations valid for ASK300 */
267 uint8_t hdr[6]; /* e4 6a 36 34 31 00 */
268
269 /* Note! All values below are ASCII hex! ie 0x23 -> 0x32 0x33 */
270
271 uint8_t adj_horiz[2]; /* +- 128, units of 0.085 mm */
272 uint8_t adj_vertA[2]; /* +- 128 */
273 uint8_t adj_vertB[2]; /* values are in units of 0.085 mm */
274 uint8_t adj_vertC[2]; /* A is -1->9, B is -4->6, C is -1->9 */
275 uint8_t adj_fine[4]; /* 00DC */
276 uint8_t adj_m3[2]; /* -100 -> 100 (converted to hex) */
277 uint8_t unk_c[28];
278 // 30 30 30 30 46 46 36 34 35 35 30 30
279 // 46 46 36 34 35 35 30 30 44 43 30 30 30 30 30 30
280
281 uint8_t adj_density[4]; /* 6800 -> 9000, def 8000 */
282 uint8_t adj_24v[4]; /* 0000 -> 00FF */
283 } __attribute__((packed));
284
285 /*
286 NOTES: Other stuff seen:
287
288 1b 45 48 [30 31 32] <-- No resp [30 any deck, 31 is lower, 32 is upper?]
289 1b 45 4a [30 31 32] <-- No resp [30 any deck, 31 is lower, 32 is upper?]
290 1b 45 53 00 10 [ ...? ] XX XX . "set printer number"..
291 1b 45 53 90 00 0a [ ... 9 bytes of something ] 10
292 1b 52 XX 00 <-- XX = something + 0x51
293 1b 54 00 [00 31 32] <-- No resp [00 any, 31 lower, 32 upper???]
294 1b 54 31 00 "feed and cut"
295 1b 54 53 90 00 0a 00 00 00 00 00 00 00 00 00 00
296 1b 56 34 [31 32] <-- 6 byte response, last two bytes are value.
297 1b 5a 43 00 <-- No resp
298 1b 67 18 ... (??)
299 1b 6a ... Various test commands
300 1b 6e ... (??)
301 1b 72 45 [31 32]
302 1b 72 67 00 00 00
303
304 */
305
306 struct mitsu70x_hdr {
307 uint8_t hdr[4]; /* 1b 5a 54 XX */ // XXX also, seen 1b 5a 43!
308 uint16_t jobid;
309 uint8_t rewind[2]; /* K60/EK305/D80 only */
310 uint8_t zero0[8];
311
312 uint16_t cols;
313 uint16_t rows;
314 uint16_t lamcols;
315 uint16_t lamrows;
316 uint8_t speed;
317 uint8_t zero1[7];
318
319 uint8_t deck; /* 0 = default, 1 = lower, 2 = upper -- Non-D70/D707 is always '1' */
320 uint8_t zero2[7];
321 uint8_t laminate; /* 00 == on, 01 == off */
322 uint8_t laminate_mode; /* 00 == glossy, 02 == matte */
323 uint8_t zero3[6];
324
325 uint8_t multicut;
326 uint8_t zero4[12]; /* NOTE: everything past this point is an extension */
327 uint8_t sharpen; /* 0-9. 5 is "normal", 0 is "off" */
328 uint8_t mode; /* 0 for cooked YMC planar, 1 for packed BGR */
329 uint8_t use_lut; /* in BGR mode, 0 disables, 1 enables */
330 uint8_t reversed; /* 1 tells the backend the row data is correct */
331 uint8_t copies; /* 1-255 */
332 uint8_t pad[446];
333 } __attribute__((packed));
334
335 STATIC_ASSERT(sizeof(struct mitsu70x_hdr) == 512);
336 STATIC_ASSERT(sizeof(struct mitsu70x_calinfo_resp) == 56);
337
338 static int mitsu70x_get_printerstatus(struct mitsu70x_ctx *ctx, struct mitsu70x_printerstatus_resp *resp);
339 static int mitsu70x_main_loop(void *vctx, const void *vjob, int wait_for_return);
340
341 /* Error dumps, etc */
342
343 static const char *mitsu70x_mechastatus(const uint8_t *sts)
344 {
345 switch(sts[0]) {
346 case MECHA_STATUS_INIT:
347 return "Initializing";
348 case MECHA_STATUS_FEED:
349 return "Paper Feeding/Cutting";
350 case MECHA_STATUS_LOAD:
351 case MECHA_STATUS_LOAD2:
352 return "Media Loading";
353 case MECHA_STATUS_PRINT:
354 return "Printing";
355 case MECHA_STATUS_IDLE:
356 return "Idle";
357 default:
358 break;
359 }
360 return "Unknown Mechanical Status";
361 }
362
363 static const char *mitsu70x_jobstatuses(const uint8_t *sts)
364 {
365 switch(sts[0]) {
366 case JOB_STATUS0_NONE:
367 return "No Job";
368 case JOB_STATUS0_DATA:
369 return "Data transfer";
370 case JOB_STATUS0_QUEUE:
371 return "Queued for printing";
372 case JOB_STATUS0_PRINT:
373 switch(sts[1]) {
374 case JOB_STATUS1_PRINT_MEDIALOAD:
375 return "Media loading";
376 case JOB_STATUS1_PRINT_PRE_Y:
377 return "Waiting to print yellow plane";
378 case JOB_STATUS1_PRINT_Y:
379 return "Printing yellow plane";
380 case JOB_STATUS1_PRINT_PRE_M:
381 return "Waiting to print magenta plane";
382 case JOB_STATUS1_PRINT_M:
383 return "Printing magenta plane";
384 case JOB_STATUS1_PRINT_PRE_C:
385 return "Waiting to print cyan plane";
386 case JOB_STATUS1_PRINT_C:
387 return "Printing cyan plane";
388 case JOB_STATUS1_PRINT_PRE_OC:
389 return "Waiting to laminate page";
390 case JOB_STATUS1_PRINT_OC:
391 return "Laminating page";
392 case JOB_STATUS1_PRINT_EJECT:
393 return "Ejecting page";
394 default:
395 return "Unknown 'Print' status1";
396 }
397 break;
398 case JOB_STATUS0_ASSIGN:
399 return "Unknown 'Assignment' status1";
400 case JOB_STATUS0_END:
401 switch(sts[1]) {
402 case JOB_STATUS1_END_OK:
403 return "Normal End";
404 case JOB_STATUS1_END_HEADER:
405 switch(sts[2]) {
406 case JOB_STATUS2_END_HEADER_ERROR:
407 return "Incorrect Header data (bad print size?)";
408 case JOB_STATUS2_END_HEADER_MEMORY:
409 return "Insufficient printer memory";
410 default:
411 return "Unknown 'End Header' status2";
412 }
413 break;
414 case JOB_STATUS1_END_PRINT:
415 switch(sts[2]) {
416 case JOB_STATUS2_END_PRINT_MEDIA:
417 return "Incorrect mediasize";
418 case JOB_STATUS2_END_PRINT_PREVERR:
419 return "Previous job terminated abnormally";
420 default:
421 return "Unknown 'End Print' status2";
422 }
423 break;
424 case JOB_STATUS1_END_INTERRUPT:
425 switch(sts[2]) {
426 case JOB_STATUS2_END_INT_TIMEOUT:
427 return "Timeout";
428 case JOB_STATUS2_END_INT_CANCEL:
429 return "Job cancelled";
430 case JOB_STATUS2_END_INT_DISCON:
431 return "Printer disconnected";
432 default:
433 return "Unknown 'End Print' status2";
434 }
435 break;
436 default:
437 if (sts[1] >= 0x10 && sts[1] <= 0x7f)
438 return "Mechanical Error";
439 else
440 return "Unknown 'End' status1";
441 }
442 break;
443 default:
444 break;
445 }
446
447 return "Unknown status0";
448 }
449
450 static const char *mitsu70x_errorclass(const uint8_t *err)
451 {
452 switch(err[1]) {
453 case ERROR_STATUS1_PAPER:
454 return "Paper";
455 case ERROR_STATUS1_RIBBON:
456 return "Ribbon";
457 case ERROR_STATUS1_SETTING:
458 return "Job settings";
459 case ERROR_STATUS1_OPEN:
460 return "Cover open";
461 case ERROR_STATUS1_NOSTRIPBIN:
462 return "No cut bin";
463 case ERROR_STATUS1_PAPERJAM:
464 return "Paper jam";
465 case ERROR_STATUS1_RIBBONSYS:
466 return "Ribbon system";
467 case ERROR_STATUS1_MECHANICAL:
468 return "Mechanical";
469 case ERROR_STATUS1_ELECTRICAL:
470 return "Electrical";
471 case ERROR_STATUS1_FIRMWARE:
472 return "Firmware";
473 case ERROR_STATUS1_OTHER:
474 return "Other";
475 default:
476 break;
477 }
478 return "Unknown error class";
479 }
480
481 static const char *mitsu70x_errorrecovery(const uint8_t *err)
482 {
483 switch(err[1]) {
484 case ERROR_STATUS2_AUTO:
485 return "Automatic recovery";
486 case ERROR_STATUS2_RELOAD_PAPER:
487 return "Reload or change paper";
488 case ERROR_STATUS2_RELOAD_RIBBON:
489 return "Reload or change ribbon";
490 case ERROR_STATUS2_CHANGE_BOTH:
491 return "Change paper and ribbon";
492 case ERROR_STATUS2_CHANGE_ONE:
493 return "Change paper or ribbon";
494 case ERROR_STATUS2_CLOSEUNIT:
495 return "Close printer";
496 case ERROR_STATUS2_ATTACHSTRIPBIN:
497 return "Attach Strip Bin";
498 case ERROR_STATUS2_CLEARJAM:
499 return "Remove and reload paper";
500 case ERROR_STATUS2_CHECKRIBBON:
501 return "Check ribbon and reload paper";
502 case ERROR_STATUS2_OPENCLOSEUNIT:
503 return "Open then close printer";
504 case ERROR_STATUS2_POWEROFF:
505 return "Power-cycle printer";
506 default:
507 break;
508 }
509 return "Unknown recovery";
510 }
511
512 static const char *mitsu70x_errors(const uint8_t *err)
513 {
514 switch(err[0]) {
515 case ERROR_STATUS0_NOSTRIPBIN:
516 return "Strip bin not attached";
517 case ERROR_STATUS0_NORIBBON:
518 return "No ribbon detected";
519 case ERROR_STATUS0_NOPAPER:
520 return "No paper loaded";
521 case ERROR_STATUS0_MEDIAMISMATCH:
522 return "Ribbon/Paper mismatch";
523 case ERROR_STATUS0_RIBBONCNTEND:
524 return "Ribbon count end";
525 case ERROR_STATUS0_BADRIBBON:
526 return "Illegal Ribbon";
527 case ERROR_STATUS0_BADJOBPARAM:
528 return "Job does not match loaded media";
529 case ERROR_STATUS0_PAPEREND:
530 return "End of paper detected";
531 case ERROR_STATUS0_RIBBONEND:
532 return "End of ribbon detected";
533 case ERROR_STATUS0_DOOROPEN_IDLE:
534 case ERROR_STATUS0_DOOROPEN_PRNT:
535 return "Printer door open";
536 case ERROR_STATUS0_POWEROFF:
537 return "Printer powered off"; // nonsense..
538 case ERROR_STATUS0_RIBBONSKIP1:
539 case ERROR_STATUS0_RIBBONSKIP2:
540 return "Ribbon skipped";
541 case ERROR_STATUS0_RIBBONJAM:
542 return "Ribbon stuck to paper";
543 case ERROR_STATUS0_RFID:
544 return "RFID read error";
545 case ERROR_STATUS0_FLASH:
546 return "FLASH read error";
547 case ERROR_STATUS0_EEPROM:
548 return "EEPROM read error";
549 case ERROR_STATUS0_PREHEAT:
550 return "Preheating unit time out";
551 case ERROR_STATUS0_MDASTATE:
552 return "Unknown MDA state";
553 case ERROR_STATUS0_PSUFANLOCKED:
554 return "Power supply fan locked up";
555 default:
556 break;
557 }
558
559 if (err[0] >= ERROR_STATUS0_RIBBON_OTHER &&
560 err[0] < ERROR_STATUS0_PAPER_JAM) {
561 return "Unknown ribbon error";
562 // XXX use err[1]/err[2] codes?
563 }
564 if (err[0] >= ERROR_STATUS0_PAPER_JAM &&
565 err[0] < ERROR_STATUS0_MECHANICAL) {
566 return "Paper jam";
567 // XXX use err[1]/err[2] codes?
568 }
569 if (err[0] >= ERROR_STATUS0_MECHANICAL &&
570 err[0] < ERROR_STATUS0_RFID) {
571 return "Unknown mechanical error";
572 // XXX use err[1]/err[2] codes?
573 }
574
575 return "Unknown error";
576 }
577
578 #define CMDBUF_LEN 512
579 #define READBACK_LEN 256
580
581 static void *mitsu70x_init(void)
582 {
583 struct mitsu70x_ctx *ctx = malloc(sizeof(struct mitsu70x_ctx));
584 if (!ctx) {
585 ERROR("Memory Allocation Failure!\n");
586 return NULL;
587 }
588 memset(ctx, 0, sizeof(struct mitsu70x_ctx));
589
590 return ctx;
591 }
592
593 static int mitsu70x_attach(void *vctx, struct dyesub_connection *conn, uint8_t jobid)
594 {
595 struct mitsu70x_ctx *ctx = vctx;
596
597 ctx->jobid = jobid;
598 if (!ctx->jobid)
599 jobid++;
600
601 ctx->conn = conn;
602
603 ctx->last_l = ctx->last_u = 65535;
604
605 #if defined(WITH_DYNAMIC)
606 /* Attempt to open the library */
607 if (mitsu_loadlib(&ctx->lib, ctx->conn->type))
608 #endif
609 WARNING("Dynamic library support not loaded, will be unable to print!\n");
610
611 struct mitsu70x_printerstatus_resp resp;
612 int ret;
613
614 if (test_mode < TEST_MODE_NOATTACH) {
615 ret = mitsu70x_get_printerstatus(ctx, &resp);
616 if (ret) {
617 ERROR("Unable to get printer status! (%d)\n", ret);
618 return CUPS_BACKEND_FAILED;
619 }
620 } else {
621 int media_code = 0xf;
622 if (getenv("MEDIA_CODE"))
623 media_code = strtol(getenv("MEDIA_CODE"), NULL, 16) & 0xf;
624
625 resp.upper.mecha_status[0] = MECHA_STATUS_INIT;
626 resp.lower.mecha_status[0] = MECHA_STATUS_INIT;
627 resp.upper.capacity = cpu_to_be16(230);
628 resp.lower.capacity = cpu_to_be16(230);
629 resp.upper.remain = cpu_to_be16(200);
630 resp.lower.remain = cpu_to_be16(200);
631 resp.upper.media_brand = 0xff;
632 resp.lower.media_brand = 0xff;
633 resp.upper.media_type = media_code;
634 resp.lower.media_type = media_code;
635 resp.dual_deck = 0x80; /* Make it a dual deck */
636 resp.vers[0].ver[0] = 0;
637 resp.subtype = 0x5e;
638 }
639
640 /* Figure out if we're a D707 with two decks */
641 if (ctx->conn->type == P_MITSU_D70X &&
642 resp.dual_deck == 0x80)
643 ctx->num_decks = 2;
644 else
645 ctx->num_decks = 1;
646
647 /* Set up markers */
648 ctx->marker[0].color = "#00FFFF#FF00FF#FFFF00";
649 ctx->marker[0].name = mitsu_media_types(ctx->conn->type, resp.lower.media_brand, resp.lower.media_type);
650 ctx->marker[0].numtype = resp.lower.media_type;
651 ctx->marker[0].levelmax = be16_to_cpu(resp.lower.capacity);
652 ctx->marker[0].levelnow = be16_to_cpu(resp.lower.remain);
653 ctx->medias[0] = resp.lower.media_type & 0xf;
654 ctx->media_subtypes[0] = resp.lower.media_subtype;
655
656 if (ctx->num_decks == 2) {
657 ctx->marker[1].color = "#00FFFF#FF00FF#FFFF00";
658 ctx->marker[1].name = mitsu_media_types(ctx->conn->type, resp.upper.media_brand, resp.upper.media_type);
659 ctx->marker[1].numtype = resp.upper.media_type;
660 ctx->marker[1].levelmax = be16_to_cpu(resp.upper.capacity);
661 ctx->marker[1].levelnow = be16_to_cpu(resp.upper.remain);
662 ctx->medias[1] = resp.upper.media_type & 0xf;
663 ctx->media_subtypes[1] = resp.upper.media_subtype;
664 }
665
666 /* Store the FW version */
667 memcpy(ctx->fwver, resp.vers[0].ver, 6);
668 ctx->fwver[6] = 0;
669 /* Store the serial number */
670 for (int i = 0 ; i < 6 ; i++) {
671 ctx->serno[i] = le16_to_cpu(resp.serno[i]) & 0x7f;
672 }
673 ctx->serno[6] = 0;
674
675 /* Check for the -S variants */
676 if (resp.subtype == 0x5e)
677 ctx->is_s = 1;
678
679 /* FW sanity checking */
680 if (ctx->conn->type == P_KODAK_305) {
681 /* Known versions:
682 v1.02: M 316E81 1433 (Add Ultrafine and matte support)
683 v1.04: M 316F83 2878 (Add 2x6 strip and support new "Triton" media)
684 v3.01: M 443A12 8908 (add 5" media support)
685 v3.02: M 443B11 2647 (Unknown)
686 */
687 if (strncmp(resp.vers[0].ver, "443B11", 6) < 0)
688 WARNING("Printer FW out of date. Highly recommend upgrading EK305 to v3.02 or newer!\n");
689 } else if (ctx->conn->type == P_MITSU_K60) {
690 /* Known versions:
691 v1.05: M 316M31 148C (Add HG media support)
692 */
693 if (strncmp(resp.vers[0].ver, "316M31", 6) < 0)
694 WARNING("Printer FW out of date. Highly recommend upgrading K60 to v1.05 or newer!\n");
695 } else if (ctx->conn->type == P_MITSU_D70X) {
696 /* Known versions for D70/D707:
697 v1.10: M 316V11 064D (Add ultrafine mode, 6x6 support, 2x6 strip, and more?)
698 v1.12: M 316W11 9FC3 (??)
699 v1.13: (??)
700
701 Known versions for D70-S/D707-S
702 v??? M 316K11 E08A (??)
703 */
704 if (strncmp(resp.vers[0].ver, "316W11", 6) < 0)
705 WARNING("Printer FW out of date. Highly recommend upgrading D70/D707 to v1.12 or newer!\n");
706 } else if (ctx->conn->type == P_FUJI_ASK300) {
707 /* Known versions:
708 v?.??: M 316A21 7998 (ancient. no matte or ultrafine)
709 v?.??: M 316H21 F8EB
710 v4.20a: M 316J21 4431 (Add 2x6 strip support)
711 */
712 if (strncmp(resp.vers[0].ver, "316J21", 6) < 0)
713 WARNING("Printer FW out of date. Highly recommend upgrading ASK300 to v4.20a or newer!\n");
714 }
715
716 return CUPS_BACKEND_OK;
717 }
718
719 static void mitsu70x_cleanup_job(const void *vjob) {
720 const struct mitsu70x_printjob *job = vjob;
721
722 if (job->databuf)
723 free(job->databuf);
724 if (job->spoolbuf)
725 free(job->spoolbuf);
726
727 free((void*)job);
728 }
729
730 static void mitsu70x_teardown(void *vctx) {
731 struct mitsu70x_ctx *ctx = vctx;
732
733 if (!ctx)
734 return;
735
736 mitsu_destroylib(&ctx->lib);
737
738 free(ctx);
739 }
740
741 #define JOB_EQUIV(__x) if (job1->__x != job2->__x) goto done
742
743 static void *mitsu70x_combine_jobs(const void *vjob1,
744 const void *vjob2)
745 {
746 const struct mitsu70x_printjob *job1 = vjob1;
747 const struct mitsu70x_printjob *job2 = vjob2;
748 struct mitsu70x_printjob *newjob = NULL;
749 uint16_t newrows;
750 uint16_t newcols;
751 int32_t middlepad, finalpad;
752 uint16_t lamoffset;
753
754 const struct mitsu70x_hdr *hdr1, *hdr2;
755 struct mitsu70x_hdr *newhdr;
756
757 /* Sanity check */
758 if (!job1 || !job2)
759 goto done;
760
761 hdr1 = (struct mitsu70x_hdr *) job1->databuf;
762 hdr2 = (struct mitsu70x_hdr *) job2->databuf;
763
764 JOB_EQUIV(rows);
765 JOB_EQUIV(cols);
766 JOB_EQUIV(matte);
767 JOB_EQUIV(sharpen);
768 JOB_EQUIV(raw_format);
769
770 if (hdr1->multicut || hdr2->multicut) // XXX type 5 (2x6*2) -> type4 (2x6*4), 6x9 needed, 2628 rows. use '4' in multicut field.
771 goto done;
772 if (hdr1->speed != hdr2->speed)
773 goto done;
774
775 switch (job1->rows) {
776 case 1218: /* K60, EK305 */
777 newrows = 2454;
778 middlepad = 18;
779 finalpad = 0;
780 lamoffset = 0;
781 break;
782 case 1228: /* D70, ASK300, D80, DS480/DS680 */
783 if (job1->cols == 1264) { // DS480 4" wide..
784 newrows = 2494;
785 middlepad = 38;
786 finalpad = 0;
787 } else {
788 newrows = 2730;
789 middlepad = 38;
790 finalpad = 236;
791 }
792 lamoffset = 12;
793 break;
794 case 1076: /* EK305, K60 3.5x5" prints */
795 newrows = 2190;
796 middlepad = 38;
797 finalpad = 0;
798 lamoffset = 0;
799 break;
800 default:
801 goto done;
802 }
803 newcols = job1->cols;
804 middlepad *= newcols;
805 finalpad *= newcols;
806
807 /* Okay, it's kosher to proceed */
808
809 DEBUG("Combining jobs to save media\n");
810
811 newjob = malloc(sizeof(*newjob));
812 if (!newjob) {
813 ERROR("Memory allocation failure!\n");
814 goto done;
815 }
816 memcpy(newjob, job1, sizeof(*newjob));
817
818 newjob->spoolbuf = NULL;
819 newjob->rows = newrows;
820 newjob->cols = newcols;
821 newjob->planelen = (((newrows * newcols * 2) + 511) /512) * 512;
822 if (newjob->matte) {
823 newjob->matte = ((((newrows + lamoffset) * newcols * 2) + 511) / 512) * 512;
824 }
825 newjob->databuf = malloc(sizeof(*newhdr) + newjob->planelen * 3 + newjob->matte);
826 newjob->datalen = 0;
827 if (!newjob->databuf) {
828 mitsu70x_cleanup_job(newjob);
829 newjob = NULL;
830 ERROR("Memory allocation failure!\n");
831 goto done;
832 }
833 newhdr = (struct mitsu70x_hdr *) newjob->databuf;
834
835 /* Copy over header */
836 memcpy(newhdr, hdr1, sizeof(*newhdr));
837 newjob->datalen += sizeof(*newhdr);
838
839 newhdr->rows = cpu_to_be16(newrows);
840 newhdr->cols = cpu_to_be16(newcols);
841
842 if (newjob->matte) {
843 newhdr->lamrows = cpu_to_be16(newrows + lamoffset);
844 newhdr->lamcols = cpu_to_be16(newcols);
845 }
846 newhdr->multicut = 1;
847 newhdr->deck = 0; /* Let printer decide */
848
849 newjob->spoolbuf = malloc(newrows * newcols * 3);
850 newjob->spoolbuflen = 0;
851 if (!newjob->spoolbuf) {
852 mitsu70x_cleanup_job(newjob);
853 newjob = NULL;
854 ERROR("Memory allocation failure!\n");
855 goto done;
856 }
857
858 /* Fill in "final" padding */
859 memset(newjob->spoolbuf + newjob->spoolbuflen, 0xff, finalpad * 3);
860 newjob->spoolbuflen += finalpad * 3;
861
862 /* Copy image1 payload */
863 memcpy(newjob->spoolbuf + newjob->spoolbuflen, job1->spoolbuf,
864 job1->spoolbuflen);
865 newjob->spoolbuflen += job1->spoolbuflen;
866
867 /* Fill in middle padding */
868 if (middlepad > 0)
869 memset(newjob->spoolbuf + newjob->spoolbuflen, 0xff, middlepad * 3);
870 newjob->spoolbuflen += middlepad * 3;
871
872 /* Copy image2 payload */
873 memcpy(newjob->spoolbuf + newjob->spoolbuflen, job2->spoolbuf,
874 job2->spoolbuflen);
875 newjob->spoolbuflen += job2->spoolbuflen;
876
877 /* Okay, we're done. */
878
879 done:
880 return newjob;
881 }
882 #undef JOB_EQUIV
883
884 static int mitsu70x_read_parse(void *vctx, const void **vjob, int data_fd, int copies) {
885 struct mitsu70x_ctx *ctx = vctx;
886 int i, remain;
887 struct mitsu70x_hdr mhdr;
888
889 struct mitsu70x_printjob *job = NULL;
890
891 if (!ctx)
892 return CUPS_BACKEND_FAILED;
893
894 job = malloc(sizeof(*job));
895 if (!job) {
896 ERROR("Memory allocation failure!\n");
897 return CUPS_BACKEND_RETRY_CURRENT;
898 }
899 memset(job, 0, sizeof(*job));
900 job->common.jobsize = sizeof(*job);
901 job->common.copies = copies;
902
903 repeat:
904 /* Read in initial header */
905 remain = sizeof(mhdr);
906 while (remain > 0) {
907 i = read(data_fd, ((uint8_t*)&mhdr) + sizeof(mhdr) - remain, remain);
908 if (i == 0) {
909 mitsu70x_cleanup_job(job);
910 return CUPS_BACKEND_CANCEL;
911 }
912 if (i < 0) {
913 mitsu70x_cleanup_job(job);
914 return CUPS_BACKEND_CANCEL;
915 }
916 remain -= i;
917 }
918
919 /* Skip over wakeup header if it's present. */
920 if (mhdr.hdr[0] == 0x1b &&
921 mhdr.hdr[1] == 0x45 &&
922 mhdr.hdr[2] == 0x57 &&
923 mhdr.hdr[3] == 0x55) {
924 goto repeat;
925 }
926
927 /* Sanity check header */
928 if (mhdr.hdr[0] != 0x1b ||
929 mhdr.hdr[1] != 0x5a ||
930 mhdr.hdr[2] != 0x54) {
931 ERROR("Unrecognized data format!\n");
932 mitsu70x_cleanup_job(job);
933 return CUPS_BACKEND_CANCEL;
934 }
935
936 job->raw_format = !mhdr.mode;
937
938 /* Sanity check Matte mode */
939 if (!mhdr.laminate && mhdr.laminate_mode) {
940 if (ctx->conn->type != P_MITSU_D70X) {
941 if (mhdr.speed != 0x03 && mhdr.speed != 0x04) {
942 WARNING("Forcing Ultrafine mode for matte printing!\n");
943 mhdr.speed = 0x04; /* Force UltraFine */
944 }
945 } else {
946 if (mhdr.speed != 0x03) {
947 mhdr.speed = 0x03; /* Force SuperFine */
948 WARNING("Forcing SuperFine mode for matte printing!\n");
949 }
950 }
951 }
952
953 /* Figure out the correction data table to use */
954 if (ctx->conn->type == P_MITSU_D70X) {
955 job->laminatefname = "D70MAT01.raw";
956 job->lutfname = "CPD70L01.lut";
957
958 if (mhdr.speed == 3) {
959 job->cpcfname = "CPD70S01.cpc";
960 } else if (mhdr.speed == 4) {
961 job->cpcfname = "CPD70U01.cpc";
962 } else {
963 job->cpcfname = "CPD70N01.cpc";
964 }
965 if (ctx->is_s && mhdr.hdr[3] != 0x00) {
966 WARNING("Print job has wrong submodel specifier (%x)\n", mhdr.hdr[3]);
967 mhdr.hdr[3] = 0x00;
968 } else if (!ctx->is_s && mhdr.hdr[3] != 0x01) {
969 WARNING("Print job has wrong submodel specifier (%x)\n", mhdr.hdr[3]);
970 mhdr.hdr[3] = 0x01;
971 }
972 } else if (ctx->conn->type == P_MITSU_D80) {
973 job->laminatefname = "D80MAT01.raw";
974 job->lutfname = "CPD80L01.lut";
975
976 if (mhdr.speed == 3) {
977 job->cpcfname = "CPD80S01.cpc";
978 if (job->matte || job->cols != 1864 || job->rows != 1228)
979 job->ecpcfname = NULL;
980 else
981 job->ecpcfname = "CPD80E01.cpc"; /* cpc vs ecpc depends on image */
982 } else if (mhdr.speed == 4) {
983 job->cpcfname = "CPD80U01.cpc";
984 job->ecpcfname = NULL;
985 } else {
986 job->cpcfname = "CPD80N01.cpc";
987 job->ecpcfname = NULL;
988 }
989 // XXX Does is_s matter?
990 if (mhdr.hdr[3] != 0x01) {
991 WARNING("Print job has wrong submodel specifier (%x)\n", mhdr.hdr[3]);
992 mhdr.hdr[3] = 0x01;
993 }
994 } else if (ctx->conn->type == P_MITSU_K60) {
995 job->laminatefname = "S60MAT02.raw";
996 job->lutfname = "CPS60L01.lut";
997
998 if (mhdr.speed == 3 || mhdr.speed == 4) {
999 mhdr.speed = 4; /* Ultra Fine */
1000 if (ctx->media_subtypes[0] == 0x10) /* HG media */
1001 job->cpcfname = "CPS60H03.cpc";
1002 else
1003 job->cpcfname = "CPS60T03.cpc";
1004 } else {
1005 if (ctx->media_subtypes[0] == 0x10) /* HG media */
1006 job->cpcfname = "CPS60H01.cpc";
1007 else
1008 job->cpcfname = "CPS60T01.cpc";
1009 }
1010 // XXX Does is_s matter?
1011 if (mhdr.hdr[3] != 0x00) {
1012 WARNING("Print job has wrong submodel specifier (%x)\n", mhdr.hdr[3]);
1013 mhdr.hdr[3] = 0x00;
1014 }
1015 } else if (ctx->conn->type == P_KODAK_305) {
1016 job->laminatefname = "EK305MAT.raw"; // Same as K60
1017 job->lutfname = "EK305L01.lut";
1018
1019 if (mhdr.speed == 3 || mhdr.speed == 4) {
1020 mhdr.speed = 4; /* Ultra Fine */
1021 job->cpcfname = "EK305T03.cpc";
1022 } else {
1023 job->cpcfname = "EK305T01.cpc";
1024 }
1025 // XXX what about using K60 media if we read back the proper code?
1026 if (mhdr.hdr[3] != 0x90) {
1027 WARNING("Print job has wrong submodel specifier (%x)\n", mhdr.hdr[3]);
1028 mhdr.hdr[3] = 0x90;
1029 }
1030 } else if (ctx->conn->type == P_FUJI_ASK300) {
1031 job->laminatefname = "ASK300M2.raw"; /* Same as D70 */
1032 job->lutfname = NULL; /* Printer does not come with external LUT */
1033 if (mhdr.speed == 3 || mhdr.speed == 4) {
1034 mhdr.speed = 3; /* Super Fine */
1035 job->cpcfname = "ASK300T3.cpc";
1036 } else {
1037 job->cpcfname = "ASK300T1.cpc";
1038 }
1039 if (mhdr.hdr[3] != 0x80) {
1040 WARNING("Print job has wrong submodel specifier (%x)\n", mhdr.hdr[3]);
1041 mhdr.hdr[3] = 0x80;
1042 }
1043 } else if (ctx->conn->type == P_DNP_DSX80) { // XXX Correct when we get it.
1044 job->laminatefname = "D70MAT01.raw";
1045 job->lutfname = "CPD70L01.lut";
1046
1047 if (mhdr.speed == 3 || mhdr.speed == 4) {
1048 mhdr.speed = 3; /* Super Fine */
1049 job->cpcfname = "CPD70S01.cpc";
1050 } else {
1051 job->cpcfname = "CPD70N01.cpc";
1052 }
1053 if (mhdr.hdr[3] != 0x04) {
1054 WARNING("Print job has wrong submodel specifier (%x)\n", mhdr.hdr[3]);
1055 mhdr.hdr[3] = 0x04;
1056 }
1057 }
1058 if (!mhdr.use_lut)
1059 job->lutfname = NULL;
1060
1061 job->sharpen = mhdr.sharpen - 1;
1062 job->reverse = !mhdr.reversed;
1063 job->common.copies = mhdr.copies;
1064
1065 /* Use larger of our copy counts */
1066 if (job->common.copies < copies)
1067 job->common.copies = copies;
1068
1069 /* Clean up header back to pristine. */
1070 mhdr.use_lut = 0;
1071 mhdr.mode = 0;
1072 mhdr.sharpen = 0;
1073 mhdr.reversed = 0;
1074 mhdr.copies = 0;
1075
1076 /* Work out total printjob size */
1077 job->cols = be16_to_cpu(mhdr.cols);
1078 job->rows = be16_to_cpu(mhdr.rows);
1079
1080 job->planelen = job->rows * job->cols * 2;
1081 job->planelen = (job->planelen + 511) / 512 * 512; /* Round to nearest 512 bytes. */
1082
1083 if (!mhdr.laminate && mhdr.laminate_mode) {
1084 i = be16_to_cpu(mhdr.lamcols) * be16_to_cpu(mhdr.lamrows) * 2;
1085 i = (i + 511) / 512 * 512; /* Round to nearest 512 bytes. */
1086 job->matte = i;
1087 }
1088
1089 remain = 3 * job->planelen + job->matte;
1090
1091 job->datalen = 0;
1092 job->databuf = malloc(sizeof(mhdr) + remain + LAMINATE_STRIDE*2); /* Give us a bit extra */
1093
1094 if (!job->databuf) {
1095 ERROR("Memory allocation failure!\n");
1096 mitsu70x_cleanup_job(job);
1097 return CUPS_BACKEND_RETRY_CURRENT;
1098 }
1099
1100 memcpy(job->databuf + job->datalen, &mhdr, sizeof(mhdr));
1101 job->datalen += sizeof(mhdr);
1102
1103 if (job->raw_format) { /* RAW MODE */
1104 DEBUG("Reading in %d bytes of 16bpp YMC%sdata\n", remain,
1105 job->matte ? "L " : " ");
1106
1107 /* Read in the spool data */
1108 while(remain) {
1109 i = read(data_fd, job->databuf + job->datalen, remain);
1110 if (i == 0) {
1111 mitsu70x_cleanup_job(job);
1112 return CUPS_BACKEND_CANCEL;
1113 }
1114 if (i < 0) {
1115 mitsu70x_cleanup_job(job);
1116 return CUPS_BACKEND_CANCEL;
1117 }
1118 job->datalen += i;
1119 remain -= i;
1120 }
1121 goto bypass_raw;
1122 }
1123
1124 /* Non-RAW mode! */
1125 remain = job->rows * job->cols * 3;
1126 DEBUG("Reading in %d bytes of 8bpp BGR data\n", remain);
1127
1128 job->spoolbuflen = 0;
1129 job->spoolbuf = malloc(remain);
1130 if (!job->spoolbuf) {
1131 ERROR("Memory allocation failure!\n");
1132 mitsu70x_cleanup_job(job);
1133 return CUPS_BACKEND_RETRY_CURRENT;
1134 }
1135
1136 /* Read in the BGR data */
1137 while (remain) {
1138 i = read(data_fd, job->spoolbuf + job->spoolbuflen, remain);
1139 if (i == 0) {
1140 mitsu70x_cleanup_job(job);
1141 return CUPS_BACKEND_CANCEL;
1142 }
1143 if (i < 0) {
1144 mitsu70x_cleanup_job(job);
1145 return CUPS_BACKEND_CANCEL;
1146 }
1147 job->spoolbuflen += i;
1148 remain -= i;
1149 }
1150
1151 if (!ctx->lib.dl_handle) {
1152 ERROR("!!! Image Processing Library not found, aborting!\n");
1153 mitsu70x_cleanup_job(job);
1154 return CUPS_BACKEND_CANCEL;
1155 }
1156
1157 /* Run through basic LUT, if present and enabled */
1158 if (job->lutfname) {
1159 int ret = mitsu_apply3dlut_packed(&ctx->lib, job->lutfname,
1160 job->spoolbuf, job->cols,
1161 job->rows, job->cols * 3,
1162 COLORCONV_BGR);
1163 if (ret) {
1164 mitsu70x_cleanup_job(job);
1165 return ret;
1166 }
1167 }
1168
1169 bypass_raw:
1170 for (i = 0 ; i < ctx->num_decks ; i++) {
1171 switch (ctx->medias[i]) {
1172 case 0x0: // 4x8 (DS480 only)
1173 if (job->rows == 1228 ||
1174 job->rows == 1614 ||
1175 job->rows == 2422 ||
1176 job->rows == 2494)
1177 job->decks_ok[i] = 1;
1178 if (job->rows == 2422 ||
1179 job->rows == 2494)
1180 job->decks_exact[i] = 1;
1181 if (job->cols != 1264)
1182 job->decks_ok[i] = job->decks_exact[i] = 0;
1183 break;
1184 case 0x1: // 5x3.5
1185 if (job->rows == 1076)
1186 job->decks_ok[i] = 1;
1187 if (job->rows == 1076)
1188 job->decks_exact[i] = 1;
1189 break;
1190 case 0x2: // 4x6
1191 if (job->rows == 1218 ||
1192 job->rows == 1228)
1193 job->decks_ok[i] = 1;
1194 if (job->rows == 1218 ||
1195 job->rows == 1228)
1196 job->decks_exact[i] = 1;
1197 break;
1198 case 0x4: // 5x7
1199 if (job->rows == 1076 ||
1200 job->rows == 1524 ||
1201 job->rows == 2128)
1202 job->decks_ok[i] = 1;
1203 if (job->rows == 1524 ||
1204 job->rows == 2128)
1205 job->decks_exact[i] = 1;
1206 break;
1207 case 0x5: // 6x9
1208 case 0xf: // 6x8
1209 /* This is made more complicated:
1210 some 6x8" jobs are 6x9" sized. Let printer
1211 sort these out. It's unlikely we'll have
1212 6x8" in one deck and 6x9" in the other!
1213 */
1214 if (job->rows == 1218 ||
1215 job->rows == 1228 ||
1216 job->rows == 1820 ||
1217 job->rows == 2422 ||
1218 job->rows == 2564 ||
1219 job->rows == 2730)
1220 job->decks_ok[i] = 1;
1221 if (job->rows == 2422 ||
1222 job->rows == 2564 ||
1223 job->rows == 2730)
1224 job->decks_exact[i] = 1;
1225 break;
1226 default:
1227 job->decks_ok[i] = 0;
1228 job->decks_exact[i] = 0;
1229 break;
1230 }
1231 }
1232
1233 /* 6x4 can be combined, only on 6x8/6x9" media. */
1234 job->common.can_combine = 0;
1235 if (job->decks_exact[0] ||
1236 job->decks_exact[1]) {
1237 /* Exact media match, don't combine. */
1238 } else if (job->rows == 1218 ||
1239 job->rows == 1228) {
1240 if (ctx->medias[0] == 0xf ||
1241 ctx->medias[0] == 0x5 ||
1242 ctx->medias[1] == 0xf || /* Two decks possible */
1243 ctx->medias[1] == 0x5 ||
1244 ctx->medias[0] == 0x00 /* DS480 only */)
1245 job->common.can_combine = !job->raw_format;
1246 } else if (job->rows == 1076) {
1247 if (ctx->conn->type == P_KODAK_305 ||
1248 ctx->conn->type == P_MITSU_K60) {
1249 if (ctx->medias[0] == 0x4) /* Only one deck */
1250 job->common.can_combine = !job->raw_format;
1251 }
1252 }
1253
1254 /* Return what we found */
1255 *vjob = job;
1256
1257 return CUPS_BACKEND_OK;
1258 }
1259
1260 static int mitsu70x_get_jobstatus(struct mitsu70x_ctx *ctx, struct mitsu70x_jobstatus *resp, uint16_t jobid)
1261 {
1262 uint8_t cmdbuf[CMDBUF_LEN];
1263 int num, ret;
1264
1265 /* Send Printer Query */
1266 memset(cmdbuf, 0, CMDBUF_LEN);
1267 cmdbuf[0] = 0x1b;
1268 cmdbuf[1] = 0x56;
1269 cmdbuf[2] = 0x31;
1270 cmdbuf[3] = 0x30; // XXX 30 == specific, 31 = "all"
1271
1272 cmdbuf[4] = (jobid >> 8) & 0xff;
1273 cmdbuf[5] = jobid & 0xff;
1274
1275 if ((ret = send_data(ctx->conn,
1276 cmdbuf, 6)))
1277 return ret;
1278
1279 memset(resp, 0, sizeof(*resp));
1280
1281 ret = read_data(ctx->conn,
1282 (uint8_t*) resp, sizeof(*resp), &num);
1283
1284 if (ret < 0)
1285 return ret;
1286 if (num != sizeof(*resp)) {
1287 ERROR("Short Read! (%d/%d)\n", num, (int)sizeof(*resp));
1288 return 4;
1289 }
1290
1291 return CUPS_BACKEND_OK;
1292 }
1293
1294 #if 0
1295 static int mitsu70x_get_jobs(struct mitsu70x_ctx *ctx, struct mitsu70x_jobs *resp)
1296 {
1297 uint8_t cmdbuf[CMDBUF_LEN];
1298 int num, ret;
1299
1300 /* Send Printer Query */
1301 memset(cmdbuf, 0, CMDBUF_LEN);
1302 cmdbuf[0] = 0x1b;
1303 cmdbuf[1] = 0x56;
1304 cmdbuf[2] = 0x31;
1305 cmdbuf[3] = 0x31;
1306 cmdbuf[4] = 0x00;
1307 cmdbuf[5] = 0x00;
1308
1309 if ((ret = send_data(ctx->conn,
1310 cmdbuf, 6)))
1311 return ret;
1312
1313 memset(resp, 0, sizeof(*resp));
1314
1315 ret = read_data(ctx->conn,
1316 (uint8_t*) resp, sizeof(*resp), &num);
1317
1318 if (ret < 0)
1319 return ret;
1320 if (num != sizeof(*resp)) {
1321 ERROR("Short Read! (%d/%d)\n", num, (int)sizeof(*resp));
1322 return 4;
1323 }
1324
1325 return CUPS_BACKEND_OK;
1326 }
1327 #endif
1328
1329 static int mitsu70x_get_memorystatus(struct mitsu70x_ctx *ctx, const struct mitsu70x_printjob *job, uint8_t mcut, struct mitsu70x_memorystatus_resp *resp)
1330 {
1331 uint8_t cmdbuf[CMDBUF_LEN];
1332
1333 uint16_t tmp;
1334
1335 int num;
1336 int ret;
1337
1338 memset(cmdbuf, 0, CMDBUF_LEN);
1339 cmdbuf[0] = 0x1b;
1340 cmdbuf[1] = 0x56;
1341 cmdbuf[2] = 0x33;
1342 cmdbuf[3] = 0x00;
1343 tmp = cpu_to_be16(job->cols);
1344 memcpy(cmdbuf + 4, &tmp, 2);
1345
1346 /* We have to lie about print sizes in 4x6*2 multicut modes */
1347 // XXX what about type4 (2x6*4) and type3 (3x6*3)
1348 tmp = job->rows;
1349 if (tmp == 2730 && mcut == 1) {
1350 if (ctx->conn->type == P_MITSU_D70X ||
1351 ctx->conn->type == P_FUJI_ASK300) {
1352 tmp = 2422;
1353 }
1354 }
1355
1356 tmp = cpu_to_be16(tmp);
1357 memcpy(cmdbuf + 6, &tmp, 2);
1358 cmdbuf[8] = job->matte ? 0x80 : 0x00;
1359 cmdbuf[9] = 0x00;
1360
1361 if ((ret = send_data(ctx->conn,
1362 cmdbuf, 10)))
1363 return CUPS_BACKEND_FAILED;
1364
1365 /* Read in the printer status */
1366 ret = read_data(ctx->conn,
1367 (uint8_t*) resp, sizeof(*resp), &num);
1368 if (ret < 0)
1369 return CUPS_BACKEND_FAILED;
1370
1371 if (num != sizeof(*resp)) {
1372 ERROR("Short Read! (%d/%d)\n", num, (int)sizeof(*resp));
1373 return CUPS_BACKEND_FAILED;
1374 }
1375
1376 /* Make sure response is sane */
1377 if (resp->hdr[0] != 0xe4 ||
1378 resp->hdr[1] != 0x56 ||
1379 resp->hdr[2] != 0x33) {
1380 ERROR("Unknown response from printer\n");
1381 return CUPS_BACKEND_FAILED;
1382 }
1383
1384 return CUPS_BACKEND_OK;
1385 }
1386
1387 static int mitsu70x_get_printerstatus(struct mitsu70x_ctx *ctx, struct mitsu70x_printerstatus_resp *resp)
1388 {
1389 uint8_t cmdbuf[CMDBUF_LEN];
1390 int num, ret;
1391
1392 /* Send Printer Query */
1393 memset(cmdbuf, 0, CMDBUF_LEN);
1394 cmdbuf[0] = 0x1b;
1395 cmdbuf[1] = 0x56;
1396 cmdbuf[2] = 0x32;
1397 cmdbuf[3] = 0x30; /* or x31 or x32, for SINGLE DECK lower/upper query!
1398 Results will only have one deck. */
1399 if ((ret = send_data(ctx->conn,
1400 cmdbuf, 4)))
1401 return ret;
1402 memset(resp, 0, sizeof(*resp));
1403 ret = read_data(ctx->conn,
1404 (uint8_t*) resp, sizeof(*resp), &num);
1405
1406 if (ret < 0)
1407 return ret;
1408 if (num != sizeof(*resp)) {
1409 ERROR("Short Read! (%d/%d)\n", num, (int)sizeof(*resp));
1410 return 4;
1411 }
1412
1413 return CUPS_BACKEND_OK;
1414 }
1415
1416 static int mitsu70x_cancel_job(struct mitsu70x_ctx *ctx, uint16_t jobid)
1417 {
1418 uint8_t cmdbuf[4];
1419 int ret;
1420
1421 /* Send Job cancel. No response. */
1422 memset(cmdbuf, 0, 4);
1423 cmdbuf[0] = 0x1b;
1424 cmdbuf[1] = 0x44;
1425 cmdbuf[2] = (jobid >> 8) & 0xff;
1426 cmdbuf[3] = jobid & 0xff;
1427 if ((ret = send_data(ctx->conn,
1428 cmdbuf, 4)))
1429 return ret;
1430
1431 return CUPS_BACKEND_OK;
1432 }
1433
1434 static int mitsu70x_test_print(struct mitsu70x_ctx *ctx, int type)
1435 {
1436 uint8_t cmdbuf[14];
1437 int ret, num = 0;
1438 uint8_t resp[256];
1439
1440 /* Send Test ON */
1441 memset(cmdbuf, 0, 8);
1442 cmdbuf[0] = 0x1b;
1443 cmdbuf[1] = 0x76;
1444 cmdbuf[2] = 0x54;
1445 cmdbuf[3] = 0x45;
1446 cmdbuf[4] = 0x53;
1447 cmdbuf[5] = 0x54;
1448 cmdbuf[6] = 0x4f;
1449 cmdbuf[7] = 0x4e;
1450 if ((ret = send_data(ctx->conn,
1451 cmdbuf, 8)))
1452 return ret;
1453
1454 memset(resp, 0, sizeof(resp));
1455
1456 ret = read_data(ctx->conn,
1457 resp, sizeof(resp), &num); // always e4 44 4f 4e 45
1458
1459 if (ret) return ret;
1460
1461 /* Send Test print. */
1462 memset(cmdbuf, 0x30, 12);
1463 cmdbuf[0] = 0x1b;
1464 cmdbuf[1] = 0x6a;
1465 cmdbuf[2] = 0x31;
1466
1467 switch(type) {
1468 default:
1469 case 0: /* Test Print */
1470 cmdbuf[4] = 0x31;
1471 cmdbuf[11] = 0x31;
1472 break;
1473 case 1: /* Solid Black */
1474 cmdbuf[3] = 0x32;
1475 cmdbuf[4] = 0x31;
1476 cmdbuf[6] = 0x46;
1477 cmdbuf[7] = 0x46;
1478 cmdbuf[11] = 0x31;
1479 break;
1480 case 2: /* Solid Gray */
1481 cmdbuf[3] = 0x32;
1482 cmdbuf[4] = 0x31;
1483 cmdbuf[6] = 0x38;
1484 cmdbuf[11] = 0x31;
1485 break;
1486 case 3: /* Head Pattern */
1487 cmdbuf[3] = 0x31;
1488 cmdbuf[4] = 0x31;
1489 cmdbuf[11] = 0x31;
1490 break;
1491 case 4: /* Color Bar */
1492 cmdbuf[3] = 0x34;
1493 cmdbuf[4] = 0x31;
1494 cmdbuf[11] = 0x31;
1495 break;
1496 case 5: /* Vertical Alignment */
1497 cmdbuf[3] = 0x32;
1498 cmdbuf[4] = 0x31;
1499 cmdbuf[6] = 0x38;
1500 cmdbuf[11] = 0x32;
1501 break;
1502 case 6: /* Horizontal Alignment; Grey Cross */
1503 cmdbuf[3] = 0x32;
1504 cmdbuf[4] = 0x31;
1505 cmdbuf[6] = 0x38;
1506 cmdbuf[11] = 0x31;
1507 break;
1508 }
1509 if ((ret = send_data(ctx->conn,
1510 cmdbuf, 12)))
1511 return ret;
1512
1513 ret = read_data(ctx->conn,
1514 resp, sizeof(resp), &num); /* Get 5 back */
1515
1516 return ret;
1517 }
1518
1519 static int mitsu70x_test_dump(struct mitsu70x_ctx *ctx)
1520 {
1521 uint8_t cmdbuf[14];
1522 int ret, num = 0;
1523 uint8_t resp[8192];
1524
1525 /* Send Test ON */
1526 memset(cmdbuf, 0, 8);
1527 cmdbuf[0] = 0x1b;
1528 cmdbuf[1] = 0x76;
1529 cmdbuf[2] = 0x54;
1530 cmdbuf[3] = 0x45;
1531 cmdbuf[4] = 0x53;
1532 cmdbuf[5] = 0x54;
1533 cmdbuf[6] = 0x4f;
1534 cmdbuf[7] = 0x4e;
1535 if ((ret = send_data(ctx->conn,
1536 cmdbuf, 8)))
1537 return ret;
1538
1539 memset(resp, 0, sizeof(resp));
1540
1541 ret = read_data(ctx->conn,
1542 resp, sizeof(resp), &num); // always e4 44 4f 4e 45
1543
1544 if (ret) return ret;
1545
1546 /* Get calibration parameters */
1547 memset(cmdbuf, 0, 6);
1548 cmdbuf[0] = 0x1b;
1549 cmdbuf[1] = 0x6a;
1550 cmdbuf[2] = 0x36;
1551 cmdbuf[3] = 0x34;
1552 cmdbuf[4] = 0x31;
1553 cmdbuf[5] = 0x00;
1554 if ((ret = send_data(ctx->conn,
1555 cmdbuf, 6)))
1556 return ret;
1557 ret = read_data(ctx->conn,
1558 resp, sizeof(resp), &num); // 56 back!
1559
1560 if (ret) return ret;
1561
1562 /* response is struct mitsu70x_calinfo_resp */
1563 {
1564 const struct mitsu70x_calinfo_resp *calinfo = (struct mitsu70x_calinfo_resp*) resp;
1565 char buf[5];
1566 float f;
1567
1568 memset(buf, 0x0, sizeof(buf));
1569 memcpy(buf, calinfo->adj_horiz, 2);
1570 f = strtol(buf, NULL, 16);
1571 if (f > 127) f -= 256;
1572 f *= 0.085; /* 300dpi = 0.085mm/pixel */
1573 INFO("Horizontal Calibration: %2.3f mm\n", f);
1574 memcpy(buf, calinfo->adj_vertA, 2);
1575 f = strtol(buf, NULL, 16);
1576 if (f > 127) f -= 256;
1577 f *= 0.085;
1578 INFO("Vertical Calibration A: %2.3f mm\n", f);
1579 memcpy(buf, calinfo->adj_vertB, 2);
1580 f = strtol(buf, NULL, 16);
1581 if (f > 127) f -= 256;
1582 f *= 0.085;
1583 INFO("Vertical Calibration B: %2.3f mm\n", f);
1584 memcpy(buf, calinfo->adj_vertC, 2);
1585 f = strtol(buf, NULL, 16);
1586 if (f > 127) f -= 256;
1587 f *= 0.085;
1588 INFO("Vertical Calibration C: %2.2f mm\n", f);
1589 }
1590
1591 /* Get eeprom dump.. */
1592 memset(cmdbuf, 0, 14);
1593 cmdbuf[0] = 0x1b;
1594 cmdbuf[1] = 0x6a;
1595 cmdbuf[2] = 0x36;
1596 cmdbuf[3] = 0x36;
1597 cmdbuf[4] = 0x31;
1598 cmdbuf[5] = 0x00;
1599 cmdbuf[6] = 0x31; //
1600 cmdbuf[7] = 0x30; //
1601 cmdbuf[8] = 0x30; //
1602 cmdbuf[9] = 0x30; // x1000 = LENGTH (4096, 1 -> 4096)
1603 cmdbuf[10] = 0x30; //
1604 cmdbuf[11] = 0x30; //
1605 cmdbuf[12] = 0x30; //
1606 cmdbuf[13] = 0x30; // x0000 = ADDRESS (0, x0-x7fff)
1607 if ((ret = send_data(ctx->conn,
1608 cmdbuf, 14)))
1609 return ret;
1610 ret = read_data(ctx->conn,
1611 resp, sizeof(resp), &num); // 4110 back!
1612
1613 /* To set calibration: 1b 6a 30 XX XX XX ?? ??
1614
1615 where ?? ?? is ASCII representation of hex value
1616
1617 Horiz = x70 31 31, range 0x00->0xff (unit is pixels or 0.085 mm, def 0)
1618 VertA = x70 31 32, range -1 -> 9 (unit is pixels or 0.085mm, def 4)
1619 VertB = x70 31 33, range -4 -> 6 (def 1)
1620 VertC = x70 31 34, range -1 -> 9 (def 4)
1621 M1 = x71 31 31, range -128 -> +127 step 0.05v (NOT on ASK300 / D70, one value ?)
1622 M1v2 = x71 31 35, range -128 -> +127 step 0.05v (for on ASK300 / D70, two values? Fine / UFine )
1623 M3 = x71 31 32, range -100 -> +100 (unknown unit, value is unique to each M3 motor)
1624 UFine = x71 31 35, (legal values are enum)
1625 Density = x73 31 31, 6800d -> 9000d (steps of 80d)
1626 24v = x61 30 00 (range 0x00 -> 0xff)
1627
1628 */
1629
1630 return ret;
1631 }
1632
1633 static int mitsu70x_set_sleeptime(struct mitsu70x_ctx *ctx, uint8_t time)
1634 {
1635 uint8_t cmdbuf[4];
1636 int ret;
1637
1638 /* 60 minutes max, according to all docs. */
1639 if (time > 60)
1640 time = 60;
1641
1642 /* Send Parameter.. */
1643 memset(cmdbuf, 0, 4);
1644 cmdbuf[0] = 0x1b;
1645 cmdbuf[1] = 0x53;
1646 cmdbuf[2] = 0x53;
1647 cmdbuf[3] = time;
1648
1649 if ((ret = send_data(ctx->conn,
1650 cmdbuf, 4)))
1651 return ret;
1652
1653 return CUPS_BACKEND_OK;
1654 }
1655
1656 static int mitsu70x_set_iserial(struct mitsu70x_ctx *ctx, uint8_t enabled)
1657 {
1658 uint8_t cmdbuf[4];
1659 int ret;
1660
1661 if (enabled)
1662 enabled = 0;
1663 else
1664 enabled = 0x80;
1665
1666 /* Send Parameter.. */
1667 memset(cmdbuf, 0, 4);
1668 cmdbuf[0] = 0x1b;
1669 cmdbuf[1] = 0x53;
1670 cmdbuf[2] = 0x4e;
1671 cmdbuf[3] = enabled;
1672
1673 if ((ret = send_data(ctx->conn,
1674 cmdbuf, 4)))
1675 return ret;
1676
1677 return CUPS_BACKEND_OK;
1678 }
1679
1680 #if 0
1681 /* Switches between "Driver" and "SDK" modes.
1682 Single-endpoint vs Multi-Endpoint, essentially.
1683 Not sure about the polarity.
1684 */
1685 static int mitsu70x_set_printermode(struct mitsu70x_ctx *ctx, uint8_t enabled)
1686 {
1687 uint8_t cmdbuf[4];
1688 int ret;
1689
1690 if (enabled)
1691 enabled = 0;
1692 else
1693 enabled = 0x80;
1694
1695 /* Send Parameter.. */
1696 memset(cmdbuf, 0, 4);
1697 cmdbuf[0] = 0x1b;
1698 cmdbuf[1] = 0x53;
1699 cmdbuf[2] = 0x50;
1700 cmdbuf[3] = enabled;
1701
1702 if ((ret = send_data(ctx->conn,
1703 cmdbuf, 4)))
1704 return ret;
1705
1706 return CUPS_BACKEND_OK;
1707 }
1708 #endif
1709
1710 static int mitsu70x_wakeup(struct mitsu70x_ctx *ctx, int wait)
1711 {
1712 int ret;
1713 uint8_t buf[512];
1714 struct mitsu70x_jobstatus jobstatus;
1715
1716 top:
1717 /* Query job status for jobid 0 (global) */
1718 ret = mitsu70x_get_jobstatus(ctx, &jobstatus, 0x0000);
1719 if (ret)
1720 return CUPS_BACKEND_FAILED;
1721
1722 /* Trigger a wakeup if necessary */
1723 if (jobstatus.power) {
1724 INFO("Waking up printer...\n");
1725
1726 memset(buf, 0, sizeof(buf));
1727 buf[0] = 0x1b;
1728 buf[1] = 0x45;
1729 buf[2] = 0x57; // XXX also, 0x53, 0x54 seen.
1730 buf[3] = 0x55;
1731
1732 if ((ret = send_data(ctx->conn,
1733 buf, sizeof(buf))))
1734 return CUPS_BACKEND_FAILED;
1735
1736 if (wait) {
1737 sleep(1);
1738 goto top;
1739 }
1740 }
1741
1742
1743 return CUPS_BACKEND_OK;
1744 }
1745
1746 static int d70_library_dout_callback(void *context, void *buffer, uint32_t len)
1747 {
1748 uint32_t chunk = len;
1749 uint32_t offset = 0;
1750 int ret = 0;
1751
1752 struct mitsu70x_ctx *ctx = context;
1753
1754 while (chunk > 0) {
1755 if (chunk > CHUNK_LEN)
1756 chunk = CHUNK_LEN;
1757
1758 ret = send_data(ctx->conn, (uint8_t*)buffer + offset, chunk);
1759 if (ret < 0)
1760 break;
1761
1762 offset += chunk;
1763 chunk = len - offset;
1764 }
1765
1766 return ret;
1767 }
1768
1769 static int mitsu70x_main_loop(void *vctx, const void *vjob, int wait_for_return)
1770 {
1771 struct mitsu70x_ctx *ctx = vctx;
1772 struct mitsu70x_jobstatus jobstatus;
1773 struct mitsu70x_printerstatus_resp resp;
1774 struct mitsu70x_hdr *hdr;
1775 uint8_t last_status[4] = {0xff, 0xff, 0xff, 0xff};
1776
1777 int ret;
1778 int copies;
1779 int deck, legal, reqdeck;
1780
1781 struct mitsu70x_printjob *job = (struct mitsu70x_printjob *) vjob;
1782
1783 if (!ctx)
1784 return CUPS_BACKEND_FAILED;
1785 if (!job)
1786 return CUPS_BACKEND_FAILED;
1787
1788 copies = job->common.copies;
1789 hdr = (struct mitsu70x_hdr*) job->databuf;
1790
1791 /* Keep track of deck requested */
1792 reqdeck = hdr->deck;
1793
1794 if (job->raw_format)
1795 goto bypass;
1796
1797 struct BandImage input;
1798 uint8_t rew[3] = { 1, 1, 1 }; /* 1 for rewind ok (default!) */
1799 // XXX only allow rewinds for appropriate sizes?
1800
1801 /* Load in the CPC file, if needed */
1802 if (job->cpcfname && job->cpcfname != ctx->last_cpcfname) {
1803 char full[2048];
1804 ctx->last_cpcfname = job->cpcfname;
1805 if (ctx->lib.cpcdata)
1806 ctx->lib.DestroyCPCData(ctx->lib.cpcdata);
1807
1808 snprintf(full, sizeof(full), "%s/%s", corrtable_path, job->cpcfname);
1809
1810 ctx->lib.cpcdata = ctx->lib.GetCPCData(full);
1811 if (!ctx->lib.cpcdata) {
1812 ERROR("Unable to load CPC file '%s'\n", full);
1813 return CUPS_BACKEND_CANCEL;
1814 }
1815 }
1816
1817 /* Load in the secondary CPC, if needed */
1818 if (job->ecpcfname != ctx->last_ecpcfname) {
1819 char full[2048];
1820 ctx->last_ecpcfname = job->ecpcfname;
1821 if (ctx->lib.ecpcdata)
1822 ctx->lib.DestroyCPCData(ctx->lib.ecpcdata);
1823
1824 snprintf(full, sizeof(full), "%s/%s", corrtable_path, job->ecpcfname);
1825
1826 if (job->ecpcfname) {
1827 ctx->lib.ecpcdata = ctx->lib.GetCPCData(full);
1828 if (!ctx->lib.ecpcdata) {
1829 ERROR("Unable to load CPC file '%s'\n", full);
1830 return CUPS_BACKEND_CANCEL;
1831 }
1832 } else {
1833 ctx->lib.ecpcdata = NULL;
1834 }
1835 }
1836
1837 /* Convert using image processing library */
1838 input.origin_rows = input.origin_cols = 0;
1839 input.rows = job->rows;
1840 input.cols = job->cols;
1841 input.imgbuf = job->spoolbuf;
1842 input.bytes_per_row = job->cols * 3;
1843
1844 ctx->output.origin_rows = ctx->output.origin_cols = 0;
1845 ctx->output.rows = job->rows;
1846 ctx->output.cols = job->cols;
1847 ctx->output.imgbuf = job->databuf + job->datalen;
1848 ctx->output.bytes_per_row = job->cols * 3 * 2;
1849
1850 DEBUG("Running print data through processing library\n");
1851 if (ctx->lib.DoImageEffect(ctx->lib.cpcdata, ctx->lib.ecpcdata,
1852 &input, &ctx->output, job->sharpen, job->reverse, rew)) {
1853 ERROR("Image Processing failed, aborting!\n");
1854 return CUPS_BACKEND_CANCEL;
1855 }
1856
1857 /* Twiddle rewind stuff if needed */
1858 if (ctx->conn->type != P_MITSU_D70X) {
1859 /* We can't rewind when printing matte or on fine (fastest) speed */
1860 if (job->matte || hdr->speed == 0)
1861 rew[0] = 0;
1862
1863 /* We may need to lower the printspeed... */
1864 if (rew[2] == 0 && hdr->speed == 3)
1865 hdr->speed = 4;
1866
1867 hdr->rewind[0] = !rew[0];
1868 hdr->rewind[1] = !rew[1];
1869 DEBUG("Rewind Inhibit? %02x %02x\n", hdr->rewind[0], hdr->rewind[1]);
1870 }
1871
1872 /* Move up the pointer to after the image data */
1873 job->datalen += 3*job->planelen;
1874
1875 /* Clean up */
1876 free(job->spoolbuf);
1877 job->spoolbuf = NULL;
1878 job->spoolbuflen = 0;
1879
1880 /* Now that we've filled everything in, read matte from file */
1881 if (job->matte) {
1882 ret = mitsu_readlamdata(job->laminatefname, LAMINATE_STRIDE,
1883 job->databuf, &job->datalen,
1884 be16_to_cpu(hdr->lamrows), be16_to_cpu(hdr->lamcols), 2);
1885 if (ret)
1886 return ret;
1887
1888 /* Zero out the tail end of the buffer. */
1889 ret = be16_to_cpu(hdr->lamcols) * be16_to_cpu(hdr->lamrows) * 2;
1890 memset(job->databuf + job->datalen, 0, job->matte - ret);
1891 }
1892 job->raw_format = 2;
1893
1894 bypass:
1895 /* Bypass */
1896 if (test_mode >= TEST_MODE_NOPRINT)
1897 return CUPS_BACKEND_OK;
1898
1899 INFO("Waiting for printer idle...\n");
1900
1901 /* Ensure printer is awake */
1902 ret = mitsu70x_wakeup(ctx, 1);
1903 if (ret)
1904 return CUPS_BACKEND_FAILED;
1905
1906 top:
1907 /* Query job status for jobid 0 (global) */
1908 ret = mitsu70x_get_jobstatus(ctx, &jobstatus, 0x0000);
1909 if (ret)
1910 return CUPS_BACKEND_FAILED;
1911
1912 /* Figure out which deck(s) can be used.
1913 This should be in the main loop due to copy retries */
1914
1915 /* First, try to respect requested deck */
1916 if (ctx->conn->type == P_MITSU_D70X) {
1917 deck = reqdeck; /* Respect D70 deck choice, 0 is automatic. */
1918 } else {
1919 deck = 1; /* All others have one deck only */
1920 }
1921
1922 /* If user requested a specific deck, go with it, if it's legal */
1923 if (deck == 1 && job->decks_ok[0]) {
1924 deck = 1;
1925 } else if (deck == 2 && job->decks_ok[1]) {
1926 deck = 2;
1927 /* If we have an exact match for media, use it exclusively */
1928 } else if (job->decks_exact[0] && job->decks_exact[1]) {
1929 deck = 1 | 2;
1930 } else if (job->decks_exact[0]) {
1931 deck = 1;
1932 } else if (job->decks_exact[1]) {
1933 deck = 2;
1934 /* Use a non-exact match only if we don't have an exact match */
1935 } else if (job->decks_ok[0] && job->decks_ok[1]) {
1936 deck = 1 | 2;
1937 } else if (job->decks_ok[0]) {
1938 deck = 1;
1939 } else if (job->decks_ok[1]) {
1940 deck = 2;
1941 } else {
1942 ERROR("Loaded media does not match job!\n");
1943 return CUPS_BACKEND_CANCEL;
1944 }
1945
1946 if (ctx->num_decks > 1)
1947 DEBUG("Deck selection mask: %d (%d %d %d/%d %d/%d) \n",
1948 deck, hdr->deck, job->rows,
1949 job->decks_exact[0], job->decks_exact[1],
1950 job->decks_ok[0], job->decks_ok[1]);
1951
1952 /* Okay, we know which decks are _legal_, pick one to use */
1953 legal = deck;
1954 if (deck & 1) {
1955 if (jobstatus.temperature == TEMPERATURE_COOLING) {
1956 if (ctx->num_decks == 2)
1957 INFO("Lower deck cooling down...\n");
1958 else
1959 INFO("Printer cooling down...\n");
1960 deck &= ~1;
1961 } else if (jobstatus.error_status[0]) {
1962 ERROR("%s %s/%s -> %s: %02x/%02x/%02x\n",
1963 ctx->num_decks == 2 ? "LOWER:": "",
1964 mitsu70x_errorclass(jobstatus.error_status),
1965 mitsu70x_errors(jobstatus.error_status),
1966 mitsu70x_errorrecovery(jobstatus.error_status),
1967 jobstatus.error_status[0],
1968 jobstatus.error_status[1],
1969 jobstatus.error_status[2]);
1970 deck &= ~1;
1971 legal &= ~1; /* Deck is offline! */
1972 } else if (jobstatus.mecha_status[0] != MECHA_STATUS_IDLE) {
1973 deck &= ~1;
1974 }
1975 }
1976 if (deck & 2) {
1977 if (jobstatus.temperature_up == TEMPERATURE_COOLING) {
1978 INFO("Upper deck cooling down...\n");
1979 deck &= ~2;
1980 } else if (jobstatus.error_status_up[0]) {
1981 ERROR("UPPER: %s/%s -> %s: %02x/%02x/%02x\n",
1982 mitsu70x_errorclass(jobstatus.error_status_up),
1983 mitsu70x_errors(jobstatus.error_status_up),
1984 mitsu70x_errorrecovery(jobstatus.error_status_up),
1985 jobstatus.error_status_up[0],
1986 jobstatus.error_status_up[1],
1987 jobstatus.error_status_up[2]);
1988 deck &= ~2;
1989 legal &= ~2; /* Deck is offline! */
1990 } else if (jobstatus.mecha_status_up[0] != MECHA_STATUS_IDLE) {
1991 deck &= ~2;
1992 }
1993 }
1994
1995 if (deck == 3) {
1996 /* Both decks OK to use, pick one at random */
1997 if (rand() & 1)
1998 deck = 1;
1999 else
2000 deck = 2;
2001 }
2002
2003 if (ctx->num_decks > 1)
2004 DEBUG("Deck selected: %d\n", deck);
2005
2006 /* Great, we have no decks we can currently print this job on.. */
2007 if (deck == 0) {
2008 /* Halt queue if printer is entirely offline */
2009 if (ctx->num_decks == 2) {
2010 if (jobstatus.error_status[0] && jobstatus.error_status_up[0]) {
2011 ERROR("Both decks offline due to errors\n");
2012 return CUPS_BACKEND_STOP;
2013 }
2014 } else {
2015 if (jobstatus.error_status[0]) {
2016 ERROR("Printer offline due to errors\n");
2017 return CUPS_BACKEND_STOP;
2018 }
2019 }
2020
2021 /* Hold job if we have no legal decks for it, but printer is online. */
2022 if (!legal) {
2023 ERROR("Legal deck for printjob has errors, aborting job\n");
2024 return CUPS_BACKEND_HOLD;
2025 }
2026
2027 /* Legal decks are busy, retry */
2028 sleep(1);
2029 goto top;
2030 }
2031
2032 /* Perform memory status query */
2033 {
2034 struct mitsu70x_memorystatus_resp memory;
2035 INFO("Checking Memory availability\n");
2036
2037 ret = mitsu70x_get_memorystatus(ctx, job, hdr->multicut, &memory);
2038 if (ret)
2039 return CUPS_BACKEND_FAILED;
2040
2041 /* Check size is sane */
2042 if (memory.size || memory.memory == 0xff) {
2043 ERROR("Unsupported print size!\n");
2044 return CUPS_BACKEND_CANCEL;
2045 }
2046 if (memory.memory) {
2047 INFO("Printer buffers full, retrying!\n");
2048 sleep(1);
2049 goto top;
2050 }
2051 }
2052
2053 #if 0
2054 /* Make sure we don't have any jobid collisions */
2055 {
2056 int i;
2057 struct mitsu70x_jobs jobs;
2058
2059 ret = mitsu70x_get_jobs(ctx, &jobs);
2060 if (ret)
2061 return CUPS_BACKEND_FAILED;
2062 for (i = 0 ; i < NUM_JOBS ; i++) {
2063 if (jobs.jobs[0].id == 0)
2064 break;
2065 if (ctx->jobid == be16_to_cpu(jobs.jobs[0].id)) {
2066 ctx->jobid++;
2067 if (!ctx->jobid)
2068 ctx->jobid++;
2069 i = -1;
2070 }
2071 }
2072 }
2073 #endif
2074 while(!ctx->jobid || ctx->jobid == be16_to_cpu(jobstatus.jobid))
2075 ctx->jobid++;
2076
2077 /* Set jobid */
2078 hdr->jobid = cpu_to_be16(ctx->jobid);
2079
2080 /* Set deck */
2081 hdr->deck = deck;
2082
2083 /* K60 and EK305 need the mcut type 1 specified for 4x6 prints! */
2084 if ((ctx->conn->type == P_MITSU_K60 || ctx->conn->type == P_KODAK_305) &&
2085 job->cols == 0x0748 &&
2086 job->rows == 0x04c2 && !hdr->multicut) {
2087 hdr->multicut = 1;
2088 }
2089
2090 /* We're clear to send data over! */
2091 INFO("Sending Print Job (internal id %u)\n", ctx->jobid);
2092
2093 if ((ret = send_data(ctx->conn,
2094 job->databuf,
2095 sizeof(struct mitsu70x_hdr))))
2096 return CUPS_BACKEND_FAILED;
2097
2098 if (job->raw_format == 2) {
2099 /* When we use the library to generate raw data, it needs to be
2100 sent in reverse row order, so use the library to _send_ it too */
2101 if (ctx->lib.SendImageData(&ctx->output, ctx, d70_library_dout_callback))
2102 return CUPS_BACKEND_FAILED;
2103 if (job->matte)
2104 if (d70_library_dout_callback(ctx, job->databuf + job->datalen - job->matte, job->matte))
2105 return CUPS_BACKEND_FAILED;
2106 } else {
2107 /* Pre-rendered jobs are already in the correct row order,
2108 so we can send the data ourselves. Note that K60 and EK305
2109 need data sent in 256K chunks, but the first chunk is
2110 reduced by the length of the 512-byte header */
2111 int chunk = CHUNK_LEN - sizeof(struct mitsu70x_hdr);
2112 int sent = sizeof(struct mitsu70x_hdr);
2113 d70_library_dout_callback(ctx, job->databuf + sent, chunk);
2114 sent += chunk;
2115 d70_library_dout_callback(ctx, job->databuf + sent, job->datalen - sent);
2116 }
2117
2118 /* Then wait for completion, if so desired.. */
2119 INFO("Waiting for printer to acknowledge completion\n");
2120
2121 do {
2122 sleep(1);
2123
2124 ret = mitsu70x_get_printerstatus(ctx, &resp);
2125 if (ret)
2126 return CUPS_BACKEND_FAILED;
2127
2128 ctx->marker[0].levelmax = be16_to_cpu(resp.lower.capacity);
2129 ctx->marker[0].levelnow = be16_to_cpu(resp.lower.remain);
2130 if (ctx->num_decks == 2) {
2131 ctx->marker[1].levelmax = be16_to_cpu(resp.upper.capacity);
2132 ctx->marker[1].levelnow = be16_to_cpu(resp.upper.remain);
2133 }
2134 if (ctx->marker[0].levelnow != ctx->last_l ||
2135 ctx->marker[1].levelnow != ctx->last_u) {
2136 dump_markers(ctx->marker, ctx->num_decks, 0);
2137 ctx->last_l = ctx->marker[0].levelnow;
2138 ctx->last_u = ctx->marker[1].levelnow;
2139 }
2140
2141 /* Query job status for our used jobid */
2142 ret = mitsu70x_get_jobstatus(ctx, &jobstatus, ctx->jobid);
2143 if (ret)
2144 return CUPS_BACKEND_FAILED;
2145
2146 /* See if we hit a printer error. */
2147 if (deck == 1) {
2148 if (jobstatus.error_status[0]) {
2149 ERROR("%s/%s -> %s: %02x/%02x/%02x\n",
2150 mitsu70x_errorclass(jobstatus.error_status),
2151 mitsu70x_errors(jobstatus.error_status),
2152 mitsu70x_errorrecovery(jobstatus.error_status),
2153 jobstatus.error_status[0],
2154 jobstatus.error_status[1],
2155 jobstatus.error_status[2]);
2156
2157 /* Retry job on the other deck.. */
2158 if (ctx->num_decks == 2)
2159 goto top;
2160
2161 return CUPS_BACKEND_STOP;
2162 }
2163 } else if (deck == 2) {
2164 if (jobstatus.error_status_up[0]) {
2165 ERROR("UPPER: %s/%s -> %s: %02x/%02x/%02x\n",
2166 mitsu70x_errorclass(jobstatus.error_status_up),
2167 mitsu70x_errors(jobstatus.error_status_up),
2168 mitsu70x_errorrecovery(jobstatus.error_status_up),
2169 jobstatus.error_status_up[0],
2170 jobstatus.error_status_up[1],
2171 jobstatus.error_status_up[2]);
2172
2173 /* Retry job on the other deck.. */
2174 if (ctx->num_decks == 2)
2175 goto top;
2176
2177 return CUPS_BACKEND_STOP;
2178 }
2179 }
2180
2181 /* Only print if job status is changed */
2182 if (jobstatus.job_status[0] != last_status[0] ||
2183 jobstatus.job_status[1] != last_status[1] ||
2184 jobstatus.job_status[2] != last_status[2] ||
2185 jobstatus.job_status[3] != last_status[3])
2186 INFO("%s: %02x/%02x/%02x/%02x\n",
2187 mitsu70x_jobstatuses(jobstatus.job_status),
2188 jobstatus.job_status[0],
2189 jobstatus.job_status[1],
2190 jobstatus.job_status[2],
2191 jobstatus.job_status[3]);
2192
2193 /* Check for job completion */
2194 if (jobstatus.job_status[0] == JOB_STATUS0_END) {
2195 if (jobstatus.job_status[1] ||
2196 jobstatus.job_status[2] ||
2197 jobstatus.job_status[3]) {
2198 ERROR("Abnormal exit: %02x/%02x/%02x\n",
2199 jobstatus.job_status[1],
2200 jobstatus.job_status[2],
2201 jobstatus.job_status[3]);
2202 return CUPS_BACKEND_STOP;
2203 }
2204 /* Job complete */
2205 break;
2206 }
2207
2208 /* See if we can return early, but wait until printing has started! */
2209 if (!wait_for_return && copies <= 1 && /* Copies generated by backend! */
2210 jobstatus.job_status[0] == JOB_STATUS0_PRINT &&
2211 jobstatus.job_status[1] > JOB_STATUS1_PRINT_MEDIALOAD)
2212 {
2213 INFO("Fast return mode enabled.\n");
2214 break;
2215 }
2216
2217 /* On a two deck system, try to use the second deck
2218 for additional copies. If we can't use it, we'll block. */
2219 if (ctx->num_decks > 1 && copies > 1)
2220 break;
2221
2222 /* Update cache for the next round */
2223 memcpy(last_status, jobstatus.job_status, 4);
2224 } while(1);
2225
2226 /* Clean up */
2227 if (terminate)
2228 copies = 1;
2229
2230 INFO("Print complete (%d copies remaining)\n", copies - 1);
2231
2232 if (copies && --copies) {
2233 goto top;
2234 }
2235
2236 return CUPS_BACKEND_OK;
2237 }
2238
2239 static void mitsu70x_dump_printerstatus(struct mitsu70x_ctx *ctx,
2240 struct mitsu70x_printerstatus_resp *resp)
2241 {
2242 uint32_t i;
2243 uint8_t memory = ~resp->memory;
2244
2245 INFO("Model : ");
2246 if (resp->model[5] == '-')
2247 resp->model[5] = ' ';
2248 for (i = 0 ; i < 6 ; i++) {
2249 DEBUG2("%c", le16_to_cpu(resp->model[i]) & 0x7f);
2250 }
2251 DEBUG2("\n");
2252 INFO("Serial Number : ");
2253 for (i = 0 ; i < 6 ; i++) {
2254 DEBUG2("%c", le16_to_cpu(resp->serno[i]) & 0x7f);
2255 }
2256 DEBUG2("\n");
2257 for (i = 0 ; i < 7 ; i++) {
2258 char buf[7];
2259 char type;
2260 if (resp->vers[i].ver[5] == '@') /* "DUMMY@" */
2261 continue;
2262 memcpy(buf, resp->vers[i].ver, 6);
2263 buf[6] = 0;
2264 if (i == 0) type = 'M'; /* Main */
2265 else if (i == 1) type = 'L'; /* Loader */
2266 else if (i == 2) type = 'R'; /* Tag */
2267 else if (i == 3) type = 'T'; /* Copy */
2268 else if (i == 4) type = 'F'; /* FPGA */
2269 else type = i + 0x30;
2270
2271 INFO("FW Component: %c %s (%04x)\n",
2272 type, buf, be16_to_cpu(resp->vers[i].checksum));
2273 }
2274 INFO("Standby Timeout: %d minutes\n", resp->sleeptime);
2275 INFO("iSerial Reporting: %s\n", resp->iserial ? "No" : "Yes" );
2276 INFO("Power Status: %s\n", resp->power ? "Sleeping" : "Awake");
2277 INFO("Available Memory Banks: %s%s%s%s%s%s%s%s\n",
2278 (memory & 0x01) ? "mem8 " : "",
2279 (memory & 0x02) ? "mem7 " : "",
2280 (memory & 0x04) ? "mem6 " : "",
2281 (memory & 0x08) ? "mem5 " : "",
2282 (memory & 0x10) ? "mem4 " : "",
2283 (memory & 0x20) ? "mem3 " : "",
2284 (memory & 0x40) ? "mem2 " : "",
2285 (memory & 0x80) ? "mem1 " : "");
2286
2287 if (resp->lower.error_status[0]) {
2288 INFO("Lower Error Status: %s/%s -> %s\n",
2289 mitsu70x_errorclass(resp->lower.error_status),
2290 mitsu70x_errors(resp->lower.error_status),
2291 mitsu70x_errorrecovery(resp->lower.error_status));
2292 }
2293 INFO("Lower Temperature: %s\n", mitsu_temperatures(resp->lower.temperature));
2294 INFO("Lower Mechanical Status: %s\n",
2295 mitsu70x_mechastatus(resp->lower.mecha_status));
2296 INFO("Lower Media Type: %s (%02x/%02x/%02x)\n",
2297 mitsu_media_types(ctx->conn->type, resp->lower.media_brand, resp->lower.media_type),
2298 resp->lower.media_brand,
2299 resp->lower.media_type,
2300 resp->lower.media_subtype);
2301 INFO("Lower Prints Remaining: %03d/%03d\n",
2302 be16_to_cpu(resp->lower.remain),
2303 be16_to_cpu(resp->lower.capacity));
2304 i = packed_bcd_to_uint32((char*)resp->lower.lifetime_prints, 4);
2305 if (i)
2306 i-= 10;
2307 INFO("Lower Lifetime Prints: %u\n", i);
2308
2309 if (ctx->num_decks == 2) {
2310 if (resp->upper.error_status[0]) {
2311 INFO("Upper Error Status: %s/%s -> %s\n",
2312 mitsu70x_errorclass(resp->upper.error_status),
2313 mitsu70x_errors(resp->upper.error_status),
2314 mitsu70x_errorrecovery(resp->upper.error_status));
2315 }
2316 INFO("Upper Temperature: %s\n", mitsu_temperatures(resp->upper.temperature));
2317 INFO("Upper Mechanical Status: %s\n",
2318 mitsu70x_mechastatus(resp->upper.mecha_status));
2319 INFO("Upper Media Type: %s (%02x/%02x/%02x)\n",
2320 mitsu_media_types(ctx->conn->type, resp->upper.media_brand, resp->upper.media_type),
2321 resp->upper.media_brand,
2322 resp->upper.media_type,
2323 resp->upper.media_subtype);
2324 INFO("Upper Prints Remaining: %03d/%03d\n",
2325 be16_to_cpu(resp->upper.remain),
2326 be16_to_cpu(resp->upper.capacity));
2327
2328 i = packed_bcd_to_uint32((char*)resp->upper.lifetime_prints, 4);
2329 if (i)
2330 i-= 10;
2331 INFO("Upper Lifetime Prints: %u\n", i);
2332 }
2333 }
2334
2335 static int mitsu70x_query_jobs(struct mitsu70x_ctx *ctx)
2336 {
2337 #if 0
2338 struct mitsu70x_jobs jobs;
2339 #endif
2340 struct mitsu70x_jobstatus jobstatus;
2341 int ret;
2342
2343 ret = mitsu70x_get_jobstatus(ctx, &jobstatus, 0x0000);
2344 if (ret)
2345 return CUPS_BACKEND_FAILED;
2346
2347 INFO("JOB00 ID : %06u\n", jobstatus.jobid);
2348 INFO("JOB00 status : %s\n", mitsu70x_jobstatuses(jobstatus.job_status));
2349 INFO("Power Status: %s\n", jobstatus.power ? "Sleeping" : "Awake");
2350
2351 if (ctx->num_decks == 2) {
2352 INFO("Lower Deck Mechanical Status: %s\n",
2353 mitsu70x_mechastatus(jobstatus.mecha_status));
2354 if (jobstatus.error_status[0]) {
2355 INFO("%s/%s -> %s\n",
2356 mitsu70x_errorclass(jobstatus.error_status),
2357 mitsu70x_errors(jobstatus.error_status),
2358 mitsu70x_errorrecovery(jobstatus.error_status));
2359 }
2360 INFO("Lower Deck Temperature: %s\n", mitsu_temperatures(jobstatus.temperature));
2361
2362 INFO("Upper Deck Mechanical Status: %s\n",
2363 mitsu70x_mechastatus(jobstatus.mecha_status_up));
2364 if (jobstatus.error_status_up[0]) {
2365 INFO("%s/%s -> %s\n",
2366 mitsu70x_errorclass(jobstatus.error_status_up),
2367 mitsu70x_errors(jobstatus.error_status_up),
2368 mitsu70x_errorrecovery(jobstatus.error_status_up));
2369 }
2370 INFO("Upper Deck Temperature: %s\n", mitsu_temperatures(jobstatus.temperature_up));
2371 } else {
2372 INFO("Mechanical Status: %s\n",
2373 mitsu70x_mechastatus(jobstatus.mecha_status));
2374 if (jobstatus.error_status[0]) {
2375 INFO("%s/%s -> %s\n",
2376 mitsu70x_errorclass(jobstatus.error_status),
2377 mitsu70x_errors(jobstatus.error_status),
2378 mitsu70x_errorrecovery(jobstatus.error_status));
2379 }
2380 INFO("Temperature: %s\n", mitsu_temperatures(jobstatus.temperature));
2381 }
2382
2383 // memory status?
2384
2385 #if 0
2386 ret = mitsu70x_get_jobs(ctx, &jobs);
2387 if (!ret) {
2388 int i;
2389 for (i = 0 ; i < NUM_JOBS ; i++) {
2390 if (jobs.jobs[i].id == 0)
2391 break;
2392 INFO("JOB%02d ID : %06u\n", i, jobs.jobs[i].id);
2393 INFO("JOB%02d status : %s\n", i, mitsu70x_jobstatuses(jobs.jobs[i].status));
2394 }
2395 }
2396
2397 done:
2398 #endif
2399 return CUPS_BACKEND_OK;
2400 }
2401
2402 static int mitsu70x_query_status(struct mitsu70x_ctx *ctx)
2403 {
2404 struct mitsu70x_printerstatus_resp resp;
2405 int ret;
2406
2407 ret = mitsu70x_get_printerstatus(ctx, &resp);
2408 if (!ret)
2409 mitsu70x_dump_printerstatus(ctx, &resp);
2410
2411 return ret;
2412 }
2413
2414 static int mitsu70x_query_serno(struct dyesub_connection *conn, char *buf, int buf_len)
2415 {
2416 int ret, i;
2417 struct mitsu70x_printerstatus_resp resp = { .hdr = { 0 } };
2418
2419 struct mitsu70x_ctx ctx = {
2420 .conn = conn,
2421 };
2422
2423 ret = mitsu70x_get_printerstatus(&ctx, &resp);
2424
2425 if (buf_len > 6) /* Will we ever have a buffer under 6 bytes? */
2426 buf_len = 6;
2427
2428 for (i = 0 ; i < buf_len ; i++) {
2429 *buf++ = le16_to_cpu(resp.serno[i]) & 0x7f;
2430 }
2431 *buf = 0; /* Null-terminate the returned string */
2432
2433 return ret;
2434 }
2435
2436
2437 static void mitsu70x_cmdline(void)
2438 {
2439 DEBUG("\t\t[ -s ] # Query printer status\n");
2440 DEBUG("\t\t[ -j ] # Query job status\n");
2441 DEBUG("\t\t[ -w ] # Wake up printer\n");
2442 DEBUG("\t\t[ -W ] # Wake up printer and wait\n");
2443 DEBUG("\t\t[ -k num ] # Set standby time (1-60 minutes, 0 disables)\n");
2444 DEBUG("\t\t[ -x num ] # Set USB iSerialNumber Reporting (1 on, 0 off)\n");
2445 DEBUG("\t\t[ -X jobid ] # Abort a printjob\n");
2446 // DEBUG("\t\t[ -t ] # Dump calibration info (use with -DDD)\n");
2447 // DEBUG("\t\t[ -T 0-6 ] # Test print\n");
2448 }
2449
2450 static int mitsu70x_cmdline_arg(void *vctx, int argc, char **argv)
2451 {
2452 struct mitsu70x_ctx *ctx = vctx;
2453 int i, j = 0;
2454
2455 if (!ctx)
2456 return -1;
2457
2458 while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "jk:tT:swWX:x:")) >= 0) {
2459 switch(i) {
2460 GETOPT_PROCESS_GLOBAL
2461 case 'j':
2462 j = mitsu70x_query_jobs(ctx);
2463 break;
2464 case 'k':
2465 j = mitsu70x_set_sleeptime(ctx, atoi(optarg));
2466 break;
2467 case 's':
2468 j = mitsu70x_query_status(ctx);
2469 break;
2470 case 't':
2471 j = mitsu70x_test_dump(ctx);
2472 break;
2473 case 'T':
2474 j = mitsu70x_test_print(ctx, atoi(optarg));
2475 break;
2476 case 'w':
2477 j = mitsu70x_wakeup(ctx, 0);
2478 break;
2479 case 'W':
2480 j = mitsu70x_wakeup(ctx, 1);
2481 break;
2482 case 'x':
2483 j = mitsu70x_set_iserial(ctx, atoi(optarg));
2484 break;
2485 case 'X':
2486 j = mitsu70x_cancel_job(ctx, atoi(optarg));
2487 break;
2488 default:
2489 break; /* Ignore completely */
2490 }
2491
2492 if (j) return j;
2493 }
2494
2495 return CUPS_BACKEND_OK;
2496 }
2497
2498 static int mitsu70x_query_markers(void *vctx, struct marker **markers, int *count)
2499 {
2500 struct mitsu70x_ctx *ctx = vctx;
2501 struct mitsu70x_printerstatus_resp resp;
2502 int ret;
2503
2504 if (markers)
2505 *markers = ctx->marker;
2506 if (count)
2507 *count = ctx->num_decks;
2508
2509 /* Tell CUPS about the consumables we report */
2510 ret = mitsu70x_get_printerstatus(ctx, &resp);
2511 if (ret)
2512 return CUPS_BACKEND_FAILED;
2513
2514 if (resp.power) {
2515 ret = mitsu70x_wakeup(ctx, 1);
2516 if (ret)
2517 return CUPS_BACKEND_FAILED;
2518
2519 ret = mitsu70x_get_printerstatus(ctx, &resp);
2520 if (ret)
2521 return CUPS_BACKEND_FAILED;
2522 }
2523
2524 ctx->marker[0].levelmax = be16_to_cpu(resp.lower.capacity);
2525 ctx->marker[0].levelnow = be16_to_cpu(resp.lower.remain);
2526 if (ctx->num_decks == 2) {
2527 ctx->marker[1].levelmax = be16_to_cpu(resp.upper.capacity);
2528 ctx->marker[1].levelnow = be16_to_cpu(resp.upper.remain);
2529 }
2530
2531 return CUPS_BACKEND_OK;
2532 }
2533
2534 static int mitsu70x_job_polarity(void *vctx)
2535 {
2536 struct mitsu70x_ctx *ctx = vctx;
2537
2538 if (test_mode >= TEST_MODE_NOATTACH)
2539 return 0;
2540
2541 if (mitsu70x_query_markers(ctx, NULL, NULL))
2542 return 0;
2543
2544 /* D70x and ASK300 don't support rewinding */
2545 if (ctx->conn->type == P_MITSU_D70X ||
2546 ctx->conn->type == P_FUJI_ASK300)
2547 return 0;
2548
2549 /* All others do, and only have one deck */
2550 return (ctx->marker[0].levelnow & 1);
2551 }
2552
2553 static int mitsu70x_query_stats(void *vctx, struct printerstats *stats)
2554 {
2555 struct mitsu70x_ctx *ctx = vctx;
2556 struct mitsu70x_printerstatus_resp resp;
2557
2558 if (mitsu70x_query_markers(ctx, NULL, NULL))
2559 return CUPS_BACKEND_FAILED;
2560
2561 if (mitsu70x_get_printerstatus(ctx, &resp))
2562 return CUPS_BACKEND_FAILED;
2563
2564 switch (ctx->conn->type) {
2565 case P_MITSU_D70X:
2566 stats->mfg = "Mitsubishi";
2567 if (ctx->num_decks == 2) {
2568 if (ctx->is_s)
2569 stats->model = "CP-D707DW-S";
2570 else
2571 stats->model = "CP-D707DW";
2572 } else {
2573 if (ctx->is_s)
2574 stats->model = "CP-D70DW-S";
2575 else
2576 stats->model = "CP-D70DW";
2577 }
2578 break;
2579 case P_MITSU_K60:
2580 stats->mfg = "Mitsubishi";
2581 stats->model = "CP-K60DW-S";
2582 // XXX does is_s matter? Mitsu SDK cares, but all models sold are labeled as -S..
2583 break;
2584 case P_MITSU_D80:
2585 stats->mfg = "Mitsubishi";
2586 if (ctx->is_s)
2587 stats->model = "CP-D80DW-S";
2588 else
2589 stats->model = "CP-D80DW";
2590 break;
2591 case P_KODAK_305:
2592 stats->mfg = "Kodak";
2593 stats->model = "305";
2594 break;
2595 case P_FUJI_ASK300:
2596 stats->mfg = "Fujifilm";
2597 stats->model = "ASK-300";
2598 break;
2599 default:
2600 stats->mfg = "Unknown";
2601 stats->model = "Unknown";
2602 break;
2603 }
2604
2605 stats->serial = ctx->serno;
2606 stats->fwver = ctx->fwver;
2607 stats->decks = ctx->num_decks;
2608
2609 stats->name[0] = "Lower";
2610 stats->status[0] = strdup(mitsu70x_mechastatus(resp.lower.mecha_status));
2611 stats->mediatype[0] = ctx->marker[0].name;
2612 stats->levelmax[0] = ctx->marker[0].levelmax;
2613 stats->levelnow[0] = ctx->marker[0].levelnow;
2614 stats->cnt_life[0] = packed_bcd_to_uint32((char*)resp.lower.lifetime_prints, 4);
2615
2616 if (stats->decks == 2) {
2617 stats->name[1] = "Upper";
2618 stats->status[1] = strdup(mitsu70x_mechastatus(resp.upper.mecha_status));
2619 stats->mediatype[1] = ctx->marker[1].name;
2620 stats->levelmax[1] = ctx->marker[1].levelmax;
2621 stats->levelnow[1] = ctx->marker[1].levelnow;
2622 stats->cnt_life[1] = packed_bcd_to_uint32((char*)resp.upper.lifetime_prints, 4);
2623 }
2624 return CUPS_BACKEND_OK;
2625 }
2626
2627 static const char *mitsu70x_prefixes[] = {
2628 "mitsu70x", // Family entry, do not nuke.
2629 // backwards compatibility
2630 "mitsud80", "mitsuk60", "kodak305", "fujiask300",
2631 NULL,
2632 };
2633
2634 static const struct device_id mitsu70x_devices[] = {
2635 { 0x06d3, 0x3b30, P_MITSU_D70X, NULL, "mitsubishi-d70dw"},
2636 { 0x06d3, 0x3b30, P_MITSU_D70X, NULL, "mitsubishi-d707dw"}, /* Duplicate */
2637 { 0x06d3, 0x3b31, P_MITSU_K60, NULL, "mitsubishi-k60dw"}, // variation type?
2638 { 0x06d3, 0x3b36, P_MITSU_D80, NULL, "mitsubishi-d80dw"},
2639 { 0x040a, 0x404f, P_KODAK_305, NULL, "kodak-305"},
2640 { 0x04cb, 0x5006, P_FUJI_ASK300, NULL, "fujifilm-ask-300"},
2641 { 0x1452, 0x8e01, P_DNP_DSX80, NULL, "dnp-ds680"},
2642 { 0x1452, 0x8f01, P_DNP_DSX80, NULL, "dnp-ds480"},
2643 { 0, 0, 0, NULL, NULL}
2644 };
2645
2646 /* Exported */
2647 const struct dyesub_backend mitsu70x_backend = {
2648 .name = "Mitsubishi CP-D70 family",
2649 .version = "0.115" " (lib " LIBMITSU_VER ")",
2650 .flags = BACKEND_FLAG_DUMMYPRINT,
2651 .uri_prefixes = mitsu70x_prefixes,
2652 .devices = mitsu70x_devices,
2653 .cmdline_usage = mitsu70x_cmdline,
2654 .cmdline_arg = mitsu70x_cmdline_arg,
2655 .init = mitsu70x_init,
2656 .attach = mitsu70x_attach,
2657 .teardown = mitsu70x_teardown,
2658 .cleanup_job = mitsu70x_cleanup_job,
2659 .read_parse = mitsu70x_read_parse,
2660 .main_loop = mitsu70x_main_loop,
2661 .query_serno = mitsu70x_query_serno,
2662 .query_markers = mitsu70x_query_markers,
2663 .query_stats = mitsu70x_query_stats,
2664 .combine_jobs = mitsu70x_combine_jobs,
2665 .job_polarity = mitsu70x_job_polarity,
2666 };
2667
2668 /* Mitsubish CP-D70DW/D707DW/K60DW-S/D80DW, Kodak 305, Fujifilm ASK-300
2669 data format:
2670
2671 Spool file consists of two headers followed by three image planes
2672 and an optional lamination data plane. All blocks are rounded up to
2673 a 512-byte boundary.
2674
2675 All multi-byte numbers are big endian, ie MSB first.
2676
2677 Header 1: (AKA Wake Up)
2678
2679 1b 45 57 55 00 00 00 00 00 00 00 00 00 00 00 00
2680 (padded by NULLs to a 512-byte boundary)
2681
2682 Header 2: (Print Header)
2683
2684 1b 5a 54 PP JJ JJ RR RR 00 00 00 00 00 00 00 00
2685 XX XX YY YY QQ QQ ZZ ZZ SS 00 00 00 00 00 00 00
2686 UU 00 00 00 00 00 00 00 LL TT 00 00 00 00 00 00
2687 MM 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2688
2689 (padded by NULLs to a 512-byte boundary)
2690
2691 PP == 0x01 on D70x/D80, 0x00 on K60, 0x90 on K305, 0x80 on ASK300, 0x04 on DSx80
2692 JJ JJ == Job ID, can leave at 00 00
2693 XX XX == columns
2694 YY YY == rows
2695 QQ QQ == lamination columns (equal to XX XX)
2696 ZZ ZZ == lamination rows (YY YY + 12 on D70x/D80/ASK300, YY YY on others)
2697 RR RR == "rewind inhibit", 01 01 enabled, normally 00 00 (All but D70x/A300)
2698 SS == Print mode: 00 = Fine, 03 = SuperFine (D70x/D80/DSx80 only), 04 = UltraFine
2699 (Matte requires Superfine or Ultrafine)
2700 UU == 00 = Auto, 01 = Lower Deck (required for !D70x), 02 = Upper Deck
2701 LL == lamination enable, 00 == on, 01 == off
2702 TT == lamination mode: 00 glossy, 02 matte
2703 MM == 00 (normal), 01 = (Double-cut 4x6), 05 = (double-cut 2x6)
2704
2705 Data planes:
2706 16-bit data, rounded up to 512-byte block (XX * YY * 2 bytes)
2707
2708 Lamination plane: (only present if QQ and ZZ are nonzero)
2709 16-byte data, rounded up to 512-byte block (QQ * ZZ * 2 bytes)
2710
2711 */