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