gadget: Add gadget disable on libusbg backend
[platform/upstream/gt.git] / source / gadget / src / gadget_libusbg.c
1 /*
2  * Copyright (c) 2012-2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <usbg/usbg.h>
18 #include <stdio.h>
19
20 #include "gadget.h"
21 #include "backend.h"
22 #include "common.h"
23
24 static int create_func(void *data)
25 {
26         struct gt_gadget_create_data *dt;
27         int i;
28         int r;
29
30         usbg_gadget *g;
31
32         dt = (struct gt_gadget_create_data *)data;
33
34         r = usbg_create_gadget(backend_ctx.libusbg_state,
35                                dt->name,
36                                NULL,
37                                NULL,
38                                &g);
39         if (r != USBG_SUCCESS) {
40                 fprintf(stderr, "Unable to create gadget %s: %s\n",
41                         dt->name, usbg_strerror(r));
42                 return -1;
43         }
44
45         for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++) {
46                 if (dt->attr_val[i] == -1)
47                         continue;
48
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),
53                                 usbg_strerror(r));
54                         goto err_usbg;
55                 }
56         }
57
58         for (i = 0; i < G_N_ELEMENTS(dt->str_val); i++) {
59                 if (dt->str_val[i] == NULL)
60                         continue;
61
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",
65                                 gadget_strs[i].name,
66                                 usbg_strerror(r));
67                         goto err_usbg;
68                 }
69         }
70
71         return 0;
72
73 err_usbg:
74         usbg_rm_gadget(g, USBG_RM_RECURSE);
75         return -1;
76 }
77
78 static int rm_func(void *data)
79 {
80         struct gt_gadget_rm_data *dt;
81         int usbg_ret;
82         usbg_udc *u;
83         usbg_gadget *g;
84         int opts = 0;
85
86         dt = (struct gt_gadget_rm_data *)data;
87
88         g = usbg_get_gadget(backend_ctx.libusbg_state, dt->name);
89         if (g == NULL) {
90                 fprintf(stderr, "Gadget '%s' not found\n", dt->name);
91                 goto err;
92         }
93
94         u = usbg_get_gadget_udc(g);
95
96         if (u) {
97                 if (!(dt->opts & GT_FORCE)) {
98                         fprintf(stderr, "Gadget is enabled, disable it first or use --force option\n");
99                         goto err;
100                 }
101
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));
106                         goto err;
107                 }
108         }
109
110         if (dt->opts & GT_RECURSIVE)
111                 opts |= USBG_RM_RECURSE;
112
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));
117                 goto err;
118         }
119
120         return 0;
121 err:
122         return -1;
123 }
124
125 static int enable_func(void *data)
126 {
127         struct gt_gadget_enable_data *dt;
128
129         dt = (struct gt_gadget_enable_data *)data;
130
131         usbg_gadget *g;
132         usbg_udc *udc = NULL;
133         int usbg_ret;
134
135         if (dt->udc != NULL) {
136                 udc = usbg_get_udc(backend_ctx.libusbg_state, dt->udc);
137                 if (udc == NULL) {
138                         fprintf(stderr, "Failed to get udc\n");
139                         return -1;
140                 }
141         }
142
143         g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
144         if (g == NULL) {
145                 fprintf(stderr, "Failed to get gadget\n");
146                 return -1;
147         }
148
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));
152                 return -1;
153         }
154
155         return 0;
156 }
157
158 static int disable_func(void *data)
159 {
160         struct gt_gadget_disable_data *dt;
161
162         usbg_gadget *g;
163         usbg_udc *u;
164         int usbg_ret;
165
166         dt = (struct gt_gadget_disable_data *)data;
167
168         if (dt->gadget) {
169                 g = usbg_get_gadget(backend_ctx.libusbg_state, dt->gadget);
170                 if (g == NULL) {
171                         fprintf(stderr, "Gadget '%s' not found\n", dt->gadget);
172                         return -1;
173                 }
174         } else if (dt->udc) {
175                 u = usbg_get_udc(backend_ctx.libusbg_state, dt->udc);
176                 if (u == NULL) {
177                         fprintf(stderr, "UDC '%s' not found\n", dt->udc);
178                         return -1;
179                 }
180
181                 g = usbg_get_udc_gadget(u);
182                 if (g == NULL) {
183                         fprintf(stderr, "No gadget enabled on this UDC\n");
184                         return -1;
185                 }
186         } else {
187                 /*TODO disabling default gadget */
188                 fprintf(stderr, "Gadget not specified\n");
189                 return -1;
190         }
191
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));
196                 return -1;
197         }
198
199         return 0;
200 }
201
202 struct gt_gadget_backend gt_gadget_backend_libusbg = {
203         .create = create_func,
204         .rm = rm_func,
205         .get = NULL,
206         .set = NULL,
207         .enable = enable_func,
208         .disable = disable_func,
209         .gadget = NULL,
210         .load = NULL,
211         .save = NULL,
212         .template_default = NULL,
213         .template_get = NULL,
214         .template_set = NULL,
215         .template_rm = NULL,
216 };