2
* "$Id: usb-libusb.c 10908 2013-03-14 14:31:07Z mike $"
4
* LIBUSB interface code for CUPS.
6
* Copyright 2007-2013 by Apple Inc.
8
* These coded instructions, statements, and computer programs are the
9
* property of Apple Inc. and are protected by Federal copyright
10
* law. Distribution and use rights are outlined in the file "LICENSE.txt"
11
* which should have been included with this file. If this file is
12
* file is missing or damaged, see the license at "http://www.cups.org/".
16
* list_devices() - List the available printers.
17
* print_device() - Print a file to a USB device.
18
* close_device() - Close the connection to the USB printer.
19
* find_device() - Find or enumerate USB printers.
20
* get_device_id() - Get the IEEE-1284 device ID for the printer.
21
* list_cb() - List USB printers for discovery.
22
* make_device_uri() - Create a device URI for a USB printer.
23
* open_device() - Open a connection to the USB printer.
24
* print_cb() - Find a USB printer for printing.
25
* printer_class_soft_reset()' - Do the soft reset request specific to
27
* quirks() - Get the known quirks of a given printer model
28
* read_thread() - Thread to read the backchannel data on.
29
* sidechannel_thread() - Handle side-channel requests.
30
* soft_reset() - Send a soft reset to the device.
34
* Include necessary headers...
38
#include <cups/cups-private.h>
40
#include <sys/select.h>
41
#include <sys/types.h>
48
* WAIT_EOF_DELAY is number of seconds we'll wait for responses from
49
* the printer after we've finished sending all the data
53
#define WAIT_EOF_DELAY 7
54
#define WAIT_SIDE_DELAY 3
55
#define DEFAULT_TIMEOUT 5000L
62
typedef struct usb_printer_s /**** USB Printer Data ****/
64
struct libusb_device *device; /* Device info */
65
int conf, /* Configuration */
66
origconf, /* Original configuration */
67
iface, /* Interface */
68
altset, /* Alternate setting */
69
write_endp, /* Write endpoint */
70
read_endp, /* Read endpoint */
71
protocol, /* Protocol: 1 = Uni-di, 2 = Bi-di. */
72
usblp_attached, /* "usblp" kernel module attached? */
73
reset_after_job; /* Set to 1 by print_device() */
74
unsigned int quirks; /* Quirks flags */
75
struct libusb_device_handle *handle; /* Open handle to device */
78
typedef int (*usb_cb_t)(usb_printer_t *, const char *, const char *,
81
typedef struct usb_globals_s
83
usb_printer_t *printer; /* Printer */
85
pthread_mutex_t read_thread_mutex;
86
pthread_cond_t read_thread_cond;
90
pthread_mutex_t readwrite_lock_mutex;
91
pthread_cond_t readwrite_lock_cond;
94
int print_fd; /* File descriptor to print */
95
ssize_t print_bytes; /* Print bytes read */
98
int drain_output; /* Drain all pending output */
99
int bidi_flag; /* 0=unidirectional, 1=bidirectional */
101
pthread_mutex_t sidechannel_thread_mutex;
102
pthread_cond_t sidechannel_thread_cond;
103
int sidechannel_thread_stop;
104
int sidechannel_thread_done;
108
* Quirks: various printer quirks are handled by this table & its flags.
110
* This is copied from the usblp kernel module. So we can easily copy and paste
111
* new quirks from the module.
114
struct quirk_printer_struct {
120
#define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires
121
unidirectional mode (no INs/reads) */
122
#define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
123
#define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific
125
#define USBLP_QUIRK_BLACKLIST 0x8 /* these printers do not conform to the USB print spec */
126
#define USBLP_QUIRK_RESET 0x4000 /* After printing do a reset
128
#define USBLP_QUIRK_NO_REATTACH 0x8000 /* After printing we cannot re-attach
129
the usblp kernel module */
131
static const struct quirk_printer_struct quirk_printers[] = {
132
{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
133
{ 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
134
{ 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
135
{ 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
136
{ 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
137
{ 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
138
{ 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
139
{ 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
140
{ 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
141
{ 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
142
{ 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
143
{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
144
{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
145
{ 0x043d, 0x00f3, USBLP_QUIRK_NO_REATTACH }, /* Lexmark International,
146
Inc. (e250d), https://bugs.launchpad.net/bugs/1084164 */
147
{ 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820,
148
by zut <kernel@zut.de> */
149
{ 0x04a9, 0x1095, USBLP_QUIRK_BIDIR }, /* Canon, Inc. PIXMA iP6000D
150
Printer, https://bugs.launchpad.net/bugs/1160638 */
151
{ 0x04a9, 0x10a2, USBLP_QUIRK_BIDIR }, /* Canon, Inc. PIXMA iP4200
152
Printer, http://www.cups.org/str.php?L4155 */
153
{ 0x04a9, 0x10b6, USBLP_QUIRK_BIDIR }, /* Canon, Inc. PIXMA iP4300
154
Printer, https://bugs.launchpad.net/bugs/1032385 */
155
{ 0x04a9, 0x1721, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP210
156
https://bugzilla.redhat.com/show_bug.cgi?id=847923#c53 */
157
{ 0x04a9, 0x170c, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP500
158
Printer, https://bugs.launchpad.net/bugs/1032456 */
159
{ 0x04a9, 0x1717, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP510
160
Printer, https://bugs.launchpad.net/bugs/1050009 */
161
{ 0x04a9, 0x173d, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP550
162
Printer, http://www.cups.org/str.php?L4155 */
163
{ 0x04a9, 0x173e, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP560
164
Printer, http://www.cups.org/str.php?L4155 */
165
{ 0x04a9, 0x26a3, USBLP_QUIRK_NO_REATTACH }, /* Canon, Inc. MF4150
166
Printer, https://bugs.launchpad.net/bugs/1160638 */
167
{ 0x04f9, 0x001a, USBLP_QUIRK_NO_REATTACH }, /* Brother Industries, Ltd
168
HL-1430 Laser Printer,
169
https://bugs.launchpad.net/bugs/1038695 */
170
{ 0x04f9, 0x000d, USBLP_QUIRK_BIDIR |
171
USBLP_QUIRK_NO_REATTACH }, /* Brother Industries, Ltd
172
HL-1440 Laser Printer,
173
https://bugs.launchpad.net/bugs/1000253 */
174
{ 0x04f9, 0x000e, USBLP_QUIRK_BIDIR |
175
USBLP_QUIRK_NO_REATTACH }, /* Brother Industries, Ltd
176
HL-1450 Laser Printer,
177
https://bugs.launchpad.net/bugs/1000253 */
178
{ 0x06bc, 0x000b, USBLP_QUIRK_NO_REATTACH }, /* Oki Data Corp.
179
Okipage 14ex Printer,
180
https://bugs.launchpad.net/bugs/872483 */
181
{ 0x06bc, 0x01c7, USBLP_QUIRK_NO_REATTACH }, /* Oki Data Corp. B410d,
182
https://bugs.launchpad.net/bugs/872483 */
183
{ 0x04b8, 0x0001, USBLP_QUIRK_BIDIR |
184
USBLP_QUIRK_NO_REATTACH }, /* Seiko Epson Corp. Stylus Color 740 / Photo 750,
185
http://bugs.debian.org/697970 */
186
{ 0x04b8, 0x0005, USBLP_QUIRK_NO_REATTACH }, /* Seiko Epson Corp. Stylus Color 670,
187
https://bugs.launchpad.net/bugs/872483 */
188
{ 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt
190
{ 0x067b, 0x2305, USBLP_QUIRK_BIDIR |
191
USBLP_QUIRK_NO_REATTACH |
193
/* Prolific Technology, Inc. PL2305 Parallel Port
194
(USB -> Parallel adapter), https://bugs.launchpad.net/bugs/987485 */
195
{ 0x0924, 0x3ce9, USBLP_QUIRK_NO_REATTACH }, /* Xerox Phaser 3124
196
https://bugzilla.redhat.com/show_bug.cgi?id=867392 */
197
{ 0x0924, 0x4293, USBLP_QUIRK_NO_REATTACH }, /* Xerox WorkCentre 3210
198
https://bugs.launchpad.net/bugs/1102470 */
199
{ 0x1a86, 0x7584, USBLP_QUIRK_NO_REATTACH }, /* QinHeng Electronics
200
CH340S (USB -> Parallel adapter), https://bugs.launchpad.net/bugs/1000253 */
201
{ 0x04e8, 0x0000, USBLP_QUIRK_RESET }, /* All Samsung devices,
202
https://bugs.launchpad.net/bugs/1032456 */
203
{ 0x0a5f, 0x0000, USBLP_QUIRK_BIDIR }, /* All Zebra devices,
204
https://bugs.launchpad.net/bugs/1001028 */
206
{ 0x04a9, 0x304a, USBLP_QUIRK_BLACKLIST }, /* Canon CP-10 */
207
{ 0x04a9, 0x3063, USBLP_QUIRK_BLACKLIST }, /* Canon CP-100 */
208
{ 0x04a9, 0x307c, USBLP_QUIRK_BLACKLIST }, /* Canon CP-200 */
209
{ 0x04a9, 0x307d, USBLP_QUIRK_BLACKLIST }, /* Canon CP-300 */
210
{ 0x04a9, 0x30bd, USBLP_QUIRK_BLACKLIST }, /* Canon CP-220 */
211
{ 0x04a9, 0x30be, USBLP_QUIRK_BLACKLIST }, /* Canon CP-330 */
212
{ 0x04a9, 0x30f6, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP400 */
213
{ 0x04a9, 0x310b, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP600 */
214
{ 0x04a9, 0x3127, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP710 */
215
{ 0x04a9, 0x3128, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP510 */
216
{ 0x04a9, 0x3141, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES1 */
217
{ 0x04a9, 0x3142, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP730 */
218
{ 0x04a9, 0x3143, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP720 */
219
{ 0x04a9, 0x3170, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP750 */
220
{ 0x04a9, 0x3171, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP740 */
221
{ 0x04a9, 0x3185, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES2 */
222
{ 0x04a9, 0x3186, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES20 */
223
{ 0x04a9, 0x31aa, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP770 */
224
{ 0x04a9, 0x31ab, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP760 */
225
{ 0x04a9, 0x31b0, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES30 */
226
{ 0x04a9, 0x31dd, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP780 */
227
{ 0x04a9, 0x31ee, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES40 */
228
{ 0x04a9, 0x3214, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP800 */
229
{ 0x04a9, 0x3255, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP900 */
230
{ 0x04a9, 0x3256, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP810 */
231
{ 0x04a9, 0x30F5, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP500 */
232
{ 0x04a9, 0x31AF, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES3 */
233
/* MISSING PIDs: CP520, CP530, CP790 */
242
usb_globals_t g = { 0 }; /* Globals */
243
libusb_device **list; /* List of connected USB devices */
250
static int close_device(usb_printer_t *printer);
251
static usb_printer_t *find_device(usb_cb_t cb, const void *data);
252
static int get_device_id(usb_printer_t *printer, char *buffer,
254
static int list_cb(usb_printer_t *printer, const char *device_uri,
255
const char *device_id, const void *data);
256
static char *make_device_uri(usb_printer_t *printer,
257
const char *device_id,
258
char *uri, size_t uri_size);
259
static int open_device(usb_printer_t *printer, int verbose);
260
static int print_cb(usb_printer_t *printer, const char *device_uri,
261
const char *device_id, const void *data);
262
static int printer_class_soft_reset(usb_printer_t *printer);
263
static unsigned int quirks(int vendor, int product);
264
static void *read_thread(void *reference);
265
static void *sidechannel_thread(void *reference);
266
static void soft_reset(void);
270
* 'list_devices()' - List the available printers.
276
fputs("DEBUG: list_devices\n", stderr);
277
find_device(list_cb, NULL);
282
* 'print_device()' - Print a file to a USB device.
285
int /* O - Exit status */
286
print_device(const char *uri, /* I - Device URI */
287
const char *hostname, /* I - Hostname/manufacturer */
288
const char *resource, /* I - Resource/modelname */
289
char *options, /* I - Device options/serial number */
290
int print_fd, /* I - File descriptor to print */
291
int copies, /* I - Copies to print */
292
int argc, /* I - Number of command-line arguments (6 or 7) */
293
char *argv[]) /* I - Command-line arguments */
295
int bytes; /* Bytes written */
296
ssize_t total_bytes; /* Total bytes written */
297
struct sigaction action; /* Actions for POSIX signals */
298
int status = CUPS_BACKEND_OK,
299
/* Function results */
300
iostatus; /* Current IO status */
301
pthread_t read_thread_id, /* Read thread */
302
sidechannel_thread_id; /* Side-channel thread */
303
int have_sidechannel = 0, /* Was the side-channel thread started? */
304
have_backchannel = 0; /* Do we have a back channel? */
305
struct stat sidechannel_info; /* Side-channel file descriptor info */
306
unsigned char print_buffer[8192], /* Print data buffer */
307
*print_ptr; /* Pointer into print data buffer */
308
fd_set input_set; /* Input set for select() */
309
int nfds; /* Number of file descriptors */
310
struct timeval *timeout, /* Timeout pointer */
312
struct timespec cond_timeout; /* pthread condition timeout */
313
int num_opts; /* Number of options */
314
cups_option_t *opts; /* Options */
315
const char *val; /* Option value */
319
* See if the side-channel descriptor is valid...
322
have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) &&
323
S_ISSOCK(sidechannel_info.st_mode);
325
g.wait_eof = WAIT_EOF;
328
* Connect to the printer...
331
fprintf(stderr, "DEBUG: Printing on printer with URI: %s\n", uri);
332
while ((g.printer = find_device(print_cb, uri)) == NULL)
334
_cupsLangPrintFilter(stderr, "INFO",
335
_("Waiting for printer to become available."));
339
g.print_fd = print_fd;
342
* Some devices need a reset after finishing a job, these devices are
343
* marked with the USBLP_QUIRK_RESET quirk.
345
g.printer->reset_after_job = (g.printer->quirks & USBLP_QUIRK_RESET ? 1 : 0);
348
* If we are printing data from a print driver on stdin, ignore SIGTERM
349
* so that the driver can finish out any page data, e.g. to eject the
350
* current page. We only do this for stdin printing as otherwise there
351
* is no way to cancel a raw print job...
356
memset(&action, 0, sizeof(action));
358
sigemptyset(&action.sa_mask);
359
action.sa_handler = SIG_IGN;
360
sigaction(SIGTERM, &action, NULL);
364
* Start the side channel thread if the descriptor is valid...
367
pthread_mutex_init(&g.readwrite_lock_mutex, NULL);
368
pthread_cond_init(&g.readwrite_lock_cond, NULL);
369
g.readwrite_lock = 1;
371
if (have_sidechannel)
373
g.sidechannel_thread_stop = 0;
374
g.sidechannel_thread_done = 0;
376
pthread_cond_init(&g.sidechannel_thread_cond, NULL);
377
pthread_mutex_init(&g.sidechannel_thread_mutex, NULL);
379
if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL))
381
fprintf(stderr, "DEBUG: Fatal USB error.\n");
382
_cupsLangPrintFilter(stderr, "ERROR",
383
_("There was an unrecoverable USB error."));
384
fputs("DEBUG: Couldn't create side-channel thread.\n", stderr);
385
close_device(g.printer);
386
return (CUPS_BACKEND_STOP);
391
* Debug mode: If option "usb-unidir" is given, always deactivate
395
num_opts = cupsParseOptions(argv[5], 0, &opts);
396
val = cupsGetOption("usb-unidir", num_opts, opts);
397
if (val && strcasecmp(val, "no") && strcasecmp(val, "off") &&
398
strcasecmp(val, "false"))
400
g.printer->read_endp = -1;
401
fprintf(stderr, "DEBUG: Forced uni-directional communication "
402
"via \"usb-unidir\" option.\n");
406
* Debug mode: If option "usb-no-reattach" is given, do not re-attach
407
* the usblp kernel module after the job has completed.
410
val = cupsGetOption("usb-no-reattach", num_opts, opts);
411
if (val && strcasecmp(val, "no") && strcasecmp(val, "off") &&
412
strcasecmp(val, "false"))
414
g.printer->usblp_attached = 0;
415
fprintf(stderr, "DEBUG: Forced not re-attaching the usblp kernel module "
416
"after the job via \"usb-no-reattach\" option.\n");
420
* Get the read thread going...
423
if (g.printer->read_endp != -1)
425
have_backchannel = 1;
427
g.read_thread_stop = 0;
428
g.read_thread_done = 0;
430
pthread_cond_init(&g.read_thread_cond, NULL);
431
pthread_mutex_init(&g.read_thread_mutex, NULL);
433
if (pthread_create(&read_thread_id, NULL, read_thread, NULL))
435
fprintf(stderr, "DEBUG: Fatal USB error.\n");
436
_cupsLangPrintFilter(stderr, "ERROR",
437
_("There was an unrecoverable USB error."));
438
fputs("DEBUG: Couldn't create read thread.\n", stderr);
439
close_device(g.printer);
440
return (CUPS_BACKEND_STOP);
444
fprintf(stderr, "DEBUG: Uni-directional device/mode, back channel "
448
* The main thread sends the print file...
454
print_ptr = print_buffer;
456
while (status == CUPS_BACKEND_OK && copies-- > 0)
458
_cupsLangPrintFilter(stderr, "INFO", _("Sending data to printer."));
460
if (print_fd != STDIN_FILENO)
462
fputs("PAGE: 1 1\n", stderr);
463
lseek(print_fd, 0, SEEK_SET);
466
while (status == CUPS_BACKEND_OK)
471
FD_SET(print_fd, &input_set);
474
* Calculate select timeout...
475
* If we have data waiting to send timeout is 100ms.
476
* else if we're draining print_fd timeout is 0.
477
* else we're waiting forever...
483
tv.tv_usec = 100000; /* 100ms */
486
else if (g.drain_output)
496
* I/O is unlocked around select...
499
pthread_mutex_lock(&g.readwrite_lock_mutex);
500
g.readwrite_lock = 0;
501
pthread_cond_signal(&g.readwrite_lock_cond);
502
pthread_mutex_unlock(&g.readwrite_lock_mutex);
504
nfds = select(print_fd + 1, &input_set, NULL, NULL, timeout);
507
* Reacquire the lock...
510
pthread_mutex_lock(&g.readwrite_lock_mutex);
511
while (g.readwrite_lock)
512
pthread_cond_wait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex);
513
g.readwrite_lock = 1;
514
pthread_mutex_unlock(&g.readwrite_lock_mutex);
518
if (errno == EINTR && total_bytes == 0)
520
fputs("DEBUG: Received an interrupt before any bytes were "
521
"written, aborting.\n", stderr);
522
close_device(g.printer);
523
return (CUPS_BACKEND_OK);
525
else if (errno != EAGAIN && errno != EINTR)
527
_cupsLangPrintFilter(stderr, "ERROR",
528
_("Unable to read print data."));
529
perror("DEBUG: select");
530
close_device(g.printer);
531
return (CUPS_BACKEND_FAILED);
536
* If drain output has finished send a response...
539
if (g.drain_output && !nfds && !g.print_bytes)
541
/* Send a response... */
542
cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
547
* Check if we have print data ready...
550
if (FD_ISSET(print_fd, &input_set))
552
g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer));
554
if (g.print_bytes < 0)
557
* Read error - bail if we don't see EAGAIN or EINTR...
560
if (errno != EAGAIN && errno != EINTR)
562
_cupsLangPrintFilter(stderr, "ERROR",
563
_("Unable to read print data."));
564
perror("DEBUG: read");
565
close_device(g.printer);
566
return (CUPS_BACKEND_FAILED);
571
else if (g.print_bytes == 0)
574
* End of file, break out of the loop...
580
print_ptr = print_buffer;
582
fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
588
iostatus = libusb_bulk_transfer(g.printer->handle,
589
g.printer->write_endp,
590
print_buffer, g.print_bytes,
593
* Ignore timeout errors, but retain the number of bytes written to
594
* avoid sending duplicate data...
597
if (iostatus == LIBUSB_ERROR_TIMEOUT)
599
fputs("DEBUG: Got USB transaction timeout during write.\n", stderr);
604
* If we've stalled, retry the write...
607
else if (iostatus == LIBUSB_ERROR_PIPE)
609
fputs("DEBUG: Got USB pipe stalled during write.\n", stderr);
611
iostatus = libusb_bulk_transfer(g.printer->handle,
612
g.printer->write_endp,
613
print_buffer, g.print_bytes,
618
* Retry a write after an aborted write since we probably just got
622
else if (iostatus == LIBUSB_ERROR_INTERRUPTED)
624
fputs("DEBUG: Got USB return aborted during write.\n", stderr);
626
iostatus = libusb_bulk_transfer(g.printer->handle,
627
g.printer->write_endp,
628
print_buffer, g.print_bytes,
635
* Write error - bail if we don't see an error we can retry...
638
_cupsLangPrintFilter(stderr, "ERROR",
639
_("Unable to send data to printer."));
640
fprintf(stderr, "DEBUG: libusb write operation returned %x.\n",
643
status = CUPS_BACKEND_FAILED;
648
fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n",
651
g.print_bytes -= bytes;
653
total_bytes += bytes;
657
if (print_fd != 0 && status == CUPS_BACKEND_OK)
658
fprintf(stderr, "DEBUG: Sending print file, " CUPS_LLFMT " bytes...\n",
659
CUPS_LLCAST total_bytes);
663
fprintf(stderr, "DEBUG: Sent " CUPS_LLFMT " bytes...\n",
664
CUPS_LLCAST total_bytes);
667
* Signal the side channel thread to exit...
670
if (have_sidechannel)
673
pthread_mutex_lock(&g.readwrite_lock_mutex);
674
g.readwrite_lock = 0;
675
pthread_cond_signal(&g.readwrite_lock_cond);
676
pthread_mutex_unlock(&g.readwrite_lock_mutex);
678
g.sidechannel_thread_stop = 1;
679
pthread_mutex_lock(&g.sidechannel_thread_mutex);
681
if (!g.sidechannel_thread_done)
683
gettimeofday(&tv, NULL);
684
cond_timeout.tv_sec = tv.tv_sec + WAIT_SIDE_DELAY;
685
cond_timeout.tv_nsec = tv.tv_usec * 1000;
687
while (!g.sidechannel_thread_done)
689
if (pthread_cond_timedwait(&g.sidechannel_thread_cond,
690
&g.sidechannel_thread_mutex,
696
pthread_mutex_unlock(&g.sidechannel_thread_mutex);
700
* Signal the read thread to exit then wait 7 seconds for it to complete...
703
if (have_backchannel)
705
g.read_thread_stop = 1;
707
pthread_mutex_lock(&g.read_thread_mutex);
709
if (!g.read_thread_done)
711
fputs("DEBUG: Waiting for read thread to exit...\n", stderr);
713
gettimeofday(&tv, NULL);
714
cond_timeout.tv_sec = tv.tv_sec + WAIT_EOF_DELAY;
715
cond_timeout.tv_nsec = tv.tv_usec * 1000;
717
while (!g.read_thread_done)
719
if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
725
* If it didn't exit abort the pending read and wait an additional
729
if (!g.read_thread_done)
731
fputs("DEBUG: Read thread still active, aborting the pending read...\n",
736
gettimeofday(&tv, NULL);
737
cond_timeout.tv_sec = tv.tv_sec + 1;
738
cond_timeout.tv_nsec = tv.tv_usec * 1000;
740
while (!g.read_thread_done)
742
if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
749
pthread_mutex_unlock(&g.read_thread_mutex);
753
* Close the connection and input file and general clean up...
756
close_device(g.printer);
762
libusb_free_device_list(list, 1);
770
* 'close_device()' - Close the connection to the USB printer.
773
static int /* I - 0 on success, -1 on failure */
774
close_device(usb_printer_t *printer) /* I - Printer */
776
struct libusb_device_descriptor devdesc;
777
/* Current device descriptor */
778
struct libusb_config_descriptor *confptr;
779
/* Pointer to current configuration */
785
* Release interfaces before closing so that we know all data is written
789
int errcode; /* Return value of libusb function */
790
int number1, /* Interface number */
791
number2; /* Configuration number */
794
libusb_get_config_descriptor(printer->device, printer->conf, &confptr);
797
number1 = confptr->interface[printer->iface].
798
altsetting[printer->altset].bInterfaceNumber;
799
libusb_release_interface(printer->handle, number1);
801
number2 = confptr->bConfigurationValue;
803
libusb_free_config_descriptor(confptr);
806
* If we have changed the configuration from one valid configuration
807
* to another, restore the old one
809
if (printer->origconf > 0 && printer->origconf != number2)
811
fprintf(stderr, "DEBUG: Restoring USB device configuration: %d -> %d\n",
812
number2, printer->origconf);
813
if ((errcode = libusb_set_configuration(printer->handle,
814
printer->origconf)) < 0)
816
if (errcode != LIBUSB_ERROR_BUSY)
819
libusb_get_device_descriptor (printer->device, &devdesc);
822
"DEBUG: Failed to set configuration %d\n",
826
"DEBUG: Failed to set configuration %d for %04x:%04x\n",
827
printer->origconf, devdesc.idVendor, devdesc.idProduct);
833
* Re-attach "usblp" kernel module if it was attached before using this
836
if (printer->usblp_attached == 1)
837
if (libusb_attach_kernel_driver(printer->handle, number1) < 0)
839
errcode = libusb_get_device_descriptor (printer->device, &devdesc);
842
"DEBUG: Failed to re-attach \"usblp\" kernel module\n");
845
"DEBUG: Failed to re-attach \"usblp\" kernel module to "
846
"%04x:%04x\n", devdesc.idVendor, devdesc.idProduct);
851
"DEBUG: Failed to get configuration descriptor %d\n",
855
* Reset the device to clean up after the job
858
if (printer->reset_after_job == 1)
860
if ((errcode = libusb_reset_device(printer->handle)) < 0)
862
"DEBUG: Device reset failed, error code: %d\n",
866
"DEBUG: Resetting printer.\n");
870
* Close the interface and return...
873
libusb_close(printer->handle);
874
printer->handle = NULL;
882
* 'find_device()' - Find or enumerate USB printers.
885
static usb_printer_t * /* O - Found printer */
886
find_device(usb_cb_t cb, /* I - Callback function */
887
const void *data) /* I - User data for callback */
889
libusb_device **list; /* List of connected USB devices */
890
libusb_device *device = NULL; /* Current device */
891
struct libusb_device_descriptor devdesc;
892
/* Current device descriptor */
893
struct libusb_config_descriptor *confptr = NULL;
894
/* Pointer to current configuration */
895
const struct libusb_interface *ifaceptr = NULL;
896
/* Pointer to current interface */
897
const struct libusb_interface_descriptor *altptr = NULL;
898
/* Pointer to current alternate setting */
899
const struct libusb_endpoint_descriptor *endpptr = NULL;
900
/* Pointer to current endpoint */
901
ssize_t numdevs, /* number of connected devices */
903
uint8_t conf, /* Current configuration */
904
iface, /* Current interface */
905
altset, /* Current alternate setting */
906
protocol, /* Current protocol */
907
endp, /* Current endpoint */
908
read_endp, /* Current read endpoint */
909
write_endp; /* Current write endpoint */
910
char device_id[1024],/* IEEE-1284 device ID */
913
static usb_printer_t printer; /* Current printer */
917
* Initialize libusb...
921
numdevs = libusb_get_device_list(NULL, &list);
922
fprintf(stderr, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs);
925
* Then loop through the devices it found...
929
for (i = 0; i < numdevs; i++)
934
* Ignore devices with no configuration data and anything that is not
938
if (libusb_get_device_descriptor(device, &devdesc) < 0)
941
if (!devdesc.bNumConfigurations || !devdesc.idVendor ||
945
printer.quirks = quirks(devdesc.idVendor, devdesc.idProduct);
948
* Ignore blacklisted printers...
951
if (printer.quirks & USBLP_QUIRK_BLACKLIST)
954
for (conf = 0; conf < devdesc.bNumConfigurations; conf ++)
956
if (libusb_get_config_descriptor(device, conf, &confptr) < 0)
958
for (iface = 0, ifaceptr = confptr->interface;
959
iface < confptr->bNumInterfaces;
960
iface ++, ifaceptr ++)
963
* Some printers offer multiple interfaces...
968
for (altset = 0, altptr = ifaceptr->altsetting;
969
altset < ifaceptr->num_altsetting;
970
altset ++, altptr ++)
973
* Currently we only support unidirectional and bidirectional
974
* printers. Future versions of this code will support the
975
* 1284.4 (packet mode) protocol as well.
978
if (((altptr->bInterfaceClass != LIBUSB_CLASS_PRINTER ||
979
altptr->bInterfaceSubClass != 1) &&
980
((printer.quirks & USBLP_QUIRK_BAD_CLASS) == 0)) ||
981
(altptr->bInterfaceProtocol != 1 && /* Unidirectional */
982
altptr->bInterfaceProtocol != 2) || /* Bidirectional */
983
altptr->bInterfaceProtocol < protocol)
986
if (printer.quirks & USBLP_QUIRK_BAD_CLASS)
987
fprintf(stderr, "DEBUG: Printer does not report class 7 and/or "
988
"subclass 1 but works as a printer anyway\n");
993
for (endp = 0, endpptr = altptr->endpoint;
994
endp < altptr->bNumEndpoints;
996
if ((endpptr->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
997
LIBUSB_TRANSFER_TYPE_BULK)
999
if (endpptr->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
1005
if (write_endp >= 0)
1008
* Save the best match so far...
1011
protocol = altptr->bInterfaceProtocol;
1012
printer.altset = altset;
1013
printer.write_endp = write_endp;
1015
printer.read_endp = read_endp;
1017
printer.read_endp = -1;
1023
printer.device = device;
1024
printer.conf = conf;
1025
printer.iface = iface;
1026
printer.protocol = protocol;
1027
printer.handle = NULL;
1029
if (!open_device(&printer, data != NULL))
1031
get_device_id(&printer, device_id, sizeof(device_id));
1032
make_device_uri(&printer, device_id, device_uri,
1033
sizeof(device_uri));
1035
fprintf(stderr, "DEBUG2: Printer found with device ID: %s "
1037
device_id, device_uri);
1039
if ((*cb)(&printer, device_uri, device_id, data))
1041
fprintf(stderr, "DEBUG: Device protocol: %d\n",
1043
if (printer.quirks & USBLP_QUIRK_BIDIR)
1045
printer.read_endp = -1;
1046
fprintf(stderr, "DEBUG: Printer reports bi-di support "
1047
"but in reality works only uni-directionally\n");
1049
if (printer.read_endp != -1)
1051
printer.read_endp = confptr->interface[printer.iface].
1052
altsetting[printer.altset].
1053
endpoint[printer.read_endp].
1057
fprintf(stderr, "DEBUG: Uni-directional USB communication "
1059
printer.write_endp = confptr->interface[printer.iface].
1060
altsetting[printer.altset].
1061
endpoint[printer.write_endp].
1063
if (printer.quirks & USBLP_QUIRK_NO_REATTACH)
1065
printer.usblp_attached = 0;
1066
fprintf(stderr, "DEBUG: Printer does not like usblp "
1067
"kernel module to be re-attached after job\n");
1069
libusb_free_config_descriptor(confptr);
1073
close_device(&printer);
1077
libusb_free_config_descriptor(confptr);
1082
* If we get this far without returning, then we haven't found a printer
1090
libusb_free_device_list(list, 1);
1098
* 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
1101
static int /* O - 0 on success, -1 on error */
1102
get_device_id(usb_printer_t *printer, /* I - Printer */
1103
char *buffer, /* I - String buffer */
1104
size_t bufsize) /* I - Number of bytes in buffer */
1106
int length; /* Length of device ID */
1109
if (libusb_control_transfer(printer->handle,
1110
LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN |
1111
LIBUSB_RECIPIENT_INTERFACE,
1113
(printer->iface << 8) | printer->altset,
1114
(unsigned char *)buffer, bufsize, 5000) < 0)
1121
* Extract the length of the device ID string from the first two
1122
* bytes. The 1284 spec says the length is stored MSB first...
1125
length = (((unsigned)buffer[0] & 255) << 8) |
1126
((unsigned)buffer[1] & 255);
1129
* Check to see if the length is larger than our buffer or less than 14 bytes
1130
* (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
1132
* If the length is out-of-range, assume that the vendor incorrectly
1133
* implemented the 1284 spec and re-read the length as LSB first,..
1136
if (length > bufsize || length < 14)
1137
length = (((unsigned)buffer[1] & 255) << 8) |
1138
((unsigned)buffer[0] & 255);
1140
if (length > bufsize)
1146
* Invalid device ID, clear it!
1156
* Copy the device ID text to the beginning of the buffer and
1160
memmove(buffer, buffer + 2, length);
1161
buffer[length] = '\0';
1168
* 'list_cb()' - List USB printers for discovery.
1171
static int /* O - 0 to continue, 1 to stop */
1172
list_cb(usb_printer_t *printer, /* I - Printer */
1173
const char *device_uri, /* I - Device URI */
1174
const char *device_id, /* I - IEEE-1284 device ID */
1175
const void *data) /* I - User data (not used) */
1177
char make_model[1024]; /* Make and model */
1181
* Get the device URI and make/model strings...
1184
if (backendGetMakeModel(device_id, make_model, sizeof(make_model)))
1185
strlcpy(make_model, "Unknown", sizeof(make_model));
1188
* Report the printer...
1191
cupsBackendReport("direct", device_uri, make_model, make_model, device_id,
1203
* 'make_device_uri()' - Create a device URI for a USB printer.
1206
static char * /* O - Device URI */
1208
usb_printer_t *printer, /* I - Printer */
1209
const char *device_id, /* I - IEEE-1284 device ID */
1210
char *uri, /* I - Device URI buffer */
1211
size_t uri_size) /* I - Size of device URI buffer */
1213
struct libusb_device_descriptor devdesc;
1214
/* Current device descriptor */
1215
char options[1024]; /* Device URI options */
1216
int num_values; /* Number of 1284 parameters */
1217
cups_option_t *values; /* 1284 parameters */
1218
const char *mfg, /* Manufacturer */
1220
*des = NULL, /* Description */
1221
*sern; /* Serial number */
1222
size_t mfglen; /* Length of manufacturer string */
1223
char tempmfg[256], /* Temporary manufacturer string */
1224
tempsern[256], /* Temporary serial number string */
1225
*tempptr; /* Pointer into temp string */
1229
* Get the make, model, and serial numbers...
1232
num_values = _cupsGet1284Values(device_id, &values);
1234
if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
1235
if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
1236
if ((sern = cupsGetOption("SN", num_values, values)) == NULL &&
1237
((libusb_get_device_descriptor(printer->device, &devdesc) >= 0) &&
1238
devdesc.iSerialNumber))
1241
* Try getting the serial number from the device itself...
1245
libusb_get_string_descriptor_ascii(printer->handle,
1246
devdesc.iSerialNumber,
1247
(unsigned char *)tempsern,
1248
sizeof(tempsern) - 1);
1251
tempsern[length] = '\0';
1256
if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
1257
mfg = cupsGetOption("MFG", num_values, values);
1259
if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
1260
mdl = cupsGetOption("MDL", num_values, values);
1263
* To maintain compatibility with the original character device backend on
1264
* Linux and *BSD, map manufacturer names...
1269
if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
1271
else if (!_cups_strcasecmp(mfg, "Lexmark International"))
1277
* No manufacturer? Use the model string or description...
1281
_ppdNormalizeMakeAndModel(mdl, tempmfg, sizeof(tempmfg));
1282
else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
1283
(des = cupsGetOption("DES", num_values, values)) != NULL)
1284
_ppdNormalizeMakeAndModel(des, tempmfg, sizeof(tempmfg));
1286
strlcpy(tempmfg, "Unknown", sizeof(tempmfg));
1288
if ((tempptr = strchr(tempmfg, ' ')) != NULL)
1297
* No model? Use description...
1300
mdl = des; /* We remove the manufacturer name below */
1301
else if (!strncasecmp(mfg, "Unknown", 7))
1304
mdl = "Unknown Model";
1307
mfglen = strlen(mfg);
1309
if (!strncasecmp(mdl, mfg, mfglen) && _cups_isspace(mdl[mfglen]))
1313
while (_cups_isspace(*mdl))
1318
* Generate the device URI from the manufacturer, model, serial number,
1319
* and interface number...
1324
if (printer->iface > 0)
1325
snprintf(options, sizeof(options), "?serial=%s&interface=%d", sern,
1328
snprintf(options, sizeof(options), "?serial=%s", sern);
1330
else if (printer->iface > 0)
1331
snprintf(options, sizeof(options), "?interface=%d", printer->iface);
1335
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, "usb", NULL, mfg, 0,
1336
"/%s%s", mdl, options);
1338
cupsFreeOptions(num_values, values);
1345
* 'open_device()' - Open a connection to the USB printer.
1348
static int /* O - 0 on success, -1 on error */
1349
open_device(usb_printer_t *printer, /* I - Printer */
1350
int verbose) /* I - Update connecting-to-device state? */
1352
struct libusb_device_descriptor devdesc;
1353
/* Current device descriptor */
1354
struct libusb_config_descriptor *confptr = NULL;
1355
/* Pointer to current configuration */
1356
int number1 = -1, /* Configuration/interface/altset */
1357
number2 = -1, /* numbers */
1359
char current; /* Current configuration */
1363
* Return immediately if we are already connected...
1366
if (printer->handle)
1370
* Try opening the printer...
1373
if ((errcode = libusb_open(printer->device, &printer->handle)) < 0)
1375
fprintf(stderr, "DEBUG: Failed to open device, code: %d\n",
1380
printer->usblp_attached = 0;
1381
printer->reset_after_job = 0;
1384
fputs("STATE: +connecting-to-device\n", stderr);
1386
if ((errcode = libusb_get_device_descriptor(printer->device, &devdesc)) < 0)
1388
fprintf(stderr, "DEBUG: Failed to get device descriptor, code: %d\n",
1394
* Get the "usblp" kernel module out of the way. This backend only
1395
* works without the module attached.
1398
errcode = libusb_kernel_driver_active(printer->handle, printer->iface);
1400
printer->usblp_attached = 0;
1401
else if (errcode == 1)
1403
printer->usblp_attached = 1;
1405
libusb_detach_kernel_driver(printer->handle, printer->iface)) < 0)
1407
fprintf(stderr, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1408
devdesc.idVendor, devdesc.idProduct);
1414
printer->usblp_attached = 0;
1415
fprintf(stderr, "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" kernel module attached\n",
1416
devdesc.idVendor, devdesc.idProduct);
1421
* Set the desired configuration, but only if it needs changing. Some
1422
* printers (e.g., Samsung) don't like libusb_set_configuration. It will
1423
* succeed, but the following print job is sometimes silently lost by the
1427
if (libusb_control_transfer(printer->handle,
1428
LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_ENDPOINT_IN |
1429
LIBUSB_RECIPIENT_DEVICE,
1430
8, /* GET_CONFIGURATION */
1431
0, 0, (unsigned char *)¤t, 1, 5000) < 0)
1432
current = 0; /* Assume not configured */
1434
printer->origconf = current;
1437
libusb_get_config_descriptor (printer->device, printer->conf, &confptr))
1440
fprintf(stderr, "DEBUG: Failed to get config descriptor for %04x:%04x\n",
1441
devdesc.idVendor, devdesc.idProduct);
1444
number1 = confptr->bConfigurationValue;
1446
if (number1 != current)
1448
fprintf(stderr, "DEBUG: Switching USB device configuration: %d -> %d\n",
1450
if ((errcode = libusb_set_configuration(printer->handle, number1)) < 0)
1453
* If the set fails, chances are that the printer only supports a
1454
* single configuration. Technically these printers don't conform to
1455
* the USB printer specification, but otherwise they'll work...
1458
if (errcode != LIBUSB_ERROR_BUSY)
1459
fprintf(stderr, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
1460
number1, devdesc.idVendor, devdesc.idProduct);
1465
* Claim interfaces as needed...
1468
number1 = confptr->interface[printer->iface].
1469
altsetting[printer->altset].bInterfaceNumber;
1471
while ((errcode = libusb_claim_interface(printer->handle, number1)) < 0)
1473
if (errcode != LIBUSB_ERROR_BUSY)
1476
"DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
1477
number1, devdesc.idVendor, devdesc.idProduct, strerror(errno));
1484
* Set alternate setting, but only if there is more than one option. Some
1485
* printers (e.g., Samsung) don't like usb_set_altinterface.
1488
if (confptr->interface[printer->iface].num_altsetting > 1)
1490
number1 = confptr->interface[printer->iface].
1491
altsetting[printer->altset].bInterfaceNumber;
1492
number2 = confptr->interface[printer->iface].
1493
altsetting[printer->altset].bAlternateSetting;
1496
libusb_set_interface_alt_setting(printer->handle, number1, number2))
1499
if (errcode != LIBUSB_ERROR_BUSY)
1502
"DEBUG: Failed to set alternate interface %d for %04x:%04x: "
1504
number2, devdesc.idVendor, devdesc.idProduct, strerror(errno));
1511
libusb_free_config_descriptor(confptr);
1514
fputs("STATE: -connecting-to-device\n", stderr);
1519
* If we get here, there was a hard error...
1525
fputs("STATE: -connecting-to-device\n", stderr);
1527
libusb_close(printer->handle);
1528
printer->handle = NULL;
1535
* 'print_cb()' - Find a USB printer for printing.
1538
static int /* O - 0 to continue, 1 to stop (found) */
1539
print_cb(usb_printer_t *printer, /* I - Printer */
1540
const char *device_uri, /* I - Device URI */
1541
const char *device_id, /* I - IEEE-1284 device ID */
1542
const void *data) /* I - User data (make, model, S/N) */
1544
char requested_uri[1024], /* Requested URI */
1545
*requested_ptr, /* Pointer into requested URI */
1546
detected_uri[1024], /* Detected URI */
1547
*detected_ptr; /* Pointer into detected URI */
1551
* If we have an exact match, stop now...
1554
if (!strcmp((char *)data, device_uri))
1558
* Work on copies of the URIs...
1561
strlcpy(requested_uri, (char *)data, sizeof(requested_uri));
1562
strlcpy(detected_uri, device_uri, sizeof(detected_uri));
1565
* libusb-discovered URIs can have an "interface" specification and this
1566
* never happens for usblp-discovered URIs, so remove the "interface"
1567
* specification from the URI which we are checking currently. This way a
1568
* queue for a usblp-discovered printer can now be accessed via libusb.
1570
* Similarly, strip "?serial=NNN...NNN" as needed.
1573
if ((requested_ptr = strstr(requested_uri, "?interface=")) == NULL)
1574
requested_ptr = strstr(requested_uri, "&interface=");
1575
if ((detected_ptr = strstr(detected_uri, "?interface=")) == NULL)
1576
detected_ptr = strstr(detected_uri, "&interface=");
1578
if (!requested_ptr && detected_ptr)
1581
* Strip "[?&]interface=nnn" from the detected printer.
1584
*detected_ptr = '\0';
1586
else if (requested_ptr && !detected_ptr)
1589
* Strip "[?&]interface=nnn" from the requested printer.
1592
*requested_ptr = '\0';
1595
if ((requested_ptr = strstr(requested_uri, "?serial=?")) != NULL)
1598
* Strip "?serial=?" from the requested printer. This is a special
1599
* case, as "?serial=?" means no serial number and not the serial
1600
* number '?'. This is not covered by the checks below...
1603
*requested_ptr = '\0';
1606
if ((requested_ptr = strstr(requested_uri, "?serial=")) == NULL &&
1607
(detected_ptr = strstr(detected_uri, "?serial=")) != NULL)
1610
* Strip "?serial=nnn" from the detected printer.
1613
*detected_ptr = '\0';
1615
else if (requested_ptr && !detected_ptr)
1618
* Strip "?serial=nnn" from the requested printer.
1621
*requested_ptr = '\0';
1624
return (!strcmp(requested_uri, detected_uri));
1629
* 'printer_class_soft_reset()' - Do the soft reset request specific to printers
1631
* This soft reset is specific to the printer device class and is much less
1632
* invasive than the general USB reset libusb_reset_device(). Especially it
1633
* does never happen that the USB addressing and configuration changes. What
1634
* is actually done is that all buffers get flushed and the bulk IN and OUT
1635
* pipes get reset to their default states. This clears all stall conditions.
1636
* See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
1639
static int /* O - 0 on success, < 0 on error */
1640
printer_class_soft_reset(usb_printer_t *printer) /* I - Printer */
1642
struct libusb_config_descriptor *confptr = NULL;
1643
/* Pointer to current configuration */
1647
if (libusb_get_config_descriptor(printer->device, printer->conf, &confptr)
1649
interface = printer->iface;
1651
interface = confptr->interface[printer->iface].
1652
altsetting[printer->altset].bInterfaceNumber;
1653
libusb_free_config_descriptor(confptr);
1654
if ((errcode = libusb_control_transfer(printer->handle,
1655
LIBUSB_REQUEST_TYPE_CLASS |
1656
LIBUSB_ENDPOINT_OUT |
1657
LIBUSB_RECIPIENT_OTHER,
1658
2, 0, interface, NULL, 0, 5000)) < 0)
1659
errcode = libusb_control_transfer(printer->handle,
1660
LIBUSB_REQUEST_TYPE_CLASS |
1661
LIBUSB_ENDPOINT_OUT |
1662
LIBUSB_RECIPIENT_INTERFACE,
1663
2, 0, interface, NULL, 0, 5000);
1669
* 'quirks()' - Get the known quirks of a given printer model
1672
static unsigned int quirks(int vendor, int product)
1676
for (i = 0; quirk_printers[i].vendorId; i++)
1678
if (vendor == quirk_printers[i].vendorId &&
1679
(quirk_printers[i].productId == 0x0000 ||
1680
product == quirk_printers[i].productId))
1681
return quirk_printers[i].quirks;
1688
* 'read_thread()' - Thread to read the backchannel data on.
1691
static void *read_thread(void *reference)
1693
unsigned char readbuffer[512];
1705
* Read frequency: once every 250 milliseconds.
1709
delay.tv_usec = 250000;
1714
* Remember when we started so we can throttle the loop after the read
1718
gettimeofday(&now, NULL);
1721
* Calculate what 250 milliSeconds are in absolute time...
1724
timeradd(&now, &delay, &end);
1726
rbytes = sizeof(readbuffer);
1727
readstatus = libusb_bulk_transfer(g.printer->handle,
1728
g.printer->read_endp,
1731
if (readstatus == LIBUSB_SUCCESS && rbytes > 0)
1733
fprintf(stderr, "DEBUG: Read %d bytes of back-channel data...\n",
1735
cupsBackChannelWrite((const char *)readbuffer, rbytes, 1.0);
1737
else if (readstatus == LIBUSB_ERROR_TIMEOUT)
1738
fputs("DEBUG: Got USB transaction timeout during read.\n", stderr);
1739
else if (readstatus == LIBUSB_ERROR_PIPE)
1740
fputs("DEBUG: Got USB pipe stalled during read.\n", stderr);
1741
else if (readstatus == LIBUSB_ERROR_INTERRUPTED)
1742
fputs("DEBUG: Got USB return aborted during read.\n", stderr);
1745
* Make sure this loop executes no more than once every 250 miliseconds...
1748
if ((readstatus != LIBUSB_SUCCESS || rbytes == 0) &&
1749
(g.wait_eof || !g.read_thread_stop))
1751
gettimeofday(&now, NULL);
1752
if (timercmp(&now, &end, <))
1754
timersub(&end, &now, &timeleft);
1755
usleep(1000000 * timeleft.tv_sec + timeleft.tv_usec);
1758
} while (g.wait_eof || !g.read_thread_stop);
1761
* Let the main thread know that we have completed the read thread...
1764
pthread_mutex_lock(&g.read_thread_mutex);
1765
g.read_thread_done = 1;
1766
pthread_cond_signal(&g.read_thread_cond);
1767
pthread_mutex_unlock(&g.read_thread_mutex);
1774
* 'sidechannel_thread()' - Handle side-channel requests.
1778
sidechannel_thread(void *reference)
1780
cups_sc_command_t command; /* Request command */
1781
cups_sc_status_t status; /* Request/response status */
1782
char data[2048]; /* Request/response data */
1783
int datalen; /* Request/response data size */
1790
datalen = sizeof(data);
1792
if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
1794
if (status == CUPS_SC_STATUS_TIMEOUT)
1802
case CUPS_SC_CMD_SOFT_RESET: /* Do a soft reset */
1803
fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1807
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
1808
fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1812
case CUPS_SC_CMD_DRAIN_OUTPUT: /* Drain all pending output */
1813
fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1819
case CUPS_SC_CMD_GET_BIDI: /* Is the connection bidirectional? */
1820
fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1823
data[0] = (g.printer->protocol >= 2 ? 1 : 0);
1824
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
1827
"DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1831
case CUPS_SC_CMD_GET_DEVICE_ID: /* Return IEEE-1284 device ID */
1832
fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1835
datalen = sizeof(data);
1836
if (get_device_id(g.printer, data, sizeof(data)))
1838
status = CUPS_SC_STATUS_IO_ERROR;
1843
status = CUPS_SC_STATUS_OK;
1844
datalen = strlen(data);
1846
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0);
1848
if (datalen < sizeof(data))
1849
data[datalen] = '\0';
1851
data[sizeof(data) - 1] = '\0';
1854
"DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1858
case CUPS_SC_CMD_GET_STATE: /* Return device state */
1859
fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1862
data[0] = CUPS_SC_STATE_ONLINE;
1863
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
1866
"DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1870
case CUPS_SC_CMD_GET_CONNECTED: /* Return whether device is
1872
fputs("DEBUG: CUPS_SC_CMD_GET_CONNECTED received from driver...\n",
1875
data[0] = (g.printer->handle ? 1 : 0);
1876
cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
1879
"DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1884
fprintf(stderr, "DEBUG: Unknown side-channel command (%d) received "
1885
"from driver...\n", command);
1887
cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
1890
fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1895
while (!g.sidechannel_thread_stop);
1897
pthread_mutex_lock(&g.sidechannel_thread_mutex);
1898
g.sidechannel_thread_done = 1;
1899
pthread_cond_signal(&g.sidechannel_thread_cond);
1900
pthread_mutex_unlock(&g.sidechannel_thread_mutex);
1907
* 'soft_reset()' - Send a soft reset to the device.
1910
static void soft_reset(void)
1912
fd_set input_set; /* Input set for select() */
1913
struct timeval tv; /* Time value */
1914
char buffer[2048]; /* Buffer */
1915
struct timespec cond_timeout; /* pthread condition timeout */
1918
* Send an abort once a second until the I/O lock is released by the main
1922
pthread_mutex_lock(&g.readwrite_lock_mutex);
1923
while (g.readwrite_lock)
1925
gettimeofday(&tv, NULL);
1926
cond_timeout.tv_sec = tv.tv_sec + 1;
1927
cond_timeout.tv_nsec = tv.tv_usec * 1000;
1929
while (g.readwrite_lock)
1931
if (pthread_cond_timedwait(&g.readwrite_lock_cond,
1932
&g.readwrite_lock_mutex,
1933
&cond_timeout) != 0)
1938
g.readwrite_lock = 1;
1939
pthread_mutex_unlock(&g.readwrite_lock_mutex);
1942
* Flush bytes waiting on print_fd...
1947
FD_ZERO(&input_set);
1948
FD_SET(g.print_fd, &input_set);
1953
while (select(g.print_fd+1, &input_set, NULL, NULL, &tv) > 0)
1954
if (read(g.print_fd, buffer, sizeof(buffer)) <= 0)
1961
printer_class_soft_reset(g.printer);
1964
* Release the I/O lock...
1967
pthread_mutex_lock(&g.readwrite_lock_mutex);
1968
g.readwrite_lock = 0;
1969
pthread_cond_signal(&g.readwrite_lock_cond);
1970
pthread_mutex_unlock(&g.readwrite_lock_mutex);
1975
* End of "$Id: usb-libusb.c 10908 2013-03-14 14:31:07Z mike $".