]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/usb-darwin.c
Merge pull request #5621 from zdohnal/cgigetarray-sigsegv
[thirdparty/cups.git] / backend / usb-darwin.c
index 2f00019979226a77737da60933a7b10ea64a3412..1c412da6f572144d7979329675864501dc1b6a87 100644 (file)
@@ -1,75 +1,44 @@
 /*
-* "$Id: usb-darwin.c 7953 2008-09-17 01:43:19Z mike $"
-*
-* Copyright 2005-2011 Apple Inc. All rights reserved.
-*
-* IMPORTANT:  This Apple software is supplied to you by Apple Computer,
-* Inc. ("Apple") in consideration of your agreement to the following
-* terms, and your use, installation, modification or redistribution of
-* this Apple software constitutes acceptance of these terms.  If you do
-* not agree with these terms, please do not use, install, modify or
-* redistribute this Apple software.
-*
-* In consideration of your agreement to abide by the following terms, and
-* subject to these terms, Apple grants you a personal, non-exclusive
-* license, under Apple's copyrights in this original Apple software (the
-* "Apple Software"), to use, reproduce, modify and redistribute the Apple
-* Software, with or without modifications, in source and/or binary forms;
-* provided that if you redistribute the Apple Software in its entirety and
-* without modifications, you must retain this notice and the following
-* text and disclaimers in all such redistributions of the Apple Software.
-* Neither the name, trademarks, service marks or logos of Apple Computer,
-* Inc. may be used to endorse or promote products derived from the Apple
-* Software without specific prior written permission from Apple.  Except
-* as expressly stated in this notice, no other rights or licenses, express
-* or implied, are granted by Apple herein, including but not limited to
-* any patent rights that may be infringed by your derivative works or by
-* other works in which the Apple Software may be incorporated.
-*
-* The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
-* MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
-* THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
-* FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
-* OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
-*
-* IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
-* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-* INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
-* MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
-* AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
-* STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
-* POSSIBILITY OF SUCH DAMAGE.
-*
-* Contents:
-*
- *   list_devices()       - List all USB devices.
- *   print_device()       - Print a file to a USB device.
- *   read_thread()        - Thread to read the backchannel data on.
- *   sidechannel_thread() - Handle side-channel requests.
- *   iterate_printers()   - Iterate over all the printers.
- *   device_added()       - Device added notifier.
- *   list_device_cb()     - list_device iterator callback.
- *   find_device_cb()     - print_device iterator callback.
- *   status_timer_cb()    - Status timer callback.
- *   copy_deviceinfo()    - Copy strings from the 1284 device ID.
- *   release_deviceinfo() - Release deviceinfo strings.
- *   load_classdriver()   - Load a classdriver.
- *   unload_classdriver() - Unload a classdriver.
- *   load_printerdriver() - Load vendor's classdriver.
- *   registry_open()      - Open a connection to the printer.
- *   registry_close()     - Close the connection to the printer.
- *   copy_deviceid()      - Copy the 1284 device id string.
- *   copy_devicestring()  - Copy the 1284 device id string.
- *   copy_value_for_key() - Copy value string associated with a key.
- *   cfstr_create_trim()  - Create CFString and trim whitespace characters.
- *   parse_options()      - Parse URI options.
- *   sigterm_handler()    - SIGTERM handler.
- *   next_line()          - Find the next line in a buffer.
- *   parse_pserror()      - Scan the backchannel data for postscript errors.
- *   soft_reset()         - Send a soft reset to the device.
- *   get_device_id()      - Return IEEE-1284 device ID.
-*/
+ * Copyright 2005-2016 Apple Inc. All rights reserved.
+ *
+ * IMPORTANT:  This Apple software is supplied to you by Apple Computer,
+ * Inc. ("Apple") in consideration of your agreement to the following
+ * terms, and your use, installation, modification or redistribution of
+ * this Apple software constitutes acceptance of these terms.  If you do
+ * not agree with these terms, please do not use, install, modify or
+ * redistribute this Apple software.
+ *
+ * In consideration of your agreement to abide by the following terms, and
+ * subject to these terms, Apple grants you a personal, non-exclusive
+ * license, under Apple's copyrights in this original Apple software (the
+ * "Apple Software"), to use, reproduce, modify and redistribute the Apple
+ * Software, with or without modifications, in source and/or binary forms;
+ * provided that if you redistribute the Apple Software in its entirety and
+ * without modifications, you must retain this notice and the following
+ * text and disclaimers in all such redistributions of the Apple Software.
+ * Neither the name, trademarks, service marks or logos of Apple Computer,
+ * Inc. may be used to endorse or promote products derived from the Apple
+ * Software without specific prior written permission from Apple.  Except
+ * as expressly stated in this notice, no other rights or licenses, express
+ * or implied, are granted by Apple herein, including but not limited to
+ * any patent rights that may be infringed by your derivative works or by
+ * other works in which the Apple Software may be incorporated.
+ *
+ * The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
+ * MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
+ * THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
+ * OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
+ *
+ * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
+ * MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
+ * AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
+ * STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
 
 /*
  * Include necessary headers.
 #include <cups/file-private.h>
 #include <cups/sidechannel.h>
 #include <cups/language-private.h>
+#include <cups/ppd-private.h>
 #include "backend-private.h"
 #include <CoreFoundation/CoreFoundation.h>
 #include <IOKit/usb/IOUSBLib.h>
 #include <IOKit/IOCFPlugIn.h>
-
+#include <libproc.h>
+#include <asl.h>
 #include <spawn.h>
 #include <pthread.h>
 
+/*
+ * Include necessary headers.
+ */
+
 extern char **environ;
 
 
@@ -119,7 +94,7 @@ extern char **environ;
 #define WAIT_SIDE_DELAY                        3
 #define DEFAULT_TIMEOUT                        5000L
 
-#define        USB_INTERFACE_KIND              CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID190)
+#define        USB_INTERFACE_KIND              CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID245)
 #define kUSBLanguageEnglish            0x409
 
 #define PRINTER_POLLING_INTERVAL       5                       /* seconds */
@@ -134,6 +109,26 @@ extern char **environ;
 #define kUSBGenericTOPrinterClassDriver        CFSTR("/System/Library/Printers/Libraries/USBGenericPrintingClass.plugin")
 #define kUSBPrinterClassDeviceNotOpen  -9664   /*kPMInvalidIOMContext*/
 
+#define CRSetCrashLogMessage(m) _crc_make_setter(message, m)
+#define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg))
+#define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden")))
+#define CRASHREPORTER_ANNOTATIONS_VERSION 4
+#define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
+
+struct crashreporter_annotations_t {
+       uint64_t version;               // unsigned long
+       uint64_t message;               // char *
+       uint64_t signature_string;      // char *
+       uint64_t backtrace;             // char *
+       uint64_t message2;              // char *
+       uint64_t thread;                // uint64_t
+       uint64_t dialog_mode;           // unsigned int
+};
+
+CRASH_REPORTER_CLIENT_HIDDEN
+struct crashreporter_annotations_t gCRAnnotations
+       __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
+       = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
 
 /*
  * Section 5.3 USB Printing Class spec
@@ -142,8 +137,9 @@ extern char **environ;
 #define kUSBPrintingProtocolNoOpen             0
 #define kUSBPrintingProtocolUnidirectional     1
 #define kUSBPrintingProtocolBidirectional      2
+#define kUSBPrintingProtocolIPP                        4
 
-typedef IOUSBInterfaceInterface190     **printer_interface_t;
+typedef IOUSBInterfaceInterface245     **printer_interface_t;
 
 typedef struct iodevice_request_s      /**** Device request ****/
 {
@@ -179,7 +175,7 @@ typedef struct classdriver_s                /**** g.classdriver context ****/
   UInt16               vendorID;               /* Vendor id */
   UInt16               productID;              /* Product id */
   printer_interface_t  interface;              /* identify the device to IOKit */
-  UInt8                        outpipe;                /* mandatory bulkOut pipe */
+  UInt8                        outpipe;                /* mandatory bulkOut pipe */
   UInt8                        inpipe;                 /* optional bulkIn pipe */
 
   /* general class requests */
@@ -206,7 +202,7 @@ typedef struct classdriver_s                /**** g.classdriver context ****/
 
 } classdriver_t;
 
