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