summaryrefslogtreecommitdiff
path: root/src/cups/backend_canonselphyneo.c
blob: 93f539d2f23943fcfa1b91f7624196356f3d5c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
 *   Canon SELPHY CPneo series CUPS backend -- libusb-1.0 version
 *
 *   (c) 2016-2018 Solomon Peachy <pizza@shaftnet.org>
 *
 *   The latest version of this program can be found at:
 *
 *     http://git.shaftnet.org/cgit/selphy_print.git
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *   for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 *          [http://www.gnu.org/licenses/gpl-2.0.html]
 *
 *   SPDX-License-Identifier: GPL-2.0+
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

#define BACKEND canonselphyneo_backend

#include "backend_common.h"

/* Exported */
#define USB_VID_CANON        0x04a9
#define USB_PID_CANON_CP820  0x327b
#define USB_PID_CANON_CP910  0x327a
#define USB_PID_CANON_CP1000 0x32ae
#define USB_PID_CANON_CP1200 0x32b1
#define USB_PID_CANON_CP1300 0x32db

/* Header data structure */
struct selphyneo_hdr {
	uint8_t data[24];
	uint32_t cols;  /* LE */
	uint32_t rows;  /* LE */
} __attribute((packed));

/* Readback data structure */
struct selphyneo_readback {
	uint8_t data[12];
} __attribute((packed));

/* Private data structure */
struct selphyneo_printjob {
	uint8_t *databuf;
	uint32_t datalen;

	int copies;
};

struct selphyneo_ctx {
	struct libusb_device_handle *dev;
	uint8_t endp_up;
	uint8_t endp_down;
	int type;

	struct marker marker;
};

static char *selphyneo_statuses(uint8_t sts)
{
	switch(sts) {
	case 0x01:
		return "Idle";
	case 0x02:
		return "Feeding Paper";
	case 0x04:
		return "Printing YELLOW";
	case 0x08:
		return "Printing MAGENTA";
	case 0x10:
		return "Printing CYAN";
	case 0x20:
		return "Printing LAMINATE";
	default:
		return "Unknown state!";
	}
}

static char *selphyneo_errors(uint8_t err)
{
	switch(err) {
	case 0x00:
		return "None";
	case 0x02:
		return "Paper Feed";
	case 0x03:
		return "No Paper";
	case 0x05:
		return "Incorrect Paper loaded";
	case 0x06:
		return "Ink Cassette Empty";
	case 0x07:
		return "No Ink";
	case 0x09:
		return "No Paper and Ink";
	case 0x0A:
		return "Incorrect media for job";
	case 0x0B:
		return "Paper jam";
	default:
		return "Unknown Error";
	}
}

static char *selphynew_pgcodes(uint8_t type) {

	switch (type & 0xf) {
	case 0x01:
		return "P";
	case 0x02:
		return "L";
	case 0x03:
		return "C";
	case 0x00:
		return "None";
	default:
		return "Unknown";
	}
}

static int selphyneo_send_reset(struct selphyneo_ctx *ctx)
{
	uint8_t rstcmd[12] = { 0x40, 0x10, 0x00, 0x00,
			       0x00, 0x00, 0x00, 0x00,
			       0x00, 0x00, 0x00, 0x00 };
	int ret;

	if ((ret = send_data(ctx->dev, ctx->endp_down,
			     rstcmd, sizeof(rstcmd))))
		return CUPS_BACKEND_FAILED;

	return CUPS_BACKEND_OK;
}

static int selphyneo_get_status(struct selphyneo_ctx *ctx)
{
	struct selphyneo_readback rdback;
	int ret, num;

	/* Read in the printer status to clear last state */
	ret = read_data(ctx->dev, ctx->endp_up,
			(uint8_t*) &rdback, sizeof(rdback), &num);

	if (ret < 0)
		return CUPS_BACKEND_FAILED;

	/* And again, for the markers */
	ret = read_data(ctx->dev, ctx->endp_up,
			(uint8_t*) &rdback, sizeof(rdback), &num);

	if (ret < 0)
		return CUPS_BACKEND_FAILED;

	INFO("Printer state: %s\n", selphyneo_statuses(rdback.data[0]));
	INFO("Media type: %s\n", selphynew_pgcodes(rdback.data[6]));
	if (rdback.data[2]) {
		INFO("Printer error: %s\n", selphyneo_errors(rdback.data[2]));
	}

	return CUPS_BACKEND_OK;
}

static void *selphyneo_init(void)
{
	struct selphyneo_ctx *ctx = malloc(sizeof(struct selphyneo_ctx));
	if (!ctx) {
		ERROR("Memory Allocation Failure!\n");
		return NULL;
	}
	memset(ctx, 0, sizeof(struct selphyneo_ctx));

	return ctx;
}

extern struct dyesub_backend selphyneo_backend;

static int selphyneo_attach(void *vctx, struct libusb_device_handle *dev, int type,
			    uint8_t endp_up, uint8_t endp_down, uint8_t jobid)
{
	struct selphyneo_ctx *ctx = vctx;
	struct selphyneo_readback rdback;
	int ret, num;

	UNUSED(jobid);

	ctx->dev = dev;
	ctx->endp_up = endp_up;
	ctx->endp_down = endp_down;
	ctx->type = type;

	if (test_mode < TEST_MODE_NOATTACH) {
		/* Read in the printer status to clear last state */
		ret = read_data(ctx->dev, ctx->endp_up,
				(uint8_t*) &rdback, sizeof(rdback), &num);

		if (ret < 0)
			return CUPS_BACKEND_FAILED;

		/* And again, for the markers */
		ret = read_data(ctx->dev, ctx->endp_up,
				(uint8_t*) &rdback, sizeof(rdback), &num);

		if (ret < 0)
			return CUPS_BACKEND_FAILED;
	} else {
		rdback.data[2] = 0;
		rdback.data[6] = 0x01;
		if (getenv("MEDIA_CODE"))
			rdback.data[6] = atoi(getenv("MEDIA_CODE"));
	}

	ctx->marker.color = "#00FFFF#FF00FF#FFFF00";
	ctx->marker.name = selphynew_pgcodes(rdback.data[6]);
	ctx->marker.levelmax = -1;
	if (rdback.data[2]) {
		ctx->marker.levelnow = 0;
	} else {
		ctx->marker.levelnow = -3;
	}

	return CUPS_BACKEND_OK;
}

static void selphyneo_cleanup_job(const void *vjob) {
	const struct selphyneo_printjob *job = vjob;

	if (job->databuf)
		free(job->databuf);

	free((void*)job);
}

static void selphyneo_teardown(void *vctx) {
	struct selphyneo_ctx *ctx = vctx;

	if (!ctx)
		return;

	free(ctx);
}

static int selphyneo_read_parse(void *vctx, const void **vjob, int data_fd, int copies)
{
	struct selphyneo_ctx *ctx = vctx;
	struct selphyneo_hdr hdr;
	int i, remain;

	struct selphyneo_printjob *job = NULL;

	if (!ctx)
		return CUPS_BACKEND_FAILED;

	job = malloc(sizeof(*job));
	if (!job) {
		ERROR("Memory allocation failure!\n");
		return CUPS_BACKEND_RETRY_CURRENT;
	}
	memset(job, 0, sizeof(*job));
	job->copies = copies;

	/* Read the header.. */
	i = read(data_fd, &hdr, sizeof(hdr));
	if (i != sizeof(hdr)) {
		if (i == 0) {
			selphyneo_cleanup_job(job);
			return CUPS_BACKEND_CANCEL;
		}
		ERROR("Read failed (%d/%d)\n",
		      i, (int)sizeof(hdr));
		perror("ERROR: Read failed");
		selphyneo_cleanup_job(job);
		return CUPS_BACKEND_FAILED;
	}

	/* Determine job length */
	switch(hdr.data[18]) {
	case 0x50:  /* P */
	case 0x4c:  /* L */
	case 0x43:  /* C */
		remain = le32_to_cpu(hdr.cols) * le32_to_cpu(hdr.rows) * 3;
		break;
	default:
		ERROR("Unknown print size! (%02x, %ux%u)\n",
		      hdr.data[10], le32_to_cpu(hdr.cols), le32_to_cpu(hdr.rows));
		selphyneo_cleanup_job(job);
		return CUPS_BACKEND_CANCEL;
	}

	// XXX Sanity check job against loaded media?

	/* Allocate a buffer */
	job->datalen = 0;
	job->databuf = malloc(remain + sizeof(hdr));
	if (!job->databuf) {
		ERROR("Memory allocation failure!\n");
		selphyneo_cleanup_job(job);
		return CUPS_BACKEND_RETRY_CURRENT;
	}

	/* Store the read-in header */
	memcpy(job->databuf, &hdr, sizeof(hdr));
	job->datalen += sizeof(hdr);

	/* Read in data */
	while (remain > 0) {
		i = read(data_fd, job->databuf + job->datalen, remain);
		if (i < 0) {
			selphyneo_cleanup_job(job);
			return CUPS_BACKEND_CANCEL;
		}
		remain -= i;
		job->datalen += i;
	}

	*vjob = job;

	return CUPS_BACKEND_OK;
}

static int selphyneo_main_loop(void *vctx, const void *vjob) {
	struct selphyneo_ctx *ctx = vctx;
	struct selphyneo_readback rdback;

	int ret, num;
	int copies;

	const struct selphyneo_printjob *job = vjob;

	if (!ctx)
		return CUPS_BACKEND_FAILED;
	if (!job)
		return CUPS_BACKEND_FAILED;

	copies = job->copies;

	/* Read in the printer status to clear last state */
	ret = read_data(ctx->dev, ctx->endp_up,
			(uint8_t*) &rdback, sizeof(rdback), &num);

	if (ret < 0)
		return CUPS_BACKEND_FAILED;

top:
	INFO("Waiting for printer idle\n");

	do {
		ret = read_data(ctx->dev, ctx->endp_up,
				(uint8_t*) &rdback, sizeof(rdback), &num);

		if (ret < 0)
			return CUPS_BACKEND_FAILED;

		if (rdback.data[0] == 0x01)
			break;

		INFO("Printer state: %s\n", selphyneo_statuses(rdback.data[0]));

		switch (rdback.data[2]) {
		case 0x00:
			break;
		case 0x0A:
			ERROR("Printer error: %s (%02x)\n", selphyneo_errors(rdback.data[2]), rdback.data[2]);
			ctx->marker.levelnow = 0;
			dump_markers(&ctx->marker, 1, 0);
			return CUPS_BACKEND_CANCEL;
		default:
			ERROR("Printer error: %s (%02x)\n", selphyneo_errors(rdback.data[2]), rdback.data[2]);
			ctx->marker.levelnow = 0;
			dump_markers(&ctx->marker, 1, 0);
			return CUPS_BACKEND_STOP;
		}

		sleep(1);
	} while(1);

	dump_markers(&ctx->marker, 1, 0);

	INFO("Sending spool data\n");
	/* Send the data over in 256K chunks */
	{
		int chunk = 256*1024;
		int sent = 0;
		while (chunk > 0) {
			if ((ret = send_data(ctx->dev, ctx->endp_down,
					     job->databuf + sent, chunk)))
				return CUPS_BACKEND_FAILED;
			sent += chunk;
			chunk = job->datalen - sent;
			if (chunk > 256*1024)
				chunk = 256*1024;
		}
	}

	/* Read in the printer status to clear last state */
	ret = read_data(ctx->dev, ctx->endp_up,
			(uint8_t*) &rdback, sizeof(rdback), &num);

	if (ret < 0)
		return CUPS_BACKEND_FAILED;

	INFO("Waiting for printer acknowledgement\n");
	do {
		ret = read_data(ctx->dev, ctx->endp_up,
				(uint8_t*) &rdback, sizeof(rdback), &num);

		if (ret < 0)
			return CUPS_BACKEND_FAILED;

		if (rdback.data[0] == 0x01)
			break;

		INFO("Printer state: %s\n", selphyneo_statuses(rdback.data[0]));

		switch (rdback.data[2]) {
		case 0x00:
			break;
		case 0x0A:
			ERROR("Printer error: %s (%02x)\n", selphyneo_errors(rdback.data[2]), rdback.data[2]);
			ctx->marker.levelnow = 0;
			dump_markers(&ctx->marker, 1, 0);
			return CUPS_BACKEND_CANCEL;
		default:
			ERROR("Printer error: %s (%02x)\n", selphyneo_errors(rdback.data[2]), rdback.data[2]);
			ctx->marker.levelnow = 0;
			dump_markers(&ctx->marker, 1, 0);
			return CUPS_BACKEND_STOP;
		}

		if (rdback.data[0] > 0x02 && fast_return && copies <= 1) {
			INFO("Fast return mode enabled.\n");
			break;
		}

		sleep(1);
	} while(1);

	/* Clean up */
	if (terminate)
		copies = 1;

	INFO("Print complete (%d copies remaining)\n", copies - 1);

	if (copies && --copies) {
		goto top;
	}

	return CUPS_BACKEND_OK;
}

static int selphyneo_cmdline_arg(void *vctx, int argc, char **argv)
{
	struct selphyneo_ctx *ctx = vctx;
	int i, j = 0;

	if (!ctx)
		return -1;

	while ((i = getopt(argc, argv, GETOPT_LIST_GLOBAL "Rs")) >= 0) {
		switch(i) {
		GETOPT_PROCESS_GLOBAL
		case 'R':
			selphyneo_send_reset(ctx);
			break;
		case 's':
			selphyneo_get_status(ctx);
			break;
		}

		if (j) return j;
	}

	return 0;
}

static void selphyneo_cmdline(void)
{
	DEBUG("\t\t[ -R ]           # Reset printer\n");
	DEBUG("\t\t[ -s ]           # Query printer status\n");
}

static int selphyneo_query_markers(void *vctx, struct marker **markers, int *count)
{
	struct selphyneo_ctx *ctx = vctx;
	struct selphyneo_readback rdback;
	int ret, num;

	/* Read in the printer status to clear last state */
	ret = read_data(ctx->dev, ctx->endp_up,
			(uint8_t*) &rdback, sizeof(rdback), &num);

	if (ret < 0)
		return CUPS_BACKEND_FAILED;

	/* And again, for the markers */
	ret = read_data(ctx->dev, ctx->endp_up,
			(uint8_t*) &rdback, sizeof(rdback), &num);

	if (ret < 0)
		return CUPS_BACKEND_FAILED;

	if (rdback.data[2])
		ctx->marker.levelnow = 0;
	else
		ctx->marker.levelnow = -3;

	*markers = &ctx->marker;
	*count = 1;

	return CUPS_BACKEND_OK;
}

static const char *canonselphyneo_prefixes[] = {
	"canonselphyneo", // Family name
	"canon-cp820", "canon-cp910", "canon-cp1000", "canon-cp1200", "canon-cp1300",
	// backwards compatibility
	"selphycp820", "selphycp910", "selphycp1000", "selphycp1200", "selphycp1300",
	NULL
};

struct dyesub_backend canonselphyneo_backend = {
	.name = "Canon SELPHY CP (new)",
	.version = "0.20",
	.uri_prefixes = canonselphyneo_prefixes,
	.cmdline_usage = selphyneo_cmdline,
	.cmdline_arg = selphyneo_cmdline_arg,
	.init = selphyneo_init,
	.attach = selphyneo_attach,
	.cleanup_job = selphyneo_cleanup_job,
	.teardown = selphyneo_teardown,
	.read_parse = selphyneo_read_parse,
	.main_loop = selphyneo_main_loop,
	.query_markers = selphyneo_query_markers,
	.devices = {
		{ USB_VID_CANON, USB_PID_CANON_CP820, P_CP910, NULL, "canon-cp820"},
		{ USB_VID_CANON, USB_PID_CANON_CP910, P_CP910, NULL, "canon-cp910"},
		{ USB_VID_CANON, USB_PID_CANON_CP1000, P_CP910, NULL, "canon-cp1000"},
		{ USB_VID_CANON, USB_PID_CANON_CP1200, P_CP910, NULL, "canon-cp1200"},
		{ USB_VID_CANON, USB_PID_CANON_CP1300, P_CP910, NULL, "canon-cp1300"},
		{ 0, 0, 0, NULL, NULL}
	}
};
/*

 ***************************************************************************

	Stream formats and readback codes for supported printers

 ***************************************************************************
 Selphy CP820/CP910/CP1000/CP1200:

  Radically different spool file format!  300dpi, same print sizes, but also
  adding a 50x50mm sticker and 22x17.3mm ministickers, though I think the
  driver treats all of those as 'C' sizes for printing purposes.

  32-byte header:

  0f 00 00 40 00 00 00 00  00 00 00 00 00 00 01 00
  01 00 TT 00 00 00 00 ZZ  XX XX XX XX YY YY YY YY

                           cols (le32) rows (le32)
        50                 e0 04       50 07          1248 * 1872  (P)
        4c                 80 04       c0 05          1152 * 1472  (L)
        43                 40 04       9c 02          1088 * 668   (C)

  TT == 50  (P)
     == 4c  (L)
     == 43  (C)

  ZZ == 00  Y'CbCr data follows
     == 01  CMY    data follows

  Followed by three planes of image data.

  P == 7008800  == 2336256 * 3 + 32 (1872*1248)
  L == 5087264  == 1695744 * 3 + 32 (1472*1152)
  C == 2180384  == 726784 * 3 + 32  (1088*668)

  It is worth mentioning that the Y'CbCr image data is surmised to use the
  JPEG coefficients, although we realistically have no way of confirming this.

  Other questions:

    * Printer supports different lamination types, how to control?

 Data Readback:

 XX 00 YY 00  00 00 ZZ 00 00 00 00 00

 XX == Status

   01  Idle
   02  Feeding Paper
   04  Printing Y
   08  Printing M
   10  Printing C
   20  Printing L

 YY == Error

   00  None
   02  No Paper (?)
   03  No Paper
   05  Wrong Paper
   07  No Ink
   09  No Paper and Ink
   0A  Media/Job mismatch
   0B  Paper Jam

 ZZ == Media?

   01
   10
   11
    ^-- Ribbon
   ^-- Paper

   1 == P
   2 == L ??
   3 == C

Also, the first time a readback happens after plugging in the printer:

34 44 35 31  01 00 01 00  01 00 45 00      "4D51" ...??


** ** ** ** This is what windows sends if you print over the network:

00 00 00 00 40 00 00 00  02 00 00 00 00 00 04 00  Header [unknown]
00 00 02 00 00 00 00 00  00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00

00 00 01 00 HH HH HH HH  02 00 00 00 PP PP PP PP  Header2 [unknown] PP == payload len, HH == payload + header2 len [ie + 3 ]
CC CC CC CC RR RR RR RR  00 00 00 00 LL LL LL LL  CC == cols, RR == rows, LL == plane len (ie RR * CC)
L2 L2 L2 L2                                       L2 == LL * 2, apparently.

[ ..followed by three planes of LL bytes, totalling PP bytes.. ]

*/