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.
19 #include <hw/usb_client.h>
20 #include <hw/shared.h>
26 #include <sys/types.h>
27 #include <sys/mount.h>
28 #include <usbg/usbg.h>
29 #include <usbg/function/net.h>
32 #include <libsyscommon/dbus-systemd.h>
35 #define zalloc(amount) calloc(1, amount)
37 #define MAX_GADGET_STR_LEN 256
40 #define CONFIGFS_PATH "/sys/kernel/config"
42 #define CONFIGFS_GADGET_NAME "hal-gadget"
43 #define CONFIGFS_CONFIG_LABEL "hal-config"
45 #define NAME_INSTANCE_SEP '.'
46 #define MAX_INSTANCE_LEN 512
48 #define USB_FUNCS_PATH "/dev/usb-funcs/"
51 #define EXPORT __attribute__ ((visibility("default")))
55 struct usb_client client;
61 /* Based on values in slp-gadget kernel module */
62 struct usbg_gadget_attrs default_g_attrs = {
69 struct usbg_gadget_strs default_g_strs = {
70 .manufacturer = "Samsung",
72 .serial = "01234TEST",
75 static void cfs_free_config(struct usb_configuration *config)
83 for (i = 0; config->strs[i].lang_code; ++i)
84 free(config->strs[i].config_str);
90 * Each function will be free later,
91 * for now we cleanup only pointers.
99 static void cfs_free_gadget(struct usb_gadget *gadget)
107 for (i = 0; gadget->strs[i].lang_code; ++i) {
108 free(gadget->strs[i].manufacturer);
109 free(gadget->strs[i].product);
110 free(gadget->strs[i].serial);
115 if (gadget->configs) {
116 for (i = 0; gadget->configs[i]; ++i)
117 cfs_free_config(gadget->configs[i]);
119 free(gadget->configs);
123 for (i = 0; gadget->funcs[i]; ++i)
124 gadget->funcs[i]->free_func(gadget->funcs[i]);
132 static int cfs_read_gadget_attrs_strs(usbg_gadget *gadget,
133 struct usb_gadget *usb_gadget)
135 struct usbg_gadget_attrs attrs;
136 struct usbg_gadget_strs strs;
139 ret = usbg_get_gadget_attrs(gadget, &attrs);
143 usb_gadget->attrs.bDeviceClass = attrs.bDeviceClass;
144 usb_gadget->attrs.bDeviceSubClass = attrs.bDeviceSubClass;
145 usb_gadget->attrs.bDeviceProtocol = attrs.bDeviceProtocol;
146 usb_gadget->attrs.idVendor = attrs.idVendor;
147 usb_gadget->attrs.idProduct = attrs.idProduct;
148 usb_gadget->attrs.bcdDevice = attrs.bcdDevice;
150 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
154 usb_gadget->strs[0].manufacturer = strdup(strs.manufacturer);
155 usb_gadget->strs[0].product = strdup(strs.product);
156 usb_gadget->strs[0].serial = strdup(strs.serial);
158 usbg_free_gadget_strs(&strs);
160 if (!usb_gadget->strs[0].manufacturer ||
161 !usb_gadget->strs[0].product ||
162 !usb_gadget->strs[0].serial) {
169 free(usb_gadget->strs[0].manufacturer);
170 free(usb_gadget->strs[0].product);
171 free(usb_gadget->strs[0].serial);
176 static bool cfs_match_func(struct usb_function *f,
177 const char *name, const char *instance) {
178 if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS))) {
179 /* Standard functions */
180 if (!strcmp(name, f->name) && !strcmp(instance, f->instance))
183 /* Function with service */
184 const char *sep, *fname, *finst;
187 sep = strchr(instance, NAME_INSTANCE_SEP);
188 if (!sep || strlen(sep + 1) < 1)
192 len = sep - instance;
195 if (strlen(f->name) == len
196 && !strncmp(f->name, fname, len)
197 && !strcmp(f->instance, finst))
205 static int cfs_find_func(const char *name, const char *instance)
209 for (i = 0; _available_funcs[i]; ++i)
210 if (cfs_match_func(_available_funcs[i], name, instance))
216 static int cfs_alloc_new_func(struct usb_gadget *gadget, const char *fname,
217 const char *instance, struct usb_function **_func)
219 struct usb_function *func;
222 ret = cfs_find_func(fname, instance);
226 ret = _available_funcs[ret]->clone(_available_funcs[ret], &func);
234 static int cfs_read_funcs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
241 usbg_for_each_function(func, gadget) {
242 char *func_name = (char *)usbg_get_function_type_str(
243 usbg_get_function_type(func));
244 char *instance = (char *)usbg_get_function_instance(func);
246 ret = cfs_alloc_new_func(usb_gadget, func_name, instance,
247 usb_gadget->funcs + i);
256 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
263 static struct usb_function *cfs_find_func_in_gadget(
264 struct usb_gadget *gadget, const char *name, const char *instance)
268 for (i = 0; gadget->funcs[i]; ++i)
269 if (cfs_match_func(gadget->funcs[i], name, instance))
270 return gadget->funcs[i];
275 static int cfs_alloc_config(int n_funcs, struct usb_configuration **_config)
277 struct usb_configuration *config;
279 config = zalloc(sizeof(*config));
283 config->strs = calloc(2, sizeof(*config->strs));
287 config->funcs = calloc(n_funcs + 1, sizeof(*config->funcs));
302 static int cfs_read_config(usbg_config *config, struct usb_gadget *gadget,
303 struct usb_configuration *usb_config)
307 char *name, *instance;
308 struct usbg_config_attrs c_attrs;
309 struct usbg_config_strs c_strs;
313 usbg_for_each_binding(b, config) {
314 func = usbg_get_binding_target(b);
316 name = (char *)usbg_get_function_type_str(
317 usbg_get_function_type(func));
318 instance = (char *)usbg_get_function_instance(func);
320 usb_config->funcs[i] = cfs_find_func_in_gadget(gadget,
322 if (!usb_config->funcs[i]) {
328 ret = usbg_get_config_attrs(config, &c_attrs);
332 usb_config->attrs.MaxPower = c_attrs.bMaxPower*2;
333 usb_config->attrs.bmAttributs = c_attrs.bmAttributes;
335 ret = usbg_get_config_strs(config, LANG_US_ENG, &c_strs);
337 usb_config->strs[0].lang_code = 0;
339 usb_config->strs[0].lang_code = LANG_US_ENG;
340 usb_config->strs[0].config_str = strdup(c_strs.configuration);
341 if (!usb_config->strs[0].config_str)
348 static int cfs_count_bindings(usbg_config *config)
353 usbg_for_each_binding(b, config) ++i;
358 static int cfs_read_configs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
365 usbg_for_each_config(config, gadget) {
366 n_funcs = cfs_count_bindings(config);
368 ret = cfs_alloc_config(n_funcs, usb_gadget->configs + i);
371 ret = cfs_read_config(config, usb_gadget,
372 usb_gadget->configs[i]);
381 free(usb_gadget->configs[i]->strs);
382 free(usb_gadget->configs[i]->funcs);
383 free(usb_gadget->configs[i]);
386 cfs_free_config(usb_gadget->configs[i--]);
390 static int cfs_count_configs(usbg_gadget *gadget)
395 usbg_for_each_config(config, gadget) ++i;
400 static int cfs_count_functions(usbg_gadget *gadget)
405 usbg_for_each_function(func, gadget) ++i;
410 static int cfs_get_current_gadget(struct usb_client *usb,
411 struct usb_gadget **_usb_gadget)
413 struct cfs_client *cfs_client;
414 struct usb_gadget *usb_gadget;
415 struct usb_gadget_strings *strs;
416 struct usb_configuration **usb_configs;
417 struct usb_function **usb_funcs;
418 int n_funcs, n_configs;
425 cfs_client = container_of(usb, struct cfs_client, client);
427 usb_gadget = zalloc(sizeof(*usb_gadget));
432 * Currently there is no interface in libusbg which
433 * allows to list all string languages.
434 * That's why we do this only for USA english
436 strs = calloc(2, sizeof(*strs));
440 strs[0].lang_code = LANG_US_ENG;
442 usb_gadget->strs = strs;
444 ret = cfs_read_gadget_attrs_strs(cfs_client->gadget, usb_gadget);
448 n_funcs = cfs_count_functions(cfs_client->gadget);
449 usb_funcs = calloc(n_funcs + 1, sizeof(*usb_funcs));
451 goto free_strs_with_content;
453 usb_gadget->funcs = usb_funcs;
455 ret = cfs_read_funcs(cfs_client->gadget, usb_gadget);
459 n_configs = cfs_count_configs(cfs_client->gadget);
460 usb_configs = calloc(n_configs + 1, sizeof(*usb_configs));
462 goto free_funcs_with_content;
464 usb_gadget->configs = usb_configs;
466 ret = cfs_read_configs(cfs_client->gadget, usb_gadget);
470 *_usb_gadget = usb_gadget;
475 free_funcs_with_content:
476 for (i = 0; usb_gadget->funcs[i]; ++i)
477 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
480 free_strs_with_content:
481 for (i = 0; usb_gadget->strs[i].lang_code; ++i) {
482 free(usb_gadget->strs[i].manufacturer);
483 free(usb_gadget->strs[i].product);
484 free(usb_gadget->strs[i].serial);
487 free(usb_gadget->strs);
494 static bool cfs_is_function_supported(struct usb_client *usb,
495 struct usb_function *func)
500 if (!func->is_functionfs) {
501 ret = usbg_lookup_function_type(func->name);
504 /* TODO: Check if socket is available */
511 static bool cfs_is_gadget_supported(struct usb_client *usb,
512 struct usb_gadget *gadget)
516 if (!gadget || !gadget->configs || !gadget->funcs)
521 * Here is a good place to ensure that serial is immutable
524 /* No real restrictions for strings */
525 for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
526 struct usb_configuration *config = gadget->configs[j];
531 for (i = 0; config->funcs[i]; ++i)
532 if (!cfs_is_function_supported(usb, config->funcs[i]))
542 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
543 struct usb_gadget_attrs *attrs)
546 struct usbg_gadget_attrs gadget_attrs;
548 ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
552 gadget_attrs.bDeviceClass = attrs->bDeviceClass;
553 gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
554 gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
555 gadget_attrs.idVendor = attrs->idVendor;
556 gadget_attrs.idProduct = attrs->idProduct;
557 gadget_attrs.bcdDevice = attrs->bcdDevice;
559 ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
564 static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
565 struct usb_gadget_strings *strs)
571 * Here is a good place to ensure that serial is immutable
573 #define SET_STR(FIELD, STR_ID) \
575 ret = usbg_set_gadget_str(cfs_client->gadget, \
583 SET_STR(manufacturer, USBG_STR_MANUFACTURER);
584 SET_STR(product, USBG_STR_PRODUCT);
585 SET_STR(serial, USBG_STR_SERIAL_NUMBER);
590 static int cfs_ensure_dir(char *path)
594 ret = mkdir(path, 0770);
596 ret = errno == EEXIST ? 0 : errno;
601 static int cfs_prep_ffs_service(const char *name, const char *instance,
602 const char *dev_name, const char *socket_name)
609 /* TODO: Add some good error handling */
615 ret = snprintf(pos, left, "%s", USB_FUNCS_PATH);
616 if (ret < 0 || ret >= left) {
617 return -ENAMETOOLONG;
622 ret = cfs_ensure_dir(buf);
626 ret = snprintf(pos, left, "/%s", name);
627 if (ret < 0 || ret >= left) {
628 return -ENAMETOOLONG;
633 ret = cfs_ensure_dir(buf);
637 ret = snprintf(pos, left, "/%s", instance);
638 if (ret < 0 || ret >= left) {
639 return -ENAMETOOLONG;
644 ret = cfs_ensure_dir(buf);
648 ret = mount(dev_name, buf, "functionfs", 0, NULL);
652 ret = systemd_start_unit_wait_started(socket_name, ".socket", -1);
662 static int cfs_cleanup_ffs_service(usbg_function *function)
667 const char *instance;
668 char buf[MAX_INSTANCE_LEN];
669 struct usb_function *usb_function;
674 instance = usbg_get_function_instance(function); /* Ex: sdb.default */
675 name = usbg_get_function_type_str(usbg_get_function_type(function)); /* Fixed: ffs */
677 index = cfs_find_func(name, instance);
681 usb_function = _available_funcs[index];
683 /* stop .socket first and stop .service later becuase of socket activation */
684 if (usb_function->service) {
685 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
686 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
689 /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
690 ret = snprintf(buf, sizeof(buf), "%s%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
702 /* remove /dev/usb-funcs/[sdb|mtp] directory */
703 ret = snprintf(buf, sizeof(buf), "%s%s", USB_FUNCS_PATH, usb_function->name);
708 if (ret < 0 && errno != ENOTEMPTY)
711 /* remove /dev/usb-funcs/ directory */
712 ret = rmdir(USB_FUNCS_PATH);
713 if (ret < 0 && errno != ENOTEMPTY)
720 static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
723 struct ether_addr ethaddr;
724 struct usbg_gadget_strs strs;
725 struct usbg_f_net *nf = usbg_to_net_function(func);
730 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
731 if (ret != USBG_SUCCESS)
734 for (i = 0; i < ETHER_ADDR_LEN; i++)
735 ethaddr.ether_addr_octet[i] = 0;
737 for (i = 0; (i < 256) && strs.serial[i]; i++) {
738 ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
740 ethaddr.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
741 ethaddr.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
743 usbg_free_gadget_strs(&strs);
745 /* host_addr changes mac address */
746 ret = usbg_f_net_set_host_addr(nf, ðaddr);
751 static int cfs_set_gadget_config(struct cfs_client *cfs_client,
753 struct usb_configuration *usb_config)
755 struct usbg_config_attrs cattrs = {
756 .bmAttributes = usb_config->attrs.bmAttributs,
757 .bMaxPower = usb_config->attrs.MaxPower/2,
763 if (!usb_config->funcs || !usb_config->funcs[0])
766 config = usbg_get_config(cfs_client->gadget, config_id, NULL);
768 ret = usbg_rm_config(config, USBG_RM_RECURSE);
773 ret = usbg_create_config(cfs_client->gadget, config_id,
774 CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
778 for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
779 ret = usbg_set_config_string(config, usb_config->strs[i].lang_code,
780 usb_config->strs[i].config_str);
785 for (i = 0; usb_config->funcs && usb_config->funcs[i]; ++i) {
786 struct usb_function *usb_func = usb_config->funcs[i];
787 char instance[MAX_INSTANCE_LEN];
791 if (!usb_func->is_functionfs) {
792 type = usbg_lookup_function_type(usb_func->name);
793 if (strlen(usb_func->instance) >= MAX_INSTANCE_LEN)
794 return -ENAMETOOLONG;
795 strncpy(instance, usb_func->instance, MAX_INSTANCE_LEN);
796 instance[MAX_INSTANCE_LEN - 1] = '\0';
799 ret = snprintf(instance, sizeof(instance), "%s%c%s",
800 usb_func->name, NAME_INSTANCE_SEP,
802 if (ret < 0 || ret >= sizeof(instance))
803 return -ENAMETOOLONG;
806 func = usbg_get_function(cfs_client->gadget, type, instance);
808 ret = usbg_create_function(cfs_client->gadget,
815 /* Setting rndis mac address. This should be done at this point,
816 * since the node host_addr changes to read only after the function
817 * is added to config. */
818 if (usbg_get_function_type(func) == USBG_F_RNDIS)
819 (void)cfs_set_rndis_mac_addr(cfs_client->gadget, func); /* A random value is used if fails */
821 if (usb_func->is_functionfs) {
822 ret = cfs_prep_ffs_service(usb_func->name,
831 ret = usbg_add_config_function(config, NULL, func);
839 static int cfs_cleanup_left_configs(struct cfs_client *cfs_client,
842 usbg_config *lconfig, *config;
845 lconfig = usbg_get_config(cfs_client->gadget, last_config, NULL);
846 for (config = usbg_get_next_config(lconfig);
848 config = usbg_get_next_config(lconfig)) {
849 ret = usbg_rm_config(config, USBG_RM_RECURSE);
857 static int cfs_function_is_binded(struct usbg_gadget *gadget, struct usbg_function *func)
862 usbg_for_each_config(config, gadget)
863 usbg_for_each_binding(b, config)
864 if (usbg_get_binding_target(b) == func)
870 static int cfs_cleanup_unused_function(usbg_gadget *gadget)
873 usbg_function *function;
879 usbg_for_each_function(function, gadget) {
880 if(cfs_function_is_binded(gadget, function))
883 if (usbg_get_function_type(function) == USBG_F_FFS) {
884 ret = cfs_cleanup_ffs_service(function);
889 ret = usbg_rm_function(function, USBG_RM_RECURSE);
893 /* You cannot delete a usbg_function directly in an iterator. */
901 static int cfs_reconfigure_gadget(struct usb_client *usb,
902 struct usb_gadget *gadget)
904 struct cfs_client *cfs_client;
908 if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
911 cfs_client = container_of(usb, struct cfs_client,
914 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
918 for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
919 ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
924 for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
925 ret = cfs_set_gadget_config(cfs_client, i + 1,
931 ret = cfs_cleanup_left_configs(cfs_client, i);
935 /* Cleanup things which are left after previous gadget */
936 ret = cfs_cleanup_unused_function(cfs_client->gadget);
942 static void cfs_start_stop_service_handler(usbg_gadget *gadget, bool start, bool post_stop)
946 const char *instance;
948 usbg_binding *binding;
949 usbg_function *function;
950 struct usb_function *usb_function;
952 /* Execute service and handler related to functions bound to configs */
953 usbg_for_each_config(config, gadget) {
954 usbg_for_each_binding(binding, config) {
955 function = usbg_get_binding_target(binding);
956 instance = usbg_get_function_instance(function);
957 name = usbg_get_function_type_str(usbg_get_function_type(function));
959 index = cfs_find_func(name, instance);
963 usb_function = _available_funcs[index];
966 if (usb_function->handler)
967 usb_function->handler(1);
969 /* functionfs service is automatically started by socket activation */
970 if (!usb_function->is_functionfs && usb_function->service)
971 (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
974 if (usb_function->is_functionfs && usb_function->service)
975 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
977 if (!usb_function->is_functionfs && usb_function->service)
978 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
980 if (usb_function->handler)
981 usb_function->handler(0);
988 static int cfs_enable(struct usb_client *usb)
991 struct cfs_client *cfs_client;
996 cfs_client = container_of(usb, struct cfs_client, client);
998 ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
1002 cfs_start_stop_service_handler(cfs_client->gadget, true, false);
1007 static int cfs_disable(struct usb_client *usb)
1010 struct cfs_client *cfs_client;
1015 cfs_client = container_of(usb, struct cfs_client, client);
1017 cfs_start_stop_service_handler(cfs_client->gadget, false, false);
1019 ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */
1022 * Since functionfs service works with socket activation, you must stop it after disabling gadget.
1023 * If usb data may come in after stopping functionfs service and before disabling gadget,
1024 * functionfs service wakes up again by socket activation.
1026 cfs_start_stop_service_handler(cfs_client->gadget, false, true);
1032 int hw_cfs_gadget_open(struct hw_info *info,
1033 const char *id, struct hw_common **common)
1035 struct cfs_client *cfs_client;
1038 if (!info || !common)
1041 cfs_client = zalloc(sizeof(*cfs_client));
1045 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
1049 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
1050 if (!cfs_client->udc) {
1055 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
1056 &default_g_attrs, &default_g_strs,
1057 &cfs_client->gadget);
1059 goto err_create_gadget;
1061 cfs_client->client.common.info = info;
1062 cfs_client->client.get_current_gadget = cfs_get_current_gadget;
1063 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
1064 cfs_client->client.is_gadget_supported = cfs_is_gadget_supported;
1065 cfs_client->client.is_function_supported = cfs_is_function_supported;
1066 cfs_client->client.enable = cfs_enable;
1067 cfs_client->client.disable = cfs_disable;
1068 cfs_client->client.free_gadget = cfs_free_gadget;
1070 *common = &cfs_client->client.common;
1075 usbg_cleanup(cfs_client->ctx);
1083 int hw_cfs_gadget_close(struct hw_common *common)
1085 struct cfs_client *cfs_client;
1086 usbg_function *func;
1092 cfs_client = container_of(common, struct cfs_client,
1095 usbg_for_each_function(func, cfs_client->gadget) {
1096 char *name = (char *)usbg_get_function_type_str(
1097 usbg_get_function_type(func));
1098 char *instance = (char *)usbg_get_function_instance(func);
1099 struct usb_function *usb_func;
1101 ret = cfs_find_func(name, instance);
1105 usb_func = _available_funcs[ret];
1107 if (usb_func->is_functionfs && usb_func->service) {
1108 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
1109 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
1114 * For now we don't check for errors
1115 * but we should somehow handle them
1117 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
1118 usbg_cleanup(cfs_client->ctx);