Reconcile with jb-mr1-release - do not merge

Change-Id: I67e44ab1acd80a961b813639d41737ac187bbd99
diff --git a/include/system/camera.h b/include/system/camera.h
index e4cacc5..7a4dd53 100644
--- a/include/system/camera.h
+++ b/include/system/camera.h
@@ -163,6 +163,17 @@
      * can silently finish itself or show a dialog.
      */
     CAMERA_CMD_PING = 9,
+
+    /**
+     * Configure the number of video buffers used for recording. The intended
+     * video buffer count for recording is passed as arg1, which must be
+     * greater than 0. This command must be sent before recording is started.
+     * This command returns INVALID_OPERATION error if it is sent after video
+     * recording is started, or the command is not supported at all. This
+     * command also returns a BAD_VALUE error if the intended video buffer
+     * count is non-positive or too big to be realized.
+     */
+    CAMERA_CMD_SET_VIDEO_BUFFER_COUNT = 10,
 };
 
 /** camera fatal errors */
diff --git a/libusbhost/usbhost.c b/libusbhost/usbhost.c
index 5d261cd..c059b89 100644
--- a/libusbhost/usbhost.c
+++ b/libusbhost/usbhost.c
@@ -49,7 +49,8 @@
 
 #include "usbhost/usbhost.h"
 
-#define USB_FS_DIR "/dev/bus/usb"
+#define DEV_DIR             "/dev"
+#define USB_FS_DIR          "/dev/bus/usb"
 #define USB_FS_ID_SCANNER   "/dev/bus/usb/%d/%d"
 #define USB_FS_ID_FORMAT    "/dev/bus/usb/%03d/%03d"
 
@@ -77,39 +78,72 @@
     return 0;
 }
 
+static int find_existing_devices_bus(char *busname,
+                                     usb_device_added_cb added_cb,
+                                     void *client_data)
+{
+    char devname[32];
+    DIR *devdir;
+    struct dirent *de;
+    int done = 0;
+
+    devdir = opendir(busname);
+    if(devdir == 0) return 0;
+
+    while ((de = readdir(devdir)) && !done) {
+        if(badname(de->d_name)) continue;
+
+        snprintf(devname, sizeof(devname), "%s/%s", busname, de->d_name);
+        done = added_cb(devname, client_data);
+    } // end of devdir while
+    closedir(devdir);
+
+    return done;
+}
+
 /* returns true if one of the callbacks indicates we are done */
 static int find_existing_devices(usb_device_added_cb added_cb,
-                                  usb_device_removed_cb removed_cb,
                                   void *client_data)
 {
-    char busname[32], devname[32];
-    DIR *busdir , *devdir ;
+    char busname[32];
+    DIR *busdir;
     struct dirent *de;
     int done = 0;
 
     busdir = opendir(USB_FS_DIR);
-    if(busdir == 0) return 1;
+    if(busdir == 0) return 0;
 
     while ((de = readdir(busdir)) != 0 && !done) {
         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)) && !done) {
-            if(badname(de->d_name)) continue;
-
-            snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
-            done = added_cb(devname, client_data);
-        } // end of devdir while
-        closedir(devdir);
-    } //end of busdir while
+        snprintf(busname, sizeof(busname), "%s/%s", USB_FS_DIR, de->d_name);
+        done = find_existing_devices_bus(busname, added_cb,
+                                         client_data);
+    }
     closedir(busdir);
 
     return done;
 }
 
+static void watch_existing_subdirs(struct usb_host_context *context,
+                                   int *wds, int wd_count)
+{
+    char path[100];
+    int i, ret;
+
+    wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE);
+    if (wds[0] < 0)
+        return;
+
+    /* 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(context->fd, path, IN_CREATE | IN_DELETE);
+        if (ret >= 0)
+            wds[i] = ret;
+    }
+}
+
 struct usb_host_context *usb_host_init()
 {
     struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context));
@@ -142,32 +176,28 @@
     char event_buf[512];
     char path[100];
     int i, ret, done = 0;
-    int wd, wds[10];
+    int wd, wdd, wds[10];
     int wd_count = sizeof(wds) / sizeof(wds[0]);
 
     D("Created device discovery thread\n");
 
     /* watch for files added and deleted within USB_FS_DIR */
-    memset(wds, 0, sizeof(wds));
+    for (i = 0; i < wd_count; i++)
+        wds[i] = -1;
+
     /* watch the root for new subdirectories */
-    wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE);
-    if (wds[0] < 0) {
+    wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE);
+    if (wdd < 0) {
         fprintf(stderr, "inotify_add_watch failed\n");
         if (discovery_done_cb)
             discovery_done_cb(client_data);
         return;
     }
 
-    /* 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(context->fd, path, IN_CREATE | IN_DELETE);
-        if (ret > 0)
-            wds[i] = ret;
-    }
+    watch_existing_subdirs(context, wds, wd_count);
 
     /* check for existing devices first, after we have inotify set up */
-    done = find_existing_devices(added_cb, removed_cb, client_data);
+    done = find_existing_devices(added_cb, client_data);
     if (discovery_done_cb)
         done |= discovery_done_cb(client_data);
 
@@ -176,14 +206,35 @@
         if (ret >= (int)sizeof(struct inotify_event)) {
             event = (struct inotify_event *)event_buf;
             wd = event->wd;
-            if (wd == wds[0]) {
+            if (wd == wdd) {
+                if ((event->mask & IN_CREATE) && !strcmp(event->name, "bus")) {
+                    watch_existing_subdirs(context, wds, wd_count);
+                    done = find_existing_devices(added_cb, client_data);
+                } else if ((event->mask & IN_DELETE) && !strcmp(event->name, "bus")) {
+                    for (i = 0; i < wd_count; i++) {
+                        if (wds[i] >= 0) {
+                            inotify_rm_watch(context->fd, wds[i]);
+                            wds[i] = -1;
+                        }
+                    }
+                }
+            } else 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);
+                D("%s subdirectory %s: index: %d\n", (event->mask & IN_CREATE) ?
+                                                     "new" : "gone", path, i);
                 if (i > 0 && i < wd_count) {
-                ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE);
-                if (ret > 0)
-                    wds[i] = ret;
+                    if (event->mask & IN_CREATE) {
+                        ret = inotify_add_watch(context->fd, path,
+                                                IN_CREATE | IN_DELETE);
+                        if (ret >= 0)
+                            wds[i] = ret;
+                        done = find_existing_devices_bus(path, added_cb,
+                                                         client_data);
+                    } else if (event->mask & IN_DELETE) {
+                        inotify_rm_watch(context->fd, wds[i]);
+                        wds[i] = -1;
+                    }
                 }
             } else {
                 for (i = 1; i < wd_count && !done; i++) {