1
0
Fork 0
mirror of https://github.com/linux-usb-gadgets/libusbgx.git synced 2025-07-14 01:49:43 +03:00

libusbgx: Add functions to set all strs and attrs with one call.

Rename usbg_create_gadget() to usbg_create_vid_pid().
dd usbg_create_gadget() to allow gadget creation
with given attributes and strings. Add usbg_set_gadget_attrs()
which allow to set all attributes with one call. Add
usbg_set_gadget_strs() which allow to set all strings with
one call.

Gadget structure creation and initialization has been moved to
usbg_create_empty_gadget() to avoid copy-paste same code in
usbg_create_gadget_vid_pid() and usbg_create_gadget().

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
[Port from libusbg and update description]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
This commit is contained in:
Krzysztof Opasiak 2014-02-14 12:52:27 +01:00
parent d1148ca3fc
commit 2a0e2bc756
3 changed files with 137 additions and 22 deletions

View file

@ -42,7 +42,7 @@ int main(void)
goto out1; goto out1;
} }
g = usbg_create_gadget(s, "g1", VENDOR, PRODUCT); g = usbg_create_gadget_vid_pid(s, "g1", VENDOR, PRODUCT);
if (!g) { if (!g) {
fprintf(stderr, "Error on create gadget\n"); fprintf(stderr, "Error on create gadget\n");
goto out2; goto out2;

View file

@ -269,9 +269,30 @@ extern struct config *usbg_get_config(struct gadget *g, const char *name);
* @param idProduct Gadget product ID * @param idProduct Gadget product ID
* @return Pointer to gadget or NULL if the gadget cannot be created * @return Pointer to gadget or NULL if the gadget cannot be created
*/ */
extern struct gadget *usbg_create_gadget(struct state *s, char *name, extern struct gadget *usbg_create_gadget_vid_pid(struct state *s, char *name,
uint16_t idVendor, uint16_t idProduct); uint16_t idVendor, uint16_t idProduct);
/**
* @brief Create a new USB gadget device and set given attributes
* and strings
* @param s Pointer to state
* @param name Name of the gadget
* @param g_attrs Gadget attributes to be set. If NULL setting is omitted.
* @param g_strs Gadget strings to be set. If NULL setting is omitted.
* @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(struct state *s, char *name,
struct gadget_attrs *g_attrs, struct gadget_strs *g_strs);
/**
* @brief Set the USB gadget attributes
* @param g Pointer to gadget
* @param g_attrs Gadget attributes
*/
extern void usbg_set_gadget_attrs(struct gadget *g,
struct gadget_attrs *g_attrs);
/** /**
* @brief Set the USB gadget vendor id * @brief Set the USB gadget vendor id
* @param g Pointer to gadget * @param g Pointer to gadget
@ -333,6 +354,15 @@ extern void usbg_set_gadget_device_bcd_device(struct gadget *g,
*/ */
extern void usbg_set_gadget_device_bcd_usb(struct gadget *g, uint16_t bcdUSB); extern void usbg_set_gadget_device_bcd_usb(struct gadget *g, uint16_t bcdUSB);
/**
* @brief Set the USB gadget strings
* @param g Pointer to gadget
* @param lang USB language ID
* @param g_sttrs Gadget attributes
*/
extern void usbg_set_gadget_strs(struct gadget *g, int lang,
struct gadget_strs *g_strs);
/** /**
* @brief Set the serial number for a gadget * @brief Set the serial number for a gadget
* @param g Pointer to gadget * @param g Pointer to gadget

View file

@ -525,22 +525,12 @@ struct binding *usbg_get_link_binding(struct config *c, struct function *f)
return NULL; return NULL;
} }
struct gadget *usbg_create_gadget(struct state *s, char *name, static struct gadget *usbg_create_empty_gadget(struct state *s, char *name)
uint16_t idVendor, uint16_t idProduct)
{ {
char gpath[USBG_MAX_PATH_LENGTH]; char gpath[USBG_MAX_PATH_LENGTH];
struct gadget *g, *cur; struct gadget *g;
int ret; int ret;
if (!s)
return NULL;
g = usbg_get_gadget(s, name);
if (g) {
ERROR("duplicate gadget name\n");
return NULL;
}
sprintf(gpath, "%s/%s", s->path, name); sprintf(gpath, "%s/%s", s->path, name);
g = malloc(sizeof(struct gadget)); g = malloc(sizeof(struct gadget));
@ -552,7 +542,7 @@ struct gadget *usbg_create_gadget(struct state *s, char *name,
TAILQ_INIT(&g->configs); TAILQ_INIT(&g->configs);
TAILQ_INIT(&g->functions); TAILQ_INIT(&g->functions);
strcpy(g->name, name); strcpy(g->name, name);
sprintf(g->path, "%s", s->path); strcpy(g->path, s->path);
g->parent = s; g->parent = s;
ret = mkdir(gpath, S_IRWXU|S_IRWXG|S_IRWXO); ret = mkdir(gpath, S_IRWXU|S_IRWXG|S_IRWXO);
@ -562,17 +552,94 @@ struct gadget *usbg_create_gadget(struct state *s, char *name,
return NULL; return NULL;
} }
usbg_write_hex16(s->path, name, "idVendor", idVendor); /* Should be empty but read the default */
usbg_write_hex16(s->path, name, "idProduct", idProduct); usbg_read_string(g->path, g->name, "UDC", g->udc);
usbg_parse_gadget_attrs(s->path, name, &g->attrs);
usbg_parse_strings(s->path, name, &g->strs);
INSERT_TAILQ_STRING_ORDER(&s->gadgets, ghead, name, g, gnode);
return g; return g;
} }
struct gadget *usbg_create_gadget_vid_pid(struct state *s, char *name,
uint16_t idVendor, uint16_t idProduct)
{
struct gadget *g;
if (!s)
return NULL;
g = usbg_get_gadget(s, name);
if (g) {
ERROR("duplicate gadget name\n");
return NULL;
}
g = usbg_create_empty_gadget(s, name);
/* Check if gadget creation was successful and set attributes */
if (g) {
usbg_write_hex16(s->path, name, "idVendor", idVendor);
usbg_write_hex16(s->path, name, "idProduct", idProduct);
usbg_parse_gadget_attrs(s->path, name, &g->attrs);
usbg_parse_strings(s->path, name, &g->strs);
INSERT_TAILQ_STRING_ORDER(&s->gadgets, ghead, name, g, gnode);
}
return g;
}
struct gadget *usbg_create_gadget(struct state *s, char *name,
struct gadget_attrs *g_attrs, struct gadget_strs *g_strs)
{
struct gadget *g;
if (!s)
return NULL;
g = usbg_get_gadget(s, name);
if (g) {
ERROR("duplicate gadget name\n");
return NULL;
}
g = usbg_create_empty_gadget(s, name);
/* Check if gadget creation was successful and set attrs and strings */
if (g) {
if (g_attrs)
usbg_set_gadget_attrs(g, g_attrs);
else
usbg_parse_gadget_attrs(s->path, name, &g->attrs);
if (g_strs)
usbg_set_gadget_strs(g, LANG_US_ENG, g_strs);
else
usbg_parse_strings(s->path, name, &g->strs);
INSERT_TAILQ_STRING_ORDER(&s->gadgets, ghead, name, g, gnode);
}
return g;
}
void usbg_set_gadget_attrs(struct gadget *g, struct gadget_attrs *g_attrs)
{
if (!g || !g_attrs)
return;
g->attrs = *g_attrs;
usbg_write_hex16(g->path, g->name, "bcdUSB", g_attrs->bcdUSB);
usbg_write_hex8(g->path, g->name, "bDeviceClass", g_attrs->bDeviceClass);
usbg_write_hex8(g->path, g->name, "bDeviceSubClass", g_attrs->bDeviceSubClass);
usbg_write_hex8(g->path, g->name, "bDeviceProtocol", g_attrs->bDeviceProtocol);
usbg_write_hex8(g->path, g->name, "bMaxPacketSize0", g_attrs->bMaxPacketSize0);
usbg_write_hex16(g->path, g->name, "idVendor", g_attrs->idVendor);
usbg_write_hex16(g->path, g->name, "idProduct", g_attrs->idProduct);
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(struct gadget *g, uint16_t idVendor)
{ {
g->attrs.idVendor = idVendor; g->attrs.idVendor = idVendor;
@ -621,6 +688,24 @@ void usbg_set_gadget_device_bcd_usb(struct gadget *g, uint16_t bcdUSB)
usbg_write_hex16(g->path, g->name, "bcdUSB", bcdUSB); usbg_write_hex16(g->path, g->name, "bcdUSB", bcdUSB);
} }
void usbg_set_gadget_strs(struct gadget *g, int lang,
struct gadget_strs *g_strs)
{
char path[USBG_MAX_PATH_LENGTH];
sprintf(path, "%s/%s/%s/0x%x", g->path, g->name, STRINGS_DIR, lang);
mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO);
/* strings in library are hardcoded to US English for now */
if (lang == LANG_US_ENG)
g->strs = *g_strs;
usbg_write_string(path, "", "serialnumber", g_strs->str_ser);
usbg_write_string(path, "", "manufacturer", g_strs->str_mnf);
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(struct gadget *g, int lang, char *serno)
{ {
char path[USBG_MAX_PATH_LENGTH]; char path[USBG_MAX_PATH_LENGTH];