File: osx-hplj-hotplug.m

package info (click to toggle)
foo2zjs 20171202dfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,320 kB
  • sloc: ansic: 40,789; xml: 12,512; sh: 7,385; makefile: 1,705; objc: 573; tcl: 173; perl: 102; python: 8
file content (698 lines) | stat: -rw-r--r-- 20,239 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
AUTHORS
	Anon		<neobisc8@gmail.com>
	Rick Richardson <rick.richardson@comcast.net>
	From: /Developer/Examples/IOKit/usb

LICENSE
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

static char Version[] = "$Id: osx-hplj-hotplug.m,v 1.14 2011/07/22 18:56:58 rick Exp $";

#import <Cocoa/Cocoa.h>
#include <CoreFoundation/CoreFoundation.h>
#include <time.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOMessage.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>

/*
 * Command line options
 */
int	Debug = 0;

typedef struct
{
    long	vid, pid;
    char	*path;
} HOTPLUG;

HOTPLUG HotPlug[] =
{
    0x03f0,	0x0517,		"/usr/share/foo2zjs/firmware/sihp1000.dl",
    0x03f0,	0x1317,		"/usr/share/foo2zjs/firmware/sihp1005.dl",
    0x03f0,	0x4117,		"/usr/share/foo2zjs/firmware/sihp1018.dl",
    0x03f0,	0x2b17,		"/usr/share/foo2zjs/firmware/sihp1020.dl",

    0x03f0,	0x3d17,		"/usr/share/foo2xqx/firmware/sihpP1005.dl",
    0x03f0,	0x3e17,		"/usr/share/foo2xqx/firmware/sihpP1006.dl",
    0x03f0,	0x4817,		"/usr/share/foo2xqx/firmware/sihpP1005.dl",
    0x03f0,	0x4917,		"/usr/share/foo2xqx/firmware/sihpP1006.dl",
    0x03f0,	0x3f17,		"/usr/share/foo2xqx/firmware/sihpP1505.dl",
    0,		0,		NULL,	// Must be last
};

#define waitTime 8		// time to wait until firmware loads after
				// printer plugged in.

typedef struct MyPrivateData {
    io_object_t			notification;
    IOUSBDeviceInterface	**deviceInterface;
    CFStringRef			deviceName;
    HOTPLUG			*hp;
} MyPrivateData;

static IONotificationPortRef	gNotifyPort;
static io_iterator_t		gAddedIter;
static CFRunLoopRef		gRunLoop;

void
usage(void)
{
    fprintf(stderr,
"Usage:\n"
"	osx-hplj-hotplug [options]\n"
"\n"
"	Daemon which watches for HP LJ 1000, 1005, 1018, 1020, P1005, P1006,\n"
"	P1007, P1008, and P1505 being plugged in.  If so, then the firmware\n"
"	is downloaded to it.\n"
    );

    exit(1);
}

void
debug(int level, char *fmt, ...)
{
    va_list ap;

    if (Debug < level)
        return;

    setvbuf(stderr, (char *) NULL, _IOLBF, BUFSIZ);
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
}

void
error(int fatal, char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);

    if (fatal)
        exit(fatal);
}

void
MyCallBackFunction(void *dummy, IOReturn result, void *arg0)
{
    //  UInt8       inPipeRef = (UInt32)dummy;

    debug(1, "MyCallbackfunction: %ld, %d, %ld\n",
	(long)dummy, (int)result, (long)arg0);
    CFRunLoopStop(CFRunLoopGetCurrent());
}

void
transferData(IOUSBInterfaceInterface **intf,
		    UInt8 inPipeRef, UInt8 outPipeRef, MyPrivateData *mpd)
{
    IOReturn                    err;
    CFRunLoopSourceRef          cfSource;
    int                         i;
    char                    outBuf[8096];
    FILE *fp;

    err = (*intf)->CreateInterfaceAsyncEventSource(intf, &cfSource);
    if (err)
    {
        error(0, "transferData: can't create event source, err = %08x\n", err);
        return;
    }
    CFRunLoopAddSource(CFRunLoopGetCurrent(), cfSource, kCFRunLoopDefaultMode);

    fp = fopen(mpd->hp->path, "r");
    if (fp)
    {
	char buf[256*1024];
	int len;

	while ( (len = fread(buf, 1, sizeof(buf), fp)) != 0)
	{
	     err = (*intf)->WritePipeAsync(intf, outPipeRef, buf, len,
		(IOAsyncCallback1)MyCallBackFunction, (void*)(long)inPipeRef);

	    if (err)
	    {
		printf("transferData: WritePipeAsyncFailed, err = %08x\n", err);
		CFRunLoopRemoveSource(CFRunLoopGetCurrent(), cfSource,
		    kCFRunLoopDefaultMode);
		return;
	    }
	}
	fclose(fp);
    }
    debug(1, "transferData: calling CFRunLoopRun\n");
    CFRunLoopRun();
    debug(1, "transferData: returned from  CFRunLoopRun\n");
    CFRunLoopRemoveSource(CFRunLoopGetCurrent(), cfSource,
	kCFRunLoopDefaultMode);
}

