2012-10-26 22:06:47 -04:00
|
|
|
/*
|
2012-11-03 09:37:12 -04:00
|
|
|
* Canon SELPHY ES/CP series print assister -- libusb-1.0 version
|
2012-10-26 22:06:47 -04:00
|
|
|
*
|
|
|
|
* (c) 2007-2012 Solomon Peachy <pizza@shaftnet.org>
|
|
|
|
*
|
|
|
|
* The latest version of this program can be found at:
|
|
|
|
*
|
2012-10-27 10:27:31 -04:00
|
|
|
* http://git.shaftnet.org/git/gitweb.cgi?p=selphy_print.git
|
2012-10-26 22:06:47 -04:00
|
|
|
*
|
|
|
|
* 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>
|
2012-10-26 23:21:33 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2012-10-26 22:06:47 -04:00
|
|
|
#include <libusb-1.0/libusb.h>
|
|
|
|
|
2012-11-19 21:50:14 -05:00
|
|
|
#include "selphy_print_common.h"
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-11-06 22:34:23 -05:00
|
|
|
#define STR_LEN_MAX 64
|
2012-11-13 07:15:19 -05:00
|
|
|
#define URI_PREFIX "selphy://"
|
2012-10-27 10:38:36 -04:00
|
|
|
|
2012-10-26 22:06:47 -04:00
|
|
|
/* USB Identifiers */
|
2012-10-27 23:58:39 -04:00
|
|
|
#define USB_VID_CANON 0x04a9
|
|
|
|
#define USB_PID_CANON_ES1 0x3141
|
2012-11-05 18:51:32 -05:00
|
|
|
#define USB_PID_CANON_ES2 0x3185
|
2012-11-11 06:36:18 -05:00
|
|
|
#define USB_PID_CANON_ES20 0x3186
|
2012-11-16 13:15:18 -05:00
|
|
|
#define USB_PID_CANON_ES3 3 // XXX 31af? 31b1??
|
2012-10-27 23:58:39 -04:00
|
|
|
#define USB_PID_CANON_ES30 0x31B0
|
|
|
|
#define USB_PID_CANON_ES40 0x31EE
|
2012-10-28 00:07:22 -04:00
|
|
|
#define USB_PID_CANON_CP10 0x304A
|
2012-11-13 07:15:19 -05:00
|
|
|
#define USB_PID_CANON_CP100 0x3063 // - incoming G
|
2012-11-19 16:41:55 -05:00
|
|
|
#define USB_PID_CANON_CP200 0x307C
|
2012-10-27 23:58:39 -04:00
|
|
|
#define USB_PID_CANON_CP220 0x30BD
|
2012-11-16 13:15:18 -05:00
|
|
|
#define USB_PID_CANON_CP300 0x307D
|
2012-11-08 19:35:09 -05:00
|
|
|
#define USB_PID_CANON_CP330 0x30BE
|
|
|
|
#define USB_PID_CANON_CP400 0x30F6
|
2012-11-21 10:59:31 -05:00
|
|
|
#define USB_PID_CANON_CP500 0x30F5
|
2012-11-08 19:35:09 -05:00
|
|
|
#define USB_PID_CANON_CP510 0x3128
|
2012-11-16 13:15:18 -05:00
|
|
|
#define USB_PID_CANON_CP520 520 // XXX 316f? 3172? (related to cp740/cp750)
|
2012-10-27 23:58:39 -04:00
|
|
|
#define USB_PID_CANON_CP530 530 // XXX
|
2012-10-28 00:07:22 -04:00
|
|
|
#define USB_PID_CANON_CP600 0x310B
|
2012-11-16 20:19:15 -05:00
|
|
|
#define USB_PID_CANON_CP710 0x3127
|
2012-11-05 18:51:32 -05:00
|
|
|
#define USB_PID_CANON_CP720 0x3143
|
2012-11-13 10:27:19 -05:00
|
|
|
#define USB_PID_CANON_CP730 0x3142
|
2012-10-28 17:32:24 -04:00
|
|
|
#define USB_PID_CANON_CP740 0x3171
|
2012-11-11 06:36:18 -05:00
|
|
|
#define USB_PID_CANON_CP750 0x3170
|
2012-10-29 13:20:51 -04:00
|
|
|
#define USB_PID_CANON_CP760 0x31AB
|
2012-11-15 18:31:00 -05:00
|
|
|
#define USB_PID_CANON_CP770 0x31AA
|
2012-11-07 18:29:47 -05:00
|
|
|
#define USB_PID_CANON_CP780 0x31DD
|
2012-10-27 23:58:39 -04:00
|
|
|
#define USB_PID_CANON_CP790 790 // XXX
|
2012-11-03 09:37:12 -04:00
|
|
|
#define USB_PID_CANON_CP800 0x3214
|
|
|
|
#define USB_PID_CANON_CP810 0x3256
|
|
|
|
#define USB_PID_CANON_CP900 0x3255
|
2012-10-26 23:21:33 -04:00
|
|
|
|
2012-10-27 10:38:36 -04:00
|
|
|
static int dump_data_libusb(int remaining, int present, int data_fd,
|
|
|
|
struct libusb_device_handle *dev,
|
2012-11-05 19:23:02 -05:00
|
|
|
uint8_t endpoint,
|
2012-10-27 10:38:36 -04:00
|
|
|
uint8_t *buf, uint16_t buflen) {
|
2012-10-26 23:21:33 -04:00
|
|
|
int cnt;
|
|
|
|
int i;
|
|
|
|
int wrote;
|
|
|
|
int num;
|
|
|
|
|
2012-10-27 09:56:47 -04:00
|
|
|
while (remaining > 0) {
|
2012-10-26 23:21:33 -04:00
|
|
|
cnt = read(data_fd, buf + present, (remaining < (buflen-present)) ? remaining : (buflen-present));
|
|
|
|
|
|
|
|
if (cnt < 0)
|
|
|
|
return -1;
|
|
|
|
|
2012-10-27 10:21:47 -04:00
|
|
|
if (present) {
|
|
|
|
cnt += present;
|
|
|
|
present = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-05 19:23:02 -05:00
|
|
|
i = libusb_bulk_transfer(dev, endpoint,
|
2012-10-26 23:21:33 -04:00
|
|
|
buf,
|
|
|
|
cnt,
|
|
|
|
&num,
|
|
|
|
2000);
|
|
|
|
if (i < 0) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("libusb error %d: (%d/%d to 0x%02x)\n", i, num, cnt, endpoint);
|
2012-10-26 23:21:33 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num != cnt) {
|
|
|
|
/* Realign buffer.. */
|
|
|
|
present = cnt - num;
|
|
|
|
memmove(buf, buf + num, present);
|
|
|
|
}
|
|
|
|
wrote += num;
|
|
|
|
remaining -= num;
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Wrote %d bytes\n", wrote);
|
2012-10-26 23:21:33 -04:00
|
|
|
|
|
|
|
return wrote;
|
|
|
|
}
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-11-06 22:20:42 -05:00
|
|
|
static int find_and_enumerate(struct libusb_context *ctx,
|
|
|
|
struct libusb_device ***list,
|
2012-11-20 19:05:03 -05:00
|
|
|
char *match_serno,
|
2012-11-06 22:20:42 -05:00
|
|
|
int printer_type,
|
|
|
|
int scan_only)
|
2012-10-26 22:06:47 -04:00
|
|
|
{
|
2012-11-06 22:20:42 -05:00
|
|
|
int num;
|
|
|
|
int i;
|
2012-11-06 21:31:04 -05:00
|
|
|
int found = -1;
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-11-06 22:20:42 -05:00
|
|
|
struct libusb_device_handle *dev;
|
2012-10-26 22:06:47 -04:00
|
|
|
|
|
|
|
/* Enumerate and find suitable device */
|
2012-11-06 22:20:42 -05:00
|
|
|
num = libusb_get_device_list(ctx, list);
|
2012-10-26 22:06:47 -04:00
|
|
|
|
|
|
|
for (i = 0 ; i < num ; i++) {
|
2012-11-06 21:31:04 -05:00
|
|
|
struct libusb_device_descriptor desc;
|
2012-11-06 22:20:42 -05:00
|
|
|
unsigned char product[STR_LEN_MAX] = "";
|
|
|
|
unsigned char serial[STR_LEN_MAX] = "";
|
2012-11-06 23:06:32 -05:00
|
|
|
unsigned char manuf[STR_LEN_MAX] = "";
|
2012-11-06 21:31:04 -05:00
|
|
|
int valid = 0;
|
2012-11-06 22:20:42 -05:00
|
|
|
libusb_get_device_descriptor((*list)[i], &desc);
|
2012-10-27 09:56:47 -04:00
|
|
|
|
2012-10-26 22:06:47 -04:00
|
|
|
if (desc.idVendor != USB_VID_CANON)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch(desc.idProduct) {
|
2012-11-05 21:07:35 -05:00
|
|
|
case USB_PID_CANON_ES1: // "Canon SELPHY ES1"
|
2012-10-26 23:21:33 -04:00
|
|
|
if (printer_type == P_ES1)
|
2012-11-06 21:31:04 -05:00
|
|
|
found = i;
|
|
|
|
valid = 1;
|
2012-10-26 23:21:33 -04:00
|
|
|
break;
|
2012-11-05 21:07:35 -05:00
|
|
|
case USB_PID_CANON_ES2: // "Canon SELPHY ES2"
|
2012-11-06 11:44:09 -05:00
|
|
|
case USB_PID_CANON_ES20: // "Canon SELPHY ES20"
|
2012-10-26 23:21:33 -04:00
|
|
|
if (printer_type == P_ES2_20)
|
2012-11-06 21:31:04 -05:00
|
|
|
found = i;
|
|
|
|
valid = 1;
|
2012-10-26 23:21:33 -04:00
|
|
|
break;
|
2012-11-06 10:21:20 -05:00
|
|
|
case USB_PID_CANON_ES3: // "Canon SELPHY ES3"
|
2012-11-05 21:07:35 -05:00
|
|
|
case USB_PID_CANON_ES30: // "Canon SELPHY ES30"
|
2012-10-26 23:21:33 -04:00
|
|
|
if (printer_type == P_ES3_30)
|
2012-11-06 21:31:04 -05:00
|
|
|
found = i;
|
|
|
|
valid = 1;
|
2012-10-26 23:21:33 -04:00
|
|
|
break;
|
2012-11-06 11:44:09 -05:00
|
|
|
case USB_PID_CANON_ES40: // "Canon SELPHY ES40"
|
2012-10-26 23:21:33 -04:00
|
|
|
case USB_PID_CANON_CP790:
|
2012-11-03 09:37:12 -04:00
|
|
|
if (printer_type == P_ES40_CP790)
|
2012-11-06 21:31:04 -05:00
|
|
|
found = i;
|
|
|
|
valid = 1;
|
2012-11-03 09:37:12 -04:00
|
|
|
break;
|
2012-11-06 11:44:09 -05:00
|
|
|
case USB_PID_CANON_CP10: // "Canon CP-10"
|
|
|
|
case USB_PID_CANON_CP100: // "Canon CP-100"
|
|
|
|
case USB_PID_CANON_CP200: // "Canon CP-200"
|
2012-11-06 10:21:20 -05:00
|
|
|
case USB_PID_CANON_CP220: // "Canon CP-220"
|
2012-11-06 11:44:09 -05:00
|
|
|
case USB_PID_CANON_CP300: // "Canon CP-300"
|
|
|
|
case USB_PID_CANON_CP330: // "Canon CP-330"
|
|
|
|
case USB_PID_CANON_CP400: // "Canon SELPHY CP400"
|
|
|
|
case USB_PID_CANON_CP500: // "Canon SELPHY CP500"
|
|
|
|
case USB_PID_CANON_CP510: // "Canon SELPHY CP510"
|
|
|
|
case USB_PID_CANON_CP520: // "Canon SELPHY CP520"
|
|
|
|
case USB_PID_CANON_CP530: // "Canon SELPHY CP530"
|
|
|
|
case USB_PID_CANON_CP600: // "Canon SELPHY CP600"
|
|
|
|
case USB_PID_CANON_CP710: // "Canon SELPHY CP710"
|
2012-11-05 21:07:35 -05:00
|
|
|
case USB_PID_CANON_CP720: // "Canon SELPHY CP720"
|
2012-11-06 11:44:09 -05:00
|
|
|
case USB_PID_CANON_CP730: // "Canon SELPHY CP730"
|
|
|
|
case USB_PID_CANON_CP740: // "Canon SELPHY CP740"
|
|
|
|
case USB_PID_CANON_CP750: // "Canon SELPHY CP750"
|
2012-11-06 10:21:20 -05:00
|
|
|
case USB_PID_CANON_CP760: // "Canon SELPHY CP760"
|
|
|
|
case USB_PID_CANON_CP770: // "Canon SELPHY CP770"
|
|
|
|
case USB_PID_CANON_CP780: // "Canon SELPHY CP780"
|
2012-11-06 11:44:09 -05:00
|
|
|
case USB_PID_CANON_CP800: // "Canon SELPHY CP800"
|
|
|
|
case USB_PID_CANON_CP810: // "Canon SELPHY CP810"
|
|
|
|
case USB_PID_CANON_CP900: // "Canon SELPHY CP900"
|
2012-10-26 23:21:33 -04:00
|
|
|
if (printer_type == P_CP_XXX)
|
2012-11-06 21:31:04 -05:00
|
|
|
found = i;
|
|
|
|
valid = 1;
|
2012-10-26 22:06:47 -04:00
|
|
|
break;
|
|
|
|
default:
|
2012-11-20 11:36:16 -05:00
|
|
|
/* Hook for testing unknown PIDs */
|
|
|
|
if (getenv("SELPHY_PID") && getenv("SELPHY_TYPE")) {
|
|
|
|
int pid = strtol(getenv("SELPHY_PID"), NULL, 16);
|
|
|
|
int type = atoi(getenv("SELPHY_TYPE"));
|
|
|
|
if (pid == desc.idProduct) {
|
|
|
|
valid = 1;
|
|
|
|
if (printer_type == type) {
|
|
|
|
found = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-26 23:21:33 -04:00
|
|
|
break;
|
2012-10-26 22:06:47 -04:00
|
|
|
}
|
2012-11-06 21:31:04 -05:00
|
|
|
|
2012-11-06 22:20:42 -05:00
|
|
|
if (libusb_open(((*list)[i]), &dev)) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("Could not open device %04x:%04x\n", desc.idVendor, desc.idProduct);
|
2012-11-06 22:20:42 -05:00
|
|
|
found = -1;
|
|
|
|
continue;
|
2012-11-06 21:31:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Query detailed info */
|
2012-11-06 23:06:32 -05:00
|
|
|
if (desc.iManufacturer) {
|
|
|
|
libusb_get_string_descriptor_ascii(dev, desc.iManufacturer, manuf, STR_LEN_MAX);
|
|
|
|
}
|
2012-11-06 21:31:04 -05:00
|
|
|
if (desc.iProduct) {
|
2012-11-06 22:20:42 -05:00
|
|
|
libusb_get_string_descriptor_ascii(dev, desc.iProduct, product, STR_LEN_MAX);
|
2012-11-06 21:31:04 -05:00
|
|
|
}
|
|
|
|
if (desc.iSerialNumber) {
|
2012-11-06 22:20:42 -05:00
|
|
|
libusb_get_string_descriptor_ascii(dev, desc.iSerialNumber, serial, STR_LEN_MAX);
|
2012-11-06 21:31:04 -05:00
|
|
|
}
|
|
|
|
|
2012-11-07 19:00:29 -05:00
|
|
|
DEBUG("%s%sPID: %04X Product: '%s' Serial: '%s'\n",
|
2012-11-06 23:16:37 -05:00
|
|
|
(!valid) ? "UNRECOGNIZED: " : "",
|
|
|
|
(found == i) ? "MATCH: " : "",
|
|
|
|
desc.idProduct, product, serial);
|
2012-11-20 11:36:16 -05:00
|
|
|
|
2012-11-06 22:20:42 -05:00
|
|
|
if (valid && scan_only) {
|
2012-11-13 18:20:41 -05:00
|
|
|
/* URL-ify model. */
|
|
|
|
char buf[128]; // XXX ugly..
|
|
|
|
i = 0;
|
|
|
|
while (*(product + i + strlen("Canon"))) {
|
|
|
|
buf[i] = *(product + i + strlen("Canon "));
|
|
|
|
if(buf[i] == ' ') {
|
|
|
|
buf[i++] = '%';
|
|
|
|
buf[i++] = '2';
|
|
|
|
buf[i] = '0';
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
fprintf(stdout, "direct %sCanon/%s?serial=%s \"%s\" \"%s\" \"MFG:Canon;CMD:SelphyRaster;CLS:PRINTER;MDL:%s;DES:%s;SN:%s\" \"\"\n", URI_PREFIX,
|
|
|
|
buf, serial, product, product,
|
2012-11-06 23:06:32 -05:00
|
|
|
product + strlen("Canon "), product, serial);
|
2012-11-06 22:20:42 -05:00
|
|
|
}
|
2012-11-06 21:31:04 -05:00
|
|
|
|
2012-11-20 19:05:03 -05:00
|
|
|
/* If a serial number was passed down, use it. */
|
|
|
|
if (found && match_serno &&
|
|
|
|
strcmp(match_serno, (char*)serial)) {
|
|
|
|
found = -1;
|
|
|
|
}
|
|
|
|
|
2012-11-06 21:31:04 -05:00
|
|
|
libusb_close(dev);
|
2012-10-26 22:06:47 -04:00
|
|
|
}
|
|
|
|
|
2012-11-06 22:20:42 -05:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct libusb_context *ctx;
|
|
|
|
struct libusb_device **list;
|
|
|
|
struct libusb_device_handle *dev;
|
|
|
|
struct libusb_config_descriptor *config;
|
|
|
|
|
|
|
|
uint8_t endp_up = 0;
|
|
|
|
uint8_t endp_down = 0;
|
|
|
|
|
|
|
|
int printer_type = P_END;
|
|
|
|
|
|
|
|
int iface = 0;
|
|
|
|
|
|
|
|
int num, i;
|
|
|
|
int ret = 0;
|
|
|
|
int claimed;
|
|
|
|
int found = -1;
|
|
|
|
|
|
|
|
uint8_t rdbuf[READBACK_LEN], rdbuf2[READBACK_LEN];
|
|
|
|
int last_state = -1, state = S_IDLE;
|
|
|
|
|
|
|
|
int plane_len = 0;
|
|
|
|
|
|
|
|
int bw_mode = 0;
|
|
|
|
int16_t paper_code_offset = -1;
|
|
|
|
int16_t paper_code = -1;
|
|
|
|
|
|
|
|
uint8_t buffer[BUF_LEN];
|
|
|
|
|
|
|
|
int data_fd = fileno(stdin);
|
|
|
|
|
2012-11-13 07:15:19 -05:00
|
|
|
char *uri = getenv("DEVICE_URI");;
|
|
|
|
char *use_serno = NULL;
|
|
|
|
|
2012-11-06 22:20:42 -05:00
|
|
|
/* Static initialization */
|
|
|
|
setup_paper_codes();
|
|
|
|
|
|
|
|
/* Cmdline help */
|
|
|
|
if (argc < 2) {
|
2012-11-13 07:15:19 -05:00
|
|
|
DEBUG("SELPHY ES/CP Print Assist version %s\nUsage:\n\t%s [ infile | - ]\n\t%s job user title num-copies options [ filename ] \n\n",
|
|
|
|
VERSION,
|
|
|
|
argv[0], argv[0]);
|
2012-11-06 22:20:42 -05:00
|
|
|
libusb_init(&ctx);
|
2012-11-20 19:05:03 -05:00
|
|
|
find_and_enumerate(ctx, &list, NULL, printer_type, 1);
|
2012-11-06 22:20:42 -05:00
|
|
|
libusb_free_device_list(list, 1);
|
|
|
|
libusb_exit(ctx);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-11-13 07:15:19 -05:00
|
|
|
/* Are we running as a CUPS backend? */
|
|
|
|
if (uri) {
|
|
|
|
if (argv[6]) { /* IOW, is it specified? */
|
|
|
|
data_fd = open(argv[6], O_RDONLY);
|
|
|
|
if (data_fd < 0) {
|
|
|
|
perror("ERROR:Can't open input file");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start parsing URI 'selphy://PID/SERIAL' */
|
2012-11-13 18:26:31 -05:00
|
|
|
if (strncmp(URI_PREFIX, uri, strlen(URI_PREFIX))) {
|
|
|
|
ERROR("Invalid URI prefix (%s)\n", uri);
|
2012-11-06 22:20:42 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-11-13 18:26:31 -05:00
|
|
|
use_serno = strchr(uri, '=');
|
|
|
|
if (!use_serno || !*(use_serno+1)) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("Invalid URI (%s)\n", uri);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
use_serno++;
|
|
|
|
} else {
|
|
|
|
/* Open Input File */
|
|
|
|
if (strcmp("-", argv[1])) {
|
|
|
|
data_fd = open(argv[1], O_RDONLY);
|
|
|
|
if (data_fd < 0) {
|
|
|
|
perror("ERROR:Can't open input file");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2012-11-06 22:20:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Figure out printer this file is intended for */
|
|
|
|
read(data_fd, buffer, MAX_HEADER);
|
|
|
|
|
|
|
|
printer_type = parse_printjob(buffer, &bw_mode, &plane_len);
|
|
|
|
if (printer_type < 0) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("Unrecognized printjob file format!\n");
|
2012-11-06 22:20:42 -05:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("%sFile intended for a '%s' printer\n", bw_mode? "B/W " : "", printers[printer_type].model);
|
2012-11-06 22:20:42 -05:00
|
|
|
|
|
|
|
plane_len += 12; /* Add in plane header */
|
|
|
|
paper_code_offset = printers[printer_type].paper_code_offset;
|
2012-11-07 19:00:29 -05:00
|
|
|
if (printers[printer_type].pgcode_offset != -1)
|
|
|
|
paper_code = printers[printer_type].paper_codes[buffer[printers[printer_type].pgcode_offset]];
|
2012-11-06 22:20:42 -05:00
|
|
|
|
|
|
|
/* Libusb setup */
|
|
|
|
libusb_init(&ctx);
|
2012-11-20 19:05:03 -05:00
|
|
|
found = find_and_enumerate(ctx, &list, use_serno, printer_type, 0);
|
2012-11-06 22:20:42 -05:00
|
|
|
|
2012-11-06 21:31:04 -05:00
|
|
|
if (found == -1) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("No suitable printers found!\n");
|
2012-11-06 22:20:42 -05:00
|
|
|
ret = 3;
|
2012-10-26 22:06:47 -04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-11-06 21:31:04 -05:00
|
|
|
ret = libusb_open(list[found], &dev);
|
2012-10-27 08:32:37 -04:00
|
|
|
if (ret) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("Could not open device (Need to be root?) (%d)\n", ret);
|
2012-11-06 22:20:42 -05:00
|
|
|
ret = 4;
|
2012-11-06 21:31:04 -05:00
|
|
|
goto done;
|
2012-11-05 20:33:10 -05:00
|
|
|
}
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-10-27 09:56:47 -04:00
|
|
|
claimed = libusb_kernel_driver_active(dev, iface);
|
2012-10-27 08:32:37 -04:00
|
|
|
if (claimed) {
|
2012-10-27 09:56:47 -04:00
|
|
|
ret = libusb_detach_kernel_driver(dev, iface);
|
2012-10-27 08:32:37 -04:00
|
|
|
if (ret) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("Could not detach printer from kernel (%d)\n", ret);
|
2012-11-06 22:20:42 -05:00
|
|
|
ret = 4;
|
2012-11-05 18:51:32 -05:00
|
|
|
goto done_close;
|
2012-10-27 08:32:37 -04:00
|
|
|
}
|
|
|
|
}
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-10-27 09:56:47 -04:00
|
|
|
ret = libusb_claim_interface(dev, iface);
|
2012-10-27 08:32:37 -04:00
|
|
|
if (ret) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("Could not claim printer interface (%d)\n", ret);
|
2012-11-06 22:20:42 -05:00
|
|
|
ret = 4;
|
2012-11-05 18:51:32 -05:00
|
|
|
goto done_close;
|
2012-10-27 08:32:37 -04:00
|
|
|
}
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-11-06 21:31:04 -05:00
|
|
|
ret = libusb_get_active_config_descriptor(list[found], &config);
|
2012-11-05 19:23:02 -05:00
|
|
|
if (ret) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("Could not fetch config descriptor (%d)\n", ret);
|
2012-11-06 22:20:42 -05:00
|
|
|
ret = 4;
|
2012-11-05 19:23:02 -05:00
|
|
|
goto done_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; i < config->interface[0].altsetting[0].bNumEndpoints ; i++) {
|
2012-11-07 18:29:47 -05:00
|
|
|
if ((config->interface[0].altsetting[0].endpoint[i].bmAttributes & 3) == LIBUSB_TRANSFER_TYPE_BULK) {
|
|
|
|
if (config->interface[0].altsetting[0].endpoint[i].bEndpointAddress & LIBUSB_ENDPOINT_IN)
|
|
|
|
endp_up = config->interface[0].altsetting[0].endpoint[i].bEndpointAddress;
|
2012-11-05 19:23:02 -05:00
|
|
|
else
|
2012-11-07 18:29:47 -05:00
|
|
|
endp_down = config->interface[0].altsetting[0].endpoint[i].bEndpointAddress;
|
2012-11-05 19:23:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 23:21:33 -04:00
|
|
|
top:
|
|
|
|
/* Read in the printer status */
|
2012-11-05 19:23:02 -05:00
|
|
|
ret = libusb_bulk_transfer(dev, endp_up,
|
2012-10-26 23:21:33 -04:00
|
|
|
rdbuf,
|
2012-10-28 12:19:17 -04:00
|
|
|
READBACK_LEN,
|
2012-10-26 23:21:33 -04:00
|
|
|
&num,
|
|
|
|
2000);
|
|
|
|
if (ret < 0) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("libusb error %d: (%d/%d from 0x%02x)\n", ret, num, READBACK_LEN, endp_up);
|
2012-11-06 22:20:42 -05:00
|
|
|
ret = 4;
|
2012-11-05 18:51:32 -05:00
|
|
|
goto done_claimed;
|
2012-10-26 23:21:33 -04:00
|
|
|
}
|
|
|
|
|
2012-10-28 12:19:17 -04:00
|
|
|
if (memcmp(rdbuf, rdbuf2, READBACK_LEN)) {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("readback: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
|
2012-10-26 23:21:33 -04:00
|
|
|
rdbuf[0], rdbuf[1], rdbuf[2], rdbuf[3],
|
|
|
|
rdbuf[4], rdbuf[5], rdbuf[6], rdbuf[7],
|
|
|
|
rdbuf[8], rdbuf[9], rdbuf[10], rdbuf[11]);
|
2012-10-28 12:19:17 -04:00
|
|
|
memcpy(rdbuf2, rdbuf, READBACK_LEN);
|
2012-10-26 23:21:33 -04:00
|
|
|
} else {
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
if (state != last_state) {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("last_state %d new %d\n", last_state, state);
|
2012-10-26 23:21:33 -04:00
|
|
|
last_state = state;
|
|
|
|
}
|
|
|
|
fflush(stderr);
|
|
|
|
|
2012-11-19 21:45:49 -05:00
|
|
|
if (!memcmp(rdbuf, printers[printer_type].error_readback, READBACK_LEN)) {
|
|
|
|
DEBUG("error condition; aborting. (Out of ribbon/paper?)\n");
|
|
|
|
ret = 4;
|
|
|
|
goto done_claimed;
|
|
|
|
}
|
|
|
|
|
2012-10-26 23:21:33 -04:00
|
|
|
switch(state) {
|
|
|
|
case S_IDLE:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].init_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_READY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY:
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending init sequence (%d bytes)\n", printers[printer_type].init_length);
|
2012-10-26 23:21:33 -04:00
|
|
|
|
|
|
|
/* Send printer init */
|
2012-11-05 19:23:02 -05:00
|
|
|
ret = libusb_bulk_transfer(dev, endp_down,
|
2012-10-26 23:21:33 -04:00
|
|
|
buffer,
|
2012-10-28 12:19:17 -04:00
|
|
|
printers[printer_type].init_length,
|
2012-10-26 23:21:33 -04:00
|
|
|
&num,
|
|
|
|
2000);
|
|
|
|
if (ret < 0) {
|
2012-11-13 07:15:19 -05:00
|
|
|
ERROR("libusb error %d: (%d/%d to 0x%02x)\n", ret, num, printers[printer_type].init_length, endp_down);
|
2012-11-06 22:20:42 -05:00
|
|
|
ret = 4;
|
2012-11-05 18:51:32 -05:00
|
|
|
goto done_claimed;
|
2012-10-26 23:21:33 -04:00
|
|
|
}
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-10-26 23:21:33 -04:00
|
|
|
/* Realign plane data to start of buffer.. */
|
2012-10-28 12:19:17 -04:00
|
|
|
memmove(buffer, buffer+printers[printer_type].init_length,
|
|
|
|
MAX_HEADER-printers[printer_type].init_length);
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_INIT_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_INIT_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].ready_y_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_READY_Y;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY_Y:
|
|
|
|
if (bw_mode)
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending BLACK plane\n");
|
2012-10-26 23:21:33 -04:00
|
|
|
else
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending YELLOW plane\n");
|
2012-11-06 22:44:56 -05:00
|
|
|
ret = dump_data_libusb(plane_len, MAX_HEADER-printers[printer_type].init_length, data_fd, dev, endp_down, buffer, BUF_LEN);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = 4;
|
|
|
|
goto done_claimed;
|
|
|
|
}
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_Y_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_Y_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].ready_m_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-26 23:35:48 -04:00
|
|
|
if (bw_mode)
|
|
|
|
state = S_PRINTER_DONE;
|
|
|
|
else
|
|
|
|
state = S_PRINTER_READY_M;
|
2012-10-26 23:21:33 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY_M:
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending MAGENTA plane\n");
|
2012-11-06 22:44:56 -05:00
|
|
|
ret = dump_data_libusb(plane_len, 0, data_fd, dev, endp_down, buffer, BUF_LEN);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = 4;
|
|
|
|
goto done_claimed;
|
|
|
|
}
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_M_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_M_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].ready_c_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_READY_C;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_READY_C:
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending CYAN plane\n");
|
2012-11-06 22:44:56 -05:00
|
|
|
ret = dump_data_libusb(plane_len, 0, data_fd, dev, endp_down, buffer, BUF_LEN);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = 4;
|
|
|
|
goto done_claimed;
|
|
|
|
}
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_C_SENT;
|
|
|
|
break;
|
|
|
|
case S_PRINTER_C_SENT:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (!fancy_memcmp(rdbuf, printers[printer_type].done_c_readback, READBACK_LEN, paper_code_offset, paper_code)) {
|
2012-10-26 23:21:33 -04:00
|
|
|
state = S_PRINTER_DONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case S_PRINTER_DONE:
|
2012-10-28 12:19:17 -04:00
|
|
|
if (printers[printer_type].foot_length) {
|
2012-11-06 22:34:23 -05:00
|
|
|
DEBUG("Sending cleanup sequence\n");
|
2012-11-06 22:44:56 -05:00
|
|
|
ret = dump_data_libusb(printers[printer_type].foot_length, 0, data_fd, dev, endp_down, buffer, BUF_LEN);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = 4;
|
|
|
|
goto done_claimed;
|
|
|
|
}
|
2012-10-26 23:21:33 -04:00
|
|
|
}
|
|
|
|
state = S_FINISHED;
|
2012-11-13 19:30:43 -05:00
|
|
|
/* Intentional Fallthrough */
|
2012-10-26 23:21:33 -04:00
|
|
|
case S_FINISHED:
|
2012-11-13 19:30:43 -05:00
|
|
|
DEBUG("All data sent to printer!\n");
|
2012-10-26 23:21:33 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (state != S_FINISHED)
|
|
|
|
goto top;
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-10-26 23:21:33 -04:00
|
|
|
/* Done printing */
|
2012-11-05 18:51:32 -05:00
|
|
|
|
|
|
|
done_claimed:
|
2012-10-27 09:56:47 -04:00
|
|
|
libusb_release_interface(dev, iface);
|
2012-10-26 22:06:47 -04:00
|
|
|
|
2012-11-05 20:33:10 -05:00
|
|
|
done_close:
|
2012-10-26 22:06:47 -04:00
|
|
|
if (claimed)
|
2012-10-27 09:56:47 -04:00
|
|
|
libusb_attach_kernel_driver(dev, iface);
|
2012-10-26 22:06:47 -04:00
|
|
|
|
|
|
|
libusb_close(dev);
|
|
|
|
|
|
|
|
done:
|
|
|
|
libusb_free_device_list(list, 1);
|
|
|
|
libusb_exit(ctx);
|
2012-10-26 23:21:33 -04:00
|
|
|
|
|
|
|
close(data_fd);
|
|
|
|
|
2012-10-26 22:06:47 -04:00
|
|
|
return ret;
|
|
|
|
}
|