2 * LIBUSB interface code for CUPS.
4 * Copyright 2007-2019 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more
11 * Include necessary headers...
15 #include <cups/cups-private.h>
16 #include <cups/ppd-private.h>
19 #include <sys/select.h>
20 #include <sys/types.h>
27 * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
28 * the printer after we've finished sending all the data
32 #define WAIT_EOF_DELAY 7
33 #define WAIT_SIDE_DELAY 3
34 #define DEFAULT_TIMEOUT 5000L
41 typedef struct usb_printer_s
/**** USB Printer Data ****/
43 struct libusb_device
*device
; /* Device info */
44 int conf
, /* Configuration */
45 origconf
, /* Original configuration */
46 iface
, /* Interface */
47 altset
, /* Alternate setting */
48 write_endp
, /* Write endpoint */
49 read_endp
, /* Read endpoint */
50 protocol
, /* Protocol: 1 = Uni-di, 2 = Bi-di. */
51 usblp_attached
, /* "usblp" kernel module attached? */
52 reset_after_job
;/* Set to 1 by print_device() */
53 unsigned quirks
; /* Quirks flags */
54 struct libusb_device_handle
*handle
; /* Open handle to device */
57 typedef int (*usb_cb_t
)(usb_printer_t
*, const char *, const char *,
60 typedef struct usb_globals_s
/* Global USB printer information */
62 usb_printer_t
*printer
; /* Printer */
64 pthread_mutex_t read_thread_mutex
;
65 pthread_cond_t read_thread_cond
;
69 pthread_mutex_t readwrite_lock_mutex
;
70 pthread_cond_t readwrite_lock_cond
;
73 int print_fd
; /* File descriptor to print */
74 ssize_t print_bytes
; /* Print bytes read */
77 int drain_output
; /* Drain all pending output */
78 int bidi_flag
; /* 0=unidirectional, 1=bidirectional */
80 pthread_mutex_t sidechannel_thread_mutex
;
81 pthread_cond_t sidechannel_thread_cond
;
82 int sidechannel_thread_stop
;
83 int sidechannel_thread_done
;
87 * Quirks: various printer quirks are handled by this structure and its flags.
89 * The quirks table used to be compiled into the backend but is now loaded from
90 * one or more files in the /usr/share/cups/usb directory.
93 #define USB_QUIRK_BLACKLIST 0x0001 /* Does not conform to the spec */
94 #define USB_QUIRK_NO_REATTACH 0x0002 /* After printing we cannot re-attach
95 the usblp kernel module */
96 #define USB_QUIRK_SOFT_RESET 0x0004 /* After printing do a soft reset
98 #define USB_QUIRK_UNIDIR 0x0008 /* Requires unidirectional mode */
99 #define USB_QUIRK_USB_INIT 0x0010 /* Needs vendor USB init string */
100 #define USB_QUIRK_VENDOR_CLASS 0x0020 /* Descriptor uses vendor-specific
102 #define USB_QUIRK_DELAY_CLOSE 0x0040 /* Delay close */
103 #define USB_QUIRK_WHITELIST 0x0000 /* no quirks */
106 typedef struct usb_quirk_s
/* USB "quirk" information */
108 int vendor_id
, /* Affected vendor ID */
109 product_id
; /* Affected product ID or 0 for all */
110 unsigned quirks
; /* Quirks bitfield */
120 cups_array_t
*all_quirks
; /* Array of printer quirks */
121 usb_globals_t g
= { 0 }; /* Globals */
122 libusb_device
**all_list
; /* List of connected USB devices */
129 static int close_device(usb_printer_t
*printer
);
130 static int compare_quirks(usb_quirk_t
*a
, usb_quirk_t
*b
);
131 static usb_printer_t
*find_device(usb_cb_t cb
, const void *data
);
132 static unsigned find_quirks(int vendor_id
, int product_id
);
133 static int get_device_id(usb_printer_t
*printer
, char *buffer
,
135 static int list_cb(usb_printer_t
*printer
, const char *device_uri
,
136 const char *device_id
, const void *data
);
137 static void load_quirks(void);
138 static char *make_device_uri(usb_printer_t
*printer
,
139 const char *device_id
,
140 char *uri
, size_t uri_size
);
141 static int open_device(usb_printer_t
*printer
, int verbose
);
142 static int print_cb(usb_printer_t
*printer
, const char *device_uri
,
143 const char *device_id
, const void *data
);
144 static void *read_thread(void *reference
);
145 static void *sidechannel_thread(void *reference
);
146 static void soft_reset(void);
147 static int soft_reset_printer(usb_printer_t
*printer
);
151 * 'list_devices()' - List the available printers.
159 fputs("DEBUG: list_devices\n", stderr
);
160 find_device(list_cb
, NULL
);
165 * 'print_device()' - Print a file to a USB device.
168 int /* O - Exit status */
169 print_device(const char *uri
, /* I - Device URI */
170 const char *hostname
, /* I - Hostname/manufacturer */
171 const char *resource
, /* I - Resource/modelname */
172 char *options
, /* I - Device options/serial number */
173 int print_fd
, /* I - File descriptor to print */
174 int copies
, /* I - Copies to print */
175 int argc
, /* I - Number of command-line arguments (6 or 7) */
176 char *argv
[]) /* I - Command-line arguments */
178 int bytes
; /* Bytes written */
179 ssize_t total_bytes
; /* Total bytes written */
180 struct sigaction action
; /* Actions for POSIX signals */
181 int status
= CUPS_BACKEND_OK
,
182 /* Function results */
183 iostatus
; /* Current IO status */
184 pthread_t read_thread_id
, /* Read thread */
185 sidechannel_thread_id
; /* Side-channel thread */
186 int have_sidechannel
= 0, /* Was the side-channel thread started? */
187 have_backchannel
= 0; /* Do we have a back channel? */
188 struct stat sidechannel_info
; /* Side-channel file descriptor info */
189 unsigned char print_buffer
[8192], /* Print data buffer */
190 *print_ptr
; /* Pointer into print data buffer */
191 fd_set input_set
; /* Input set for select() */
192 int nfds
; /* Number of file descriptors */
193 struct timeval
*timeout
, /* Timeout pointer */
195 struct timespec cond_timeout
; /* pthread condition timeout */
196 int num_opts
; /* Number of options */
197 cups_option_t
*opts
; /* Options */
198 const char *val
; /* Option value */
204 * See if the side-channel descriptor is valid...
207 have_sidechannel
= !fstat(CUPS_SC_FD
, &sidechannel_info
) &&
208 S_ISSOCK(sidechannel_info
.st_mode
);
210 g
.wait_eof
= WAIT_EOF
;
213 * Connect to the printer...
216 fprintf(stderr
, "DEBUG: Printing on printer with URI: %s\n", uri
);
217 while ((g
.printer
= find_device(print_cb
, uri
)) == NULL
)
219 _cupsLangPrintFilter(stderr
, "INFO",
220 _("Waiting for printer to become available."));
224 g
.print_fd
= print_fd
;
227 * Some devices need a reset after finishing a job, these devices are
228 * marked with the USB_QUIRK_SOFT_RESET quirk.
230 g
.printer
->reset_after_job
= (g
.printer
->quirks
& USB_QUIRK_SOFT_RESET
? 1 : 0);
233 * If we are printing data from a print driver on stdin, ignore SIGTERM
234 * so that the driver can finish out any page data, e.g. to eject the
235 * current page. We only do this for stdin printing as otherwise there
236 * is no way to cancel a raw print job...
241 memset(&action
, 0, sizeof(action
));
243 sigemptyset(&action
.sa_mask
);
244 action
.sa_handler
= SIG_IGN
;
245 sigaction(SIGTERM
, &action
, NULL
);
249 * Start the side channel thread if the descriptor is valid...
252 pthread_mutex_init(&g
.readwrite_lock_mutex
, NULL
);
253 pthread_cond_init(&g
.readwrite_lock_cond
, NULL
);
254 g
.readwrite_lock
= 1;
256 if (have_sidechannel
)
258 g
.sidechannel_thread_stop
= 0;
259 g
.sidechannel_thread_done
= 0;
261 pthread_cond_init(&g
.sidechannel_thread_cond
, NULL
);
262 pthread_mutex_init(&g
.sidechannel_thread_mutex
, NULL
);
264 if (pthread_create(&sidechannel_thread_id
, NULL
, sidechannel_thread
, NULL
))
266 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
267 _cupsLangPrintFilter(stderr
, "ERROR",
268 _("There was an unrecoverable USB error."));
269 fputs("DEBUG: Couldn't create side-channel thread.\n", stderr
);
270 close_device(g
.printer
);
271 return (CUPS_BACKEND_STOP
);
276 * Debug mode: If option "usb-unidir" is given, always deactivate
280 num_opts
= cupsParseOptions(argv
[5], 0, &opts
);
281 val
= cupsGetOption("usb-unidir", num_opts
, opts
);
282 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
283 strcasecmp(val
, "false"))
285 g
.printer
->read_endp
= -1;
286 fprintf(stderr
, "DEBUG: Forced uni-directional communication "
287 "via \"usb-unidir\" option.\n");
291 * Debug mode: If option "usb-no-reattach" is given, do not re-attach
292 * the usblp kernel module after the job has completed.
295 val
= cupsGetOption("usb-no-reattach", num_opts
, opts
);
296 if (val
&& strcasecmp(val
, "no") && strcasecmp(val
, "off") &&
297 strcasecmp(val
, "false"))
299 g
.printer
->usblp_attached
= 0;
300 fprintf(stderr
, "DEBUG: Forced not re-attaching the usblp kernel module "
301 "after the job via \"usb-no-reattach\" option.\n");
305 * Get the read thread going...
308 if (g
.printer
->read_endp
!= -1)
310 have_backchannel
= 1;
312 g
.read_thread_stop
= 0;
313 g
.read_thread_done
= 0;
315 pthread_cond_init(&g
.read_thread_cond
, NULL
);
316 pthread_mutex_init(&g
.read_thread_mutex
, NULL
);
318 if (pthread_create(&read_thread_id
, NULL
, read_thread
, NULL
))
320 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
321 _cupsLangPrintFilter(stderr
, "ERROR",
322 _("There was an unrecoverable USB error."));
323 fputs("DEBUG: Couldn't create read thread.\n", stderr
);
324 close_device(g
.printer
);
325 return (CUPS_BACKEND_STOP
);
329 fprintf(stderr
, "DEBUG: Uni-directional device/mode, back channel "
333 * The main thread sends the print file...
339 print_ptr
= print_buffer
;
341 while (status
== CUPS_BACKEND_OK
&& copies
-- > 0)
343 _cupsLangPrintFilter(stderr
, "INFO", _("Sending data to printer."));
345 if (print_fd
!= STDIN_FILENO
)
347 fputs("PAGE: 1 1\n", stderr
);
348 lseek(print_fd
, 0, SEEK_SET
);
351 while (status
== CUPS_BACKEND_OK
)
356 FD_SET(print_fd
, &input_set
);
359 * Calculate select timeout...
360 * If we have data waiting to send timeout is 100ms.
361 * else if we're draining print_fd timeout is 0.
362 * else we're waiting forever...
368 tv
.tv_usec
= 100000; /* 100ms */
371 else if (g
.drain_output
)
381 * I/O is unlocked around select...
384 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
385 g
.readwrite_lock
= 0;
386 pthread_cond_signal(&g
.readwrite_lock_cond
);
387 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
389 nfds
= select(print_fd
+ 1, &input_set
, NULL
, NULL
, timeout
);
392 * Reacquire the lock...
395 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
396 while (g
.readwrite_lock
)
397 pthread_cond_wait(&g
.readwrite_lock_cond
, &g
.readwrite_lock_mutex
);
398 g
.readwrite_lock
= 1;
399 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
403 if (errno
== EINTR
&& total_bytes
== 0)
405 fputs("DEBUG: Received an interrupt before any bytes were "
406 "written, aborting.\n", stderr
);
407 close_device(g
.printer
);
408 return (CUPS_BACKEND_OK
);
410 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
412 _cupsLangPrintFilter(stderr
, "ERROR",
413 _("Unable to read print data."));
414 perror("DEBUG: select");
415 close_device(g
.printer
);
416 return (CUPS_BACKEND_FAILED
);
421 * If drain output has finished send a response...
424 if (g
.drain_output
&& !nfds
&& !g
.print_bytes
)
426 /* Send a response... */
427 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
432 * Check if we have print data ready...
435 if (FD_ISSET(print_fd
, &input_set
))
437 g
.print_bytes
= read(print_fd
, print_buffer
, sizeof(print_buffer
));
439 if (g
.print_bytes
< 0)
442 * Read error - bail if we don't see EAGAIN or EINTR...
445 if (errno
!= EAGAIN
&& errno
!= EINTR
)
447 _cupsLangPrintFilter(stderr
, "ERROR",
448 _("Unable to read print data."));
449 perror("DEBUG: read");
450 close_device(g
.printer
);
451 return (CUPS_BACKEND_FAILED
);
456 else if (g
.print_bytes
== 0)
459 * End of file, break out of the loop...
465 print_ptr
= print_buffer
;
467 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
473 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
474 g
.printer
->write_endp
,
475 print_buffer
, g
.print_bytes
,
478 * Ignore timeout errors, but retain the number of bytes written to
479 * avoid sending duplicate data...
482 if (iostatus
== LIBUSB_ERROR_TIMEOUT
)
484 fputs("DEBUG: Got USB transaction timeout during write.\n", stderr
);
489 * If we've stalled, retry the write...
492 else if (iostatus
== LIBUSB_ERROR_PIPE
)
494 fputs("DEBUG: Got USB pipe stalled during write.\n", stderr
);
496 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
497 g
.printer
->write_endp
,
498 print_buffer
, g
.print_bytes
,
503 * Retry a write after an aborted write since we probably just got
507 else if (iostatus
== LIBUSB_ERROR_INTERRUPTED
)
509 fputs("DEBUG: Got USB return aborted during write.\n", stderr
);
511 iostatus
= libusb_bulk_transfer(g
.printer
->handle
,
512 g
.printer
->write_endp
,
513 print_buffer
, g
.print_bytes
,
520 * Write error - bail if we don't see an error we can retry...
523 _cupsLangPrintFilter(stderr
, "ERROR",
524 _("Unable to send data to printer."));
525 fprintf(stderr
, "DEBUG: libusb write operation returned %x.\n",
528 status
= CUPS_BACKEND_FAILED
;
533 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n",
536 g
.print_bytes
-= bytes
;
538 total_bytes
+= bytes
;
542 if (print_fd
!= 0 && status
== CUPS_BACKEND_OK
)
543 fprintf(stderr
, "DEBUG: Sending print file, " CUPS_LLFMT
" bytes...\n",
544 CUPS_LLCAST total_bytes
);
548 fprintf(stderr
, "DEBUG: Sent " CUPS_LLFMT
" bytes...\n",
549 CUPS_LLCAST total_bytes
);
552 * Signal the side channel thread to exit...
555 if (have_sidechannel
)
558 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
559 g
.readwrite_lock
= 0;
560 pthread_cond_signal(&g
.readwrite_lock_cond
);
561 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
563 g
.sidechannel_thread_stop
= 1;
564 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
566 if (!g
.sidechannel_thread_done
)
568 gettimeofday(&tv
, NULL
);
569 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_SIDE_DELAY
;
570 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
572 while (!g
.sidechannel_thread_done
)
574 if (pthread_cond_timedwait(&g
.sidechannel_thread_cond
,
575 &g
.sidechannel_thread_mutex
,
581 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
585 * Signal the read thread to exit then wait 7 seconds for it to complete...
588 if (have_backchannel
)
590 g
.read_thread_stop
= 1;
592 pthread_mutex_lock(&g
.read_thread_mutex
);
594 if (!g
.read_thread_done
)
596 fputs("DEBUG: Waiting for read thread to exit...\n", stderr
);
598 gettimeofday(&tv
, NULL
);
599 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_EOF_DELAY
;
600 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
602 while (!g
.read_thread_done
)
604 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
610 * If it didn't exit abort the pending read and wait an additional
614 if (!g
.read_thread_done
)
616 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
621 gettimeofday(&tv
, NULL
);
622 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
623 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
625 while (!g
.read_thread_done
)
627 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
634 pthread_mutex_unlock(&g
.read_thread_mutex
);
638 * Close the connection and input file and general clean up...
641 if (g
.printer
->quirks
& USB_QUIRK_DELAY_CLOSE
)
644 close_device(g
.printer
);
650 libusb_free_device_list(all_list
, 1);
658 * 'close_device()' - Close the connection to the USB printer.
661 static int /* I - 0 on success, -1 on failure */
662 close_device(usb_printer_t
*printer
) /* I - Printer */
664 struct libusb_device_descriptor devdesc
;
665 /* Current device descriptor */
666 struct libusb_config_descriptor
*confptr
;
667 /* Pointer to current configuration */
673 * Release interfaces before closing so that we know all data is written
677 int errcode
; /* Return value of libusb function */
678 int number1
, /* Interface number */
679 number2
; /* Configuration number */
682 libusb_get_config_descriptor(printer
->device
, printer
->conf
, &confptr
);
685 number1
= confptr
->interface
[printer
->iface
].
686 altsetting
[printer
->altset
].bInterfaceNumber
;
687 libusb_release_interface(printer
->handle
, number1
);
689 number2
= confptr
->bConfigurationValue
;
691 libusb_free_config_descriptor(confptr
);
694 * If we have changed the configuration from one valid configuration
695 * to another, restore the old one
697 if (printer
->origconf
> 0 && printer
->origconf
!= number2
)
699 fprintf(stderr
, "DEBUG: Restoring USB device configuration: %d -> %d\n",
700 number2
, printer
->origconf
);
701 if ((errcode
= libusb_set_configuration(printer
->handle
,
702 printer
->origconf
)) < 0)
704 if (errcode
!= LIBUSB_ERROR_BUSY
)
707 libusb_get_device_descriptor (printer
->device
, &devdesc
);
710 "DEBUG: Failed to set configuration %d\n",
714 "DEBUG: Failed to set configuration %d for %04x:%04x\n",
715 printer
->origconf
, devdesc
.idVendor
, devdesc
.idProduct
);
721 * Re-attach "usblp" kernel module if it was attached before using this
724 if (printer
->usblp_attached
== 1)
725 if (libusb_attach_kernel_driver(printer
->handle
, number1
) < 0)
727 errcode
= libusb_get_device_descriptor (printer
->device
, &devdesc
);
730 "DEBUG: Failed to re-attach \"usblp\" kernel module\n");
733 "DEBUG: Failed to re-attach \"usblp\" kernel module to "
734 "%04x:%04x\n", devdesc
.idVendor
, devdesc
.idProduct
);
739 "DEBUG: Failed to get configuration descriptor %d\n",
743 * Reset the device to clean up after the job
746 if (printer
->reset_after_job
== 1)
748 if ((errcode
= libusb_reset_device(printer
->handle
)) < 0)
750 "DEBUG: Device reset failed, error code: %d\n",
754 "DEBUG: Resetting printer.\n");
758 * Close the interface and return...
761 libusb_close(printer
->handle
);
762 printer
->handle
= NULL
;
770 * 'compare_quirks()' - Compare two quirks entries.
773 static int /* O - Result of comparison */
774 compare_quirks(usb_quirk_t
*a
, /* I - First quirk entry */
775 usb_quirk_t
*b
) /* I - Second quirk entry */
777 int result
; /* Result of comparison */
779 if ((result
= b
->vendor_id
- a
->vendor_id
) == 0)
780 result
= b
->product_id
- a
->product_id
;
787 * 'find_device()' - Find or enumerate USB printers.
790 static usb_printer_t
* /* O - Found printer */
791 find_device(usb_cb_t cb
, /* I - Callback function */
792 const void *data
) /* I - User data for callback */
794 libusb_device
**list
; /* List of connected USB devices */
795 libusb_device
*device
= NULL
; /* Current device */
796 struct libusb_device_descriptor devdesc
;
797 /* Current device descriptor */
798 struct libusb_config_descriptor
*confptr
= NULL
;
799 /* Pointer to current configuration */
800 const struct libusb_interface
*ifaceptr
= NULL
;
801 /* Pointer to current interface */
802 const struct libusb_interface_descriptor
*altptr
= NULL
;
803 /* Pointer to current alternate setting */
804 const struct libusb_endpoint_descriptor
*endpptr
= NULL
;
805 /* Pointer to current endpoint */
806 ssize_t err
= 0, /* Error code */
807 numdevs
, /* number of connected devices */
809 uint8_t conf
, /* Current configuration */
810 iface
, /* Current interface */
811 altset
, /* Current alternate setting */
812 protocol
, /* Current protocol */
813 endp
, /* Current endpoint */
814 read_endp
, /* Current read endpoint */
815 write_endp
; /* Current write endpoint */
816 char device_id
[1024],/* IEEE-1284 device ID */
819 static usb_printer_t printer
; /* Current printer */
823 * Initialize libusb...
826 err
= libusb_init(NULL
);
829 fprintf(stderr
, "ERROR: Unable to initialize USB access via libusb, libusb error %i (%s)\n", (int)err
, libusb_strerror((int)err
));
833 numdevs
= libusb_get_device_list(NULL
, &list
);
834 fprintf(stderr
, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs
);
837 * Then loop through the devices it found...
841 for (i
= 0; i
< numdevs
; i
++)
846 * Ignore devices with no configuration data and anything that is not
850 if (libusb_get_device_descriptor(device
, &devdesc
) < 0)
853 if (!devdesc
.bNumConfigurations
|| !devdesc
.idVendor
||
857 printer
.quirks
= find_quirks(devdesc
.idVendor
, devdesc
.idProduct
);
860 * Ignore blacklisted printers...
863 if (printer
.quirks
& USB_QUIRK_BLACKLIST
)
866 for (conf
= 0; conf
< devdesc
.bNumConfigurations
; conf
++)
868 if (libusb_get_config_descriptor(device
, conf
, &confptr
) < 0)
870 for (iface
= 0, ifaceptr
= confptr
->interface
;
871 iface
< confptr
->bNumInterfaces
;
872 iface
++, ifaceptr
++)
875 * Some printers offer multiple interfaces...
880 for (altset
= 0, altptr
= ifaceptr
->altsetting
;
881 altset
< (uint8_t)ifaceptr
->num_altsetting
;
882 altset
++, altptr
++)
885 * Currently we only support unidirectional and bidirectional
886 * printers. Future versions of this code will support the
887 * 1284.4 (packet mode) protocol as well.
890 if (((altptr
->bInterfaceClass
!= LIBUSB_CLASS_PRINTER
||
891 altptr
->bInterfaceSubClass
!= 1) &&
892 ((printer
.quirks
& USB_QUIRK_VENDOR_CLASS
) == 0)) ||
893 (altptr
->bInterfaceProtocol
!= 1 && /* Unidirectional */
894 altptr
->bInterfaceProtocol
!= 2) || /* Bidirectional */
895 altptr
->bInterfaceProtocol
< protocol
)
898 if (printer
.quirks
& USB_QUIRK_VENDOR_CLASS
)
899 fprintf(stderr
, "DEBUG: Printer does not report class 7 and/or "
900 "subclass 1 but works as a printer anyway\n");
905 for (endp
= 0, endpptr
= altptr
->endpoint
;
906 endp
< altptr
->bNumEndpoints
;
908 if ((endpptr
->bmAttributes
& LIBUSB_TRANSFER_TYPE_MASK
) ==
909 LIBUSB_TRANSFER_TYPE_BULK
)
911 if (endpptr
->bEndpointAddress
& LIBUSB_ENDPOINT_DIR_MASK
)
917 if (write_endp
!= 0xff)
920 * Save the best match so far...
923 protocol
= altptr
->bInterfaceProtocol
;
924 printer
.altset
= altset
;
925 printer
.write_endp
= write_endp
;
927 printer
.read_endp
= read_endp
;
929 printer
.read_endp
= -1;
935 printer
.device
= device
;
937 printer
.iface
= iface
;
938 printer
.protocol
= protocol
;
939 printer
.handle
= NULL
;
941 if (!open_device(&printer
, data
!= NULL
))
943 get_device_id(&printer
, device_id
, sizeof(device_id
));
944 make_device_uri(&printer
, device_id
, device_uri
,
947 fprintf(stderr
, "DEBUG2: Printer found with device ID: %s "
949 device_id
, device_uri
);
951 if ((*cb
)(&printer
, device_uri
, device_id
, data
))
953 fprintf(stderr
, "DEBUG: Device protocol: %d\n",
955 if (printer
.quirks
& USB_QUIRK_UNIDIR
)
957 printer
.read_endp
= -1;
958 fprintf(stderr
, "DEBUG: Printer reports bi-di support "
959 "but in reality works only uni-directionally\n");
961 if (printer
.read_endp
!= -1)
963 printer
.read_endp
= confptr
->interface
[printer
.iface
].
964 altsetting
[printer
.altset
].
965 endpoint
[printer
.read_endp
].
969 fprintf(stderr
, "DEBUG: Uni-directional USB communication "
971 printer
.write_endp
= confptr
->interface
[printer
.iface
].
972 altsetting
[printer
.altset
].
973 endpoint
[printer
.write_endp
].
975 if (printer
.quirks
& USB_QUIRK_NO_REATTACH
)
977 printer
.usblp_attached
= 0;
978 fprintf(stderr
, "DEBUG: Printer does not like usblp "
979 "kernel module to be re-attached after job\n");
981 libusb_free_config_descriptor(confptr
);
985 close_device(&printer
);
989 libusb_free_config_descriptor(confptr
);
994 * If we get this far without returning, then we haven't found a printer
1003 libusb_free_device_list(list
, 1);
1011 * 'find_quirks()' - Find the quirks for the given printer, if any.
1013 * First looks for an exact match, then looks for the vendor ID wildcard match.
1016 static unsigned /* O - Quirks flags */
1017 find_quirks(int vendor_id
, /* I - Vendor ID */
1018 int product_id
) /* I - Product ID */
1020 usb_quirk_t key
, /* Search key */
1021 *match
; /* Matching quirk entry */
1024 key
.vendor_id
= vendor_id
;
1025 key
.product_id
= product_id
;
1027 if ((match
= cupsArrayFind(all_quirks
, &key
)) != NULL
)
1028 return (match
->quirks
);
1032 if ((match
= cupsArrayFind(all_quirks
, &key
)) != NULL
)
1033 return (match
->quirks
);
1035 return (USB_QUIRK_WHITELIST
);
1040 * 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
1043 static int /* O - 0 on success, -1 on error */
1044 get_device_id(usb_printer_t
*printer
, /* I - Printer */
1045 char *buffer
, /* I - String buffer */
1046 size_t bufsize
) /* I - Number of bytes in buffer */
1048 int length
; /* Length of device ID */
1051 if (libusb_control_transfer(printer
->handle
,
1052 LIBUSB_REQUEST_TYPE_CLASS
| LIBUSB_ENDPOINT_IN
|
1053 LIBUSB_RECIPIENT_INTERFACE
,
1055 (printer
->iface
<< 8) | printer
->altset
,
1056 (unsigned char *)buffer
, bufsize
, 5000) < 0)
1063 * Extract the length of the device ID string from the first two
1064 * bytes. The 1284 spec says the length is stored MSB first...
1067 length
= (int)((((unsigned)buffer
[0] & 255) << 8) | ((unsigned)buffer
[1] & 255));
1070 * Check to see if the length is larger than our buffer or less than 14 bytes
1071 * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length).
1073 * If the length is out-of-range, assume that the vendor incorrectly
1074 * implemented the 1284 spec and re-read the length as LSB first,..
1077 if (length
> bufsize
|| length
< 14)
1078 length
= (int)((((unsigned)buffer
[1] & 255) << 8) | ((unsigned)buffer
[0] & 255));
1080 if (length
> bufsize
)
1086 * Invalid device ID, clear it!
1096 * Copy the device ID text to the beginning of the buffer and
1100 memmove(buffer
, buffer
+ 2, (size_t)length
);
1101 buffer
[length
] = '\0';
1108 * 'list_cb()' - List USB printers for discovery.
1111 static int /* O - 0 to continue, 1 to stop */
1112 list_cb(usb_printer_t
*printer
, /* I - Printer */
1113 const char *device_uri
, /* I - Device URI */
1114 const char *device_id
, /* I - IEEE-1284 device ID */
1115 const void *data
) /* I - User data (not used) */
1117 char make_model
[1024]; /* Make and model */
1121 * Get the device URI and make/model strings...
1124 if (backendGetMakeModel(device_id
, make_model
, sizeof(make_model
)))
1125 strlcpy(make_model
, "Unknown", sizeof(make_model
));
1128 * Report the printer...
1131 cupsBackendReport("direct", device_uri
, make_model
, make_model
, device_id
,
1143 * 'load_quirks()' - Load all quirks files in the /usr/share/cups/usb directory.
1149 const char *datadir
; /* CUPS_DATADIR environment variable */
1150 char filename
[1024], /* Filename */
1151 line
[1024]; /* Line from file */
1152 cups_dir_t
*dir
; /* Directory */
1153 cups_dentry_t
*dent
; /* Directory entry */
1154 cups_file_t
*fp
; /* Quirks file */
1155 usb_quirk_t
*quirk
; /* New quirk */
1158 all_quirks
= cupsArrayNew((cups_array_func_t
)compare_quirks
, NULL
);
1160 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
1161 datadir
= CUPS_DATADIR
;
1163 snprintf(filename
, sizeof(filename
), "%s/usb", datadir
);
1164 if ((dir
= cupsDirOpen(filename
)) == NULL
)
1170 fprintf(stderr
, "DEBUG: Loading USB quirks from \"%s\".\n", filename
);
1172 while ((dent
= cupsDirRead(dir
)) != NULL
)
1174 if (!S_ISREG(dent
->fileinfo
.st_mode
))
1177 snprintf(filename
, sizeof(filename
), "%s/usb/%s", datadir
, dent
->filename
);
1178 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1184 while (cupsFileGets(fp
, line
, sizeof(line
)))
1187 * Skip blank and comment lines...
1190 if (line
[0] == '#' || !line
[0])
1197 if ((quirk
= calloc(1, sizeof(usb_quirk_t
))) == NULL
)
1199 perror("DEBUG: Unable to allocate memory for quirk");
1203 if (sscanf(line
, "%x%x", &quirk
->vendor_id
, &quirk
->product_id
) < 1)
1205 fprintf(stderr
, "DEBUG: Bad line: %s\n", line
);
1210 if (strstr(line
, " blacklist"))
1211 quirk
->quirks
|= USB_QUIRK_BLACKLIST
;
1213 if (strstr(line
, " delay-close"))
1214 quirk
->quirks
|= USB_QUIRK_DELAY_CLOSE
;
1216 if (strstr(line
, " no-reattach"))
1217 quirk
->quirks
|= USB_QUIRK_NO_REATTACH
;
1219 if (strstr(line
, " soft-reset"))
1220 quirk
->quirks
|= USB_QUIRK_SOFT_RESET
;
1222 if (strstr(line
, " unidir"))
1223 quirk
->quirks
|= USB_QUIRK_UNIDIR
;
1225 if (strstr(line
, " usb-init"))
1226 quirk
->quirks
|= USB_QUIRK_USB_INIT
;
1228 if (strstr(line
, " vendor-class"))
1229 quirk
->quirks
|= USB_QUIRK_VENDOR_CLASS
;
1231 cupsArrayAdd(all_quirks
, quirk
);
1237 fprintf(stderr
, "DEBUG: Loaded %d quirks.\n", cupsArrayCount(all_quirks
));
1244 * 'make_device_uri()' - Create a device URI for a USB printer.
1247 static char * /* O - Device URI */
1249 usb_printer_t
*printer
, /* I - Printer */
1250 const char *device_id
, /* I - IEEE-1284 device ID */
1251 char *uri
, /* I - Device URI buffer */
1252 size_t uri_size
) /* I - Size of device URI buffer */
1254 struct libusb_device_descriptor devdesc
;
1255 /* Current device descriptor */
1256 char options
[1024]; /* Device URI options */
1257 int num_values
; /* Number of 1284 parameters */
1258 cups_option_t
*values
; /* 1284 parameters */
1259 const char *mfg
, /* Manufacturer */
1261 *des
= NULL
, /* Description */
1262 *sern
; /* Serial number */
1263 size_t mfglen
; /* Length of manufacturer string */
1264 char tempmfg
[256], /* Temporary manufacturer string */
1265 tempsern
[256], /* Temporary serial number string */
1266 *tempptr
; /* Pointer into temp string */
1270 * Get the make, model, and serial numbers...
1273 num_values
= _cupsGet1284Values(device_id
, &values
);
1275 if ((sern
= cupsGetOption("SERIALNUMBER", num_values
, values
)) == NULL
)
1276 if ((sern
= cupsGetOption("SERN", num_values
, values
)) == NULL
)
1277 if ((sern
= cupsGetOption("SN", num_values
, values
)) == NULL
&&
1278 ((libusb_get_device_descriptor(printer
->device
, &devdesc
) >= 0) &&
1279 devdesc
.iSerialNumber
))
1282 * Try getting the serial number from the device itself...
1286 libusb_get_string_descriptor_ascii(printer
->handle
,
1287 devdesc
.iSerialNumber
,
1288 (unsigned char *)tempsern
,
1289 sizeof(tempsern
) - 1);
1292 tempsern
[length
] = '\0';
1297 if ((mfg
= cupsGetOption("MANUFACTURER", num_values
, values
)) == NULL
)
1298 mfg
= cupsGetOption("MFG", num_values
, values
);
1300 if ((mdl
= cupsGetOption("MODEL", num_values
, values
)) == NULL
)
1301 mdl
= cupsGetOption("MDL", num_values
, values
);
1304 * To maintain compatibility with the original character device backend on
1305 * Linux and *BSD, map manufacturer names...
1310 if (!_cups_strcasecmp(mfg
, "Hewlett-Packard"))
1312 else if (!_cups_strcasecmp(mfg
, "Lexmark International"))
1318 * No manufacturer? Use the model string or description...
1322 _ppdNormalizeMakeAndModel(mdl
, tempmfg
, sizeof(tempmfg
));
1323 else if ((des
= cupsGetOption("DESCRIPTION", num_values
, values
)) != NULL
||
1324 (des
= cupsGetOption("DES", num_values
, values
)) != NULL
)
1325 _ppdNormalizeMakeAndModel(des
, tempmfg
, sizeof(tempmfg
));
1327 strlcpy(tempmfg
, "Unknown", sizeof(tempmfg
));
1329 if ((tempptr
= strchr(tempmfg
, ' ')) != NULL
)
1338 * No model? Use description...
1341 mdl
= des
; /* We remove the manufacturer name below */
1342 else if (!strncasecmp(mfg
, "Unknown", 7))
1345 mdl
= "Unknown Model";
1348 mfglen
= strlen(mfg
);
1350 if (!strncasecmp(mdl
, mfg
, mfglen
) && _cups_isspace(mdl
[mfglen
]))
1354 while (_cups_isspace(*mdl
))
1359 * Generate the device URI from the manufacturer, model, serial number,
1360 * and interface number...
1365 if (printer
->iface
> 0)
1366 snprintf(options
, sizeof(options
), "?serial=%s&interface=%d", sern
,
1369 snprintf(options
, sizeof(options
), "?serial=%s", sern
);
1371 else if (printer
->iface
> 0)
1372 snprintf(options
, sizeof(options
), "?interface=%d", printer
->iface
);
1376 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, uri_size
, "usb", NULL
, mfg
, 0,
1377 "/%s%s", mdl
, options
);
1379 cupsFreeOptions(num_values
, values
);
1386 * 'open_device()' - Open a connection to the USB printer.
1389 static int /* O - 0 on success, -1 on error */
1390 open_device(usb_printer_t
*printer
, /* I - Printer */
1391 int verbose
) /* I - Update connecting-to-device state? */
1393 struct libusb_device_descriptor devdesc
;
1394 /* Current device descriptor */
1395 struct libusb_config_descriptor
*confptr
= NULL
;
1396 /* Pointer to current configuration */
1397 int number1
= -1, /* Configuration/interface/altset */
1398 number2
= -1, /* numbers */
1400 char current
; /* Current configuration */
1404 * Return immediately if we are already connected...
1407 if (printer
->handle
)
1411 * Try opening the printer...
1414 if ((errcode
= libusb_open(printer
->device
, &printer
->handle
)) < 0)
1416 fprintf(stderr
, "DEBUG: Failed to open device, code: %d\n",
1421 printer
->usblp_attached
= 0;
1422 printer
->reset_after_job
= 0;
1425 fputs("STATE: +connecting-to-device\n", stderr
);
1427 if ((errcode
= libusb_get_device_descriptor(printer
->device
, &devdesc
)) < 0)
1429 fprintf(stderr
, "DEBUG: Failed to get device descriptor, code: %d\n",
1435 * Get the "usblp" kernel module out of the way. This backend only
1436 * works without the module attached.
1439 errcode
= libusb_kernel_driver_active(printer
->handle
, printer
->iface
);
1441 printer
->usblp_attached
= 0;
1442 else if (errcode
== 1)
1444 printer
->usblp_attached
= 1;
1446 libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1448 fprintf(stderr
, "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1449 devdesc
.idVendor
, devdesc
.idProduct
);
1455 printer
->usblp_attached
= 0;
1457 if (errcode
!= LIBUSB_ERROR_NOT_SUPPORTED
)
1460 "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" "
1461 "kernel module attached\n", devdesc
.idVendor
, devdesc
.idProduct
);
1467 * Set the desired configuration, but only if it needs changing. Some
1468 * printers (e.g., Samsung) don't like libusb_set_configuration. It will
1469 * succeed, but the following print job is sometimes silently lost by the
1473 if (libusb_control_transfer(printer
->handle
,
1474 LIBUSB_REQUEST_TYPE_STANDARD
| LIBUSB_ENDPOINT_IN
|
1475 LIBUSB_RECIPIENT_DEVICE
,
1476 8, /* GET_CONFIGURATION */
1477 0, 0, (unsigned char *)¤t
, 1, 5000) < 0)
1478 current
= 0; /* Assume not configured */
1480 printer
->origconf
= current
;
1483 libusb_get_config_descriptor (printer
->device
, printer
->conf
, &confptr
))
1486 fprintf(stderr
, "DEBUG: Failed to get config descriptor for %04x:%04x\n",
1487 devdesc
.idVendor
, devdesc
.idProduct
);
1490 number1
= confptr
->bConfigurationValue
;
1492 if (number1
!= current
)
1494 fprintf(stderr
, "DEBUG: Switching USB device configuration: %d -> %d\n",
1496 if ((errcode
= libusb_set_configuration(printer
->handle
, number1
)) < 0)
1499 * If the set fails, chances are that the printer only supports a
1500 * single configuration. Technically these printers don't conform to
1501 * the USB printer specification, but otherwise they'll work...
1504 if (errcode
!= LIBUSB_ERROR_BUSY
)
1505 fprintf(stderr
, "DEBUG: Failed to set configuration %d for %04x:%04x\n",
1506 number1
, devdesc
.idVendor
, devdesc
.idProduct
);
1511 * Claim interfaces as needed...
1514 number1
= confptr
->interface
[printer
->iface
].
1515 altsetting
[printer
->altset
].bInterfaceNumber
;
1517 while ((errcode
= libusb_claim_interface(printer
->handle
, number1
)) < 0)
1519 if (errcode
!= LIBUSB_ERROR_BUSY
)
1522 "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
1523 number1
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1527 else if ((errcode
= libusb_detach_kernel_driver(printer
->handle
, printer
->iface
)) < 0)
1530 "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
1531 devdesc
.idVendor
, devdesc
.idProduct
);
1540 * Set alternate setting, but only if there is more than one option. Some
1541 * printers (e.g., Samsung) don't like usb_set_altinterface.
1544 if (confptr
->interface
[printer
->iface
].num_altsetting
> 1)
1546 number1
= confptr
->interface
[printer
->iface
].
1547 altsetting
[printer
->altset
].bInterfaceNumber
;
1548 number2
= confptr
->interface
[printer
->iface
].
1549 altsetting
[printer
->altset
].bAlternateSetting
;
1552 libusb_set_interface_alt_setting(printer
->handle
, number1
, number2
))
1555 if (errcode
!= LIBUSB_ERROR_BUSY
)
1558 "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
1560 number2
, devdesc
.idVendor
, devdesc
.idProduct
, strerror(errno
));
1567 libusb_free_config_descriptor(confptr
);
1570 fputs("STATE: -connecting-to-device\n", stderr
);
1575 * If we get here, there was a hard error...
1581 fputs("STATE: -connecting-to-device\n", stderr
);
1583 libusb_close(printer
->handle
);
1584 printer
->handle
= NULL
;
1591 * 'print_cb()' - Find a USB printer for printing.
1594 static int /* O - 0 to continue, 1 to stop (found) */
1595 print_cb(usb_printer_t
*printer
, /* I - Printer */
1596 const char *device_uri
, /* I - Device URI */
1597 const char *device_id
, /* I - IEEE-1284 device ID */
1598 const void *data
) /* I - User data (make, model, S/N) */
1600 char requested_uri
[1024], /* Requested URI */
1601 *requested_ptr
, /* Pointer into requested URI */
1602 detected_uri
[1024], /* Detected URI */
1603 *detected_ptr
; /* Pointer into detected URI */
1607 * If we have an exact match, stop now...
1610 if (!strcmp((char *)data
, device_uri
))
1614 * Work on copies of the URIs...
1617 strlcpy(requested_uri
, (char *)data
, sizeof(requested_uri
));
1618 strlcpy(detected_uri
, device_uri
, sizeof(detected_uri
));
1621 * libusb-discovered URIs can have an "interface" specification and this
1622 * never happens for usblp-discovered URIs, so remove the "interface"
1623 * specification from the URI which we are checking currently. This way a
1624 * queue for a usblp-discovered printer can now be accessed via libusb.
1626 * Similarly, strip "?serial=NNN...NNN" as needed.
1629 if ((requested_ptr
= strstr(requested_uri
, "?interface=")) == NULL
)
1630 requested_ptr
= strstr(requested_uri
, "&interface=");
1631 if ((detected_ptr
= strstr(detected_uri
, "?interface=")) == NULL
)
1632 detected_ptr
= strstr(detected_uri
, "&interface=");
1634 if (!requested_ptr
&& detected_ptr
)
1637 * Strip "[?&]interface=nnn" from the detected printer.
1640 *detected_ptr
= '\0';
1642 else if (requested_ptr
&& !detected_ptr
)
1645 * Strip "[?&]interface=nnn" from the requested printer.
1648 *requested_ptr
= '\0';
1651 if ((requested_ptr
= strstr(requested_uri
, "?serial=?")) != NULL
)
1654 * Strip "?serial=?" from the requested printer. This is a special
1655 * case, as "?serial=?" means no serial number and not the serial
1656 * number '?'. This is not covered by the checks below...
1659 *requested_ptr
= '\0';
1662 if ((requested_ptr
= strstr(requested_uri
, "?serial=")) == NULL
&&
1663 (detected_ptr
= strstr(detected_uri
, "?serial=")) != NULL
)
1666 * Strip "?serial=nnn" from the detected printer.
1669 *detected_ptr
= '\0';
1671 else if (requested_ptr
&& !detected_ptr
)
1674 * Strip "?serial=nnn" from the requested printer.
1677 *requested_ptr
= '\0';
1680 return (!strcmp(requested_uri
, detected_uri
));
1685 * 'read_thread()' - Thread to read the backchannel data on.
1688 static void *read_thread(void *reference
)
1690 unsigned char readbuffer
[512];
1702 * Read frequency: once every 250 milliseconds.
1706 delay
.tv_usec
= 250000;
1711 * Remember when we started so we can throttle the loop after the read
1715 gettimeofday(&now
, NULL
);
1718 * Calculate what 250 milliSeconds are in absolute time...
1721 timeradd(&now
, &delay
, &end
);
1723 rbytes
= sizeof(readbuffer
);
1724 readstatus
= libusb_bulk_transfer(g
.printer
->handle
,
1725 g
.printer
->read_endp
,
1728 if (readstatus
== LIBUSB_SUCCESS
&& rbytes
> 0)
1730 fprintf(stderr
, "DEBUG: Read %d bytes of back-channel data...\n",
1732 cupsBackChannelWrite((const char *)readbuffer
, (size_t)rbytes
, 1.0);
1734 else if (readstatus
== LIBUSB_ERROR_TIMEOUT
)
1735 fputs("DEBUG: Got USB transaction timeout during read.\n", stderr
);
1736 else if (readstatus
== LIBUSB_ERROR_PIPE
)
1737 fputs("DEBUG: Got USB pipe stalled during read.\n", stderr
);
1738 else if (readstatus
== LIBUSB_ERROR_INTERRUPTED
)
1739 fputs("DEBUG: Got USB return aborted during read.\n", stderr
);
1742 * Make sure this loop executes no more than once every 250 miliseconds...
1745 if ((g
.wait_eof
|| !g
.read_thread_stop
))
1747 gettimeofday(&now
, NULL
);
1748 if (timercmp(&now
, &end
, <))
1750 timersub(&end
, &now
, &timeleft
);
1751 usleep(1000000 * timeleft
.tv_sec
+ timeleft
.tv_usec
);
1754 } while (g
.wait_eof
|| !g
.read_thread_stop
);
1757 * Let the main thread know that we have completed the read thread...
1760 pthread_mutex_lock(&g
.read_thread_mutex
);
1761 g
.read_thread_done
= 1;
1762 pthread_cond_signal(&g
.read_thread_cond
);
1763 pthread_mutex_unlock(&g
.read_thread_mutex
);
1770 * 'sidechannel_thread()' - Handle side-channel requests.
1774 sidechannel_thread(void *reference
)
1776 cups_sc_command_t command
; /* Request command */
1777 cups_sc_status_t status
; /* Request/response status */
1778 char data
[2048]; /* Request/response data */
1779 int datalen
; /* Request/response data size */
1786 datalen
= sizeof(data
);
1788 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
1790 if (status
== CUPS_SC_STATUS_TIMEOUT
)
1798 case CUPS_SC_CMD_SOFT_RESET
: /* Do a soft reset */
1799 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1803 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
1804 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1808 case CUPS_SC_CMD_DRAIN_OUTPUT
: /* Drain all pending output */
1809 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1815 case CUPS_SC_CMD_GET_BIDI
: /* Is the connection bidirectional? */
1816 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1819 data
[0] = (g
.printer
->protocol
>= 2 ? 1 : 0);
1820 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1823 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1827 case CUPS_SC_CMD_GET_DEVICE_ID
: /* Return IEEE-1284 device ID */
1828 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1831 datalen
= sizeof(data
);
1832 if (get_device_id(g
.printer
, data
, sizeof(data
)))
1834 status
= CUPS_SC_STATUS_IO_ERROR
;
1839 status
= CUPS_SC_STATUS_OK
;
1840 datalen
= strlen(data
);
1842 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, datalen
, 1.0);
1844 if (datalen
< sizeof(data
))
1845 data
[datalen
] = '\0';
1847 data
[sizeof(data
) - 1] = '\0';
1850 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1854 case CUPS_SC_CMD_GET_STATE
: /* Return device state */
1855 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1858 data
[0] = CUPS_SC_STATE_ONLINE
;
1859 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1862 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1866 case CUPS_SC_CMD_GET_CONNECTED
: /* Return whether device is
1868 fputs("DEBUG: CUPS_SC_CMD_GET_CONNECTED received from driver...\n",
1871 data
[0] = (g
.printer
->handle
? 1 : 0);
1872 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1875 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1880 fprintf(stderr
, "DEBUG: Unknown side-channel command (%d) received "
1881 "from driver...\n", command
);
1883 cupsSideChannelWrite(command
, CUPS_SC_STATUS_NOT_IMPLEMENTED
,
1886 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1891 while (!g
.sidechannel_thread_stop
);
1893 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
1894 g
.sidechannel_thread_done
= 1;
1895 pthread_cond_signal(&g
.sidechannel_thread_cond
);
1896 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
1903 * 'soft_reset()' - Send a soft reset to the device.
1909 fd_set input_set
; /* Input set for select() */
1910 struct timeval tv
; /* Time value */
1911 char buffer
[2048]; /* Buffer */
1912 struct timespec cond_timeout
; /* pthread condition timeout */
1916 * Send an abort once a second until the I/O lock is released by the main
1920 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1921 while (g
.readwrite_lock
)
1923 gettimeofday(&tv
, NULL
);
1924 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
1925 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
1927 while (g
.readwrite_lock
)
1929 if (pthread_cond_timedwait(&g
.readwrite_lock_cond
,
1930 &g
.readwrite_lock_mutex
,
1931 &cond_timeout
) != 0)
1936 g
.readwrite_lock
= 1;
1937 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1940 * Flush bytes waiting on print_fd...
1945 FD_ZERO(&input_set
);
1946 FD_SET(g
.print_fd
, &input_set
);
1951 while (select(g
.print_fd
+1, &input_set
, NULL
, NULL
, &tv
) > 0)
1952 if (read(g
.print_fd
, buffer
, sizeof(buffer
)) <= 0)
1959 soft_reset_printer(g
.printer
);
1962 * Release the I/O lock...
1965 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
1966 g
.readwrite_lock
= 0;
1967 pthread_cond_signal(&g
.readwrite_lock_cond
);
1968 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
1973 * 'soft_reset_printer()' - Do the soft reset request specific to printers
1975 * This soft reset is specific to the printer device class and is much less
1976 * invasive than the general USB reset libusb_reset_device(). Especially it
1977 * does never happen that the USB addressing and configuration changes. What
1978 * is actually done is that all buffers get flushed and the bulk IN and OUT
1979 * pipes get reset to their default states. This clears all stall conditions.
1980 * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
1983 static int /* O - 0 on success, < 0 on error */
1985 usb_printer_t
*printer
) /* I - Printer */
1987 struct libusb_config_descriptor
*confptr
= NULL
;
1988 /* Pointer to current configuration */
1989 int interface
, /* Interface to reset */
1990 errcode
; /* Error code */
1993 if (libusb_get_config_descriptor(printer
->device
, printer
->conf
,
1995 interface
= printer
->iface
;
1997 interface
= confptr
->interface
[printer
->iface
].
1998 altsetting
[printer
->altset
].bInterfaceNumber
;
2000 libusb_free_config_descriptor(confptr
);
2002 if ((errcode
= libusb_control_transfer(printer
->handle
,
2003 LIBUSB_REQUEST_TYPE_CLASS
|
2004 LIBUSB_ENDPOINT_OUT
|
2005 LIBUSB_RECIPIENT_OTHER
,
2006 2, 0, interface
, NULL
, 0, 5000)) < 0)
2007 errcode
= libusb_control_transfer(printer
->handle
,
2008 LIBUSB_REQUEST_TYPE_CLASS
|
2009 LIBUSB_ENDPOINT_OUT
|
2010 LIBUSB_RECIPIENT_INTERFACE
,
2011 2, 0, interface
, NULL
, 0, 5000);