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.
22 #include <sys/types.h>
23 #include <sys/mount.h>
25 #include <usbg/usbg.h>
26 #include <usbg/function/net.h>
27 #include <libsyscommon/dbus-systemd.h>
29 #include <hw/usb_client.h>
31 #define CONFIGFS_PATH "/sys/kernel/config"
33 #define CONFIGFS_GADGET_NAME "hal-gadget"
34 #define CONFIGFS_CONFIG_LABEL "hal-config"
36 #define NAME_INSTANCE_SEP '.'
37 #define MAX_INSTANCE_LEN 512
39 #define USB_FUNCS_PATH "/dev/usb-funcs"
42 #define EXPORT __attribute__ ((visibility("default")))
45 enum cfs_function_service_operation {
46 CFS_FUNCTION_SERVICE_START,
47 CFS_FUNCTION_SERVICE_STOP,
48 CFS_FUNCTION_SERVICE_POST_STOP,
52 struct usb_client client;
58 /* Based on values in slp-gadget kernel module */
59 struct usbg_gadget_attrs default_g_attrs = {
66 struct usbg_gadget_strs default_g_strs = {
67 .manufacturer = "Samsung",
69 .serial = "01234TEST",
72 static struct usb_function *cfs_find_usb_function(usbg_function *function)
75 char buf[MAX_INSTANCE_LEN];
76 const char *instance = usbg_get_function_instance(function);
77 const char *name = usbg_get_function_type_str(usbg_get_function_type(function));
79 /* Ex. name:"ffs", instance: "sdb.default" */
80 if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS)) == 0) {
81 strncpy(buf, instance, sizeof(buf) - 1);
82 buf[sizeof(buf) - 1] = '\0';
84 /* Ex. "sdb.default" ==> "sdb" + "default" */
85 sep = strchr(buf, NAME_INSTANCE_SEP);
94 return find_usb_function_by_name_instance(name, instance);
97 static bool cfs_is_function_supported(struct usb_client *usb,
98 struct usb_function *func)
103 if (!func->is_functionfs) {
104 ret = usbg_lookup_function_type(func->name);
107 /* TODO: Check if socket is available */
114 static bool cfs_is_gadget_supported(struct usb_client *usb,
115 struct usb_gadget *gadget)
119 if (!gadget || !gadget->configs || !gadget->funcs)
122 /* No real restrictions for strings */
123 for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
124 struct usb_configuration *config = gadget->configs[j];
129 for (i = 0; config->funcs[i]; ++i)
130 if (!cfs_is_function_supported(usb, config->funcs[i]))
140 static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
141 struct usb_gadget_attrs *attrs)
144 struct usbg_gadget_attrs gadget_attrs;
146 ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
150 gadget_attrs.bDeviceClass = attrs->bDeviceClass;
151 gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
152 gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
153 gadget_attrs.idVendor = attrs->idVendor;
154 gadget_attrs.idProduct = attrs->idProduct;
155 gadget_attrs.bcdDevice = attrs->bcdDevice;
157 ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);
162 static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
163 struct usb_gadget_strings *strs)
169 * Here is a good place to ensure that serial is immutable
171 #define SET_STR(FIELD, STR_ID) \
173 ret = usbg_set_gadget_str(cfs_client->gadget, \
181 SET_STR(manufacturer, USBG_STR_MANUFACTURER);
182 SET_STR(product, USBG_STR_PRODUCT);
183 SET_STR(serial, USBG_STR_SERIAL_NUMBER);
188 static int cfs_ensure_dir(char *path)
190 if (mkdir(path, 0770) < 0)
191 return (errno == EEXIST) ? 0 : -errno;
197 static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
202 const char *instance;
203 const char *dev_name;
204 char buf[MAX_INSTANCE_LEN];
206 if (!usb_func || !function)
209 if (usbg_get_function_type(function) != USBG_F_FFS)
212 name = usb_func->name;
213 service = usb_func->service;
214 instance = usb_func->instance;
215 dev_name = usbg_get_function_instance(function);
217 /* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default" + '0' */
218 if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3 > sizeof(buf))
219 return -ENAMETOOLONG;
221 /* mkdir /dev/usb-funcs */
222 ret = cfs_ensure_dir(USB_FUNCS_PATH);
226 /* mkdir /dev/usb-funcs/sdb */
227 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
228 ret = cfs_ensure_dir(buf);
232 /* mkdir /dev/usb-funcs/sdb/default */
233 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
234 ret = cfs_ensure_dir(buf);
238 /* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
239 ret = mount(dev_name, buf, "functionfs", 0, NULL);
243 /* start sdbd.socket */
244 ret = systemd_start_unit_wait_started(service, ".socket", -1);
254 snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
257 snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
260 rmdir(USB_FUNCS_PATH);
265 static int cfs_cleanup_ffs_service(usbg_function *function)
268 char buf[MAX_INSTANCE_LEN];
269 struct usb_function *usb_function;
274 usb_function = cfs_find_usb_function(function);
278 /* stop .socket first and stop .service later becuase of socket activation */
279 if (usb_function->service) {
280 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
281 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
284 /* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
285 ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
297 /* remove /dev/usb-funcs/[sdb|mtp] directory */
298 ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
303 if (ret < 0 && errno != ENOTEMPTY)
306 /* remove /dev/usb-funcs/ directory */
307 ret = rmdir(USB_FUNCS_PATH);
308 if (ret < 0 && errno != ENOTEMPTY)
315 static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
318 struct ether_addr ethaddr;
319 struct usbg_gadget_strs strs;
320 struct usbg_f_net *nf = usbg_to_net_function(func);
325 ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
326 if (ret != USBG_SUCCESS)
329 for (i = 0; i < ETHER_ADDR_LEN; i++)
330 ethaddr.ether_addr_octet[i] = 0;
332 for (i = 0; (i < 256) && strs.serial[i]; i++) {
333 ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
335 ethaddr.ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
336 ethaddr.ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
338 usbg_free_gadget_strs(&strs);
340 /* host_addr changes mac address */
341 ret = usbg_f_net_set_host_addr(nf, ðaddr);
346 static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
350 usbg_function *function;
352 /* delete all configs */
354 usbg_for_each_config(config, cfs_client->gadget) {
355 ret = usbg_rm_config(config, USBG_RM_RECURSE);
359 goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
362 /* delete all functions */
364 usbg_for_each_function(function, cfs_client->gadget) {
365 if (usbg_get_function_type(function) == USBG_F_FFS) {
366 ret = cfs_cleanup_ffs_service(function);
371 ret = usbg_rm_function(function, USBG_RM_RECURSE);
375 goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
381 static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
387 usbg_function *function;
388 struct usb_function *usb_func;
389 char instance[MAX_INSTANCE_LEN];
390 struct usbg_config_attrs cattrs = {
391 .bmAttributes = usb_config->attrs.bmAttributs,
392 .bMaxPower = usb_config->attrs.MaxPower/2,
395 if (!usb_config->funcs || !usb_config->funcs[0])
398 ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
402 for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
403 ret = usbg_set_config_string(config, usb_config->strs[i].lang_code, usb_config->strs[i].config_str);
408 for (i = 0; usb_config->funcs[i]; ++i) {
409 usb_func = usb_config->funcs[i];
411 /* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
412 if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
413 return -ENAMETOOLONG;
415 /* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
416 if (usb_func->is_functionfs) {
417 function_type = USBG_F_FFS;
418 snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
420 function_type = usbg_lookup_function_type(usb_func->name);
421 strncpy(instance, usb_func->instance, sizeof(instance) - 1);
422 instance[sizeof(instance) - 1] = '\0';
425 function = usbg_get_function(cfs_client->gadget, function_type, instance);
427 ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
431 /* Setting rndis mac address. This should be done at this point,
432 * since the node host_addr changes to read only after the function
433 * is added to config. */
434 if (usbg_get_function_type(function) == USBG_F_RNDIS)
435 (void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */
437 if (usbg_get_function_type(function) == USBG_F_FFS) {
438 ret = cfs_prep_ffs_service(usb_func, function);
444 ret = usbg_add_config_function(config, NULL, function);
452 static int cfs_reconfigure_gadget(struct usb_client *usb,
453 struct usb_gadget *gadget)
457 struct cfs_client *cfs_client;
459 if (!usb || !gadget || !cfs_is_gadget_supported(usb, gadget))
462 cfs_client = container_of(usb, struct cfs_client, client);
464 ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
468 for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
469 ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
474 ret = cfs_cleanup_all_config_and_function(cfs_client);
478 for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
479 ret = cfs_set_gadget_config(cfs_client, i + 1, gadget->configs[i]);
487 static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
489 usbg_function *function;
490 struct usb_function *usb_function;
492 usbg_for_each_function(function, gadget) {
493 usb_function = cfs_find_usb_function(function);
498 case CFS_FUNCTION_SERVICE_START:
499 if (usb_function->handler)
500 usb_function->handler(1);
502 /* functionfs service is automatically started by socket activation */
503 if (!usb_function->is_functionfs && usb_function->service)
504 (void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
507 case CFS_FUNCTION_SERVICE_STOP:
508 if (!usb_function->is_functionfs && usb_function->service)
509 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
511 if (usb_function->handler)
512 usb_function->handler(0);
515 case CFS_FUNCTION_SERVICE_POST_STOP:
516 if (usb_function->is_functionfs && usb_function->service)
517 (void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
526 static int cfs_enable(struct usb_client *usb)
529 struct cfs_client *cfs_client;
534 cfs_client = container_of(usb, struct cfs_client, client);
536 ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
540 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_START);
545 static int cfs_disable(struct usb_client *usb)
548 struct cfs_client *cfs_client;
553 cfs_client = container_of(usb, struct cfs_client, client);
555 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);
557 ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */
560 * Since functionfs service works with socket activation, you must stop it after disabling gadget.
561 * If usb data may come in after stopping functionfs service and before disabling gadget,
562 * functionfs service wakes up again by socket activation.
564 cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);
570 int hw_cfs_gadget_open(struct hw_info *info,
571 const char *id, struct hw_common **common)
573 struct cfs_client *cfs_client;
576 if (!info || !common)
579 cfs_client = calloc(1, sizeof(*cfs_client));
583 ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
587 cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
588 if (!cfs_client->udc) {
593 ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
594 &default_g_attrs, &default_g_strs,
595 &cfs_client->gadget);
597 goto err_create_gadget;
599 cfs_client->client.common.info = info;
601 cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
602 cfs_client->client.enable = cfs_enable;
603 cfs_client->client.disable = cfs_disable;
605 *common = &cfs_client->client.common;
611 usbg_cleanup(cfs_client->ctx);
619 int hw_cfs_gadget_close(struct hw_common *common)
621 usbg_function *function;
622 struct cfs_client *cfs_client;
623 struct usb_function *usb_func;
628 cfs_client = container_of(common, struct cfs_client, client.common);
630 usbg_for_each_function(function, cfs_client->gadget) {
631 usb_func = cfs_find_usb_function(function);
635 if (usb_func->is_functionfs && usb_func->service) {
636 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
637 (void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
642 * For now we don't check for errors
643 * but we should somehow handle them
645 usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
646 usbg_cleanup(cfs_client->ctx);