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.
22 #include <sys/types.h>
23 #include <sys/mount.h>
25 #include <usbg/usbg.h>
26 #include <usbg/function/net.h>
27 #include <libsyscommon/dbus-systemd.h>
29 #include <hw/usb_gadget.h>
30 #include <hw/usb_client.h>
32 #define CONFIGFS_PATH "/sys/kernel/config"
34 #define CONFIGFS_GADGET_NAME "hal-gadget"
35 #define CONFIGFS_CONFIG_LABEL "hal-config"
37 #define NAME_INSTANCE_SEP '.'
38 #define MAX_INSTANCE_LEN 512
40 #define USB_FUNCS_PATH "/dev/usb-funcs"
43 #define EXPORT __attribute__ ((visibility("default")))
46 enum cfs_function_service_operation {
47 CFS_FUNCTION_SERVICE_START,
48 CFS_FUNCTION_SERVICE_STOP,
49 CFS_FUNCTION_SERVICE_POST_STOP,
53 struct usb_client client;
59 /* Based on values in slp-gadget kernel module */
60 struct usbg_gadget_attrs default_g_attrs = {
61 .idVendor = DEFAULT_VID,
62 .idProduct = DEFAULT_PID,
63 .bcdDevice = DEFAULT_BCD_DEVICE,
66 struct usbg_gadget_strs default_g_strs = {
67 .manufacturer = DEFAULT_MANUFACTURER,
68 .product = DEFAULT_PRODUCT,
69 .serial = DEFAULT_SERIAL,
72 static struct usb_function *cfs_find_usb_function(usbg_function *function)
75 char buf[MAX_INSTANCE_LEN];
76 const char *instance = usbg_get_function_instance(function);
77 const char *name = usbg_get_function_type_str(usbg_get_function_type(function));
79 /* Ex. name:"ffs", instance: "sdb.default" */
80 if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS)) == 0) {
81 strncpy(buf, instance, sizeof(buf) - 1);
82 buf[sizeof(buf) - 1] = '\0';
84 /* Ex. "sdb.default" ==> "sdb" + "default" */
85 sep = strchr(buf, NAME_INSTANCE_SEP);
94 return find_usb_function_by_name_instance(name, instance);
97 static bool cfs_is_function_supported(struct usb_client *usb,
98 struct usb_function *func)
103 if (!func->is_functionfs) {
104 ret = usbg_lookup_function_type(func->name);
107 /* TODO: Check if socket is available */
114 static bool cfs_is_gadget_supported(struct usb_client *usb, struct usb_gadget *gadget)
118 if (!gadget || !gadget->configs || !gadget->funcs)
121 /* only strings in US_en are allowed */
122 if (gadget->strs.lang_code != DEFAULT_LANG)
125 if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
128 for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
129 struct usb_configuration *config = gadget->configs[j];
134 for (i = 0; config->funcs[i]; ++i)
135 if (!cfs_is_function_supported(usb, config->funcs[i]))
145 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
146 struct usb_gadget_attrs *attrs)
149 struct usbg_gadget_attrs gadget_attrs;
151 ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
155 gadget_attrs.bDeviceClass = attrs->bDeviceClass;
156 gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
157 gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
158 gadget_attrs.idVendor = attrs->idVendor;
159 gadget_attrs.idProduct = attrs->idProduct;
160 gadget_attrs.bcdDevice = attrs->bcdDevice;
162 ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
167 static int cfs_set_gadget_strs(struct cfs_client *cfs_client, struct usb_gadget_strings *strs)
171 if (!strs->manufacturer || !strs->product || !strs->serial)
174 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_MANUFACTURER, strs->lang_code, strs->manufacturer);
178 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_PRODUCT, strs->lang_code, strs->product);
182 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_SERIAL_NUMBER, strs->lang_code, strs->serial);
189 static int cfs_ensure_dir(char *path)
191 if (mkdir(path, 0770) < 0)
192 return (errno == EEXIST) ? 0 : -errno;
198 static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
203 const char *instance;
204 const char *dev_name;
205 char buf[MAX_INSTANCE_LEN];
207 if (!usb_func || !function)
210 if (usbg_get_function_type(function) != USBG_F_FFS)
213 name = usb_func->name;
214 service = usb_func->service;
215 instance = usb_func->instance;
216 dev_name = usbg_get_function_instance(function);
218 /* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default" + '0' */
219 if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3 > sizeof(buf))
220 return -ENAMETOOLONG;
222 /* mkdir /dev/usb-funcs */
223 ret = cfs_ensure_dir(USB_FUNCS_PATH);
227 /* mkdir /dev/usb-funcs/sdb */
228 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
229 ret = cfs_ensure_dir(buf);
233 /* mkdir /dev/usb-funcs/sdb/default */
234 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
235 ret = cfs_ensure_dir(buf);
239 /* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
240 ret = mount(dev_name, buf, "functionfs", 0, NULL);
244 /* start sdbd.socket */
245 ret = systemd_start_unit_wait_started(service, ".socket", -1);
255 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
258 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
261 rmdir(USB_FUNCS_PATH);
266 static int cfs_cleanup_ffs_service(usbg_function *function)
269 char buf[MAX_INSTANCE_LEN];
270 struct usb_function *usb_function;
275 usb_function = cfs_find_usb_function(function);
279 /* stop .socket first and stop .service later becuase of socket activation */
280 if (usb_function->service) {
281 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
282 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
285 /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
286 ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
298 /* remove /dev/usb-funcs/[sdb|mtp] directory */
299 ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
304 if (ret < 0 && errno != ENOTEMPTY)
307 /* remove /dev/usb-funcs/ directory */
308 ret = rmdir(USB_FUNCS_PATH);
309 if (ret < 0 && errno != ENOTEMPTY)
316 static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
319 struct ether_addr ethaddr;
320 struct usbg_gadget_strs strs;
321 struct usbg_f_net *nf = usbg_to_net_function(func);
326 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
327 if (ret != USBG_SUCCESS)
330 for (i = 0; i < ETHER_ADDR_LEN; i++)
331 ethaddr.ether_addr_octet[i] = 0;
333 for (i = 0; (i < 256) && strs.serial[i]; i++) {
334 ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
336 ethaddr.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
337 ethaddr.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
339 usbg_free_gadget_strs(&strs);
341 /* host_addr changes mac address */
342 ret = usbg_f_net_set_host_addr(nf, ðaddr);
347 static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
351 usbg_function *function;
353 /* delete all configs */
355 usbg_for_each_config(config, cfs_client->gadget) {
356 ret = usbg_rm_config(config, USBG_RM_RECURSE);
360 goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
363 /* delete all functions */
365 usbg_for_each_function(function, cfs_client->gadget) {
366 if (usbg_get_function_type(function) == USBG_F_FFS) {
367 ret = cfs_cleanup_ffs_service(function);
372 ret = usbg_rm_function(function, USBG_RM_RECURSE);
376 goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
382 static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
388 usbg_function *function;
389 struct usb_function *usb_func;
390 char instance[MAX_INSTANCE_LEN];
391 struct usbg_config_attrs cattrs = {
392 .bmAttributes = usb_config->attrs.bmAttributs,
393 .bMaxPower = usb_config->attrs.MaxPower/2,
396 if (!usb_config->funcs || !usb_config->funcs[0])
399 ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
403 if (usb_config->strs.config_str) {
404 ret = usbg_set_config_string(config, usb_config->strs.lang_code, usb_config->strs.config_str);
409 for (i = 0; usb_config->funcs[i]; ++i) {
410 usb_func = usb_config->funcs[i];
412 /* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
413 if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
414 return -ENAMETOOLONG;
416 /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
417 if (usb_func->is_functionfs) {
418 function_type = USBG_F_FFS;
419 snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
421 function_type = usbg_lookup_function_type(usb_func->name);
422 strncpy(instance, usb_func->instance, sizeof(instance) - 1);
423 instance[sizeof(instance) - 1] = '\0';
426 function = usbg_get_function(cfs_client->gadget, function_type, instance);
428 ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
432 /* Setting rndis mac address. This should be done at this point,
433 * since the node host_addr changes to read only after the function
434 * is added to config. */
435 if (usbg_get_function_type(function) == USBG_F_RNDIS)
436 (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
438 if (usbg_get_function_type(function) == USBG_F_FFS) {
439 ret = cfs_prep_ffs_service(usb_func, function);
445 ret = usbg_add_config_function(config, NULL, function);
453 static int cfs_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *gadget)
457 struct cfs_client *cfs_client;
459 if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
462 cfs_client = container_of(usb, struct cfs_client, client);
464 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
468 ret = cfs_set_gadget_strs(cfs_client, &gadget->strs);
472 ret = cfs_cleanup_all_config_and_function(cfs_client);
476 for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
477 ret = cfs_set_gadget_config(cfs_client, i + 1, gadget->configs[i]);
485 static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
487 usbg_function *function;
488 struct usb_function *usb_function;
490 usbg_for_each_function(function, gadget) {
491 usb_function = cfs_find_usb_function(function);
496 case CFS_FUNCTION_SERVICE_START:
497 if (usb_function->handler)
498 usb_function->handler(1);
500 /* functionfs service is automatically started by socket activation */
501 if (!usb_function->is_functionfs && usb_function->service)
502 (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
505 case CFS_FUNCTION_SERVICE_STOP:
506 if (!usb_function->is_functionfs && usb_function->service)
507 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
509 if (usb_function->handler)
510 usb_function->handler(0);
513 case CFS_FUNCTION_SERVICE_POST_STOP:
514 if (usb_function->is_functionfs && usb_function->service)
515 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
524 static int cfs_enable(struct usb_client *usb)
527 struct cfs_client *cfs_client;
532 cfs_client = container_of(usb, struct cfs_client, client);
534 ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
538 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_START);
543 static int cfs_disable(struct usb_client *usb)
546 struct cfs_client *cfs_client;
551 cfs_client = container_of(usb, struct cfs_client, client);
553 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);
555 ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */
558 * Since functionfs service works with socket activation, you must stop it after disabling gadget.
559 * If usb data may come in after stopping functionfs service and before disabling gadget,
560 * functionfs service wakes up again by socket activation.
562 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);
568 int hw_cfs_gadget_open(struct hw_info *info,
569 const char *id, struct hw_common **common)
571 struct cfs_client *cfs_client;
574 if (!info || !common)
577 cfs_client = calloc(1, sizeof(*cfs_client));
581 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
585 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
586 if (!cfs_client->udc) {
591 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
592 &default_g_attrs, &default_g_strs, &cfs_client->gadget);
594 goto err_create_gadget;
596 cfs_client->client.common.info = info;
598 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
599 cfs_client->client.enable = cfs_enable;
600 cfs_client->client.disable = cfs_disable;
602 *common = &cfs_client->client.common;
608 usbg_cleanup(cfs_client->ctx);
616 int hw_cfs_gadget_close(struct hw_common *common)
618 usbg_function *function;
619 struct cfs_client *cfs_client;
620 struct usb_function *usb_func;
625 cfs_client = container_of(common, struct cfs_client, client.common);
627 usbg_for_each_function(function, cfs_client->gadget) {
628 usb_func = cfs_find_usb_function(function);
632 if (usb_func->is_functionfs && usb_func->service) {
633 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
634 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
639 * For now we don't check for errors
640 * but we should somehow handle them
642 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
643 usbg_cleanup(cfs_client->ctx);