selphy_print/backend_kodak6800.c

1187 lines
29 KiB
C
Raw Normal View History

/*
* Kodak 6800/6850 Photo Printer CUPS backend -- libusb-1.0 version
*
* (c) 2013-2014 Solomon Peachy <pizza@shaftnet.org>
*
* Development of this backend was sponsored by:
*
* LiveLink Technology [ www.livelinktechnology.net ]
*
* 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 3 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* [http://www.gnu.org/licenses/gpl-3.0.html]
*
*/
#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>
#include "backend_common.h"
#define USB_VID_KODAK 0x040A
#define USB_PID_KODAK_6800 0x4021
#define USB_PID_KODAK_6850 0x402B
/* File header */
struct kodak6800_hdr {
uint8_t hdr[9]; /* Always 03 1b 43 48 43 0a 00 04 00 [6850]
03 1b 43 48 43 0a 00 01 00 [6800] */
uint8_t copies;
uint16_t columns; /* BE */
uint16_t rows; /* BE */
uint8_t size; /* 0x06 for 6x8, 0x00 for 6x4, 0x07 for 5x7 */
uint8_t laminate; /* 0x01 to laminate, 0x00 for not */
uint8_t unk1; /* 0x00 or 0x01 (for 4x6 on 6x8 media) */
} __attribute__((packed));
struct kodak68x0_status_readback {
uint8_t hdr; /* Always 01 */
uint8_t sts1; /* Always 0x02 (idle) or 0x01 (busy) */
uint8_t sts2; /* 0x01 == ready, 0x02 == no media, 0x03 == not ready */
uint8_t null0[3];
uint8_t unkA; /* 0x00 or 0x01 or 0x10 */
uint8_t nullA;
uint32_t ctr0; /* Total Prints (BE) */
uint32_t ctr1; /* Total Prints (BE) */
uint32_t ctr2; /* Increments by 1 for each print (6850), unk (6800). BE */
uint32_t ctr3; /* Increments by 2 for each print. BE */
uint8_t nullB[3];
uint8_t donor; /* Percentage, 0-100 */
uint8_t unkC[2]; /* Always 00 03 */
uint16_t main_fw; /* seen 652 and 656 (6850) */
uint8_t unkD[2]; /* Always 00 01 */
uint16_t dsp_fw; /* Seen 540 and 541 (6850) and 131 (6800) */
uint8_t unk1; /* Seen 0x00, 0x01, 0x03, 0x04 */
uint8_t null1[2];
uint8_t unk2; /* Seen 0x01, 0x00 */
uint8_t null2;
uint8_t unk3; /* Seen 0x01, 0x00 */
uint8_t null4;
uint8_t unk4; /* Seen 0x01, 0x00 */
uint8_t null5[7];
} __attribute__((packed));
struct kodak6800_printsize {
uint8_t hdr; /* Always 0x06 */
uint16_t width; /* BE */
uint16_t height; /* BE */
uint8_t hdr2; /* Always 0x01 */
uint8_t code; /* 00, 01, 02, 03, 04, 05 seen. An index? */
uint8_t code2; /* 00, 01 seen. Seems to be 1 only after a 4x6 printed. */
uint8_t null[2];
} __attribute__((packed));
#define MAX_MEDIA_LEN 128
struct kodak68x0_media_readback {
uint8_t hdr; /* Always 0x01 */
uint8_t media; /* Always 0x00 (none), 0x0b or 0x03 */
uint8_t null[5];
uint8_t count; /* Always 0x04 (6800) or 0x06 (6850)? */
struct kodak6800_printsize sizes[];
} __attribute__((packed));
#define KODAK68x0_MEDIA_6R 0x0b
#define CMDBUF_LEN 17
/* Private data stucture */
struct kodak6800_ctx {
struct libusb_device_handle *dev;
uint8_t endp_up;
uint8_t endp_down;
int type;
int media;
struct kodak6800_hdr hdr;
uint8_t *databuf;
int datalen;
};
#define READBACK_LEN 68
static void kodak68x0_dump_mediainfo(struct kodak68x0_media_readback *media)
{
int i;
if (media->media == KODAK68x0_MEDIA_6R) {
DEBUG("Media type: 6R (Kodak 197-4096 or equivalent)\n");
} else {
DEBUG("Media type %02x (unknown, please report!)\n", media->media);
}
DEBUG("Legal print sizes:\n");
for (i = 0 ; i < media->count ; i++) {
DEBUG("\t%d: %dx%d (%02x) %s\n", i,
be16_to_cpu(media->sizes[i].width),
be16_to_cpu(media->sizes[i].height),
media->sizes[i].code,
media->sizes[i].code2? "Disallowed" : "");
}
DEBUG("\n");
}
static int kodak6800_get_mediainfo(struct kodak6800_ctx *ctx, struct kodak68x0_media_readback *media)
{
uint8_t req[16];
int ret, num;
memset(req, 0, sizeof(req));
memset(media, 0, sizeof(*media));
req[0] = 0x03;
req[1] = 0x1b;
req[2] = 0x43;
req[3] = 0x48;
req[4] = 0x43;
req[5] = 0x1a;
/* Send request */
if ((ret = send_data(ctx->dev, ctx->endp_down,
req, sizeof(req))))
return ret;
/* Get response */
ret = read_data(ctx->dev, ctx->endp_up,
(uint8_t*)media, MAX_MEDIA_LEN, &num);
if (ret < 0)
return ret;
if (num < (int)sizeof(*media)) {
ERROR("Short read! (%d/%d)\n", num, (int) sizeof(*media));
return 4;
}
/* Validate proper response */
if (media->hdr != 0x01 ||
media->null[0] != 0x00) {
ERROR("Unexpected response from media query!\n");
return CUPS_BACKEND_STOP;
}
ctx->media = media->media;
return 0;
}
static void kodak68x0_dump_status(struct kodak6800_ctx *ctx, struct kodak68x0_status_readback *status)
{
DEBUG("Total prints : %d\n", be32_to_cpu(status->ctr0));
DEBUG("Media prints : %d\n", be32_to_cpu(status->ctr2));
if (ctx->type == P_KODAK_6850) {
int max;
if (ctx->media == KODAK68x0_MEDIA_6R) {
max = 375;
} else {
max = 0;
}
if (max) {
DEBUG("Remaining prints : %d\n", max - be32_to_cpu(status->ctr2));
} else {
DEBUG("Remaining prints : Unknown media type\n");
}
}
DEBUG("Main FW version : %d\n", be16_to_cpu(status->main_fw));
DEBUG("DSP FW version : %d\n", be16_to_cpu(status->dsp_fw));
DEBUG("Donor : %d%%\n", status->donor);
DEBUG("\n");
}
static int kodak6800_get_status(struct kodak6800_ctx *ctx,
struct kodak68x0_status_readback *status)
{
uint8_t req[16];
int ret, num;
memset(req, 0, sizeof(req));
memset(status, 0, sizeof(*status));
req[0] = 0x03;
req[1] = 0x1b;
req[2] = 0x43;
req[3] = 0x48;
req[4] = 0x43;
req[5] = 0x03;
/* Send request */
if ((ret = send_data(ctx->dev, ctx->endp_down,
req, sizeof(req))))
return ret;
/* Get response */
ret = read_data(ctx->dev, ctx->endp_up,
(uint8_t*)status, sizeof(*status), &num);
if (ret < 0)
return ret;
if (num < (int)sizeof(*status)) {
ERROR("Short read! (%d/%d)\n", num, (int) sizeof(*status));
return CUPS_BACKEND_FAILED;
}
if (status->hdr != 0x01) {
ERROR("Unexpected response from status query!\n");
return CUPS_BACKEND_FAILED;
}
return 0;
}
#define UPDATE_SIZE 1536
static int kodak6800_get_tonecurve(struct kodak6800_ctx *ctx, char *fname)
{
libusb_device_handle *dev = ctx->dev;
uint8_t endp_down = ctx->endp_down;
uint8_t endp_up = ctx->endp_up;
uint8_t cmdbuf[16];
uint8_t respbuf[64];
int ret, num = 0;
int i;
uint16_t *data = malloc(UPDATE_SIZE);
INFO("Dump Tone Curve to '%s'\n", fname);
/* Initial Request */
cmdbuf[0] = 0x03;
cmdbuf[1] = 0x1b;
cmdbuf[2] = 0x43;
cmdbuf[3] = 0x48;
cmdbuf[4] = 0x43;
cmdbuf[5] = 0x0c;
cmdbuf[6] = 0x54;
cmdbuf[7] = 0x4f;
cmdbuf[8] = 0x4e;
cmdbuf[9] = 0x45;
cmdbuf[10] = 0x72;
cmdbuf[11] = 0x01;
cmdbuf[12] = 0x00;
cmdbuf[13] = 0x00;
cmdbuf[14] = 0x00;
cmdbuf[15] = 0x00;
if ((ret = send_data(dev, endp_down,
cmdbuf, 16)))
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
goto done;
if (num != 51) {
ERROR("Short read! (%d/%d)\n", num, 51);
ret = 4;
goto done;
}
/* Then we can poll the data */
cmdbuf[0] = 0x03;
cmdbuf[1] = 0x1b;
cmdbuf[2] = 0x43;
cmdbuf[3] = 0x48;
cmdbuf[4] = 0x43;
cmdbuf[5] = 0x0c;
cmdbuf[6] = 0x54;
cmdbuf[7] = 0x4f;
cmdbuf[8] = 0x4e;
cmdbuf[9] = 0x45;
cmdbuf[10] = 0x20;
for (i = 0 ; i < 24 ; i++) {
if ((ret = send_data(dev, endp_down,
cmdbuf, 11)))
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
goto done;
if (num != 64) {
ERROR("Short read! (%d/%d)\n", num, 51);
ret = 4;
goto done;
}
/* Copy into buffer */
memcpy(((uint8_t*)data)+i*64, respbuf, 64);
}
/* Open file and write it out */
{
int tc_fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
if (tc_fd < 0) {
ret = 4;
goto done;
}
for (i = 0 ; i < 768; i++) {
/* Byteswap appropriately */
data[i] = cpu_to_be16(le16_to_cpu(data[i]));
write(tc_fd, &data[i], sizeof(uint16_t));
}
close(tc_fd);
}
done:
/* We're done */
free(data);
return 0;
}
static int kodak6800_set_tonecurve(struct kodak6800_ctx *ctx, char *fname)
{
libusb_device_handle *dev = ctx->dev;
uint8_t endp_down = ctx->endp_down;
uint8_t endp_up = ctx->endp_up;
uint8_t cmdbuf[64];
uint8_t respbuf[64];
int ret, num = 0;
int remain;
uint16_t *data = malloc(UPDATE_SIZE);
uint8_t *ptr;
INFO("Set Tone Curve from '%s'\n", fname);
/* Read in file */
int tc_fd = open(fname, O_RDONLY);
if (tc_fd < 0) {
ret = -1;
goto done;
}
if (read(tc_fd, data, UPDATE_SIZE) != UPDATE_SIZE) {
ret = -2;
goto done;
}
close(tc_fd);
/* Byteswap data to printer's format */
for (ret = 0; ret < (UPDATE_SIZE)/2 ; ret++) {
data[ret] = cpu_to_le16(be16_to_cpu(data[ret]));
}
/* Initial Request */
cmdbuf[0] = 0x03;
cmdbuf[1] = 0x1b;
cmdbuf[2] = 0x43;
cmdbuf[3] = 0x48;
cmdbuf[4] = 0x43;
cmdbuf[5] = 0x0c;
cmdbuf[6] = 0x54;
cmdbuf[7] = 0x4f;
cmdbuf[8] = 0x4e;
cmdbuf[9] = 0x45;
cmdbuf[10] = 0x77;
cmdbuf[11] = 0x01;
cmdbuf[12] = 0x00;
cmdbuf[13] = 0x00;
cmdbuf[14] = 0x00;
cmdbuf[15] = 0x00;
if ((ret = send_data(dev, endp_down,
cmdbuf, 16)))
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
goto done;
if (num != 51) {
ERROR("Short read! (%d/%d)\n", num, 51);
ret = 4;
goto done;
}
ptr = (uint8_t*) data;
remain = UPDATE_SIZE;
while (remain > 0) {
int count = remain > 63 ? 63 : remain;
cmdbuf[0] = 0x03;
memcpy(cmdbuf+1, ptr, count);
remain -= count;
ptr += count;
/* Send next block over */
if ((ret = send_data(dev, endp_down,
cmdbuf, count+1)))
goto done;
ret = read_data(dev, endp_up,
respbuf, sizeof(respbuf), &num);
if (ret < 0)
goto done;
if (num != 51) {
ERROR("Short read! (%d/%d)\n", num, 51);
ret = 4;
goto done;
}
};
done:
/* We're done */
free(data);
return ret;
}
static int kodak6800_query_serno(struct libusb_device_handle *dev, uint8_t endp_up, uint8_t endp_down, char *buf, int buf_len)
{
int ret;
int num;
uint8_t resp[33];
uint8_t req[16];
memset(req, 0, sizeof(req));
memset(resp, 0, sizeof(resp));
req[0] = 0x03;
req[1] = 0x1b;
req[2] = 0x43;
req[3] = 0x48;
req[4] = 0x43;
req[5] = 0x03;
/* Send request */
if ((ret = send_data(dev, endp_down,
req, sizeof(req))))
return ret;
/* Get response */
ret = read_data(dev, endp_up,
resp, sizeof(resp) - 1, &num);
if (ret < 0)
return ret;
if (num != 32) {
ERROR("Short read! (%d/%d)\n", num, 32);
return 4;
}
strncpy(buf, (char*)resp+24, buf_len);
buf[buf_len-1] = 0;
return 0;
}
static int kodak6850_send_init(struct kodak6800_ctx *ctx)
{
uint8_t cmdbuf[64];
uint8_t rdbuf[64];
int ret = 0, num = 0;
memset(cmdbuf, 0, CMDBUF_LEN);
cmdbuf[0] = 0x03;
cmdbuf[1] = 0x1b;
cmdbuf[2] = 0x43;
cmdbuf[3] = 0x48;
cmdbuf[4] = 0x43;
cmdbuf[5] = 0x4c;
if ((ret = send_data(ctx->dev, ctx->endp_down,
cmdbuf, CMDBUF_LEN -1)))
return CUPS_BACKEND_FAILED;
/* Read response */
ret = read_data(ctx->dev, ctx->endp_up,
rdbuf, READBACK_LEN, &num);
if (ret < 0)
return CUPS_BACKEND_FAILED;
if (num < 51) {
ERROR("Short read! (%d/%d)\n", num, 51);
return CUPS_BACKEND_FAILED;
}
if (num != 51) {
ERROR("Unexpected readback from printer (%d/%d from 0x%02x))\n",
num, READBACK_LEN, ctx->endp_up);
return CUPS_BACKEND_FAILED;
}
if (rdbuf[0] != 0x01 ||
rdbuf[2] != 0x43) {
ERROR("Unexpected response from printer init!\n");
return CUPS_BACKEND_FAILED;
}
// XXX I believe this the media position
// saying when we have a 4x6 left on an 8x6 blank
if (rdbuf[1] != 0x01 && rdbuf[1] != 0x00) {
ERROR("Unexpected status code (0x%02x)!\n", rdbuf[1]);
return CUPS_BACKEND_FAILED;
}
return ret;
}
static void kodak6800_cmdline(void)
{
DEBUG("\t\t[ -c filename ] # Get tone curve\n");
DEBUG("\t\t[ -C filename ] # Set tone curve\n");
DEBUG("\t\t[ -m ] # Query media\n");
DEBUG("\t\t[ -s ] # Query status\n");
}
static int kodak6800_cmdline_arg(void *vctx, int argc, char **argv)
{
struct kodak6800_ctx *ctx = vctx;
int i, j = 0;
/* Reset arg parsing */
optind = 1;
opterr = 0;
while ((i = getopt(argc, argv, "C:c:ms")) >= 0) {
switch(i) {
case 'c':
if (ctx) {
j = kodak6800_get_tonecurve(ctx, optarg);
break;
}
return 1;
case 'C':
if (ctx) {
j = kodak6800_set_tonecurve(ctx, optarg);
break;
}
return 1;
case 'm':
if (ctx) {
uint8_t mediabuf[MAX_MEDIA_LEN];
struct kodak68x0_media_readback *media = (struct kodak68x0_media_readback*)mediabuf;
j = kodak6800_get_mediainfo(ctx, media);
if (!j)
kodak68x0_dump_mediainfo(media);
break;
}
return 1;
case 's':
if (ctx) {
uint8_t mediabuf[MAX_MEDIA_LEN];
struct kodak68x0_media_readback *media = (struct kodak68x0_media_readback*)mediabuf;
struct kodak68x0_status_readback status;
j = kodak6800_get_mediainfo(ctx, media);
if (!j)
j = kodak6800_get_status(ctx, &status);
if (!j)
kodak68x0_dump_status(ctx, &status);
break;
}
return 1;
default:
break; /* Ignore completely */
}
if (j) return j;
}
return 0;
}
static void *kodak6800_init(void)
{
struct kodak6800_ctx *ctx = malloc(sizeof(struct kodak6800_ctx));
if (!ctx)
return NULL;
memset(ctx, 0, sizeof(struct kodak6800_ctx));
ctx->type = P_ANY;
ctx->media = -1;
return ctx;
}
static void kodak6800_attach(void *vctx, struct libusb_device_handle *dev,
uint8_t endp_up, uint8_t endp_down, uint8_t jobid)
{
struct kodak6800_ctx *ctx = vctx;
struct libusb_device *device;
struct libusb_device_descriptor desc;
2013-11-23 19:51:55 -05:00
UNUSED(jobid);
ctx->dev = dev;
ctx->endp_up = endp_up;
ctx->endp_down = endp_down;
device = libusb_get_device(dev);
libusb_get_device_descriptor(device, &desc);
/* Map out device type */
if (desc.idProduct == USB_PID_KODAK_6850)
ctx->type = P_KODAK_6850;
else
ctx->type = P_KODAK_6800;
}
static void kodak6800_teardown(void *vctx) {
struct kodak6800_ctx *ctx = vctx;
if (!ctx)
return;
if (ctx->databuf)
free(ctx->databuf);
free(ctx);
}
static int kodak6800_read_parse(void *vctx, int data_fd) {
struct kodak6800_ctx *ctx = vctx;
int ret;
if (!ctx)
return CUPS_BACKEND_FAILED;
if (ctx->databuf) {
free(ctx->databuf);
ctx->databuf = NULL;
}
/* Read in then validate header */
ret = read(data_fd, &ctx->hdr, sizeof(ctx->hdr));
if (ret < 0 || ret != sizeof(ctx->hdr)) {
if (ret == 0)
return CUPS_BACKEND_CANCEL;
ERROR("Read failed (%d/%d/%d)\n",
ret, 0, (int)sizeof(ctx->hdr));
perror("ERROR: Read failed");
return CUPS_BACKEND_CANCEL;
}
if (ctx->hdr.hdr[0] != 0x03 ||
ctx->hdr.hdr[1] != 0x1b ||
ctx->hdr.hdr[2] != 0x43 ||
ctx->hdr.hdr[3] != 0x48 ||
ctx->hdr.hdr[4] != 0x43) {
ERROR("Unrecognized data format!\n");
return CUPS_BACKEND_CANCEL;
}
ctx->datalen = be16_to_cpu(ctx->hdr.rows) * be16_to_cpu(ctx->hdr.columns) * 3;
ctx->databuf = malloc(ctx->datalen);
if (!ctx->databuf) {
ERROR("Memory allocation failure!\n");
return CUPS_BACKEND_FAILED;
}
{
int remain = ctx->datalen;
uint8_t *ptr = ctx->databuf;
do {
ret = read(data_fd, ptr, remain);
if (ret < 0) {
ERROR("Read failed (%d/%d/%d)\n",
ret, remain, ctx->datalen);
perror("ERROR: Read failed");
return CUPS_BACKEND_CANCEL;
}
ptr += ret;
remain -= ret;
} while (remain);
}
return CUPS_BACKEND_OK;
}
static int kodak6800_main_loop(void *vctx, int copies) {
struct kodak6800_ctx *ctx = vctx;
struct kodak68x0_status_readback status;
uint8_t cmdbuf[CMDBUF_LEN];
uint8_t mediabuf[MAX_MEDIA_LEN];
struct kodak68x0_media_readback *media = (struct kodak68x0_media_readback*)mediabuf;
int num, ret;
if (!ctx)
return CUPS_BACKEND_FAILED;
/* Printer handles generating copies.. */
if (ctx->hdr.copies < copies)
ctx->hdr.copies = copies;
copies = 1;
/* Query loaded media */
INFO("Querying loaded media\n");
ret = kodak6800_get_mediainfo(ctx, media);
if (ret < 0)
return CUPS_BACKEND_FAILED;
/* Appears to depend on media */
if (media->media != KODAK68x0_MEDIA_6R &&
media->media != 0x03) {
ERROR("Unrecognized media type %02x\n", media->media);
return CUPS_BACKEND_STOP;
}
/* Validate against supported media list */
for (num = 0 ; num < media->count; num++) {
if (media->sizes[num].height == ctx->hdr.rows &&
media->sizes[num].width == ctx->hdr.columns)
break;
}
if (num == media->count) {
ERROR("Print size unsupported by media!\n");
return CUPS_BACKEND_HOLD;
}
top:
INFO