4 * Copyright (c) 2016 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>
21 #include "../shared.h"
27 #include <sys/types.h>
28 #include <sys/mount.h>
29 #include <usbg/usbg.h>
31 #include <systemd/sd-bus.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 struct usb_client client;
57 /* Based on values in slp-gadget kernel module */
58 struct usbg_gadget_attrs default_g_attrs = {
65 struct usbg_gadget_strs default_g_strs = {
66 .manufacturer = "Samsung",
68 .serial = "01234TEST",
71 static void cfs_free_config(struct usb_configuration *config)
79 for (i = 0; config->strs[i].lang_code; ++i)
80 free(config->strs[i].config_str);
86 * Each function will be free later,
87 * for now we cleanup only pointers.
95 static void cfs_free_gadget(struct usb_gadget *gadget)
103 for (i = 0; gadget->strs[i].lang_code; ++i) {
104 free(gadget->strs[i].manufacturer);
105 free(gadget->strs[i].product);
106 free(gadget->strs[i].serial);
111 if (gadget->configs) {
112 for (i = 0; gadget->configs[i]; ++i)
113 cfs_free_config(gadget->configs[i]);
115 free(gadget->configs);
119 for (i = 0; gadget->funcs[i]; ++i)
120 gadget->funcs[i]->free_func(gadget->funcs[i]);
126 static int cfs_read_gadget_attrs_strs(usbg_gadget *gadget,
127 struct usb_gadget *usb_gadget)
129 struct usbg_gadget_attrs attrs;
130 struct usbg_gadget_strs strs;
133 ret = usbg_get_gadget_attrs(gadget, &attrs);
137 usb_gadget->attrs.bDeviceClass = attrs.bDeviceClass;
138 usb_gadget->attrs.bDeviceSubClass = attrs.bDeviceSubClass;
139 usb_gadget->attrs.bDeviceProtocol = attrs.bDeviceProtocol;
140 usb_gadget->attrs.idVendor = attrs.idVendor;
141 usb_gadget->attrs.idProduct = attrs.idProduct;
142 usb_gadget->attrs.bcdDevice = attrs.bcdDevice;
145 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
149 usb_gadget->strs[0].manufacturer = strdup(strs.manufacturer);
150 usb_gadget->strs[0].product = strdup(strs.product);
151 usb_gadget->strs[0].serial = strdup(strs.serial);
153 if (!usb_gadget->strs[0].manufacturer ||
154 !usb_gadget->strs[0].product ||
155 !usb_gadget->strs[0].serial) {
162 free(usb_gadget->strs[0].manufacturer);
163 free(usb_gadget->strs[0].product);
164 free(usb_gadget->strs[0].serial);
169 static bool cfs_match_func(struct usb_function *f,
170 const char *name, const char *instance) {
171 if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS))) {
172 /* Standard functions */
173 if (!strcmp(name, f->name) && !strcmp(instance, f->instance))
176 /* Function with service */
177 const char *sep, *fname, *finst;
180 sep = strchr(instance, NAME_INSTANCE_SEP);
181 if (!sep || strlen(sep + 1) < 1)
185 len = sep - instance;
188 if (strlen(f->name) == len
189 && !strncmp(f->name, fname, len)
190 && !strcmp(f->instance, finst))
198 static int cfs_find_func(const char *name, const char *instance)
202 for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i)
203 if (cfs_match_func(_available_funcs[i], name, instance))
209 static int cfs_alloc_new_func(struct usb_gadget *gadget, const char *fname,
210 const char *instance, struct usb_function **_func)
212 struct usb_function *func;
215 ret = cfs_find_func(fname, instance);
219 ret = _available_funcs[ret]->clone(_available_funcs[ret], &func);
227 static int cfs_read_funcs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
234 usbg_for_each_function(func, gadget) {
235 char *func_name = (char *)usbg_get_function_type_str(
236 usbg_get_function_type(func));
237 char *instance = (char *)usbg_get_function_instance(func);
239 ret = cfs_alloc_new_func(usb_gadget, func_name, instance,
240 usb_gadget->funcs + i);
249 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
256 static struct usb_function *cfs_find_func_in_gadget(
257 struct usb_gadget *gadget, const char *name, const char *instance)
261 for (i = 0; gadget->funcs[i]; ++i)
262 if (cfs_match_func(gadget->funcs[i], name, instance))
263 return gadget->funcs[i];
268 static int cfs_alloc_config(int n_funcs, struct usb_configuration **_config)
270 struct usb_configuration *config;
272 config = zalloc(sizeof(*config));
276 config->strs = calloc(2, sizeof(*config->strs));
280 config->funcs = calloc(n_funcs + 1, sizeof(*config->funcs));
295 static int cfs_read_config(usbg_config *config, struct usb_gadget *gadget,
296 struct usb_configuration *usb_config)
300 char *name, *instance;
301 struct usbg_config_attrs c_attrs;
302 struct usbg_config_strs c_strs;
306 usbg_for_each_binding(b, config) {
307 func = usbg_get_binding_target(b);
309 name = (char *)usbg_get_function_type_str(
310 usbg_get_function_type(func));
311 instance = (char *)usbg_get_function_instance(func);
313 usb_config->funcs[i] = cfs_find_func_in_gadget(gadget,
315 if (!usb_config->funcs[i]) {
321 ret = usbg_get_config_attrs(config, &c_attrs);
325 usb_config->attrs.MaxPower = c_attrs.bMaxPower*2;
326 usb_config->attrs.bmAttributs = c_attrs.bmAttributes;
328 ret = usbg_get_config_strs(config, LANG_US_ENG, &c_strs);
330 usb_config->strs[0].lang_code = 0;
332 usb_config->strs[0].lang_code = LANG_US_ENG;
333 usb_config->strs[0].config_str = strdup(c_strs.configuration);
334 if (!usb_config->strs[0].config_str)
341 static int cfs_count_bindings(usbg_config *config)
346 usbg_for_each_binding(b, config) ++i;
351 static int cfs_read_configs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
358 usbg_for_each_config(config, gadget) {
359 n_funcs = cfs_count_bindings(config);
361 ret = cfs_alloc_config(n_funcs, usb_gadget->configs + i);
364 ret = cfs_read_config(config, usb_gadget,
365 usb_gadget->configs[i]);
374 free(usb_gadget->configs[i]->strs);
375 free(usb_gadget->configs[i]->funcs);
376 free(usb_gadget->configs[i]);
379 cfs_free_config(usb_gadget->configs[i--]);
383 static int cfs_count_configs(usbg_gadget *gadget)
388 usbg_for_each_config(config, gadget) ++i;
393 static int cfs_count_functions(usbg_gadget *gadget)
398 usbg_for_each_function(func, gadget) ++i;
403 static int cfs_get_current_gadget(struct usb_client *usb,
404 struct usb_gadget **_usb_gadget)
406 struct cfs_client *cfs_client;
407 struct usb_gadget *usb_gadget;
408 struct usb_gadget_strings *strs;
409 struct usb_configuration **usb_configs;
410 struct usb_function **usb_funcs;
411 int n_funcs, n_configs;
418 cfs_client = container_of(usb, struct cfs_client,
421 usb_gadget = zalloc(sizeof(*usb_gadget));
426 * Currently there is no interface in libusbg which
427 * allows to list all string languages.
428 * That's why we do this only for USA english
430 strs = calloc(2, sizeof(*strs));
434 strs[0].lang_code = LANG_US_ENG;
436 usb_gadget->strs = strs;
438 ret = cfs_read_gadget_attrs_strs(cfs_client->gadget, usb_gadget);
443 n_funcs = cfs_count_functions(cfs_client->gadget);
444 usb_funcs = calloc(n_funcs + 1, sizeof(*usb_funcs));
446 goto free_strs_with_content;
448 usb_gadget->funcs = usb_funcs;
450 ret = cfs_read_funcs(cfs_client->gadget, usb_gadget);
454 n_configs = cfs_count_configs(cfs_client->gadget);
455 usb_configs = calloc(n_configs + 1, sizeof(*usb_configs));
457 goto free_funcs_with_content;
459 usb_gadget->configs = usb_configs;
461 ret = cfs_read_configs(cfs_client->gadget, usb_gadget);
465 *_usb_gadget = usb_gadget;
470 free_funcs_with_content:
471 for (i = 0; usb_gadget->funcs[i]; ++i)
472 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
475 free_strs_with_content:
476 for (i = 0; usb_gadget->strs[i].lang_code; ++i) {
477 free(usb_gadget->strs[i].manufacturer);
478 free(usb_gadget->strs[i].product);
479 free(usb_gadget->strs[i].serial);
482 free(usb_gadget->strs);
489 static bool cfs_is_function_supported(struct usb_client *usb,
490 struct usb_function *func)
495 switch (func->function_group) {
496 case USB_FUNCTION_GROUP_SIMPLE:
497 ret = usbg_lookup_function_type(func->name);
500 case USB_FUNCTION_GROUP_WITH_SERVICE:
501 /* 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 #define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1"
591 #define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1"
592 #define SYSTEMD_DBUS_MANAGER_IFACE "org.freedesktop.systemd1.Manager"
594 #define SYSTEMD_SOCKET_SUFFIX ".socket"
595 #define MAX_SOCKET_NAME 1024
602 static int socket_started(sd_bus_message *m, void *userdata,
603 sd_bus_error *ret_error)
605 struct bus_ctx *ctx = userdata;
609 ret = sd_bus_message_read(m, "uoss", NULL, NULL, &signal_unit, NULL);
611 sd_event_exit(ctx->loop, ret);
615 if (!strcmp(signal_unit, ctx->unit))
616 sd_event_exit(ctx->loop, 0);
621 static int systemd_unit_interface_sync(const char *method, const char *unit,
625 sd_event *loop = NULL;
629 ret = sd_bus_open_system(&bus);
634 ret = sd_event_new(&loop);
641 ret = sd_bus_attach_event(bus, loop, SD_EVENT_PRIORITY_NORMAL);
645 ret = sd_bus_add_match(bus, NULL,
647 "sender='" SYSTEMD_DBUS_SERVICE "',"
648 "interface='" SYSTEMD_DBUS_MANAGER_IFACE "',"
649 "member='JobRemoved',"
650 "path_namespace='" SYSTEMD_DBUS_PATH "'",
658 ret = sd_bus_call_method(bus,
659 SYSTEMD_DBUS_SERVICE,
661 SYSTEMD_DBUS_MANAGER_IFACE,
672 ret = sd_event_loop(loop);
676 sd_event_unref(loop);
682 static int systemd_start_socket(const char *socket_name)
684 char unit[MAX_SOCKET_NAME];
687 ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SOCKET_SUFFIX,
689 if (ret < 0 || ret >= sizeof(unit))
690 return -ENAMETOOLONG;
693 return systemd_unit_interface_sync("StartUnit", unit, true);
696 static int systemd_stop_socket(const char *socket_name)
698 char unit[MAX_SOCKET_NAME];
701 ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SOCKET_SUFFIX,
703 if (ret < 0 || ret >= sizeof(unit))
704 return -ENAMETOOLONG;
706 return systemd_unit_interface_sync("StopUnit", unit, false);
709 static int cfs_ensure_dir(char *path)
713 ret = mkdir(path, 0770);
715 ret = errno == EEXIST ? 0 : errno;
720 static int cfs_prep_ffs_service(const char *name, const char *instance,
721 const char *dev_name, const char *socket_name)
728 /* TODO: Add some good error handling */
732 ret = snprintf(pos, left, "%s", USB_FUNCS_PATH);
733 if (ret < 0 || ret >= left) {
734 return -ENAMETOOLONG;
739 ret = cfs_ensure_dir(buf);
743 ret = snprintf(pos, left, "/%s", name);
744 if (ret < 0 || ret >= left) {
745 return -ENAMETOOLONG;
750 ret = cfs_ensure_dir(buf);
754 ret = snprintf(pos, left, "/%s", instance);
755 if (ret < 0 || ret >= left) {
756 return -ENAMETOOLONG;
761 ret = cfs_ensure_dir(buf);
765 ret = mount(dev_name, buf, "functionfs", 0, NULL);
769 ret = systemd_start_socket(socket_name);
779 static int cfs_set_gadget_config(struct cfs_client *cfs_client,
781 struct usb_configuration *usb_config)
783 struct usbg_config_attrs cattrs = {
784 .bmAttributes = usb_config->attrs.bmAttributs,
785 .bMaxPower = usb_config->attrs.MaxPower/2,
791 if (!usb_config->funcs || !usb_config->funcs[0])
794 config = usbg_get_config(cfs_client->gadget, config_id, NULL);
796 ret = usbg_rm_config(config, USBG_RM_RECURSE);
801 ret = usbg_create_config(cfs_client->gadget, config_id,
802 CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
806 for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
807 ret = usbg_set_config_string(config, usb_config->strs[i].lang_code,
808 usb_config->strs[i].config_str);
813 for (i = 0; usb_config->funcs && usb_config->funcs[i]; ++i) {
814 struct usb_function *usb_func = usb_config->funcs[i];
815 char instance[MAX_INSTANCE_LEN];
819 switch (usb_func->function_group) {
820 case USB_FUNCTION_GROUP_SIMPLE:
821 type = usbg_lookup_function_type(usb_func->name);
822 if (strlen(usb_func->instance) >= MAX_INSTANCE_LEN)
823 return -ENAMETOOLONG;
824 strncpy(instance, usb_func->instance, MAX_INSTANCE_LEN);
825 instance[MAX_INSTANCE_LEN - 1] = '\0';
827 case USB_FUNCTION_GROUP_WITH_SERVICE:
829 ret = snprintf(instance, sizeof(instance), "%s%c%s",
830 usb_func->name, NAME_INSTANCE_SEP,
832 if (ret < 0 || ret >= sizeof(instance))
833 return -ENAMETOOLONG;
840 func = usbg_get_function(cfs_client->gadget, type, instance);
842 ret = usbg_create_function(cfs_client->gadget,
849 if (usb_func->function_group ==
850 USB_FUNCTION_GROUP_WITH_SERVICE) {
851 struct usb_function_with_service *fws;
853 fws = container_of(usb_func,
854 struct usb_function_with_service,
856 ret = cfs_prep_ffs_service(usb_func->name,
866 ret = usbg_add_config_function(config, NULL, func);
874 static int cfs_cleanup_left_configs(struct cfs_client *cfs_client,
877 usbg_config *lconfig, *config;
880 lconfig = usbg_get_config(cfs_client->gadget, last_config, NULL);
881 for (config = usbg_get_next_config(lconfig);
883 config = usbg_get_next_config(lconfig)) {
884 ret = usbg_rm_config(config, USBG_RM_RECURSE);
892 static int cfs_reconfigure_gadget(struct usb_client *usb,
893 struct usb_gadget *gadget)
895 struct cfs_client *cfs_client;
902 cfs_client = container_of(usb, struct cfs_client,
905 if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
908 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
912 for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
913 ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
918 for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
919 ret = cfs_set_gadget_config(cfs_client, i + 1,
925 ret = cfs_cleanup_left_configs(cfs_client, i);
928 * Cleanup things which are left after previous gadget
934 static int cfs_enable(struct usb_client *usb)
936 struct cfs_client *cfs_client;
941 cfs_client = container_of(usb, struct cfs_client,
944 return usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
947 static int cfs_disable(struct usb_client *usb)
949 struct cfs_client *cfs_client;
954 cfs_client = container_of(usb, struct cfs_client,
957 return usbg_disable_gadget(cfs_client->gadget);
960 static int cfs_gadget_open(struct hw_info *info,
961 const char *id, struct hw_common **common)
963 struct cfs_client *cfs_client;
966 if (!info || !common)
969 /* used exclusively with slp usb_client*/
970 if (!access("/sys/class/usb_mode/usb0/enable", F_OK))
973 cfs_client = zalloc(sizeof(*cfs_client));
977 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
981 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
982 if (!cfs_client->udc) {
987 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
988 &default_g_attrs, &default_g_strs,
989 &cfs_client->gadget);
991 goto err_create_gadget;
993 cfs_client->client.common.info = info;
994 cfs_client->client.get_current_gadget = cfs_get_current_gadget;
995 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
996 cfs_client->client.is_gadget_supported = cfs_is_gadget_supported;
997 cfs_client->client.is_function_supported = cfs_is_function_supported;
998 cfs_client->client.enable = cfs_enable;
999 cfs_client->client.disable = cfs_disable;
1000 cfs_client->client.free_gadget = cfs_free_gadget;
1002 *common = &cfs_client->client.common;
1007 usbg_cleanup(cfs_client->ctx);
1014 static int cfs_gadget_close(struct hw_common *common)
1016 struct cfs_client *cfs_client;
1021 cfs_client = container_of(common, struct cfs_client,
1025 * For now we don't check for errors
1026 * but we should somehow handle them
1028 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
1029 usbg_cleanup(cfs_client->ctx);
1035 HARDWARE_MODULE_STRUCTURE = {
1036 .magic = HARDWARE_INFO_TAG,
1037 .hal_version = HARDWARE_INFO_VERSION,
1038 .device_version = USB_CLIENT_HARDWARE_DEVICE_VERSION,
1039 .id = USB_CFS_CLIENT_HARDWARE_DEVICE_ID,
1040 .name = "cfs-gadget",
1041 .open = cfs_gadget_open,
1042 .close = cfs_gadget_close,