Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02e4569b authored by Mike Lockwood's avatar Mike Lockwood
Browse files

USB: Support for new USB gadget drivers



Change-Id: Id08df50acb873a94f4765a991ee6a6f5b898ddf5
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 5d27b7a2
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -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);
}
+43 −31
Original line number Diff line number Diff line
@@ -22,12 +22,9 @@ import android.content.Context;
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;

/**
@@ -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
@@ -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.
@@ -388,55 +385,70 @@ public class UsbManager {
        }
    }

    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);
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -406,8 +406,8 @@ class ServerThread extends Thread {
            }

            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) {
+357 −305

File changed.

Preview size limit exceeded, changes collapsed.

+29 −2
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ public class UsbService extends IUsbManager.Stub {
        if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {
            mHostManager = new UsbHostManager(context, mSettingsManager);
        }
        if (new File("/sys/class/usb_composite").exists()) {
        if (new File("/sys/class/android_usb").exists()) {
            mDeviceManager = new UsbDeviceManager(context, mSettingsManager);
        }
    }
@@ -92,7 +92,7 @@ public class UsbService extends IUsbManager.Stub {
    /* opens the currently attached USB accessory (device mode) */
    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
        if (mDeviceManager != null) {
            return openAccessory(accessory);
            return mDeviceManager.openAccessory(accessory);
        } else {
            return null;
        }
@@ -146,6 +146,33 @@ public class UsbService extends IUsbManager.Stub {
        mSettingsManager.clearDefaults(packageName);
    }

    public void setPrimaryFunction(String function) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        if (mDeviceManager != null) {
            mDeviceManager.setPrimaryFunction(function);
        } else {
            throw new IllegalStateException("USB device mode not supported");
        }
    }

    public void setDefaultFunction(String function) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        if (mDeviceManager != null) {
            mDeviceManager.setDefaultFunction(function);
        } else {
            throw new IllegalStateException("USB device mode not supported");
        }
    }

    public void setMassStorageBackingFile(String path) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        if (mDeviceManager != null) {
            mDeviceManager.setMassStorageBackingFile(path);
        } else {
            throw new IllegalStateException("USB device mode not supported");
        }
    }

    @Override
    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)