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