/*
- * "$Id: usb-darwin.c 6365 2007-03-19 20:56:57Z mike $"
- *
- * Copyright © 2005-2007 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.
- */
+* "$Id: usb-darwin.c 6993 2007-09-28 18:05:28Z mike $"
+*
+* Copyright 2005-2008 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.
+* sidechannel_thread() - Thread to handle side-channel requests.
+* read_thread() - Thread to read the backchannel data on.
+* list_device_cb() - list_device iterator callback.
+* find_device_cb() - print_device iterator callback.
+* status_timer_cb() - Status timer callback.
+* iterate_printers() - Iterate over all the printers.
+* device_added() - Device added notifier.
+* 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.
+* setup_cfLanguage() - Create AppleLanguages array from LANG environment var.
+* run_ppc_backend() - Re-exec i386 backend as ppc.
+* sigterm_handler() - SIGTERM handler.
+* next_line() - Find the next line in a buffer.
+* parse_pserror() - Scan the backchannel data for postscript errors.
+* get_device_id() - Return IEEE-1284 device ID.
+*/
/*
- * USB port on Darwin backend for the Common UNIX Printing System (CUPS).
+ * Include necessary headers.
*/
+
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
+#include <sys/stat.h>
#include <sys/sysctl.h>
#include <libgen.h>
-#include <mach/mach.h>
+#include <mach/mach.h>
#include <mach/mach_error.h>
#include <mach/mach_time.h>
#include <cups/debug.h>
#include <cups/sidechannel.h>
+#include <cups/i18n.h>
+#include "backend-private.h"
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/usb/IOUSBLib.h>
#include <pthread.h>
-/*
- * WAITEOF_DELAY is number of seconds we'll wait for responses from
- * the printer after we've finished sending all the data
+/*
+ * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
+ * the printer after we've finished sending all the data
*/
-#define WAITEOF_DELAY 7
-#define DEFAULT_TIMEOUT 60L
+#define WAIT_EOF_DELAY 7
+#define WAIT_SIDE_DELAY 3
+#define DEFAULT_TIMEOUT 5000L
#define USB_INTERFACE_KIND CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID190)
#define kUSBLanguageEnglish 0x409
-#define PRINTER_POLLING_INTERVAL 5 /* seconds */
-#define INITIAL_LOG_INTERVAL (PRINTER_POLLING_INTERVAL)
-#define SUBSEQUENT_LOG_INTERVAL (3*INITIAL_LOG_INTERVAL)
+#define PRINTER_POLLING_INTERVAL 5 /* seconds */
+#define INITIAL_LOG_INTERVAL PRINTER_POLLING_INTERVAL
+#define SUBSEQUENT_LOG_INTERVAL 3 * INITIAL_LOG_INTERVAL
-#define kUSBPrinterClassTypeID (CFUUIDGetConstantUUIDWithBytes(NULL, 0x06, 0x04, 0x7D, 0x16, 0x53, 0xA2, 0x11, 0xD6, 0x92, 0x06, 0x00, 0x30, 0x65, 0x52, 0x45, 0x92))
-#define kUSBPrinterClassInterfaceID (CFUUIDGetConstantUUIDWithBytes(NULL, 0x03, 0x34, 0x6D, 0x74, 0x53, 0xA3, 0x11, 0xD6, 0x9E, 0xA1, 0x76, 0x30, 0x65, 0x52, 0x45, 0x92))
+#define kUSBPrinterClassTypeID CFUUIDGetConstantUUIDWithBytes(NULL, 0x06, 0x04, 0x7D, 0x16, 0x53, 0xA2, 0x11, 0xD6, 0x92, 0x06, 0x00, 0x30, 0x65, 0x52, 0x45, 0x92)
+#define kUSBPrinterClassInterfaceID CFUUIDGetConstantUUIDWithBytes(NULL, 0x03, 0x34, 0x6D, 0x74, 0x53, 0xA3, 0x11, 0xD6, 0x9E, 0xA1, 0x76, 0x30, 0x65, 0x52, 0x45, 0x92)
#define kUSBClassDriverProperty CFSTR("USB Printing Class")
#define kUSBGenericTOPrinterClassDriver CFSTR("/System/Library/Printers/Libraries/USBGenericTOPrintingClass.plugin")
#define kUSBPrinterClassDeviceNotOpen -9664 /*kPMInvalidIOMContext*/
-#define kWriteBufferSize 2048
-#pragma mark -
/*
* Section 5.3 USB Printing Class spec
*/
typedef IOUSBInterfaceInterface190 **printer_interface_t;
-typedef struct iodevice_request_s /**** Device request ****/
+typedef struct iodevice_request_s /**** Device request ****/
{
- UInt8 requestType;
+ UInt8 requestType;
UInt8 request;
UInt16 value;
UInt16 index;
UInt16 length;
- void *buffer;
+ void *buffer;
} iodevice_request_t;
-typedef union { /**** Centronics status byte ****/
+typedef union /**** Centronics status byte ****/
+{
char b;
- struct {
+ struct
+ {
unsigned reserved0:2;
unsigned paperError:1;
unsigned select:1;
} status;
} centronics_status_t;
-typedef struct classdriver_context_s /**** Classdriver context ****/
+typedef struct classdriver_s /**** g.classdriver context ****/
{
IUNKNOWN_C_GUTS;
CFPlugInRef plugin; /* release plugin */
UInt8 inpipe; /* optional bulkIn pipe */
/* general class requests */
- kern_return_t (*DeviceRequest)( struct classdriver_context_s **printer, iodevice_request_t *iorequest, UInt16 timeout );
- kern_return_t (*GetString)( struct classdriver_context_s **printer, UInt8 whichString, UInt16 language, UInt16 timeout, CFStringRef *result );
+ kern_return_t (*DeviceRequest)(struct classdriver_s **printer, iodevice_request_t *iorequest, UInt16 timeout);
+ kern_return_t (*GetString)(struct classdriver_s **printer, UInt8 whichString, UInt16 language, UInt16 timeout, CFStringRef *result);
/* standard printer class requests */
- kern_return_t (*SoftReset)( struct classdriver_context_s **printer, UInt16 timeout );
- kern_return_t (*GetCentronicsStatus)( struct classdriver_context_s **printer, centronics_status_t *result, UInt16 timeout );
- kern_return_t (*GetDeviceID)( struct classdriver_context_s **printer, CFStringRef *devid, UInt16 timeout );
+ kern_return_t (*SoftReset)(struct classdriver_s **printer, UInt16 timeout);
+ kern_return_t (*GetCentronicsStatus)(struct classdriver_s **printer, centronics_status_t *result, UInt16 timeout);
+ kern_return_t (*GetDeviceID)(struct classdriver_s **printer, CFStringRef *devid, UInt16 timeout);
/* standard bulk device requests */
- kern_return_t (*ReadPipe)( struct classdriver_context_s **printer, UInt8 *buffer, UInt32 *count );
- kern_return_t (*WritePipe)( struct classdriver_context_s **printer, UInt8 *buffer, UInt32 *count, Boolean eoj );
+ kern_return_t (*ReadPipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count);
+ kern_return_t (*WritePipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count, Boolean eoj);
/* interface requests */
- kern_return_t (*Open)( struct classdriver_context_s **printer, UInt32 location, UInt8 protocol );
- kern_return_t (*Abort)( struct classdriver_context_s **printer );
- kern_return_t (*Close)( struct classdriver_context_s **printer );
+ kern_return_t (*Open)(struct classdriver_s **printer, UInt32 location, UInt8 protocol);
+ kern_return_t (*Abort)(struct classdriver_s **printer);
+ kern_return_t (*Close)(struct classdriver_s **printer);
/* initialize and terminate */
- kern_return_t (*Initialize)( struct classdriver_context_s **printer, struct classdriver_context_s **baseclass );
- kern_return_t (*Terminate)( struct classdriver_context_s **printer );
-
-} classdriver_context_t;
+ kern_return_t (*Initialize)(struct classdriver_s **printer, struct classdriver_s **baseclass);
+ kern_return_t (*Terminate)(struct classdriver_s **printer);
+} classdriver_t;
typedef Boolean (*iterator_callback_t)(void *refcon, io_service_t obj);
-typedef struct iterator_reference_s { /**** Iterator reference data */
+typedef struct iterator_reference_s /**** Iterator reference data */
+{
iterator_callback_t callback;
void *userdata;
Boolean keepRunning;
} iterator_reference_t;
-typedef struct printer_data_s { /**** Printer context data ****/
- io_service_t printerObj;
- classdriver_context_t **printerDriver;
+typedef struct globals_s
+{
+ io_service_t printer_obj;
+ classdriver_t **classdriver;
+
+ pthread_mutex_t read_thread_mutex;
+ pthread_cond_t read_thread_cond;
+ int read_thread_stop;
+ int read_thread_done;
- pthread_cond_t readCompleteCondition;
- pthread_mutex_t readMutex;
- int done;
+ pthread_mutex_t readwrite_lock_mutex;
+ pthread_cond_t readwrite_lock_cond;
+ int readwrite_lock;
- const char *uri;
CFStringRef make;
CFStringRef model;
CFStringRef serial;
-
UInt32 location;
- Boolean waitEOF;
-
- CFRunLoopTimerRef statusTimer;
-
- pthread_cond_t reqWaitCompCond;
- pthread_mutex_t reqWaitMutex;
- pthread_mutex_t waitCloseMutex;
- pthread_mutex_t writeCompMutex;
- int writeDone;
- int reqWaitDone;
- int reqWqitFlag;
- int directionalFlag; /* 0=uni, 1=bidi */
- ssize_t dataSize;
- ssize_t dataOffset;
- char dataBuffer[kWriteBufferSize];
-} printer_data_t;
+ UInt8 interfaceNum;
+
+ CFRunLoopTimerRef status_timer;
+
+ int print_fd; /* File descriptor to print */
+ ssize_t print_bytes; /* Print bytes read */
+
+ Boolean wait_eof;
+ int drain_output; /* Drain all pending output */
+ int bidi_flag; /* 0=unidirectional, 1=bidirectional */
+
+ pthread_mutex_t sidechannel_thread_mutex;
+ pthread_cond_t sidechannel_thread_cond;
+ int sidechannel_thread_stop;
+ int sidechannel_thread_done;
+} globals_t;
+
+
+/*
+ * Globals...
+ */
+
+globals_t g = { 0 }; /* Globals */
/*
* Local functions...
*/
-static Boolean list_device_callback(void *refcon, io_service_t obj);
-static Boolean find_device_callback(void *refcon, io_service_t obj);
-static void statusTimerCallback(CFRunLoopTimerRef timer, void *info);
-static void iterate_printers(iterator_callback_t callBack, void *userdata);
-static void device_added(void *userdata, io_iterator_t iterator);
+static Boolean find_device_cb(void *refcon, io_service_t obj);
+static Boolean list_device_cb(void *refcon, io_service_t obj);
+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_printerdriver(CFStringRef *driverBundlePath);
+static kern_return_t registry_close();
+static kern_return_t registry_open(CFStringRef *driverBundlePath);
+static kern_return_t unload_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 kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t intf, classdriver_context_t ***driver);
-static kern_return_t unload_classdriver(classdriver_context_t ***classDriver);
-static kern_return_t load_printerdriver(printer_data_t *printer, CFStringRef *driverBundlePath);
-static kern_return_t registry_open(printer_data_t *printer, CFStringRef *driverBundlePath);
-static kern_return_t registry_close(printer_data_t *printer);
-static OSStatus copy_deviceid(classdriver_context_t **printer, CFStringRef *deviceID);
-static void copy_devicestring(io_service_t usbInterface, CFStringRef *deviceID, UInt32 *deviceLocation);
-static CFStringRef copy_value_for_key(CFStringRef deviceID, CFStringRef *keys);
-static CFStringRef cfstr_create_and_trim(const char *cstr);
-static void parse_options(const char *options, char *serial, UInt32 *location, Boolean *waitEOF);
static void setup_cfLanguage(void);
-static void *read_thread(void *reference);
-static void *reqestWait_thread(void *reference);
-static void usbSoftReset(printer_data_t *userData, cups_sc_status_t *status);
-static void usbDrainOutput(printer_data_t *userData, cups_sc_status_t *status);
-static void usbGetBidirectional(printer_data_t *userData, cups_sc_status_t *status, char *data, int *datalen);
-static void usbGetDeviceID(printer_data_t *userData, cups_sc_status_t *status, char *data, int *datalen);
-static void usbGetDevState(printer_data_t *userData, cups_sc_status_t *status, char *data, int *datalen);
-
+static void soft_reset();
+static void status_timer_cb(CFRunLoopTimerRef timer, void *info);
#if defined(__i386__)
static pid_t child_pid; /* Child PID */
void list_devices()
{
- iterate_printers(list_device_callback, NULL);
+ iterate_printers(list_device_cb, NULL);
}
print_device(const char *uri, /* I - Device URI */
const char *hostname, /* I - Hostname/manufacturer */
const char *resource, /* I - Resource/modelname */
- const char *options, /* I - Device options/serial number */
- int fd, /* I - File descriptor to print */
+ char *options, /* I - Device options/serial number */
+ int print_fd, /* I - File descriptor to print */
int copies, /* I - Copies to print */
int argc, /* I - Number of command-line arguments (6 or 7) */
char *argv[]) /* I - Command-line arguments */
{
- printer_data_t printer_data = { 0x0 }; /* Printer context */
- char serial[1024]; /* Serial number buffer */
- OSStatus status = noErr; /* Function results */
- pthread_t thr; /* Read thread */
- char buffer[2048]; /* Write buffer */
- int thread_created = 0; /* Thread created? */
- int countdown = INITIAL_LOG_INTERVAL; /* Logging interval */
- pthread_cond_t *readCompleteConditionPtr = NULL; /* Read complete condition */
- pthread_mutex_t *readMutexPtr = NULL; /* Read mutex */
- CFStringRef driverBundlePath; /* Class driver path */
- int reqWait_create = 0; /* RequestWait thread created? */
- pthread_t reqWaitThread; /* RequestWait thread */
- pthread_cond_t *reqWaitCompCondPtr = NULL; /* RequestWait complete condition */
- pthread_mutex_t *reqWaitMutexPtr = NULL; /* RequestWait mutex */
- pthread_mutex_t *waitCloseMutexPtr = NULL; /* wait close mutex */
- pthread_mutex_t *writeCompMutexPtr = NULL; /* write complete mutex */
+ char serial[1024]; /* Serial number buffer */
+ OSStatus status; /* Function results */
+ pthread_t read_thread_id, /* Read thread */
+ sidechannel_thread_id;/* Side-channel thread */
+ int have_sidechannel = 0; /* Was the side-channel thread started? */
+ struct stat sidechannel_info; /* Side-channel file descriptor info */
+ char print_buffer[8192], /* Print data buffer */
+ *print_ptr; /* Pointer into print data buffer */
+ UInt32 location; /* Unique location in bus topology */
+ fd_set input_set; /* Input set for select() */
+ CFStringRef driverBundlePath; /* Class driver path */
+ int countdown, /* Logging interval */
+ nfds; /* Number of file descriptors */
+ ssize_t total_bytes; /* Total bytes written */
+ UInt32 bytes; /* Bytes written */
+ struct timeval *timeout, /* Timeout pointer */
+ stimeout; /* Timeout for select() */
+ struct timespec cond_timeout; /* pthread condition timeout */
+
+
+ /*
+ * See if the side-channel descriptor is valid...
+ */
+
+ have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) &&
+ S_ISSOCK(sidechannel_info.st_mode);
+
+ /*
+ * Localize using CoreFoundation...
+ */
setup_cfLanguage();
- parse_options(options, serial, &printer_data.location, &printer_data.waitEOF);
+
+ parse_options(options, serial, sizeof(serial), &location, &g.wait_eof);
if (resource[0] == '/')
resource++;
- printer_data.uri = uri;
-
- printer_data.make = cfstr_create_and_trim(hostname);
- printer_data.model = cfstr_create_and_trim(resource);
- printer_data.serial = cfstr_create_and_trim(serial);
+ g.print_fd = print_fd;
+ g.make = cfstr_create_trim(hostname);
+ g.model = cfstr_create_trim(resource);
+ g.serial = cfstr_create_trim(serial);
+ g.location = location;
+
+ if (!g.make || !g.model)
+ {
+ _cupsLangPrintf(stderr,
+ _("ERROR: Unable to create make and model strings\n"));
+ return CUPS_BACKEND_STOP;
+ }
fputs("STATE: +connecting-to-device\n", stderr);
- do {
- if (printer_data.printerObj != 0x0) {
- IOObjectRelease(printer_data.printerObj);
- unload_classdriver(&printer_data.printerDriver);
- printer_data.printerObj = 0x0;
- printer_data.printerDriver = 0x0;
+ countdown = INITIAL_LOG_INTERVAL;
+
+ do
+ {
+ if (g.printer_obj)
+ {
+ IOObjectRelease(g.printer_obj);
+ unload_classdriver(&g.classdriver);
+ g.printer_obj = 0x0;
+ g.classdriver = 0x0;
}
- fprintf(stderr, "INFO: Looking for '%s %s'\n", hostname, resource);
- iterate_printers(find_device_callback, &printer_data);
+ fprintf(stderr, "DEBUG: Looking for '%s %s'\n", hostname, resource);
- fprintf(stderr, "INFO: Opening Connection\n");
+ iterate_printers(find_device_cb, NULL);
+
+ fputs("DEBUG: Opening connection\n", stderr);
driverBundlePath = NULL;
- status = registry_open(&printer_data, &driverBundlePath);
+
+ status = registry_open(&driverBundlePath);
+
#if defined(__i386__)
/*
* If we were unable to load the class drivers for this printer it's probably because they're ppc-only.
* In this case try to fork & exec this backend as a ppc executable so we can use them...
*/
- if (status == -2 /* kPMInvalidIOMContext */) {
- run_ppc_backend(argc, argv, fd);
+ if (status == -2)
+ {
+ run_ppc_backend(argc, argv, print_fd);
/* Never returns here */
}
#endif /* __i386__ */
- if (status == -2) {
+
+ if (status == -2)
+ {
/*
* If we still were unable to load the class drivers for this printer log
* the error and stop the queue...
*/
- if (driverBundlePath == NULL || !CFStringGetCString(driverBundlePath, buffer, sizeof(buffer), kCFStringEncodingUTF8))
- strlcpy(buffer, "USB class driver", sizeof(buffer));
+ if (driverBundlePath == NULL || !CFStringGetCString(driverBundlePath, print_buffer, sizeof(print_buffer), kCFStringEncodingUTF8))
+ strlcpy(print_buffer, "USB class driver", sizeof(print_buffer));
- fprintf(stderr, "STATE: +apple-missing-usbclassdriver-error\n" \
- "FATAL: Could not load %s\n", buffer);
+ fputs("STATE: +apple-missing-usbclassdriver-error\n", stderr);
+ _cupsLangPrintf(stderr, _("FATAL: Could not load %s\n"), print_buffer);
if (driverBundlePath)
CFRelease(driverBundlePath);
if (driverBundlePath)
CFRelease(driverBundlePath);
- if (status != noErr) {
- sleep( PRINTER_POLLING_INTERVAL );
+ if (status != noErr)
+ {
+ sleep(PRINTER_POLLING_INTERVAL);
countdown -= PRINTER_POLLING_INTERVAL;
- if ( countdown <= 0 ) {
- fprintf(stderr, "INFO: Printer busy (status:0x%08x)\n", (int)status);
+ if (countdown <= 0)
+ {
+ _cupsLangPrintf(stderr, _("INFO: Printer busy (status:0x%08x)\n"),
+ (int)status);
countdown = SUBSEQUENT_LOG_INTERVAL; /* subsequent log entries, every 15 seconds */
}
}
* stdin (otherwise you can't cancel raw jobs...)
*/
- if (!fd) {
-#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
- sigset(SIGTERM, SIG_IGN);
-#elif defined(HAVE_SIGACTION)
+ if (!print_fd)
+ {
+ struct sigaction action; /* POSIX signal action */
+
+
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = SIG_IGN;
sigaction(SIGTERM, &action, NULL);
-#else
- signal(SIGTERM, SIG_IGN);
-#endif /* HAVE_SIGSET */
}
- if (status == noErr) {
- if (pthread_cond_init(&printer_data.readCompleteCondition, NULL) == 0)
- readCompleteConditionPtr = &printer_data.readCompleteCondition;
+ /*
+ * Start the side channel thread if the descriptor is valid...
+ */
- if (pthread_mutex_init(&printer_data.readMutex, NULL) == 0)
- readMutexPtr = &printer_data.readMutex;
+ pthread_mutex_init(&g.readwrite_lock_mutex, NULL);
+ pthread_cond_init(&g.readwrite_lock_cond, NULL);
+ g.readwrite_lock = 1;
- printer_data.done = 0;
+ if (have_sidechannel)
+ {
+ g.sidechannel_thread_stop = 0;
+ g.sidechannel_thread_done = 0;
- if (pthread_create(&thr, NULL, read_thread, &printer_data) == 0)
- thread_created = 1;
+ pthread_cond_init(&g.sidechannel_thread_cond, NULL);
+ pthread_mutex_init(&g.sidechannel_thread_mutex, NULL);
- if (thread_created == 0)
- fprintf(stderr, "WARNING: Couldn't create read channel\n");
+ if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL))
+ {
+ _cupsLangPuts(stderr, _("WARNING: Couldn't create side channel\n"));
+ return CUPS_BACKEND_STOP;
+ }
+ }
- if (pthread_cond_init(&printer_data.reqWaitCompCond, NULL) == 0)
- reqWaitCompCondPtr = &printer_data.reqWaitCompCond;
+ /*
+ * Get the read thread going...
+ */
- if (pthread_mutex_init(&printer_data.reqWaitMutex, NULL) == 0)
- reqWaitMutexPtr = &printer_data.reqWaitMutex;
+ g.read_thread_stop = 0;
+ g.read_thread_done = 0;
- printer_data.reqWaitDone = 0;
- printer_data.reqWqitFlag = 0;
+ pthread_cond_init(&g.read_thread_cond, NULL);
+ pthread_mutex_init(&g.read_thread_mutex, NULL);
- if (pthread_create(&reqWaitThread, NULL, reqestWait_thread, &printer_data) == 0)
- reqWait_create = 1;
+ if (pthread_create(&read_thread_id, NULL, read_thread, NULL))
+ {
+ _cupsLangPuts(stderr, _("WARNING: Couldn't create read channel\n"));
+ return CUPS_BACKEND_STOP;
+ }
- if (reqWait_create == 0)
- fprintf(stderr, "WARNING: Couldn't create sidechannel thread!\n");
+ /*
+ * The main thread sends the print file...
+ */
- if (pthread_mutex_init(&printer_data.waitCloseMutex, NULL) == 0)
- waitCloseMutexPtr = &printer_data.waitCloseMutex;
+ g.drain_output = 0;
+ g.print_bytes = 0;
+ total_bytes = 0;
+ print_ptr = print_buffer;
- if (pthread_mutex_init(&printer_data.writeCompMutex, NULL) == 0)
- writeCompMutexPtr = &printer_data.writeCompMutex;
- }
+ while (status == noErr && copies-- > 0)
+ {
+ _cupsLangPuts(stderr, _("INFO: Sending data\n"));
- /*
- * The main thread sends the print file...
- */
+ if (print_fd != STDIN_FILENO)
+ {
+ fputs("PAGE: 1 1", stderr);
+ lseek(print_fd, 0, SEEK_SET);
+ }
- printer_data.writeDone = 0;
- printer_data.dataSize = 0;
- printer_data.dataOffset = 0;
- pthread_mutex_lock(writeCompMutexPtr);
+ while (status == noErr)
+ {
+ FD_ZERO(&input_set);
- while (status == noErr && copies-- > 0) {
- UInt32 wbytes; /* Number of bytes written */
- ssize_t nbytes; /* Number of bytes read */
- off_t tbytes = 0; /* Total number of bytes written */
+ if (!g.print_bytes)
+ FD_SET(print_fd, &input_set);
- fprintf(stderr, "INFO: Sending data\n");
+ /*
+ * Calculate select timeout...
+ * If we have data waiting to send timeout is 100ms.
+ * else if we're draining print_fd timeout is 0.
+ * else we're waiting forever...
+ */
- if (STDIN_FILENO != fd) {
- fputs("PAGE: 1 1", stderr);
- lseek( fd, 0, SEEK_SET );
- }
+ if (g.print_bytes)
+ {
+ stimeout.tv_sec = 0;
+ stimeout.tv_usec = 100000; /* 100ms */
+ timeout = &stimeout;
+ }
+ else if (g.drain_output)
+ {
+ stimeout.tv_sec = 0;
+ stimeout.tv_usec = 0;
+ timeout = &stimeout;
+ }
+ else
+ timeout = NULL;
+
+ /*
+ * I/O is unlocked around select...
+ */
+
+ pthread_mutex_lock(&g.readwrite_lock_mutex);
+ g.readwrite_lock = 0;
+ pthread_cond_signal(&g.readwrite_lock_cond);
+ pthread_mutex_unlock(&g.readwrite_lock_mutex);
+
+ nfds = select(print_fd + 1, &input_set, NULL, NULL, timeout);
+
+ /*
+ * Reacquire the lock...
+ */
+
+ pthread_mutex_lock(&g.readwrite_lock_mutex);
+ while (g.readwrite_lock)
+ pthread_cond_wait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex);
+ g.readwrite_lock = 1;
+ pthread_mutex_unlock(&g.readwrite_lock_mutex);
+
+ if (nfds < 0)
+ {
+ if (errno == EINTR && total_bytes == 0)
+ {
+ fputs("DEBUG: Received an interrupt before any bytes were "
+ "written, aborting!\n", stderr);
+ return (0);
+ }
+ else if (errno != EAGAIN)
+ {
+ _cupsLangPrintf(stderr, _("ERROR: select() returned %d\n"), (int)errno);
+ return CUPS_BACKEND_STOP;
+ }
+ }
+
+ /*
+ * If drain output has finished send a response...
+ */
+
+ if (g.drain_output && !nfds && !g.print_bytes)
+ {
+ /* Send a response... */
+ cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
+ g.drain_output = 0;
+ }
+
+ /*
+ * Check if we have print data ready...
+ */
- while (status == noErr && (nbytes = read(fd, buffer, sizeof(buffer))) > 0) {
- char *bufptr = buffer;
- tbytes += nbytes;
+ if (FD_ISSET(print_fd, &input_set))
+ {
+ g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer));
+
+ if (g.print_bytes < 0)
+ {
+ /*
+ * Read error - bail if we don't see EAGAIN or EINTR...
+ */
+
+ if (errno != EAGAIN || errno != EINTR)
+ {
+ _cupsLangPrintError(_("ERROR: Unable to read print data"));
+ return CUPS_BACKEND_STOP;
+ }
- while (nbytes > 0 && status == noErr) {
- if (printer_data.writeDone) {
- printer_data.dataSize = nbytes;
- printer_data.dataOffset = bufptr - buffer;
- memcpy(printer_data.dataBuffer, buffer, nbytes);
+ g.print_bytes = 0;
+ }
+ else if (g.print_bytes == 0)
+ {
+ /*
+ * End of file, break out of the loop...
+ */
- status = -1;
break;
}
- wbytes = nbytes;
- status = (*(printer_data.printerDriver))->WritePipe( printer_data.printerDriver, (UInt8*)bufptr, &wbytes, 0 /* nbytes > wbytes? 0: feof(fp) */ );
- if (wbytes < 0 || noErr != status) {
- OSStatus err = (*(printer_data.printerDriver))->Abort(printer_data.printerDriver);
- fprintf(stderr, "ERROR: %ld: Unable to send print file to printer (canceled:%ld)\n", status, err);
+ print_ptr = print_buffer;
+
+ fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
+ (int)g.print_bytes);
+ }
+
+ if (g.print_bytes)
+ {
+ bytes = g.print_bytes;
+
+ status = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0);
+
+ /*
+ * Ignore timeout errors...
+ */
+
+ if (status == kIOUSBTransactionTimeout)
+ {
+ status = 0;
+ bytes = 0;
+ }
+
+ if (status || bytes < 0)
+ {
+ /*
+ * Write error - bail if we don't see an error we can retry...
+ */
+
+ OSStatus err = (*g.classdriver)->Abort(g.classdriver);
+ _cupsLangPrintf(stderr, _("ERROR: %ld: (canceled:%ld)\n"),
+ (long)status, (long)err);
+ status = CUPS_BACKEND_STOP;
break;
}
- nbytes -= wbytes;
- bufptr += wbytes;
+ fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
+
+ g.print_bytes -= bytes;
+ print_ptr += bytes;
+ total_bytes += bytes;
}
- if (fd != 0 && status == noErr)
- fprintf(stderr, "DEBUG: Sending print file, %qd bytes...\n", (off_t)tbytes);
+ if (print_fd != 0 && status == noErr)
+ fprintf(stderr, "DEBUG: Sending print file, %lld bytes...\n",
+ (off_t)total_bytes);
}
}
- printer_data.writeDone = 1;
- pthread_mutex_unlock(writeCompMutexPtr);
+ fprintf(stderr, "DEBUG: Sent %lld bytes...\n", (off_t)total_bytes);
- if (thread_created) {
- /* Signal the read thread that we are done... */
- printer_data.done = 1;
+ /*
+ * Wait for the side channel thread to exit...
+ */
- /* Give the read thread WAITEOF_DELAY seconds to complete all the data. If
- * we are not signaled in that time then force the thread to exit by setting
- * the waiteof to be false. Plese note that this relies on us using the timeout
- * class driver.
- */
- struct timespec sleepUntil = { time(NULL) + WAITEOF_DELAY, 0 };
- pthread_mutex_lock(&printer_data.readMutex);
- if (pthread_cond_timedwait(&printer_data.readCompleteCondition, &printer_data.readMutex, (const struct timespec *)&sleepUntil) != 0)
- printer_data.waitEOF = false;
- pthread_mutex_unlock(&printer_data.readMutex);
- pthread_join( thr,NULL); /* wait for the child thread to return */
- }
+ if (have_sidechannel)
+ {
+ close(CUPS_SC_FD);
+ pthread_mutex_lock(&g.readwrite_lock_mutex);
+ g.readwrite_lock = 0;
+ pthread_cond_signal(&g.readwrite_lock_cond);
+ pthread_mutex_unlock(&g.readwrite_lock_mutex);
+
+ g.sidechannel_thread_stop = 1;
+ pthread_mutex_lock(&g.sidechannel_thread_mutex);
+ if (!g.sidechannel_thread_done)
+ {
+ /*
+ * Wait for the side-channel thread to exit...
+ */
- if (reqWait_create) {
- /* Signal the cupsSideChannelDoRequest wait thread that we are done... */
- printer_data.reqWaitDone = 1;
+ cond_timeout.tv_sec = time(NULL) + WAIT_SIDE_DELAY;
+ cond_timeout.tv_nsec = 0;
+ if (pthread_cond_timedwait(&g.sidechannel_thread_cond,
+ &g.sidechannel_thread_mutex,
+ &cond_timeout) != 0)
+ {
+ /*
+ * Force the side-channel thread to exit...
+ */
- /*
- * Give the cupsSideChannelDoRequest wait thread WAITEOF_DELAY seconds to complete
- * all the data. If we are not signaled in that time then force the thread to exit
- * by setting the waiteof to be false. Plese note that this relies on us using the
- * timeout class driver.
- */
- struct timespec reqWaitSleepUntil = { time(NULL) + WAITEOF_DELAY, 0 };
- pthread_mutex_lock(&printer_data.reqWaitMutex);
-
- while (!printer_data.reqWqitFlag) {
- if (pthread_cond_timedwait(&printer_data.reqWaitCompCond,
- &printer_data.reqWaitMutex,
- (const struct timespec *)&reqWaitSleepUntil) != 0) {
- printer_data.waitEOF = false;
- printer_data.reqWqitFlag = 1;
+ pthread_kill(sidechannel_thread_id, SIGTERM);
}
}
- pthread_mutex_unlock(&printer_data.reqWaitMutex);
- pthread_join(reqWaitThread,NULL); /* wait for the child thread to return */
+ 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);
}
- /* interface close wait mutex(for softreset) */
- pthread_mutex_lock(waitCloseMutexPtr);
- pthread_mutex_unlock(waitCloseMutexPtr);
+ pthread_cond_destroy(&g.readwrite_lock_cond);
+ pthread_mutex_destroy(&g.readwrite_lock_mutex);
- /*
- * Close the connection and input file and general clean up...
- */
- registry_close(&printer_data);
+ /*
+ * Signal the read thread to stop...
+ */
- if (STDIN_FILENO != fd)
- close(fd);
+ g.read_thread_stop = 1;
- if (readCompleteConditionPtr != NULL)
- pthread_cond_destroy(&printer_data.readCompleteCondition);
+ /*
+ * 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.
+ */
- if (readMutexPtr != NULL)
- pthread_mutex_destroy(&printer_data.readMutex);
+ pthread_mutex_lock(&g.read_thread_mutex);
- if (waitCloseMutexPtr != NULL)
- pthread_mutex_destroy(&printer_data.waitCloseMutex);
+ if (!g.read_thread_done)
+ {
+ cond_timeout.tv_sec = time(NULL) + WAIT_EOF_DELAY;
+ cond_timeout.tv_nsec = 0;
- if (writeCompMutexPtr != NULL)
- pthread_mutex_destroy(&printer_data.writeCompMutex);
+ if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex,
+ &cond_timeout) != 0)
+ {
+ /*
+ * Force the read thread to exit...
+ */
- if (reqWaitCompCondPtr != NULL)
- pthread_cond_destroy(&printer_data.reqWaitCompCond);
+ pthread_kill(read_thread_id, SIGTERM);
+ }
+ }
+ pthread_mutex_unlock(&g.read_thread_mutex);
- if (reqWaitMutexPtr != NULL)
- pthread_mutex_destroy(&printer_data.reqWaitMutex);
+ pthread_join(read_thread_id, NULL); /* wait for the read thread to return */
- if (printer_data.make != NULL)
- CFRelease(printer_data.make);
+ pthread_cond_destroy(&g.read_thread_cond);
+ pthread_mutex_destroy(&g.read_thread_mutex);
- if (printer_data.model != NULL)
- CFRelease(printer_data.model);
+ /*
+ * Close the connection and input file and general clean up...
+ */
- if (printer_data.serial != NULL)
- CFRelease(printer_data.serial);
+ registry_close();
- if (printer_data.printerObj != 0x0)
- IOObjectRelease(printer_data.printerObj);
+ if (print_fd != STDIN_FILENO)
+ close(print_fd);
+
+ if (g.make != NULL)
+ CFRelease(g.make);
+
+ if (g.model != NULL)
+ CFRelease(g.model);
+
+ if (g.serial != NULL)
+ CFRelease(g.serial);
+
+ if (g.printer_obj != 0x0)
+ IOObjectRelease(g.printer_obj);
return status;
}
+
+/*
+ * 'read_thread()' - Thread to read the backchannel data on.
+ */
+
+static void *read_thread(void *reference)
+{
+ UInt8 readbuffer[512];
+ UInt32 rbytes;
+ kern_return_t readstatus;
+ struct mach_timebase_info timeBaseInfo;
+ uint64_t start,
+ delay;
+
+ /* Calculate what 250 milliSeconds are in mach absolute time...
+ */
+ mach_timebase_info(&timeBaseInfo);
+ delay = ((uint64_t)250000000 * (uint64_t)timeBaseInfo.denom) / (uint64_t)timeBaseInfo.numer;
+
+ do
+ {
+ /*
+ * Remember when we started so we can throttle the loop after the read call...
+ */
+
+ start = mach_absolute_time();
+
+ rbytes = sizeof(readbuffer);
+ readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes);
+ if (readstatus == kIOReturnSuccess && rbytes > 0)
+ {
+ cupsBackChannelWrite((char*)readbuffer, rbytes, 1.0);
+
+ /* cntrl-d is echoed by the printer.
+ * NOTES:
+ * Xerox Phaser 6250D doesn't echo the cntrl-d.
+ * Xerox Phaser 6250D doesn't always send the product query.
+ */
+ if (g.wait_eof && readbuffer[rbytes-1] == 0x4)
+ break;
+
+#ifdef PARSE_PS_ERRORS
+ parse_pserror(readbuffer, rbytes);
+#endif
+ }
+
+ /*
+ * Make sure this loop executes no more than once every 250 miliseconds...
+ */
+
+ if ((readstatus != kIOReturnSuccess || rbytes == 0) && (g.wait_eof || !g.read_thread_stop))
+ mach_wait_until(start + delay);
+
+ } while (g.wait_eof || !g.read_thread_stop); /* Abort from main thread tests error here */
+
+ /*
+ * Let the main thread know that we have completed the read thread...
+ */
+
+ pthread_mutex_lock(&g.read_thread_mutex);
+ g.read_thread_done = 1;
+ pthread_cond_signal(&g.read_thread_cond);
+ pthread_mutex_unlock(&g.read_thread_mutex);
+
+ return NULL;
+}
+
+
+/*
+ * 'sidechannel_thread()' - Handle side-channel requests.
+ */
+
+static void*
+sidechannel_thread(void *reference)
+{
+ cups_sc_command_t command; /* Request command */
+ cups_sc_status_t status; /* Request/response status */
+ char data[2048]; /* Request/response data */
+ int datalen; /* Request/response data size */
+
+
+ do
+ {
+ datalen = sizeof(data);
+
+ if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
+ continue;
+
+ switch (command)
+ {
+ case CUPS_SC_CMD_SOFT_RESET: /* Do a soft reset */
+ if ((*g.classdriver)->SoftReset != NULL)
+ {
+ soft_reset();
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0);
+ }
+ else
+ {
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
+ NULL, 0, 1.0);
+ }
+ break;
+
+ case CUPS_SC_CMD_DRAIN_OUTPUT: /* Drain all pending output */
+ g.drain_output = 1;
+ break;
+
+ case CUPS_SC_CMD_GET_BIDI: /* Is the connection bidirectional? */
+ data[0] = g.bidi_flag;
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
+ break;
+
+ case CUPS_SC_CMD_GET_DEVICE_ID: /* Return IEEE-1284 device ID */
+ datalen = sizeof(data);
+ get_device_id(&status, data, &datalen);
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0);
+ break;
+
+ case CUPS_SC_CMD_GET_STATE: /* Return device state */
+ data[0] = CUPS_SC_STATE_ONLINE;
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0);
+ break;
+
+ default:
+ cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED,
+ NULL, 0, 1.0);
+ break;
+ }
+ }
+ while (!g.sidechannel_thread_stop);
+
+ pthread_mutex_lock(&g.sidechannel_thread_mutex);
+ g.sidechannel_thread_done = 1;
+ pthread_cond_signal(&g.sidechannel_thread_cond);
+ pthread_mutex_unlock(&g.sidechannel_thread_mutex);
+
+ return NULL;
+}
+
+
#pragma mark -
/*
- * 'list_device_callback()' - list_device iterator callback.
+ * 'iterate_printers()' - Iterate over all the printers.
+ */
+
+static void iterate_printers(iterator_callback_t callBack,
+ void *userdata)
+{
+ mach_port_t masterPort = 0x0;
+ kern_return_t kr = IOMasterPort (bootstrap_port, &masterPort);
+
+ if (kr == kIOReturnSuccess && masterPort != 0x0)
+ {
+ io_iterator_t addIterator = 0x0;
+
+ iterator_reference_t reference = { callBack, userdata, true };
+ IONotificationPortRef addNotification = IONotificationPortCreate(masterPort);
+
+ int klass = kUSBPrintingClass;
+ int subklass = kUSBPrintingSubclass;
+
+ CFNumberRef usb_klass = CFNumberCreate(NULL, kCFNumberIntType, &klass);
+ CFNumberRef usb_subklass = CFNumberCreate(NULL, kCFNumberIntType, &subklass);
+ CFMutableDictionaryRef usbPrinterMatchDictionary = IOServiceMatching(kIOUSBInterfaceClassName);
+
+ CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceClass"), usb_klass);
+ CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceSubClass"), usb_subklass);
+
+ CFRelease(usb_klass);
+ CFRelease(usb_subklass);
+
+ kr = IOServiceAddMatchingNotification(addNotification, kIOMatchedNotification, usbPrinterMatchDictionary, &device_added, &reference, &addIterator);
+ if (addIterator != 0x0)
+ {
+ device_added (&reference, addIterator);
+
+ if (reference.keepRunning)
+ {
+ CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification), kCFRunLoopDefaultMode);
+ CFRunLoopRun();
+ }
+ IOObjectRelease(addIterator);
+ }
+ mach_port_deallocate(mach_task_self(), masterPort);
+ }
+}
+
+
+/*
+ * 'device_added()' - Device added notifier.
*/
-static Boolean list_device_callback(void *refcon, io_service_t obj)
+static void device_added(void *userdata,
+ io_iterator_t iterator)
+{
+ iterator_reference_t *reference = userdata;
+
+ io_service_t obj;
+ while (reference->keepRunning && (obj = IOIteratorNext(iterator)) != 0x0)
+ {
+ if (reference->callback != NULL)
+ reference->keepRunning = reference->callback(reference->userdata, obj);
+
+ IOObjectRelease(obj);
+ }
+
+ /* 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)
+ CFRunLoopStop(CFRunLoopGetCurrent());
+}
+
+
+/*
+ * 'list_device_cb()' - list_device iterator callback.
+ */
+
+static Boolean list_device_cb(void *refcon,
+ io_service_t obj)
{
Boolean keepRunning = (obj != 0x0);
- if (keepRunning) {
+ if (keepRunning)
+ {
CFStringRef deviceIDString = NULL;
UInt32 deviceLocation = 0;
+ UInt8 interfaceNum = 0;
- copy_devicestring(obj, &deviceIDString, &deviceLocation);
- if (deviceIDString != NULL) {
+ 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];
+ char optionsstr[1024], idstr[1024], make_modelstr[1024];
copy_deviceinfo(deviceIDString, &make, &model, &serial);
-
- modelstr[0] = '/';
-
CFStringGetCString(deviceIDString, idstr, sizeof(idstr),
kCFStringEncodingUTF8);
+ backendGetMakeModel(idstr, make_modelstr, sizeof(make_modelstr));
- if (make)
- CFStringGetCString(make, makestr, sizeof(makestr),
- kCFStringEncodingUTF8);
- else
+ modelstr[0] = '/';
+
+ if (!CFStringGetCString(make, makestr, sizeof(makestr),
+ kCFStringEncodingUTF8))
strcpy(makestr, "Unknown");
- if (model)
- CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1,
- kCFStringEncodingUTF8);
- else
+ if (!CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1,
+ kCFStringEncodingUTF8))
strcpy(modelstr + 1, "Printer");
- /*
- * Fix common HP 1284 bug...
- */
-
- if (!strcasecmp(makestr, "Hewlett-Packard"))
- strcpy(makestr, "HP");
-
- if (!strncasecmp(modelstr + 1, "hp ", 3))
- _cups_strcpy(modelstr + 1, modelstr + 4);
-
optionsstr[0] = '\0';
if (serial != NULL)
{
snprintf(optionsstr, sizeof(optionsstr), "?serial=%s", serialstr);
}
else if (deviceLocation != 0)
- {
- snprintf(optionsstr, sizeof(optionsstr), "?location=%lx", deviceLocation);
- }
+ snprintf(optionsstr, sizeof(optionsstr), "?location=%x", (unsigned)deviceLocation);
httpAssembleURI(HTTP_URI_CODING_ALL, uristr, sizeof(uristr), "usb", NULL, makestr, 0, modelstr);
strncat(uristr, optionsstr, sizeof(uristr));
- printf("direct %s \"%s %s\" \"%s %s USB\" \"%s\"\n", uristr, makestr,
- &modelstr[1], makestr, &modelstr[1], idstr);
+ printf("direct %s \"%s\" \"%s USB\" \"%s\"\n", uristr, make_modelstr,
+ make_modelstr, idstr);
release_deviceinfo(&make, &model, &serial);
CFRelease(deviceIDString);
/*
- * 'find_device_callback()' - print_device iterator callback.
+ * 'find_device_cb()' - print_device iterator callback.
*/
-static Boolean find_device_callback(void *refcon, io_service_t obj)
+static Boolean find_device_cb(void *refcon,
+ io_service_t obj)
{
Boolean keepLooking = true;
- printer_data_t *userData = (printer_data_t *)refcon;
- if (obj != 0x0) {
+ if (obj != 0x0)
+ {
CFStringRef idString = NULL;
UInt32 location = -1;
+ UInt8 interfaceNum = 0;
- copy_devicestring(obj, &idString, &location);
- if (idString != NULL) {
+ copy_devicestring(obj, &idString, &location, &interfaceNum);
+ if (idString != NULL)
+ {
CFStringRef make = NULL, model = NULL, serial = NULL;
copy_deviceinfo(idString, &make, &model, &serial);
- if (CFStringCompare(make, userData->make, kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
- if (CFStringCompare(model, userData->model, kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
- if (userData->serial != NULL) {
- if (serial != NULL && CFStringCompare(serial, userData->serial, kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
+ 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);
- userData->printerObj = obj;
+ g.printer_obj = obj;
keepLooking = false;
}
}
- else {
- if (userData->printerObj != 0) {
- IOObjectRetain(userData->printerObj);
- }
- userData->printerObj = obj;
+ else
+ {
+ if (g.printer_obj != 0)
+ IOObjectRelease(g.printer_obj);
+
+ g.printer_obj = obj;
IOObjectRetain(obj);
- if (userData->location == 0 || userData->location == location) {
+ if (g.location == 0 || g.location == location)
keepLooking = false;
- }
}
+ if ( !keepLooking )
+ g.interfaceNum = interfaceNum;
}
}
CFRelease(idString);
}
}
- else {
- keepLooking = (userData->printerObj == 0);
- if (obj == 0x0 && keepLooking) {
- CFRunLoopTimerContext context = { 0, userData, NULL, NULL, NULL };
- CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + 1.0, 10, 0x0, 0x0, statusTimerCallback, &context);
- if (timer != NULL) {
+ else
+ {
+ keepLooking = (g.printer_obj == 0);
+ if (obj == 0x0 && 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);
- userData->statusTimer = timer;
+ g.status_timer = timer;
}
}
}
-
- if (!keepLooking && userData->statusTimer != NULL) {
+
+ if (!keepLooking && g.status_timer != NULL)
+ {
fputs("STATE: -offline-error\n", stderr);
- fputs("INFO: Printer is now on-line.\n", stderr);
- CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), userData->statusTimer, kCFRunLoopDefaultMode);
- CFRelease(userData->statusTimer);
- userData->statusTimer = NULL;
+ _cupsLangPuts(stderr, _("INFO: Printer is now online.\n"));
+ CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), g.status_timer, kCFRunLoopDefaultMode);
+ CFRelease(g.status_timer);
+ g.status_timer = NULL;
}
return keepLooking;
}
-static void statusTimerCallback (CFRunLoopTimerRef timer, void *info)
-{
- fputs("STATE: +offline-error\n", stderr);
- fputs("INFO: Printer is currently off-line.\n", stderr);
-}
-#pragma mark -
/*
- * 'iterate_printers()' - iterate over all the printers.
+ * 'status_timer_cb()' - Status timer callback.
*/
-static void iterate_printers(iterator_callback_t callBack, void *userdata)
+static void status_timer_cb(CFRunLoopTimerRef timer,
+ void *info)
{
- mach_port_t masterPort = 0x0;
- kern_return_t kr = IOMasterPort (bootstrap_port, &masterPort);
-
- if (kr == kIOReturnSuccess && masterPort != 0x0) {
- io_iterator_t addIterator = 0x0;
-
- iterator_reference_t reference = { callBack, userdata, true };
- IONotificationPortRef addNotification = IONotificationPortCreate(masterPort);
-
- int klass = kUSBPrintingClass;
- int subklass = kUSBPrintingSubclass;
-
- CFNumberRef usb_klass = CFNumberCreate(NULL, kCFNumberIntType, &klass);
- CFNumberRef usb_subklass = CFNumberCreate(NULL, kCFNumberIntType, &subklass);
- CFMutableDictionaryRef usbPrinterMatchDictionary = IOServiceMatching(kIOUSBInterfaceClassName);
-
- CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceClass"), usb_klass);
- CFDictionaryAddValue(usbPrinterMatchDictionary, CFSTR("bInterfaceSubClass"), usb_subklass);
-
- CFRelease(usb_klass);
- CFRelease(usb_subklass);
-
- kr = IOServiceAddMatchingNotification(addNotification, kIOMatchedNotification, usbPrinterMatchDictionary, &device_added, &reference, &addIterator);
- if (addIterator != 0x0) {
- device_added (&reference, addIterator);
-
- if (reference.keepRunning) {
- CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification), kCFRunLoopDefaultMode);
- CFRunLoopRun();
- }
- IOObjectRelease(addIterator);
- }
- mach_port_deallocate(mach_task_self(), masterPort);
- }
-}
-
-
-/*
- * 'device_added()' - device added notifier.
- */
-
-static void device_added(void *userdata, io_iterator_t iterator)
-{
- iterator_reference_t *reference = userdata;
-
- io_service_t obj;
- while (reference->keepRunning && (obj = IOIteratorNext(iterator)) != 0x0) {
- if (reference->callback != NULL) {
- reference->keepRunning = reference->callback(reference->userdata, obj);
- }
- IOObjectRelease(obj);
- }
-
- /* 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);
+ fputs("STATE: +offline-error\n", stderr);
+ _cupsLangPuts(stderr, _("INFO: Printer is currently offline.\n"));
- if (!reference->keepRunning) {
- CFRunLoopStop(CFRunLoopGetCurrent());
+ if (getenv("CLASS") != NULL)
+ {
+ /*
+ * If the CLASS environment variable is set, the job was submitted
+ * to a class and not to a specific queue. In this case, we want
+ * to abort immediately so that the job can be requeued on the next
+ * available printer in the class.
+ *
+ * Sleep 5 seconds to keep the job from requeuing too rapidly...
+ */
+
+ sleep(5);
+
+ exit(CUPS_BACKEND_FAILED);
}
}
* 'copy_deviceinfo()' - Copy strings from the 1284 device ID.
*/
-static void copy_deviceinfo(CFStringRef deviceIDString, CFStringRef *make, CFStringRef *model, CFStringRef *serial)
-{
+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 ((*make = copy_value_for_key(deviceIDString, makeKeys)) == NULL)
+ *make = CFStringCreateWithCString(kCFAllocatorDefault, "Unknown",
+ kCFStringEncodingUTF8);
+ }
+
if (model != NULL)
- *model = copy_value_for_key(deviceIDString, modelKeys);
+ {
+ if ((*model = copy_value_for_key(deviceIDString, modelKeys)) == NULL)
+ *model = CFStringCreateWithCString(kCFAllocatorDefault, "Printer",
+ kCFStringEncodingUTF8);
+ }
+
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)
+static void release_deviceinfo(CFStringRef *make,
+ CFStringRef *model,
+ CFStringRef *serial)
{
- if (make != NULL && *make != NULL) {
+ if (make != NULL && *make != NULL)
+ {
CFRelease(*make);
*make = NULL;
}
- if (model != NULL && *model != NULL) {
+ if (model != NULL && *model != NULL)
+ {
CFRelease(*model);
*model = NULL;
}
- if (serial != NULL && *serial != NULL) {
+ if (serial != NULL && *serial != NULL)
+ {
CFRelease(*serial);
*serial = NULL;
}
* 'load_classdriver()' - Load a classdriver.
*/
-static kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t intf, classdriver_context_t ***printerDriver)
+static kern_return_t load_classdriver(CFStringRef driverPath,
+ printer_interface_t intf,
+ classdriver_t ***printerDriver)
{
kern_return_t kr = kUSBPrinterClassDeviceNotOpen;
- classdriver_context_t **driver = NULL;
+ classdriver_t **driver = NULL;
CFStringRef bundle = (driverPath == NULL ? kUSBGenericTOPrinterClassDriver : driverPath);
- if ( NULL != bundle ) {
+ if (bundle != NULL)
+ {
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, bundle, kCFURLPOSIXPathStyle, true);
CFPlugInRef plugin = (url != NULL ? CFPlugInCreate(NULL, url) : NULL);
- if (url != NULL)
+ if (url != NULL)
CFRelease(url);
- if (plugin != NULL) {
+ if (plugin != NULL)
+ {
CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID, plugin);
- if (factories != NULL && CFArrayGetCount(factories) > 0) {
+ if (factories != NULL && CFArrayGetCount(factories) > 0)
+ {
CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0);
IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kUSBPrinterClassTypeID);
- if (NULL != iunknown) {
+ if (iunknown != NULL)
+ {
kr = (*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID), (LPVOID *)&driver);
- if (kr == kIOReturnSuccess && driver != NULL) {
- classdriver_context_t **genericDriver = NULL;
- if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo) {
+ if (kr == kIOReturnSuccess && driver != NULL)
+ {
+ classdriver_t **genericDriver = NULL;
+ if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo)
kr = load_classdriver(NULL, intf, &genericDriver);
- }
- if (kr == kIOReturnSuccess) {
+ if (kr == kIOReturnSuccess)
+ {
(*driver)->interface = intf;
(*driver)->Initialize(driver, genericDriver);
#ifdef DEBUG
char bundlestr[1024];
CFStringGetCString(bundle, bundlestr, sizeof(bundlestr), kCFStringEncodingUTF8);
- fprintf(stderr, "DEBUG:load_classdriver(%s) (kr:0x%08x)\n", bundlestr, (int)kr);
+ fprintf(stderr, "DEBUG: load_classdriver(%s) (kr:0x%08x)\n", bundlestr, (int)kr);
#endif /* DEBUG */
return kr;
* 'unload_classdriver()' - Unload a classdriver.
*/
-static kern_return_t unload_classdriver(classdriver_context_t ***classDriver)
+static kern_return_t unload_classdriver(classdriver_t ***classdriver)
{
- if (*classDriver != NULL) {
- (**classDriver)->Release(*classDriver);
- *classDriver = NULL;
+ if (*classdriver != NULL)
+ {
+ (**classdriver)->Release(*classdriver);
+ *classdriver = NULL;
}
return kIOReturnSuccess;
/*
- * 'load_printerdriver()' - Load a vendor's (or generic) classdriver.
+ * 'load_printerdriver()' - Load vendor's classdriver.
*
* If driverBundlePath is not NULL on return it is the callers responsbility to release it!
*/
-static kern_return_t load_printerdriver(printer_data_t *printer, CFStringRef *driverBundlePath)
+static kern_return_t load_printerdriver(CFStringRef *driverBundlePath)
{
IOCFPlugInInterface **iodev = NULL;
SInt32 score;
printer_interface_t intf;
HRESULT res;
- kr = IOCreatePlugInInterfaceForService(printer->printerObj, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
+ kr = IOCreatePlugInInterfaceForService(g.printer_obj, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
if (kr == kIOReturnSuccess)
{
if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *) &intf)) == noErr)
{
- *driverBundlePath = IORegistryEntryCreateCFProperty(printer->printerObj, kUSBClassDriverProperty, NULL, kNilOptions);
+ *driverBundlePath = IORegistryEntryCreateCFProperty(g.printer_obj, kUSBClassDriverProperty, NULL, kNilOptions);
- kr = load_classdriver(*driverBundlePath, intf, &printer->printerDriver);
+ kr = load_classdriver(*driverBundlePath, intf, &g.classdriver);
if (kr != kIOReturnSuccess)
(*intf)->Release(intf);
* 'registry_open()' - Open a connection to the printer.
*/
-static kern_return_t registry_open(printer_data_t *printer, CFStringRef *driverBundlePath)
+static kern_return_t registry_open(CFStringRef *driverBundlePath)
{
- printer->directionalFlag = 0;
+ g.bidi_flag = 0; /* 0=unidirectional */
- kern_return_t kr = load_printerdriver(printer, driverBundlePath);
- if (kr != kIOReturnSuccess) {
+ kern_return_t kr = load_printerdriver(driverBundlePath);
+ if (kr != kIOReturnSuccess)
kr = -2;
- }
- if (printer->printerDriver != NULL) {
- kr = (*(printer->printerDriver))->Open(printer->printerDriver, printer->location, kUSBPrintingProtocolBidirectional);
- if (kr != kIOReturnSuccess || (*(printer->printerDriver))->interface == NULL) {
- kr = (*(printer->printerDriver))->Open(printer->printerDriver, printer->location, kUSBPrintingProtocolUnidirectional);
- if (kr == kIOReturnSuccess) {
- if ((*(printer->printerDriver))->interface == NULL) {
- (*(printer->printerDriver))->Close(printer->printerDriver);
+ if (g.classdriver != NULL)
+ {
+ (*g.classdriver)->interfaceNumber = g.interfaceNum;
+ kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolBidirectional);
+ if (kr != kIOReturnSuccess || (*g.classdriver)->interface == NULL)
+ {
+ kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolUnidirectional);
+ if (kr == kIOReturnSuccess)
+ {
+ if ((*g.classdriver)->interface == NULL)
+ {
+ (*g.classdriver)->Close(g.classdriver);
kr = -1;
}
}
- } else {
- printer->directionalFlag = 1;
}
+ else
+ g.bidi_flag = 1; /* 1=bidirectional */
}
- if (kr != kIOReturnSuccess) {
- unload_classdriver(&printer->printerDriver);
- }
+ if (kr != kIOReturnSuccess)
+ unload_classdriver(&g.classdriver);
return kr;
}
* 'registry_close()' - Close the connection to the printer.
*/
-static kern_return_t registry_close(printer_data_t *printer)
+static kern_return_t registry_close()
{
- if (printer->printerDriver != NULL) {
- (*(printer->printerDriver))->Close(printer->printerDriver);
- }
- unload_classdriver(&printer->printerDriver);
+ if (g.classdriver != NULL)
+ (*g.classdriver)->Close(g.classdriver);
+
+ unload_classdriver(&g.classdriver);
return kIOReturnSuccess;
}
* 'copy_deviceid()' - Copy the 1284 device id string.
*/
-static OSStatus copy_deviceid(classdriver_context_t **printer, CFStringRef *deviceID)
+static OSStatus copy_deviceid(classdriver_t **classdriver,
+ CFStringRef *deviceID)
{
CFStringRef devID = NULL,
deviceModel = NULL,
deviceSerial = NULL;
- OSStatus err = (*printer)->GetDeviceID(printer, &devID, DEFAULT_TIMEOUT);
+ OSStatus err = (*classdriver)->GetDeviceID(classdriver, &devID, DEFAULT_TIMEOUT);
copy_deviceinfo(devID, &deviceMake, &deviceModel, &deviceSerial);
- if (deviceMake == NULL || deviceModel == NULL || deviceSerial == NULL) {
+ if (deviceMake == NULL || deviceModel == NULL || deviceSerial == NULL)
+ {
IOUSBDeviceDescriptor desc;
iodevice_request_t request;
- request.requestType = USBmakebmRequestType( kUSBIn, kUSBStandard, kUSBDevice );
+ 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 = (*printer)->DeviceRequest(printer, &request, DEFAULT_TIMEOUT);
- if (err == kIOReturnSuccess) {
+ err = (*classdriver)->DeviceRequest(classdriver, &request, DEFAULT_TIMEOUT);
+ if (err == kIOReturnSuccess)
+ {
CFMutableStringRef newDevID = CFStringCreateMutable(NULL, 0);
- if (deviceMake == NULL) {
+ if (deviceMake == NULL)
+ {
CFStringRef data = NULL;
- err = (*printer)->GetString(printer, desc.iManufacturer, kUSBLanguageEnglish, DEFAULT_TIMEOUT, &data);
- if (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) {
+ if (deviceModel == NULL)
+ {
CFStringRef data = NULL;
- err = (*printer)->GetString(printer, desc.iProduct, kUSBLanguageEnglish, DEFAULT_TIMEOUT, &data);
- if (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) {
+ if (deviceSerial == NULL && desc.iSerialNumber != 0)
+ {
CFStringRef data = NULL;
- err = (*printer)->GetString(printer, desc.iSerialNumber, kUSBLanguageEnglish, DEFAULT_TIMEOUT, &data);
- if (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) {
+ if (devID != NULL)
+ {
CFStringAppend(newDevID, devID);
CFRelease(devID);
}
*deviceID = newDevID;
}
}
- else {
+ else
+ {
*deviceID = devID;
}
release_deviceinfo(&deviceMake, &deviceModel, &deviceSerial);
* 'copy_devicestring()' - Copy the 1284 device id string.
*/
-static void copy_devicestring(io_service_t usbInterface, CFStringRef *deviceID, UInt32 *deviceLocation)
+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_context_t **klassDriver = NULL;
+ classdriver_t **klassDriver = NULL;
CFStringRef driverBundlePath;
- kr = IOCreatePlugInInterfaceForService(usbInterface, kIOUSBInterfaceUserClientTypeID,
- kIOCFPlugInInterfaceID, &iodev, &score);
- if (kr == kIOReturnSuccess)
+ if ((kr = IOCreatePlugInInterfaceForService(usbInterface,
+ kIOUSBInterfaceUserClientTypeID,
+ kIOCFPlugInInterfaceID,
+ &iodev, &score)) == kIOReturnSuccess)
{
- if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *) &intf)) == noErr)
+ if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *)
+ &intf)) == noErr)
{
- /* ignore the result for location id... */
- (void)(*intf)->GetLocationID(intf, deviceLocation);
+ (*intf)->GetLocationID(intf, deviceLocation);
+ (*intf)->GetInterfaceNumber(intf, interfaceNumber);
- driverBundlePath = IORegistryEntryCreateCFProperty( usbInterface, kUSBClassDriverProperty, NULL, kNilOptions );
+ 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);
+ if (kr == kIOReturnSuccess && klassDriver != NULL)
+ kr = copy_deviceid(klassDriver, deviceID);
unload_classdriver(&klassDriver);
CFRelease(driverBundlePath);
/* (*intf)->Release(intf); */
- }
+ }
IODestroyPlugInInterface(iodev);
}
}
* 'copy_value_for_key()' - Copy value string associated with a key.
*/
-static CFStringRef copy_value_for_key(CFStringRef deviceID, CFStringRef *keys)
+static CFStringRef copy_value_for_key(CFStringRef deviceID,
+ CFStringRef *keys)
{
CFStringRef value = NULL;
CFArrayRef kvPairs = deviceID != NULL ? CFStringCreateArrayBySeparatingStrings(NULL, deviceID, CFSTR(";")) : NULL;
CFIndex max = kvPairs != NULL ? CFArrayGetCount(kvPairs) : 0;
CFIndex idx = 0;
- while (idx < max && value == NULL) {
+ while (idx < max && value == NULL)
+ {
CFStringRef kvpair = CFArrayGetValueAtIndex(kvPairs, idx);
CFIndex idxx = 0;
- while (keys[idxx] != NULL && value == NULL) {
+ while (keys[idxx] != NULL && value == NULL)
+ {
CFRange range = CFStringFind(kvpair, keys[idxx], kCFCompareCaseInsensitive);
- if (range.length != -1) {
- if (range.location != 0) {
+ if (range.length != -1)
+ {
+ if (range.location != 0)
+ {
CFMutableStringRef theString = CFStringCreateMutableCopy(NULL, 0, kvpair);
CFStringTrimWhitespace(theString);
range = CFStringFind(theString, keys[idxx], kCFCompareCaseInsensitive);
- if (range.location == 0) {
+ if (range.location == 0)
value = CFStringCreateWithSubstring(NULL, theString, CFRangeMake(range.length, CFStringGetLength(theString) - range.length));
- }
+
CFRelease(theString);
}
- else {
+ else
+ {
CFStringRef theString = CFStringCreateWithSubstring(NULL, kvpair, CFRangeMake(range.length, CFStringGetLength(kvpair) - range.length));
CFMutableStringRef theString2 = CFStringCreateMutableCopy(NULL, 0, theString);
CFRelease(theString);
}
if (kvPairs != NULL)
- CFRelease(kvPairs);
+ CFRelease(kvPairs);
return value;
}
/*
- * 'cfstr_create_and_trim()' - Create a CFString from a c-string and
- * trim it's whitespace characters.
+ * 'cfstr_create_trim()' - Create CFString and trim whitespace characters.
*/
-CFStringRef cfstr_create_and_trim(const char *cstr)
+CFStringRef cfstr_create_trim(const char *cstr)
{
CFStringRef cfstr;
CFMutableStringRef cfmutablestr = NULL;
-
+
if ((cfstr = CFStringCreateWithCString(NULL, cstr, kCFStringEncodingUTF8)) != NULL)
{
if ((cfmutablestr = CFStringCreateMutableCopy(NULL, 1024, cfstr)) != NULL)
#pragma mark -
/*
- * 'parse_options()' - Parse uri options.
+ * 'parse_options()' - Parse URI options.
*/
-static void parse_options(const char *options, char *serial, UInt32 *location, Boolean *waitEOF)
+static void parse_options(char *options,
+ char *serial,
+ int serial_size,
+ UInt32 *location,
+ Boolean *wait_eof)
{
- char *serialnumber; /* ?serial=<serial> or ?location=<location> */
- char optionName[255], /* Name of option */
- value[255], /* Value of option */
- *ptr; /* Pointer into name or value */
+ char sep, /* Separator character */
+ *name, /* Name of option */
+ *value; /* Value of option */
+
if (serial)
*serial = '\0';
if (!options)
return;
- serialnumber = NULL;
-
- while (*options != '\0') {
- /* Get the name... */
- for (ptr = optionName; *options && *options != '=' && *options != '+'; )
- *ptr++ = *options++;
+ while (*options)
+ {
+ /*
+ * Get the name...
+ */
- *ptr = '\0';
- value[0] = '\0';
+ name = options;
- if (*options == '=') {
- /* Get the value... */
+ while (*options && *options != '=' && *options != '+' && *options != '&')
options ++;
- for (ptr = value; *options && *options != '+';)
- *ptr++ = *options++;
+ if ((sep = *options) != '\0')
+ *options++ = '\0';
+
+ if (sep == '=')
+ {
+ /*
+ * Get the value...
+ */
- *ptr = '\0';
+ value = options;
- if (*options == '+')
+ while (*options && *options != '+' && *options != '&')
options ++;
- }
- else if (*options == '+') {
- options ++;
- }
- /*
- * Process the option...
- */
- if (strcasecmp(optionName, "waiteof") == 0) {
- if (strcasecmp(value, "on") == 0 ||
- strcasecmp(value, "yes") == 0 ||
- strcasecmp(value, "true") == 0) {
- *waitEOF = true;
- }
- else if (strcasecmp(value, "off") == 0 ||
- strcasecmp(value, "no") == 0 ||
- strcasecmp(value, "false") == 0) {
- *waitEOF = false;
- }
- else {
- fprintf(stderr, "WARNING: Boolean expected for waiteof option \"%s\"\n", value);
- }
+ if (*options)
+ *options++ = '\0';
}
- else if (strcasecmp(optionName, "serial") == 0) {
- strcpy(serial, value);
- serialnumber = serial;
+ else
+ value = (char *)"";
+
+ /*
+ * Process the option...
+ */
+
+ if (!strcasecmp(name, "waiteof"))
+ {
+ if (!strcasecmp(value, "on") ||
+ !strcasecmp(value, "yes") ||
+ !strcasecmp(value, "true"))
+ *wait_eof = true;
+ else if (!strcasecmp(value, "off") ||
+ !strcasecmp(value, "no") ||
+ !strcasecmp(value, "false"))
+ *wait_eof = false;
+ else
+ _cupsLangPrintf(stderr,
+ _("WARNING: Boolean expected for waiteof option "
+ "\"%s\"\n"), value);
}
- else if (strcasecmp(optionName, "location") == 0 && location) {
+ else if (!strcasecmp(name, "serial"))
+ strlcpy(serial, value, serial_size);
+ else if (!strcasecmp(name, "location") && location)
*location = strtol(value, NULL, 16);
- }
}
-
- return;
}
/*!
* @function setup_cfLanguage
- * @abstract Convert the contents of the CUPS 'LANG' environment
+ * @abstract Convert the contents of the CUPS 'APPLE_LANGUAGE' environment
* variable into a one element CF array of languages.
*
* @discussion Each submitted job comes with a natural language. CUPS passes
CFArrayRef langArray = NULL;
const char *requestedLang = NULL;
- requestedLang = getenv("LANG");
- if (requestedLang != NULL) {
+ if ((requestedLang = getenv("APPLE_LANGUAGE")) == NULL)
+ requestedLang = getenv("LANG");
+
+ if (requestedLang != NULL)
+ {
lang[0] = CFStringCreateWithCString(kCFAllocatorDefault, requestedLang, kCFStringEncodingUTF8);
langArray = CFArrayCreate(kCFAllocatorDefault, (const void **)lang, sizeof(lang) / sizeof(lang[0]), &kCFTypeArrayCallBacks);
CFPreferencesSetAppValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication);
- DEBUG_printf((stderr, "DEBUG: usb: AppleLanguages = \"%s\"\n", requestedLang));
+ fprintf(stderr, "DEBUG: usb: AppleLanguages = \"%s\"\n", requestedLang);
CFRelease(lang[0]);
CFRelease(langArray);
- } else {
- fprintf(stderr, "DEBUG: usb: LANG environment variable missing.\n");
}
+ else
+ fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr);
}
#pragma mark -
*
* @result Never returns; always calls exit().
*
- * @discussion
+ * @discussion
*/
-static void run_ppc_backend(int argc, char *argv[], int fd)
+static void run_ppc_backend(int argc,
+ char *argv[],
+ int fd)
{
int i;
int exitstatus = 0;
*/
usb_ppc_status = getenv("USB_PPC_STATUS");
- if (usb_ppc_status == NULL) {
- /* Catch SIGTERM if we are _not_ printing data from
- * stdin (otherwise you can't cancel raw jobs...)
- */
+ if (usb_ppc_status == NULL)
+ {
+ /*
+ * Setup a SIGTERM handler then block it before forking...
+ */
+
+ struct sigaction action; /* POSIX signal action */
+ sigset_t newmask, /* New signal mask */
+ oldmask; /* Old signal mask */
+
+ memset(&action, 0, sizeof(action));
+ sigaddset(&action.sa_mask, SIGTERM);
+ action.sa_handler = sigterm_handler;
+ sigaction(SIGTERM, &action, NULL);
+
+ sigemptyset(&newmask);
+ sigaddset(&newmask, SIGTERM);
+ sigprocmask(SIG_BLOCK, &newmask, &oldmask);
+
+ if ((child_pid = fork()) == 0)
+ {
+ /*
+ * Child comes here...
+ */
+
+ setenv("USB_PPC_STATUS", "1", false);
+
+ /*
+ * Unblock signals before doing the exec...
+ */
- if (fd != 0) {
-#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
- sigset(SIGTERM, sigterm_handler);
-#elif defined(HAVE_SIGACTION)
- struct sigaction action; /* Actions for POSIX signals */
memset(&action, 0, sizeof(action));
- sigaddset(&action.sa_mask, SIGTERM);
- action.sa_handler = sigterm_handler;
+ sigemptyset(&action.sa_mask);
+ action.sa_handler = SIG_DFL;
sigaction(SIGTERM, &action, NULL);
-#else
- signal(SIGTERM, sigterm_handler);
-#endif /* HAVE_SIGSET */
- }
- if ((child_pid = fork()) == 0) {
- /* Child comes here. */
- setenv("USB_PPC_STATUS", "1", false);
+ sigprocmask(SIG_SETMASK, &oldmask, NULL);
+
+ /*
+ * Tell the kernel the next exec call should favor the ppc architecture...
+ */
- /* Tell the kernel we want the next exec call to favor the ppc architecture... */
int mib[] = { CTL_KERN, KERN_AFFINITY, 1, 1 };
int namelen = 4;
sysctl(mib, namelen, NULL, NULL, NULL, 0);
- /* Set up the arguments and call exec... */
+ /*
+ * Set up the arguments and call exec...
+ */
+
for (i = 0; i < argc && i < (sizeof(my_argv)/sizeof(my_argv[0])) - 1; i++)
my_argv[i] = argv[i];
execv("/usr/libexec/cups/backend/usb", my_argv);
- fprintf(stderr, "DEBUG: execv: %s\n", strerror(errno));
- exitstatus = errno;
+ _cupsLangPrintError(_("ERROR: Unable to exec /usr/libexec/cups/backend/usb"));
+ exit(errno);
+ }
+ else if (child_pid < 0)
+ {
+ /*
+ * Error - couldn't fork a new process!
+ */
+
+ _cupsLangPrintError(_("ERROR: Unable to fork"));
+ exit(errno);
}
- else if (child_pid > 0) {
- /* Parent comes here.
- *
- * Close the fds we won't be using then wait for the child backend to exit.
- */
- close(fd);
- close(1);
- fprintf(stderr, "DEBUG: Started usb(ppc) backend (PID %d)\n", (int)child_pid);
+ /*
+ * Unblock signals...
+ */
- while ((waitpid_status = waitpid(child_pid, &childstatus, 0)) == (pid_t)-1 && errno == EINTR)
- usleep(1000);
+ sigprocmask(SIG_SETMASK, &oldmask, NULL);
- if (WIFSIGNALED(childstatus)) {
- exitstatus = WTERMSIG(childstatus);
- fprintf(stderr, "DEBUG: usb(ppc) backend %d crashed on signal %d!\n", child_pid, exitstatus);
- }
- else {
- if ((exitstatus = WEXITSTATUS(childstatus)) != 0)
- fprintf(stderr, "DEBUG: usb(ppc) backend %d stopped with status %d!\n", child_pid, exitstatus);
- else
- fprintf(stderr, "DEBUG: PID %d exited with no errors\n", child_pid);
- }
+ /*
+ * Close the fds we won't be using then wait for the child backend to exit.
+ */
+
+ close(fd);
+ close(1);
+
+ fprintf(stderr, "DEBUG: Started usb(ppc) backend (PID %d)\n", (int)child_pid);
+
+ while ((waitpid_status = waitpid(child_pid, &childstatus, 0)) == (pid_t)-1 && errno == EINTR)
+ usleep(1000);
+
+ if (WIFSIGNALED(childstatus))
+ {
+ exitstatus = WTERMSIG(childstatus);
+ fprintf(stderr, "DEBUG: usb(ppc) backend %d crashed on signal %d!\n", child_pid, exitstatus);
}
- else {
- /* fork() error */
- fprintf(stderr, "DEBUG: fork: %s\n", strerror(errno));
- exitstatus = errno;
+ else
+ {
+ if ((exitstatus = WEXITSTATUS(childstatus)) != 0)
+ fprintf(stderr, "DEBUG: usb(ppc) backend %d stopped with status %d!\n", child_pid, exitstatus);
+ else
+ fprintf(stderr, "DEBUG: PID %d exited with no errors\n", child_pid);
}
}
- else {
- fprintf(stderr, "DEBUG: usb child running i386 again\n");
+ else
+ {
+ fputs("DEBUG: usb child running i386 again\n", stderr);
exitstatus = ENOENT;
}
* 'parse_pserror()' - Scan the backchannel data for postscript errors.
*/
-static void parse_pserror (char *sockBuffer, int len)
+static void parse_pserror(char *sockBuffer,
+ int len)
{
static char gErrorBuffer[1024] = "";
static char *gErrorBufferPtr = gErrorBuffer;
gErrorBufferPtr += len;
*(gErrorBufferPtr + 1) = '\0';
-
pLineEnd = (char *)next_line((const char *)gErrorBuffer);
- while (pLineEnd != NULL) {
+ while (pLineEnd != NULL)
+ {
*pLineEnd++ = '\0';
pCommentBegin = strstr(gErrorBuffer,"%%[");
pCommentEnd = strstr(gErrorBuffer, "]%%");
- if (pCommentBegin != gErrorBuffer && pCommentEnd != NULL) {
+ if (pCommentBegin != gErrorBuffer && pCommentEnd != NULL)
+ {
pCommentEnd += 3; /* Skip past "]%%" */
*pCommentEnd = '\0'; /* There's always room for the nul */
else
logLevel = "INFO";
- if ((logstrlen = snprintf(logstr, sizeof(logstr), "%s: %s\n", logLevel, pCommentBegin)) >= sizeof(logstr)) {
+ if ((logstrlen = snprintf(logstr, sizeof(logstr), "%s: %s\n", logLevel, pCommentBegin)) >= sizeof(logstr))
+ {
/* If the string was trucnated make sure it has a linefeed before the nul */
logstrlen = sizeof(logstr) - 1;
logstr[logstrlen - 1] = '\n';
/*
- * 'read_thread()' - A thread to read the backchannel data.
+ * 'soft_reset()' - Send a soft reset to the device.
*/
-static void *read_thread(void *reference)
+static void soft_reset()
{
- /* post a read to the device and write results to stdout
- * the final pending read will be Aborted in the main thread
- */
- UInt8 readbuffer[512];
- UInt32 rbytes;
- kern_return_t readstatus;
- printer_data_t *userData = (printer_data_t *)reference;
- classdriver_context_t **classdriver = userData->printerDriver;
- struct mach_timebase_info timeBaseInfo;
- uint64_t start,
- delay;
-
- /* Calculate what 250 milliSeconds are in mach absolute time...
- */
- mach_timebase_info(&timeBaseInfo);
- delay = ((uint64_t)250000000 * (uint64_t)timeBaseInfo.denom) / (uint64_t)timeBaseInfo.numer;
-
- do {
- /* Remember when we started so we can throttle the loop after the read call...
- */
- start = mach_absolute_time();
-
- rbytes = sizeof(readbuffer);
- readstatus = (*classdriver)->ReadPipe( classdriver, readbuffer, &rbytes );
- if ( kIOReturnSuccess == readstatus && rbytes > 0 ) {
-
- cupsBackChannelWrite((char*)readbuffer, rbytes, 1.0);
-
- /* cntrl-d is echoed by the printer.
- * NOTES:
- * Xerox Phaser 6250D doesn't echo the cntrl-d.
- * Xerox Phaser 6250D doesn't always send the product query.
- */
- if (userData->waitEOF && readbuffer[rbytes-1] == 0x4)
- break;
-#ifdef PARSE_PS_ERRORS
- parse_pserror(readbuffer, rbytes);
-#endif
- }
-
- /* Make sure this loop executes no more than once every 250 miliseconds...
- */
- if ((readstatus != kIOReturnSuccess || rbytes == 0) && (userData->waitEOF || !userData->done))
- mach_wait_until(start + delay);
-
- } while ( userData->waitEOF || !userData->done ); /* Abort from main thread tests error here */
-
- /* Let the other thread (main thread) know that we have completed the read thread...
- */
- pthread_mutex_lock(&userData->readMutex);
- pthread_cond_signal(&userData->readCompleteCondition);
- pthread_mutex_unlock(&userData->readMutex);
-
- return NULL;
-}
+ fd_set input_set; /* Input set for select() */
+ struct timeval stimeout; /* Timeout for select() */
+ char buffer[2048]; /* Buffer */
+ struct timespec cond_timeout; /* pthread condition timeout */
-/*
- * 'reqestWait_thread()' - A thread cupsSideChannelDoRequest wait.
- */
-static void *reqestWait_thread(void *reference) {
- printer_data_t *userData = (printer_data_t *)reference;
- int datalen;
- cups_sc_command_t command;
- cups_sc_status_t status;
- uint64_t start, delay;
- struct mach_timebase_info timeBaseInfo;
- char data[2048];
-
- /*
- * Calculate what 100 milliSeconds are in mach absolute time...
- */
- mach_timebase_info(&timeBaseInfo);
- delay = ((uint64_t)100000000 * (uint64_t)timeBaseInfo.denom) / (uint64_t)timeBaseInfo.numer;
-
- /* interface close wait mutex lock. */
- pthread_mutex_lock(&(userData->waitCloseMutex));
-
- do {
- /*
- * Remember when we started so we can throttle the loop after the cupsSideChannelDoRequest call...
- */
- start = mach_absolute_time();
+ /*
+ * Send an abort once a second until the I/O lock is released by the main thread...
+ */
- /* Poll for a command... */
- command=0;
- datalen = sizeof(data);
- bzero(data, sizeof(data));
-
- if (!cupsSideChannelRead(&command, &status, data, &datalen, 0.0)) {
- datalen = sizeof(data);
-
- switch (command) {
- case CUPS_SC_CMD_SOFT_RESET:
- /* do a soft reset */
- usbSoftReset(userData, &status);
- datalen = 0;
- userData->reqWaitDone = 1;
- break;
- case CUPS_SC_CMD_DRAIN_OUTPUT:
- /* drain all pending output */
- usbDrainOutput(userData, &status);
- datalen = 0;
- break;
- case CUPS_SC_CMD_GET_BIDI:
- /* return whether the connection is bidirectional */
- usbGetBidirectional(userData, &status, data, &datalen);
- break;
- case CUPS_SC_CMD_GET_DEVICE_ID:
- /* return the IEEE-1284 device ID */
- usbGetDeviceID(userData, &status, data, &datalen);
- break;
- case CUPS_SC_CMD_GET_STATE:
- /* return the device state */
- usbGetDevState(userData, &status, data, &datalen);
- break;
- default:
- status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
- datalen = 0;
- break;
- }
+ pthread_mutex_lock(&g.readwrite_lock_mutex);
+ while (g.readwrite_lock)
+ {
+ (*g.classdriver)->Abort(g.classdriver);
- if (userData->writeDone) {
- status = CUPS_SC_STATUS_NONE;
- }
+ cond_timeout.tv_sec = time(NULL) + 1;
+ cond_timeout.tv_nsec = 0;
- /* Send a response... */
- cupsSideChannelWrite(command, status, data, datalen, 1.0);
- }
+ pthread_cond_timedwait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex, &cond_timeout);
+ }
- /*
- * Make sure this loop executes no more than once every 500 miliseconds...
- */
- if ((userData->waitEOF) || (!userData->reqWaitDone)) {
- mach_wait_until(start + delay);
- }
- } while(!userData->reqWaitDone);
+ g.readwrite_lock = 1;
+ pthread_mutex_unlock(&g.readwrite_lock_mutex);
- sleep(1);
- pthread_mutex_lock(&userData->reqWaitMutex);
- userData->reqWqitFlag = 1;
- pthread_cond_signal(&userData->reqWaitCompCond);
- pthread_mutex_unlock(&userData->reqWaitMutex);
+ /*
+ * Flush bytes waiting on print_fd...
+ */
- /* interface close wait mutex unlock. */
- pthread_mutex_unlock(&(userData->waitCloseMutex));
+ g.print_bytes = 0;
- return NULL;
-}
+ FD_ZERO(&input_set);
+ FD_SET(g.print_fd, &input_set);
-#pragma mark -
-/*
- * 'usbSoftReset'
- */
-static void usbSoftReset(printer_data_t *userData, cups_sc_status_t *status) {
- OSStatus err;
+ stimeout.tv_sec = 0;
+ stimeout.tv_usec = 0;
- /* write stop. */
- userData->writeDone = 1;
+ while (select(g.print_fd+1, &input_set, NULL, NULL, &stimeout) > 0)
+ if (read(g.print_fd, buffer, sizeof(buffer)) <= 0)
+ break;
- /* Abort (print_device()-WritePipe kIOReturnAborted return) */
- if (userData->printerDriver != NULL)
- err = (*(userData->printerDriver))->Abort(userData->printerDriver);
+ /*
+ * Send the reset...
+ */
- /* print_device() WritePipe_Loop break wait. */
- pthread_mutex_lock(&(userData->writeCompMutex));
- pthread_mutex_unlock(&(userData->writeCompMutex));
+ (*g.classdriver)->SoftReset(g.classdriver, DEFAULT_TIMEOUT);
- /* SoftReset */
- if (userData->printerDriver != NULL)
- (*(userData->printerDriver))->SoftReset(userData->printerDriver, 0);
+ /*
+ * Release the I/O lock...
+ */
- if (status != NULL)
- *status = CUPS_SC_STATUS_OK;
+ pthread_mutex_lock(&g.readwrite_lock_mutex);
+ g.readwrite_lock = 0;
+ pthread_cond_signal(&g.readwrite_lock_cond);
+ pthread_mutex_unlock(&g.readwrite_lock_mutex);
}
-/*
- * 'usbDrainOutput'
- */
-static void usbDrainOutput(printer_data_t *userData, cups_sc_status_t *status) {
- OSStatus osSts = noErr; /* Function results */
- OSStatus err = noErr;
- UInt32 wbytes; /* Number of bytes written */
- ssize_t nbytes; /* Number of bytes read */
- char *bufptr;
-
- bufptr = userData->dataBuffer+userData->dataOffset;
- nbytes = userData->dataSize;
-
- while((nbytes > 0) && (osSts == noErr)) {
- wbytes = nbytes;
- osSts = (*(userData->printerDriver))->WritePipe(userData->printerDriver, (UInt8*)bufptr, &wbytes, 0);
-
- if (wbytes < 0 || noErr != osSts) {
- if (osSts != kIOReturnAborted) {
- err = (*(userData->printerDriver))->Abort(userData->printerDriver);
- break;
- }
- }
-
- nbytes -= wbytes;
- bufptr += wbytes;
- }
-
- if (status != NULL) {
- if ((osSts != noErr) || (err != noErr)) {
- *status = CUPS_SC_STATUS_IO_ERROR;
- } else {
- *status = CUPS_SC_STATUS_OK;
- }
- }
-}
/*
- * 'usbGetBidirectional'
+ * 'get_device_id()' - Return IEEE-1284 device ID.
*/
-static void usbGetBidirectional(printer_data_t *userData, cups_sc_status_t *status, char *data, int *datalen) {
- *data = userData->directionalFlag;
- *datalen = 1;
- if (status != NULL)
- *status = CUPS_SC_STATUS_OK;
-}
-
-/*
- * 'usbGetDeviceID'
- */
-static void usbGetDeviceID(printer_data_t *userData, cups_sc_status_t *status, char *data, int *datalen) {
+static void get_device_id(cups_sc_status_t *status,
+ char *data,
+ int *datalen)
+{
UInt32 deviceLocation = 0;
+ UInt8 interfaceNum = 0;
CFStringRef deviceIDString = NULL;
/* GetDeviceID */
- copy_devicestring(userData->printerObj, &deviceIDString, &deviceLocation);
- CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8);
- *datalen = strlen(data);
-
- if (status != NULL) {
- *status = CUPS_SC_STATUS_OK;
+ copy_devicestring(g.printer_obj, &deviceIDString, &deviceLocation, &interfaceNum);
+ if (deviceIDString)
+ {
+ CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8);
+ *datalen = strlen(data);
+ CFRelease(deviceIDString);
}
+ *status = CUPS_SC_STATUS_OK;
}
-/*
- * 'usbGetDevState'
- */
-static void usbGetDevState(printer_data_t *userData, cups_sc_status_t *status, char *data, int *datalen) {
- *data = CUPS_SC_STATE_ONLINE;
- *datalen = 1;
-
- if (status != NULL) {
- *status = CUPS_SC_STATUS_OK;
- }
-}
/*
- * End of "$Id: usb-darwin.c 6365 2007-03-19 20:56:57Z mike $".
+ * End of "$Id: usb-darwin.c 6993 2007-09-28 18:05:28Z mike $".
*/