/* UVC device enumeration Copyright (c) 2011,12 Simon Urbanek All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 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. * Neither the name of the author, associated companies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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. */ #import "UVCDevice.h" /* video interface class */ #define CC_VIDEO 0x0E /* video interface subclasses */ #define SC_VIDEOCONTROL 0x01 /* helper function - takes the device interface and queries it for CC_VIDEO:SC_VIDEOCONTROL interface */ static IOUSBInterfaceInterface220** getControlInterfaceWithDeviceInterface(IOUSBDeviceInterface ** deviceInterface) { IOUSBInterfaceInterface220 **controlInterface; io_iterator_t interfaceIterator; IOUSBFindInterfaceRequest interfaceRequest; interfaceRequest.bInterfaceClass = CC_VIDEO; interfaceRequest.bInterfaceSubClass = SC_VIDEOCONTROL; interfaceRequest.bInterfaceProtocol = kIOUSBFindInterfaceDontCare; interfaceRequest.bAlternateSetting = kIOUSBFindInterfaceDontCare; IOReturn success = (*deviceInterface)->CreateInterfaceIterator( deviceInterface, &interfaceRequest, &interfaceIterator ); if( success != kIOReturnSuccess ) return NULL; io_service_t usbInterface; HRESULT result; /* pick the first video control interface */ if ((usbInterface = IOIteratorNext(interfaceIterator))) { IOCFPlugInInterface **plugInInterface = NULL; //Create an intermediate plug-in SInt32 score; kern_return_t kr = IOCreatePlugInInterfaceForService( usbInterface, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score ); //Release the usbInterface object after getting the plug-in kr = IOObjectRelease(usbInterface); if( (kr != kIOReturnSuccess) || !plugInInterface ) { NSLog( @"UVCDevice Error: Unable to create a plug-in (%08x)\n", kr ); return NULL; } //Now create the device interface for the interface result = (*plugInInterface)->QueryInterface( plugInInterface, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID *) &controlInterface ); //No longer need the intermediate plug-in (*plugInInterface)->Release(plugInInterface); if( result || !controlInterface ) { NSLog( @"UVCDevice Error: Couldn’t create a device interface for the interface (%08x)", (int) result ); return NULL; } return controlInterface; } return NULL; } @implementation UVCDevice + (NSArray*) findUVCDevices { NSMutableArray *devices = [[NSMutableArray alloc] init]; CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName); io_iterator_t serviceIterator; IOServiceGetMatchingServices( kIOMasterPortDefault, matchingDict, &serviceIterator ); io_service_t camera; while ((camera = IOIteratorNext(serviceIterator))) { // Get DeviceInterface unsigned char deviceClass, deviceSubClass, deviceProtocol; BOOL valid = NO; IOUSBDeviceInterface **deviceInterface = NULL; IOCFPlugInInterface **plugInInterface = NULL; SInt32 score; kern_return_t kr = IOCreatePlugInInterfaceForService( camera, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score ); if( (kIOReturnSuccess != kr) || !plugInInterface ) { NSLog( @"UVCDevice Error: IOCreatePlugInInterfaceForService returned 0x%08x.", kr ); continue; } HRESULT res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*) &deviceInterface ); (*plugInInterface)->Release(plugInInterface); if( res || deviceInterface == NULL ) { NSLog( @"UVCDevice Error: QueryInterface returned %d.\n", (int)res ); continue; } /* accodring to the standard the class/subclass/protocol for video devices * is 0xEF, 0x02, 0x01 for devices with more than VC and 0x00, *, * for * those with only VC -- so we check that to avoid too much interface querying */ (*deviceInterface)->GetDeviceClass(deviceInterface, &deviceClass); if (deviceClass == 0xEF) { (*deviceInterface)->GetDeviceSubClass(deviceInterface, &deviceSubClass); (*deviceInterface)->GetDeviceProtocol(deviceInterface, &deviceProtocol); if (deviceSubClass == 2 && deviceProtocol == 1) valid = YES; } else if (deviceClass == 0) valid = YES; if (valid) { /* time to check the interfaces */ IOUSBInterfaceInterface220 **interface = getControlInterfaceWithDeviceInterface(deviceInterface); if (interface) { (*interface)->Release(interface); [devices addObject: [UVCDevice deviceWithDeviceInterface: deviceInterface]]; } else (*deviceInterface)->Release(deviceInterface); } else (*deviceInterface)->Release(deviceInterface); } return devices; } + (UVCDevice*) deviceWithDeviceInterface: (IOUSBDeviceInterface**) dev { return (UVCDevice*) [[[UVCDevice alloc] initWithDeviceInterface: dev] autorelease]; } - (id) initWithDeviceInterface: (IOUSBDeviceInterface**) dev { if ((self = [super init])) { deviceInterface = dev; } return self; } - (void)dealloc { if (deviceInterface) (*deviceInterface)->Release(deviceInterface); [super dealloc]; } - (UVCCameraControl*) cameraControl { UVCCameraControl *ctrl = [[UVCCameraControl alloc] initWithDeviceInterface: deviceInterface]; if (ctrl) [ctrl autorelease]; return ctrl; } - (NSString*) description { if (deviceInterface) { UInt16 devVendor = 0, devProduct = 0; UInt8 devClass = 0, devSubClass = 0, devProtocol = 0; (*deviceInterface)->GetDeviceVendor(deviceInterface, &devVendor); (*deviceInterface)->GetDeviceProduct(deviceInterface, &devProduct); (*deviceInterface)->GetDeviceProtocol(deviceInterface, &devProtocol); (*deviceInterface)->GetDeviceClass(deviceInterface, &devClass); (*deviceInterface)->GetDeviceSubClass(deviceInterface, &devSubClass); return [NSString stringWithFormat: @"UVCDevice<%p> (vendor:product=%04x:%04x, class=%02x:%02x:%02x)", self, devVendor, devProduct, devClass, devSubClass, devProtocol]; } return [NSString stringWithFormat: @"UVCDevice<%p> (NULL interface!)", self]; } @end