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"
26 #include <usbg/usbg.h>
28 #define zalloc(amount) calloc(1, amount)
30 #define MAX_GADGET_STR_LEN 256
33 #define CONFIGFS_PATH "/sys/kernel/config"
35 #define CONFIGFS_GADGET_NAME "hal-gadget"
36 #define CONFIGFS_CONFIG_LABEL "hal-config"
39 struct usb_client client;
45 /* Based on values in slp-gadget kernel module */
46 struct usbg_gadget_attrs default_g_attrs = {
53 struct usbg_gadget_strs default_g_strs = {
54 .manufacturer = "Samsung",
56 .serial = "01234TEST",
59 static void cfs_free_config(struct usb_configuration *config)
67 for (i = 0; config->strs[i].lang_code; ++i)
68 free(config->strs[i].config_str);
74 * Each function will be free later,
75 * for now we cleanup only pointers.
83 static void cfs_free_gadget(struct usb_gadget *gadget)
91 for (i = 0; gadget->strs[i].lang_code; ++i) {
92 free(gadget->strs[i].manufacturer);
93 free(gadget->strs[i].product);
94 free(gadget->strs[i].serial);
99 if (gadget->configs) {
100 for (i = 0; gadget->configs[i]; ++i)
101 cfs_free_config(gadget->configs[i]);
103 free(gadget->configs);
107 for (i = 0; gadget->funcs[i]; ++i)
108 gadget->funcs[i]->free_func(gadget->funcs[i]);
114 static int cfs_read_gadget_attrs_strs(usbg_gadget *gadget,
115 struct usb_gadget *usb_gadget)
117 struct usbg_gadget_attrs attrs;
118 struct usbg_gadget_strs strs;
121 ret = usbg_get_gadget_attrs(gadget, &attrs);
125 usb_gadget->attrs.bDeviceClass = attrs.bDeviceClass;
126 usb_gadget->attrs.bDeviceSubClass = attrs.bDeviceSubClass;
127 usb_gadget->attrs.bDeviceProtocol = attrs.bDeviceProtocol;
128 usb_gadget->attrs.idVendor = attrs.idVendor;
129 usb_gadget->attrs.idProduct = attrs.idProduct;
130 usb_gadget->attrs.bcdDevice = attrs.bcdDevice;
133 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
137 usb_gadget->strs[0].manufacturer = strdup(strs.manufacturer);
138 usb_gadget->strs[0].product = strdup(strs.product);
139 usb_gadget->strs[0].serial = strdup(strs.serial);
141 if (!usb_gadget->strs[0].manufacturer ||
142 !usb_gadget->strs[0].product ||
143 !usb_gadget->strs[0].serial) {
150 free(usb_gadget->strs[0].manufacturer);
151 free(usb_gadget->strs[0].product);
152 free(usb_gadget->strs[0].serial);
157 static int cfs_find_func(const char *name, const char *instance)
161 for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i)
162 if (!strcmp(name, _available_funcs[i]->name) &&
163 !strcmp(instance, _available_funcs[i]->instance))
169 static int cfs_alloc_new_func(struct usb_gadget *gadget, const char *fname,
170 const char *instance, struct usb_function **_func)
172 struct usb_function *func;
175 ret = cfs_find_func(fname, instance);
179 ret = _available_funcs[ret]->clone(_available_funcs[ret], &func);
187 static int cfs_read_funcs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
194 usbg_for_each_function(func, gadget) {
195 char *func_name = (char *)usbg_get_function_type_str(
196 usbg_get_function_type(func));
197 char *instance = (char *)usbg_get_function_instance(func);
199 ret = cfs_alloc_new_func(usb_gadget, func_name, instance,
200 usb_gadget->funcs + i);
209 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
216 static struct usb_function *cfs_find_func_in_gadget(
217 struct usb_gadget *gadget, const char *name, const char *instance)
221 for (i = 0; gadget->funcs[i]; ++i)
222 if (!strcmp(name, gadget->funcs[i]->name)
223 && !strcmp(instance, gadget->funcs[i]->instance))
224 return gadget->funcs[i];
228 static int cfs_alloc_config(int n_funcs, struct usb_configuration **_config)
230 struct usb_configuration *config;
232 config = zalloc(sizeof(*config));
236 config->strs = calloc(2, sizeof(*config->strs));
240 config->funcs = calloc(n_funcs + 1, sizeof(*config->funcs));
255 static int cfs_read_config(usbg_config *config, struct usb_gadget *gadget,
256 struct usb_configuration *usb_config)
260 char *name, *instance;
261 struct usbg_config_attrs c_attrs;
262 struct usbg_config_strs c_strs;
266 usbg_for_each_binding(b, config) {
267 func = usbg_get_binding_target(b);
269 name = (char *)usbg_get_function_type_str(
270 usbg_get_function_type(func));
271 instance = (char *)usbg_get_function_instance(func);
273 usb_config->funcs[i] = cfs_find_func_in_gadget(gadget,
275 if (!usb_config->funcs[i]) {
281 ret = usbg_get_config_attrs(config, &c_attrs);
285 usb_config->attrs.MaxPower = c_attrs.bMaxPower*2;
286 usb_config->attrs.bmAttributs = c_attrs.bmAttributes;
288 ret = usbg_get_config_strs(config, LANG_US_ENG, &c_strs);
290 usb_config->strs[0].lang_code = 0;
292 usb_config->strs[0].lang_code = LANG_US_ENG;
293 usb_config->strs[0].config_str = strdup(c_strs.configuration);
294 if (!usb_config->strs[0].config_str)
301 static int cfs_count_bindings(usbg_config *config)
306 usbg_for_each_binding(b, config) ++i;
311 static int cfs_read_configs(usbg_gadget *gadget, struct usb_gadget *usb_gadget)
318 usbg_for_each_config(config, gadget) {
319 n_funcs = cfs_count_bindings(config);
321 ret = cfs_alloc_config(n_funcs, usb_gadget->configs + i);
324 ret = cfs_read_config(config, usb_gadget,
325 usb_gadget->configs[i]);
334 free(usb_gadget->configs[i]->strs);
335 free(usb_gadget->configs[i]->funcs);
336 free(usb_gadget->configs[i]);
339 cfs_free_config(usb_gadget->configs[i]);
343 static int cfs_count_configs(usbg_gadget *gadget)
348 usbg_for_each_config(config, gadget) ++i;
353 static int cfs_count_functions(usbg_gadget *gadget)
358 usbg_for_each_function(func, gadget) ++i;
363 static int cfs_get_current_gadget(struct usb_client *usb,
364 struct usb_gadget **_usb_gadget)
366 struct cfs_client *cfs_client;
367 struct usb_gadget *usb_gadget;
368 struct usb_gadget_strings *strs;
369 struct usb_configuration **usb_configs;
370 struct usb_function **usb_funcs;
371 int n_funcs, n_configs;
378 cfs_client = container_of(usb, struct cfs_client,
381 usb_gadget = zalloc(sizeof(*usb_gadget));
386 * Currently there is no interface in libusbg which
387 * allows to list all string languages.
388 * That's why we do this only for USA english
390 strs = calloc(2, sizeof(*strs));
394 strs[0].lang_code = LANG_US_ENG;
396 usb_gadget->strs = strs;
398 ret = cfs_read_gadget_attrs_strs(cfs_client->gadget, usb_gadget);
403 n_funcs = cfs_count_functions(cfs_client->gadget);
404 usb_funcs = calloc(n_funcs + 1, sizeof(*usb_funcs));
406 goto free_strs_with_content;
408 usb_gadget->funcs = usb_funcs;
410 ret = cfs_read_funcs(cfs_client->gadget, usb_gadget);
414 n_configs = cfs_count_configs(cfs_client->gadget);
415 usb_configs = calloc(n_configs + 1, sizeof(*usb_configs));
417 goto free_funcs_with_content;
419 usb_gadget->configs = usb_configs;
421 ret = cfs_read_configs(cfs_client->gadget, usb_gadget);
425 *_usb_gadget = usb_gadget;
430 free_funcs_with_content:
431 for (i = 0; usb_gadget->funcs[i]; ++i)
432 usb_gadget->funcs[i]->free_func(usb_gadget->funcs[i]);
435 free_strs_with_content:
436 for (i = 0; usb_gadget->strs[i].lang_code; ++i) {
437 free(usb_gadget->strs[0].manufacturer);
438 free(usb_gadget->strs[0].product);
439 free(usb_gadget->strs[0].serial);
442 free(usb_gadget->strs);
449 static bool cfs_is_function_supported(struct usb_client *usb,
450 struct usb_function *func)
454 /* for now only simple functions without userspace service */
455 if (func->function_group != USB_FUNCTION_GROUP_SIMPLE)
460 * Instead of only checking whether we know this function
461 * we should also somehow check that it's realy available
464 ret = usbg_lookup_function_type(func->name);
469 static bool cfs_is_gadget_supported(struct usb_client *usb,
470 struct usb_gadget *gadget)
474 if (!gadget || !gadget->configs || !gadget->funcs)
479 * Here is a good place to ensure that serial is immutable
482 /* No real restrictions for strings */
483 for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
484 struct usb_configuration *config = gadget->configs[j];
489 for (i = 0; config->funcs[i]; ++i)
490 if (!cfs_is_function_supported(usb, config->funcs[i]))
500 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
501 struct usb_gadget_attrs *attrs)
504 struct usbg_gadget_attrs gadget_attrs;
506 ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
510 gadget_attrs.bDeviceClass = attrs->bDeviceClass;
511 gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
512 gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
513 gadget_attrs.idVendor = attrs->idVendor;
514 gadget_attrs.idProduct = attrs->idProduct;
515 gadget_attrs.bcdDevice = attrs->bcdDevice;
517 ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
522 static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
523 struct usb_gadget_strings *strs)
529 * Here is a good place to ensure that serial is immutable
531 #define SET_STR(FIELD, STR_ID) \
533 ret = usbg_set_gadget_str(cfs_client->gadget, \
541 SET_STR(manufacturer, USBG_STR_MANUFACTURER);
542 SET_STR(product, USBG_STR_PRODUCT);
543 SET_STR(serial, USBG_STR_SERIAL_NUMBER);
548 static int cfs_set_gadget_config(struct cfs_client *cfs_client,
550 struct usb_configuration *usb_config)
552 struct usbg_config_attrs cattrs = {
553 .bmAttributes = usb_config->attrs.bmAttributs,
554 .bMaxPower = usb_config->attrs.MaxPower/2,
560 if (!usb_config->funcs || !usb_config->funcs[0])
563 config = usbg_get_config(cfs_client->gadget, config_id, NULL);
565 ret = usbg_rm_config(config, USBG_RM_RECURSE);
570 ret = usbg_create_config(cfs_client->gadget, config_id,
571 CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
575 for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
576 ret = usbg_set_config_string(config, usb_config->strs[i].lang_code,
577 usb_config->strs[i].config_str);
582 for (i = 0; usb_config->funcs && usb_config->funcs[i]; ++i) {
583 struct usb_function *usb_func = usb_config->funcs[i];
584 int type = usbg_lookup_function_type(usb_func->name);
587 func = usbg_get_function(cfs_client->gadget,
588 type, usb_func->instance);
590 ret = usbg_create_function(cfs_client->gadget,
598 ret = usbg_add_config_function(config, NULL, func);
606 static int cfs_cleanup_left_configs(struct cfs_client *cfs_client,
609 usbg_config *lconfig, *config;
612 lconfig = usbg_get_config(cfs_client->gadget, last_config, NULL);
613 for (config = usbg_get_next_config(lconfig);
615 config = usbg_get_next_config(lconfig)) {
616 ret = usbg_rm_config(config, USBG_RM_RECURSE);
624 static int cfs_reconfigure_gadget(struct usb_client *usb,
625 struct usb_gadget *gadget)
627 struct cfs_client *cfs_client;
634 cfs_client = container_of(usb, struct cfs_client,
637 if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
640 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
644 for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
645 ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
650 for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
651 ret = cfs_set_gadget_config(cfs_client, i + 1,
657 ret = cfs_cleanup_left_configs(cfs_client, i);
660 * Cleanup things which are left after previous gadget
666 static int cfs_enable(struct usb_client *usb)
668 struct cfs_client *cfs_client;
673 cfs_client = container_of(usb, struct cfs_client,
676 return usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
679 static int cfs_disable(struct usb_client *usb)
681 struct cfs_client *cfs_client;
686 cfs_client = container_of(usb, struct cfs_client,
689 return usbg_disable_gadget(cfs_client->gadget);
692 static int cfs_gadget_open(struct hw_info *info,
693 const char *id, struct hw_common **common)
695 struct cfs_client *cfs_client;
698 if (!info || !common)
701 cfs_client = zalloc(sizeof(*cfs_client));
705 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
709 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
710 if (!cfs_client->udc) {
715 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
716 &default_g_attrs, &default_g_strs,
717 &cfs_client->gadget);
719 goto err_create_gadget;
721 cfs_client->client.common.info = info;
722 cfs_client->client.get_current_gadget = cfs_get_current_gadget;
723 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
724 cfs_client->client.is_gadget_supported = cfs_is_gadget_supported;
725 cfs_client->client.is_function_supported = cfs_is_function_supported;
726 cfs_client->client.enable = cfs_enable;
727 cfs_client->client.disable = cfs_disable;
728 cfs_client->client.free_gadget = cfs_free_gadget;
730 *common = &cfs_client->client.common;
735 usbg_cleanup(cfs_client->ctx);
742 static int cfs_gadget_close(struct hw_common *common)
744 struct cfs_client *cfs_client;
749 cfs_client = container_of(common, struct cfs_client,
753 * For now we don't check for errors
754 * but we should somehow handle them
756 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
757 usbg_cleanup(cfs_client->ctx);
763 HARDWARE_MODULE_STRUCTURE = {
764 .magic = HARDWARE_INFO_TAG,
765 .hal_version = HARDWARE_INFO_VERSION,
766 .device_version = USB_CLIENT_HARDWARE_DEVICE_VERSION,
767 .id = USB_CLIENT_HARDWARE_DEVICE_ID,
768 .name = "cfs-gadget",
769 .open = cfs_gadget_open,
770 .close = cfs_gadget_close,