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->ffs_service) {
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_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
665 struct ether_addr ethaddr;
666 struct usbg_gadget_strs strs;
667 struct usbg_f_net *nf = usbg_to_net_function(func);
672 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
673 if (ret != USBG_SUCCESS)
676 for (i = 0; i < ETHER_ADDR_LEN; i++)
677 ethaddr.ether_addr_octet[i] = 0;
679 for (i = 0; (i < 256) && strs.serial[i]; i++) {
680 ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
682 ethaddr.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
683 ethaddr.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
685 usbg_free_gadget_strs(&strs);
687 /* host_addr changes mac address */
688 ret = usbg_f_net_set_host_addr(nf, ðaddr);
693 static int cfs_set_gadget_config(struct cfs_client *cfs_client,
695 struct usb_configuration *usb_config)
697 struct usbg_config_attrs cattrs = {
698 .bmAttributes = usb_config->attrs.bmAttributs,
699 .bMaxPower = usb_config->attrs.MaxPower/2,
705 if (!usb_config->funcs || !usb_config->funcs[0])
708 config = usbg_get_config(cfs_client->gadget, config_id, NULL);
710 ret = usbg_rm_config(config, USBG_RM_RECURSE);
715 ret = usbg_create_config(cfs_client->gadget, config_id,
716 CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
720 for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
721 ret = usbg_set_config_string(config, usb_config->strs[i].lang_code,
722 usb_config->strs[i].config_str);
727 for (i = 0; usb_config->funcs && usb_config->funcs[i]; ++i) {
728 struct usb_function *usb_func = usb_config->funcs[i];
729 char instance[MAX_INSTANCE_LEN];
733 if (!usb_func->ffs_service) {
734 type = usbg_lookup_function_type(usb_func->name);
735 if (strlen(usb_func->instance) >= MAX_INSTANCE_LEN)
736 return -ENAMETOOLONG;
737 strncpy(instance, usb_func->instance, MAX_INSTANCE_LEN);
738 instance[MAX_INSTANCE_LEN - 1] = '\0';
741 ret = snprintf(instance, sizeof(instance), "%s%c%s",
742 usb_func->name, NAME_INSTANCE_SEP,
744 if (ret < 0 || ret >= sizeof(instance))
745 return -ENAMETOOLONG;
748 func = usbg_get_function(cfs_client->gadget, type, instance);
750 ret = usbg_create_function(cfs_client->gadget,
757 /* Setting rndis mac address. This should be done at this point,
758 * since the node host_addr changes to read only after the function
759 * is added to config. */
760 if (usbg_get_function_type(func) == USBG_F_RNDIS)
761 (void)cfs_set_rndis_mac_addr(cfs_client->gadget, func); /* A random value is used if fails */
763 if (usb_func->ffs_service) {
764 ret = cfs_prep_ffs_service(usb_func->name,
767 usb_func->ffs_service);
773 ret = usbg_add_config_function(config, NULL, func);
781 static int cfs_cleanup_left_configs(struct cfs_client *cfs_client,
784 usbg_config *lconfig, *config;
787 lconfig = usbg_get_config(cfs_client->gadget, last_config, NULL);
788 for (config = usbg_get_next_config(lconfig);
790 config = usbg_get_next_config(lconfig)) {
791 ret = usbg_rm_config(config, USBG_RM_RECURSE);
799 static int cfs_reconfigure_gadget(struct usb_client *usb,
800 struct usb_gadget *gadget)
802 struct cfs_client *cfs_client;
806 if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
809 cfs_client = container_of(usb, struct cfs_client,
812 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
816 for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
817 ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
822 for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
823 ret = cfs_set_gadget_config(cfs_client, i + 1,
829 ret = cfs_cleanup_left_configs(cfs_client, i);
832 * Cleanup things which are left after previous gadget
838 static int cfs_enable(struct usb_client *usb)
842 struct usb_gadget *gadget;
843 struct usb_function *func;
844 struct cfs_client *cfs_client;
849 cfs_client = container_of(usb, struct cfs_client, client);
850 ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
854 ret = cfs_get_current_gadget(usb, &gadget);
856 usbg_disable_gadget(cfs_client->gadget);
860 for (i = 0; gadget->funcs[i]; ++i) {
861 func = gadget->funcs[i];
867 (void)systemd_start_unit_wait_started(func->service, ".service", -1);
869 /* func->ffs_service is automatically started by socket activation */
872 cfs_free_gadget(gadget);
877 static int cfs_disable(struct usb_client *usb)
881 struct usb_gadget *gadget;
882 struct usb_function *func;
883 struct cfs_client *cfs_client;
888 ret = cfs_get_current_gadget(usb, &gadget);
892 for (i = 0; gadget->funcs[i]; ++i) {
893 func = gadget->funcs[i];
896 (void)systemd_stop_unit_wait_stopped(func->service, ".service", -1);
902 cfs_client = container_of(usb, struct cfs_client, client);
903 ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */
906 * Since ffs_service works with socket activation, you must stop it after disabling gadget.
907 * If usb data may come in after stopping ffs_service and before disabling gadget,
908 * ffs_service wakes up again by socket activation.
910 for (i = 0; gadget->funcs[i]; ++i) {
911 func = gadget->funcs[i];
913 if (func->ffs_service)
914 (void)systemd_stop_unit_wait_stopped(func->ffs_service, ".service", -1);
917 cfs_free_gadget(gadget);
923 int hw_cfs_gadget_open(struct hw_info *info,
924 const char *id, struct hw_common **common)
926 struct cfs_client *cfs_client;
929 if (!info || !common)
932 cfs_client = zalloc(sizeof(*cfs_client));
936 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
940 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
941 if (!cfs_client->udc) {
946 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
947 &default_g_attrs, &default_g_strs,
948 &cfs_client->gadget);
950 goto err_create_gadget;
952 cfs_client->client.common.info = info;
953 cfs_client->client.get_current_gadget = cfs_get_current_gadget;
954 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
955 cfs_client->client.is_gadget_supported = cfs_is_gadget_supported;
956 cfs_client->client.is_function_supported = cfs_is_function_supported;
957 cfs_client->client.enable = cfs_enable;
958 cfs_client->client.disable = cfs_disable;
959 cfs_client->client.free_gadget = cfs_free_gadget;
961 *common = &cfs_client->client.common;
966 usbg_cleanup(cfs_client->ctx);
974 int hw_cfs_gadget_close(struct hw_common *common)
976 struct cfs_client *cfs_client;
983 cfs_client = container_of(common, struct cfs_client,
986 usbg_for_each_function(func, cfs_client->gadget) {
987 char *name = (char *)usbg_get_function_type_str(
988 usbg_get_function_type(func));
989 char *instance = (char *)usbg_get_function_instance(func);
990 struct usb_function *usb_func;
992 ret = cfs_find_func(name, instance);
996 usb_func = _available_funcs[ret];
997 if (usb_func->ffs_service) {
998 systemd_stop_unit_wait_stopped(usb_func->ffs_service, ".socket", -1);
999 systemd_stop_unit_wait_stopped(usb_func->ffs_service, ".service", -1);
1004 * For now we don't check for errors
1005 * but we should somehow handle them
1007 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
1008 usbg_cleanup(cfs_client->ctx);