-typedef Boolean (*iterator_callback_t)(void *refcon, io_service_t obj);
+typedef Boolean (*iterator_callback_t)(io_service_t obj, printer_interface_t printerIntf, void *refcon);
 
 typedef struct iterator_reference_s    /**** Iterator reference data */
 {
@@ -234,6 +230,8 @@ typedef struct globals_s
   CFStringRef          serial;
   UInt32               location;
   UInt8                        interfaceNum;
+  UInt8                        alternateSetting;
+  UInt8                 interfaceProtocol;
 
   CFRunLoopTimerRef    status_timer;
 
@@ -243,6 +241,7 @@ typedef struct globals_s
   ssize_t              debug_bytes;    /* Current bytes to read */
 #endif /* DEBUG_WRITES */
 
+  Boolean              use_generic_class_driver;
   Boolean              wait_eof;
   int                  drain_output;   /* Drain all pending output */
   int                  bidi_flag;      /* 0=unidirectional, 1=bidirectional */
@@ -259,46 +258,56 @@ typedef struct globals_s
  */
 
 globals_t g = { 0 };                   /* Globals */
+int Iterating = 0;                     /* Are we iterating the bus? */
 
 
 /*
  * Local functions...
  */
 
-static Boolean find_device_cb(void *refcon, io_service_t obj);
-static Boolean list_device_cb(void *refcon, io_service_t obj);
+static Boolean list_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon);
+static Boolean find_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon);
+
 static CFStringRef cfstr_create_trim(const char *cstr);
 static CFStringRef copy_value_for_key(CFStringRef deviceID, CFStringRef *keys);
-static kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t intf, classdriver_t ***printerDriver);
+static kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t interface, classdriver_t ***printerDriver);
 static kern_return_t load_printerdriver(CFStringRef *driverBundlePath);
 static kern_return_t registry_close(void);
 static kern_return_t registry_open(CFStringRef *driverBundlePath);
 static kern_return_t unload_classdriver(classdriver_t ***classdriver);
-static OSStatus copy_deviceid(classdriver_t **printer, CFStringRef *deviceID);
+
 static void *read_thread(void *reference);
 static void *sidechannel_thread(void *reference);
-static void copy_deviceinfo(CFStringRef deviceIDString, CFStringRef *make, CFStringRef *model, CFStringRef *serial);
-static void copy_devicestring(io_service_t usbInterface, CFStringRef *deviceID, UInt32 *deviceLocation, UInt8 *interfaceNum);
 static void device_added(void *userdata, io_iterator_t iterator);
 static void get_device_id(cups_sc_status_t *status, char *data, int *datalen);
 static void iterate_printers(iterator_callback_t callBack, void *userdata);
 static void parse_options(char *options, char *serial, int serial_size, UInt32 *location, Boolean *wait_eof);
-static void release_deviceinfo(CFStringRef *make, CFStringRef *model, CFStringRef *serial);
 static void setup_cfLanguage(void);
 static void soft_reset(void);
 static void status_timer_cb(CFRunLoopTimerRef timer, void *info);
+#define IS_64BIT 1
+#define IS_NOT_64BIT 0
 
 #if defined(__i386__) || defined(__x86_64__)
 static pid_t   child_pid;              /* Child PID */
-static void run_legacy_backend(int argc, char *argv[], int fd);        /* Starts child backend process running as a ppc executable */
+static void run_legacy_backend(int argc, char *argv[], int fd) _CUPS_NORETURN; /* Starts child backend process running as a ppc executable */
 #endif /* __i386__ || __x86_64__ */
 static void sigterm_handler(int sig);  /* SIGTERM handler */
+static void sigquit_handler(int sig, siginfo_t *si, void *unused) _CUPS_NORETURN;
 
 #ifdef PARSE_PS_ERRORS
 static const char *next_line (const char *buffer);
 static void parse_pserror (char *sockBuffer, int len);
 #endif /* PARSE_PS_ERRORS */
 
+static printer_interface_t usb_printer_interface_interface(io_service_t usbClass);
+
+static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting);
+static CFStringRef copy_printer_interface_indexed_description(printer_interface_t  printer, UInt8 index, UInt16 language);
+static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID);
+static CFStringRef deviceIDCopyModel(CFStringRef deviceID);
+static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID);
+
 #pragma mark -
 
 /*
@@ -344,7 +353,19 @@ print_device(const char *uri,              /* I - Device URI */
   struct timeval  *timeout,            /* Timeout pointer */
                  tv;                   /* Time value */
   struct timespec cond_timeout;                /* pthread condition timeout */
+  struct sigaction action;             /* Actions for POSIX signals */
+
+
+  (void)uri;
+
+ /*
+  * Catch SIGQUIT to determine who is sending it...
+  */
 
+  memset(&action, 0, sizeof(action));
+  action.sa_sigaction = sigquit_handler;
+  action.sa_flags = SA_SIGINFO;
+  sigaction(SIGQUIT, &action, NULL);
 
  /*
   * See if the side-channel descriptor is valid...
@@ -397,10 +418,17 @@ print_device(const char *uri,             /* I - Device URI */
       g.printer_obj = 0x0;
       g.classdriver = 0x0;
     }
-
     fprintf(stderr, "DEBUG: Looking for '%s %s'\n", hostname, resource);
 
-    iterate_printers(find_device_cb, NULL);
+    do
+    {
+      iterate_printers(find_device_cb, NULL);
+      if (g.printer_obj != 0x0)
+        break;
+
+      _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to become available."));
+      sleep(5);
+    } while (true);
 
     fputs("DEBUG: Opening connection\n", stderr);
 