void
dealWithPipes(IOUSBInterfaceInterface **intf, UInt8 numPipes,
		MyPrivateData  *mpd)
{
    int		i;
    IOReturn    err;
    UInt8       inPipeRef = 0;
    UInt8       outPipeRef = 0;
    UInt8       direction, number, transferType, interval;
    UInt16      maxPacketSize;
   
    // pipes are one based, since zero is the default control pipe
    for (i=1; i <= numPipes; i++)
    {
        err = (*intf)->GetPipeProperties(intf, i,
		&direction, &number, &transferType, &maxPacketSize, &interval);
        if (err)
        {
            error(0, "dealWithPipes: can't get pipe properties for pipe %d,"
		    " err = %08x\n", i, err);
            return;
        }
        if (transferType != kUSBBulk)
        {
            printf("dealWithPipes: skipping pipe %d: it is not a bulk pipe\n",
		    i);
            continue;
        }
        if ((direction == kUSBIn) && !inPipeRef)
        {
            debug(1, "dealWithPipes: grabbing BULK IN pipe index %d, num %d\n",
		    i, number);
            inPipeRef = i;
        }
        if ((direction == kUSBOut) && !outPipeRef)
        {
            debug(1, "dealWithPipes: grabbing BULK OUT pipe index %d, num %d\n",
		    i, number);
            outPipeRef = i;
        }
    }
    if (inPipeRef && outPipeRef)
      transferData(intf, inPipeRef, outPipeRef, mpd);
}

IOReturn
GetDeviceID(IOUSBInterfaceInterface **intf, char *buf, int maxlen)
{
    IOUSBDevRequest req;
    IOReturn err;
    int	length;

    // This class-specific request returns a device ID string that is
    // compatible with IEEE 1284. See IEEE 1284 for syntax and formatting
    // information. A printer with multiple configurations, interfaces, or
    // alternate settings may contain multiple IEEE 1284 device ID strings.
    // The wValue field is used to specify a zero-based configuration index.
    // The high-byte of the wIndex field is used to specify the zero-based
    // interface index. The low-byte of the wIndex field is used to specify
    // the zero-based alternate setting. The device ID string is returned
    // in the following format:
    // IEEE 1284 device ID string (including length in the first two bytes
    // in big endian format).

    req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBClass, kUSBInterface);
    req.bRequest = 0;		// GetDeviceID
    req.wValue = 0;		// see above
    req.wIndex = 0;		// see above
    req.wLength = maxlen;
    req.pData = buf;

    err = (*intf)->ControlRequest(intf, 0, &req);
    if (err)
    {
	buf[0] = 0;
	return(err);
    }

    length = (((unsigned)buf[0] & 255) << 8) + ((unsigned)buf[1] & 255);

    /*
     * Check to see if the length is larger than our buffer; first
     * assume that the vendor incorrectly implemented the 1284 spec,
     * and then limit the length to the size of our buffer...
     */
    if (length > (maxlen - 2))
	length = (((unsigned)buf[1] & 255) << 8) + ((unsigned)buf[0] & 255);

    if (length > (maxlen - 2))
	length = maxlen - 2;

    memmove(buf, buf + 2, length);
    buf[length] = '\0';

    return (err);
}

