aknopov
aknopov

Reputation: 31

Is it possible to select an alternate USB interface using Android SDK?

I have an USB device with interface that uses alternate settings for its endpoints. How can I select these settings using classes in 'android.hardware.usb' package?

In native code I am using 'libusb_set_interface_alt_setting' function from libUsb, which essentially is IOCTL. However, I wouldn't like to use NDK for that.

Thanks

Upvotes: 2

Views: 1494

Answers (3)

jensk
jensk

Reputation: 414

I had this issue as well - check my solution over here:

Android USB host DeviceConnection.setInterface prior to API Level 21

I first tried the method with a controlTransfer as suggested above, but it failed for me too. The SET_INTERFACE command was OK, and the GET_INTERFACE I read back returned the expected alternate value, so the device got the correct interface set. Even so, reads and writes to the endpoints failed :-/

I traced the libusb_set_interface_alt_setting() through libusb, usbfs and the kernel. In the kernel there is more code than simply sending a SET_INTERFACE to the USB device, for instance a comment about "9.1.1.5: reset toggles for all endpoints in the new altsetting". libusb's documentation for libusb_set_interface_alt_setting() also says:

You should always use this function rather than formulating your own SET_INTERFACE control request. This is because the underlying operating system needs to know when such changes happen.

So I figured I'd better do that. Long story short: look at the code in the link above.

Upvotes: 0

aknopov
aknopov

Reputation: 31

Just to close this question.

I resolved the issue by writing kernel driver for my device and using it as a char device.

As a side note, I could say that it would be nice to implement set_interface_alt_settings in libusbhost library and use it in 'android.hardware.usb' package through JNI.

Upvotes: 1

Alexander
Alexander

Reputation: 1098

To enable an alternative setting for an interface is nothing more than a Standard Device Request according to chapter 9.4 in the USB specification (usb_2.0.pdf).

So this:

UsbDeviceConnection.controlTransfer(UsbConstants.USB_DIR_OUT | 0x01, SET_INTERFACE, _alternate_setting_, _interface_nr_, null, 0, _timeout_);

should work. SET_INTERFACE (= 11 = 0x0B) is a USB specification constant. I could not find it in the android API as Java const.

Upvotes: 1

Related Questions