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/systemd.h>
21 #include <hw/shared.h>
27 #include <sys/types.h>
28 #include <sys/mount.h>
29 #include <usbg/usbg.h>
34 #define zalloc(amount) calloc(1, amount)
36 #define MAX_GADGET_STR_LEN 256
39 #define CONFIGFS_PATH "/sys/kernel/config"
41 #define CONFIGFS_GADGET_NAME "hal-gadget"
42 #define CONFIGFS_CONFIG_LABEL "hal-config"
44 #define NAME_INSTANCE_SEP '.'
45 #define MAX_INSTANCE_LEN 512
47 #define USB_FUNCS_PATH "/dev/usb-funcs/"
50 #define EXPORT __attribute__ ((visibility("default")))
54 struct usb_client client;
60 /* Based on values in slp-gadget kernel module */
61 struct usbg_gadget_attrs default_g_attrs = {
68 struct usbg_gadget_strs default_g_strs = {
69 .manufacturer = "Samsung",
71 .serial = "01234TEST",
74 static void cfs_free_config(struct usb_configuration *config)
82 for (i = 0; config->strs[i].lang_code; ++i)
83 free(config->strs[i].config_str);
89 * Each function will be free later,
90 * for now we cleanup only pointers.
98 static void cfs_free_gadget(struct usb_gadget *gadget)
106 for (i = 0; gadget->strs[i].lang_code; ++i) {
107 free(gadget->strs[i].manufacturer);
108 free(gadget->strs[i].product);
109 free(gadget->strs[i].serial);
114 if (gadget->configs) {
115 for (i = 0; gadget->configs[i]; ++i)
116 cfs_free_config(gadget->configs[i]);
118 free(gadget->configs);
122 for (i = 0; gadget->funcs[i]; ++i)
123 gadget->funcs[i]->free_func(gadget->funcs[i]);
129 static int cfs_read_gadget_attrs_strs(usbg_gadget *gadget,
130 struct usb_gadget *usb_gadget)
132 struct usbg_gadget_attrs attrs;
133 struct usbg_gadget_strs strs;
136 ret = usbg_get_gadget_attrs(gadget, &attrs);
140 usb_gadget->attrs.bDeviceClass = attrs.bDeviceClass;
141 usb_gadget->attrs.bDeviceSubClass = attrs.bDeviceSubClass;
142 usb_gadget->attrs.bDeviceProtocol = attrs.bDeviceProtocol;
143 usb_gadget->attrs.idVendor = attrs.idVendor;
144 usb_gadget->attrs.idProduct = attrs.idProduct;
145 usb_gadget->attrs.bcdDevice = attrs.bcdDevice;
148 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
152 usb_gadget->strs[0].manufacturer = strdup(strs.manufacturer);
153 usb_gadget->strs[0].product = strdup(strs.product);
154 usb_gadget->strs[0].serial = strdup(strs.serial);
156 if (!usb_gadget->strs[0].manufacturer ||
157 !usb_gadget->strs[0].product ||
158 !usb_gadget->strs[0].serial) {
165 free(usb_gadget->strs[0].manufacturer);
166 free(usb_gadget->strs[0].product);
167 free(usb_gadget->strs[0].serial);
172 static bool cfs_match_func(struct usb_function *f,
173 const char *name, const char *instance) {
174 if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS))) {
175 /* Standard functions */
176 if (!strcmp(name, f->name) && !strcmp(instance, f->instance))
179 /* Function with service */
180 const char *sep, *fname, *finst;
183 sep = strchr(instance, NAME_INSTANCE_SEP);
184 if (!sep || strlen(sep + 1) < 1)
188 len = sep - instance;
191 if (strlen(f->name) == len
192 && !strncmp(f->name, fname, len)
193 && !strcmp(f->instance, finst))
201 static int cfs_find_func(const char *name, const char *instance)
205 for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i)
206 if (cfs_match_func(_available_funcs[i], name, instance))
212 static int cfs_alloc_new_func(struct usb_gadget *gadget, const char *fname,
213 const char *instance, struct usb_function **_func)
215 struct usb_function *func;
218 ret = cfs_find_func(fname, instance);
222 ret = _available_funcs[ret]->clone(_available_funcs[ret], &func);
230 static int cfs_read_funcs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
237 usbg_for_each_function(func, gadget) {
238 char *func_name = (char *)usbg_get_function_type_str(
239 usbg_get_function_type(func));
240 char *instance = (char *)usbg_get_function_instance(func);
242 ret = cfs_alloc_new_func(usb_gadget, func_name, instance,
243 usb_gadget->funcs + i);
252 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
259 static struct usb_function *cfs_find_func_in_gadget(
260 struct usb_gadget *gadget, const char *name, const char *instance)
264 for (i = 0; gadget->funcs[i]; ++i)
265 if (cfs_match_func(gadget->funcs[i], name, instance))
266 return gadget->funcs[i];
271 static int cfs_alloc_config(int n_funcs, struct usb_configuration **_config)
273 struct usb_configuration *config;
275 config = zalloc(sizeof(*config));
279 config->strs = calloc(2, sizeof(*config->strs));
283 config->funcs = calloc(n_funcs + 1, sizeof(*config->funcs));
298 static int cfs_read_config(usbg_config *config, struct usb_gadget *gadget,
299 struct usb_configuration *usb_config)
303 char *name, *instance;
304 struct usbg_config_attrs c_attrs;
305 struct usbg_config_strs c_strs;
309 usbg_for_each_binding(b, config) {
310 func = usbg_get_binding_target(b);
312 name = (char *)usbg_get_function_type_str(
313 usbg_get_function_type(func));
314 instance = (char *)usbg_get_function_instance(func);
316 usb_config->funcs[i] = cfs_find_func_in_gadget(gadget,
318 if (!usb_config->funcs[i]) {
324 ret = usbg_get_config_attrs(config, &c_attrs);
328 usb_config->attrs.MaxPower = c_attrs.bMaxPower*2;
329 usb_config->attrs.bmAttributs = c_attrs.bmAttributes;
331 ret = usbg_get_config_strs(config, LANG_US_ENG, &c_strs);
333 usb_config->strs[0].lang_code = 0;
335 usb_config->strs[0].lang_code = LANG_US_ENG;
336 usb_config->strs[0].config_str = strdup(c_strs.configuration);
337 if (!usb_config->strs[0].config_str)
344 static int cfs_count_bindings(usbg_config *config)
349 usbg_for_each_binding(b, config) ++i;
354 static int cfs_read_configs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
361 usbg_for_each_config(config, gadget) {
362 n_funcs = cfs_count_bindings(config);
364 ret = cfs_alloc_config(n_funcs, usb_gadget->configs + i);
367 ret = cfs_read_config(config, usb_gadget,
368 usb_gadget->configs[i]);
377 free(usb_gadget->configs[i]->strs);
378 free(usb_gadget->configs[i]->funcs);
379 free(usb_gadget->configs[i]);
382 cfs_free_config(usb_gadget->configs[i--]);
386 static int cfs_count_configs(usbg_gadget *gadget)
391 usbg_for_each_config(config, gadget) ++i;
396 static int cfs_count_functions(usbg_gadget *gadget)
401 usbg_for_each_function(func, gadget) ++i;
406 static int cfs_get_current_gadget(struct usb_client *usb,
407 struct usb_gadget **_usb_gadget)
409 struct cfs_client *cfs_client;
410 struct usb_gadget *usb_gadget;
411 struct usb_gadget_strings *strs;
412 struct usb_configuration **usb_configs;
413 struct usb_function **usb_funcs;
414 int n_funcs, n_configs;
421 cfs_client = container_of(usb, struct cfs_client,
424 usb_gadget = zalloc(sizeof(*usb_gadget));
429 * Currently there is no interface in libusbg which
430 * allows to list all string languages.
431 * That's why we do this only for USA english
433 strs = calloc(2, sizeof(*strs));
437 strs[0].lang_code = LANG_US_ENG;
439 usb_gadget->strs = strs;
441 ret = cfs_read_gadget_attrs_strs(cfs_client->gadget, usb_gadget);
446 n_funcs = cfs_count_functions(cfs_client->gadget);
447 usb_funcs = calloc(n_funcs + 1, sizeof(*usb_funcs));
449 goto free_strs_with_content;
451 usb_gadget->funcs = usb_funcs;
453 ret = cfs_read_funcs(cfs_client->gadget, usb_gadget);
457 n_configs = cfs_count_configs(cfs_client->gadget);
458 usb_configs = calloc(n_configs + 1, sizeof(*usb_configs));
460 goto free_funcs_with_content;
462 usb_gadget->configs = usb_configs;
464 ret = cfs_read_configs(cfs_client->gadget, usb_gadget);
468 *_usb_gadget = usb_gadget;
473 free_funcs_with_content:
474 for (i = 0; usb_gadget->funcs[i]; ++i)
475 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
478 free_strs_with_content:
479 for (i = 0; usb_gadget->strs[i].lang_code; ++i) {
480 free(usb_gadget->strs[i].manufacturer);
481 free(usb_gadget->strs[i].product);
482 free(usb_gadget->strs[i].serial);
485 free(usb_gadget->strs);
492 static bool cfs_is_function_supported(struct usb_client *usb,
493 struct usb_function *func)
498 switch (func->function_group) {
499 case USB_FUNCTION_GROUP_SIMPLE:
500 ret = usbg_lookup_function_type(func->name);
503 case USB_FUNCTION_GROUP_WITH_SERVICE:
504 /* TODO: Check if socket is available */
514 static bool cfs_is_gadget_supported(struct usb_client *usb,
515 struct usb_gadget *gadget)
519 if (!gadget || !gadget->configs || !gadget->funcs)
524 * Here is a good place to ensure that serial is immutable
527 /* No real restrictions for strings */
528 for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
529 struct usb_configuration *config = gadget->configs[j];
534 for (i = 0; config->funcs[i]; ++i)
535 if (!cfs_is_function_supported(usb, config->funcs[i]))
545 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
546 struct usb_gadget_attrs *attrs)
549 struct usbg_gadget_attrs gadget_attrs;
551 ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
555 gadget_attrs.bDeviceClass = attrs->bDeviceClass;
556 gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
557 gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
558 gadget_attrs.idVendor = attrs->idVendor;
559 gadget_attrs.idProduct = attrs->idProduct;
560 gadget_attrs.bcdDevice = attrs->bcdDevice;
562 ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
567 static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
568 struct usb_gadget_strings *strs)
574 * Here is a good place to ensure that serial is immutable
576 #define SET_STR(FIELD, STR_ID) \
578 ret = usbg_set_gadget_str(cfs_client->gadget, \
586 SET_STR(manufacturer, USBG_STR_MANUFACTURER);
587 SET_STR(product, USBG_STR_PRODUCT);
588 SET_STR(serial, USBG_STR_SERIAL_NUMBER);
593 static int cfs_ensure_dir(char *path)
597 ret = mkdir(path, 0770);
599 ret = errno == EEXIST ? 0 : errno;
604 static int cfs_prep_ffs_service(const char *name, const char *instance,
605 const char *dev_name, const char *socket_name)
612 /* TODO: Add some good error handling */
616 ret = snprintf(pos, left, "%s", USB_FUNCS_PATH);
617 if (ret < 0 || ret >= left) {
618 return -ENAMETOOLONG;
623 ret = cfs_ensure_dir(buf);
627 ret = snprintf(pos, left, "/%s", name);
628 if (ret < 0 || ret >= left) {
629 return -ENAMETOOLONG;
634 ret = cfs_ensure_dir(buf);
638 ret = snprintf(pos, left, "/%s", instance);
639 if (ret < 0 || ret >= left) {
640 return -ENAMETOOLONG;
645 ret = cfs_ensure_dir(buf);
649 ret = mount(dev_name, buf, "functionfs", 0, NULL);
653 ret = systemd_start_socket(socket_name);
663 static int cfs_set_gadget_config(struct cfs_client *cfs_client,
665 struct usb_configuration *usb_config)
667 struct usbg_config_attrs cattrs = {
668 .bmAttributes = usb_config->attrs.bmAttributs,
669 .bMaxPower = usb_config->attrs.MaxPower/2,
675 if (!usb_config->funcs || !usb_config->funcs[0])
678 config = usbg_get_config(cfs_client->gadget, config_id, NULL);
680 ret = usbg_rm_config(config, USBG_RM_RECURSE);
685 ret = usbg_create_config(cfs_client->gadget, config_id,
686 CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
690 for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
691 ret = usbg_set_config_string(config, usb_config->strs[i].lang_code,
692 usb_config->strs[i].config_str);
697 for (i = 0; usb_config->funcs && usb_config->funcs[i]; ++i) {
698 struct usb_function *usb_func = usb_config->funcs[i];
699 char instance[MAX_INSTANCE_LEN];
703 switch (usb_func->function_group) {
704 case USB_FUNCTION_GROUP_SIMPLE:
705 type = usbg_lookup_function_type(usb_func->name);
706 if (strlen(usb_func->instance) >= MAX_INSTANCE_LEN)
707 return -ENAMETOOLONG;
708 strncpy(instance, usb_func->instance, MAX_INSTANCE_LEN);
709 instance[MAX_INSTANCE_LEN - 1] = '\0';
711 case USB_FUNCTION_GROUP_WITH_SERVICE:
713 ret = snprintf(instance, sizeof(instance), "%s%c%s",
714 usb_func->name, NAME_INSTANCE_SEP,
716 if (ret < 0 || ret >= sizeof(instance))
717 return -ENAMETOOLONG;
724 func = usbg_get_function(cfs_client->gadget, type, instance);
726 ret = usbg_create_function(cfs_client->gadget,
733 if (usb_func->function_group ==
734 USB_FUNCTION_GROUP_WITH_SERVICE) {
735 struct usb_function_with_service *fws;
737 fws = container_of(usb_func,
738 struct usb_function_with_service,
740 ret = cfs_prep_ffs_service(usb_func->name,
750 ret = usbg_add_config_function(config, NULL, func);
758 static int cfs_cleanup_left_configs(struct cfs_client *cfs_client,
761 usbg_config *lconfig, *config;
764 lconfig = usbg_get_config(cfs_client->gadget, last_config, NULL);
765 for (config = usbg_get_next_config(lconfig);
767 config = usbg_get_next_config(lconfig)) {
768 ret = usbg_rm_config(config, USBG_RM_RECURSE);
776 static int cfs_reconfigure_gadget(struct usb_client *usb,
777 struct usb_gadget *gadget)
779 struct cfs_client *cfs_client;
786 cfs_client = container_of(usb, struct cfs_client,
789 if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
792 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
796 for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
797 ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
802 for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
803 ret = cfs_set_gadget_config(cfs_client, i + 1,
809 /* Workaround for enabling extcon notification */
810 ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
814 ret = cfs_cleanup_left_configs(cfs_client, i);
817 * Cleanup things which are left after previous gadget
823 static int cfs_enable(struct usb_client *usb)
825 struct cfs_client *cfs_client;
830 cfs_client = container_of(usb, struct cfs_client,
833 return usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
836 static int cfs_disable(struct usb_client *usb)
838 struct cfs_client *cfs_client;
843 cfs_client = container_of(usb, struct cfs_client,
846 return usbg_disable_gadget(cfs_client->gadget);
850 int hw_cfs_gadget_open(struct hw_info *info,
851 const char *id, struct hw_common **common)
853 struct cfs_client *cfs_client;
856 if (!info || !common)
859 cfs_client = zalloc(sizeof(*cfs_client));
863 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
867 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
868 if (!cfs_client->udc) {
873 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
874 &default_g_attrs, &default_g_strs,
875 &cfs_client->gadget);
877 goto err_create_gadget;
879 cfs_client->client.common.info = info;
880 cfs_client->client.get_current_gadget = cfs_get_current_gadget;
881 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
882 cfs_client->client.is_gadget_supported = cfs_is_gadget_supported;
883 cfs_client->client.is_function_supported = cfs_is_function_supported;
884 cfs_client->client.enable = cfs_enable;
885 cfs_client->client.disable = cfs_disable;
886 cfs_client->client.free_gadget = cfs_free_gadget;
888 *common = &cfs_client->client.common;
893 usbg_cleanup(cfs_client->ctx);
901 int hw_cfs_gadget_close(struct hw_common *common)
903 struct cfs_client *cfs_client;
908 cfs_client = container_of(common, struct cfs_client,
912 * For now we don't check for errors
913 * but we should somehow handle them
915 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
916 usbg_cleanup(cfs_client->ctx);