@@ -470,9 +498,6 @@ print_device(const char *uri,               /* I - Device URI */
 
   if (!print_fd)
   {
-    struct sigaction   action;         /* POSIX signal action */
-
-
     memset(&action, 0, sizeof(action));
 
     sigemptyset(&action.sa_mask);
@@ -677,12 +702,12 @@ print_device(const char *uri,             /* I - Device URI */
 
       if (g.print_bytes)
       {
-       bytes    = g.print_bytes;
+       bytes    = (UInt32)g.print_bytes;
        iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
 
        /*
        * Ignore timeout errors, but retain the number of bytes written to
-       * avoid sending duplicate data (<rdar://problem/6254911>)...
+       * avoid sending duplicate data...
        */
 
        if (iostatus == kIOUSBTransactionTimeout)
@@ -699,13 +724,13 @@ print_device(const char *uri,             /* I - Device URI */
        {
          fputs("DEBUG: Got USB pipe stalled during write\n", stderr);
 
-         bytes    = g.print_bytes;
+         bytes    = (UInt32)g.print_bytes;
          iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
        }
 
        /*
        * Retry a write after an aborted write since we probably just got
-       * SIGTERM (<rdar://problem/6860126>)...
+       * SIGTERM...
        */
 
        else if (iostatus == kIOReturnAborted)
@@ -719,7 +744,7 @@ print_device(const char *uri,               /* I - Device URI */
           sleep(5);
 #endif /* DEBUG_WRITES */
 
-         bytes    = g.print_bytes;
+         bytes    = (UInt32)g.print_bytes;
          iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
         }
 
@@ -758,25 +783,10 @@ print_device(const char *uri,             /* I - Device URI */
   }
 
   fprintf(stderr, "DEBUG: Sent %lld bytes...\n", (off_t)total_bytes);
-
-  if (!print_fd)
-  {
-   /*
-    * Re-enable the SIGTERM handler so pthread_kill() will work...
-    */
-
-    struct sigaction   action;         /* POSIX signal action */
-
-    memset(&action, 0, sizeof(action));
-
-    sigemptyset(&action.sa_mask);
-    sigaddset(&action.sa_mask, SIGTERM);
-    action.sa_handler = sigterm_handler;
-    sigaction(SIGTERM, &action, NULL);
-  }
+  fputs("STATE: +cups-waiting-for-job-completed\n", stderr);
 
  /*
-  * Wait for the side channel thread to exit...
+  * Signal the side channel thread to exit...
   */
 
   if (have_sidechannel)
@@ -803,44 +813,23 @@ print_device(const char *uri,             /* I - Device URI */
                                   &cond_timeout) != 0)
          break;
       }
-
-      if (!g.sidechannel_thread_done)
-      {
-       /*
-       * Force the side-channel thread to exit...
-       */
-
-       fputs("DEBUG: Force the side-channel thread to exit...\n", stderr);
-       pthread_kill(sidechannel_thread_id, SIGTERM);
-      }
     }
 
     pthread_mutex_unlock(&g.sidechannel_thread_mutex);
-
-    pthread_join(sidechannel_thread_id, NULL);
-
-    pthread_cond_destroy(&g.sidechannel_thread_cond);
-    pthread_mutex_destroy(&g.sidechannel_thread_mutex);
   }
 
-  pthread_cond_destroy(&g.readwrite_lock_cond);
-  pthread_mutex_destroy(&g.readwrite_lock_mutex);
-
  /*
-  * Signal the read thread to stop...
+  * Signal the read thread to exit then wait 7 seconds for it to complete...
   */
 
   g.read_thread_stop = 1;
 
- /*
-  * Give the read thread WAIT_EOF_DELAY seconds to complete all the data. If
-  * we are not signaled in that time then force the thread to exit.
-  */
-
   pthread_mutex_lock(&g.read_thread_mutex);
 
   if (!g.read_thread_done)
   {
+    fputs("DEBUG: Waiting for read thread to exit...\n", stderr);
+
     gettimeofday(&tv, NULL);
     cond_timeout.tv_sec  = tv.tv_sec + WAIT_EOF_DELAY;
     cond_timeout.tv_nsec = tv.tv_usec * 1000;
@@ -852,25 +841,34 @@ print_device(const char *uri,             /* I - Device URI */
        break;
     }
 
+   /*
+    * If it didn't exit abort the pending read and wait an additional second...
+    */
+
     if (!g.read_thread_done)
     {
-     /*
-      * Force the read thread to exit...
-      */
+      fputs("DEBUG: Read thread still active, aborting the pending read...\n",
+           stderr);
 
       g.wait_eof = 0;
-      fputs("DEBUG: Force the read thread to exit...\n", stderr);
-      pthread_kill(read_thread_id, SIGTERM);
+
+      (*g.classdriver)->Abort(g.classdriver);
+
+      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,
+                                  &cond_timeout) != 0)
+         break;
+      }
     }
   }
 
   pthread_mutex_unlock(&g.read_thread_mutex);
 
-  pthread_join(read_thread_id, NULL);  /* wait for the read thread to return */
-
-  pthread_cond_destroy(&g.read_thread_cond);
-  pthread_mutex_destroy(&g.read_thread_mutex);
-
  /*
   * Close the connection and input file and general clean up...
   */
@@ -909,6 +907,9 @@ static void *read_thread(void *reference)
   uint64_t                     start,
                                delay;
 
+
+  (void)reference;
+
   /* Calculate what 250 milliSeconds are in mach absolute time...
    */
   mach_timebase_info(&timeBaseInfo);
@@ -958,6 +959,27 @@ static void *read_thread(void *reference)
 
   } while (g.wait_eof || !g.read_thread_stop); /* Abort from main thread tests error here */
 
+  /* Workaround for usb race condition. <rdar://problem/21882551> */
+  if (!g.wait_eof && g.use_generic_class_driver)
+  {
+     const char *pdl = getenv("FINAL_CONTENT_TYPE");
+     if (pdl && strcmp(pdl, "application/vnd.cups-postscript") == 0)
+     {
+       while (readstatus == kIOReturnSuccess && ((rbytes > 0 && readbuffer[rbytes-1] != 0x4) || rbytes == 0))
+       {
+         start = mach_absolute_time();
+
+         rbytes = sizeof(readbuffer);
+         readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes);
+         if (readstatus == kIOReturnSuccess && rbytes > 0 && readbuffer[rbytes-1] == 0x4)
+           break;
+
+         /* Make sure this loop executes no more than once every 250 miliseconds... */
+         mach_wait_until(start + delay);
+       }
+     }
+  }
+
  /*
   * Let the main thread know that we have completed the read thread...
   */
@@ -984,6 +1006,8 @@ sidechannel_thread(void *reference)
   int                  datalen;        /* Request/response data size */
 
 
+  (void)reference;
+
   do
   {
     datalen = sizeof(data);
@@ -1029,7 +1053,7 @@ sidechannel_thread(void *reference)
          fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
                stderr);
 
-         data[0] = g.bidi_flag;
+         data[0] = (char)g.bidi_flag;
          cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
 
          fprintf(stderr,
@@ -1045,7 +1069,7 @@ sidechannel_thread(void *reference)
          get_device_id(&status, data, &datalen);
          cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0);
 
-          if (datalen < sizeof(data))
+          if ((size_t)datalen < sizeof(data))
            data[datalen] = '\0';
          else
            data[sizeof(data) - 1] = '\0';
@@ -1094,206 +1118,220 @@ sidechannel_thread(void *reference)
 /*
  * 'iterate_printers()' - Iterate over all the printers.
  */
-
-static void iterate_printers(iterator_callback_t callBack,
-                            void *userdata)
+static void iterate_printers(iterator_callback_t callBack, void *userdata)
 {
-  mach_port_t  masterPort = 0x0;
-  kern_return_t kr = IOMasterPort (bootstrap_port, &masterPort);
+  Iterating = 1;
 
-  if (kr == kIOReturnSuccess && masterPort != 0x0)
-  {
-    io_iterator_t addIterator = 0x0;
+  iterator_reference_t reference = { callBack, userdata, true };
 
-    iterator_reference_t reference = { callBack, userdata, true };
-    IONotificationPortRef addNotification = IONotificationPortCreate(masterPort);
+  IONotificationPortRef addNotification = IONotificationPortCreate(kIOMasterPortDefault);
 
-    int klass = kUSBPrintingClass;
-    int subklass = kUSBPrintingSubclass;
+  int printingClass = kUSBPrintingClass;
+  int printingSubclass = kUSBPrintingSubclass;
 
-    CFNumberRef usb_klass = CFNumberCreate(NULL, kCFNumberIntType, &klass);
-    CFNumberRef usb_subklass = CFNumberCreate(NULL, kCFNumberIntType, &subklass);
-    CFMutableDictionaryRef usbPrinterMatchDictionary = IOServiceMatching(kIOUSBInterfaceClassName);
+  CFNumberRef interfaceClass = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &printingClass);
+  CFNumberRef interfaceSubClass = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &printingSubclass);
 
-    CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceClass"), usb_klass);
-    CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceSubClass"), usb_subklass);
+  CFMutableDictionaryRef usbPrinterMatchDictionary = IOServiceMatching(kIOUSBInterfaceClassName);
+  CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceClass"), interfaceClass);
+  CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceSubClass"), interfaceSubClass);
 
-    CFRelease(usb_klass);
-    CFRelease(usb_subklass);
+  CFRelease(interfaceClass);
+  CFRelease(interfaceSubClass);
 
-    kr = IOServiceAddMatchingNotification(addNotification, kIOMatchedNotification, usbPrinterMatchDictionary, &device_added, &reference, &addIterator);
-    if (addIterator != 0x0)
+  io_iterator_t add_iterator = IO_OBJECT_NULL;
+  IOServiceAddMatchingNotification(addNotification, kIOMatchedNotification,
+                usbPrinterMatchDictionary, &device_added, &reference, &add_iterator);
+  if (add_iterator != IO_OBJECT_NULL)
+  {
+    device_added (&reference, add_iterator);
+    if (reference.keepRunning)
     {
-      device_added (&reference, addIterator);
-
-      if (reference.keepRunning)
-      {
-       CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification), kCFRunLoopDefaultMode);
-       CFRunLoopRun();
-      }
-      IOObjectRelease(addIterator);
+      CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification), kCFRunLoopDefaultMode);
+      CFRunLoopRun();
     }
