From 8aef2cb34b1e4dfec381956453a2f44869451354 Mon Sep 17 00:00:00 2001 From: "Wu, Xiaoguang" Date: Sun, 6 May 2018 21:02:11 +0800 Subject: [PATCH] DM USB: Add some APIs for check native related configurations. New USB APIs: usb_native_is_bus_existed/usb_native_is_port_existed: Check if specific usb bus or port are valid or not. usb_native_is_ss_port: Check if the specific port is supper speed usb port. Change-Id: I9ab54f6e81742321128d6abd5845ef966f0e9f37 Signed-off-by: Wu, Xiaoguang Reviewed-by: Shuo Liu Reviewed-by: Yu Wang Reviewed-by: Zhao Yakui Acked-by: Eddie Dong --- devicemodel/hw/pci/xhci.c | 1 + devicemodel/hw/usb_core.c | 98 +++++++++++++++++++++++++++++++++- devicemodel/include/usb_core.h | 8 ++- 3 files changed, 105 insertions(+), 2 deletions(-) diff --git a/devicemodel/hw/pci/xhci.c b/devicemodel/hw/pci/xhci.c index d807dfc28..2d1697ec6 100644 --- a/devicemodel/hw/pci/xhci.c +++ b/devicemodel/hw/pci/xhci.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "usb.h" #include "usbdi.h" #include "xhcireg.h" diff --git a/devicemodel/hw/usb_core.c b/devicemodel/hw/usb_core.c index 906ab8cf0..e44ecfca4 100644 --- a/devicemodel/hw/usb_core.c +++ b/devicemodel/hw/usb_core.c @@ -83,7 +83,8 @@ #include #include #include - +#include +#include #include "usb_core.h" SET_DECLARE(usb_emu_set, struct usb_devemu); @@ -123,3 +124,98 @@ usb_data_xfer_append(struct usb_data_xfer *xfer, void *buf, int blen, xfer->tail = (xfer->tail + 1) % USB_MAX_XFER_BLOCKS; return xb; } + +int +usb_native_is_bus_existed(uint8_t bus_num) +{ + char buf[128]; + + snprintf(buf, sizeof(buf), "%s/usb%d", NATIVE_USBSYS_DEVDIR, bus_num); + return access(buf, R_OK) ? 0 : 1; +} + +int +usb_native_is_ss_port(uint8_t bus_of_port) +{ + char buf[128]; + char speed[8]; + int rc, fd; + int usb2_speed_sz = sizeof(NATIVE_USB2_SPEED); + int usb3_speed_sz = sizeof(NATIVE_USB3_SPEED); + + assert(usb_native_is_bus_existed(bus_of_port)); + snprintf(buf, sizeof(buf), "%s/usb%d/speed", NATIVE_USBSYS_DEVDIR, + bus_of_port); + if (access(buf, R_OK)) { + UPRINTF(LWRN, "can't find speed file\r\n"); + return 0; + } + + fd = open(buf, O_RDONLY); + if (fd < 0) { + UPRINTF(LWRN, "fail to open maxchild file\r\n"); + return 0; + } + + rc = read(fd, &speed, sizeof(speed)); + if (rc < 0) { + UPRINTF(LWRN, "fail to read speed file\r\n"); + goto errout; + } + + if (rc < usb2_speed_sz) { + UPRINTF(LWRN, "read invalid speed data\r\n"); + goto errout; + } + + if (strncmp(speed, NATIVE_USB3_SPEED, usb3_speed_sz)) + goto errout; + + close(fd); + return 1; +errout: + close(fd); + return 0; +} + +int +usb_native_is_port_existed(uint8_t bus_num, uint8_t port_num) +{ + int native_port_cnt; + int rc, fd; + char buf[128]; + char cnt[8]; + + if (!usb_native_is_bus_existed(bus_num)) + return 0; + + snprintf(buf, sizeof(buf), "%s/usb%d/maxchild", NATIVE_USBSYS_DEVDIR, + bus_num); + if (access(buf, R_OK)) { + UPRINTF(LWRN, "can't find maxchild file\r\n"); + return 0; + } + + fd = open(buf, O_RDONLY); + if (fd < 0) { + UPRINTF(LWRN, "fail to open maxchild file\r\n"); + return 0; + } + + rc = read(fd, &cnt, sizeof(cnt)); + if (rc < 0) { + UPRINTF(LWRN, "fail to read maxchild file\r\n"); + close(fd); + return 0; + } + + native_port_cnt = atoi(cnt); + if (port_num > native_port_cnt || port_num < 0) { + UPRINTF(LWRN, "invalid port_num %d, max port count %d\r\n", + port_num, native_port_cnt); + close(fd); + return 0; + } + close(fd); + return 1; +} diff --git a/devicemodel/include/usb_core.h b/devicemodel/include/usb_core.h index ed5c65519..a482df03e 100644 --- a/devicemodel/include/usb_core.h +++ b/devicemodel/include/usb_core.h @@ -196,11 +196,17 @@ enum USB_ERRCODE { #define UPRINTF(lvl, fmt, args...) \ do { if (lvl <= usb_log_level) printf(LOG_TAG fmt, ##args); } while (0) +#define NATIVE_USBSYS_DEVDIR "/sys/bus/usb/devices" +#define NATIVE_USB2_SPEED "480" +#define NATIVE_USB3_SPEED "5000" + extern int usb_log_level; inline int usb_get_log_level(void) { return usb_log_level; } inline void usb_set_log_level(int level) { usb_log_level = level; } struct usb_devemu *usb_emu_finddev(char *name); - +int usb_native_is_bus_existed(uint8_t bus_num); +int usb_native_is_ss_port(uint8_t bus_of_port); +int usb_native_is_port_existed(uint8_t bus_num, uint8_t port_num); struct usb_data_xfer_block *usb_data_xfer_append(struct usb_data_xfer *xfer, void *buf, int blen,