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_client *usb,
63 struct usb_function *func)
67 * Instead of only checking whether we know this function
68 * we should also parse sysfs to check if it is build into
71 if (find_usb_function_by_name(func->name))
77 static bool legacy_is_gadget_supported(struct usb_client *usb,
78 struct usb_gadget *gadget)
82 if (!gadget || !gadget->configs || !gadget->funcs)
86 /* only strings in US_en are allowed */
87 if (gadget->strs[0].lang_code != DEFAULT_LANG || gadget->strs[1].lang_code)
91 for (j = 0; gadget->configs[j]; ++j) {
92 struct usb_configuration *config = gadget->configs[j];
94 if (config->strs && config->strs[0].lang_code)
100 for (i = 0; config->funcs[i]; ++i)
101 if (!legacy_is_function_supported(usb, config->funcs[i]))
111 /* TODO. Maybe move this to sys ? */
112 static int legacy_set_int_hex(char *path, int val)
114 char buf[MAX_GADGET_STR_LEN];
120 snprintf(buf, sizeof(buf), "%x", val);
121 r = sys_set_str(path, buf);
128 static int legacy_set_gadget_attrs(struct usb_gadget_attrs *attrs)
132 ret = sys_set_int(LEGACY_CLASS_PATH, attrs->bDeviceClass);
136 ret = sys_set_int(LEGACY_SUBCLASS_PATH, attrs->bDeviceSubClass);
140 ret = sys_set_int(LEGACY_PROTOCOL_PATH, attrs->bDeviceProtocol);
144 ret = legacy_set_int_hex(LEGACY_ID_VENDOR_PATH, attrs->idVendor);
148 ret = legacy_set_int_hex(LEGACY_ID_PRODUCT_PATH, attrs->idProduct);
152 ret = legacy_set_int_hex(LEGACY_BCD_DEVICE_PATH, attrs->bcdDevice);
157 static int legacy_set_gadget_strs(struct usb_gadget_strings *strs)
161 /* SLP gadget's kernel driver writes serial number directly */
163 if (strs->manufacturer) {
164 ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, strs->manufacturer);
170 ret = sys_set_str(LEGACY_IPRODUCT_PATH, strs->product);
178 static int legacy_set_gadget_config(char *cpath,
179 struct usb_configuration *config)
181 char buf[MAX_GADGET_STR_LEN];
182 int left = sizeof(buf);
193 for (i = 0; config->funcs[i]; ++i) {
194 ret = snprintf(pos, left, "%s" LEGACY_FUNC_SEP,
195 config->funcs[i]->name);
203 /* eliminate last separator */
207 return sys_set_str(cpath, buf);
210 static int legacy_reconfigure_gadget(struct usb_client *usb,
211 struct usb_gadget *gadget)
215 if (!usb || !gadget || !legacy_is_gadget_supported(usb, gadget))
218 ret = legacy_set_gadget_attrs(&gadget->attrs);
223 ret = legacy_set_gadget_strs(gadget->strs + 0);
228 ret = legacy_set_gadget_config(LEGACY_CONFIG_1_PATH, gadget->configs[0]);
232 ret = legacy_set_gadget_config(LEGACY_CONFIG_2_PATH, gadget->configs[1]);
237 static void legacy_start_stop_service_and_handler(bool start)
245 char *sep = LEGACY_FUNC_SEP;
246 char buf[MAX_GADGET_STR_LEN];
247 struct usb_function *func;
248 struct usb_function *funcs[USB_FUNCTION_IDX_MAX];
250 /* SLP gadget uses two USB configuration.
251 * (/sys/class/usb_mode/usb0/funcs_fconf and /sys/class/usb_mode/usb0/funcs_sconf)
253 * One usb function can be included in two configurations simultaneously.
254 * In this situation, a handler associated with function can be called twice for a usb function.
255 * To prevent duplicate calls,
256 * Collect all functions and remove duplicates and process them.
259 /* First configuration */
260 ret = sys_get_str(LEGACY_CONFIG_1_PATH, buf, sizeof(buf));
262 goto second_configuration;
264 /* Caution: buf ends with '\n' */
265 ptr = strchr(buf, '\n');
270 for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
271 func = find_usb_function_by_name(fname);
275 for (i = 0; i < n_func; i++)
276 if (funcs[i] == func)
279 if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
282 funcs[n_func] = func;
286 /* Second configuration */
287 second_configuration:
288 ret = sys_get_str(LEGACY_CONFIG_2_PATH, buf, sizeof(buf));
292 /* Caution: buf ends with '\n' */
293 ptr = strchr(buf, '\n');
298 for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
299 func = find_usb_function_by_name(fname);
303 for (i = 0; i < n_func; i++)
304 if (funcs[i] == func)
307 if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
310 funcs[n_func] = func;
314 for (i = 0; i < n_func; i++) {
316 if (funcs[i]->handler)
317 funcs[i]->handler(1);
319 if (funcs[i]->service)
320 (void)systemd_start_unit_wait_started(funcs[i]->service, ".service", -1);
322 if (funcs[i]->service)
323 (void)systemd_stop_unit_wait_stopped(funcs[i]->service, ".service", -1);
325 if (funcs[i]->handler)
326 funcs[i]->handler(0);
331 static int legacy_enable(struct usb_client *usb)
335 ret = sys_set_str(LEGACY_ENABLE_PATH, LEGACY_ENABLE);
339 legacy_start_stop_service_and_handler(true);
344 static int legacy_disable(struct usb_client *usb)
347 legacy_start_stop_service_and_handler(false);
349 return sys_set_str(LEGACY_ENABLE_PATH, LEGACY_DISABLE);
353 int hw_legacy_gadget_open(struct hw_info *info,
354 const char *id, struct hw_common **common)
356 struct usb_client *legacy;
358 if (!info || !common)
361 /* check if slp usb gadget exists */
362 if (access("/sys/class/usb_mode/usb0/enable", F_OK))
365 legacy = calloc(1, sizeof(*legacy));
369 legacy->common.info = info;
371 legacy->reconfigure_gadget = legacy_reconfigure_gadget;
372 legacy->enable = legacy_enable;
373 legacy->disable = legacy_disable;
375 *common = &legacy->common;
380 int hw_legacy_gadget_close(struct hw_common *common)
382 struct usb_client *legacy;
387 legacy = container_of(common, struct usb_client,