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