2 * Copyright (c) 2018 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.
21 #include <linux/limits.h>
23 #include <libsyscommon/file.h>
24 #include <libsyscommon/libsystemd.h>
25 #include <hal/device/hal-usb_gadget-interface.h>
27 #include "usb_gadget.h"
28 #include "hal-backend-common.h"
30 #define MAX_GADGET_STR_LEN 256
32 #define LEGACY_ROOTPATH "/sys/class/usb_mode/usb0"
34 /* Device descriptor values */
35 #define LEGACY_ID_VENDOR_PATH LEGACY_ROOTPATH"/idVendor"
36 #define LEGACY_ID_PRODUCT_PATH LEGACY_ROOTPATH"/idProduct"
37 #define LEGACY_BCD_DEVICE_PATH LEGACY_ROOTPATH"/bcdDevice"
38 #define LEGACY_CLASS_PATH LEGACY_ROOTPATH"/bDeviceClass"
39 #define LEGACY_SUBCLASS_PATH LEGACY_ROOTPATH"/bDeviceSubClass"
40 #define LEGACY_PROTOCOL_PATH LEGACY_ROOTPATH"/bDeviceProtocol"
43 #define LEGACY_IMANUFACTURER_PATH LEGACY_ROOTPATH"/iManufacturer"
44 #define LEGACY_IPRODUCT_PATH LEGACY_ROOTPATH"/iProduct"
46 /* Functions in each config */
47 #define LEGACY_CONFIG_1_PATH LEGACY_ROOTPATH"/funcs_fconf"
48 #define LEGACY_CONFIG_2_PATH LEGACY_ROOTPATH"/funcs_sconf"
50 /* should be single char */
51 #define LEGACY_FUNC_SEP ","
54 #define LEGACY_ENABLE_PATH LEGACY_ROOTPATH"/enable"
55 #define LEGACY_ENABLE "1"
56 #define LEGACY_DISABLE "0"
58 static bool legacy_is_function_supported(struct usb_function *func)
62 snprintf (buf, sizeof(buf), "%s/f_%s", LEGACY_ROOTPATH, func->name);
63 if (access(buf, F_OK))
69 static bool legacy_is_gadget_supported(struct usb_gadget *gadget)
72 struct usb_configuration *config;
74 if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
77 if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
80 /* only strings in US_en are allowed */
81 if (gadget->strs.lang_code != DEFAULT_LANG)
84 if (!gadget->strs.manufacturer || !gadget->strs.product)
87 for (j = 0; gadget->configs[j]; ++j) {
88 config = gadget->configs[j];
93 for (i = 0; config->funcs[i]; ++i)
94 if (!legacy_is_function_supported(config->funcs[i]))
101 /* TODO. Maybe move this to sys ? */
102 static int legacy_set_int_hex(char *path, int val)
105 char buf[MAX_GADGET_STR_LEN];
110 snprintf(buf, sizeof(buf), "%x", val);
111 r = sys_set_str(path, buf);
118 static int legacy_set_gadget_attrs(struct usb_gadget_attrs *attrs)
122 ret = sys_set_int(LEGACY_CLASS_PATH, attrs->bDeviceClass);
126 ret = sys_set_int(LEGACY_SUBCLASS_PATH, attrs->bDeviceSubClass);
130 ret = sys_set_int(LEGACY_PROTOCOL_PATH, attrs->bDeviceProtocol);
134 ret = legacy_set_int_hex(LEGACY_ID_VENDOR_PATH, attrs->idVendor);
138 ret = legacy_set_int_hex(LEGACY_ID_PRODUCT_PATH, attrs->idProduct);
142 ret = legacy_set_int_hex(LEGACY_BCD_DEVICE_PATH, attrs->bcdDevice);
149 static int legacy_set_gadget_strs(struct usb_gadget_strings *strs)
153 if (!strs->manufacturer || !strs->product)
156 ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, strs->manufacturer);
160 ret = sys_set_str(LEGACY_IPRODUCT_PATH, strs->product);
164 /* The serial is written by the slp gadget kernel driver */
169 static int legacy_set_gadget_config(char *cpath,
170 struct usb_configuration *config)
172 char buf[MAX_GADGET_STR_LEN];
173 int left = sizeof(buf);
184 for (i = 0; config->funcs[i]; ++i) {
185 ret = snprintf(pos, left, "%s" LEGACY_FUNC_SEP,
186 config->funcs[i]->name);
194 /* eliminate last separator */
198 return sys_set_str(cpath, buf);
201 static int legacy_reconfigure_gadget(struct usb_gadget *gadget)
208 /* Verify the gadget and check if function is supported */
209 if (!legacy_is_gadget_supported(gadget))
212 ret = legacy_set_gadget_attrs(&gadget->attrs);
216 ret = legacy_set_gadget_strs(&gadget->strs);
220 ret = legacy_set_gadget_config(LEGACY_CONFIG_1_PATH, gadget->configs[0]);
224 ret = legacy_set_gadget_config(LEGACY_CONFIG_2_PATH, gadget->configs[1]);
231 static void legacy_start_stop_service_and_handler(bool start)
239 char *sep = LEGACY_FUNC_SEP;
240 char buf[MAX_GADGET_STR_LEN];
241 struct usb_function *func;
242 struct usb_function *funcs[USB_FUNCTION_IDX_MAX];
244 /* SLP gadget uses two USB configuration.
245 * (/sys/class/usb_mode/usb0/funcs_fconf and /sys/class/usb_mode/usb0/funcs_sconf)
247 * One usb function can be included in two configurations simultaneously.
248 * In this situation, a handler associated with function can be called twice for a usb function.
249 * To prevent duplicate calls,
250 * Collect all functions and remove duplicates and process them.
253 /* First configuration */
254 ret = sys_get_str(LEGACY_CONFIG_1_PATH, buf, sizeof(buf));
256 goto second_configuration;
258 /* Caution: buf ends with '\n' */
259 ptr = strchr(buf, '\n');
264 for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
265 func = find_usb_function_by_name(fname);
269 for (i = 0; i < n_func; i++)
270 if (funcs[i] == func)
273 if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
276 funcs[n_func] = func;
280 /* Second configuration */
281 second_configuration:
282 ret = sys_get_str(LEGACY_CONFIG_2_PATH, buf, sizeof(buf));
286 /* Caution: buf ends with '\n' */
287 ptr = strchr(buf, '\n');
292 for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
293 func = find_usb_function_by_name(fname);
297 for (i = 0; i < n_func; i++)
298 if (funcs[i] == func)
301 if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
304 funcs[n_func] = func;
308 for (i = 0; i < n_func; i++) {
310 if (funcs[i]->handler)
311 funcs[i]->handler(1);
313 if (funcs[i]->service)
314 (void)systemd_start_unit_wait_started(funcs[i]->service, ".service", -1);
316 if (funcs[i]->service && !funcs[i]->remain_after_disable)
317 (void)systemd_stop_unit_wait_stopped(funcs[i]->service, ".service", -1);
319 if (funcs[i]->handler)
320 funcs[i]->handler(0);
325 static int legacy_enable(void)
329 ret = sys_set_str(LEGACY_ENABLE_PATH, LEGACY_ENABLE);
333 legacy_start_stop_service_and_handler(true);
338 static int legacy_disable(void)
340 legacy_start_stop_service_and_handler(false);
342 return sys_set_str(LEGACY_ENABLE_PATH, LEGACY_DISABLE);
346 int hw_legacy_gadget_open(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
348 if (!usb_gadget_funcs)
351 /* check if slp usb gadget exists */
352 if (access("/sys/class/usb_mode/usb0/enable", F_OK))
355 usb_gadget_funcs->enable = legacy_enable;
356 usb_gadget_funcs->disable = legacy_disable;
357 usb_gadget_funcs->reconfigure_gadget = legacy_reconfigure_gadget;
363 int hw_legacy_gadget_close(hal_backend_usb_gadget_funcs *usb_gadget_funcs)