Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

CHAPTER 15: User-Space USB Drivers > The IOUSBInterfaceInterface Class - Pg. 369

CHAPTER 15 USER-SPACE USB DRIVERS Next, the application iterates over all interfaces in the active configuration. If we were interested in a particular interface, we could narrow down the list of interfaces returned by the iterator by specifying the desired class, subclass, protocol, or alternate setting for the interface. Obtaining a USB interface object is particularly important, since the only way an application can access the device's endpoints, other than the control endpoint, is through the IOUSBInterfaceInterface class. The IOUSBInterfaceInterface Class The IOUSBInterfaceInterface class is a user-space class that provides equivalent functionality to the IOUSBInterface class used by kernel drivers. An application can obtain a reference to an IOUSBInterfaceInterface class by iterating over the device's interfaces, as shown in Listing 15-5, or an application can obtain an IOUSBInterfaceInterface object directly by creating a matching dictionary that specifies the service name kIOUSBInterfaceClassName. A USB interface contains one or more endpoints, which allow data to either be written to the device or data to be read from the device. A user-space USB driver is not limited in any way regarding the type of endpoints it is able to use; all endpoint types, including bulk, isochronous, and interrupt endpoints are available to a user-space driver. A user-space driver is able to achieve similar data bandwidth to that of a kernel driver, meaning that even applications that require large data transfers can be written in user space. Whether an application uses the IOUSBDeviceInterface to iterate over USB interfaces or obtains a USB interface directly using a matching dictionary to create an iterator, the application will receive an io_service_t that provides a user-space representation of the underlying IOUSBInterface object in the kernel. Listing 15-6 demonstrates how to create an IOUSBInterfaceInterface object from the io_service_t object. Listing 15-6. Instantiating an IOUSBInterfaceInterface object from an io_service_t IOUSBInterfaceInterface300** MyCreateInterfaceClass (io_service_t usbInterfaceRef) { SInt32 score; IOCFPlugInInterface** plugin; IOUSBInterfaceInterface300** usbInterface = NULL; kern_return_t err; err = IOCreatePlugInInterfaceForService(usbInterfaceRef, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score); if (err == 0) { err = (*plugin)->QueryInterface(plugin, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID300), (LPVOID)&usbInterface); IODestroyPlugInInterface(plugin); } return usbInterface; } As with the USB device class, an application must obtain exclusive access to the USB interface object before it is able to transfer any data to or from an endpoint on the USB interface. This is achieved by calling the IOUSBInterfaceInterface method USBInterfaceOpen(), as follows: 369