]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/usb-libusb.c
Merge pull request #5621 from zdohnal/cgigetarray-sigsegv
[thirdparty/cups.git] / backend / usb-libusb.c
index 4c71e6586cc36d3e05fa207c8d5c1da893b05e11..1c4d9f11721d41df3a762a3177ca3f0fcaea0a40 100644 (file)
@@ -1,33 +1,10 @@
 /*
- * "$Id$"
+ * LIBUSB interface code for CUPS.
  *
- *   LIBUSB interface code for CUPS.
+ * Copyright 2007-2019 by Apple Inc.
  *
- *   Copyright 2007-2013 by Apple Inc.
- *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- *   list_devices()      - List the available printers.
- *   print_device()      - Print a file to a USB device.
- *   close_device()      - Close the connection to the USB printer.
- *   find_device()       - Find or enumerate USB printers.
- *   get_device_id()     - Get the IEEE-1284 device ID for the printer.
- *   list_cb()           - List USB printers for discovery.
- *   make_device_uri()   - Create a device URI for a USB printer.
- *   open_device()       - Open a connection to the USB printer.
- *   print_cb()          - Find a USB printer for printing.
- *   printer_class_soft_reset()' - Do the soft reset request specific to
- *                          printers
- *   quirks()            - Get the known quirks of a given printer model
- *   read_thread()       - Thread to read the backchannel data on.
- *   sidechannel_thread() - Handle side-channel requests.
- *   soft_reset()        - Send a soft reset to the device.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
@@ -36,6 +13,8 @@
 
 #include <libusb.h>
 #include <cups/cups-private.h>
+#include <cups/ppd-private.h>
+#include <cups/dir.h>
 #include <pthread.h>
 #include <sys/select.h>
 #include <sys/types.h>
@@ -70,15 +49,15 @@ typedef struct usb_printer_s                /**** USB Printer Data ****/
                        read_endp,      /* Read endpoint */
                        protocol,       /* Protocol: 1 = Uni-di, 2 = Bi-di. */
                        usblp_attached, /* "usblp" kernel module attached? */
-                       reset_after_job; /* Set to 1 by print_device() */
-  unsigned int         quirks;         /* Quirks flags */
+                       reset_after_job;/* Set to 1 by print_device() */
+  unsigned             quirks;         /* Quirks flags */
   struct libusb_device_handle *handle; /* Open handle to device */
 } usb_printer_t;
 
 typedef int (*usb_cb_t)(usb_printer_t *, const char *, const char *,
                         const void *);
 
-typedef struct usb_globals_s
+typedef struct usb_globals_s           /* Global USB printer information */
 {
   usb_printer_t                *printer;       /* Printer */
 
@@ -105,127 +84,42 @@ typedef struct usb_globals_s
 } usb_globals_t;
 
 /*
- * Quirks: various printer quirks are handled by this table & its flags.
+ * Quirks: various printer quirks are handled by this structure and its flags.
  *
- * This is copied from the usblp kernel module. So we can easily copy and paste
- * new quirks from the module.
+ * The quirks table used to be compiled into the backend but is now loaded from
+ * one or more files in the /usr/share/cups/usb directory.
  */
 
-struct quirk_printer_struct {
-       int vendorId;
-       int productId;
-       unsigned int quirks;
-};
-
-#define USBLP_QUIRK_BIDIR      0x1     /* reports bidir but requires
-                                          unidirectional mode (no INs/reads) */
-#define USBLP_QUIRK_USB_INIT   0x2     /* needs vendor USB init string */
-#define USBLP_QUIRK_BAD_CLASS  0x4     /* descriptor uses vendor-specific
-                                          Class or SubClass */
-#define USBLP_QUIRK_BLACKLIST  0x8     /* these printers do not conform to the USB print spec */
-#define USBLP_QUIRK_RESET      0x4000  /* After printing do a reset
-                                          for clean-up */
-#define USBLP_QUIRK_NO_REATTACH        0x8000  /* After printing we cannot re-attach
+#define USB_QUIRK_BLACKLIST    0x0001  /* Does not conform to the spec */
+#define USB_QUIRK_NO_REATTACH  0x0002  /* After printing we cannot re-attach
                                           the usblp kernel module */
