4 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <libsyscommon/dbus-systemd.h>
26 #include <hw/shared.h>
27 #include <hw/usb_gadget.h>
28 #include <hw/usb_client.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"
59 #define EXPORT __attribute__ ((visibility("default")))
62 static bool legacy_is_function_supported(struct usb_function *func)
66 snprintf (buf, sizeof(buf), "%s/f_%s", LEGACY_ROOTPATH, func->name);
67 if (access(buf, F_OK))
73 static bool legacy_is_gadget_supported(struct usb_gadget *gadget)
76 struct usb_configuration *config;
78 if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
81 if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
84 /* only strings in US_en are allowed */
85 if (gadget->strs.lang_code != DEFAULT_LANG)
88 if (!gadget->strs.manufacturer || !gadget->strs.product)
91 for (j = 0; gadget->configs[j]; ++j) {
92 config = gadget->configs[j];
97 for (i = 0; config->funcs[i]; ++i)
98 if (!legacy_is_function_supported(config->funcs[i]))
105 /* TODO. Maybe move this to sys ? */
106 static int legacy_set_int_hex(char *path, int val)
109 char buf[MAX_GADGET_STR_LEN];
114 snprintf(buf, sizeof(buf), "%x", val);
115 r = sys_set_str(path, buf);
122 static int legacy_set_gadget_attrs(struct usb_gadget_attrs *attrs)
126 ret = sys_set_int(LEGACY_CLASS_PATH, attrs->bDeviceClass);
130 ret = sys_set_int(LEGACY_SUBCLASS_PATH, attrs->bDeviceSubClass);
134 ret = sys_set_int(LEGACY_PROTOCOL_PATH, attrs->bDeviceProtocol);
138 ret = legacy_set_int_hex(LEGACY_ID_VENDOR_PATH, attrs->idVendor);
142 ret = legacy_set_int_hex(LEGACY_ID_PRODUCT_PATH, attrs->idProduct);
146 ret = legacy_set_int_hex(LEGACY_BCD_DEVICE_PATH, attrs->bcdDevice);
153 static int legacy_set_gadget_strs(struct usb_gadget_strings *strs)
157 if (!strs->manufacturer || !strs->product)
160 ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, strs->manufacturer);
164 ret = sys_set_str(LEGACY_IPRODUCT_PATH, strs->product);
168 /* The serial is written by the slp gadget kernel driver */
173 static int legacy_set_gadget_config(char *cpath,
174 struct usb_configuration *config)
176 char buf[MAX_GADGET_STR_LEN];
177 int left = sizeof(buf);
188 for (i = 0; config->funcs[i]; ++i) {
189 ret = snprintf(pos, left, "%s" LEGACY_FUNC_SEP,
190 config->funcs[i]->name);
198 /* eliminate last separator */
202 return sys_set_str(cpath, buf);
205 static int legacy_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *gadget)
212 /* Verify the gadget and check if function is supported */
213 if (!legacy_is_gadget_supported(gadget))
216 ret = legacy_set_gadget_attrs(&gadget->attrs);
220 ret = legacy_set_gadget_strs(&gadget->strs);
224 ret = legacy_set_gadget_config(LEGACY_CONFIG_1_PATH, gadget->configs[0]);
228 ret = legacy_set_gadget_config(LEGACY_CONFIG_2_PATH, gadget->configs[1]);
235 static void legacy_start_stop_service_and_handler(bool start)
243 char *sep = LEGACY_FUNC_SEP;
244 char buf[MAX_GADGET_STR_LEN];
245 struct usb_function *func;
246 struct usb_function *funcs[USB_FUNCTION_IDX_MAX];
248 /* SLP gadget uses two USB configuration.
249 * (/sys/class/usb_mode/usb0/funcs_fconf and /sys/class/usb_mode/usb0/funcs_sconf)
251 * One usb function can be included in two configurations simultaneously.
252 * In this situation, a handler associated with function can be called twice for a usb function.
253 * To prevent duplicate calls,
254 * Collect all functions and remove duplicates and process them.
257 /* First configuration */
258 ret = sys_get_str(LEGACY_CONFIG_1_PATH, buf, sizeof(buf));
260 goto second_configuration;
262 /* Caution: buf ends with '\n' */
263 ptr = strchr(buf, '\n');
268 for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
269 func = find_usb_function_by_name(fname);
273 for (i = 0; i < n_func; i++)
274 if (funcs[i] == func)
277 if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
280 funcs[n_func] = func;
284 /* Second configuration */
285 second_configuration:
286 ret = sys_get_str(LEGACY_CONFIG_2_PATH, buf, sizeof(buf));
290 /* Caution: buf ends with '\n' */
291 ptr = strchr(buf, '\n');
296 for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
297 func = find_usb_function_by_name(fname);
301 for (i = 0; i < n_func; i++)
302 if (funcs[i] == func)
305 if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
308 funcs[n_func] = func;
312 for (i = 0; i < n_func; i++) {
314 if (funcs[i]->handler)
315 funcs[i]->handler(1);
317 if (funcs[i]->service)
318 (void)systemd_start_unit_wait_started(funcs[i]->service, ".service", -1);
320 if (funcs[i]->service)
321 (void)systemd_stop_unit_wait_stopped(funcs[i]->service, ".service", -1);
323 if (funcs[i]->handler)
324 funcs[i]->handler(0);
329 static int legacy_enable(struct usb_client *usb)
333 ret = sys_set_str(LEGACY_ENABLE_PATH, LEGACY_ENABLE);
337 legacy_start_stop_service_and_handler(true);
342 static int legacy_disable(struct usb_client *usb)
345 legacy_start_stop_service_and_handler(false);
347 return sys_set_str(LEGACY_ENABLE_PATH, LEGACY_DISABLE);
351 int hw_legacy_gadget_open(struct hw_info *info,
352 const char *id, struct hw_common **common)
354 struct usb_client *legacy;
356 if (!info || !common)
359 /* check if slp usb gadget exists */
360 if (access("/sys/class/usb_mode/usb0/enable", F_OK))
363 legacy = calloc(1, sizeof(*legacy));
367 legacy->common.info = info;
369 legacy->reconfigure_gadget = legacy_reconfigure_gadget;
370 legacy->enable = legacy_enable;
371 legacy->disable = legacy_disable;
373 *common = &legacy->common;
378 int hw_legacy_gadget_close(struct hw_common *common)
380 struct usb_client *legacy;
385 legacy = container_of(common, struct usb_client,