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