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

Commit cc6ff107 authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

Merge "libusbhost: Remove global data to allow multiple instances in the same process."

parents c6793c52 6ac3aa15
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -32,17 +32,19 @@ struct usb_descriptor_iter {
};

/* callback for notification when new USB devices are attached */
typedef void (* usb_device_added_cb)(const char *dev_name);
typedef void (* usb_device_added_cb)(const char *dev_name, void *client_data);

/* callback for notification when USB devices are removed */
typedef void (* usb_device_removed_cb)(const char *dev_name);
typedef void (* usb_device_removed_cb)(const char *dev_name, void *client_data);

/* 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);
int usb_host_init(usb_device_added_cb added_cb,
                  usb_device_removed_cb removed_cb,
                  void *client_data);

/* Creates a usb_device object for a USB device */
struct usb_device *usb_device_open(const char *dev_name);
+25 −23
Original line number Diff line number Diff line
@@ -48,6 +48,12 @@
#define D(...)
#endif

struct usb_host_context {
    usb_device_added_cb     added_cb;
    usb_device_removed_cb   removed_cb;
    void                    *client_data;
};

struct usb_device {
    char dev_name[64];
    unsigned char desc[256];
@@ -63,9 +69,6 @@ struct usb_endpoint
    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) {
@@ -74,7 +77,7 @@ static inline int badname(const char *name)
    return 0;
}

static void find_existing_devices()
static void find_existing_devices(struct usb_host_context *context)
{
    char busname[32], devname[32];
    DIR *busdir , *devdir ;
@@ -94,15 +97,16 @@ static void find_existing_devices()
            if(badname(de->d_name)) continue;

            snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
            s_added_cb(devname);
            context->added_cb(devname, context->client_data);
        } // end of devdir while
        closedir(devdir);
    } //end of busdir while
    closedir(busdir);
}

static void* device_discovery_thread(void* unused)
static void* device_discovery_thread(void *client_data)
{
    struct usb_host_context *context = (struct usb_host_context *)client_data;
    struct inotify_event* event;
    char event_buf[512];
    char path[100];
@@ -136,8 +140,7 @@ static void* device_discovery_thread(void* unused)
    }

    /* check for existing devices first, after we have inotify set up */
    if (s_added_cb)
        find_existing_devices();
    find_existing_devices(context);

    while (1) {
        ret = read(fd, event_buf, sizeof(event_buf));
@@ -159,12 +162,10 @@ static void* device_discovery_thread(void* unused)
                        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);
                            context->added_cb(path, context->client_data);
                        } else if (event->mask == IN_DELETE) {
                            D("gone device %s\n", path);
                            if (s_removed_cb)
                                s_removed_cb(path);
                            context->removed_cb(path, context->client_data);
                        }
                    }
                }
@@ -174,22 +175,23 @@ static void* device_discovery_thread(void* unused)
    return NULL;
}

int usb_host_init(usb_device_added_cb added_cb, usb_device_removed_cb removed_cb)
int usb_host_init(usb_device_added_cb added_cb, usb_device_removed_cb removed_cb, void *client_data)
{
    struct usb_host_context *context;
    pthread_t tid;
    pthread_attr_t   attr;

    s_added_cb = added_cb;
    s_removed_cb = removed_cb;
    if (!added_cb || !removed_cb)
        return -EINVAL;

    if (added_cb || removed_cb) {
        pthread_attr_t   attr;
    context = calloc(1, sizeof(struct usb_host_context));
    context->added_cb = added_cb;
    context->removed_cb = removed_cb;
    context->client_data = client_data;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        return pthread_create(&tid, &attr, device_discovery_thread, NULL);
    }
    else
        return 0;
    return pthread_create(&tid, &attr, device_discovery_thread, context);
}

struct usb_device *usb_device_open(const char *dev_name)