void
dealWithInterface(io_service_t usbInterfaceRef, MyPrivateData *mpd)
{
    IOReturn                    err;
    IOCFPlugInInterface         **iodev;	// requires <IOKit/IOCFPlugIn.h>
    IOUSBInterfaceInterface	**intf;
    SInt32                      score;
    UInt8                       numPipes;
    char			buf[256];

    err = IOCreatePlugInInterfaceForService(usbInterfaceRef,
	kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID,
	&iodev, &score);
    if (err || !iodev)
    {
	error(0, "dealWithIface: can't create plugin. ret = %08x, iodev = %p\n",
	    err, iodev);
	return;
    }
    err = (*iodev)->QueryInterface(iodev,
	CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID)&intf);
    IODestroyPlugInInterface(iodev);	// done with this

    if (err || !intf)
    {
	error(0, "dealWithIface: can't create an iface. ret=%08x, intf=%p\n",
		err, intf);
	return;
    }

    err = (*intf)->USBInterfaceOpen(intf);
    if (err)
    {
	error(0, "dealWithInterface: can't open interface. ret = %08x\n", err);
	return;
    }

    err = (*intf)->GetNumEndpoints(intf, &numPipes);
    if (err)
    {
	error(0, "dealWithInterface: can't get number of endpoints. rc=%08x\n",
		 err);
	(*intf)->USBInterfaceClose(intf);
	(*intf)->Release(intf);
	return;
    }

    err = GetDeviceID(intf, buf, sizeof(buf));
    if (err)
    {
	error(0, "dealWithInterface: can't get device id. ret = %08x\n",
		 err);
	(*intf)->USBInterfaceClose(intf);
	(*intf)->Release(intf);
	return;
    }

    // If FWVER is present, then no download is needed
    printf("GetDeviceID: %s\n", buf);
    if (strstr(buf, ";FWVER:"))
    {
	(*intf)->USBInterfaceClose(intf);
	(*intf)->Release(intf);
	return;
    }

    debug(1, "dealWithInterface: found %d pipes\n", numPipes);
    if (numPipes == 0)
    {
	// try alternate setting 1
	err = (*intf)->SetAlternateInterface(intf, 1);
	if (err)
	{
	    error(0, "dealWithInterface: can't set alt interface 1. rc=%08x\n",
		 err);
	    (*intf)->USBInterfaceClose(intf);
	    (*intf)->Release(intf);
	    return;
	}
	err = (*intf)->GetNumEndpoints(intf, &numPipes);
	if (err)
	{
	    error(0, "dealWithInterface: can't get number of endpoints - "
		    "alt setting 1. rc = %08x\n", err);
	    (*intf)->USBInterfaceClose(intf);
	    (*intf)->Release(intf);
	    return;
	}
	// workaround. GetNumEndpoints does not work after SetAlternateInterface
	numPipes = 13;
    }

    if (numPipes)
        dealWithPipes(intf, numPipes, mpd);

    err = (*intf)->USBInterfaceClose(intf);
    if (err)
    {
	error(0, "dealWithInterface: can't close interface. rc=%08x\n", err);
	return;
    }
    err = (*intf)->Release(intf);
    if (err)
    {
	error(0, "dealWithInterface: can't release interface. rc=%08x\n", err);
	return;
    }
}


IOReturn
ConfigureDevice(IOUSBDeviceInterface **dev)
{
    UInt8                               numConf;
    IOReturn                            rc;
    IOUSBConfigurationDescriptorPtr     confDesc;
   
    rc = (*dev)->GetNumberOfConfigurations(dev, &numConf);
    if (!numConf)
        return -1;

    // get the configuration descriptor for index 0
    rc = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &confDesc);
    if (rc)
    {
        error(0, "unable to get config descriptor for index %d (rc=%08x)\n",
	     0, rc);
        return -1;
    }

    rc = (*dev)->SetConfiguration(dev, confDesc->bConfigurationValue);
    if (rc)
    {
        error(0, "unable to set configuration to value %d (err=%08x)\n", 0, rc);
        return -1;
    }
   
    return kIOReturnSuccess;
}

// ============================================================================
// 
// DeviceNotification
// 
// This routine will get called whenever any kIOGeneralInterest notification
// happens.  We are
// interested in the kIOMessageServiceIsTerminated message so that's what we
// look for.  Other
// messages are defined in IOMessage.h.
// 
// ============================================================================
void
DeviceNotification(void *refCon, io_service_t service, natural_t messageType,
		   void *messageArgument)
{
    kern_return_t   rc;
    MyPrivateData  *privateDataRef = (MyPrivateData *) refCon;

    if (messageType == kIOMessageServiceIsTerminated)
    {
	printf("LaserJet unplugged...\n");

	// Dump our private data to stderr just to see what it looks like.
	CFShow(privateDataRef->deviceName);

	// Free the data we're no longer using now that the device is going away
	CFRelease(privateDataRef->deviceName);

	if (privateDataRef->deviceInterface)
	{
	    rc = (*privateDataRef->deviceInterface)->
			    Release(privateDataRef->deviceInterface);
	}

	rc = IOObjectRelease(privateDataRef->notification);

	free(privateDataRef);
    }
}

