-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotplug_macosx.c
857 lines (731 loc) · 21.3 KB
/
hotplug_macosx.c
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
* MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
*
* Copyright (C) 2002-2004
* Stephen M. Webb <stephenw@cryptocard.com>
* Copyright (C) 2002-2011
* Ludovic Rousseau <ludovic.rousseau@free.fr>
* Copyright (C) 2002
* David Corcoran <corcoran@musclecard.com>
* Copyright (C) 2003
* Antti Tapaninen
*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: hotplug_macosx.c 7004 2014-10-02 09:26:36Z rousseau $
*/
/**
* @file
* @brief This provides a search API for hot pluggble devices.
*/
#include <PCSC/config.h>
#include <PCSC/misc.h>
#include <PCSC/pcscd.h>
#if defined(__APPLE__) && !defined(HAVE_LIBUSB)
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
#include <stdlib.h>
#include <string.h>
#include "debuglog.h"
#include "parser.h"
#include "readerfactory.h"
#include "winscard_msg.h"
#include "utils.h"
#include "hotplug.h"
#undef DEBUG_HOTPLUG
/*
* An aggregation of useful information on a driver bundle in the
* drop directory.
*/
typedef struct HPDriver
{
UInt32 m_vendorId; /* unique vendor's manufacturer code */
UInt32 m_productId; /* manufacturer's unique product code */
char *m_friendlyName; /* bundle friendly name */
char *m_libPath; /* bundle's plugin library location */
} HPDriver, *HPDriverVector;
/*
* An aggregation on information on currently active reader drivers.
*/
typedef struct HPDevice
{
HPDriver *m_driver; /* driver bundle information */
UInt32 m_address; /* unique system address of device */
struct HPDevice *m_next; /* next device in list */
} HPDevice, *HPDeviceList;
/*
* Pointer to a list of (currently) known hotplug reader devices (and their
* drivers).
*/
static HPDeviceList sDeviceList = NULL;
/*
* A callback to handle the asynchronous appearance of new devices that are
* candidates for PCSC readers.
*/
static void HPDeviceAppeared(void *refCon, io_iterator_t iterator)
{
kern_return_t kret;
io_service_t obj;
(void)refCon;
while ((obj = IOIteratorNext(iterator)))
kret = IOObjectRelease(obj);
HPSearchHotPluggables();
}
/*
* A callback to handle the asynchronous disappearance of devices that are
* possibly PCSC readers.
*/
static void HPDeviceDisappeared(void *refCon, io_iterator_t iterator)
{
kern_return_t kret;
io_service_t obj;
(void)refCon;
while ((obj = IOIteratorNext(iterator)))
kret = IOObjectRelease(obj);
HPSearchHotPluggables();
}
/*
* Creates a vector of driver bundle info structures from the hot-plug driver
* directory.
*
* Returns NULL on error and a pointer to an allocated HPDriver vector on
* success. The caller must free the HPDriver with a call to
* HPDriversRelease().
*/
static HPDriverVector HPDriversGetFromDirectory(const char *driverBundlePath)
{
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_DEBUG, "Entering HPDriversGetFromDirectory: %s",
driverBundlePath);
#endif
int readersNumber = 0;
HPDriverVector bundleVector = NULL;
CFArrayRef bundleArray;
CFStringRef driverBundlePathString =
CFStringCreateWithCString(kCFAllocatorDefault,
driverBundlePath,
kCFStringEncodingMacRoman);
CFURLRef pluginUrl = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
driverBundlePathString,
kCFURLPOSIXPathStyle, TRUE);
CFRelease(driverBundlePathString);
if (!pluginUrl)
{
Log1(PCSC_LOG_ERROR, "error getting plugin directory URL");
return NULL;
}
bundleArray = CFBundleCreateBundlesFromDirectory(kCFAllocatorDefault,
pluginUrl, NULL);
if (!bundleArray)
{
Log1(PCSC_LOG_ERROR, "error getting plugin directory bundles");
return NULL;
}
CFRelease(pluginUrl);
size_t bundleArraySize = CFArrayGetCount(bundleArray);
size_t i;
/* get the number of readers (including aliases) */
for (i = 0; i < bundleArraySize; i++)
{
CFBundleRef currBundle =
(CFBundleRef) CFArrayGetValueAtIndex(bundleArray, i);
CFDictionaryRef dict = CFBundleGetInfoDictionary(currBundle);
const void * blobValue = CFDictionaryGetValue(dict,
CFSTR(PCSCLITE_HP_MANUKEY_NAME));
if (!blobValue)
{
Log1(PCSC_LOG_ERROR, "error getting vendor ID from bundle");
return NULL;
}
if (CFGetTypeID(blobValue) == CFArrayGetTypeID())
{
/* alias found, each reader count as 1 */
CFArrayRef propertyArray = blobValue;
readersNumber += CFArrayGetCount(propertyArray);
}
else
/* No alias, only one reader supported */
readersNumber++;
}
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_DEBUG, "Total of %d readers supported", readersNumber);
#endif
/* The last entry is an end marker (m_vendorId = 0)
* see checks in HPDriversMatchUSBDevices:503
* and HPDriverVectorRelease:376 */
readersNumber++;
bundleVector = calloc(readersNumber, sizeof(HPDriver));
if (!bundleVector)
{
Log1(PCSC_LOG_ERROR, "memory allocation failure");
return NULL;
}
HPDriver *driverBundle = bundleVector;
for (i = 0; i < bundleArraySize; i++)
{
CFBundleRef currBundle =
(CFBundleRef) CFArrayGetValueAtIndex(bundleArray, i);
CFDictionaryRef dict = CFBundleGetInfoDictionary(currBundle);
CFURLRef bundleUrl = CFBundleCopyBundleURL(currBundle);
CFStringRef bundlePath = CFURLCopyPath(bundleUrl);
driverBundle->m_libPath = strdup(CFStringGetCStringPtr(bundlePath,
CFStringGetSystemEncoding()));
const void * blobValue = CFDictionaryGetValue(dict,
CFSTR(PCSCLITE_HP_MANUKEY_NAME));
if (!blobValue)
{
Log1(PCSC_LOG_ERROR, "error getting vendor ID from bundle");
return bundleVector;
}
if (CFGetTypeID(blobValue) == CFArrayGetTypeID())
{
CFArrayRef vendorArray = blobValue;
CFArrayRef productArray;
CFArrayRef friendlyNameArray;
char *libPath = driverBundle->m_libPath;
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_DEBUG, "Driver with aliases: %s", libPath);
#endif
/* get list of ProductID */
productArray = CFDictionaryGetValue(dict,
CFSTR(PCSCLITE_HP_PRODKEY_NAME));
if (!productArray)
{
Log1(PCSC_LOG_ERROR, "error getting product ID from bundle");
return bundleVector;
}
/* get list of FriendlyName */
friendlyNameArray = CFDictionaryGetValue(dict,
CFSTR(PCSCLITE_HP_NAMEKEY_NAME));
if (!friendlyNameArray)
{
Log1(PCSC_LOG_ERROR, "error getting product ID from bundle");
return bundleVector;
}
int reader_nb = CFArrayGetCount(vendorArray);
if (reader_nb != CFArrayGetCount(productArray))
{
Log3(PCSC_LOG_ERROR,
"Malformed Info.plist: %d vendors and %ld products",
reader_nb, CFArrayGetCount(productArray));
return bundleVector;
}
if (reader_nb != CFArrayGetCount(friendlyNameArray))
{
Log3(PCSC_LOG_ERROR,
"Malformed Info.plist: %d vendors and %ld friendlynames",
reader_nb, CFArrayGetCount(friendlyNameArray));
return bundleVector;
}
int j;
for (j=0; j<reader_nb; j++)
{
CFStringRef strValue = CFArrayGetValueAtIndex(vendorArray, j);
driverBundle->m_vendorId = strtoul(CFStringGetCStringPtr(strValue,
CFStringGetSystemEncoding()), NULL, 16);
strValue = CFArrayGetValueAtIndex(productArray, j);
driverBundle->m_productId = strtoul(CFStringGetCStringPtr(strValue,
CFStringGetSystemEncoding()), NULL, 16);
strValue = CFArrayGetValueAtIndex(friendlyNameArray, j);
const char *cstr = CFStringGetCStringPtr(strValue,
CFStringGetSystemEncoding());
driverBundle->m_friendlyName = strdup(cstr);
if (!driverBundle->m_libPath)
driverBundle->m_libPath = strdup(libPath);
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_DEBUG, "VendorID: 0x%04X",
driverBundle->m_vendorId);
Log2(PCSC_LOG_DEBUG, "ProductID: 0x%04X",
driverBundle->m_productId);
Log2(PCSC_LOG_DEBUG, "Friendly name: %s",
driverBundle->m_friendlyName);
Log2(PCSC_LOG_DEBUG, "Driver: %s", driverBundle->m_libPath);
#endif
/* go to next bundle in the vector */
driverBundle++;
}
}
else
{
CFStringRef strValue = blobValue;
#ifdef DEBUG_HOTPLUG
Log3(PCSC_LOG_DEBUG, "Driver without alias: %s %s",
driverBundle->m_friendlyName, driverBundle->m_libPath);
#endif
driverBundle->m_vendorId = strtoul(CFStringGetCStringPtr(strValue,
CFStringGetSystemEncoding()), NULL, 16);
strValue = (CFStringRef) CFDictionaryGetValue(dict,
CFSTR(PCSCLITE_HP_PRODKEY_NAME));
if (!strValue)
{
Log1(PCSC_LOG_ERROR, "error getting product ID from bundle");
return bundleVector;
}
driverBundle->m_productId = strtoul(CFStringGetCStringPtr(strValue,
CFStringGetSystemEncoding()), NULL, 16);
strValue = (CFStringRef) CFDictionaryGetValue(dict,
CFSTR(PCSCLITE_HP_NAMEKEY_NAME));
if (!strValue)
{
Log1(PCSC_LOG_ERROR, "error getting product friendly name from bundle");
driverBundle->m_friendlyName = strdup("unnamed device");
}
else
{
const char *cstr = CFStringGetCStringPtr(strValue,
CFStringGetSystemEncoding());
driverBundle->m_friendlyName = strdup(cstr);
}
#ifdef DEBUG_HOTPLUG
Log2(PCSC_LOG_DEBUG, "VendorID: 0x%04X", driverBundle->m_vendorId);
Log2(PCSC_LOG_DEBUG, "ProductID: 0x%04X", driverBundle->m_productId);
Log2(PCSC_LOG_DEBUG, "Friendly name: %s", driverBundle->m_friendlyName);
Log2(PCSC_LOG_DEBUG, "Driver: %s", driverBundle->m_libPath);
#endif
/* go to next bundle in the vector */
driverBundle++;
}
}
CFRelease(bundleArray);
return bundleVector;
}
/*
* Copies a driver bundle instance.
*/
static HPDriver *HPDriverCopy(HPDriver * rhs)
{
if (!rhs)
return NULL;
HPDriver *newDriverBundle = calloc(1, sizeof(HPDriver));
if (!newDriverBundle)
return NULL;
newDriverBundle->m_vendorId = rhs->m_vendorId;
newDriverBundle->m_productId = rhs->m_productId;
newDriverBundle->m_friendlyName = strdup(rhs->m_friendlyName);
newDriverBundle->m_libPath = strdup(rhs->m_libPath);
return newDriverBundle;
}
/*
* Releases resources allocated to a driver bundle vector.
*/
static void HPDriverRelease(HPDriver * driverBundle)
{
if (driverBundle)
{
free(driverBundle->m_friendlyName);
free(driverBundle->m_libPath);
}
}
/*
* Releases resources allocated to a driver bundle vector.
*/
static void HPDriverVectorRelease(HPDriverVector driverBundleVector)
{
if (driverBundleVector)
{
HPDriver *b;
for (b = driverBundleVector; b->m_vendorId; ++b)
HPDriverRelease(b);
free(driverBundleVector);
}
}
/*
* Inserts a new reader device in the list.
*/
static HPDeviceList
HPDeviceListInsert(HPDeviceList list, HPDriver * bundle, UInt32 address)
{
HPDevice *newReader = calloc(1, sizeof(HPDevice));
if (!newReader)
{
Log1(PCSC_LOG_ERROR, "memory allocation failure");
return list;
}
newReader->m_driver = HPDriverCopy(bundle);
newReader->m_address = address;
newReader->m_next = list;
return newReader;
}
/*
* Frees resources allocated to a HPDeviceList.
*/
static void HPDeviceListRelease(HPDeviceList list)
{
HPDevice *p;
for (p = list; p; p = p->m_next)
HPDriverRelease(p->m_driver);
}
/*
* Compares two driver bundle instances for equality.
*/
static int HPDeviceEquals(HPDevice * a, HPDevice * b)
{
return (a->m_driver->m_vendorId == b->m_driver->m_vendorId)
&& (a->m_driver->m_productId == b->m_driver->m_productId)
&& (a->m_address == b->m_address);
}
/*
* Finds USB devices currently registered in the system that match any of
* the drivers detected in the driver bundle vector.
*/
static int
HPDriversMatchUSBDevices(HPDriverVector driverBundle,
HPDeviceList * readerList)
{
CFDictionaryRef usbMatch = IOServiceMatching("IOUSBDevice");
if (0 == usbMatch)
{
Log1(PCSC_LOG_ERROR,
"error getting USB match from IOServiceMatching()");
return 1;
}
io_iterator_t usbIter;
kern_return_t kret = IOServiceGetMatchingServices(kIOMasterPortDefault,
usbMatch, &usbIter);
if (kret != 0)
{
Log1(PCSC_LOG_ERROR,
"error getting iterator from IOServiceGetMatchingServices()");
return 1;
}
IOIteratorReset(usbIter);
io_object_t usbDevice = 0;
while ((usbDevice = IOIteratorNext(usbIter)))
{
char namebuf[1024];
kret = IORegistryEntryGetName(usbDevice, namebuf);
if (kret != 0)
{
Log1(PCSC_LOG_ERROR,
"error getting device name from IORegistryEntryGetName()");
return 1;
}
IOCFPlugInInterface **iodev;
SInt32 score;
kret = IOCreatePlugInInterfaceForService(usbDevice,
kIOUSBDeviceUserClientTypeID,
kIOCFPlugInInterfaceID, &iodev, &score);
if (kret != 0)
{
Log1(PCSC_LOG_ERROR, "error getting plugin interface from IOCreatePlugInInterfaceForService()");
return 1;
}
IOObjectRelease(usbDevice);
IOUSBDeviceInterface **usbdev;
HRESULT hres = (*iodev)->QueryInterface(iodev,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
(LPVOID *) & usbdev);
(*iodev)->Release(iodev);
if (hres)
{
Log1(PCSC_LOG_ERROR,
"error querying interface in QueryInterface()");
return 1;
}
UInt16 vendorId = 0;
UInt16 productId = 0;
UInt32 usbAddress = 0;
kret = (*usbdev)->GetDeviceVendor(usbdev, &vendorId);
kret = (*usbdev)->GetDeviceProduct(usbdev, &productId);
kret = (*usbdev)->GetLocationID(usbdev, &usbAddress);
(*usbdev)->Release(usbdev);
#ifdef DEBUG_HOTPLUG
Log4(PCSC_LOG_DEBUG, "Found USB device 0x%04X:0x%04X at 0x%X",
vendorId, productId, usbAddress);
#endif
HPDriver *driver;
for (driver = driverBundle; driver->m_vendorId; ++driver)
{
if ((driver->m_vendorId == vendorId)
&& (driver->m_productId == productId))
{
#ifdef DEBUG_HOTPLUG
Log4(PCSC_LOG_DEBUG, "Adding USB device %04X:%04X at 0x%X",
vendorId, productId, usbAddress);
#endif
*readerList =
HPDeviceListInsert(*readerList, driver, usbAddress);
}
}
}
IOObjectRelease(usbIter);
return 0;
}
/*
* Finds PC Card devices currently registered in the system that match any of
* the drivers detected in the driver bundle vector.
*/
static int
HPDriversMatchPCCardDevices(HPDriver * driverBundle,
HPDeviceList * readerList)
{
CFDictionaryRef pccMatch = IOServiceMatching("IOPCCard16Device");
if (pccMatch == NULL)
{
Log1(PCSC_LOG_ERROR,
"error getting PCCard match from IOServiceMatching()");
return 1;
}
io_iterator_t pccIter;
kern_return_t kret =
IOServiceGetMatchingServices(kIOMasterPortDefault, pccMatch,
&pccIter);
if (kret != 0)
{
Log1(PCSC_LOG_ERROR,
"error getting iterator from IOServiceGetMatchingServices()");
return 1;
}
IOIteratorReset(pccIter);
io_object_t pccDevice = 0;
while ((pccDevice = IOIteratorNext(pccIter)))
{
char namebuf[1024];
kret = IORegistryEntryGetName(pccDevice, namebuf);
if (kret != 0)
{
Log1(PCSC_LOG_ERROR, "error getting plugin interface from IOCreatePlugInInterfaceForService()");
return 1;
}
UInt32 vendorId = 0;
UInt32 productId = 0;
UInt32 pccAddress = 0;
CFTypeRef valueRef =
IORegistryEntryCreateCFProperty(pccDevice, CFSTR("VendorID"),
kCFAllocatorDefault, 0);
if (!valueRef)
{
Log1(PCSC_LOG_ERROR, "error getting vendor");
}
else
{
CFNumberGetValue((CFNumberRef) valueRef, kCFNumberSInt32Type,
&vendorId);
}
valueRef =
IORegistryEntryCreateCFProperty(pccDevice, CFSTR("DeviceID"),
kCFAllocatorDefault, 0);
if (!valueRef)
{
Log1(PCSC_LOG_ERROR, "error getting device");
}
else
{
CFNumberGetValue((CFNumberRef) valueRef, kCFNumberSInt32Type,
&productId);
}
valueRef =
IORegistryEntryCreateCFProperty(pccDevice, CFSTR("SocketNumber"),
kCFAllocatorDefault, 0);
if (!valueRef)
{
Log1(PCSC_LOG_ERROR, "error getting PC Card socket");
}
else
{
CFNumberGetValue((CFNumberRef) valueRef, kCFNumberSInt32Type,
&pccAddress);
}
HPDriver *driver = driverBundle;
for (; driver->m_vendorId; ++driver)
{
if ((driver->m_vendorId == vendorId)
&& (driver->m_productId == productId))
{
*readerList =
HPDeviceListInsert(*readerList, driver, pccAddress);
}
}
}
IOObjectRelease(pccIter);
return 0;
}
static void HPEstablishUSBNotification(void)
{
io_iterator_t deviceAddedIterator;
io_iterator_t deviceRemovedIterator;
CFMutableDictionaryRef matchingDictionary;
IONotificationPortRef notificationPort;
IOReturn kret;
notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notificationPort),
kCFRunLoopDefaultMode);
matchingDictionary = IOServiceMatching("IOUSBDevice");
if (!matchingDictionary)
{
Log1(PCSC_LOG_ERROR, "IOServiceMatching() failed");
}
matchingDictionary =
(CFMutableDictionaryRef) CFRetain(matchingDictionary);
kret = IOServiceAddMatchingNotification(notificationPort,
kIOMatchedNotification,
matchingDictionary, HPDeviceAppeared, NULL, &deviceAddedIterator);
if (kret)
{
Log2(PCSC_LOG_ERROR,
"IOServiceAddMatchingNotification()-1 failed with code %d", kret);
}
HPDeviceAppeared(NULL, deviceAddedIterator);
kret = IOServiceAddMatchingNotification(notificationPort,
kIOTerminatedNotification,
matchingDictionary,
HPDeviceDisappeared, NULL, &deviceRemovedIterator);
if (kret)
{
Log2(PCSC_LOG_ERROR,
"IOServiceAddMatchingNotification()-2 failed with code %d", kret);
}
HPDeviceDisappeared(NULL, deviceRemovedIterator);
}
static void HPEstablishPCCardNotification(void)
{
io_iterator_t deviceAddedIterator;
io_iterator_t deviceRemovedIterator;
CFMutableDictionaryRef matchingDictionary;
IONotificationPortRef notificationPort;
IOReturn kret;
notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(notificationPort),
kCFRunLoopDefaultMode);
matchingDictionary = IOServiceMatching("IOPCCard16Device");
if (!matchingDictionary)
{
Log1(PCSC_LOG_ERROR, "IOServiceMatching() failed");
}
matchingDictionary =
(CFMutableDictionaryRef) CFRetain(matchingDictionary);
kret = IOServiceAddMatchingNotification(notificationPort,
kIOMatchedNotification,
matchingDictionary, HPDeviceAppeared, NULL, &deviceAddedIterator);
if (kret)
{
Log2(PCSC_LOG_ERROR,
"IOServiceAddMatchingNotification()-1 failed with code %d", kret);
}
HPDeviceAppeared(NULL, deviceAddedIterator);
kret = IOServiceAddMatchingNotification(notificationPort,
kIOTerminatedNotification,
matchingDictionary,
HPDeviceDisappeared, NULL, &deviceRemovedIterator);
if (kret)
{
Log2(PCSC_LOG_ERROR,
"IOServiceAddMatchingNotification()-2 failed with code %d", kret);
}
HPDeviceDisappeared(NULL, deviceRemovedIterator);
}
/*
* Thread runner (does not return).
*/
static void HPDeviceNotificationThread(void)
{
HPEstablishUSBNotification();
HPEstablishPCCardNotification();
CFRunLoopRun();
}
/*
* Scans the hotplug driver directory and looks in the system for
* matching devices.
* Adds or removes matching readers as necessary.
*/
LONG HPSearchHotPluggables(void)
{
HPDriver *drivers = HPDriversGetFromDirectory(PCSCLITE_HP_DROPDIR);
if (!drivers)
return 1;
HPDeviceList devices = NULL;
if (HPDriversMatchUSBDevices(drivers, &devices))
return -1;
if (HPDriversMatchPCCardDevices(drivers, &devices))
return -1;
HPDevice *a;
for (a = devices; a; a = a->m_next)
{
int found = FALSE;
HPDevice *b;
for (b = sDeviceList; b; b = b->m_next)
{
if (HPDeviceEquals(a, b))
{
found = TRUE;
break;
}
}
if (!found)
{
char deviceName[MAX_DEVICENAME];
/* the format should be "usb:%04x/%04x" but Apple uses the
* friendly name instead */
snprintf(deviceName, sizeof(deviceName),
"%s", a->m_driver->m_friendlyName);
deviceName[sizeof(deviceName)-1] = '\0';
RFAddReader(a->m_driver->m_friendlyName,
PCSCLITE_HP_BASE_PORT + a->m_address, a->m_driver->m_libPath,
deviceName);
}
}
for (a = sDeviceList; a; a = a->m_next)
{
int found = FALSE;
HPDevice *b;
for (b = devices; b; b = b->m_next)
{
if (HPDeviceEquals(a, b))
{
found = TRUE;
break;
}
}
if (!found)
{
RFRemoveReader(a->m_driver->m_friendlyName,
PCSCLITE_HP_BASE_PORT + a->m_address);
}
}
HPDeviceListRelease(sDeviceList);
sDeviceList = devices;
HPDriverVectorRelease(drivers);
return 0;
}
pthread_t sHotplugWatcherThread;
/*
* Sets up callbacks for device hotplug events.
*/
ULONG HPRegisterForHotplugEvents(void)
{
ThreadCreate(&sHotplugWatcherThread,
THREAD_ATTR_DEFAULT,
(PCSCLITE_THREAD_FUNCTION( )) HPDeviceNotificationThread, NULL);
return 0;
}
LONG HPStopHotPluggables(void)
{
return 0;
}
void HPReCheckSerialReaders(void)
{
}
#endif /* __APPLE__ */