am 42d38127: Merge "Allow pixelflinger to work when NX (No Execute) is enabled." into kraken
diff --git a/adb/adb.c b/adb/adb.c
index 04785fd..dd40eef 100644
--- a/adb/adb.c
+++ b/adb/adb.c
@@ -967,6 +967,99 @@
return 0;
}
+#if ADB_HOST
+void connect_device(char* host, char* buffer, int buffer_size)
+{
+ int port, fd;
+ char* portstr = strchr(host, ':');
+ char buf[4096];
+
+ if (!portstr) {
+ snprintf(buffer, buffer_size, "unable to parse %s as <host>:<port>", host);
+ return;
+ }
+ if (find_transport(host)) {
+ snprintf(buffer, buffer_size, "already connected to %s", host);
+ return;
+ }
+
+ // zero terminate host by overwriting the ':'
+ *portstr++ = 0;
+ if (sscanf(portstr, "%d", &port) == 0) {
+ snprintf(buffer, buffer_size, "bad port number %s", portstr);
+ return;
+ }
+
+ fd = socket_network_client(host, port, SOCK_STREAM);
+ if (fd < 0) {
+ snprintf(buffer, buffer_size, "unable to connect to %s:%d", host, port);
+ return;
+ }
+
+ D("client: connected on remote on fd %d\n", fd);
+ close_on_exec(fd);
+ disable_tcp_nagle(fd);
+ snprintf(buf, sizeof buf, "%s:%d", host, port);
+ register_socket_transport(fd, buf, port, 0);
+ snprintf(buffer, buffer_size, "connected to %s:%d", host, port);
+}
+
+void connect_emulator(char* port_spec, char* buffer, int buffer_size)
+{
+ char* port_separator = strchr(port_spec, ',');
+ if (!port_separator) {
+ snprintf(buffer, buffer_size,
+ "unable to parse '%s' as <console port>,<adb port>",
+ port_spec);
+ return;
+ }
+
+ // Zero-terminate console port and make port_separator point to 2nd port.
+ *port_separator++ = 0;
+ int console_port = strtol(port_spec, NULL, 0);
+ int adb_port = strtol(port_separator, NULL, 0);
+ if (!(console_port > 0 && adb_port > 0)) {
+ *(port_separator - 1) = ',';
+ snprintf(buffer, buffer_size,
+ "Invalid port numbers: Expected positive numbers, got '%s'",
+ port_spec);
+ return;
+ }
+
+ /* Check if the emulator is already known.
+ * Note: There's a small but harmless race condition here: An emulator not
+ * present just yet could be registered by another invocation right
+ * after doing this check here. However, local_connect protects
+ * against double-registration too. From here, a better error message
+ * can be produced. In the case of the race condition, the very specific
+ * error message won't be shown, but the data doesn't get corrupted. */
+ atransport* known_emulator = find_emulator_transport_by_adb_port(adb_port);
+ if (known_emulator != NULL) {
+ snprintf(buffer, buffer_size,
+ "Emulator on port %d already registered.", adb_port);
+ return;
+ }
+
+ /* Check if more emulators can be registered. Similar unproblematic
+ * race condition as above. */
+ int candidate_slot = get_available_local_transport_index();
+ if (candidate_slot < 0) {
+ snprintf(buffer, buffer_size, "Cannot accept more emulators.");
+ return;
+ }
+
+ /* Preconditions met, try to connect to the emulator. */
+ if (!local_connect_arbitrary_ports(console_port, adb_port)) {
+ snprintf(buffer, buffer_size,
+ "Connected to emulator on ports %d,%d", console_port, adb_port);
+ } else {
+ snprintf(buffer, buffer_size,
+ "Could not connect to emulator on ports %d,%d",
+ console_port, adb_port);
+ }
+}
+#endif
+
int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)
{
atransport *transport = NULL;
@@ -1024,43 +1117,16 @@
return 0;
}
- // add a new TCP transport
+ // add a new TCP transport, device or emulator
if (!strncmp(service, "connect:", 8)) {
char buffer[4096];
- int port, fd;
char* host = service + 8;
- char* portstr = strchr(host, ':');
-
- if (!portstr) {
- snprintf(buffer, sizeof(buffer), "unable to parse %s as <host>:<port>", host);
- goto done;
+ if (!strncmp(host, "emu:", 4)) {
+ connect_emulator(host + 4, buffer, sizeof(buffer));
+ } else {
+ connect_device(host, buffer, sizeof(buffer));
}
- if (find_transport(host)) {
- snprintf(buffer, sizeof(buffer), "Already connected to %s", host);
- goto done;
- }
-
- // zero terminate host by overwriting the ':'
- *portstr++ = 0;
- if (sscanf(portstr, "%d", &port) == 0) {
- snprintf(buffer, sizeof(buffer), "bad port number %s", portstr);
- goto done;
- }
-
- fd = socket_network_client(host, port, SOCK_STREAM);
- if (fd < 0) {
- snprintf(buffer, sizeof(buffer), "unable to connect to %s:%d", host, port);
- goto done;
- }
-
- D("client: connected on remote on fd %d\n", fd);
- close_on_exec(fd);
- disable_tcp_nagle(fd);
- snprintf(buf, sizeof buf, "%s:%d", host, port);
- register_socket_transport(fd, buf, port, 0);
- snprintf(buffer, sizeof(buffer), "connected to %s:%d", host, port);
-
-done:
+ // Send response for emulator and device
snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer), buffer);
writex(reply_fd, buf, strlen(buf));
return 0;
diff --git a/adb/adb.h b/adb/adb.h
index a2b611e..292e415 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -183,6 +183,7 @@
/* used to identify transports for clients */
char *serial;
char *product;
+ int adb_port; // Use for emulators (local transport)
/* a list of adisconnect callbacks called when the transport is kicked */
int kicked;
@@ -262,6 +263,9 @@
void kick_transport( atransport* t );
/* initialize a transport object's func pointers and state */
+#if ADB_HOST
+int get_available_local_transport_index();
+#endif
int init_socket_transport(atransport *t, int s, int port, int local);
void init_usb_transport(atransport *t, usb_handle *usb, int state);
@@ -280,6 +284,9 @@
void unregister_usb_transport(usb_handle *usb);
atransport *find_transport(const char *serial);
+#if ADB_HOST
+atransport* find_emulator_transport_by_adb_port(int adb_port);
+#endif
int service_to_fd(const char *name);
#if ADB_HOST
@@ -368,6 +375,7 @@
void local_init(int port);
int local_connect(int port);
+int local_connect_arbitrary_ports(int console_port, int adb_port);
/* usb host/client interface */
void usb_init();
diff --git a/adb/remount_service.c b/adb/remount_service.c
index 26bc841..4cb41e7 100644
--- a/adb/remount_service.c
+++ b/adb/remount_service.c
@@ -30,19 +30,19 @@
static int system_ro = 1;
-/* Returns the mount number of the requested partition from /proc/mtd */
-static int find_mount(const char *findme)
+/* Returns the device used to mount a directory in /proc/mounts */
+static char *find_mount(const char *dir)
{
int fd;
int res;
int size;
char *token = NULL;
const char delims[] = "\n";
- char buf[1024];
+ char buf[4096];
- fd = unix_open("/proc/mtd", O_RDONLY);
+ fd = unix_open("/proc/mounts", O_RDONLY);
if (fd < 0)
- return -errno;
+ return NULL;
buf[sizeof(buf) - 1] = '\0';
size = adb_read(fd, buf, sizeof(buf) - 1);
@@ -51,33 +51,41 @@
token = strtok(buf, delims);
while (token) {
- char mtdname[16];
- int mtdnum, mtdsize, mtderasesize;
+ char mount_dev[256];
+ char mount_dir[256];
+ int mount_freq;
+ int mount_passno;
- res = sscanf(token, "mtd%d: %x %x %15s",
- &mtdnum, &mtdsize, &mtderasesize, mtdname);
-
- if (res == 4 && !strcmp(mtdname, findme))
- return mtdnum;
+ res = sscanf(token, "%255s %255s %*s %*s %d %d\n",
+ mount_dev, mount_dir, &mount_freq, &mount_passno);
+ mount_dev[255] = 0;
+ mount_dir[255] = 0;
+ if (res == 4 && (strcmp(dir, mount_dir) == 0))
+ return strdup(mount_dev);
token = strtok(NULL, delims);
}
- return -1;
+ return NULL;
}
/* Init mounts /system as read only, remount to enable writes. */
static int remount_system()
{
- int num;
- char source[64];
+ char *dev;
+
if (system_ro == 0) {
return 0;
}
- if ((num = find_mount("\"system\"")) < 0)
+
+ dev = find_mount("/system");
+
+ if (!dev)
return -1;
- snprintf(source, sizeof source, "/dev/block/mtdblock%d", num);
- system_ro = mount(source, "/system", "yaffs2", MS_REMOUNT, NULL);
+ system_ro = mount(dev, "/system", "none", MS_REMOUNT, NULL);
+
+ free(dev);
+
return system_ro;
}
diff --git a/adb/transport_local.c b/adb/transport_local.c
index cfd3b4b..8dfc98d 100644
--- a/adb/transport_local.c
+++ b/adb/transport_local.c
@@ -41,9 +41,9 @@
#endif
#if ADB_HOST
-/* we keep a list of opened transports, transport 0 is bound to 5555,
- * transport 1 to 5557, .. transport n to 5555 + n*2. the list is used
- * to detect when we're trying to connect twice to a given local transport
+/* we keep a list of opened transports. The atransport struct knows to which
+ * local transport it is connected. The list is used to detect when we're
+ * trying to connect twice to a given local transport.
*/
#define ADB_LOCAL_TRANSPORT_MAX 16
@@ -102,7 +102,11 @@
}
-int local_connect(int port)
+int local_connect(int port) {
+ return local_connect_arbitrary_ports(port-1, port);
+}
+
+int local_connect_arbitrary_ports(int console_port, int adb_port)
{
char buf[64];
int fd = -1;
@@ -110,19 +114,19 @@
#if ADB_HOST
const char *host = getenv("ADBHOST");
if (host) {
- fd = socket_network_client(host, port, SOCK_STREAM);
+ fd = socket_network_client(host, adb_port, SOCK_STREAM);
}
#endif
if (fd < 0) {
- fd = socket_loopback_client(port, SOCK_STREAM);
+ fd = socket_loopback_client(adb_port, SOCK_STREAM);
}
if (fd >= 0) {
D("client: connected on remote on fd %d\n", fd);
close_on_exec(fd);
disable_tcp_nagle(fd);
- snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, port - 1);
- register_socket_transport(fd, buf, port, 1);
+ snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port);
+ register_socket_transport(fd, buf, adb_port, 1);
return 0;
}
return -1;
@@ -227,7 +231,50 @@
adb_close(t->fd);
}
-int init_socket_transport(atransport *t, int s, int port, int local)
+
+#if ADB_HOST
+/* Only call this function if you already hold local_transports_lock. */
+atransport* find_emulator_transport_by_adb_port_locked(int adb_port)
+{
+ int i;
+ for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
+ if (local_transports[i] && local_transports[i]->adb_port == adb_port) {
+ return local_transports[i];
+ }
+ }
+ return NULL;
+}
+
+atransport* find_emulator_transport_by_adb_port(int adb_port)
+{
+ adb_mutex_lock( &local_transports_lock );
+ atransport* result = find_emulator_transport_by_adb_port_locked(adb_port);
+ adb_mutex_unlock( &local_transports_lock );
+ return result;
+}
+
+/* Only call this function if you already hold local_transports_lock. */
+int get_available_local_transport_index_locked()
+{
+ int i;
+ for (i = 0; i < ADB_LOCAL_TRANSPORT_MAX; i++) {
+ if (local_transports[i] == NULL) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+int get_available_local_transport_index()
+{
+ adb_mutex_lock( &local_transports_lock );
+ int result = get_available_local_transport_index_locked();
+ adb_mutex_unlock( &local_transports_lock );
+ return result;
+}
+#endif
+
+int init_socket_transport(atransport *t, int s, int adb_port, int local)
{
int fail = 0;
@@ -239,26 +286,30 @@
t->sync_token = 1;
t->connection_state = CS_OFFLINE;
t->type = kTransportLocal;
+ t->adb_port = 0;
#if ADB_HOST
if (HOST && local) {
adb_mutex_lock( &local_transports_lock );
{
- int index = (port - DEFAULT_ADB_LOCAL_TRANSPORT_PORT)/2;
-
- if (!(port & 1) || index < 0 || index >= ADB_LOCAL_TRANSPORT_MAX) {
- D("bad local transport port number: %d\n", port);
- fail = -1;
- }
- else if (local_transports[index] != NULL) {
+ t->adb_port = adb_port;
+ atransport* existing_transport =
+ find_emulator_transport_by_adb_port_locked(adb_port);
+ int index = get_available_local_transport_index_locked();
+ if (existing_transport != NULL) {
D("local transport for port %d already registered (%p)?\n",
- port, local_transports[index]);
+ adb_port, existing_transport);
fail = -1;
- }
- else
+ } else if (index < 0) {
+ // Too many emulators.
+ D("cannot register more emulators. Maximum is %d\n",
+ ADB_LOCAL_TRANSPORT_MAX);
+ fail = -1;
+ } else {
local_transports[index] = t;
- }
- adb_mutex_unlock( &local_transports_lock );
+ }
+ }
+ adb_mutex_unlock( &local_transports_lock );
}
#endif
return fail;
diff --git a/fastboot/engine.c b/fastboot/engine.c
index 6d62c6e..f29cc03 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -271,11 +271,11 @@
a->data = (void*) notice;
}
-void fb_execute_queue(usb_handle *usb)
+int fb_execute_queue(usb_handle *usb)
{
Action *a;
char resp[FB_RESPONSE_SZ+1];
- int status;
+ int status = 0;
a = action_list;
resp[FB_RESPONSE_SZ] = 0;
@@ -307,5 +307,5 @@
}
fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
+ return status;
}
-
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index bed30b2..5389351 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -550,11 +550,12 @@
void *data;
unsigned sz;
unsigned page_size = 2048;
+ int status;
skip(1);
if (argc == 0) {
usage();
- return 0;
+ return 1;
}
if (!strcmp(*argv, "devices")) {
@@ -686,6 +687,7 @@
argc = do_oem_command(argc, argv);
} else {
usage();
+ return 1;
}
}
@@ -701,6 +703,6 @@
usb = open_device();
- fb_execute_queue(usb);
- return 0;
+ status = fb_execute_queue(usb);
+ return (status) ? 1 : 0;
}
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index a36c569..a4b27a0 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -49,7 +49,7 @@
void fb_queue_command(const char *cmd, const char *msg);
void fb_queue_download(const char *name, void *data, unsigned size);
void fb_queue_notice(const char *notice);
-void fb_execute_queue(usb_handle *usb);
+int fb_execute_queue(usb_handle *usb);
/* util stuff */
void die(const char *fmt, ...);
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 474cfac..1dbe171 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -51,6 +51,7 @@
#define AID_SDCARD_RW 1015 /* external storage write access */
#define AID_VPN 1016 /* vpn system */
#define AID_KEYSTORE 1017 /* keystore subsystem */
+#define AID_USB 1018 /* USB devices */
#define AID_SHELL 2000 /* adb and debug shell user */
#define AID_CACHE 2001 /* cache access */
@@ -100,6 +101,7 @@
{ "sdcard_rw", AID_SDCARD_RW, },
{ "vpn", AID_VPN, },
{ "keystore", AID_KEYSTORE, },
+ { "usb", AID_USB, },
{ "inet", AID_INET, },
{ "net_raw", AID_NET_RAW, },
{ "net_admin", AID_NET_ADMIN, },
diff --git a/include/usbhost/usbhost.h b/include/usbhost/usbhost.h
new file mode 100644
index 0000000..29b6ea3
--- /dev/null
+++ b/include/usbhost/usbhost.h
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __USB_HOST_H
+#define __USB_HOST_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+struct usb_endpoint_descriptor;
+
+struct usb_descriptor_iter {
+ unsigned char* config;
+ unsigned char* config_end;
+ unsigned char* curr_desc;
+};
+
+/* callback for notification when new USB devices are attached */
+typedef void (* usb_device_added_cb)(const char *dev_name);
+
+/* callback for notification when USB devices are removed */
+typedef void (* usb_device_removed_cb)(const char *dev_name);
+
+/* Call this to start monitoring the USB bus.
+ * added_cb will be called immediately for each existing USB device,
+ * and subsequently each time a new device is added.
+ * removed_cb is called when USB devices are removed from the bus.
+ */
+int usb_host_init(usb_device_added_cb added_cb, usb_device_removed_cb removed_cb);
+
+/* Creates a usb_device object for a USB device */
+struct usb_device *usb_device_open(const char *dev_name);
+
+/* Releases all resources associated with the USB device */
+void usb_device_close(struct usb_device *device);
+
+/* Returns the name for the USB device, which is the same as
+ * the dev_name passed to usb_device_open()
+ */
+const char* usb_device_get_name(struct usb_device *device);
+
+/* Returns the USB vendor ID from the device descriptor for the USB device */
+uint16_t usb_device_get_vendor_id(struct usb_device *device);
+
+/* Returns the USB product ID from the device descriptor for the USB device */
+uint16_t usb_device_get_product_id(struct usb_device *device);
+
+/* Returns a USB descriptor string for the given string ID.
+ * Used to implement usb_device_get_manufacturer_name,
+ * usb_device_get_product_name and usb_device_get_serial.
+ * Call free() to free the result when you are done with it.
+ */
+char* usb_device_get_string(struct usb_device *device, int id);
+
+/* Returns the manufacturer name for the USB device.
+ * Call free() to free the result when you are done with it.
+ */
+char* usb_device_get_manufacturer_name(struct usb_device *device);
+
+/* Returns the product name for the USB device.
+ * Call free() to free the result when you are done with it.
+ */
+char* usb_device_get_product_name(struct usb_device *device);
+
+/* Returns the USB serial number for the USB device.
+ * Call free() to free the result when you are done with it.
+ */
+char* usb_device_get_serial(struct usb_device *device);
+
+/* Returns true if we have write access to the USB device,
+ * and false if we only have access to the USB device configuration.
+ */
+int usb_device_is_writeable(struct usb_device *device);
+
+/* Initializes a usb_descriptor_iter, which can be used to iterate through all
+ * the USB descriptors for a USB device.
+ */
+void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter);
+
+/* Returns the next USB descriptor for a device, or NULL if we have reached the
+ * end of the list.
+ */
+struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter);
+
+/* Claims the specified interface of a USB device */
+int usb_device_claim_interface(struct usb_device *device, unsigned int interface);
+
+/* Releases the specified interface of a USB device */
+int usb_device_release_interface(struct usb_device *device, unsigned int interface);
+
+
+/* Creates a new usb_endpoint for the specified endpoint of a USB device.
+ * This can be used to read or write data across the endpoint.
+ */
+struct usb_endpoint *usb_endpoint_open(struct usb_device *dev,
+ const struct usb_endpoint_descriptor *desc);
+
+/* Releases all resources associated with the endpoint */
+void usb_endpoint_close(struct usb_endpoint *ep);
+
+/* Begins a read or write operation on the specified endpoint */
+int usb_endpoint_queue(struct usb_endpoint *ep, void *data, int len);
+
+ /* Waits for the results of a previous usb_endpoint_queue operation on the
+ * specified endpoint. Returns number of bytes transferred, or a negative
+ * value for error.
+ */
+int usb_endpoint_wait(struct usb_device *device, int *out_ep_num);
+
+/* Cancels a pending usb_endpoint_queue() operation on an endpoint. */
+int usb_endpoint_cancel(struct usb_endpoint *ep);
+
+/* Returns the endpoint address for the given endpoint */
+int usb_endpoint_number(struct usb_endpoint *ep);
+
+/* Returns the maximum packet size for the given endpoint.
+ * For bulk endpoints this should be 512 for highspeed or 64 for fullspeed.
+ */
+int usb_endpoint_max_packet(struct usb_endpoint *ep);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* __USB_HOST_H */
diff --git a/init/devices.c b/init/devices.c
index fa96f7c..663cdfe 100644
--- a/init/devices.c
+++ b/init/devices.c
@@ -327,6 +327,7 @@
static void handle_device_event(struct uevent *uevent)
{
char devpath[96];
+ int devpath_ready = 0;
char *base, *name;
char **links = NULL;
int block;
@@ -356,7 +357,26 @@
} else {
block = 0;
/* this should probably be configurable somehow */
- if(!strncmp(uevent->subsystem, "graphics", 8)) {
+ if (!strncmp(uevent->subsystem, "usb", 3)) {
+ if (!strcmp(uevent->subsystem, "usb")) {
+ /* This imitates the file system that would be created
+ * if we were using devfs instead.
+ * Minors are broken up into groups of 128, starting at "001"
+ */
+ int bus_id = uevent->minor / 128 + 1;
+ int device_id = uevent->minor % 128 + 1;
+ /* build directories */
+ mkdir("/dev/bus", 0755);
+ mkdir("/dev/bus/usb", 0755);
+ snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
+ mkdir(devpath, 0755);
+ snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
+ devpath_ready = 1;
+ } else {
+ /* ignore other USB events */
+ return;
+ }
+ } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
base = "/dev/graphics/";
mkdir(base, 0755);
} else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
@@ -386,7 +406,8 @@
base = "/dev/";
}
- snprintf(devpath, sizeof(devpath), "%s%s", base, name);
+ if (!devpath_ready)
+ snprintf(devpath, sizeof(devpath), "%s%s", base, name);
if(!strcmp(uevent->action, "add")) {
make_device(devpath, block, uevent->major, uevent->minor);
diff --git a/libusbhost/Android.mk b/libusbhost/Android.mk
new file mode 100644
index 0000000..c8c8758
--- /dev/null
+++ b/libusbhost/Android.mk
@@ -0,0 +1,41 @@
+#
+# Copyright (C) 2010 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(my-dir)
+
+# Static library for Linux host
+# ========================================================
+
+ifeq ($(HOST_OS),linux)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libusbhost
+LOCAL_SRC_FILES := usbhost.c
+
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+endif
+
+# Static library for target
+# ========================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := libusbhost
+LOCAL_SRC_FILES := usbhost.c
+
+include $(BUILD_STATIC_LIBRARY)
diff --git a/libusbhost/usbhost.c b/libusbhost/usbhost.c
new file mode 100644
index 0000000..d2eaf0c
--- /dev/null
+++ b/libusbhost/usbhost.c
@@ -0,0 +1,456 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/inotify.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <ctype.h>
+#include <pthread.h>
+
+#include <linux/usbdevice_fs.h>
+#include <linux/version.h>
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
+#include <linux/usb/ch9.h>
+#else
+#include <linux/usb_ch9.h>
+#endif
+#include <asm/byteorder.h>
+
+#include "usbhost/usbhost.h"
+
+#define USB_FS_DIR "/dev/bus/usb"
+
+#if 0
+#define D printf
+#else
+#define D(...)
+#endif
+
+struct usb_device {
+ char dev_name[64];
+ unsigned char desc[256];
+ int desc_length;
+ int fd;
+ int writeable;
+};
+
+struct usb_endpoint
+{
+ struct usb_device *dev;
+ struct usb_endpoint_descriptor desc;
+ struct usbdevfs_urb urb;
+};
+
+static usb_device_added_cb s_added_cb;
+static usb_device_removed_cb s_removed_cb;
+
+static inline int badname(const char *name)
+{
+ while(*name) {
+ if(!isdigit(*name++)) return 1;
+ }
+ return 0;
+}
+
+static void find_existing_devices()
+{
+ char busname[32], devname[32];
+ DIR *busdir , *devdir ;
+ struct dirent *de;
+
+ busdir = opendir(USB_FS_DIR);
+ if(busdir == 0) return;
+
+ while((de = readdir(busdir)) != 0) {
+ if(badname(de->d_name)) continue;
+
+ snprintf(busname, sizeof busname, "%s/%s", USB_FS_DIR, de->d_name);
+ devdir = opendir(busname);
+ if(devdir == 0) continue;
+
+ while((de = readdir(devdir))) {
+ if(badname(de->d_name)) continue;
+
+ snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
+ s_added_cb(devname);
+ } // end of devdir while
+ closedir(devdir);
+ } //end of busdir while
+ closedir(busdir);
+}
+
+static void* device_discovery_thread(void* unused)
+{
+ struct inotify_event* event;
+ char event_buf[512];
+ char path[100];
+ int i, fd, ret;
+ int wd, wds[10];
+ int wd_count = sizeof(wds) / sizeof(wds[0]);
+
+ D("Created device discovery thread\n");
+
+ fd = inotify_init();
+ if (fd < 0) {
+ fprintf(stderr, "inotify_init failed\n");
+ return NULL;
+ }
+
+ /* watch for files added and deleted within USB_FS_DIR */
+ memset(wds, 0, sizeof(wds));
+ /* watch the root for new subdirectories */
+ wds[0] = inotify_add_watch(fd, USB_FS_DIR, IN_CREATE | IN_DELETE);
+ if (wds[0] < 0) {
+ fprintf(stderr, "inotify_add_watch failed\n");
+ return NULL;
+ }
+
+ /* watch existing subdirectories of USB_FS_DIR */
+ for (i = 1; i < wd_count; i++) {
+ snprintf(path, sizeof(path), "%s/%03d", USB_FS_DIR, i);
+ ret = inotify_add_watch(fd, path, IN_CREATE | IN_DELETE);
+ if (ret > 0)
+ wds[i] = ret;
+ }
+
+ /* check for existing devices first, after we have inotify set up */
+ if (s_added_cb)
+ find_existing_devices();
+
+ while (1) {
+ ret = read(fd, event_buf, sizeof(event_buf));
+ if (ret >= (int)sizeof(struct inotify_event)) {
+ event = (struct inotify_event *)event_buf;
+ wd = event->wd;
+ if (wd == wds[0]) {
+ i = atoi(event->name);
+ snprintf(path, sizeof(path), "%s/%s", USB_FS_DIR, event->name);
+ D("new subdirectory %s: index: %d\n", path, i);
+ if (i > 0 && i < wd_count) {
+ ret = inotify_add_watch(fd, path, IN_CREATE | IN_DELETE);
+ if (ret > 0)
+ wds[i] = ret;
+ }
+ } else {
+ for (i = 1; i < wd_count; i++) {
+ if (wd == wds[i]) {
+ snprintf(path, sizeof(path), "%s/%03d/%s", USB_FS_DIR, i, event->name);
+ if (event->mask == IN_CREATE) {
+ D("new device %s\n", path);
+ if (s_added_cb)
+ s_added_cb(path);
+ } else if (event->mask == IN_DELETE) {
+ D("gone device %s\n", path);
+ if (s_removed_cb)
+ s_removed_cb(path);
+ }
+ }
+ }
+ }
+ }
+ }
+ return NULL;
+}
+
+int usb_host_init(usb_device_added_cb added_cb, usb_device_removed_cb removed_cb)
+{
+ pthread_t tid;
+
+ s_added_cb = added_cb;
+ s_removed_cb = removed_cb;
+
+ if (added_cb || removed_cb) {
+ pthread_attr_t attr;
+
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ return pthread_create(&tid, &attr, device_discovery_thread, NULL);
+ }
+ else
+ return 0;
+}
+
+struct usb_device *usb_device_open(const char *dev_name)
+{
+ struct usb_device *device = calloc(1, sizeof(struct usb_device));
+ int fd, length, did_retry = 0;
+
+ strcpy(device->dev_name, dev_name);
+ device->writeable = 1;
+
+retry:
+ fd = open(dev_name, O_RDWR);
+ if (fd < 0) {
+ /* if we fail, see if have read-only access */
+ fd = open(dev_name, O_RDONLY);
+ if (fd < 0 && errno == EACCES && !did_retry) {
+ /* work around race condition between inotify and permissions management */
+ sleep(1);
+ did_retry = 1;
+ goto retry;
+ }
+
+ if (fd < 0) goto fail;
+ device->writeable = 0;
+ D("[ usb open read-only %s fd = %d]\n", dev_name, fd);
+ }
+
+ length = read(fd, device->desc, sizeof(device->desc));
+ if (length < 0)
+ goto fail;
+
+ device->fd = fd;
+ device->desc_length = length;
+ return device;
+fail:
+ close(fd);
+ free(device);
+ return NULL;
+}
+
+void usb_device_close(struct usb_device *device)
+{
+ close(device->fd);
+ free(device);
+}
+
+const char* usb_device_get_name(struct usb_device *device)
+{
+ return device->dev_name;
+}
+
+uint16_t usb_device_get_vendor_id(struct usb_device *device)
+{
+ struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
+ return __le16_to_cpu(desc->idVendor);
+}
+
+uint16_t usb_device_get_product_id(struct usb_device *device)
+{
+ struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc;
+ return __le16_to_cpu(desc->idProduct);
+}
+
+char* usb_device_get_string(struct usb_device *device, int id)
+{
+ char string[256];
+ struct usbdevfs_ctrltransfer ctrl;
+ __u16 buffer[128];
+ __u16 languages[128];
+ int i, result;
+ int languageCount = 0;
+
+ string[0] = 0;
+
+ // reading the string requires read/write permission
+ if (!device->writeable) {
+ int fd = open(device->dev_name, O_RDWR);
+ if (fd > 0) {
+ close(device->fd);
+ device->fd = fd;
+ device->writeable = 1;
+ } else {
+ return NULL;
+ }
+ }
+
+ memset(languages, 0, sizeof(languages));
+ memset(&ctrl, 0, sizeof(ctrl));
+
+ // read list of supported languages
+ ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
+ ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
+ ctrl.wValue = (USB_DT_STRING << 8) | 0;
+ ctrl.wIndex = 0;
+ ctrl.wLength = sizeof(languages);
+ ctrl.data = languages;
+
+ result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
+ if (result > 0)
+ languageCount = (result - 2) / 2;
+
+ for (i = 1; i <= languageCount; i++) {
+ memset(buffer, 0, sizeof(buffer));
+ memset(&ctrl, 0, sizeof(ctrl));
+
+ ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
+ ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
+ ctrl.wValue = (USB_DT_STRING << 8) | id;
+ ctrl.wIndex = languages[i];
+ ctrl.wLength = sizeof(buffer);
+ ctrl.data = buffer;
+
+ result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
+ if (result > 0) {
+ int i;
+ // skip first word, and copy the rest to the string, changing shorts to bytes.
+ result /= 2;
+ for (i = 1; i < result; i++)
+ string[i - 1] = buffer[i];
+ string[i - 1] = 0;
+ return strdup(string);
+ }
+ }
+
+ return NULL;
+}
+
+char* usb_device_get_manufacturer_name(struct usb_device *device)
+{
+ struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
+
+ if (desc->iManufacturer)
+ return usb_device_get_string(device, desc->iManufacturer);
+ else
+ return NULL;
+}
+
+char* usb_device_get_product_name(struct usb_device *device)
+{
+ struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
+
+ if (desc->iProduct)
+ return usb_device_get_string(device, desc->iProduct);
+ else
+ return NULL;
+}
+
+char* usb_device_get_serial(struct usb_device *device)
+{
+ struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
+
+ if (desc->iSerialNumber)
+ return usb_device_get_string(device, desc->iSerialNumber);
+ else
+ return NULL;
+}
+
+int usb_device_is_writeable(struct usb_device *device)
+{
+ return device->writeable;
+}
+
+void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter)
+{
+ iter->config = device->desc;
+ iter->config_end = device->desc + device->desc_length;
+ iter->curr_desc = device->desc;
+}
+
+struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter)
+{
+ struct usb_descriptor_header* next;
+ if (iter->curr_desc >= iter->config_end)
+ return NULL;
+ next = (struct usb_descriptor_header*)iter->curr_desc;
+ iter->curr_desc += next->bLength;
+ return next;
+}
+
+int usb_device_claim_interface(struct usb_device *device, unsigned int interface)
+{
+ return ioctl(device->fd, USBDEVFS_CLAIMINTERFACE, &interface);
+}
+
+int usb_device_release_interface(struct usb_device *device, unsigned int interface)
+{
+ return ioctl(device->fd, USBDEVFS_RELEASEINTERFACE, &interface);
+}
+
+struct usb_endpoint *usb_endpoint_open(struct usb_device *dev,
+ const struct usb_endpoint_descriptor *desc)
+{
+ struct usb_endpoint *ep = calloc(1, sizeof(struct usb_endpoint));
+ memcpy(&ep->desc, desc, sizeof(ep->desc));
+ ep->dev = dev;
+ return ep;
+}
+
+void usb_endpoint_close(struct usb_endpoint *ep)
+{
+ // cancel IO here?
+ free(ep);
+}
+
+int usb_endpoint_queue(struct usb_endpoint *ep, void *data, int len)
+{
+ struct usbdevfs_urb *urb = &ep->urb;
+ int res;
+
+ D("usb_endpoint_queue\n");
+ memset(urb, 0, sizeof(*urb));
+ urb->type = USBDEVFS_URB_TYPE_BULK;
+ urb->endpoint = ep->desc.bEndpointAddress;
+ urb->status = -1;
+ urb->buffer = data;
+ urb->buffer_length = len;
+
+ do {
+ res = ioctl(ep->dev->fd, USBDEVFS_SUBMITURB, urb);
+ } while((res < 0) && (errno == EINTR));
+
+ return res;
+}
+
+int usb_endpoint_wait(struct usb_device *dev, int *out_ep_num)
+{
+ struct usbdevfs_urb *out = NULL;
+ int res;
+
+ while (1) {
+ res = ioctl(dev->fd, USBDEVFS_REAPURB, &out);
+ D("USBDEVFS_REAPURB returned %d\n", res);
+ if (res < 0) {
+ if(errno == EINTR) {
+ continue;
+ }
+ D("[ reap urb - error ]\n");
+ *out_ep_num = -1;
+ } else {
+ D("[ urb @%p status = %d, actual = %d ]\n",
+ out, out->status, out->actual_length);
+ res = out->actual_length;
+ *out_ep_num = out->endpoint;
+ }
+ break;
+ }
+ return res;
+}
+
+int usb_endpoint_cancel(struct usb_endpoint *ep)
+{
+ return ioctl(ep->dev->fd, USBDEVFS_DISCARDURB, &ep->urb);
+}
+
+int usb_endpoint_number(struct usb_endpoint *ep)
+{
+ return ep->desc.bEndpointAddress;
+}
+
+int usb_endpoint_max_packet(struct usb_endpoint *ep)
+{
+ return __le16_to_cpu(ep->desc.wMaxPacketSize);
+}
+
diff --git a/rootdir/ueventd.rc b/rootdir/ueventd.rc
index 46f640f..7845eb9 100644
--- a/rootdir/ueventd.rc
+++ b/rootdir/ueventd.rc
@@ -69,6 +69,7 @@
/dev/qmi0 0640 radio radio
/dev/qmi1 0640 radio radio
/dev/qmi2 0640 radio radio
+/dev/bus/usb/* 0660 root usb
# CDMA radio interface MUX
/dev/ts0710mux* 0640 radio radio
diff --git a/toolbox/mount.c b/toolbox/mount.c
index 472c952..82ecc56 100644
--- a/toolbox/mount.c
+++ b/toolbox/mount.c
@@ -222,9 +222,50 @@
return 0;
}
+static int get_mounts_dev_dir(const char *arg, char **dev, char **dir)
+{
+ FILE *f;
+ char mount_dev[256];
+ char mount_dir[256];
+ char mount_type[256];
+ char mount_opts[256];
+ int mount_freq;
+ int mount_passno;
+ int match;
+
+ f = fopen("/proc/mounts", "r");
+ if (!f) {
+ fprintf(stdout, "could not open /proc/mounts\n");
+ return -1;
+ }
+
+ do {
+ match = fscanf(f, "%255s %255s %255s %255s %d %d\n",
+ mount_dev, mount_dir, mount_type,
+ mount_opts, &mount_freq, &mount_passno);
+ mount_dev[255] = 0;
+ mount_dir[255] = 0;
+ mount_type[255] = 0;
+ mount_opts[255] = 0;
+ if (match == 6 &&
+ (strcmp(arg, mount_dev) == 0 ||
+ strcmp(arg, mount_dir) == 0)) {
+ *dev = strdup(mount_dev);
+ *dir = strdup(mount_dir);
+ fclose(f);
+ return 0;
+ }
+ } while (match != EOF);
+
+ fclose(f);
+ return -1;
+}
+
int mount_main(int argc, char *argv[])
{
char *type = NULL;
+ char *dev = NULL;
+ char *dir = NULL;
int c;
int loop = 0;
@@ -265,12 +306,19 @@
if (rwflag & MS_TYPE)
type = "none";
- if (optind + 2 != argc || type == NULL) {
+ if (optind + 2 == argc) {
+ dev = argv[optind];
+ dir = argv[optind + 1];
+ } else if (optind + 1 == argc && rwflag & MS_REMOUNT) {
+ get_mounts_dev_dir(argv[optind], &dev, &dir);
+ }
+
+ if (dev == NULL || dir == NULL || type == NULL) {
fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] "
"device directory\n", progname);
exit(1);
}
- return do_mount(argv[optind], argv[optind + 1], type, rwflag,
- extra.str, loop);
+ return do_mount(dev, dir, type, rwflag, extra.str, loop);
+ /* We leak dev and dir in some cases, but we're about to exit */
}