Remove definition of gadget structure to avoid direct access to its fields. Rename that structure to usbg_gadget. Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx> --- examples/gadget-acm-ecm.c | 4 +- examples/show-gadgets.c | 64 ++++++++++++------ include/usbg/usbg.h | 139 +++++++++++++++++---------------------- src/usbg.c | 159 ++++++++++++++++++++++++++------------------- 4 files changed, 196 insertions(+), 170 deletions(-) diff --git a/examples/gadget-acm-ecm.c b/examples/gadget-acm-ecm.c index 672d4b5..2a37f14 100644 --- a/examples/gadget-acm-ecm.c +++ b/examples/gadget-acm-ecm.c @@ -30,9 +30,9 @@ int main(void) { usbg_state *s; - struct gadget *g; + usbg_gadget *g; struct function *f; - struct config *c; + usbg_config *c; struct function *f_acm0, *f_acm1, *f_ecm; int ret = -EINVAL; diff --git a/examples/show-gadgets.c b/examples/show-gadgets.c index a53c355..f7bd3c1 100644 --- a/examples/show-gadgets.c +++ b/examples/show-gadgets.c @@ -27,22 +27,34 @@ * in the system */ -void show_gadget(struct gadget *g) +void show_gadget(usbg_gadget *g) { + char buf[USBG_MAX_STR_LENGTH]; + struct gadget_attrs g_attrs; + struct gadget_strs g_strs; + + usbg_get_gadget_name(g, buf, USBG_MAX_STR_LENGTH); + usbg_get_gadget_attrs(g, &g_attrs); + fprintf(stdout, "ID %04x:%04x '%s'\n", - g->attrs.idVendor, g->attrs.idProduct, g->name); - fprintf(stdout, " UDC\t\t\t%s\n", g->udc); - fprintf(stdout, " bDeviceClass\t\t0x%02x\n", g->attrs.bDeviceClass); - fprintf(stdout, " bDeviceSubClass\t0x%02x\n", g->attrs.bDeviceSubClass); - fprintf(stdout, " bDeviceProtocol\t0x%02x\n", g->attrs.bDeviceProtocol); - fprintf(stdout, " bMaxPacketSize0\t0x%02x\n", g->attrs.bMaxPacketSize0); - fprintf(stdout, " bcdDevice\t\t0x%04x\n", g->attrs.bcdDevice); - fprintf(stdout, " bcdUSB\t\t0x%04x\n", g->attrs.bcdUSB); - fprintf(stdout, " idVendor\t\t0x%04x\n", g->attrs.idVendor); - fprintf(stdout, " idProduct\t\t0x%04x\n", g->attrs.idProduct); - fprintf(stdout, " Serial Number\t\t%s\n", g->strs.str_ser); - fprintf(stdout, " Manufacturer\t\t%s\n", g->strs.str_mnf); - fprintf(stdout, " Product\t\t%s\n", g->strs.str_prd); + g_attrs.idVendor, g_attrs.idProduct, buf); + + usbg_get_gadget_udc(g, buf, USBG_MAX_STR_LENGTH); + fprintf(stdout, " UDC\t\t\t%s\n", buf); + + fprintf(stdout, " bDeviceClass\t\t0x%02x\n", g_attrs.bDeviceClass); + fprintf(stdout, " bDeviceSubClass\t0x%02x\n", g_attrs.bDeviceSubClass); + fprintf(stdout, " bDeviceProtocol\t0x%02x\n", g_attrs.bDeviceProtocol); + fprintf(stdout, " bMaxPacketSize0\t0x%02x\n", g_attrs.bMaxPacketSize0); + fprintf(stdout, " bcdDevice\t\t0x%04x\n", g_attrs.bcdDevice); + fprintf(stdout, " bcdUSB\t\t0x%04x\n", g_attrs.bcdUSB); + fprintf(stdout, " idVendor\t\t0x%04x\n", g_attrs.idVendor); + fprintf(stdout, " idProduct\t\t0x%04x\n", g_attrs.idProduct); + + usbg_get_gadget_strs(g, &g_strs); + fprintf(stdout, " Serial Number\t\t%s\n", g_strs.str_ser); + fprintf(stdout, " Manufacturer\t\t%s\n", g_strs.str_mnf); + fprintf(stdout, " Product\t\t%s\n", g_strs.str_prd); } void show_function(struct function *f) @@ -75,14 +87,24 @@ void show_function(struct function *f) } } -void show_config(struct config *c) +void show_config(usbg_config *c) { struct binding *b; + struct function *f; + char buf[USBG_MAX_STR_LENGTH], buf2[USBG_MAX_STR_LENGTH]; + struct config_attrs c_attrs; + struct config_strs c_strs; + + usbg_get_config_name(c, buf, USBG_MAX_STR_LENGTH); + fprintf(stdout, " Configuration '%s'\n", buf); + + usbg_get_config_attrs(c, &c_attrs); + fprintf(stdout, " MaxPower\t\t%d\n", c_attrs.bMaxPower); + fprintf(stdout, " bmAttributes\t0x%02x\n", c_attrs.bmAttributes); + + usbg_get_config_strs(c, &c_strs); + fprintf(stdout, " configuration\t%s\n", c_strs.configuration); - fprintf(stdout, " Configuration '%s'\n", c->name); - fprintf(stdout, " MaxPower\t\t%d\n", c->attrs.bMaxPower); - fprintf(stdout, " bmAttributes\t0x%02x\n", c->attrs.bmAttributes); - fprintf(stdout, " configuration\t%s\n", c->strs.configuration); usbg_for_each_binding(b, c) fprintf(stdout, " %s -> %s\n", b->name,b->target->name); } @@ -90,9 +112,9 @@ void show_config(struct config *c) int main(void) { usbg_state *s; - struct gadget *g; + usbg_gadget *g; struct function *f; - struct config *c; + usbg_config *c; struct binding *b; struct function *f_acm0, *f_acm1, *f_ecm; diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h index 9b80407..7706a1b 100644 --- a/include/usbg/usbg.h +++ b/include/usbg/usbg.h @@ -45,6 +45,8 @@ * Internal structures */ struct usbg_state; +struct usbg_gadget; +struct usbg_config; /** * @brief State of the gadget devices in the system @@ -52,6 +54,16 @@ struct usbg_state; typedef struct usbg_state usbg_state; /** + * @brief USB gadget device + */ +typedef struct usbg_gadget usbg_gadget; + +/** + * @brief USB configuration + */ +typedef struct usbg_config usbg_config; + +/** * @struct gadget_attrs * @brief USB gadget device attributes */ @@ -79,25 +91,6 @@ struct gadget_strs }; /** - * @struct gadget - * @brief USB gadget device - */ -struct gadget -{ - char name[USBG_MAX_NAME_LENGTH]; - char path[USBG_MAX_PATH_LENGTH]; - char udc[USBG_MAX_STR_LENGTH]; - - struct gadget_attrs attrs; - struct gadget_strs strs; - - TAILQ_ENTRY(gadget) gnode; - TAILQ_HEAD(chead, config) configs; - TAILQ_HEAD(fhead, function) functions; - usbg_state *parent; -}; - -/** * @struct config_attrs * @brief USB configuration attributes */ @@ -117,22 +110,6 @@ struct config_strs }; /** - * @struct config - * @brief USB gadget configuration attributes - */ -struct config -{ - TAILQ_ENTRY(config) cnode; - TAILQ_HEAD(bhead, binding) bindings; - struct gadget *parent; - - char name[USBG_MAX_NAME_LENGTH]; - char path[USBG_MAX_PATH_LENGTH]; - struct config_attrs attrs; - struct config_strs strs; -}; - -/** * @enum function_type * @brief Supported USB function types */ @@ -193,7 +170,7 @@ union attrs { struct function { TAILQ_ENTRY(function) fnode; - struct gadget *parent; + usbg_gadget *parent; char name[USBG_MAX_NAME_LENGTH]; char path[USBG_MAX_PATH_LENGTH]; @@ -211,7 +188,7 @@ struct function struct binding { TAILQ_ENTRY(binding) bnode; - struct config *parent; + usbg_config *parent; struct function *target; char name[USBG_MAX_NAME_LENGTH]; @@ -257,7 +234,7 @@ extern char *usbg_get_configfs_path(usbg_state *s, char *buf, size_t len); * @param name Name of the gadget device * @return Pointer to gadget or NULL if a matching gadget isn't found */ -extern struct gadget *usbg_get_gadget(usbg_state *s, const char *name); +extern usbg_gadget *usbg_get_gadget(usbg_state *s, const char *name); /** * @brief Get a function by name @@ -265,7 +242,7 @@ extern struct gadget *usbg_get_gadget(usbg_state *s, const char *name); * @param name Name of the function * @return Pointer to function or NULL if a matching function isn't found */ -extern struct function *usbg_get_function(struct gadget *g, const char *name); +extern struct function *usbg_get_function(usbg_gadget *g, const char *name); /** * @brief Get a configuration by name @@ -273,7 +250,7 @@ extern struct function *usbg_get_function(struct gadget *g, const char *name); * @param name Name of the configuration * @return Pointer to config or NULL if a matching config isn't found */ -extern struct config *usbg_get_config(struct gadget *g, const char *name); +extern usbg_config *usbg_get_config(usbg_gadget *g, const char *name); /* USB gadget allocation and configuration */ @@ -285,7 +262,7 @@ extern struct config *usbg_get_config(struct gadget *g, const char *name); * @param idProduct Gadget product ID * @return Pointer to gadget or NULL if the gadget cannot be created */ -extern struct gadget *usbg_create_gadget_vid_pid(usbg_state *s, char *name, +extern usbg_gadget *usbg_create_gadget_vid_pid(usbg_state *s, char *name, uint16_t idVendor, uint16_t idProduct); /** @@ -298,7 +275,7 @@ extern struct gadget *usbg_create_gadget_vid_pid(usbg_state *s, char *name, * @note Given strings are assumed to be in US English * @return Pointer to gadget or NULL if the gadget cannot be created */ -extern struct gadget *usbg_create_gadget(usbg_state *s, char *name, +extern usbg_gadget *usbg_create_gadget(usbg_state *s, char *name, struct gadget_attrs *g_attrs, struct gadget_strs *g_strs); /** @@ -306,7 +283,7 @@ extern struct gadget *usbg_create_gadget(usbg_state *s, char *name, * @param g Pointer to gadget * @param g_attrs Gadget attributes */ -extern void usbg_set_gadget_attrs(struct gadget *g, +extern void usbg_set_gadget_attrs(usbg_gadget *g, struct gadget_attrs *g_attrs); /** @@ -315,7 +292,7 @@ extern void usbg_set_gadget_attrs(struct gadget *g, * @param g_attrs Structure to be filled * @retur Pointer to filled structure or NULL if error occurred. */ -extern struct gadget_attrs *usbg_get_gadget_attrs(struct gadget *g, +extern struct gadget_attrs *usbg_get_gadget_attrs(usbg_gadget *g, struct gadget_attrs *g_attrs); /** @@ -323,7 +300,7 @@ extern struct gadget_attrs *usbg_get_gadget_attrs(struct gadget *g, * @param g Gadget which name length should be returned * @return Length of name string or -1 if error occurred. */ -extern size_t usbg_get_gadget_name_len(struct gadget *g); +extern size_t usbg_get_gadget_name_len(usbg_gadget *g); /** * @brieg Get gadget name @@ -332,28 +309,28 @@ extern size_t usbg_get_gadget_name_len(struct gadget *g); * @param len Length of given buffer * @return Pointer to destination or NULL if error occurred. */ -extern char *usbg_get_gadget_name(struct gadget* g, char *buf, size_t len); +extern char *usbg_get_gadget_name(usbg_gadget* g, char *buf, size_t len); /** * @brief Set the USB gadget vendor id * @param g Pointer to gadget * @param idVendor USB device vendor id */ -extern void usbg_set_gadget_vendor_id(struct gadget *g, uint16_t idVendor); +extern void usbg_set_gadget_vendor_id(usbg_gadget *g, uint16_t idVendor); /** * @brief Set the USB gadget product id * @param g Pointer to gadget * @param idProduct USB device product id */ -extern void usbg_set_gadget_product_id(struct gadget *g, uint16_t idProduct); +extern void usbg_set_gadget_product_id(usbg_gadget *g, uint16_t idProduct); /** * @brief Set the USB gadget device class code * @param g Pointer to gadget * @param bDeviceClass USB device class code */ -extern void usbg_set_gadget_device_class(struct gadget *g, +extern void usbg_set_gadget_device_class(usbg_gadget *g, uint8_t bDeviceClass); /** @@ -361,7 +338,7 @@ extern void usbg_set_gadget_device_class(struct gadget *g, * @param g Pointer to gadget * @param bDeviceProtocol USB protocol code */ -extern void usbg_set_gadget_device_protocol(struct gadget *g, +extern void usbg_set_gadget_device_protocol(usbg_gadget *g, uint8_t bDeviceProtocol); /** @@ -369,7 +346,7 @@ extern void usbg_set_gadget_device_protocol(struct gadget *g, * @param g Pointer to gadget * @param bDeviceSubClass USB device subclass code */ -extern void usbg_set_gadget_device_subclass(struct gadget *g, +extern void usbg_set_gadget_device_subclass(usbg_gadget *g, uint8_t bDeviceSubClass); /** @@ -377,7 +354,7 @@ extern void usbg_set_gadget_device_subclass(struct gadget *g, * @param g Pointer to gadget * @param bMaxPacketSize0 Maximum packet size */ -extern void usbg_set_gadget_device_max_packet(struct gadget *g, +extern void usbg_set_gadget_device_max_packet(usbg_gadget *g, uint8_t bMaxPacketSize0); /** @@ -385,7 +362,7 @@ extern void usbg_set_gadget_device_max_packet(struct gadget *g, * @param g Pointer to gadget * @param bcdDevice BCD release number */ -extern void usbg_set_gadget_device_bcd_device(struct gadget *g, +extern void usbg_set_gadget_device_bcd_device(usbg_gadget *g, uint16_t bcdDevice); /** @@ -393,7 +370,7 @@ extern void usbg_set_gadget_device_bcd_device(struct gadget *g, * @param g Pointer to gadget * @param bcdUSB BCD USB version */ -extern void usbg_set_gadget_device_bcd_usb(struct gadget *g, uint16_t bcdUSB); +extern void usbg_set_gadget_device_bcd_usb(usbg_gadget *g, uint16_t bcdUSB); /** * @brief Get the USB gadget strings @@ -401,7 +378,7 @@ extern void usbg_set_gadget_device_bcd_usb(struct gadget *g, uint16_t bcdUSB); * @param g_sttrs Structure to be filled * @retur Pointer to filled structure or NULL if error occurred. */ -extern struct gadget_strs *usbg_get_gadget_strs(struct gadget *g, +extern struct gadget_strs *usbg_get_gadget_strs(usbg_gadget *g, struct gadget_strs *g_strs); /** @@ -410,7 +387,7 @@ extern struct gadget_strs *usbg_get_gadget_strs(struct gadget *g, * @param lang USB language ID * @param g_sttrs Gadget attributes */ -extern void usbg_set_gadget_strs(struct gadget *g, int lang, +extern void usbg_set_gadget_strs(usbg_gadget *g, int lang, struct gadget_strs *g_strs); /** @@ -419,7 +396,7 @@ extern void usbg_set_gadget_strs(struct gadget *g, int lang, * @param lang USB language ID * @param ser Serial number */ -extern void usbg_set_gadget_serial_number(struct gadget *g, int lang, char *ser); +extern void usbg_set_gadget_serial_number(usbg_gadget *g, int lang, char *ser); /** * @brief Set the manufacturer name for a gadget @@ -427,7 +404,7 @@ extern void usbg_set_gadget_serial_number(struct gadget *g, int lang, char *ser) * @param lang USB language ID * @param mnf Manufacturer */ -extern void usbg_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf); +extern void usbg_set_gadget_manufacturer(usbg_gadget *g, int lang, char *mnf); /** * @brief Set the product name for a gadget @@ -435,7 +412,7 @@ extern void usbg_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf); * @param lang USB language ID * @param prd Product */ -extern void usbg_set_gadget_product(struct gadget *g, int lang, char *prd); +extern void usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd); /* USB function allocation and configuration */ @@ -447,7 +424,7 @@ extern void usbg_set_gadget_product(struct gadget *g, int lang, char *prd); * @param f_attrs Function attributes to be set. If NULL setting is omitted. * @return Pointer to function or NULL if it cannot be created */ -extern struct function *usbg_create_function(struct gadget *g, enum function_type type, +extern struct function *usbg_create_function(usbg_gadget *g, enum function_type type, char *instance, union attrs* f_attrs); /** @@ -476,7 +453,7 @@ extern char *usbg_get_function_name(struct function *f, char *buf, size_t len); * @param c_strs Configuration strings to be set * @return Pointer to configuration or NULL if it cannot be created */ -extern struct config *usbg_create_config(struct gadget *g, char *name, +extern usbg_config *usbg_create_config(usbg_gadget *g, char *name, struct config_attrs *c_attrs, struct config_strs *c_strs); /** @@ -484,7 +461,7 @@ extern struct config *usbg_create_config(struct gadget *g, char *name, * @param c Config which name length should be returned * @return Length of name string or -1 if error occurred. */ -extern size_t usbg_get_config_name_len(struct config *c); +extern size_t usbg_get_config_name_len(usbg_config *c); /** * @brieg Get config name @@ -493,14 +470,14 @@ extern size_t usbg_get_config_name_len(struct config *c); * @param len Length of given buffer * @return Pointer to destination or NULL if error occurred. */ -extern char *usbg_get_config_name(struct config* c, char *buf, size_t len); +extern char *usbg_get_config_name(usbg_config* c, char *buf, size_t len); /** * @brief Set the USB configuration attributes * @param c Pointer to configuration * @param c_attrs Configuration attributes */ -extern void usbg_set_config_attrs(struct config *c, +extern void usbg_set_config_attrs(usbg_config *c, struct config_attrs *c_attrs); /** @@ -509,7 +486,7 @@ extern void usbg_set_config_attrs(struct config *c, * @param c_attrs Structure to be filled * @retur Pointer to filled structure or NULL if error occurred. */ -extern struct config_attrs *usbg_get_config_attrs(struct config *c, +extern struct config_attrs *usbg_get_config_attrs(usbg_config *c, struct config_attrs *c_attrs); /** @@ -517,14 +494,14 @@ extern struct config_attrs *usbg_get_config_attrs(struct config *c, * @param c Pointer to config * @param bMaxPower Maximum power (in 2 mA units) */ -extern void usbg_set_config_max_power(struct config *c, int bMaxPower); +extern void usbg_set_config_max_power(usbg_config *c, int bMaxPower); /** * @brief Set the configuration bitmap attributes * @param c Pointer to config * @param bmAttributes Configuration characteristics */ -extern void usbg_set_config_bm_attrs(struct config *c, int bmAttributes); +extern void usbg_set_config_bm_attrs(usbg_config *c, int bmAttributes); /** * @brief Get the USB configuration strings @@ -532,7 +509,7 @@ extern void usbg_set_config_bm_attrs(struct config *c, int bmAttributes); * @param c_sttrs Structure to be filled * @retur Pointer to filled structure or NULL if error occurred. */ -extern struct config_strs *usbg_get_config_strs(struct config *c, +extern struct config_strs *usbg_get_config_strs(usbg_config *c, struct config_strs *c_strs); /** @@ -541,7 +518,7 @@ extern struct config_strs *usbg_get_config_strs(struct config *c, * @param lang USB language ID * @param c_sttrs Configuration strings */ -extern void usbg_set_config_strs(struct config *c, int lang, +extern void usbg_set_config_strs(usbg_config *c, int lang, struct config_strs *c_strs); /** @@ -550,7 +527,7 @@ extern void usbg_set_config_strs(struct config *c, int lang, * @param lang USB language ID * @param string Configuration description */ -extern void usbg_set_config_string(struct config *c, int lang, char *string); +extern void usbg_set_config_string(usbg_config *c, int lang, char *string); /** * @brief Add a function to a configuration @@ -559,7 +536,7 @@ extern void usbg_set_config_string(struct config *c, int lang, char *string); * @param f Pointer to function * @return 0 on success, -1 on failure. */ -extern int usbg_add_config_function(struct config *c, char *name, struct function *f); +extern int usbg_add_config_function(usbg_config *c, char *name, struct function *f); /** * @brief Get target function of given binding @@ -598,13 +575,13 @@ extern int usbg_get_udcs(struct dirent ***udc_list); * @param g Pointer to gadget * @param udc Name of UDC to enable gadget */ -extern void usbg_enable_gadget(struct gadget *g, char *udc); +extern void usbg_enable_gadget(usbg_gadget *g, char *udc); /** * @brief Disable a USB gadget device * @param g Pointer to gadget */ -extern void usbg_disable_gadget(struct gadget *g); +extern void usbg_disable_gadget(usbg_gadget *g); /** * @brief Get gadget name length @@ -612,7 +589,7 @@ extern void usbg_disable_gadget(struct gadget *g); * @return Length of name string or -1 if error occurred. * @note If gadget isn't enabled on any udc returned size is 0. */ -extern size_t usbg_get_gadget_udc_len(struct gadget *g); +extern size_t usbg_get_gadget_udc_len(usbg_gadget *g); /** * @brieg Get name of udc to which gadget is binded @@ -622,7 +599,7 @@ extern size_t usbg_get_gadget_udc_len(struct gadget *g); * @return Pointer to destination or NULL if error occurred. * @note If gadget isn't enabled on any udc returned string is empty. */ -extern char *usbg_get_gadget_udc(struct gadget* g, char *buf, size_t len); +extern char *usbg_get_gadget_udc(usbg_gadget* g, char *buf, size_t len); /* * USB function-specific attribute configuration @@ -715,7 +692,7 @@ extern void usbg_set_net_qmult(struct function *f, int qmult); * @return Pointer to gadget or NULL if list is empty. * @note Gadgets are sorted in strings (name) order */ -extern struct gadget *usbg_get_first_gadget(usbg_state *s); +extern usbg_gadget *usbg_get_first_gadget(usbg_state *s); /** * @brief Get first function in function list @@ -723,7 +700,7 @@ extern struct gadget *usbg_get_first_gadget(usbg_state *s); * @return Pointer to function or NULL if list is empty. * @note Functions are sorted in strings (name) order */ -extern struct function *usbg_get_first_function(struct gadget *g); +extern struct function *usbg_get_first_function(usbg_gadget *g); /** * @brief Get first config in config list @@ -731,7 +708,7 @@ extern struct function *usbg_get_first_function(struct gadget *g); * @return Pointer to configuration or NULL if list is empty. * @note Configs are sorted in strings (name) order */ -extern struct config *usbg_get_first_config(struct gadget *g); +extern usbg_config *usbg_get_first_config(usbg_gadget *g); /** * @brief Get first binding in binding list @@ -739,14 +716,14 @@ extern struct config *usbg_get_first_config(struct gadget *g); * @return Pointer to binding or NULL if list is empty. * @note Bindings are sorted in strings (name) order */ -extern struct binding *usbg_get_first_binding(struct config *c); +extern struct binding *usbg_get_first_binding(usbg_config *c); /** * @brief Get the next gadget on a list. * @pram g Pointer to current gadget * @return Next gadget or NULL if end of list. */ -extern struct gadget *usbg_get_next_gadget(struct gadget *g); +extern usbg_gadget *usbg_get_next_gadget(usbg_gadget *g); /** * @brief Get the next function on a list. @@ -760,7 +737,7 @@ extern struct function *usbg_get_next_function(struct function *f); * @pram g Pointer to current config * @return Next config or NULL if end of list. */ -extern struct config *usbg_get_next_config(struct config *c); +extern usbg_config *usbg_get_next_config(usbg_config *c); /** * @brief Get the next binding on a list. diff --git a/src/usbg.c b/src/usbg.c index 13aa890..467191f 100644 --- a/src/usbg.c +++ b/src/usbg.c @@ -40,7 +40,34 @@ struct usbg_state { char path[USBG_MAX_PATH_LENGTH]; - TAILQ_HEAD(ghead, gadget) gadgets; + TAILQ_HEAD(ghead, usbg_gadget) gadgets; +}; + +struct usbg_gadget +{ + char name[USBG_MAX_NAME_LENGTH]; + char path[USBG_MAX_PATH_LENGTH]; + char udc[USBG_MAX_STR_LENGTH]; + + struct gadget_attrs attrs; + struct gadget_strs strs; + + TAILQ_ENTRY(usbg_gadget) gnode; + TAILQ_HEAD(chead, usbg_config) configs; + TAILQ_HEAD(fhead, function) functions; + usbg_state *parent; +}; + +struct usbg_config +{ + TAILQ_ENTRY(usbg_config) cnode; + TAILQ_HEAD(bhead, binding) bindings; + usbg_gadget *parent; + + char name[USBG_MAX_NAME_LENGTH]; + char path[USBG_MAX_PATH_LENGTH]; + struct config_attrs attrs; + struct config_strs strs; }; /** @@ -262,7 +289,7 @@ static void usbg_parse_function_attrs(struct function *f) } } -static int usbg_parse_functions(char *path, struct gadget *g) +static int usbg_parse_functions(char *path, usbg_gadget *g) { struct function *f; int i, n; @@ -305,12 +332,12 @@ static void usbg_parse_config_strs(char *cpath, struct config_strs *c_attrs) usbg_read_string(spath, "", "configuration", c_attrs->configuration); } -static void usbg_parse_config_bindings(struct config *c) +static void usbg_parse_config_bindings(usbg_config *c) { int i, n; struct dirent **dent; char bpath[USBG_MAX_PATH_LENGTH]; - struct gadget *g = c->parent; + usbg_gadget *g = c->parent; struct binding *b; struct function *f; @@ -345,9 +372,9 @@ static void usbg_parse_config_bindings(struct config *c) free(dent); } -static int usbg_parse_configs(char *path, struct gadget *g) +static int usbg_parse_configs(char *path, usbg_gadget *g) { - struct config *c; + usbg_config *c; int i, n; struct dirent **dent; char cpath[USBG_MAX_PATH_LENGTH]; @@ -358,7 +385,7 @@ static int usbg_parse_configs(char *path, struct gadget *g) n = scandir(cpath, &dent, file_select, alphasort); for (i=0; i < n; i++) { - c = malloc(sizeof(struct config)); + c = malloc(sizeof(usbg_config)); c->parent = g; strcpy(c->name, dent[i]->d_name); strcpy(c->path, cpath); @@ -402,7 +429,7 @@ static void usbg_parse_strings(char *path, char *name, struct gadget_strs *g_str static int usbg_parse_gadgets(char *path, usbg_state *s) { - struct gadget *g; + usbg_gadget *g; int i, n; struct dirent **dent; @@ -410,7 +437,7 @@ static int usbg_parse_gadgets(char *path, usbg_state *s) n = scandir(path, &dent, file_select, alphasort); for (i=0; i < n; i++) { - g = malloc(sizeof(struct gadget)); + g = malloc(sizeof(usbg_gadget)); strcpy(g->name, dent[i]->d_name); strcpy(g->path, s->path); g->parent = s; @@ -475,8 +502,8 @@ out: void usbg_cleanup(usbg_state *s) { - struct gadget *g; - struct config *c; + usbg_gadget *g; + usbg_config *c; struct binding *b; struct function *f; @@ -514,9 +541,9 @@ char *usbg_get_configfs_path(usbg_state *s, char *buf, size_t len) return s ? strncpy(buf, s->path, len): NULL; } -struct gadget *usbg_get_gadget(usbg_state *s, const char *name) +usbg_gadget *usbg_get_gadget(usbg_state *s, const char *name) { - struct gadget *g; + usbg_gadget *g; TAILQ_FOREACH(g, &s->gadgets, gnode) if (!strcmp(g->name, name)) @@ -525,7 +552,7 @@ struct gadget *usbg_get_gadget(usbg_state *s, const char *name) return NULL; } -struct function *usbg_get_function(struct gadget *g, const char *name) +struct function *usbg_get_function(usbg_gadget *g, const char *name) { struct function *f; @@ -536,9 +563,9 @@ struct function *usbg_get_function(struct gadget *g, const char *name) return NULL; } -struct config *usbg_get_config(struct gadget *g, const char *name) +usbg_config *usbg_get_config(usbg_gadget *g, const char *name) { - struct config *c; + usbg_config *c; TAILQ_FOREACH(c, &g->configs, cnode) if (!strcmp(c->name, name)) @@ -547,7 +574,7 @@ struct config *usbg_get_config(struct gadget *g, const char *name) return NULL; } -struct binding *usbg_get_binding(struct config *c, const char *name) +struct binding *usbg_get_binding(usbg_config *c, const char *name) { struct binding *b; @@ -558,7 +585,7 @@ struct binding *usbg_get_binding(struct config *c, const char *name) return NULL; } -struct binding *usbg_get_link_binding(struct config *c, struct function *f) +struct binding *usbg_get_link_binding(usbg_config *c, struct function *f) { struct binding *b; @@ -569,15 +596,15 @@ struct binding *usbg_get_link_binding(struct config *c, struct function *f) return NULL; } -static struct gadget *usbg_create_empty_gadget(usbg_state *s, char *name) +static usbg_gadget *usbg_create_empty_gadget(usbg_state *s, char *name) { char gpath[USBG_MAX_PATH_LENGTH]; - struct gadget *g; + usbg_gadget *g; int ret; sprintf(gpath, "%s/%s", s->path, name); - g = malloc(sizeof(struct gadget)); + g = malloc(sizeof(usbg_gadget)); if (!g) { ERRORNO("allocating gadget\n"); return NULL; @@ -604,10 +631,10 @@ static struct gadget *usbg_create_empty_gadget(usbg_state *s, char *name) -struct gadget *usbg_create_gadget_vid_pid(usbg_state *s, char *name, +usbg_gadget *usbg_create_gadget_vid_pid(usbg_state *s, char *name, uint16_t idVendor, uint16_t idProduct) { - struct gadget *g; + usbg_gadget *g; if (!s) return NULL; @@ -635,10 +662,10 @@ struct gadget *usbg_create_gadget_vid_pid(usbg_state *s, char *name, return g; } -struct gadget *usbg_create_gadget(usbg_state *s, char *name, +usbg_gadget *usbg_create_gadget(usbg_state *s, char *name, struct gadget_attrs *g_attrs, struct gadget_strs *g_strs) { - struct gadget *g; + usbg_gadget *g; if (!s) return NULL; @@ -672,7 +699,7 @@ struct gadget *usbg_create_gadget(usbg_state *s, char *name, return g; } -struct gadget_attrs *usbg_get_gadget_attrs(struct gadget *g, +struct gadget_attrs *usbg_get_gadget_attrs(usbg_gadget *g, struct gadget_attrs *g_attrs) { if (g && g_attrs) { @@ -684,27 +711,27 @@ struct gadget_attrs *usbg_get_gadget_attrs(struct gadget *g, return g_attrs; } -size_t usbg_get_gadget_name_len(struct gadget *g) +size_t usbg_get_gadget_name_len(usbg_gadget *g) { return g ? strlen(g->name): -1; } -char *usbg_get_gadget_name(struct gadget* g, char *buf, size_t len) +char *usbg_get_gadget_name(usbg_gadget* g, char *buf, size_t len) { return g ? strncpy(buf, g->name, len) : NULL; } -size_t usbg_get_gadget_udc_len(struct gadget *g) +size_t usbg_get_gadget_udc_len(usbg_gadget *g) { return g ? strlen(g->udc): -1; } -char *usbg_get_gadget_udc(struct gadget* g, char *buf, size_t len) +char *usbg_get_gadget_udc(usbg_gadget* g, char *buf, size_t len) { return g ? strncpy(buf, g->udc, len) : NULL; } -void usbg_set_gadget_attrs(struct gadget *g, struct gadget_attrs *g_attrs) +void usbg_set_gadget_attrs(usbg_gadget *g, struct gadget_attrs *g_attrs) { if(!g || !g_attrs) { return; @@ -721,55 +748,55 @@ void usbg_set_gadget_attrs(struct gadget *g, struct gadget_attrs *g_attrs) usbg_write_hex16(g->path, g->name, "bcdDevice", g_attrs->bcdDevice); } -void usbg_set_gadget_vendor_id(struct gadget *g, uint16_t idVendor) +void usbg_set_gadget_vendor_id(usbg_gadget *g, uint16_t idVendor) { g->attrs.idVendor = idVendor; usbg_write_hex16(g->path, g->name, "idVendor", idVendor); } -void usbg_set_gadget_product_id(struct gadget *g, uint16_t idProduct) +void usbg_set_gadget_product_id(usbg_gadget *g, uint16_t idProduct) { g->attrs.idProduct = idProduct; usbg_write_hex16(g->path, g->name, "idProduct", idProduct); } -void usbg_set_gadget_device_class(struct gadget *g, uint8_t bDeviceClass) +void usbg_set_gadget_device_class(usbg_gadget *g, uint8_t bDeviceClass) { g->attrs.bDeviceClass = bDeviceClass; usbg_write_hex8(g->path, g->name, "bDeviceClass", bDeviceClass); } -void usbg_set_gadget_device_protocol(struct gadget *g, uint8_t bDeviceProtocol) +void usbg_set_gadget_device_protocol(usbg_gadget *g, uint8_t bDeviceProtocol) { g->attrs.bDeviceProtocol = bDeviceProtocol; usbg_write_hex8(g->path, g->name, "bDeviceProtocol", bDeviceProtocol); } -void usbg_set_gadget_device_subclass(struct gadget *g, uint8_t bDeviceSubClass) +void usbg_set_gadget_device_subclass(usbg_gadget *g, uint8_t bDeviceSubClass) { g->attrs.bDeviceSubClass = bDeviceSubClass; usbg_write_hex8(g->path, g->name, "bDeviceSubClass", bDeviceSubClass); } -void usbg_set_gadget_device_max_packet(struct gadget *g, uint8_t bMaxPacketSize0) +void usbg_set_gadget_device_max_packet(usbg_gadget *g, uint8_t bMaxPacketSize0) { g->attrs.bMaxPacketSize0 = bMaxPacketSize0; usbg_write_hex8(g->path, g->name, "bMaxPacketSize0", bMaxPacketSize0); } -void usbg_set_gadget_device_bcd_device(struct gadget *g, uint16_t bcdDevice) +void usbg_set_gadget_device_bcd_device(usbg_gadget *g, uint16_t bcdDevice) { g->attrs.bcdDevice = bcdDevice; usbg_write_hex16(g->path, g->name, "bcdDevice", bcdDevice); } -void usbg_set_gadget_device_bcd_usb(struct gadget *g, uint16_t bcdUSB) +void usbg_set_gadget_device_bcd_usb(usbg_gadget *g, uint16_t bcdUSB) { g->attrs.bcdUSB = bcdUSB; usbg_write_hex16(g->path, g->name, "bcdUSB", bcdUSB); } -struct gadget_strs *usbg_get_gadget_strs(struct gadget *g, +struct gadget_strs *usbg_get_gadget_strs(usbg_gadget *g, struct gadget_strs *g_strs) { if (g && g_strs) { @@ -781,7 +808,7 @@ struct gadget_strs *usbg_get_gadget_strs(struct gadget *g, return g_strs; } -void usbg_set_gadget_strs(struct gadget *g, int lang, +void usbg_set_gadget_strs(usbg_gadget *g, int lang, struct gadget_strs *g_strs) { char path[USBG_MAX_PATH_LENGTH]; @@ -800,7 +827,7 @@ void usbg_set_gadget_strs(struct gadget *g, int lang, usbg_write_string(path, "", "product", g_strs->str_prd); } -void usbg_set_gadget_serial_number(struct gadget *g, int lang, char *serno) +void usbg_set_gadget_serial_number(usbg_gadget *g, int lang, char *serno) { char path[USBG_MAX_PATH_LENGTH]; @@ -816,7 +843,7 @@ void usbg_set_gadget_serial_number(struct gadget *g, int lang, char *serno) usbg_write_string(path, "", "serialnumber", serno); } -void usbg_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf) +void usbg_set_gadget_manufacturer(usbg_gadget *g, int lang, char *mnf) { char path[USBG_MAX_PATH_LENGTH]; @@ -832,7 +859,7 @@ void usbg_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf) usbg_write_string(path, "", "manufacturer", mnf); } -void usbg_set_gadget_product(struct gadget *g, int lang, char *prd) +void usbg_set_gadget_product(usbg_gadget *g, int lang, char *prd) { char path[USBG_MAX_PATH_LENGTH]; @@ -848,7 +875,7 @@ void usbg_set_gadget_product(struct gadget *g, int lang, char *prd) usbg_write_string(path, "", "product", prd); } -struct function *usbg_create_function(struct gadget *g, enum function_type type, +struct function *usbg_create_function(usbg_gadget *g, enum function_type type, char *instance, union attrs* f_attrs) { char fpath[USBG_MAX_PATH_LENGTH]; @@ -899,11 +926,11 @@ struct function *usbg_create_function(struct gadget *g, enum function_type type, return f; } -struct config *usbg_create_config(struct gadget *g, char *name, +usbg_config *usbg_create_config(usbg_gadget *g, char *name, struct config_attrs *c_attrs, struct config_strs *c_strs) { char cpath[USBG_MAX_PATH_LENGTH]; - struct config *c; + usbg_config *c; int ret; if (!g) @@ -920,7 +947,7 @@ struct config *usbg_create_config(struct gadget *g, char *name, sprintf(cpath, "%s/%s/%s/%s", g->path, g->name, CONFIGS_DIR, name); - c = malloc(sizeof(struct config)); + c = malloc(sizeof(usbg_config)); if (!c) { ERRORNO("allocating configuration\n"); return NULL; @@ -954,12 +981,12 @@ struct config *usbg_create_config(struct gadget *g, char *name, return c; } -size_t usbg_get_config_name_len(struct config *c) +size_t usbg_get_config_name_len(usbg_config *c) { return c ? strlen(c->name): -1; } -char *usbg_get_config_name(struct config* c, char *buf, size_t len) +char *usbg_get_config_name(usbg_config *c, char *buf, size_t len) { return c ? strncpy(buf, c->name, len) : NULL; } @@ -974,7 +1001,7 @@ char *usbg_get_function_name(struct function *f, char *buf, size_t len) return f ? strncpy(buf, f->name, len) : NULL; } -void usbg_set_config_attrs(struct config *c, struct config_attrs *c_attrs) +void usbg_set_config_attrs(usbg_config *c, struct config_attrs *c_attrs) { if(!c || !c_attrs) { return; @@ -986,7 +1013,7 @@ void usbg_set_config_attrs(struct config *c, struct config_attrs *c_attrs) usbg_write_hex8(c->path, c->name, "bmAttributes", c_attrs->bmAttributes); } -struct config_attrs *usbg_get_config_attrs(struct config *c, +struct config_attrs *usbg_get_config_attrs(usbg_config *c, struct config_attrs *c_attrs) { if (c && c_attrs) { @@ -998,19 +1025,19 @@ struct config_attrs *usbg_get_config_attrs(struct config *c, return c_attrs; } -void usbg_set_config_max_power(struct config *c, int bMaxPower) +void usbg_set_config_max_power(usbg_config *c, int bMaxPower) { c->attrs.bMaxPower = bMaxPower; usbg_write_dec(c->path, c->name, "MaxPower", bMaxPower); } -void usbg_set_config_bm_attrs(struct config *c, int bmAttributes) +void usbg_set_config_bm_attrs(usbg_config *c, int bmAttributes) { c->attrs.bmAttributes = bmAttributes; usbg_write_hex8(c->path, c->name, "bmAttributes", bmAttributes); } -struct config_strs *usbg_get_config_strs(struct config *c, +struct config_strs *usbg_get_config_strs(usbg_config *c, struct config_strs *c_strs) { if (c && c_strs) { @@ -1021,13 +1048,13 @@ struct config_strs *usbg_get_config_strs(struct config *c, return c_strs; } -void usbg_set_config_strs(struct config *c, int lang, +void usbg_set_config_strs(usbg_config *c, int lang, struct config_strs *c_strs) { usbg_set_config_string(c, lang, c_strs->configuration); } -void usbg_set_config_string(struct config *c, int lang, char *str) +void usbg_set_config_string(usbg_config *c, int lang, char *str) { char path[USBG_MAX_PATH_LENGTH]; @@ -1043,7 +1070,7 @@ void usbg_set_config_string(struct config *c, int lang, char *str) usbg_write_string(path, "", "configuration", str); } -int usbg_add_config_function(struct config *c, char *name, struct function *f) +int usbg_add_config_function(usbg_config *c, char *name, struct function *f) { char bpath[USBG_MAX_PATH_LENGTH]; char fpath[USBG_MAX_PATH_LENGTH]; @@ -1110,7 +1137,7 @@ int usbg_get_udcs(struct dirent ***udc_list) return scandir("/sys/class/udc", udc_list, file_select, alphasort); } -void usbg_enable_gadget(struct gadget *g, char *udc) +void usbg_enable_gadget(usbg_gadget *g, char *udc) { char gudc[USBG_MAX_STR_LENGTH]; struct dirent **udc_list; @@ -1131,7 +1158,7 @@ void usbg_enable_gadget(struct gadget *g, char *udc) usbg_write_string(g->path, g->name, "UDC", gudc); } -void usbg_disable_gadget(struct gadget *g) +void usbg_disable_gadget(usbg_gadget *g) { strcpy(g->udc, ""); usbg_write_string(g->path, g->name, "UDC", ""); @@ -1222,27 +1249,27 @@ void usbg_set_net_qmult(struct function *f, int qmult) usbg_write_dec(f->path, f->name, "qmult", qmult); } -struct gadget *usbg_get_first_gadget(usbg_state *s) +usbg_gadget *usbg_get_first_gadget(usbg_state *s) { return s ? TAILQ_FIRST(&s->gadgets) : NULL; } -struct function *usbg_get_first_function(struct gadget *g) +struct function *usbg_get_first_function(usbg_gadget *g) { return g ? TAILQ_FIRST(&g->functions) : NULL; } -struct config *usbg_get_first_config(struct gadget *g) +usbg_config *usbg_get_first_config(usbg_gadget *g) { return g ? TAILQ_FIRST(&g->configs) : NULL; } -struct binding *usbg_get_first_binding(struct config *c) +struct binding *usbg_get_first_binding(usbg_config *c) { return c ? TAILQ_FIRST(&c->bindings) : NULL; } -struct gadget *usbg_get_next_gadget(struct gadget *g) +usbg_gadget *usbg_get_next_gadget(usbg_gadget *g) { return g ? TAILQ_NEXT(g, gnode) : NULL; } @@ -1252,7 +1279,7 @@ struct function *usbg_get_next_function(struct function *f) return f ? TAILQ_NEXT(f, fnode) : NULL; } -struct config *usbg_get_next_config(struct config *c) +usbg_config *usbg_get_next_config(usbg_config *c) { return c ? TAILQ_NEXT(c, cnode) : NULL; } -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html