summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Chant <achant@google.com>2018-03-02 13:45:09 -0800
committerAndrew Chant <achant@google.com>2018-03-26 20:55:54 +0000
commit608ec66d62647f60c3988922fead33fd7e07755e (patch)
tree603144475060416c137fefb5169faf0e8cd2952f
parent0491f5aa0f3cc6b46bdf433bc900433b965943e2 (diff)
UsbHostManager: Restore inserted device logging
Restore logcat logging of newly-added USB devices. Eliminate blacklist log lines. Test: Connected USB Storage and USB audio devices. Updated UsbDescriptorParserTests for device descriptor version. example output: UsbHostManager: USB device attached: vidpid 03eb:2433 mfg/product/ver/serial Libratone/Libratone_INEAR/1.00/Inear_mcu_app_0.2.1_20160304 hasAudio/HID/Storage: true/true/false UsbHostManager: USB device attached: vidpid 05dc:a82b mfg/product/ver/serial Lexar/ARA Storage /2.08/0024070163400215 hasAudio/HID/Storage: false/false/true UsbHostManager: USB device attached: vidpid 18d1:5029 mfg/product/ver/serial Google/USB-C to 3.5mm-Headphone Adapter/22.80/201405280001 hasAudio/HID/Storage: false/true/false UsbHostManager: USB device attached: vidpid 18d1:5025 mfg/product/ver/serial Google/USB-C to 3.5mm-Headphone Adapter/22.80/201405280001 hasAudio/HID/Storage: true/true/false Bug: 74119682 Change-Id: I72688f651c819d4bdc48f6d6316570ca5fc54d1e
-rw-r--r--services/usb/java/com/android/server/usb/UsbHostManager.java59
-rw-r--r--services/usb/java/com/android/server/usb/descriptors/UsbDescriptorParser.java24
-rw-r--r--services/usb/java/com/android/server/usb/descriptors/UsbDeviceDescriptor.java39
-rw-r--r--tests/UsbTests/res/raw/usbdescriptors_massstorage.binbin0 -> 50 bytes
-rw-r--r--tests/UsbTests/src/com/android/server/usb/UsbDescriptorParserTests.java92
5 files changed, 188 insertions, 26 deletions
diff --git a/services/usb/java/com/android/server/usb/UsbHostManager.java b/services/usb/java/com/android/server/usb/UsbHostManager.java
index e3e381ec6fe1..67ad0907181b 100644
--- a/services/usb/java/com/android/server/usb/UsbHostManager.java
+++ b/services/usb/java/com/android/server/usb/UsbHostManager.java
@@ -54,6 +54,7 @@ import java.util.LinkedList;
public class UsbHostManager {
private static final String TAG = UsbHostManager.class.getSimpleName();
private static final boolean DEBUG = false;
+ private static final int LINUX_FOUNDATION_VID = 0x1d6b;
private final Context mContext;
@@ -267,7 +268,6 @@ public class UsbHostManager {
}
private boolean isBlackListed(String deviceAddress) {
- Slog.i(TAG, "isBlackListed(" + deviceAddress + ")");
int count = mHostBlacklist.length;
for (int i = 0; i < count; i++) {
if (deviceAddress.startsWith(mHostBlacklist[i])) {
@@ -279,7 +279,6 @@ public class UsbHostManager {
/* returns true if the USB device should not be accessible by applications */
private boolean isBlackListed(int clazz, int subClass) {
- Slog.i(TAG, "isBlackListed(" + clazz + ", " + subClass + ")");
// blacklist hubs
if (clazz == UsbConstants.USB_CLASS_HUB) return true;
@@ -302,6 +301,40 @@ public class UsbHostManager {
}
}
+ private void logUsbDevice(UsbDescriptorParser descriptorParser) {
+ int vid = 0;
+ int pid = 0;
+ String mfg = "<unknown>";
+ String product = "<unknown>";
+ String version = "<unknown>";
+ String serial = "<unknown>";
+
+ UsbDeviceDescriptor deviceDescriptor = descriptorParser.getDeviceDescriptor();
+ if (deviceDescriptor != null) {
+ vid = deviceDescriptor.getVendorID();
+ pid = deviceDescriptor.getProductID();
+ mfg = deviceDescriptor.getMfgString(descriptorParser);
+ product = deviceDescriptor.getProductString(descriptorParser);
+ version = deviceDescriptor.getDeviceReleaseString();
+ serial = deviceDescriptor.getSerialString(descriptorParser);
+ }
+
+ if (vid == LINUX_FOUNDATION_VID) {
+ return; // don't care about OS-constructed virtual USB devices.
+ }
+ boolean hasAudio = descriptorParser.hasAudioInterface();
+ boolean hasHid = descriptorParser.hasHIDInterface();
+ boolean hasStorage = descriptorParser.hasStorageInterface();
+
+ String attachedString = "USB device attached: ";
+ attachedString += String.format("vidpid %04x:%04x", vid, pid);
+ attachedString += String.format(" mfg/product/ver/serial %s/%s/%s/%s",
+ mfg, product, version, serial);
+ attachedString += String.format(" hasAudio/HID/Storage: %b/%b/%b",
+ hasAudio, hasHid, hasStorage);
+ Slog.d(TAG, attachedString);
+ }
+
/* Called from JNI in monitorUsbHostBus() to report new USB devices
Returns true if successful, i.e. the USB Audio device descriptors are
correctly parsed and the unique device is added to the audio device list.
@@ -313,10 +346,18 @@ public class UsbHostManager {
Slog.d(TAG, "usbDeviceAdded(" + deviceAddress + ") - start");
}
- // check class/subclass first as it is more likely to be blacklisted
- if (isBlackListed(deviceClass, deviceSubclass) || isBlackListed(deviceAddress)) {
+ if (isBlackListed(deviceAddress)) {
if (DEBUG) {
- Slog.d(TAG, "device is black listed");
+ Slog.d(TAG, "device address is black listed");
+ }
+ return false;
+ }
+ UsbDescriptorParser parser = new UsbDescriptorParser(deviceAddress, descriptors);
+ logUsbDevice(parser);
+
+ if (isBlackListed(deviceClass, deviceSubclass)) {
+ if (DEBUG) {
+ Slog.d(TAG, "device class is black listed");
}
return false;
}
@@ -329,7 +370,6 @@ public class UsbHostManager {
return false;
}
- UsbDescriptorParser parser = new UsbDescriptorParser(deviceAddress, descriptors);
UsbDevice newDevice = parser.toAndroidUsbDevice();
if (newDevice == null) {
Slog.e(TAG, "Couldn't create UsbDevice object.");
@@ -338,6 +378,7 @@ public class UsbHostManager {
parser.getRawDescriptors());
} else {
mDevices.put(deviceAddress, newDevice);
+ Slog.d(TAG, "Added device " + newDevice);
// It is fine to call this only for the current user as all broadcasts are
// sent to all profiles of the user and the dialogs should only show once.
@@ -367,18 +408,18 @@ public class UsbHostManager {
/* Called from JNI in monitorUsbHostBus to report USB device removal */
@SuppressWarnings("unused")
private void usbDeviceRemoved(String deviceAddress) {
- if (DEBUG) {
- Slog.d(TAG, "usbDeviceRemoved(" + deviceAddress + ") - start");
- }
synchronized (mLock) {
UsbDevice device = mDevices.remove(deviceAddress);
if (device != null) {
+ Slog.d(TAG, "Removed device at " + deviceAddress + ": " + device.getProductName());
mUsbAlsaManager.usbDeviceRemoved(deviceAddress/*device*/);
mSettingsManager.usbDeviceRemoved(device);
getCurrentUserSettings().usbDeviceRemoved(device);
// Tracking
addConnectionRecord(deviceAddress, ConnectionRecord.DISCONNECT, null);
+ } else {
+ Slog.d(TAG, "Removed device at " + deviceAddress + " was already gone");
}
}
}
diff --git a/services/usb/java/com/android/server/usb/descriptors/UsbDescriptorParser.java b/services/usb/java/com/android/server/usb/descriptors/UsbDescriptorParser.java
index c704fe34866f..e61542824083 100644
--- a/services/usb/java/com/android/server/usb/descriptors/UsbDescriptorParser.java
+++ b/services/usb/java/com/android/server/usb/descriptors/UsbDescriptorParser.java
@@ -475,9 +475,18 @@ public final class UsbDescriptorParser {
}
/**
+ *@ hide
+ */
+ public boolean hasAudioInterface() {
+ ArrayList<UsbDescriptor> descriptors =
+ getInterfaceDescriptorsForClass(UsbDescriptor.CLASSID_AUDIO);
+ return !descriptors.isEmpty();
+ }
+
+ /**
* @hide
*/
- public boolean hasHIDDescriptor() {
+ public boolean hasHIDInterface() {
ArrayList<UsbDescriptor> descriptors =
getInterfaceDescriptorsForClass(UsbDescriptor.CLASSID_HID);
return !descriptors.isEmpty();
@@ -486,6 +495,15 @@ public final class UsbDescriptorParser {
/**
* @hide
*/
+ public boolean hasStorageInterface() {
+ ArrayList<UsbDescriptor> descriptors =
+ getInterfaceDescriptorsForClass(UsbDescriptor.CLASSID_STORAGE);
+ return !descriptors.isEmpty();
+ }
+
+ /**
+ * @hide
+ */
public boolean hasMIDIInterface() {
ArrayList<UsbDescriptor> descriptors =
getInterfaceDescriptorsForClass(UsbDescriptor.CLASSID_AUDIO);
@@ -524,7 +542,7 @@ public final class UsbDescriptorParser {
probability += 0.75f;
}
- if (hasMic && hasHIDDescriptor()) {
+ if (hasMic && hasHIDInterface()) {
probability += 0.25f;
}
@@ -577,7 +595,7 @@ public final class UsbDescriptorParser {
probability += 0.75f;
}
- if (hasSpeaker && hasHIDDescriptor()) {
+ if (hasSpeaker && hasHIDInterface()) {
probability += 0.25f;
}
diff --git a/services/usb/java/com/android/server/usb/descriptors/UsbDeviceDescriptor.java b/services/usb/java/com/android/server/usb/descriptors/UsbDeviceDescriptor.java
index e31e3a312648..fae594ac4986 100644
--- a/services/usb/java/com/android/server/usb/descriptors/UsbDeviceDescriptor.java
+++ b/services/usb/java/com/android/server/usb/descriptors/UsbDeviceDescriptor.java
@@ -48,7 +48,7 @@ public final class UsbDeviceDescriptor extends UsbDescriptor {
private int mDeviceRelease; // 12:2 Device Release number - BCD
private byte mMfgIndex; // 14:1 Index of Manufacturer String Descriptor
private byte mProductIndex; // 15:1 Index of Product String Descriptor
- private byte mSerialNum; // 16:1 Index of Serial Number String Descriptor
+ private byte mSerialIndex; // 16:1 Index of Serial Number String Descriptor
private byte mNumConfigs; // 17:1 Number of Possible Configurations
private ArrayList<UsbConfigDescriptor> mConfigDescriptors =
@@ -91,16 +91,37 @@ public final class UsbDeviceDescriptor extends UsbDescriptor {
return mDeviceRelease;
}
+ // mDeviceRelease is binary-coded decimal, format DD.DD
+ public String getDeviceReleaseString() {
+ int hundredths = mDeviceRelease & 0xF;
+ int tenths = (mDeviceRelease & 0xF0) >> 4;
+ int ones = (mDeviceRelease & 0xF00) >> 8;
+ int tens = (mDeviceRelease & 0xF000) >> 12;
+ return String.format("%d.%d%d", tens * 10 + ones, tenths, hundredths);
+ }
+
public byte getMfgIndex() {
return mMfgIndex;
}
+ public String getMfgString(UsbDescriptorParser p) {
+ return p.getDescriptorString(mMfgIndex);
+ }
+
public byte getProductIndex() {
return mProductIndex;
}
- public byte getSerialNum() {
- return mSerialNum;
+ public String getProductString(UsbDescriptorParser p) {
+ return p.getDescriptorString(mProductIndex);
+ }
+
+ public byte getSerialIndex() {
+ return mSerialIndex;
+ }
+
+ public String getSerialString(UsbDescriptorParser p) {
+ return p.getDescriptorString(mSerialIndex);
}
public byte getNumConfigs() {
@@ -119,16 +140,14 @@ public final class UsbDeviceDescriptor extends UsbDescriptor {
Log.d(TAG, "toAndroid()");
}
- String mfgName = parser.getDescriptorString(mMfgIndex);
- String prodName = parser.getDescriptorString(mProductIndex);
+ String mfgName = getMfgString(parser);
+ String prodName = getProductString(parser);
if (DEBUG) {
Log.d(TAG, " mfgName:" + mfgName + " prodName:" + prodName);
}
- // Create version string in "%.%" format
- String versionString =
- Integer.toString(mDeviceRelease >> 8) + "." + (mDeviceRelease & 0xFF);
- String serialStr = parser.getDescriptorString(mSerialNum);
+ String versionString = getDeviceReleaseString();
+ String serialStr = getSerialString(parser);
if (DEBUG) {
Log.d(TAG, " versionString:" + versionString + " serialStr:" + serialStr);
}
@@ -159,7 +178,7 @@ public final class UsbDeviceDescriptor extends UsbDescriptor {
mDeviceRelease = stream.unpackUsbShort();
mMfgIndex = stream.getByte();
mProductIndex = stream.getByte();
- mSerialNum = stream.getByte();
+ mSerialIndex = stream.getByte();
mNumConfigs = stream.getByte();
return mLength;
diff --git a/tests/UsbTests/res/raw/usbdescriptors_massstorage.bin b/tests/UsbTests/res/raw/usbdescriptors_massstorage.bin
new file mode 100644
index 000000000000..1790369c5026
--- /dev/null
+++ b/tests/UsbTests/res/raw/usbdescriptors_massstorage.bin
Binary files differ
diff --git a/tests/UsbTests/src/com/android/server/usb/UsbDescriptorParserTests.java b/tests/UsbTests/src/com/android/server/usb/UsbDescriptorParserTests.java
index 4b64083c2367..ea027d7ae049 100644
--- a/tests/UsbTests/src/com/android/server/usb/UsbDescriptorParserTests.java
+++ b/tests/UsbTests/src/com/android/server/usb/UsbDescriptorParserTests.java
@@ -18,6 +18,7 @@ package com.android.server.usb;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.content.Context;
@@ -28,6 +29,7 @@ import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import com.android.server.usb.descriptors.UsbDescriptorParser;
+import com.android.server.usb.descriptors.UsbDeviceDescriptor;
import com.google.common.io.ByteStreams;
import java.io.InputStream;
@@ -65,7 +67,20 @@ public class UsbDescriptorParserTests {
return parser;
}
- // A Headset has a microphone and a speaker and is a headset.
+ /** A Headset has a microphone and a speaker and is a headset.
+ * Descriptors for this example show up on lsusb -v with:
+ * bcdDevice 22.80
+ * and a UAC1 audio device with the following control interface:
+ * bInterfaceClass 1 Audio
+ * ...
+ * bDescriptorSubtype 2 (INPUT_TERMINAL)
+ * bTerminalID 1
+ * wTerminalType 0x0201 Microphone
+ * ...
+ * bDescriptorSubtype 3 (OUTPUT_TERMINAL)
+ * bTerminalID 15
+ * wTerminalType 0x0302 Headphones
+ */
@Test
@SmallTest
public void testHeadsetDescriptorParser() {
@@ -74,9 +89,24 @@ public class UsbDescriptorParserTests {
assertTrue(parser.hasOutput());
assertTrue(parser.isInputHeadset());
assertTrue(parser.isOutputHeadset());
+
+ assertTrue(parser.hasAudioInterface());
+ assertTrue(parser.hasHIDInterface());
+ assertFalse(parser.hasStorageInterface());
+
+ assertEquals(parser.getDeviceDescriptor().getDeviceReleaseString(), "22.80");
}
- // Headphones have no microphones but are considered a headset.
+ /** Headphones have no microphones but are considered a headset.
+ * Descriptors for this example show up on lsusb -v with:
+ * bcdDevice 22.80
+ * and a UAC1 audio device with the following control interface:
+ * bInterfaceClass 1 Audio
+ * ...
+ * bDescriptorSubtype 3 (OUTPUT_TERMINAL)
+ * bTerminalID 15
+ * wTerminalType 0x0302 Headphones
+ */
@Test
@SmallTest
public void testHeadphoneDescriptorParser() {
@@ -85,9 +115,24 @@ public class UsbDescriptorParserTests {
assertTrue(parser.hasOutput());
assertFalse(parser.isInputHeadset());
assertTrue(parser.isOutputHeadset());
+
+ assertTrue(parser.hasAudioInterface());
+ assertTrue(parser.hasHIDInterface());
+ assertFalse(parser.hasStorageInterface());
+
+ assertEquals(parser.getDeviceDescriptor().getDeviceReleaseString(), "22.80");
}
- // Line out has no microphones and aren't considered a headset.
+ /** Line out with no microphones aren't considered a headset.
+ * Descriptors for this example show up on lsusb -v with:
+ * bcdDevice 22.80
+ * and the following UAC1 audio control interface
+ * bInterfaceClass 1 Audio
+ * ...
+ * bDescriptorSubtype 3 (OUTPUT_TERMINAL)
+ * bTerminalID 15
+ * wTerminalType 0x0603 Line Connector
+ */
@Test
@SmallTest
public void testLineoutDescriptorParser() {
@@ -96,9 +141,20 @@ public class UsbDescriptorParserTests {
assertTrue(parser.hasOutput());
assertFalse(parser.isInputHeadset());
assertFalse(parser.isOutputHeadset());
+
+ assertTrue(parser.hasAudioInterface());
+ assertTrue(parser.hasHIDInterface());
+ assertFalse(parser.hasStorageInterface());
+
+ assertEquals(parser.getDeviceDescriptor().getDeviceReleaseString(), "22.80");
}
- // An HID-only device shouldn't be considered anything at all.
+ /** An HID-only device shouldn't be considered anything at all.
+ /* Descriptors show up on lsusb -v with:
+ * bcdDevice 22.80
+ * and a single HID interface,
+ * bInterfaceClass 3 Human Interface Device
+ */
@Test
@SmallTest
public void testNothingDescriptorParser() {
@@ -107,6 +163,34 @@ public class UsbDescriptorParserTests {
assertFalse(parser.hasOutput());
assertFalse(parser.isInputHeadset());
assertFalse(parser.isOutputHeadset());
+
+ assertFalse(parser.hasAudioInterface());
+ assertTrue(parser.hasHIDInterface());
+ assertFalse(parser.hasStorageInterface());
+
+ assertEquals(parser.getDeviceDescriptor().getDeviceReleaseString(), "22.80");
+ }
+
+ /** A USB mass-storage device.
+ * Shows up on lsusb -v with:
+ * bcdDevice 2.08
+ * and a single interface descriptor,
+ * bInterfaceClass 8 Mass Storage
+ */
+ @Test
+ @SmallTest
+ public void testMassStorageDescriptorParser() {
+ UsbDescriptorParser parser = loadParser(R.raw.usbdescriptors_massstorage);
+ assertFalse(parser.hasInput());
+ assertFalse(parser.hasOutput());
+ assertFalse(parser.isInputHeadset());
+ assertFalse(parser.isOutputHeadset());
+
+ assertFalse(parser.hasAudioInterface());
+ assertFalse(parser.hasHIDInterface());
+ assertTrue(parser.hasStorageInterface());
+
+ assertEquals(parser.getDeviceDescriptor().getDeviceReleaseString(), "2.08");
}
}