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 <xiaoguang.wu@intel.com>
Reviewed-by: Shuo Liu <shuo.a.liu@intel.com>
Reviewed-by: Yu Wang <yu1.wang@intel.com>
Reviewed-by: Zhao Yakui <yakui.zhao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Wu, Xiaoguang 2018-05-06 21:02:11 +08:00 committed by lijinxia
parent 0181d19a61
commit 8aef2cb34b
3 changed files with 105 additions and 2 deletions

View File

@ -41,6 +41,7 @@
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include "usb.h"
#include "usbdi.h"
#include "xhcireg.h"

View File

@ -83,7 +83,8 @@
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#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;
}

View File

@ -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,