-    mach_port_deallocate(mach_task_self(), masterPort);
+    IOObjectRelease(add_iterator);
   }
+  Iterating = 0;
 }
 
 
 /*
  * 'device_added()' - Device added notifier.
  */
-
-static void device_added(void *userdata,
-                        io_iterator_t iterator)
+static void device_added(void *userdata, io_iterator_t iterator)
 {
   iterator_reference_t *reference = userdata;
+  io_service_t intf;
 
-  io_service_t obj;
-  while (reference->keepRunning && (obj = IOIteratorNext(iterator)) != 0x0)
+  while (reference->keepRunning && (intf = IOIteratorNext(iterator)) != 0x0)
   {
-    if (reference->callback != NULL)
-      reference->keepRunning = reference->callback(reference->userdata, obj);
+    printer_interface_t printerIntf = usb_printer_interface_interface(intf);
+    if (printerIntf != NULL)
+    {
+      UInt8 intfClass = 0, intfSubClass = 0;
 
-    IOObjectRelease(obj);
-  }
+      (*printerIntf)->GetInterfaceClass(printerIntf, &intfClass);
+      (*printerIntf)->GetInterfaceSubClass(printerIntf, &intfSubClass);
+      if (intfClass == kUSBPrintingInterfaceClass && intfSubClass == kUSBPrintingSubclass)
+        reference->keepRunning = reference->callback(intf, printerIntf, userdata);
+        (*printerIntf)->Release(printerIntf);
+      }
+      IOObjectRelease(intf);
+    }
 
-  /* One last call to the call back now that we are not longer have printers left to iterate...
-   */
-  if (reference->keepRunning)
-    reference->keepRunning = reference->callback(reference->userdata, 0x0);
+    if (reference->keepRunning && reference->callback)
+      reference->keepRunning = reference->callback(IO_OBJECT_NULL, NULL, reference->userdata);
 
-  if (!reference->keepRunning)
-    CFRunLoopStop(CFRunLoopGetCurrent());
+    if (!reference->keepRunning)
+      CFRunLoopStop(CFRunLoopGetCurrent());
 }
 
-
 /*
  * 'list_device_cb()' - list_device iterator callback.
  */
-
-static Boolean list_device_cb(void *refcon,
-                             io_service_t obj)
+static Boolean list_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon)
 {
-  Boolean keepRunning = (obj != 0x0);
+  (void)refcon;
 
-  if (keepRunning)
+  if (obj != IO_OBJECT_NULL)
   {
     CFStringRef deviceIDString = NULL;
-    UInt32 deviceLocation = 0;
-    UInt8      interfaceNum = 0;
+    CFStringRef make = NULL;
+    CFStringRef model = NULL;
+    CFStringRef serial = NULL;
+    UInt32 intfLocation;
 
-    copy_devicestring(obj, &deviceIDString, &deviceLocation, &interfaceNum);
-    if (deviceIDString != NULL)
-    {
-      CFStringRef make = NULL,  model = NULL, serial = NULL;
-      char uristr[1024], makestr[1024], modelstr[1024], serialstr[1024];
-      char optionsstr[1024], idstr[1024], make_modelstr[1024];
+    deviceIDString = copy_printer_interface_deviceid(printerIntf, 0);
+    if (deviceIDString == NULL)
+      goto list_device_done;
 
-      copy_deviceinfo(deviceIDString, &make, &model, &serial);
-      CFStringGetCString(deviceIDString, idstr, sizeof(idstr),
-                         kCFStringEncodingUTF8);
-      backendGetMakeModel(idstr, make_modelstr, sizeof(make_modelstr));
+    make = deviceIDCopyManufacturer(deviceIDString);
+    model = deviceIDCopyModel(deviceIDString);
+    serial = deviceIDCopySerialNumber(deviceIDString);
 
-      modelstr[0] = '/';
+    char uristr[1024], makestr[1024], modelstr[1024], serialstr[1024];
+    char optionsstr[1024], idstr[1024], make_modelstr[1024];
 
-      if (!make ||
-          !CFStringGetCString(make, makestr, sizeof(makestr),
-                             kCFStringEncodingUTF8))
-        strcpy(makestr, "Unknown");
+    CFStringGetCString(deviceIDString, idstr, sizeof(idstr), kCFStringEncodingUTF8);
+    backendGetMakeModel(idstr, make_modelstr, sizeof(make_modelstr));
 
-      if (!model ||
-          !CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1,
-                             kCFStringEncodingUTF8))
-        strcpy(modelstr + 1, "Printer");
+    modelstr[0] = '/';
 
-      optionsstr[0] = '\0';
-      if (serial != NULL)
-      {
-        CFStringGetCString(serial, serialstr, sizeof(serialstr), kCFStringEncodingUTF8);
-       snprintf(optionsstr, sizeof(optionsstr), "?serial=%s", serialstr);
-      }
-      else if (deviceLocation != 0)
-       snprintf(optionsstr, sizeof(optionsstr), "?location=%x", (unsigned)deviceLocation);
+    if (make  == NULL || !CFStringGetCString(make, makestr, sizeof(makestr), kCFStringEncodingUTF8))
+      strlcpy(makestr, "Unknown", sizeof(makestr));
 
-      httpAssembleURI(HTTP_URI_CODING_ALL, uristr, sizeof(uristr), "usb", NULL, makestr, 0, modelstr);
-      strlcat(uristr, optionsstr, sizeof(uristr));
+    if (model == NULL || !CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1, kCFStringEncodingUTF8))
+      strlcpy(modelstr + 1, "Printer", sizeof(modelstr) - 1);
 
-      cupsBackendReport("direct", uristr, make_modelstr, make_modelstr, idstr,
-                        NULL);
+    optionsstr[0] = '\0';
+    if (serial != NULL && CFStringGetCString(serial, serialstr, sizeof(serialstr), kCFStringEncodingUTF8))
+      snprintf(optionsstr, sizeof(optionsstr), "?serial=%s", serialstr);
+    else if ((*printerIntf)->GetLocationID(printerIntf, &intfLocation) == kIOReturnSuccess)
+      snprintf(optionsstr, sizeof(optionsstr), "?location=%x", (unsigned)intfLocation);
 
-      release_deviceinfo(&make, &model, &serial);
-      CFRelease(deviceIDString);
-    }
-  }
+    httpAssembleURI(HTTP_URI_CODING_ALL, uristr, sizeof(uristr), "usb", NULL, makestr, 0, modelstr);
+    strlcat(uristr, optionsstr, sizeof(uristr));
 
-  return keepRunning;
-}
+    cupsBackendReport("direct", uristr, make_modelstr, make_modelstr, idstr,
+                          NULL);
+  list_device_done:
 
+    if (make != NULL) CFRelease(make);
+    if (model != NULL) CFRelease(model);
+    if (serial != NULL) CFRelease(serial);
+  }
+  return obj != IO_OBJECT_NULL;
+}
 
 /*
  * 'find_device_cb()' - print_device iterator callback.
  */
