Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
USB: Support for new USB gadget drivers
Browse files Browse the repository at this point in the history
Change-Id: Id08df50acb873a94f4765a991ee6a6f5b898ddf5
Signed-off-by: Mike Lockwood <lockwood@android.com>
  • Loading branch information
mikeandroid committed Jun 17, 2011
1 parent 5d27b7a commit 02e4569
Show file tree
Hide file tree
Showing 5 changed files with 440 additions and 340 deletions.
9 changes: 9 additions & 0 deletions core/java/android/hardware/usb/IUsbManager.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ interface IUsbManager

/* Clears default preferences and permissions for the package */
void clearDefaults(String packageName);

/* Sets the current primary USB function. */
void setPrimaryFunction(String functions);

/* Sets the default primary USB function. */
void setDefaultFunction(String functions);

/* Sets the file path for USB mass storage backing file. */
void setMassStorageBackingFile(String path);
}
74 changes: 43 additions & 31 deletions core/java/android/hardware/usb/UsbManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
Expand All @@ -50,7 +47,7 @@ public class UsbManager {
* This is a sticky broadcast for clients that includes USB connected/disconnected state,
* <ul>
* <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
* <li> {@link #USB_CONFIGURATION} integer containing current USB configuration
* <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured.
* currently zero if not configured, one for configured.
* <li> {@link #USB_FUNCTION_MASS_STORAGE} boolean extra indicating whether the
* mass storage function is enabled
Expand Down Expand Up @@ -128,12 +125,12 @@ public class UsbManager {
public static final String USB_CONNECTED = "connected";

/**
* Integer extra containing currently set USB configuration.
* Boolean extra indicating whether USB is configured.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast.
*
* {@hide}
*/
public static final String USB_CONFIGURATION = "configuration";
public static final String USB_CONFIGURED = "configured";

/**
* Name of the USB mass storage USB function.
Expand Down Expand Up @@ -388,55 +385,70 @@ public void requestPermission(UsbAccessory accessory, PendingIntent pi) {
}
}

private static File getFunctionEnableFile(String function) {
return new File("/sys/class/usb_composite/" + function + "/enable");
private static boolean propertyContainsFunction(String property, String function) {
String functions = SystemProperties.get(property, "");
int index = functions.indexOf(function);
if (index < 0) return false;
if (index > 0 && functions.charAt(index - 1) != ',') return false;
int charAfter = index + function.length();
if (charAfter < functions.length() && functions.charAt(charAfter) != ',') return false;
return true;
}

/**
* Returns true if the specified USB function is supported by the kernel.
* Note that a USB function maybe supported but disabled.
* Returns true if the specified USB function is currently enabled.
*
* @param function name of the USB function
* @return true if the USB function is supported.
* @return true if the USB function is enabled.
*
* {@hide}
*/
public static boolean isFunctionSupported(String function) {
return getFunctionEnableFile(function).exists();
public boolean isFunctionEnabled(String function) {
return propertyContainsFunction("sys.usb.config", function);
}

/**
* Returns true if the specified USB function is currently enabled.
* Sets the primary USB function.
*
* @param function name of the USB function
* @return true if the USB function is enabled.
*
* {@hide}
*/
public static boolean isFunctionEnabled(String function) {
public void setPrimaryFunction(String function) {
try {
FileInputStream stream = new FileInputStream(getFunctionEnableFile(function));
boolean enabled = (stream.read() == '1');
stream.close();
return enabled;
} catch (IOException e) {
return false;
mService.setPrimaryFunction(function);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setPrimaryFunction", e);
}
}

/**
* Enables or disables a USB function.
* Sets the default primary USB function.
*
* @param function name of the USB function
*
* {@hide}
*/
public static boolean setFunctionEnabled(String function, boolean enable) {
public void setDefaultFunction(String function) {
try {
FileOutputStream stream = new FileOutputStream(getFunctionEnableFile(function));
stream.write(enable ? '1' : '0');
stream.close();
return true;
} catch (IOException e) {
return false;
mService.setDefaultFunction(function);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setDefaultFunction", e);
}
}

/**
* Sets the file path for USB mass storage backing file.
*
* @param path backing file path
*
* {@hide}
*/
public void setMassStorageBackingFile(String path) {
try {
mService.setMassStorageBackingFile(path);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setDefaultFunction", e);
}
}
}
4 changes: 2 additions & 2 deletions services/java/com/android/server/SystemServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ public void run() {
}

try {
Slog.i(TAG, "USB Observer");
// Listen for USB changes
Slog.i(TAG, "USB Service");
// Manage USB host and device support
usb = new UsbService(context);
ServiceManager.addService(Context.USB_SERVICE, usb);
} catch (Throwable e) {
Expand Down

0 comments on commit 02e4569

Please sign in to comment.