From 1c426172aa3a5047914ee14bb0ade36bff0e6826 Mon Sep 17 00:00:00 2001 From: INSUN PYO Date: Thu, 30 Jan 2020 15:22:03 +0900 Subject: [PATCH] Refactoring usb gadget header #2 Change-Id: Ic9473dbbeee14d609e357c1d3fcc00dbea1dc434 --- hw/shared.h | 2 - hw/usb_cfs_client_common.c | 2 +- hw/usb_client_common.c | 2 +- hw/usb_gadget.h | 76 +++++++++++++++++++------------------- hw/usb_gadget_common.c | 3 +- 5 files changed, 40 insertions(+), 45 deletions(-) diff --git a/hw/shared.h b/hw/shared.h index a0687e2..990c55f 100644 --- a/hw/shared.h +++ b/hw/shared.h @@ -40,8 +40,6 @@ #define _E(x, ...) do { } while (0) #endif -#define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0])) - #define SHARED_H_BUF_MAX 255 static inline int sys_read_buf(char *file, char *buf, int len) diff --git a/hw/usb_cfs_client_common.c b/hw/usb_cfs_client_common.c index 99d5923..b67d86d 100644 --- a/hw/usb_cfs_client_common.c +++ b/hw/usb_cfs_client_common.c @@ -205,7 +205,7 @@ static int cfs_find_func(const char *name, const char *instance) { int i; - for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i) + for (i = 0; _available_funcs[i]; ++i) if (cfs_match_func(_available_funcs[i], name, instance)) return i; diff --git a/hw/usb_client_common.c b/hw/usb_client_common.c index ad1208e..d008485 100644 --- a/hw/usb_client_common.c +++ b/hw/usb_client_common.c @@ -146,7 +146,7 @@ static int legacy_find_func(const char *name) { int i; - for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i) + for (i = 0; _available_funcs[i]; ++i) if (!strcmp(name, _available_funcs[i]->name)) return i; diff --git a/hw/usb_gadget.h b/hw/usb_gadget.h index 06c6ef8..a3e5d58 100644 --- a/hw/usb_gadget.h +++ b/hw/usb_gadget.h @@ -36,26 +36,14 @@ */ #define USB_GADGET_DEVICE_VERSION MAKE_VERSION(0,1) -#define _HELPER_Y(x) ((x) & -(x)) - -/* Count number of trailing zeros using Dean Gaudet's algorithm */ -#define _HELPER_CTZ(mask) \ - ((_HELPER_Y(mask) ? 0 : 1) + \ - ((_HELPER_Y(mask) & 0x0000FFFF) ? 0 : 16) + \ - ((_HELPER_Y(mask) & 0x00FF00FF) ? 0 : 8) + \ - ((_HELPER_Y(mask) & 0x0F0F0F0F) ? 0 : 4) + \ - ((_HELPER_Y(mask) & 0x33333333) ? 0 : 2) + \ - ((_HELPER_Y(mask) & 0x55555555) ? 0 : 1)) - -/* Function IDX in array is number of trailing zeros */ -#define FUNC_IDX_FROM_MASK(mask) _HELPER_CTZ(mask) - /* - * legacy enable : enable gadget -> handler(1) -> ffs_service start -> service start - * legacy disable : service stop -> ffs_service stop -> handler(0) -> disable gadget + * legacy enable(usb plug) : enable gadget -> handler(1) -> ffs_service start -> service start + * legacy disable(usb unplug) : service stop -> ffs_service stop -> handler(0) -> disable gadget * - * configfs enable : ffs_service start -> enable gadget -> handler(1) -> service start - * configfs disable : service stop -> handler(0) -> disable gadget (ffs_service is never stopped until changing usb mode) + * configfs init(booting) : ffs_service start + * configfs enable(usb plug) : enable gadget -> handler(1) -> service start + * configfs disable(usb unplug) : service stop -> handler(0) -> disable gadget + * configfs deinit : ffs_service stop */ struct usb_function { int id; @@ -111,15 +99,27 @@ struct usb_gadget { }; typedef enum { - USB_FUNCTION_NONE = 0, - USB_FUNCTION_MTP = 1 << 0, - USB_FUNCTION_ACM = 1 << 1, - USB_FUNCTION_SDB = 1 << 2, - USB_FUNCTION_RNDIS = 1 << 3, - USB_FUNCTION_DIAG = 1 << 4, - USB_FUNCTION_CONN_GADGET = 1 << 5, - USB_FUNCTION_DM = 1 << 6, - USB_FUNCTION_RMNET = 1 << 7, + USB_FUNCTION_IDX_MTP = 0, + USB_FUNCTION_IDX_ACM = 1, + USB_FUNCTION_IDX_SDB = 2, + USB_FUNCTION_IDX_RNDIS = 3, + USB_FUNCTION_IDX_DIAG = 4, + USB_FUNCTION_IDX_CONN_GADGET = 5, + USB_FUNCTION_IDX_DM = 6, + USB_FUNCTION_IDX_RMNET = 7, + USB_FUNCTION_IDX_MAX = USB_FUNCTION_IDX_RMNET + 1 +} usb_function_idx_e; + +typedef enum { + USB_FUNCTION_NONE = 0, + USB_FUNCTION_MTP = 1 << USB_FUNCTION_IDX_MTP, + USB_FUNCTION_ACM = 1 << USB_FUNCTION_IDX_ACM, + USB_FUNCTION_SDB = 1 << USB_FUNCTION_IDX_SDB, + USB_FUNCTION_RNDIS = 1 << USB_FUNCTION_IDX_RNDIS, + USB_FUNCTION_DIAG = 1 << USB_FUNCTION_IDX_DIAG, + USB_FUNCTION_CONN_GADGET = 1 << USB_FUNCTION_IDX_CONN_GADGET, + USB_FUNCTION_DM = 1 << USB_FUNCTION_IDX_DM, + USB_FUNCTION_RMNET = 1 << USB_FUNCTION_IDX_RMNET } usb_function_e; static void free_simple_func(struct usb_function *func) @@ -196,20 +196,18 @@ DEFINE_USB_FUNCTION(USB_FUNCTION_ACM, acm, NULL, "data-router", DEFINE_USB_FUNCTION(USB_FUNCTION_RNDIS, rndis, NULL, "sshd", rndis_handler); #undef DEFINE_USB_FUNCTION -#define MAKE_FUNC_AVAILABLE(_name, _vname) \ - [FUNC_IDX_FROM_MASK(USB_FUNCTION_##_name)] = &_##_vname##_function - +/* Caution: index order of arrary is important, because simple_id_to_gadget() uses it. */ static struct usb_function *_available_funcs[] = { - MAKE_FUNC_AVAILABLE(MTP, mtp), - MAKE_FUNC_AVAILABLE(ACM, acm), - MAKE_FUNC_AVAILABLE(SDB, sdb), - MAKE_FUNC_AVAILABLE(RNDIS, rndis), - MAKE_FUNC_AVAILABLE(DIAG, diag), - MAKE_FUNC_AVAILABLE(CONN_GADGET, conn_gadget), - MAKE_FUNC_AVAILABLE(DM, dm), - MAKE_FUNC_AVAILABLE(RMNET, rmnet), + [USB_FUNCTION_IDX_MTP] = &_mtp_function, + [USB_FUNCTION_IDX_ACM] = &_acm_function, + [USB_FUNCTION_IDX_SDB] = &_sdb_function, + [USB_FUNCTION_IDX_RNDIS] = &_rndis_function, + [USB_FUNCTION_IDX_DIAG] = &_diag_function, + [USB_FUNCTION_IDX_CONN_GADGET] = &_conn_gadget_function, + [USB_FUNCTION_IDX_DM] = &_dm_function, + [USB_FUNCTION_IDX_RMNET] = &_rmnet_function, + [USB_FUNCTION_IDX_MAX] = NULL /* An indicator to end the array */ }; -#undef MAKE_FUNC_AVAILABLE struct usb_gadget_id { unsigned int function_mask; diff --git a/hw/usb_gadget_common.c b/hw/usb_gadget_common.c index e9db26a..4552b53 100644 --- a/hw/usb_gadget_common.c +++ b/hw/usb_gadget_common.c @@ -26,7 +26,6 @@ #include -#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) #define zalloc(amount) calloc(1, amount) /* Based on slp-gadget and initial version of USB HAL by Taeyoung Kim */ @@ -355,7 +354,7 @@ static int simple_id_to_gadget(struct usb_gadget_id *gadget_id, gadget->funcs = funcs; idx = 0; - for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i) { + for (i = 0; _available_funcs[i]; ++i) { int func_id = 1 << i; if (!(gadget_id->function_mask & func_id)) -- 2.34.1