// ============================================================================
// 
// DeviceAdded
// 
// This routine is the callback for our IOServiceAddMatchingNotification.  When 
// we get called
// we will look at all the devices that were added and we will:
// 
// 1.  Create some private data to relate to each device (in this case we use
// the service's name
// and the location ID of the device
// 2.  Submit an IOServiceAddInterestNotification of type kIOGeneralInterest
// for this device,
// using the refCon field to store a pointer to our private data.  When we get
// called with
// this interest notification, we can grab the refCon and access our private
// data.
// 
// ============================================================================
void
DeviceFound(void *refCon, io_iterator_t iterator)
{
    kern_return_t	kr;
    io_service_t	usbDevice;
    IOCFPlugInInterface **plugInInterface = NULL;
    SInt32		score;
    HRESULT		res;
    char		buf[512];
    HOTPLUG		*hp = (HOTPLUG *) refCon;

    while ((usbDevice = IOIteratorNext(iterator)))
    {
	io_name_t       deviceName;
	CFStringRef     deviceNameAsCFString;
	MyPrivateData  *privateDataRef = NULL;
	UInt32          locationID;
	int             t1 = 0;
	int             t2 = 0;
	int             timer = 0;

	printf("LaserJet %s Found!\n", hp->path);
	sleep(waitTime);

	// Add some app-specific information about this device.
	// Create a buffer to hold the data.
	privateDataRef = malloc(sizeof(MyPrivateData));
	bzero(privateDataRef, sizeof(MyPrivateData));

	// Save the HOTPLUG entry
	privateDataRef->hp = hp;

	// Get the USB device's name.
	kr = IORegistryEntryGetName(usbDevice, deviceName);
	if (KERN_SUCCESS != kr)
	    deviceName[0] = '\0';

	deviceNameAsCFString =
	    CFStringCreateWithCString(kCFAllocatorDefault, deviceName,
				      kCFStringEncodingASCII);

	// Dump our data to stderr just to see what it looks like.
	// fprintf(stderr, "deviceName: ");
	CFShow(deviceNameAsCFString);

	// Save the device's name to our private data.  
	privateDataRef->deviceName = deviceNameAsCFString;

	// Now, get the locationID of this device. In order to do this, we need 
	// to create an IOUSBDeviceInterface 
	// for our device. This will create the necessary connections between
	// our userland application and the 
	// kernel object for the USB Device.
	kr = IOCreatePlugInInterfaceForService(usbDevice,
					       kIOUSBDeviceUserClientTypeID,
					       kIOCFPlugInInterfaceID,
					       &plugInInterface, &score);

	if ((kIOReturnSuccess != kr) || !plugInInterface)
	{
	    // fprintf(stderr, "IOCreatePlugInInterfaceForService returned
	    // 0x%08x.\n", kr);
	    continue;
	}

	// Use the plugin interface to retrieve the device interface.
	res = (*plugInInterface)->QueryInterface(
		    plugInInterface,
		    CFUUIDGetUUIDBytes
		    (kIOUSBDeviceInterfaceID),
		    (LPVOID *) & privateDataRef->deviceInterface);

	// Now done with the plugin interface.
	(*plugInInterface)->Release(plugInInterface);

	if (res || privateDataRef->deviceInterface == NULL)
	{
	    // fprintf(stderr, "QueryInterface returned %d.\n", (int) res);
	    continue;
	}

	{
	    IOUSBFindInterfaceRequest           interfaceRequest;
	    IOReturn                                            err;
	    io_iterator_t                                       iterator;
	    io_service_t                                        usbInterfaceRef;
	    IOUSBDeviceInterface        **dev = NULL;
	    dev = privateDataRef->deviceInterface;

	    interfaceRequest.bInterfaceClass = kIOUSBFindInterfaceDontCare;
	    interfaceRequest.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
	    interfaceRequest.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
	    interfaceRequest.bAlternateSetting = kIOUSBFindInterfaceDontCare;

	    err = (*dev)->CreateInterfaceIterator(dev, &interfaceRequest,
			    &iterator);
	    if (err)
	    {
		error(0, "dealWithDevice: can't create interface iterator\n");
		(*dev)->USBDeviceClose(dev);
		(*dev)->Release(dev);
		return;
	    }
	   
	    while ( (usbInterfaceRef = IOIteratorNext(iterator)) )
	    {
		debug(1, "found interface: %ld\n", (long)usbInterfaceRef);
		dealWithInterface(usbInterfaceRef, privateDataRef);
		IOObjectRelease(usbInterfaceRef);
	    }
	   
	    IOObjectRelease(iterator);
	    iterator = 0;
	}

	// Register for an interest notification of this device being removed.
	// Use a reference to our
	// private data as the refCon which will be passed to the notification
	// callback.
	kr = IOServiceAddInterestNotification(
		    gNotifyPort,			// notifyPort
		    usbDevice,				// service
		    kIOGeneralInterest,			// interestType
		    DeviceNotification,			// callback
		    privateDataRef,			// refCon
		    &(privateDataRef->notification)	// notification
	    );

	if (KERN_SUCCESS != kr)
	{
	    // printf("IOServiceAddInterestNotification returned 0x%08x.\n",
	    // kr);
	}

	// Done with this USB device; release the reference added by
	// IOIteratorNext
	kr = IOObjectRelease(usbDevice);
    }
}

