#include #include #include #include typedef IOUSBDeviceInterface320 usb_device_t; const CFUUIDRef DeviceInterfaceID=kIOUSBDeviceInterfaceID320; const int DeviceVersion=320; typedef IOCFPlugInInterface *io_cf_plugin_ref_t; static mach_port_t libusb_darwin_mp = 0; /* master port */ void init() { int kresult = IOMasterPort (MACH_PORT_NULL, &libusb_darwin_mp); printf("IOCreatePlugInInterfaceForService-> kresult=%d\n", kresult); } void deinit() { mach_port_deallocate(mach_task_self(), libusb_darwin_mp); } void enumusb() { io_iterator_t deviceIterator; int rc= IOServiceGetMatchingServices(libusb_darwin_mp, IOServiceMatching(kIOUSBDeviceClassName), &deviceIterator); printf("IOServiceGetMatchingServices-> rc=%d\n", rc); if (!IOIteratorIsValid (deviceIterator)) return ; io_service_t usbDevice; while ((usbDevice = IOIteratorNext(deviceIterator))) { SInt32 score; io_cf_plugin_ref_t *plugInInterface = NULL; int result = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score); printf("IOCreatePlugInInterfaceForServiceres=%d score=%d\n", result, score); IOObjectRelease(usbDevice); if (kIOReturnSuccess == result && plugInInterface) { usb_device_t **device; (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(DeviceInterfaceID), (void**)&device); (*plugInInterface)->Stop(plugInInterface); IODestroyPlugInInterface (plugInInterface); UInt32 location; (*(device))->GetLocationID(device, &location); UInt16 address, idVendor, idProduct; UInt8 bDeviceClass, bDeviceSubClass; (*(device))->GetDeviceAddress (device, (USBDeviceAddress *)&address); (*(device))->GetDeviceProduct (device, &idProduct); (*(device))->GetDeviceVendor (device, &idVendor); (*(device))->GetDeviceClass (device, &bDeviceClass); (*(device))->GetDeviceSubClass (device, &bDeviceSubClass); printf("%08x %04x %04x %04x %02x %02x\n", location, address, idVendor, idProduct, bDeviceClass, bDeviceSubClass); (*(device))->Release(device); } } IOObjectRelease(deviceIterator); } usb_device_t *finddevice(UInt16 find_idVendor, UInt16 find_idProduct) { io_iterator_t deviceIterator; int rc= IOServiceGetMatchingServices(libusb_darwin_mp, IOServiceMatching(kIOUSBDeviceClassName), &deviceIterator); printf("IOServiceGetMatchingServices-> rc=%d\n", rc); if (!IOIteratorIsValid (deviceIterator)) return 0; io_service_t usbDevice; while ((usbDevice = IOIteratorNext(deviceIterator))) { SInt32 score; io_cf_plugin_ref_t *plugInInterface = NULL; int result = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score); printf("IOCreatePlugInInterfaceForServiceres=%d score=%d\n", result, score); IOObjectRelease(usbDevice); if (kIOReturnSuccess == result && plugInInterface) { usb_device_t **device; (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(DeviceInterfaceID), (void**)&device); (*plugInInterface)->Stop(plugInInterface); IODestroyPlugInInterface (plugInInterface); UInt32 location; (*(device))->GetLocationID(device, &location); UInt16 address, idVendor, idProduct; UInt8 bDeviceClass, bDeviceSubClass; (*(device))->GetDeviceAddress (device, (USBDeviceAddress *)&address); (*(device))->GetDeviceProduct (device, &idProduct); (*(device))->GetDeviceVendor (device, &idVendor); (*(device))->GetDeviceClass (device, &bDeviceClass); (*(device))->GetDeviceSubClass (device, &bDeviceSubClass); printf("%08x %04x %04x %04x %02x %02x\n", location, address, idVendor, idProduct, bDeviceClass, bDeviceSubClass); if (idProduct==find_idProduct && idVendor==find_idVendor) return *device; (*(device))->Release(device); } } IOObjectRelease(deviceIterator); return 0; }