-
-static Boolean find_device_cb(void *refcon,
-                             io_service_t obj)
+static Boolean find_device_cb(io_service_t obj, printer_interface_t printerIntf, void *refcon)
 {
+  (void)refcon;
+
   Boolean keepLooking = true;
 
-  if (obj != 0x0)
+  if (obj != IO_OBJECT_NULL)
   {
-    CFStringRef idString = NULL;
-    UInt32 location = -1;
-    UInt8      interfaceNum = 0;
+    CFStringRef deviceIDString = NULL;
+    CFStringRef make = NULL;
+    CFStringRef model = NULL;
+    CFStringRef serial = NULL;
 
-    copy_devicestring(obj, &idString, &location, &interfaceNum);
-    if (idString != NULL)
-    {
-      CFStringRef make = NULL,  model = NULL, serial = NULL;
+    deviceIDString = copy_printer_interface_deviceid(printerIntf, 0);
+    if (deviceIDString == NULL)
+      goto find_device_done;
 
-      copy_deviceinfo(idString, &make, &model, &serial);
-      if (make && CFStringCompare(make, g.make, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
-      {
-       if (model && CFStringCompare(model, g.model, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
-       {
-         if (g.serial != NULL && CFStringGetLength(g.serial) > 0)
-         {
-           if (serial != NULL && CFStringCompare(serial, g.serial, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
-           {
-             IOObjectRetain(obj);
-             g.printer_obj = obj;
-             keepLooking = false;
-           }
-         }
-         else
-         {
-           if (g.printer_obj != 0)
-             IOObjectRelease(g.printer_obj);
+    make = deviceIDCopyManufacturer(deviceIDString);
+    model = deviceIDCopyModel(deviceIDString);
+    serial = deviceIDCopySerialNumber(deviceIDString);
 
-           g.printer_obj = obj;
-           IOObjectRetain(obj);
+    if (make && CFStringCompare(make, g.make, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
+    {
+      if (model && CFStringCompare(model, g.model, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
+      {
+        UInt8 intfAltSetting = 0, intfNumber = 0, intfProtocol = 0;
+        UInt32 intfLocation = 0;
+
+        (*printerIntf)->GetInterfaceProtocol(printerIntf, &intfProtocol);
+        (*printerIntf)->GetAlternateSetting(printerIntf, &intfAltSetting);
+        (*printerIntf)->GetInterfaceNumber(printerIntf, &intfNumber);
+        (*printerIntf)->GetLocationID(printerIntf, &intfLocation);
+
+        if (intfProtocol == kUSBPrintingProtocolIPP)
+            return keepLooking;
+
+        if (g.serial != NULL && CFStringGetLength(g.serial) > 0)
+        {
+          if (serial != NULL && CFStringCompare(serial, g.serial, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
+          {
+            g.interfaceProtocol = intfProtocol;
+            g.location = intfLocation;
+            g.alternateSetting = intfAltSetting;
+            g.printer_obj = obj;
+            IOObjectRetain(obj);
+            keepLooking = false;
+          }
+        }
+        else
+        {
+          if (g.printer_obj != 0)
+            IOObjectRelease(g.printer_obj);
+
+            if (g.location == 0 || g.location == intfLocation)
+                keepLooking = false;
+
+            g.location = intfLocation;
+            g.alternateSetting = intfAltSetting;
+            g.interfaceProtocol = intfProtocol;
+            g.printer_obj = obj;
+            IOObjectRetain(obj);
+        }
 
-           if (g.location == 0 || g.location == location)
-             keepLooking = false;
-         }
-         if ( !keepLooking )
-               g.interfaceNum = interfaceNum;
-       }
+        if (!keepLooking)
+          g.interfaceNum = intfNumber;
       }
-
-      release_deviceinfo(&make, &model, &serial);
-      CFRelease(idString);
     }
+
+  find_device_done:
+    if (deviceIDString != NULL) CFRelease(deviceIDString);
+    if (make != NULL) CFRelease(make);
+    if (model != NULL) CFRelease(model);
+    if (serial != NULL) CFRelease(serial);
   }
   else
   {
-    keepLooking = (g.printer_obj == 0);
-    if (obj == 0x0 && keepLooking)
+    keepLooking = (g.printer_obj == 0 && g.interfaceProtocol != kUSBPrintingProtocolIPP);
+    if (obj == IO_OBJECT_NULL && keepLooking)
     {
       CFRunLoopTimerContext context = { 0, refcon, NULL, NULL, NULL };
       CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + 1.0, 10, 0x0, 0x0, status_timer_cb, &context);
       if (timer != NULL)
       {
-       CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
-       g.status_timer = timer;
+        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
+        g.status_timer = timer;
       }
     }
   }
@@ -1301,7 +1339,7 @@ static Boolean find_device_cb(void *refcon,
   if (!keepLooking && g.status_timer != NULL)
   {
     fputs("STATE: -offline-report\n", stderr);
-    _cupsLangPrintFilter(stderr, "INFO", _("Printer is now online."));
+    _cupsLangPrintFilter(stderr, "INFO", _("The printer is now online."));
     CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), g.status_timer, kCFRunLoopDefaultMode);
     CFRelease(g.status_timer);
     g.status_timer = NULL;
@@ -1310,6 +1348,24 @@ static Boolean find_device_cb(void *refcon,
   return keepLooking;
 }
 
+static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID)
+{
+    CFStringRef serialKeys[] = { CFSTR("SN:"),  CFSTR("SERN:"), NULL };
+
+    return copy_value_for_key(deviceID, serialKeys);
+}
+
+static CFStringRef deviceIDCopyModel(CFStringRef deviceID)
+{
+    CFStringRef modelKeys[] = { CFSTR("MDL:"), CFSTR("MODEL:"), NULL };
+    return copy_value_for_key(deviceID, modelKeys);
+}
+
+static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID)
+{
+    CFStringRef makeKeys[]   = { CFSTR("MFG:"), CFSTR("MANUFACTURER:"), NULL };
+    return copy_value_for_key(deviceID, makeKeys);
+}
 
 /*
  * 'status_timer_cb()' - Status timer callback.
@@ -1318,8 +1374,11 @@ static Boolean find_device_cb(void *refcon,
 static void status_timer_cb(CFRunLoopTimerRef timer,
                            void *info)
 {
+  (void)timer;
+  (void)info;
+
   fputs("STATE: +offline-report\n", stderr);
-  _cupsLangPrintFilter(stderr, "INFO", _("Printer is offline."));
+  _cupsLangPrintFilter(stderr, "INFO", _("The printer is offline."));
 
   if (getenv("CLASS") != NULL)
   {
@@ -1339,66 +1398,13 @@ static void status_timer_cb(CFRunLoopTimerRef timer,
 }
 
 
-#pragma mark -
-/*
- * 'copy_deviceinfo()' - Copy strings from the 1284 device ID.
- */
-
-static void copy_deviceinfo(CFStringRef deviceIDString,
-                           CFStringRef *make,
-                           CFStringRef *model,
-                           CFStringRef *serial)
-{
-  CFStringRef modelKeys[]  = { CFSTR("MDL:"), CFSTR("MODEL:"), NULL };
-  CFStringRef makeKeys[]   = { CFSTR("MFG:"), CFSTR("MANUFACTURER:"), NULL };
-  CFStringRef serialKeys[] = { CFSTR("SN:"),  CFSTR("SERN:"), NULL };
-
-  if (make != NULL)
-    *make = copy_value_for_key(deviceIDString, makeKeys);
-
-  if (model != NULL)
-    *model = copy_value_for_key(deviceIDString, modelKeys);
-
-  if (serial != NULL)
-    *serial = copy_value_for_key(deviceIDString, serialKeys);
-}
-
-
-/*
- * 'release_deviceinfo()' - Release deviceinfo strings.
- */
-
-static void release_deviceinfo(CFStringRef *make,
-                              CFStringRef *model,
-                              CFStringRef *serial)
-{
-  if (make != NULL && *make != NULL)
-  {
-    CFRelease(*make);
-    *make = NULL;
-  }
-
-  if (model != NULL && *model != NULL)
-  {
-    CFRelease(*model);
-    *model = NULL;
-  }
-
-  if (serial != NULL && *serial != NULL)
-  {
-    CFRelease(*serial);
-    *serial = NULL;
-  }
-}
-
-
 #pragma mark -
 /*
  * 'load_classdriver()' - Load a classdriver.
  */
 
 static kern_return_t load_classdriver(CFStringRef          driverPath,
-                                     printer_interface_t   intf,
+                                     printer_interface_t   interface,
                                      classdriver_t         ***printerDriver)
 {
   kern_return_t        kr = kUSBPrinterClassDeviceNotOpen;
@@ -1417,10 +1423,10 @@ static kern_return_t load_classdriver(CFStringRef           driverPath,
 
   _cups_fc_result_t result = _cupsFileCheck(bundlestr,
                                             _CUPS_FILE_CHECK_DIRECTORY, 1,
-                                            _cupsFileCheckFilter, NULL);
+                                            Iterating ? NULL : _cupsFileCheckFilter, NULL);
 
   if (result && driverPath)
-    return (load_classdriver(NULL, intf, printerDriver));
+    return (load_classdriver(NULL, interface, printerDriver));
   else if (result)
     return (kr);
 
@@ -1452,15 +1458,15 @@ static kern_return_t load_classdriver(CFStringRef           driverPath,
        {
          classdriver_t **genericDriver = NULL;
          if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo)
-           kr = load_classdriver(NULL, intf, &genericDriver);
+           kr = load_classdriver(NULL, interface, &genericDriver);
 
          if (kr == kIOReturnSuccess)
          {
-           (*driver)->interface = intf;
+           (*driver)->interface = interface;
            (*driver)->Initialize(driver, genericDriver);
 
            (*driver)->plugin = plugin;
-           (*driver)->interface = intf;
+           (*driver)->interface = interface;
            *printerDriver = driver;
          }
        }
@@ -1503,26 +1509,305 @@ static kern_return_t load_printerdriver(CFStringRef *driverBundlePath)
   IOCFPlugInInterface  **iodev = NULL;
   SInt32               score;
   kern_return_t                kr;
-  printer_interface_t  intf;
+  printer_interface_t  interface;
   HRESULT              res;
 
   kr = IOCreatePlugInInterfaceForService(g.printer_obj, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
   if (kr == kIOReturnSuccess)
   {
-    if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *) &intf)) == noErr)
+    if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *) &interface)) == noErr)
     {
       *driverBundlePath = IORegistryEntryCreateCFProperty(g.printer_obj, kUSBClassDriverProperty, NULL, kNilOptions);
 
-      kr = load_classdriver(*driverBundlePath, intf, &g.classdriver);
+      g.use_generic_class_driver = (*driverBundlePath == NULL || (CFStringCompare(*driverBundlePath, kUSBGenericTOPrinterClassDriver, 0x0) == kCFCompareEqualTo));
+      kr = load_classdriver(*driverBundlePath, interface, &g.classdriver);
 
       if (kr != kIOReturnSuccess)
-       (*intf)->Release(intf);
+       (*interface)->Release(interface);
     }
     IODestroyPlugInInterface(iodev);
   }
   return kr;
 }
 
+static printer_interface_t usb_printer_interface_interface(io_service_t usbClass)
+{
+       printer_interface_t  intf = NULL;
+       IOCFPlugInInterface **plugin = NULL;
+       SInt32  score;
+       int kr = IOCreatePlugInInterfaceForService(usbClass, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score);
+       if (kr == kIOReturnSuccess)
+       {
+               (*plugin)->QueryInterface(plugin, USB_INTERFACE_KIND, (LPVOID *)&intf);
+               IODestroyPlugInInterface(plugin);
+       }
+
+       return intf;
+}
+
+static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting)
+{
+       // I have tried to make this function as neat as I can, but the possibility of needing to resend
+       // a request to get the entire string makes it hideous...
+       //
+       // We package the job of sending a request up into the block (^sendRequest), which takes the size
+       // it should allocate for the message buffer. It frees the current buffer if one is set and
+       // allocates one of the specified size, then performs the request. We can then easily retry by
+       // calling the block again if we fail to get the whole string the first time around.
+
+       #define kUSBPrintClassGetDeviceID           0
+       #define kDefaultNoDataTimeout               5000L
+       #define pack_device_id_wIndex(intf, alt)  ((UInt16)((((UInt16)(intf)) << 8) | ((UInt8)(alt))))
+
+       if (printer == NULL)
+                       return NULL;
+
+
+       IOReturn err        = kIOReturnError;
+       UInt8    configurationIndex     = 0;
+       UInt8    interfaceNumber        = 0;
+       size_t   bufferLength           = 256;
+       CFStringRef ret             = NULL;
+
+       if ((*printer)->GetConfigurationValue( printer, &configurationIndex) == kIOReturnSuccess &&
+                       (*printer)->GetInterfaceNumber( printer, &interfaceNumber) == kIOReturnSuccess)
+       {
+               __block IOUSBDevRequestTO       request;
+               IOReturn (^sendRequest)(size_t) = ^ (size_t size)
+               {
+                       if (request.pData)
+                       {
+                               free(request.pData);
+                               request.wLength = 0;
+                               request.pData = NULL;
+                       }
+
+                       IOReturn berr = kIOReturnError;
+                       char *buffer = malloc(size);
+                       if (buffer == NULL)
+                               return kIOReturnNoMemory;
+
+                       request.wLength = HostToUSBWord(size);
+                       request.pData = buffer;
+                       berr = (*printer)->ControlRequestTO(printer, (UInt8)0, &request);
+                       return berr;
+               };
+
+               /* This request takes the 0 based configuration index. IOKit returns a 1 based configuration index */
+               configurationIndex -= 1;
+
+               bzero(&request, sizeof(request));
+
+               request.bmRequestType           = USBmakebmRequestType(kUSBIn, kUSBClass, kUSBInterface);
+               request.bRequest                        = kUSBPrintClassGetDeviceID;
+               request.wValue                          = HostToUSBWord(configurationIndex);
+               request.wIndex                          = HostToUSBWord(pack_device_id_wIndex(interfaceNumber, alternateSetting));
+               request.noDataTimeout           = kDefaultNoDataTimeout;
+               request.completionTimeout       = 0; // Copying behavior from Generic Class Driver
+
+               err = sendRequest(bufferLength);
+
+               if (err == kIOReturnSuccess && request.wLenDone > 1)
+               {
+                       UInt16 actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData));
+
+                       if (actualLength > 2 && actualLength <= bufferLength - 2)
+                       {
+                               ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false);
+                       }
+                       else if (actualLength > 2) {
+                               err = sendRequest(actualLength);
+                               if (err == kIOReturnSuccess && request.wLenDone > 0)
+                               {
+                                       actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData));
+                                       ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false);
+                               }
+                       }
+               }
+
+               if (request.pData)
+                       free(request.pData);
+       }
+
+       CFStringRef manufacturer = deviceIDCopyManufacturer(ret);
+       CFStringRef model = deviceIDCopyModel(ret);
+       CFStringRef serial = deviceIDCopySerialNumber(ret);
+
+       if (manufacturer == NULL || serial == NULL || model == NULL)
+       {
+               IOUSBDevRequestTO               request;
+               IOUSBDeviceDescriptor   desc;
+
+               bzero(&request, sizeof(request));
+
+               request.bmRequestType = USBmakebmRequestType( kUSBIn,  kUSBStandard, kUSBDevice );
+               request.bRequest = kUSBRqGetDescriptor;
+               request.wValue = kUSBDeviceDesc << 8;
+               request.wIndex = 0;
+               request.wLength = sizeof(desc);
+               request.pData = &desc;
+               request.completionTimeout = 0;
+               request.noDataTimeout = 60L;
+
+               err = (*printer)->ControlRequestTO(printer, 0, &request);
+               if (err == kIOReturnSuccess)
+               {
+                       CFMutableStringRef extras = CFStringCreateMutable(NULL, 0);
+                       if (manufacturer == NULL)
+                       {
+                               manufacturer = copy_printer_interface_indexed_description(printer, desc.iManufacturer, kUSBLanguageEnglish);
+                               if (manufacturer && CFStringGetLength(manufacturer) > 0)
+                                       CFStringAppendFormat(extras, NULL, CFSTR("MFG:%@;"), manufacturer);
+                       }
+
+                       if (model == NULL)
+                       {
+                               model = copy_printer_interface_indexed_description(printer, desc.iProduct, kUSBLanguageEnglish);
+                               if (model && CFStringGetLength(model) > 0)
+                                       CFStringAppendFormat(extras, NULL, CFSTR("MDL:%@;"), model);
+                       }
+
+                       if (serial == NULL && desc.iSerialNumber != 0)
+                       {
+                               serial = copy_printer_interface_indexed_description(printer, desc.iSerialNumber, kUSBLanguageEnglish);
+                               if (serial && CFStringGetLength(serial) > 0)
+                                       CFStringAppendFormat(extras, NULL, CFSTR("SERN:%@;"), serial);
+                       }
+
+                       if (ret != NULL)
+                       {
+                               CFStringAppend(extras, ret);
+                               CFRelease(ret);
+
+                               ret = extras;
+                       }
+                       else
+                       {
+                               ret = extras;
+                       }
+               }
+       }
+
+       if (ret != NULL)
+       {
+       /* Remove special characters from the serial number */
+       CFRange range = (serial != NULL ? CFStringFind(serial, CFSTR("+"), 0) : CFRangeMake(0, 0));
+       if (range.length == 1)
+       {
+               range = CFStringFind(ret, serial, 0);
+
+               CFMutableStringRef deviceIDString = CFStringCreateMutableCopy(NULL, 0, ret);
+               CFRelease(ret);
+
+               ret = deviceIDString;
+               CFStringFindAndReplace(deviceIDString, CFSTR("+"), CFSTR(""), range, 0);
+       }
+       }
+
+       if (manufacturer != NULL)
+               CFRelease(manufacturer);
+
+       if (model != NULL)
+               CFRelease(model);
+
+       if (serial != NULL)
+               CFRelease(serial);
+
+       if (ret != NULL && CFStringGetLength(ret) == 0)
+       {
+               CFRelease(ret);
+               return NULL;
+       }
+
+       return ret;
+}
+
+static CFStringRef copy_printer_interface_indexed_description(printer_interface_t  printer, UInt8 index, UInt16 language)
+{
+       IOReturn err;
+       UInt8 description[256]; // Max possible descriptor length
+       IOUSBDevRequestTO       request;
+
+       bzero(description, 2);
+
+       request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
+       request.bRequest = kUSBRqGetDescriptor;
+       request.wValue = (kUSBStringDesc << 8) | index;
+       request.wIndex = language;
+       request.wLength = 2;
+       request.pData = &description;
+       request.completionTimeout = 0;
+       request.noDataTimeout = 60L;
+
+       err = (*printer)->ControlRequestTO(printer, 0, &request);
+       if (err != kIOReturnSuccess && err != kIOReturnOverrun)
+       {
+               bzero(description, request.wLength);
+
+               // Let's try again full length. Here's why:
+               //      On USB 2.0 controllers, we will not get an overrun error.  We just get a "babble" error
+               //      and no valid data.  So, if we ask for the max size, we will either get it, or we'll get an underrun.
+               //      It looks like we get it w/out an underrun
+
+               request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
+               request.bRequest = kUSBRqGetDescriptor;
+               request.wValue = (kUSBStringDesc << 8) | index;
+               request.wIndex = language;
+               request.wLength = sizeof description;
+               request.pData = &description;
+               request.completionTimeout = 0;
+               request.noDataTimeout = 60L;
+
+               err = (*printer)->ControlRequestTO(printer, 0, &request);
+               if (err != kIOReturnSuccess && err != kIOReturnUnderrun)
+                       return NULL;
+       }
+
+       unsigned int length = description[0];
+       if (length == 0)
+               return CFStringCreateWithCString(NULL, "", kCFStringEncodingUTF8);
+
+       if (description[1] != kUSBStringDesc)
+               return NULL;
+
+       request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
+       request.bRequest = kUSBRqGetDescriptor;
+       request.wValue = (kUSBStringDesc << 8) | index;
+       request.wIndex = language;
+
+       bzero(description, length);
+       request.wLength = (UInt16)length;
+       request.pData = &description;
+       request.completionTimeout = 0;
+       request.noDataTimeout = 60L;
+
+       err = (*printer)->ControlRequestTO(printer, 0, &request);
+       if (err != kIOReturnSuccess)
+               return NULL;
+
+       if (description[1] != kUSBStringDesc)
+               return NULL;
+
+       if ((description[0] & 1) != 0)
+               description[0] &= 0xfe;
+
+       char buffer[258] = {};
+       unsigned int maxLength = sizeof buffer;
+       if (description[0] > 1)
+       {
+               length = (description[0]-2)/2;
+
+               if (length > maxLength - 1)
+                       length = maxLength -1;
+
+               for (unsigned i = 0; i < length; i++)
+                       buffer[i] = (char) description[2*i+2];
+
+               buffer[length] = 0;
+       }
+
+       return CFStringCreateWithCString(NULL, buffer, kCFStringEncodingUTF8);
+}
 
 /*
  * 'registry_open()' - Open a connection to the printer.
@@ -1576,144 +1861,6 @@ static kern_return_t registry_close(void)
   return kIOReturnSuccess;
 }
 
-
-/*
- * 'copy_deviceid()' - Copy the 1284 device id string.
- */
-
-static OSStatus copy_deviceid(classdriver_t **classdriver,
-                             CFStringRef *deviceID)
-{
-  CFStringRef devID = NULL,
-
-  deviceMake = NULL,
-  deviceModel = NULL,
-  deviceSerial = NULL;
-
-  OSStatus err = (*classdriver)->GetDeviceID(classdriver, &devID, DEFAULT_TIMEOUT);
-
-  copy_deviceinfo(devID, &deviceMake, &deviceModel, &deviceSerial);
-
-  if (deviceMake == NULL || deviceModel == NULL || deviceSerial == NULL)
-  {
-    IOUSBDeviceDescriptor      desc;
-    iodevice_request_t         request;
-
-    request.requestType = USBmakebmRequestType(kUSBIn,  kUSBStandard, kUSBDevice);
-    request.request = kUSBRqGetDescriptor;
-    request.value = (kUSBDeviceDesc << 8) | 0;
-    request.index = 0;
-    request.length = sizeof(desc);
-    request.buffer = &desc;
-    err = (*classdriver)->DeviceRequest(classdriver, &request, DEFAULT_TIMEOUT);
-    if (err == kIOReturnSuccess)
-    {
-      CFMutableStringRef newDevID = CFStringCreateMutable(NULL, 0);
-
-      if (deviceMake == NULL)
-      {
-       CFStringRef data = NULL;
-       err = (*classdriver)->GetString(classdriver, desc.iManufacturer, kUSBLanguageEnglish, DEFAULT_TIMEOUT, &data);
-       if (data != NULL)
-       {
-         CFStringAppendFormat(newDevID, NULL, CFSTR("MFG:%@;"), data);
-         CFRelease(data);
-       }
-      }
-
-      if (deviceModel == NULL)
-      {
-       CFStringRef data = NULL;
-       err = (*classdriver)->GetString(classdriver, desc.iProduct, kUSBLanguageEnglish, DEFAULT_TIMEOUT, &data);
-       if (data != NULL)
-       {
-         CFStringAppendFormat(newDevID, NULL, CFSTR("MDL:%@;"), data);
-         CFRelease(data);
-       }
-      }
-
-      if (deviceSerial == NULL && desc.iSerialNumber != 0)
-      {
-       CFStringRef data = NULL;
-       err = (*classdriver)->GetString(classdriver, desc.iSerialNumber, kUSBLanguageEnglish, DEFAULT_TIMEOUT, &data);
-       if (data != NULL)
-       {
-         CFStringAppendFormat(newDevID, NULL, CFSTR("SERN:%@;"), data);
-         CFRelease(data);
-       }
-      }
-
-      if (devID != NULL)
-      {
-       CFStringAppend(newDevID, devID);
-       CFRelease(devID);
-      }
-
-      *deviceID = newDevID;
-    }
-  }
-  else
-  {
-    *deviceID = devID;
-  }
-  release_deviceinfo(&deviceMake, &deviceModel, &deviceSerial);
-
-  return err;
-}
-
-
-/*
- * 'copy_devicestring()' - Copy the 1284 device id string.
- */
-
-static void copy_devicestring(io_service_t usbInterface,
-                             CFStringRef *deviceID,
-                             UInt32 *deviceLocation,
-                             UInt8     *interfaceNumber )
-{
-  IOCFPlugInInterface  **iodev = NULL;
-  SInt32               score;
-  kern_return_t                kr;
-  printer_interface_t  intf;
-  HRESULT              res;
-  classdriver_t        **klassDriver = NULL;
-  CFStringRef          driverBundlePath;
-
-  if ((kr = IOCreatePlugInInterfaceForService(usbInterface,
-                                        kIOUSBInterfaceUserClientTypeID,
-                                        kIOCFPlugInInterfaceID,
-                                        &iodev, &score)) == kIOReturnSuccess)
-  {
-    if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *)
-                                       &intf)) == noErr)
-    {
-      (*intf)->GetLocationID(intf, deviceLocation);
-      (*intf)->GetInterfaceNumber(intf, interfaceNumber);
-
-      driverBundlePath = IORegistryEntryCreateCFProperty(usbInterface,
-                                                        kUSBClassDriverProperty,
-                                                        NULL, kNilOptions);
-
-      kr = load_classdriver(driverBundlePath, intf, &klassDriver);
-
-      if (kr != kIOReturnSuccess && driverBundlePath != NULL)
-       kr = load_classdriver(NULL, intf, &klassDriver);
-
-      if (kr == kIOReturnSuccess && klassDriver != NULL)
-         kr = copy_deviceid(klassDriver, deviceID);
-
-      unload_classdriver(&klassDriver);
-
-      if (driverBundlePath != NULL)
-       CFRelease(driverBundlePath);
-
-      /* (*intf)->Release(intf); */
-    }
-    IODestroyPlugInInterface(iodev);
-  }
-}
-
-
 #pragma mark -
 /*
  * 'copy_value_for_key()' - Copy value string associated with a key.
@@ -1846,25 +1993,25 @@ static void parse_options(char *options,
     * Process the option...
     */
 
