]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
581dae2d MS |
2 | * "$Id$" |
3 | * | |
7530b13a | 4 | * Copyright 2005-2015 Apple Inc. All rights reserved. |
581dae2d MS |
5 | * |
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. | |
12 | * | |
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. | |
28 | * | |
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. | |
34 | * | |
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. | |
43 | */ | |
ef416fc2 | 44 | |
45 | /* | |
09a101d6 | 46 | * Include necessary headers. |
ef416fc2 | 47 | */ |
09a101d6 | 48 | |
ef416fc2 | 49 | #include <stdio.h> |
50 | #include <stdlib.h> | |
51 | #include <errno.h> | |
52 | #include <signal.h> | |
53 | #include <fcntl.h> | |
54 | #include <termios.h> | |
55 | #include <unistd.h> | |
ac884b6a | 56 | #include <sys/stat.h> |
e53920b9 | 57 | #include <sys/sysctl.h> |
58 | #include <libgen.h> | |
09a101d6 | 59 | #include <mach/mach.h> |
e53920b9 | 60 | #include <mach/mach_error.h> |
61 | #include <mach/mach_time.h> | |
71e16022 | 62 | #include <cups/debug-private.h> |
22c9029b | 63 | #include <cups/file-private.h> |
f7deaa1a | 64 | #include <cups/sidechannel.h> |
71e16022 | 65 | #include <cups/language-private.h> |
f787e1e3 | 66 | #include <cups/ppd-private.h> |
db0bd74a | 67 | #include "backend-private.h" |
e53920b9 | 68 | #include <CoreFoundation/CoreFoundation.h> |
69 | #include <IOKit/usb/IOUSBLib.h> | |
70 | #include <IOKit/IOCFPlugIn.h> | |
a469f8a5 | 71 | #include <libproc.h> |
9fb65b89 | 72 | #include <asl.h> |
749b1e90 | 73 | #include <spawn.h> |
e53920b9 | 74 | #include <pthread.h> |
ef416fc2 | 75 | |
fe750b86 MS |
76 | /* |
77 | * Include necessary headers. | |
78 | */ | |
79 | ||
749b1e90 MS |
80 | extern char **environ; |
81 | ||
f7deaa1a | 82 | |
acb056cb MS |
83 | /* |
84 | * DEBUG_WRITES, if defined, causes the backend to write data to the printer in | |
85 | * 512 byte increments, up to 8192 bytes, to make debugging with a USB bus | |
86 | * analyzer easier. | |
87 | */ | |
88 | ||
89 | #define DEBUG_WRITES 0 | |
90 | ||
7cf5915e MS |
91 | /* |
92 | * WAIT_EOF_DELAY is number of seconds we'll wait for responses from | |
93 | * the printer after we've finished sending all the data | |
94 | */ | |
95 | #define WAIT_EOF_DELAY 7 | |
96 | #define WAIT_SIDE_DELAY 3 | |
7dfedb92 | 97 | #define DEFAULT_TIMEOUT 5000L |
ef416fc2 | 98 | |
f1ebe30e | 99 | #define USB_INTERFACE_KIND CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID245) |
e53920b9 | 100 | #define kUSBLanguageEnglish 0x409 |
ef416fc2 | 101 | |
09a101d6 | 102 | #define PRINTER_POLLING_INTERVAL 5 /* seconds */ |
103 | #define INITIAL_LOG_INTERVAL PRINTER_POLLING_INTERVAL | |
104 | #define SUBSEQUENT_LOG_INTERVAL 3 * INITIAL_LOG_INTERVAL | |
ef416fc2 | 105 | |
09a101d6 | 106 | #define kUSBPrinterClassTypeID CFUUIDGetConstantUUIDWithBytes(NULL, 0x06, 0x04, 0x7D, 0x16, 0x53, 0xA2, 0x11, 0xD6, 0x92, 0x06, 0x00, 0x30, 0x65, 0x52, 0x45, 0x92) |
107 | #define kUSBPrinterClassInterfaceID CFUUIDGetConstantUUIDWithBytes(NULL, 0x03, 0x34, 0x6D, 0x74, 0x53, 0xA3, 0x11, 0xD6, 0x9E, 0xA1, 0x76, 0x30, 0x65, 0x52, 0x45, 0x92) | |
ef416fc2 | 108 | |
109 | #define kUSBClassDriverProperty CFSTR("USB Printing Class") | |
ef416fc2 | 110 | |
84315f46 | 111 | #define kUSBGenericTOPrinterClassDriver CFSTR("/System/Library/Printers/Libraries/USBGenericPrintingClass.plugin") |
e53920b9 | 112 | #define kUSBPrinterClassDeviceNotOpen -9664 /*kPMInvalidIOMContext*/ |
ef416fc2 | 113 | |
a469f8a5 MS |
114 | #define CRSetCrashLogMessage(m) _crc_make_setter(message, m) |
115 | #define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg)) | |
116 | #define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden"))) | |
117 | #define CRASHREPORTER_ANNOTATIONS_VERSION 4 | |
118 | #define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info" | |
119 | ||
120 | struct crashreporter_annotations_t { | |
121 | uint64_t version; // unsigned long | |
122 | uint64_t message; // char * | |
123 | uint64_t signature_string; // char * | |
124 | uint64_t backtrace; // char * | |
125 | uint64_t message2; // char * | |
126 | uint64_t thread; // uint64_t | |
127 | uint64_t dialog_mode; // unsigned int | |
128 | }; | |
129 | ||
0fa6c7fa MS |
130 | CRASH_REPORTER_CLIENT_HIDDEN |
131 | struct crashreporter_annotations_t gCRAnnotations | |
132 | __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) | |
a469f8a5 | 133 | = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 }; |
ef416fc2 | 134 | |
e53920b9 | 135 | /* |
136 | * Section 5.3 USB Printing Class spec | |
137 | */ | |
138 | #define kUSBPrintingSubclass 1 | |
139 | #define kUSBPrintingProtocolNoOpen 0 | |
140 | #define kUSBPrintingProtocolUnidirectional 1 | |
141 | #define kUSBPrintingProtocolBidirectional 2 | |
fe750b86 | 142 | #define kUSBPrintingProtocolIPP 4 |
e53920b9 | 143 | |
f1ebe30e | 144 | typedef IOUSBInterfaceInterface245 **printer_interface_t; |
ef416fc2 | 145 | |
09a101d6 | 146 | typedef struct iodevice_request_s /**** Device request ****/ |
ef416fc2 | 147 | { |
09a101d6 | 148 | UInt8 requestType; |
e53920b9 | 149 | UInt8 request; |
150 | UInt16 value; | |
151 | UInt16 index; | |
152 | UInt16 length; | |
09a101d6 | 153 | void *buffer; |
e53920b9 | 154 | } iodevice_request_t; |
155 | ||
09a101d6 | 156 | typedef union /**** Centronics status byte ****/ |
157 | { | |
e53920b9 | 158 | char b; |
09a101d6 | 159 | struct |
160 | { | |
e53920b9 | 161 | unsigned reserved0:2; |
162 | unsigned paperError:1; | |
163 | unsigned select:1; | |
164 | unsigned notError:1; | |
165 | unsigned reserved1:3; | |
166 | } status; | |
167 | } centronics_status_t; | |
168 | ||
09a101d6 | 169 | typedef struct classdriver_s /**** g.classdriver context ****/ |
ef416fc2 | 170 | { |
171 | IUNKNOWN_C_GUTS; | |
e53920b9 | 172 | CFPlugInRef plugin; /* release plugin */ |
173 | IUnknownVTbl **factory; /* Factory */ | |
174 | void *vendorReference; /* vendor class specific usage */ | |
175 | UInt32 location; /* unique location in bus topology */ | |
176 | UInt8 interfaceNumber; /* Interface number */ | |
177 | UInt16 vendorID; /* Vendor id */ | |
178 | UInt16 productID; /* Product id */ | |
179 | printer_interface_t interface; /* identify the device to IOKit */ | |
a469f8a5 | 180 | UInt8 outpipe; /* mandatory bulkOut pipe */ |
e53920b9 | 181 | UInt8 inpipe; /* optional bulkIn pipe */ |
ef416fc2 | 182 | |
e53920b9 | 183 | /* general class requests */ |
09a101d6 | 184 | kern_return_t (*DeviceRequest)(struct classdriver_s **printer, iodevice_request_t *iorequest, UInt16 timeout); |
185 | kern_return_t (*GetString)(struct classdriver_s **printer, UInt8 whichString, UInt16 language, UInt16 timeout, CFStringRef *result); | |
ef416fc2 | 186 | |
e53920b9 | 187 | /* standard printer class requests */ |
09a101d6 | 188 | kern_return_t (*SoftReset)(struct classdriver_s **printer, UInt16 timeout); |
189 | kern_return_t (*GetCentronicsStatus)(struct classdriver_s **printer, centronics_status_t *result, UInt16 timeout); | |
190 | kern_return_t (*GetDeviceID)(struct classdriver_s **printer, CFStringRef *devid, UInt16 timeout); | |
ef416fc2 | 191 | |
e53920b9 | 192 | /* standard bulk device requests */ |
09a101d6 | 193 | kern_return_t (*ReadPipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count); |
194 | kern_return_t (*WritePipe)(struct classdriver_s **printer, UInt8 *buffer, UInt32 *count, Boolean eoj); | |
ef416fc2 | 195 | |
e53920b9 | 196 | /* interface requests */ |
09a101d6 | 197 | kern_return_t (*Open)(struct classdriver_s **printer, UInt32 location, UInt8 protocol); |
198 | kern_return_t (*Abort)(struct classdriver_s **printer); | |
199 | kern_return_t (*Close)(struct classdriver_s **printer); | |
ef416fc2 | 200 | |
e53920b9 | 201 | /* initialize and terminate */ |
09a101d6 | 202 | kern_return_t (*Initialize)(struct classdriver_s **printer, struct classdriver_s **baseclass); |
203 | kern_return_t (*Terminate)(struct classdriver_s **printer); | |
ef416fc2 | 204 | |
09a101d6 | 205 | } classdriver_t; |
ef416fc2 | 206 | |
fe750b86 | 207 | typedef Boolean (*iterator_callback_t)(void *refcon, io_service_t obj, CFStringRef deviceIDString, UInt32 deviceLocation, UInt8 interfaceNum, UInt8 alternateSetting); |
ef416fc2 | 208 | |
09a101d6 | 209 | typedef struct iterator_reference_s /**** Iterator reference data */ |
210 | { | |
e53920b9 | 211 | iterator_callback_t callback; |
212 | void *userdata; | |
213 | Boolean keepRunning; | |
214 | } iterator_reference_t; | |
ef416fc2 | 215 | |
09a101d6 | 216 | typedef struct globals_s |
217 | { | |
218 | io_service_t printer_obj; | |
219 | classdriver_t **classdriver; | |
220 | ||
221 | pthread_mutex_t read_thread_mutex; | |
222 | pthread_cond_t read_thread_cond; | |
223 | int read_thread_stop; | |
224 | int read_thread_done; | |
ef416fc2 | 225 | |
09a101d6 | 226 | pthread_mutex_t readwrite_lock_mutex; |
227 | pthread_cond_t readwrite_lock_cond; | |
228 | int readwrite_lock; | |
ef416fc2 | 229 | |
e53920b9 | 230 | CFStringRef make; |
231 | CFStringRef model; | |
232 | CFStringRef serial; | |
e53920b9 | 233 | UInt32 location; |
bc44d920 | 234 | UInt8 interfaceNum; |
fe750b86 | 235 | UInt8 alternateSetting; |
09a101d6 | 236 | |
237 | CFRunLoopTimerRef status_timer; | |
238 | ||
239 | int print_fd; /* File descriptor to print */ | |
240 | ssize_t print_bytes; /* Print bytes read */ | |
acb056cb MS |
241 | #if DEBUG_WRITES |
242 | ssize_t debug_bytes; /* Current bytes to read */ | |
243 | #endif /* DEBUG_WRITES */ | |
09a101d6 | 244 | |
7530b13a | 245 | Boolean use_generic_class_driver; |
09a101d6 | 246 | Boolean wait_eof; |
247 | int drain_output; /* Drain all pending output */ | |
248 | int bidi_flag; /* 0=unidirectional, 1=bidirectional */ | |
7cf5915e MS |
249 | |
250 | pthread_mutex_t sidechannel_thread_mutex; | |
251 | pthread_cond_t sidechannel_thread_cond; | |
252 | int sidechannel_thread_stop; | |
253 | int sidechannel_thread_done; | |
09a101d6 | 254 | } globals_t; |
255 | ||
256 | ||
257 | /* | |
258 | * Globals... | |
259 | */ | |
260 | ||
261 | globals_t g = { 0 }; /* Globals */ | |
0fa6c7fa | 262 | int Iterating = 0; /* Are we iterating the bus? */ |
ef416fc2 | 263 | |
ef416fc2 | 264 | |
e53920b9 | 265 | /* |
266 | * Local functions... | |
267 | */ | |
ef416fc2 | 268 | |
fe750b86 MS |
269 | static Boolean find_device_cb(void *refcon, io_service_t obj, CFStringRef deviceIDString, UInt32 deviceLocation, UInt8 interfaceNum, UInt8 alternateSetting); |
270 | static Boolean list_device_cb(void *refcon, io_service_t obj, CFStringRef deviceIDString, UInt32 deviceLocation, UInt8 interfaceNum, UInt8 alternateSetting); | |
271 | ||
09a101d6 | 272 | static CFStringRef cfstr_create_trim(const char *cstr); |
273 | static CFStringRef copy_value_for_key(CFStringRef deviceID, CFStringRef *keys); | |
321d8d57 | 274 | static kern_return_t load_classdriver(CFStringRef driverPath, printer_interface_t interface, classdriver_t ***printerDriver); |
09a101d6 | 275 | static kern_return_t load_printerdriver(CFStringRef *driverBundlePath); |
eac3a0a0 | 276 | static kern_return_t registry_close(void); |
09a101d6 | 277 | static kern_return_t registry_open(CFStringRef *driverBundlePath); |
eac3a0a0 | 278 | static kern_return_t unload_classdriver(classdriver_t ***classdriver); |
fe750b86 | 279 | |
09a101d6 | 280 | static void *read_thread(void *reference); |
281 | static void *sidechannel_thread(void *reference); | |
09a101d6 | 282 | static void device_added(void *userdata, io_iterator_t iterator); |
283 | static void get_device_id(cups_sc_status_t *status, char *data, int *datalen); | |
284 | static void iterate_printers(iterator_callback_t callBack, void *userdata); | |
db1f069b | 285 | static void parse_options(char *options, char *serial, int serial_size, UInt32 *location, Boolean *wait_eof); |
e53920b9 | 286 | static void setup_cfLanguage(void); |
eac3a0a0 | 287 | static void soft_reset(void); |
09a101d6 | 288 | static void status_timer_cb(CFRunLoopTimerRef timer, void *info); |
9fb65b89 MS |
289 | static void log_usb_class_driver(int is_64bit); |
290 | #define IS_64BIT 1 | |
291 | #define IS_NOT_64BIT 0 | |
e53920b9 | 292 | |
749b1e90 | 293 | #if defined(__i386__) || defined(__x86_64__) |
f11a948a | 294 | static pid_t child_pid; /* Child PID */ |
7e86f2f6 | 295 | static void run_legacy_backend(int argc, char *argv[], int fd) __attribute__((noreturn)); /* Starts child backend process running as a ppc executable */ |
18ecb428 | 296 | #endif /* __i386__ || __x86_64__ */ |
6c48a6ca | 297 | static void sigterm_handler(int sig); /* SIGTERM handler */ |
7e86f2f6 | 298 | static void sigquit_handler(int sig, siginfo_t *si, void *unused) __attribute__((noreturn)); |
ef416fc2 | 299 | |
e53920b9 | 300 | #ifdef PARSE_PS_ERRORS |
301 | static const char *next_line (const char *buffer); | |
302 | static void parse_pserror (char *sockBuffer, int len); | |
303 | #endif /* PARSE_PS_ERRORS */ | |
ef416fc2 | 304 | |
f1ebe30e | 305 | static printer_interface_t usb_printer_interface_interface(io_service_t usbClass); |
fe750b86 | 306 | static IOUSBDeviceInterface **usb_device_interface_for_device(io_service_t usbDevice); |
f1ebe30e MS |
307 | static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting); |
308 | static CFStringRef copy_printer_interface_indexed_description(printer_interface_t printer, UInt8 index, UInt16 language); | |
fe750b86 MS |
309 | static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID); |
310 | static CFStringRef deviceIDCopyModel(CFStringRef deviceID); | |
311 | static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID); | |
312 | ||
e53920b9 | 313 | #pragma mark - |
ef416fc2 | 314 | |
e53920b9 | 315 | /* |
316 | * 'list_devices()' - List all USB devices. | |
317 | */ | |
ef416fc2 | 318 | |
e53920b9 | 319 | void list_devices() |
ef416fc2 | 320 | { |
09a101d6 | 321 | iterate_printers(list_device_cb, NULL); |
ef416fc2 | 322 | } |
323 | ||
ef416fc2 | 324 | |
e53920b9 | 325 | /* |
326 | * 'print_device()' - Print a file to a USB device. | |
327 | */ | |
ef416fc2 | 328 | |
e53920b9 | 329 | int /* O - Exit status */ |
330 | print_device(const char *uri, /* I - Device URI */ | |
331 | const char *hostname, /* I - Hostname/manufacturer */ | |
332 | const char *resource, /* I - Resource/modelname */ | |
db1f069b | 333 | char *options, /* I - Device options/serial number */ |
09a101d6 | 334 | int print_fd, /* I - File descriptor to print */ |
e53920b9 | 335 | int copies, /* I - Copies to print */ |
336 | int argc, /* I - Number of command-line arguments (6 or 7) */ | |
337 | char *argv[]) /* I - Command-line arguments */ | |
ef416fc2 | 338 | { |
09a101d6 | 339 | char serial[1024]; /* Serial number buffer */ |
340 | OSStatus status; /* Function results */ | |
acb056cb | 341 | IOReturn iostatus; /* Current IO status */ |
09a101d6 | 342 | pthread_t read_thread_id, /* Read thread */ |
db1f069b | 343 | sidechannel_thread_id;/* Side-channel thread */ |
ac884b6a MS |
344 | int have_sidechannel = 0; /* Was the side-channel thread started? */ |
345 | struct stat sidechannel_info; /* Side-channel file descriptor info */ | |
09a101d6 | 346 | char print_buffer[8192], /* Print data buffer */ |
347 | *print_ptr; /* Pointer into print data buffer */ | |
348 | UInt32 location; /* Unique location in bus topology */ | |
349 | fd_set input_set; /* Input set for select() */ | |
350 | CFStringRef driverBundlePath; /* Class driver path */ | |
351 | int countdown, /* Logging interval */ | |
352 | nfds; /* Number of file descriptors */ | |
353 | ssize_t total_bytes; /* Total bytes written */ | |
354 | UInt32 bytes; /* Bytes written */ | |
355 | struct timeval *timeout, /* Timeout pointer */ | |
84315f46 | 356 | tv; /* Time value */ |
7cf5915e | 357 | struct timespec cond_timeout; /* pthread condition timeout */ |
a469f8a5 | 358 | struct sigaction action; /* Actions for POSIX signals */ |
e53920b9 | 359 | |
db1f069b | 360 | |
321d8d57 MS |
361 | (void)uri; |
362 | ||
a469f8a5 MS |
363 | /* |
364 | * Catch SIGQUIT to determine who is sending it... | |
365 | */ | |
366 | ||
367 | memset(&action, 0, sizeof(action)); | |
368 | action.sa_sigaction = sigquit_handler; | |
369 | action.sa_flags = SA_SIGINFO; | |
370 | sigaction(SIGQUIT, &action, NULL); | |
371 | ||
ac884b6a MS |
372 | /* |
373 | * See if the side-channel descriptor is valid... | |
374 | */ | |
375 | ||
376 | have_sidechannel = !fstat(CUPS_SC_FD, &sidechannel_info) && | |
377 | S_ISSOCK(sidechannel_info.st_mode); | |
378 | ||
379 | /* | |
380 | * Localize using CoreFoundation... | |
381 | */ | |
382 | ||
e53920b9 | 383 | setup_cfLanguage(); |
09a101d6 | 384 | |
76cd9e37 | 385 | parse_options(options, serial, sizeof(serial), &location, &g.wait_eof); |
e53920b9 | 386 | |
387 | if (resource[0] == '/') | |
388 | resource++; | |
389 | ||
09a101d6 | 390 | g.print_fd = print_fd; |
391 | g.make = cfstr_create_trim(hostname); | |
392 | g.model = cfstr_create_trim(resource); | |
393 | g.serial = cfstr_create_trim(serial); | |
394 | g.location = location; | |
e53920b9 | 395 | |
7dfedb92 MS |
396 | if (!g.make || !g.model) |
397 | { | |
c7017ecc | 398 | fprintf(stderr, "DEBUG: Fatal USB error.\n"); |
0837b7e8 MS |
399 | _cupsLangPrintFilter(stderr, "ERROR", |
400 | _("There was an unrecoverable USB error.")); | |
005dd1eb MS |
401 | |
402 | if (!g.make) | |
4d301e69 | 403 | fputs("DEBUG: USB make string is NULL\n", stderr); |
005dd1eb | 404 | if (!g.model) |
4d301e69 | 405 | fputs("DEBUG: USB model string is NULL\n", stderr); |
005dd1eb MS |
406 | |
407 | return (CUPS_BACKEND_STOP); | |
7dfedb92 MS |
408 | } |
409 | ||
e53920b9 | 410 | fputs("STATE: +connecting-to-device\n", stderr); |
411 | ||
09a101d6 | 412 | countdown = INITIAL_LOG_INTERVAL; |
413 | ||
414 | do | |
415 | { | |
416 | if (g.printer_obj) | |
417 | { | |
418 | IOObjectRelease(g.printer_obj); | |
419 | unload_classdriver(&g.classdriver); | |
420 | g.printer_obj = 0x0; | |
421 | g.classdriver = 0x0; | |
ef416fc2 | 422 | } |
ef416fc2 | 423 | |
c0e1af83 | 424 | fprintf(stderr, "DEBUG: Looking for '%s %s'\n", hostname, resource); |
09a101d6 | 425 | |
426 | iterate_printers(find_device_cb, NULL); | |
e53920b9 | 427 | |
c0e1af83 | 428 | fputs("DEBUG: Opening connection\n", stderr); |
411affcf | 429 | |
430 | driverBundlePath = NULL; | |
09a101d6 | 431 | |
432 | status = registry_open(&driverBundlePath); | |
433 | ||
749b1e90 | 434 | #if defined(__i386__) || defined(__x86_64__) |
e53920b9 | 435 | /* |
749b1e90 MS |
436 | * If we were unable to load the class drivers for this printer it's |
437 | * probably because they're ppc or i386. In this case try to run this | |
438 | * backend as i386 or ppc executables so we can use them... | |
e53920b9 | 439 | */ |
09a101d6 | 440 | if (status == -2) |
441 | { | |
749b1e90 | 442 | run_legacy_backend(argc, argv, print_fd); |
e53920b9 | 443 | /* Never returns here */ |
ef416fc2 | 444 | } |
749b1e90 | 445 | #endif /* __i386__ || __x86_64__ */ |
09a101d6 | 446 | |
447 | if (status == -2) | |
448 | { | |
411affcf | 449 | /* |
450 | * If we still were unable to load the class drivers for this printer log | |
451 | * the error and stop the queue... | |
452 | */ | |
453 | ||
09a101d6 | 454 | if (driverBundlePath == NULL || !CFStringGetCString(driverBundlePath, print_buffer, sizeof(print_buffer), kCFStringEncodingUTF8)) |
455 | strlcpy(print_buffer, "USB class driver", sizeof(print_buffer)); | |
411affcf | 456 | |
c0e1af83 | 457 | fputs("STATE: +apple-missing-usbclassdriver-error\n", stderr); |
0837b7e8 MS |
458 | _cupsLangPrintFilter(stderr, "ERROR", |
459 | _("There was an unrecoverable USB error.")); | |
005dd1eb | 460 | fprintf(stderr, "DEBUG: Could not load %s\n", print_buffer); |
411affcf | 461 | |
462 | if (driverBundlePath) | |
463 | CFRelease(driverBundlePath); | |
464 | ||
005dd1eb | 465 | return (CUPS_BACKEND_STOP); |
411affcf | 466 | } |
467 | ||
3b3a130a MS |
468 | #ifdef __x86_64__ |
469 | if (status == noErr && driverBundlePath != NULL && CFStringCompare(driverBundlePath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo) | |
470 | log_usb_class_driver(IS_64BIT); | |
471 | #endif /* __x86_64__ */ | |
472 | ||
411affcf | 473 | if (driverBundlePath) |
474 | CFRelease(driverBundlePath); | |
e53920b9 | 475 | |
09a101d6 | 476 | if (status != noErr) |
477 | { | |
478 | sleep(PRINTER_POLLING_INTERVAL); | |
e53920b9 | 479 | countdown -= PRINTER_POLLING_INTERVAL; |
09a101d6 | 480 | if (countdown <= 0) |
481 | { | |
0837b7e8 MS |
482 | _cupsLangPrintFilter(stderr, "INFO", |
483 | _("Waiting for printer to become available.")); | |
005dd1eb | 484 | fprintf(stderr, "DEBUG: USB printer status: 0x%08x\n", (int)status); |
e53920b9 | 485 | countdown = SUBSEQUENT_LOG_INTERVAL; /* subsequent log entries, every 15 seconds */ |
486 | } | |
487 | } | |
488 | } while (status != noErr); | |
ef416fc2 | 489 | |
e53920b9 | 490 | fputs("STATE: -connecting-to-device\n", stderr); |
ef416fc2 | 491 | |
e53920b9 | 492 | /* |
18ecb428 | 493 | * Now that we are "connected" to the port, ignore SIGTERM so that we |
e53920b9 | 494 | * can finish out any page data the driver sends (e.g. to eject the |
18ecb428 | 495 | * current page... Only ignore SIGTERM if we are printing data from |
e53920b9 | 496 | * stdin (otherwise you can't cancel raw jobs...) |
497 | */ | |
ef416fc2 | 498 | |
09a101d6 | 499 | if (!print_fd) |
500 | { | |
e53920b9 | 501 | memset(&action, 0, sizeof(action)); |
ef416fc2 | 502 | |
e53920b9 | 503 | sigemptyset(&action.sa_mask); |
18ecb428 | 504 | action.sa_handler = SIG_IGN; |
e53920b9 | 505 | sigaction(SIGTERM, &action, NULL); |
e53920b9 | 506 | } |
ef416fc2 | 507 | |
09a101d6 | 508 | /* |
ac884b6a | 509 | * Start the side channel thread if the descriptor is valid... |
09a101d6 | 510 | */ |
ef416fc2 | 511 | |
09a101d6 | 512 | pthread_mutex_init(&g.readwrite_lock_mutex, NULL); |
513 | pthread_cond_init(&g.readwrite_lock_cond, NULL); | |
514 | g.readwrite_lock = 1; | |
f7deaa1a | 515 | |
ac884b6a | 516 | if (have_sidechannel) |
09a101d6 | 517 | { |
7cf5915e MS |
518 | g.sidechannel_thread_stop = 0; |
519 | g.sidechannel_thread_done = 0; | |
520 | ||
521 | pthread_cond_init(&g.sidechannel_thread_cond, NULL); | |
522 | pthread_mutex_init(&g.sidechannel_thread_mutex, NULL); | |
523 | ||
09a101d6 | 524 | if (pthread_create(&sidechannel_thread_id, NULL, sidechannel_thread, NULL)) |
525 | { | |
c7017ecc | 526 | fprintf(stderr, "DEBUG: Fatal USB error.\n"); |
0837b7e8 MS |
527 | _cupsLangPrintFilter(stderr, "ERROR", |
528 | _("There was an unrecoverable USB error.")); | |
4d301e69 | 529 | fputs("DEBUG: Couldn't create side-channel thread\n", stderr); |
ef55b745 | 530 | registry_close(); |
005dd1eb | 531 | return (CUPS_BACKEND_STOP); |
09a101d6 | 532 | } |
533 | } | |
f7deaa1a | 534 | |
09a101d6 | 535 | /* |
536 | * Get the read thread going... | |
537 | */ | |
f7deaa1a | 538 | |
09a101d6 | 539 | g.read_thread_stop = 0; |
540 | g.read_thread_done = 0; | |
f7deaa1a | 541 | |
09a101d6 | 542 | pthread_cond_init(&g.read_thread_cond, NULL); |
543 | pthread_mutex_init(&g.read_thread_mutex, NULL); | |
f7deaa1a | 544 | |
09a101d6 | 545 | if (pthread_create(&read_thread_id, NULL, read_thread, NULL)) |
546 | { | |
c7017ecc | 547 | fprintf(stderr, "DEBUG: Fatal USB error.\n"); |
0837b7e8 MS |
548 | _cupsLangPrintFilter(stderr, "ERROR", |
549 | _("There was an unrecoverable USB error.")); | |
4d301e69 | 550 | fputs("DEBUG: Couldn't create read thread\n", stderr); |
ef55b745 | 551 | registry_close(); |
005dd1eb | 552 | return (CUPS_BACKEND_STOP); |
e53920b9 | 553 | } |
ef416fc2 | 554 | |
09a101d6 | 555 | /* |
556 | * The main thread sends the print file... | |
557 | */ | |
f7deaa1a | 558 | |
09a101d6 | 559 | g.drain_output = 0; |
560 | g.print_bytes = 0; | |
561 | total_bytes = 0; | |
562 | print_ptr = print_buffer; | |
ef416fc2 | 563 | |
09a101d6 | 564 | while (status == noErr && copies-- > 0) |
565 | { | |
0837b7e8 | 566 | _cupsLangPrintFilter(stderr, "INFO", _("Sending data to printer.")); |
757d2cad | 567 | |
09a101d6 | 568 | if (print_fd != STDIN_FILENO) |
569 | { | |
749b1e90 | 570 | fputs("PAGE: 1 1\n", stderr); |
09a101d6 | 571 | lseek(print_fd, 0, SEEK_SET); |
ef416fc2 | 572 | } |
573 | ||
09a101d6 | 574 | while (status == noErr) |
575 | { | |
576 | FD_ZERO(&input_set); | |
e53920b9 | 577 | |
09a101d6 | 578 | if (!g.print_bytes) |
579 | FD_SET(print_fd, &input_set); | |
f7deaa1a | 580 | |
09a101d6 | 581 | /* |
582 | * Calculate select timeout... | |
583 | * If we have data waiting to send timeout is 100ms. | |
584 | * else if we're draining print_fd timeout is 0. | |
585 | * else we're waiting forever... | |
586 | */ | |
587 | ||
588 | if (g.print_bytes) | |
589 | { | |
84315f46 MS |
590 | tv.tv_sec = 0; |
591 | tv.tv_usec = 100000; /* 100ms */ | |
592 | timeout = &tv; | |
09a101d6 | 593 | } |
594 | else if (g.drain_output) | |
595 | { | |
84315f46 MS |
596 | tv.tv_sec = 0; |
597 | tv.tv_usec = 0; | |
598 | timeout = &tv; | |
09a101d6 | 599 | } |
600 | else | |
601 | timeout = NULL; | |
602 | ||
603 | /* | |
604 | * I/O is unlocked around select... | |
605 | */ | |
606 | ||
607 | pthread_mutex_lock(&g.readwrite_lock_mutex); | |
608 | g.readwrite_lock = 0; | |
609 | pthread_cond_signal(&g.readwrite_lock_cond); | |
610 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
611 | ||
612 | nfds = select(print_fd + 1, &input_set, NULL, NULL, timeout); | |
613 | ||
614 | /* | |
615 | * Reacquire the lock... | |
616 | */ | |
617 | ||
618 | pthread_mutex_lock(&g.readwrite_lock_mutex); | |
619 | while (g.readwrite_lock) | |
620 | pthread_cond_wait(&g.readwrite_lock_cond, &g.readwrite_lock_mutex); | |
621 | g.readwrite_lock = 1; | |
622 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
623 | ||
624 | if (nfds < 0) | |
625 | { | |
626 | if (errno == EINTR && total_bytes == 0) | |
627 | { | |
628 | fputs("DEBUG: Received an interrupt before any bytes were " | |
4d301e69 | 629 | "written, aborting\n", stderr); |
ef55b745 | 630 | registry_close(); |
f11a948a | 631 | return (CUPS_BACKEND_OK); |
09a101d6 | 632 | } |
536bc2c6 | 633 | else if (errno != EAGAIN && errno != EINTR) |
09a101d6 | 634 | { |
0837b7e8 MS |
635 | _cupsLangPrintFilter(stderr, "ERROR", |
636 | _("Unable to read print data.")); | |
005dd1eb | 637 | perror("DEBUG: select"); |
ef55b745 MS |
638 | registry_close(); |
639 | return (CUPS_BACKEND_FAILED); | |
09a101d6 | 640 | } |
641 | } | |
642 | ||
643 | /* | |
644 | * If drain output has finished send a response... | |
645 | */ | |
646 | ||
647 | if (g.drain_output && !nfds && !g.print_bytes) | |
648 | { | |
649 | /* Send a response... */ | |
650 | cupsSideChannelWrite(CUPS_SC_CMD_DRAIN_OUTPUT, CUPS_SC_STATUS_OK, NULL, 0, 1.0); | |
651 | g.drain_output = 0; | |
652 | } | |
653 | ||
654 | /* | |
655 | * Check if we have print data ready... | |
656 | */ | |
657 | ||
658 | if (FD_ISSET(print_fd, &input_set)) | |
659 | { | |
acb056cb MS |
660 | #if DEBUG_WRITES |
661 | g.debug_bytes += 512; | |
662 | if (g.debug_bytes > sizeof(print_buffer)) | |
663 | g.debug_bytes = 512; | |
664 | ||
665 | g.print_bytes = read(print_fd, print_buffer, g.debug_bytes); | |
666 | ||
667 | #else | |
09a101d6 | 668 | g.print_bytes = read(print_fd, print_buffer, sizeof(print_buffer)); |
acb056cb | 669 | #endif /* DEBUG_WRITES */ |
09a101d6 | 670 | |
671 | if (g.print_bytes < 0) | |
672 | { | |
673 | /* | |
674 | * Read error - bail if we don't see EAGAIN or EINTR... | |
675 | */ | |
676 | ||
536bc2c6 | 677 | if (errno != EAGAIN && errno != EINTR) |
09a101d6 | 678 | { |
0837b7e8 MS |
679 | _cupsLangPrintFilter(stderr, "ERROR", |
680 | _("Unable to read print data.")); | |
005dd1eb | 681 | perror("DEBUG: read"); |
ef55b745 | 682 | registry_close(); |
f11a948a | 683 | return (CUPS_BACKEND_FAILED); |
09a101d6 | 684 | } |
685 | ||
686 | g.print_bytes = 0; | |
f7deaa1a | 687 | } |
09a101d6 | 688 | else if (g.print_bytes == 0) |
689 | { | |
690 | /* | |
691 | * End of file, break out of the loop... | |
692 | */ | |
f7deaa1a | 693 | |
e53920b9 | 694 | break; |
ef416fc2 | 695 | } |
696 | ||
09a101d6 | 697 | print_ptr = print_buffer; |
698 | ||
699 | fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", | |
700 | (int)g.print_bytes); | |
e53920b9 | 701 | } |
ef416fc2 | 702 | |
09a101d6 | 703 | if (g.print_bytes) |
704 | { | |
7e86f2f6 | 705 | bytes = (UInt32)g.print_bytes; |
178cb736 | 706 | iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0); |
f7deaa1a | 707 | |
09a101d6 | 708 | /* |
58dc1933 | 709 | * Ignore timeout errors, but retain the number of bytes written to |
5a9febac | 710 | * avoid sending duplicate data... |
09a101d6 | 711 | */ |
e53920b9 | 712 | |
178cb736 | 713 | if (iostatus == kIOUSBTransactionTimeout) |
b0f6947b | 714 | { |
4d301e69 | 715 | fputs("DEBUG: Got USB transaction timeout during write\n", stderr); |
178cb736 | 716 | iostatus = 0; |
b0f6947b | 717 | } |
ef416fc2 | 718 | |
178cb736 | 719 | /* |
acb056cb | 720 | * If we've stalled, retry the write... |
178cb736 MS |
721 | */ |
722 | ||
b0f6947b MS |
723 | else if (iostatus == kIOUSBPipeStalled) |
724 | { | |
4d301e69 | 725 | fputs("DEBUG: Got USB pipe stalled during write\n", stderr); |
b0f6947b | 726 | |
7e86f2f6 | 727 | bytes = (UInt32)g.print_bytes; |
acb056cb | 728 | iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0); |
b0f6947b | 729 | } |
178cb736 MS |
730 | |
731 | /* | |
acb056cb | 732 | * Retry a write after an aborted write since we probably just got |
5a9febac | 733 | * SIGTERM... |
178cb736 MS |
734 | */ |
735 | ||
b0f6947b | 736 | else if (iostatus == kIOReturnAborted) |
178cb736 | 737 | { |
3fb9c47e | 738 | fputs("DEBUG: Got USB return aborted during write\n", stderr); |
b0f6947b MS |
739 | |
740 | IOReturn err = (*g.classdriver)->Abort(g.classdriver); | |
741 | fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n", err); | |
742 | ||
acb056cb MS |
743 | #if DEBUG_WRITES |
744 | sleep(5); | |
745 | #endif /* DEBUG_WRITES */ | |
b0f6947b | 746 | |
7e86f2f6 | 747 | bytes = (UInt32)g.print_bytes; |
acb056cb | 748 | iostatus = (*g.classdriver)->WritePipe(g.classdriver, (UInt8*)print_ptr, &bytes, 0); |
b0f6947b | 749 | } |
178cb736 | 750 | |
82f97232 | 751 | if (iostatus) |
09a101d6 | 752 | { |
753 | /* | |
754 | * Write error - bail if we don't see an error we can retry... | |
755 | */ | |
84315f46 | 756 | |
0837b7e8 MS |
757 | _cupsLangPrintFilter(stderr, "ERROR", |
758 | _("Unable to send data to printer.")); | |
178cb736 MS |
759 | fprintf(stderr, "DEBUG: USB class driver WritePipe returned %x\n", |
760 | iostatus); | |
b0f6947b MS |
761 | |
762 | IOReturn err = (*g.classdriver)->Abort(g.classdriver); | |
178cb736 MS |
763 | fprintf(stderr, "DEBUG: USB class driver Abort returned %x\n", |
764 | err); | |
b0f6947b | 765 | |
18ecb428 | 766 | status = CUPS_BACKEND_FAILED; |
bc44d920 | 767 | break; |
09a101d6 | 768 | } |
b0f6947b MS |
769 | else if (bytes > 0) |
770 | { | |
771 | fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); | |
09a101d6 | 772 | |
b0f6947b MS |
773 | g.print_bytes -= bytes; |
774 | print_ptr += bytes; | |
775 | total_bytes += bytes; | |
776 | } | |
f7deaa1a | 777 | } |
09a101d6 | 778 | |
779 | if (print_fd != 0 && status == noErr) | |
780 | fprintf(stderr, "DEBUG: Sending print file, %lld bytes...\n", | |
781 | (off_t)total_bytes); | |
f7deaa1a | 782 | } |
f7deaa1a | 783 | } |
784 | ||
09a101d6 | 785 | fprintf(stderr, "DEBUG: Sent %lld bytes...\n", (off_t)total_bytes); |
1e3e80bb | 786 | fputs("STATE: +cups-waiting-for-job-completed\n", stderr); |
f7deaa1a | 787 | |
7cf5915e | 788 | /* |
a4845881 | 789 | * Signal the side channel thread to exit... |
7cf5915e MS |
790 | */ |
791 | ||
792 | if (have_sidechannel) | |
793 | { | |
794 | close(CUPS_SC_FD); | |
795 | pthread_mutex_lock(&g.readwrite_lock_mutex); | |
796 | g.readwrite_lock = 0; | |
797 | pthread_cond_signal(&g.readwrite_lock_cond); | |
798 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
799 | ||
800 | g.sidechannel_thread_stop = 1; | |
801 | pthread_mutex_lock(&g.sidechannel_thread_mutex); | |
84315f46 | 802 | |
7cf5915e MS |
803 | if (!g.sidechannel_thread_done) |
804 | { | |
84315f46 MS |
805 | gettimeofday(&tv, NULL); |
806 | cond_timeout.tv_sec = tv.tv_sec + WAIT_SIDE_DELAY; | |
807 | cond_timeout.tv_nsec = tv.tv_usec * 1000; | |
7cf5915e | 808 | |
84315f46 MS |
809 | while (!g.sidechannel_thread_done) |
810 | { | |
811 | if (pthread_cond_timedwait(&g.sidechannel_thread_cond, | |
812 | &g.sidechannel_thread_mutex, | |
813 | &cond_timeout) != 0) | |
814 | break; | |
815 | } | |
7cf5915e | 816 | } |
84315f46 | 817 | |
7cf5915e | 818 | pthread_mutex_unlock(&g.sidechannel_thread_mutex); |
7cf5915e MS |
819 | } |
820 | ||
7cf5915e | 821 | /* |
a4845881 | 822 | * Signal the read thread to exit then wait 7 seconds for it to complete... |
7cf5915e MS |
823 | */ |
824 | ||
825 | g.read_thread_stop = 1; | |
826 | ||
7cf5915e MS |
827 | pthread_mutex_lock(&g.read_thread_mutex); |
828 | ||
829 | if (!g.read_thread_done) | |
830 | { | |
a4845881 MS |
831 | fputs("DEBUG: Waiting for read thread to exit...\n", stderr); |
832 | ||
84315f46 MS |
833 | gettimeofday(&tv, NULL); |
834 | cond_timeout.tv_sec = tv.tv_sec + WAIT_EOF_DELAY; | |
835 | cond_timeout.tv_nsec = tv.tv_usec * 1000; | |
7cf5915e | 836 | |
84315f46 MS |
837 | while (!g.read_thread_done) |
838 | { | |
839 | if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex, | |
840 | &cond_timeout) != 0) | |
841 | break; | |
842 | } | |
843 | ||
a4845881 MS |
844 | /* |
845 | * If it didn't exit abort the pending read and wait an additional second... | |
846 | */ | |
f3c17241 | 847 | |
84315f46 | 848 | if (!g.read_thread_done) |
7cf5915e | 849 | { |
f3c17241 | 850 | fputs("DEBUG: Read thread still active, aborting the pending read...\n", |
a4845881 | 851 | stderr); |
7cf5915e MS |
852 | |
853 | g.wait_eof = 0; | |
a4845881 MS |
854 | |
855 | (*g.classdriver)->Abort(g.classdriver); | |
856 | ||
857 | gettimeofday(&tv, NULL); | |
858 | cond_timeout.tv_sec = tv.tv_sec + 1; | |
859 | cond_timeout.tv_nsec = tv.tv_usec * 1000; | |
f3c17241 | 860 | |
a4845881 MS |
861 | while (!g.read_thread_done) |
862 | { | |
863 | if (pthread_cond_timedwait(&g.read_thread_cond, &g.read_thread_mutex, | |
864 | &cond_timeout) != 0) | |
865 | break; | |
866 | } | |
7cf5915e MS |
867 | } |
868 | } | |
84315f46 | 869 | |
7cf5915e MS |
870 | pthread_mutex_unlock(&g.read_thread_mutex); |
871 | ||
09a101d6 | 872 | /* |
873 | * Close the connection and input file and general clean up... | |
874 | */ | |
f7deaa1a | 875 | |
09a101d6 | 876 | registry_close(); |
f7deaa1a | 877 | |
09a101d6 | 878 | if (print_fd != STDIN_FILENO) |
879 | close(print_fd); | |
f7deaa1a | 880 | |
09a101d6 | 881 | if (g.make != NULL) |
882 | CFRelease(g.make); | |
e53920b9 | 883 | |
09a101d6 | 884 | if (g.model != NULL) |
885 | CFRelease(g.model); | |
e53920b9 | 886 | |
09a101d6 | 887 | if (g.serial != NULL) |
888 | CFRelease(g.serial); | |
e53920b9 | 889 | |
09a101d6 | 890 | if (g.printer_obj != 0x0) |
891 | IOObjectRelease(g.printer_obj); | |
e53920b9 | 892 | |
893 | return status; | |
ef416fc2 | 894 | } |
895 | ||
09a101d6 | 896 | |
897 | /* | |
898 | * 'read_thread()' - Thread to read the backchannel data on. | |
899 | */ | |
900 | ||
901 | static void *read_thread(void *reference) | |
902 | { | |
903 | UInt8 readbuffer[512]; | |
904 | UInt32 rbytes; | |
905 | kern_return_t readstatus; | |
906 | struct mach_timebase_info timeBaseInfo; | |
907 | uint64_t start, | |
908 | delay; | |
909 | ||
321d8d57 MS |
910 | |
911 | (void)reference; | |
912 | ||
09a101d6 | 913 | /* Calculate what 250 milliSeconds are in mach absolute time... |
914 | */ | |
915 | mach_timebase_info(&timeBaseInfo); | |
916 | delay = ((uint64_t)250000000 * (uint64_t)timeBaseInfo.denom) / (uint64_t)timeBaseInfo.numer; | |
917 | ||
918 | do | |
919 | { | |
920 | /* | |
921 | * Remember when we started so we can throttle the loop after the read call... | |
922 | */ | |
923 | ||
924 | start = mach_absolute_time(); | |
925 | ||
926 | rbytes = sizeof(readbuffer); | |
927 | readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes); | |
928 | if (readstatus == kIOReturnSuccess && rbytes > 0) | |
929 | { | |
b0f6947b MS |
930 | fprintf(stderr, "DEBUG: Read %d bytes of back-channel data...\n", |
931 | (int)rbytes); | |
09a101d6 | 932 | cupsBackChannelWrite((char*)readbuffer, rbytes, 1.0); |
933 | ||
934 | /* cntrl-d is echoed by the printer. | |
935 | * NOTES: | |
936 | * Xerox Phaser 6250D doesn't echo the cntrl-d. | |
937 | * Xerox Phaser 6250D doesn't always send the product query. | |
938 | */ | |
939 | if (g.wait_eof && readbuffer[rbytes-1] == 0x4) | |
940 | break; | |
941 | ||
942 | #ifdef PARSE_PS_ERRORS | |
943 | parse_pserror(readbuffer, rbytes); | |
944 | #endif | |
945 | } | |
b0f6947b | 946 | else if (readstatus == kIOUSBTransactionTimeout) |
3fb9c47e | 947 | fputs("DEBUG: Got USB transaction timeout during read\n", stderr); |
b0f6947b | 948 | else if (readstatus == kIOUSBPipeStalled) |
4d301e69 | 949 | fputs("DEBUG: Got USB pipe stalled during read\n", stderr); |
b0f6947b | 950 | else if (readstatus == kIOReturnAborted) |
3fb9c47e | 951 | fputs("DEBUG: Got USB return aborted during read\n", stderr); |
09a101d6 | 952 | |
953 | /* | |
954 | * Make sure this loop executes no more than once every 250 miliseconds... | |
955 | */ | |
956 | ||
957 | if ((readstatus != kIOReturnSuccess || rbytes == 0) && (g.wait_eof || !g.read_thread_stop)) | |
958 | mach_wait_until(start + delay); | |
959 | ||
960 | } while (g.wait_eof || !g.read_thread_stop); /* Abort from main thread tests error here */ | |
961 | ||
7530b13a MS |
962 | /* Workaround for usb race condition. <rdar://problem/21882551> */ |
963 | if (!g.wait_eof && g.use_generic_class_driver) | |
964 | { | |
965 | const char *pdl = getenv("FINAL_CONTENT_TYPE"); | |
966 | if (pdl && strcmp(pdl, "application/vnd.cups-postscript") == 0) | |
967 | { | |
968 | while (readstatus == kIOReturnSuccess && ((rbytes > 0 && readbuffer[rbytes-1] != 0x4) || rbytes == 0)) | |
969 | { | |
970 | start = mach_absolute_time(); | |
971 | ||
972 | rbytes = sizeof(readbuffer); | |
973 | readstatus = (*g.classdriver)->ReadPipe(g.classdriver, readbuffer, &rbytes); | |
974 | if (readstatus == kIOReturnSuccess && rbytes > 0 && readbuffer[rbytes-1] == 0x4) | |
975 | break; | |
976 | ||
977 | /* Make sure this loop executes no more than once every 250 miliseconds... */ | |
978 | mach_wait_until(start + delay); | |
979 | } | |
980 | } | |
981 | } | |
982 | ||
09a101d6 | 983 | /* |
984 | * Let the main thread know that we have completed the read thread... | |
985 | */ | |
986 | ||
987 | pthread_mutex_lock(&g.read_thread_mutex); | |
988 | g.read_thread_done = 1; | |
989 | pthread_cond_signal(&g.read_thread_cond); | |
990 | pthread_mutex_unlock(&g.read_thread_mutex); | |
991 | ||
992 | return NULL; | |
993 | } | |
994 | ||
995 | ||
996 | /* | |
997 | * 'sidechannel_thread()' - Handle side-channel requests. | |
998 | */ | |
999 | ||
1000 | static void* | |
1001 | sidechannel_thread(void *reference) | |
1002 | { | |
1003 | cups_sc_command_t command; /* Request command */ | |
1004 | cups_sc_status_t status; /* Request/response status */ | |
1005 | char data[2048]; /* Request/response data */ | |
1006 | int datalen; /* Request/response data size */ | |
1007 | ||
bc44d920 | 1008 | |
321d8d57 MS |
1009 | (void)reference; |
1010 | ||
bc44d920 | 1011 | do |
09a101d6 | 1012 | { |
1013 | datalen = sizeof(data); | |
1014 | ||
1015 | if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0)) | |
ef55b745 MS |
1016 | { |
1017 | if (status == CUPS_SC_STATUS_TIMEOUT) | |
1018 | continue; | |
1019 | else | |
1020 | break; | |
1021 | } | |
09a101d6 | 1022 | |
1023 | switch (command) | |
1024 | { | |
1025 | case CUPS_SC_CMD_SOFT_RESET: /* Do a soft reset */ | |
b0f6947b MS |
1026 | fputs("DEBUG: CUPS_SC_CMD_SOFT_RESET received from driver...\n", |
1027 | stderr); | |
1028 | ||
db1f069b MS |
1029 | if ((*g.classdriver)->SoftReset != NULL) |
1030 | { | |
1031 | soft_reset(); | |
1032 | cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, NULL, 0, 1.0); | |
b0f6947b MS |
1033 | fputs("DEBUG: Returning status CUPS_STATUS_OK with no bytes...\n", |
1034 | stderr); | |
db1f069b MS |
1035 | } |
1036 | else | |
1037 | { | |
1038 | cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED, | |
1039 | NULL, 0, 1.0); | |
b0f6947b MS |
1040 | fputs("DEBUG: Returning status CUPS_STATUS_NOT_IMPLEMENTED with " |
1041 | "no bytes...\n", stderr); | |
db1f069b | 1042 | } |
09a101d6 | 1043 | break; |
1044 | ||
1045 | case CUPS_SC_CMD_DRAIN_OUTPUT: /* Drain all pending output */ | |
b0f6947b MS |
1046 | fputs("DEBUG: CUPS_SC_CMD_DRAIN_OUTPUT received from driver...\n", |
1047 | stderr); | |
1048 | ||
09a101d6 | 1049 | g.drain_output = 1; |
1050 | break; | |
1051 | ||
1052 | case CUPS_SC_CMD_GET_BIDI: /* Is the connection bidirectional? */ | |
b0f6947b MS |
1053 | fputs("DEBUG: CUPS_SC_CMD_GET_BIDI received from driver...\n", |
1054 | stderr); | |
1055 | ||
7e86f2f6 | 1056 | data[0] = (char)g.bidi_flag; |
09a101d6 | 1057 | cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0); |
b0f6947b MS |
1058 | |
1059 | fprintf(stderr, | |
1060 | "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n", | |
1061 | data[0]); | |
09a101d6 | 1062 | break; |
1063 | ||
1064 | case CUPS_SC_CMD_GET_DEVICE_ID: /* Return IEEE-1284 device ID */ | |
b0f6947b MS |
1065 | fputs("DEBUG: CUPS_SC_CMD_GET_DEVICE_ID received from driver...\n", |
1066 | stderr); | |
1067 | ||
09a101d6 | 1068 | datalen = sizeof(data); |
1069 | get_device_id(&status, data, &datalen); | |
1070 | cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, datalen, 1.0); | |
b0f6947b | 1071 | |
7e86f2f6 | 1072 | if ((size_t)datalen < sizeof(data)) |
b0f6947b MS |
1073 | data[datalen] = '\0'; |
1074 | else | |
1075 | data[sizeof(data) - 1] = '\0'; | |
1076 | ||
1077 | fprintf(stderr, | |
1078 | "DEBUG: Returning CUPS_SC_STATUS_OK with %d bytes (%s)...\n", | |
1079 | datalen, data); | |
09a101d6 | 1080 | break; |
1081 | ||
1082 | case CUPS_SC_CMD_GET_STATE: /* Return device state */ | |
b0f6947b MS |
1083 | fputs("DEBUG: CUPS_SC_CMD_GET_STATE received from driver...\n", |
1084 | stderr); | |
1085 | ||
09a101d6 | 1086 | data[0] = CUPS_SC_STATE_ONLINE; |
1087 | cupsSideChannelWrite(command, CUPS_SC_STATUS_OK, data, 1, 1.0); | |
b0f6947b MS |
1088 | |
1089 | fprintf(stderr, | |
1090 | "DEBUG: Returned CUPS_SC_STATUS_OK with 1 byte (%02X)...\n", | |
1091 | data[0]); | |
09a101d6 | 1092 | break; |
1093 | ||
1094 | default: | |
b0f6947b MS |
1095 | fprintf(stderr, "DEBUG: Unknown side-channel command (%d) received " |
1096 | "from driver...\n", command); | |
1097 | ||
09a101d6 | 1098 | cupsSideChannelWrite(command, CUPS_SC_STATUS_NOT_IMPLEMENTED, |
1099 | NULL, 0, 1.0); | |
b0f6947b MS |
1100 | |
1101 | fputs("DEBUG: Returned CUPS_SC_STATUS_NOT_IMPLEMENTED with no bytes...\n", | |
1102 | stderr); | |
09a101d6 | 1103 | break; |
1104 | } | |
1105 | } | |
7cf5915e MS |
1106 | while (!g.sidechannel_thread_stop); |
1107 | ||
1108 | pthread_mutex_lock(&g.sidechannel_thread_mutex); | |
1109 | g.sidechannel_thread_done = 1; | |
1110 | pthread_cond_signal(&g.sidechannel_thread_cond); | |
1111 | pthread_mutex_unlock(&g.sidechannel_thread_mutex); | |
bc44d920 | 1112 | |
09a101d6 | 1113 | return NULL; |
1114 | } | |
1115 | ||
1116 | ||
e53920b9 | 1117 | #pragma mark - |
ef416fc2 | 1118 | /* |
09a101d6 | 1119 | * 'iterate_printers()' - Iterate over all the printers. |
1120 | */ | |
1121 | ||
fe750b86 | 1122 | static void iterate_printers(iterator_callback_t callBack, void *userdata) |
09a101d6 | 1123 | { |
fe750b86 | 1124 | Iterating = 1; |
de3edeba | 1125 | |
fe750b86 MS |
1126 | mach_port_t masterPort = 0x0; |
1127 | kern_return_t kr = IOMasterPort (bootstrap_port, &masterPort); | |
de3edeba | 1128 | |
fe750b86 | 1129 | if (kr == kIOReturnSuccess && masterPort != 0x0) |
09a101d6 | 1130 | { |
fe750b86 | 1131 | iterator_reference_t reference = { callBack, userdata, true }; |
de3edeba | 1132 | |
fe750b86 MS |
1133 | IONotificationPortRef addNotification = IONotificationPortCreate(masterPort); |
1134 | io_iterator_t addIterator = IO_OBJECT_NULL; | |
de3edeba | 1135 | |
fe750b86 MS |
1136 | kr = IOServiceAddMatchingNotification(addNotification, kIOFirstMatchNotification, IOServiceMatching(kIOUSBDeviceClassName), |
1137 | &device_added, &reference, &addIterator); | |
1138 | if (kr == kIOReturnSuccess && addIterator != IO_OBJECT_NULL) | |
1139 | { | |
1140 | device_added(&reference, addIterator); | |
1141 | if (reference.keepRunning) | |
1142 | { | |
1143 | CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(addNotification), kCFRunLoopDefaultMode); | |
1144 | CFRunLoopRun(); | |
1145 | } | |
1146 | IOObjectRelease(addIterator); | |
1147 | } | |
1148 | mach_port_deallocate(mach_task_self(), masterPort); | |
09a101d6 | 1149 | } |
de3edeba | 1150 | |
fe750b86 | 1151 | Iterating = 0; |
09a101d6 | 1152 | } |
1153 | ||
1154 | ||
1155 | /* | |
1156 | * 'device_added()' - Device added notifier. | |
e53920b9 | 1157 | */ |
fe750b86 | 1158 | #define IsPrintingInterface(c,s,p) ((c) == kUSBPrintingInterfaceClass && (s) == kUSBPrintingSubclass && (p) != kUSBPrintingProtocolIPP) |
e53920b9 | 1159 | |
fe750b86 | 1160 | static void device_added(void *userdata, io_iterator_t iterator) |
09a101d6 | 1161 | { |
fe750b86 MS |
1162 | iterator_reference_t *reference = userdata; |
1163 | io_service_t device; | |
de3edeba | 1164 | |
fe750b86 MS |
1165 | while (reference->keepRunning && (device = IOIteratorNext(iterator)) != 0x0) |
1166 | { | |
1167 | UInt32 locationID = 0; | |
1168 | IOUSBDeviceInterface **devIntf = NULL; | |
1169 | io_iterator_t intfIterator = IO_OBJECT_NULL; | |
1170 | io_object_t intf = IO_OBJECT_NULL; | |
1171 | ||
1172 | devIntf = usb_device_interface_for_device(device); | |
1173 | if (devIntf == NULL) | |
1174 | goto device_added_done; | |
1175 | ||
1176 | UInt16 vendorUniqueID; | |
1177 | if ((*devIntf)->GetDeviceVendor(devIntf, &vendorUniqueID) != kIOReturnSuccess || vendorUniqueID == kAppleVendorID || vendorUniqueID == 0x0A5C) | |
1178 | goto device_added_done; | |
1179 | ||
1180 | if ((*devIntf)->GetLocationID(devIntf, &locationID) != kIOReturnSuccess) | |
1181 | goto device_added_done; | |
1182 | ||
1183 | IOUSBFindInterfaceRequest req = { kIOUSBFindInterfaceDontCare, kIOUSBFindInterfaceDontCare, kIOUSBFindInterfaceDontCare, kIOUSBFindInterfaceDontCare }; | |
1184 | if ((*devIntf)->CreateInterfaceIterator(devIntf, &req, &intfIterator) != kIOReturnSuccess) | |
1185 | goto device_added_done; | |
1186 | ||
1187 | while (reference->keepRunning && (intf = IOIteratorNext(intfIterator))) | |
1188 | { | |
f1ebe30e MS |
1189 | printer_interface_t printerIntf = usb_printer_interface_interface(intf); |
1190 | if (printerIntf != NULL) | |
fe750b86 MS |
1191 | { |
1192 | UInt8 intfClass = 0, intfSubclass = 0, intfProtocol = 0, intfNumber = 0; | |
1193 | ||
f1ebe30e MS |
1194 | (*printerIntf)->GetInterfaceClass(printerIntf, &intfClass); |
1195 | (*printerIntf)->GetInterfaceSubClass(printerIntf, &intfSubclass); | |
1196 | (*printerIntf)->GetInterfaceProtocol(printerIntf, &intfProtocol); | |
1197 | (*printerIntf)->GetInterfaceNumber(printerIntf, &intfNumber); | |
fe750b86 MS |
1198 | |
1199 | if (IsPrintingInterface(intfClass, intfSubclass, intfProtocol)) | |
1200 | { | |
f1ebe30e | 1201 | CFStringRef deviceIDString = copy_printer_interface_deviceid(printerIntf, 0); |
fe750b86 MS |
1202 | if (deviceIDString != NULL) |
1203 | { | |
1204 | reference->keepRunning = reference->callback(userdata, intf, deviceIDString, locationID, intfNumber, 0); | |
1205 | CFRelease(deviceIDString); | |
1206 | } | |
1207 | } | |
1208 | ||
de3edeba | 1209 | IOUSBInterfaceDescriptor *intfDesc = NULL; |
f1ebe30e | 1210 | while (reference->keepRunning && (intfDesc = (IOUSBInterfaceDescriptor *)(*printerIntf)->FindNextAssociatedDescriptor(printerIntf, intfDesc, kUSBInterfaceDesc))) |
fe750b86 MS |
1211 | { |
1212 | intfClass = intfDesc->bInterfaceClass; | |
1213 | intfSubclass = intfDesc->bInterfaceSubClass; | |
1214 | intfProtocol = intfDesc->bInterfaceProtocol; | |
1215 | ||
1216 | if ((IsPrintingInterface(intfClass, intfSubclass, intfProtocol))) | |
1217 | { | |
f1ebe30e | 1218 | CFStringRef deviceIDString = copy_printer_interface_deviceid(printerIntf, intfDesc->bAlternateSetting); |
fe750b86 MS |
1219 | if (deviceIDString != NULL) |
1220 | { | |
1221 | reference->keepRunning = reference->callback(userdata, intf, deviceIDString, locationID, intfNumber, intfDesc->bAlternateSetting); | |
1222 | CFRelease(deviceIDString); | |
1223 | } | |
1224 | } | |
1225 | } | |
f1ebe30e | 1226 | (*printerIntf)->Release(printerIntf); |
fe750b86 | 1227 | } |
de3edeba | 1228 | IOObjectRelease(intf); |
fe750b86 | 1229 | } |
09a101d6 | 1230 | |
fe750b86 MS |
1231 | device_added_done: |
1232 | if (devIntf != NULL) (*devIntf)->Release(devIntf); | |
1233 | if (intfIterator != IO_OBJECT_NULL) IOObjectRelease(intfIterator); | |
09a101d6 | 1234 | |
fe750b86 MS |
1235 | IOObjectRelease(device); |
1236 | } | |
de3edeba | 1237 | |
fe750b86 MS |
1238 | /* One last call to the call back now that we are not longer have printers left to iterate... |
1239 | */ | |
1240 | if (reference->keepRunning && reference->callback) | |
1241 | reference->keepRunning = reference->callback(reference->userdata, IO_OBJECT_NULL, NULL, 0, 0, 0); | |
de3edeba | 1242 | |
fe750b86 MS |
1243 | if (!reference->keepRunning) |
1244 | CFRunLoopStop(CFRunLoopGetCurrent()); | |
09a101d6 | 1245 | } |
1246 | ||
09a101d6 | 1247 | /* |
1248 | * 'list_device_cb()' - list_device iterator callback. | |
1249 | */ | |
1250 | ||
fe750b86 | 1251 | static Boolean list_device_cb(void *refcon, io_service_t obj, CFStringRef deviceIDString, UInt32 deviceLocation, UInt8 interfaceNum, UInt8 alternateSetting) |
ef416fc2 | 1252 | { |
f8f7636e | 1253 | (void)refcon; |
fe750b86 MS |
1254 | (void)interfaceNum; |
1255 | (void)alternateSetting; | |
321d8d57 | 1256 | |
fe750b86 | 1257 | if (obj != IO_OBJECT_NULL) |
09a101d6 | 1258 | { |
de3edeba MS |
1259 | CFStringRef make = deviceIDCopyManufacturer(deviceIDString); |
1260 | CFStringRef model = deviceIDCopyModel(deviceIDString); | |
1261 | CFStringRef serial = deviceIDCopySerialNumber(deviceIDString); | |
1262 | ||
fe750b86 MS |
1263 | char uristr[1024], makestr[1024], modelstr[1024], serialstr[1024]; |
1264 | char optionsstr[1024], idstr[1024], make_modelstr[1024]; | |
de3edeba | 1265 | |
fe750b86 MS |
1266 | CFStringGetCString(deviceIDString, idstr, sizeof(idstr), kCFStringEncodingUTF8); |
1267 | backendGetMakeModel(idstr, make_modelstr, sizeof(make_modelstr)); | |
de3edeba | 1268 | |
fe750b86 | 1269 | modelstr[0] = '/'; |
de3edeba | 1270 | |
fe750b86 MS |
1271 | if (make == NULL || !CFStringGetCString(make, makestr, sizeof(makestr), kCFStringEncodingUTF8)) |
1272 | strlcpy(makestr, "Unknown", sizeof(makestr)); | |
de3edeba | 1273 | |
fe750b86 MS |
1274 | if (model == NULL || !CFStringGetCString(model, &modelstr[1], sizeof(modelstr)-1, kCFStringEncodingUTF8)) |
1275 | strlcpy(modelstr + 1, "Printer", sizeof(modelstr) - 1); | |
de3edeba | 1276 | |
fe750b86 MS |
1277 | optionsstr[0] = '\0'; |
1278 | if (serial != NULL && CFStringGetCString(serial, serialstr, sizeof(serialstr), kCFStringEncodingUTF8)) | |
1279 | snprintf(optionsstr, sizeof(optionsstr), "?serial=%s", serialstr); | |
1280 | else if (deviceLocation != 0) | |
1281 | snprintf(optionsstr, sizeof(optionsstr), "?location=%x", (unsigned)deviceLocation); | |
de3edeba | 1282 | |
fe750b86 MS |
1283 | httpAssembleURI(HTTP_URI_CODING_ALL, uristr, sizeof(uristr), "usb", NULL, makestr, 0, modelstr); |
1284 | strlcat(uristr, optionsstr, sizeof(uristr)); | |
de3edeba | 1285 | |
fe750b86 MS |
1286 | cupsBackendReport("direct", uristr, make_modelstr, make_modelstr, idstr, |
1287 | NULL); | |
7530b13a | 1288 | |
de3edeba MS |
1289 | if (make != NULL) CFRelease(make); |
1290 | if (model != NULL) CFRelease(model); | |
1291 | if (serial != NULL) CFRelease(serial); | |
1292 | } | |
1293 | ||
fe750b86 | 1294 | return obj != IO_OBJECT_NULL; |
ef416fc2 | 1295 | } |
1296 | ||
e53920b9 | 1297 | /* |
09a101d6 | 1298 | * 'find_device_cb()' - print_device iterator callback. |
e53920b9 | 1299 | */ |
fe750b86 | 1300 | static Boolean find_device_cb(void *refcon, io_service_t obj, CFStringRef deviceIDString, UInt32 deviceLocation, UInt8 interfaceNum, UInt8 alternateSetting) |
e53920b9 | 1301 | { |
fe750b86 | 1302 | Boolean keepLooking = true; |
de3edeba | 1303 | |
fe750b86 | 1304 | if (obj != IO_OBJECT_NULL) |
09a101d6 | 1305 | { |
fe750b86 MS |
1306 | CFStringRef make = deviceIDCopyManufacturer(deviceIDString); |
1307 | CFStringRef model = deviceIDCopyModel(deviceIDString); | |
1308 | CFStringRef serial = deviceIDCopySerialNumber(deviceIDString); | |
de3edeba | 1309 | |
fe750b86 MS |
1310 | if (make && CFStringCompare(make, g.make, kCFCompareCaseInsensitive) == kCFCompareEqualTo) |
1311 | { | |
1312 | if (model && CFStringCompare(model, g.model, kCFCompareCaseInsensitive) == kCFCompareEqualTo) | |
1313 | { | |
1314 | if (g.serial != NULL && CFStringGetLength(g.serial) > 0) | |
1315 | { | |
1316 | if (serial != NULL && CFStringCompare(serial, g.serial, kCFCompareCaseInsensitive) == kCFCompareEqualTo) | |
1317 | { | |
1318 | IOObjectRetain(obj); | |
1319 | g.printer_obj = obj; | |
de3edeba | 1320 | g.location = deviceLocation; |
fe750b86 MS |
1321 | g.alternateSetting = alternateSetting; |
1322 | keepLooking = false; | |
1323 | } | |
1324 | } | |
1325 | else | |
1326 | { | |
1327 | if (g.printer_obj != 0) | |
1328 | IOObjectRelease(g.printer_obj); | |
de3edeba | 1329 | |
fe750b86 MS |
1330 | g.alternateSetting = alternateSetting; |
1331 | g.printer_obj = obj; | |
1332 | IOObjectRetain(obj); | |
de3edeba | 1333 | |
fe750b86 MS |
1334 | if (g.location == 0 || g.location == deviceLocation) |
1335 | keepLooking = false; | |
1336 | } | |
de3edeba | 1337 | |
fe750b86 MS |
1338 | if ( !keepLooking ) |
1339 | g.interfaceNum = interfaceNum; | |
1340 | } | |
1341 | } | |
de3edeba | 1342 | |
fe750b86 MS |
1343 | if (make) CFRelease(make); |
1344 | if (model) CFRelease(model); | |
1345 | if (serial) CFRelease(serial); | |
ef416fc2 | 1346 | } |
fe750b86 | 1347 | else |
09a101d6 | 1348 | { |
fe750b86 MS |
1349 | keepLooking = (g.printer_obj == 0); |
1350 | if (obj == IO_OBJECT_NULL && keepLooking) | |
1351 | { | |
1352 | CFRunLoopTimerContext context = { 0, refcon, NULL, NULL, NULL }; | |
1353 | CFRunLoopTimerRef timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() + 1.0, 10, 0x0, 0x0, status_timer_cb, &context); | |
1354 | if (timer != NULL) | |
1355 | { | |
1356 | CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode); | |
1357 | g.status_timer = timer; | |
1358 | } | |
1359 | } | |
7594b224 | 1360 | } |
de3edeba | 1361 | |
fe750b86 MS |
1362 | if (!keepLooking && g.status_timer != NULL) |
1363 | { | |
1364 | fputs("STATE: -offline-report\n", stderr); | |
1365 | _cupsLangPrintFilter(stderr, "INFO", _("The printer is now online.")); | |
1366 | CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), g.status_timer, kCFRunLoopDefaultMode); | |
1367 | CFRelease(g.status_timer); | |
1368 | g.status_timer = NULL; | |
1369 | } | |
de3edeba | 1370 | |
fe750b86 MS |
1371 | return keepLooking; |
1372 | } | |
09a101d6 | 1373 | |
fe750b86 MS |
1374 | static CFStringRef deviceIDCopySerialNumber(CFStringRef deviceID) |
1375 | { | |
1376 | CFStringRef serialKeys[] = { CFSTR("SN:"), CFSTR("SERN:"), NULL }; | |
de3edeba | 1377 | |
fe750b86 MS |
1378 | return copy_value_for_key(deviceID, serialKeys); |
1379 | } | |
ef416fc2 | 1380 | |
fe750b86 MS |
1381 | static CFStringRef deviceIDCopyModel(CFStringRef deviceID) |
1382 | { | |
1383 | CFStringRef modelKeys[] = { CFSTR("MDL:"), CFSTR("MODEL:"), NULL }; | |
1384 | return copy_value_for_key(deviceID, modelKeys); | |
ef416fc2 | 1385 | } |
1386 | ||
fe750b86 MS |
1387 | static CFStringRef deviceIDCopyManufacturer(CFStringRef deviceID) |
1388 | { | |
1389 | CFStringRef makeKeys[] = { CFSTR("MFG:"), CFSTR("MANUFACTURER:"), NULL }; | |
1390 | return copy_value_for_key(deviceID, makeKeys); | |
1391 | } | |
ef416fc2 | 1392 | |
e53920b9 | 1393 | /* |
09a101d6 | 1394 | * 'status_timer_cb()' - Status timer callback. |
ef416fc2 | 1395 | */ |
e53920b9 | 1396 | |
09a101d6 | 1397 | static void status_timer_cb(CFRunLoopTimerRef timer, |
1398 | void *info) | |
ef416fc2 | 1399 | { |
321d8d57 MS |
1400 | (void)timer; |
1401 | (void)info; | |
1402 | ||
c5571a1d | 1403 | fputs("STATE: +offline-report\n", stderr); |
f3c17241 | 1404 | _cupsLangPrintFilter(stderr, "INFO", _("The printer is offline.")); |
0a682745 MS |
1405 | |
1406 | if (getenv("CLASS") != NULL) | |
1407 | { | |
1408 | /* | |
1409 | * If the CLASS environment variable is set, the job was submitted | |
1410 | * to a class and not to a specific queue. In this case, we want | |
1411 | * to abort immediately so that the job can be requeued on the next | |
1412 | * available printer in the class. | |
1413 | * | |
1414 | * Sleep 5 seconds to keep the job from requeuing too rapidly... | |
1415 | */ | |
1416 | ||
1417 | sleep(5); | |
1418 | ||
1419 | exit(CUPS_BACKEND_FAILED); | |
1420 | } | |
ef416fc2 | 1421 | } |
1422 | ||
ef416fc2 | 1423 | |
e53920b9 | 1424 | #pragma mark - |
1425 | /* | |
1426 | * 'load_classdriver()' - Load a classdriver. | |
1427 | */ | |
1428 | ||
09a101d6 | 1429 | static kern_return_t load_classdriver(CFStringRef driverPath, |
321d8d57 | 1430 | printer_interface_t interface, |
09a101d6 | 1431 | classdriver_t ***printerDriver) |
e53920b9 | 1432 | { |
bf3816c7 MS |
1433 | kern_return_t kr = kUSBPrinterClassDeviceNotOpen; |
1434 | classdriver_t **driver = NULL; | |
1435 | CFStringRef bundle = driverPath ? driverPath : kUSBGenericTOPrinterClassDriver; | |
1436 | char bundlestr[1024]; /* Bundle path */ | |
bf3816c7 MS |
1437 | CFURLRef url; /* URL for driver */ |
1438 | CFPlugInRef plugin = NULL; /* Plug-in address */ | |
e53920b9 | 1439 | |
bf3816c7 MS |
1440 | |
1441 | CFStringGetCString(bundle, bundlestr, sizeof(bundlestr), kCFStringEncodingUTF8); | |
1442 | ||
1443 | /* | |
1444 | * Validate permissions for the class driver... | |
1445 | */ | |
1446 | ||
22c9029b MS |
1447 | _cups_fc_result_t result = _cupsFileCheck(bundlestr, |
1448 | _CUPS_FILE_CHECK_DIRECTORY, 1, | |
0fa6c7fa | 1449 | Iterating ? NULL : _cupsFileCheckFilter, NULL); |
22c9029b MS |
1450 | |
1451 | if (result && driverPath) | |
321d8d57 | 1452 | return (load_classdriver(NULL, interface, printerDriver)); |
22c9029b MS |
1453 | else if (result) |
1454 | return (kr); | |
bf3816c7 MS |
1455 | |
1456 | /* | |
1457 | * Try loading the class driver... | |
1458 | */ | |
1459 | ||
1460 | url = CFURLCreateWithFileSystemPath(NULL, bundle, kCFURLPOSIXPathStyle, true); | |
e53920b9 | 1461 | |
bf3816c7 MS |
1462 | if (url) |
1463 | { | |
1464 | plugin = CFPlugInCreate(NULL, url); | |
1465 | CFRelease(url); | |
1466 | } | |
1467 | else | |
1468 | plugin = NULL; | |
e53920b9 | 1469 | |
bf3816c7 MS |
1470 | if (plugin) |
1471 | { | |
1472 | CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID, plugin); | |
1473 | if (factories != NULL && CFArrayGetCount(factories) > 0) | |
09a101d6 | 1474 | { |
bf3816c7 MS |
1475 | CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0); |
1476 | IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kUSBPrinterClassTypeID); | |
1477 | if (iunknown != NULL) | |
09a101d6 | 1478 | { |
bf3816c7 MS |
1479 | kr = (*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID), (LPVOID *)&driver); |
1480 | if (kr == kIOReturnSuccess && driver != NULL) | |
09a101d6 | 1481 | { |
bf3816c7 MS |
1482 | classdriver_t **genericDriver = NULL; |
1483 | if (driverPath != NULL && CFStringCompare(driverPath, kUSBGenericTOPrinterClassDriver, 0) != kCFCompareEqualTo) | |
321d8d57 | 1484 | kr = load_classdriver(NULL, interface, &genericDriver); |
e53920b9 | 1485 | |
bf3816c7 MS |
1486 | if (kr == kIOReturnSuccess) |
1487 | { | |
321d8d57 | 1488 | (*driver)->interface = interface; |
bf3816c7 | 1489 | (*driver)->Initialize(driver, genericDriver); |
e53920b9 | 1490 | |
bf3816c7 | 1491 | (*driver)->plugin = plugin; |
321d8d57 | 1492 | (*driver)->interface = interface; |
bf3816c7 | 1493 | *printerDriver = driver; |
e53920b9 | 1494 | } |
e53920b9 | 1495 | } |
bf3816c7 | 1496 | (*iunknown)->Release(iunknown); |
e53920b9 | 1497 | } |
bf3816c7 | 1498 | CFRelease(factories); |
ef416fc2 | 1499 | } |
e53920b9 | 1500 | } |
1501 | ||
c0e1af83 | 1502 | fprintf(stderr, "DEBUG: load_classdriver(%s) (kr:0x%08x)\n", bundlestr, (int)kr); |
ef416fc2 | 1503 | |
bf3816c7 | 1504 | return (kr); |
ef416fc2 | 1505 | } |
1506 | ||
1507 | ||
e53920b9 | 1508 | /* |
1509 | * 'unload_classdriver()' - Unload a classdriver. | |
1510 | */ | |
ef416fc2 | 1511 | |
09a101d6 | 1512 | static kern_return_t unload_classdriver(classdriver_t ***classdriver) |
e53920b9 | 1513 | { |
09a101d6 | 1514 | if (*classdriver != NULL) |
1515 | { | |
1516 | (**classdriver)->Release(*classdriver); | |
1517 | *classdriver = NULL; | |
e53920b9 | 1518 | } |
ef416fc2 | 1519 | |
e53920b9 | 1520 | return kIOReturnSuccess; |
1521 | } | |
ef416fc2 | 1522 | |
ef416fc2 | 1523 | |
e53920b9 | 1524 | /* |
09a101d6 | 1525 | * 'load_printerdriver()' - Load vendor's classdriver. |
411affcf | 1526 | * |
1527 | * If driverBundlePath is not NULL on return it is the callers responsbility to release it! | |
e53920b9 | 1528 | */ |
ef416fc2 | 1529 | |
09a101d6 | 1530 | static kern_return_t load_printerdriver(CFStringRef *driverBundlePath) |
ef416fc2 | 1531 | { |
7594b224 | 1532 | IOCFPlugInInterface **iodev = NULL; |
1533 | SInt32 score; | |
1534 | kern_return_t kr; | |
321d8d57 | 1535 | printer_interface_t interface; |
7594b224 | 1536 | HRESULT res; |
1537 | ||
09a101d6 | 1538 | kr = IOCreatePlugInInterfaceForService(g.printer_obj, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score); |
7594b224 | 1539 | if (kr == kIOReturnSuccess) |
1540 | { | |
321d8d57 | 1541 | if ((res = (*iodev)->QueryInterface(iodev, USB_INTERFACE_KIND, (LPVOID *) &interface)) == noErr) |
7594b224 | 1542 | { |
09a101d6 | 1543 | *driverBundlePath = IORegistryEntryCreateCFProperty(g.printer_obj, kUSBClassDriverProperty, NULL, kNilOptions); |
e53920b9 | 1544 | |
7530b13a | 1545 | g.use_generic_class_driver = (*driverBundlePath == NULL || (CFStringCompare(*driverBundlePath, kUSBGenericTOPrinterClassDriver, 0x0) == kCFCompareEqualTo)); |
321d8d57 | 1546 | kr = load_classdriver(*driverBundlePath, interface, &g.classdriver); |
ef416fc2 | 1547 | |
e53920b9 | 1548 | if (kr != kIOReturnSuccess) |
321d8d57 | 1549 | (*interface)->Release(interface); |
e53920b9 | 1550 | } |
1551 | IODestroyPlugInInterface(iodev); | |
1552 | } | |
1553 | return kr; | |
ef416fc2 | 1554 | } |
1555 | ||
f1ebe30e | 1556 | static printer_interface_t usb_printer_interface_interface(io_service_t usbClass) |
fe750b86 | 1557 | { |
f1ebe30e | 1558 | printer_interface_t intf = NULL; |
fe750b86 MS |
1559 | IOCFPlugInInterface **plugin = NULL; |
1560 | SInt32 score; | |
1561 | int kr = IOCreatePlugInInterfaceForService(usbClass, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score); | |
1562 | if (kr == kIOReturnSuccess) | |
1563 | { | |
f1ebe30e | 1564 | (*plugin)->QueryInterface(plugin, USB_INTERFACE_KIND, (LPVOID *)&intf); |
fe750b86 MS |
1565 | IODestroyPlugInInterface(plugin); |
1566 | } | |
1567 | ||
1568 | return intf; | |
1569 | } | |
1570 | ||
1571 | static IOUSBDeviceInterface **usb_device_interface_for_device(io_service_t usbDevice) | |
1572 | { | |
1573 | IOUSBDeviceInterface ** intf = NULL; | |
1574 | IOCFPlugInInterface **plugin = NULL; | |
1575 | SInt32 score; | |
1576 | ||
1577 | int kr = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score); | |
1578 | if (kr == kIOReturnSuccess) | |
1579 | { | |
1580 | (*plugin)->QueryInterface(plugin, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID *)&intf); | |
1581 | IODestroyPlugInInterface(plugin); | |
1582 | } | |
1583 | ||
1584 | return intf; | |
1585 | } | |
1586 | ||
1587 | ||
f1ebe30e | 1588 | static CFStringRef copy_printer_interface_deviceid(printer_interface_t printer, UInt8 alternateSetting) |
fe750b86 MS |
1589 | { |
1590 | // I have tried to make this function as neat as I can, but the possibility of needing to resend | |
1591 | // a request to get the entire string makes it hideous... | |
1592 | // | |
1593 | // We package the job of sending a request up into the block (^sendRequest), which takes the size | |
1594 | // it should allocate for the message buffer. It frees the current buffer if one is set and | |
1595 | // allocates one of the specified size, then performs the request. We can then easily retry by | |
1596 | // calling the block again if we fail to get the whole string the first time around. | |
1597 | ||
1598 | #define kUSBPrintClassGetDeviceID 0 | |
1599 | #define kDefaultNoDataTimeout 5000L | |
1600 | #define pack_device_id_wIndex(intf, alt) ((UInt16)((((UInt16)(intf)) << 8) | ((UInt8)(alt)))) | |
1601 | ||
de3edeba MS |
1602 | if (printer == NULL) |
1603 | return NULL; | |
fe750b86 MS |
1604 | |
1605 | ||
1606 | IOReturn err = kIOReturnError; | |
1607 | UInt8 configurationIndex = 0; | |
1608 | UInt8 interfaceNumber = 0; | |
1609 | size_t bufferLength = 256; | |
1610 | CFStringRef ret = NULL; | |
1611 | ||
1612 | if ((*printer)->GetConfigurationValue( printer, &configurationIndex) == kIOReturnSuccess && | |
1613 | (*printer)->GetInterfaceNumber( printer, &interfaceNumber) == kIOReturnSuccess) | |
1614 | { | |
1615 | __block IOUSBDevRequestTO request; | |
1616 | IOReturn (^sendRequest)(size_t) = ^ (size_t size) | |
1617 | { | |
1618 | if (request.pData) | |
1619 | { | |
1620 | free(request.pData); | |
1621 | request.wLength = 0; | |
1622 | request.pData = NULL; | |
1623 | } | |
1624 | ||
f8f7636e | 1625 | IOReturn berr = kIOReturnError; |
fe750b86 MS |
1626 | char *buffer = malloc(size); |
1627 | if (buffer == NULL) | |
1628 | return kIOReturnNoMemory; | |
1629 | ||
1630 | request.wLength = HostToUSBWord(size); | |
1631 | request.pData = buffer; | |
f8f7636e MS |
1632 | berr = (*printer)->ControlRequestTO(printer, (UInt8)0, &request); |
1633 | return berr; | |
fe750b86 MS |
1634 | }; |
1635 | ||
1636 | /* This request takes the 0 based configuration index. IOKit returns a 1 based configuration index */ | |
1637 | configurationIndex -= 1; | |
1638 | ||
1639 | bzero(&request, sizeof(request)); | |
1640 | ||
1641 | request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBClass, kUSBInterface); | |
1642 | request.bRequest = kUSBPrintClassGetDeviceID; | |
1643 | request.wValue = HostToUSBWord(configurationIndex); | |
1644 | request.wIndex = HostToUSBWord(pack_device_id_wIndex(interfaceNumber, alternateSetting)); | |
1645 | request.noDataTimeout = kDefaultNoDataTimeout; | |
1646 | request.completionTimeout = 0; // Copying behavior from Generic Class Driver | |
1647 | ||
1648 | err = sendRequest(bufferLength); | |
1649 | ||
1650 | if (err == kIOReturnSuccess && request.wLenDone > 1) | |
1651 | { | |
1652 | UInt16 actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData)); | |
1653 | ||
1654 | if (actualLength > 2 && actualLength <= bufferLength - 2) | |
1655 | { | |
1656 | ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false); | |
1657 | } | |
fd5ce3a4 | 1658 | else if (actualLength > 2) { |
fe750b86 MS |
1659 | err = sendRequest(actualLength); |
1660 | if (err == kIOReturnSuccess && request.wLenDone > 0) | |
1661 | { | |
1662 | actualLength = OSSwapBigToHostInt16(*((UInt16 *)request.pData)); | |
1663 | ret = CFStringCreateWithBytes(NULL, (const UInt8 *) &request.pData[2], actualLength - 2, kCFStringEncodingUTF8, false); | |
1664 | } | |
1665 | } | |
1666 | } | |
1667 | ||
1668 | if (request.pData) | |
1669 | free(request.pData); | |
1670 | } | |
1671 | ||
1672 | CFStringRef manufacturer = deviceIDCopyManufacturer(ret); | |
de3edeba | 1673 | CFStringRef model = deviceIDCopyModel(ret); |
fe750b86 MS |
1674 | CFStringRef serial = deviceIDCopySerialNumber(ret); |
1675 | ||
1676 | if (manufacturer == NULL || serial == NULL || model == NULL) | |
1677 | { | |
1678 | IOUSBDevRequestTO request; | |
1679 | IOUSBDeviceDescriptor desc; | |
1680 | ||
1681 | bzero(&request, sizeof(request)); | |
1682 | ||
1683 | request.bmRequestType = USBmakebmRequestType( kUSBIn, kUSBStandard, kUSBDevice ); | |
1684 | request.bRequest = kUSBRqGetDescriptor; | |
1685 | request.wValue = kUSBDeviceDesc << 8; | |
1686 | request.wIndex = 0; | |
1687 | request.wLength = sizeof(desc); | |
1688 | request.pData = &desc; | |
1689 | request.completionTimeout = 0; | |
1690 | request.noDataTimeout = 60L; | |
1691 | ||
1692 | err = (*printer)->ControlRequestTO(printer, 0, &request); | |
1693 | if (err == kIOReturnSuccess) | |
1694 | { | |
1695 | CFMutableStringRef extras = CFStringCreateMutable(NULL, 0); | |
1696 | if (manufacturer == NULL) | |
1697 | { | |
de3edeba MS |
1698 | manufacturer = copy_printer_interface_indexed_description(printer, desc.iManufacturer, kUSBLanguageEnglish); |
1699 | if (manufacturer && CFStringGetLength(manufacturer) > 0) | |
fe750b86 MS |
1700 | CFStringAppendFormat(extras, NULL, CFSTR("MFG:%@;"), manufacturer); |
1701 | } | |
1702 | ||
1703 | if (model == NULL) | |
1704 | { | |
de3edeba MS |
1705 | model = copy_printer_interface_indexed_description(printer, desc.iProduct, kUSBLanguageEnglish); |
1706 | if (model && CFStringGetLength(model) > 0) | |
1707 | CFStringAppendFormat(extras, NULL, CFSTR("MDL:%@;"), model); | |
fe750b86 MS |
1708 | } |
1709 | ||
1710 | if (serial == NULL && desc.iSerialNumber != 0) | |
1711 | { | |
de3edeba MS |
1712 | serial = copy_printer_interface_indexed_description(printer, desc.iSerialNumber, kUSBLanguageEnglish); |
1713 | if (serial && CFStringGetLength(serial) > 0) | |
fe750b86 MS |
1714 | CFStringAppendFormat(extras, NULL, CFSTR("SERN:%@;"), serial); |
1715 | } | |
1716 | ||
1717 | if (ret != NULL) | |
1718 | { | |
1719 | CFStringAppend(extras, ret); | |
1720 | CFRelease(ret); | |
1721 | ||
1722 | ret = extras; | |
1723 | } | |
1724 | else | |
1725 | { | |
1726 | ret = extras; | |
1727 | } | |
1728 | } | |
1729 | } | |
1730 | ||
de3edeba MS |
1731 | if (ret != NULL) |
1732 | { | |
fe750b86 MS |
1733 | /* Remove special characters from the serial number */ |
1734 | CFRange range = (serial != NULL ? CFStringFind(serial, CFSTR("+"), 0) : CFRangeMake(0, 0)); | |
1735 | if (range.length == 1) | |
1736 | { | |
1737 | range = CFStringFind(ret, serial, 0); | |
1738 | ||
1739 | CFMutableStringRef deviceIDString = CFStringCreateMutableCopy(NULL, 0, ret); | |
1740 | CFRelease(ret); | |
1741 | ||
1742 | ret = deviceIDString; | |
1743 | CFStringFindAndReplace(deviceIDString, CFSTR("+"), CFSTR(""), range, 0); | |
1744 | } | |
de3edeba MS |
1745 | } |
1746 | ||
1747 | if (manufacturer != NULL) | |
1748 | CFRelease(manufacturer); | |
7530b13a | 1749 | |
de3edeba MS |
1750 | if (model != NULL) |
1751 | CFRelease(model); | |
7530b13a | 1752 | |
de3edeba MS |
1753 | if (serial != NULL) |
1754 | CFRelease(serial); | |
fe750b86 | 1755 | |
de3edeba MS |
1756 | if (ret != NULL && CFStringGetLength(ret) == 0) |
1757 | { | |
1758 | CFRelease(ret); | |
1759 | return NULL; | |
1760 | } | |
7530b13a | 1761 | |
de3edeba | 1762 | return ret; |
fe750b86 MS |
1763 | } |
1764 | ||
f1ebe30e | 1765 | static CFStringRef copy_printer_interface_indexed_description(printer_interface_t printer, UInt8 index, UInt16 language) |
fe750b86 MS |
1766 | { |
1767 | IOReturn err; | |
1768 | UInt8 description[256]; // Max possible descriptor length | |
1769 | IOUSBDevRequestTO request; | |
1770 | ||
1771 | bzero(description, 2); | |
1772 | ||
1773 | request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); | |
1774 | request.bRequest = kUSBRqGetDescriptor; | |
1775 | request.wValue = (kUSBStringDesc << 8) | index; | |
1776 | request.wIndex = language; | |
1777 | request.wLength = 2; | |
1778 | request.pData = &description; | |
1779 | request.completionTimeout = 0; | |
1780 | request.noDataTimeout = 60L; | |
1781 | ||
1782 | err = (*printer)->ControlRequestTO(printer, 0, &request); | |
1783 | if (err != kIOReturnSuccess && err != kIOReturnOverrun) | |
1784 | { | |
1785 | bzero(description, request.wLength); | |
1786 | ||
1787 | // Let's try again full length. Here's why: | |
1788 | // On USB 2.0 controllers, we will not get an overrun error. We just get a "babble" error | |
1789 | // and no valid data. So, if we ask for the max size, we will either get it, or we'll get an underrun. | |
1790 | // It looks like we get it w/out an underrun | |
1791 | ||
1792 | request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); | |
1793 | request.bRequest = kUSBRqGetDescriptor; | |
1794 | request.wValue = (kUSBStringDesc << 8) | index; | |
1795 | request.wIndex = language; | |
1796 | request.wLength = sizeof description; | |
1797 | request.pData = &description; | |
1798 | request.completionTimeout = 0; | |
1799 | request.noDataTimeout = 60L; | |
1800 | ||
1801 | err = (*printer)->ControlRequestTO(printer, 0, &request); | |
1802 | if (err != kIOReturnSuccess && err != kIOReturnUnderrun) | |
de3edeba | 1803 | return NULL; |
fe750b86 MS |
1804 | } |
1805 | ||
1806 | unsigned int length = description[0]; | |
1807 | if (length == 0) | |
1808 | return CFStringCreateWithCString(NULL, "", kCFStringEncodingUTF8); | |
1809 | ||
1810 | if (description[1] != kUSBStringDesc) | |
1811 | return NULL; | |
1812 | ||
1813 | request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); | |
1814 | request.bRequest = kUSBRqGetDescriptor; | |
1815 | request.wValue = (kUSBStringDesc << 8) | index; | |
1816 | request.wIndex = language; | |
1817 | ||
1818 | bzero(description, length); | |
f8f7636e | 1819 | request.wLength = (UInt16)length; |
fe750b86 MS |
1820 | request.pData = &description; |
1821 | request.completionTimeout = 0; | |
1822 | request.noDataTimeout = 60L; | |
1823 | ||
1824 | err = (*printer)->ControlRequestTO(printer, 0, &request); | |
1825 | if (err != kIOReturnSuccess) | |
de3edeba | 1826 | return NULL; |
fe750b86 MS |
1827 | |
1828 | if (description[1] != kUSBStringDesc) | |
de3edeba | 1829 | return NULL; |
fe750b86 MS |
1830 | |
1831 | if ((description[0] & 1) != 0) | |
1832 | description[0] &= 0xfe; | |
1833 | ||
1834 | char buffer[258] = {}; | |
1835 | unsigned int maxLength = sizeof buffer; | |
1836 | if (description[0] > 1) | |
1837 | { | |
1838 | length = (description[0]-2)/2; | |
1839 | ||
1840 | if (length > maxLength - 1) | |
1841 | length = maxLength -1; | |
1842 | ||
f8f7636e | 1843 | for (unsigned i = 0; i < length; i++) |
fe750b86 MS |
1844 | buffer[i] = (char) description[2*i+2]; |
1845 | ||
1846 | buffer[length] = 0; | |
1847 | } | |
1848 | ||
1849 | return CFStringCreateWithCString(NULL, buffer, kCFStringEncodingUTF8); | |
1850 | } | |
ef416fc2 | 1851 | |
e53920b9 | 1852 | /* |
1853 | * 'registry_open()' - Open a connection to the printer. | |
1854 | */ | |
ef416fc2 | 1855 | |
09a101d6 | 1856 | static kern_return_t registry_open(CFStringRef *driverBundlePath) |
e53920b9 | 1857 | { |
09a101d6 | 1858 | g.bidi_flag = 0; /* 0=unidirectional */ |
f7deaa1a | 1859 | |
09a101d6 | 1860 | kern_return_t kr = load_printerdriver(driverBundlePath); |
1861 | if (kr != kIOReturnSuccess) | |
e53920b9 | 1862 | kr = -2; |
ef416fc2 | 1863 | |
09a101d6 | 1864 | if (g.classdriver != NULL) |
1865 | { | |
bc44d920 | 1866 | (*g.classdriver)->interfaceNumber = g.interfaceNum; |
09a101d6 | 1867 | kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolBidirectional); |
1868 | if (kr != kIOReturnSuccess || (*g.classdriver)->interface == NULL) | |
1869 | { | |
1870 | kr = (*g.classdriver)->Open(g.classdriver, g.location, kUSBPrintingProtocolUnidirectional); | |
1871 | if (kr == kIOReturnSuccess) | |
1872 | { | |
1873 | if ((*g.classdriver)->interface == NULL) | |
1874 | { | |
1875 | (*g.classdriver)->Close(g.classdriver); | |
e53920b9 | 1876 | kr = -1; |
1877 | } | |
1878 | } | |
1879 | } | |
09a101d6 | 1880 | else |
1881 | g.bidi_flag = 1; /* 1=bidirectional */ | |
e53920b9 | 1882 | } |
ef416fc2 | 1883 | |
09a101d6 | 1884 | if (kr != kIOReturnSuccess) |
1885 | unload_classdriver(&g.classdriver); | |
ef416fc2 | 1886 | |
e53920b9 | 1887 | return kr; |
1888 | } | |
ef416fc2 | 1889 | |
ef416fc2 | 1890 | |
e53920b9 | 1891 | /* |
1892 | * 'registry_close()' - Close the connection to the printer. | |
1893 | */ | |
ef416fc2 | 1894 | |
eac3a0a0 | 1895 | static kern_return_t registry_close(void) |
ef416fc2 | 1896 | { |
09a101d6 | 1897 | if (g.classdriver != NULL) |
1898 | (*g.classdriver)->Close(g.classdriver); | |
1899 | ||
1900 | unload_classdriver(&g.classdriver); | |
e53920b9 | 1901 | return kIOReturnSuccess; |
ef416fc2 | 1902 | } |
1903 | ||
e53920b9 | 1904 | #pragma mark - |
1905 | /* | |
1906 | * 'copy_value_for_key()' - Copy value string associated with a key. | |
1907 | */ | |
ef416fc2 | 1908 | |
09a101d6 | 1909 | static CFStringRef copy_value_for_key(CFStringRef deviceID, |
1910 | CFStringRef *keys) | |
ef416fc2 | 1911 | { |
e53920b9 | 1912 | CFStringRef value = NULL; |
1913 | CFArrayRef kvPairs = deviceID != NULL ? CFStringCreateArrayBySeparatingStrings(NULL, deviceID, CFSTR(";")) : NULL; | |
1914 | CFIndex max = kvPairs != NULL ? CFArrayGetCount(kvPairs) : 0; | |
1915 | CFIndex idx = 0; | |
1916 | ||
09a101d6 | 1917 | while (idx < max && value == NULL) |
1918 | { | |
e53920b9 | 1919 | CFStringRef kvpair = CFArrayGetValueAtIndex(kvPairs, idx); |
1920 | CFIndex idxx = 0; | |
09a101d6 | 1921 | while (keys[idxx] != NULL && value == NULL) |
1922 | { | |
e53920b9 | 1923 | CFRange range = CFStringFind(kvpair, keys[idxx], kCFCompareCaseInsensitive); |
09a101d6 | 1924 | if (range.length != -1) |
1925 | { | |
1926 | if (range.location != 0) | |
1927 | { | |
e53920b9 | 1928 | CFMutableStringRef theString = CFStringCreateMutableCopy(NULL, 0, kvpair); |
1929 | CFStringTrimWhitespace(theString); | |
1930 | range = CFStringFind(theString, keys[idxx], kCFCompareCaseInsensitive); | |
09a101d6 | 1931 | if (range.location == 0) |
e53920b9 | 1932 | value = CFStringCreateWithSubstring(NULL, theString, CFRangeMake(range.length, CFStringGetLength(theString) - range.length)); |
09a101d6 | 1933 | |
e53920b9 | 1934 | CFRelease(theString); |
1935 | } | |
09a101d6 | 1936 | else |
1937 | { | |
e53920b9 | 1938 | CFStringRef theString = CFStringCreateWithSubstring(NULL, kvpair, CFRangeMake(range.length, CFStringGetLength(kvpair) - range.length)); |
1939 | CFMutableStringRef theString2 = CFStringCreateMutableCopy(NULL, 0, theString); | |
1940 | CFRelease(theString); | |
ef416fc2 | 1941 | |
e53920b9 | 1942 | CFStringTrimWhitespace(theString2); |
1943 | value = theString2; | |
ef416fc2 | 1944 | } |
e53920b9 | 1945 | } |
1946 | idxx++; | |
ef416fc2 | 1947 | } |
e53920b9 | 1948 | idx++; |
1949 | } | |
1950 | ||
1951 | if (kvPairs != NULL) | |
09a101d6 | 1952 | CFRelease(kvPairs); |
e53920b9 | 1953 | return value; |
ef416fc2 | 1954 | } |
1955 | ||
1956 | ||
411affcf | 1957 | /* |
09a101d6 | 1958 | * 'cfstr_create_trim()' - Create CFString and trim whitespace characters. |
411affcf | 1959 | */ |
1960 | ||
09a101d6 | 1961 | CFStringRef cfstr_create_trim(const char *cstr) |
411affcf | 1962 | { |
1963 | CFStringRef cfstr; | |
1964 | CFMutableStringRef cfmutablestr = NULL; | |
09a101d6 | 1965 | |
411affcf | 1966 | if ((cfstr = CFStringCreateWithCString(NULL, cstr, kCFStringEncodingUTF8)) != NULL) |
1967 | { | |
1968 | if ((cfmutablestr = CFStringCreateMutableCopy(NULL, 1024, cfstr)) != NULL) | |
1969 | CFStringTrimWhitespace(cfmutablestr); | |
1970 | ||
1971 | CFRelease(cfstr); | |
1972 | } | |
1973 | return (CFStringRef) cfmutablestr; | |
1974 | } | |
1975 | ||
1976 | ||
e53920b9 | 1977 | #pragma mark - |
1978 | /* | |
db1f069b | 1979 | * 'parse_options()' - Parse URI options. |
e53920b9 | 1980 | */ |
ef416fc2 | 1981 | |
db1f069b | 1982 | static void parse_options(char *options, |
09a101d6 | 1983 | char *serial, |
76cd9e37 | 1984 | int serial_size, |
09a101d6 | 1985 | UInt32 *location, |
1986 | Boolean *wait_eof) | |
e53920b9 | 1987 | { |
db1f069b MS |
1988 | char sep, /* Separator character */ |
1989 | *name, /* Name of option */ | |
1990 | *value; /* Value of option */ | |
1991 | ||
ef416fc2 | 1992 | |
e53920b9 | 1993 | if (serial) |
1994 | *serial = '\0'; | |
1995 | if (location) | |
1996 | *location = 0; | |
ef416fc2 | 1997 | |
e53920b9 | 1998 | if (!options) |
1999 | return; | |
ef416fc2 | 2000 | |
db1f069b | 2001 | while (*options) |
09a101d6 | 2002 | { |
db1f069b MS |
2003 | /* |
2004 | * Get the name... | |
2005 | */ | |
ef416fc2 | 2006 | |
db1f069b | 2007 | name = options; |
ef416fc2 | 2008 | |
db1f069b | 2009 | while (*options && *options != '=' && *options != '+' && *options != '&') |
e53920b9 | 2010 | options ++; |
ef416fc2 | 2011 | |
db1f069b MS |
2012 | if ((sep = *options) != '\0') |
2013 | *options++ = '\0'; | |
ef416fc2 | 2014 | |
db1f069b MS |
2015 | if (sep == '=') |
2016 | { | |
2017 | /* | |
2018 | * Get the value... | |
2019 | */ | |
ef416fc2 | 2020 | |
db1f069b MS |
2021 | value = options; |
2022 | ||
2023 | while (*options && *options != '+' && *options != '&') | |
e53920b9 | 2024 | options ++; |
db1f069b MS |
2025 | |
2026 | if (*options) | |
2027 | *options++ = '\0'; | |
e53920b9 | 2028 | } |
db1f069b MS |
2029 | else |
2030 | value = (char *)""; | |
ef416fc2 | 2031 | |
db1f069b MS |
2032 | /* |
2033 | * Process the option... | |
2034 | */ | |
2035 | ||
88f9aafc | 2036 | if (!_cups_strcasecmp(name, "waiteof")) |
09a101d6 | 2037 | { |
88f9aafc MS |
2038 | if (!_cups_strcasecmp(value, "on") || |
2039 | !_cups_strcasecmp(value, "yes") || | |
2040 | !_cups_strcasecmp(value, "true")) | |
09a101d6 | 2041 | *wait_eof = true; |
88f9aafc MS |
2042 | else if (!_cups_strcasecmp(value, "off") || |
2043 | !_cups_strcasecmp(value, "no") || | |
2044 | !_cups_strcasecmp(value, "false")) | |
09a101d6 | 2045 | *wait_eof = false; |
2046 | else | |
0837b7e8 MS |
2047 | _cupsLangPrintFilter(stderr, "WARNING", |
2048 | _("Boolean expected for waiteof option \"%s\"."), | |
2049 | value); | |
e53920b9 | 2050 | } |
88f9aafc | 2051 | else if (!_cups_strcasecmp(name, "serial")) |
76cd9e37 | 2052 | strlcpy(serial, value, serial_size); |
88f9aafc | 2053 | else if (!_cups_strcasecmp(name, "location") && location) |
7e86f2f6 | 2054 | *location = (UInt32)strtoul(value, NULL, 16); |
e53920b9 | 2055 | } |
e53920b9 | 2056 | } |
ef416fc2 | 2057 | |
ef416fc2 | 2058 | |
e53920b9 | 2059 | /*! |
2060 | * @function setup_cfLanguage | |
ac884b6a | 2061 | * @abstract Convert the contents of the CUPS 'APPLE_LANGUAGE' environment |
e53920b9 | 2062 | * variable into a one element CF array of languages. |
2063 | * | |
2064 | * @discussion Each submitted job comes with a natural language. CUPS passes | |
2065 | * that language in an environment variable. We take that language | |
2066 | * and jam it into the AppleLanguages array so that CF will use | |
2067 | * it when reading localized resources. We need to do this before | |
2068 | * any CF code reads and caches the languages array, so this function | |
2069 | * should be called early in main() | |
2070 | */ | |
2071 | static void setup_cfLanguage(void) | |
2072 | { | |
2073 | CFStringRef lang[1] = {NULL}; | |
2074 | CFArrayRef langArray = NULL; | |
2075 | const char *requestedLang = NULL; | |
2076 | ||
ac884b6a MS |
2077 | if ((requestedLang = getenv("APPLE_LANGUAGE")) == NULL) |
2078 | requestedLang = getenv("LANG"); | |
2079 | ||
09a101d6 | 2080 | if (requestedLang != NULL) |
2081 | { | |
e53920b9 | 2082 | lang[0] = CFStringCreateWithCString(kCFAllocatorDefault, requestedLang, kCFStringEncodingUTF8); |
2083 | langArray = CFArrayCreate(kCFAllocatorDefault, (const void **)lang, sizeof(lang) / sizeof(lang[0]), &kCFTypeArrayCallBacks); | |
2084 | ||
28b9d139 | 2085 | CFPreferencesSetValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost); |
005dd1eb | 2086 | fprintf(stderr, "DEBUG: usb: AppleLanguages=\"%s\"\n", requestedLang); |
e53920b9 | 2087 | |
2088 | CFRelease(lang[0]); | |
2089 | CFRelease(langArray); | |
e53920b9 | 2090 | } |
09a101d6 | 2091 | else |
ac884b6a | 2092 | fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr); |
e53920b9 | 2093 | } |
ef416fc2 | 2094 | |
e53920b9 | 2095 | #pragma mark - |
749b1e90 | 2096 | #if defined(__i386__) || defined(__x86_64__) |
e53920b9 | 2097 | /*! |
749b1e90 | 2098 | * @function run_legacy_backend |
e53920b9 | 2099 | * |
749b1e90 | 2100 | * @abstract Starts child backend process running as a ppc or i386 executable. |
e53920b9 | 2101 | * |
2102 | * @result Never returns; always calls exit(). | |
2103 | * | |
09a101d6 | 2104 | * @discussion |
e53920b9 | 2105 | */ |
749b1e90 MS |
2106 | static void run_legacy_backend(int argc, |
2107 | char *argv[], | |
2108 | int fd) | |
e53920b9 | 2109 | { |
2110 | int i; | |
2111 | int exitstatus = 0; | |
2112 | int childstatus; | |
2113 | pid_t waitpid_status; | |
2114 | char *my_argv[32]; | |
749b1e90 | 2115 | char *usb_legacy_status; |
ef416fc2 | 2116 | |
7e86f2f6 | 2117 | |
749b1e90 MS |
2118 | /* |
2119 | * If we're running as x86_64 or i386 and couldn't load the class driver | |
2120 | * (because it's ppc or i386), then try to re-exec ourselves in ppc or i386 | |
2121 | * mode to try again. If we don't have a ppc or i386 architecture we may be | |
2122 | * running with the same architecture again so guard against this by setting | |
2123 | * and testing an environment variable... | |
2124 | */ | |
e53920b9 | 2125 | |
749b1e90 MS |
2126 | # ifdef __x86_64__ |
2127 | usb_legacy_status = getenv("USB_I386_STATUS"); | |
2128 | # else | |
2129 | usb_legacy_status = getenv("USB_PPC_STATUS"); | |
2130 | # endif /* __x86_64__ */ | |
2131 | ||
2132 | if (!usb_legacy_status) | |
09a101d6 | 2133 | { |
9fb65b89 MS |
2134 | log_usb_class_driver(IS_NOT_64BIT); |
2135 | ||
bc44d920 | 2136 | /* |
2137 | * Setup a SIGTERM handler then block it before forking... | |
2138 | */ | |
ef416fc2 | 2139 | |
ef55b745 | 2140 | int err; /* posix_spawn result */ |
bc44d920 | 2141 | struct sigaction action; /* POSIX signal action */ |
2142 | sigset_t newmask, /* New signal mask */ | |
2143 | oldmask; /* Old signal mask */ | |
58dc1933 MS |
2144 | char usbpath[1024]; /* Path to USB backend */ |
2145 | const char *cups_serverbin;/* Path to CUPS binaries */ | |
2146 | ||
bc44d920 | 2147 | |
2148 | memset(&action, 0, sizeof(action)); | |
2149 | sigaddset(&action.sa_mask, SIGTERM); | |
2150 | action.sa_handler = sigterm_handler; | |
2151 | sigaction(SIGTERM, &action, NULL); | |
2152 | ||
2153 | sigemptyset(&newmask); | |
2154 | sigaddset(&newmask, SIGTERM); | |
2155 | sigprocmask(SIG_BLOCK, &newmask, &oldmask); | |
ef416fc2 | 2156 | |
749b1e90 MS |
2157 | /* |
2158 | * Set the environment variable... | |
2159 | */ | |
bc44d920 | 2160 | |
749b1e90 MS |
2161 | # ifdef __x86_64__ |
2162 | setenv("USB_I386_STATUS", "1", false); | |
2163 | # else | |
2164 | setenv("USB_PPC_STATUS", "1", false); | |
2165 | # endif /* __x86_64__ */ | |
bc44d920 | 2166 | |
749b1e90 MS |
2167 | /* |
2168 | * Tell the kernel to use the specified CPU architecture... | |
2169 | */ | |
bc44d920 | 2170 | |
749b1e90 MS |
2171 | # ifdef __x86_64__ |
2172 | cpu_type_t cpu = CPU_TYPE_I386; | |
2173 | # else | |
2174 | cpu_type_t cpu = CPU_TYPE_POWERPC; | |
2175 | # endif /* __x86_64__ */ | |
9a4f8274 | 2176 | size_t ocount = 1; |
749b1e90 | 2177 | posix_spawnattr_t attrs; |
ef416fc2 | 2178 | |
749b1e90 MS |
2179 | if (!posix_spawnattr_init(&attrs)) |
2180 | { | |
b19ccc9e MS |
2181 | posix_spawnattr_setsigdefault(&attrs, &oldmask); |
2182 | if (posix_spawnattr_setbinpref_np(&attrs, 1, &cpu, &ocount) || ocount != 1) | |
9a4f8274 MS |
2183 | { |
2184 | # ifdef __x86_64__ | |
2185 | perror("DEBUG: Unable to set binary preference to i386"); | |
2186 | # else | |
2187 | perror("DEBUG: Unable to set binary preference to ppc"); | |
2188 | # endif /* __x86_64__ */ | |
0837b7e8 MS |
2189 | _cupsLangPrintFilter(stderr, "ERROR", |
2190 | _("Unable to use legacy USB class driver.")); | |
9a4f8274 MS |
2191 | exit(CUPS_BACKEND_STOP); |
2192 | } | |
749b1e90 | 2193 | } |
bc44d920 | 2194 | |
749b1e90 MS |
2195 | /* |
2196 | * Set up the arguments and call posix_spawn... | |
2197 | */ | |
ef416fc2 | 2198 | |
58dc1933 MS |
2199 | if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) |
2200 | cups_serverbin = CUPS_SERVERBIN; | |
2201 | snprintf(usbpath, sizeof(usbpath), "%s/backend/usb", cups_serverbin); | |
2202 | ||
7e86f2f6 | 2203 | for (i = 0; i < argc && i < (int)(sizeof(my_argv) / sizeof(my_argv[0])) - 1; i ++) |
749b1e90 | 2204 | my_argv[i] = argv[i]; |
ef416fc2 | 2205 | |
749b1e90 | 2206 | my_argv[i] = NULL; |
ef416fc2 | 2207 | |
ef55b745 MS |
2208 | if ((err = posix_spawn(&child_pid, usbpath, NULL, &attrs, my_argv, |
2209 | environ)) != 0) | |
09a101d6 | 2210 | { |
58dc1933 | 2211 | fprintf(stderr, "DEBUG: Unable to exec %s: %s\n", usbpath, |
ef55b745 | 2212 | strerror(err)); |
0837b7e8 MS |
2213 | _cupsLangPrintFilter(stderr, "ERROR", |
2214 | _("Unable to use legacy USB class driver.")); | |
749b1e90 | 2215 | exit(CUPS_BACKEND_STOP); |
bc44d920 | 2216 | } |
e53920b9 | 2217 | |
bc44d920 | 2218 | /* |
2219 | * Unblock signals... | |
2220 | */ | |
e53920b9 | 2221 | |
bc44d920 | 2222 | sigprocmask(SIG_SETMASK, &oldmask, NULL); |
e53920b9 | 2223 | |
bc44d920 | 2224 | /* |
2225 | * Close the fds we won't be using then wait for the child backend to exit. | |
2226 | */ | |
2227 | ||
2228 | close(fd); | |
2229 | close(1); | |
2230 | ||
005dd1eb MS |
2231 | fprintf(stderr, "DEBUG: Started usb(legacy) backend (PID %d)\n", |
2232 | (int)child_pid); | |
bc44d920 | 2233 | |
2234 | while ((waitpid_status = waitpid(child_pid, &childstatus, 0)) == (pid_t)-1 && errno == EINTR) | |
2235 | usleep(1000); | |
2236 | ||
2237 | if (WIFSIGNALED(childstatus)) | |
2238 | { | |
749b1e90 | 2239 | exitstatus = CUPS_BACKEND_STOP; |
4d301e69 | 2240 | fprintf(stderr, "DEBUG: usb(legacy) backend %d crashed on signal %d\n", |
749b1e90 | 2241 | child_pid, WTERMSIG(childstatus)); |
ef416fc2 | 2242 | } |
09a101d6 | 2243 | else |
2244 | { | |
bc44d920 | 2245 | if ((exitstatus = WEXITSTATUS(childstatus)) != 0) |
005dd1eb | 2246 | fprintf(stderr, |
4d301e69 | 2247 | "DEBUG: usb(legacy) backend %d stopped with status %d\n", |
005dd1eb | 2248 | child_pid, exitstatus); |
bc44d920 | 2249 | else |
005dd1eb MS |
2250 | fprintf(stderr, "DEBUG: usb(legacy) backend %d exited with no errors\n", |
2251 | child_pid); | |
e53920b9 | 2252 | } |
2253 | } | |
09a101d6 | 2254 | else |
2255 | { | |
005dd1eb | 2256 | fputs("DEBUG: usb(legacy) backend running native again\n", stderr); |
749b1e90 | 2257 | exitstatus = CUPS_BACKEND_STOP; |
e53920b9 | 2258 | } |
ef416fc2 | 2259 | |
e53920b9 | 2260 | exit(exitstatus); |
2261 | } | |
6c48a6ca MS |
2262 | #endif /* __i386__ || __x86_64__ */ |
2263 | ||
ef416fc2 | 2264 | |
e53920b9 | 2265 | /* |
2266 | * 'sigterm_handler()' - SIGTERM handler. | |
2267 | */ | |
ef416fc2 | 2268 | |
f11a948a MS |
2269 | static void |
2270 | sigterm_handler(int sig) /* I - Signal */ | |
ef416fc2 | 2271 | { |
6c48a6ca MS |
2272 | #if defined(__i386__) || defined(__x86_64__) |
2273 | /* | |
2274 | * If we started a child process pass the signal on to it... | |
2275 | */ | |
2276 | ||
e53920b9 | 2277 | if (child_pid) |
f11a948a MS |
2278 | { |
2279 | /* | |
2280 | * If we started a child process pass the signal on to it... | |
2281 | */ | |
e53920b9 | 2282 | |
f11a948a MS |
2283 | int status; |
2284 | ||
2285 | kill(child_pid, sig); | |
2286 | while (waitpid(child_pid, &status, 0) < 0 && errno == EINTR); | |
ef416fc2 | 2287 | |
f11a948a | 2288 | if (WIFEXITED(status)) |
c1de66f9 | 2289 | _exit(WEXITSTATUS(status)); |
f11a948a | 2290 | else if (status == SIGTERM || status == SIGKILL) |
c1de66f9 | 2291 | _exit(0); |
f11a948a MS |
2292 | else |
2293 | { | |
c1de66f9 MS |
2294 | write(2, "DEBUG: Child crashed.\n", 22); |
2295 | _exit(CUPS_BACKEND_STOP); | |
f11a948a MS |
2296 | } |
2297 | } | |
18ecb428 | 2298 | #endif /* __i386__ || __x86_64__ */ |
6c48a6ca | 2299 | } |
18ecb428 | 2300 | |
ef416fc2 | 2301 | |
a469f8a5 MS |
2302 | /* |
2303 | * 'sigquit_handler()' - SIGQUIT handler. | |
2304 | */ | |
2305 | ||
2306 | static void sigquit_handler(int sig, siginfo_t *si, void *unused) | |
2307 | { | |
2308 | char *path; | |
2309 | char pathbuf[PROC_PIDPATHINFO_MAXSIZE]; | |
2310 | static char msgbuf[256] = ""; | |
2311 | ||
2312 | ||
2313 | (void)sig; | |
2314 | (void)unused; | |
2315 | ||
2316 | if (proc_pidpath(si->si_pid, pathbuf, sizeof(pathbuf)) > 0 && | |
2317 | (path = basename(pathbuf)) != NULL) | |
2318 | snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by %s(%d)", path, (int)si->si_pid); | |
2319 | else | |
2320 | snprintf(msgbuf, sizeof(msgbuf), "SIGQUIT sent by PID %d", (int)si->si_pid); | |
2321 | ||
2322 | CRSetCrashLogMessage(msgbuf); | |
2323 | ||
2324 | abort(); | |
2325 | } | |
2326 | ||
2327 | ||
e53920b9 | 2328 | #ifdef PARSE_PS_ERRORS |
2329 | /* | |
2330 | * 'next_line()' - Find the next line in a buffer. | |
2331 | */ | |
ef416fc2 | 2332 | |
e53920b9 | 2333 | static const char *next_line (const char *buffer) |
ef416fc2 | 2334 | { |
e53920b9 | 2335 | const char *cptr, *lptr = NULL; |
ef416fc2 | 2336 | |
e53920b9 | 2337 | for (cptr = buffer; *cptr && lptr == NULL; cptr++) |
2338 | if (*cptr == '\n' || *cptr == '\r') | |
2339 | lptr = cptr; | |
2340 | return lptr; | |
ef416fc2 | 2341 | } |
2342 | ||
ef416fc2 | 2343 | |
e53920b9 | 2344 | /* |
2345 | * 'parse_pserror()' - Scan the backchannel data for postscript errors. | |
2346 | */ | |
ef416fc2 | 2347 | |
09a101d6 | 2348 | static void parse_pserror(char *sockBuffer, |
2349 | int len) | |
ef416fc2 | 2350 | { |
e53920b9 | 2351 | static char gErrorBuffer[1024] = ""; |
2352 | static char *gErrorBufferPtr = gErrorBuffer; | |
2353 | static char *gErrorBufferEndPtr = gErrorBuffer + sizeof(gErrorBuffer); | |
2354 | ||
2355 | char *pCommentBegin, *pCommentEnd, *pLineEnd; | |
2356 | char *logLevel; | |
2357 | char logstr[1024]; | |
2358 | int logstrlen; | |
2359 | ||
2360 | if (gErrorBufferPtr + len > gErrorBufferEndPtr - 1) | |
2361 | gErrorBufferPtr = gErrorBuffer; | |
2362 | if (len > sizeof(gErrorBuffer) - 1) | |
2363 | len = sizeof(gErrorBuffer) - 1; | |
2364 | ||
2365 | memcpy(gErrorBufferPtr, (const void *)sockBuffer, len); | |
2366 | gErrorBufferPtr += len; | |
2367 | *(gErrorBufferPtr + 1) = '\0'; | |
2368 | ||
e53920b9 | 2369 | pLineEnd = (char *)next_line((const char *)gErrorBuffer); |
09a101d6 | 2370 | while (pLineEnd != NULL) |
2371 | { | |
e53920b9 | 2372 | *pLineEnd++ = '\0'; |
2373 | ||
2374 | pCommentBegin = strstr(gErrorBuffer,"%%["); | |
2375 | pCommentEnd = strstr(gErrorBuffer, "]%%"); | |
09a101d6 | 2376 | if (pCommentBegin != gErrorBuffer && pCommentEnd != NULL) |
2377 | { | |
e53920b9 | 2378 | pCommentEnd += 3; /* Skip past "]%%" */ |
2379 | *pCommentEnd = '\0'; /* There's always room for the nul */ | |
2380 | ||
88f9aafc | 2381 | if (_cups_strncasecmp(pCommentBegin, "%%[ Error:", 10) == 0) |
e53920b9 | 2382 | logLevel = "DEBUG"; |
88f9aafc | 2383 | else if (_cups_strncasecmp(pCommentBegin, "%%[ Flushing", 12) == 0) |
e53920b9 | 2384 | logLevel = "DEBUG"; |
2385 | else | |
2386 | logLevel = "INFO"; | |
2387 | ||
09a101d6 | 2388 | if ((logstrlen = snprintf(logstr, sizeof(logstr), "%s: %s\n", logLevel, pCommentBegin)) >= sizeof(logstr)) |
2389 | { | |
e53920b9 | 2390 | /* If the string was trucnated make sure it has a linefeed before the nul */ |
2391 | logstrlen = sizeof(logstr) - 1; | |
2392 | logstr[logstrlen - 1] = '\n'; | |
2393 | } | |
2394 | write(STDERR_FILENO, logstr, logstrlen); | |
ef416fc2 | 2395 | } |
ef416fc2 | 2396 | |
e53920b9 | 2397 | /* move everything over... */ |
5a9febac | 2398 | strlcpy(gErrorBuffer, pLineEnd, sizeof(gErrorBuffer)); |
e53920b9 | 2399 | gErrorBufferPtr = gErrorBuffer; |
2400 | pLineEnd = (char *)next_line((const char *)gErrorBuffer); | |
2401 | } | |
ef416fc2 | 2402 | } |
e53920b9 | 2403 | #endif /* PARSE_PS_ERRORS */ |
ef416fc2 | 2404 | |
e53920b9 | 2405 | |
2406 | /* | |
db1f069b | 2407 | * 'soft_reset()' - Send a soft reset to the device. |
ef416fc2 | 2408 | */ |
e53920b9 | 2409 | |
eac3a0a0 | 2410 | static void soft_reset(void) |
ef416fc2 | 2411 | { |
09a101d6 | 2412 | fd_set input_set; /* Input set for select() */ |
84315f46 | 2413 | struct timeval tv; /* Time value */ |
09a101d6 | 2414 | char buffer[2048]; /* Buffer */ |
2415 | struct timespec cond_timeout; /* pthread condition timeout */ | |
e53920b9 | 2416 | |
09a101d6 | 2417 | /* |
2418 | * Send an abort once a second until the I/O lock is released by the main thread... | |
2419 | */ | |
e53920b9 | 2420 | |
09a101d6 | 2421 | pthread_mutex_lock(&g.readwrite_lock_mutex); |
2422 | while (g.readwrite_lock) | |
2423 | { | |
2424 | (*g.classdriver)->Abort(g.classdriver); | |
ef416fc2 | 2425 | |
84315f46 MS |
2426 | gettimeofday(&tv, NULL); |
2427 | cond_timeout.tv_sec = tv.tv_sec + 1; | |
2428 | cond_timeout.tv_nsec = tv.tv_usec * 1000; | |
e53920b9 | 2429 | |
84315f46 MS |
2430 | while (g.readwrite_lock) |
2431 | { | |
2432 | if (pthread_cond_timedwait(&g.readwrite_lock_cond, | |
2433 | &g.readwrite_lock_mutex, | |
2434 | &cond_timeout) != 0) | |
2435 | break; | |
2436 | } | |
09a101d6 | 2437 | } |
f7deaa1a | 2438 | |
09a101d6 | 2439 | g.readwrite_lock = 1; |
2440 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
f7deaa1a | 2441 | |
09a101d6 | 2442 | /* |
2443 | * Flush bytes waiting on print_fd... | |
2444 | */ | |
f7deaa1a | 2445 | |
09a101d6 | 2446 | g.print_bytes = 0; |
f7deaa1a | 2447 | |
09a101d6 | 2448 | FD_ZERO(&input_set); |
2449 | FD_SET(g.print_fd, &input_set); | |
f7deaa1a | 2450 | |
84315f46 MS |
2451 | tv.tv_sec = 0; |
2452 | tv.tv_usec = 0; | |
f7deaa1a | 2453 | |
84315f46 | 2454 | while (select(g.print_fd+1, &input_set, NULL, NULL, &tv) > 0) |
09a101d6 | 2455 | if (read(g.print_fd, buffer, sizeof(buffer)) <= 0) |
2456 | break; | |
f7deaa1a | 2457 | |
09a101d6 | 2458 | /* |
2459 | * Send the reset... | |
2460 | */ | |
f7deaa1a | 2461 | |
7dfedb92 | 2462 | (*g.classdriver)->SoftReset(g.classdriver, DEFAULT_TIMEOUT); |
f7deaa1a | 2463 | |
09a101d6 | 2464 | /* |
2465 | * Release the I/O lock... | |
2466 | */ | |
f7deaa1a | 2467 | |
09a101d6 | 2468 | pthread_mutex_lock(&g.readwrite_lock_mutex); |
2469 | g.readwrite_lock = 0; | |
2470 | pthread_cond_signal(&g.readwrite_lock_cond); | |
2471 | pthread_mutex_unlock(&g.readwrite_lock_mutex); | |
f7deaa1a | 2472 | } |
2473 | ||
f7deaa1a | 2474 | |
2475 | /* | |
09a101d6 | 2476 | * 'get_device_id()' - Return IEEE-1284 device ID. |
f7deaa1a | 2477 | */ |
f7deaa1a | 2478 | |
09a101d6 | 2479 | static void get_device_id(cups_sc_status_t *status, |
2480 | char *data, | |
2481 | int *datalen) | |
2482 | { | |
f7deaa1a | 2483 | CFStringRef deviceIDString = NULL; |
2484 | ||
fe750b86 MS |
2485 | if (g.printer_obj != IO_OBJECT_NULL) |
2486 | { | |
f1ebe30e MS |
2487 | printer_interface_t printerIntf = usb_printer_interface_interface(g.printer_obj); |
2488 | if (printerIntf) | |
fe750b86 | 2489 | { |
f1ebe30e MS |
2490 | deviceIDString = copy_printer_interface_deviceid(printerIntf, g.alternateSetting); |
2491 | (*printerIntf)->Release(printerIntf); | |
fe750b86 MS |
2492 | } |
2493 | } | |
2494 | ||
e38f5e9c | 2495 | |
09a101d6 | 2496 | if (deviceIDString) |
2497 | { | |
fe750b86 MS |
2498 | if (CFStringGetCString(deviceIDString, data, *datalen, kCFStringEncodingUTF8)) |
2499 | *datalen = (int)strlen(data); | |
2500 | else | |
2501 | *datalen = 0; | |
de3edeba | 2502 | |
09a101d6 | 2503 | CFRelease(deviceIDString); |
f7deaa1a | 2504 | } |
fe750b86 MS |
2505 | else |
2506 | { | |
2507 | *datalen = 0; | |
2508 | } | |
2509 | ||
09a101d6 | 2510 | *status = CUPS_SC_STATUS_OK; |
f7deaa1a | 2511 | } |
2512 | ||
f7deaa1a | 2513 | |
9fb65b89 MS |
2514 | static void |
2515 | log_usb_class_driver(int is_64bit) /* I - Is the USB class driver 64-bit? */ | |
2516 | { | |
2517 | /* | |
2518 | * Report the usage of legacy USB class drivers to Apple if the user opts into providing | |
2519 | * feedback to Apple... | |
2520 | */ | |
2521 | ||
2522 | aslmsg aslm = asl_new(ASL_TYPE_MSG); | |
2523 | if (aslm) | |
2524 | { | |
2525 | ppd_file_t *ppd = ppdOpenFile(getenv("PPD")); | |
2526 | const char *make_model = ppd ? ppd->nickname : NULL; | |
2527 | ppd_attr_t *version = ppdFindAttr(ppd, "FileVersion", ""); | |
2528 | ||
2529 | asl_set(aslm, "com.apple.message.domain", "com.apple.printing.usb.64bit"); | |
2530 | asl_set(aslm, "com.apple.message.result", is_64bit ? "yes" : "no"); | |
2531 | asl_set(aslm, "com.apple.message.signature", make_model ? make_model : "Unknown"); | |
2532 | asl_set(aslm, "com.apple.message.signature2", version ? version->value : "?.?"); | |
2533 | asl_set(aslm, "com.apple.message.summarize", "YES"); | |
b846cf01 | 2534 | asl_log(NULL, aslm, ASL_LEVEL_NOTICE, ""); |
9fb65b89 MS |
2535 | asl_free(aslm); |
2536 | } | |
2537 | } | |
2538 | ||
2539 | ||
f7deaa1a | 2540 | /* |
f2d18633 | 2541 | * End of "$Id$". |
ef416fc2 | 2542 | */ |