This is an example of how to create gadget with FunctionFS based functions in two ways. After executing this program gadget will not be enabled because ffs instances should be mounted and both descriptors and strings should be written to ep0. For more details about FunctionFS please refer to FunctionFS documentation in linux kernel repository.
#include <errno.h>
#include <stdio.h>
#include <linux/usb/ch9.h>
#define VENDOR 0x1d6b
#define PRODUCT 0x0104
int main(void)
{
int ret = -EINVAL;
int usbg_ret;
.bDeviceClass = USB_CLASS_PER_INTERFACE,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64,
.idVendor = VENDOR,
.idProduct = PRODUCT,
.bcdDevice = 0x0001,
};
.manufacturer = "Foo Inc.",
.product = "Bar Gadget"
};
};
usbg_ret =
usbg_init(
"/sys/kernel/config", &s);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error on USB gadget init\n");
goto out1;
}
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error on create gadget\n");
goto out2;
}
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error creating ffs1 function\n");
goto out2;
}
NULL, &f_ffs2);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error creating ffs2 function\n");
goto out2;
}
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error creating config\n");
goto out2;
}
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error adding ffs1\n");
goto out2;
}
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error adding ffs2\n");
goto out2;
}
fprintf(stdout, "2xFFS gadget has been created.\n"
"Enable it after preparing your functions.\n");
ret = 0;
out2:
out1:
return ret;
}
int usbg_create_gadget(usbg_state *s, const char *name, const struct usbg_gadget_attrs *g_attrs, const struct usbg_gadget_strs *g_strs, usbg_gadget **g)
Create a new USB gadget device and set given attributes and strings.
Definition usbg.c:1532
const char * usbg_strerror(usbg_error e)
Get the short description of error.
Definition usbg_error.c:123
int usbg_add_config_function(usbg_config *c, const char *name, usbg_function *f)
Add a function to a configuration.
Definition usbg.c:2368
int usbg_create_config(usbg_gadget *g, int id, const char *label, const struct usbg_config_attrs *c_attrs, const struct usbg_config_strs *c_strs, usbg_config **c)
Create a new USB gadget configuration.
Definition usbg.c:2179
int usbg_init(const char *configfs_path, usbg_state **state)
Initialize the libusbgx library state.
Definition usbg.c:1091
void usbg_cleanup(usbg_state *s)
Clean up the libusbgx library state.
Definition usbg.c:1136
int usbg_create_function(usbg_gadget *g, usbg_function_type type, const char *instance, void *f_attrs, usbg_function **f)
Create a new USB gadget function and set its attributes.
Definition usbg.c:2051
const char * usbg_error_name(usbg_error e)
Get the error name as a constant string.
Definition usbg_error.c:62
USB configuration strings.
Definition usbg.h:199
USB gadget device attributes.
Definition usbg.h:135
USB gadget device strings.
Definition usbg.h:158
Definition usbg_internal.h:117
Definition usbg_internal.h:129
Definition usbg_internal.h:103
Definition usbg_internal.h:93