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