// ============================================================================
// main
// ============================================================================

int
main(int argc, char *argv[])
{
    CFMutableDictionaryRef	matchingDict;
    CFRunLoopSourceRef		runLoopSource;
    CFNumberRef			numberRef;
    kern_return_t		kr;
    HOTPLUG			*hp;
    int				c;

    while ( (c = getopt(argc, argv, "D:V:?h")) != EOF)
	switch (c)
	{
        case 'D':       Debug = atoi(optarg); break;
        case 'V':       printf("%s\n", Version); exit(0);
        default:        usage(); exit(1);
	}

    argc -= optind;
    argv += optind;

    for (hp = &HotPlug[0]; hp->path != NULL; ++hp)
    {
	// Interested in instances of class
	matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
     
	// IOUSBDevice and its subclasses
	if (matchingDict == NULL)
	{
	    // fprintf(stderr, "IOServiceMatching returned NULL.\n");
	    return -1;
	}

	// We are interested in all USB devices (as opposed to USB interfaces).
	// The Common Class Specification
	// tells us that we need to specify the idVendor, idProduct, and
	// bcdDevice fields, or, if we're not interested
	// in particular bcdDevices, just the idVendor and idProduct.
	// Note that if we were trying to match an 
	// IOUSBInterface, we would need to set more values in the matching
	// dictionary (e.g. idVendor, idProduct, bInterfaceNumber,
	// and bConfigurationValue.

	// Create a CFNumber for the VID and set the value in the dictionary
	numberRef =
	    CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &hp->vid);
	CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID), numberRef);
	CFRelease(numberRef);

	// Create a CFNumber for the PID and set the value in the dictionary
	numberRef =
	    CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &hp->pid);
	CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID), numberRef);
	CFRelease(numberRef);
	numberRef = NULL;

	// Create a notification port and add its run loop event source to our
	// run loop.  This is how async notifications get set up.
	gNotifyPort = IONotificationPortCreate(kIOMasterPortDefault);
	runLoopSource = IONotificationPortGetRunLoopSource(gNotifyPort);

	gRunLoop = CFRunLoopGetCurrent();
	CFRunLoopAddSource(gRunLoop, runLoopSource, kCFRunLoopDefaultMode);

	// Now set up a notification to be called when a device is first
	// matched by I/O Kit.
	kr = IOServiceAddMatchingNotification(
		gNotifyPort,			// notifyPort
		kIOFirstMatchNotification,	// notificationType
		matchingDict,			// matching
		DeviceFound,			// callback
		hp,				// refCon
		&gAddedIter			// notification
	    );

	// Iterate once to get already-present devices and arm the notification 
	DeviceFound(hp, gAddedIter);
    }

    // Start the run loop. Now we'll receive notifications.
    printf("Waiting for LaserJet Printer Firmware DL...\n\n");
    CFRunLoopRun();

    // We should never get here
    error(0, "Unexpectedly back from CFRunLoopRun()!\n");
    return 0;
}