]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/usb-libusb.c
Merge changes from CUPS 1.6svn-r10510.
[thirdparty/cups.git] / backend / usb-libusb.c
index 103a0e30bbec861f14f9dccad2128ef6da5e2041..5811fde1f0dfb3dd6a244efc0a079608e0201e27 100644 (file)
@@ -537,10 +537,10 @@ print_device(const char *uri,             /* I - Device URI */
    /*
     * If it didn't exit abort the pending read and wait an additional second...
     */
-  
+
     if (!g.read_thread_done)
     {
-      fputs("DEBUG: Read thread still active, aborting the pending read...\n", 
+      fputs("DEBUG: Read thread still active, aborting the pending read...\n",
            stderr);
 
       g.wait_eof = 0;
@@ -548,7 +548,7 @@ print_device(const char *uri,               /* I - Device URI */
       gettimeofday(&tv, NULL);
       cond_timeout.tv_sec  = tv.tv_sec + 1;
       cond_timeout.tv_nsec = tv.tv_usec * 1000;
-  
+
       while (!g.read_thread_done)
       {
        if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
@@ -602,8 +602,8 @@ close_device(usb_printer_t *printer)        /* I - Printer */
 
     int number;                                /* Interface number */
 
-    libusb_get_device_descriptor (printer->device, &devdesc);
-    libusb_get_config_descriptor (printer->device, printer->conf, &confptr);
+    libusb_get_device_descriptor(printer->device, &devdesc);
+    libusb_get_config_descriptor(printer->device, printer->conf, &confptr);
     number = confptr->interface[printer->iface].
                  altsetting[printer->altset].bInterfaceNumber;
     libusb_release_interface(printer->handle, number);
@@ -694,7 +694,7 @@ find_device(usb_cb_t   cb,          /* I - Callback function */
       * a printer...
       */
 
-      libusb_get_device_descriptor (device, &devdesc);
+      libusb_get_device_descriptor(device, &devdesc);
 
       if (!devdesc.bNumConfigurations || !devdesc.idVendor ||
           !devdesc.idProduct)
@@ -702,7 +702,7 @@ find_device(usb_cb_t   cb,          /* I - Callback function */
 
       for (conf = 0; conf < devdesc.bNumConfigurations; conf ++)
       {
-       if (libusb_get_config_descriptor (device, conf, &confptr) < 0)
+       if (libusb_get_config_descriptor(device, conf, &confptr) < 0)
          continue;
         for (iface = 0, ifaceptr = confptr->interface;
             iface < confptr->bNumInterfaces;
@@ -950,7 +950,7 @@ make_device_uri(
   if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
     if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
       if ((sern = cupsGetOption("SN", num_values, values)) == NULL &&
-         ((libusb_get_device_descriptor (printer->device, &devdesc) >= 0) &&
+         ((libusb_get_device_descriptor(printer->device, &devdesc) >= 0) &&
           devdesc.iSerialNumber))
       {
        /*
@@ -1089,9 +1089,45 @@ open_device(usb_printer_t *printer,      /* I - Printer */
   if (libusb_open(printer->device, &printer->handle) < 0)
     return (-1);
 
+  printer->usblp_attached = 0;
+
   if (verbose)
     fputs("STATE: +connecting-to-device\n", stderr);
 
+  if ((errcode = libusb_get_device_descriptor (printer->device, &devdesc)) < 0)
+  {
+    fprintf(stderr, "DEBUG: Failed to get device descriptor, code: %d\n",
+           errcode);
+    goto error;
+  }
+
+ /*
+  * Get the "usblp" kernel module out of the way. This backend only
+  * works without the module attached.
+  */
+
+  errcode = libusb_kernel_driver_active(printer->handle, printer->iface);
+  if (errcode == 0)
+    printer->usblp_attached = 0;
+  else if (errcode == 1)
+  {
+    printer->usblp_attached = 1;
+    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;
+    }
+  }
+  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;
+  }
+
  /*
   * Set the desired configuration, but only if it needs changing. Some
   * printers (e.g., Samsung) don't like libusb_set_configuration. It will
@@ -1106,8 +1142,8 @@ open_device(usb_printer_t *printer,       /* I - Printer */
                0, 0, (unsigned char *)&current, 1, 5000) < 0)
     current = 0;                       /* Assume not configured */
 
-  libusb_get_device_descriptor (printer->device, &devdesc);
-  libusb_get_config_descriptor (printer->device, printer->conf, &confptr);
+  libusb_get_device_descriptor(printer->device, &devdesc);
+  libusb_get_config_descriptor(printer->device, printer->conf, &confptr);
   number1 = confptr->bConfigurationValue;
 
   if (number1 != current)
@@ -1148,9 +1184,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;
+    }
   }
 
  /*
@@ -1163,11 +1204,13 @@ open_device(usb_printer_t *printer,     /* I - Printer */
   while ((errcode = libusb_claim_interface(printer->handle, number1)) < 0)
   {
     if (errcode != LIBUSB_ERROR_BUSY)
+    {
       fprintf(stderr,
               "DEBUG: Failed to claim interface %d for %04x:%04x: %s\n",
               number1, devdesc.idVendor, devdesc.idProduct, strerror(errno));
 
-    goto error;
+      goto error;
+    }
   }
 
  /*
@@ -1187,12 +1230,14 @@ open_device(usb_printer_t *printer,     /* I - Printer */
           < 0)
     {
       if (errcode != LIBUSB_ERROR_BUSY)
+      {
         fprintf(stderr,
                 "DEBUG: Failed to set alternate interface %d for %04x:%04x: "
                 "%s\n",
                 number2, devdesc.idVendor, devdesc.idProduct, strerror(errno));
 
-      goto error;
+       goto error;
+      }
     }
   }