加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
usb_linux_client.c 15.10 KB
一键复制 编辑 原始数据 按行查看 历史
liwencong 提交于 2022-02-15 08:54 . smartx-d1-tina-v1.0.0-release release
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
/*
* Copyright (C) 2007 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 <linux/usb/ch9.h>
#include <linux/usb/functionfs.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <dirent.h>
#include "sysdeps.h"
#define TRACE_TAG TRACE_USB
#include "adb.h"
#define MAX_PACKET_SIZE_FS 64
#define MAX_PACKET_SIZE_HS 512
#define __uint16_identity(x) x
#define __uint32_identity(x) x
#define __uint64_identity(x) x
#define __bswap_constant_16(x) \
((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
#define __bswap_16(x) __bswap_constant_16(x)
#define __bswap_constant_32(x) \
((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
#define __bswap_32(x) __bswap_constant_32(x)
#define __bswap_constant_64(x) \
((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
#define __bswap_64(x) __bswap_constant_64(x)
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htobe16(x) __bswap_16 (x)
# define htole16(x) __uint16_identity (x)
# define be16toh(x) __bswap_16 (x)
# define le16toh(x) __uint16_identity (x)
# define htobe32(x) __bswap_32 (x)
# define htole32(x) __uint32_identity (x)
# define be32toh(x) __bswap_32 (x)
# define le32toh(x) __uint32_identity (x)
# define htobe64(x) __bswap_64 (x)
# define htole64(x) __uint64_identity (x)
# define be64toh(x) __bswap_64 (x)
# define le64toh(x) __uint64_identity (x)
# else
# define htobe16(x) __uint16_identity (x)
# define htole16(x) __bswap_16 (x)
# define be16toh(x) __uint16_identity (x)
# define le16toh(x) __bswap_16 (x)
# define htobe32(x) __uint32_identity (x)
# define htole32(x) __bswap_32 (x)
# define be32toh(x) __uint32_identity (x)
# define le32toh(x) __bswap_32 (x)
# define htobe64(x) __uint64_identity (x)
# define htole64(x) __bswap_64 (x)
# define be64toh(x) __uint64_identity (x)
# define le64toh(x) __bswap_64 (x)
#endif
#define cpu_to_le16(x) htole16(x)
#define cpu_to_le32(x) htole32(x)
struct usb_handle
{
adb_cond_t notify;
adb_mutex_t lock;
int (*write)(usb_handle *h, const void *data, int len);
int (*read)(usb_handle *h, void *data, int len);
void (*kick)(usb_handle *h);
// Legacy f_adb
int fd;
// FunctionFS
int control;
int bulk_out; /* "out" from the host's perspective => source for adbd */
int bulk_in; /* "in" from the host's perspective => sink for adbd */
};
static const struct {
struct usb_functionfs_descs_head header;
struct {
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor_no_audio source;
struct usb_endpoint_descriptor_no_audio sink;
} __attribute__((packed)) fs_descs, hs_descs;
} __attribute__((packed)) descriptors = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
.length = cpu_to_le32(sizeof(descriptors)),
.fs_count = 3,
.hs_count = 3,
},
.fs_descs = {
.intf = {
.bLength = sizeof(descriptors.fs_descs.intf),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = ADB_CLASS,
.bInterfaceSubClass = ADB_SUBCLASS,
.bInterfaceProtocol = ADB_PROTOCOL,
.iInterface = 1, /* first string from the provided table */
},
.source = {
.bLength = sizeof(descriptors.fs_descs.source),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = MAX_PACKET_SIZE_FS,
},
.sink = {
.bLength = sizeof(descriptors.fs_descs.sink),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = MAX_PACKET_SIZE_FS,
},
},
.hs_descs = {
.intf = {
.bLength = sizeof(descriptors.hs_descs.intf),
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = ADB_CLASS,
.bInterfaceSubClass = ADB_SUBCLASS,
.bInterfaceProtocol = ADB_PROTOCOL,
.iInterface = 1, /* first string from the provided table */
},
.source = {
.bLength = sizeof(descriptors.hs_descs.source),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = MAX_PACKET_SIZE_HS,
},
.sink = {
.bLength = sizeof(descriptors.hs_descs.sink),
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = MAX_PACKET_SIZE_HS,
},
},
};
#define STR_INTERFACE_ "ADB Interface"
static const struct {
struct usb_functionfs_strings_head header;
struct {
__le16 code;
const char str1[sizeof(STR_INTERFACE_)];
} __attribute__((packed)) lang0;
} __attribute__((packed)) strings = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
.length = cpu_to_le32(sizeof(strings)),
.str_count = cpu_to_le32(1),
.lang_count = cpu_to_le32(1),
},
.lang0 = {
cpu_to_le16(0x0409), /* en-us */
STR_INTERFACE_,
},
};
static void *usb_adb_open_thread(void *x)
{
struct usb_handle *usb = (struct usb_handle *)x;
int fd;
while (1) {
// wait until the USB device needs opening
adb_mutex_lock(&usb->lock);
while (usb->fd != -1)
adb_cond_wait(&usb->notify, &usb->lock);
adb_mutex_unlock(&usb->lock);
D("[ usb_thread - opening device ]\n");
do {
/* XXX use inotify? */
fd = unix_open("/dev/android_adb", O_RDWR);
if (fd < 0) {
// to support older kernels
fd = unix_open("/dev/android", O_RDWR);
}
if (fd < 0) {
adb_sleep_ms(1000);
}
} while (fd < 0);
D("[ opening device succeeded ]\n");
close_on_exec(fd);
usb->fd = fd;
D("[ usb_thread - registering device ]\n");
register_usb_transport(usb, 0, 0, 1);
}
// never gets here
return 0;
}
static int usb_adb_write(usb_handle *h, const void *data, int len)
{
int n;
D("about to write (fd=%d, len=%d)\n", h->fd, len);
n = adb_write(h->fd, data, len);
if(n != len) {
D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
h->fd, n, errno, strerror(errno));
return -1;
}
D("[ done fd=%d ]\n", h->fd);
return 0;
}
static int usb_adb_read(usb_handle *h, void *data, int len)
{
int n;
D("about to read (fd=%d, len=%d)\n", h->fd, len);
n = adb_read(h->fd, data, len);
if(n != len) {
D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
h->fd, n, errno, strerror(errno));
return -1;
}
D("[ done fd=%d ]\n", h->fd);
return 0;
}
static void usb_adb_kick(usb_handle *h)
{
D("usb_kick\n");
adb_mutex_lock(&h->lock);
adb_close(h->fd);
h->fd = -1;
// notify usb_adb_open_thread that we are disconnected
adb_cond_signal(&h->notify);
adb_mutex_unlock(&h->lock);
}
static void usb_adb_init()
{
usb_handle *h;
adb_thread_t tid;
int fd;
h = calloc(1, sizeof(usb_handle));
h->write = usb_adb_write;
h->read = usb_adb_read;
h->kick = usb_adb_kick;
h->fd = -1;
adb_cond_init(&h->notify, 0);
adb_mutex_init(&h->lock, 0);
// Open the file /dev/android_adb_enable to trigger
// the enabling of the adb USB function in the kernel.
// We never touch this file again - just leave it open
// indefinitely so the kernel will know when we are running
// and when we are not.
fd = unix_open("/dev/android_adb_enable", O_RDWR);
if (fd < 0) {
D("failed to open /dev/android_adb_enable\n");
} else {
close_on_exec(fd);
}
D("[ usb_init - starting thread ]\n");
if(adb_thread_create(&tid, usb_adb_open_thread, h)){
fatal_errno("cannot create usb thread");
}
}
static void set_udc(void)
{
static char g_udc_controller[64];
const char *udc_config_path = "/sys/kernel/config/usb_gadget/g1/UDC";
int fd;
if (strlen(g_udc_controller) <= 0) {
const char *udc_path = "/sys/class/udc";
DIR *dir;
struct dirent *ent;
dir = opendir(udc_path);
if (!dir) {
D("[%s: opendir %s failed]\n", __func__, udc_path);
return;
}
while((ent = readdir(dir)) != NULL) {
if (ent->d_type & DT_REG) {
if (!strcmp(".", ent->d_name))
continue;
else if (!strcmp("..", ent->d_name))
continue;
else
break;
}
}
if (!ent) {
D("cannot found udc dir, udc driver maybe not select\n");
exit(-1);
}
D("%s: get udc controller: %s \n", __func__, ent->d_name);
strncpy(g_udc_controller, ent->d_name, sizeof(g_udc_controller));
closedir(dir);
}
fd = adb_open(udc_config_path, O_RDWR);
if (fd<0) {
D("[%s: open %s failed]\n", __func__, udc_config_path);
return;
}
adb_write(fd, g_udc_controller, strlen(g_udc_controller));
adb_close(fd);
return;
}
static void init_functionfs(struct usb_handle *h)
{
ssize_t ret;
D("OPENING %s\n", USB_FFS_ADB_EP0);
h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
if (h->control < 0) {
D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
goto err;
}
ret = adb_write(h->control, &descriptors, sizeof(descriptors));
if (ret < 0) {
D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
goto err;
}
ret = adb_write(h->control, &strings, sizeof(strings));
if (ret < 0) {
D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
goto err;
}
h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
if (h->bulk_out < 0) {
D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
goto err;
}
h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
if (h->bulk_in < 0) {
D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
goto err;
}
/* add by ForeverCai
* In FFS mode, udc name must be set after initializaion
* So add this function.
* */
set_udc();
return;
err:
if (h->bulk_in > 0) {
adb_close(h->bulk_in);
h->bulk_in = -1;
}
if (h->bulk_out > 0) {
adb_close(h->bulk_out);
h->bulk_out = -1;
}
if (h->control > 0) {
adb_close(h->control);
h->control = -1;
}
return;
}
static void *usb_ffs_open_thread(void *x)
{
struct usb_handle *usb = (struct usb_handle *)x;
while (1) {
// wait until the USB device needs opening
adb_mutex_lock(&usb->lock);
while (usb->control != -1)
adb_cond_wait(&usb->notify, &usb->lock);
adb_mutex_unlock(&usb->lock);
while (1) {
init_functionfs(usb);
if (usb->control >= 0)
break;
adb_sleep_ms(1000);
}
D("[ usb_thread - registering device ]\n");
register_usb_transport(usb, 0, 0, 1);
}
// never gets here
return 0;
}
static int bulk_write(int bulk_in, const char *buf, size_t length)
{
size_t count = 0;
int ret;
do {
ret = adb_write(bulk_in, buf + count, length - count);
if (ret < 0) {
if (errno != EINTR)
return ret;
} else {
count += ret;
}
} while (count < length);
D("[ bulk_write done fd=%d ]\n", bulk_in);
return count;
}
static int usb_ffs_write(usb_handle *h, const void *data, int len)
{
int n;
D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
n = bulk_write(h->bulk_in, data, len);
if (n != len) {
D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
h->bulk_in, n, errno, strerror(errno));
return -1;
}
D("[ done fd=%d ]\n", h->bulk_in);
return 0;
}
static int bulk_read(int bulk_out, char *buf, size_t length)
{
size_t count = 0;
int ret;
do {
ret = adb_read(bulk_out, buf + count, length - count);
if (ret < 0) {
if (errno != EINTR) {
D("[ bulk_read failed fd=%d length=%d count=%d ]\n",
bulk_out, length, count);
return ret;
}
} else {
count += ret;
}
} while (count < length);
return count;
}
static int usb_ffs_read(usb_handle *h, void *data, int len)
{
int n;
D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
n = bulk_read(h->bulk_out, data, len);
if (n != len) {
D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
h->bulk_out, n, errno, strerror(errno));
return -1;
}
D("[ done fd=%d ]\n", h->bulk_out);
return 0;
}
static void usb_ffs_kick(usb_handle *h)
{
int err;
err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
if (err < 0)
D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
if (err < 0)
D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
adb_mutex_lock(&h->lock);
adb_close(h->control);
adb_close(h->bulk_out);
adb_close(h->bulk_in);
h->control = h->bulk_out = h->bulk_in = -1;
// notify usb_ffs_open_thread that we are disconnected
adb_cond_signal(&h->notify);
adb_mutex_unlock(&h->lock);
}
static void usb_ffs_init()
{
usb_handle *h;
adb_thread_t tid;
D("[ usb_init - using FunctionFS ]\n");
h = calloc(1, sizeof(usb_handle));
h->write = usb_ffs_write;
h->read = usb_ffs_read;
h->kick = usb_ffs_kick;
h->control = -1;
h->bulk_out = -1;
h->bulk_out = -1;
adb_cond_init(&h->notify, 0);
adb_mutex_init(&h->lock, 0);
D("[ usb_init - starting thread ]\n");
if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
fatal_errno("[ cannot create usb thread ]\n");
}
}
void usb_init()
{
if (access(USB_FFS_ADB_EP0, F_OK) == 0)
usb_ffs_init();
else
usb_adb_init();
}
void usb_cleanup()
{
}
int usb_write(usb_handle *h, const void *data, int len)
{
return h->write(h, data, len);
}
int usb_read(usb_handle *h, void *data, int len)
{
return h->read(h, data, len);
}
int usb_close(usb_handle *h)
{
return 0;
}
void usb_kick(usb_handle *h)
{
h->kick(h);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化