libusbg-0.1.0
include/usbg/usbg.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Linaro Limited
00003  *
00004  * Matt Porter <mporter@linaro.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  */
00016 
00017 #include <dirent.h>
00018 #include <sys/queue.h>
00019 #include <netinet/ether.h>
00020 
00033 #define DEFAULT_UDC             NULL
00034 #define LANG_US_ENG             0x0409
00035 
00040 struct state
00041 {
00042         char path[256];
00043 
00044         TAILQ_HEAD(ghead, gadget) gadgets;
00045 };
00046 
00051 struct gadget
00052 {
00053         char name[40];
00054         char path[256];
00055         char udc[256];
00056         int dclass;
00057         int dsubclass;
00058         int dproto;
00059         int maxpacket;
00060         int bcddevice;
00061         int bcdusb;
00062         int product;
00063         int vendor;
00064         char str_ser[256];
00065         char str_mnf[256];
00066         char str_prd[256];
00067         TAILQ_ENTRY(gadget) gnode;
00068         TAILQ_HEAD(chead, config) configs;
00069         TAILQ_HEAD(fhead, function) functions;
00070         struct state *parent;
00071 };
00072 
00077 struct config
00078 {
00079         TAILQ_ENTRY(config) cnode;
00080         TAILQ_HEAD(bhead, binding) bindings;
00081         struct gadget *parent;
00082 
00083         char name[40];
00084         char path[256];
00085         int maxpower;
00086         int bmattrs;
00087         char str_cfg[256];
00088 };
00089 
00094 enum function_type
00095 {
00096         F_SERIAL,
00097         F_ACM,
00098         F_OBEX,
00099         F_ECM,
00100         F_SUBSET,
00101         F_NCM,
00102         F_EEM,
00103         F_RNDIS,
00104         F_PHONET,
00105 };
00106 
00111 const char *function_names[] =
00112 {
00113         "gser",
00114         "acm",
00115         "obex",
00116         "ecm",
00117         "geth",
00118         "ncm",
00119         "eem",
00120         "rndis",
00121         "phonet",
00122 };
00123 
00128 struct serial_attrs {
00129         int port_num;
00130 };
00131 
00136 struct net_attrs {
00137         struct ether_addr dev_addr;
00138         struct ether_addr host_addr;
00139         char ifname[256];
00140         int qmult;
00141 };
00142 
00147 struct phonet_attrs {
00148         char ifname[256];
00149 };
00150 
00155 union attrs {
00156         struct serial_attrs serial;
00157         struct net_attrs net;
00158         struct phonet_attrs phonet;
00159 };
00160 
00165 struct function
00166 {
00167         TAILQ_ENTRY(function) fnode;
00168         struct gadget *parent;
00169 
00170         char name[40];
00171         char path[256];
00172 
00173         enum function_type type;
00174         union attrs attr;
00175 };
00176 
00177 
00183 struct binding
00184 {
00185         TAILQ_ENTRY(binding) bnode;
00186         struct config *parent;
00187         struct function *target;
00188 
00189         char name[40];
00190         char path[256];
00191 };
00192 
00193 /* Library init and cleanup */
00194 
00200 extern struct state *usbg_init(char *configfs_path);
00201 
00206 extern void usbg_cleanup(struct state *s);
00207 
00208 /* USB gadget queries */
00209 
00216 extern struct gadget *usbg_get_gadget(struct state *s, const char *name);
00217 
00224 extern struct function *usbg_get_function(struct gadget *g, const char *name);
00225 
00232 extern struct config *usbg_get_config(struct gadget *g, const char *name);
00233 
00234 /* USB gadget allocation and configuration */
00235 
00244 extern struct gadget *usbg_create_gadget(struct state *s, char *name, int vendor, int product);
00245 
00251 extern void usbg_set_gadget_device_class(struct gadget *g, int dclass);
00252 
00258 extern void usbg_set_gadget_device_protocol(struct gadget *g, int dproto);
00259 
00265 extern void usbg_set_gadget_device_subclass(struct gadget *g, int dsubclass);
00266 
00272 extern void usbg_set_gadget_device_max_packet(struct gadget *g, int maxpacket);
00273 
00279 extern void usbg_set_gadget_device_bcd_device(struct gadget *g, int bcddevice);
00280 
00286 extern void usbg_set_gadget_device_bcd_usb(struct gadget *g, int bcdusb);
00287 
00294 extern void usbg_set_gadget_serial_number(struct gadget *g, int lang, char *ser);
00295 
00302 extern void usbg_set_gadget_manufacturer(struct gadget *g, int lang, char *mnf);
00303 
00310 extern void usbg_set_gadget_product(struct gadget *g, int lang, char *prd);
00311 
00312 /* USB function allocation and configuration */
00313 
00321 extern struct function *usbg_create_function(struct gadget *g, enum function_type type, char *instance);
00322 
00323 /* USB configurations allocation and configuration */
00324 
00331 extern struct config *usbg_create_config(struct gadget *g, char *name);
00332 
00338 extern void usbg_set_config_max_power(struct config *c, int maxpower);
00339 
00345 extern void usbg_set_config_bm_attrs(struct config *c, int bmattrs);
00346 
00353 extern void usbg_set_config_string(struct config *c, int lang, char *string);
00354 
00362 extern int usbg_add_config_function(struct config *c, char *name, struct function *f);
00363 
00364 /* USB gadget setup and teardown */
00365 
00371 extern int usbg_get_udcs(struct dirent ***udc_list);
00372 
00378 extern void usbg_enable_gadget(struct gadget *g, char *udc);
00379 
00384 extern void usbg_disable_gadget(struct gadget *g);
00385 
00386 /*
00387  * USB function-specific attribute configuration
00388  */
00389 
00395 extern void usbg_set_net_dev_addr(struct function *f, struct ether_addr *addr);
00396 
00402 extern void usbg_set_net_host_addr(struct function *f, struct ether_addr *addr);
00403 
00409 extern void usbg_set_net_qmult(struct function *f, int qmult);
00410 
00415 #define usbg_for_each_gadget(g, s)      TAILQ_FOREACH(g, &s->gadgets, gnode)
00416 
00421 #define usbg_for_each_function(f, g)    TAILQ_FOREACH(f, &g->functions, fnode)
00422 
00427 #define usbg_for_each_config(c, g)      TAILQ_FOREACH(c, &g->configs, cnode)
00428 
00433 #define usbg_for_each_binding(b, c)     TAILQ_FOREACH(b, &c->bindings, bnode)
00434 
 All Data Structures Files Functions Variables Enumerations Defines