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