+#define USB_QUIRK_SOFT_RESET   0x0004  /* After printing do a soft reset
+                                          for clean-up */
+#define USB_QUIRK_UNIDIR       0x0008  /* Requires unidirectional mode */
+#define USB_QUIRK_USB_INIT     0x0010  /* Needs vendor USB init string */
+#define USB_QUIRK_VENDOR_CLASS 0x0020  /* Descriptor uses vendor-specific
+                                          Class or SubClass */
+#define USB_QUIRK_DELAY_CLOSE  0x0040  /* Delay close */
+#define USB_QUIRK_WHITELIST    0x0000  /* no quirks */
+
+
+typedef struct usb_quirk_s             /* USB "quirk" information */
+{
+  int          vendor_id,              /* Affected vendor ID */
+               product_id;             /* Affected product ID or 0 for all */
+  unsigned     quirks;                 /* Quirks bitfield */
+} usb_quirk_t;
+
 
-static const struct quirk_printer_struct quirk_printers[] = {
-       { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
-       { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
-       { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
-       { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
-       { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
-       { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
-       { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
-       { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
-       { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
-       { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
-       { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
-       { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
-       { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
-       { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820,
-                                                 by zut <kernel@zut.de> */
-       { 0x04a9, 0x10a2, USBLP_QUIRK_BIDIR }, /* Canon, Inc. PIXMA iP4200
-                           Printer, http://www.cups.org/str.php?L4155 */
-       { 0x04a9, 0x10b6, USBLP_QUIRK_BIDIR }, /* Canon, Inc. PIXMA iP4300
-                           Printer, https://bugs.launchpad.net/bugs/1032385 */
-       { 0x04a9, 0x1721, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP210
-                     https://bugzilla.redhat.com/show_bug.cgi?id=847923#c53 */
-       { 0x04a9, 0x170c, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP500
-                           Printer, https://bugs.launchpad.net/bugs/1032456 */
-       { 0x04a9, 0x1717, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP510
-                           Printer, https://bugs.launchpad.net/bugs/1050009 */
-       { 0x04a9, 0x173d, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP550
-                           Printer, http://www.cups.org/str.php?L4155 */
-       { 0x04a9, 0x173e, USBLP_QUIRK_BIDIR }, /* Canon, Inc. MP560
-                           Printer, http://www.cups.org/str.php?L4155 */
-       { 0x04f9, 0x001a, USBLP_QUIRK_NO_REATTACH }, /* Brother Industries, Ltd
-                                                 HL-1430 Laser Printer,
-                                    https://bugs.launchpad.net/bugs/1038695 */
-       { 0x04f9, 0x000d, USBLP_QUIRK_BIDIR |
-                         USBLP_QUIRK_NO_REATTACH }, /* Brother Industries, Ltd
-                                                 HL-1440 Laser Printer,
-                                    https://bugs.launchpad.net/bugs/1000253 */
-       { 0x06bc, 0x000b, USBLP_QUIRK_NO_REATTACH }, /* Oki Data Corp.
-                                                 Okipage 14ex Printer,
-                                    https://bugs.launchpad.net/bugs/872483 */
-       { 0x06bc, 0x01c7, USBLP_QUIRK_NO_REATTACH }, /* Oki Data Corp. B410d,
-                                    https://bugs.launchpad.net/bugs/872483 */
-       { 0x04b8, 0x0001, USBLP_QUIRK_BIDIR }, /* Seiko Epson Corp. Stylus Color 740 / Photo 750,
-                                    http://bugs.debian.org/697970 */
-       { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt
-                                                     Printer M129C */
-       { 0x067b, 0x2305, USBLP_QUIRK_BIDIR |
-                         USBLP_QUIRK_NO_REATTACH |
-                         USBLP_QUIRK_RESET },
-       { 0x0924, 0x3ce9, USBLP_QUIRK_NO_REATTACH }, /* Xerox Phaser 3124
-                         https://bugzilla.redhat.com/show_bug.cgi?id=867392 */
-       { 0x0924, 0x4293, USBLP_QUIRK_NO_REATTACH }, /* Xerox WorkCentre 3210
-                                    https://bugs.launchpad.net/bugs/1102470 */
-       /* Prolific Technology, Inc. PL2305 Parallel Port
-          (USB -> Parallel adapter), https://bugs.launchpad.net/bugs/987485 */
-       { 0x04e8, 0x0000, USBLP_QUIRK_RESET }, /* All Samsung devices,
-                                    https://bugs.launchpad.net/bugs/1032456 */
-       { 0x0a5f, 0x0000, USBLP_QUIRK_BIDIR }, /* All Zebra devices,
-                                    https://bugs.launchpad.net/bugs/1001028 */
-       /* Canon */
-       { 0x04a9, 0x304a, USBLP_QUIRK_BLACKLIST }, /* Canon CP-10 */
-       { 0x04a9, 0x3063, USBLP_QUIRK_BLACKLIST }, /* Canon CP-100 */
-       { 0x04a9, 0x307c, USBLP_QUIRK_BLACKLIST }, /* Canon CP-200 */
-       { 0x04a9, 0x307d, USBLP_QUIRK_BLACKLIST }, /* Canon CP-300 */
-       { 0x04a9, 0x30bd, USBLP_QUIRK_BLACKLIST }, /* Canon CP-220 */
-       { 0x04a9, 0x30be, USBLP_QUIRK_BLACKLIST }, /* Canon CP-330 */
-       { 0x04a9, 0x30f6, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP400 */
-       { 0x04a9, 0x310b, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP600 */
-       { 0x04a9, 0x3127, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP710 */
-       { 0x04a9, 0x3128, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP510 */
-       { 0x04a9, 0x3141, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES1 */
-       { 0x04a9, 0x3142, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP730 */
-       { 0x04a9, 0x3143, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP720 */
-       { 0x04a9, 0x3170, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP750 */
-       { 0x04a9, 0x3171, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP740 */
-       { 0x04a9, 0x3185, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES2 */
-       { 0x04a9, 0x3186, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES20 */
-       { 0x04a9, 0x31aa, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP770 */
-       { 0x04a9, 0x31ab, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP760 */
-       { 0x04a9, 0x31b0, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES30 */
-       { 0x04a9, 0x31dd, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP780 */
-       { 0x04a9, 0x31ee, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES40 */
-       { 0x04a9, 0x3214, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP800 */
-       { 0x04a9, 0x3255, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP900 */
-       { 0x04a9, 0x3256, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP810 */
-       { 0x04a9, 0x30F5, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY CP500 */
-       { 0x04a9, 0x31AF, USBLP_QUIRK_BLACKLIST }, /* Canon SELPHY ES3 */
-        /* MISSING PIDs: CP520, CP530, CP790 */
-       { 0, 0 }
-};
 
 
 /*
  * Globals...
  */
 
+cups_array_t           *all_quirks;    /* Array of printer quirks */
 usb_globals_t          g = { 0 };      /* Globals */
-libusb_device           **list;         /* List of connected USB devices */
+libusb_device          **all_list;     /* List of connected USB devices */
 
 
 /*
@@ -233,22 +127,24 @@ libusb_device           **list;         /* List of connected USB devices */
  */
 
 static int             close_device(usb_printer_t *printer);
+static int             compare_quirks(usb_quirk_t *a, usb_quirk_t *b);
 static usb_printer_t   *find_device(usb_cb_t cb, const void *data);
+static unsigned                find_quirks(int vendor_id, int product_id);
 static int             get_device_id(usb_printer_t *printer, char *buffer,
                                      size_t bufsize);
 static int             list_cb(usb_printer_t *printer, const char *device_uri,
                                const char *device_id, const void *data);
+static void            load_quirks(void);
 static char            *make_device_uri(usb_printer_t *printer,
                                         const char *device_id,
                                         char *uri, size_t uri_size);
 static int             open_device(usb_printer_t *printer, int verbose);
 static int             print_cb(usb_printer_t *printer, const char *device_uri,
                                 const char *device_id, const void *data);
-static int             printer_class_soft_reset(usb_printer_t *printer);
-static unsigned int    quirks(int vendor, int product);
 static void            *read_thread(void *reference);
 static void            *sidechannel_thread(void *reference);
 static void            soft_reset(void);
+static int             soft_reset_printer(usb_printer_t *printer);
 
 
 /*
@@ -258,6 +154,8 @@ static void         soft_reset(void);
 void
 list_devices(void)
 {
+  load_quirks();
+
   fputs("DEBUG: list_devices\n", stderr);
   find_device(list_cb, NULL);
 }
@@ -300,6 +198,8 @@ print_device(const char *uri,               /* I - Device URI */
   const char   *val;                   /* Option value */
 
 
+  load_quirks();
+
  /*
   * See if the side-channel descriptor is valid...
   */
@@ -325,9 +225,9 @@ print_device(const char *uri,               /* I - Device URI */
 
  /*
   * Some devices need a reset after finishing a job, these devices are
-  * marked with the USBLP_QUIRK_RESET quirk.
+  * marked with the USB_QUIRK_SOFT_RESET quirk.
   */
-  g.printer->reset_after_job = (g.printer->quirks & USBLP_QUIRK_RESET ? 1 : 0);
+  g.printer->reset_after_job = (g.printer->quirks & USB_QUIRK_SOFT_RESET ? 1 : 0);
 
  /*
   * If we are printing data from a print driver on stdin, ignore SIGTERM
@@ -573,7 +473,7 @@ print_device(const char *uri,               /* I - Device URI */
        iostatus = libusb_bulk_transfer(g.printer->handle,
                                        g.printer->write_endp,
                                        print_buffer, g.print_bytes,
-                                       &bytes, 60000);
+                                       &bytes, 0);
        /*
        * Ignore timeout errors, but retain the number of bytes written to
        * avoid sending duplicate data...
@@ -596,7 +496,7 @@ print_device(const char *uri,               /* I - Device URI */
          iostatus = libusb_bulk_transfer(g.printer->handle,
                                          g.printer->write_endp,
                                          print_buffer, g.print_bytes,
-                                         &bytes, 60000);
+                                         &bytes, 0);
        }
 
        /*
@@ -611,7 +511,7 @@ print_device(const char *uri,               /* I - Device URI */
          iostatus = libusb_bulk_transfer(g.printer->handle,
                                          g.printer->write_endp,
                                          print_buffer, g.print_bytes,
-                                         &bytes, 60000);
+                                         &bytes, 0);
         }
 
        if (iostatus)
@@ -738,13 +638,16 @@ print_device(const char *uri,             /* I - Device URI */
   * Close the connection and input file and general clean up...
   */
 
+  if (g.printer->quirks & USB_QUIRK_DELAY_CLOSE)
+    sleep(1);
+
   close_device(g.printer);
 
  /*
   * Clean up ....
   */
 
-  libusb_free_device_list(list, 1);
+  libusb_free_device_list(all_list, 1);
   libusb_exit(NULL);
 
   return (status);
@@ -863,6 +766,23 @@ close_device(usb_printer_t *printer)       /* I - Printer */
 }
 
 
+/*
+ * 'compare_quirks()' - Compare two quirks entries.
+ */
+
+static int                             /* O - Result of comparison */
+compare_quirks(usb_quirk_t *a,         /* I - First quirk entry */
+               usb_quirk_t *b)         /* I - Second quirk entry */
+{
+  int result;                          /* Result of comparison */
+
+  if ((result = b->vendor_id - a->vendor_id) == 0)
+    result = b->product_id - a->product_id;
+
+  return (result);
+}
+
+
 /*
  * 'find_device()' - Find or enumerate USB printers.
  */
@@ -883,7 +803,8 @@ find_device(usb_cb_t   cb,          /* I - Callback function */
                                        /* Pointer to current alternate setting */
   const struct libusb_endpoint_descriptor *endpptr = NULL;
                                        /* Pointer to current endpoint */
-  ssize_t               numdevs,        /* number of connected devices */
+  ssize_t               err = 0,       /* Error code */
+                        numdevs,        /* number of connected devices */
                         i = 0;
   uint8_t              conf,           /* Current configuration */
                        iface,          /* Current interface */
@@ -902,7 +823,13 @@ find_device(usb_cb_t   cb,         /* I - Callback function */
   * Initialize libusb...
   */
 
-  libusb_init(NULL);
+  err = libusb_init(NULL);
+  if (err)
+  {
+    fprintf(stderr, "ERROR: Unable to initialize USB access via libusb, libusb error %i (%s)\n", (int)err, libusb_strerror((int)err));
+    return (NULL);
+  }
+
   numdevs = libusb_get_device_list(NULL, &list);
   fprintf(stderr, "DEBUG: libusb_get_device_list=%d\n", (int)numdevs);
 
@@ -927,13 +854,13 @@ find_device(usb_cb_t   cb,                /* I - Callback function */
           !devdesc.idProduct)
        continue;
 
-      printer.quirks = quirks(devdesc.idVendor, devdesc.idProduct);
+      printer.quirks = find_quirks(devdesc.idVendor, devdesc.idProduct);
 
      /*
       * Ignore blacklisted printers...
       */
 
-      if (printer.quirks & USBLP_QUIRK_BLACKLIST)
+      if (printer.quirks & USB_QUIRK_BLACKLIST)
         continue;
 
       for (conf = 0; conf < devdesc.bNumConfigurations; conf ++)
@@ -962,18 +889,18 @@ find_device(usb_cb_t   cb,                /* I - Callback function */
 
            if (((altptr->bInterfaceClass != LIBUSB_CLASS_PRINTER ||
                  altptr->bInterfaceSubClass != 1) &&
-                ((printer.quirks & USBLP_QUIRK_BAD_CLASS) == 0)) ||
+                ((printer.quirks & USB_QUIRK_VENDOR_CLASS) == 0)) ||
                (altptr->bInterfaceProtocol != 1 &&     /* Unidirectional */
                 altptr->bInterfaceProtocol != 2) ||    /* Bidirectional */
                altptr->bInterfaceProtocol < protocol)
              continue;
 
-           if (printer.quirks & USBLP_QUIRK_BAD_CLASS)
+           if (printer.quirks & USB_QUIRK_VENDOR_CLASS)
              fprintf(stderr, "DEBUG: Printer does not report class 7 and/or "
                      "subclass 1 but works as a printer anyway\n");
 
-           read_endp  = -1;
-           write_endp = -1;
+           read_endp  = 0xff;
+           write_endp = 0xff;
 
            for (endp = 0, endpptr = altptr->endpoint;
                 endp < altptr->bNumEndpoints;
@@ -987,7 +914,7 @@ find_device(usb_cb_t   cb,          /* I - Callback function */
                  write_endp = endp;
              }
 
-            if (write_endp >= 0)
+            if (write_endp != 0xff)
            {
             /*
              * Save the best match so far...
@@ -1025,7 +952,7 @@ find_device(usb_cb_t   cb,         /* I - Callback function */
              {
                fprintf(stderr, "DEBUG: Device protocol: %d\n",
                        printer.protocol);
-               if (printer.quirks & USBLP_QUIRK_BIDIR)
+               if (printer.quirks & USB_QUIRK_UNIDIR)
                {
                  printer.read_endp = -1;
                  fprintf(stderr, "DEBUG: Printer reports bi-di support "
@@ -1045,7 +972,7 @@ find_device(usb_cb_t   cb,         /* I - Callback function */
                                           altsetting[printer.altset].
                                           endpoint[printer.write_endp].
                                           bEndpointAddress;
-               if (printer.quirks & USBLP_QUIRK_NO_REATTACH)
+               if (printer.quirks & USB_QUIRK_NO_REATTACH)
                {
                  printer.usblp_attached = 0;
                  fprintf(stderr, "DEBUG: Printer does not like usblp "
@@ -1072,13 +999,43 @@ find_device(usb_cb_t   cb,               /* I - Callback function */
   * Clean up ....
   */
 
-  libusb_free_device_list(list, 1);
+  if (numdevs >= 0)
+    libusb_free_device_list(list, 1);
   libusb_exit(NULL);
 
   return (NULL);
 }
 
 
+/*
+ * 'find_quirks()' - Find the quirks for the given printer, if any.
+ *
+ * First looks for an exact match, then looks for the vendor ID wildcard match.
+ */
+
+static unsigned                                /* O - Quirks flags */
+find_quirks(int vendor_id,             /* I - Vendor ID */
+            int product_id)            /* I - Product ID */
+{
+  usb_quirk_t  key,                    /* Search key */
+               *match;                 /* Matching quirk entry */
+
+
+  key.vendor_id  = vendor_id;
+  key.product_id = product_id;
+
+  if ((match = cupsArrayFind(all_quirks, &key)) != NULL)
+    return (match->quirks);
+
+  key.product_id = 0;
+
+  if ((match = cupsArrayFind(all_quirks, &key)) != NULL)
+    return (match->quirks);
+
+  return (USB_QUIRK_WHITELIST);
+}
+
+
 /*
  * 'get_device_id()' - Get the IEEE-1284 device ID for the printer.
  */
@@ -1107,8 +1064,7 @@ get_device_id(usb_printer_t *printer,     /* I - Printer */
   * bytes.  The 1284 spec says the length is stored MSB first...
   */
 
-  length = (((unsigned)buffer[0] & 255) << 8) |
-          ((unsigned)buffer[1] & 255);
+  length = (int)((((unsigned)buffer[0] & 255) << 8) | ((unsigned)buffer[1] & 255));
 
  /*
   * Check to see if the length is larger than our buffer or less than 14 bytes
@@ -1119,8 +1075,7 @@ get_device_id(usb_printer_t *printer,     /* I - Printer */
   */
 
   if (length > bufsize || length < 14)
-    length = (((unsigned)buffer[1] & 255) << 8) |
-            ((unsigned)buffer[0] & 255);
+    length = (int)((((unsigned)buffer[1] & 255) << 8) | ((unsigned)buffer[0] & 255));
 
   if (length > bufsize)
     length = bufsize;
@@ -1142,7 +1097,7 @@ get_device_id(usb_printer_t *printer,     /* I - Printer */
   * nul-terminate.
   */
 
-  memmove(buffer, buffer + 2, length);
+  memmove(buffer, buffer + 2, (size_t)length);
   buffer[length] = '\0';
 
   return (0);
@@ -1184,6 +1139,107 @@ list_cb(usb_printer_t *printer,         /* I - Printer */
 }
 
 
+/*
+ * 'load_quirks()' - Load all quirks files in the /usr/share/cups/usb directory.
+ */
+
+static void
+load_quirks(void)
+{
+  const char   *datadir;               /* CUPS_DATADIR environment variable */
+  char         filename[1024],         /* Filename */
+               line[1024];             /* Line from file */
+  cups_dir_t   *dir;                   /* Directory */
+  cups_dentry_t        *dent;                  /* Directory entry */
+  cups_file_t  *fp;                    /* Quirks file */
+  usb_quirk_t  *quirk;                 /* New quirk */
+
+
+  all_quirks = cupsArrayNew((cups_array_func_t)compare_quirks, NULL);
+
+  if ((datadir = getenv("CUPS_DATADIR")) == NULL)
+    datadir = CUPS_DATADIR;
+
+  snprintf(filename, sizeof(filename), "%s/usb", datadir);
+  if ((dir = cupsDirOpen(filename)) == NULL)
+  {
+    perror(filename);
+    return;
+  }
+
+  fprintf(stderr, "DEBUG: Loading USB quirks from \"%s\".\n", filename);
+
+  while ((dent = cupsDirRead(dir)) != NULL)
+  {
+    if (!S_ISREG(dent->fileinfo.st_mode))
+      continue;
+
+    snprintf(filename, sizeof(filename), "%s/usb/%s", datadir, dent->filename);
+    if ((fp = cupsFileOpen(filename, "r")) == NULL)
+    {
+      perror(filename);
+      continue;
+    }
+
+    while (cupsFileGets(fp, line, sizeof(line)))
+    {
+     /*
+      * Skip blank and comment lines...
+      */
+
+      if (line[0] == '#' || !line[0])
+        continue;
+
+     /*
+      * Add a quirk...
+      */
+
+      if ((quirk = calloc(1, sizeof(usb_quirk_t))) == NULL)
+      {
+        perror("DEBUG: Unable to allocate memory for quirk");
+        break;
+      }
+
+      if (sscanf(line, "%x%x", &quirk->vendor_id, &quirk->product_id) < 1)
+      {
+        fprintf(stderr, "DEBUG: Bad line: %s\n", line);
+        free(quirk);
+        continue;
+      }
+
+      if (strstr(line, " blacklist"))
+        quirk->quirks |= USB_QUIRK_BLACKLIST;
+
+      if (strstr(line, " delay-close"))
+        quirk->quirks |= USB_QUIRK_DELAY_CLOSE;
+
+      if (strstr(line, " no-reattach"))
+        quirk->quirks |= USB_QUIRK_NO_REATTACH;
+
+      if (strstr(line, " soft-reset"))
+        quirk->quirks |= USB_QUIRK_SOFT_RESET;
+
+      if (strstr(line, " unidir"))
+        quirk->quirks |= USB_QUIRK_UNIDIR;
+
+      if (strstr(line, " usb-init"))
+        quirk->quirks |= USB_QUIRK_USB_INIT;
+
+      if (strstr(line, " vendor-class"))
+        quirk->quirks |= USB_QUIRK_VENDOR_CLASS;
+
+      cupsArrayAdd(all_quirks, quirk);
+    }
+
+    cupsFileClose(fp);
+  }
+
+  fprintf(stderr, "DEBUG: Loaded %d quirks.\n", cupsArrayCount(all_quirks));
+
+  cupsDirClose(dir);
+}
+
+
 /*
  * 'make_device_uri()' - Create a device URI for a USB printer.
  */
@@ -1397,9 +1453,14 @@ open_device(usb_printer_t *printer,      /* I - Printer */
   else
   {
     printer->usblp_attached = 0;
-    fprintf(stderr, "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" kernel module attached\n",
-             devdesc.idVendor, devdesc.idProduct);
-    goto error;
+
+    if (errcode != LIBUSB_ERROR_NOT_SUPPORTED)
+    {
+      fprintf(stderr,
+              "DEBUG: Failed to check whether %04x:%04x has the \"usblp\" "
+              "kernel module attached\n", devdesc.idVendor, devdesc.idProduct);
+      goto error;
+    }
   }
 
  /*
@@ -1463,6 +1524,16 @@ open_device(usb_printer_t *printer,      /* I - Printer */
 
       goto error;
     }
+    else if ((errcode = libusb_detach_kernel_driver(printer->handle, printer->iface)) < 0)
+    {
+      fprintf(stderr,
+              "DEBUG: Failed to detach \"usblp\" module from %04x:%04x\n",
+              devdesc.idVendor, devdesc.idProduct);
+
+      goto error;
+    }
+
+    sleep (1);
   }
 
  /*
@@ -1610,65 +1681,6 @@ print_cb(usb_printer_t *printer, /* I - Printer */
 }
 
 
-/*
- * 'printer_class_soft_reset()' - Do the soft reset request specific to printers
- *
- * This soft reset is specific to the printer device class and is much less
- * invasive than the general USB reset libusb_reset_device(). Especially it
- * does never happen that the USB addressing and configuration changes. What
- * is actually done is that all buffers get flushed and the bulk IN and OUT
- * pipes get reset to their default states. This clears all stall conditions.
- * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
- */
-
-static int                             /* O - 0 on success, < 0 on error */
-printer_class_soft_reset(usb_printer_t *printer) /* I - Printer */
-{
-  struct libusb_config_descriptor *confptr = NULL;
-                                        /* Pointer to current configuration */
-  int interface,
-      errcode;
-
-  if (libusb_get_config_descriptor(printer->device, printer->conf, &confptr)
-      < 0)
-    interface = printer->iface;
-  else
-    interface = confptr->interface[printer->iface].
-      altsetting[printer->altset].bInterfaceNumber;
-  libusb_free_config_descriptor(confptr);
-  if ((errcode = libusb_control_transfer(printer->handle,
-                                        LIBUSB_REQUEST_TYPE_CLASS |
-                                        LIBUSB_ENDPOINT_OUT |
-                                        LIBUSB_RECIPIENT_OTHER,
-                                        2, 0, interface, NULL, 0, 5000)) < 0)
-    errcode = libusb_control_transfer(printer->handle,
-                                     LIBUSB_REQUEST_TYPE_CLASS |
-                                     LIBUSB_ENDPOINT_OUT |
-                                     LIBUSB_RECIPIENT_INTERFACE,
-                                     2, 0, interface, NULL, 0, 5000);
-  return errcode;
-}
-
-
-/*
- * 'quirks()' - Get the known quirks of a given printer model
- */
-
-static unsigned int quirks(int vendor, int product)
-{
-  int i;
-
-  for (i = 0; quirk_printers[i].vendorId; i++)
-  {
-    if (vendor == quirk_printers[i].vendorId &&
-       (quirk_printers[i].productId == 0x0000 ||
-        product == quirk_printers[i].productId))
-      return quirk_printers[i].quirks;
-  }
-  return 0;
-}
-
-
 /*
  * 'read_thread()' - Thread to read the backchannel data on.
  */
@@ -1717,7 +1729,7 @@ static void *read_thread(void *reference)
     {
       fprintf(stderr, "DEBUG: Read %d bytes of back-channel data...\n",
               (int)rbytes);
-      cupsBackChannelWrite((const char *)readbuffer, rbytes, 1.0);
+      cupsBackChannelWrite((const char *)readbuffer, (size_t)rbytes, 1.0);
     }
     else if (readstatus == LIBUSB_ERROR_TIMEOUT)
       fputs("DEBUG: Got USB transaction timeout during read.\n", stderr);
@@ -1730,8 +1742,7 @@ static void *read_thread(void *reference)
     * Make sure this loop executes no more than once every 250 miliseconds...
     */
 
-    if ((readstatus != LIBUSB_SUCCESS || rbytes == 0) &&
-        (g.wait_eof || !g.read_thread_stop))
+    if ((g.wait_eof || !g.read_thread_stop))
     {
       gettimeofday(&now, NULL);
       if (timercmp(&now, &end, <))
@@ -1892,13 +1903,15 @@ sidechannel_thread(void *reference)
  * 'soft_reset()' - Send a soft reset to the device.
  */
 
-static void soft_reset(void)
+static void
+soft_reset(void)
 {
   fd_set         input_set;            /* Input set for select() */
   struct timeval  tv;                  /* Time value */
   char           buffer[2048];         /* Buffer */
   struct timespec cond_timeout;                /* pthread condition timeout */
 
+
  /*
   * Send an abort once a second until the I/O lock is released by the main
   * thread...
@@ -1943,7 +1956,7 @@ static void soft_reset(void)
   * Send the reset...
   */
 
-  printer_class_soft_reset(g.printer);
+  soft_reset_printer(g.printer);
 
  /*
   * Release the I/O lock...
@@ -1957,6 +1970,45 @@ static void soft_reset(void)
 
 
 /*
- * End of "$Id$".
+ * 'soft_reset_printer()' - Do the soft reset request specific to printers
+ *
+ * This soft reset is specific to the printer device class and is much less
+ * invasive than the general USB reset libusb_reset_device(). Especially it
+ * does never happen that the USB addressing and configuration changes. What
+ * is actually done is that all buffers get flushed and the bulk IN and OUT
+ * pipes get reset to their default states. This clears all stall conditions.
+ * See http://cholla.mmto.org/computers/linux/usb/usbprint11.pdf
  */
 
+static int                             /* O - 0 on success, < 0 on error */
+soft_reset_printer(
+    usb_printer_t *printer)            /* I - Printer */
+{
+  struct libusb_config_descriptor *confptr = NULL;
+                                        /* Pointer to current configuration */
+  int interface,                       /* Interface to reset */
+      errcode;                         /* Error code */
+
+
+  if (libusb_get_config_descriptor(printer->device, printer->conf,
+                                   &confptr) < 0)
+    interface = printer->iface;
+  else
+    interface = confptr->interface[printer->iface].
+                         altsetting[printer->altset].bInterfaceNumber;
+
+  libusb_free_config_descriptor(confptr);
+
+  if ((errcode = libusb_control_transfer(printer->handle,
+                                        LIBUSB_REQUEST_TYPE_CLASS |
+                                        LIBUSB_ENDPOINT_OUT |
+                                        LIBUSB_RECIPIENT_OTHER,
+                                        2, 0, interface, NULL, 0, 5000)) < 0)
+    errcode = libusb_control_transfer(printer->handle,
+                                     LIBUSB_REQUEST_TYPE_CLASS |
+                                     LIBUSB_ENDPOINT_OUT |
+                                     LIBUSB_RECIPIENT_INTERFACE,
+                                     2, 0, interface, NULL, 0, 5000);
+
+  return (errcode);
+}