-    if (!strcasecmp(name, "waiteof"))
+    if (!_cups_strcasecmp(name, "waiteof"))
     {
-      if (!strcasecmp(value, "on") ||
-         !strcasecmp(value, "yes") ||
-         !strcasecmp(value, "true"))
+      if (!_cups_strcasecmp(value, "on") ||
+         !_cups_strcasecmp(value, "yes") ||
+         !_cups_strcasecmp(value, "true"))
        *wait_eof = true;
-      else if (!strcasecmp(value, "off") ||
-              !strcasecmp(value, "no") ||
-              !strcasecmp(value, "false"))
+      else if (!_cups_strcasecmp(value, "off") ||
+              !_cups_strcasecmp(value, "no") ||
+              !_cups_strcasecmp(value, "false"))
        *wait_eof = false;
       else
        _cupsLangPrintFilter(stderr, "WARNING",
                             _("Boolean expected for waiteof option \"%s\"."),
                             value);
     }
-    else if (!strcasecmp(name, "serial"))
-      strlcpy(serial, value, serial_size);
-    else if (!strcasecmp(name, "location") && location)
-      *location = strtol(value, NULL, 16);
+    else if (!_cups_strcasecmp(name, "serial"))
+      strlcpy(serial, value, (size_t)serial_size);
+    else if (!_cups_strcasecmp(name, "location") && location)
+      *location = (UInt32)strtoul(value, NULL, 16);
   }
 }
 
