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 { 0x04a9, 0x31DD, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP780 */
234 /* MISSING PIDs: CP520, CP530, CP790 */
243 usb_globals_t g
= { 0 }; /* Globals */
244 libusb_device
**list
; /* List of connected USB devices */
251 static int close_device(usb_printer_t
*printer
);
252 static usb_printer_t
*find_device(usb_cb_t cb
, const void *data
);
253 static int get_device_id(usb_printer_t
*printer
, char *buffer
,
255 static int list_cb(usb_printer_t
*printer
, const char *device_uri
,
256 const char *device_id
, const void *data
);
257 static char *make_device_uri(usb_printer_t
*printer
,
258 const char *device_id
,
259 char *uri
, size_t uri_size
);
260 static int open_device(usb_printer_t
*printer
, int verbose
);
261 static int print_cb(usb_printer_t
*printer
, const char *device_uri
,
262 const char *device_id
, const void *data
);
263 static int printer_class_soft_reset(usb_printer_t
*printer
);
264 static unsigned int quirks(int vendor
, int product
);
265 static void *read_thread(void *reference
);
266 static void *sidechannel_thread(void *reference
);
267 static void soft_reset(void);
271 * 'list_devices()' - List the available printers.
277 fputs("DEBUG: list_devices\n", stderr
);
278 find_device(list_cb
, NULL
);
283 * 'print_device()' - Print a file to a USB device.
286 int /* O - Exit status */
287 print_device(const char *uri
, /* I - Device URI */
288 const char *hostname
, /* I - Hostname/manufacturer */
289 const char *resource
, /* I - Resource/modelname */
290 char *options
, /* I - Device options/serial number */
291 int print_fd
, /* I - File descriptor to print */
292 int copies
, /* I - Copies to print */
293 int argc
, /* I - Number of command-line arguments (6 or 7) */
294 char *argv
[]) /* I - Command-line arguments */
296 int bytes
; /* Bytes written */
297 ssize_t total_bytes
; /* Total bytes written */
298 struct sigaction action
; /* Actions for POSIX signals */
299 int status
= CUPS_BACKEND_OK
,
300 /* Function results */
301 iostatus
; /* Current IO status */
302 pthread_t read_thread_id
, /* Read thread */
303 sidechannel_thread_id
; /* Side-channel thread */
304 int have_sidechannel
= 0, /* Was the side-channel thread started? */
305 have_backchannel
= 0; /* Do we have a back channel? */
306 struct stat sidechannel_info
; /* Side-channel file descriptor info */
307 unsigned char print_buffer
[8192], /* Print data buffer */
308 *print_ptr
; /* Pointer into print data buffer */
309 fd_set input_set
; /* Input set for select() */
310 int nfds
; /* Number of file descriptors */
311 struct timeval
*timeout
, /* Timeout pointer */
313 struct timespec cond_timeout
; /* pthread condition timeout */
314 int num_opts
; /* Number of options */
315 cups_option_t
*opts
; /* Options */
316 const char *val
; /* Option value */
320 * See if the side-channel descriptor is valid...
323 have_sidechannel
= !fstat(CUPS_SC_FD
, &sidechannel_info
) &&
324 S_ISSOCK(sidechannel_info
.st_mode
);
326 g
.wait_eof
= WAIT_EOF
;
329 * Connect to the printer...
332 fprintf(stderr
, "DEBUG: Printing on printer with URI: %s\n", uri
);
333 while ((g
.printer
= find_device(print_cb
, uri
)) == NULL
)
335 _cupsLangPrintFilter(stderr
, "INFO",
336 _("Waiting for printer to become available."));
340 g
.print_fd
= print_fd
;
343 * Some devices need a reset after finishing a job, these devices are
344 * marked with the USBLP_QUIRK_RESET quirk.
346 g
.printer
->reset_after_job
= (g
.printer
->quirks
& USBLP_QUIRK_RESET
? 1 : 0);
349 * If we are printing data from a print driver on stdin, ignore SIGTERM
350 * so that the driver can finish out any page data, e.g. to eject the
351 * current page. We only do this for stdin printing as otherwise there
352 * is no way to cancel a raw print job...
357 memset(&action
, 0, sizeof(action
));
359 sigemptyset(&action
.sa_mask
);
360 action
.sa_handler
= SIG_IGN
;
361 sigaction(SIGTERM
, &action
, NULL
);
365 * Start the side channel thread if the descriptor is valid...
368 pthread_mutex_init(&g
.readwrite_lock_mutex
, NULL
);
369 pthread_cond_init(&g
.readwrite_lock_cond
, NULL
);
370 g
.readwrite_lock
= 1;
372 if (have_sidechannel
)
374 g
.sidechannel_thread_stop
= 0;
375 g
.sidechannel_thread_done
= 0;
377 pthread_cond_init(&g
.sidechannel_thread_cond
, NULL
);
378 pthread_mutex_init(&g
.sidechannel_thread_mutex
, NULL
);
380 if (pthread_create(&sidechannel_thread_id
, NULL
, sidechannel_thread
, NULL
))
382 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
383 _cupsLangPrintFilter(stderr
, "ERROR",
384 _("There was an unrecoverable USB error."));
385 fputs("DEBUG: Couldn't create side-channel thread.\n", stderr
);
386 close_device(g
.printer
);
387 return (CUPS_BACKEND_STOP
);
392 * Debug mode: If option "usb-unidir" is given, always deactivate
396 num_opts
= cupsParseOptions(argv
[5], 0, &opts
);
397 val
= cupsGetOption("usb-unidir", num_opts
, opts
);
398 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
399 strcasecmp(val
, "false"))
401 g
.printer
->read_endp
= -1;
402 fprintf(stderr
, "DEBUG: Forced uni-directional communication "
403 "via \"usb-unidir\" option.\n");
407 * Debug mode: If option "usb-no-reattach" is given, do not re-attach
408 * the usblp kernel module after the job has completed.
411 val
= cupsGetOption("usb-no-reattach", num_opts
, opts
);
412 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
413 strcasecmp(val
, "false"))
415 g
.printer
->usblp_attached
= 0;
416 fprintf(stderr
, "DEBUG: Forced not re-attaching the usblp kernel module "
417 "after the job via \"usb-no-reattach\" option.\n");
421 * Get the read thread going...
424 if (g
.printer
->read_endp
!= -1)
426 have_backchannel
= 1;
428 g
.read_thread_stop
= 0;
429 g
.read_thread_done
= 0;
431 pthread_cond_init(&g
.read_thread_cond
, NULL
);
432 pthread_mutex_init(&g
.read_thread_mutex
, NULL
);
434 if (pthread_create(&read_thread_id
, NULL
, read_thread
, NULL
))
436 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
437 _cupsLangPrintFilter(stderr
, "ERROR",
438 _("There was an unrecoverable USB error."));
439 fputs("DEBUG: Couldn't create read thread.\n", stderr
);
440 close_device(g
.printer
);
441 return (CUPS_BACKEND_STOP
);
445 fprintf(stderr
, "DEBUG: Uni-directional device/mode, back channel "
449 * The main thread sends the print file...
455 print_ptr
= print_buffer
;
457 while (status
== CUPS_BACKEND_OK
&& copies
-- > 0)
459 _cupsLangPrintFilter(stderr
, "INFO", _("Sending data to printer."));
461 if (print_fd
!= STDIN_FILENO
)
463 fputs("PAGE: 1 1\n", stderr
);
464 lseek(print_fd
, 0, SEEK_SET
);
467 while (status
== CUPS_BACKEND_OK
)
472 FD_SET(print_fd
, &input_set
);
475 * Calculate select timeout...
476 * If we have data waiting to send timeout is 100ms.
477 * else if we're draining print_fd timeout is 0.
478 * else we're waiting forever...
484 tv
.tv_usec
= 100000; /* 100ms */
487 else if (g
.drain_output
)
497 * I/O is unlocked around select...
500 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
501 g
.readwrite_lock
= 0;
502 pthread_cond_signal(&g
.readwrite_lock_cond
);
503 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
505 nfds
= select(print_fd
+ 1, &input_set
, NULL
, NULL
, timeout
);
508 * Reacquire the lock...
511 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
512 while (g
.readwrite_lock
)
513 pthread_cond_wait(&g
.readwrite_lock_cond
, &g
.readwrite_lock_mutex
);
514 g
.readwrite_lock
= 1;
515 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
519 if (errno
== EINTR
&& total_bytes
== 0)
521 fputs("DEBUG: Received an interrupt before any bytes were "
522 "written, aborting.\n", stderr
);
523 close_device(g
.printer
);
524 return (CUPS_BACKEND_OK
);
526 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
528 _cupsLangPrintFilter(stderr
, "ERROR",
529 _("Unable to read print data."));
530 perror("DEBUG: select");
531 close_device(g
.printer
);
532 return (CUPS_BACKEND_FAILED
);
537 * If drain output has finished send a response...
540 if (g
.drain_output
&& !nfds
&& !g
.print_bytes
)
542 /* Send a response... */
543 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
548 * Check if we have print data ready...
551 if (FD_ISSET(print_fd
, &input_set
))
553 g
.print_bytes
= read(print_fd
, print_buffer
, sizeof(print_buffer
));
555 if (g
.print_bytes
< 0)
558 * Read error - bail if we don't see EAGAIN or EINTR...
561 if (errno
!= EAGAIN
&& errno
!= EINTR
)
563 _cupsLangPrintFilter(stderr
, "ERROR",
564 _("Unable to read print data."));
565 perror("DEBUG: read");
566 close_device(g
.printer
);
567 return (CUPS_BACKEND_FAILED
);
572 else if (g
.print_bytes
== 0)
575 * End of file, break out of the loop...
581 print_ptr
= print_buffer
;
583 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
589 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
590 g
.printer
->write_endp
,
591 print_buffer
, g
.print_bytes
,
594 * Ignore timeout errors, but retain the number of bytes written to
595 * avoid sending duplicate data...
598 if (iostatus
== LIBUSB_ERROR_TIMEOUT
)
600 fputs("DEBUG: Got USB transaction timeout during write.\n", stderr
);
605 * If we've stalled, retry the write...
608 else if (iostatus
== LIBUSB_ERROR_PIPE
)
610 fputs("DEBUG: Got USB pipe stalled during write.\n", stderr
);
612 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
613 g
.printer
->write_endp
,
614 print_buffer
, g
.print_bytes
,
619 * Retry a write after an aborted write since we probably just got
623 else if (iostatus
== LIBUSB_ERROR_INTERRUPTED
)
625 fputs("DEBUG: Got USB return aborted during write.\n", stderr
);
627 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
628 g
.printer
->write_endp
,
629 print_buffer
, g
.print_bytes
,
636 * Write error - bail if we don't see an error we can retry...
639 _cupsLangPrintFilter(stderr
, "ERROR",
640 _("Unable to send data to printer."));
641 fprintf(stderr
, "DEBUG: libusb write operation returned %x.\n",
644 status
= CUPS_BACKEND_FAILED
;
649 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n",
652 g
.print_bytes
-= bytes
;
654 total_bytes
+= bytes
;
658 if (print_fd
!= 0 && status
== CUPS_BACKEND_OK
)
659 fprintf(stderr
, "DEBUG: Sending print file, " CUPS_LLFMT
" bytes...\n",
660 CUPS_LLCAST total_bytes
);
664 fprintf(stderr
, "DEBUG: Sent " CUPS_LLFMT
" bytes...\n",
665 CUPS_LLCAST total_bytes
);
668 * Signal the side channel thread to exit...
671 if (have_sidechannel
)
674 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
675 g
.readwrite_lock
= 0;
676 pthread_cond_signal(&g
.readwrite_lock_cond
);
677 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
679 g
.sidechannel_thread_stop
= 1;
680 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
682 if (!g
.sidechannel_thread_done
)
684 gettimeofday(&tv
, NULL
);
685 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_SIDE_DELAY
;
686 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
688 while (!g
.sidechannel_thread_done
)
690 if (pthread_cond_timedwait(&g
.sidechannel_thread_cond
,
691 &g
.sidechannel_thread_mutex
,
697 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
701 * Signal the read thread to exit then wait 7 seconds for it to complete...
704 if (have_backchannel
)
706 g
.read_thread_stop
= 1;
708 pthread_mutex_lock(&g
.read_thread_mutex
);
710 if (!g
.read_thread_done
)
712 fputs("DEBUG: Waiting for read thread to exit...\n", stderr
);
714 gettimeofday(&tv
, NULL
);
715 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_EOF_DELAY
;
716 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
718 while (!g
.read_thread_done
)
720 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
726 * If it didn't exit abort the pending read and wait an additional
730 if (!g
.read_thread_done
)
732 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
737 gettimeofday(&tv
, NULL
);
738 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
739 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
741 while (!g
.read_thread_done
)
743 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
750 pthread_mutex_unlock(&g
.read_thread_mutex
);
754 * Close the connection and input file and general clean up...
757 close_device(g
.printer
);
763 libusb_free_device_list(list
, 1);
771 * 'close_device()' - Close the connection to the USB printer.
774 static int /* I - 0 on success, -1 on failure */
775 close_device(usb_printer_t
*printer
) /* I - Printer */
777 struct libusb_device_descriptor devdesc
;
778 /* Current device descriptor */
779 struct libusb_config_descriptor
*confptr
;
780 /* Pointer to current configuration */
786 * Release interfaces before closing so that we know all data is written
790 int errcode
; /* Return value of libusb function */
791 int number1
, /* Interface number */
792 number2
; /* Configuration number */
795 libusb_get_config_descriptor(printer
->device
, printer
->conf
, &confptr
);
798 number1
= confptr
->interface
[printer
->iface
].
799 altsetting
[printer
->altset
].bInterfaceNumber
;
800 libusb_release_interface(printer
->handle
, number1
);
802 number2
= confptr
->bConfigurationValue
;
804 libusb_free_config_descriptor(confptr
);
807 * If we have changed the configuration from one valid configuration
808 * to another, restore the old one
810 if (printer
->origconf
> 0 && printer
->origconf
!= number2
)
812 fprintf(stderr
, "DEBUG: Restoring USB device configuration: %d -> %d\n",
813 number2
, printer
->origconf
);
814 if ((errcode
= libusb_set_configuration(printer
->handle
,
815 printer
->origconf
)) < 0)
817 if (errcode
!= LIBUSB_ERROR_BUSY
)
820 libusb_get_device_descriptor (printer
->device
, &devdesc
);
823 "DEBUG: Failed to set configuration %d\n",
827 "DEBUG: Failed to set configuration %d for %04x:%04x\n",
828 printer
->origconf
, devdesc
.idVendor
, devdesc
.idProduct
);
834 * Re-attach "usblp" kernel module if it was attached before using this
837 if (printer
->usblp_attached
== 1)
838 if (libusb_attach_kernel_driver(printer
->handle
, number1
) < 0)
840 errcode
= libusb_get_device_descriptor (printer
->device
, &devdesc
);
843 "DEBUG: Failed to re-attach \"usblp\" kernel module\n");
846 "DEBUG: Failed to re-attach \"usblp\" kernel module to "
847 "%04x:%04x\n", devdesc
.idVendor
, devdesc
.idProduct
);
852 "DEBUG: Failed to get configuration descriptor %d\n",
856 * Reset the device to clean up after the job
859 if (printer
->reset_after_job
== 1)
861 if ((errcode
= libusb_reset_device(printer
->handle
)) < 0)
863 "DEBUG: Device reset failed, error code: %d\n",
867 "DEBUG: Resetting printer.\n");
871 * Close the interface and return...
874 libusb_close(printer
->handle
);
875 printer
->handle
= NULL
;
883 * 'find_device()' - Find or enumerate USB printers.
886 static usb_printer_t
* /* O - Found printer */
887 find_device(usb_cb_t cb
, /* I - Callback function */
888 const void *data
) /* I - User data for callback */
890 libusb_device
**list
; /* List of connected USB devices */
891 libusb_device
*device
= NULL
; /* Current device */
892 struct libusb_device_descriptor devdesc
;
893 /* Current device descriptor */
894 struct libusb_config_descriptor
*confptr
= NULL
;
895 /* Pointer to current configuration */
896 const struct libusb_interface
*ifaceptr
= NULL
;
897 /* Pointer to current interface */
898 const struct libusb_interface_descriptor
*altptr
= NULL
;
899 /* Pointer to current alternate setting */
900 const struct libusb_endpoint_descriptor
*endpptr
= NULL
;
901 /* Pointer to current endpoint */
902 ssize_t err
= 0, /* Error code */
903 numdevs
, /* number of connected devices */
905 uint8_t conf
, /* Current configuration */
906 iface
, /* Current interface */
907 altset
, /* Current alternate setting */
908 protocol
, /* Current protocol */
909 endp
, /* Current endpoint */
910 read_endp
, /* Current read endpoint */
911 write_endp
; /* Current write endpoint */
912 char device_id
[1024],/* IEEE-1284 device ID */
915 static usb_printer_t printer
; /* Current printer */
919 * Initialize libusb...
922 err
= libusb_init(NULL
);
925 fprintf(stderr
, "DEBUG: Unable to initialize USB access via libusb, "
926 "libusb error %i\n", err
);
930 numdevs
= libusb_get_device_list(NULL
, &list
);
931 fprintf(stderr
, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs
);
934 * Then loop through the devices it found...
938 for (i
= 0; i
< numdevs
; i
++)
943 * Ignore devices with no configuration data and anything that is not
947 if (libusb_get_device_descriptor(device
, &devdesc
) < 0)
950 if (!devdesc
.bNumConfigurations
|| !devdesc
.idVendor
||
954 printer
.quirks
= quirks(devdesc
.idVendor
, devdesc
.idProduct
);
957 * Ignore blacklisted printers...
960 if (printer
.quirks
& USBLP_QUIRK_BLACKLIST
)
963 for (conf
= 0; conf
< devdesc
.bNumConfigurations
; conf
++)
965 if (libusb_get_config_descriptor(device
, conf
, &confptr
) < 0)
967 for (iface
= 0, ifaceptr
= confptr
->interface
;
968 iface
< confptr
->bNumInterfaces
;
969 iface
++, ifaceptr
++)
972 * Some printers offer multiple interfaces...
977 for (altset
= 0, altptr
= ifaceptr
->altsetting
;
978 altset
< ifaceptr
->num_altsetting
;
979 altset
++, altptr
++)
982 * Currently we only support unidirectional and bidirectional
983 * printers. Future versions of this code will support the
984 * 1284.4 (packet mode) protocol as well.
987 if (((altptr
->bInterfaceClass
!= LIBUSB_CLASS_PRINTER
||
988 altptr
->bInterfaceSubClass
!= 1) &&
989 ((printer
.quirks
& USBLP_QUIRK_BAD_CLASS
) == 0)) ||
990 (altptr
->bInterfaceProtocol
!= 1 && /* Unidirectional */
991 altptr
->bInterfaceProtocol
!= 2) || /* Bidirectional */
992 altptr
->bInterfaceProtocol
< protocol
)
995 if (printer
.quirks
& USBLP_QUIRK_BAD_CLASS
)
996 fprintf(stderr
, "DEBUG: Printer does not report class 7 and/or "
997 "subclass 1 but works as a printer anyway\n");
1002 for (endp
= 0, endpptr
= altptr
->endpoint
;
1003 endp
< altptr
->bNumEndpoints
;
1004 endp
++, endpptr
++)
1005 if ((endpptr
->bmAttributes
& LIBUSB_TRANSFER_TYPE_MASK
) ==
1006 LIBUSB_TRANSFER_TYPE_BULK
)
1008 if (endpptr
->bEndpointAddress
& LIBUSB_ENDPOINT_DIR_MASK
)
1014 if (write_endp
>= 0)
1017 * Save the best match so far...
1020 protocol
= altptr
->bInterfaceProtocol
;
1021 printer
.altset
= altset
;
1022 printer
.write_endp
= write_endp
;
1024 printer
.read_endp
= read_endp
;
1026 printer
.read_endp
= -1;
1032 printer
.device
= device
;
1033 printer
.conf
= conf
;
1034 printer
.iface
= iface
;
1035 printer
.protocol
= protocol
;
1036 printer
.handle
= NULL
;
1038 if (!open_device(&printer
, data
!= NULL
))
1040 get_device_id(&printer
, device_id
, sizeof(device_id
));
1041 make_device_uri(&printer
, device_id
, device_uri
,
1042 sizeof(device_uri
));
1044 fprintf(stderr
, "DEBUG2: Printer found with device ID: %s "
1046 device_id
, device_uri
);
1048 if ((*cb
)(&printer
, device_uri
, device_id
, data
))
1050 fprintf(stderr
, "DEBUG: Device protocol: %d\n",
1052 if (printer
.quirks
& USBLP_QUIRK_BIDIR
)
1054 printer
.read_endp
= -1;
1055 fprintf(stderr
, "DEBUG: Printer reports bi-di support "
1056 "but in reality works only uni-directionally\n");
1058 if (printer
.read_endp
!= -1)
1060 printer
.read_endp
= confptr
->interface
[printer
.iface
].
1061 altsetting
[printer
.altset
].
1062 endpoint
[printer
.read_endp
].
1066 fprintf(stderr
, "DEBUG: Uni-directional USB communication "
1068 printer
.write_endp
= confptr
->interface
[printer
.iface
].
1069 altsetting
[printer
.altset
].
1070 endpoint
[printer
.write_endp
].
1072 if (printer
.quirks
& USBLP_QUIRK_NO_REATTACH
)
1074 printer
.usblp_attached
= 0;
1075 fprintf(stderr
, "DEBUG: Printer does not like usblp "
1076 "kernel module to be re-attached after job\n");
1078 libusb_free_config_descriptor(confptr
);
1082 close_device(&printer
);
1086 libusb_free_config_descriptor(confptr
);
1091 * If we get this far without returning, then we haven't found a printer
1100 libusb_free_device_list(list
, 1);
1108 * 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
1111 static int /* O - 0 on success, -1 on error */
1112 get_device_id(usb_printer_t
*printer
, /* I - Printer */
1113 char *buffer
, /* I - String buffer */
1114 size_t bufsize
) /* I - Number of bytes in buffer */
1116 int length
; /* Length of device ID */
1119 if (libusb_control_transfer(printer
->handle
,
1120 LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_ENDPOINT_IN
|
1121 LIBUSB_RECIPIENT_INTERFACE
,
1123 (printer
->iface
<< 8) | printer
->altset
,
1124 (unsigned char *)buffer
, bufsize
, 5000) < 0)
1131 * Extract the length of the device ID string from the first two
1132 * bytes. The 1284 spec says the length is stored MSB first...
1135 length
= (((unsigned)buffer
[0] & 255) << 8) |
1136 ((unsigned)buffer
[1] & 255);
1139 * Check to see if the length is larger than our buffer or less than 14 bytes
1140 * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
1142 * If the length is out-of-range, assume that the vendor incorrectly
1143 * implemented the 1284 spec and re-read the length as LSB first,..
1146 if (length
> bufsize
|| length
< 14)
1147 length
= (((unsigned)buffer
[1] & 255) << 8) |
1148 ((unsigned)buffer
[0] & 255);
1150 if (length
> bufsize
)
1156 * Invalid device ID, clear it!
1166 * Copy the device ID text to the beginning of the buffer and
1170 memmove(buffer
, buffer
+ 2, length
);
1171 buffer
[length
] = '\0';
1178 * 'list_cb()' - List USB printers for discovery.
1181 static int /* O - 0 to continue, 1 to stop */
1182 list_cb(usb_printer_t
*printer
, /* I - Printer */
1183 const char *device_uri
, /* I - Device URI */
1184 const char *device_id
, /* I - IEEE-1284 device ID */
1185 const void *data
) /* I - User data (not used) */
1187 char make_model
[1024]; /* Make and model */
1191 * Get the device URI and make/model strings...
1194 if (backendGetMakeModel(device_id
, make_model
, sizeof(make_model
)))
1195 strlcpy(make_model
, "Unknown", sizeof(make_model
));
1198 * Report the printer...
1201 cupsBackendReport("direct", device_uri
, make_model
, make_model
, device_id
,
1213 * 'make_device_uri()' - Create a device URI for a USB printer.
1216 static char * /* O - Device URI */
1218 usb_printer_t
*printer
, /* I - Printer */
1219 const char *device_id
, /* I - IEEE-1284 device ID */
1220 char *uri
, /* I - Device URI buffer */
1221 size_t uri_size
) /* I - Size of device URI buffer */
1223 struct libusb_device_descriptor devdesc
;
1224 /* Current device descriptor */
1225 char options
[1024]; /* Device URI options */
1226 int num_values
; /* Number of 1284 parameters */
1227 cups_option_t
*values
; /* 1284 parameters */
1228 const char *mfg
, /* Manufacturer */
1230 *des
= NULL
, /* Description */
1231 *sern
; /* Serial number */
1232 size_t mfglen
; /* Length of manufacturer string */
1233 char tempmfg
[256], /* Temporary manufacturer string */
1234 tempsern
[256], /* Temporary serial number string */
1235 *tempptr
; /* Pointer into temp string */
1239 * Get the make, model, and serial numbers...
1242 num_values
= _cupsGet1284Values(device_id
, &values
);
1244 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
1245 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
1246 if ((sern
= cupsGetOption("SN", num_values
, values
)) == NULL
&&
1247 ((libusb_get_device_descriptor(printer
->device
, &devdesc
) >= 0) &&
1248 devdesc
.iSerialNumber
))
1251 * Try getting the serial number from the device itself...
1255 libusb_get_string_descriptor_ascii(printer
->handle
,
1256 devdesc
.iSerialNumber
,
1257 (unsigned char *)tempsern
,
1258 sizeof(tempsern
) - 1);
1261 tempsern
[length
] = '\0';
1266 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
1267 mfg
= cupsGetOption("MFG", num_values
, values
);
1269 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
1270 mdl
= cupsGetOption("MDL", num_values
, values
);
1273 * To maintain compatibility with the original character device backend on
1274 * Linux and *BSD, map manufacturer names...
1279 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
1281 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
1287 * No manufacturer? Use the model string or description...
1291 _ppdNormalizeMakeAndModel(mdl
, tempmfg
, sizeof(tempmfg
));
1292 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
1293 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
1294 _ppdNormalizeMakeAndModel(des
, tempmfg
, sizeof(tempmfg
));
1296 strlcpy(tempmfg
, "Unknown", sizeof(tempmfg
));
1298 if ((tempptr
= strchr(tempmfg
, ' ')) != NULL
)
1307 * No model? Use description...
1310 mdl
= des
; /* We remove the manufacturer name below */
1311 else if (!strncasecmp(mfg
, "Unknown", 7))
1314 mdl
= "Unknown Model";
1317 mfglen
= strlen(mfg
);
1319 if (!strncasecmp(mdl
, mfg
, mfglen
) && _cups_isspace(mdl
[mfglen
]))
1323 while (_cups_isspace(*mdl
))
1328 * Generate the device URI from the manufacturer, model, serial number,
1329 * and interface number...
1334 if (printer
->iface
> 0)
1335 snprintf(options
, sizeof(options
), "?serial=%s&interface=%d", sern
,
1338 snprintf(options
, sizeof(options
), "?serial=%s", sern
);
1340 else if (printer
->iface
> 0)
1341 snprintf(options
, sizeof(options
), "?interface=%d", printer
->iface
);
1345 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, "usb", NULL
, mfg
, 0,
1346 "/%s%s", mdl
, options
);
1348 cupsFreeOptions(num_values
, values
);
1355 * 'open_device()' - Open a connection to the USB printer.
1358 static int /* O - 0 on success, -1 on error */
1359 open_device(usb_printer_t
*printer
, /* I - Printer */
1360 int verbose
) /* I - Update connecting-to-device state? */
1362 struct libusb_device_descriptor devdesc
;
1363 /* Current device descriptor */
1364 struct libusb_config_descriptor
*confptr
= NULL
;
1365 /* Pointer to current configuration */
1366 int number1
= -1, /* Configuration/interface/altset */
1367 number2
= -1, /* numbers */
1369 char current
; /* Current configuration */
1373 * Return immediately if we are already connected...
1376 if (printer
->handle
)
1380 * Try opening the printer...
1383 if ((errcode
= libusb_open(printer
->device
, &printer
->handle
)) < 0)
1385 fprintf(stderr
, "DEBUG: Failed to open device, code: %d\n",
1390 printer
->usblp_attached
= 0;
1391 printer
->reset_after_job
= 0;
1394 fputs("STATE: +connecting-to-device\n", stderr
);
1396 if ((errcode
= libusb_get_device_descriptor(printer
->device
, &devdesc
)) < 0)
1398 fprintf(stderr
, "DEBUG: Failed to get device descriptor, code: %d\n",
1404 * Get the "usblp" kernel module out of the way. This backend only
1405 * works without the module attached.
1408 errcode
= libusb_kernel_driver_active(printer
->handle
, printer
->iface
);
1410 printer
->usblp_attached
= 0;
1411 else if (errcode
== 1)
1413 printer
->usblp_attached
= 1;
1415 libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1417 fprintf(stderr
, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1418 devdesc
.idVendor
, devdesc
.idProduct
);
1424 printer
->usblp_attached
= 0;
1425 fprintf(stderr
, "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" kernel module attached\n",
1426 devdesc
.idVendor
, devdesc
.idProduct
);
1431 * Set the desired configuration, but only if it needs changing. Some
1432 * printers (e.g., Samsung) don't like libusb_set_configuration. It will
1433 * succeed, but the following print job is sometimes silently lost by the
1437 if (libusb_control_transfer(printer
->handle
,
1438 LIBUSB_REQUEST_TYPE_STANDARD
| LIBUSB_ENDPOINT_IN
|
1439 LIBUSB_RECIPIENT_DEVICE
,
1440 8, /* GET_CONFIGURATION */
1441 0, 0, (unsigned char *)¤t
, 1, 5000) < 0)
1442 current
= 0; /* Assume not configured */
1444 printer
->origconf
= current
;
1447 libusb_get_config_descriptor (printer
->device
, printer
->conf
, &confptr
))
1450 fprintf(stderr
, "DEBUG: Failed to get config descriptor for %04x:%04x\n",
1451 devdesc
.idVendor
, devdesc
.idProduct
);
1454 number1
= confptr
->bConfigurationValue
;
1456 if (number1
!= current
)
1458 fprintf(stderr
, "DEBUG: Switching USB device configuration: %d -> %d\n",
1460 if ((errcode
= libusb_set_configuration(printer
->handle
, number1
)) < 0)
1463 * If the set fails, chances are that the printer only supports a
1464 * single configuration. Technically these printers don't conform to
1465 * the USB printer specification, but otherwise they'll work...
1468 if (errcode
!= LIBUSB_ERROR_BUSY
)
1469 fprintf(stderr
, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
1470 number1
, devdesc
.idVendor
, devdesc
.idProduct
);
1475 * Claim interfaces as needed...
1478 number1
= confptr
->interface
[printer
->iface
].
1479 altsetting
[printer
->altset
].bInterfaceNumber
;
1481 while ((errcode
= libusb_claim_interface(printer
->handle
, number1
)) < 0)
1483 if (errcode
!= LIBUSB_ERROR_BUSY
)
1486 "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
1487 number1
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1494 * Set alternate setting, but only if there is more than one option. Some
1495 * printers (e.g., Samsung) don't like usb_set_altinterface.
1498 if (confptr
->interface
[printer
->iface
].num_altsetting
> 1)
1500 number1
= confptr
->interface
[printer
->iface
].
1501 altsetting
[printer
->altset
].bInterfaceNumber
;
1502 number2
= confptr
->interface
[printer
->iface
].
1503 altsetting
[printer
->altset
].bAlternateSetting
;
1506 libusb_set_interface_alt_setting(printer
->handle
, number1
, number2
))
1509 if (errcode
!= LIBUSB_ERROR_BUSY
)
1512 "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
1514 number2
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1521 libusb_free_config_descriptor(confptr
);
1524 fputs("STATE: -connecting-to-device\n", stderr
);
1529 * If we get here, there was a hard error...
1535 fputs("STATE: -connecting-to-device\n", stderr
);
1537 libusb_close(printer
->handle
);
1538 printer
->handle
= NULL
;
1545 * 'print_cb()' - Find a USB printer for printing.
1548 static int /* O - 0 to continue, 1 to stop (found) */
1549 print_cb(usb_printer_t
*printer
, /* I - Printer */
1550 const char *device_uri
, /* I - Device URI */
1551 const char *device_id
, /* I - IEEE-1284 device ID */
1552 const void *data
) /* I - User data (make, model, S/N) */
1554 char requested_uri
[1024], /* Requested URI */
1555 *requested_ptr
, /* Pointer into requested URI */
1556 detected_uri
[1024], /* Detected URI */
1557 *detected_ptr
; /* Pointer into detected URI */
1561 * If we have an exact match, stop now...
1564 if (!strcmp((char *)data
, device_uri
))
1568 * Work on copies of the URIs...
1571 strlcpy(requested_uri
, (char *)data
, sizeof(requested_uri
));
1572 strlcpy(detected_uri
, device_uri
, sizeof(detected_uri
));
1575 * libusb-discovered URIs can have an "interface" specification and this
1576 * never happens for usblp-discovered URIs, so remove the "interface"
1577 * specification from the URI which we are checking currently. This way a
1578 * queue for a usblp-discovered printer can now be accessed via libusb.
1580 * Similarly, strip "?serial=NNN...NNN" as needed.
1583 if ((requested_ptr
= strstr(requested_uri
, "?interface=")) == NULL
)
1584 requested_ptr
= strstr(requested_uri
, "&interface=");
1585 if ((detected_ptr
= strstr(detected_uri
, "?interface=")) == NULL
)
1586 detected_ptr
= strstr(detected_uri
, "&interface=");
1588 if (!requested_ptr
&& detected_ptr
)
1591 * Strip "[?&]interface=nnn" from the detected printer.
1594 *detected_ptr
= '\0';
1596 else if (requested_ptr
&& !detected_ptr
)
1599 * Strip "[?&]interface=nnn" from the requested printer.
1602 *requested_ptr
= '\0';
1605 if ((requested_ptr
= strstr(requested_uri
, "?serial=?")) != NULL
)
1608 * Strip "?serial=?" from the requested printer. This is a special
1609 * case, as "?serial=?" means no serial number and not the serial
1610 * number '?'. This is not covered by the checks below...
1613 *requested_ptr
= '\0';
1616 if ((requested_ptr
= strstr(requested_uri
, "?serial=")) == NULL
&&
1617 (detected_ptr
= strstr(detected_uri
, "?serial=")) != NULL
)
1620 * Strip "?serial=nnn" from the detected printer.
1623 *detected_ptr
= '\0';
1625 else if (requested_ptr
&& !detected_ptr
)
1628 * Strip "?serial=nnn" from the requested printer.
1631 *requested_ptr
= '\0';
1634 return (!strcmp(requested_uri
, detected_uri
));
1639 * 'printer_class_soft_reset()' - Do the soft reset request specific to printers
1641 * This soft reset is specific to the printer device class and is much less
1642 * invasive than the general USB reset libusb_reset_device(). Especially it
1643 * does never happen that the USB addressing and configuration changes. What
1644 * is actually done is that all buffers get flushed and the bulk IN and OUT
1645 * pipes get reset to their default states. This clears all stall conditions.
1646 * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
1649 static int /* O - 0 on success, < 0 on error */
1650 printer_class_soft_reset(usb_printer_t
*printer
) /* I - Printer */
1652 struct libusb_config_descriptor
*confptr
= NULL
;
1653 /* Pointer to current configuration */
1657 if (libusb_get_config_descriptor(printer
->device
, printer
->conf
, &confptr
)
1659 interface
= printer
->iface
;
1661 interface
= confptr
->interface
[printer
->iface
].
1662 altsetting
[printer
->altset
].bInterfaceNumber
;
1663 libusb_free_config_descriptor(confptr
);
1664 if ((errcode
= libusb_control_transfer(printer
->handle
,
1665 LIBUSB_REQUEST_TYPE_CLASS
|
1666 LIBUSB_ENDPOINT_OUT
|
1667 LIBUSB_RECIPIENT_OTHER
,
1668 2, 0, interface
, NULL
, 0, 5000)) < 0)
1669 errcode
= libusb_control_transfer(printer
->handle
,
1670 LIBUSB_REQUEST_TYPE_CLASS
|
1671 LIBUSB_ENDPOINT_OUT
|
1672 LIBUSB_RECIPIENT_INTERFACE
,
1673 2, 0, interface
, NULL
, 0, 5000);
1679 * 'quirks()' - Get the known quirks of a given printer model
1682 static unsigned int quirks(int vendor
, int product
)
1686 for (i
= 0; quirk_printers
[i
].vendorId
; i
++)
1688 if (vendor
== quirk_printers
[i
].vendorId
&&
1689 (quirk_printers
[i
].productId
== 0x0000 ||
1690 product
== quirk_printers
[i
].productId
))
1691 return quirk_printers
[i
].quirks
;
1698 * 'read_thread()' - Thread to read the backchannel data on.
1701 static void *read_thread(void *reference
)
1703 unsigned char readbuffer
[512];
1715 * Read frequency: once every 250 milliseconds.
1719 delay
.tv_usec
= 250000;
1724 * Remember when we started so we can throttle the loop after the read
1728 gettimeofday(&now
, NULL
);
1731 * Calculate what 250 milliSeconds are in absolute time...
1734 timeradd(&now
, &delay
, &end
);
1736 rbytes
= sizeof(readbuffer
);
1737 readstatus
= libusb_bulk_transfer(g
.printer
->handle
,
1738 g
.printer
->read_endp
,
1741 if (readstatus
== LIBUSB_SUCCESS
&& rbytes
> 0)
1743 fprintf(stderr
, "DEBUG: Read %d bytes of back-channel data...\n",
1745 cupsBackChannelWrite((const char *)readbuffer
, rbytes
, 1.0);
1747 else if (readstatus
== LIBUSB_ERROR_TIMEOUT
)
1748 fputs("DEBUG: Got USB transaction timeout during read.\n", stderr
);
1749 else if (readstatus
== LIBUSB_ERROR_PIPE
)
1750 fputs("DEBUG: Got USB pipe stalled during read.\n", stderr
);
1751 else if (readstatus
== LIBUSB_ERROR_INTERRUPTED
)
1752 fputs("DEBUG: Got USB return aborted during read.\n", stderr
);
1755 * Make sure this loop executes no more than once every 250 miliseconds...
1758 if ((readstatus
!= LIBUSB_SUCCESS
|| rbytes
== 0) &&
1759 (g
.wait_eof
|| !g
.read_thread_stop
))
1761 gettimeofday(&now
, NULL
);
1762 if (timercmp(&now
, &end
, <))
1764 timersub(&end
, &now
, &timeleft
);
1765 usleep(1000000 * timeleft
.tv_sec
+ timeleft
.tv_usec
);
1768 } while (g
.wait_eof
|| !g
.read_thread_stop
);
1771 * Let the main thread know that we have completed the read thread...
1774 pthread_mutex_lock(&g
.read_thread_mutex
);
1775 g
.read_thread_done
= 1;
1776 pthread_cond_signal(&g
.read_thread_cond
);
1777 pthread_mutex_unlock(&g
.read_thread_mutex
);
1784 * 'sidechannel_thread()' - Handle side-channel requests.
1788 sidechannel_thread(void *reference
)
1790 cups_sc_command_t command
; /* Request command */
1791 cups_sc_status_t status
; /* Request/response status */
1792 char data
[2048]; /* Request/response data */
1793 int datalen
; /* Request/response data size */
1800 datalen
= sizeof(data
);
1802 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
1804 if (status
== CUPS_SC_STATUS_TIMEOUT
)
1812 case CUPS_SC_CMD_SOFT_RESET
: /* Do a soft reset */
1813 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1817 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
1818 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1822 case CUPS_SC_CMD_DRAIN_OUTPUT
: /* Drain all pending output */
1823 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1829 case CUPS_SC_CMD_GET_BIDI
: /* Is the connection bidirectional? */
1830 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1833 data
[0] = (g
.printer
->protocol
>= 2 ? 1 : 0);
1834 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1837 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1841 case CUPS_SC_CMD_GET_DEVICE_ID
: /* Return IEEE-1284 device ID */
1842 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1845 datalen
= sizeof(data
);
1846 if (get_device_id(g
.printer
, data
, sizeof(data
)))
1848 status
= CUPS_SC_STATUS_IO_ERROR
;
1853 status
= CUPS_SC_STATUS_OK
;
1854 datalen
= strlen(data
);
1856 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, datalen
, 1.0);
1858 if (datalen
< sizeof(data
))
1859 data
[datalen
] = '\0';
1861 data
[sizeof(data
) - 1] = '\0';
1864 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1868 case CUPS_SC_CMD_GET_STATE
: /* Return device state */
1869 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1872 data
[0] = CUPS_SC_STATE_ONLINE
;
1873 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1876 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1880 case CUPS_SC_CMD_GET_CONNECTED
: /* Return whether device is
1882 fputs("DEBUG: CUPS_SC_CMD_GET_CONNECTED received from driver...\n",
1885 data
[0] = (g
.printer
->handle
? 1 : 0);
1886 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1889 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1894 fprintf(stderr
, "DEBUG: Unknown side-channel command (%d) received "
1895 "from driver...\n", command
);
1897 cupsSideChannelWrite(command
, CUPS_SC_STATUS_NOT_IMPLEMENTED
,
1900 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1905 while (!g
.sidechannel_thread_stop
);
1907 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
1908 g
.sidechannel_thread_done
= 1;
1909 pthread_cond_signal(&g
.sidechannel_thread_cond
);
1910 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
1917 * 'soft_reset()' - Send a soft reset to the device.
1920 static void soft_reset(void)
1922 fd_set input_set
; /* Input set for select() */
1923 struct timeval tv
; /* Time value */
1924 char buffer
[2048]; /* Buffer */
1925 struct timespec cond_timeout
; /* pthread condition timeout */
1928 * Send an abort once a second until the I/O lock is released by the main
1932 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1933 while (g
.readwrite_lock
)
1935 gettimeofday(&tv
, NULL
);
1936 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
1937 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
1939 while (g
.readwrite_lock
)
1941 if (pthread_cond_timedwait(&g
.readwrite_lock_cond
,
1942 &g
.readwrite_lock_mutex
,
1943 &cond_timeout
) != 0)
1948 g
.readwrite_lock
= 1;
1949 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1952 * Flush bytes waiting on print_fd...
1957 FD_ZERO(&input_set
);
1958 FD_SET(g
.print_fd
, &input_set
);
1963 while (select(g
.print_fd
+1, &input_set
, NULL
, NULL
, &tv
) > 0)
1964 if (read(g
.print_fd
, buffer
, sizeof(buffer
)) <= 0)
1971 printer_class_soft_reset(g
.printer
);
1974 * Release the I/O lock...
1977 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1978 g
.readwrite_lock
= 0;
1979 pthread_cond_signal(&g
.readwrite_lock_cond
);
1980 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);