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