4 * LIBUSB interface code for CUPS.
6 * Copyright 2007-2015 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 * Include necessary headers...
20 #include <cups/cups-private.h>
21 #include <cups/ppd-private.h>
24 #include <sys/select.h>
25 #include <sys/types.h>
32 * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
33 * the printer after we've finished sending all the data
37 #define WAIT_EOF_DELAY 7
38 #define WAIT_SIDE_DELAY 3
39 #define DEFAULT_TIMEOUT 5000L
46 typedef struct usb_printer_s
/**** USB Printer Data ****/
48 struct libusb_device
*device
; /* Device info */
49 int conf
, /* Configuration */
50 origconf
, /* Original configuration */
51 iface
, /* Interface */
52 altset
, /* Alternate setting */
53 write_endp
, /* Write endpoint */
54 read_endp
, /* Read endpoint */
55 protocol
, /* Protocol: 1 = Uni-di, 2 = Bi-di. */
56 usblp_attached
, /* "usblp" kernel module attached? */
57 reset_after_job
;/* Set to 1 by print_device() */
58 unsigned quirks
; /* Quirks flags */
59 struct libusb_device_handle
*handle
; /* Open handle to device */
62 typedef int (*usb_cb_t
)(usb_printer_t
*, const char *, const char *,
65 typedef struct usb_globals_s
/* Global USB printer information */
67 usb_printer_t
*printer
; /* Printer */
69 pthread_mutex_t read_thread_mutex
;
70 pthread_cond_t read_thread_cond
;
74 pthread_mutex_t readwrite_lock_mutex
;
75 pthread_cond_t readwrite_lock_cond
;
78 int print_fd
; /* File descriptor to print */
79 ssize_t print_bytes
; /* Print bytes read */
82 int drain_output
; /* Drain all pending output */
83 int bidi_flag
; /* 0=unidirectional, 1=bidirectional */
85 pthread_mutex_t sidechannel_thread_mutex
;
86 pthread_cond_t sidechannel_thread_cond
;
87 int sidechannel_thread_stop
;
88 int sidechannel_thread_done
;
92 * Quirks: various printer quirks are handled by this structure and its flags.
94 * The quirks table used to be compiled into the backend but is now loaded from
95 * one or more files in the /usr/share/cups/usb directory.
98 #define USB_QUIRK_BLACKLIST 0x0001 /* Does not conform to the spec */
99 #define USB_QUIRK_NO_REATTACH 0x0002 /* After printing we cannot re-attach
100 the usblp kernel module */
101 #define USB_QUIRK_SOFT_RESET 0x0004 /* After printing do a soft reset
103 #define USB_QUIRK_UNIDIR 0x0008 /* Requires unidirectional mode */
104 #define USB_QUIRK_USB_INIT 0x0010 /* Needs vendor USB init string */
105 #define USB_QUIRK_VENDOR_CLASS 0x0020 /* Descriptor uses vendor-specific
107 #define USB_QUIRK_DELAY_CLOSE 0x0040 /* Delay close */
108 #define USB_QUIRK_WHITELIST 0x0000 /* no quirks */
111 typedef struct usb_quirk_s
/* USB "quirk" information */
113 int vendor_id
, /* Affected vendor ID */
114 product_id
; /* Affected product ID or 0 for all */
115 unsigned quirks
; /* Quirks bitfield */
125 cups_array_t
*all_quirks
; /* Array of printer quirks */
126 usb_globals_t g
= { 0 }; /* Globals */
127 libusb_device
**all_list
; /* List of connected USB devices */
134 static int close_device(usb_printer_t
*printer
);
135 static int compare_quirks(usb_quirk_t
*a
, usb_quirk_t
*b
);
136 static usb_printer_t
*find_device(usb_cb_t cb
, const void *data
);
137 static unsigned find_quirks(int vendor_id
, int product_id
);
138 static int get_device_id(usb_printer_t
*printer
, char *buffer
,
140 static int list_cb(usb_printer_t
*printer
, const char *device_uri
,
141 const char *device_id
, const void *data
);
142 static void load_quirks(void);
143 static char *make_device_uri(usb_printer_t
*printer
,
144 const char *device_id
,
145 char *uri
, size_t uri_size
);
146 static int open_device(usb_printer_t
*printer
, int verbose
);
147 static int print_cb(usb_printer_t
*printer
, const char *device_uri
,
148 const char *device_id
, const void *data
);
149 static void *read_thread(void *reference
);
150 static void *sidechannel_thread(void *reference
);
151 static void soft_reset(void);
152 static int soft_reset_printer(usb_printer_t
*printer
);
156 * 'list_devices()' - List the available printers.
164 fputs("DEBUG: list_devices\n", stderr
);
165 find_device(list_cb
, NULL
);
170 * 'print_device()' - Print a file to a USB device.
173 int /* O - Exit status */
174 print_device(const char *uri
, /* I - Device URI */
175 const char *hostname
, /* I - Hostname/manufacturer */
176 const char *resource
, /* I - Resource/modelname */
177 char *options
, /* I - Device options/serial number */
178 int print_fd
, /* I - File descriptor to print */
179 int copies
, /* I - Copies to print */
180 int argc
, /* I - Number of command-line arguments (6 or 7) */
181 char *argv
[]) /* I - Command-line arguments */
183 int bytes
; /* Bytes written */
184 ssize_t total_bytes
; /* Total bytes written */
185 struct sigaction action
; /* Actions for POSIX signals */
186 int status
= CUPS_BACKEND_OK
,
187 /* Function results */
188 iostatus
; /* Current IO status */
189 pthread_t read_thread_id
, /* Read thread */
190 sidechannel_thread_id
; /* Side-channel thread */
191 int have_sidechannel
= 0, /* Was the side-channel thread started? */
192 have_backchannel
= 0; /* Do we have a back channel? */
193 struct stat sidechannel_info
; /* Side-channel file descriptor info */
194 unsigned char print_buffer
[8192], /* Print data buffer */
195 *print_ptr
; /* Pointer into print data buffer */
196 fd_set input_set
; /* Input set for select() */
197 int nfds
; /* Number of file descriptors */
198 struct timeval
*timeout
, /* Timeout pointer */
200 struct timespec cond_timeout
; /* pthread condition timeout */
201 int num_opts
; /* Number of options */
202 cups_option_t
*opts
; /* Options */
203 const char *val
; /* Option value */
209 * See if the side-channel descriptor is valid...
212 have_sidechannel
= !fstat(CUPS_SC_FD
, &sidechannel_info
) &&
213 S_ISSOCK(sidechannel_info
.st_mode
);
215 g
.wait_eof
= WAIT_EOF
;
218 * Connect to the printer...
221 fprintf(stderr
, "DEBUG: Printing on printer with URI: %s\n", uri
);
222 while ((g
.printer
= find_device(print_cb
, uri
)) == NULL
)
224 _cupsLangPrintFilter(stderr
, "INFO",
225 _("Waiting for printer to become available."));
229 g
.print_fd
= print_fd
;
232 * Some devices need a reset after finishing a job, these devices are
233 * marked with the USB_QUIRK_SOFT_RESET quirk.
235 g
.printer
->reset_after_job
= (g
.printer
->quirks
& USB_QUIRK_SOFT_RESET
? 1 : 0);
238 * If we are printing data from a print driver on stdin, ignore SIGTERM
239 * so that the driver can finish out any page data, e.g. to eject the
240 * current page. We only do this for stdin printing as otherwise there
241 * is no way to cancel a raw print job...
246 memset(&action
, 0, sizeof(action
));
248 sigemptyset(&action
.sa_mask
);
249 action
.sa_handler
= SIG_IGN
;
250 sigaction(SIGTERM
, &action
, NULL
);
254 * Start the side channel thread if the descriptor is valid...
257 pthread_mutex_init(&g
.readwrite_lock_mutex
, NULL
);
258 pthread_cond_init(&g
.readwrite_lock_cond
, NULL
);
259 g
.readwrite_lock
= 1;
261 if (have_sidechannel
)
263 g
.sidechannel_thread_stop
= 0;
264 g
.sidechannel_thread_done
= 0;
266 pthread_cond_init(&g
.sidechannel_thread_cond
, NULL
);
267 pthread_mutex_init(&g
.sidechannel_thread_mutex
, NULL
);
269 if (pthread_create(&sidechannel_thread_id
, NULL
, sidechannel_thread
, NULL
))
271 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
272 _cupsLangPrintFilter(stderr
, "ERROR",
273 _("There was an unrecoverable USB error."));
274 fputs("DEBUG: Couldn't create side-channel thread.\n", stderr
);
275 close_device(g
.printer
);
276 return (CUPS_BACKEND_STOP
);
281 * Debug mode: If option "usb-unidir" is given, always deactivate
285 num_opts
= cupsParseOptions(argv
[5], 0, &opts
);
286 val
= cupsGetOption("usb-unidir", num_opts
, opts
);
287 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
288 strcasecmp(val
, "false"))
290 g
.printer
->read_endp
= -1;
291 fprintf(stderr
, "DEBUG: Forced uni-directional communication "
292 "via \"usb-unidir\" option.\n");
296 * Debug mode: If option "usb-no-reattach" is given, do not re-attach
297 * the usblp kernel module after the job has completed.
300 val
= cupsGetOption("usb-no-reattach", num_opts
, opts
);
301 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
302 strcasecmp(val
, "false"))
304 g
.printer
->usblp_attached
= 0;
305 fprintf(stderr
, "DEBUG: Forced not re-attaching the usblp kernel module "
306 "after the job via \"usb-no-reattach\" option.\n");
310 * Get the read thread going...
313 if (g
.printer
->read_endp
!= -1)
315 have_backchannel
= 1;
317 g
.read_thread_stop
= 0;
318 g
.read_thread_done
= 0;
320 pthread_cond_init(&g
.read_thread_cond
, NULL
);
321 pthread_mutex_init(&g
.read_thread_mutex
, NULL
);
323 if (pthread_create(&read_thread_id
, NULL
, read_thread
, NULL
))
325 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
326 _cupsLangPrintFilter(stderr
, "ERROR",
327 _("There was an unrecoverable USB error."));
328 fputs("DEBUG: Couldn't create read thread.\n", stderr
);
329 close_device(g
.printer
);
330 return (CUPS_BACKEND_STOP
);
334 fprintf(stderr
, "DEBUG: Uni-directional device/mode, back channel "
338 * The main thread sends the print file...
344 print_ptr
= print_buffer
;
346 while (status
== CUPS_BACKEND_OK
&& copies
-- > 0)
348 _cupsLangPrintFilter(stderr
, "INFO", _("Sending data to printer."));
350 if (print_fd
!= STDIN_FILENO
)
352 fputs("PAGE: 1 1\n", stderr
);
353 lseek(print_fd
, 0, SEEK_SET
);
356 while (status
== CUPS_BACKEND_OK
)
361 FD_SET(print_fd
, &input_set
);
364 * Calculate select timeout...
365 * If we have data waiting to send timeout is 100ms.
366 * else if we're draining print_fd timeout is 0.
367 * else we're waiting forever...
373 tv
.tv_usec
= 100000; /* 100ms */
376 else if (g
.drain_output
)
386 * I/O is unlocked around select...
389 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
390 g
.readwrite_lock
= 0;
391 pthread_cond_signal(&g
.readwrite_lock_cond
);
392 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
394 nfds
= select(print_fd
+ 1, &input_set
, NULL
, NULL
, timeout
);
397 * Reacquire the lock...
400 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
401 while (g
.readwrite_lock
)
402 pthread_cond_wait(&g
.readwrite_lock_cond
, &g
.readwrite_lock_mutex
);
403 g
.readwrite_lock
= 1;
404 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
408 if (errno
== EINTR
&& total_bytes
== 0)
410 fputs("DEBUG: Received an interrupt before any bytes were "
411 "written, aborting.\n", stderr
);
412 close_device(g
.printer
);
413 return (CUPS_BACKEND_OK
);
415 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
417 _cupsLangPrintFilter(stderr
, "ERROR",
418 _("Unable to read print data."));
419 perror("DEBUG: select");
420 close_device(g
.printer
);
421 return (CUPS_BACKEND_FAILED
);
426 * If drain output has finished send a response...
429 if (g
.drain_output
&& !nfds
&& !g
.print_bytes
)
431 /* Send a response... */
432 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
437 * Check if we have print data ready...
440 if (FD_ISSET(print_fd
, &input_set
))
442 g
.print_bytes
= read(print_fd
, print_buffer
, sizeof(print_buffer
));
444 if (g
.print_bytes
< 0)
447 * Read error - bail if we don't see EAGAIN or EINTR...
450 if (errno
!= EAGAIN
&& errno
!= EINTR
)
452 _cupsLangPrintFilter(stderr
, "ERROR",
453 _("Unable to read print data."));
454 perror("DEBUG: read");
455 close_device(g
.printer
);
456 return (CUPS_BACKEND_FAILED
);
461 else if (g
.print_bytes
== 0)
464 * End of file, break out of the loop...
470 print_ptr
= print_buffer
;
472 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
478 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
479 g
.printer
->write_endp
,
480 print_buffer
, g
.print_bytes
,
483 * Ignore timeout errors, but retain the number of bytes written to
484 * avoid sending duplicate data...
487 if (iostatus
== LIBUSB_ERROR_TIMEOUT
)
489 fputs("DEBUG: Got USB transaction timeout during write.\n", stderr
);
494 * If we've stalled, retry the write...
497 else if (iostatus
== LIBUSB_ERROR_PIPE
)
499 fputs("DEBUG: Got USB pipe stalled during write.\n", stderr
);
501 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
502 g
.printer
->write_endp
,
503 print_buffer
, g
.print_bytes
,
508 * Retry a write after an aborted write since we probably just got
512 else if (iostatus
== LIBUSB_ERROR_INTERRUPTED
)
514 fputs("DEBUG: Got USB return aborted during write.\n", stderr
);
516 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
517 g
.printer
->write_endp
,
518 print_buffer
, g
.print_bytes
,
525 * Write error - bail if we don't see an error we can retry...
528 _cupsLangPrintFilter(stderr
, "ERROR",
529 _("Unable to send data to printer."));
530 fprintf(stderr
, "DEBUG: libusb write operation returned %x.\n",
533 status
= CUPS_BACKEND_FAILED
;
538 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n",
541 g
.print_bytes
-= bytes
;
543 total_bytes
+= bytes
;
547 if (print_fd
!= 0 && status
== CUPS_BACKEND_OK
)
548 fprintf(stderr
, "DEBUG: Sending print file, " CUPS_LLFMT
" bytes...\n",
549 CUPS_LLCAST total_bytes
);
553 fprintf(stderr
, "DEBUG: Sent " CUPS_LLFMT
" bytes...\n",
554 CUPS_LLCAST total_bytes
);
557 * Signal the side channel thread to exit...
560 if (have_sidechannel
)
563 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
564 g
.readwrite_lock
= 0;
565 pthread_cond_signal(&g
.readwrite_lock_cond
);
566 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
568 g
.sidechannel_thread_stop
= 1;
569 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
571 if (!g
.sidechannel_thread_done
)
573 gettimeofday(&tv
, NULL
);
574 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_SIDE_DELAY
;
575 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
577 while (!g
.sidechannel_thread_done
)
579 if (pthread_cond_timedwait(&g
.sidechannel_thread_cond
,
580 &g
.sidechannel_thread_mutex
,
586 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
590 * Signal the read thread to exit then wait 7 seconds for it to complete...
593 if (have_backchannel
)
595 g
.read_thread_stop
= 1;
597 pthread_mutex_lock(&g
.read_thread_mutex
);
599 if (!g
.read_thread_done
)
601 fputs("DEBUG: Waiting for read thread to exit...\n", stderr
);
603 gettimeofday(&tv
, NULL
);
604 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_EOF_DELAY
;
605 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
607 while (!g
.read_thread_done
)
609 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
615 * If it didn't exit abort the pending read and wait an additional
619 if (!g
.read_thread_done
)
621 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
626 gettimeofday(&tv
, NULL
);
627 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
628 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
630 while (!g
.read_thread_done
)
632 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
639 pthread_mutex_unlock(&g
.read_thread_mutex
);
643 * Close the connection and input file and general clean up...
646 if (g
.printer
->quirks
& USB_QUIRK_DELAY_CLOSE
)
649 close_device(g
.printer
);
655 libusb_free_device_list(all_list
, 1);
663 * 'close_device()' - Close the connection to the USB printer.
666 static int /* I - 0 on success, -1 on failure */
667 close_device(usb_printer_t
*printer
) /* I - Printer */
669 struct libusb_device_descriptor devdesc
;
670 /* Current device descriptor */
671 struct libusb_config_descriptor
*confptr
;
672 /* Pointer to current configuration */
678 * Release interfaces before closing so that we know all data is written
682 int errcode
; /* Return value of libusb function */
683 int number1
, /* Interface number */
684 number2
; /* Configuration number */
687 libusb_get_config_descriptor(printer
->device
, printer
->conf
, &confptr
);
690 number1
= confptr
->interface
[printer
->iface
].
691 altsetting
[printer
->altset
].bInterfaceNumber
;
692 libusb_release_interface(printer
->handle
, number1
);
694 number2
= confptr
->bConfigurationValue
;
696 libusb_free_config_descriptor(confptr
);
699 * If we have changed the configuration from one valid configuration
700 * to another, restore the old one
702 if (printer
->origconf
> 0 && printer
->origconf
!= number2
)
704 fprintf(stderr
, "DEBUG: Restoring USB device configuration: %d -> %d\n",
705 number2
, printer
->origconf
);
706 if ((errcode
= libusb_set_configuration(printer
->handle
,
707 printer
->origconf
)) < 0)
709 if (errcode
!= LIBUSB_ERROR_BUSY
)
712 libusb_get_device_descriptor (printer
->device
, &devdesc
);
715 "DEBUG: Failed to set configuration %d\n",
719 "DEBUG: Failed to set configuration %d for %04x:%04x\n",
720 printer
->origconf
, devdesc
.idVendor
, devdesc
.idProduct
);
726 * Re-attach "usblp" kernel module if it was attached before using this
729 if (printer
->usblp_attached
== 1)
730 if (libusb_attach_kernel_driver(printer
->handle
, number1
) < 0)
732 errcode
= libusb_get_device_descriptor (printer
->device
, &devdesc
);
735 "DEBUG: Failed to re-attach \"usblp\" kernel module\n");
738 "DEBUG: Failed to re-attach \"usblp\" kernel module to "
739 "%04x:%04x\n", devdesc
.idVendor
, devdesc
.idProduct
);
744 "DEBUG: Failed to get configuration descriptor %d\n",
748 * Reset the device to clean up after the job
751 if (printer
->reset_after_job
== 1)
753 if ((errcode
= libusb_reset_device(printer
->handle
)) < 0)
755 "DEBUG: Device reset failed, error code: %d\n",
759 "DEBUG: Resetting printer.\n");
763 * Close the interface and return...
766 libusb_close(printer
->handle
);
767 printer
->handle
= NULL
;
775 * 'compare_quirks()' - Compare two quirks entries.
778 static int /* O - Result of comparison */
779 compare_quirks(usb_quirk_t
*a
, /* I - First quirk entry */
780 usb_quirk_t
*b
) /* I - Second quirk entry */
782 int result
; /* Result of comparison */
784 if ((result
= b
->vendor_id
- a
->vendor_id
) == 0)
785 result
= b
->product_id
- a
->product_id
;
792 * 'find_device()' - Find or enumerate USB printers.
795 static usb_printer_t
* /* O - Found printer */
796 find_device(usb_cb_t cb
, /* I - Callback function */
797 const void *data
) /* I - User data for callback */
799 libusb_device
**list
; /* List of connected USB devices */
800 libusb_device
*device
= NULL
; /* Current device */
801 struct libusb_device_descriptor devdesc
;
802 /* Current device descriptor */
803 struct libusb_config_descriptor
*confptr
= NULL
;
804 /* Pointer to current configuration */
805 const struct libusb_interface
*ifaceptr
= NULL
;
806 /* Pointer to current interface */
807 const struct libusb_interface_descriptor
*altptr
= NULL
;
808 /* Pointer to current alternate setting */
809 const struct libusb_endpoint_descriptor
*endpptr
= NULL
;
810 /* Pointer to current endpoint */
811 ssize_t err
= 0, /* Error code */
812 numdevs
, /* number of connected devices */
814 uint8_t conf
, /* Current configuration */
815 iface
, /* Current interface */
816 altset
, /* Current alternate setting */
817 protocol
, /* Current protocol */
818 endp
, /* Current endpoint */
819 read_endp
, /* Current read endpoint */
820 write_endp
; /* Current write endpoint */
821 char device_id
[1024],/* IEEE-1284 device ID */
824 static usb_printer_t printer
; /* Current printer */
828 * Initialize libusb...
831 err
= libusb_init(NULL
);
834 fprintf(stderr
, "DEBUG: Unable to initialize USB access via libusb, "
835 "libusb error %i\n", (int)err
);
839 numdevs
= libusb_get_device_list(NULL
, &list
);
840 fprintf(stderr
, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs
);
843 * Then loop through the devices it found...
847 for (i
= 0; i
< numdevs
; i
++)
852 * Ignore devices with no configuration data and anything that is not
856 if (libusb_get_device_descriptor(device
, &devdesc
) < 0)
859 if (!devdesc
.bNumConfigurations
|| !devdesc
.idVendor
||
863 printer
.quirks
= find_quirks(devdesc
.idVendor
, devdesc
.idProduct
);
866 * Ignore blacklisted printers...
869 if (printer
.quirks
& USB_QUIRK_BLACKLIST
)
872 for (conf
= 0; conf
< devdesc
.bNumConfigurations
; conf
++)
874 if (libusb_get_config_descriptor(device
, conf
, &confptr
) < 0)
876 for (iface
= 0, ifaceptr
= confptr
->interface
;
877 iface
< confptr
->bNumInterfaces
;
878 iface
++, ifaceptr
++)
881 * Some printers offer multiple interfaces...
886 for (altset
= 0, altptr
= ifaceptr
->altsetting
;
887 altset
< ifaceptr
->num_altsetting
;
888 altset
++, altptr
++)
891 * Currently we only support unidirectional and bidirectional
892 * printers. Future versions of this code will support the
893 * 1284.4 (packet mode) protocol as well.
896 if (((altptr
->bInterfaceClass
!= LIBUSB_CLASS_PRINTER
||
897 altptr
->bInterfaceSubClass
!= 1) &&
898 ((printer
.quirks
& USB_QUIRK_VENDOR_CLASS
) == 0)) ||
899 (altptr
->bInterfaceProtocol
!= 1 && /* Unidirectional */
900 altptr
->bInterfaceProtocol
!= 2) || /* Bidirectional */
901 altptr
->bInterfaceProtocol
< protocol
)
904 if (printer
.quirks
& USB_QUIRK_VENDOR_CLASS
)
905 fprintf(stderr
, "DEBUG: Printer does not report class 7 and/or "
906 "subclass 1 but works as a printer anyway\n");
911 for (endp
= 0, endpptr
= altptr
->endpoint
;
912 endp
< altptr
->bNumEndpoints
;
914 if ((endpptr
->bmAttributes
& LIBUSB_TRANSFER_TYPE_MASK
) ==
915 LIBUSB_TRANSFER_TYPE_BULK
)
917 if (endpptr
->bEndpointAddress
& LIBUSB_ENDPOINT_DIR_MASK
)
923 if (write_endp
!= 0xff)
926 * Save the best match so far...
929 protocol
= altptr
->bInterfaceProtocol
;
930 printer
.altset
= altset
;
931 printer
.write_endp
= write_endp
;
933 printer
.read_endp
= read_endp
;
935 printer
.read_endp
= -1;
941 printer
.device
= device
;
943 printer
.iface
= iface
;
944 printer
.protocol
= protocol
;
945 printer
.handle
= NULL
;
947 if (!open_device(&printer
, data
!= NULL
))
949 get_device_id(&printer
, device_id
, sizeof(device_id
));
950 make_device_uri(&printer
, device_id
, device_uri
,
953 fprintf(stderr
, "DEBUG2: Printer found with device ID: %s "
955 device_id
, device_uri
);
957 if ((*cb
)(&printer
, device_uri
, device_id
, data
))
959 fprintf(stderr
, "DEBUG: Device protocol: %d\n",
961 if (printer
.quirks
& USB_QUIRK_UNIDIR
)
963 printer
.read_endp
= -1;
964 fprintf(stderr
, "DEBUG: Printer reports bi-di support "
965 "but in reality works only uni-directionally\n");
967 if (printer
.read_endp
!= -1)
969 printer
.read_endp
= confptr
->interface
[printer
.iface
].
970 altsetting
[printer
.altset
].
971 endpoint
[printer
.read_endp
].
975 fprintf(stderr
, "DEBUG: Uni-directional USB communication "
977 printer
.write_endp
= confptr
->interface
[printer
.iface
].
978 altsetting
[printer
.altset
].
979 endpoint
[printer
.write_endp
].
981 if (printer
.quirks
& USB_QUIRK_NO_REATTACH
)
983 printer
.usblp_attached
= 0;
984 fprintf(stderr
, "DEBUG: Printer does not like usblp "
985 "kernel module to be re-attached after job\n");
987 libusb_free_config_descriptor(confptr
);
991 close_device(&printer
);
995 libusb_free_config_descriptor(confptr
);
1000 * If we get this far without returning, then we haven't found a printer
1009 libusb_free_device_list(list
, 1);
1017 * 'find_quirks()' - Find the quirks for the given printer, if any.
1019 * First looks for an exact match, then looks for the vendor ID wildcard match.
1022 static unsigned /* O - Quirks flags */
1023 find_quirks(int vendor_id
, /* I - Vendor ID */
1024 int product_id
) /* I - Product ID */
1026 usb_quirk_t key
, /* Search key */
1027 *match
; /* Matching quirk entry */
1030 key
.vendor_id
= vendor_id
;
1031 key
.product_id
= product_id
;
1033 if ((match
= cupsArrayFind(all_quirks
, &key
)) != NULL
)
1034 return (match
->quirks
);
1038 if ((match
= cupsArrayFind(all_quirks
, &key
)) != NULL
)
1039 return (match
->quirks
);
1041 return (USB_QUIRK_WHITELIST
);
1046 * 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
1049 static int /* O - 0 on success, -1 on error */
1050 get_device_id(usb_printer_t
*printer
, /* I - Printer */
1051 char *buffer
, /* I - String buffer */
1052 size_t bufsize
) /* I - Number of bytes in buffer */
1054 int length
; /* Length of device ID */
1057 if (libusb_control_transfer(printer
->handle
,
1058 LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_ENDPOINT_IN
|
1059 LIBUSB_RECIPIENT_INTERFACE
,
1061 (printer
->iface
<< 8) | printer
->altset
,
1062 (unsigned char *)buffer
, bufsize
, 5000) < 0)
1069 * Extract the length of the device ID string from the first two
1070 * bytes. The 1284 spec says the length is stored MSB first...
1073 length
= (int)((((unsigned)buffer
[0] & 255) << 8) | ((unsigned)buffer
[1] & 255));
1076 * Check to see if the length is larger than our buffer or less than 14 bytes
1077 * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
1079 * If the length is out-of-range, assume that the vendor incorrectly
1080 * implemented the 1284 spec and re-read the length as LSB first,..
1083 if (length
> bufsize
|| length
< 14)
1084 length
= (int)((((unsigned)buffer
[1] & 255) << 8) | ((unsigned)buffer
[0] & 255));
1086 if (length
> bufsize
)
1092 * Invalid device ID, clear it!
1102 * Copy the device ID text to the beginning of the buffer and
1106 memmove(buffer
, buffer
+ 2, (size_t)length
);
1107 buffer
[length
] = '\0';
1114 * 'list_cb()' - List USB printers for discovery.
1117 static int /* O - 0 to continue, 1 to stop */
1118 list_cb(usb_printer_t
*printer
, /* I - Printer */
1119 const char *device_uri
, /* I - Device URI */
1120 const char *device_id
, /* I - IEEE-1284 device ID */
1121 const void *data
) /* I - User data (not used) */
1123 char make_model
[1024]; /* Make and model */
1127 * Get the device URI and make/model strings...
1130 if (backendGetMakeModel(device_id
, make_model
, sizeof(make_model
)))
1131 strlcpy(make_model
, "Unknown", sizeof(make_model
));
1134 * Report the printer...
1137 cupsBackendReport("direct", device_uri
, make_model
, make_model
, device_id
,
1149 * 'load_quirks()' - Load all quirks files in the /usr/share/cups/usb directory.
1155 const char *datadir
; /* CUPS_DATADIR environment variable */
1156 char filename
[1024], /* Filename */
1157 line
[1024]; /* Line from file */
1158 cups_dir_t
*dir
; /* Directory */
1159 cups_dentry_t
*dent
; /* Directory entry */
1160 cups_file_t
*fp
; /* Quirks file */
1161 usb_quirk_t
*quirk
; /* New quirk */
1164 all_quirks
= cupsArrayNew((cups_array_func_t
)compare_quirks
, NULL
);
1166 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
1167 datadir
= CUPS_DATADIR
;
1169 snprintf(filename
, sizeof(filename
), "%s/usb", datadir
);
1170 if ((dir
= cupsDirOpen(filename
)) == NULL
)
1176 fprintf(stderr
, "DEBUG: Loading USB quirks from \"%s\".\n", filename
);
1178 while ((dent
= cupsDirRead(dir
)) != NULL
)
1180 if (!S_ISREG(dent
->fileinfo
.st_mode
))
1183 snprintf(filename
, sizeof(filename
), "%s/usb/%s", datadir
, dent
->filename
);
1184 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1190 while (cupsFileGets(fp
, line
, sizeof(line
)))
1193 * Skip blank and comment lines...
1196 if (line
[0] == '#' || !line
[0])
1203 if ((quirk
= calloc(1, sizeof(usb_quirk_t
))) == NULL
)
1205 perror("DEBUG: Unable to allocate memory for quirk");
1209 if (sscanf(line
, "%x%x", &quirk
->vendor_id
, &quirk
->product_id
) < 1)
1211 fprintf(stderr
, "DEBUG: Bad line: %s\n", line
);
1216 if (strstr(line
, " blacklist"))
1217 quirk
->quirks
|= USB_QUIRK_BLACKLIST
;
1219 if (strstr(line
, " delay-close"))
1220 quirk
->quirks
|= USB_QUIRK_DELAY_CLOSE
;
1222 if (strstr(line
, " no-reattach"))
1223 quirk
->quirks
|= USB_QUIRK_NO_REATTACH
;
1225 if (strstr(line
, " soft-reset"))
1226 quirk
->quirks
|= USB_QUIRK_SOFT_RESET
;
1228 if (strstr(line
, " unidir"))
1229 quirk
->quirks
|= USB_QUIRK_UNIDIR
;
1231 if (strstr(line
, " usb-init"))
1232 quirk
->quirks
|= USB_QUIRK_USB_INIT
;
1234 if (strstr(line
, " vendor-class"))
1235 quirk
->quirks
|= USB_QUIRK_VENDOR_CLASS
;
1237 cupsArrayAdd(all_quirks
, quirk
);
1243 fprintf(stderr
, "DEBUG: Loaded %d quirks.\n", cupsArrayCount(all_quirks
));
1250 * 'make_device_uri()' - Create a device URI for a USB printer.
1253 static char * /* O - Device URI */
1255 usb_printer_t
*printer
, /* I - Printer */
1256 const char *device_id
, /* I - IEEE-1284 device ID */
1257 char *uri
, /* I - Device URI buffer */
1258 size_t uri_size
) /* I - Size of device URI buffer */
1260 struct libusb_device_descriptor devdesc
;
1261 /* Current device descriptor */
1262 char options
[1024]; /* Device URI options */
1263 int num_values
; /* Number of 1284 parameters */
1264 cups_option_t
*values
; /* 1284 parameters */
1265 const char *mfg
, /* Manufacturer */
1267 *des
= NULL
, /* Description */
1268 *sern
; /* Serial number */
1269 size_t mfglen
; /* Length of manufacturer string */
1270 char tempmfg
[256], /* Temporary manufacturer string */
1271 tempsern
[256], /* Temporary serial number string */
1272 *tempptr
; /* Pointer into temp string */
1276 * Get the make, model, and serial numbers...
1279 num_values
= _cupsGet1284Values(device_id
, &values
);
1281 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
1282 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
1283 if ((sern
= cupsGetOption("SN", num_values
, values
)) == NULL
&&
1284 ((libusb_get_device_descriptor(printer
->device
, &devdesc
) >= 0) &&
1285 devdesc
.iSerialNumber
))
1288 * Try getting the serial number from the device itself...
1292 libusb_get_string_descriptor_ascii(printer
->handle
,
1293 devdesc
.iSerialNumber
,
1294 (unsigned char *)tempsern
,
1295 sizeof(tempsern
) - 1);
1298 tempsern
[length
] = '\0';
1303 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
1304 mfg
= cupsGetOption("MFG", num_values
, values
);
1306 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
1307 mdl
= cupsGetOption("MDL", num_values
, values
);
1310 * To maintain compatibility with the original character device backend on
1311 * Linux and *BSD, map manufacturer names...
1316 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
1318 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
1324 * No manufacturer? Use the model string or description...
1328 _ppdNormalizeMakeAndModel(mdl
, tempmfg
, sizeof(tempmfg
));
1329 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
1330 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
1331 _ppdNormalizeMakeAndModel(des
, tempmfg
, sizeof(tempmfg
));
1333 strlcpy(tempmfg
, "Unknown", sizeof(tempmfg
));
1335 if ((tempptr
= strchr(tempmfg
, ' ')) != NULL
)
1344 * No model? Use description...
1347 mdl
= des
; /* We remove the manufacturer name below */
1348 else if (!strncasecmp(mfg
, "Unknown", 7))
1351 mdl
= "Unknown Model";
1354 mfglen
= strlen(mfg
);
1356 if (!strncasecmp(mdl
, mfg
, mfglen
) && _cups_isspace(mdl
[mfglen
]))
1360 while (_cups_isspace(*mdl
))
1365 * Generate the device URI from the manufacturer, model, serial number,
1366 * and interface number...
1371 if (printer
->iface
> 0)
1372 snprintf(options
, sizeof(options
), "?serial=%s&interface=%d", sern
,
1375 snprintf(options
, sizeof(options
), "?serial=%s", sern
);
1377 else if (printer
->iface
> 0)
1378 snprintf(options
, sizeof(options
), "?interface=%d", printer
->iface
);
1382 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, "usb", NULL
, mfg
, 0,
1383 "/%s%s", mdl
, options
);
1385 cupsFreeOptions(num_values
, values
);
1392 * 'open_device()' - Open a connection to the USB printer.
1395 static int /* O - 0 on success, -1 on error */
1396 open_device(usb_printer_t
*printer
, /* I - Printer */
1397 int verbose
) /* I - Update connecting-to-device state? */
1399 struct libusb_device_descriptor devdesc
;
1400 /* Current device descriptor */
1401 struct libusb_config_descriptor
*confptr
= NULL
;
1402 /* Pointer to current configuration */
1403 int number1
= -1, /* Configuration/interface/altset */
1404 number2
= -1, /* numbers */
1406 char current
; /* Current configuration */
1410 * Return immediately if we are already connected...
1413 if (printer
->handle
)
1417 * Try opening the printer...
1420 if ((errcode
= libusb_open(printer
->device
, &printer
->handle
)) < 0)
1422 fprintf(stderr
, "DEBUG: Failed to open device, code: %d\n",
1427 printer
->usblp_attached
= 0;
1428 printer
->reset_after_job
= 0;
1431 fputs("STATE: +connecting-to-device\n", stderr
);
1433 if ((errcode
= libusb_get_device_descriptor(printer
->device
, &devdesc
)) < 0)
1435 fprintf(stderr
, "DEBUG: Failed to get device descriptor, code: %d\n",
1441 * Get the "usblp" kernel module out of the way. This backend only
1442 * works without the module attached.
1445 errcode
= libusb_kernel_driver_active(printer
->handle
, printer
->iface
);
1447 printer
->usblp_attached
= 0;
1448 else if (errcode
== 1)
1450 printer
->usblp_attached
= 1;
1452 libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1454 fprintf(stderr
, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1455 devdesc
.idVendor
, devdesc
.idProduct
);
1461 printer
->usblp_attached
= 0;
1463 if (errcode
!= LIBUSB_ERROR_NOT_SUPPORTED
)
1466 "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" "
1467 "kernel module attached\n", devdesc
.idVendor
, devdesc
.idProduct
);
1473 * Set the desired configuration, but only if it needs changing. Some
1474 * printers (e.g., Samsung) don't like libusb_set_configuration. It will
1475 * succeed, but the following print job is sometimes silently lost by the
1479 if (libusb_control_transfer(printer
->handle
,
1480 LIBUSB_REQUEST_TYPE_STANDARD
| LIBUSB_ENDPOINT_IN
|
1481 LIBUSB_RECIPIENT_DEVICE
,
1482 8, /* GET_CONFIGURATION */
1483 0, 0, (unsigned char *)¤t
, 1, 5000) < 0)
1484 current
= 0; /* Assume not configured */
1486 printer
->origconf
= current
;
1489 libusb_get_config_descriptor (printer
->device
, printer
->conf
, &confptr
))
1492 fprintf(stderr
, "DEBUG: Failed to get config descriptor for %04x:%04x\n",
1493 devdesc
.idVendor
, devdesc
.idProduct
);
1496 number1
= confptr
->bConfigurationValue
;
1498 if (number1
!= current
)
1500 fprintf(stderr
, "DEBUG: Switching USB device configuration: %d -> %d\n",
1502 if ((errcode
= libusb_set_configuration(printer
->handle
, number1
)) < 0)
1505 * If the set fails, chances are that the printer only supports a
1506 * single configuration. Technically these printers don't conform to
1507 * the USB printer specification, but otherwise they'll work...
1510 if (errcode
!= LIBUSB_ERROR_BUSY
)
1511 fprintf(stderr
, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
1512 number1
, devdesc
.idVendor
, devdesc
.idProduct
);
1517 * Claim interfaces as needed...
1520 number1
= confptr
->interface
[printer
->iface
].
1521 altsetting
[printer
->altset
].bInterfaceNumber
;
1523 while ((errcode
= libusb_claim_interface(printer
->handle
, number1
)) < 0)
1525 if (errcode
!= LIBUSB_ERROR_BUSY
)
1528 "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
1529 number1
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1533 else if ((errcode
= libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1536 "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1537 devdesc
.idVendor
, devdesc
.idProduct
);
1546 * Set alternate setting, but only if there is more than one option. Some
1547 * printers (e.g., Samsung) don't like usb_set_altinterface.
1550 if (confptr
->interface
[printer
->iface
].num_altsetting
> 1)
1552 number1
= confptr
->interface
[printer
->iface
].
1553 altsetting
[printer
->altset
].bInterfaceNumber
;
1554 number2
= confptr
->interface
[printer
->iface
].
1555 altsetting
[printer
->altset
].bAlternateSetting
;
1558 libusb_set_interface_alt_setting(printer
->handle
, number1
, number2
))
1561 if (errcode
!= LIBUSB_ERROR_BUSY
)
1564 "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
1566 number2
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1573 libusb_free_config_descriptor(confptr
);
1576 fputs("STATE: -connecting-to-device\n", stderr
);
1581 * If we get here, there was a hard error...
1587 fputs("STATE: -connecting-to-device\n", stderr
);
1589 libusb_close(printer
->handle
);
1590 printer
->handle
= NULL
;
1597 * 'print_cb()' - Find a USB printer for printing.
1600 static int /* O - 0 to continue, 1 to stop (found) */
1601 print_cb(usb_printer_t
*printer
, /* I - Printer */
1602 const char *device_uri
, /* I - Device URI */
1603 const char *device_id
, /* I - IEEE-1284 device ID */
1604 const void *data
) /* I - User data (make, model, S/N) */
1606 char requested_uri
[1024], /* Requested URI */
1607 *requested_ptr
, /* Pointer into requested URI */
1608 detected_uri
[1024], /* Detected URI */
1609 *detected_ptr
; /* Pointer into detected URI */
1613 * If we have an exact match, stop now...
1616 if (!strcmp((char *)data
, device_uri
))
1620 * Work on copies of the URIs...
1623 strlcpy(requested_uri
, (char *)data
, sizeof(requested_uri
));
1624 strlcpy(detected_uri
, device_uri
, sizeof(detected_uri
));
1627 * libusb-discovered URIs can have an "interface" specification and this
1628 * never happens for usblp-discovered URIs, so remove the "interface"
1629 * specification from the URI which we are checking currently. This way a
1630 * queue for a usblp-discovered printer can now be accessed via libusb.
1632 * Similarly, strip "?serial=NNN...NNN" as needed.
1635 if ((requested_ptr
= strstr(requested_uri
, "?interface=")) == NULL
)
1636 requested_ptr
= strstr(requested_uri
, "&interface=");
1637 if ((detected_ptr
= strstr(detected_uri
, "?interface=")) == NULL
)
1638 detected_ptr
= strstr(detected_uri
, "&interface=");
1640 if (!requested_ptr
&& detected_ptr
)
1643 * Strip "[?&]interface=nnn" from the detected printer.
1646 *detected_ptr
= '\0';
1648 else if (requested_ptr
&& !detected_ptr
)
1651 * Strip "[?&]interface=nnn" from the requested printer.
1654 *requested_ptr
= '\0';
1657 if ((requested_ptr
= strstr(requested_uri
, "?serial=?")) != NULL
)
1660 * Strip "?serial=?" from the requested printer. This is a special
1661 * case, as "?serial=?" means no serial number and not the serial
1662 * number '?'. This is not covered by the checks below...
1665 *requested_ptr
= '\0';
1668 if ((requested_ptr
= strstr(requested_uri
, "?serial=")) == NULL
&&
1669 (detected_ptr
= strstr(detected_uri
, "?serial=")) != NULL
)
1672 * Strip "?serial=nnn" from the detected printer.
1675 *detected_ptr
= '\0';
1677 else if (requested_ptr
&& !detected_ptr
)
1680 * Strip "?serial=nnn" from the requested printer.
1683 *requested_ptr
= '\0';
1686 return (!strcmp(requested_uri
, detected_uri
));
1691 * 'read_thread()' - Thread to read the backchannel data on.
1694 static void *read_thread(void *reference
)
1696 unsigned char readbuffer
[512];
1708 * Read frequency: once every 250 milliseconds.
1712 delay
.tv_usec
= 250000;
1717 * Remember when we started so we can throttle the loop after the read
1721 gettimeofday(&now
, NULL
);
1724 * Calculate what 250 milliSeconds are in absolute time...
1727 timeradd(&now
, &delay
, &end
);
1729 rbytes
= sizeof(readbuffer
);
1730 readstatus
= libusb_bulk_transfer(g
.printer
->handle
,
1731 g
.printer
->read_endp
,
1734 if (readstatus
== LIBUSB_SUCCESS
&& rbytes
> 0)
1736 fprintf(stderr
, "DEBUG: Read %d bytes of back-channel data...\n",
1738 cupsBackChannelWrite((const char *)readbuffer
, (size_t)rbytes
, 1.0);
1740 else if (readstatus
== LIBUSB_ERROR_TIMEOUT
)
1741 fputs("DEBUG: Got USB transaction timeout during read.\n", stderr
);
1742 else if (readstatus
== LIBUSB_ERROR_PIPE
)
1743 fputs("DEBUG: Got USB pipe stalled during read.\n", stderr
);
1744 else if (readstatus
== LIBUSB_ERROR_INTERRUPTED
)
1745 fputs("DEBUG: Got USB return aborted during read.\n", stderr
);
1748 * Make sure this loop executes no more than once every 250 miliseconds...
1751 if ((readstatus
!= LIBUSB_SUCCESS
|| rbytes
== 0) &&
1752 (g
.wait_eof
|| !g
.read_thread_stop
))
1754 gettimeofday(&now
, NULL
);
1755 if (timercmp(&now
, &end
, <))
1757 timersub(&end
, &now
, &timeleft
);
1758 usleep(1000000 * timeleft
.tv_sec
+ timeleft
.tv_usec
);
1761 } while (g
.wait_eof
|| !g
.read_thread_stop
);
1764 * Let the main thread know that we have completed the read thread...
1767 pthread_mutex_lock(&g
.read_thread_mutex
);
1768 g
.read_thread_done
= 1;
1769 pthread_cond_signal(&g
.read_thread_cond
);
1770 pthread_mutex_unlock(&g
.read_thread_mutex
);
1777 * 'sidechannel_thread()' - Handle side-channel requests.
1781 sidechannel_thread(void *reference
)
1783 cups_sc_command_t command
; /* Request command */
1784 cups_sc_status_t status
; /* Request/response status */
1785 char data
[2048]; /* Request/response data */
1786 int datalen
; /* Request/response data size */
1793 datalen
= sizeof(data
);
1795 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
1797 if (status
== CUPS_SC_STATUS_TIMEOUT
)
1805 case CUPS_SC_CMD_SOFT_RESET
: /* Do a soft reset */
1806 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1810 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
1811 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1815 case CUPS_SC_CMD_DRAIN_OUTPUT
: /* Drain all pending output */
1816 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1822 case CUPS_SC_CMD_GET_BIDI
: /* Is the connection bidirectional? */
1823 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1826 data
[0] = (g
.printer
->protocol
>= 2 ? 1 : 0);
1827 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1830 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1834 case CUPS_SC_CMD_GET_DEVICE_ID
: /* Return IEEE-1284 device ID */
1835 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1838 datalen
= sizeof(data
);
1839 if (get_device_id(g
.printer
, data
, sizeof(data
)))
1841 status
= CUPS_SC_STATUS_IO_ERROR
;
1846 status
= CUPS_SC_STATUS_OK
;
1847 datalen
= strlen(data
);
1849 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, datalen
, 1.0);
1851 if (datalen
< sizeof(data
))
1852 data
[datalen
] = '\0';
1854 data
[sizeof(data
) - 1] = '\0';
1857 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1861 case CUPS_SC_CMD_GET_STATE
: /* Return device state */
1862 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1865 data
[0] = CUPS_SC_STATE_ONLINE
;
1866 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1869 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1873 case CUPS_SC_CMD_GET_CONNECTED
: /* Return whether device is
1875 fputs("DEBUG: CUPS_SC_CMD_GET_CONNECTED received from driver...\n",
1878 data
[0] = (g
.printer
->handle
? 1 : 0);
1879 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1882 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1887 fprintf(stderr
, "DEBUG: Unknown side-channel command (%d) received "
1888 "from driver...\n", command
);
1890 cupsSideChannelWrite(command
, CUPS_SC_STATUS_NOT_IMPLEMENTED
,
1893 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1898 while (!g
.sidechannel_thread_stop
);
1900 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
1901 g
.sidechannel_thread_done
= 1;
1902 pthread_cond_signal(&g
.sidechannel_thread_cond
);
1903 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
1910 * 'soft_reset()' - Send a soft reset to the device.
1916 fd_set input_set
; /* Input set for select() */
1917 struct timeval tv
; /* Time value */
1918 char buffer
[2048]; /* Buffer */
1919 struct timespec cond_timeout
; /* pthread condition timeout */
1923 * Send an abort once a second until the I/O lock is released by the main
1927 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1928 while (g
.readwrite_lock
)
1930 gettimeofday(&tv
, NULL
);
1931 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
1932 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
1934 while (g
.readwrite_lock
)
1936 if (pthread_cond_timedwait(&g
.readwrite_lock_cond
,
1937 &g
.readwrite_lock_mutex
,
1938 &cond_timeout
) != 0)
1943 g
.readwrite_lock
= 1;
1944 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1947 * Flush bytes waiting on print_fd...
1952 FD_ZERO(&input_set
);
1953 FD_SET(g
.print_fd
, &input_set
);
1958 while (select(g
.print_fd
+1, &input_set
, NULL
, NULL
, &tv
) > 0)
1959 if (read(g
.print_fd
, buffer
, sizeof(buffer
)) <= 0)
1966 soft_reset_printer(g
.printer
);
1969 * Release the I/O lock...
1972 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1973 g
.readwrite_lock
= 0;
1974 pthread_cond_signal(&g
.readwrite_lock_cond
);
1975 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1980 * 'soft_reset_printer()' - Do the soft reset request specific to printers
1982 * This soft reset is specific to the printer device class and is much less
1983 * invasive than the general USB reset libusb_reset_device(). Especially it
1984 * does never happen that the USB addressing and configuration changes. What
1985 * is actually done is that all buffers get flushed and the bulk IN and OUT
1986 * pipes get reset to their default states. This clears all stall conditions.
1987 * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
1990 static int /* O - 0 on success, < 0 on error */
1992 usb_printer_t
*printer
) /* I - Printer */
1994 struct libusb_config_descriptor
*confptr
= NULL
;
1995 /* Pointer to current configuration */
1996 int interface
, /* Interface to reset */
1997 errcode
; /* Error code */
2000 if (libusb_get_config_descriptor(printer
->device
, printer
->conf
,
2002 interface
= printer
->iface
;
2004 interface
= confptr
->interface
[printer
->iface
].
2005 altsetting
[printer
->altset
].bInterfaceNumber
;
2007 libusb_free_config_descriptor(confptr
);
2009 if ((errcode
= libusb_control_transfer(printer
->handle
,
2010 LIBUSB_REQUEST_TYPE_CLASS
|
2011 LIBUSB_ENDPOINT_OUT
|
2012 LIBUSB_RECIPIENT_OTHER
,
2013 2, 0, interface
, NULL
, 0, 5000)) < 0)
2014 errcode
= libusb_control_transfer(printer
->handle
,
2015 LIBUSB_REQUEST_TYPE_CLASS
|
2016 LIBUSB_ENDPOINT_OUT
|
2017 LIBUSB_RECIPIENT_INTERFACE
,
2018 2, 0, interface
, NULL
, 0, 5000);