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