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_function *func)
101 if (func->is_functionfs) {
102 /* functionfs must have a service */
106 snprintf(buf, sizeof(buf), "/usr/lib/systemd/system/%s.socket", func->service);
107 if (access(buf, F_OK))
110 if (usbg_lookup_function_type(func->name) < 0)
117 static bool cfs_is_gadget_supported(struct usb_gadget *gadget)
120 struct usb_configuration *config;
122 if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
125 if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
128 /* only strings in US_en are allowed */
129 if (gadget->strs.lang_code != DEFAULT_LANG)
132 if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
135 for (j = 0; gadget->configs[j]; ++j) {
136 config = gadget->configs[j];
141 for (i = 0; config->funcs[i]; ++i)
142 if (!cfs_is_function_supported(config->funcs[i]))
149 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
150 struct usb_gadget_attrs *attrs)
153 struct usbg_gadget_attrs gadget_attrs;
155 ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
159 gadget_attrs.bDeviceClass = attrs->bDeviceClass;
160 gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
161 gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
162 gadget_attrs.idVendor = attrs->idVendor;
163 gadget_attrs.idProduct = attrs->idProduct;
164 gadget_attrs.bcdDevice = attrs->bcdDevice;
166 ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
171 static int cfs_set_gadget_strs(struct cfs_client *cfs_client, struct usb_gadget_strings *strs)
175 if (!strs->manufacturer || !strs->product || !strs->serial)
178 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_MANUFACTURER, strs->lang_code, strs->manufacturer);
182 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_PRODUCT, strs->lang_code, strs->product);
186 ret = usbg_set_gadget_str(cfs_client->gadget, USBG_STR_SERIAL_NUMBER, strs->lang_code, strs->serial);
193 static int cfs_ensure_dir(char *path)
195 if (mkdir(path, 0770) < 0)
196 return (errno == EEXIST) ? 0 : -errno;
202 static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
207 const char *instance;
208 const char *dev_name;
209 char buf[MAX_INSTANCE_LEN];
211 if (!usb_func || !function)
214 if (usbg_get_function_type(function) != USBG_F_FFS)
217 name = usb_func->name;
218 service = usb_func->service;
219 instance = usb_func->instance;
220 dev_name = usbg_get_function_instance(function);
222 /* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default" + '0' */
223 if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3 > sizeof(buf))
224 return -ENAMETOOLONG;
226 /* mkdir /dev/usb-funcs */
227 ret = cfs_ensure_dir(USB_FUNCS_PATH);
231 /* mkdir /dev/usb-funcs/sdb */
232 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
233 ret = cfs_ensure_dir(buf);
237 /* mkdir /dev/usb-funcs/sdb/default */
238 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
239 ret = cfs_ensure_dir(buf);
243 /* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
244 ret = mount(dev_name, buf, "functionfs", 0, NULL);
248 /* start sdbd.socket */
249 ret = systemd_start_unit_wait_started(service, ".socket", -1);
259 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
262 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
265 rmdir(USB_FUNCS_PATH);
270 static int cfs_cleanup_ffs_service(usbg_function *function)
273 char buf[MAX_INSTANCE_LEN];
274 struct usb_function *usb_function;
279 usb_function = cfs_find_usb_function(function);
283 /* stop .socket first and stop .service later becuase of socket activation */
284 if (usb_function->service) {
285 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
286 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
289 /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
290 ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
302 /* remove /dev/usb-funcs/[sdb|mtp] directory */
303 ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
308 if (ret < 0 && errno != ENOTEMPTY)
311 /* remove /dev/usb-funcs/ directory */
312 ret = rmdir(USB_FUNCS_PATH);
313 if (ret < 0 && errno != ENOTEMPTY)
320 static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
323 struct ether_addr ethaddr;
324 struct usbg_gadget_strs strs;
325 struct usbg_f_net *nf = usbg_to_net_function(func);
330 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
331 if (ret != USBG_SUCCESS)
334 for (i = 0; i < ETHER_ADDR_LEN; i++)
335 ethaddr.ether_addr_octet[i] = 0;
337 for (i = 0; (i < 256) && strs.serial[i]; i++) {
338 ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
340 ethaddr.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
341 ethaddr.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
343 usbg_free_gadget_strs(&strs);
345 /* host_addr changes mac address */
346 ret = usbg_f_net_set_host_addr(nf, ðaddr);
351 static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
355 usbg_function *function;
357 /* delete all configs */
359 usbg_for_each_config(config, cfs_client->gadget) {
360 ret = usbg_rm_config(config, USBG_RM_RECURSE);
364 goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
367 /* delete all functions */
369 usbg_for_each_function(function, cfs_client->gadget) {
370 if (usbg_get_function_type(function) == USBG_F_FFS) {
371 ret = cfs_cleanup_ffs_service(function);
376 ret = usbg_rm_function(function, USBG_RM_RECURSE);
380 goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
386 static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
392 usbg_function *function;
393 struct usb_function *usb_func;
394 char instance[MAX_INSTANCE_LEN];
395 struct usbg_config_attrs cattrs = {
396 .bmAttributes = usb_config->attrs.bmAttributs,
397 .bMaxPower = usb_config->attrs.MaxPower/2,
400 if (!usb_config->funcs || !usb_config->funcs[0])
403 ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
407 if (usb_config->strs.config_str) {
408 ret = usbg_set_config_string(config, usb_config->strs.lang_code, usb_config->strs.config_str);
413 for (i = 0; usb_config->funcs[i]; ++i) {
414 usb_func = usb_config->funcs[i];
416 /* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
417 if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
418 return -ENAMETOOLONG;
420 /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
421 if (usb_func->is_functionfs) {
422 function_type = USBG_F_FFS;
423 snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
425 function_type = usbg_lookup_function_type(usb_func->name);
426 strncpy(instance, usb_func->instance, sizeof(instance) - 1);
427 instance[sizeof(instance) - 1] = '\0';
430 function = usbg_get_function(cfs_client->gadget, function_type, instance);
432 ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
436 /* Setting rndis mac address. This should be done at this point,
437 * since the node host_addr changes to read only after the function
438 * is added to config. */
439 if (usbg_get_function_type(function) == USBG_F_RNDIS)
440 (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
442 if (usbg_get_function_type(function) == USBG_F_FFS) {
443 ret = cfs_prep_ffs_service(usb_func, function);
449 ret = usbg_add_config_function(config, NULL, function);
457 static int cfs_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *gadget)
461 struct cfs_client *cfs_client;
466 /* Verify the gadget and check if function is supported */
467 if (!cfs_is_gadget_supported(gadget))
470 cfs_client = container_of(usb, struct cfs_client, client);
472 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
476 ret = cfs_set_gadget_strs(cfs_client, &gadget->strs);
480 ret = cfs_cleanup_all_config_and_function(cfs_client);
484 for (i = 0; gadget->configs[i]; ++i) {
485 ret = cfs_set_gadget_config(cfs_client, i + 1, gadget->configs[i]);
493 static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
495 usbg_function *function;
496 struct usb_function *usb_function;
498 usbg_for_each_function(function, gadget) {
499 usb_function = cfs_find_usb_function(function);
504 case CFS_FUNCTION_SERVICE_START:
505 if (usb_function->handler)
506 usb_function->handler(1);
508 /* functionfs service is automatically started by socket activation */
509 if (!usb_function->is_functionfs && usb_function->service)
510 (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
513 case CFS_FUNCTION_SERVICE_STOP:
514 if (!usb_function->is_functionfs && usb_function->service)
515 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
517 if (usb_function->handler)
518 usb_function->handler(0);
521 case CFS_FUNCTION_SERVICE_POST_STOP:
522 if (usb_function->is_functionfs && usb_function->service)
523 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
532 static int cfs_enable(struct usb_client *usb)
535 struct cfs_client *cfs_client;
540 cfs_client = container_of(usb, struct cfs_client, client);
542 ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
546 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_START);
551 static int cfs_disable(struct usb_client *usb)
554 struct cfs_client *cfs_client;
559 cfs_client = container_of(usb, struct cfs_client, client);
561 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);
563 ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */
566 * Since functionfs service works with socket activation, you must stop it after disabling gadget.
567 * If usb data may come in after stopping functionfs service and before disabling gadget,
568 * functionfs service wakes up again by socket activation.
570 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);
576 int hw_cfs_gadget_open(struct hw_info *info,
577 const char *id, struct hw_common **common)
579 struct cfs_client *cfs_client;
582 if (!info || !common)
585 cfs_client = calloc(1, sizeof(*cfs_client));
589 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
593 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
594 if (!cfs_client->udc) {
599 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
600 &default_g_attrs, &default_g_strs, &cfs_client->gadget);
602 goto err_create_gadget;
604 cfs_client->client.common.info = info;
606 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
607 cfs_client->client.enable = cfs_enable;
608 cfs_client->client.disable = cfs_disable;
610 *common = &cfs_client->client.common;
616 usbg_cleanup(cfs_client->ctx);
624 int hw_cfs_gadget_close(struct hw_common *common)
626 usbg_function *function;
627 struct cfs_client *cfs_client;
628 struct usb_function *usb_func;
633 cfs_client = container_of(common, struct cfs_client, client.common);
635 usbg_for_each_function(function, cfs_client->gadget) {
636 usb_func = cfs_find_usb_function(function);
640 if (usb_func->is_functionfs && usb_func->service) {
641 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
642 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
647 * For now we don't check for errors
648 * but we should somehow handle them
650 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
651 usbg_cleanup(cfs_client->ctx);