forked from projectara/gbsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgadget.c
115 lines (94 loc) · 2.43 KB
/
gadget.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Greybus Simulator
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Provided under the three clause BSD license found in the LICENSE file.
*/
#include <errno.h>
#include <stdio.h>
#include <usbg/usbg.h>
#include "gbsim.h"
#define VENDOR 0xffff
#define PRODUCT 0x0001
int gadget_create(usbg_state **s, usbg_gadget **g)
{
usbg_config *c;
usbg_function *f;
int ret = -EINVAL;
int usbg_ret;
usbg_gadget_attrs g_attrs = {
0x0200, /* bcdUSB */
0x00, /* Defined at interface level */
0x00, /* subclass */
0x00, /* device protocol */
0x0040, /* Max allowed packet size */
VENDOR,
PRODUCT,
0x0001, /* Verson of device */
};
usbg_gadget_strs g_strs = {
"0123456789", /* Serial number */
"Toshiba", /* Manufacturer */
"AP Bridge" /* Product string */
};
usbg_config_strs c_strs = {
"AP Bridge"
};
usbg_ret = usbg_init("/sys/kernel/config", s);
if (usbg_ret != USBG_SUCCESS) {
gbsim_error("Error on USB gadget init\n");
gbsim_error("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) {
gbsim_error("Error on create gadget\n");
gbsim_error("Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_create_function(*g, F_FFS, "gbsim", NULL, &f);
if (usbg_ret != USBG_SUCCESS) {
gbsim_error("Error creating gbsim function\n");
gbsim_error("Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_create_config(*g, 1, NULL, NULL, &c_strs, &c);
if (usbg_ret != USBG_SUCCESS) {
gbsim_error("Error creating config\n");
gbsim_error("Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
usbg_ret = usbg_add_config_function(c, "gbsim", f);
if (usbg_ret != USBG_SUCCESS) {
gbsim_error("Error adding gbsim configuration\n");
gbsim_error("Error: %s : %s\n", usbg_error_name(usbg_ret),
usbg_strerror(usbg_ret));
goto out2;
}
gbsim_info("USB gadget created\n");
return 0;
out2:
gadget_cleanup(*s, *g);
out1:
return ret;
}
int gadget_enable(usbg_gadget *g)
{
return usbg_enable_gadget(g, NULL);
}
int gadget_cleanup(usbg_state *s, usbg_gadget *g)
{
gbsim_debug("gadget_cleanup\n");
if (g) {
usbg_disable_gadget(g);
usbg_rm_gadget(g, USBG_RM_RECURSE);
}
usbg_cleanup(s);
return 0;
}