@@ -1927,6 +2074,7 @@ static void run_legacy_backend(int argc,
   char *my_argv[32];
   char *usb_legacy_status;
 
+
  /*
   * If we're running as x86_64 or i386 and couldn't load the class driver
   * (because it's ppc or i386), then try to re-exec ourselves in ppc or i386
@@ -2010,7 +2158,7 @@ static void run_legacy_backend(int argc,
       cups_serverbin = CUPS_SERVERBIN;
     snprintf(usbpath, sizeof(usbpath), "%s/backend/usb", cups_serverbin);
 
-    for (i = 0; i < argc && i < (sizeof(my_argv) / sizeof(my_argv[0])) - 1; i ++)
+    for (i = 0; i < argc && i < (int)(sizeof(my_argv) / sizeof(my_argv[0])) - 1; i ++)
       my_argv[i] = argv[i];
 
     my_argv[i] = NULL;
@@ -2096,19 +2244,45 @@ sigterm_handler(int sig)                /* I - Signal */
     while (waitpid(child_pid, &status, 0) < 0 && errno == EINTR);
 
     if (WIFEXITED(status))
-      exit(WEXITSTATUS(status));
+      _exit(WEXITSTATUS(status));
     else if (status == SIGTERM || status == SIGKILL)
-      exit(0);
+      _exit(0);
     else
     {
-      fprintf(stderr, "DEBUG: Child crashed on signal %d\n", status);
-      exit(CUPS_BACKEND_STOP);
+      write(2, "DEBUG: Child crashed.\n", 22);
+      _exit(CUPS_BACKEND_STOP);
     }
   }
 #endif /* __i386__ || __x86_64__ */
 }
 
 
