libusbgx-0.2.0
gadget-rndis-os-desc.c

This is an example of how to create an gadget with OS String support.

/*
* Copyright (C) 2017 Samsung Electronics
*
* Krzysztof Opasiak <k.opasiak@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*/
#include <errno.h>
#include <stdio.h>
#include <linux/usb/ch9.h>
#include <usbg/usbg.h>
#define VENDOR 0x1d6b
#define PRODUCT 0x0104
int main(void)
{
usbg_function *f_rndis;
int ret = -EINVAL;
int usbg_ret;
struct usbg_gadget_attrs g_attrs = {
.bcdUSB = 0x0200,
.bDeviceClass = USB_CLASS_PER_INTERFACE,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
.bMaxPacketSize0 = 64, /* Max allowed ep0 packet size */
.idVendor = VENDOR,
.idProduct = PRODUCT,
.bcdDevice = 0x0001, /* Verson of device */
};
struct usbg_gadget_strs g_strs = {
.serial = "0123456789", /* Serial number */
.manufacturer = "Foo Inc.", /* Manufacturer */
.product = "Bar Gadget" /* Product string */
};
struct usbg_config_strs c_strs = {
.configuration = "RNDIS",
};
struct usbg_gadget_os_descs g_os_desc = {
.use = true,
.b_vendor_code = 0xBC,
.qw_sign = "MSFT100",
};
struct usbg_function_os_desc f_os_desc = {
.compatible_id = "RNDIS",
.sub_compatible_id = "5162001",
};
usbg_ret = usbg_init("/sys/kernel/config", &s);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error on USB gadget init\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out1;
}
usbg_ret = usbg_create_gadget(s, "g1", &g_attrs, &g_strs, &g);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error on create gadget\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_create_function(g, USBG_F_RNDIS, "usb0", NULL, &f_rndis);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error creating RNDIS function\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_set_interf_os_desc(f_rndis, "rndis", &f_os_desc);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error setting function OS desc\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
/* NULL can be passed to use kernel defaults */
usbg_ret = usbg_create_config(g, 1, "The only one", NULL, &c_strs, &c);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error creating config\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_add_config_function(c, "rndis.usb0", f_rndis);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error adding rndis.usb0\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_set_gadget_os_descs(g, &g_os_desc);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error setting gadget OS desc\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_set_os_desc_config(g, c);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error setting gadget OS desc config\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_enable_gadget(g, DEFAULT_UDC);
if (usbg_ret != USBG_SUCCESS) {
fprintf(stderr, "Error enabling gadget\n");
fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
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:1530
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:2366
int usbg_set_interf_os_desc(usbg_function *f, const char *iname, const struct usbg_function_os_desc *f_os_desc)
Set OS Descriptor compatibility of given function.
Definition: usbg.c:2148
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:2177
int usbg_enable_gadget(usbg_gadget *g, usbg_udc *udc)
Enable a USB gadget device.
Definition: usbg.c:2546
int usbg_init(const char *configfs_path, usbg_state **state)
Initialize the libusbgx library state.
Definition: usbg.c:1089
void usbg_cleanup(usbg_state *s)
Clean up the libusbgx library state.
Definition: usbg.c:1134
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:2049
int usbg_set_os_desc_config(usbg_gadget *g, usbg_config *c)
Set configuration for OS Descriptors.
Definition: usbg.c:2525
int usbg_set_gadget_os_descs(usbg_gadget *g, const struct usbg_gadget_os_descs *g_os_descs)
Set the USB gadget OS Descriptor.
Definition: usbg.c:2014
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:196
USB OS Descriptor function attributes.
Definition: usbg.h:231
USB gadget device attributes.
Definition: usbg.h:132
USB gadget Microsoft OS Descriptors.
Definition: usbg.h:165
USB gadget device strings.
Definition: usbg.h:155
Definition: usbg_internal.h:117
Definition: usbg_internal.h:129
Definition: usbg_internal.h:103
Definition: usbg_internal.h:93