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 <sys/types.h>
22 #include <sys/mount.h>
23 #include <linux/limits.h>
25 #include <usbg/usbg.h>
26 #include <usbg/function/net.h>
28 #include <libsyscommon/libsystemd.h>
29 #include <hal/device/hal-usb_gadget-interface.h>
31 #include "usb_gadget.h"
32 #include "hal-backend-common.h"
34 #define CONFIGFS_PATH "/sys/kernel/config"
36 #define CONFIGFS_GADGET_NAME "hal-gadget"
37 #define CONFIGFS_CONFIG_LABEL "hal-config"
39 #define NAME_INSTANCE_SEP '.'
40 #define MAX_INSTANCE_LEN 512
42 #define USB_FUNCS_PATH "/dev/usb-funcs"
44 enum cfs_function_service_operation {
45 CFS_FUNCTION_SERVICE_START,
46 CFS_FUNCTION_SERVICE_STOP,
47 CFS_FUNCTION_SERVICE_POST_STOP,
56 /* Based on values in slp-gadget kernel module */
57 struct usbg_gadget_attrs default_g_attrs = {
58 .idVendor = DEFAULT_VID,
59 .idProduct = DEFAULT_PID,
60 .bcdDevice = DEFAULT_BCD_DEVICE,
63 struct usbg_gadget_strs default_g_strs = {
64 .manufacturer = DEFAULT_MANUFACTURER,
65 .product = DEFAULT_PRODUCT,
66 .serial = DEFAULT_SERIAL,
69 struct cfs_client *g_cfs_client;
71 static struct usb_function *cfs_find_usb_function(usbg_function *function)
74 char buf[MAX_INSTANCE_LEN];
75 const char *instance = usbg_get_function_instance(function);
76 const char *name = usbg_get_function_type_str(usbg_get_function_type(function));
78 /* Ex. name:"ffs", instance: "sdb.default" */
79 if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS)) == 0) {
80 strncpy(buf, instance, sizeof(buf) - 1);
81 buf[sizeof(buf) - 1] = '\0';
83 /* Ex. "sdb.default" ==> "sdb" + "default" */
84 sep = strchr(buf, NAME_INSTANCE_SEP);
93 return find_usb_function_by_name_instance(name, instance);
96 static bool cfs_is_function_supported(struct usb_function *func)
100 if (func->is_functionfs) {
101 /* functionfs must have a service */
105 snprintf(buf, sizeof(buf), "/usr/lib/systemd/system/%s.socket", func->service);
106 if (access(buf, F_OK))
109 if (usbg_lookup_function_type(func->name) < 0)
116 static bool cfs_is_gadget_supported(struct usb_gadget *gadget)
119 struct usb_configuration *config;
121 if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
124 if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
127 /* only strings in US_en are allowed */
128 if (gadget->strs.lang_code != DEFAULT_LANG)
131 if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
134 for (j = 0; gadget->configs[j]; ++j) {
135 config = gadget->configs[j];
140 for (i = 0; config->funcs[i]; ++i)
141 if (!cfs_is_function_supported(config->funcs[i]))
148 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
149 struct usb_gadget_attrs *attrs)
152 struct usbg_gadget_attrs gadget_attrs;
154 ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
158 gadget_attrs.bDeviceClass = attrs->bDeviceClass;
159 gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
160 gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
161 gadget_attrs.idVendor = attrs->idVendor;
162 gadget_attrs.idProduct = attrs->idProduct;
163 gadget_attrs.bcdDevice = attrs->bcdDevice;
165 ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
170 static int cfs_set_gadget_strs(struct cfs_client *cfs_client, struct usb_gadget_strings *strs)
174 if (!strs->manufacturer || !strs->product || !strs->serial)
177 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_MANUFACTURER, strs->lang_code, strs->manufacturer);
181 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_PRODUCT, strs->lang_code, strs->product);
185 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_SERIAL_NUMBER, strs->lang_code, strs->serial);
192 static int cfs_ensure_dir(char *path)
194 if (mkdir(path, 0770) < 0)
195 return (errno == EEXIST) ? 0 : -errno;
201 static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
206 const char *instance;
207 const char *dev_name;
208 char buf[MAX_INSTANCE_LEN];
210 if (!usb_func || !function)
213 if (usbg_get_function_type(function) != USBG_F_FFS)
216 name = usb_func->name;
217 service = usb_func->service;
218 instance = usb_func->instance;
219 dev_name = usbg_get_function_instance(function);
221 /* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default" + '0' */
222 if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3 > sizeof(buf))
223 return -ENAMETOOLONG;
225 /* mkdir /dev/usb-funcs */
226 ret = cfs_ensure_dir(USB_FUNCS_PATH);
230 /* mkdir /dev/usb-funcs/sdb */
231 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
232 ret = cfs_ensure_dir(buf);
236 /* mkdir /dev/usb-funcs/sdb/default */
237 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
238 ret = cfs_ensure_dir(buf);
242 /* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
243 ret = mount(dev_name, buf, "functionfs", 0, NULL);
247 /* start sdbd.socket */
248 ret = systemd_start_unit_wait_started(service, ".socket", -1);
258 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
261 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
264 rmdir(USB_FUNCS_PATH);
269 static int cfs_cleanup_ffs_service(usbg_function *function)
272 char buf[MAX_INSTANCE_LEN];
273 struct usb_function *usb_function;
278 usb_function = cfs_find_usb_function(function);
282 /* stop .socket first and stop .service later becuase of socket activation */
283 if (usb_function->service) {
284 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
285 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
288 /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
289 ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
301 /* remove /dev/usb-funcs/[sdb|mtp] directory */
302 ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
307 if (ret < 0 && errno != ENOTEMPTY)
310 /* remove /dev/usb-funcs/ directory */
311 ret = rmdir(USB_FUNCS_PATH);
312 if (ret < 0 && errno != ENOTEMPTY)
319 static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
322 struct ether_addr ethaddr;
323 struct usbg_gadget_strs strs;
324 struct usbg_f_net *nf = usbg_to_net_function(func);
329 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
330 if (ret != USBG_SUCCESS)
333 for (i = 0; i < ETHER_ADDR_LEN; i++)
334 ethaddr.ether_addr_octet[i] = 0;
336 for (i = 0; (i < 256) && strs.serial[i]; i++) {
337 ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
339 ethaddr.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
340 ethaddr.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
342 usbg_free_gadget_strs(&strs);
344 /* host_addr changes mac address */
345 ret = usbg_f_net_set_host_addr(nf, ðaddr);
350 static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
354 usbg_function *function;
356 /* delete all configs */
358 usbg_for_each_config(config, cfs_client->gadget) {
359 ret = usbg_rm_config(config, USBG_RM_RECURSE);
363 goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
366 /* delete all functions */
368 usbg_for_each_function(function, cfs_client->gadget) {
369 if (usbg_get_function_type(function) == USBG_F_FFS) {
370 ret = cfs_cleanup_ffs_service(function);
375 ret = usbg_rm_function(function, USBG_RM_RECURSE);
379 goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
385 static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
391 usbg_function *function;
392 struct usb_function *usb_func;
393 char instance[MAX_INSTANCE_LEN];
394 struct usbg_config_attrs cattrs = {
395 .bmAttributes = usb_config->attrs.bmAttributs,
396 .bMaxPower = usb_config->attrs.MaxPower/2,
399 if (!usb_config->funcs || !usb_config->funcs[0])
402 ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
406 if (usb_config->strs.config_str) {
407 ret = usbg_set_config_string(config, usb_config->strs.lang_code, usb_config->strs.config_str);
412 for (i = 0; usb_config->funcs[i]; ++i) {
413 usb_func = usb_config->funcs[i];
415 /* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
416 if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
417 return -ENAMETOOLONG;
419 /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
420 if (usb_func->is_functionfs) {
421 function_type = USBG_F_FFS;
422 snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
424 function_type = usbg_lookup_function_type(usb_func->name);
425 strncpy(instance, usb_func->instance, sizeof(instance) - 1);
426 instance[sizeof(instance) - 1] = '\0';
429 function = usbg_get_function(cfs_client->gadget, function_type, instance);
431 ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
435 /* Setting rndis mac address. This should be done at this point,
436 * since the node host_addr changes to read only after the function
437 * is added to config. */
438 if (usbg_get_function_type(function) == USBG_F_RNDIS)
439 (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
441 if (usbg_get_function_type(function) == USBG_F_FFS) {
442 ret = cfs_prep_ffs_service(usb_func, function);
448 ret = usbg_add_config_function(config, NULL, function);
456 static int cfs_reconfigure_gadget(struct usb_gadget *gadget)
461 if (!g_cfs_client || !gadget)
464 /* Verify the gadget and check if function is supported */
465 if (!cfs_is_gadget_supported(gadget))
468 ret = cfs_set_gadget_attrs(g_cfs_client, &gadget->attrs);
472 ret = cfs_set_gadget_strs(g_cfs_client, &gadget->strs);
476 ret = cfs_cleanup_all_config_and_function(g_cfs_client);
480 for (i = 0; gadget->configs[i]; ++i) {
481 ret = cfs_set_gadget_config(g_cfs_client, i + 1, gadget->configs[i]);
489 static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
491 usbg_function *function;
492 struct usb_function *usb_function;
494 usbg_for_each_function(function, gadget) {
495 usb_function = cfs_find_usb_function(function);
500 case CFS_FUNCTION_SERVICE_START:
501 if (usb_function->handler)
502 usb_function->handler(1);
504 /* functionfs service is automatically started by socket activation */
505 if (!usb_function->is_functionfs && usb_function->service)
506 (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
509 case CFS_FUNCTION_SERVICE_STOP:
510 if (!usb_function->is_functionfs && usb_function->service && !usb_function->remain_after_disable)
511 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
513 if (usb_function->handler)
514 usb_function->handler(0);
517 case CFS_FUNCTION_SERVICE_POST_STOP:
518 if (usb_function->is_functionfs && usb_function->service && !usb_function->remain_after_disable)
519 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
528 static int cfs_enable(void)
535 ret = usbg_enable_gadget(g_cfs_client->gadget, g_cfs_client->udc);
539 cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_START);
544 static int cfs_disable(void)
551 cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);
553 ret = usbg_disable_gadget(g_cfs_client->gadget); /* ignore error checking */
556 * Since functionfs service works with socket activation, you must stop it after disabling gadget.
557 * If usb data may come in after stopping functionfs service and before disabling gadget,
558 * functionfs service wakes up again by socket activation.
560 cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);
566 int hw_cfs_gadget_open(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
570 if (!usb_gadget_funcs)
573 g_cfs_client = calloc(1, sizeof(*g_cfs_client));
577 ret = usbg_init(CONFIGFS_PATH, &g_cfs_client->ctx);
581 g_cfs_client->udc = usbg_get_first_udc(g_cfs_client->ctx);
582 if (!g_cfs_client->udc) {
587 ret = usbg_create_gadget(g_cfs_client->ctx, CONFIGFS_GADGET_NAME,
588 &default_g_attrs, &default_g_strs, &g_cfs_client->gadget);
590 goto err_create_gadget;
592 usb_gadget_funcs->enable = cfs_enable;
593 usb_gadget_funcs->disable = cfs_disable;
594 usb_gadget_funcs->reconfigure_gadget = cfs_reconfigure_gadget;
600 usbg_cleanup(g_cfs_client->ctx);
608 int hw_cfs_gadget_close(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
610 usbg_function *function;
611 struct usb_function *usb_func;
613 if (!usb_gadget_funcs || !g_cfs_client)
616 usbg_for_each_function(function, g_cfs_client->gadget) {
617 usb_func = cfs_find_usb_function(function);
621 if (usb_func->is_functionfs && usb_func->service) {
622 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
623 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
628 * For now we don't check for errors
629 * but we should somehow handle them
631 usbg_rm_gadget(g_cfs_client->gadget, USBG_RM_RECURSE);
632 usbg_cleanup(g_cfs_client->ctx);