2010-05-09 22:23:47 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-10-18 01:57:43 +02:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2010-06-10 16:20:49 +02:00
|
|
|
// #define DEBUG 1
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
#ifdef USE_LIBLOG
|
|
|
|
#define LOG_TAG "usbhost"
|
2017-04-20 17:32:35 +02:00
|
|
|
#include "log/log.h"
|
2011-12-20 17:07:45 +01:00
|
|
|
#define D ALOGD
|
2010-06-10 16:20:49 +02:00
|
|
|
#else
|
|
|
|
#define D printf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define D(...)
|
|
|
|
#endif
|
|
|
|
|
2010-05-09 22:23:47 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2012-10-23 17:11:44 +02:00
|
|
|
#include <stddef.h>
|
2010-05-09 22:23:47 +02:00
|
|
|
|
|
|
|
#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>
|
2016-10-18 01:57:43 +02:00
|
|
|
#include <poll.h>
|
2010-05-09 22:23:47 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <linux/usbdevice_fs.h>
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
|
|
|
|
#include "usbhost/usbhost.h"
|
|
|
|
|
2012-08-02 03:54:02 +02:00
|
|
|
#define DEV_DIR "/dev"
|
2013-08-07 18:29:17 +02:00
|
|
|
#define DEV_BUS_DIR DEV_DIR "/bus"
|
|
|
|
#define USB_FS_DIR DEV_BUS_DIR "/usb"
|
2012-10-23 17:11:44 +02:00
|
|
|
#define USB_FS_ID_SCANNER USB_FS_DIR "/%d/%d"
|
|
|
|
#define USB_FS_ID_FORMAT USB_FS_DIR "/%03d/%03d"
|
2010-05-09 22:23:47 +02:00
|
|
|
|
2015-06-18 22:38:31 +02:00
|
|
|
// Some devices fail to send string descriptors if we attempt reading > 255 bytes
|
|
|
|
#define MAX_STRING_DESCRIPTOR_LENGTH 255
|
|
|
|
|
2012-10-23 17:11:44 +02:00
|
|
|
#define MAX_USBFS_WD_COUNT 10
|
|
|
|
|
2010-05-25 14:10:02 +02:00
|
|
|
struct usb_host_context {
|
2012-10-23 17:11:44 +02:00
|
|
|
int fd;
|
|
|
|
usb_device_added_cb cb_added;
|
|
|
|
usb_device_removed_cb cb_removed;
|
|
|
|
void *data;
|
|
|
|
int wds[MAX_USBFS_WD_COUNT];
|
|
|
|
int wdd;
|
2013-08-07 18:29:17 +02:00
|
|
|
int wddbus;
|
2010-05-25 14:10:02 +02:00
|
|
|
};
|
|
|
|
|
2017-11-06 18:22:51 +01:00
|
|
|
#define MAX_DESCRIPTORS_LENGTH 4096
|
|
|
|
|
2010-05-09 22:23:47 +02:00
|
|
|
struct usb_device {
|
|
|
|
char dev_name[64];
|
2017-11-06 18:22:51 +01:00
|
|
|
unsigned char desc[MAX_DESCRIPTORS_LENGTH];
|
2010-05-09 22:23:47 +02:00
|
|
|
int desc_length;
|
|
|
|
int fd;
|
|
|
|
int writeable;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int badname(const char *name)
|
|
|
|
{
|
|
|
|
while(*name) {
|
|
|
|
if(!isdigit(*name++)) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-01 04:22:06 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-01 17:33:41 +02:00
|
|
|
/* returns true if one of the callbacks indicates we are done */
|
|
|
|
static int find_existing_devices(usb_device_added_cb added_cb,
|
|
|
|
void *client_data)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2012-08-01 04:22:06 +02:00
|
|
|
char busname[32];
|
|
|
|
DIR *busdir;
|
2010-05-09 22:23:47 +02:00
|
|
|
struct dirent *de;
|
2010-07-01 17:33:41 +02:00
|
|
|
int done = 0;
|
2010-05-09 22:23:47 +02:00
|
|
|
|
|
|
|
busdir = opendir(USB_FS_DIR);
|
2012-08-02 03:54:02 +02:00
|
|
|
if(busdir == 0) return 0;
|
2010-05-09 22:23:47 +02:00
|
|
|
|
2010-07-01 17:33:41 +02:00
|
|
|
while ((de = readdir(busdir)) != 0 && !done) {
|
2010-05-09 22:23:47 +02:00
|
|
|
if(badname(de->d_name)) continue;
|
|
|
|
|
2012-10-23 17:11:44 +02:00
|
|
|
snprintf(busname, sizeof(busname), USB_FS_DIR "/%s", de->d_name);
|
2012-08-01 04:22:06 +02:00
|
|
|
done = find_existing_devices_bus(busname, added_cb,
|
|
|
|
client_data);
|
2012-10-23 17:11:44 +02:00
|
|
|
} //end of busdir while
|
2010-05-09 22:23:47 +02:00
|
|
|
closedir(busdir);
|
2010-07-01 17:33:41 +02:00
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2012-08-02 03:54:02 +02:00
|
|
|
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++) {
|
2012-10-23 17:11:44 +02:00
|
|
|
snprintf(path, sizeof(path), USB_FS_DIR "/%03d", i);
|
2012-08-02 03:54:02 +02:00
|
|
|
ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE);
|
|
|
|
if (ret >= 0)
|
|
|
|
wds[i] = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-01 17:33:41 +02:00
|
|
|
struct usb_host_context *usb_host_init()
|
|
|
|
{
|
|
|
|
struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context));
|
|
|
|
if (!context) {
|
|
|
|
fprintf(stderr, "out of memory in usb_host_context\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
context->fd = inotify_init();
|
|
|
|
if (context->fd < 0) {
|
|
|
|
fprintf(stderr, "inotify_init failed\n");
|
|
|
|
free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_host_cleanup(struct usb_host_context *context)
|
|
|
|
{
|
|
|
|
close(context->fd);
|
|
|
|
free(context);
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 17:11:44 +02:00
|
|
|
int usb_host_get_fd(struct usb_host_context *context)
|
|
|
|
{
|
|
|
|
return context->fd;
|
|
|
|
} /* usb_host_get_fd() */
|
|
|
|
|
|
|
|
int usb_host_load(struct usb_host_context *context,
|
2010-07-01 17:33:41 +02:00
|
|
|
usb_device_added_cb added_cb,
|
|
|
|
usb_device_removed_cb removed_cb,
|
2010-07-20 02:15:15 +02:00
|
|
|
usb_discovery_done_cb discovery_done_cb,
|
2010-07-01 17:33:41 +02:00
|
|
|
void *client_data)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2012-10-23 17:11:44 +02:00
|
|
|
int done = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
context->cb_added = added_cb;
|
|
|
|
context->cb_removed = removed_cb;
|
|
|
|
context->data = client_data;
|
2010-05-09 22:23:47 +02:00
|
|
|
|
|
|
|
D("Created device discovery thread\n");
|
|
|
|
|
|
|
|
/* watch for files added and deleted within USB_FS_DIR */
|
2013-08-07 18:29:17 +02:00
|
|
|
context->wddbus = -1;
|
2012-10-23 17:11:44 +02:00
|
|
|
for (i = 0; i < MAX_USBFS_WD_COUNT; i++)
|
|
|
|
context->wds[i] = -1;
|
2012-08-02 03:54:02 +02:00
|
|
|
|
2010-05-09 22:23:47 +02:00
|
|
|
/* watch the root for new subdirectories */
|
2012-10-23 17:11:44 +02:00
|
|
|
context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE);
|
|
|
|
if (context->wdd < 0) {
|
2010-05-09 22:23:47 +02:00
|
|
|
fprintf(stderr, "inotify_add_watch failed\n");
|
2010-07-20 22:31:42 +02:00
|
|
|
if (discovery_done_cb)
|
|
|
|
discovery_done_cb(client_data);
|
2012-10-23 17:11:44 +02:00
|
|
|
return done;
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 17:11:44 +02:00
|
|
|
watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
|
2010-05-09 22:23:47 +02:00
|
|
|
|
|
|
|
/* check for existing devices first, after we have inotify set up */
|
2012-08-01 04:22:06 +02:00
|
|
|
done = find_existing_devices(added_cb, client_data);
|
2010-07-20 02:15:15 +02:00
|
|
|
if (discovery_done_cb)
|
|
|
|
done |= discovery_done_cb(client_data);
|
2010-05-09 22:23:47 +02:00
|
|
|
|
2012-10-23 17:11:44 +02:00
|
|
|
return done;
|
|
|
|
} /* usb_host_load() */
|
|
|
|
|
|
|
|
int usb_host_read_event(struct usb_host_context *context)
|
|
|
|
{
|
|
|
|
struct inotify_event* event;
|
|
|
|
char event_buf[512];
|
|
|
|
char path[100];
|
|
|
|
int i, ret, done = 0;
|
2013-03-24 17:10:52 +01:00
|
|
|
int offset = 0;
|
2012-10-23 17:11:44 +02:00
|
|
|
int wd;
|
|
|
|
|
|
|
|
ret = read(context->fd, event_buf, sizeof(event_buf));
|
|
|
|
if (ret >= (int)sizeof(struct inotify_event)) {
|
2013-08-07 18:29:17 +02:00
|
|
|
while (offset < ret && !done) {
|
2013-03-24 17:10:52 +01:00
|
|
|
event = (struct inotify_event*)&event_buf[offset];
|
|
|
|
done = 0;
|
|
|
|
wd = event->wd;
|
|
|
|
if (wd == context->wdd) {
|
|
|
|
if ((event->mask & IN_CREATE) && !strcmp(event->name, "bus")) {
|
2013-08-07 18:29:17 +02:00
|
|
|
context->wddbus = inotify_add_watch(context->fd, DEV_BUS_DIR, IN_CREATE | IN_DELETE);
|
|
|
|
if (context->wddbus < 0) {
|
|
|
|
done = 1;
|
|
|
|
} else {
|
|
|
|
watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
|
|
|
|
done = find_existing_devices(context->cb_added, context->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (wd == context->wddbus) {
|
|
|
|
if ((event->mask & IN_CREATE) && !strcmp(event->name, "usb")) {
|
2013-03-24 17:10:52 +01:00
|
|
|
watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT);
|
|
|
|
done = find_existing_devices(context->cb_added, context->data);
|
2013-08-07 18:29:17 +02:00
|
|
|
} else if ((event->mask & IN_DELETE) && !strcmp(event->name, "usb")) {
|
2013-03-24 17:10:52 +01:00
|
|
|
for (i = 0; i < MAX_USBFS_WD_COUNT; i++) {
|
|
|
|
if (context->wds[i] >= 0) {
|
|
|
|
inotify_rm_watch(context->fd, context->wds[i]);
|
|
|
|
context->wds[i] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (wd == context->wds[0]) {
|
|
|
|
i = atoi(event->name);
|
|
|
|
snprintf(path, sizeof(path), USB_FS_DIR "/%s", event->name);
|
|
|
|
D("%s subdirectory %s: index: %d\n", (event->mask & IN_CREATE) ?
|
|
|
|
"new" : "gone", path, i);
|
|
|
|
if (i > 0 && i < MAX_USBFS_WD_COUNT) {
|
2014-01-21 07:25:22 +01:00
|
|
|
int local_ret = 0;
|
2013-03-24 17:10:52 +01:00
|
|
|
if (event->mask & IN_CREATE) {
|
2014-01-21 07:25:22 +01:00
|
|
|
local_ret = inotify_add_watch(context->fd, path,
|
2013-03-24 17:10:52 +01:00
|
|
|
IN_CREATE | IN_DELETE);
|
2014-01-21 07:25:22 +01:00
|
|
|
if (local_ret >= 0)
|
|
|
|
context->wds[i] = local_ret;
|
2013-03-24 17:10:52 +01:00
|
|
|
done = find_existing_devices_bus(path, context->cb_added,
|
|
|
|
context->data);
|
|
|
|
} else if (event->mask & IN_DELETE) {
|
2012-10-23 17:11:44 +02:00
|
|
|
inotify_rm_watch(context->fd, context->wds[i]);
|
|
|
|
context->wds[i] = -1;
|
2012-08-02 03:54:02 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-24 17:10:52 +01:00
|
|
|
} else {
|
|
|
|
for (i = 1; (i < MAX_USBFS_WD_COUNT) && !done; i++) {
|
|
|
|
if (wd == context->wds[i]) {
|
|
|
|
snprintf(path, sizeof(path), USB_FS_DIR "/%03d/%s", i, event->name);
|
|
|
|
if (event->mask == IN_CREATE) {
|
|
|
|
D("new device %s\n", path);
|
|
|
|
done = context->cb_added(path, context->data);
|
|
|
|
} else if (event->mask == IN_DELETE) {
|
|
|
|
D("gone device %s\n", path);
|
|
|
|
done = context->cb_removed(path, context->data);
|
|
|
|
}
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-24 17:10:52 +01:00
|
|
|
|
|
|
|
offset += sizeof(struct inotify_event) + event->len;
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-23 17:11:44 +02:00
|
|
|
|
|
|
|
return done;
|
|
|
|
} /* usb_host_read_event() */
|
|
|
|
|
|
|
|
void usb_host_run(struct usb_host_context *context,
|
|
|
|
usb_device_added_cb added_cb,
|
|
|
|
usb_device_removed_cb removed_cb,
|
|
|
|
usb_discovery_done_cb discovery_done_cb,
|
|
|
|
void *client_data)
|
|
|
|
{
|
|
|
|
int done;
|
|
|
|
|
|
|
|
done = usb_host_load(context, added_cb, removed_cb, discovery_done_cb, client_data);
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
|
|
|
|
done = usb_host_read_event(context);
|
|
|
|
}
|
|
|
|
} /* usb_host_run() */
|
2010-05-09 22:23:47 +02:00
|
|
|
|
|
|
|
struct usb_device *usb_device_open(const char *dev_name)
|
|
|
|
{
|
2017-11-02 01:32:35 +01:00
|
|
|
int fd, attempts, writeable = 1;
|
|
|
|
const int SLEEP_BETWEEN_ATTEMPTS_US = 100000; /* 100 ms */
|
|
|
|
const int64_t MAX_ATTEMPTS = 10; /* 1s */
|
2011-01-22 18:17:07 +01:00
|
|
|
D("usb_device_open %s\n", dev_name);
|
|
|
|
|
2017-11-02 01:32:35 +01:00
|
|
|
/* Hack around waiting for permissions to be set on the USB device node.
|
|
|
|
* Should really be a timeout instead of attempt count, and should REALLY
|
|
|
|
* be triggered by the perm change via inotify rather than polling.
|
|
|
|
*/
|
|
|
|
for (attempts = 0; attempts < MAX_ATTEMPTS; ++attempts) {
|
|
|
|
if (access(dev_name, R_OK | W_OK) == 0) {
|
|
|
|
writeable = 1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (access(dev_name, R_OK) == 0) {
|
|
|
|
/* double check that write permission didn't just come along too! */
|
|
|
|
writeable = (access(dev_name, R_OK | W_OK) == 0);
|
|
|
|
break;
|
|
|
|
}
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
2017-11-02 01:32:35 +01:00
|
|
|
/* not writeable or readable - sleep and try again. */
|
|
|
|
D("usb_device_open no access sleeping\n");
|
|
|
|
usleep(SLEEP_BETWEEN_ATTEMPTS_US);
|
|
|
|
}
|
2010-05-09 22:23:47 +02:00
|
|
|
|
2017-11-02 01:32:35 +01:00
|
|
|
if (writeable) {
|
|
|
|
fd = open(dev_name, O_RDWR);
|
|
|
|
} else {
|
|
|
|
fd = open(dev_name, O_RDONLY);
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
2017-11-02 01:32:35 +01:00
|
|
|
D("usb_device_open open returned %d writeable %d errno %d\n", fd, writeable, errno);
|
|
|
|
if (fd < 0) return NULL;
|
2010-05-09 22:23:47 +02:00
|
|
|
|
2010-12-12 23:17:02 +01:00
|
|
|
struct usb_device* result = usb_device_new(dev_name, fd);
|
|
|
|
if (result)
|
|
|
|
result->writeable = writeable;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_device_close(struct usb_device *device)
|
|
|
|
{
|
|
|
|
close(device->fd);
|
|
|
|
free(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct usb_device *usb_device_new(const char *dev_name, int fd)
|
|
|
|
{
|
|
|
|
struct usb_device *device = calloc(1, sizeof(struct usb_device));
|
|
|
|
int length;
|
|
|
|
|
2011-01-22 18:17:07 +01:00
|
|
|
D("usb_device_new %s fd: %d\n", dev_name, fd);
|
|
|
|
|
2010-12-12 23:17:02 +01:00
|
|
|
if (lseek(fd, 0, SEEK_SET) != 0)
|
|
|
|
goto failed;
|
2010-05-09 22:23:47 +02:00
|
|
|
length = read(fd, device->desc, sizeof(device->desc));
|
2011-01-22 18:17:07 +01:00
|
|
|
D("usb_device_new read returned %d errno %d\n", length, errno);
|
2010-05-09 22:23:47 +02:00
|
|
|
if (length < 0)
|
2010-12-12 23:17:02 +01:00
|
|
|
goto failed;
|
2010-05-09 22:23:47 +02:00
|
|
|
|
2010-12-15 21:58:04 +01:00
|
|
|
strncpy(device->dev_name, dev_name, sizeof(device->dev_name) - 1);
|
2010-05-09 22:23:47 +02:00
|
|
|
device->fd = fd;
|
|
|
|
device->desc_length = length;
|
2010-12-12 23:17:02 +01:00
|
|
|
// assume we are writeable, since usb_device_get_fd will only return writeable fds
|
|
|
|
device->writeable = 1;
|
2010-05-09 22:23:47 +02:00
|
|
|
return device;
|
2010-12-12 23:17:02 +01:00
|
|
|
|
|
|
|
failed:
|
2017-11-06 18:22:51 +01:00
|
|
|
// TODO It would be more appropriate to have callers do this
|
|
|
|
// since this function doesn't "own" this file descriptor.
|
2010-05-09 22:23:47 +02:00
|
|
|
close(fd);
|
|
|
|
free(device);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-12 23:17:02 +01:00
|
|
|
static int usb_device_reopen_writeable(struct usb_device *device)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2010-12-12 23:17:02 +01:00
|
|
|
if (device->writeable)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int fd = open(device->dev_name, O_RDWR);
|
|
|
|
if (fd >= 0) {
|
|
|
|
close(device->fd);
|
|
|
|
device->fd = fd;
|
|
|
|
device->writeable = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
D("usb_device_reopen_writeable failed errno %d\n", errno);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_device_get_fd(struct usb_device *device)
|
|
|
|
{
|
|
|
|
if (!usb_device_reopen_writeable(device))
|
|
|
|
return -1;
|
|
|
|
return device->fd;
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* usb_device_get_name(struct usb_device *device)
|
|
|
|
{
|
|
|
|
return device->dev_name;
|
|
|
|
}
|
|
|
|
|
2010-05-27 16:12:03 +02:00
|
|
|
int usb_device_get_unique_id(struct usb_device *device)
|
|
|
|
{
|
|
|
|
int bus = 0, dev = 0;
|
|
|
|
sscanf(device->dev_name, USB_FS_ID_SCANNER, &bus, &dev);
|
|
|
|
return bus * 1000 + dev;
|
|
|
|
}
|
|
|
|
|
2010-07-28 01:05:33 +02:00
|
|
|
int usb_device_get_unique_id_from_name(const char* name)
|
|
|
|
{
|
|
|
|
int bus = 0, dev = 0;
|
|
|
|
sscanf(name, USB_FS_ID_SCANNER, &bus, &dev);
|
|
|
|
return bus * 1000 + dev;
|
|
|
|
}
|
|
|
|
|
2010-12-29 14:47:29 +01:00
|
|
|
char* usb_device_get_name_from_unique_id(int id)
|
|
|
|
{
|
|
|
|
int bus = id / 1000;
|
|
|
|
int dev = id % 1000;
|
|
|
|
char* result = (char *)calloc(1, strlen(USB_FS_ID_FORMAT));
|
|
|
|
snprintf(result, strlen(USB_FS_ID_FORMAT) - 1, USB_FS_ID_FORMAT, bus, dev);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-05-09 22:23:47 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-11-06 18:22:51 +01:00
|
|
|
const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device* device) {
|
2010-12-13 19:15:25 +01:00
|
|
|
return (struct usb_device_descriptor*)device->desc;
|
|
|
|
}
|
|
|
|
|
2017-11-06 18:22:51 +01:00
|
|
|
size_t usb_device_get_descriptors_length(const struct usb_device* device) {
|
|
|
|
return device->desc_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* usb_device_get_raw_descriptors(const struct usb_device* device) {
|
|
|
|
return device->desc;
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:51:49 +01:00
|
|
|
/* Returns a USB descriptor string for the given string ID.
|
|
|
|
* Return value: < 0 on error. 0 on success.
|
|
|
|
* The string is returned in ucs2_out in USB-native UCS-2 encoding.
|
|
|
|
*
|
|
|
|
* parameters:
|
|
|
|
* id - the string descriptor index.
|
|
|
|
* timeout - in milliseconds (see Documentation/driver-api/usb/usb.rst)
|
|
|
|
* ucs2_out - Must point to null on call.
|
|
|
|
* Will be filled in with a buffer on success.
|
|
|
|
* If this is non-null on return, it must be free()d.
|
|
|
|
* response_size - size, in bytes, of ucs-2 string in ucs2_out.
|
|
|
|
* The size isn't guaranteed to include null termination.
|
|
|
|
* Call free() to free the result when you are done with it.
|
|
|
|
*/
|
|
|
|
int usb_device_get_string_ucs2(struct usb_device* device, int id, int timeout, void** ucs2_out,
|
|
|
|
size_t* response_size) {
|
2015-06-18 22:38:31 +02:00
|
|
|
__u16 languages[MAX_STRING_DESCRIPTOR_LENGTH / sizeof(__u16)];
|
2017-12-08 01:51:49 +01:00
|
|
|
char response[MAX_STRING_DESCRIPTOR_LENGTH];
|
|
|
|
int result;
|
2010-07-24 19:57:21 +02:00
|
|
|
int languageCount = 0;
|
|
|
|
|
2017-12-08 01:51:49 +01:00
|
|
|
if (id == 0) return -1;
|
|
|
|
if (*ucs2_out != NULL) return -1;
|
2014-01-13 18:54:13 +01:00
|
|
|
|
2010-07-24 19:57:21 +02:00
|
|
|
memset(languages, 0, sizeof(languages));
|
2010-05-09 22:23:47 +02:00
|
|
|
|
|
|
|
// read list of supported languages
|
2011-01-27 19:46:19 +01:00
|
|
|
result = usb_device_control_transfer(device,
|
2010-07-24 19:57:21 +02:00
|
|
|
USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
|
2016-10-14 19:19:26 +02:00
|
|
|
(USB_DT_STRING << 8) | 0, 0, languages, sizeof(languages),
|
|
|
|
timeout);
|
2010-05-09 22:23:47 +02:00
|
|
|
if (result > 0)
|
|
|
|
languageCount = (result - 2) / 2;
|
|
|
|
|
2017-12-08 01:51:49 +01:00
|
|
|
for (int i = 1; i <= languageCount; i++) {
|
|
|
|
memset(response, 0, sizeof(response));
|
|
|
|
|
|
|
|
result = usb_device_control_transfer(
|
|
|
|
device, USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
|
|
|
|
(USB_DT_STRING << 8) | id, languages[i], response, sizeof(response), timeout);
|
|
|
|
if (result >= 2) { // string contents begin at offset 2.
|
|
|
|
int descriptor_len = result - 2;
|
|
|
|
char* out = malloc(descriptor_len + 3);
|
|
|
|
if (out == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(out, response + 2, descriptor_len);
|
|
|
|
// trail with three additional NULLs, so that there's guaranteed
|
|
|
|
// to be a UCS-2 NULL character beyond whatever USB returned.
|
|
|
|
// The returned string length is still just what USB returned.
|
|
|
|
memset(out + descriptor_len, '\0', 3);
|
|
|
|
*ucs2_out = (void*)out;
|
|
|
|
*response_size = descriptor_len;
|
|
|
|
return 0;
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-08 01:51:49 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-05-09 22:23:47 +02:00
|
|
|
|
2017-12-08 01:51:49 +01:00
|
|
|
/* Warning: previously this blindly returned the lower 8 bits of
|
|
|
|
* every UCS-2 character in a USB descriptor. Now it will replace
|
|
|
|
* values > 127 with ascii '?'.
|
|
|
|
*/
|
|
|
|
char* usb_device_get_string(struct usb_device* device, int id, int timeout) {
|
|
|
|
char* ascii_string = NULL;
|
|
|
|
size_t raw_string_len = 0;
|
|
|
|
size_t i;
|
|
|
|
if (usb_device_get_string_ucs2(device, id, timeout, (void**)&ascii_string, &raw_string_len) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ascii_string == NULL) return NULL;
|
|
|
|
for (i = 0; i < raw_string_len / 2; ++i) {
|
|
|
|
// wire format for USB is always little-endian.
|
|
|
|
char lower = ascii_string[2 * i];
|
|
|
|
char upper = ascii_string[2 * i + 1];
|
|
|
|
if (upper || (lower & 0x80)) {
|
|
|
|
ascii_string[i] = '?';
|
|
|
|
} else {
|
|
|
|
ascii_string[i] = lower;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ascii_string[i] = '\0';
|
|
|
|
return ascii_string;
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 19:19:26 +02:00
|
|
|
char* usb_device_get_manufacturer_name(struct usb_device *device, int timeout)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
|
|
|
struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
|
2016-10-14 19:19:26 +02:00
|
|
|
return usb_device_get_string(device, desc->iManufacturer, timeout);
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 19:19:26 +02:00
|
|
|
char* usb_device_get_product_name(struct usb_device *device, int timeout)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
|
|
|
struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
|
2016-10-14 19:19:26 +02:00
|
|
|
return usb_device_get_string(device, desc->iProduct, timeout);
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 22:04:10 +02:00
|
|
|
int usb_device_get_version(struct usb_device *device)
|
|
|
|
{
|
|
|
|
struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
|
|
|
|
return desc->bcdUSB;
|
|
|
|
}
|
|
|
|
|
2016-10-14 19:19:26 +02:00
|
|
|
char* usb_device_get_serial(struct usb_device *device, int timeout)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
|
|
|
struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc;
|
2016-10-14 19:19:26 +02:00
|
|
|
return usb_device_get_string(device, desc->iSerialNumber, timeout);
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-22 18:17:07 +01:00
|
|
|
int usb_device_connect_kernel_driver(struct usb_device *device,
|
|
|
|
unsigned int interface, int connect)
|
|
|
|
{
|
|
|
|
struct usbdevfs_ioctl ctl;
|
|
|
|
|
|
|
|
ctl.ifno = interface;
|
|
|
|
ctl.ioctl_code = (connect ? USBDEVFS_CONNECT : USBDEVFS_DISCONNECT);
|
|
|
|
ctl.data = NULL;
|
|
|
|
return ioctl(device->fd, USBDEVFS_IOCTL, &ctl);
|
|
|
|
}
|
|
|
|
|
2014-01-13 18:54:13 +01:00
|
|
|
int usb_device_set_configuration(struct usb_device *device, int configuration)
|
|
|
|
{
|
|
|
|
return ioctl(device->fd, USBDEVFS_SETCONFIGURATION, &configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_device_set_interface(struct usb_device *device, unsigned int interface,
|
|
|
|
unsigned int alt_setting)
|
|
|
|
{
|
|
|
|
struct usbdevfs_setinterface ctl;
|
|
|
|
|
|
|
|
ctl.interface = interface;
|
|
|
|
ctl.altsetting = alt_setting;
|
|
|
|
return ioctl(device->fd, USBDEVFS_SETINTERFACE, &ctl);
|
|
|
|
}
|
|
|
|
|
2011-01-27 19:46:19 +01:00
|
|
|
int usb_device_control_transfer(struct usb_device *device,
|
|
|
|
int requestType,
|
|
|
|
int request,
|
|
|
|
int value,
|
|
|
|
int index,
|
|
|
|
void* buffer,
|
|
|
|
int length,
|
|
|
|
unsigned int timeout)
|
|
|
|
{
|
|
|
|
struct usbdevfs_ctrltransfer ctrl;
|
|
|
|
|
|
|
|
// this usually requires read/write permission
|
|
|
|
if (!usb_device_reopen_writeable(device))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(&ctrl, 0, sizeof(ctrl));
|
|
|
|
ctrl.bRequestType = requestType;
|
|
|
|
ctrl.bRequest = request;
|
|
|
|
ctrl.wValue = value;
|
|
|
|
ctrl.wIndex = index;
|
|
|
|
ctrl.wLength = length;
|
|
|
|
ctrl.data = buffer;
|
|
|
|
ctrl.timeout = timeout;
|
|
|
|
return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_device_bulk_transfer(struct usb_device *device,
|
|
|
|
int endpoint,
|
|
|
|
void* buffer,
|
2016-09-20 23:11:26 +02:00
|
|
|
unsigned int length,
|
2011-01-27 19:46:19 +01:00
|
|
|
unsigned int timeout)
|
|
|
|
{
|
|
|
|
struct usbdevfs_bulktransfer ctrl;
|
|
|
|
|
|
|
|
memset(&ctrl, 0, sizeof(ctrl));
|
|
|
|
ctrl.ep = endpoint;
|
|
|
|
ctrl.len = length;
|
|
|
|
ctrl.data = buffer;
|
|
|
|
ctrl.timeout = timeout;
|
|
|
|
return ioctl(device->fd, USBDEVFS_BULK, &ctrl);
|
|
|
|
}
|
|
|
|
|
2016-01-13 03:22:36 +01:00
|
|
|
int usb_device_reset(struct usb_device *device)
|
|
|
|
{
|
|
|
|
return ioctl(device->fd, USBDEVFS_RESET);
|
|
|
|
}
|
|
|
|
|
2011-01-05 02:04:36 +01:00
|
|
|
struct usb_request *usb_request_new(struct usb_device *dev,
|
|
|
|
const struct usb_endpoint_descriptor *ep_desc)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2011-01-05 02:04:36 +01:00
|
|
|
struct usbdevfs_urb *urb = calloc(1, sizeof(struct usbdevfs_urb));
|
|
|
|
if (!urb)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK)
|
|
|
|
urb->type = USBDEVFS_URB_TYPE_BULK;
|
|
|
|
else if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
|
|
|
|
urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
|
|
|
|
else {
|
|
|
|
D("Unsupported endpoint type %d", ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
|
|
|
|
free(urb);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
urb->endpoint = ep_desc->bEndpointAddress;
|
|
|
|
|
|
|
|
struct usb_request *req = calloc(1, sizeof(struct usb_request));
|
|
|
|
if (!req) {
|
|
|
|
free(urb);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->dev = dev;
|
|
|
|
req->max_packet_size = __le16_to_cpu(ep_desc->wMaxPacketSize);
|
|
|
|
req->private_data = urb;
|
2011-02-14 14:05:40 +01:00
|
|
|
req->endpoint = urb->endpoint;
|
2011-01-05 02:04:36 +01:00
|
|
|
urb->usercontext = req;
|
|
|
|
|
|
|
|
return req;
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2011-01-05 02:04:36 +01:00
|
|
|
void usb_request_free(struct usb_request *req)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2011-01-05 02:04:36 +01:00
|
|
|
free(req->private_data);
|
|
|
|
free(req);
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2011-01-05 02:04:36 +01:00
|
|
|
int usb_request_queue(struct usb_request *req)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2011-01-05 02:04:36 +01:00
|
|
|
struct usbdevfs_urb *urb = (struct usbdevfs_urb*)req->private_data;
|
2010-05-09 22:23:47 +02:00
|
|
|
int res;
|
|
|
|
|
|
|
|
urb->status = -1;
|
2011-01-05 02:04:36 +01:00
|
|
|
urb->buffer = req->buffer;
|
2018-02-07 03:25:44 +01:00
|
|
|
urb->buffer_length = req->buffer_length;
|
2010-05-09 22:23:47 +02:00
|
|
|
|
|
|
|
do {
|
2011-01-05 02:04:36 +01:00
|
|
|
res = ioctl(req->dev->fd, USBDEVFS_SUBMITURB, urb);
|
2010-05-09 22:23:47 +02:00
|
|
|
} while((res < 0) && (errno == EINTR));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-18 01:57:43 +02:00
|
|
|
struct usb_request *usb_request_wait(struct usb_device *dev, int timeoutMillis)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2016-10-18 01:57:43 +02:00
|
|
|
// Poll until a request becomes available if there is a timeout
|
|
|
|
if (timeoutMillis > 0) {
|
|
|
|
struct pollfd p = {.fd = dev->fd, .events = POLLOUT, .revents = 0};
|
|
|
|
|
|
|
|
int res = poll(&p, 1, timeoutMillis);
|
|
|
|
|
|
|
|
if (res != 1 || p.revents != POLLOUT) {
|
|
|
|
D("[ poll - event %d, error %d]\n", p.revents, errno);
|
2011-01-05 02:04:36 +01:00
|
|
|
return NULL;
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-18 01:57:43 +02:00
|
|
|
|
|
|
|
// Read the request. This should usually succeed as we polled before, but it can fail e.g. when
|
|
|
|
// two threads are reading usb requests at the same time and only a single request is available.
|
|
|
|
struct usbdevfs_urb *urb = NULL;
|
|
|
|
int res = TEMP_FAILURE_RETRY(ioctl(dev->fd, timeoutMillis == -1 ? USBDEVFS_REAPURB :
|
|
|
|
USBDEVFS_REAPURBNDELAY, &urb));
|
|
|
|
D("%s returned %d\n", timeoutMillis == -1 ? "USBDEVFS_REAPURB" : "USBDEVFS_REAPURBNDELAY", res);
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
D("[ reap urb - error %d]\n", errno);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
D("[ urb @%p status = %d, actual = %d ]\n", urb, urb->status, urb->actual_length);
|
|
|
|
|
|
|
|
struct usb_request *req = (struct usb_request*)urb->usercontext;
|
|
|
|
req->actual_length = urb->actual_length;
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|
|
|
|
|
2011-01-05 02:04:36 +01:00
|
|
|
int usb_request_cancel(struct usb_request *req)
|
2010-05-09 22:23:47 +02:00
|
|
|
{
|
2011-01-05 02:04:36 +01:00
|
|
|
struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data);
|
2014-08-06 21:34:30 +02:00
|
|
|
return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, urb);
|
2010-05-09 22:23:47 +02:00
|
|
|
}
|