2013-06-27 23:02:34 -04:00
|
|
|
/*
|
|
|
|
* CUPS Backend common code
|
|
|
|
*
|
|
|
|
* (c) 2013 Solomon Peachy <pizza@shaftnet.org>
|
|
|
|
*
|
|
|
|
* The latest version of this program can be found at:
|
|
|
|
*
|
|
|
|
* http://git.shaftnet.org/git/gitweb.cgi?p=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]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
#include "backend_common.h"
|
2013-06-27 23:02:34 -04:00
|
|
|
|
2013-07-24 13:49:06 -04:00
|
|
|
#define BACKEND_VERSION "0.16"
|
2013-07-18 08:46:44 -04:00
|
|
|
#ifndef URI_PREFIX
|
|
|
|
#define URI_PREFIX "gutenprint+usb"
|
2013-06-27 23:02:34 -04:00
|
|
|
#endif
|
|
|
|
|
2013-07-17 23:39:31 -04:00
|
|
|
/* Support Functions */
|
|
|
|
|
2013-06-27 23:02:34 -04:00
|
|
|
#define ID_BUF_SIZE 2048
|
|
|
|
static char *get_device_id(struct libusb_device_handle *dev)
|
|
|
|
{
|
2013-07-17 23:39:31 -04:00
|
|
|
int length;
|
2013-06-27 23:02:34 -04:00
|
|
|
int claimed = 0;
|
|
|
|
int iface = 0;
|
|
|
|
char *buf = malloc(ID_BUF_SIZE + 1);
|
|
|
|
|
|
|
|
claimed = libusb_kernel_driver_active(dev, iface);
|
|
|
|
if (claimed)
|
|
|
|
libusb_detach_kernel_driver(dev, iface);
|
|
|
|
|
|
|
|
libusb_claim_interface(dev, iface);
|
|
|
|
|
|
|
|
if (libusb_control_transfer(dev,
|
|
|
|
LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN |
|
|
|
|
LIBUSB_RECIPIENT_INTERFACE,
|
|
|
|
0, 0,
|
|
|
|
(iface << 8),
|
|
|
|
(unsigned char *)buf, ID_BUF_SIZE, 5000) < 0)
|
|
|
|
{
|
|
|
|
*buf = '\0';
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* length is the first two bytes, MSB first */
|
|
|
|
length = (((unsigned)buf[0] & 255) << 8) |
|
|
|
|
((unsigned)buf[1] & 255);
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
if (length > ID_BUF_SIZE || length < 14)
|
|
|
|
length = (((unsigned)buf[1] & 255) << 8) |
|
|
|
|
((unsigned)buf[0] & 255);
|
|
|
|
|
|
|
|
if (length > ID_BUF_SIZE)
|
|
|
|
length = ID_BUF_SIZE;
|
|
|
|
|
|
|
|
if (length < 14) {
|
|
|
|
*buf = '\0';
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move, and terminate */
|
|
|
|
memmove(buf, buf + 2, length);
|
|
|
|
buf[length] = '\0';
|
|
|
|
|
|
|
|
done:
|
|
|
|
libusb_release_interface(dev, iface);
|
2013-07-17 23:40:48 -04:00
|
|
|
#if 0
|
2013-06-27 23:02:34 -04:00
|
|
|
if (claimed)
|
|
|
|
libusb_attach_kernel_driver(dev, iface);
|
2013-07-17 23:40:48 -04:00
|
|
|
#endif
|
2013-06-27 23:02:34 -04:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-07-17 23:39:31 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
int send_data(struct libusb_device_handle *dev, uint8_t endp,
|
|
|
|
uint8_t *buf, int len)
|
2013-06-27 23:02:34 -04:00
|
|
|
{
|
|
|
|
int num;
|
|
|
|
|
2013-06-30 11:15:03 -04:00
|
|
|
while (len) {
|
|
|
|
int ret = libusb_bulk_transfer(dev, endp,
|
2013-07-06 10:12:19 -04:00
|
|
|
buf, (len > 65536) ? 65536: len,
|
|
|
|
&num, 5000);
|
2013-06-30 11:15:03 -04:00
|
|
|
if (ret < 0) {
|
|
|
|
ERROR("Failure to send data to printer (libusb error %d: (%d/%d to 0x%02x))\n", ret, num, len, endp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
len -= num;
|
|
|
|
buf += num;
|
2013-06-30 12:05:32 -04:00
|
|
|
// DEBUG("Sent %d (%d remaining) to 0x%x\n", num, len, endp);
|
2013-06-27 23:02:34 -04:00
|
|
|
}
|
2013-06-30 11:15:03 -04:00
|
|
|
|
2013-06-27 23:02:34 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
int terminate = 0;
|
2013-06-27 23:02:34 -04:00
|
|
|
|
2013-06-30 11:15:03 -04:00
|
|
|
static void sigterm_handler(int signum) {
|
2013-06-27 23:02:34 -04:00
|
|
|
terminate = 1;
|
|
|
|
INFO("Job Cancelled");
|
|
|
|
}
|
2013-06-30 11:15:03 -04:00
|
|
|
|
2013-07-06 20:58:23 -04:00
|
|
|
static char *sanitize_string(char *str) {
|
|
|
|
int len = strlen(str);
|
|
|
|
|
|
|
|
while(len && (str[len-1] <= 0x20)) {
|
|
|
|
str[len-1] = 0;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2013-06-30 11:32:41 -04:00
|
|
|
static int print_scan_output(struct libusb_device *device,
|
|
|
|
struct libusb_device_descriptor *desc,
|
|
|
|
char *prefix, char *manuf2,
|
2013-07-17 22:43:49 -04:00
|
|
|
int found, int match,
|
2013-07-24 13:49:06 -04:00
|
|
|
int scan_only, char *match_serno,
|
|
|
|
struct dyesub_backend *backend)
|
2013-06-30 11:15:03 -04:00
|
|
|
{
|
2013-06-30 11:32:41 -04:00
|
|
|
struct libusb_device_handle *dev;
|
|
|
|
|
|
|
|
unsigned char product[STR_LEN_MAX] = "";
|
|
|
|
unsigned char serial[STR_LEN_MAX] = "";
|
|
|
|
unsigned char manuf[STR_LEN_MAX] = "";
|
|
|
|
|
|
|
|
if (libusb_open(device, &dev)) {
|
|
|
|
ERROR("Could not open device %04x:%04x\n", desc->idVendor, desc->idProduct);
|
2013-06-30 12:05:32 -04:00
|
|
|
found = -1;
|
|
|
|
goto abort;
|
2013-06-30 11:32:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Query detailed info */
|
|
|
|
if (desc->iManufacturer) {
|
|
|
|
libusb_get_string_descriptor_ascii(dev, desc->iManufacturer, manuf, STR_LEN_MAX);
|
2013-07-06 20:58:23 -04:00
|
|
|
sanitize_string((char*)manuf);
|
2013-06-30 11:32:41 -04:00
|
|
|
}
|
|
|
|
if (desc->iProduct) {
|
|
|
|
libusb_get_string_descriptor_ascii(dev, desc->iProduct, product, STR_LEN_MAX);
|
2013-07-06 20:58:23 -04:00
|
|
|
sanitize_string((char*)product);
|
2013-06-30 11:15:03 -04:00
|
|
|
}
|
2013-06-30 11:32:41 -04:00
|
|
|
if (desc->iSerialNumber) {
|
|
|
|
libusb_get_string_descriptor_ascii(dev, desc->iSerialNumber, serial, STR_LEN_MAX);
|
2013-07-06 20:58:23 -04:00
|
|
|
sanitize_string((char*)serial);
|
2013-07-24 13:49:06 -04:00
|
|
|
} else if (backend->query_serno) {
|
|
|
|
backend->query_serno(dev, (char*)serial, STR_LEN_MAX);
|
2013-06-30 11:32:41 -04:00
|
|
|
}
|
|
|
|
|
2013-07-16 21:08:33 -04:00
|
|
|
if (!strlen((char*)serial)) {
|
|
|
|
uint8_t bus_num;
|
|
|
|
uint8_t port_num;
|
|
|
|
|
|
|
|
bus_num = libusb_get_bus_number(device);
|
2013-07-20 19:10:15 -04:00
|
|
|
#if defined(LIBUSBX_API_VERSION)
|
2013-07-16 21:08:33 -04:00
|
|
|
port_num = libusb_get_port_number(device);
|
2013-07-20 19:10:15 -04:00
|
|
|
#else
|
|
|
|
port_num = 255;
|
|
|
|
#endif
|
2013-07-16 21:08:33 -04:00
|
|
|
sprintf((char*)serial, "NONE_B%03d_D%03d", bus_num, port_num);
|
|
|
|
}
|
2013-06-30 11:32:41 -04:00
|
|
|
|
2013-07-18 23:54:43 -04:00
|
|
|
DEBUG("%sVID: %04X PID: %04X Manuf: '%s' Product: '%s' Serial: '%s'\n",
|
2013-06-30 12:05:32 -04:00
|
|
|
match ? "MATCH: " : "",
|
2013-07-18 23:54:43 -04:00
|
|
|
desc->idVendor, desc->idProduct, manuf, product, serial);
|
2013-06-30 11:15:03 -04:00
|
|
|
|
2013-07-17 22:43:49 -04:00
|
|
|
if (scan_only) {
|
2013-06-30 11:32:41 -04:00
|
|
|
/* URL-ify model. */
|
|
|
|
char buf[128]; // XXX ugly..
|
|
|
|
int j = 0, k = 0;
|
2013-07-06 20:58:23 -04:00
|
|
|
char *ieee_id = get_device_id(dev);
|
2013-06-30 11:32:41 -04:00
|
|
|
while (*(product + j + strlen(manuf2))) {
|
2013-07-06 20:58:23 -04:00
|
|
|
buf[k] = *(product + j + (strlen(manuf2) ? (strlen(manuf2) + 1) : 0));
|
2013-06-30 11:32:41 -04:00
|
|
|
if(buf[k] == ' ') {
|
|
|
|
buf[k++] = '%';
|
|
|
|
buf[k++] = '2';
|
|
|
|
buf[k] = '0';
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
j++;
|
|
|
|
}
|
2013-07-06 20:58:23 -04:00
|
|
|
buf[k] = 0;
|
2013-06-30 11:32:41 -04:00
|
|
|
|
2013-07-18 23:54:43 -04:00
|
|
|
fprintf(stdout, "direct %s://%s/%s?serial=%s \"%s\" \"%s\" \"%s\" \"\"\n",
|
2013-07-06 20:58:23 -04:00
|
|
|
prefix, strlen(manuf2) ? manuf2 : (char*)manuf,
|
2013-06-30 11:32:41 -04:00
|
|
|
buf, serial, product, product,
|
|
|
|
ieee_id);
|
|
|
|
|
|
|
|
if (ieee_id)
|
|
|
|
free(ieee_id);
|
|
|
|
}
|
2013-06-30 11:15:03 -04:00
|
|
|
|
2013-06-30 11:32:41 -04:00
|
|
|
/* If a serial number was passed down, use it. */
|
|
|
|
if (found && match_serno &&
|
|
|
|
strcmp(match_serno, (char*)serial)) {
|
|
|
|
found = -1;
|
|
|
|
}
|
2013-06-30 11:15:03 -04:00
|
|
|
|
2013-06-30 11:32:41 -04:00
|
|
|
libusb_close(dev);
|
2013-06-30 12:05:32 -04:00
|
|
|
abort:
|
2013-06-30 11:32:41 -04:00
|
|
|
return found;
|
2013-06-30 11:15:03 -04:00
|
|
|
}
|
2013-07-17 23:39:31 -04:00
|
|
|
|
2013-07-18 23:39:36 -04:00
|
|
|
static struct dyesub_backend *backends[] = {
|
|
|
|
&canonselphy_backend,
|
|
|
|
&kodak6800_backend,
|
|
|
|
&kodak1400_backend,
|
|
|
|
&shinkos2145_backend,
|
|
|
|
&updr150_backend,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2013-07-17 23:39:31 -04:00
|
|
|
static int find_and_enumerate(struct libusb_context *ctx,
|
|
|
|
struct libusb_device ***list,
|
|
|
|
char *match_serno,
|
|
|
|
int printer_type,
|
2013-07-19 07:44:20 -04:00
|
|
|
int media_type, /* XXX for future use */
|
2013-07-17 23:39:31 -04:00
|
|
|
int scan_only)
|
|
|
|
{
|
|
|
|
int num;
|
2013-07-18 23:39:36 -04:00
|
|
|
int i, j, k;
|
2013-07-17 23:39:31 -04:00
|
|
|
int found = -1;
|
|
|
|
|
|
|
|
/* Enumerate and find suitable device */
|
|
|
|
num = libusb_get_device_list(ctx, list);
|
|
|
|
|
|
|
|
for (i = 0 ; i < num ; i++) {
|
|
|
|
struct libusb_device_descriptor desc;
|
|
|
|
int match = 0;
|
|
|
|
libusb_get_device_descriptor((*list)[i], &desc);
|
2013-07-18 23:39:36 -04:00
|
|
|
|
|
|
|
for (k = 0 ; backends[k] ; k++) {
|
|
|
|
for (j = 0 ; backends[k]->devices[j].vid ; j++) {
|
|
|
|
if (desc.idVendor == backends[k]->devices[j].vid &&
|
|
|
|
desc.idProduct == backends[k]->devices[j].pid) {
|
|
|
|
match = 1;
|
2013-07-19 19:53:08 -04:00
|
|
|
if (printer_type == P_ANY ||
|
|
|
|
printer_type == backends[k]->devices[j].type)
|
2013-07-18 23:39:36 -04:00
|
|
|
found = i;
|
2013-07-18 23:54:43 -04:00
|
|
|
goto match;
|
2013-07-18 23:39:36 -04:00
|
|
|
}
|
2013-07-17 23:39:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 23:54:43 -04:00
|
|
|
match:
|
2013-07-17 23:39:31 -04:00
|
|
|
if (!match) {
|
|
|
|
if (getenv("EXTRA_PID") && getenv("EXTRA_TYPE") && getenv("EXTRA_VID")) {
|
|
|
|
int pid = strtol(getenv("EXTRA_PID"), NULL, 16);
|
|
|
|
int vid = strtol(getenv("EXTRA_VID"), NULL, 16);
|
|
|
|
int type = atoi(getenv("EXTRA_TYPE"));
|
|
|
|
if (vid == desc.idVendor &&
|
|
|
|
pid == desc.idProduct) {
|
|
|
|
match = 1;
|
2013-07-19 22:21:15 -04:00
|
|
|
if (printer_type == P_ANY ||
|
|
|
|
printer_type == type)
|
2013-07-17 23:39:31 -04:00
|
|
|
found = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
found = print_scan_output((*list)[i], &desc,
|
2013-07-18 23:39:36 -04:00
|
|
|
URI_PREFIX, backends[k]->devices[j].manuf_str,
|
2013-07-17 23:39:31 -04:00
|
|
|
found, (found == i),
|
2013-07-24 13:49:06 -04:00
|
|
|
scan_only, match_serno,
|
|
|
|
backends[k]);
|
2013-07-17 23:39:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
2013-07-18 08:46:44 -04:00
|
|
|
|
2013-07-18 12:45:35 -04:00
|
|
|
static struct dyesub_backend *find_backend(char *uri_prefix)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dyesub_backend *backend;
|
|
|
|
|
|
|
|
if (!uri_prefix)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
backend = backends[i];
|
|
|
|
if (!backend)
|
|
|
|
return NULL;
|
|
|
|
if (!strcmp(uri_prefix, backend->uri_prefix))
|
|
|
|
return backend;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
/* MAIN */
|
|
|
|
|
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
2013-07-20 19:10:15 -04:00
|
|
|
struct libusb_context *ctx = NULL;
|
|
|
|
struct libusb_device **list = NULL;
|
2013-07-18 08:46:44 -04:00
|
|
|
struct libusb_device_handle *dev;
|
|
|
|
struct libusb_config_descriptor *config;
|
|
|
|
|
|
|
|
struct dyesub_backend *backend;
|
|
|
|
void * backend_ctx = NULL;
|
|
|
|
|
|
|
|
uint8_t endp_up = 0;
|
|
|
|
uint8_t endp_down = 0;
|
|
|
|
|
|
|
|
int data_fd = fileno(stdin);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int claimed;
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
int iface = 0;
|
|
|
|
int found = -1;
|
|
|
|
int copies = 1;
|
2013-07-18 22:10:56 -04:00
|
|
|
int jobid = 0;
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
char *uri = getenv("DEVICE_URI");
|
|
|
|
char *use_serno = NULL;
|
2013-07-18 18:03:40 -04:00
|
|
|
int query_only = 0;
|
2013-07-18 23:22:24 -04:00
|
|
|
int printer_type = P_ANY;
|
2013-07-18 08:46:44 -04:00
|
|
|
|
2013-07-18 12:45:35 -04:00
|
|
|
DEBUG("Multi-Call Gutenprint DyeSub CUPS Backend version %s\n",
|
2013-07-18 08:46:44 -04:00
|
|
|
BACKEND_VERSION);
|
2013-07-18 12:45:35 -04:00
|
|
|
DEBUG("Copyright 2007-2013 Solomon Peachy\n");
|
2013-07-18 08:46:44 -04:00
|
|
|
|
|
|
|
/* Cmdline help */
|
|
|
|
if (argc < 2) {
|
2013-07-18 12:45:35 -04:00
|
|
|
char *ptr = strrchr(argv[0], '/');
|
|
|
|
if (ptr)
|
|
|
|
ptr++;
|
|
|
|
else
|
|
|
|
ptr = argv[0];
|
|
|
|
backend = find_backend(getenv("BACKEND"));
|
|
|
|
if (!backend)
|
|
|
|
backend = find_backend(ptr);
|
|
|
|
|
|
|
|
if (!backend) {
|
|
|
|
DEBUG("CUPS Usage:\n\tDEVICE_URI=someuri %s job user title num-copies options [ filename ]\n\n",
|
|
|
|
URI_PREFIX);
|
2013-07-18 18:03:40 -04:00
|
|
|
DEBUG("Internal Backends: (prefix with SERIAL=serno for specific device)\n");
|
2013-07-18 12:45:35 -04:00
|
|
|
for (i = 0; ; i++) {
|
|
|
|
backend = backends[i];
|
|
|
|
if (!backend)
|
|
|
|
break;
|
|
|
|
DEBUG(" %s backend version %s (BACKEND=%s)\n",
|
|
|
|
backend->name, backend->version, backend->uri_prefix);
|
|
|
|
DEBUG("\t\t%s [ infile | - ]\n",
|
|
|
|
backend->uri_prefix);
|
|
|
|
|
|
|
|
if (backend->cmdline_usage) {
|
|
|
|
backend->cmdline_usage(backend->uri_prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG(" %s backend version %s (BACKEND=%s)\n",
|
|
|
|
backend->name, backend->version, backend->uri_prefix);
|
|
|
|
DEBUG(" Standalone Usage: (prefix with SERIAL=serno for specific device)\n");
|
|
|
|
DEBUG("\t\t%s [ infile | - ]\n",
|
|
|
|
backend->uri_prefix);
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
if (backend->cmdline_usage) {
|
|
|
|
backend->cmdline_usage(backend->uri_prefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libusb_init(&ctx);
|
2013-07-19 07:44:20 -04:00
|
|
|
find_and_enumerate(ctx, &list, NULL, P_ANY, -1, 1);
|
2013-07-18 08:46:44 -04:00
|
|
|
libusb_free_device_list(list, 1);
|
|
|
|
libusb_exit(ctx);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we running as a CUPS backend? */
|
|
|
|
if (uri) {
|
2013-07-18 22:10:56 -04:00
|
|
|
if (argv[1])
|
|
|
|
jobid = atoi(argv[1]);
|
2013-07-18 08:46:44 -04:00
|
|
|
if (argv[4])
|
|
|
|
copies = atoi(argv[4]);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we're using BLOCKING I/O */
|
|
|
|
i = fcntl(data_fd, F_GETFL, 0);
|
|
|
|
if (i < 0) {
|
|
|
|
perror("ERROR:Can't open input");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
i &= ~O_NONBLOCK;
|
|
|
|
i = fcntl(data_fd, F_SETFL, 0);
|
|
|
|
if (i < 0) {
|
|
|
|
perror("ERROR:Can't open input");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-18 08:51:02 -04:00
|
|
|
|
2013-07-18 12:45:35 -04:00
|
|
|
/* Figure out backend */
|
|
|
|
{
|
|
|
|
char *ptr = strchr (uri, ':');
|
|
|
|
if (!ptr) {
|
|
|
|
ERROR("Invalid URI prefix (%s)\n", uri);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*ptr = 0;
|
|
|
|
backend = find_backend(uri);
|
|
|
|
if (!backend) {
|
|
|
|
ERROR("Invalid backend (%s)\n", uri);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*ptr = ':';
|
2013-07-18 08:46:44 -04:00
|
|
|
}
|
2013-07-18 08:51:02 -04:00
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
use_serno = strchr(uri, '=');
|
|
|
|
if (!use_serno || !*(use_serno+1)) {
|
|
|
|
ERROR("Invalid URI (%s)\n", uri);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
use_serno++;
|
|
|
|
} else {
|
|
|
|
use_serno = getenv("DEVICE");
|
|
|
|
|
2013-07-18 12:45:35 -04:00
|
|
|
/* find backend */
|
|
|
|
backend = find_backend(getenv("BACKEND"));
|
|
|
|
if (!backend) {
|
|
|
|
char *ptr = strrchr(argv[0], '/');
|
|
|
|
if (ptr)
|
|
|
|
ptr++;
|
|
|
|
else
|
|
|
|
ptr = argv[0];
|
|
|
|
backend = find_backend(ptr);
|
|
|
|
}
|
|
|
|
if (!backend) {
|
|
|
|
ERROR("Invalid backend (%s)\n", uri);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-18 08:51:02 -04:00
|
|
|
|
2013-07-18 22:10:56 -04:00
|
|
|
srand(getpid());
|
|
|
|
jobid = rand();
|
|
|
|
|
2013-07-19 10:13:35 -04:00
|
|
|
if (backend->cmdline_arg && backend->cmdline_arg(NULL, 0, argv[1], argv[2])) {
|
|
|
|
query_only = 1;
|
|
|
|
} 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);
|
|
|
|
}
|
2013-07-18 08:46:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ignore SIGPIPE */
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
signal(SIGTERM, sigterm_handler);
|
|
|
|
|
2013-07-19 09:23:53 -04:00
|
|
|
/* Initialize backend */
|
|
|
|
backend_ctx = backend->init();
|
|
|
|
|
2013-07-19 10:13:35 -04:00
|
|
|
/* Parse printjob if necessary */
|
|
|
|
if (!query_only && backend->early_parse) {
|
2013-07-19 11:12:14 -04:00
|
|
|
printer_type = backend->early_parse(backend_ctx, data_fd);
|
2013-07-19 10:13:35 -04:00
|
|
|
if (printer_type < 0) {
|
|
|
|
ret = 4;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 08:46:44 -04:00
|
|
|
/* Libusb setup */
|
|
|
|
libusb_init(&ctx);
|
2013-07-20 19:10:15 -04:00
|
|
|
/* Enumerate devices */
|
2013-07-19 07:44:20 -04:00
|
|
|
found = find_and_enumerate(ctx, &list, use_serno, printer_type, -1, 0);
|
2013-07-18 08:46:44 -04:00
|
|
|
|
|
|
|
if (found == -1) {
|
|
|
|
ERROR("Printer open failure (No suitable printers found!)\n");
|
|
|
|
ret = 3;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = libusb_open(list[found], &dev);
|
|
|
|
if (ret) {
|
|
|
|
ERROR("Printer open failure (Need to be root?) (%d)\n", ret);
|
|
|
|
ret = 4;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
claimed = libusb_kernel_driver_active(dev, iface);
|
|
|
|
if (claimed) {
|
|
|
|
ret = libusb_detach_kernel_driver(dev, iface);
|
|
|
|
if (ret) {
|
|
|
|
ERROR("Printer open failure (Could not detach printer from kernel)\n");
|
|
|
|
ret = 4;
|
|
|
|
goto done_close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = libusb_claim_interface(dev, iface);
|
|
|
|
if (ret) {
|
|
|
|
ERROR("Printer open failure (Could not claim printer interface)\n");
|
|
|
|
ret = 4;
|
|
|
|
goto done_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = libusb_get_active_config_descriptor(list[found], &config);
|
|
|
|
if (ret) {
|
|
|
|
ERROR("Printer open failure (Could not fetch config descriptor)\n");
|
|
|
|
ret = 4;
|
|
|
|
goto done_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0 ; i < config->interface[0].altsetting[0].bNumEndpoints ; i++) {
|
|
|
|
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;
|
|
|
|
else
|
|
|
|
endp_down = config->interface[0].altsetting[0].endpoint[i].bEndpointAddress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 09:23:53 -04:00
|
|
|
/* Attach backend to device */
|
|
|
|
backend->attach(backend_ctx, dev, endp_up, endp_down, jobid);
|
2013-07-18 18:03:40 -04:00
|
|
|
|
|
|
|
if (query_only) {
|
|
|
|
backend->cmdline_arg(backend_ctx, 1, argv[1], argv[2]);
|
|
|
|
goto done_claimed;
|
|
|
|
}
|
2013-07-18 08:46:44 -04:00
|
|
|
|
|
|
|
/* Read in data */
|
|
|
|
if (backend->read_parse(backend_ctx, data_fd))
|
2013-07-18 23:22:24 -04:00
|
|
|
goto done_claimed;
|
2013-07-18 08:46:44 -04:00
|
|
|
close(data_fd);
|
|
|
|
|
|
|
|
/* Time for the main processing loop */
|
|
|
|
INFO("Printing started (%d copies)\n", copies);
|
|
|
|
|
|
|
|
ret = backend->main_loop(backend_ctx, copies);
|
|
|
|
if (ret)
|
|
|
|
goto done_claimed;
|
|
|
|
|
|
|
|
/* Done printing */
|
|
|
|
INFO("All printing done\n");
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
done_claimed:
|
|
|
|
libusb_release_interface(dev, iface);
|
|
|
|
|
|
|
|
done_close:
|
|
|
|
#if 0
|
|
|
|
if (claimed)
|
|
|
|
libusb_attach_kernel_driver(dev, iface);
|
|
|
|
#endif
|
|
|
|
libusb_close(dev);
|
|
|
|
done:
|
|
|
|
|
|
|
|
if (backend && backend_ctx)
|
|
|
|
backend->teardown(backend_ctx);
|
|
|
|
|
2013-07-20 19:10:15 -04:00
|
|
|
if (list)
|
|
|
|
libusb_free_device_list(list, 1);
|
|
|
|
if (ctx)
|
|
|
|
libusb_exit(ctx);
|
2013-07-18 08:46:44 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|