Libgadget is an old project hosted on github. To avoid confusion with that library, this one should have different name. Libusbg is the most suitable name. Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx> --- AUTHORS | 1 + ChangeLog | 5 + INSTALL | 2 +- Makefile.am | 6 +- README | 4 +- configure.ac | 4 +- doxygen.cfg | 2 +- examples/Makefile.am | 2 +- examples/gadget-acm-ecm.c | 42 ++-- examples/show-gadgets.c | 52 ++--- include/gadget/gadget.h | 444 -------------------------------------- include/libusbg/libusbg.h | 444 ++++++++++++++++++++++++++++++++++++++ libgadget.pc.in => libusbg.pc.in | 4 +- src/Makefile.am | 6 +- src/{gadget.c => libusbg.c} | 366 +++++++++++++++---------------- 15 files changed, 695 insertions(+), 689 deletions(-) delete mode 100644 include/gadget/gadget.h create mode 100644 include/libusbg/libusbg.h rename libgadget.pc.in => libusbg.pc.in (81%) rename src/{gadget.c => libusbg.c} (54%) diff --git a/AUTHORS b/AUTHORS index 53a1185..af8ee32 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,2 @@ Matt Porter <matt.porter@xxxxxxxxxx> +Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx> diff --git a/ChangeLog b/ChangeLog index 84868ac..14bb6ad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,2 +1,7 @@ +Fri, 10 Jan 2014 Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx> 0.0.2 +- Rename library to avoid confusion with some old project +- Change operation sequence - first alloc memory then create directory +- Fix bugs + Wed, 04 Sep 2013 Matt Porter <matt.porter@xxxxxxxxxx> 0.0.1 - Initial release diff --git a/INSTALL b/INSTALL index fef58f8..786ecee 100644 --- a/INSTALL +++ b/INSTALL @@ -1,4 +1,4 @@ -Installing libgadget: +Installing libusbg: $ autoreconf -i $ ./configure diff --git a/Makefile.am b/Makefile.am index 7290d21..9f94db1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ include $(top_srcdir)/aminclude.am SUBDIRS = src examples ACLOCAL_AMFLAGS = -I m4 EXTRA_DIST = doxygen.cfg -library_includedir=$(includedir)/gadget -library_include_HEADERS = include/gadget/gadget.h +library_includedir=$(includedir)/libusbg +library_include_HEADERS = include/libusbg/libusbg.h pkgconfigdir = $(libdir)/pkgconfig -pkgconfig_DATA = libgadget.pc +pkgconfig_DATA = libusbg.pc diff --git a/README b/README index 353cd90..6edd316 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ -libgadget +libusbg --------- -libgadget is a C library encapsulating the kernel USB gadget-configfs +libusbg is a C library encapsulating the kernel USB gadget-configfs userspace API functionality. It provides routines for creating and parsing USB gadget devices using diff --git a/configure.ac b/configure.ac index bdff17a..8d3199b 100644 --- a/configure.ac +++ b/configure.ac @@ -1,9 +1,9 @@ -AC_INIT([libgadget], [0.0.1], [matt.porter@xxxxxxxxxx]) +AC_INIT([libusbg], [0.0.2], [matt.porter@xxxxxxxxxx]) AM_INIT_AUTOMAKE([-Wall -Werror foreign]) AC_PROG_CC AM_PROG_AR AC_CONFIG_MACRO_DIR([m4]) LT_INIT -AC_CONFIG_FILES([Makefile src/Makefile examples/Makefile libgadget.pc]) +AC_CONFIG_FILES([Makefile src/Makefile examples/Makefile libusbg.pc]) DX_INIT_DOXYGEN([$PACKAGE_NAME],[doxygen.cfg]) AC_OUTPUT diff --git a/doxygen.cfg b/doxygen.cfg index c4fd608..93b3977 100644 --- a/doxygen.cfg +++ b/doxygen.cfg @@ -648,7 +648,7 @@ WARN_LOGFILE = # directories like "/usr/src/myproject". Separate the files or directories # with spaces. -INPUT = $(SRCDIR)/include/gadget/ $(SRCDIR)/src $(SRCDIR)/examples/ +INPUT = $(SRCDIR)/include/libusbg/ $(SRCDIR)/src $(SRCDIR)/examples/ # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is diff --git a/examples/Makefile.am b/examples/Makefile.am index eb98bdc..f9f9407 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -2,4 +2,4 @@ bin_PROGRAMS = show-gadgets gadget-acm-ecm gadget_acm_ecm_SOURCES = gadget-acm-ecm.c show_gadgets_SOURCES = show-gadgets.c AM_CPPFLAGS=-I../include/ -AM_LDFLAGS=-L../src/ -lgadget +AM_LDFLAGS=-L../src/ -lusbg diff --git a/examples/gadget-acm-ecm.c b/examples/gadget-acm-ecm.c index 9493613..27da182 100644 --- a/examples/gadget-acm-ecm.c +++ b/examples/gadget-acm-ecm.c @@ -16,7 +16,7 @@ #include <errno.h> #include <stdio.h> -#include <gadget/gadget.h> +#include <libusbg/libusbg.h> /** * @file gadget-acm-ecm.c @@ -29,62 +29,62 @@ int main(void) { - struct state *s; - struct gadget *g; - struct function *f; - struct config *c; - struct function *f_acm0, *f_acm1, *f_ecm; + struct libusbg_state *s; + struct libusbg_gadget *g; + struct libusbg_function *f; + struct libusbg_config *c; + struct libusbg_function *f_acm0, *f_acm1, *f_ecm; int ret = -EINVAL; - s = gadget_init("/config"); + s = libusbg_init("/config"); if (!s) { fprintf(stderr, "Error on USB gadget init\n"); goto out1; } - g = gadget_create_gadget(s, "g1", VENDOR, PRODUCT); + g = libusbg_create_gadget(s, "g1", VENDOR, PRODUCT); if (!g) { fprintf(stderr, "Error on create gadget\n"); goto out2; } - gadget_set_gadget_serial_number(g, LANG_US_ENG, "0123456789"); - gadget_set_gadget_manufacturer(g, LANG_US_ENG, "Foo Inc."); - gadget_set_gadget_product(g, LANG_US_ENG, "Bar Gadget"); + libusbg_set_gadget_serial_number(g, LIBUSBG_LANG_US_ENG, "0123456789"); + libusbg_set_gadget_manufacturer(g, LIBUSBG_LANG_US_ENG, "Foo Inc."); + libusbg_set_gadget_product(g, LIBUSBG_LANG_US_ENG, "Bar Gadget"); - f_acm0 = gadget_create_function(g, F_ACM, "usb0"); + f_acm0 = libusbg_create_function(g, LIBUSBG_F_ACM, "usb0"); if (!f_acm0) { fprintf(stderr, "Error creating acm0 function\n"); goto out2; } - f_acm1 = gadget_create_function(g, F_ACM, "usb1"); + f_acm1 = libusbg_create_function(g, LIBUSBG_F_ACM, "usb1"); if (!f_acm1) { fprintf(stderr, "Error creating acm1 function\n"); goto out2; } - f_ecm = gadget_create_function(g, F_ECM, "usb0"); + f_ecm = libusbg_create_function(g, LIBUSBG_F_ECM, "usb0"); if (!f_ecm) { fprintf(stderr, "Error creating ecm function\n"); goto out2; } - c = gadget_create_config(g, "c.1"); + c = libusbg_create_config(g, "c.1"); if (!c) { fprintf(stderr, "Error creating config\n"); goto out2; } - gadget_set_config_string(c, LANG_US_ENG, "CDC 2xACM+ECM"); - gadget_add_config_function(c, "acm.GS0", f_acm0); - gadget_add_config_function(c, "acm.GS1", f_acm1); - gadget_add_config_function(c, "ecm.usb0", f_ecm); + libusbg_set_config_string(c, LIBUSBG_LANG_US_ENG, "CDC 2xACM+ECM"); + libusbg_add_config_function(c, "acm.GS0", f_acm0); + libusbg_add_config_function(c, "acm.GS1", f_acm1); + libusbg_add_config_function(c, "ecm.usb0", f_ecm); - gadget_enable_gadget(g, DEFAULT_UDC); + libusbg_enable_gadget(g, LIBUSBG_DEFAULT_UDC); ret = 0; out2: - gadget_cleanup(s); + libusbg_cleanup(s); out1: return ret; diff --git a/examples/show-gadgets.c b/examples/show-gadgets.c index a7e20aa..c28f2dc 100644 --- a/examples/show-gadgets.c +++ b/examples/show-gadgets.c @@ -17,7 +17,7 @@ #include <errno.h> #include <stdio.h> #include <string.h> -#include <gadget/gadget.h> +#include <libusbg/libusbg.h> #include <netinet/ether.h> /** @@ -27,7 +27,7 @@ * in the system */ -void show_gadget(struct gadget *g) +void show_gadget(struct libusbg_gadget *g) { fprintf(stdout, "ID %04x:%04x '%s'\n", g->vendor, g->product, g->name); @@ -45,21 +45,21 @@ void show_gadget(struct gadget *g) fprintf(stdout, " Product\t\t%s\n", g->str_prd); } -void show_function(struct function *f) +void show_function(struct libusbg_function *f) { fprintf(stdout, " Function '%s'\n", f->name); switch (f->type) { - case F_SERIAL: - case F_ACM: - case F_OBEX: + case LIBUSBG_F_SERIAL: + case LIBUSBG_F_ACM: + case LIBUSBG_F_OBEX: fprintf(stdout, " port_num\t\t%d\n", f->attr.serial.port_num); break; - case F_ECM: - case F_SUBSET: - case F_NCM: - case F_EEM: - case F_RNDIS: + case LIBUSBG_F_ECM: + case LIBUSBG_F_SUBSET: + case LIBUSBG_F_NCM: + case LIBUSBG_F_EEM: + case LIBUSBG_F_RNDIS: fprintf(stdout, " dev_addr\t\t%s\n", ether_ntoa(&f->attr.net.dev_addr)); fprintf(stdout, " host_addr\t\t%s\n", @@ -67,7 +67,7 @@ void show_function(struct function *f) fprintf(stdout, " ifname\t\t%s\n", f->attr.net.ifname); fprintf(stdout, " qmult\t\t%d\n", f->attr.net.qmult); break; - case F_PHONET: + case LIBUSBG_F_PHONET: fprintf(stdout, " ifname\t\t%s\n", f->attr.phonet.ifname); break; default: @@ -75,42 +75,42 @@ void show_function(struct function *f) } } -void show_config(struct config *c) +void show_config(struct libusbg_config *c) { - struct binding *b; + struct libusbg_binding *b; fprintf(stdout, " Configuration '%s'\n", c->name); fprintf(stdout, " MaxPower\t\t%d\n", c->maxpower); fprintf(stdout, " bmAttributes\t0x%02x\n", c->bmattrs); fprintf(stdout, " configuration\t%s\n", c->str_cfg); - gadget_for_each_binding(b, c) + libusbg_for_each_binding(b, c) fprintf(stdout, " %s -> %s\n", b->name,b->target->name); } int main(void) { - struct state *s; - struct gadget *g; - struct function *f; - struct config *c; - struct binding *b; - struct function *f_acm0, *f_acm1, *f_ecm; + struct libusbg_state *s; + struct libusbg_gadget *g; + struct libusbg_function *f; + struct libusbg_config *c; + struct libusbg_binding *b; + struct libusbg_function *f_acm0, *f_acm1, *f_ecm; - s = gadget_init("/config"); + s = libusbg_init("/config"); if (!s) { fprintf(stderr, "Error on USB gadget init\n"); return -EINVAL; } - gadget_for_each_gadget(g, s) { + libusbg_for_each_gadget(g, s) { show_gadget(g); - gadget_for_each_function(f, g) + libusbg_for_each_function(f, g) show_function(f); - gadget_for_each_config(c, g) + libusbg_for_each_config(c, g) show_config(c); } - gadget_cleanup(s); + libusbg_cleanup(s); return 0; } diff --git a/include/gadget/gadget.h b/include/gadget/gadget.h deleted file mode 100644 index f3c08e9..0000000 --- a/include/gadget/gadget.h +++ /dev/null @@ -1,444 +0,0 @@ -/* - * Copyright (C) 2013 Linaro Limited - * - * Matt Porter <matt.porter@xxxxxxxxxx> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - */ - -#ifndef __GADGET_H__ -#define __GADGET_H__ - -#include <dirent.h> -#include <sys/queue.h> -#include <netinet/ether.h> - -/** - * @file include/gadget/gadget.h - * @todo Add gadget_remove_[gadget|config|function|binding] APIs - * @todo Clean up static buffers in structures - */ - -/** - * @addtogroup libgadget - * Public API for USB gadget-configfs library - * @{ - */ - -#define DEFAULT_UDC NULL -#define LANG_US_ENG 0x0409 - -#define MAX_LENGTH 256 -#define MAX_PATH_LENGTH 256 - -/** - * @struct state - * @brief State of the gadget devices in the system - */ -struct state -{ - char path[MAX_PATH_LENGTH]; - - TAILQ_HEAD(ghead, gadget) gadgets; -}; - -/** - * @struct gadget - * @brief USB gadget device attributes - */ -struct gadget -{ - char name[40]; - char path[MAX_PATH_LENGTH]; - char udc[MAX_LENGTH]; - int dclass; - int dsubclass; - int dproto; - int maxpacket; - int bcddevice; - int bcdusb; - int product; - int vendor; - char str_ser[MAX_LENGTH]; - char str_mnf[MAX_LENGTH]; - char str_prd[MAX_LENGTH]; - TAILQ_ENTRY(gadget) gnode; - TAILQ_HEAD(chead, config) configs; - TAILQ_HEAD(fhead, function) functions; - struct state *parent; -}; - -/** - * @struct config - * @brief USB gadget configuration attributes - */ -struct config -{ - TAILQ_ENTRY(config) cnode; - TAILQ_HEAD(bhead, binding) bindings; - struct gadget *parent; - - char name[40]; - char path[MAX_PATH_LENGTH]; - int maxpower; - int bmattrs; - char str_cfg[MAX_LENGTH]; -}; - -/** - * @enum function_type - * @brief Supported USB function types - */ -enum function_type -{ - F_SERIAL, - F_ACM, - F_OBEX, - F_ECM, - F_SUBSET, - F_NCM, - F_EEM, - F_RNDIS, - F_PHONET, -}; - -/** - * @var function_names - * @brief Name strings for supported USB function types - */ -const char *function_names[] = -{ - "gser", - "acm", - "obex", - "ecm", - "geth", - "ncm", - "eem", - "rndis", - "phonet", -}; - -/** - * @struct serial_attrs - * @brief Attributes for Serial, ACM, and OBEX USB functions - */ -struct serial_attrs { - int port_num; -}; - -/** - * @struct net_attrs - * @brief Attributes for ECM, ECM subset, NCM, EEM, and RNDIS USB functions - */ -struct net_attrs { - struct ether_addr dev_addr; - struct ether_addr host_addr; - char ifname[MAX_LENGTH]; - int qmult; -}; - -/** - * @struct phonet_attrs - * @brief Attributes for the phonet USB function - */ -struct phonet_attrs { - char ifname[MAX_LENGTH]; -}; - -/** - * @union attrs - * @brief Attributes for a given function type - */ -union attrs { - struct serial_attrs serial; - struct net_attrs net; - struct phonet_attrs phonet; -}; - -/** - * @struct function - * @brief USB gadget function attributes - */ -struct function -{ - TAILQ_ENTRY(function) fnode; - struct gadget *parent; - - char name[40]; - char path[MAX_PATH_LENGTH]; - - enum function_type type; - union attrs attr; -}; - - -/** - * @struct binding - * @brief Describes a binding between a USB gadget configuration - * and a USB gadget function - */ -struct binding -{ - TAILQ_ENTRY(binding) bnode; - struct config *parent; - struct function *target; - - char name[40]; - char path[MAX_PATH_LENGTH]; -}; - -/* Library init and cleanup */ - -/** - * @brief Initialize the libgadget library state - * @param configfs_path Path to the mounted configfs filesystem - * @return Pointer to a state structure - */ -extern struct state *gadget_init(char *configfs_path); - -/** - * @brief Clean up the libgadget library state - * @param s Pointer to state - */ -extern void gadget_cleanup(struct state *s); - -/* USB gadget queries */ - -/** - * @brief Get a gadget device by name - * @param s Pointer to state - * @param name Name of the gadget device - * @return Pointer to gadget or NULL if a matching gadget isn't found - */ -extern struct gadget *gadget_get_gadget(struct state *s, const char *name); - -/** - * @brief Get a function by name - * @param g Pointer to gadget - * @param name Name of the function - * @return Pointer to function or NULL if a matching function isn't found - */ -extern struct function *gadget_get_function(struct gadget *g, const char *name); - -/** - * @brief Get a configuration by name - * @param g Pointer to gadget - * @param name Name of the configuration - * @return Pointer to config or NULL if a matching config isn't found - */ -extern struct config *gadget_get_config(struct gadget *g, const char *name); - -/* USB gadget allocation and configuration */ - -/** - * @brief Create a new USB gadget device - * @param s Pointer to state - * @param name Name of the gadget - * @param vendor Gadget vendor ID - * @param product Gadget product ID - * @return Pointer to gadget or NULL if the gadget cannot be created - */ -extern struct gadget *gadget_create_gadget(struct state *s, char *name, int vendor, int product); - -/** - * @brief Set the USB gadget device class code - * @param g Pointer to gadget - * @param dclass USB device class code - */ -extern void gadget_set_gadget_device_class(struct gadget *g, int dclass); - -/** - * @brief Set the USB gadget protocol code - * @param g Pointer to gadget - * @param dprotocol USB protocol code - */ -extern void gadget_set_gadget_device_protocol(struct gadget *g, int dproto); - -/** - * @brief Set the USB gadget device subclass code - * @param g Pointer to gadget - * @param dsubclass USB device subclass code - */ -extern void gadget_set_gadget_device_subclass(struct gadget *g, int dsubclass); - -/** - * @brief Set the maximum packet size for a gadget - * @param g Pointer to gadget - * @param maxpacket Maximum packet size - */ -extern void gadget_set_gadget_device_max_packet(struct gadget *g, int maxpacket); - -/** - * @brief Set the gadget device BCD release number - * @param g Pointer to gadget - * @param bcddevice BCD release number - */ -extern void gadget_set_gadget_device_bcd_device(struct gadget *g, int bcddevice); - -/** - * @brief Set the gadget device BCD USB version - * @param g Pointer to gadget - * @param bcdusb BCD USB version - */ -extern void gadget_set_gadget_device_bcd_usb(struct gadget *g, int bcdusb); - -/** - * @brief Set the serial number for a gadget - * @param g Pointer to gadget - * @param lang USB language ID - * @param ser Serial number - */ -extern void gadget_set_gadget_serial_number(struct gadget *g, int lang, char *ser); - -/** - * @brief Set the manufacturer name for a gadget - * @param g Pointer to gadget - * @param lang USB language ID - * @param mnf Manufacturer - */ -extern void gadget_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf); - -/** - * @brief Set the product name for a gadget - * @param g Pointer to gadget - * @param lang USB language ID - * @param prd Product - */ -extern void gadget_set_gadget_product(struct gadget *g, int lang, char *prd); - -/* USB function allocation and configuration */ - -/** - * @brief Create a new USB gadget function - * @param g Pointer to gadget - * @param type Type of function - * @param instance Function instance name - * @return Pointer to function or NULL if it cannot be created - */ -extern struct function *gadget_create_function(struct gadget *g, enum function_type type, char *instance); - -/* USB configurations allocation and configuration */ - -/** - * @brief Create a new USB gadget configuration - * @param g Pointer to gadget - * @param name Name of configuration - * @return Pointer to configuration or NULL if it cannot be created - */ -extern struct config *gadget_create_config(struct gadget *g, char *name); - -/** - * @brief Set the configuration maximum power - * @param c Pointer to config - * @param maxpower Maximum power (in 2 mA units) - */ -extern void gadget_set_config_max_power(struct config *c, int maxpower); - -/** - * @brief Set the configuration bitmap attributes - * @param c Pointer to config - * @param bmattrs Configuration characteristics - */ -extern void gadget_set_config_bm_attrs(struct config *c, int bmattrs); - -/** - * @brief Set the configuration string - * @param c Pointer to config - * @param lang USB language ID - * @param string Configuration description - */ -extern void gadget_set_config_string(struct config *c, int lang, char *string); - -/** - * @brief Add a function to a configuration - * @param c Pointer to config - * @param name Name of configuration function binding - * @param f Pointer to function - * @return 0 on success, -1 on failure. - */ -extern int gadget_add_config_function(struct config *c, char *name, struct function *f); - -/* USB gadget setup and teardown */ - -/** - * @brief Get a list of UDC devices on the system - * @param udc_list Pointer to pointer to dirent pointer - * @return Number of UDC devices on success, -1 on failure - */ -extern int gadget_get_udcs(struct dirent ***udc_list); - -/** - * @brief Enable a USB gadget device - * @param g Pointer to gadget - * @param udc Name of UDC to enable gadget - */ -extern void gadget_enable_gadget(struct gadget *g, char *udc); - -/** - * @brief Disable a USB gadget device - * @param g Pointer to gadget - */ -extern void gadget_disable_gadget(struct gadget *g); - -/* - * USB function-specific attribute configuration - */ - -/** - * @brief Set USB function network device address - * @param f Pointer to function - * @param addr Pointer to Ethernet address - */ -extern void gadget_set_net_dev_addr(struct function *f, struct ether_addr *addr); - -/** - * @brief Set USB function network host address - * @param f Pointer to function - * @param addr Pointer to Ethernet address - */ -extern void gadget_set_net_host_addr(struct function *f, struct ether_addr *addr); - -/** - * @brief Set USB function network qmult - * @param f Pointer to function - * @param qmult Queue length multiplier - */ -extern void gadget_set_net_qmult(struct function *f, int qmult); - -/** - * @def gadget_for_each_gadget(g, s) - * Iterates over each gadget - */ -#define gadget_for_each_gadget(g, s) TAILQ_FOREACH(g, &s->gadgets, gnode) - -/** - * @def gadget_for_each_function(f, g) - * Iterates over each function - */ -#define gadget_for_each_function(f, g) TAILQ_FOREACH(f, &g->functions, fnode) - -/** - * @def gadget_for_each_config(c, g) - * Iterates over each config - */ -#define gadget_for_each_config(c, g) TAILQ_FOREACH(c, &g->configs, cnode) - -/** - * @def gadget_for_each_binding(b, c) - * Iterates over each binding - */ -#define gadget_for_each_binding(b, c) TAILQ_FOREACH(b, &c->bindings, bnode) - -/** - * @} - */ -#endif //__GADGET_H__ diff --git a/include/libusbg/libusbg.h b/include/libusbg/libusbg.h new file mode 100644 index 0000000..a49ec03 --- /dev/null +++ b/include/libusbg/libusbg.h @@ -0,0 +1,444 @@ +/* + * Copyright (C) 2013 Linaro Limited + * + * Matt Porter <matt.porter@xxxxxxxxxx> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#ifndef __LIBUSBG_H__ +#define __LIBUSBG_H__ + +#include <dirent.h> +#include <sys/queue.h> +#include <netinet/ether.h> + +/** + * @file include/libusbg/libusbg.h + * @todo Add libusbg_remove_[gadget|config|function|binding] APIs + * @todo Clean up static buffers in structures + */ + +/** + * @addtogroup libusbg + * Public API for USB gadget-configfs library + * @{ + */ + +#define LIBUSBG_DEFAULT_UDC NULL +#define LIBUSBG_LANG_US_ENG 0x0409 + +#define LIBUSBG_MAX_LENGTH 256 +#define LIBUSBG_MAX_PATH_LENGTH 256 + +/** + * @struct libusbg_state + * @brief State of the gadget devices in the system + */ +struct libusbg_state +{ + char path[LIBUSBG_MAX_PATH_LENGTH]; + + TAILQ_HEAD(ghead, libusbg_gadget) gadgets; +}; + +/** + * @struct libusbg_gadget + * @brief USB gadget device attributes + */ +struct libusbg_gadget +{ + char name[40]; + char path[LIBUSBG_MAX_PATH_LENGTH]; + char udc[LIBUSBG_MAX_LENGTH]; + int dclass; + int dsubclass; + int dproto; + int maxpacket; + int bcddevice; + int bcdusb; + int product; + int vendor; + char str_ser[LIBUSBG_MAX_LENGTH]; + char str_mnf[LIBUSBG_MAX_LENGTH]; + char str_prd[LIBUSBG_MAX_LENGTH]; + TAILQ_ENTRY(libusbg_gadget) gnode; + TAILQ_HEAD(chead, libusbg_config) configs; + TAILQ_HEAD(fhead, libusbg_function) functions; + struct libusbg_state *parent; +}; + +/** + * @struct libusbg_config + * @brief USB gadget configuration attributes + */ +struct libusbg_config +{ + TAILQ_ENTRY(libusbg_config) cnode; + TAILQ_HEAD(bhead, libusbg_binding) bindings; + struct libusbg_gadget *parent; + + char name[40]; + char path[LIBUSBG_MAX_PATH_LENGTH]; + int maxpower; + int bmattrs; + char str_cfg[LIBUSBG_MAX_LENGTH]; +}; + +/** + * @enum libusbg_function_type + * @brief Supported USB function types + */ +enum libusbg_function_type +{ + LIBUSBG_F_SERIAL, + LIBUSBG_F_ACM, + LIBUSBG_F_OBEX, + LIBUSBG_F_ECM, + LIBUSBG_F_SUBSET, + LIBUSBG_F_NCM, + LIBUSBG_F_EEM, + LIBUSBG_F_RNDIS, + LIBUSBG_F_PHONET, +}; + +/** + * @var libusbg_function_names + * @brief Name strings for supported USB function types + */ +const char *libusbg_function_names[] = +{ + "gser", + "acm", + "obex", + "ecm", + "geth", + "ncm", + "eem", + "rndis", + "phonet", +}; + +/** + * @struct libusbg_serial_attrs + * @brief Attributes for Serial, ACM, and OBEX USB functions + */ +struct libusbg_serial_attrs { + int port_num; +}; + +/** + * @struct libusbg_net_attrs + * @brief Attributes for ECM, ECM subset, NCM, EEM, and RNDIS USB functions + */ +struct libusbg_net_attrs { + struct ether_addr dev_addr; + struct ether_addr host_addr; + char ifname[LIBUSBG_MAX_LENGTH]; + int qmult; +}; + +/** + * @struct libusbg_phonet_attrs + * @brief Attributes for the phonet USB function + */ +struct libusbg_phonet_attrs { + char ifname[LIBUSBG_MAX_LENGTH]; +}; + +/** + * @union libusbg_attrs + * @brief Attributes for a given function type + */ +union libusbg_attrs { + struct libusbg_serial_attrs serial; + struct libusbg_net_attrs net; + struct libusbg_phonet_attrs phonet; +}; + +/** + * @struct libusbg_function + * @brief USB gadget function attributes + */ +struct libusbg_function +{ + TAILQ_ENTRY(libusbg_function) fnode; + struct libusbg_gadget *parent; + + char name[40]; + char path[LIBUSBG_MAX_PATH_LENGTH]; + + enum libusbg_function_type type; + union libusbg_attrs attr; +}; + + +/** + * @struct libusbg_binding + * @brief Describes a binding between a USB gadget configuration + * and a USB gadget function + */ +struct libusbg_binding +{ + TAILQ_ENTRY(libusbg_binding) bnode; + struct libusbg_config *parent; + struct libusbg_function *target; + + char name[40]; + char path[LIBUSBG_MAX_PATH_LENGTH]; +}; + +/* Library init and cleanup */ + +/** + * @brief Initialize the libusbg library state + * @param configfs_path Path to the mounted configfs filesystem + * @return Pointer to a libusbg_state structure + */ +extern struct libusbg_state *libusbg_init(char *configfs_path); + +/** + * @brief Clean up the libusbg library state + * @param s Pointer to libusbg_state + */ +extern void libusbg_cleanup(struct libusbg_state *s); + +/* USB gadget queries */ + +/** + * @brief Get a gadget device by name + * @param s Pointer to libusbg_state + * @param name Name of the gadget device + * @return Pointer to libusbg_gadget or NULL if a matching gadget isn't found + */ +extern struct libusbg_gadget *libusbg_get_gadget(struct libusbg_state *s, const char *name); + +/** + * @brief Get a function by name + * @param g Pointer to libusbg_gadget + * @param name Name of the function + * @return Pointer to libusbg_function or NULL if a matching function isn't found + */ +extern struct libusbg_function *libusbg_get_function(struct libusbg_gadget *g, const char *name); + +/** + * @brief Get a configuration by name + * @param g Pointer to libusbg_gadget + * @param name Name of the configuration + * @return Pointer to libusbg_config or NULL if a matching config isn't found + */ +extern struct libusbg_config *libusbg_get_config(struct libusbg_gadget *g, const char *name); + +/* USB gadget allocation and configuration */ + +/** + * @brief Create a new USB gadget device + * @param s Pointer to state + * @param name Name of the libusbg_gadget + * @param vendor Gadget vendor ID + * @param product Gadget product ID + * @return Pointer to libusbg_gadget or NULL if the gadget cannot be created + */ +extern struct libusbg_gadget *libusbg_create_gadget(struct libusbg_state *s, char *name, int vendor, int product); + +/** + * @brief Set the USB gadget device class code + * @param g Pointer to libusbg_gadget + * @param dclass USB device class code + */ +extern void libusbg_set_gadget_device_class(struct libusbg_gadget *g, int dclass); + +/** + * @brief Set the USB gadget protocol code + * @param g Pointer to libusbg_gadget + * @param dprotocol USB protocol code + */ +extern void libusbg_set_gadget_device_protocol(struct libusbg_gadget *g, int dproto); + +/** + * @brief Set the USB gadget device subclass code + * @param g Pointer to libusbg_gadget + * @param dsubclass USB device subclass code + */ +extern void libusbg_set_gadget_device_subclass(struct libusbg_gadget *g, int dsubclass); + +/** + * @brief Set the maximum packet size for a gadget + * @param g Pointer to libusbg_gadget + * @param maxpacket Maximum packet size + */ +extern void libusbg_set_gadget_device_max_packet(struct libusbg_gadget *g, int maxpacket); + +/** + * @brief Set the gadget device BCD release number + * @param g Pointer to libusbg_gadget + * @param bcddevice BCD release number + */ +extern void libusbg_set_gadget_device_bcd_device(struct libusbg_gadget *g, int bcddevice); + +/** + * @brief Set the gadget device BCD USB version + * @param g Pointer to libusbg_gadget + * @param bcdusb BCD USB version + */ +extern void libusbg_set_gadget_device_bcd_usb(struct libusbg_gadget *g, int bcdusb); + +/** + * @brief Set the serial number for a gadget + * @param g Pointer to libusbg_gadget + * @param lang USB language ID + * @param ser Serial number + */ +extern void libusbg_set_gadget_serial_number(struct libusbg_gadget *g, int lang, char *ser); + +/** + * @brief Set the manufacturer name for a gadget + * @param g Pointer to libusbg_gadget + * @param lang USB language ID + * @param mnf Manufacturer + */ +extern void libusbg_set_gadget_manufacturer(struct libusbg_gadget *g, int lang, char *mnf); + +/** + * @brief Set the product name for a gadget + * @param g Pointer to libusbg_gadget + * @param lang USB language ID + * @param prd Product + */ +extern void libusbg_set_gadget_product(struct libusbg_gadget *g, int lang, char *prd); + +/* USB function allocation and configuration */ + +/** + * @brief Create a new USB gadget function + * @param g Pointer to libusbg_gadget + * @param type Type of function + * @param instance Function instance name + * @return Pointer to libusbg_function or NULL if it cannot be created + */ +extern struct libusbg_function *libusbg_create_function(struct libusbg_gadget *g, enum libusbg_function_type type, char *instance); + +/* USB configurations allocation and configuration */ + +/** + * @brief Create a new USB gadget configuration + * @param g Pointer to libusbg_gadget + * @param name Name of configuration + * @return Pointer to libusbg_configuration or NULL if it cannot be created + */ +extern struct libusbg_config *libusbg_create_config(struct libusbg_gadget *g, char *name); + +/** + * @brief Set the configuration maximum power + * @param c Pointer to libusbg_config + * @param maxpower Maximum power (in 2 mA units) + */ +extern void libusbg_set_config_max_power(struct libusbg_config *c, int maxpower); + +/** + * @brief Set the configuration bitmap attributes + * @param c Pointer to libusbg_config + * @param bmattrs Configuration characteristics + */ +extern void libusbg_set_config_bm_attrs(struct libusbg_config *c, int bmattrs); + +/** + * @brief Set the configuration string + * @param c Pointer to libusbg_config + * @param lang USB language ID + * @param string Configuration description + */ +extern void libusbg_set_config_string(struct libusbg_config *c, int lang, char *string); + +/** + * @brief Add a function to a configuration + * @param c Pointer to libusbg_config + * @param name Name of configuration function binding + * @param f Pointer to libusbg_function + * @return 0 on success, -1 on failure. + */ +extern int libusbg_add_config_function(struct libusbg_config *c, char *name, struct libusbg_function *f); + +/* USB gadget setup and teardown */ + +/** + * @brief Get a list of UDC devices on the system + * @param udc_list Pointer to pointer to dirent pointer + * @return Number of UDC devices on success, -1 on failure + */ +extern int libusbg_get_udcs(struct dirent ***udc_list); + +/** + * @brief Enable a USB gadget device + * @param g Pointer to libusbg_gadget + * @param udc Name of UDC to enable gadget + */ +extern void libusbg_enable_gadget(struct libusbg_gadget *g, char *udc); + +/** + * @brief Disable a USB gadget device + * @param g Pointer to libusbg_gadget + */ +extern void libusbg_disable_gadget(struct libusbg_gadget *g); + +/* + * USB function-specific attribute configuration + */ + +/** + * @brief Set USB function network device address + * @param f Pointer to libusbg_function + * @param addr Pointer to Ethernet address + */ +extern void libusbg_set_net_dev_addr(struct libusbg_function *f, struct ether_addr *addr); + +/** + * @brief Set USB function network host address + * @param f Pointer to libusbg_function + * @param addr Pointer to Ethernet address + */ +extern void libusbg_set_net_host_addr(struct libusbg_function *f, struct ether_addr *addr); + +/** + * @brief Set USB function network qmult + * @param f Pointer to libusbg_function + * @param qmult Queue length multiplier + */ +extern void libusbg_set_net_qmult(struct libusbg_function *f, int qmult); + +/** + * @def libusbg_for_each_gadget(g, s) + * Iterates over each gadget + */ +#define libusbg_for_each_gadget(g, s) TAILQ_FOREACH(g, &s->gadgets, gnode) + +/** + * @def libusbg_for_each_function(f, g) + * Iterates over each function + */ +#define libusbg_for_each_function(f, g) TAILQ_FOREACH(f, &g->functions, fnode) + +/** + * @def libusbg_for_each_config(c, g) + * Iterates over each config + */ +#define libusbg_for_each_config(c, g) TAILQ_FOREACH(c, &g->configs, cnode) + +/** + * @def libusbg_for_each_binding(b, c) + * Iterates over each binding + */ +#define libusbg_for_each_binding(b, c) TAILQ_FOREACH(b, &c->bindings, bnode) + +/** + * @} + */ +#endif //__LIBUSBG_H__ diff --git a/libgadget.pc.in b/libusbg.pc.in similarity index 81% rename from libgadget.pc.in rename to libusbg.pc.in index 0c980ed..46eb245 100644 --- a/libgadget.pc.in +++ b/libusbg.pc.in @@ -3,9 +3,9 @@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ -Name: libgadget +Name: libusbg Description: USB gadget-configfs library Requires: Version: @PACKAGE_VERSION@ -Libs: -L${libdir} -lgadget +Libs: -L${libdir} -lusbg Cflags: -I${includedir} diff --git a/src/Makefile.am b/src/Makefile.am index 2155360..883a326 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -lib_LTLIBRARIES = libgadget.la -libgadget_la_SOURCES = gadget.c -libgadget_la_LDFLAGS = -version-info 0:1:0 +lib_LTLIBRARIES = libusbg.la +libusbg_la_SOURCES = libusbg.c +libusbg_la_LDFLAGS = -version-info 0:1:0 AM_CPPFLAGS=-I../include/ diff --git a/src/gadget.c b/src/libusbg.c similarity index 54% rename from src/gadget.c rename to src/libusbg.c index 9447bde..98a95e3 100644 --- a/src/gadget.c +++ b/src/libusbg.c @@ -16,7 +16,7 @@ #include <dirent.h> #include <errno.h> -#include <gadget/gadget.h> +#include <libusbg/libusbg.h> #include <netinet/ether.h> #include <stdio.h> #include <stdlib.h> @@ -31,7 +31,7 @@ #define FUNCTIONS_DIR "functions" /** - * @file gadget.c + * @file libusbg.c * @todo Handle buffer overflows * @todo Error checking and return code propagation */ @@ -48,16 +48,16 @@ __func__, strerror(errno), ##__VA_ARGS__);\ } while (0) -static int gadget_lookup_function_type(char *name) +static int libusbg_lookup_function_type(char *name) { int i = 0; - int max = sizeof(function_names)/sizeof(char *); + int max = sizeof(libusbg_function_names)/sizeof(char *); if (!name) return -1; do { - if (!strcmp(name, function_names[i])) + if (!strcmp(name, libusbg_function_names[i])) break; i++; } while (i != max); @@ -84,9 +84,9 @@ static int file_select(const struct dirent *dent) return 1; } -static char *gadget_read_buf(char *path, char *name, char *file, char *buf) +static char *libusbg_read_buf(char *path, char *name, char *file, char *buf) { - char p[MAX_LENGTH]; + char p[LIBUSBG_MAX_LENGTH]; FILE *fp; char *ret = NULL; @@ -96,7 +96,7 @@ static char *gadget_read_buf(char *path, char *name, char *file, char *buf) if (!fp) goto out; - ret = fgets(buf, MAX_LENGTH, fp); + ret = fgets(buf, LIBUSBG_MAX_LENGTH, fp); if (ret == NULL) { ERROR("read error"); fclose(fp); @@ -109,25 +109,25 @@ out: return ret; } -static int gadget_read_int(char *path, char *name, char *file, int base) +static int libusbg_read_int(char *path, char *name, char *file, int base) { - char buf[MAX_LENGTH]; + char buf[LIBUSBG_MAX_LENGTH]; - if (gadget_read_buf(path, name, file, buf)) + if (libusbg_read_buf(path, name, file, buf)) return strtol(buf, NULL, base); else return 0; } -#define gadget_read_dec(p, n, f) gadget_read_int(p, n, f, 10) -#define gadget_read_hex(p, n, f) gadget_read_int(p, n, f, 16) +#define libusbg_read_dec(p, n, f) libusbg_read_int(p, n, f, 10) +#define libusbg_read_hex(p, n, f) libusbg_read_int(p, n, f, 16) -static void gadget_read_string(char *path, char *name, char *file, char *buf) +static void libusbg_read_string(char *path, char *name, char *file, char *buf) { char *p = NULL; - p = gadget_read_buf(path, name, file, buf); + p = libusbg_read_buf(path, name, file, buf); /* Check whether read was successful */ if (p != NULL) { if ((p = strchr(buf, '\n')) != NULL) @@ -139,9 +139,9 @@ static void gadget_read_string(char *path, char *name, char *file, char *buf) } -static void gadget_write_buf(char *path, char *name, char *file, char *buf) +static void libusbg_write_buf(char *path, char *name, char *file, char *buf) { - char p[MAX_LENGTH]; + char p[LIBUSBG_MAX_LENGTH]; FILE *fp; sprintf(p, "%s/%s/%s", path, name, file); @@ -161,66 +161,66 @@ static void gadget_write_buf(char *path, char *name, char *file, char *buf) fclose(fp); } -static void gadget_write_int(char *path, char *name, char *file, int value, char *str) +static void libusbg_write_int(char *path, char *name, char *file, int value, char *str) { - char buf[MAX_LENGTH]; + char buf[LIBUSBG_MAX_LENGTH]; sprintf(buf, str, value); - gadget_write_buf(path, name, file, buf); + libusbg_write_buf(path, name, file, buf); } -#define gadget_write_dec(p, n, f, v) gadget_write_int(p, n, f, v, "%d\n") -#define gadget_write_hex16(p, n, f, v) gadget_write_int(p, n, f, v, "0x%04x\n") -#define gadget_write_hex8(p, n, f, v) gadget_write_int(p, n, f, v, "0x%02x\n") +#define libusbg_write_dec(p, n, f, v) libusbg_write_int(p, n, f, v, "%d\n") +#define libusbg_write_hex16(p, n, f, v) libusbg_write_int(p, n, f, v, "0x%04x\n") +#define libusbg_write_hex8(p, n, f, v) libusbg_write_int(p, n, f, v, "0x%02x\n") -static inline void gadget_write_string(char *path, char *name, char *file, char *buf) +static inline void libusbg_write_string(char *path, char *name, char *file, char *buf) { - gadget_write_buf(path, name, file, buf); + libusbg_write_buf(path, name, file, buf); } -static void gadget_parse_function_attrs(struct function *f) +static void libusbg_parse_function_attrs(struct libusbg_function *f) { struct ether_addr *addr; char str_addr[40]; switch (f->type) { - case F_SERIAL: - case F_ACM: - case F_OBEX: - f->attr.serial.port_num = gadget_read_dec(f->path, f->name, "port_num"); + case LIBUSBG_F_SERIAL: + case LIBUSBG_F_ACM: + case LIBUSBG_F_OBEX: + f->attr.serial.port_num = libusbg_read_dec(f->path, f->name, "port_num"); break; - case F_ECM: - case F_SUBSET: - case F_NCM: - case F_EEM: - case F_RNDIS: - gadget_read_string(f->path, f->name, "dev_addr", str_addr); + case LIBUSBG_F_ECM: + case LIBUSBG_F_SUBSET: + case LIBUSBG_F_NCM: + case LIBUSBG_F_EEM: + case LIBUSBG_F_RNDIS: + libusbg_read_string(f->path, f->name, "dev_addr", str_addr); addr = ether_aton(str_addr); if (addr) f->attr.net.dev_addr = *addr; - gadget_read_string(f->path, f->name, "host_addr", str_addr); + libusbg_read_string(f->path, f->name, "host_addr", str_addr); addr = ether_aton(str_addr); if(addr) f->attr.net.host_addr = *addr; - gadget_read_string(f->path, f->name, "ifname", f->attr.net.ifname); - f->attr.net.qmult = gadget_read_dec(f->path, f->name, "qmult"); + libusbg_read_string(f->path, f->name, "ifname", f->attr.net.ifname); + f->attr.net.qmult = libusbg_read_dec(f->path, f->name, "qmult"); break; - case F_PHONET: - gadget_read_string(f->path, f->name, "ifname", f->attr.phonet.ifname); + case LIBUSBG_F_PHONET: + libusbg_read_string(f->path, f->name, "ifname", f->attr.phonet.ifname); break; default: ERROR("Unsupported function type\n"); } } -static int gadget_parse_functions(char *path, struct gadget *g) +static int libusbg_parse_functions(char *path, struct libusbg_gadget *g) { - struct function *f; + struct libusbg_function *f; int i, n; struct dirent **dent; - char fpath[MAX_PATH_LENGTH]; + char fpath[LIBUSBG_MAX_PATH_LENGTH]; sprintf(fpath, "%s/%s/%s", path, g->name, FUNCTIONS_DIR); @@ -228,12 +228,12 @@ static int gadget_parse_functions(char *path, struct gadget *g) n = scandir(fpath, &dent, file_select, alphasort); for (i=0; i < n; i++) { - f = malloc(sizeof(struct function)); + f = malloc(sizeof(struct libusbg_function)); f->parent = g; strcpy(f->name, dent[i]->d_name); strcpy(f->path, fpath); - f->type = gadget_lookup_function_type(strtok(dent[i]->d_name, ".")); - gadget_parse_function_attrs(f); + f->type = libusbg_lookup_function_type(strtok(dent[i]->d_name, ".")); + libusbg_parse_function_attrs(f); TAILQ_INSERT_TAIL(&g->functions, f, fnode); free(dent[i]); } @@ -242,21 +242,21 @@ static int gadget_parse_functions(char *path, struct gadget *g) return 0; } -static void gadget_parse_config_attrs(struct config *c) +static void libusbg_parse_config_attrs(struct libusbg_config *c) { - c->maxpower = gadget_read_dec(c->path, c->name, "MaxPower"); - c->bmattrs = gadget_read_hex(c->path, c->name, "bmAttributes"); - gadget_read_string(c->path, c->name, "strings/0x409/configuration", c->str_cfg); + c->maxpower = libusbg_read_dec(c->path, c->name, "MaxPower"); + c->bmattrs = libusbg_read_hex(c->path, c->name, "bmAttributes"); + libusbg_read_string(c->path, c->name, "strings/0x409/configuration", c->str_cfg); } -static void gadget_parse_config_bindings(struct config *c) +static void libusbg_parse_config_bindings(struct libusbg_config *c) { int i, n; struct dirent **dent; - char bpath[MAX_PATH_LENGTH]; - struct gadget *g = c->parent; - struct binding *b; - struct function *f; + char bpath[LIBUSBG_MAX_PATH_LENGTH]; + struct libusbg_gadget *g = c->parent; + struct libusbg_binding *b; + struct libusbg_function *f; sprintf(bpath, "%s/%s", c->path, c->name); @@ -266,17 +266,17 @@ static void gadget_parse_config_bindings(struct config *c) for (i=0; i < n; i++) { TAILQ_FOREACH(f, &g->functions, fnode) { int n; - char contents[MAX_LENGTH]; - char cpath[MAX_PATH_LENGTH]; + char contents[LIBUSBG_MAX_LENGTH]; + char cpath[LIBUSBG_MAX_PATH_LENGTH]; char fname[40]; sprintf(cpath, "%s/%s", bpath, dent[i]->d_name); - n = readlink(cpath, contents, MAX_PATH_LENGTH); + n = readlink(cpath, contents, LIBUSBG_MAX_PATH_LENGTH); if (n<0) ERRORNO("bytes %d contents %s\n", n, contents); strcpy(fname, f->name); if (strstr(contents, strtok(fname, "."))) { - b = malloc(sizeof(struct binding)); + b = malloc(sizeof(struct libusbg_binding)); strcpy(b->name, dent[i]->d_name); strcpy(b->path, bpath); b->target = f; @@ -289,12 +289,12 @@ static void gadget_parse_config_bindings(struct config *c) free(dent); } -static int gadget_parse_configs(char *path, struct gadget *g) +static int libusbg_parse_configs(char *path, struct libusbg_gadget *g) { - struct config *c; + struct libusbg_config *c; int i, n; struct dirent **dent; - char cpath[MAX_PATH_LENGTH]; + char cpath[LIBUSBG_MAX_PATH_LENGTH]; sprintf(cpath, "%s/%s/%s", path, g->name, CONFIGS_DIR); @@ -302,12 +302,12 @@ static int gadget_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(struct libusbg_config)); c->parent = g; strcpy(c->name, dent[i]->d_name); strcpy(c->path, cpath); - gadget_parse_config_attrs(c); - gadget_parse_config_bindings(c); + libusbg_parse_config_attrs(c); + libusbg_parse_config_bindings(c); TAILQ_INSERT_TAIL(&g->configs, c, cnode); free(dent[i]); } @@ -316,35 +316,35 @@ static int gadget_parse_configs(char *path, struct gadget *g) return 0; } -static void gadget_parse_attrs(char *path, struct gadget *g) +static void libusbg_parse_attrs(char *path, struct libusbg_gadget *g) { /* Actual attributes */ - g->dclass = gadget_read_hex(path, g->name, "bDeviceClass"); - g->dsubclass = gadget_read_hex(path, g->name, "bDeviceSubClass"); - g->dproto = gadget_read_hex(path, g->name, "bDeviceProtocol"); - g->maxpacket = gadget_read_hex(path, g->name, "bMaxPacketSize0"); - g->bcddevice = gadget_read_hex(path, g->name, "bcdDevice"); - g->bcdusb = gadget_read_hex(path, g->name, "bcdUSB"); - g->vendor = gadget_read_hex(path, g->name, "idVendor"); - g->product = gadget_read_hex(path, g->name, "idProduct"); + g->dclass = libusbg_read_hex(path, g->name, "bDeviceClass"); + g->dsubclass = libusbg_read_hex(path, g->name, "bDeviceSubClass"); + g->dproto = libusbg_read_hex(path, g->name, "bDeviceProtocol"); + g->maxpacket = libusbg_read_hex(path, g->name, "bMaxPacketSize0"); + g->bcddevice = libusbg_read_hex(path, g->name, "bcdDevice"); + g->bcdusb = libusbg_read_hex(path, g->name, "bcdUSB"); + g->vendor = libusbg_read_hex(path, g->name, "idVendor"); + g->product = libusbg_read_hex(path, g->name, "idProduct"); } -static void gadget_parse_strings(char *path, struct gadget *g) +static void libusbg_parse_strings(char *path, struct libusbg_gadget *g) { /* Strings - hardcoded to U.S. English only for now */ - int lang = LANG_US_ENG; - char spath[MAX_PATH_LENGTH]; + int lang = LIBUSBG_LANG_US_ENG; + char spath[LIBUSBG_MAX_PATH_LENGTH]; sprintf(spath, "%s/%s/%s/0x%x", path, g->name, STRINGS_DIR, lang); - gadget_read_string(spath, "", "serialnumber", g->str_ser); - gadget_read_string(spath, "", "manufacturer", g->str_mnf); - gadget_read_string(spath, "", "product", g->str_prd); + libusbg_read_string(spath, "", "serialnumber", g->str_ser); + libusbg_read_string(spath, "", "manufacturer", g->str_mnf); + libusbg_read_string(spath, "", "product", g->str_prd); } -static int gadget_parse_gadgets(char *path, struct state *s) +static int libusbg_parse_gadgets(char *path, struct libusbg_state *s) { - struct gadget *g; + struct libusbg_gadget *g; int i, n; struct dirent **dent; @@ -352,16 +352,16 @@ static int gadget_parse_gadgets(char *path, struct state *s) n = scandir(path, &dent, file_select, alphasort); for (i=0; i < n; i++) { - g = malloc(sizeof(struct gadget)); + g = malloc(sizeof(struct libusbg_gadget)); strcpy(g->name, dent[i]->d_name); strcpy(g->path, s->path); g->parent = s; /* UDC bound to, if any */ - gadget_read_string(path, g->name, "UDC", g->udc); - gadget_parse_attrs(path, g); - gadget_parse_strings(path, g); - gadget_parse_functions(path, g); - gadget_parse_configs(path, g); + libusbg_read_string(path, g->name, "UDC", g->udc); + libusbg_parse_attrs(path, g); + libusbg_parse_strings(path, g); + libusbg_parse_functions(path, g); + libusbg_parse_configs(path, g); TAILQ_INSERT_TAIL(&s->gadgets, g, gnode); free(dent[i]); } @@ -370,11 +370,11 @@ static int gadget_parse_gadgets(char *path, struct state *s) return 0; } -static int gadget_init_state(char *path, struct state *s) +static int libusbg_init_state(char *path, struct libusbg_state *s) { strcpy(s->path, path); - if (gadget_parse_gadgets(path, s) < 0) { + if (libusbg_parse_gadgets(path, s) < 0) { ERRORNO("unable to parse %s\n", path); return -1; } @@ -386,12 +386,12 @@ static int gadget_init_state(char *path, struct state *s) * User API */ -struct state *gadget_init(char *configfs_path) +struct libusbg_state *libusbg_init(char *configfs_path) { int ret; struct stat sts; - char path[MAX_PATH_LENGTH]; - struct state *s = NULL; + char path[LIBUSBG_MAX_PATH_LENGTH]; + struct libusbg_state *s = NULL; strcpy(path, configfs_path); ret = stat(strcat(path, "/usb_gadget"), &sts); @@ -405,9 +405,9 @@ struct state *gadget_init(char *configfs_path) goto out; } - s = malloc(sizeof(struct state)); + s = malloc(sizeof(struct libusbg_state)); if (s) - gadget_init_state(path, s); + libusbg_init_state(path, s); else ERRORNO("couldn't init gadget state\n"); @@ -415,12 +415,12 @@ out: return s; } -void gadget_cleanup(struct state *s) +void libusbg_cleanup(struct libusbg_state *s) { - struct gadget *g; - struct config *c; - struct binding *b; - struct function *f; + struct libusbg_gadget *g; + struct libusbg_config *c; + struct libusbg_binding *b; + struct libusbg_function *f; while (!TAILQ_EMPTY(&s->gadgets)) { g = TAILQ_FIRST(&s->gadgets); @@ -446,9 +446,9 @@ void gadget_cleanup(struct state *s) free(s); } -struct gadget *gadget_get_gadget(struct state *s, const char *name) +struct libusbg_gadget *libusbg_get_gadget(struct libusbg_state *s, const char *name) { - struct gadget *g; + struct libusbg_gadget *g; TAILQ_FOREACH(g, &s->gadgets, gnode) if (!strcmp(g->name, name)) @@ -457,9 +457,9 @@ struct gadget *gadget_get_gadget(struct state *s, const char *name) return NULL; } -struct function *gadget_get_function(struct gadget *g, const char *name) +struct libusbg_function *libusbg_get_function(struct libusbg_gadget *g, const char *name) { - struct function *f; + struct libusbg_function *f; TAILQ_FOREACH(f, &g->functions, fnode) if (!strcmp(f->name, name)) @@ -468,9 +468,9 @@ struct function *gadget_get_function(struct gadget *g, const char *name) return NULL; } -struct config *gadget_get_config(struct gadget *g, const char *name) +struct libusbg_config *libusbg_get_config(struct libusbg_gadget *g, const char *name) { - struct config *c; + struct libusbg_config *c; TAILQ_FOREACH(c, &g->configs, cnode) if (!strcmp(c->name, name)) @@ -479,9 +479,9 @@ struct config *gadget_get_config(struct gadget *g, const char *name) return NULL; } -struct binding *gadget_get_binding(struct config *c, const char *name) +struct libusbg_binding *libusbg_get_binding(struct libusbg_config *c, const char *name) { - struct binding *b; + struct libusbg_binding *b; TAILQ_FOREACH(b, &c->bindings, bnode) if (!strcmp(b->name, name)) @@ -490,9 +490,9 @@ struct binding *gadget_get_binding(struct config *c, const char *name) return NULL; } -struct binding *gadget_get_link_binding(struct config *c, struct function *f) +struct libusbg_binding *libusbg_get_link_binding(struct libusbg_config *c, struct libusbg_function *f) { - struct binding *b; + struct libusbg_binding *b; TAILQ_FOREACH(b, &c->bindings, bnode) if (b->target == f) @@ -501,17 +501,17 @@ struct binding *gadget_get_link_binding(struct config *c, struct function *f) return NULL; } -struct gadget *gadget_create_gadget(struct state *s, char *name, +struct libusbg_gadget *libusbg_create_gadget(struct libusbg_state *s, char *name, int vendor, int product) { - char gpath[MAX_PATH_LENGTH]; - struct gadget *g, *cur; + char gpath[LIBUSBG_MAX_PATH_LENGTH]; + struct libusbg_gadget *g, *cur; int ret; if (!s) return NULL; - g = gadget_get_gadget(s, name); + g = libusbg_get_gadget(s, name); if (g) { ERROR("duplicate gadget name\n"); return NULL; @@ -519,7 +519,7 @@ struct gadget *gadget_create_gadget(struct state *s, char *name, sprintf(gpath, "%s/%s", s->path, name); - g = malloc(sizeof(struct gadget)); + g = malloc(sizeof(struct libusbg_gadget)); if (!g) { ERRORNO("allocating gadget\n"); return NULL; @@ -538,11 +538,11 @@ struct gadget *gadget_create_gadget(struct state *s, char *name, return NULL; } - gadget_write_hex16(s->path, name, "idVendor", vendor); - gadget_write_hex16(s->path, name, "idProduct", product); + libusbg_write_hex16(s->path, name, "idVendor", vendor); + libusbg_write_hex16(s->path, name, "idProduct", product); - gadget_parse_attrs(s->path, g); - gadget_parse_strings(s->path, g); + libusbg_parse_attrs(s->path, g); + libusbg_parse_strings(s->path, g); /* Insert in string order */ if (TAILQ_EMPTY(&s->gadgets) || @@ -560,45 +560,45 @@ struct gadget *gadget_create_gadget(struct state *s, char *name, return g; } -void gadget_set_gadget_device_class(struct gadget *g, int dclass) +void libusbg_set_gadget_device_class(struct libusbg_gadget *g, int dclass) { g->dclass = dclass; - gadget_write_hex8(g->path, "", "bDeviceClass", dclass); + libusbg_write_hex8(g->path, "", "bDeviceClass", dclass); } -void gadget_set_gadget_device_protocol(struct gadget *g, int dproto) +void libusbg_set_gadget_device_protocol(struct libusbg_gadget *g, int dproto) { g->dproto = dproto; - gadget_write_hex8(g->path, "", "bDeviceProtocol", dproto); + libusbg_write_hex8(g->path, "", "bDeviceProtocol", dproto); } -void gadget_set_gadget_device_subclass(struct gadget *g, int dsubclass) +void libusbg_set_gadget_device_subclass(struct libusbg_gadget *g, int dsubclass) { g->dsubclass = dsubclass; - gadget_write_hex8(g->path, "", "bDeviceSubClass", dsubclass); + libusbg_write_hex8(g->path, "", "bDeviceSubClass", dsubclass); } -void gadget_set_gadget_device_max_packet(struct gadget *g, int maxpacket) +void libusbg_set_gadget_device_max_packet(struct libusbg_gadget *g, int maxpacket) { g->maxpacket = maxpacket; - gadget_write_hex8(g->path, "", "bMaxPacketSize0", maxpacket); + libusbg_write_hex8(g->path, "", "bMaxPacketSize0", maxpacket); } -void gadget_set_gadget_device_bcd_device(struct gadget *g, int bcddevice) +void libusbg_set_gadget_device_bcd_device(struct libusbg_gadget *g, int bcddevice) { g->bcddevice = bcddevice; - gadget_write_hex16(g->path, "", "bcdDevice", bcddevice); + libusbg_write_hex16(g->path, "", "bcdDevice", bcddevice); } -void gadget_set_gadget_device_bcd_usb(struct gadget *g, int bcdusb) +void libusbg_set_gadget_device_bcd_usb(struct libusbg_gadget *g, int bcdusb) { g->bcdusb = bcdusb; - gadget_write_hex16(g->path, "", "bcdUSB", bcdusb); + libusbg_write_hex16(g->path, "", "bcdUSB", bcdusb); } -void gadget_set_gadget_serial_number(struct gadget *g, int lang, char *serno) +void libusbg_set_gadget_serial_number(struct libusbg_gadget *g, int lang, char *serno) { - char path[MAX_PATH_LENGTH]; + char path[LIBUSBG_MAX_PATH_LENGTH]; sprintf(path, "%s/%s/%s/0x%x", g->path, g->name, STRINGS_DIR, lang); @@ -606,12 +606,12 @@ void gadget_set_gadget_serial_number(struct gadget *g, int lang, char *serno) strcpy(g->str_ser, serno); - gadget_write_string(path, "", "serialnumber", serno); + libusbg_write_string(path, "", "serialnumber", serno); } -void gadget_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf) +void libusbg_set_gadget_manufacturer(struct libusbg_gadget *g, int lang, char *mnf) { - char path[MAX_PATH_LENGTH]; + char path[LIBUSBG_MAX_PATH_LENGTH]; sprintf(path, "%s/%s/%s/0x%x", g->path, g->name, STRINGS_DIR, lang); @@ -619,12 +619,12 @@ void gadget_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf) strcpy(g->str_mnf, mnf); - gadget_write_string(path, "", "manufacturer", mnf); + libusbg_write_string(path, "", "manufacturer", mnf); } -void gadget_set_gadget_product(struct gadget *g, int lang, char *prd) +void libusbg_set_gadget_product(struct libusbg_gadget *g, int lang, char *prd) { - char path[MAX_PATH_LENGTH]; + char path[LIBUSBG_MAX_PATH_LENGTH]; sprintf(path, "%s/%s/%s/0x%x", g->path, g->name, STRINGS_DIR, lang); @@ -632,14 +632,14 @@ void gadget_set_gadget_product(struct gadget *g, int lang, char *prd) strcpy(g->str_prd, prd); - gadget_write_string(path, "", "product", prd); + libusbg_write_string(path, "", "product", prd); } -struct function *gadget_create_function(struct gadget *g, enum function_type type, char *instance) +struct libusbg_function *libusbg_create_function(struct libusbg_gadget *g, enum libusbg_function_type type, char *instance) { - char fpath[MAX_PATH_LENGTH]; - char name[MAX_LENGTH]; - struct function *f, *cur; + char fpath[LIBUSBG_MAX_PATH_LENGTH]; + char name[LIBUSBG_MAX_LENGTH]; + struct libusbg_function *f, *cur; int ret; if (!g) @@ -648,8 +648,8 @@ struct function *gadget_create_function(struct gadget *g, enum function_type typ /** * @todo Check for legal function type */ - sprintf(name, "%s.%s", function_names[type], instance); - f = gadget_get_function(g, name); + sprintf(name, "%s.%s", libusbg_function_names[type], instance); + f = libusbg_get_function(g, name); if (f) { ERROR("duplicate function name\n"); return NULL; @@ -657,7 +657,7 @@ struct function *gadget_create_function(struct gadget *g, enum function_type typ sprintf(fpath, "%s/%s/%s/%s", g->path, g->name, FUNCTIONS_DIR, name); - f = malloc(sizeof(struct function)); + f = malloc(sizeof(struct libusbg_function)); if (!f) { ERRORNO("allocating function\n"); return NULL; @@ -674,7 +674,7 @@ struct function *gadget_create_function(struct gadget *g, enum function_type typ return NULL; } - gadget_parse_function_attrs(f); + libusbg_parse_function_attrs(f); /* Insert in string order */ if (TAILQ_EMPTY(&g->functions) || @@ -692,10 +692,10 @@ struct function *gadget_create_function(struct gadget *g, enum function_type typ return f; } -struct config *gadget_create_config(struct gadget *g, char *name) +struct libusbg_config *libusbg_create_config(struct libusbg_gadget *g, char *name) { - char cpath[MAX_PATH_LENGTH]; - struct config *c, *cur; + char cpath[LIBUSBG_MAX_PATH_LENGTH]; + struct libusbg_config *c, *cur; int ret; if (!g) @@ -704,7 +704,7 @@ struct config *gadget_create_config(struct gadget *g, char *name) /** * @todo Check for legal configuration name */ - c = gadget_get_config(g, name); + c = libusbg_get_config(g, name); if (c) { ERROR("duplicate configuration name\n"); return NULL; @@ -712,7 +712,7 @@ struct config *gadget_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(struct libusbg_config)); if (!c) { ERRORNO("allocating configuration\n"); return NULL; @@ -729,7 +729,7 @@ struct config *gadget_create_config(struct gadget *g, char *name) return NULL; } - gadget_parse_config_attrs(c); + libusbg_parse_config_attrs(c); /* Insert in string order */ if (TAILQ_EMPTY(&g->configs) || @@ -747,21 +747,21 @@ struct config *gadget_create_config(struct gadget *g, char *name) return c; } -void gadget_set_config_max_power(struct config *c, int maxpower) +void libusbg_set_config_max_power(struct libusbg_config *c, int maxpower) { c->maxpower = maxpower; - gadget_write_dec(c->path, c->name, "MaxPower", maxpower); + libusbg_write_dec(c->path, c->name, "MaxPower", maxpower); } -void gadget_set_config_bm_attrs(struct config *c, int bmattrs) +void libusbg_set_config_bm_attrs(struct libusbg_config *c, int bmattrs) { c->bmattrs = bmattrs; - gadget_write_hex8(c->path, c->name, "bmAttributes", bmattrs); + libusbg_write_hex8(c->path, c->name, "bmAttributes", bmattrs); } -void gadget_set_config_string(struct config *c, int lang, char *str) +void libusbg_set_config_string(struct libusbg_config *c, int lang, char *str) { - char path[MAX_PATH_LENGTH]; + char path[LIBUSBG_MAX_PATH_LENGTH]; sprintf(path, "%s/%s/%s/0x%x", c->path, c->name, STRINGS_DIR, lang); @@ -769,27 +769,27 @@ void gadget_set_config_string(struct config *c, int lang, char *str) strcpy(c->str_cfg, str); - gadget_write_string(path, "", "configuration", str); + libusbg_write_string(path, "", "configuration", str); } -int gadget_add_config_function(struct config *c, char *name, struct function *f) +int libusbg_add_config_function(struct libusbg_config *c, char *name, struct libusbg_function *f) { - char bpath[MAX_PATH_LENGTH]; - char fpath[MAX_PATH_LENGTH]; - struct binding *b; - struct binding *cur; + char bpath[LIBUSBG_MAX_PATH_LENGTH]; + char fpath[LIBUSBG_MAX_PATH_LENGTH]; + struct libusbg_binding *b; + struct libusbg_binding *cur; int ret = -1; if (!c || !f) return ret; - b = gadget_get_binding(c, name); + b = libusbg_get_binding(c, name); if (b) { ERROR("duplicate binding name\n"); return ret; } - b = gadget_get_link_binding(c, f); + b = libusbg_get_link_binding(c, f); if (b) { ERROR("duplicate binding link\n"); return ret; @@ -798,7 +798,7 @@ int gadget_add_config_function(struct config *c, char *name, struct function *f) sprintf(bpath, "%s/%s/%s", c->path, c->name, name); sprintf(fpath, "%s/%s", f->path, f->name); - b = malloc(sizeof(struct binding)); + b = malloc(sizeof(struct libusbg_binding)); if (!b) { ERRORNO("allocating binding\n"); return -1; @@ -831,19 +831,19 @@ int gadget_add_config_function(struct config *c, char *name, struct function *f) return 0; } -int gadget_get_udcs(struct dirent ***udc_list) +int libusbg_get_udcs(struct dirent ***udc_list) { return scandir("/sys/class/udc", udc_list, file_select, alphasort); } -void gadget_enable_gadget(struct gadget *g, char *udc) +void libusbg_enable_gadget(struct libusbg_gadget *g, char *udc) { - char gudc[MAX_LENGTH]; + char gudc[LIBUSBG_MAX_LENGTH]; struct dirent **udc_list; int n; if (!udc) { - n = gadget_get_udcs(&udc_list); + n = libusbg_get_udcs(&udc_list); if (!n) return; strcpy(gudc, udc_list[0]->d_name); @@ -854,41 +854,41 @@ void gadget_enable_gadget(struct gadget *g, char *udc) strcpy (gudc, udc); strcpy(g->udc, gudc); - gadget_write_string(g->path, g->name, "UDC", gudc); + libusbg_write_string(g->path, g->name, "UDC", gudc); } -void gadget_disable_gadget(struct gadget *g) +void libusbg_disable_gadget(struct libusbg_gadget *g) { strcpy(g->udc, ""); - gadget_write_string(g->path, g->name, "UDC", ""); + libusbg_write_string(g->path, g->name, "UDC", ""); } /* * USB function-specific attribute configuration */ -void gadget_set_net_dev_addr(struct function *f, struct ether_addr *dev_addr) +void libusbg_set_net_dev_addr(struct libusbg_function *f, struct ether_addr *dev_addr) { char *str_addr; f->attr.net.dev_addr = *dev_addr; str_addr = ether_ntoa(dev_addr); - gadget_write_string(f->path, f->name, "dev_addr", str_addr); + libusbg_write_string(f->path, f->name, "dev_addr", str_addr); } -void gadget_set_net_host_addr(struct function *f, struct ether_addr *host_addr) +void libusbg_set_net_host_addr(struct libusbg_function *f, struct ether_addr *host_addr) { char *str_addr; f->attr.net.host_addr = *host_addr; str_addr = ether_ntoa(host_addr); - gadget_write_string(f->path, f->name, "host_addr", str_addr); + libusbg_write_string(f->path, f->name, "host_addr", str_addr); } -void gadget_set_net_qmult(struct function *f, int qmult) +void libusbg_set_net_qmult(struct libusbg_function *f, int qmult) { f->attr.net.qmult = qmult; - gadget_write_dec(f->path, f->name, "qmult", qmult); + libusbg_write_dec(f->path, f->name, "qmult", qmult); } -- 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