4 * Copyright 2005-2013 Apple Inc. All rights reserved.
6 * IMPORTANT: This Apple software is supplied to you by Apple Computer,
7 * Inc. ("Apple") in consideration of your agreement to the following
8 * terms, and your use, installation, modification or redistribution of
9 * this Apple software constitutes acceptance of these terms. If you do
10 * not agree with these terms, please do not use, install, modify or
11 * redistribute this Apple software.
13 * In consideration of your agreement to abide by the following terms, and
14 * subject to these terms, Apple grants you a personal, non-exclusive
15 * license, under Apple's copyrights in this original Apple software (the
16 * "Apple Software"), to use, reproduce, modify and redistribute the Apple
17 * Software, with or without modifications, in source and/or binary forms;
18 * provided that if you redistribute the Apple Software in its entirety and
19 * without modifications, you must retain this notice and the following
20 * text and disclaimers in all such redistributions of the Apple Software.
21 * Neither the name, trademarks, service marks or logos of Apple Computer,
22 * Inc. may be used to endorse or promote products derived from the Apple
23 * Software without specific prior written permission from Apple. Except
24 * as expressly stated in this notice, no other rights or licenses, express
25 * or implied, are granted by Apple herein, including but not limited to
26 * any patent rights that may be infringed by your derivative works or by
27 * other works in which the Apple Software may be incorporated.
29 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30 * MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31 * THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32 * FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33 * OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39 * MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40 * AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41 * STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGE.
46 * list_devices() - List all USB devices.
47 * print_device() - Print a file to a USB device.
48 * read_thread() - Thread to read the backchannel data on.
49 * sidechannel_thread() - Handle side-channel requests.
50 * iterate_printers() - Iterate over all the printers.
51 * device_added() - Device added notifier.
52 * list_device_cb() - list_device iterator callback.
53 * find_device_cb() - print_device iterator callback.
54 * status_timer_cb() - Status timer callback.
55 * copy_deviceinfo() - Copy strings from the 1284 device ID.
56 * release_deviceinfo() - Release deviceinfo strings.
57 * load_classdriver() - Load a classdriver.
58 * unload_classdriver() - Unload a classdriver.
59 * load_printerdriver() - Load vendor's classdriver.
60 * registry_open() - Open a connection to the printer.
61 * registry_close() - Close the connection to the printer.
62 * copy_deviceid() - Copy the 1284 device id string.
63 * copy_devicestring() - Copy the 1284 device id string.
64 * copy_value_for_key() - Copy value string associated with a key.
65 * cfstr_create_trim() - Create CFString and trim whitespace characters.
66 * parse_options() - Parse URI options.
67 * sigterm_handler() - SIGTERM handler.
68 * next_line() - Find the next line in a buffer.
69 * parse_pserror() - Scan the backchannel data for postscript errors.
70 * soft_reset() - Send a soft reset to the device.
71 * get_device_id() - Return IEEE-1284 device ID.
75 * Include necessary headers.
86 #include <sys/sysctl.h>
88 #include <mach/mach.h>
89 #include <mach/mach_error.h>
90 #include <mach/mach_time.h>
91 #include <cups/debug-private.h>
92 #include <cups/file-private.h>
93 #include <cups/sidechannel.h>
94 #include <cups/language-private.h>
95 #include "backend-private.h"
96 #include <CoreFoundation/CoreFoundation.h>
97 #include <IOKit/usb/IOUSBLib.h>
98 #include <IOKit/IOCFPlugIn.h>
104 extern char **environ
;
108 * DEBUG_WRITES, if defined, causes the backend to write data to the printer in
109 * 512 byte increments, up to 8192 bytes, to make debugging with a USB bus
113 #define DEBUG_WRITES 0
116 * WAIT_EOF_DELAY is number of seconds we'll wait for responses from
117 * the printer after we've finished sending all the data
119 #define WAIT_EOF_DELAY 7
120 #define WAIT_SIDE_DELAY 3
121 #define DEFAULT_TIMEOUT 5000L
123 #define USB_INTERFACE_KIND CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID190)
124 #define kUSBLanguageEnglish 0x409
126 #define PRINTER_POLLING_INTERVAL 5 /* seconds */
127 #define INITIAL_LOG_INTERVAL PRINTER_POLLING_INTERVAL
128 #define SUBSEQUENT_LOG_INTERVAL 3 * INITIAL_LOG_INTERVAL
130 #define kUSBPrinterClassTypeID CFUUIDGetConstantUUIDWithBytes(NULL, 0x06, 0x04, 0x7D, 0x16, 0x53, 0xA2, 0x11, 0xD6, 0x92, 0x06, 0x00, 0x30, 0x65, 0x52, 0x45, 0x92)
131 #define kUSBPrinterClassInterfaceID CFUUIDGetConstantUUIDWithBytes(NULL, 0x03, 0x34, 0x6D, 0x74, 0x53, 0xA3, 0x11, 0xD6, 0x9E, 0xA1, 0x76, 0x30, 0x65, 0x52, 0x45, 0x92)
133 #define kUSBClassDriverProperty CFSTR("USB Printing Class")
135 #define kUSBGenericTOPrinterClassDriver CFSTR("/System/Library/Printers/Libraries/USBGenericPrintingClass.plugin")
136 #define kUSBPrinterClassDeviceNotOpen -9664 /*kPMInvalidIOMContext*/
138 #define CRSetCrashLogMessage(m) _crc_make_setter(message, m)
139 #define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg))
140 #define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden")))
141 #define CRASHREPORTER_ANNOTATIONS_VERSION 4
142 #define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
144 struct crashreporter_annotations_t
{
145 uint64_t version
; // unsigned long
146 uint64_t message
; // char *
147 uint64_t signature_string
; // char *
148 uint64_t backtrace
; // char *
149 uint64_t message2
; // char *
150 uint64_t thread
; // uint64_t
151 uint64_t dialog_mode
; // unsigned int
154 CRASH_REPORTER_CLIENT_HIDDEN
155 struct crashreporter_annotations_t gCRAnnotations
156 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION
)))
157 = { CRASHREPORTER_ANNOTATIONS_VERSION
, 0, 0, 0, 0, 0, 0 };
160 * Section 5.3 USB Printing Class spec
162 #define kUSBPrintingSubclass 1
163 #define kUSBPrintingProtocolNoOpen 0
164 #define kUSBPrintingProtocolUnidirectional 1
165 #define kUSBPrintingProtocolBidirectional 2
167 typedef IOUSBInterfaceInterface190
**printer_interface_t
;
169 typedef struct iodevice_request_s
/**** Device request ****/
177 } iodevice_request_t
;
179 typedef union /**** Centronics status byte ****/
184 unsigned reserved0
:2;
185 unsigned paperError
:1;
188 unsigned reserved1
:3;
190 } centronics_status_t
;
192 typedef struct classdriver_s
/**** g.classdriver context ****/
195 CFPlugInRef plugin
; /* release plugin */
196 IUnknownVTbl
**factory
; /* Factory */
197 void *vendorReference
; /* vendor class specific usage */
198 UInt32 location
; /* unique location in bus topology */
199 UInt8 interfaceNumber
; /* Interface number */
200 UInt16 vendorID
; /* Vendor id */
201 UInt16 productID
; /* Product id */
202 printer_interface_t interface
; /* identify the device to IOKit */
203 UInt8 outpipe
; /* mandatory bulkOut pipe */
204 UInt8 inpipe
; /* optional bulkIn pipe */
206 /* general class requests */
207 kern_return_t (*DeviceRequest
)(struct classdriver_s
**printer
, iodevice_request_t
*iorequest
, UInt16 timeout
);
208 kern_return_t (*GetString
)(struct classdriver_s
**printer
, UInt8 whichString
, UInt16 language
, UInt16 timeout
, CFStringRef
*result
);
210 /* standard printer class requests */
211 kern_return_t (*SoftReset
)(struct classdriver_s
**printer
, UInt16 timeout
);
212 kern_return_t (*GetCentronicsStatus
)(struct classdriver_s
**printer
, centronics_status_t
*result
, UInt16 timeout
);
213 kern_return_t (*GetDeviceID
)(struct classdriver_s
**printer
, CFStringRef
*devid
, UInt16 timeout
);
215 /* standard bulk device requests */
216 kern_return_t (*ReadPipe
)(struct classdriver_s
**printer
, UInt8
*buffer
, UInt32
*count
);
217 kern_return_t (*WritePipe
)(struct classdriver_s
**printer
, UInt8
*buffer
, UInt32
*count
, Boolean eoj
);
219 /* interface requests */
220 kern_return_t (*Open
)(struct classdriver_s
**printer
, UInt32 location
, UInt8 protocol
);
221 kern_return_t (*Abort
)(struct classdriver_s
**printer
);
222 kern_return_t (*Close
)(struct classdriver_s
**printer
);
224 /* initialize and terminate */
225 kern_return_t (*Initialize
)(struct classdriver_s
**printer
, struct classdriver_s
**baseclass
);
226 kern_return_t (*Terminate
)(struct classdriver_s
**printer
);
230 typedef Boolean (*iterator_callback_t
)(void *refcon
, io_service_t obj
);
232 typedef struct iterator_reference_s
/**** Iterator reference data */
234 iterator_callback_t callback
;
237 } iterator_reference_t
;
239 typedef struct globals_s
241 io_service_t printer_obj
;
242 classdriver_t
**classdriver
;
244 pthread_mutex_t read_thread_mutex
;
245 pthread_cond_t read_thread_cond
;
246 int read_thread_stop
;
247 int read_thread_done
;
249 pthread_mutex_t readwrite_lock_mutex
;
250 pthread_cond_t readwrite_lock_cond
;
259 CFRunLoopTimerRef status_timer
;
261 int print_fd
; /* File descriptor to print */
262 ssize_t print_bytes
; /* Print bytes read */
264 ssize_t debug_bytes
; /* Current bytes to read */
265 #endif /* DEBUG_WRITES */
268 int drain_output
; /* Drain all pending output */
269 int bidi_flag
; /* 0=unidirectional, 1=bidirectional */
271 pthread_mutex_t sidechannel_thread_mutex
;
272 pthread_cond_t sidechannel_thread_cond
;
273 int sidechannel_thread_stop
;
274 int sidechannel_thread_done
;
282 globals_t g
= { 0 }; /* Globals */
283 int Iterating
= 0; /* Are we iterating the bus? */
290 static Boolean
find_device_cb(void *refcon
, io_service_t obj
);
291 static Boolean
list_device_cb(void *refcon
, io_service_t obj
);
292 static CFStringRef
cfstr_create_trim(const char *cstr
);
293 static CFStringRef
copy_value_for_key(CFStringRef deviceID
, CFStringRef
*keys
);
294 static kern_return_t
load_classdriver(CFStringRef driverPath
, printer_interface_t interface
, classdriver_t
***printerDriver
);
295 static kern_return_t
load_printerdriver(CFStringRef
*driverBundlePath
);
296 static kern_return_t
registry_close(void);
297 static kern_return_t
registry_open(CFStringRef
*driverBundlePath
);
298 static kern_return_t
unload_classdriver(classdriver_t
***classdriver
);
299 static OSStatus
copy_deviceid(classdriver_t
**printer
, CFStringRef
*deviceID
);
300 static void *read_thread(void *reference
);
301 static void *sidechannel_thread(void *reference
);
302 static void copy_deviceinfo(CFStringRef deviceIDString
, CFStringRef
*make
, CFStringRef
*model
, CFStringRef
*serial
);
303 static void copy_devicestring(io_service_t usbInterface
, CFStringRef
*deviceID
, UInt32
*deviceLocation
, UInt8
*interfaceNum
);
304 static void device_added(void *userdata
, io_iterator_t iterator
);
305 static void get_device_id(cups_sc_status_t
*status
, char *data
, int *datalen
);
306 static void iterate_printers(iterator_callback_t callBack
, void *userdata
);
307 static void parse_options(char *options
, char *serial
, int serial_size
, UInt32
*location
, Boolean
*wait_eof
);
308 static void release_deviceinfo(CFStringRef
*make
, CFStringRef
*model
, CFStringRef
*serial
);
309 static void setup_cfLanguage(void);
310 static void soft_reset(void);
311 static void status_timer_cb(CFRunLoopTimerRef timer
, void *info
);
313 #if defined(__i386__) || defined(__x86_64__)
314 static pid_t child_pid
; /* Child PID */
315 static void run_legacy_backend(int argc
, char *argv
[], int fd
); /* Starts child backend process running as a ppc executable */
316 #endif /* __i386__ || __x86_64__ */
317 static void sigterm_handler(int sig
); /* SIGTERM handler */
318 static void sigquit_handler(int sig
, siginfo_t
*si
, void *unused
);
320 #ifdef PARSE_PS_ERRORS
321 static const char *next_line (const char *buffer
);
322 static void parse_pserror (char *sockBuffer
, int len
);
323 #endif /* PARSE_PS_ERRORS */
328 * 'list_devices()' - List all USB devices.
333 iterate_printers(list_device_cb
, NULL
);
338 * 'print_device()' - Print a file to a USB device.
341 int /* O - Exit status */
342 print_device(const char *uri
, /* I - Device URI */
343 const char *hostname
, /* I - Hostname/manufacturer */
344 const char *resource
, /* I - Resource/modelname */
345 char *options
, /* I - Device options/serial number */
346 int print_fd
, /* I - File descriptor to print */
347 int copies
, /* I - Copies to print */
348 int argc
, /* I - Number of command-line arguments (6 or 7) */
349 char *argv
[]) /* I - Command-line arguments */
351 char serial
[1024]; /* Serial number buffer */
352 OSStatus status
; /* Function results */
353 IOReturn iostatus
; /* Current IO status */
354 pthread_t read_thread_id
, /* Read thread */
355 sidechannel_thread_id
;/* Side-channel thread */
356 int have_sidechannel
= 0; /* Was the side-channel thread started? */
357 struct stat sidechannel_info
; /* Side-channel file descriptor info */
358 char print_buffer
[8192], /* Print data buffer */
359 *print_ptr
; /* Pointer into print data buffer */
360 UInt32 location
; /* Unique location in bus topology */
361 fd_set input_set
; /* Input set for select() */
362 CFStringRef driverBundlePath
; /* Class driver path */
363 int countdown
, /* Logging interval */
364 nfds
; /* Number of file descriptors */
365 ssize_t total_bytes
; /* Total bytes written */
366 UInt32 bytes
; /* Bytes written */
367 struct timeval
*timeout
, /* Timeout pointer */
369 struct timespec cond_timeout
; /* pthread condition timeout */
370 struct sigaction action
; /* Actions for POSIX signals */
376 * Catch SIGQUIT to determine who is sending it...
379 memset(&action
, 0, sizeof(action
));
380 action
.sa_sigaction
= sigquit_handler
;
381 action
.sa_flags
= SA_SIGINFO
;
382 sigaction(SIGQUIT
, &action
, NULL
);
385 * See if the side-channel descriptor is valid...
388 have_sidechannel
= !fstat(CUPS_SC_FD
, &sidechannel_info
) &&
389 S_ISSOCK(sidechannel_info
.st_mode
);
392 * Localize using CoreFoundation...
397 parse_options(options
, serial
, sizeof(serial
), &location
, &g
.wait_eof
);
399 if (resource
[0] == '/')
402 g
.print_fd
= print_fd
;
403 g
.make
= cfstr_create_trim(hostname
);
404 g
.model
= cfstr_create_trim(resource
);
405 g
.serial
= cfstr_create_trim(serial
);
406 g
.location
= location
;
408 if (!g
.make
|| !g
.model
)
410 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
411 _cupsLangPrintFilter(stderr
, "ERROR",
412 _("There was an unrecoverable USB error."));
415 fputs("DEBUG: USB make string is NULL\n", stderr
);
417 fputs("DEBUG: USB model string is NULL\n", stderr
);
419 return (CUPS_BACKEND_STOP
);
422 fputs("STATE: +connecting-to-device\n", stderr
);
424 countdown
= INITIAL_LOG_INTERVAL
;
430 IOObjectRelease(g
.printer_obj
);
431 unload_classdriver(&g
.classdriver
);
436 fprintf(stderr
, "DEBUG: Looking for '%s %s'\n", hostname
, resource
);
438 iterate_printers(find_device_cb
, NULL
);
440 fputs("DEBUG: Opening connection\n", stderr
);
442 driverBundlePath
= NULL
;
444 status
= registry_open(&driverBundlePath
);
446 #if defined(__i386__) || defined(__x86_64__)
448 * If we were unable to load the class drivers for this printer it's
449 * probably because they're ppc or i386. In this case try to run this
450 * backend as i386 or ppc executables so we can use them...
454 run_legacy_backend(argc
, argv
, print_fd
);
455 /* Never returns here */
457 #endif /* __i386__ || __x86_64__ */
462 * If we still were unable to load the class drivers for this printer log
463 * the error and stop the queue...
466 if (driverBundlePath
== NULL
|| !CFStringGetCString(driverBundlePath
, print_buffer
, sizeof(print_buffer
), kCFStringEncodingUTF8
))
467 strlcpy(print_buffer
, "USB class driver", sizeof(print_buffer
));
469 fputs("STATE: +apple-missing-usbclassdriver-error\n", stderr
);
470 _cupsLangPrintFilter(stderr
, "ERROR",
471 _("There was an unrecoverable USB error."));
472 fprintf(stderr
, "DEBUG: Could not load %s\n", print_buffer
);
474 if (driverBundlePath
)
475 CFRelease(driverBundlePath
);
477 return (CUPS_BACKEND_STOP
);
480 if (driverBundlePath
)
481 CFRelease(driverBundlePath
);
485 sleep(PRINTER_POLLING_INTERVAL
);
486 countdown
-= PRINTER_POLLING_INTERVAL
;
489 _cupsLangPrintFilter(stderr
, "INFO",
490 _("Waiting for printer to become available."));
491 fprintf(stderr
, "DEBUG: USB printer status: 0x%08x\n", (int)status
);
492 countdown
= SUBSEQUENT_LOG_INTERVAL
; /* subsequent log entries, every 15 seconds */
495 } while (status
!= noErr
);
497 fputs("STATE: -connecting-to-device\n", stderr
);
500 * Now that we are "connected" to the port, ignore SIGTERM so that we
501 * can finish out any page data the driver sends (e.g. to eject the
502 * current page... Only ignore SIGTERM if we are printing data from
503 * stdin (otherwise you can't cancel raw jobs...)
508 memset(&action
, 0, sizeof(action
));
510 sigemptyset(&action
.sa_mask
);
511 action
.sa_handler
= SIG_IGN
;
512 sigaction(SIGTERM
, &action
, NULL
);
516 * Start the side channel thread if the descriptor is valid...
519 pthread_mutex_init(&g
.readwrite_lock_mutex
, NULL
);
520 pthread_cond_init(&g
.readwrite_lock_cond
, NULL
);
521 g
.readwrite_lock
= 1;
523 if (have_sidechannel
)
525 g
.sidechannel_thread_stop
= 0;
526 g
.sidechannel_thread_done
= 0;
528 pthread_cond_init(&g
.sidechannel_thread_cond
, NULL
);
529 pthread_mutex_init(&g
.sidechannel_thread_mutex
, NULL
);
531 if (pthread_create(&sidechannel_thread_id
, NULL
, sidechannel_thread
, NULL
))
533 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
534 _cupsLangPrintFilter(stderr
, "ERROR",
535 _("There was an unrecoverable USB error."));
536 fputs("DEBUG: Couldn't create side-channel thread\n", stderr
);
538 return (CUPS_BACKEND_STOP
);
543 * Get the read thread going...
546 g
.read_thread_stop
= 0;
547 g
.read_thread_done
= 0;
549 pthread_cond_init(&g
.read_thread_cond
, NULL
);
550 pthread_mutex_init(&g
.read_thread_mutex
, NULL
);
552 if (pthread_create(&read_thread_id
, NULL
, read_thread
, NULL
))
554 fprintf(stderr
, "DEBUG: Fatal USB error.\n");
555 _cupsLangPrintFilter(stderr
, "ERROR",
556 _("There was an unrecoverable USB error."));
557 fputs("DEBUG: Couldn't create read thread\n", stderr
);
559 return (CUPS_BACKEND_STOP
);
563 * The main thread sends the print file...
569 print_ptr
= print_buffer
;
571 while (status
== noErr
&& copies
-- > 0)
573 _cupsLangPrintFilter(stderr
, "INFO", _("Sending data to printer."));
575 if (print_fd
!= STDIN_FILENO
)
577 fputs("PAGE: 1 1\n", stderr
);
578 lseek(print_fd
, 0, SEEK_SET
);
581 while (status
== noErr
)
586 FD_SET(print_fd
, &input_set
);
589 * Calculate select timeout...
590 * If we have data waiting to send timeout is 100ms.
591 * else if we're draining print_fd timeout is 0.
592 * else we're waiting forever...
598 tv
.tv_usec
= 100000; /* 100ms */
601 else if (g
.drain_output
)
611 * I/O is unlocked around select...
614 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
615 g
.readwrite_lock
= 0;
616 pthread_cond_signal(&g
.readwrite_lock_cond
);
617 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
619 nfds
= select(print_fd
+ 1, &input_set
, NULL
, NULL
, timeout
);
622 * Reacquire the lock...
625 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
626 while (g
.readwrite_lock
)
627 pthread_cond_wait(&g
.readwrite_lock_cond
, &g
.readwrite_lock_mutex
);
628 g
.readwrite_lock
= 1;
629 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
633 if (errno
== EINTR
&& total_bytes
== 0)
635 fputs("DEBUG: Received an interrupt before any bytes were "
636 "written, aborting\n", stderr
);
638 return (CUPS_BACKEND_OK
);
640 else if (errno
!= EAGAIN
&& errno
!= EINTR
)
642 _cupsLangPrintFilter(stderr
, "ERROR",
643 _("Unable to read print data."));
644 perror("DEBUG: select");
646 return (CUPS_BACKEND_FAILED
);
651 * If drain output has finished send a response...
654 if (g
.drain_output
&& !nfds
&& !g
.print_bytes
)
656 /* Send a response... */
657 cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
662 * Check if we have print data ready...
665 if (FD_ISSET(print_fd
, &input_set
))
668 g
.debug_bytes
+= 512;
669 if (g
.debug_bytes
> sizeof(print_buffer
))
672 g
.print_bytes
= read(print_fd
, print_buffer
, g
.debug_bytes
);
675 g
.print_bytes
= read(print_fd
, print_buffer
, sizeof(print_buffer
));
676 #endif /* DEBUG_WRITES */
678 if (g
.print_bytes
< 0)
681 * Read error - bail if we don't see EAGAIN or EINTR...
684 if (errno
!= EAGAIN
&& errno
!= EINTR
)
686 _cupsLangPrintFilter(stderr
, "ERROR",
687 _("Unable to read print data."));
688 perror("DEBUG: read");
690 return (CUPS_BACKEND_FAILED
);
695 else if (g
.print_bytes
== 0)
698 * End of file, break out of the loop...
704 print_ptr
= print_buffer
;
706 fprintf(stderr
, "DEBUG: Read %d bytes of print data...\n",
712 bytes
= g
.print_bytes
;
713 iostatus
= (*g
.classdriver
)->WritePipe(g
.classdriver
, (UInt8
*)print_ptr
, &bytes
, 0);
716 * Ignore timeout errors, but retain the number of bytes written to
717 * avoid sending duplicate data...
720 if (iostatus
== kIOUSBTransactionTimeout
)
722 fputs("DEBUG: Got USB transaction timeout during write\n", stderr
);
727 * If we've stalled, retry the write...
730 else if (iostatus
== kIOUSBPipeStalled
)
732 fputs("DEBUG: Got USB pipe stalled during write\n", stderr
);
734 bytes
= g
.print_bytes
;
735 iostatus
= (*g
.classdriver
)->WritePipe(g
.classdriver
, (UInt8
*)print_ptr
, &bytes
, 0);
739 * Retry a write after an aborted write since we probably just got
743 else if (iostatus
== kIOReturnAborted
)
745 fputs("DEBUG: Got USB return aborted during write\n", stderr
);
747 IOReturn err
= (*g
.classdriver
)->Abort(g
.classdriver
);
748 fprintf(stderr
, "DEBUG: USB class driver Abort returned %x\n", err
);
752 #endif /* DEBUG_WRITES */
754 bytes
= g
.print_bytes
;
755 iostatus
= (*g
.classdriver
)->WritePipe(g
.classdriver
, (UInt8
*)print_ptr
, &bytes
, 0);
761 * Write error - bail if we don't see an error we can retry...
764 _cupsLangPrintFilter(stderr
, "ERROR",
765 _("Unable to send data to printer."));
766 fprintf(stderr
, "DEBUG: USB class driver WritePipe returned %x\n",
769 IOReturn err
= (*g
.classdriver
)->Abort(g
.classdriver
);
770 fprintf(stderr
, "DEBUG: USB class driver Abort returned %x\n",
773 status
= CUPS_BACKEND_FAILED
;
778 fprintf(stderr
, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes
);
780 g
.print_bytes
-= bytes
;
782 total_bytes
+= bytes
;
786 if (print_fd
!= 0 && status
== noErr
)
787 fprintf(stderr
, "DEBUG: Sending print file, %lld bytes...\n",
792 fprintf(stderr
, "DEBUG: Sent %lld bytes...\n", (off_t
)total_bytes
);
795 * Signal the side channel thread to exit...
798 if (have_sidechannel
)
801 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
802 g
.readwrite_lock
= 0;
803 pthread_cond_signal(&g
.readwrite_lock_cond
);
804 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
806 g
.sidechannel_thread_stop
= 1;
807 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
809 if (!g
.sidechannel_thread_done
)
811 gettimeofday(&tv
, NULL
);
812 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_SIDE_DELAY
;
813 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
815 while (!g
.sidechannel_thread_done
)
817 if (pthread_cond_timedwait(&g
.sidechannel_thread_cond
,
818 &g
.sidechannel_thread_mutex
,
824 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
828 * Signal the read thread to exit then wait 7 seconds for it to complete...
831 g
.read_thread_stop
= 1;
833 pthread_mutex_lock(&g
.read_thread_mutex
);
835 if (!g
.read_thread_done
)
837 fputs("DEBUG: Waiting for read thread to exit...\n", stderr
);
839 gettimeofday(&tv
, NULL
);
840 cond_timeout
.tv_sec
= tv
.tv_sec
+ WAIT_EOF_DELAY
;
841 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
843 while (!g
.read_thread_done
)
845 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
851 * If it didn't exit abort the pending read and wait an additional second...
854 if (!g
.read_thread_done
)
856 fputs("DEBUG: Read thread still active, aborting the pending read...\n",
861 (*g
.classdriver
)->Abort(g
.classdriver
);
863 gettimeofday(&tv
, NULL
);
864 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
865 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
867 while (!g
.read_thread_done
)
869 if (pthread_cond_timedwait(&g
.read_thread_cond
, &g
.read_thread_mutex
,
876 pthread_mutex_unlock(&g
.read_thread_mutex
);
879 * Close the connection and input file and general clean up...
884 if (print_fd
!= STDIN_FILENO
)
893 if (g
.serial
!= NULL
)
896 if (g
.printer_obj
!= 0x0)
897 IOObjectRelease(g
.printer_obj
);
904 * 'read_thread()' - Thread to read the backchannel data on.
907 static void *read_thread(void *reference
)
909 UInt8 readbuffer
[512];
911 kern_return_t readstatus
;
912 struct mach_timebase_info timeBaseInfo
;
919 /* Calculate what 250 milliSeconds are in mach absolute time...
921 mach_timebase_info(&timeBaseInfo
);
922 delay
= ((uint64_t)250000000 * (uint64_t)timeBaseInfo
.denom
) / (uint64_t)timeBaseInfo
.numer
;
927 * Remember when we started so we can throttle the loop after the read call...
930 start
= mach_absolute_time();
932 rbytes
= sizeof(readbuffer
);
933 readstatus
= (*g
.classdriver
)->ReadPipe(g
.classdriver
, readbuffer
, &rbytes
);
934 if (readstatus
== kIOReturnSuccess
&& rbytes
> 0)
936 fprintf(stderr
, "DEBUG: Read %d bytes of back-channel data...\n",
938 cupsBackChannelWrite((char*)readbuffer
, rbytes
, 1.0);
940 /* cntrl-d is echoed by the printer.
942 * Xerox Phaser 6250D doesn't echo the cntrl-d.
943 * Xerox Phaser 6250D doesn't always send the product query.
945 if (g
.wait_eof
&& readbuffer
[rbytes
-1] == 0x4)
948 #ifdef PARSE_PS_ERRORS
949 parse_pserror(readbuffer
, rbytes
);
952 else if (readstatus
== kIOUSBTransactionTimeout
)
953 fputs("DEBUG: Got USB transaction timeout during read\n", stderr
);
954 else if (readstatus
== kIOUSBPipeStalled
)
955 fputs("DEBUG: Got USB pipe stalled during read\n", stderr
);
956 else if (readstatus
== kIOReturnAborted
)
957 fputs("DEBUG: Got USB return aborted during read\n", stderr
);
960 * Make sure this loop executes no more than once every 250 miliseconds...
963 if ((readstatus
!= kIOReturnSuccess
|| rbytes
== 0) && (g
.wait_eof
|| !g
.read_thread_stop
))
964 mach_wait_until(start
+ delay
);
966 } while (g
.wait_eof
|| !g
.read_thread_stop
); /* Abort from main thread tests error here */
969 * Let the main thread know that we have completed the read thread...
972 pthread_mutex_lock(&g
.read_thread_mutex
);
973 g
.read_thread_done
= 1;
974 pthread_cond_signal(&g
.read_thread_cond
);
975 pthread_mutex_unlock(&g
.read_thread_mutex
);
982 * 'sidechannel_thread()' - Handle side-channel requests.
986 sidechannel_thread(void *reference
)
988 cups_sc_command_t command
; /* Request command */
989 cups_sc_status_t status
; /* Request/response status */
990 char data
[2048]; /* Request/response data */
991 int datalen
; /* Request/response data size */
998 datalen
= sizeof(data
);
1000 if (cupsSideChannelRead(&command
, &status
, data
, &datalen
, 1.0))
1002 if (status
== CUPS_SC_STATUS_TIMEOUT
)
1010 case CUPS_SC_CMD_SOFT_RESET
: /* Do a soft reset */
1011 fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n",
1014 if ((*g
.classdriver
)->SoftReset
!= NULL
)
1017 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, NULL
, 0, 1.0);
1018 fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n",
1023 cupsSideChannelWrite(command
, CUPS_SC_STATUS_NOT_IMPLEMENTED
,
1025 fputs("DEBUG: Returning status CUPS_STATUS_NOT_IMPLEMENTED with "
1026 "no bytes...\n", stderr
);
1030 case CUPS_SC_CMD_DRAIN_OUTPUT
: /* Drain all pending output */
1031 fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n",
1037 case CUPS_SC_CMD_GET_BIDI
: /* Is the connection bidirectional? */
1038 fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n",
1041 data
[0] = g
.bidi_flag
;
1042 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1045 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1049 case CUPS_SC_CMD_GET_DEVICE_ID
: /* Return IEEE-1284 device ID */
1050 fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n",
1053 datalen
= sizeof(data
);
1054 get_device_id(&status
, data
, &datalen
);
1055 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, datalen
, 1.0);
1057 if (datalen
< sizeof(data
))
1058 data
[datalen
] = '\0';
1060 data
[sizeof(data
) - 1] = '\0';
1063 "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n",
1067 case CUPS_SC_CMD_GET_STATE
: /* Return device state */
1068 fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n",
1071 data
[0] = CUPS_SC_STATE_ONLINE
;
1072 cupsSideChannelWrite(command
, CUPS_SC_STATUS_OK
, data
, 1, 1.0);
1075 "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n",
1080 fprintf(stderr
, "DEBUG: Unknown side-channel command (%d) received "
1081 "from driver...\n", command
);
1083 cupsSideChannelWrite(command
, CUPS_SC_STATUS_NOT_IMPLEMENTED
,
1086 fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n",
1091 while (!g
.sidechannel_thread_stop
);
1093 pthread_mutex_lock(&g
.sidechannel_thread_mutex
);
1094 g
.sidechannel_thread_done
= 1;
1095 pthread_cond_signal(&g
.sidechannel_thread_cond
);
1096 pthread_mutex_unlock(&g
.sidechannel_thread_mutex
);
1104 * 'iterate_printers()' - Iterate over all the printers.
1107 static void iterate_printers(iterator_callback_t callBack
,
1112 mach_port_t masterPort
= 0x0;
1113 kern_return_t kr
= IOMasterPort (bootstrap_port
, &masterPort
);
1115 if (kr
== kIOReturnSuccess
&& masterPort
!= 0x0)
1117 io_iterator_t addIterator
= 0x0;
1119 iterator_reference_t reference
= { callBack
, userdata
, true };
1120 IONotificationPortRef addNotification
= IONotificationPortCreate(masterPort
);
1122 int klass
= kUSBPrintingClass
;
1123 int subklass
= kUSBPrintingSubclass
;
1125 CFNumberRef usb_klass
= CFNumberCreate(NULL
, kCFNumberIntType
, &klass
);
1126 CFNumberRef usb_subklass
= CFNumberCreate(NULL
, kCFNumberIntType
, &subklass
);
1127 CFMutableDictionaryRef usbPrinterMatchDictionary
= IOServiceMatching(kIOUSBInterfaceClassName
);
1129 CFDictionaryAddValue(usbPrinterMatchDictionary
, CFSTR("bInterfaceClass"), usb_klass
);
1130 CFDictionaryAddValue(usbPrinterMatchDictionary
, CFSTR("bInterfaceSubClass"), usb_subklass
);
1132 CFRelease(usb_klass
);
1133 CFRelease(usb_subklass
);
1135 IOServiceAddMatchingNotification(addNotification
, kIOMatchedNotification
, usbPrinterMatchDictionary
, &device_added
, &reference
, &addIterator
);
1136 if (addIterator
!= 0x0)
1138 device_added (&reference
, addIterator
);
1140 if (reference
.keepRunning
)
1142 CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification
), kCFRunLoopDefaultMode
);
1145 IOObjectRelease(addIterator
);
1147 mach_port_deallocate(mach_task_self(), masterPort
);
1155 * 'device_added()' - Device added notifier.
1158 static void device_added(void *userdata
,
1159 io_iterator_t iterator
)
1161 iterator_reference_t
*reference
= userdata
;
1164 while (reference
->keepRunning
&& (obj
= IOIteratorNext(iterator
)) != 0x0)
1166 if (reference
->callback
!= NULL
)
1167 reference
->keepRunning
= reference
->callback(reference
->userdata
, obj
);
1169 IOObjectRelease(obj
);
1172 /* One last call to the call back now that we are not longer have printers left to iterate...
1174 if (reference
->keepRunning
&& reference
->callback
)
1175 reference
->keepRunning
= reference
->callback(reference
->userdata
, 0x0);
1177 if (!reference
->keepRunning
)
1178 CFRunLoopStop(CFRunLoopGetCurrent());
1183 * 'list_device_cb()' - list_device iterator callback.
1186 static Boolean
list_device_cb(void *refcon
,
1189 Boolean keepRunning
= (obj
!= 0x0);
1196 CFStringRef deviceIDString
= NULL
;
1197 UInt32 deviceLocation
= 0;
1198 UInt8 interfaceNum
= 0;
1200 copy_devicestring(obj
, &deviceIDString
, &deviceLocation
, &interfaceNum
);
1201 if (deviceIDString
!= NULL
)
1203 CFStringRef make
= NULL
, model
= NULL
, serial
= NULL
;
1204 char uristr
[1024], makestr
[1024], modelstr
[1024], serialstr
[1024];
1205 char optionsstr
[1024], idstr
[1024], make_modelstr
[1024];
1207 copy_deviceinfo(deviceIDString
, &make
, &model
, &serial
);
1208 CFStringGetCString(deviceIDString
, idstr
, sizeof(idstr
),
1209 kCFStringEncodingUTF8
);
1210 backendGetMakeModel(idstr
, make_modelstr
, sizeof(make_modelstr
));
1215 !CFStringGetCString(make
, makestr
, sizeof(makestr
),
1216 kCFStringEncodingUTF8
))
1217 strlcpy(makestr
, "Unknown", sizeof(makestr
));
1220 !CFStringGetCString(model
, &modelstr
[1], sizeof(modelstr
)-1,
1221 kCFStringEncodingUTF8
))
1222 strlcpy(modelstr
+ 1, "Printer", sizeof(modelstr
) - 1);
1224 optionsstr
[0] = '\0';
1227 CFStringGetCString(serial
, serialstr
, sizeof(serialstr
), kCFStringEncodingUTF8
);
1228 snprintf(optionsstr
, sizeof(optionsstr
), "?serial=%s", serialstr
);
1230 else if (deviceLocation
!= 0)
1231 snprintf(optionsstr
, sizeof(optionsstr
), "?location=%x", (unsigned)deviceLocation
);
1233 httpAssembleURI(HTTP_URI_CODING_ALL
, uristr
, sizeof(uristr
), "usb", NULL
, makestr
, 0, modelstr
);
1234 strlcat(uristr
, optionsstr
, sizeof(uristr
));
1236 cupsBackendReport("direct", uristr
, make_modelstr
, make_modelstr
, idstr
,
1239 release_deviceinfo(&make
, &model
, &serial
);
1240 CFRelease(deviceIDString
);
1249 * 'find_device_cb()' - print_device iterator callback.
1252 static Boolean
find_device_cb(void *refcon
,
1255 Boolean keepLooking
= true;
1259 CFStringRef idString
= NULL
;
1260 UInt32 location
= -1;
1261 UInt8 interfaceNum
= 0;
1263 copy_devicestring(obj
, &idString
, &location
, &interfaceNum
);
1264 if (idString
!= NULL
)
1266 CFStringRef make
= NULL
, model
= NULL
, serial
= NULL
;
1268 copy_deviceinfo(idString
, &make
, &model
, &serial
);
1269 if (make
&& CFStringCompare(make
, g
.make
, kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
1271 if (model
&& CFStringCompare(model
, g
.model
, kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
1273 if (g
.serial
!= NULL
&& CFStringGetLength(g
.serial
) > 0)
1275 if (serial
!= NULL
&& CFStringCompare(serial
, g
.serial
, kCFCompareCaseInsensitive
) == kCFCompareEqualTo
)
1277 IOObjectRetain(obj
);
1278 g
.printer_obj
= obj
;
1279 keepLooking
= false;
1284 if (g
.printer_obj
!= 0)
1285 IOObjectRelease(g
.printer_obj
);
1287 g
.printer_obj
= obj
;
1288 IOObjectRetain(obj
);
1290 if (g
.location
== 0 || g
.location
== location
)
1291 keepLooking
= false;
1294 g
.interfaceNum
= interfaceNum
;
1298 release_deviceinfo(&make
, &model
, &serial
);
1299 CFRelease(idString
);
1304 keepLooking
= (g
.printer_obj
== 0);
1305 if (obj
== 0x0 && keepLooking
)
1307 CFRunLoopTimerContext context
= { 0, refcon
, NULL
, NULL
, NULL
};
1308 CFRunLoopTimerRef timer
= CFRunLoopTimerCreate(NULL
, CFAbsoluteTimeGetCurrent() + 1.0, 10, 0x0, 0x0, status_timer_cb
, &context
);
1311 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer
, kCFRunLoopDefaultMode
);
1312 g
.status_timer
= timer
;
1317 if (!keepLooking
&& g
.status_timer
!= NULL
)
1319 fputs("STATE: -offline-report\n", stderr
);
1320 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is now online."));
1321 CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), g
.status_timer
, kCFRunLoopDefaultMode
);
1322 CFRelease(g
.status_timer
);
1323 g
.status_timer
= NULL
;
1331 * 'status_timer_cb()' - Status timer callback.
1334 static void status_timer_cb(CFRunLoopTimerRef timer
,
1340 fputs("STATE: +offline-report\n", stderr
);
1341 _cupsLangPrintFilter(stderr
, "INFO", _("The printer is offline."));
1343 if (getenv("CLASS") != NULL
)
1346 * If the CLASS environment variable is set, the job was submitted
1347 * to a class and not to a specific queue. In this case, we want
1348 * to abort immediately so that the job can be requeued on the next
1349 * available printer in the class.
1351 * Sleep 5 seconds to keep the job from requeuing too rapidly...
1356 exit(CUPS_BACKEND_FAILED
);
1363 * 'copy_deviceinfo()' - Copy strings from the 1284 device ID.
1366 static void copy_deviceinfo(CFStringRef deviceIDString
,
1369 CFStringRef
*serial
)
1371 CFStringRef modelKeys
[] = { CFSTR("MDL:"), CFSTR("MODEL:"), NULL
};
1372 CFStringRef makeKeys
[] = { CFSTR("MFG:"), CFSTR("MANUFACTURER:"), NULL
};
1373 CFStringRef serialKeys
[] = { CFSTR("SN:"), CFSTR("SERN:"), NULL
};
1376 *make
= copy_value_for_key(deviceIDString
, makeKeys
);
1379 *model
= copy_value_for_key(deviceIDString
, modelKeys
);
1382 *serial
= copy_value_for_key(deviceIDString
, serialKeys
);
1387 * 'release_deviceinfo()' - Release deviceinfo strings.
1390 static void release_deviceinfo(CFStringRef
*make
,
1392 CFStringRef
*serial
)
1394 if (make
!= NULL
&& *make
!= NULL
)
1400 if (model
!= NULL
&& *model
!= NULL
)
1406 if (serial
!= NULL
&& *serial
!= NULL
)
1416 * 'load_classdriver()' - Load a classdriver.
1419 static kern_return_t
load_classdriver(CFStringRef driverPath
,
1420 printer_interface_t interface
,
1421 classdriver_t
***printerDriver
)
1423 kern_return_t kr
= kUSBPrinterClassDeviceNotOpen
;
1424 classdriver_t
**driver
= NULL
;
1425 CFStringRef bundle
= driverPath
? driverPath
: kUSBGenericTOPrinterClassDriver
;
1426 char bundlestr
[1024]; /* Bundle path */
1427 CFURLRef url
; /* URL for driver */
1428 CFPlugInRef plugin
= NULL
; /* Plug-in address */
1431 CFStringGetCString(bundle
, bundlestr
, sizeof(bundlestr
), kCFStringEncodingUTF8
);
1434 * Validate permissions for the class driver...
1437 _cups_fc_result_t result
= _cupsFileCheck(bundlestr
,
1438 _CUPS_FILE_CHECK_DIRECTORY
, 1,
1439 Iterating
? NULL
: _cupsFileCheckFilter
, NULL
);
1441 if (result
&& driverPath
)
1442 return (load_classdriver(NULL
, interface
, printerDriver
));
1447 * Try loading the class driver...
1450 url
= CFURLCreateWithFileSystemPath(NULL
, bundle
, kCFURLPOSIXPathStyle
, true);
1454 plugin
= CFPlugInCreate(NULL
, url
);
1462 CFArrayRef factories
= CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID
, plugin
);
1463 if (factories
!= NULL
&& CFArrayGetCount(factories
) > 0)
1465 CFUUIDRef factoryID
= CFArrayGetValueAtIndex(factories
, 0);
1466 IUnknownVTbl
**iunknown
= CFPlugInInstanceCreate(NULL
, factoryID
, kUSBPrinterClassTypeID
);
1467 if (iunknown
!= NULL
)
1469 kr
= (*iunknown
)->QueryInterface(iunknown
, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID
), (LPVOID
*)&driver
);
1470 if (kr
== kIOReturnSuccess
&& driver
!= NULL
)
1472 classdriver_t
**genericDriver
= NULL
;
1473 if (driverPath
!= NULL
&& CFStringCompare(driverPath
, kUSBGenericTOPrinterClassDriver
, 0) != kCFCompareEqualTo
)
1474 kr
= load_classdriver(NULL
, interface
, &genericDriver
);
1476 if (kr
== kIOReturnSuccess
)
1478 (*driver
)->interface
= interface
;
1479 (*driver
)->Initialize(driver
, genericDriver
);
1481 (*driver
)->plugin
= plugin
;
1482 (*driver
)->interface
= interface
;
1483 *printerDriver
= driver
;
1486 (*iunknown
)->Release(iunknown
);
1488 CFRelease(factories
);
1492 fprintf(stderr
, "DEBUG: load_classdriver(%s) (kr:0x%08x)\n", bundlestr
, (int)kr
);
1499 * 'unload_classdriver()' - Unload a classdriver.
1502 static kern_return_t
unload_classdriver(classdriver_t
***classdriver
)
1504 if (*classdriver
!= NULL
)
1506 (**classdriver
)->Release(*classdriver
);
1507 *classdriver
= NULL
;
1510 return kIOReturnSuccess
;
1515 * 'load_printerdriver()' - Load vendor's classdriver.
1517 * If driverBundlePath is not NULL on return it is the callers responsbility to release it!
1520 static kern_return_t
load_printerdriver(CFStringRef
*driverBundlePath
)
1522 IOCFPlugInInterface
**iodev
= NULL
;
1525 printer_interface_t interface
;
1528 kr
= IOCreatePlugInInterfaceForService(g
.printer_obj
, kIOUSBInterfaceUserClientTypeID
, kIOCFPlugInInterfaceID
, &iodev
, &score
);
1529 if (kr
== kIOReturnSuccess
)
1531 if ((res
= (*iodev
)->QueryInterface(iodev
, USB_INTERFACE_KIND
, (LPVOID
*) &interface
)) == noErr
)
1533 *driverBundlePath
= IORegistryEntryCreateCFProperty(g
.printer_obj
, kUSBClassDriverProperty
, NULL
, kNilOptions
);
1535 kr
= load_classdriver(*driverBundlePath
, interface
, &g
.classdriver
);
1537 if (kr
!= kIOReturnSuccess
)
1538 (*interface
)->Release(interface
);
1540 IODestroyPlugInInterface(iodev
);
1547 * 'registry_open()' - Open a connection to the printer.
1550 static kern_return_t
registry_open(CFStringRef
*driverBundlePath
)
1552 g
.bidi_flag
= 0; /* 0=unidirectional */
1554 kern_return_t kr
= load_printerdriver(driverBundlePath
);
1555 if (kr
!= kIOReturnSuccess
)
1558 if (g
.classdriver
!= NULL
)
1560 (*g
.classdriver
)->interfaceNumber
= g
.interfaceNum
;
1561 kr
= (*g
.classdriver
)->Open(g
.classdriver
, g
.location
, kUSBPrintingProtocolBidirectional
);
1562 if (kr
!= kIOReturnSuccess
|| (*g
.classdriver
)->interface
== NULL
)
1564 kr
= (*g
.classdriver
)->Open(g
.classdriver
, g
.location
, kUSBPrintingProtocolUnidirectional
);
1565 if (kr
== kIOReturnSuccess
)
1567 if ((*g
.classdriver
)->interface
== NULL
)
1569 (*g
.classdriver
)->Close(g
.classdriver
);
1575 g
.bidi_flag
= 1; /* 1=bidirectional */
1578 if (kr
!= kIOReturnSuccess
)
1579 unload_classdriver(&g
.classdriver
);
1586 * 'registry_close()' - Close the connection to the printer.
1589 static kern_return_t
registry_close(void)
1591 if (g
.classdriver
!= NULL
)
1592 (*g
.classdriver
)->Close(g
.classdriver
);
1594 unload_classdriver(&g
.classdriver
);
1595 return kIOReturnSuccess
;
1600 * 'copy_deviceid()' - Copy the 1284 device id string.
1603 static OSStatus
copy_deviceid(classdriver_t
**classdriver
,
1604 CFStringRef
*deviceID
)
1606 CFStringRef devID
= NULL
;
1607 CFStringRef deviceMake
= NULL
;
1608 CFStringRef deviceModel
= NULL
;
1609 CFStringRef deviceSerial
= NULL
;
1613 OSStatus err
= (*classdriver
)->GetDeviceID(classdriver
, &devID
, DEFAULT_TIMEOUT
);
1615 copy_deviceinfo(devID
, &deviceMake
, &deviceModel
, &deviceSerial
);
1617 if (deviceMake
== NULL
|| deviceModel
== NULL
|| deviceSerial
== NULL
)
1619 IOUSBDeviceDescriptor desc
;
1620 iodevice_request_t request
;
1622 request
.requestType
= USBmakebmRequestType(kUSBIn
, kUSBStandard
, kUSBDevice
);
1623 request
.request
= kUSBRqGetDescriptor
;
1624 request
.value
= (kUSBDeviceDesc
<< 8) | 0;
1626 request
.length
= sizeof(desc
);
1627 request
.buffer
= &desc
;
1628 err
= (*classdriver
)->DeviceRequest(classdriver
, &request
, DEFAULT_TIMEOUT
);
1629 if (err
== kIOReturnSuccess
)
1631 CFMutableStringRef newDevID
= CFStringCreateMutable(NULL
, 0);
1633 if (deviceMake
== NULL
)
1635 CFStringRef data
= NULL
;
1636 err
= (*classdriver
)->GetString(classdriver
, desc
.iManufacturer
, kUSBLanguageEnglish
, DEFAULT_TIMEOUT
, &data
);
1639 CFStringAppendFormat(newDevID
, NULL
, CFSTR("MFG:%@;"), data
);
1644 if (deviceModel
== NULL
)
1646 CFStringRef data
= NULL
;
1647 err
= (*classdriver
)->GetString(classdriver
, desc
.iProduct
, kUSBLanguageEnglish
, DEFAULT_TIMEOUT
, &data
);
1650 CFStringAppendFormat(newDevID
, NULL
, CFSTR("MDL:%@;"), data
);
1655 if (deviceSerial
== NULL
&& desc
.iSerialNumber
!= 0)
1657 err
= (*classdriver
)->GetString(classdriver
, desc
.iSerialNumber
, kUSBLanguageEnglish
, DEFAULT_TIMEOUT
, &deviceSerial
);
1658 if (deviceSerial
!= NULL
)
1660 CFStringAppendFormat(newDevID
, NULL
, CFSTR("SERN:%@;"), deviceSerial
);
1666 CFStringAppend(newDevID
, devID
);
1670 *deviceID
= newDevID
;
1678 if (*deviceID
== NULL
)
1681 /* Remove special characters from the serial number */
1682 CFRange range
= (deviceSerial
!= NULL
? CFStringFind(deviceSerial
, CFSTR("+"), 0) : CFRangeMake(0, 0));
1683 if (range
.length
== 1) {
1684 range
= CFStringFind(*deviceID
, deviceSerial
, 0);
1686 CFMutableStringRef deviceIDString
= CFStringCreateMutableCopy(NULL
, 0, *deviceID
);
1687 CFStringFindAndReplace(deviceIDString
, CFSTR("+"), CFSTR(""), range
, 0);
1688 CFRelease(*deviceID
);
1689 *deviceID
= deviceIDString
;
1692 release_deviceinfo(&deviceMake
, &deviceModel
, &deviceSerial
);
1699 * 'copy_devicestring()' - Copy the 1284 device id string.
1702 static void copy_devicestring(io_service_t usbInterface
,
1703 CFStringRef
*deviceID
,
1704 UInt32
*deviceLocation
,
1705 UInt8
*interfaceNumber
)
1707 IOCFPlugInInterface
**iodev
= NULL
;
1710 printer_interface_t interface
;
1712 classdriver_t
**klassDriver
= NULL
;
1713 CFStringRef driverBundlePath
;
1715 if ((kr
= IOCreatePlugInInterfaceForService(usbInterface
,
1716 kIOUSBInterfaceUserClientTypeID
,
1717 kIOCFPlugInInterfaceID
,
1718 &iodev
, &score
)) == kIOReturnSuccess
)
1720 if ((res
= (*iodev
)->QueryInterface(iodev
, USB_INTERFACE_KIND
, (LPVOID
*)
1721 &interface
)) == noErr
)
1723 (*interface
)->GetLocationID(interface
, deviceLocation
);
1724 (*interface
)->GetInterfaceNumber(interface
, interfaceNumber
);
1726 driverBundlePath
= IORegistryEntryCreateCFProperty(usbInterface
,
1727 kUSBClassDriverProperty
,
1730 kr
= load_classdriver(driverBundlePath
, interface
, &klassDriver
);
1732 if (kr
!= kIOReturnSuccess
&& driverBundlePath
!= NULL
)
1733 kr
= load_classdriver(NULL
, interface
, &klassDriver
);
1735 if (kr
== kIOReturnSuccess
&& klassDriver
!= NULL
)
1736 copy_deviceid(klassDriver
, deviceID
);
1738 unload_classdriver(&klassDriver
);
1740 if (driverBundlePath
!= NULL
)
1741 CFRelease(driverBundlePath
);
1743 /* (*interface)->Release(interface); */
1745 IODestroyPlugInInterface(iodev
);
1752 * 'copy_value_for_key()' - Copy value string associated with a key.
1755 static CFStringRef
copy_value_for_key(CFStringRef deviceID
,
1758 CFStringRef value
= NULL
;
1759 CFArrayRef kvPairs
= deviceID
!= NULL
? CFStringCreateArrayBySeparatingStrings(NULL
, deviceID
, CFSTR(";")) : NULL
;
1760 CFIndex max
= kvPairs
!= NULL
? CFArrayGetCount(kvPairs
) : 0;
1763 while (idx
< max
&& value
== NULL
)
1765 CFStringRef kvpair
= CFArrayGetValueAtIndex(kvPairs
, idx
);
1767 while (keys
[idxx
] != NULL
&& value
== NULL
)
1769 CFRange range
= CFStringFind(kvpair
, keys
[idxx
], kCFCompareCaseInsensitive
);
1770 if (range
.length
!= -1)
1772 if (range
.location
!= 0)
1774 CFMutableStringRef theString
= CFStringCreateMutableCopy(NULL
, 0, kvpair
);
1775 CFStringTrimWhitespace(theString
);
1776 range
= CFStringFind(theString
, keys
[idxx
], kCFCompareCaseInsensitive
);
1777 if (range
.location
== 0)
1778 value
= CFStringCreateWithSubstring(NULL
, theString
, CFRangeMake(range
.length
, CFStringGetLength(theString
) - range
.length
));
1780 CFRelease(theString
);
1784 CFStringRef theString
= CFStringCreateWithSubstring(NULL
, kvpair
, CFRangeMake(range
.length
, CFStringGetLength(kvpair
) - range
.length
));
1785 CFMutableStringRef theString2
= CFStringCreateMutableCopy(NULL
, 0, theString
);
1786 CFRelease(theString
);
1788 CFStringTrimWhitespace(theString2
);
1797 if (kvPairs
!= NULL
)
1804 * 'cfstr_create_trim()' - Create CFString and trim whitespace characters.
1807 CFStringRef
cfstr_create_trim(const char *cstr
)
1810 CFMutableStringRef cfmutablestr
= NULL
;
1812 if ((cfstr
= CFStringCreateWithCString(NULL
, cstr
, kCFStringEncodingUTF8
)) != NULL
)
1814 if ((cfmutablestr
= CFStringCreateMutableCopy(NULL
, 1024, cfstr
)) != NULL
)
1815 CFStringTrimWhitespace(cfmutablestr
);
1819 return (CFStringRef
) cfmutablestr
;
1825 * 'parse_options()' - Parse URI options.
1828 static void parse_options(char *options
,
1834 char sep
, /* Separator character */
1835 *name
, /* Name of option */
1836 *value
; /* Value of option */
1855 while (*options
&& *options
!= '=' && *options
!= '+' && *options
!= '&')
1858 if ((sep
= *options
) != '\0')
1869 while (*options
&& *options
!= '+' && *options
!= '&')
1879 * Process the option...
1882 if (!_cups_strcasecmp(name
, "waiteof"))
1884 if (!_cups_strcasecmp(value
, "on") ||
1885 !_cups_strcasecmp(value
, "yes") ||
1886 !_cups_strcasecmp(value
, "true"))
1888 else if (!_cups_strcasecmp(value
, "off") ||
1889 !_cups_strcasecmp(value
, "no") ||
1890 !_cups_strcasecmp(value
, "false"))
1893 _cupsLangPrintFilter(stderr
, "WARNING",
1894 _("Boolean expected for waiteof option \"%s\"."),
1897 else if (!_cups_strcasecmp(name
, "serial"))
1898 strlcpy(serial
, value
, serial_size
);
1899 else if (!_cups_strcasecmp(name
, "location") && location
)
1900 *location
= strtol(value
, NULL
, 16);
1906 * @function setup_cfLanguage
1907 * @abstract Convert the contents of the CUPS 'APPLE_LANGUAGE' environment
1908 * variable into a one element CF array of languages.
1910 * @discussion Each submitted job comes with a natural language. CUPS passes
1911 * that language in an environment variable. We take that language
1912 * and jam it into the AppleLanguages array so that CF will use
1913 * it when reading localized resources. We need to do this before
1914 * any CF code reads and caches the languages array, so this function
1915 * should be called early in main()
1917 static void setup_cfLanguage(void)
1919 CFStringRef lang
[1] = {NULL
};
1920 CFArrayRef langArray
= NULL
;
1921 const char *requestedLang
= NULL
;
1923 if ((requestedLang
= getenv("APPLE_LANGUAGE")) == NULL
)
1924 requestedLang
= getenv("LANG");
1926 if (requestedLang
!= NULL
)
1928 lang
[0] = CFStringCreateWithCString(kCFAllocatorDefault
, requestedLang
, kCFStringEncodingUTF8
);
1929 langArray
= CFArrayCreate(kCFAllocatorDefault
, (const void **)lang
, sizeof(lang
) / sizeof(lang
[0]), &kCFTypeArrayCallBacks
);
1931 CFPreferencesSetValue(CFSTR("AppleLanguages"), langArray
, kCFPreferencesCurrentApplication
, kCFPreferencesAnyUser
, kCFPreferencesAnyHost
);
1932 fprintf(stderr
, "DEBUG: usb: AppleLanguages=\"%s\"\n", requestedLang
);
1935 CFRelease(langArray
);
1938 fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr
);
1942 #if defined(__i386__) || defined(__x86_64__)
1944 * @function run_legacy_backend
1946 * @abstract Starts child backend process running as a ppc or i386 executable.
1948 * @result Never returns; always calls exit().
1952 static void run_legacy_backend(int argc
,
1959 pid_t waitpid_status
;
1961 char *usb_legacy_status
;
1964 * If we're running as x86_64 or i386 and couldn't load the class driver
1965 * (because it's ppc or i386), then try to re-exec ourselves in ppc or i386
1966 * mode to try again. If we don't have a ppc or i386 architecture we may be
1967 * running with the same architecture again so guard against this by setting
1968 * and testing an environment variable...
1972 usb_legacy_status
= getenv("USB_I386_STATUS");
1974 usb_legacy_status
= getenv("USB_PPC_STATUS");
1975 # endif /* __x86_64__ */
1977 if (!usb_legacy_status
)
1980 * Setup a SIGTERM handler then block it before forking...
1983 int err
; /* posix_spawn result */
1984 struct sigaction action
; /* POSIX signal action */
1985 sigset_t newmask
, /* New signal mask */
1986 oldmask
; /* Old signal mask */
1987 char usbpath
[1024]; /* Path to USB backend */
1988 const char *cups_serverbin
;/* Path to CUPS binaries */
1991 memset(&action
, 0, sizeof(action
));
1992 sigaddset(&action
.sa_mask
, SIGTERM
);
1993 action
.sa_handler
= sigterm_handler
;
1994 sigaction(SIGTERM
, &action
, NULL
);
1996 sigemptyset(&newmask
);
1997 sigaddset(&newmask
, SIGTERM
);
1998 sigprocmask(SIG_BLOCK
, &newmask
, &oldmask
);
2001 * Set the environment variable...
2005 setenv("USB_I386_STATUS", "1", false);
2007 setenv("USB_PPC_STATUS", "1", false);
2008 # endif /* __x86_64__ */
2011 * Tell the kernel to use the specified CPU architecture...
2015 cpu_type_t cpu
= CPU_TYPE_I386
;
2017 cpu_type_t cpu
= CPU_TYPE_POWERPC
;
2018 # endif /* __x86_64__ */
2020 posix_spawnattr_t attrs
;
2022 if (!posix_spawnattr_init(&attrs
))
2024 posix_spawnattr_setsigdefault(&attrs
, &oldmask
);
2025 if (posix_spawnattr_setbinpref_np(&attrs
, 1, &cpu
, &ocount
) || ocount
!= 1)
2028 perror("DEBUG: Unable to set binary preference to i386");
2030 perror("DEBUG: Unable to set binary preference to ppc");
2031 # endif /* __x86_64__ */
2032 _cupsLangPrintFilter(stderr
, "ERROR",
2033 _("Unable to use legacy USB class driver."));
2034 exit(CUPS_BACKEND_STOP
);
2039 * Set up the arguments and call posix_spawn...
2042 if ((cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
2043 cups_serverbin
= CUPS_SERVERBIN
;
2044 snprintf(usbpath
, sizeof(usbpath
), "%s/backend/usb", cups_serverbin
);
2046 for (i
= 0; i
< argc
&& i
< (sizeof(my_argv
) / sizeof(my_argv
[0])) - 1; i
++)
2047 my_argv
[i
] = argv
[i
];
2051 if ((err
= posix_spawn(&child_pid
, usbpath
, NULL
, &attrs
, my_argv
,
2054 fprintf(stderr
, "DEBUG: Unable to exec %s: %s\n", usbpath
,
2056 _cupsLangPrintFilter(stderr
, "ERROR",
2057 _("Unable to use legacy USB class driver."));
2058 exit(CUPS_BACKEND_STOP
);
2062 * Unblock signals...
2065 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
2068 * Close the fds we won't be using then wait for the child backend to exit.
2074 fprintf(stderr
, "DEBUG: Started usb(legacy) backend (PID %d)\n",
2077 while ((waitpid_status
= waitpid(child_pid
, &childstatus
, 0)) == (pid_t
)-1 && errno
== EINTR
)
2080 if (WIFSIGNALED(childstatus
))
2082 exitstatus
= CUPS_BACKEND_STOP
;
2083 fprintf(stderr
, "DEBUG: usb(legacy) backend %d crashed on signal %d\n",
2084 child_pid
, WTERMSIG(childstatus
));
2088 if ((exitstatus
= WEXITSTATUS(childstatus
)) != 0)
2090 "DEBUG: usb(legacy) backend %d stopped with status %d\n",
2091 child_pid
, exitstatus
);
2093 fprintf(stderr
, "DEBUG: usb(legacy) backend %d exited with no errors\n",
2099 fputs("DEBUG: usb(legacy) backend running native again\n", stderr
);
2100 exitstatus
= CUPS_BACKEND_STOP
;
2105 #endif /* __i386__ || __x86_64__ */
2109 * 'sigterm_handler()' - SIGTERM handler.
2113 sigterm_handler(int sig
) /* I - Signal */
2115 #if defined(__i386__) || defined(__x86_64__)
2117 * If we started a child process pass the signal on to it...
2123 * If we started a child process pass the signal on to it...
2128 kill(child_pid
, sig
);
2129 while (waitpid(child_pid
, &status
, 0) < 0 && errno
== EINTR
);
2131 if (WIFEXITED(status
))
2132 exit(WEXITSTATUS(status
));
2133 else if (status
== SIGTERM
|| status
== SIGKILL
)
2137 fprintf(stderr
, "DEBUG: Child crashed on signal %d\n", status
);
2138 exit(CUPS_BACKEND_STOP
);
2141 #endif /* __i386__ || __x86_64__ */
2146 * 'sigquit_handler()' - SIGQUIT handler.
2149 static void sigquit_handler(int sig
, siginfo_t
*si
, void *unused
)
2152 char pathbuf
[PROC_PIDPATHINFO_MAXSIZE
];
2153 static char msgbuf
[256] = "";
2159 if (proc_pidpath(si
->si_pid
, pathbuf
, sizeof(pathbuf
)) > 0 &&
2160 (path
= basename(pathbuf
)) != NULL
)
2161 snprintf(msgbuf
, sizeof(msgbuf
), "SIGQUIT sent by %s(%d)", path
, (int)si
->si_pid
);
2163 snprintf(msgbuf
, sizeof(msgbuf
), "SIGQUIT sent by PID %d", (int)si
->si_pid
);
2165 CRSetCrashLogMessage(msgbuf
);
2171 #ifdef PARSE_PS_ERRORS
2173 * 'next_line()' - Find the next line in a buffer.
2176 static const char *next_line (const char *buffer
)
2178 const char *cptr
, *lptr
= NULL
;
2180 for (cptr
= buffer
; *cptr
&& lptr
== NULL
; cptr
++)
2181 if (*cptr
== '\n' || *cptr
== '\r')
2188 * 'parse_pserror()' - Scan the backchannel data for postscript errors.
2191 static void parse_pserror(char *sockBuffer
,
2194 static char gErrorBuffer
[1024] = "";
2195 static char *gErrorBufferPtr
= gErrorBuffer
;
2196 static char *gErrorBufferEndPtr
= gErrorBuffer
+ sizeof(gErrorBuffer
);
2198 char *pCommentBegin
, *pCommentEnd
, *pLineEnd
;
2203 if (gErrorBufferPtr
+ len
> gErrorBufferEndPtr
- 1)
2204 gErrorBufferPtr
= gErrorBuffer
;
2205 if (len
> sizeof(gErrorBuffer
) - 1)
2206 len
= sizeof(gErrorBuffer
) - 1;
2208 memcpy(gErrorBufferPtr
, (const void *)sockBuffer
, len
);
2209 gErrorBufferPtr
+= len
;
2210 *(gErrorBufferPtr
+ 1) = '\0';
2212 pLineEnd
= (char *)next_line((const char *)gErrorBuffer
);
2213 while (pLineEnd
!= NULL
)
2217 pCommentBegin
= strstr(gErrorBuffer
,"%%[");
2218 pCommentEnd
= strstr(gErrorBuffer
, "]%%");
2219 if (pCommentBegin
!= gErrorBuffer
&& pCommentEnd
!= NULL
)
2221 pCommentEnd
+= 3; /* Skip past "]%%" */
2222 *pCommentEnd
= '\0'; /* There's always room for the nul */
2224 if (_cups_strncasecmp(pCommentBegin
, "%%[ Error:", 10) == 0)
2226 else if (_cups_strncasecmp(pCommentBegin
, "%%[ Flushing", 12) == 0)
2231 if ((logstrlen
= snprintf(logstr
, sizeof(logstr
), "%s: %s\n", logLevel
, pCommentBegin
)) >= sizeof(logstr
))
2233 /* If the string was trucnated make sure it has a linefeed before the nul */
2234 logstrlen
= sizeof(logstr
) - 1;
2235 logstr
[logstrlen
- 1] = '\n';
2237 write(STDERR_FILENO
, logstr
, logstrlen
);
2240 /* move everything over... */
2241 strlcpy(gErrorBuffer
, pLineEnd
, sizeof(gErrorBuffer
));
2242 gErrorBufferPtr
= gErrorBuffer
;
2243 pLineEnd
= (char *)next_line((const char *)gErrorBuffer
);
2246 #endif /* PARSE_PS_ERRORS */
2250 * 'soft_reset()' - Send a soft reset to the device.
2253 static void soft_reset(void)
2255 fd_set input_set
; /* Input set for select() */
2256 struct timeval tv
; /* Time value */
2257 char buffer
[2048]; /* Buffer */
2258 struct timespec cond_timeout
; /* pthread condition timeout */
2261 * Send an abort once a second until the I/O lock is released by the main thread...
2264 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
2265 while (g
.readwrite_lock
)
2267 (*g
.classdriver
)->Abort(g
.classdriver
);
2269 gettimeofday(&tv
, NULL
);
2270 cond_timeout
.tv_sec
= tv
.tv_sec
+ 1;
2271 cond_timeout
.tv_nsec
= tv
.tv_usec
* 1000;
2273 while (g
.readwrite_lock
)
2275 if (pthread_cond_timedwait(&g
.readwrite_lock_cond
,
2276 &g
.readwrite_lock_mutex
,
2277 &cond_timeout
) != 0)
2282 g
.readwrite_lock
= 1;
2283 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
2286 * Flush bytes waiting on print_fd...
2291 FD_ZERO(&input_set
);
2292 FD_SET(g
.print_fd
, &input_set
);
2297 while (select(g
.print_fd
+1, &input_set
, NULL
, NULL
, &tv
) > 0)
2298 if (read(g
.print_fd
, buffer
, sizeof(buffer
)) <= 0)
2305 (*g
.classdriver
)->SoftReset(g
.classdriver
, DEFAULT_TIMEOUT
);
2308 * Release the I/O lock...
2311 pthread_mutex_lock(&g
.readwrite_lock_mutex
);
2312 g
.readwrite_lock
= 0;
2313 pthread_cond_signal(&g
.readwrite_lock_cond
);
2314 pthread_mutex_unlock(&g
.readwrite_lock_mutex
);
2319 * 'get_device_id()' - Return IEEE-1284 device ID.
2322 static void get_device_id(cups_sc_status_t
*status
,
2326 CFStringRef deviceIDString
= NULL
;
2329 copy_deviceid(g
.classdriver
, &deviceIDString
);
2333 CFStringGetCString(deviceIDString
, data
, *datalen
, kCFStringEncodingUTF8
);
2334 *datalen
= strlen(data
);
2335 CFRelease(deviceIDString
);
2337 *status
= CUPS_SC_STATUS_OK
;