2 * Copyright (c) 2012-2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <usbg/usbg.h>
24 static int create_func(void *data)
26 struct gt_gadget_create_data *dt;
32 dt = (struct gt_gadget_create_data *)data;
34 r = usbg_create_gadget(backend_ctx.libusbg_state,
39 if (r != USBG_SUCCESS) {
40 fprintf(stderr, "Unable to create gadget %s: %s\n",
41 dt->name, usbg_strerror(r));
45 for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++) {
46 if (dt->attr_val[i] == -1)
49 r = usbg_set_gadget_attr(g, i, dt->attr_val[i]);
50 if (r != USBG_SUCCESS) {
51 fprintf(stderr, "Unable to set attribute %s: %s\n",
52 usbg_get_gadget_attr_str(i),
58 for (i = 0; i < G_N_ELEMENTS(dt->str_val); i++) {
59 if (dt->str_val[i] == NULL)
62 r = gadget_strs[i].set_fn(g, LANG_US_ENG, dt->str_val[i]);
63 if (r != USBG_SUCCESS) {
64 fprintf(stderr, "Unable to set string %s: %s\n",
74 usbg_rm_gadget(g, USBG_RM_RECURSE);
78 static int rm_func(void *data)
80 struct gt_gadget_rm_data *dt;
86 dt = (struct gt_gadget_rm_data *)data;
88 g = usbg_get_gadget(backend_ctx.libusbg_state, dt->name);
90 fprintf(stderr, "Gadget '%s' not found\n", dt->name);
94 u = usbg_get_gadget_udc(g);
97 if (!(dt->opts & GT_FORCE)) {
98 fprintf(stderr, "Gadget is enabled, disable it first or use --force option\n");
102 usbg_ret = usbg_disable_gadget(g);
103 if (usbg_ret != USBG_SUCCESS) {
104 fprintf(stderr, "Error on disable gadget: %s : %s\n",
105 usbg_error_name(usbg_ret), usbg_strerror(usbg_ret));
110 if (dt->opts & GT_RECURSIVE)
111 opts |= USBG_RM_RECURSE;
113 usbg_ret = usbg_rm_gadget(g, opts);
114 if (usbg_ret != USBG_SUCCESS){
115 fprintf(stderr, "Error on gadget remove: %s : %s\n",
116 usbg_error_name(usbg_ret), usbg_strerror(usbg_ret));
125 static int enable_func(void *data)
127 struct gt_gadget_enable_data *dt;
129 dt = (struct gt_gadget_enable_data *)data;
132 usbg_udc *udc = NULL;
135 if (dt->udc != NULL) {
136 udc = usbg_get_udc(backend_ctx.libusbg_state, dt->udc);
138 fprintf(stderr, "Failed to get udc\n");
143 g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
145 fprintf(stderr, "Failed to get gadget\n");
149 usbg_ret = usbg_enable_gadget(g, udc);
150 if (usbg_ret != USBG_SUCCESS) {
151 fprintf(stderr, "Failed to enable gadget %s\n", usbg_strerror(usbg_ret));
158 static int disable_func(void *data)
160 struct gt_gadget_disable_data *dt;
166 dt = (struct gt_gadget_disable_data *)data;
169 g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
171 fprintf(stderr, "Gadget '%s' not found\n", dt->gadget);
174 } else if (dt->udc) {
175 u = usbg_get_udc(backend_ctx.libusbg_state, dt->udc);
177 fprintf(stderr, "UDC '%s' not found\n", dt->udc);
181 g = usbg_get_udc_gadget(u);
183 fprintf(stderr, "No gadget enabled on this UDC\n");
187 /*TODO disabling default gadget */
188 fprintf(stderr, "Gadget not specified\n");
192 usbg_ret = usbg_disable_gadget(g);
193 if (usbg_ret != USBG_SUCCESS) {
194 fprintf(stderr, "Error on disable gadget: %s : %s\n",
195 usbg_error_name(usbg_ret), usbg_strerror(usbg_ret));
202 struct gt_gadget_backend gt_gadget_backend_libusbg = {
203 .create = create_func,
207 .enable = enable_func,
208 .disable = disable_func,
212 .template_default = NULL,
213 .template_get = NULL,
214 .template_set = NULL,