+/*
+ * 'sigquit_handler()' - SIGQUIT handler.
+ */
+
+static void sigquit_handler(int sig, siginfo_t *si, void *unused)
+{
+  char  *path;
+  char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
+  static char msgbuf[256] = "";
+
+
+  (void)sig;
+  (void)unused;
+
+  if (proc_pidpath(si->si_pid, pathbuf, sizeof(pathbuf)) > 0 &&
+      (path = basename(pathbuf)) != NULL)
+    snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by %s(%d)", path, (int)si->si_pid);
+  else
+    snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by PID %d", (int)si->si_pid);
+
+  CRSetCrashLogMessage(msgbuf);
+
+  abort();
+}
+
+
 #ifdef PARSE_PS_ERRORS
 /*
  * 'next_line()' - Find the next line in a buffer.
@@ -2162,9 +2336,9 @@ static void parse_pserror(char *sockBuffer,
       pCommentEnd += 3;            /* Skip past "]%%" */
       *pCommentEnd = '\0';         /* There's always room for the nul */
 
-      if (strncasecmp(pCommentBegin, "%%[ Error:", 10) == 0)
+      if (_cups_strncasecmp(pCommentBegin, "%%[ Error:", 10) == 0)
        logLevel = "DEBUG";
-      else if (strncasecmp(pCommentBegin, "%%[ Flushing", 12) == 0)
+      else if (_cups_strncasecmp(pCommentBegin, "%%[ Flushing", 12) == 0)
        logLevel = "DEBUG";
       else
        logLevel = "INFO";
@@ -2179,7 +2353,7 @@ static void parse_pserror(char *sockBuffer,
     }
 
     /* move everything over... */
-    strcpy(gErrorBuffer, pLineEnd);
+    strlcpy(gErrorBuffer, pLineEnd, sizeof(gErrorBuffer));
     gErrorBufferPtr = gErrorBuffer;
     pLineEnd = (char *)next_line((const char *)gErrorBuffer);
   }
@@ -2266,19 +2440,30 @@ static void get_device_id(cups_sc_status_t *status,
 {
   CFStringRef deviceIDString = NULL;
 
-  /* GetDeviceID */
-  copy_deviceid(g.classdriver, &deviceIDString);
+  if (g.printer_obj != IO_OBJECT_NULL)
+  {
+    printer_interface_t printerIntf = usb_printer_interface_interface(g.printer_obj);
+    if (printerIntf)
+    {
+      deviceIDString = copy_printer_interface_deviceid(printerIntf, g.alternateSetting);
+      (*printerIntf)->Release(printerIntf);
+    }
+  }
+
 
   if (deviceIDString)
   {
-    CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8);
-    *datalen = strlen(data);
+    if (CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8))
+      *datalen = (int)strlen(data);
+    else
+      *datalen = 0;
+
     CFRelease(deviceIDString);
   }
+  else
+  {
+    *datalen = 0;
+  }
+
   *status  = CUPS_SC_STATUS_OK;
 }
-
-
-/*
- * End of "$Id: usb-darwin.c 7953 2008-09-17 01:43:19Z mike $".
- */