Merge "Don't define __linux__ if it is already defined by the toolchain. This change does not affect existing behavior at all and makes the file compatible with new toolchain."
diff --git a/Android.mk b/Android.mk
index fa2f6f8..a307719 100644
--- a/Android.mk
+++ b/Android.mk
@@ -25,6 +25,7 @@
liblog \
libnetutils \
libpixelflinger \
+ libusbhost \
libzipfile \
))
endif
diff --git a/adb/Android.mk b/adb/Android.mk
index 0c93c1e..6fea574 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -81,6 +81,9 @@
ifeq ($(USE_SYSDEPS_WIN32),)
LOCAL_STATIC_LIBRARIES += libcutils
endif
+ifeq ($(HOST_OS),linux)
+ LOCAL_STATIC_LIBRARIES += libusbhost
+endif
include $(BUILD_HOST_EXECUTABLE)
diff --git a/adb/OVERVIEW.TXT b/adb/OVERVIEW.TXT
index 6a5191a..c40695a 100644
--- a/adb/OVERVIEW.TXT
+++ b/adb/OVERVIEW.TXT
@@ -35,7 +35,7 @@
(through USB for devices, through TCP for emulators) and provide a
few services for clients that run on the host.
- The ADB server considers that a device is ONLINE when it has succesfully
+ The ADB server considers that a device is ONLINE when it has successfully
connected to the adbd program within it. Otherwise, the device is OFFLINE,
meaning that the ADB server detected a new device/emulator, but could not
connect to the adbd daemon.
diff --git a/adb/SERVICES.TXT b/adb/SERVICES.TXT
index b0124a4..be4d50b 100644
--- a/adb/SERVICES.TXT
+++ b/adb/SERVICES.TXT
@@ -74,7 +74,7 @@
host-local:<request>
A variant of host-serial used to target the single emulator instance
- running on the host. This will fail if therre is none or more than one.
+ running on the host. This will fail if there is none or more than one.
host:<request>
When asking for information related to a device, 'host:' can also be
@@ -146,7 +146,7 @@
dev:<path>
Opens a device file and connects the client directly to it for
read/write purposes. Useful for debugging, but may require special
- priviledges and thus may not run on all devices. <path> is a full
+ privileges and thus may not run on all devices. <path> is a full
path from the root of the filesystem.
tcp:<port>
@@ -173,7 +173,7 @@
framebuffer:
This service is used to send snapshots of the framebuffer to a client.
- It requires sufficient priviledges but works as follow:
+ It requires sufficient privileges but works as follow:
After the OKAY, the service sends 16-byte binary structure
containing the following fields (little-endian format):
@@ -190,14 +190,14 @@
one byte through the channel, which will trigger the service
to send it 'size' bytes of framebuffer data.
- If the adbd daemon doesn't have sufficient priviledges to open
+ If the adbd daemon doesn't have sufficient privileges to open
the framebuffer device, the connection is simply closed immediately.
dns:<server-name>
This service is an exception because it only runs within the ADB server.
It is used to implement USB networking, i.e. to provide a network connection
to the device through the host machine (note: this is the exact opposite of
- network thetering).
+ network tethering).
It is used to perform a gethostbyname(<address>) on the host and return
the corresponding IP address as a 4-byte string.
@@ -209,7 +209,7 @@
- creating a file named /tmp/update
- reading 'size' bytes from the client and writing them to /tmp/update
- - when everything is read succesfully, create a file named /tmp/update.start
+ - when everything is read successfully, create a file named /tmp/update.start
This service can only work when the device is in recovery mode. Otherwise,
the /tmp directory doesn't exist and the connection will be closed immediately.
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/commandline.c b/adb/commandline.c
index 8566066..d97e97d 100644
--- a/adb/commandline.c
+++ b/adb/commandline.c
@@ -171,6 +171,12 @@
"\n"
" - If it is \"system\" or \"data\", only the corresponding partition\n"
" is updated.\n"
+ "\n"
+ "environmental variables:\n"
+ " ADB_TRACE - Print debug information. A comma separated list of the following values\n"
+ " 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n"
+ " ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.\n"
+ " ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.\n"
);
}
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/adb/usb_linux.c b/adb/usb_linux.c
index bb86813..4b5edb6 100644
--- a/adb/usb_linux.c
+++ b/adb/usb_linux.c
@@ -19,22 +19,13 @@
#include <unistd.h>
#include <string.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <ctype.h>
-
-#include <linux/usbdevice_fs.h>
+#include <usbhost/usbhost.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 "sysdeps.h"
@@ -52,28 +43,19 @@
usb_handle *prev;
usb_handle *next;
- char fname[64];
- int desc;
- unsigned char ep_in;
- unsigned char ep_out;
+ struct usb_device *device;
+ struct usb_endpoint *ep_in;
+ struct usb_endpoint *ep_out;
- unsigned zero_mask;
- unsigned writeable;
-
- struct usbdevfs_urb urb_in;
- struct usbdevfs_urb urb_out;
-
- int urb_in_busy;
- int urb_out_busy;
- int dead;
-
- adb_cond_t notify;
+ adb_cond_t notify_in;
+ adb_cond_t notify_out;
adb_mutex_t lock;
- // for garbage collecting disconnected devices
- int mark;
+ int read_result, write_result;
+ int zero_mask;
+ int dead;
- // ID of thread currently in REAPURB
+ // Thread ID for our reaper thread
pthread_t reaper_thread;
};
@@ -87,10 +69,8 @@
usb_handle *usb;
adb_mutex_lock(&usb_lock);
- for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
- if(!strcmp(usb->fname, dev_name)) {
- // set mark flag to indicate this device is still alive
- usb->mark = 1;
+ for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
+ if (!strcmp(usb_device_get_name(usb->device), dev_name)) {
adb_mutex_unlock(&usb_lock);
return 1;
}
@@ -99,185 +79,204 @@
return 0;
}
-static void kick_disconnected_devices()
+static void kick_disconnected_device(const char *devname)
{
usb_handle *usb;
adb_mutex_lock(&usb_lock);
- // kick any devices in the device list that were not found in the device scan
- for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
- if (usb->mark == 0) {
+ /* kick the device if it is in our list */
+ for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
+ if (!strcmp(devname, usb_device_get_name(usb->device)))
usb_kick(usb);
- } else {
- usb->mark = 0;
- }
}
adb_mutex_unlock(&usb_lock);
}
-static void register_device(const char *dev_name, unsigned char ep_in, unsigned char ep_out,
- int ifc, int serial_index, unsigned zero_mask);
-
-static inline int badname(const char *name)
+static void* reaper_thread(void* arg)
{
- while(*name) {
- if(!isdigit(*name++)) return 1;
+ struct usb_handle* h = (struct usb_handle *)arg;
+ int ep_in = usb_endpoint_number(h->ep_in);
+ int ep_out = usb_endpoint_number(h->ep_out);
+ int reaped_ep, res;
+
+ while (1) {
+ D("[ reap urb - wait ]\n");
+ adb_mutex_unlock(&h->lock);
+ res = usb_endpoint_wait(h->device, &reaped_ep);
+ adb_mutex_lock(&h->lock);
+ if(h->dead) {
+ res = -1;
+ break;
+ }
+
+ D("[ reaped ep %d ret = %d ]\n", reaped_ep, res);
+
+ if (reaped_ep == ep_in) {
+ D("[ reap urb - IN complete ]\n");
+ h->read_result = res;
+ adb_cond_broadcast(&h->notify_in);
+ }
+ if (reaped_ep == ep_out) {
+ D("[ reap urb - OUT compelete ]\n");
+ h->write_result = res;
+ adb_cond_broadcast(&h->notify_out);
+ }
}
- return 0;
+
+ return NULL;
}
-static void find_usb_device(const char *base,
- void (*register_device_callback)
- (const char *, unsigned char, unsigned char, int, int, unsigned))
+static void register_device(struct usb_device *device, int interface,
+ struct usb_endpoint *ep_in, struct usb_endpoint *ep_out)
{
- char busname[32], devname[32];
- unsigned char local_ep_in, local_ep_out;
- DIR *busdir , *devdir ;
- struct dirent *de;
- int fd ;
+ usb_handle* usb = 0;
+ int ret = 0;
+ int writeable;
+ char *serial;
+ pthread_attr_t attr;
+ const char* dev_name = usb_device_get_name(device);
- busdir = opendir(base);
- if(busdir == 0) return;
+ /* Since Linux will not reassign the device ID (and dev_name)
+ ** as long as the device is open, we can add to the list here
+ ** once we open it and remove from the list when we're finally
+ ** closed and everything will work out fine.
+ **
+ ** If we have a usb_handle on the list 'o handles with a matching
+ ** name, we have no further work to do.
+ */
+ adb_mutex_lock(&usb_lock);
+ for (usb = handle_list.next; usb != &handle_list; usb = usb->next) {
+ if (!strcmp(usb_device_get_name(usb->device), dev_name)) {
+ adb_mutex_unlock(&usb_lock);
+ return;
+ }
+ }
+ adb_mutex_unlock(&usb_lock);
- while((de = readdir(busdir)) != 0) {
- if(badname(de->d_name)) continue;
+ usb = calloc(1, sizeof(usb_handle));
+ adb_cond_init(&usb->notify_in, 0);
+ adb_cond_init(&usb->notify_out, 0);
+ adb_mutex_init(&usb->lock, 0);
- snprintf(busname, sizeof busname, "%s/%s", base, de->d_name);
- devdir = opendir(busname);
- if(devdir == 0) continue;
+ usb->device = device;
+ usb->ep_in = ep_in;
+ usb->ep_out = ep_out;
+ usb->zero_mask = usb_endpoint_max_packet(usb->ep_out) - 1;
-// DBGX("[ scanning %s ]\n", busname);
- while((de = readdir(devdir))) {
- unsigned char devdesc[256];
- unsigned char* bufptr = devdesc;
- unsigned char* bufend;
- struct usb_device_descriptor* device;
- struct usb_config_descriptor* config;
- struct usb_interface_descriptor* interface;
- struct usb_endpoint_descriptor *ep1, *ep2;
- unsigned zero_mask = 0;
- unsigned vid, pid;
- size_t desclength;
+ D("[ usb open %s ]\n", dev_name);
+ writeable = usb_device_is_writeable(device);
+ if (writeable) {
+ ret = usb_device_claim_interface(device, interface);
+ if(ret != 0) goto fail;
+ }
- if(badname(de->d_name)) continue;
- snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
+ /* add to the end of the active handles */
+ adb_mutex_lock(&usb_lock);
+ usb->next = &handle_list;
+ usb->prev = handle_list.prev;
+ usb->prev->next = usb;
+ usb->next->prev = usb;
+ adb_mutex_unlock(&usb_lock);
- if(known_device(devname)) {
- DBGX("skipping %s\n", devname);
- continue;
- }
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+ pthread_create(&usb->reaper_thread, &attr, reaper_thread, usb);
-// DBGX("[ scanning %s ]\n", devname);
- if((fd = unix_open(devname, O_RDONLY)) < 0) {
- continue;
- }
+ serial = usb_device_get_serial(device);
+ register_usb_transport(usb, serial, writeable);
+ if (serial)
+ free(serial);
+ return;
- desclength = adb_read(fd, devdesc, sizeof(devdesc));
- bufend = bufptr + desclength;
+fail:
+ D("[ usb open %s error=%d, err_str = %s]\n",
+ dev_name, errno, strerror(errno));
+ if (usb->ep_in)
+ usb_endpoint_close(usb->ep_in);
+ if (usb->ep_out)
+ usb_endpoint_close(usb->ep_out);
+ if(device) {
+ usb_device_close(device);
+ }
+ free(usb);
+}
- // should have device and configuration descriptors, and atleast two endpoints
- if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
- D("desclength %d is too small\n", desclength);
- adb_close(fd);
- continue;
- }
+static void check_usb_device(const char *devname) {
+ struct usb_device *device;
+ struct usb_descriptor_iter iter;
+ struct usb_descriptor_header* header;
+ struct usb_interface_descriptor* interface;
+ struct usb_endpoint_descriptor *ep1, *ep2;
+ struct usb_endpoint *ep_in = NULL, *ep_out = NULL;
+ uint16_t vid, pid;
- device = (struct usb_device_descriptor*)bufptr;
- bufptr += USB_DT_DEVICE_SIZE;
+ if(known_device(devname)) {
+ DBGX("skipping %s\n", devname);
+ return;
+ }
- if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
- adb_close(fd);
- continue;
- }
+ device = usb_device_open(devname);
+ if (!device) return;
- vid = __le16_to_cpu(device->idVendor);
- pid = __le16_to_cpu(device->idProduct);
- pid = devdesc[10] | (devdesc[11] << 8);
- DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid);
+ vid = usb_device_get_vendor_id(device);
+ pid = usb_device_get_product_id(device);
+ DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid);
- // should have config descriptor next
- config = (struct usb_config_descriptor *)bufptr;
- bufptr += USB_DT_CONFIG_SIZE;
- if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
- D("usb_config_descriptor not found\n");
- adb_close(fd);
- continue;
- }
+ // loop through all the descriptors and look for the ADB interface
+ usb_descriptor_iter_init(device, &iter);
- // loop through all the descriptors and look for the ADB interface
- while (bufptr < bufend) {
- unsigned char length = bufptr[0];
- unsigned char type = bufptr[1];
+ while ((header = usb_descriptor_iter_next(&iter)) != NULL) {
+ if (header->bDescriptorType == USB_DT_INTERFACE) {
+ interface = (struct usb_interface_descriptor *)header;
- if (type == USB_DT_INTERFACE) {
- interface = (struct usb_interface_descriptor *)bufptr;
- bufptr += length;
+ DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
+ "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
+ interface->bInterfaceClass, interface->bInterfaceSubClass,
+ interface->bInterfaceProtocol, interface->bNumEndpoints);
- if (length != USB_DT_INTERFACE_SIZE) {
- D("interface descriptor has wrong size\n");
- break;
- }
+ if (interface->bNumEndpoints == 2 &&
+ is_adb_interface(vid, pid, interface->bInterfaceClass,
+ interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
- DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
- "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
- interface->bInterfaceClass, interface->bInterfaceSubClass,
- interface->bInterfaceProtocol, interface->bNumEndpoints);
+ DBGX("looking for bulk endpoints\n");
+ // looks like ADB...
+ ep1 = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
+ ep2 = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
- if (interface->bNumEndpoints == 2 &&
- is_adb_interface(vid, pid, interface->bInterfaceClass,
- interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
-
- DBGX("looking for bulk endpoints\n");
- // looks like ADB...
- ep1 = (struct usb_endpoint_descriptor *)bufptr;
- bufptr += USB_DT_ENDPOINT_SIZE;
- ep2 = (struct usb_endpoint_descriptor *)bufptr;
- bufptr += USB_DT_ENDPOINT_SIZE;
-
- if (bufptr > devdesc + desclength ||
- ep1->bLength != USB_DT_ENDPOINT_SIZE ||
- ep1->bDescriptorType != USB_DT_ENDPOINT ||
- ep2->bLength != USB_DT_ENDPOINT_SIZE ||
- ep2->bDescriptorType != USB_DT_ENDPOINT) {
- D("endpoints not found\n");
- break;
- }
-
- // both endpoints should be bulk
- if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
- ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
- D("bulk endpoints not found\n");
- continue;
- }
- /* aproto 01 needs 0 termination */
- if(interface->bInterfaceProtocol == 0x01) {
- zero_mask = ep1->wMaxPacketSize - 1;
- }
-
- // we have a match. now we just need to figure out which is in and which is out.
- if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
- local_ep_in = ep1->bEndpointAddress;
- local_ep_out = ep2->bEndpointAddress;
- } else {
- local_ep_in = ep2->bEndpointAddress;
- local_ep_out = ep1->bEndpointAddress;
- }
-
- register_device_callback(devname, local_ep_in, local_ep_out,
- interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
- break;
- }
- } else {
- bufptr += length;
+ if (!ep1 || !ep2 ||
+ ep1->bDescriptorType != USB_DT_ENDPOINT ||
+ ep2->bDescriptorType != USB_DT_ENDPOINT) {
+ D("endpoints not found\n");
+ continue;
}
- } // end of while
- adb_close(fd);
- } // end of devdir while
- closedir(devdir);
- } //end of busdir while
- closedir(busdir);
+ // both endpoints should be bulk
+ if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
+ ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
+ D("bulk endpoints not found\n");
+ continue;
+ }
+
+ // we have a match. now we just need to figure out which is in and which is out.
+ if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
+ ep_in = usb_endpoint_open(device, ep1);
+ ep_out = usb_endpoint_open(device, ep2);
+ } else {
+ ep_in = usb_endpoint_open(device, ep2);
+ ep_out = usb_endpoint_open(device, ep1);
+ }
+
+ register_device(device, interface->bInterfaceNumber, ep_in, ep_out);
+ // so we don't free it at the bottom
+ device = NULL;
+ break;
+ }
+ }
+ } // end of while
+
+ if (device)
+ usb_device_close(device);
}
void usb_cleanup()
@@ -286,17 +285,8 @@
static int usb_bulk_write(usb_handle *h, const void *data, int len)
{
- struct usbdevfs_urb *urb = &h->urb_out;
+ struct usb_endpoint *ep = h->ep_out;
int res;
- struct timeval tv;
- struct timespec ts;
-
- memset(urb, 0, sizeof(*urb));
- urb->type = USBDEVFS_URB_TYPE_BULK;
- urb->endpoint = h->ep_out;
- urb->status = -1;
- urb->buffer = (void*) data;
- urb->buffer_length = len;
D("++ write ++\n");
@@ -305,32 +295,15 @@
res = -1;
goto fail;
}
- do {
- res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
- } while((res < 0) && (errno == EINTR));
-
+ res = usb_endpoint_queue(ep, (void *)data, len);
if(res < 0) {
goto fail;
}
- res = -1;
- h->urb_out_busy = 1;
- for(;;) {
- /* time out after five seconds */
- gettimeofday(&tv, NULL);
- ts.tv_sec = tv.tv_sec + 5;
- ts.tv_nsec = tv.tv_usec * 1000L;
- res = pthread_cond_timedwait(&h->notify, &h->lock, &ts);
- if(res < 0 || h->dead) {
- break;
- }
- if(h->urb_out_busy == 0) {
- if(urb->status == 0) {
- res = urb->actual_length;
- }
- break;
- }
- }
+ res = pthread_cond_wait(&h->notify_out, &h->lock);
+ if (!res)
+ res = h->write_result;
+
fail:
adb_mutex_unlock(&h->lock);
D("-- write --\n");
@@ -339,75 +312,27 @@
static int usb_bulk_read(usb_handle *h, void *data, int len)
{
- struct usbdevfs_urb *urb = &h->urb_in;
- struct usbdevfs_urb *out = NULL;
+ struct usb_endpoint *ep = h->ep_in;
int res;
- memset(urb, 0, sizeof(*urb));
- urb->type = USBDEVFS_URB_TYPE_BULK;
- urb->endpoint = h->ep_in;
- urb->status = -1;
- urb->buffer = data;
- urb->buffer_length = len;
-
-
adb_mutex_lock(&h->lock);
if(h->dead) {
res = -1;
goto fail;
}
- do {
- res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
- } while((res < 0) && (errno == EINTR));
-
- if(res < 0) {
+ res = usb_endpoint_queue(ep, data, len);
+ if (res < 0) {
goto fail;
}
+ res = pthread_cond_wait(&h->notify_in, &h->lock);
+ if (!res)
+ res = h->read_result;
- h->urb_in_busy = 1;
- for(;;) {
- D("[ reap urb - wait ]\n");
- h->reaper_thread = pthread_self();
- adb_mutex_unlock(&h->lock);
- res = ioctl(h->desc, USBDEVFS_REAPURB, &out);
- adb_mutex_lock(&h->lock);
- h->reaper_thread = 0;
- if(h->dead) {
- res = -1;
- break;
- }
- if(res < 0) {
- if(errno == EINTR) {
- continue;
- }
- D("[ reap urb - error ]\n");
- break;
- }
- D("[ urb @%p status = %d, actual = %d ]\n",
- out, out->status, out->actual_length);
-
- if(out == &h->urb_in) {
- D("[ reap urb - IN complete ]\n");
- h->urb_in_busy = 0;
- if(urb->status == 0) {
- res = urb->actual_length;
- } else {
- res = -1;
- }
- break;
- }
- if(out == &h->urb_out) {
- D("[ reap urb - OUT compelete ]\n");
- h->urb_out_busy = 0;
- adb_cond_broadcast(&h->notify);
- }
- }
fail:
adb_mutex_unlock(&h->lock);
return res;
}
-
int usb_write(usb_handle *h, const void *_data, int len)
{
unsigned char *data = (unsigned char*) _data;
@@ -438,7 +363,7 @@
data += xfer;
}
- if(need_zero){
+ if(need_zero) {
n = usb_bulk_write(h, _data, 0);
return n;
}
@@ -455,11 +380,9 @@
while(len > 0) {
int xfer = (len > 4096) ? 4096 : len;
- D("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname);
n = usb_bulk_read(h, data, xfer);
- D("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname);
if(n != xfer) {
- if((errno == ETIMEDOUT) && (h->desc != -1)) {
+ if(errno == ETIMEDOUT && h->device) {
D("[ timeout ]\n");
if(n > 0){
data += n;
@@ -482,12 +405,12 @@
void usb_kick(usb_handle *h)
{
- D("[ kicking %p (fd = %d) ]\n", h, h->desc);
+ D("[ kicking %p (fd = %s) ]\n", h, usb_device_get_name(h->device));
adb_mutex_lock(&h->lock);
if(h->dead == 0) {
h->dead = 1;
- if (h->writeable) {
+ if (usb_device_is_writeable(h->device)) {
/* HACK ALERT!
** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
** This is a workaround for that problem.
@@ -501,13 +424,10 @@
** but this ensures that a reader blocked on REAPURB
** will get unblocked
*/
- ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_in);
- ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_out);
- h->urb_in.status = -ENODEV;
- h->urb_out.status = -ENODEV;
- h->urb_in_busy = 0;
- h->urb_out_busy = 0;
- adb_cond_broadcast(&h->notify);
+ usb_endpoint_cancel(h->ep_in);
+ usb_endpoint_cancel(h->ep_out);
+ adb_cond_broadcast(&h->notify_in);
+ adb_cond_broadcast(&h->notify_out);
} else {
unregister_usb_transport(h);
}
@@ -524,148 +444,14 @@
h->prev = 0;
h->next = 0;
- adb_close(h->desc);
- D("[ usb closed %p (fd = %d) ]\n", h, h->desc);
+ usb_device_close(h->device);
+ D("[ usb closed %p ]\n", h);
adb_mutex_unlock(&usb_lock);
free(h);
return 0;
}
-static void register_device(const char *dev_name,
- unsigned char ep_in, unsigned char ep_out,
- int interface, int serial_index, unsigned zero_mask)
-{
- usb_handle* usb = 0;
- int n = 0;
- char serial[256];
-
- /* Since Linux will not reassign the device ID (and dev_name)
- ** as long as the device is open, we can add to the list here
- ** once we open it and remove from the list when we're finally
- ** closed and everything will work out fine.
- **
- ** If we have a usb_handle on the list 'o handles with a matching
- ** name, we have no further work to do.
- */
- adb_mutex_lock(&usb_lock);
- for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
- if(!strcmp(usb->fname, dev_name)) {
- adb_mutex_unlock(&usb_lock);
- return;
- }
- }
- adb_mutex_unlock(&usb_lock);
-
- D("[ usb located new device %s (%d/%d/%d) ]\n",
- dev_name, ep_in, ep_out, interface);
- usb = calloc(1, sizeof(usb_handle));
- strcpy(usb->fname, dev_name);
- usb->ep_in = ep_in;
- usb->ep_out = ep_out;
- usb->zero_mask = zero_mask;
- usb->writeable = 1;
-
- adb_cond_init(&usb->notify, 0);
- adb_mutex_init(&usb->lock, 0);
- /* initialize mark to 1 so we don't get garbage collected after the device scan */
- usb->mark = 1;
- usb->reaper_thread = 0;
-
- usb->desc = unix_open(usb->fname, O_RDWR);
- if(usb->desc < 0) {
- /* if we fail, see if have read-only access */
- usb->desc = unix_open(usb->fname, O_RDONLY);
- if(usb->desc < 0) goto fail;
- usb->writeable = 0;
- D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc);
- } else {
- D("[ usb open %s fd = %d]\n", usb->fname, usb->desc);
- n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface);
- if(n != 0) goto fail;
- }
-
- /* read the device's serial number */
- serial[0] = 0;
- memset(serial, 0, sizeof(serial));
- if (serial_index) {
- struct usbdevfs_ctrltransfer ctrl;
- __u16 buffer[128];
- __u16 languages[128];
- int i, result;
- int languageCount = 0;
-
- 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(usb->desc, 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) | serial_index;
- ctrl.wIndex = languages[i];
- ctrl.wLength = sizeof(buffer);
- ctrl.data = buffer;
-
- result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
- if (result > 0) {
- int i;
- // skip first word, and copy the rest to the serial string, changing shorts to bytes.
- result /= 2;
- for (i = 1; i < result; i++)
- serial[i - 1] = buffer[i];
- serial[i - 1] = 0;
- break;
- }
- }
- }
-
- /* add to the end of the active handles */
- adb_mutex_lock(&usb_lock);
- usb->next = &handle_list;
- usb->prev = handle_list.prev;
- usb->prev->next = usb;
- usb->next->prev = usb;
- adb_mutex_unlock(&usb_lock);
-
- register_usb_transport(usb, serial, usb->writeable);
- return;
-
-fail:
- D("[ usb open %s error=%d, err_str = %s]\n",
- usb->fname, errno, strerror(errno));
- if(usb->desc >= 0) {
- adb_close(usb->desc);
- }
- free(usb);
-}
-
-void* device_poll_thread(void* unused)
-{
- D("Created device thread\n");
- for(;;) {
- /* XXX use inotify */
- find_usb_device("/dev/bus/usb", register_device);
- kick_disconnected_devices();
- sleep(1);
- }
- return NULL;
-}
-
static void sigalrm_handler(int signo)
{
// don't need to do anything here
@@ -673,17 +459,15 @@
void usb_init()
{
- adb_thread_t tid;
struct sigaction actions;
+ if (usb_host_init(check_usb_device, kick_disconnected_device))
+ fatal_errno("usb_host_init failed\n");
+
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = sigalrm_handler;
sigaction(SIGALRM,& actions, NULL);
-
- if(adb_thread_create(&tid, device_poll_thread, NULL)){
- fatal_errno("cannot create input thread");
- }
}
diff --git a/adb/usb_vendors.c b/adb/usb_vendors.c
index 7f3cb54..6d85fb1 100644
--- a/adb/usb_vendors.c
+++ b/adb/usb_vendors.c
@@ -67,6 +67,8 @@
#define VENDOR_ID_KYOCERA 0x0482
// Pantech's USB Vendor ID
#define VENDOR_ID_PANTECH 0x10A9
+// Qualcomm's USB Vendor ID
+#define VENDOR_ID_QUALCOMM 0x05c6
/** built-in vendor list */
@@ -87,6 +89,7 @@
VENDOR_ID_ZTE,
VENDOR_ID_KYOCERA,
VENDOR_ID_PANTECH,
+ VENDOR_ID_QUALCOMM,
};
#define BUILT_IN_VENDOR_COUNT (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 5389351..63208bc 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -150,6 +150,8 @@
(info->dev_vendor != 0x18d1) && // Google
(info->dev_vendor != 0x0451) &&
(info->dev_vendor != 0x0502) &&
+ (info->dev_vendor != 0x0fce) && // Sony Ericsson
+ (info->dev_vendor != 0x05c6) && // Qualcomm
(info->dev_vendor != 0x22b8) && // Motorola
(info->dev_vendor != 0x0955) && // Nvidia
(info->dev_vendor != 0x413c) && // DELL
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..c2387e5
--- /dev/null
+++ b/include/usbhost/usbhost.h
@@ -0,0 +1,143 @@
+/*
+ * 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 usb_device for the given endpoint */
+struct usb_device *usb_endpoint_get_device(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/libacc/acc.cpp b/libacc/acc.cpp
index 8f33b0b..6e13f9a 100644
--- a/libacc/acc.cpp
+++ b/libacc/acc.cpp
@@ -23,10 +23,11 @@
#include <cutils/hashmap.h>
-#if defined(__i386__)
#include <sys/mman.h>
-#endif
+#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
+#define MAP_ANONYMOUS MAP_ANON
+#endif
#if defined(__arm__)
#define DEFAULT_ARM_CODEGEN
@@ -230,7 +231,7 @@
void release() {
if (pProgramBase != 0) {
- free(pProgramBase);
+ munmap(pProgramBase, mSize);
pProgramBase = 0;
}
}
@@ -263,7 +264,9 @@
virtual void init(int size) {
release();
mSize = size;
- pProgramBase = (char*) calloc(1, size);
+ pProgramBase = (char*) mmap(NULL, size,
+ PROT_EXEC | PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
ind = pProgramBase;
}
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c
index 3a18b0b..f9c111e 100644
--- a/libcutils/sched_policy.c
+++ b/libcutils/sched_policy.c
@@ -27,8 +27,10 @@
#include "cutils/log.h"
#ifdef HAVE_SCHED_H
+#ifdef HAVE_PTHREADS
#include <sched.h>
+#include <pthread.h>
#include <cutils/sched_policy.h>
@@ -42,48 +44,74 @@
#define POLICY_DEBUG 0
+static pthread_once_t the_once = PTHREAD_ONCE_INIT;
+
static int __sys_supports_schedgroups = -1;
-static int add_tid_to_cgroup(int tid, const char *grp_name)
+// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
+static int normal_cgroup_fd = -1;
+static int bg_cgroup_fd = -1;
+
+/* Add tid to the scheduling group defined by the policy */
+static int add_tid_to_cgroup(int tid, SchedPolicy policy)
{
int fd;
- char path[255];
- char text[64];
- sprintf(path, "/dev/cpuctl/%s/tasks", grp_name);
+ if (policy == SP_BACKGROUND) {
+ fd = bg_cgroup_fd;
+ } else {
+ fd = normal_cgroup_fd;
+ }
- if ((fd = open(path, O_WRONLY)) < 0) {
- SLOGE("add_tid_to_cgroup failed to open '%s' (%s)\n", path,
- strerror(errno));
+ if (fd < 0) {
+ SLOGE("add_tid_to_cgroup failed; background=%d\n",
+ policy == SP_BACKGROUND ? 1 : 0);
return -1;
}
- sprintf(text, "%d", tid);
- if (write(fd, text, strlen(text)) < 0) {
- close(fd);
- /*
- * If the thread is in the process of exiting,
- * don't flag an error
- */
- if (errno == ESRCH)
- return 0;
- SLOGW("add_tid_to_cgroup failed to write '%s' (%s)\n", path,
- strerror(errno));
+ // specialized itoa -- works for tid > 0
+ char text[22];
+ char *end = text + sizeof(text) - 1;
+ char *ptr = end;
+ *ptr = '\0';
+ while (tid > 0) {
+ *--ptr = '0' + (tid % 10);
+ tid = tid / 10;
+ }
+
+ if (write(fd, ptr, end - ptr) < 0) {
+ /*
+ * If the thread is in the process of exiting,
+ * don't flag an error
+ */
+ if (errno == ESRCH)
+ return 0;
+ SLOGW("add_tid_to_cgroup failed to write '%s' (%s); background=%d\n",
+ ptr, strerror(errno), policy == SP_BACKGROUND ? 1 : 0);
return -1;
}
- close(fd);
return 0;
}
-static inline void initialize()
-{
- if (__sys_supports_schedgroups < 0) {
- if (!access("/dev/cpuctl/tasks", F_OK)) {
- __sys_supports_schedgroups = 1;
- } else {
- __sys_supports_schedgroups = 0;
+static void __initialize(void) {
+ char* filename;
+ if (!access("/dev/cpuctl/tasks", F_OK)) {
+ __sys_supports_schedgroups = 1;
+
+ filename = "/dev/cpuctl/tasks";
+ normal_cgroup_fd = open(filename, O_WRONLY);
+ if (normal_cgroup_fd < 0) {
+ SLOGE("open of %s failed: %s\n", filename, strerror(errno));
}
+
+ filename = "/dev/cpuctl/bg_non_interactive/tasks";
+ bg_cgroup_fd = open(filename, O_WRONLY);
+ if (bg_cgroup_fd < 0) {
+ SLOGE("open of %s failed: %s\n", filename, strerror(errno));
+ }
+ } else {
+ __sys_supports_schedgroups = 0;
}
}
@@ -161,7 +189,7 @@
int get_sched_policy(int tid, SchedPolicy *policy)
{
- initialize();
+ pthread_once(&the_once, __initialize);
if (__sys_supports_schedgroups) {
char grpBuf[32];
@@ -193,7 +221,7 @@
int set_sched_policy(int tid, SchedPolicy policy)
{
- initialize();
+ pthread_once(&the_once, __initialize);
#if POLICY_DEBUG
char statfile[64];
@@ -228,13 +256,7 @@
#endif
if (__sys_supports_schedgroups) {
- const char *grp = "";
-
- if (policy == SP_BACKGROUND) {
- grp = "bg_non_interactive";
- }
-
- if (add_tid_to_cgroup(tid, grp)) {
+ if (add_tid_to_cgroup(tid, policy)) {
if (errno != ESRCH && errno != ENOENT)
return -errno;
}
@@ -251,4 +273,5 @@
return 0;
}
+#endif /* HAVE_PTHREADS */
#endif /* HAVE_SCHED_H */
diff --git a/libdiskconfig/Android.mk b/libdiskconfig/Android.mk
index f264103..1d0ecb4 100644
--- a/libdiskconfig/Android.mk
+++ b/libdiskconfig/Android.mk
@@ -12,7 +12,6 @@
config_mbr.c
LOCAL_MODULE := libdiskconfig
-LOCAL_PRELINK_MODULE := false
LOCAL_SYSTEM_SHARED_LIBRARIES := libcutils liblog libc
include $(BUILD_SHARED_LIBRARY)
diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c
index 45e392a..6755ba1 100644
--- a/libnetutils/dhcpclient.c
+++ b/libnetutils/dhcpclient.c
@@ -70,7 +70,7 @@
vsnprintf(errmsg, sizeof(errmsg), fmt, ap);
va_end(ap);
- LOGD(errmsg);
+ LOGD("%s", errmsg);
}
const char *dhcp_lasterror()
diff --git a/libpixelflinger/Android.mk b/libpixelflinger/Android.mk
index 0cc85d9..6491d24 100644
--- a/libpixelflinger/Android.mk
+++ b/libpixelflinger/Android.mk
@@ -40,7 +40,13 @@
buffer.cpp
ifeq ($(TARGET_ARCH),arm)
+ifeq ($(TARGET_ARCH_VERSION),armv7-a)
+PIXELFLINGER_SRC_FILES += col32cb16blend_neon.S
+PIXELFLINGER_SRC_FILES += col32cb16blend.S
+else
PIXELFLINGER_SRC_FILES += t32cb16blend.S
+PIXELFLINGER_SRC_FILES += col32cb16blend.S
+endif
endif
ifeq ($(TARGET_ARCH),arm)
diff --git a/libpixelflinger/codeflinger/ARMAssembler.cpp b/libpixelflinger/codeflinger/ARMAssembler.cpp
index ff7b0b3..d3720c3 100644
--- a/libpixelflinger/codeflinger/ARMAssembler.cpp
+++ b/libpixelflinger/codeflinger/ARMAssembler.cpp
@@ -424,5 +424,15 @@
*mPC++ = (cc<<28) | 0x1200080 | (Rd<<16) | (Rn<<12) | (Rs<<8) | (y<<4) | Rm;
}
+#if 0
+#pragma mark -
+#pragma mark Byte/half word extract and extend (ARMv6+ only)...
+#endif
+
+void ARMAssembler::UXTB16(int cc, int Rd, int Rm, int rotate)
+{
+ *mPC++ = (cc<<28) | 0x6CF0070 | (Rd<<12) | ((rotate >> 3) << 10) | Rm;
+}
+
}; // namespace android
diff --git a/libpixelflinger/codeflinger/ARMAssembler.h b/libpixelflinger/codeflinger/ARMAssembler.h
index ef3b66a..a667cb5 100644
--- a/libpixelflinger/codeflinger/ARMAssembler.h
+++ b/libpixelflinger/codeflinger/ARMAssembler.h
@@ -123,6 +123,7 @@
int RdHi, int RdLo, int Rs, int Rm);
virtual void SMLAW(int cc, int y,
int Rd, int Rm, int Rs, int Rn);
+ virtual void UXTB16(int cc, int Rd, int Rm, int rotate);
private:
ARMAssembler(const ARMAssembler& rhs);
diff --git a/libpixelflinger/codeflinger/ARMAssemblerInterface.h b/libpixelflinger/codeflinger/ARMAssemblerInterface.h
index 465b3bd..ff6af2a 100644
--- a/libpixelflinger/codeflinger/ARMAssemblerInterface.h
+++ b/libpixelflinger/codeflinger/ARMAssemblerInterface.h
@@ -203,6 +203,9 @@
virtual void SMLAW(int cc, int y,
int Rd, int Rm, int Rs, int Rn) = 0;
+ // byte/half word extract...
+ virtual void UXTB16(int cc, int Rd, int Rm, int rotate) = 0;
+
// -----------------------------------------------------------------------
// convenience...
// -----------------------------------------------------------------------
diff --git a/libpixelflinger/codeflinger/ARMAssemblerProxy.cpp b/libpixelflinger/codeflinger/ARMAssemblerProxy.cpp
index 18c4618..7c422db 100644
--- a/libpixelflinger/codeflinger/ARMAssemblerProxy.cpp
+++ b/libpixelflinger/codeflinger/ARMAssemblerProxy.cpp
@@ -195,6 +195,9 @@
mTarget->SMLAW(cc, y, Rd, Rm, Rs, Rn);
}
+void ARMAssemblerProxy::UXTB16(int cc, int Rd, int Rm, int rotate) {
+ mTarget->UXTB16(cc, Rd, Rm, rotate);
+}
}; // namespace android
diff --git a/libpixelflinger/codeflinger/ARMAssemblerProxy.h b/libpixelflinger/codeflinger/ARMAssemblerProxy.h
index 4bdca9c..9134cce 100644
--- a/libpixelflinger/codeflinger/ARMAssemblerProxy.h
+++ b/libpixelflinger/codeflinger/ARMAssemblerProxy.h
@@ -114,6 +114,8 @@
virtual void SMLAW(int cc, int y,
int Rd, int Rm, int Rs, int Rn);
+ virtual void UXTB16(int cc, int Rd, int Rm, int rotate);
+
private:
ARMAssemblerInterface* mTarget;
};
diff --git a/libpixelflinger/codeflinger/CodeCache.cpp b/libpixelflinger/codeflinger/CodeCache.cpp
index 29410c8..5877ff4 100644
--- a/libpixelflinger/codeflinger/CodeCache.cpp
+++ b/libpixelflinger/codeflinger/CodeCache.cpp
@@ -19,6 +19,8 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
@@ -39,15 +41,14 @@
Assembly::Assembly(size_t size)
: mCount(1), mSize(0)
{
- mBase = (uint32_t*)malloc(size);
- if (mBase) {
- mSize = size;
- }
+ mBase = (uint32_t*)mspace_malloc(getMspace(), size);
+ mSize = size;
+ ensureMbaseExecutable();
}
Assembly::~Assembly()
{
- free(mBase);
+ mspace_free(getMspace(), mBase);
}
void Assembly::incStrong(const void*) const
@@ -75,11 +76,32 @@
ssize_t Assembly::resize(size_t newSize)
{
- mBase = (uint32_t*)realloc(mBase, newSize);
+ mBase = (uint32_t*)mspace_realloc(getMspace(), mBase, newSize);
mSize = newSize;
+ ensureMbaseExecutable();
return size();
}
+mspace Assembly::getMspace()
+{
+ static mspace msp = create_contiguous_mspace(2 * 1024, 1024 * 1024, /*locked=*/ false);
+ return msp;
+}
+
+void Assembly::ensureMbaseExecutable()
+{
+ long pagesize = sysconf(_SC_PAGESIZE);
+ long pagemask = ~(pagesize - 1); // assumes pagesize is a power of 2
+
+ uint32_t* pageStart = (uint32_t*) (((uintptr_t) mBase) & pagemask);
+ size_t adjustedLength = mBase - pageStart + mSize;
+
+ if (mBase && mprotect(pageStart, adjustedLength, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
+ mspace_free(getMspace(), mBase);
+ mBase = NULL;
+ }
+}
+
// ----------------------------------------------------------------------------
CodeCache::CodeCache(size_t size)
diff --git a/libpixelflinger/codeflinger/CodeCache.h b/libpixelflinger/codeflinger/CodeCache.h
index 8ff1366..aaafd26 100644
--- a/libpixelflinger/codeflinger/CodeCache.h
+++ b/libpixelflinger/codeflinger/CodeCache.h
@@ -22,6 +22,7 @@
#include <stdint.h>
#include <pthread.h>
#include <sys/types.h>
+#include <cutils/mspace.h>
#include "tinyutils/KeyedVector.h"
#include "tinyutils/smartpointer.h"
@@ -67,9 +68,12 @@
typedef void weakref_type;
private:
+ static mspace getMspace();
+ void ensureMbaseExecutable();
+
mutable int32_t mCount;
uint32_t* mBase;
- ssize_t mSize;
+ size_t mSize;
};
// ----------------------------------------------------------------------------
diff --git a/libpixelflinger/codeflinger/disassem.c b/libpixelflinger/codeflinger/disassem.c
index 4676da0..c17f3ec 100644
--- a/libpixelflinger/codeflinger/disassem.c
+++ b/libpixelflinger/codeflinger/disassem.c
@@ -80,6 +80,7 @@
* f - 1st fp operand (register) (bits 12-14)
* g - 2nd fp operand (register) (bits 16-18)
* h - 3rd fp operand (register/immediate) (bits 0-4)
+ * j - xtb rotate literal (bits 10-11)
* b - branch address
* t - thumb branch address (bits 24, 0-23)
* k - breakpoint comment (bits 0-3, 8-19)
@@ -122,6 +123,7 @@
{ 0x0fe000f0, 0x00c00090, "smull", "Sdnms" },
{ 0x0fe000f0, 0x00a00090, "umlal", "Sdnms" },
{ 0x0fe000f0, 0x00e00090, "smlal", "Sdnms" },
+ { 0x0fff03f0, 0x06cf0070, "uxtb16", "dmj" },
{ 0x0d700000, 0x04200000, "strt", "daW" },
{ 0x0d700000, 0x04300000, "ldrt", "daW" },
{ 0x0d700000, 0x04600000, "strbt", "daW" },
@@ -276,7 +278,7 @@
#define insn_condition(x) arm32_insn_conditions[(x >> 28) & 0x0f]
#define insn_blktrans(x) insn_block_transfers[(x >> 23) & 3]
-#define insn_stkblktrans(x) insn_stack_block_transfers[(x >> 23) & 3]
+#define insn_stkblktrans(x) insn_stack_block_transfers[(3*((x >> 20)&1))^((x >> 23)&3)]
#define op2_shift(x) op_shifts[(x >> 5) & 3]
#define insn_fparnd(x) insn_fpa_rounding[(x >> 5) & 0x03]
#define insn_fpaprec(x) insn_fpa_precision[(((x >> 18) & 2)|(x >> 7)) & 1]
@@ -406,6 +408,10 @@
else
di->di_printf("f%d", insn & 7);
break;
+ /* j - xtb rotate literal (bits 10-11) */
+ case 'j':
+ di->di_printf("ror #%d", ((insn >> 10) & 3) << 3);
+ break;
/* b - branch address */
case 'b':
branch = ((insn << 2) & 0x03ffffff);
diff --git a/libpixelflinger/codeflinger/texturing.cpp b/libpixelflinger/codeflinger/texturing.cpp
index 90e6584..7f6f8da 100644
--- a/libpixelflinger/codeflinger/texturing.cpp
+++ b/libpixelflinger/codeflinger/texturing.cpp
@@ -25,6 +25,9 @@
#include "codeflinger/GGLAssembler.h"
+#ifdef __ARM_ARCH__
+#include <machine/cpu-features.h>
+#endif
namespace android {
@@ -567,7 +570,7 @@
RSB(GE, 0, height, height, imm(0));
MUL(AL, 0, height, stride, height);
} else {
- // u has not been CLAMPed yet
+ // v has not been CLAMPed yet
CMP(AL, height, reg_imm(v, ASR, FRAC_BITS));
MOV(LE, 0, v, reg_imm(height, LSL, FRAC_BITS));
MOV(LE, 0, height, imm(0));
@@ -868,6 +871,106 @@
load(txPtr, texel, 0);
}
+#if __ARM_ARCH__ >= 6
+// ARMv6 version, using UXTB16, and scheduled for Cortex-A8 pipeline
+void GGLAssembler::filter32(
+ const fragment_parts_t& parts,
+ pixel_t& texel, const texture_unit_t& tmu,
+ int U, int V, pointer_t& txPtr,
+ int FRAC_BITS)
+{
+ const int adjust = FRAC_BITS*2 - 8;
+ const int round = 0;
+ const int prescale = 16 - adjust;
+
+ Scratch scratches(registerFile());
+
+ int pixel= scratches.obtain();
+ int dh = scratches.obtain();
+ int u = scratches.obtain();
+ int k = scratches.obtain();
+
+ int temp = scratches.obtain();
+ int dl = scratches.obtain();
+
+ int offsetrt = scratches.obtain();
+ int offsetlb = scratches.obtain();
+
+ int pixellb = offsetlb;
+
+ // RB -> U * V
+ CONTEXT_LOAD(offsetrt, generated_vars.rt);
+ CONTEXT_LOAD(offsetlb, generated_vars.lb);
+ if(!round) {
+ MOV(AL, 0, U, reg_imm(U, LSL, prescale));
+ }
+ ADD(AL, 0, u, offsetrt, offsetlb);
+
+ LDR(AL, pixel, txPtr.reg, reg_scale_pre(u));
+ if (round) {
+ SMULBB(AL, u, U, V);
+ RSB(AL, 0, U, U, imm(1<<FRAC_BITS));
+ } else {
+ SMULWB(AL, u, U, V);
+ RSB(AL, 0, U, U, imm(1<<(FRAC_BITS+prescale)));
+ }
+ UXTB16(AL, temp, pixel, 0);
+ if (round) {
+ ADD(AL, 0, u, u, imm(1<<(adjust-1)));
+ MOV(AL, 0, u, reg_imm(u, LSR, adjust));
+ }
+ LDR(AL, pixellb, txPtr.reg, reg_scale_pre(offsetlb));
+ MUL(AL, 0, dh, temp, u);
+ UXTB16(AL, temp, pixel, 8);
+ MUL(AL, 0, dl, temp, u);
+ RSB(AL, 0, k, u, imm(0x100));
+
+ // LB -> (1-U) * V
+ if (round) {
+ SMULBB(AL, u, U, V);
+ } else {
+ SMULWB(AL, u, U, V);
+ }
+ UXTB16(AL, temp, pixellb, 0);
+ if (round) {
+ ADD(AL, 0, u, u, imm(1<<(adjust-1)));
+ MOV(AL, 0, u, reg_imm(u, LSR, adjust));
+ }
+ MLA(AL, 0, dh, temp, u, dh);
+ UXTB16(AL, temp, pixellb, 8);
+ MLA(AL, 0, dl, temp, u, dl);
+ SUB(AL, 0, k, k, u);
+
+ // LT -> (1-U)*(1-V)
+ RSB(AL, 0, V, V, imm(1<<FRAC_BITS));
+ LDR(AL, pixel, txPtr.reg);
+ if (round) {
+ SMULBB(AL, u, U, V);
+ } else {
+ SMULWB(AL, u, U, V);
+ }
+ UXTB16(AL, temp, pixel, 0);
+ if (round) {
+ ADD(AL, 0, u, u, imm(1<<(adjust-1)));
+ MOV(AL, 0, u, reg_imm(u, LSR, adjust));
+ }
+ MLA(AL, 0, dh, temp, u, dh);
+ UXTB16(AL, temp, pixel, 8);
+ MLA(AL, 0, dl, temp, u, dl);
+
+ // RT -> U*(1-V)
+ LDR(AL, pixel, txPtr.reg, reg_scale_pre(offsetrt));
+ SUB(AL, 0, u, k, u);
+ UXTB16(AL, temp, pixel, 0);
+ MLA(AL, 0, dh, temp, u, dh);
+ UXTB16(AL, temp, pixel, 8);
+ MLA(AL, 0, dl, temp, u, dl);
+
+ UXTB16(AL, dh, dh, 8);
+ UXTB16(AL, dl, dl, 8);
+ ORR(AL, 0, texel.reg, dh, reg_imm(dl, LSL, 8));
+}
+#else
void GGLAssembler::filter32(
const fragment_parts_t& parts,
pixel_t& texel, const texture_unit_t& tmu,
@@ -955,6 +1058,7 @@
AND(AL, 0, dl, dl, reg_imm(mask, LSL, 8));
ORR(AL, 0, texel.reg, dh, dl);
}
+#endif
void GGLAssembler::build_texture_environment(
component_t& fragment,
diff --git a/libpixelflinger/col32cb16blend.S b/libpixelflinger/col32cb16blend.S
new file mode 100644
index 0000000..1450bde
--- /dev/null
+++ b/libpixelflinger/col32cb16blend.S
@@ -0,0 +1,78 @@
+/* libs/pixelflinger/col32cb16blend.S
+**
+** (C) COPYRIGHT 2009 ARM Limited.
+**
+** 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.
+**
+*/
+
+ .text
+ .align
+
+ .global scanline_col32cb16blend_arm
+
+//
+// This function alpha blends a fixed color into a destination scanline, using
+// the formula:
+//
+// d = s + (((a + (a >> 7)) * d) >> 8)
+//
+// where d is the destination pixel,
+// s is the source color,
+// a is the alpha channel of the source color.
+//
+
+// r0 = destination buffer pointer
+// r1 = color value
+// r2 = count
+
+
+scanline_col32cb16blend_arm:
+ push {r4-r10, lr} // stack ARM regs
+
+ mov r5, r1, lsr #24 // shift down alpha
+ mov r9, #0xff // create mask
+ add r5, r5, r5, lsr #7 // add in top bit
+ rsb r5, r5, #256 // invert alpha
+ and r10, r1, #0xff // extract red
+ and r12, r9, r1, lsr #8 // extract green
+ and r4, r9, r1, lsr #16 // extract blue
+ mov r10, r10, lsl #5 // prescale red
+ mov r12, r12, lsl #6 // prescale green
+ mov r4, r4, lsl #5 // prescale blue
+ mov r9, r9, lsr #2 // create dest green mask
+
+1:
+ ldrh r8, [r0] // load dest pixel
+ subs r2, r2, #1 // decrement loop counter
+ mov r6, r8, lsr #11 // extract dest red
+ and r7, r9, r8, lsr #5 // extract dest green
+ and r8, r8, #0x1f // extract dest blue
+
+ smlabb r6, r6, r5, r10 // dest red * alpha + src red
+ smlabb r7, r7, r5, r12 // dest green * alpha + src green
+ smlabb r8, r8, r5, r4 // dest blue * alpha + src blue
+
+ mov r6, r6, lsr #8 // shift down red
+ mov r7, r7, lsr #8 // shift down green
+ mov r6, r6, lsl #11 // shift red into 565
+ orr r6, r7, lsl #5 // shift green into 565
+ orr r6, r8, lsr #8 // shift blue into 565
+
+ strh r6, [r0], #2 // store pixel to dest, update ptr
+ bne 1b // if count != 0, loop
+
+ pop {r4-r10, pc} // return
+
+
+
diff --git a/libpixelflinger/col32cb16blend_neon.S b/libpixelflinger/col32cb16blend_neon.S
new file mode 100644
index 0000000..17b0d01
--- /dev/null
+++ b/libpixelflinger/col32cb16blend_neon.S
@@ -0,0 +1,153 @@
+/* libs/pixelflinger/col32cb16blend_neon.S
+**
+** (C) COPYRIGHT 2009 ARM Limited.
+**
+** 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.
+**
+*/
+
+ .text
+ .align
+
+ .global scanline_col32cb16blend_neon
+
+//
+// This function alpha blends a fixed color into a destination scanline, using
+// the formula:
+//
+// d = s + (((a + (a >> 7)) * d) >> 8)
+//
+// where d is the destination pixel,
+// s is the source color,
+// a is the alpha channel of the source color.
+//
+// The NEON implementation processes 16 pixels per iteration. The remaining 0 - 15
+// pixels are processed in ARM code.
+//
+
+// r0 = destination buffer pointer
+// r1 = color pointer
+// r2 = count
+
+
+scanline_col32cb16blend_neon:
+ push {r4-r11, lr} // stack ARM regs
+
+ vmov.u16 q15, #256 // create alpha constant
+ movs r3, r2, lsr #4 // calc. sixteens iterations
+ vmov.u16 q14, #0x1f // create blue mask
+
+ beq 2f // if r3 == 0, branch to singles
+
+ vld4.8 {d0[], d2[], d4[], d6[]}, [r1] // load color into four registers
+ // split and duplicate them, such that
+ // d0 = 8 equal red values
+ // d2 = 8 equal green values
+ // d4 = 8 equal blue values
+ // d6 = 8 equal alpha values
+ vshll.u8 q0, d0, #5 // shift up red and widen
+ vshll.u8 q1, d2, #6 // shift up green and widen
+ vshll.u8 q2, d4, #5 // shift up blue and widen
+
+ vshr.u8 d7, d6, #7 // extract top bit of alpha
+ vaddl.u8 q3, d6, d7 // add top bit into alpha
+ vsub.u16 q3, q15, q3 // invert alpha
+
+1:
+ // This loop processes 16 pixels per iteration. In the comments, references to
+ // the first eight pixels are suffixed with "0" (red0, green0, blue0),
+ // the second eight are suffixed "1".
+ // q8 = dst red0
+ // q9 = dst green0
+ // q10 = dst blue0
+ // q13 = dst red1
+ // q12 = dst green1
+ // q11 = dst blue1
+
+ vld1.16 {d20, d21, d22, d23}, [r0] // load 16 dest pixels
+ vshr.u16 q8, q10, #11 // shift dst red0 to low 5 bits
+ pld [r0, #63] // preload next dest pixels
+ vshl.u16 q9, q10, #5 // shift dst green0 to top 6 bits
+ vand q10, q10, q14 // extract dst blue0
+ vshr.u16 q9, q9, #10 // shift dst green0 to low 6 bits
+ vmul.u16 q8, q8, q3 // multiply dst red0 by src alpha
+ vshl.u16 q12, q11, #5 // shift dst green1 to top 6 bits
+ vmul.u16 q9, q9, q3 // multiply dst green0 by src alpha
+ vshr.u16 q13, q11, #11 // shift dst red1 to low 5 bits
+ vmul.u16 q10, q10, q3 // multiply dst blue0 by src alpha
+ vshr.u16 q12, q12, #10 // shift dst green1 to low 6 bits
+ vand q11, q11, q14 // extract dst blue1
+ vadd.u16 q8, q8, q0 // add src red to dst red0
+ vmul.u16 q13, q13, q3 // multiply dst red1 by src alpha
+ vadd.u16 q9, q9, q1 // add src green to dst green0
+ vmul.u16 q12, q12, q3 // multiply dst green1 by src alpha
+ vadd.u16 q10, q10, q2 // add src blue to dst blue0
+ vmul.u16 q11, q11, q3 // multiply dst blue1 by src alpha
+ vshr.u16 q8, q8, #8 // shift down red0
+ vadd.u16 q13, q13, q0 // add src red to dst red1
+ vshr.u16 q9, q9, #8 // shift down green0
+ vadd.u16 q12, q12, q1 // add src green to dst green1
+ vshr.u16 q10, q10, #8 // shift down blue0
+ vadd.u16 q11, q11, q2 // add src blue to dst blue1
+ vsli.u16 q10, q9, #5 // shift & insert green0 into blue0
+ vshr.u16 q13, q13, #8 // shift down red1
+ vsli.u16 q10, q8, #11 // shift & insert red0 into blue0
+ vshr.u16 q12, q12, #8 // shift down green1
+ vshr.u16 q11, q11, #8 // shift down blue1
+ subs r3, r3, #1 // decrement loop counter
+ vsli.u16 q11, q12, #5 // shift & insert green1 into blue1
+ vsli.u16 q11, q13, #11 // shift & insert red1 into blue1
+
+ vst1.16 {d20, d21, d22, d23}, [r0]! // write 16 pixels back to dst
+ bne 1b // if count != 0, loop
+
+2:
+ ands r3, r2, #15 // calc. single iterations
+ beq 4f // if r3 == 0, exit
+
+ ldr r4, [r1] // load source color
+ mov r5, r4, lsr #24 // shift down alpha
+ add r5, r5, r5, lsr #7 // add in top bit
+ rsb r5, r5, #256 // invert alpha
+ and r11, r4, #0xff // extract red
+ ubfx r12, r4, #8, #8 // extract green
+ ubfx r4, r4, #16, #8 // extract blue
+ mov r11, r11, lsl #5 // prescale red
+ mov r12, r12, lsl #6 // prescale green
+ mov r4, r4, lsl #5 // prescale blue
+
+3:
+ ldrh r8, [r0] // load dest pixel
+ subs r3, r3, #1 // decrement loop counter
+ mov r6, r8, lsr #11 // extract dest red
+ ubfx r7, r8, #5, #6 // extract dest green
+ and r8, r8, #0x1f // extract dest blue
+
+ smlabb r6, r6, r5, r11 // dest red * alpha + src red
+ smlabb r7, r7, r5, r12 // dest green * alpha + src green
+ smlabb r8, r8, r5, r4 // dest blue * alpha + src blue
+
+ mov r6, r6, lsr #8 // shift down red
+ mov r7, r7, lsr #8 // shift down green
+ mov r6, r6, lsl #11 // shift red into 565
+ orr r6, r7, lsl #5 // shift green into 565
+ orr r6, r8, lsr #8 // shift blue into 565
+
+ strh r6, [r0], #2 // store pixel to dest, update ptr
+ bne 3b // if count != 0, loop
+4:
+
+ pop {r4-r11, pc} // return
+
+
+
diff --git a/libpixelflinger/scanline.cpp b/libpixelflinger/scanline.cpp
index f700306..a2f43eb 100644
--- a/libpixelflinger/scanline.cpp
+++ b/libpixelflinger/scanline.cpp
@@ -80,6 +80,7 @@
static void scanline_perspective_single(context_t* c);
static void scanline_t32cb16blend(context_t* c);
static void scanline_t32cb16(context_t* c);
+static void scanline_col32cb16blend(context_t* c);
static void scanline_memcpy(context_t* c);
static void scanline_memset8(context_t* c);
static void scanline_memset16(context_t* c);
@@ -93,6 +94,8 @@
extern "C" void scanline_t32cb16blend_arm(uint16_t*, uint32_t*, size_t);
extern "C" void scanline_t32cb16_arm(uint16_t *dst, uint32_t *src, size_t ct);
+extern "C" void scanline_col32cb16blend_neon(uint16_t *dst, uint32_t *col, size_t ct);
+extern "C" void scanline_col32cb16blend_arm(uint16_t *dst, uint32_t col, size_t ct);
// ----------------------------------------------------------------------------
@@ -111,6 +114,9 @@
{ { { 0x03010104, 0x00000077, { 0x00000A01, 0x00000000 } },
{ 0xFFFFFFFF, 0xFFFFFFFF, { 0xFFFFFFFF, 0x0000003F } } },
"565 fb, 8888 tx", scanline_t32cb16, init_y_noop },
+ { { { 0x03515104, 0x00000077, { 0x00000000, 0x00000000 } },
+ { 0xFFFFFFFF, 0xFFFFFFFF, { 0xFFFFFFFF, 0xFFFFFFFF } } },
+ "565 fb, 8888 fixed color", scanline_col32cb16blend, init_y_packed },
{ { { 0x00000000, 0x00000000, { 0x00000000, 0x00000000 } },
{ 0x00000000, 0x00000007, { 0x00000000, 0x00000000 } } },
"(nop) alpha test", scanline_noop, init_y_noop },
@@ -943,6 +949,8 @@
uint8_t f = c->state.buffers.color.format;
c->packed = ggl_pack_color(c, f,
c->shade.r0, c->shade.g0, c->shade.b0, c->shade.a0);
+ c->packed8888 = ggl_pack_color(c, GGL_PIXEL_FORMAT_RGBA_8888,
+ c->shade.r0, c->shade.g0, c->shade.b0, c->shade.a0);
c->iterators.y = y0;
c->step_y = step_y__nop;
// choose the rectangle blitter
@@ -1253,6 +1261,45 @@
// ----------------------------------------------------------------------------
+void scanline_col32cb16blend(context_t* c)
+{
+ int32_t x = c->iterators.xl;
+ size_t ct = c->iterators.xr - x;
+ int32_t y = c->iterators.y;
+ surface_t* cb = &(c->state.buffers.color);
+ union {
+ uint16_t* dst;
+ uint32_t* dst32;
+ };
+ dst = reinterpret_cast<uint16_t*>(cb->data) + (x+(cb->stride*y));
+
+#if ((ANDROID_CODEGEN >= ANDROID_CODEGEN_ASM) && defined(__arm__))
+#if defined(__ARM_HAVE_NEON) && BYTE_ORDER == LITTLE_ENDIAN
+ scanline_col32cb16blend_neon(dst, &(c->packed8888), ct);
+#else // defined(__ARM_HAVE_NEON) && BYTE_ORDER == LITTLE_ENDIAN
+ scanline_col32cb16blend_arm(dst, GGL_RGBA_TO_HOST(c->packed8888), ct);
+#endif // defined(__ARM_HAVE_NEON) && BYTE_ORDER == LITTLE_ENDIAN
+#else
+ uint32_t s = GGL_RGBA_TO_HOST(c->packed8888);
+ int sA = (s>>24);
+ int f = 0x100 - (sA + (sA>>7));
+ while (ct--) {
+ uint16_t d = *dst;
+ int dR = (d>>11)&0x1f;
+ int dG = (d>>5)&0x3f;
+ int dB = (d)&0x1f;
+ int sR = (s >> ( 3))&0x1F;
+ int sG = (s >> ( 8+2))&0x3F;
+ int sB = (s >> (16+3))&0x1F;
+ sR += (f*dR)>>8;
+ sG += (f*dG)>>8;
+ sB += (f*dB)>>8;
+ *dst++ = uint16_t((sR<<11)|(sG<<5)|sB);
+ }
+#endif
+
+}
+
void scanline_t32cb16(context_t* c)
{
int32_t x = c->iterators.xl;
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..3604a42
--- /dev/null
+++ b/libusbhost/usbhost.c
@@ -0,0 +1,461 @@
+/*
+ * 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);
+}
+
+struct usb_device *usb_endpoint_get_device(struct usb_endpoint *ep)
+{
+ return ep->dev;
+}
+
+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/logwrapper/logwrapper.c b/logwrapper/logwrapper.c
index f00bfbf..bdf53e8 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.c
@@ -27,8 +27,8 @@
#include "cutils/log.h"
void fatal(const char *msg) {
- fprintf(stderr, msg);
- LOG(LOG_ERROR, "logwrapper", msg);
+ fprintf(stderr, "%s", msg);
+ LOG(LOG_ERROR, "logwrapper", "%s", msg);
exit(-1);
}
@@ -60,7 +60,7 @@
buffer[b] = '\0';
} else if (buffer[b] == '\n') {
buffer[b] = '\0';
- LOG(LOG_INFO, tag, &buffer[a]);
+ LOG(LOG_INFO, tag, "%s", &buffer[a]);
a = b + 1;
}
}
@@ -68,7 +68,7 @@
if (a == 0 && b == sizeof(buffer) - 1) {
// buffer is full, flush
buffer[b] = '\0';
- LOG(LOG_INFO, tag, &buffer[a]);
+ LOG(LOG_INFO, tag, "%s", &buffer[a]);
b = 0;
} else if (a != b) {
// Keep left-overs
@@ -84,7 +84,7 @@
// Flush remaining data
if (a != b) {
buffer[b] = '\0';
- LOG(LOG_INFO, tag, &buffer[a]);
+ LOG(LOG_INFO, tag, "%s", &buffer[a]);
}
status = 0xAAAA;
if (wait(&status) != -1) { // Wait for child
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 8d8955b..31a9bd7 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -16,7 +16,7 @@
export ANDROID_DATA /data
export EXTERNAL_STORAGE /mnt/sdcard
export ASEC_MOUNTPOINT /mnt/asec
- export BOOTCLASSPATH /system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar
+ export BOOTCLASSPATH /system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/core-junit.jar
# Backward compatibility
symlink /system/etc /etc
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/sh/trap.c b/sh/trap.c
index b3b2db4..7cb5201 100644
--- a/sh/trap.c
+++ b/sh/trap.c
@@ -60,20 +60,6 @@
#include "mystring.h"
#include "var.h"
-static const char *sys_signame[NSIG] = {
- "Unused",
- "HUP", "INT", "QUIT", "ILL",
- "TRAP", "ABRT", "BUS", "FPE",
- "KILL", "USR1", "SEGV", "USR2",
- "PIPE", "ALRM", "TERM",
- "Unknown",
- "CHLD",
- "CONT", "STOP", "TSTP", "TTIN",
- "TTOU", "URG", "XCPU", "XFSZ",
- "VTALRM", "PROF", "WINCH", "IO",
- "PWR", "SYS"
-};
-
/*
* Sigmode records the current value of the signal handlers for the various
* modes. A value of zero means that the current handler is not known.
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 */
}
diff --git a/toolbox/route.c b/toolbox/route.c
index 4f66201..107e48a 100644
--- a/toolbox/route.c
+++ b/toolbox/route.c
@@ -61,7 +61,7 @@
if (!strcmp(argv[2], "default")) {
/* route add default dev wlan0 */
if (argc > 4 && !strcmp(argv[3], "dev")) {
- rt.rt_flags = RTF_UP | RTF_HOST;
+ rt.rt_flags = RTF_UP;
rt.rt_dev = argv[4];
errno = 0;
goto apply;