2 * LIBUSB interface code for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * Include necessary headers...
18 #include <cups/cups-private.h>
19 #include <cups/ppd-private.h>
22 #include <sys/select.h>
23 #include <sys/types.h>
30 * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
31 * the printer after we've finished sending all the data
35 #define WAIT_EOF_DELAY 7
36 #define WAIT_SIDE_DELAY 3
37 #define DEFAULT_TIMEOUT 5000L
44 typedef struct usb_printer_s
/**** USB Printer Data ****/
46 struct libusb_device
*device
; /* Device info */
47 int conf
, /* Configuration */
48 origconf
, /* Original configuration */
49 iface
, /* Interface */
50 altset
, /* Alternate setting */
51 write_endp
, /* Write endpoint */
52 read_endp
, /* Read endpoint */
53 protocol
, /* Protocol: 1 = Uni-di, 2 = Bi-di. */
54 usblp_attached
, /* "usblp" kernel module attached? */
55 reset_after_job
;/* Set to 1 by print_device() */
56 unsigned quirks
; /* Quirks flags */
57 struct libusb_device_handle
*handle
; /* Open handle to device */
60 typedef int (*usb_cb_t
)(usb_printer_t
*, const char *, const char *,
63 typedef struct usb_globals_s
/* Global USB printer information */
65 usb_printer_t
*printer
; /* Printer */
67 pthread_mutex_t read_thread_mutex
;
68 pthread_cond_t read_thread_cond
;
72 pthread_mutex_t readwrite_lock_mutex
;
73 pthread_cond_t readwrite_lock_cond
;
76 int print_fd
; /* File descriptor to print */
77 ssize_t print_bytes
; /* Print bytes read */
80 int drain_output
; /* Drain all pending output */
81 int bidi_flag
; /* 0=unidirectional, 1=bidirectional */
83 pthread_mutex_t sidechannel_thread_mutex
;
84 pthread_cond_t sidechannel_thread_cond
;
85 int sidechannel_thread_stop
;
86 int sidechannel_thread_done
;
90 * Quirks: various printer quirks are handled by this structure and its flags.
92 * The quirks table used to be compiled into the backend but is now loaded from
93 * one or more files in the /usr/share/cups/usb directory.
96 #define USB_QUIRK_BLACKLIST 0x0001 /* Does not conform to the spec */
97 #define USB_QUIRK_NO_REATTACH 0x0002 /* After printing we cannot re-attach
98 the usblp kernel module */
99 #define USB_QUIRK_SOFT_RESET 0x0004 /* After printing do a soft reset
101 #define USB_QUIRK_UNIDIR 0x0008 /* Requires unidirectional mode */
102 #define USB_QUIRK_USB_INIT 0x0010 /* Needs vendor USB init string */
103 #define USB_QUIRK_VENDOR_CLASS 0x0020 /* Descriptor uses vendor-specific
105 #define USB_QUIRK_DELAY_CLOSE 0x0040 /* Delay close */
106 #define USB_QUIRK_WHITELIST 0x0000 /* no quirks */
109 typedef struct usb_quirk_s
/* USB "quirk" information */
111 int vendor_id
, /* Affected vendor ID */
112 product_id
; /* Affected product ID or 0 for all */
113 unsigned quirks
; /* Quirks bitfield */
123 cups_array_t
*all_quirks
; /* Array of printer quirks */
124 usb_globals_t g
= { 0 }; /* Globals */
125 libusb_device
**all_list
; /* List of connected USB devices */
132 static int close_device(usb_printer_t
*printer
);
133 static int compare_quirks(usb_quirk_t
*a
, usb_quirk_t
*b
);
134 static usb_printer_t
*find_device(usb_cb_t cb
, const void *data
);
135 static unsigned find_quirks(int vendor_id
, int product_id
);
136 static int get_device_id(usb_printer_t
*printer
, char *buffer
,
138 static int list_cb(usb_printer_t
*printer
, const char *device_uri
,
139 const char *device_id
, const void *data
);
140 static void load_quirks(void);
141 static char *make_device_uri(usb_printer_t
*printer
,
142 const char *device_id
,
143 char *uri
, size_t uri_size
);
144 static int open_device(usb_printer_t
*printer
, int verbose
);
145 static int print_cb(usb_printer_t
*printer
, const char *device_uri
,
146 const char *device_id
, const void *data
);
147 static void *read_thread(void *reference
);
148 static void *sidechannel_thread(void *reference
);
149 static void soft_reset(void);
150 static int soft_reset_printer(usb_printer_t
*printer
);
154 * 'list_devices()' - List the available printers.
162 fputs("DEBUG: list_devices\n", stderr
);
163 find_device(list_cb
, NULL
);
168 * 'print_device()' - Print a file to a USB device.
171 int /* O - Exit status */
172 print_device(const char *uri
, /* I - Device URI */
173 const char *hostname
, /* I - Hostname/manufacturer */
174 const char *resource
, /* I - Resource/modelname */
175 char *options
, /* I - Device options/serial number */
176 int print_fd
, /* I - File descriptor to print */
177 int copies
, /* I - Copies to print */
178 int argc
, /* I - Number of command-line arguments (6 or 7) */
179 char *argv
[]) /* I - Command-line arguments */
181 int bytes
; /* Bytes written */
182 ssize_t total_bytes
; /* Total bytes written */
183 struct sigaction action
; /* Actions for POSIX signals */
184 int status
= CUPS_BACKEND_OK
,
185 /* Function results */
186 iostatus
; /* Current IO status */
187 pthread_t read_thread_id
, /* Read thread */
188 sidechannel_thread_id
; /* Side-channel thread */
189 int have_sidechannel
= 0, /* Was the side-channel thread started? */
190 have_backchannel
= 0; /* Do we have a back channel? */
191 struct stat sidechannel_info
; /* Side-channel file descriptor info */
192 unsigned char print_buffer
[8192], /* Print data buffer */
193 *print_ptr
; /* Pointer into print data buffer */
194 fd_set input_set
; /* Input set for select() */
195 int nfds
; /* Number of file descriptors */
196 struct timeval
*timeout
, /* Timeout pointer */
198 struct timespec cond_timeout
; /* pthread condition timeout */
199 int num_opts
; /* Number of options */
200 cups_option_t
*opts
; /* Options */
201 const char *val
; /* Option value */
207 * See if the side-channel descriptor is valid...
210 have_sidechannel
= !fstat(CUPS_SC_FD
, &sidechannel_info
) &&
211 S_ISSOCK(sidechannel_info
.st_mode
);
213 g
.wait_eof
= WAIT_EOF
;
216 * Connect to the printer...
219 fprintf(stderr
, "DEBUG: Printing on printer with URI: %s\n", uri
);
220 while ((g
.printer
= find_device(print_cb
, uri
)) == NULL
)
222 _cupsLangPrintFilter(stderr
, "INFO",
223 _("Waiting for printer to become available."));
227 g
.print_fd
= print_fd
;
230 * Some devices need a reset after finishing a job, these devices are
231 * marked with the USB_QUIRK_SOFT_RESET quirk.
233 g
.printer
->reset_after_job
= (g
.printer
->quirks
& USB_QUIRK_SOFT_RESET
? 1 : 0);
236 * If we are printing data from a print driver on stdin, ignore SIGTERM
237 * so that the driver can finish out any page data, e.g. to eject the
238 * current page. We only do this for stdin printing as otherwise there
239 * is no way to cancel a raw print job...
244 memset(&action
, 0, sizeof(action
));
246 sigemptyset(&action
.sa_mask
);
247 action
.sa_handler
= SIG_IGN
;
248 sigaction(SIGTERM
, &action
, NULL
);
252 * Start the side channel thread if the descriptor is valid...
255 pthread_mutex_init(&g
.readwrite_lock_mutex
, NULL
);
256 pthread_cond_init(&g
.readwrite_lock_cond
, NULL
);
257 g
.readwrite_lock
= 1;
259 if (have_sidechannel
)
261 g
.sidechannel_thread_stop
= 0;
262 g
.sidechannel_thread_done
= 0;
264 pthread_cond_init(&g
.sidechannel_thread_cond
, NULL
);
265 pthread_mutex_init(&g
.sidechannel_thread_mutex
, NULL
);
267 if (pthread_create(&sidechannel_thread_id
, NULL
, sidechannel_thread
, NULL
))
269 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
270 _cupsLangPrintFilter(stderr
, "ERROR",
271 _("There was an unrecoverable USB error."));
272 fputs("DEBUG: Couldn't create side-channel thread.\n", stderr
);
273 close_device(g
.printer
);
274 return (CUPS_BACKEND_STOP
);
279 * Debug mode: If option "usb-unidir" is given, always deactivate
283 num_opts
= cupsParseOptions(argv
[5], 0, &opts
);
284 val
= cupsGetOption("usb-unidir", num_opts
, opts
);
285 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
286 strcasecmp(val
, "false"))
288 g
.printer
->read_endp
= -1;
289 fprintf(stderr
, "DEBUG: Forced uni-directional communication "
290 "via \"usb-unidir\" option.\n");
294 * Debug mode: If option "usb-no-reattach" is given, do not re-attach
295 * the usblp kernel module after the job has completed.
298 val
= cupsGetOption("usb-no-reattach", num_opts
, opts
);
299 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
300 strcasecmp(val
, "false"))
302 g
.printer
->usblp_attached
= 0;
303 fprintf(stderr
, "DEBUG: Forced not re-attaching the usblp kernel module "
304 "after the job via \"usb-no-reattach\" option.\n");
308 * Get the read thread going...
311 if (g
.printer
->read_endp
!= -1)
313 have_backchannel
= 1;
315 g
.read_thread_stop
= 0;
316 g
.read_thread_done
= 0;
318 pthread_cond_init(&g
.read_thread_cond
, NULL
);
319 pthread_mutex_init(&g
.read_thread_mutex
, NULL
);
321 if (pthread_create(&read_thread_id
, NULL
, read_thread
, NULL
))
323 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
324 _cupsLangPrintFilter(stderr
, "ERROR",
325 _("There was an unrecoverable USB error."));
326 fputs("DEBUG: Couldn't create read thread.\n", stderr
);
327 close_device(g
.printer
);
328 return (CUPS_BACKEND_STOP
);
332 fprintf(stderr
, "DEBUG: Uni-directional device/mode, back channel "
336 * The main thread sends the print file...
342 print_ptr
= print_buffer
;
344 while (status
== CUPS_BACKEND_OK
&& copies
-- > 0)
346 _cupsLangPrintFilter(stderr
, "INFO", _("Sending data to printer."));
348 if (print_fd
!= STDIN_FILENO
)
350 fputs("PAGE: 1 1\n", stderr
);
351 lseek(print_fd
, 0, SEEK_SET
);
354 while (status
== CUPS_BACKEND_OK
)
359 FD_SET(print_fd
, &input_set
);
362 * Calculate select timeout...
363 * If we have data waiting to send timeout is 100ms.
364 * else if we're draining print_fd timeout is 0.
365 * else we're waiting forever...
371 tv
.tv_usec
= 100000; /* 100ms */
374 else if (g
.drain_output
)
384 * I/O is unlocked around select...
387 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
388 g
.readwrite_lock
= 0;
389 pthread_cond_signal(&g
.readwrite_lock_cond
);
390 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
392 nfds
= select(print_fd
+ 1, &input_set
, NULL
, NULL
, timeout
);
395 * Reacquire the lock...
398 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
399 while (g
.readwrite_lock
)
400 pthread_cond_wait(&g
.readwrite_lock_cond
, &g
.readwrite_lock_mutex
);
401 g
.readwrite_lock
= 1;
402 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
406 if (errno
== EINTR
&& total_bytes
== 0)
408 fputs("DEBUG: Received an interrupt before any bytes were "
409 "written, aborting.\n", stderr
);
410 close_device(g
.printer
);
411 return (CUPS_BACKEND_OK
);
413 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
415 _cupsLangPrintFilter(stderr
, "ERROR",
416 _("Unable to read print data."));
417 perror("DEBUG: select");
418 close_device(g
.printer
);
419 return (CUPS_BACKEND_FAILED
);
424 * If drain output has finished send a response...
427 if (g
.drain_output
&& !nfds
&& !g
.print_bytes
)
429 /* Send a response... */
430 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
435 * Check if we have print data ready...
438 if (FD_ISSET(print_fd
, &input_set
))
440 g
.print_bytes
= read(print_fd
, print_buffer
, sizeof(print_buffer
));
442 if (g
.print_bytes
< 0)
445 * Read error - bail if we don't see EAGAIN or EINTR...
448 if (errno
!= EAGAIN
&& errno
!= EINTR
)
450 _cupsLangPrintFilter(stderr
, "ERROR",
451 _("Unable to read print data."));
452 perror("DEBUG: read");
453 close_device(g
.printer
);
454 return (CUPS_BACKEND_FAILED
);
459 else if (g
.print_bytes
== 0)
462 * End of file, break out of the loop...
468 print_ptr
= print_buffer
;
470 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
476 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
477 g
.printer
->write_endp
,
478 print_buffer
, g
.print_bytes
,
481 * Ignore timeout errors, but retain the number of bytes written to
482 * avoid sending duplicate data...
485 if (iostatus
== LIBUSB_ERROR_TIMEOUT
)
487 fputs("DEBUG: Got USB transaction timeout during write.\n", stderr
);
492 * If we've stalled, retry the write...
495 else if (iostatus
== LIBUSB_ERROR_PIPE
)
497 fputs("DEBUG: Got USB pipe stalled during write.\n", stderr
);
499 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
500 g
.printer
->write_endp
,
501 print_buffer
, g
.print_bytes
,
506 * Retry a write after an aborted write since we probably just got
510 else if (iostatus
== LIBUSB_ERROR_INTERRUPTED
)
512 fputs("DEBUG: Got USB return aborted during write.\n", stderr
);
514 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
515 g
.printer
->write_endp
,
516 print_buffer
, g
.print_bytes
,
523 * Write error - bail if we don't see an error we can retry...
526 _cupsLangPrintFilter(stderr
, "ERROR",
527 _("Unable to send data to printer."));
528 fprintf(stderr
, "DEBUG: libusb write operation returned %x.\n",
531 status
= CUPS_BACKEND_FAILED
;
536 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n",
539 g
.print_bytes
-= bytes
;
541 total_bytes
+= bytes
;
545 if (print_fd
!= 0 && status
== CUPS_BACKEND_OK
)
546 fprintf(stderr
, "DEBUG: Sending print file, " CUPS_LLFMT
" bytes...\n",
547 CUPS_LLCAST total_bytes
);
551 fprintf(stderr
, "DEBUG: Sent " CUPS_LLFMT
" bytes...\n",
552 CUPS_LLCAST total_bytes
);
555 * Signal the side channel thread to exit...
558 if (have_sidechannel
)
561 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
562 g
.readwrite_lock
= 0;
563 pthread_cond_signal(&g
.readwrite_lock_cond
);
564 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
566 g
.sidechannel_thread_stop
= 1;
567 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
569 if (!g
.sidechannel_thread_done
)
571 gettimeofday(&tv
, NULL
);
572 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_SIDE_DELAY
;
573 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
575 while (!g
.sidechannel_thread_done
)
577 if (pthread_cond_timedwait(&g
.sidechannel_thread_cond
,
578 &g
.sidechannel_thread_mutex
,
584 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
588 * Signal the read thread to exit then wait 7 seconds for it to complete...
591 if (have_backchannel
)
593 g
.read_thread_stop
= 1;
595 pthread_mutex_lock(&g
.read_thread_mutex
);
597 if (!g
.read_thread_done
)
599 fputs("DEBUG: Waiting for read thread to exit...\n", stderr
);
601 gettimeofday(&tv
, NULL
);
602 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_EOF_DELAY
;
603 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
605 while (!g
.read_thread_done
)
607 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
613 * If it didn't exit abort the pending read and wait an additional
617 if (!g
.read_thread_done
)
619 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
624 gettimeofday(&tv
, NULL
);
625 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
626 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
628 while (!g
.read_thread_done
)
630 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
637 pthread_mutex_unlock(&g
.read_thread_mutex
);
641 * Close the connection and input file and general clean up...
644 if (g
.printer
->quirks
& USB_QUIRK_DELAY_CLOSE
)
647 close_device(g
.printer
);
653 libusb_free_device_list(all_list
, 1);
661 * 'close_device()' - Close the connection to the USB printer.
664 static int /* I - 0 on success, -1 on failure */
665 close_device(usb_printer_t
*printer
) /* I - Printer */
667 struct libusb_device_descriptor devdesc
;
668 /* Current device descriptor */
669 struct libusb_config_descriptor
*confptr
;
670 /* Pointer to current configuration */
676 * Release interfaces before closing so that we know all data is written
680 int errcode
; /* Return value of libusb function */
681 int number1
, /* Interface number */
682 number2
; /* Configuration number */
685 libusb_get_config_descriptor(printer
->device
, printer
->conf
, &confptr
);
688 number1
= confptr
->interface
[printer
->iface
].
689 altsetting
[printer
->altset
].bInterfaceNumber
;
690 libusb_release_interface(printer
->handle
, number1
);
692 number2
= confptr
->bConfigurationValue
;
694 libusb_free_config_descriptor(confptr
);
697 * If we have changed the configuration from one valid configuration
698 * to another, restore the old one
700 if (printer
->origconf
> 0 && printer
->origconf
!= number2
)
702 fprintf(stderr
, "DEBUG: Restoring USB device configuration: %d -> %d\n",
703 number2
, printer
->origconf
);
704 if ((errcode
= libusb_set_configuration(printer
->handle
,
705 printer
->origconf
)) < 0)
707 if (errcode
!= LIBUSB_ERROR_BUSY
)
710 libusb_get_device_descriptor (printer
->device
, &devdesc
);
713 "DEBUG: Failed to set configuration %d\n",
717 "DEBUG: Failed to set configuration %d for %04x:%04x\n",
718 printer
->origconf
, devdesc
.idVendor
, devdesc
.idProduct
);
724 * Re-attach "usblp" kernel module if it was attached before using this
727 if (printer
->usblp_attached
== 1)
728 if (libusb_attach_kernel_driver(printer
->handle
, number1
) < 0)
730 errcode
= libusb_get_device_descriptor (printer
->device
, &devdesc
);
733 "DEBUG: Failed to re-attach \"usblp\" kernel module\n");
736 "DEBUG: Failed to re-attach \"usblp\" kernel module to "
737 "%04x:%04x\n", devdesc
.idVendor
, devdesc
.idProduct
);
742 "DEBUG: Failed to get configuration descriptor %d\n",
746 * Reset the device to clean up after the job
749 if (printer
->reset_after_job
== 1)
751 if ((errcode
= libusb_reset_device(printer
->handle
)) < 0)
753 "DEBUG: Device reset failed, error code: %d\n",
757 "DEBUG: Resetting printer.\n");
761 * Close the interface and return...
764 libusb_close(printer
->handle
);
765 printer
->handle
= NULL
;
773 * 'compare_quirks()' - Compare two quirks entries.
776 static int /* O - Result of comparison */
777 compare_quirks(usb_quirk_t
*a
, /* I - First quirk entry */
778 usb_quirk_t
*b
) /* I - Second quirk entry */
780 int result
; /* Result of comparison */
782 if ((result
= b
->vendor_id
- a
->vendor_id
) == 0)
783 result
= b
->product_id
- a
->product_id
;
790 * 'find_device()' - Find or enumerate USB printers.
793 static usb_printer_t
* /* O - Found printer */
794 find_device(usb_cb_t cb
, /* I - Callback function */
795 const void *data
) /* I - User data for callback */
797 libusb_device
**list
; /* List of connected USB devices */
798 libusb_device
*device
= NULL
; /* Current device */
799 struct libusb_device_descriptor devdesc
;
800 /* Current device descriptor */
801 struct libusb_config_descriptor
*confptr
= NULL
;
802 /* Pointer to current configuration */
803 const struct libusb_interface
*ifaceptr
= NULL
;
804 /* Pointer to current interface */
805 const struct libusb_interface_descriptor
*altptr
= NULL
;
806 /* Pointer to current alternate setting */
807 const struct libusb_endpoint_descriptor
*endpptr
= NULL
;
808 /* Pointer to current endpoint */
809 ssize_t err
= 0, /* Error code */
810 numdevs
, /* number of connected devices */
812 uint8_t conf
, /* Current configuration */
813 iface
, /* Current interface */
814 altset
, /* Current alternate setting */
815 protocol
, /* Current protocol */
816 endp
, /* Current endpoint */
817 read_endp
, /* Current read endpoint */
818 write_endp
; /* Current write endpoint */
819 char device_id
[1024],/* IEEE-1284 device ID */
822 static usb_printer_t printer
; /* Current printer */
826 * Initialize libusb...
829 err
= libusb_init(NULL
);
832 fprintf(stderr
, "DEBUG: Unable to initialize USB access via libusb, "
833 "libusb error %i\n", (int)err
);
837 numdevs
= libusb_get_device_list(NULL
, &list
);
838 fprintf(stderr
, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs
);
841 * Then loop through the devices it found...
845 for (i
= 0; i
< numdevs
; i
++)
850 * Ignore devices with no configuration data and anything that is not
854 if (libusb_get_device_descriptor(device
, &devdesc
) < 0)
857 if (!devdesc
.bNumConfigurations
|| !devdesc
.idVendor
||
861 printer
.quirks
= find_quirks(devdesc
.idVendor
, devdesc
.idProduct
);
864 * Ignore blacklisted printers...
867 if (printer
.quirks
& USB_QUIRK_BLACKLIST
)
870 for (conf
= 0; conf
< devdesc
.bNumConfigurations
; conf
++)
872 if (libusb_get_config_descriptor(device
, conf
, &confptr
) < 0)
874 for (iface
= 0, ifaceptr
= confptr
->interface
;
875 iface
< confptr
->bNumInterfaces
;
876 iface
++, ifaceptr
++)
879 * Some printers offer multiple interfaces...
884 for (altset
= 0, altptr
= ifaceptr
->altsetting
;
885 altset
< ifaceptr
->num_altsetting
;
886 altset
++, altptr
++)
889 * Currently we only support unidirectional and bidirectional
890 * printers. Future versions of this code will support the
891 * 1284.4 (packet mode) protocol as well.
894 if (((altptr
->bInterfaceClass
!= LIBUSB_CLASS_PRINTER
||
895 altptr
->bInterfaceSubClass
!= 1) &&
896 ((printer
.quirks
& USB_QUIRK_VENDOR_CLASS
) == 0)) ||
897 (altptr
->bInterfaceProtocol
!= 1 && /* Unidirectional */
898 altptr
->bInterfaceProtocol
!= 2) || /* Bidirectional */
899 altptr
->bInterfaceProtocol
< protocol
)
902 if (printer
.quirks
& USB_QUIRK_VENDOR_CLASS
)
903 fprintf(stderr
, "DEBUG: Printer does not report class 7 and/or "
904 "subclass 1 but works as a printer anyway\n");
909 for (endp
= 0, endpptr
= altptr
->endpoint
;
910 endp
< altptr
->bNumEndpoints
;
912 if ((endpptr
->bmAttributes
& LIBUSB_TRANSFER_TYPE_MASK
) ==
913 LIBUSB_TRANSFER_TYPE_BULK
)
915 if (endpptr
->bEndpointAddress
& LIBUSB_ENDPOINT_DIR_MASK
)
921 if (write_endp
!= 0xff)
924 * Save the best match so far...
927 protocol
= altptr
->bInterfaceProtocol
;
928 printer
.altset
= altset
;
929 printer
.write_endp
= write_endp
;
931 printer
.read_endp
= read_endp
;
933 printer
.read_endp
= -1;
939 printer
.device
= device
;
941 printer
.iface
= iface
;
942 printer
.protocol
= protocol
;
943 printer
.handle
= NULL
;
945 if (!open_device(&printer
, data
!= NULL
))
947 get_device_id(&printer
, device_id
, sizeof(device_id
));
948 make_device_uri(&printer
, device_id
, device_uri
,
951 fprintf(stderr
, "DEBUG2: Printer found with device ID: %s "
953 device_id
, device_uri
);
955 if ((*cb
)(&printer
, device_uri
, device_id
, data
))
957 fprintf(stderr
, "DEBUG: Device protocol: %d\n",
959 if (printer
.quirks
& USB_QUIRK_UNIDIR
)
961 printer
.read_endp
= -1;
962 fprintf(stderr
, "DEBUG: Printer reports bi-di support "
963 "but in reality works only uni-directionally\n");
965 if (printer
.read_endp
!= -1)
967 printer
.read_endp
= confptr
->interface
[printer
.iface
].
968 altsetting
[printer
.altset
].
969 endpoint
[printer
.read_endp
].
973 fprintf(stderr
, "DEBUG: Uni-directional USB communication "
975 printer
.write_endp
= confptr
->interface
[printer
.iface
].
976 altsetting
[printer
.altset
].
977 endpoint
[printer
.write_endp
].
979 if (printer
.quirks
& USB_QUIRK_NO_REATTACH
)
981 printer
.usblp_attached
= 0;
982 fprintf(stderr
, "DEBUG: Printer does not like usblp "
983 "kernel module to be re-attached after job\n");
985 libusb_free_config_descriptor(confptr
);
989 close_device(&printer
);
993 libusb_free_config_descriptor(confptr
);
998 * If we get this far without returning, then we haven't found a printer
1007 libusb_free_device_list(list
, 1);
1015 * 'find_quirks()' - Find the quirks for the given printer, if any.
1017 * First looks for an exact match, then looks for the vendor ID wildcard match.
1020 static unsigned /* O - Quirks flags */
1021 find_quirks(int vendor_id
, /* I - Vendor ID */
1022 int product_id
) /* I - Product ID */
1024 usb_quirk_t key
, /* Search key */
1025 *match
; /* Matching quirk entry */
1028 key
.vendor_id
= vendor_id
;
1029 key
.product_id
= product_id
;
1031 if ((match
= cupsArrayFind(all_quirks
, &key
)) != NULL
)
1032 return (match
->quirks
);
1036 if ((match
= cupsArrayFind(all_quirks
, &key
)) != NULL
)
1037 return (match
->quirks
);
1039 return (USB_QUIRK_WHITELIST
);
1044 * 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
1047 static int /* O - 0 on success, -1 on error */
1048 get_device_id(usb_printer_t
*printer
, /* I - Printer */
1049 char *buffer
, /* I - String buffer */
1050 size_t bufsize
) /* I - Number of bytes in buffer */
1052 int length
; /* Length of device ID */
1055 if (libusb_control_transfer(printer
->handle
,
1056 LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_ENDPOINT_IN
|
1057 LIBUSB_RECIPIENT_INTERFACE
,
1059 (printer
->iface
<< 8) | printer
->altset
,
1060 (unsigned char *)buffer
, bufsize
, 5000) < 0)
1067 * Extract the length of the device ID string from the first two
1068 * bytes. The 1284 spec says the length is stored MSB first...
1071 length
= (int)((((unsigned)buffer
[0] & 255) << 8) | ((unsigned)buffer
[1] & 255));
1074 * Check to see if the length is larger than our buffer or less than 14 bytes
1075 * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
1077 * If the length is out-of-range, assume that the vendor incorrectly
1078 * implemented the 1284 spec and re-read the length as LSB first,..
1081 if (length
> bufsize
|| length
< 14)
1082 length
= (int)((((unsigned)buffer
[1] & 255) << 8) | ((unsigned)buffer
[0] & 255));
1084 if (length
> bufsize
)
1090 * Invalid device ID, clear it!
1100 * Copy the device ID text to the beginning of the buffer and
1104 memmove(buffer
, buffer
+ 2, (size_t)length
);
1105 buffer
[length
] = '\0';
1112 * 'list_cb()' - List USB printers for discovery.
1115 static int /* O - 0 to continue, 1 to stop */
1116 list_cb(usb_printer_t
*printer
, /* I - Printer */
1117 const char *device_uri
, /* I - Device URI */
1118 const char *device_id
, /* I - IEEE-1284 device ID */
1119 const void *data
) /* I - User data (not used) */
1121 char make_model
[1024]; /* Make and model */
1125 * Get the device URI and make/model strings...
1128 if (backendGetMakeModel(device_id
, make_model
, sizeof(make_model
)))
1129 strlcpy(make_model
, "Unknown", sizeof(make_model
));
1132 * Report the printer...
1135 cupsBackendReport("direct", device_uri
, make_model
, make_model
, device_id
,
1147 * 'load_quirks()' - Load all quirks files in the /usr/share/cups/usb directory.
1153 const char *datadir
; /* CUPS_DATADIR environment variable */
1154 char filename
[1024], /* Filename */
1155 line
[1024]; /* Line from file */
1156 cups_dir_t
*dir
; /* Directory */
1157 cups_dentry_t
*dent
; /* Directory entry */
1158 cups_file_t
*fp
; /* Quirks file */
1159 usb_quirk_t
*quirk
; /* New quirk */
1162 all_quirks
= cupsArrayNew((cups_array_func_t
)compare_quirks
, NULL
);
1164 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
1165 datadir
= CUPS_DATADIR
;
1167 snprintf(filename
, sizeof(filename
), "%s/usb", datadir
);
1168 if ((dir
= cupsDirOpen(filename
)) == NULL
)
1174 fprintf(stderr
, "DEBUG: Loading USB quirks from \"%s\".\n", filename
);
1176 while ((dent
= cupsDirRead(dir
)) != NULL
)
1178 if (!S_ISREG(dent
->fileinfo
.st_mode
))
1181 snprintf(filename
, sizeof(filename
), "%s/usb/%s", datadir
, dent
->filename
);
1182 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1188 while (cupsFileGets(fp
, line
, sizeof(line
)))
1191 * Skip blank and comment lines...
1194 if (line
[0] == '#' || !line
[0])
1201 if ((quirk
= calloc(1, sizeof(usb_quirk_t
))) == NULL
)
1203 perror("DEBUG: Unable to allocate memory for quirk");
1207 if (sscanf(line
, "%x%x", &quirk
->vendor_id
, &quirk
->product_id
) < 1)
1209 fprintf(stderr
, "DEBUG: Bad line: %s\n", line
);
1214 if (strstr(line
, " blacklist"))
1215 quirk
->quirks
|= USB_QUIRK_BLACKLIST
;
1217 if (strstr(line
, " delay-close"))
1218 quirk
->quirks
|= USB_QUIRK_DELAY_CLOSE
;
1220 if (strstr(line
, " no-reattach"))
1221 quirk
->quirks
|= USB_QUIRK_NO_REATTACH
;
1223 if (strstr(line
, " soft-reset"))
1224 quirk
->quirks
|= USB_QUIRK_SOFT_RESET
;
1226 if (strstr(line
, " unidir"))
1227 quirk
->quirks
|= USB_QUIRK_UNIDIR
;
1229 if (strstr(line
, " usb-init"))
1230 quirk
->quirks
|= USB_QUIRK_USB_INIT
;
1232 if (strstr(line
, " vendor-class"))
1233 quirk
->quirks
|= USB_QUIRK_VENDOR_CLASS
;
1235 cupsArrayAdd(all_quirks
, quirk
);
1241 fprintf(stderr
, "DEBUG: Loaded %d quirks.\n", cupsArrayCount(all_quirks
));
1248 * 'make_device_uri()' - Create a device URI for a USB printer.
1251 static char * /* O - Device URI */
1253 usb_printer_t
*printer
, /* I - Printer */
1254 const char *device_id
, /* I - IEEE-1284 device ID */
1255 char *uri
, /* I - Device URI buffer */
1256 size_t uri_size
) /* I - Size of device URI buffer */
1258 struct libusb_device_descriptor devdesc
;
1259 /* Current device descriptor */
1260 char options
[1024]; /* Device URI options */
1261 int num_values
; /* Number of 1284 parameters */
1262 cups_option_t
*values
; /* 1284 parameters */
1263 const char *mfg
, /* Manufacturer */
1265 *des
= NULL
, /* Description */
1266 *sern
; /* Serial number */
1267 size_t mfglen
; /* Length of manufacturer string */
1268 char tempmfg
[256], /* Temporary manufacturer string */
1269 tempsern
[256], /* Temporary serial number string */
1270 *tempptr
; /* Pointer into temp string */
1274 * Get the make, model, and serial numbers...
1277 num_values
= _cupsGet1284Values(device_id
, &values
);
1279 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
1280 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
1281 if ((sern
= cupsGetOption("SN", num_values
, values
)) == NULL
&&
1282 ((libusb_get_device_descriptor(printer
->device
, &devdesc
) >= 0) &&
1283 devdesc
.iSerialNumber
))
1286 * Try getting the serial number from the device itself...
1290 libusb_get_string_descriptor_ascii(printer
->handle
,
1291 devdesc
.iSerialNumber
,
1292 (unsigned char *)tempsern
,
1293 sizeof(tempsern
) - 1);
1296 tempsern
[length
] = '\0';
1301 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
1302 mfg
= cupsGetOption("MFG", num_values
, values
);
1304 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
1305 mdl
= cupsGetOption("MDL", num_values
, values
);
1308 * To maintain compatibility with the original character device backend on
1309 * Linux and *BSD, map manufacturer names...
1314 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
1316 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
1322 * No manufacturer? Use the model string or description...
1326 _ppdNormalizeMakeAndModel(mdl
, tempmfg
, sizeof(tempmfg
));
1327 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
1328 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
1329 _ppdNormalizeMakeAndModel(des
, tempmfg
, sizeof(tempmfg
));
1331 strlcpy(tempmfg
, "Unknown", sizeof(tempmfg
));
1333 if ((tempptr
= strchr(tempmfg
, ' ')) != NULL
)
1342 * No model? Use description...
1345 mdl
= des
; /* We remove the manufacturer name below */
1346 else if (!strncasecmp(mfg
, "Unknown", 7))
1349 mdl
= "Unknown Model";
1352 mfglen
= strlen(mfg
);
1354 if (!strncasecmp(mdl
, mfg
, mfglen
) && _cups_isspace(mdl
[mfglen
]))
1358 while (_cups_isspace(*mdl
))
1363 * Generate the device URI from the manufacturer, model, serial number,
1364 * and interface number...
1369 if (printer
->iface
> 0)
1370 snprintf(options
, sizeof(options
), "?serial=%s&interface=%d", sern
,
1373 snprintf(options
, sizeof(options
), "?serial=%s", sern
);
1375 else if (printer
->iface
> 0)
1376 snprintf(options
, sizeof(options
), "?interface=%d", printer
->iface
);
1380 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, "usb", NULL
, mfg
, 0,
1381 "/%s%s", mdl
, options
);
1383 cupsFreeOptions(num_values
, values
);
1390 * 'open_device()' - Open a connection to the USB printer.
1393 static int /* O - 0 on success, -1 on error */
1394 open_device(usb_printer_t
*printer
, /* I - Printer */
1395 int verbose
) /* I - Update connecting-to-device state? */
1397 struct libusb_device_descriptor devdesc
;
1398 /* Current device descriptor */
1399 struct libusb_config_descriptor
*confptr
= NULL
;
1400 /* Pointer to current configuration */
1401 int number1
= -1, /* Configuration/interface/altset */
1402 number2
= -1, /* numbers */
1404 char current
; /* Current configuration */
1408 * Return immediately if we are already connected...
1411 if (printer
->handle
)
1415 * Try opening the printer...
1418 if ((errcode
= libusb_open(printer
->device
, &printer
->handle
)) < 0)
1420 fprintf(stderr
, "DEBUG: Failed to open device, code: %d\n",
1425 printer
->usblp_attached
= 0;
1426 printer
->reset_after_job
= 0;
1429 fputs("STATE: +connecting-to-device\n", stderr
);
1431 if ((errcode
= libusb_get_device_descriptor(printer
->device
, &devdesc
)) < 0)
1433 fprintf(stderr
, "DEBUG: Failed to get device descriptor, code: %d\n",
1439 * Get the "usblp" kernel module out of the way. This backend only
1440 * works without the module attached.
1443 errcode
= libusb_kernel_driver_active(printer
->handle
, printer
->iface
);
1445 printer
->usblp_attached
= 0;
1446 else if (errcode
== 1)
1448 printer
->usblp_attached
= 1;
1450 libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1452 fprintf(stderr
, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1453 devdesc
.idVendor
, devdesc
.idProduct
);
1459 printer
->usblp_attached
= 0;
1461 if (errcode
!= LIBUSB_ERROR_NOT_SUPPORTED
)
1464 "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" "
1465 "kernel module attached\n", devdesc
.idVendor
, devdesc
.idProduct
);
1471 * Set the desired configuration, but only if it needs changing. Some
1472 * printers (e.g., Samsung) don't like libusb_set_configuration. It will
1473 * succeed, but the following print job is sometimes silently lost by the
1477 if (libusb_control_transfer(printer
->handle
,
1478 LIBUSB_REQUEST_TYPE_STANDARD
| LIBUSB_ENDPOINT_IN
|
1479 LIBUSB_RECIPIENT_DEVICE
,
1480 8, /* GET_CONFIGURATION */
1481 0, 0, (unsigned char *)¤t
, 1, 5000) < 0)
1482 current
= 0; /* Assume not configured */
1484 printer
->origconf
= current
;
1487 libusb_get_config_descriptor (printer
->device
, printer
->conf
, &confptr
))
1490 fprintf(stderr
, "DEBUG: Failed to get config descriptor for %04x:%04x\n",
1491 devdesc
.idVendor
, devdesc
.idProduct
);
1494 number1
= confptr
->bConfigurationValue
;
1496 if (number1
!= current
)
1498 fprintf(stderr
, "DEBUG: Switching USB device configuration: %d -> %d\n",
1500 if ((errcode
= libusb_set_configuration(printer
->handle
, number1
)) < 0)
1503 * If the set fails, chances are that the printer only supports a
1504 * single configuration. Technically these printers don't conform to
1505 * the USB printer specification, but otherwise they'll work...
1508 if (errcode
!= LIBUSB_ERROR_BUSY
)
1509 fprintf(stderr
, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
1510 number1
, devdesc
.idVendor
, devdesc
.idProduct
);
1515 * Claim interfaces as needed...
1518 number1
= confptr
->interface
[printer
->iface
].
1519 altsetting
[printer
->altset
].bInterfaceNumber
;
1521 while ((errcode
= libusb_claim_interface(printer
->handle
, number1
)) < 0)
1523 if (errcode
!= LIBUSB_ERROR_BUSY
)
1526 "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
1527 number1
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1531 else if ((errcode
= libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1534 "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1535 devdesc
.idVendor
, devdesc
.idProduct
);
1544 * Set alternate setting, but only if there is more than one option. Some
1545 * printers (e.g., Samsung) don't like usb_set_altinterface.
1548 if (confptr
->interface
[printer
->iface
].num_altsetting
> 1)
1550 number1
= confptr
->interface
[printer
->iface
].
1551 altsetting
[printer
->altset
].bInterfaceNumber
;
1552 number2
= confptr
->interface
[printer
->iface
].
1553 altsetting
[printer
->altset
].bAlternateSetting
;
1556 libusb_set_interface_alt_setting(printer
->handle
, number1
, number2
))
1559 if (errcode
!= LIBUSB_ERROR_BUSY
)
1562 "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
1564 number2
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1571 libusb_free_config_descriptor(confptr
);
1574 fputs("STATE: -connecting-to-device\n", stderr
);
1579 * If we get here, there was a hard error...
1585 fputs("STATE: -connecting-to-device\n", stderr
);
1587 libusb_close(printer
->handle
);
1588 printer
->handle
= NULL
;
1595 * 'print_cb()' - Find a USB printer for printing.
1598 static int /* O - 0 to continue, 1 to stop (found) */
1599 print_cb(usb_printer_t
*printer
, /* I - Printer */
1600 const char *device_uri
, /* I - Device URI */
1601 const char *device_id
, /* I - IEEE-1284 device ID */
1602 const void *data
) /* I - User data (make, model, S/N) */
1604 char requested_uri
[1024], /* Requested URI */
1605 *requested_ptr
, /* Pointer into requested URI */
1606 detected_uri
[1024], /* Detected URI */
1607 *detected_ptr
; /* Pointer into detected URI */
1611 * If we have an exact match, stop now...
1614 if (!strcmp((char *)data
, device_uri
))
1618 * Work on copies of the URIs...
1621 strlcpy(requested_uri
, (char *)data
, sizeof(requested_uri
));
1622 strlcpy(detected_uri
, device_uri
, sizeof(detected_uri
));
1625 * libusb-discovered URIs can have an "interface" specification and this
1626 * never happens for usblp-discovered URIs, so remove the "interface"
1627 * specification from the URI which we are checking currently. This way a
1628 * queue for a usblp-discovered printer can now be accessed via libusb.
1630 * Similarly, strip "?serial=NNN...NNN" as needed.
1633 if ((requested_ptr
= strstr(requested_uri
, "?interface=")) == NULL
)
1634 requested_ptr
= strstr(requested_uri
, "&interface=");
1635 if ((detected_ptr
= strstr(detected_uri
, "?interface=")) == NULL
)
1636 detected_ptr
= strstr(detected_uri
, "&interface=");
1638 if (!requested_ptr
&& detected_ptr
)
1641 * Strip "[?&]interface=nnn" from the detected printer.
1644 *detected_ptr
= '\0';
1646 else if (requested_ptr
&& !detected_ptr
)
1649 * Strip "[?&]interface=nnn" from the requested printer.
1652 *requested_ptr
= '\0';
1655 if ((requested_ptr
= strstr(requested_uri
, "?serial=?")) != NULL
)
1658 * Strip "?serial=?" from the requested printer. This is a special
1659 * case, as "?serial=?" means no serial number and not the serial
1660 * number '?'. This is not covered by the checks below...
1663 *requested_ptr
= '\0';
1666 if ((requested_ptr
= strstr(requested_uri
, "?serial=")) == NULL
&&
1667 (detected_ptr
= strstr(detected_uri
, "?serial=")) != NULL
)
1670 * Strip "?serial=nnn" from the detected printer.
1673 *detected_ptr
= '\0';
1675 else if (requested_ptr
&& !detected_ptr
)
1678 * Strip "?serial=nnn" from the requested printer.
1681 *requested_ptr
= '\0';
1684 return (!strcmp(requested_uri
, detected_uri
));
1689 * 'read_thread()' - Thread to read the backchannel data on.
1692 static void *read_thread(void *reference
)
1694 unsigned char readbuffer
[512];
1706 * Read frequency: once every 250 milliseconds.
1710 delay
.tv_usec
= 250000;
1715 * Remember when we started so we can throttle the loop after the read
1719 gettimeofday(&now
, NULL
);
1722 * Calculate what 250 milliSeconds are in absolute time...
1725 timeradd(&now
, &delay
, &end
);
1727 rbytes
= sizeof(readbuffer
);
1728 readstatus
= libusb_bulk_transfer(g
.printer
->handle
,
1729 g
.printer
->read_endp
,
1732 if (readstatus
== LIBUSB_SUCCESS
&& rbytes
> 0)
1734 fprintf(stderr
, "DEBUG: Read %d bytes of back-channel data...\n",
1736 cupsBackChannelWrite((const char *)readbuffer
, (size_t)rbytes
, 1.0);
1738 else if (readstatus
== LIBUSB_ERROR_TIMEOUT
)
1739 fputs("DEBUG: Got USB transaction timeout during read.\n", stderr
);
1740 else if (readstatus
== LIBUSB_ERROR_PIPE
)
1741 fputs("DEBUG: Got USB pipe stalled during read.\n", stderr
);
1742 else if (readstatus
== LIBUSB_ERROR_INTERRUPTED
)
1743 fputs("DEBUG: Got USB return aborted during read.\n", stderr
);
1746 * Make sure this loop executes no more than once every 250 miliseconds...
1749 if ((readstatus
!= LIBUSB_SUCCESS
|| rbytes
== 0) &&
1750 (g
.wait_eof
|| !g
.read_thread_stop
))
1752 gettimeofday(&now
, NULL
);
1753 if (timercmp(&now
, &end
, <))
1755 timersub(&end
, &now
, &timeleft
);
1756 usleep(1000000 * timeleft
.tv_sec
+ timeleft
.tv_usec
);
1759 } while (g
.wait_eof
|| !g
.read_thread_stop
);
1762 * Let the main thread know that we have completed the read thread...
1765 pthread_mutex_lock(&g
.read_thread_mutex
);
1766 g
.read_thread_done
= 1;
1767 pthread_cond_signal(&g
.read_thread_cond
);
1768 pthread_mutex_unlock(&g
.read_thread_mutex
);
1775 * 'sidechannel_thread()' - Handle side-channel requests.
1779 sidechannel_thread(void *reference
)
1781 cups_sc_command_t command
; /* Request command */
1782 cups_sc_status_t status
; /* Request/response status */
1783 char data
[2048]; /* Request/response data */
1784 int datalen
; /* Request/response data size */
1791 datalen
= sizeof(data
);
1793 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
1795 if (status
== CUPS_SC_STATUS_TIMEOUT
)
1803 case CUPS_SC_CMD_SOFT_RESET
: /* Do a soft reset */
1804 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1808 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
1809 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1813 case CUPS_SC_CMD_DRAIN_OUTPUT
: /* Drain all pending output */
1814 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1820 case CUPS_SC_CMD_GET_BIDI
: /* Is the connection bidirectional? */
1821 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1824 data
[0] = (g
.printer
->protocol
>= 2 ? 1 : 0);
1825 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1828 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1832 case CUPS_SC_CMD_GET_DEVICE_ID
: /* Return IEEE-1284 device ID */
1833 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1836 datalen
= sizeof(data
);
1837 if (get_device_id(g
.printer
, data
, sizeof(data
)))
1839 status
= CUPS_SC_STATUS_IO_ERROR
;
1844 status
= CUPS_SC_STATUS_OK
;
1845 datalen
= strlen(data
);
1847 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, datalen
, 1.0);
1849 if (datalen
< sizeof(data
))
1850 data
[datalen
] = '\0';
1852 data
[sizeof(data
) - 1] = '\0';
1855 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1859 case CUPS_SC_CMD_GET_STATE
: /* Return device state */
1860 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1863 data
[0] = CUPS_SC_STATE_ONLINE
;
1864 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1867 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1871 case CUPS_SC_CMD_GET_CONNECTED
: /* Return whether device is
1873 fputs("DEBUG: CUPS_SC_CMD_GET_CONNECTED received from driver...\n",
1876 data
[0] = (g
.printer
->handle
? 1 : 0);
1877 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1880 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1885 fprintf(stderr
, "DEBUG: Unknown side-channel command (%d) received "
1886 "from driver...\n", command
);
1888 cupsSideChannelWrite(command
, CUPS_SC_STATUS_NOT_IMPLEMENTED
,
1891 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1896 while (!g
.sidechannel_thread_stop
);
1898 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
1899 g
.sidechannel_thread_done
= 1;
1900 pthread_cond_signal(&g
.sidechannel_thread_cond
);
1901 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
1908 * 'soft_reset()' - Send a soft reset to the device.
1914 fd_set input_set
; /* Input set for select() */
1915 struct timeval tv
; /* Time value */
1916 char buffer
[2048]; /* Buffer */
1917 struct timespec cond_timeout
; /* pthread condition timeout */
1921 * Send an abort once a second until the I/O lock is released by the main
1925 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1926 while (g
.readwrite_lock
)
1928 gettimeofday(&tv
, NULL
);
1929 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
1930 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
1932 while (g
.readwrite_lock
)
1934 if (pthread_cond_timedwait(&g
.readwrite_lock_cond
,
1935 &g
.readwrite_lock_mutex
,
1936 &cond_timeout
) != 0)
1941 g
.readwrite_lock
= 1;
1942 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1945 * Flush bytes waiting on print_fd...
1950 FD_ZERO(&input_set
);
1951 FD_SET(g
.print_fd
, &input_set
);
1956 while (select(g
.print_fd
+1, &input_set
, NULL
, NULL
, &tv
) > 0)
1957 if (read(g
.print_fd
, buffer
, sizeof(buffer
)) <= 0)
1964 soft_reset_printer(g
.printer
);
1967 * Release the I/O lock...
1970 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1971 g
.readwrite_lock
= 0;
1972 pthread_cond_signal(&g
.readwrite_lock_cond
);
1973 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1978 * 'soft_reset_printer()' - Do the soft reset request specific to printers
1980 * This soft reset is specific to the printer device class and is much less
1981 * invasive than the general USB reset libusb_reset_device(). Especially it
1982 * does never happen that the USB addressing and configuration changes. What
1983 * is actually done is that all buffers get flushed and the bulk IN and OUT
1984 * pipes get reset to their default states. This clears all stall conditions.
1985 * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
1988 static int /* O - 0 on success, < 0 on error */
1990 usb_printer_t
*printer
) /* I - Printer */
1992 struct libusb_config_descriptor
*confptr
= NULL
;
1993 /* Pointer to current configuration */
1994 int interface
, /* Interface to reset */
1995 errcode
; /* Error code */
1998 if (libusb_get_config_descriptor(printer
->device
, printer
->conf
,
2000 interface
= printer
->iface
;
2002 interface
= confptr
->interface
[printer
->iface
].
2003 altsetting
[printer
->altset
].bInterfaceNumber
;
2005 libusb_free_config_descriptor(confptr
);
2007 if ((errcode
= libusb_control_transfer(printer
->handle
,
2008 LIBUSB_REQUEST_TYPE_CLASS
|
2009 LIBUSB_ENDPOINT_OUT
|
2010 LIBUSB_RECIPIENT_OTHER
,
2011 2, 0, interface
, NULL
, 0, 5000)) < 0)
2012 errcode
= libusb_control_transfer(printer
->handle
,
2013 LIBUSB_REQUEST_TYPE_CLASS
|
2014 LIBUSB_ENDPOINT_OUT
|
2015 LIBUSB_RECIPIENT_INTERFACE
,
2016 2, 0, interface
, NULL
, 0, 5000);