-/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <errno.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/mount.h>
-#include <linux/limits.h>
-
-#include <usbg/usbg.h>
-#include <usbg/function/net.h>
-
-#include <libsyscommon/libsystemd.h>
-#include <hal/device/hal-usb_gadget-interface.h>
-
-#include "usb_gadget.h"
-#include "hal-backend-common.h"
-
-#define CONFIGFS_PATH "/sys/kernel/config"
-
-#define CONFIGFS_GADGET_NAME "hal-gadget"
-#define CONFIGFS_CONFIG_LABEL "hal-config"
-
-#define NAME_INSTANCE_SEP '.'
-#define MAX_INSTANCE_LEN 512
-
-#define USB_FUNCS_PATH "/dev/usb-funcs"
-
-enum cfs_function_service_operation {
- CFS_FUNCTION_SERVICE_START,
- CFS_FUNCTION_SERVICE_STOP,
- CFS_FUNCTION_SERVICE_POST_STOP,
-};
-
-struct cfs_client {
- usbg_state *ctx;
- usbg_gadget *gadget;
- usbg_udc *udc;
-};
-
-/* Based on values in slp-gadget kernel module */
-struct usbg_gadget_attrs default_g_attrs = {
- .idVendor = DEFAULT_VID,
- .idProduct = DEFAULT_PID,
- .bcdDevice = DEFAULT_BCD_DEVICE,
-};
-
-struct usbg_gadget_strs default_g_strs = {
- .manufacturer = DEFAULT_MANUFACTURER,
- .product = DEFAULT_PRODUCT,
- .serial = DEFAULT_SERIAL,
-};
-
-struct cfs_client *g_cfs_client;
-
-static struct usb_function *cfs_find_usb_function(usbg_function *function)
-{
- char *sep;
- char buf[MAX_INSTANCE_LEN];
- const char *instance = usbg_get_function_instance(function);
- const char *name = usbg_get_function_type_str(usbg_get_function_type(function));
-
- /* Ex. name:"ffs", instance: "sdb.default" */
- if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS)) == 0) {
- strncpy(buf, instance, sizeof(buf) - 1);
- buf[sizeof(buf) - 1] = '\0';
-
- /* Ex. "sdb.default" ==> "sdb" + "default" */
- sep = strchr(buf, NAME_INSTANCE_SEP);
- if (!sep || !sep[1])
- return NULL;
- *sep = '\0';
-
- name = buf;
- instance = sep + 1;
- }
-
- return find_usb_function_by_name_instance(name, instance);
-}
-
-static bool cfs_is_function_supported(struct usb_function *func)
-{
- char buf[PATH_MAX];
-
- if (func->is_functionfs) {
- /* functionfs must have a service */
- if (!func->service)
- return false;
-
- snprintf(buf, sizeof(buf), "/usr/lib/systemd/system/%s.socket", func->service);
- if (access(buf, F_OK))
- return false;
- } else {
- if (usbg_lookup_function_type(func->name) < 0)
- return false;
- }
-
- return true;
-}
-
-static bool cfs_is_gadget_supported(struct usb_gadget *gadget)
-{
- int i, j;
- struct usb_configuration *config;
-
- if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
- return false;
-
- if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
- return false;
-
- /* only strings in US_en are allowed */
- if (gadget->strs.lang_code != DEFAULT_LANG)
- return false;
-
- if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
- return false;
-
- for (j = 0; gadget->configs[j]; ++j) {
- config = gadget->configs[j];
-
- if (!config->funcs)
- return false;
-
- for (i = 0; config->funcs[i]; ++i)
- if (!cfs_is_function_supported(config->funcs[i]))
- return false;
- }
-
- return true;
-}
-
-static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
- struct usb_gadget_attrs *attrs)
-{
- int ret;
- struct usbg_gadget_attrs gadget_attrs;
-
- ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
- if (ret)
- return ret;
-
- gadget_attrs.bDeviceClass = attrs->bDeviceClass;
- gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
- gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
- gadget_attrs.idVendor = attrs->idVendor;
- gadget_attrs.idProduct = attrs->idProduct;
- gadget_attrs.bcdDevice = attrs->bcdDevice;
-
- ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
-
- return ret;
-}
-
-static int cfs_set_gadget_strs(struct cfs_client *cfs_client, struct usb_gadget_strings *strs)
-{
- int ret;
-
- if (!strs->manufacturer || !strs->product || !strs->serial)
- return -EINVAL;
-
- ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_MANUFACTURER, strs->lang_code, strs->manufacturer);
- if (ret)
- return ret;
-
- ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_PRODUCT, strs->lang_code, strs->product);
- if (ret)
- return ret;
-
- ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_SERIAL_NUMBER, strs->lang_code, strs->serial);
- if (ret)
- return ret;
-
- return 0;
-}
-
-static int cfs_ensure_dir(char *path)
-{
- if (mkdir(path, 0770) < 0)
- return (errno == EEXIST) ? 0 : -errno;
-
- return 0;
-}
-
-
-static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
-{
- int ret;
- const char *name;
- const char *service;
- const char *instance;
- const char *dev_name;
- char buf[MAX_INSTANCE_LEN];
-
- if (!usb_func || !function)
- return -EINVAL;
-
- if (usbg_get_function_type(function) != USBG_F_FFS)
- return -EINVAL;
-
- name = usb_func->name;
- service = usb_func->service;
- instance = usb_func->instance;
- dev_name = usbg_get_function_instance(function);
-
- /* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default" + '0' */
- if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3 > sizeof(buf))
- return -ENAMETOOLONG;
-
- /* mkdir /dev/usb-funcs */
- ret = cfs_ensure_dir(USB_FUNCS_PATH);
- if (ret < 0)
- return ret;
-
- /* mkdir /dev/usb-funcs/sdb */
- snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
- ret = cfs_ensure_dir(buf);
- if (ret < 0)
- goto out_rmdir;
-
- /* mkdir /dev/usb-funcs/sdb/default */
- snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
- ret = cfs_ensure_dir(buf);
- if (ret < 0)
- goto out_rmdir;
-
- /* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
- ret = mount(dev_name, buf, "functionfs", 0, NULL);
- if (ret < 0)
- goto out_rmdir;
-
- /* start sdbd.socket */
- ret = systemd_start_unit_wait_started(service, ".socket", -1);
- if (ret < 0)
- goto out_unmount;
-
- return 0;
-
-out_unmount:
- umount(buf);
-
-out_rmdir:
- snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
- rmdir(buf);
-
- snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
- rmdir(buf);
-
- rmdir(USB_FUNCS_PATH);
-
- return ret;
-}
-
-static int cfs_cleanup_ffs_service(usbg_function *function)
-{
- int ret;
- char buf[MAX_INSTANCE_LEN];
- struct usb_function *usb_function;
-
- if (!function)
- return -EINVAL;
-
- usb_function = cfs_find_usb_function(function);
- if (!usb_function)
- return -ENOENT;
-
- /* stop .socket first and stop .service later becuase of socket activation */
- if (usb_function->service) {
- (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
- (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
- }
-
- /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
- ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
- if (ret < 0)
- return ret;
-
- ret = umount(buf);
- if (ret < 0)
- return ret;
-
- ret = rmdir(buf);
- if (ret < 0)
- return ret;
-
- /* remove /dev/usb-funcs/[sdb|mtp] directory */
- ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
- if (ret < 0)
- return ret;
-
- ret = rmdir(buf);
- if (ret < 0 && errno != ENOTEMPTY)
- return ret;
-
- /* remove /dev/usb-funcs/ directory */
- ret = rmdir(USB_FUNCS_PATH);
- if (ret < 0 && errno != ENOTEMPTY)
- return ret;
-
- return 0;
-}
-
-
-static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
-{
- int i, ret;
- struct ether_addr ethaddr;
- struct usbg_gadget_strs strs;
- struct usbg_f_net *nf = usbg_to_net_function(func);
-
- if (!nf)
- return -EINVAL;
-
- ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
- if (ret != USBG_SUCCESS)
- return ret;
-
- for (i = 0; i < ETHER_ADDR_LEN; i++)
- ethaddr.ether_addr_octet[i] = 0;
-
- for (i = 0; (i < 256) && strs.serial[i]; i++) {
- ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
- }
- ethaddr.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
- ethaddr.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
-
- usbg_free_gadget_strs(&strs);
-
- /* host_addr changes mac address */
- ret = usbg_f_net_set_host_addr(nf, ðaddr);
-
- return ret;
-}
-
-static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
-{
- int ret;
- usbg_config *config;
- usbg_function *function;
-
- /* delete all configs */
-restart_rm_config:
- usbg_for_each_config(config, cfs_client->gadget) {
- ret = usbg_rm_config(config, USBG_RM_RECURSE);
- if (ret)
- return ret;
-
- goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
- }
-
- /* delete all functions */
-restart_rm_function:
- usbg_for_each_function(function, cfs_client->gadget) {
- if (usbg_get_function_type(function) == USBG_F_FFS) {
- ret = cfs_cleanup_ffs_service(function);
- if (ret)
- return ret;
- }
-
- ret = usbg_rm_function(function, USBG_RM_RECURSE);
- if (ret)
- return ret;
-
- goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
- }
-
- return 0;
-}
-
-static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
-{
- int i;
- int ret;
- int function_type;
- usbg_config *config;
- usbg_function *function;
- struct usb_function *usb_func;
- char instance[MAX_INSTANCE_LEN];
- struct usbg_config_attrs cattrs = {
- .bmAttributes = usb_config->attrs.bmAttributs,
- .bMaxPower = usb_config->attrs.MaxPower/2,
- };
-
- if (!usb_config->funcs || !usb_config->funcs[0])
- return -EINVAL;
-
- ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
- if (ret)
- return ret;
-
- if (usb_config->strs.config_str) {
- ret = usbg_set_config_string(config, usb_config->strs.lang_code, usb_config->strs.config_str);
- if (ret)
- return ret;
- }
-
- for (i = 0; usb_config->funcs[i]; ++i) {
- usb_func = usb_config->funcs[i];
-
- /* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
- if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
- return -ENAMETOOLONG;
-
- /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
- if (usb_func->is_functionfs) {
- function_type = USBG_F_FFS;
- snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
- } else {
- function_type = usbg_lookup_function_type(usb_func->name);
- strncpy(instance, usb_func->instance, sizeof(instance) - 1);
- instance[sizeof(instance) - 1] = '\0';
- }
-
- function = usbg_get_function(cfs_client->gadget, function_type, instance);
- if (!function) {
- ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
- if (ret)
- return ret;
-
- /* Setting rndis mac address. This should be done at this point,
- * since the node host_addr changes to read only after the function
- * is added to config. */
- if (usbg_get_function_type(function) == USBG_F_RNDIS)
- (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
-
- if (usbg_get_function_type(function) == USBG_F_FFS) {
- ret = cfs_prep_ffs_service(usb_func, function);
- if (ret)
- return ret;
- }
- }
-
- ret = usbg_add_config_function(config, NULL, function);
- if (ret)
- return ret;
- }
-
- return 0;
-}
-
-static int cfs_reconfigure_gadget(struct usb_gadget *gadget)
-{
- int i;
- int ret;
-
- if (!g_cfs_client || !gadget)
- return -EINVAL;
-
- /* Verify the gadget and check if function is supported */
- if (!cfs_is_gadget_supported(gadget))
- return -ENOTSUP;
-
- ret = cfs_set_gadget_attrs(g_cfs_client, &gadget->attrs);
- if (ret)
- return ret;
-
- ret = cfs_set_gadget_strs(g_cfs_client, &gadget->strs);
- if (ret)
- return ret;
-
- ret = cfs_cleanup_all_config_and_function(g_cfs_client);
- if (ret)
- return ret;
-
- for (i = 0; gadget->configs[i]; ++i) {
- ret = cfs_set_gadget_config(g_cfs_client, i + 1, gadget->configs[i]);
- if (ret)
- return ret;
- }
-
- return 0;
-}
-
-static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
-{
- usbg_function *function;
- struct usb_function *usb_function;
-
- usbg_for_each_function(function, gadget) {
- usb_function = cfs_find_usb_function(function);
- if (!usb_function)
- continue;
-
- switch(operation) {
- case CFS_FUNCTION_SERVICE_START:
- if (usb_function->handler)
- usb_function->handler(1);
-
- /* functionfs service is automatically started by socket activation */
- if (!usb_function->is_functionfs && usb_function->service)
- (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
- break;
-
- case CFS_FUNCTION_SERVICE_STOP:
- if (!usb_function->is_functionfs && usb_function->service && !usb_function->remain_after_disable)
- (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
-
- if (usb_function->handler)
- usb_function->handler(0);
- break;
-
- case CFS_FUNCTION_SERVICE_POST_STOP:
- if (usb_function->is_functionfs && usb_function->service && !usb_function->remain_after_disable)
- (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
- break;
-
- default:
- break;
- }
- }
-}
-
-static int cfs_enable(void)
-{
- int ret;
-
- if (!g_cfs_client)
- return -EINVAL;
-
- ret = usbg_enable_gadget(g_cfs_client->gadget, g_cfs_client->udc);
- if (ret)
- return ret;
-
- cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_START);
-
- return 0;
-}
-
-static int cfs_disable(void)
-{
- int ret;
-
- if (!g_cfs_client)
- return -EINVAL;
-
- cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);
-
- ret = usbg_disable_gadget(g_cfs_client->gadget); /* ignore error checking */
-
- /*
- * Since functionfs service works with socket activation, you must stop it after disabling gadget.
- * If usb data may come in after stopping functionfs service and before disabling gadget,
- * functionfs service wakes up again by socket activation.
- */
- cfs_start_stop_service_and_handler(g_cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);
-
- return ret;
-}
-
-EXPORT
-int hw_cfs_gadget_open(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
-{
- int ret;
-
- if (!usb_gadget_funcs)
- return -EINVAL;
-
- g_cfs_client = calloc(1, sizeof(*g_cfs_client));
- if (!g_cfs_client)
- return -ENOMEM;
-
- ret = usbg_init(CONFIGFS_PATH, &g_cfs_client->ctx);
- if (ret)
- goto err_usbg_init;
-
- g_cfs_client->udc = usbg_get_first_udc(g_cfs_client->ctx);
- if (!g_cfs_client->udc) {
- ret = -ENODEV;
- goto err_no_udc;
- }
-
- ret = usbg_create_gadget(g_cfs_client->ctx, CONFIGFS_GADGET_NAME,
- &default_g_attrs, &default_g_strs, &g_cfs_client->gadget);
- if (ret)
- goto err_create_gadget;
-
- usb_gadget_funcs->enable = cfs_enable;
- usb_gadget_funcs->disable = cfs_disable;
- usb_gadget_funcs->reconfigure_gadget = cfs_reconfigure_gadget;
-
- return 0;
-
-err_create_gadget:
-err_no_udc:
- usbg_cleanup(g_cfs_client->ctx);
-err_usbg_init:
- free(g_cfs_client);
-
- return ret;
-}
-
-EXPORT
-int hw_cfs_gadget_close(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
-{
- usbg_function *function;
- struct usb_function *usb_func;
-
- if (!usb_gadget_funcs || !g_cfs_client)
- return -EINVAL;
-
- usbg_for_each_function(function, g_cfs_client->gadget) {
- usb_func = cfs_find_usb_function(function);
- if (!usb_func)
- continue;
-
- if (usb_func->is_functionfs && usb_func->service) {
- (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
- (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
- }
- }
-
- /*
- * For now we don't check for errors
- * but we should somehow handle them
- */
- usbg_rm_gadget(g_cfs_client->gadget, USBG_RM_RECURSE);
- usbg_cleanup(g_cfs_client->ctx);
- free(g_cfs_client);
-
- return 0;
-}
-