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_QUICK_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 { 0x0482, 0x0010, USBLP_QUIRK_BIDIR
}, /* Kyocera Mita FS 820,
146 by zut <kernel@zut.de> */
147 { 0x04a9, 0x10a2, USBLP_QUIRK_BIDIR
}, /* Canon, Inc. PIXMA iP4200
148 Printer, http://www.cups.org/str.php?L4155 */
149 { 0x04a9, 0x10b6, USBLP_QUIRK_BIDIR
}, /* Canon, Inc. PIXMA iP4300
150 Printer, https://bugs.launchpad.net/bugs/1032385 */
151 { 0x04a9, 0x1721, USBLP_QUIRK_BIDIR
}, /* Canon, Inc. MP210
152 https://bugzilla.redhat.com/show_bug.cgi?id=847923#c53 */
153 { 0x04a9, 0x170c, USBLP_QUIRK_BIDIR
}, /* Canon, Inc. MP500
154 Printer, https://bugs.launchpad.net/bugs/1032456 */
155 { 0x04a9, 0x1717, USBLP_QUIRK_BIDIR
}, /* Canon, Inc. MP510
156 Printer, https://bugs.launchpad.net/bugs/1050009 */
157 { 0x04a9, 0x173d, USBLP_QUIRK_BIDIR
}, /* Canon, Inc. MP550
158 Printer, http://www.cups.org/str.php?L4155 */
159 { 0x04a9, 0x173e, USBLP_QUIRK_BIDIR
}, /* Canon, Inc. MP560
160 Printer, http://www.cups.org/str.php?L4155 */
161 { 0x04f9, 0x001a, USBLP_QUIRK_NO_REATTACH
}, /* Brother Industries, Ltd
162 HL-1430 Laser Printer,
163 https://bugs.launchpad.net/bugs/1038695 */
164 { 0x04f9, 0x000d, USBLP_QUIRK_BIDIR
|
165 USBLP_QUIRK_NO_REATTACH
}, /* Brother Industries, Ltd
166 HL-1440 Laser Printer,
167 https://bugs.launchpad.net/bugs/1000253 */
168 { 0x06bc, 0x000b, USBLP_QUIRK_NO_REATTACH
}, /* Oki Data Corp.
169 Okipage 14ex Printer,
170 https://bugs.launchpad.net/bugs/872483 */
171 { 0x06bc, 0x01c7, USBLP_QUIRK_NO_REATTACH
}, /* Oki Data Corp. B410d,
172 https://bugs.launchpad.net/bugs/872483 */
173 { 0x04b8, 0x0001, USBLP_QUIRK_BIDIR
}, /* Seiko Epson Corp. Stylus Color 740 / Photo 750,
174 http://bugs.debian.org/697970 */
175 { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS
}, /* Seiko Epson Receipt
177 { 0x067b, 0x2305, USBLP_QUIRK_BIDIR
|
178 USBLP_QUIRK_NO_REATTACH
|
180 { 0x0924, 0x3ce9, USBLP_QUIRK_NO_REATTACH
}, /* Xerox Phaser 3124
181 https://bugzilla.redhat.com/show_bug.cgi?id=867392 */
182 { 0x0924, 0x4293, USBLP_QUIRK_NO_REATTACH
}, /* Xerox WorkCentre 3210
183 https://bugs.launchpad.net/bugs/1102470 */
184 /* Prolific Technology, Inc. PL2305 Parallel Port
185 (USB -> Parallel adapter), https://bugs.launchpad.net/bugs/987485 */
186 { 0x04e8, 0x0000, USBLP_QUIRK_RESET
}, /* All Samsung devices,
187 https://bugs.launchpad.net/bugs/1032456 */
188 { 0x0a5f, 0x0000, USBLP_QUIRK_BIDIR
}, /* All Zebra devices,
189 https://bugs.launchpad.net/bugs/1001028 */
191 { 0x04a9, 0x304a, USBLP_QUIRK_BLACKLIST
}, /* Canon CP-10 */
192 { 0x04a9, 0x3063, USBLP_QUIRK_BLACKLIST
}, /* Canon CP-100 */
193 { 0x04a9, 0x307c, USBLP_QUIRK_BLACKLIST
}, /* Canon CP-200 */
194 { 0x04a9, 0x307d, USBLP_QUIRK_BLACKLIST
}, /* Canon CP-300 */
195 { 0x04a9, 0x30bd, USBLP_QUIRK_BLACKLIST
}, /* Canon CP-220 */
196 { 0x04a9, 0x30be, USBLP_QUIRK_BLACKLIST
}, /* Canon CP-330 */
197 { 0x04a9, 0x30f6, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP400 */
198 { 0x04a9, 0x310b, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP600 */
199 { 0x04a9, 0x3127, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP710 */
200 { 0x04a9, 0x3128, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP510 */
201 { 0x04a9, 0x3141, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY ES1 */
202 { 0x04a9, 0x3142, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP730 */
203 { 0x04a9, 0x3143, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP720 */
204 { 0x04a9, 0x3170, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP750 */
205 { 0x04a9, 0x3171, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP740 */
206 { 0x04a9, 0x3185, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY ES2 */
207 { 0x04a9, 0x3186, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY ES20 */
208 { 0x04a9, 0x31aa, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP770 */
209 { 0x04a9, 0x31ab, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP760 */
210 { 0x04a9, 0x31b0, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY ES30 */
211 { 0x04a9, 0x31dd, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP780 */
212 { 0x04a9, 0x31ee, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY ES40 */
213 { 0x04a9, 0x3214, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP800 */
214 { 0x04a9, 0x3255, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP900 */
215 { 0x04a9, 0x3256, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP810 */
216 { 0x04a9, 0x30F5, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY CP500 */
217 { 0x04a9, 0x31AF, USBLP_QUIRK_BLACKLIST
}, /* Canon SELPHY ES3 */
218 /* MISSING PIDs: CP520, CP530, CP790 */
227 usb_globals_t g
= { 0 }; /* Globals */
228 libusb_device
**list
; /* List of connected USB devices */
235 static int close_device(usb_printer_t
*printer
);
236 static usb_printer_t
*find_device(usb_cb_t cb
, const void *data
);
237 static int get_device_id(usb_printer_t
*printer
, char *buffer
,
239 static int list_cb(usb_printer_t
*printer
, const char *device_uri
,
240 const char *device_id
, const void *data
);
241 static char *make_device_uri(usb_printer_t
*printer
,
242 const char *device_id
,
243 char *uri
, size_t uri_size
);
244 static int open_device(usb_printer_t
*printer
, int verbose
);
245 static int print_cb(usb_printer_t
*printer
, const char *device_uri
,
246 const char *device_id
, const void *data
);
247 static int printer_class_soft_reset(usb_printer_t
*printer
);
248 static unsigned int quirks(int vendor
, int product
);
249 static void *read_thread(void *reference
);
250 static void *sidechannel_thread(void *reference
);
251 static void soft_reset(void);
255 * 'list_devices()' - List the available printers.
261 fputs("DEBUG: list_devices\n", stderr
);
262 find_device(list_cb
, NULL
);
267 * 'print_device()' - Print a file to a USB device.
270 int /* O - Exit status */
271 print_device(const char *uri
, /* I - Device URI */
272 const char *hostname
, /* I - Hostname/manufacturer */
273 const char *resource
, /* I - Resource/modelname */
274 char *options
, /* I - Device options/serial number */
275 int print_fd
, /* I - File descriptor to print */
276 int copies
, /* I - Copies to print */
277 int argc
, /* I - Number of command-line arguments (6 or 7) */
278 char *argv
[]) /* I - Command-line arguments */
280 int bytes
; /* Bytes written */
281 ssize_t total_bytes
; /* Total bytes written */
282 struct sigaction action
; /* Actions for POSIX signals */
283 int status
= CUPS_BACKEND_OK
,
284 /* Function results */
285 iostatus
; /* Current IO status */
286 pthread_t read_thread_id
, /* Read thread */
287 sidechannel_thread_id
; /* Side-channel thread */
288 int have_sidechannel
= 0, /* Was the side-channel thread started? */
289 have_backchannel
= 0; /* Do we have a back channel? */
290 struct stat sidechannel_info
; /* Side-channel file descriptor info */
291 unsigned char print_buffer
[8192], /* Print data buffer */
292 *print_ptr
; /* Pointer into print data buffer */
293 fd_set input_set
; /* Input set for select() */
294 int nfds
; /* Number of file descriptors */
295 struct timeval
*timeout
, /* Timeout pointer */
297 struct timespec cond_timeout
; /* pthread condition timeout */
298 int num_opts
; /* Number of options */
299 cups_option_t
*opts
; /* Options */
300 const char *val
; /* Option value */
304 * See if the side-channel descriptor is valid...
307 have_sidechannel
= !fstat(CUPS_SC_FD
, &sidechannel_info
) &&
308 S_ISSOCK(sidechannel_info
.st_mode
);
310 g
.wait_eof
= WAIT_EOF
;
313 * Connect to the printer...
316 fprintf(stderr
, "DEBUG: Printing on printer with URI: %s\n", uri
);
317 while ((g
.printer
= find_device(print_cb
, uri
)) == NULL
)
319 _cupsLangPrintFilter(stderr
, "INFO",
320 _("Waiting for printer to become available."));
324 g
.print_fd
= print_fd
;
327 * Some devices need a reset after finishing a job, these devices are
328 * marked with the USBLP_QUIRK_RESET quirk.
330 g
.printer
->reset_after_job
= (g
.printer
->quirks
& USBLP_QUIRK_RESET
? 1 : 0);
333 * If we are printing data from a print driver on stdin, ignore SIGTERM
334 * so that the driver can finish out any page data, e.g. to eject the
335 * current page. We only do this for stdin printing as otherwise there
336 * is no way to cancel a raw print job...
341 memset(&action
, 0, sizeof(action
));
343 sigemptyset(&action
.sa_mask
);
344 action
.sa_handler
= SIG_IGN
;
345 sigaction(SIGTERM
, &action
, NULL
);
349 * Start the side channel thread if the descriptor is valid...
352 pthread_mutex_init(&g
.readwrite_lock_mutex
, NULL
);
353 pthread_cond_init(&g
.readwrite_lock_cond
, NULL
);
354 g
.readwrite_lock
= 1;
356 if (have_sidechannel
)
358 g
.sidechannel_thread_stop
= 0;
359 g
.sidechannel_thread_done
= 0;
361 pthread_cond_init(&g
.sidechannel_thread_cond
, NULL
);
362 pthread_mutex_init(&g
.sidechannel_thread_mutex
, NULL
);
364 if (pthread_create(&sidechannel_thread_id
, NULL
, sidechannel_thread
, NULL
))
366 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
367 _cupsLangPrintFilter(stderr
, "ERROR",
368 _("There was an unrecoverable USB error."));
369 fputs("DEBUG: Couldn't create side-channel thread.\n", stderr
);
370 close_device(g
.printer
);
371 return (CUPS_BACKEND_STOP
);
376 * Debug mode: If option "usb-unidir" is given, always deactivate
380 num_opts
= cupsParseOptions(argv
[5], 0, &opts
);
381 val
= cupsGetOption("usb-unidir", num_opts
, opts
);
382 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
383 strcasecmp(val
, "false"))
385 g
.printer
->read_endp
= -1;
386 fprintf(stderr
, "DEBUG: Forced uni-directional communication "
387 "via \"usb-unidir\" option.\n");
391 * Debug mode: If option "usb-no-reattach" is given, do not re-attach
392 * the usblp kernel module after the job has completed.
395 val
= cupsGetOption("usb-no-reattach", num_opts
, opts
);
396 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
397 strcasecmp(val
, "false"))
399 g
.printer
->usblp_attached
= 0;
400 fprintf(stderr
, "DEBUG: Forced not re-attaching the usblp kernel module "
401 "after the job via \"usb-no-reattach\" option.\n");
405 * Get the read thread going...
408 if (g
.printer
->read_endp
!= -1)
410 have_backchannel
= 1;
412 g
.read_thread_stop
= 0;
413 g
.read_thread_done
= 0;
415 pthread_cond_init(&g
.read_thread_cond
, NULL
);
416 pthread_mutex_init(&g
.read_thread_mutex
, NULL
);
418 if (pthread_create(&read_thread_id
, NULL
, read_thread
, NULL
))
420 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
421 _cupsLangPrintFilter(stderr
, "ERROR",
422 _("There was an unrecoverable USB error."));
423 fputs("DEBUG: Couldn't create read thread.\n", stderr
);
424 close_device(g
.printer
);
425 return (CUPS_BACKEND_STOP
);
429 fprintf(stderr
, "DEBUG: Uni-directional device/mode, back channel "
433 * The main thread sends the print file...
439 print_ptr
= print_buffer
;
441 while (status
== CUPS_BACKEND_OK
&& copies
-- > 0)
443 _cupsLangPrintFilter(stderr
, "INFO", _("Sending data to printer."));
445 if (print_fd
!= STDIN_FILENO
)
447 fputs("PAGE: 1 1\n", stderr
);
448 lseek(print_fd
, 0, SEEK_SET
);
451 while (status
== CUPS_BACKEND_OK
)
456 FD_SET(print_fd
, &input_set
);
459 * Calculate select timeout...
460 * If we have data waiting to send timeout is 100ms.
461 * else if we're draining print_fd timeout is 0.
462 * else we're waiting forever...
468 tv
.tv_usec
= 100000; /* 100ms */
471 else if (g
.drain_output
)
481 * I/O is unlocked around select...
484 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
485 g
.readwrite_lock
= 0;
486 pthread_cond_signal(&g
.readwrite_lock_cond
);
487 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
489 nfds
= select(print_fd
+ 1, &input_set
, NULL
, NULL
, timeout
);
492 * Reacquire the lock...
495 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
496 while (g
.readwrite_lock
)
497 pthread_cond_wait(&g
.readwrite_lock_cond
, &g
.readwrite_lock_mutex
);
498 g
.readwrite_lock
= 1;
499 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
503 if (errno
== EINTR
&& total_bytes
== 0)
505 fputs("DEBUG: Received an interrupt before any bytes were "
506 "written, aborting.\n", stderr
);
507 close_device(g
.printer
);
508 return (CUPS_BACKEND_OK
);
510 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
512 _cupsLangPrintFilter(stderr
, "ERROR",
513 _("Unable to read print data."));
514 perror("DEBUG: select");
515 close_device(g
.printer
);
516 return (CUPS_BACKEND_FAILED
);
521 * If drain output has finished send a response...
524 if (g
.drain_output
&& !nfds
&& !g
.print_bytes
)
526 /* Send a response... */
527 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
532 * Check if we have print data ready...
535 if (FD_ISSET(print_fd
, &input_set
))
537 g
.print_bytes
= read(print_fd
, print_buffer
, sizeof(print_buffer
));
539 if (g
.print_bytes
< 0)
542 * Read error - bail if we don't see EAGAIN or EINTR...
545 if (errno
!= EAGAIN
&& errno
!= EINTR
)
547 _cupsLangPrintFilter(stderr
, "ERROR",
548 _("Unable to read print data."));
549 perror("DEBUG: read");
550 close_device(g
.printer
);
551 return (CUPS_BACKEND_FAILED
);
556 else if (g
.print_bytes
== 0)
559 * End of file, break out of the loop...
565 print_ptr
= print_buffer
;
567 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
573 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
574 g
.printer
->write_endp
,
575 print_buffer
, g
.print_bytes
,
578 * Ignore timeout errors, but retain the number of bytes written to
579 * avoid sending duplicate data...
582 if (iostatus
== LIBUSB_ERROR_TIMEOUT
)
584 fputs("DEBUG: Got USB transaction timeout during write.\n", stderr
);
589 * If we've stalled, retry the write...
592 else if (iostatus
== LIBUSB_ERROR_PIPE
)
594 fputs("DEBUG: Got USB pipe stalled during write.\n", stderr
);
596 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
597 g
.printer
->write_endp
,
598 print_buffer
, g
.print_bytes
,
603 * Retry a write after an aborted write since we probably just got
607 else if (iostatus
== LIBUSB_ERROR_INTERRUPTED
)
609 fputs("DEBUG: Got USB return aborted during write.\n", stderr
);
611 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
612 g
.printer
->write_endp
,
613 print_buffer
, g
.print_bytes
,
620 * Write error - bail if we don't see an error we can retry...
623 _cupsLangPrintFilter(stderr
, "ERROR",
624 _("Unable to send data to printer."));
625 fprintf(stderr
, "DEBUG: libusb write operation returned %x.\n",
628 status
= CUPS_BACKEND_FAILED
;
633 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n",
636 g
.print_bytes
-= bytes
;
638 total_bytes
+= bytes
;
642 if (print_fd
!= 0 && status
== CUPS_BACKEND_OK
)
643 fprintf(stderr
, "DEBUG: Sending print file, " CUPS_LLFMT
" bytes...\n",
644 CUPS_LLCAST total_bytes
);
648 fprintf(stderr
, "DEBUG: Sent " CUPS_LLFMT
" bytes...\n",
649 CUPS_LLCAST total_bytes
);
652 * Signal the side channel thread to exit...
655 if (have_sidechannel
)
658 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
659 g
.readwrite_lock
= 0;
660 pthread_cond_signal(&g
.readwrite_lock_cond
);
661 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
663 g
.sidechannel_thread_stop
= 1;
664 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
666 if (!g
.sidechannel_thread_done
)
668 gettimeofday(&tv
, NULL
);
669 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_SIDE_DELAY
;
670 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
672 while (!g
.sidechannel_thread_done
)
674 if (pthread_cond_timedwait(&g
.sidechannel_thread_cond
,
675 &g
.sidechannel_thread_mutex
,
681 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
685 * Signal the read thread to exit then wait 7 seconds for it to complete...
688 if (have_backchannel
)
690 g
.read_thread_stop
= 1;
692 pthread_mutex_lock(&g
.read_thread_mutex
);
694 if (!g
.read_thread_done
)
696 fputs("DEBUG: Waiting for read thread to exit...\n", stderr
);
698 gettimeofday(&tv
, NULL
);
699 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_EOF_DELAY
;
700 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
702 while (!g
.read_thread_done
)
704 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
710 * If it didn't exit abort the pending read and wait an additional
714 if (!g
.read_thread_done
)
716 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
721 gettimeofday(&tv
, NULL
);
722 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
723 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
725 while (!g
.read_thread_done
)
727 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
734 pthread_mutex_unlock(&g
.read_thread_mutex
);
738 * Close the connection and input file and general clean up...
741 close_device(g
.printer
);
747 libusb_free_device_list(list
, 1);
755 * 'close_device()' - Close the connection to the USB printer.
758 static int /* I - 0 on success, -1 on failure */
759 close_device(usb_printer_t
*printer
) /* I - Printer */
761 struct libusb_device_descriptor devdesc
;
762 /* Current device descriptor */
763 struct libusb_config_descriptor
*confptr
;
764 /* Pointer to current configuration */
770 * Release interfaces before closing so that we know all data is written
774 int errcode
; /* Return value of libusb function */
775 int number1
, /* Interface number */
776 number2
; /* Configuration number */
779 libusb_get_config_descriptor(printer
->device
, printer
->conf
, &confptr
);
782 number1
= confptr
->interface
[printer
->iface
].
783 altsetting
[printer
->altset
].bInterfaceNumber
;
784 libusb_release_interface(printer
->handle
, number1
);
786 number2
= confptr
->bConfigurationValue
;
788 libusb_free_config_descriptor(confptr
);
791 * If we have changed the configuration from one valid configuration
792 * to another, restore the old one
794 if (printer
->origconf
> 0 && printer
->origconf
!= number2
)
796 fprintf(stderr
, "DEBUG: Restoring USB device configuration: %d -> %d\n",
797 number2
, printer
->origconf
);
798 if ((errcode
= libusb_set_configuration(printer
->handle
,
799 printer
->origconf
)) < 0)
801 if (errcode
!= LIBUSB_ERROR_BUSY
)
804 libusb_get_device_descriptor (printer
->device
, &devdesc
);
807 "DEBUG: Failed to set configuration %d\n",
811 "DEBUG: Failed to set configuration %d for %04x:%04x\n",
812 printer
->origconf
, devdesc
.idVendor
, devdesc
.idProduct
);
818 * Re-attach "usblp" kernel module if it was attached before using this
821 if (printer
->usblp_attached
== 1)
822 if (libusb_attach_kernel_driver(printer
->handle
, number1
) < 0)
824 errcode
= libusb_get_device_descriptor (printer
->device
, &devdesc
);
827 "DEBUG: Failed to re-attach \"usblp\" kernel module\n");
830 "DEBUG: Failed to re-attach \"usblp\" kernel module to "
831 "%04x:%04x\n", devdesc
.idVendor
, devdesc
.idProduct
);
836 "DEBUG: Failed to get configuration descriptor %d\n",
840 * Reset the device to clean up after the job
843 if (printer
->reset_after_job
== 1)
845 if ((errcode
= libusb_reset_device(printer
->handle
)) < 0)
847 "DEBUG: Device reset failed, error code: %d\n",
851 "DEBUG: Resetting printer.\n");
855 * Close the interface and return...
858 libusb_close(printer
->handle
);
859 printer
->handle
= NULL
;
867 * 'find_device()' - Find or enumerate USB printers.
870 static usb_printer_t
* /* O - Found printer */
871 find_device(usb_cb_t cb
, /* I - Callback function */
872 const void *data
) /* I - User data for callback */
874 libusb_device
**list
; /* List of connected USB devices */
875 libusb_device
*device
= NULL
; /* Current device */
876 struct libusb_device_descriptor devdesc
;
877 /* Current device descriptor */
878 struct libusb_config_descriptor
*confptr
= NULL
;
879 /* Pointer to current configuration */
880 const struct libusb_interface
*ifaceptr
= NULL
;
881 /* Pointer to current interface */
882 const struct libusb_interface_descriptor
*altptr
= NULL
;
883 /* Pointer to current alternate setting */
884 const struct libusb_endpoint_descriptor
*endpptr
= NULL
;
885 /* Pointer to current endpoint */
886 ssize_t numdevs
, /* number of connected devices */
888 uint8_t conf
, /* Current configuration */
889 iface
, /* Current interface */
890 altset
, /* Current alternate setting */
891 protocol
, /* Current protocol */
892 endp
, /* Current endpoint */
893 read_endp
, /* Current read endpoint */
894 write_endp
; /* Current write endpoint */
895 char device_id
[1024],/* IEEE-1284 device ID */
898 static usb_printer_t printer
; /* Current printer */
902 * Initialize libusb...
906 numdevs
= libusb_get_device_list(NULL
, &list
);
907 fprintf(stderr
, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs
);
910 * Then loop through the devices it found...
914 for (i
= 0; i
< numdevs
; i
++)
919 * Ignore devices with no configuration data and anything that is not
923 if (libusb_get_device_descriptor(device
, &devdesc
) < 0)
926 if (!devdesc
.bNumConfigurations
|| !devdesc
.idVendor
||
930 printer
.quirks
= quirks(devdesc
.idVendor
, devdesc
.idProduct
);
933 * Ignore blacklisted printers...
936 if (printer
.quirks
& USBLP_QUIRK_BLACKLIST
)
939 for (conf
= 0; conf
< devdesc
.bNumConfigurations
; conf
++)
941 if (libusb_get_config_descriptor(device
, conf
, &confptr
) < 0)
943 for (iface
= 0, ifaceptr
= confptr
->interface
;
944 iface
< confptr
->bNumInterfaces
;
945 iface
++, ifaceptr
++)
948 * Some printers offer multiple interfaces...
953 for (altset
= 0, altptr
= ifaceptr
->altsetting
;
954 altset
< ifaceptr
->num_altsetting
;
955 altset
++, altptr
++)
958 * Currently we only support unidirectional and bidirectional
959 * printers. Future versions of this code will support the
960 * 1284.4 (packet mode) protocol as well.
963 if (((altptr
->bInterfaceClass
!= LIBUSB_CLASS_PRINTER
||
964 altptr
->bInterfaceSubClass
!= 1) &&
965 ((printer
.quirks
& USBLP_QUIRK_BAD_CLASS
) == 0)) ||
966 (altptr
->bInterfaceProtocol
!= 1 && /* Unidirectional */
967 altptr
->bInterfaceProtocol
!= 2) || /* Bidirectional */
968 altptr
->bInterfaceProtocol
< protocol
)
971 if (printer
.quirks
& USBLP_QUIRK_BAD_CLASS
)
972 fprintf(stderr
, "DEBUG: Printer does not report class 7 and/or "
973 "subclass 1 but works as a printer anyway\n");
978 for (endp
= 0, endpptr
= altptr
->endpoint
;
979 endp
< altptr
->bNumEndpoints
;
981 if ((endpptr
->bmAttributes
& LIBUSB_TRANSFER_TYPE_MASK
) ==
982 LIBUSB_TRANSFER_TYPE_BULK
)
984 if (endpptr
->bEndpointAddress
& LIBUSB_ENDPOINT_DIR_MASK
)
993 * Save the best match so far...
996 protocol
= altptr
->bInterfaceProtocol
;
997 printer
.altset
= altset
;
998 printer
.write_endp
= write_endp
;
1000 printer
.read_endp
= read_endp
;
1002 printer
.read_endp
= -1;
1008 printer
.device
= device
;
1009 printer
.conf
= conf
;
1010 printer
.iface
= iface
;
1011 printer
.protocol
= protocol
;
1012 printer
.handle
= NULL
;
1014 if (!open_device(&printer
, data
!= NULL
))
1016 get_device_id(&printer
, device_id
, sizeof(device_id
));
1017 make_device_uri(&printer
, device_id
, device_uri
,
1018 sizeof(device_uri
));
1020 fprintf(stderr
, "DEBUG2: Printer found with device ID: %s "
1022 device_id
, device_uri
);
1024 if ((*cb
)(&printer
, device_uri
, device_id
, data
))
1026 fprintf(stderr
, "DEBUG: Device protocol: %d\n",
1028 if (printer
.quirks
& USBLP_QUIRK_BIDIR
)
1030 printer
.read_endp
= -1;
1031 fprintf(stderr
, "DEBUG: Printer reports bi-di support "
1032 "but in reality works only uni-directionally\n");
1034 if (printer
.read_endp
!= -1)
1036 printer
.read_endp
= confptr
->interface
[printer
.iface
].
1037 altsetting
[printer
.altset
].
1038 endpoint
[printer
.read_endp
].
1042 fprintf(stderr
, "DEBUG: Uni-directional USB communication "
1044 printer
.write_endp
= confptr
->interface
[printer
.iface
].
1045 altsetting
[printer
.altset
].
1046 endpoint
[printer
.write_endp
].
1048 if (printer
.quirks
& USBLP_QUIRK_NO_REATTACH
)
1050 printer
.usblp_attached
= 0;
1051 fprintf(stderr
, "DEBUG: Printer does not like usblp "
1052 "kernel module to be re-attached after job\n");
1054 libusb_free_config_descriptor(confptr
);
1058 close_device(&printer
);
1062 libusb_free_config_descriptor(confptr
);
1067 * If we get this far without returning, then we haven't found a printer
1075 libusb_free_device_list(list
, 1);
1083 * 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
1086 static int /* O - 0 on success, -1 on error */
1087 get_device_id(usb_printer_t
*printer
, /* I - Printer */
1088 char *buffer
, /* I - String buffer */
1089 size_t bufsize
) /* I - Number of bytes in buffer */
1091 int length
; /* Length of device ID */
1094 if (libusb_control_transfer(printer
->handle
,
1095 LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_ENDPOINT_IN
|
1096 LIBUSB_RECIPIENT_INTERFACE
,
1098 (printer
->iface
<< 8) | printer
->altset
,
1099 (unsigned char *)buffer
, bufsize
, 5000) < 0)
1106 * Extract the length of the device ID string from the first two
1107 * bytes. The 1284 spec says the length is stored MSB first...
1110 length
= (((unsigned)buffer
[0] & 255) << 8) |
1111 ((unsigned)buffer
[1] & 255);
1114 * Check to see if the length is larger than our buffer or less than 14 bytes
1115 * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
1117 * If the length is out-of-range, assume that the vendor incorrectly
1118 * implemented the 1284 spec and re-read the length as LSB first,..
1121 if (length
> bufsize
|| length
< 14)
1122 length
= (((unsigned)buffer
[1] & 255) << 8) |
1123 ((unsigned)buffer
[0] & 255);
1125 if (length
> bufsize
)
1131 * Invalid device ID, clear it!
1141 * Copy the device ID text to the beginning of the buffer and
1145 memmove(buffer
, buffer
+ 2, length
);
1146 buffer
[length
] = '\0';
1153 * 'list_cb()' - List USB printers for discovery.
1156 static int /* O - 0 to continue, 1 to stop */
1157 list_cb(usb_printer_t
*printer
, /* I - Printer */
1158 const char *device_uri
, /* I - Device URI */
1159 const char *device_id
, /* I - IEEE-1284 device ID */
1160 const void *data
) /* I - User data (not used) */
1162 char make_model
[1024]; /* Make and model */
1166 * Get the device URI and make/model strings...
1169 if (backendGetMakeModel(device_id
, make_model
, sizeof(make_model
)))
1170 strlcpy(make_model
, "Unknown", sizeof(make_model
));
1173 * Report the printer...
1176 cupsBackendReport("direct", device_uri
, make_model
, make_model
, device_id
,
1188 * 'make_device_uri()' - Create a device URI for a USB printer.
1191 static char * /* O - Device URI */
1193 usb_printer_t
*printer
, /* I - Printer */
1194 const char *device_id
, /* I - IEEE-1284 device ID */
1195 char *uri
, /* I - Device URI buffer */
1196 size_t uri_size
) /* I - Size of device URI buffer */
1198 struct libusb_device_descriptor devdesc
;
1199 /* Current device descriptor */
1200 char options
[1024]; /* Device URI options */
1201 int num_values
; /* Number of 1284 parameters */
1202 cups_option_t
*values
; /* 1284 parameters */
1203 const char *mfg
, /* Manufacturer */
1205 *des
= NULL
, /* Description */
1206 *sern
; /* Serial number */
1207 size_t mfglen
; /* Length of manufacturer string */
1208 char tempmfg
[256], /* Temporary manufacturer string */
1209 tempsern
[256], /* Temporary serial number string */
1210 *tempptr
; /* Pointer into temp string */
1214 * Get the make, model, and serial numbers...
1217 num_values
= _cupsGet1284Values(device_id
, &values
);
1219 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
1220 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
1221 if ((sern
= cupsGetOption("SN", num_values
, values
)) == NULL
&&
1222 ((libusb_get_device_descriptor(printer
->device
, &devdesc
) >= 0) &&
1223 devdesc
.iSerialNumber
))
1226 * Try getting the serial number from the device itself...
1230 libusb_get_string_descriptor_ascii(printer
->handle
,
1231 devdesc
.iSerialNumber
,
1232 (unsigned char *)tempsern
,
1233 sizeof(tempsern
) - 1);
1236 tempsern
[length
] = '\0';
1241 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
1242 mfg
= cupsGetOption("MFG", num_values
, values
);
1244 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
1245 mdl
= cupsGetOption("MDL", num_values
, values
);
1248 * To maintain compatibility with the original character device backend on
1249 * Linux and *BSD, map manufacturer names...
1254 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
1256 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
1262 * No manufacturer? Use the model string or description...
1266 _ppdNormalizeMakeAndModel(mdl
, tempmfg
, sizeof(tempmfg
));
1267 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
1268 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
1269 _ppdNormalizeMakeAndModel(des
, tempmfg
, sizeof(tempmfg
));
1271 strlcpy(tempmfg
, "Unknown", sizeof(tempmfg
));
1273 if ((tempptr
= strchr(tempmfg
, ' ')) != NULL
)
1282 * No model? Use description...
1285 mdl
= des
; /* We remove the manufacturer name below */
1286 else if (!strncasecmp(mfg
, "Unknown", 7))
1289 mdl
= "Unknown Model";
1292 mfglen
= strlen(mfg
);
1294 if (!strncasecmp(mdl
, mfg
, mfglen
) && _cups_isspace(mdl
[mfglen
]))
1298 while (_cups_isspace(*mdl
))
1303 * Generate the device URI from the manufacturer, model, serial number,
1304 * and interface number...
1309 if (printer
->iface
> 0)
1310 snprintf(options
, sizeof(options
), "?serial=%s&interface=%d", sern
,
1313 snprintf(options
, sizeof(options
), "?serial=%s", sern
);
1315 else if (printer
->iface
> 0)
1316 snprintf(options
, sizeof(options
), "?interface=%d", printer
->iface
);
1320 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, "usb", NULL
, mfg
, 0,
1321 "/%s%s", mdl
, options
);
1323 cupsFreeOptions(num_values
, values
);
1330 * 'open_device()' - Open a connection to the USB printer.
1333 static int /* O - 0 on success, -1 on error */
1334 open_device(usb_printer_t
*printer
, /* I - Printer */
1335 int verbose
) /* I - Update connecting-to-device state? */
1337 struct libusb_device_descriptor devdesc
;
1338 /* Current device descriptor */
1339 struct libusb_config_descriptor
*confptr
= NULL
;
1340 /* Pointer to current configuration */
1341 int number1
= -1, /* Configuration/interface/altset */
1342 number2
= -1, /* numbers */
1344 char current
; /* Current configuration */
1348 * Return immediately if we are already connected...
1351 if (printer
->handle
)
1355 * Try opening the printer...
1358 if ((errcode
= libusb_open(printer
->device
, &printer
->handle
)) < 0)
1360 fprintf(stderr
, "DEBUG: Failed to open device, code: %d\n",
1365 printer
->usblp_attached
= 0;
1366 printer
->reset_after_job
= 0;
1369 fputs("STATE: +connecting-to-device\n", stderr
);
1371 if ((errcode
= libusb_get_device_descriptor(printer
->device
, &devdesc
)) < 0)
1373 fprintf(stderr
, "DEBUG: Failed to get device descriptor, code: %d\n",
1379 * Get the "usblp" kernel module out of the way. This backend only
1380 * works without the module attached.
1383 errcode
= libusb_kernel_driver_active(printer
->handle
, printer
->iface
);
1385 printer
->usblp_attached
= 0;
1386 else if (errcode
== 1)
1388 printer
->usblp_attached
= 1;
1390 libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1392 fprintf(stderr
, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1393 devdesc
.idVendor
, devdesc
.idProduct
);
1399 printer
->usblp_attached
= 0;
1400 fprintf(stderr
, "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" kernel module attached\n",
1401 devdesc
.idVendor
, devdesc
.idProduct
);
1406 * Set the desired configuration, but only if it needs changing. Some
1407 * printers (e.g., Samsung) don't like libusb_set_configuration. It will
1408 * succeed, but the following print job is sometimes silently lost by the
1412 if (libusb_control_transfer(printer
->handle
,
1413 LIBUSB_REQUEST_TYPE_STANDARD
| LIBUSB_ENDPOINT_IN
|
1414 LIBUSB_RECIPIENT_DEVICE
,
1415 8, /* GET_CONFIGURATION */
1416 0, 0, (unsigned char *)¤t
, 1, 5000) < 0)
1417 current
= 0; /* Assume not configured */
1419 printer
->origconf
= current
;
1422 libusb_get_config_descriptor (printer
->device
, printer
->conf
, &confptr
))
1425 fprintf(stderr
, "DEBUG: Failed to get config descriptor for %04x:%04x\n",
1426 devdesc
.idVendor
, devdesc
.idProduct
);
1429 number1
= confptr
->bConfigurationValue
;
1431 if (number1
!= current
)
1433 fprintf(stderr
, "DEBUG: Switching USB device configuration: %d -> %d\n",
1435 if ((errcode
= libusb_set_configuration(printer
->handle
, number1
)) < 0)
1438 * If the set fails, chances are that the printer only supports a
1439 * single configuration. Technically these printers don't conform to
1440 * the USB printer specification, but otherwise they'll work...
1443 if (errcode
!= LIBUSB_ERROR_BUSY
)
1444 fprintf(stderr
, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
1445 number1
, devdesc
.idVendor
, devdesc
.idProduct
);
1450 * Claim interfaces as needed...
1453 number1
= confptr
->interface
[printer
->iface
].
1454 altsetting
[printer
->altset
].bInterfaceNumber
;
1456 while ((errcode
= libusb_claim_interface(printer
->handle
, number1
)) < 0)
1458 if (errcode
!= LIBUSB_ERROR_BUSY
)
1461 "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
1462 number1
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1469 * Set alternate setting, but only if there is more than one option. Some
1470 * printers (e.g., Samsung) don't like usb_set_altinterface.
1473 if (confptr
->interface
[printer
->iface
].num_altsetting
> 1)
1475 number1
= confptr
->interface
[printer
->iface
].
1476 altsetting
[printer
->altset
].bInterfaceNumber
;
1477 number2
= confptr
->interface
[printer
->iface
].
1478 altsetting
[printer
->altset
].bAlternateSetting
;
1481 libusb_set_interface_alt_setting(printer
->handle
, number1
, number2
))
1484 if (errcode
!= LIBUSB_ERROR_BUSY
)
1487 "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
1489 number2
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1496 libusb_free_config_descriptor(confptr
);
1499 fputs("STATE: -connecting-to-device\n", stderr
);
1504 * If we get here, there was a hard error...
1510 fputs("STATE: -connecting-to-device\n", stderr
);
1512 libusb_close(printer
->handle
);
1513 printer
->handle
= NULL
;
1520 * 'print_cb()' - Find a USB printer for printing.
1523 static int /* O - 0 to continue, 1 to stop (found) */
1524 print_cb(usb_printer_t
*printer
, /* I - Printer */
1525 const char *device_uri
, /* I - Device URI */
1526 const char *device_id
, /* I - IEEE-1284 device ID */
1527 const void *data
) /* I - User data (make, model, S/N) */
1529 char requested_uri
[1024], /* Requested URI */
1530 *requested_ptr
, /* Pointer into requested URI */
1531 detected_uri
[1024], /* Detected URI */
1532 *detected_ptr
; /* Pointer into detected URI */
1536 * If we have an exact match, stop now...
1539 if (!strcmp((char *)data
, device_uri
))
1543 * Work on copies of the URIs...
1546 strlcpy(requested_uri
, (char *)data
, sizeof(requested_uri
));
1547 strlcpy(detected_uri
, device_uri
, sizeof(detected_uri
));
1550 * libusb-discovered URIs can have an "interface" specification and this
1551 * never happens for usblp-discovered URIs, so remove the "interface"
1552 * specification from the URI which we are checking currently. This way a
1553 * queue for a usblp-discovered printer can now be accessed via libusb.
1555 * Similarly, strip "?serial=NNN...NNN" as needed.
1558 if ((requested_ptr
= strstr(requested_uri
, "?interface=")) == NULL
)
1559 requested_ptr
= strstr(requested_uri
, "&interface=");
1560 if ((detected_ptr
= strstr(detected_uri
, "?interface=")) == NULL
)
1561 detected_ptr
= strstr(detected_uri
, "&interface=");
1563 if (!requested_ptr
&& detected_ptr
)
1566 * Strip "[?&]interface=nnn" from the detected printer.
1569 *detected_ptr
= '\0';
1571 else if (requested_ptr
&& !detected_ptr
)
1574 * Strip "[?&]interface=nnn" from the requested printer.
1577 *requested_ptr
= '\0';
1580 if ((requested_ptr
= strstr(requested_uri
, "?serial=?")) != NULL
)
1583 * Strip "?serial=?" from the requested printer. This is a special
1584 * case, as "?serial=?" means no serial number and not the serial
1585 * number '?'. This is not covered by the checks below...
1588 *requested_ptr
= '\0';
1591 if ((requested_ptr
= strstr(requested_uri
, "?serial=")) == NULL
&&
1592 (detected_ptr
= strstr(detected_uri
, "?serial=")) != NULL
)
1595 * Strip "?serial=nnn" from the detected printer.
1598 *detected_ptr
= '\0';
1600 else if (requested_ptr
&& !detected_ptr
)
1603 * Strip "?serial=nnn" from the requested printer.
1606 *requested_ptr
= '\0';
1609 return (!strcmp(requested_uri
, detected_uri
));
1614 * 'printer_class_soft_reset()' - Do the soft reset request specific to printers
1616 * This soft reset is specific to the printer device class and is much less
1617 * invasive than the general USB reset libusb_reset_device(). Especially it
1618 * does never happen that the USB addressing and configuration changes. What
1619 * is actually done is that all buffers get flushed and the bulk IN and OUT
1620 * pipes get reset to their default states. This clears all stall conditions.
1621 * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
1624 static int /* O - 0 on success, < 0 on error */
1625 printer_class_soft_reset(usb_printer_t
*printer
) /* I - Printer */
1627 struct libusb_config_descriptor
*confptr
= NULL
;
1628 /* Pointer to current configuration */
1632 if (libusb_get_config_descriptor(printer
->device
, printer
->conf
, &confptr
)
1634 interface
= printer
->iface
;
1636 interface
= confptr
->interface
[printer
->iface
].
1637 altsetting
[printer
->altset
].bInterfaceNumber
;
1638 libusb_free_config_descriptor(confptr
);
1639 if ((errcode
= libusb_control_transfer(printer
->handle
,
1640 LIBUSB_REQUEST_TYPE_CLASS
|
1641 LIBUSB_ENDPOINT_OUT
|
1642 LIBUSB_RECIPIENT_OTHER
,
1643 2, 0, interface
, NULL
, 0, 5000)) < 0)
1644 errcode
= libusb_control_transfer(printer
->handle
,
1645 LIBUSB_REQUEST_TYPE_CLASS
|
1646 LIBUSB_ENDPOINT_OUT
|
1647 LIBUSB_RECIPIENT_INTERFACE
,
1648 2, 0, interface
, NULL
, 0, 5000);
1654 * 'quirks()' - Get the known quirks of a given printer model
1657 static unsigned int quirks(int vendor
, int product
)
1661 for (i
= 0; quirk_printers
[i
].vendorId
; i
++)
1663 if (vendor
== quirk_printers
[i
].vendorId
&&
1664 (quirk_printers
[i
].productId
== 0x0000 ||
1665 product
== quirk_printers
[i
].productId
))
1666 return quirk_printers
[i
].quirks
;
1673 * 'read_thread()' - Thread to read the backchannel data on.
1676 static void *read_thread(void *reference
)
1678 unsigned char readbuffer
[512];
1690 * Read frequency: once every 250 milliseconds.
1694 delay
.tv_usec
= 250000;
1699 * Remember when we started so we can throttle the loop after the read
1703 gettimeofday(&now
, NULL
);
1706 * Calculate what 250 milliSeconds are in absolute time...
1709 timeradd(&now
, &delay
, &end
);
1711 rbytes
= sizeof(readbuffer
);
1712 readstatus
= libusb_bulk_transfer(g
.printer
->handle
,
1713 g
.printer
->read_endp
,
1716 if (readstatus
== LIBUSB_SUCCESS
&& rbytes
> 0)
1718 fprintf(stderr
, "DEBUG: Read %d bytes of back-channel data...\n",
1720 cupsBackChannelWrite((const char *)readbuffer
, rbytes
, 1.0);
1722 else if (readstatus
== LIBUSB_ERROR_TIMEOUT
)
1723 fputs("DEBUG: Got USB transaction timeout during read.\n", stderr
);
1724 else if (readstatus
== LIBUSB_ERROR_PIPE
)
1725 fputs("DEBUG: Got USB pipe stalled during read.\n", stderr
);
1726 else if (readstatus
== LIBUSB_ERROR_INTERRUPTED
)
1727 fputs("DEBUG: Got USB return aborted during read.\n", stderr
);
1730 * Make sure this loop executes no more than once every 250 miliseconds...
1733 if ((readstatus
!= LIBUSB_SUCCESS
|| rbytes
== 0) &&
1734 (g
.wait_eof
|| !g
.read_thread_stop
))
1736 gettimeofday(&now
, NULL
);
1737 if (timercmp(&now
, &end
, <))
1739 timersub(&end
, &now
, &timeleft
);
1740 usleep(1000000 * timeleft
.tv_sec
+ timeleft
.tv_usec
);
1743 } while (g
.wait_eof
|| !g
.read_thread_stop
);
1746 * Let the main thread know that we have completed the read thread...
1749 pthread_mutex_lock(&g
.read_thread_mutex
);
1750 g
.read_thread_done
= 1;
1751 pthread_cond_signal(&g
.read_thread_cond
);
1752 pthread_mutex_unlock(&g
.read_thread_mutex
);
1759 * 'sidechannel_thread()' - Handle side-channel requests.
1763 sidechannel_thread(void *reference
)
1765 cups_sc_command_t command
; /* Request command */
1766 cups_sc_status_t status
; /* Request/response status */
1767 char data
[2048]; /* Request/response data */
1768 int datalen
; /* Request/response data size */
1775 datalen
= sizeof(data
);
1777 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
1779 if (status
== CUPS_SC_STATUS_TIMEOUT
)
1787 case CUPS_SC_CMD_SOFT_RESET
: /* Do a soft reset */
1788 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1792 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
1793 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1797 case CUPS_SC_CMD_DRAIN_OUTPUT
: /* Drain all pending output */
1798 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1804 case CUPS_SC_CMD_GET_BIDI
: /* Is the connection bidirectional? */
1805 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1808 data
[0] = (g
.printer
->protocol
>= 2 ? 1 : 0);
1809 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1812 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1816 case CUPS_SC_CMD_GET_DEVICE_ID
: /* Return IEEE-1284 device ID */
1817 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1820 datalen
= sizeof(data
);
1821 if (get_device_id(g
.printer
, data
, sizeof(data
)))
1823 status
= CUPS_SC_STATUS_IO_ERROR
;
1828 status
= CUPS_SC_STATUS_OK
;
1829 datalen
= strlen(data
);
1831 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, datalen
, 1.0);
1833 if (datalen
< sizeof(data
))
1834 data
[datalen
] = '\0';
1836 data
[sizeof(data
) - 1] = '\0';
1839 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1843 case CUPS_SC_CMD_GET_STATE
: /* Return device state */
1844 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1847 data
[0] = CUPS_SC_STATE_ONLINE
;
1848 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1851 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1855 case CUPS_SC_CMD_GET_CONNECTED
: /* Return whether device is
1857 fputs("DEBUG: CUPS_SC_CMD_GET_CONNECTED received from driver...\n",
1860 data
[0] = (g
.printer
->handle
? 1 : 0);
1861 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1864 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1869 fprintf(stderr
, "DEBUG: Unknown side-channel command (%d) received "
1870 "from driver...\n", command
);
1872 cupsSideChannelWrite(command
, CUPS_SC_STATUS_NOT_IMPLEMENTED
,
1875 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1880 while (!g
.sidechannel_thread_stop
);
1882 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
1883 g
.sidechannel_thread_done
= 1;
1884 pthread_cond_signal(&g
.sidechannel_thread_cond
);
1885 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
1892 * 'soft_reset()' - Send a soft reset to the device.
1895 static void soft_reset(void)
1897 fd_set input_set
; /* Input set for select() */
1898 struct timeval tv
; /* Time value */
1899 char buffer
[2048]; /* Buffer */
1900 struct timespec cond_timeout
; /* pthread condition timeout */
1903 * Send an abort once a second until the I/O lock is released by the main
1907 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1908 while (g
.readwrite_lock
)
1910 gettimeofday(&tv
, NULL
);
1911 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
1912 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
1914 while (g
.readwrite_lock
)
1916 if (pthread_cond_timedwait(&g
.readwrite_lock_cond
,
1917 &g
.readwrite_lock_mutex
,
1918 &cond_timeout
) != 0)
1923 g
.readwrite_lock
= 1;
1924 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1927 * Flush bytes waiting on print_fd...
1932 FD_ZERO(&input_set
);
1933 FD_SET(g
.print_fd
, &input_set
);
1938 while (select(g
.print_fd
+1, &input_set
, NULL
, NULL
, &tv
) > 0)
1939 if (read(g
.print_fd
, buffer
, sizeof(buffer
)) <= 0)
1946 printer_class_soft_reset(g
.printer
);
1949 * Release the I/O lock...
1952 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1953 g
.readwrite_lock
= 0;
1954 pthread_cond_signal(&g
.readwrite_lock_cond
);
1955 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);