#include "core/log.h"
-#include "usb.h"
#include "usb-gadget.h"
+#include "usb-gadget-ops.h"
#include "usb-debug.h"
/* Legacy signals */
return;
}
- _I("USB mode is changed by dbus signal to %d.", req_vconf);
+ // XXX: The mode has been changed to bitmap not only the predefined set but any possible combination
+ // of usb-gadget functions. But it hadn't been thoroughly investigated where this dbus signal is
+ // broadcasted. (I found one. It was invocation from factory testing of MCD, but that code has
+ // been dropped nowadays.) Therefore broadcasting from an old code with an old fashioned req_vconf
+ // might be an error.
+ _I("USB mode is changed by dbus signal to %#x.", req_vconf);
- mode = get_mode_bitmap_from_vconf(req_vconf);
+ mode = req_vconf;
- if (mode == USB_FUNCTION_INVALID) {
+ if (mode == USB_GADGET_FUNC_INVALID) {
_E("Failed to convert vconf to USB mode. There is no mode matches up with vconf %d.", req_vconf);
return;
}
- /* Deviced must never run in USB_FUNCTION_NONE mode. */
- if (mode == USB_FUNCTION_NONE) {
- _E("Ignore this request. There is USB_FUNCTION_NONE mode matches up with vconf %d.", req_vconf);
+ /* Deviced must never run in USB_GADGET_FUNC_NONE mode. */
+ if (mode == USB_GADGET_FUNC_NONE) {
+ _E("Ignore this request. There is USB_GADGET_FUNC_NONE mode matches up with vconf %d.", req_vconf);
return;
}
#include "core/log.h"
#include "shared/device-notifier.h"
-#include "usb.h"
#include "usb-gadget.h"
+#include "usb-gadget-ops.h"
static bool debug_state;
{
int ret;
unsigned int new_mode;
- bool rndis_is_enabled = false;
const unsigned int curr_mode = usb_state_get_selected_mode();
- if (curr_mode & USB_FUNCTION_RNDIS)
- rndis_is_enabled = true;
-
- /*
- * debug on + rndis on : USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS
- * debug on + rndis off : USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB
- * debug off + rndis on : USB_FUNCTION_RNDIS
- * debug off + rndis off : USB_FUNCTION_MTP | USB_FUNCTION_ACM
- */
-
- if ((int)(intptr_t) on) {
- if (rndis_is_enabled)
- new_mode = USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS;
- else
- new_mode = USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB;
- } else {
- if (rndis_is_enabled)
- new_mode = USB_FUNCTION_RNDIS;
- else
- new_mode = USB_FUNCTION_MTP | USB_FUNCTION_ACM;
- }
+ if ((int)(intptr_t) on)
+ new_mode = curr_mode | USB_GADGET_FUNC_SDB;
+ else
+ new_mode = curr_mode & (~USB_GADGET_FUNC_SDB);
ret = usb_change_mode(new_mode, false);
if (ret < 0)
* limitations under the License.
*/
-#ifndef __USB_DEBUG_H__
-#define __USB_DEBUG_H__
+#ifndef __USB_GADGET_DEBUG_H__
+#define __USB_GADGET_DEBUG_H__
#include <stdbool.h>
void add_usb_debug_handler(void);
void remove_usb_debug_handler(void);
-#endif /* __USB_DEBUG_H__ */
+#endif /* __USB_GADGET_DEBUG_H__ */
#include <libsyscommon/libsystemd.h>
#include <shared/log.h>
-#include "usb-gadget.h"
+#include "usb-gadget-ops.h"
#define CONFIGFS_PATH "/sys/kernel/config"
return 0;
while (getline(&line, &len, fp) != -1) {
- if (strstr(line, "configfs"))
+ if (strstr(line, "configfs")) {
configfs = 1;
+ break;
+ }
}
fclose(fp);
return configfs;
}
-void usb_gadget_bind_cfs_gadget(int (**open) (void), int (**close) (void), int (**enable) (void),
+void usb_gadget_bind_cfs_ops(int (**open) (void), int (**close) (void), int (**enable) (void),
int (**disable) (void), int (**reconfigure) (struct usb_gadget *))
{
*open = usb_gadget_cfs_open;
--- /dev/null
+#ifndef __USB_GADGET_CFS_OPS_H__
+#define __USB_GADGET_CFS_OPS_H__
+
+/* configfs/functionfs gadget */
+int usb_gadget_cfs_supported(void);
+void usb_gadget_bind_cfs_ops(int (**open) (void),
+ int (**close) (void),
+ int (**enable) (void),
+ int (**disable) (void),
+ int (**reconfigure) (struct usb_gadget *));
+
+#endif //__USB_GADGET_CFS_OPS_H__
#include <libsyscommon/libsystemd.h>
#include <shared/log.h>
-#include "usb-gadget.h"
+#include "usb-gadget-ops.h"
#define MAX_GADGET_STR_LEN 256
char *sep = LEGACY_FUNC_SEP;
char buf[MAX_GADGET_STR_LEN];
struct usb_function *func;
- struct usb_function *funcs[USB_FUNCTION_IDX_MAX];
+ struct usb_function *funcs[USB_GADGET_FUNC_IDX_MAX];
/* SLP gadget uses two USB configuration.
* (/sys/class/usb_mode/usb0/funcs_fconf and /sys/class/usb_mode/usb0/funcs_sconf)
if (funcs[i] == func)
continue;
- if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
+ if(n_func >= USB_GADGET_FUNC_IDX_MAX) /* What happen */
break;
funcs[n_func] = func;
if (funcs[i] == func)
continue;
- if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
+ if(n_func >= USB_GADGET_FUNC_IDX_MAX) /* What happen */
break;
funcs[n_func] = func;
return ret;
}
-void usb_gadget_bind_legacy_gadget(int (**open) (void), int (**close) (void), int (**enable) (void),
+void usb_gadget_bind_legacy_ops(int (**open) (void), int (**close) (void), int (**enable) (void),
int (**disable) (void), int (**reconfigure) (struct usb_gadget *))
{
*open = usb_gadget_legacy_open;
--- /dev/null
+#ifndef __USB_GADGET_LEGACY_OPS_H__
+#define __USB_GADGET_LEGACY_OPS_H__
+
+/* legacy samsung gadget */
+int usb_gadget_legacy_supported(void);
+void usb_gadget_bind_legacy_ops(int (**open) (void),
+ int (**close) (void),
+ int (**enable) (void),
+ int (**disable) (void),
+ int (**reconfigure) (struct usb_gadget *));
+
+#endif //__USB_GADGET_LEGACY_OPS_H__
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libsyscommon/libsystemd.h>
+#include <libsyscommon/list.h>
+#include <libsyscommon/ini-parser.h>
+#include <hal/device/hal-board.h>
+
+#include <shared/log.h>
+
+#include "usb-gadget-ops.h"
+#include "usb-gadget-cfs-ops.h"
+#include "usb-gadget-legacy-ops.h"
+
+#define PATH_USB_GADGET_CONF "/hal/etc/deviced/usb-gadget.conf"
+
+/* These usb-gadget operations are determined by available subsystem,
+ * configfs usb-gadget or legacy samsung-gadget */
+static int (*__usb_gadget_open) (void);
+static int (*__usb_gadget_close) (void);
+static int (*__usb_gadget_enable) (void);
+static int (*__usb_gadget_disable) (void);
+static int (*__usb_gadget_reconfigure) (struct usb_gadget *gadget);
+
+struct usb_gadget_config {
+ unsigned int mode; /* Bitmap of usb function combination */
+ struct usb_gadget_attrs attrs;
+};
+static GList *usb_gadget_config_list;
+
+struct service_config {
+ char name[128];
+ int remain_after_disable; /* do not stop the service on disabling usb-gadget function */
+};
+static GList *service_config_list;
+
+static char board_serial[128] = "01234TEST";
+
+static void rndis_handler(int enable)
+{
+ if (enable)
+ (void)systemd_start_unit_wait_started("rndis.service", NULL, -1);
+ else
+ (void)systemd_stop_unit_wait_stopped("rndis.service", NULL, -1);
+}
+
+#define DEFINE_USB_FUNCTION(_id, _name, _is_functionfs, _service, _handler) \
+ static struct usb_function _##_name##_function = { \
+ .id = _id, \
+ .name = #_name, \
+ .instance = "default", \
+ .is_functionfs = _is_functionfs, \
+ .service = _service, \
+ .remain_after_disable = 0, \
+ .handler = _handler, \
+ }
+
+DEFINE_USB_FUNCTION(USB_GADGET_FUNC_MTP, mtp, 1, "mtp-responder", NULL);
+DEFINE_USB_FUNCTION(USB_GADGET_FUNC_ACM, acm, 0, "data-router", NULL);
+DEFINE_USB_FUNCTION(USB_GADGET_FUNC_SDB, sdb, 1, "sdbd", NULL);
+DEFINE_USB_FUNCTION(USB_GADGET_FUNC_RNDIS, rndis, 0, "sshd", rndis_handler);
+DEFINE_USB_FUNCTION(USB_GADGET_FUNC_DIAG, diag, 1, "diag", NULL);
+
+#undef DEFINE_USB_FUNCTION
+
+static struct usb_function *available_funcs[] = {
+ [USB_GADGET_FUNC_IDX_MTP] = &_mtp_function,
+ [USB_GADGET_FUNC_IDX_ACM] = &_acm_function,
+ [USB_GADGET_FUNC_IDX_SDB] = &_sdb_function,
+ [USB_GADGET_FUNC_IDX_RNDIS] = &_rndis_function,
+ [USB_GADGET_FUNC_IDX_DIAG] = &_diag_function,
+ [USB_GADGET_FUNC_IDX_MAX] = NULL /* An indicator to end the array */
+};
+static struct usb_function *find_usb_function_by_id(int id);
+
+struct usb_function *find_usb_function_by_name(const char *name)
+{
+ int i;
+
+ if(!name || !name[0])
+ return NULL;
+
+ for (i = 0; available_funcs[i]; i++)
+ if (!strcmp(name, available_funcs[i]->name))
+ return available_funcs[i];
+
+ return NULL;
+}
+
+struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance)
+{
+ int i;
+
+ if(!name || !name[0] || !instance || !instance[0])
+ return NULL;
+
+ for (i = 0; available_funcs[i]; ++i)
+ if (!strcmp(name, available_funcs[i]->name) && !strcmp(instance, available_funcs[i]->instance))
+ return available_funcs[i];
+
+ return NULL;
+}
+
+static void simple_cleanup_config(struct usb_configuration *config)
+{
+ if (!config)
+ return;
+
+ free(config->strs.config_str);
+
+ if (config->funcs)
+ free(config->funcs);
+
+ free(config);
+}
+
+static void cleanup_gadget(struct usb_gadget *gadget)
+{
+ int i;
+
+ if (!gadget)
+ return;
+
+ free(gadget->strs.manufacturer);
+ free(gadget->strs.product);
+ free(gadget->strs.serial);
+
+ if (gadget->configs) {
+ for (i = 0; gadget->configs[i]; ++i)
+ simple_cleanup_config(gadget->configs[i]);
+
+ free(gadget->configs);
+ }
+
+ free(gadget);
+}
+
+static int alloc_default_config(struct usb_configuration **_config)
+{
+ struct usb_configuration *config;
+
+ config = calloc(1, sizeof(*config));
+ if (!config)
+ return -ENOMEM;
+
+ config->attrs.bmAttributs = DEFAULT_BMATTRIBUTES;
+ config->attrs.MaxPower = DEFAULT_MAX_POWER;
+
+ /* TODO. Here is where to set the string used in config of configfs */
+
+ *_config = config;
+
+ return 0;
+}
+
+static int alloc_default_gadget(struct usb_gadget **_gadget)
+{
+ struct usb_gadget *gadget;
+ struct usb_configuration **configs;
+
+ gadget = calloc(1, sizeof(*gadget));
+ if (!gadget)
+ goto out;
+
+ gadget->attrs.idVendor = DEFAULT_VID;
+ gadget->attrs.idProduct = DEFAULT_PID;
+ gadget->attrs.bcdDevice = DEFAULT_BCD_DEVICE;
+
+ gadget->strs.lang_code = DEFAULT_LANG;
+ gadget->strs.manufacturer = strdup(DEFAULT_MANUFACTURER);
+ gadget->strs.product = strdup(DEFAULT_PRODUCT);
+ gadget->strs.serial = strdup(board_serial);
+
+ if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
+ goto free_strs;
+
+ /* slp-gadget use max 2 confiuration and NULL termination */
+ configs = calloc(3, sizeof(*configs));
+ if (!configs)
+ goto free_strs;
+
+ gadget->configs = configs;
+ *_gadget = gadget;
+
+ return 0;
+
+free_strs:
+ free(gadget->strs.manufacturer);
+ free(gadget->strs.product);
+ free(gadget->strs.serial);
+ free(gadget);
+out:
+ return -ENOMEM;
+}
+
+static int id_to_gadget(struct usb_gadget_id *gadget_id, struct usb_gadget **_gadget)
+{
+ int ret;
+ int i, j;
+ int n_configs;
+ struct usb_gadget *gadget;
+ int functions[2][sizeof(gadget_id->function_mask)*8]; /* zero terminates */
+
+ GList *elem;
+ const struct usb_gadget_config *gc = NULL;
+
+ if (!gadget_id || !_gadget)
+ return -EINVAL;
+
+ ret = alloc_default_gadget(&gadget);
+ if (ret)
+ goto out;
+
+ /* set custom attrs */
+ SYS_G_LIST_FOREACH(usb_gadget_config_list, elem, gc) {
+ if (gc->mode == gadget_id->function_mask) {
+ if (gc->attrs.idVendor) {
+ gadget->attrs.idVendor = gc->attrs.idVendor;
+ _D("Reconfigure usb-gadget attribute idVendor=%#x", gadget->attrs.idVendor);
+ }
+ if (gc->attrs.idProduct) {
+ gadget->attrs.idProduct = gc->attrs.idProduct;
+ _D("Reconfigure usb-gadget attribute idProduct=%#x", gadget->attrs.idProduct);
+ }
+ break;
+ }
+ }
+
+ if ((gadget_id->function_mask & USB_GADGET_FUNC_SDB) && (gadget->attrs.idVendor != DEFAULT_VID))
+ _W("The sdb of host PC might not able to recognize target as it expects idVendor of %#x(current=%#x).",
+ DEFAULT_VID, gadget->attrs.idVendor);
+
+ j = 0;
+ n_configs = 1;
+ for (i = 0; i < USB_GADGET_FUNC_IDX_MAX; ++i) {
+ if (gadget_id->function_mask & (1 << i))
+ functions[0][j++] = (1 << i);
+ }
+ functions[0][j] = 0;
+
+ for (j = 0; j < n_configs; ++j) {
+ int n_funcs_in_config;
+ struct usb_configuration *config;
+
+ for (i = 0; functions[j][i]; ++i);
+ n_funcs_in_config = i;
+
+ ret = alloc_default_config(&config);
+ if (ret)
+ goto free_configs;
+
+ gadget->configs[j] = config;
+ config->funcs = calloc(n_funcs_in_config + 1, sizeof(void *));
+ if (!config->funcs)
+ goto free_configs;
+
+ for (i = 0; functions[j][i]; ++i) {
+ config->funcs[i] = find_usb_function_by_id(functions[j][i]);
+ if (!config->funcs[i])
+ goto free_configs;
+ }
+ }
+
+ *_gadget = gadget;
+
+ return 0;
+
+free_configs:
+ cleanup_gadget(gadget);
+out:
+ return ret;
+}
+
+
+static struct usb_function *find_usb_function_by_id(int id)
+{
+ int i;
+
+ for (i = 0; available_funcs[i]; i++)
+ if (available_funcs[i]->id == id)
+ return available_funcs[i];
+
+ return NULL;
+}
+
+
+int usb_gadget_enable(void)
+{
+ if (!__usb_gadget_enable) {
+ _E("Not supported usb_gadget_enable.");
+ return -ENOTSUP;
+ }
+
+ return __usb_gadget_enable();
+}
+
+int usb_gadget_disable(void)
+{
+ if (!__usb_gadget_disable) {
+ _E("Not supported usb_gadget_disable.");
+ return -ENOTSUP;
+ }
+
+ return __usb_gadget_disable();
+}
+
+int usb_gadget_change_mode(unsigned int mode)
+{
+ struct usb_gadget *gadget;
+ struct usb_gadget_id gadget_id = {0, };
+ int ret;
+
+ if (!__usb_gadget_reconfigure) {
+ _E("Not supported usb_gadget_reconfigure.");
+ return -ENOTSUP;
+ }
+
+ gadget_id.function_mask = mode;
+
+ ret = id_to_gadget(&gadget_id, &gadget);
+ if (ret) {
+ _E("Failed to id_to_gadget, %d", ret);
+ return ret;
+ }
+
+ ret = __usb_gadget_reconfigure(gadget);
+ cleanup_gadget(gadget);
+ if (ret) {
+ _E("Failed to reconfigure gadget, %d", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+
+static void parse_property_systemd_unit(gpointer data, gpointer udata)
+{
+ struct section_property *prop = (struct section_property *) data;
+ struct service_config *svc = (struct service_config *) udata;
+
+ if (MATCH(prop->key, "Service")) {
+ strncpy(svc->name, prop->value, sizeof(svc->name) - 1);
+ svc->name[sizeof(svc->name) - 1] = '\0';
+ } else if (MATCH(prop->key, "RemainAfterDisable")) {
+ svc->remain_after_disable = MATCH(prop->value, "yes");
+ }
+}
+
+static unsigned int parse_usb_function(char *fstr)
+{
+ // Parse Function=, e.g., "Function=diag|acm"
+ unsigned int ret = USB_GADGET_FUNC_NONE;
+ char *p, *saveptr;
+
+ if (!fstr)
+ return ret;
+
+ p = strtok_r(fstr, "|", &saveptr);
+ if (!p)
+ return ret;
+
+ do {
+ if (strncasecmp(p, "mtp", sizeof("mtp")) == 0) {
+ ret |= USB_GADGET_FUNC_MTP;
+ } else if (strncasecmp(p, "acm", sizeof("acm")) == 0) {
+ ret |= USB_GADGET_FUNC_ACM;
+ } else if(strncasecmp(p, "sdb", sizeof("sdb")) == 0) {
+ ret |= USB_GADGET_FUNC_SDB;
+ } else if (strncasecmp(p, "rndis", sizeof("rndis")) == 0) {
+ ret |= USB_GADGET_FUNC_RNDIS;
+ } else if (strncasecmp(p, "diag", sizeof("diag")) == 0) {
+ ret |= USB_GADGET_FUNC_DIAG;
+ } else if (strncasecmp(p, "rmnet", sizeof("rmnet")) == 0) {
+ ret |= USB_GADGET_FUNC_RMNET;
+ }
+ } while ((p = strtok_r(NULL, "|", &saveptr)));
+
+ return ret;
+}
+
+static void parse_property_attribute(gpointer data, gpointer udata)
+{
+ struct section_property *prop = (struct section_property *) data;
+ struct usb_gadget_config *gc = (struct usb_gadget_config *) udata;
+ unsigned int value;
+
+ if (MATCH(prop->key, "Function")) {
+ gc->mode = parse_usb_function(prop->value);
+ } else if (MATCH(prop->key, "bDeviceClass")) {
+ sscanf(prop->value, "%x", &value);
+ gc->attrs.bDeviceClass = (uint8_t) value;
+ } else if (MATCH(prop->key, "bDeviceSubClass")) {
+ sscanf(prop->value, "%x", &value);
+ gc->attrs.bDeviceSubClass = (uint8_t) value;
+ } else if (MATCH(prop->key, "bDeviceProtocol")) {
+ sscanf(prop->value, "%x", &value);
+ gc->attrs.bDeviceProtocol = (uint8_t) value;
+ } else if (MATCH(prop->key, "idVendor")) {
+ sscanf(prop->value, "%x", &value);
+ gc->attrs.idVendor = (uint16_t) value;
+ } else if (MATCH(prop->key, "idProduct")) {
+ sscanf(prop->value, "%x", &value);
+ gc->attrs.idProduct = (uint16_t) value;
+ } else if (MATCH(prop->key, "bcdDevice")) {
+ sscanf(prop->value, "%x", &value);
+ gc->attrs.bcdDevice = (uint16_t) value;
+ }
+}
+
+static int load_usb_gadget_config(const struct parse_result *result, void *data)
+{
+ if (MATCH(result->section, "SystemdUnit")) {
+ struct service_config svc = { 0, };
+ void *entry = NULL;
+
+ g_list_foreach(result->props, parse_property_systemd_unit, &svc);
+
+ entry = malloc(sizeof(struct service_config));
+ if (!entry) {
+ _E("Failed to alloc service config");
+ return 0;
+ }
+
+ _I("usb-gadget service config: name=%s, remain_after_disable=%d",
+ svc.name, svc.remain_after_disable);
+ service_config_list = g_list_prepend(service_config_list, memcpy(entry, &svc, sizeof(svc)));
+
+ } else if (MATCH(result->section, "Attribute")) {
+ struct usb_gadget_config gc = { 0, };
+ void *entry = NULL;
+
+ g_list_foreach(result->props, parse_property_attribute, &gc);
+
+ // if it hasn't defined mode, use default
+ if (gc.mode == USB_GADGET_FUNC_NONE)
+ gc.mode = USB_GADGET_FUNC_MTP | USB_GADGET_FUNC_ACM;
+
+ entry = malloc(sizeof(struct usb_gadget_config));
+ if (!entry) {
+ _E("Failed to alloc mapping gc");
+ return 0;
+ }
+
+ _I("Custom usb-gadget: mode=%d, function=%#x, idVendor=%#x, idProduct=%#x",
+ gc.mode, gc.mode, gc.attrs.idVendor, gc.attrs.idProduct);
+
+ usb_gadget_config_list = g_list_prepend(usb_gadget_config_list, memcpy(entry, &gc, sizeof(gc)));
+ }
+
+ return 0;
+}
+
+int usb_gadget_ops_init(void)
+{
+ int ret;
+ int retval;
+
+ libsys_config_parse_by_section(PATH_USB_GADGET_CONF, load_usb_gadget_config, NULL);
+
+ if (usb_gadget_legacy_supported()) {
+ usb_gadget_bind_legacy_ops(&__usb_gadget_open,
+ &__usb_gadget_close,
+ &__usb_gadget_enable,
+ &__usb_gadget_disable,
+ &__usb_gadget_reconfigure);
+ } else if (usb_gadget_cfs_supported()) {
+ usb_gadget_bind_cfs_ops(&__usb_gadget_open,
+ &__usb_gadget_close,
+ &__usb_gadget_enable,
+ &__usb_gadget_disable,
+ &__usb_gadget_reconfigure);
+ } else {
+ CRITICAL_LOG("Usb-gadget is not supported.");
+ return -ENOTSUP;
+ }
+
+ /* open supported usb-gadget */
+ if (__usb_gadget_open) {
+ ret = __usb_gadget_open();
+ if (ret != 0) {
+ CRITICAL_LOG("Failed to open usb-gadget, %d", ret);
+ return ret;
+ }
+ }
+
+ /* Use mtp-responder-dummy.socket when there is no mtp-responser.socket.
+ *
+ * The mtp-responder.socket is special in the configfs environment.
+ * If mtp-responder.socket is missing, gadget configuration will fail.
+ * As a result, all usb operations do not work properly.
+ * So in environments that mtp doesn't support, use dummy mtp.
+ */
+ if (access("/usr/lib/systemd/system/mtp-responder.socket", F_OK)) {
+ available_funcs[USB_GADGET_FUNC_IDX_MTP]->service = "mtp-responder-dummy";
+ }
+
+ retval = hal_device_board_get_device_serial_number(board_serial, sizeof(board_serial));
+ if (retval != 0)
+ _E("Failed to get board serial number, use default=%s", board_serial);
+
+ return 0;
+}
+
+int usb_gadget_ops_exit(void)
+{
+ if (__usb_gadget_close)
+ __usb_gadget_close();
+
+ __usb_gadget_open = NULL;
+ __usb_gadget_close = NULL;
+ __usb_gadget_enable = NULL;
+ __usb_gadget_disable = NULL;
+ __usb_gadget_reconfigure = NULL;
+
+ return 0;
+}
--- /dev/null
+#ifndef __USB_GADGET_OPS_H__
+#define __USB_GADGET_OPS_H__
+
+#include <stdint.h>
+#include <device/usb-gadget.h>
+
+/*The default USB configuration */
+#define DEFAULT_VID 0x04e8
+#define DEFAULT_PID 0x6860
+#define DEFAULT_BCD_DEVICE 0x0100
+
+#define DEFAULT_LANG 0x409 /* US_en */
+#define DEFAULT_MANUFACTURER "Samsung"
+#define DEFAULT_PRODUCT "TIZEN"
+#define DEFAULT_SERIAL "01234TEST"
+
+#define DEFAULT_BMATTRIBUTES ((1 << 7) | (1 << 6))
+#define DEFAULT_MAX_POWER 500
+
+struct usb_function {
+ int id;
+ const char *name;
+ const char *instance;
+
+ int is_functionfs;
+ const char *service;
+
+ /* do not stop the service on disabling usb-gadget function */
+ int remain_after_disable;
+
+ void (*handler)(int enable);
+};
+
+struct usb_configuration_attributes {
+ uint8_t bmAttributs;
+ int MaxPower;
+};
+
+struct usb_configuration_strings {
+ uint16_t lang_code;
+ char *config_str;
+};
+
+struct usb_configuration {
+ struct usb_configuration_attributes attrs;
+ struct usb_configuration_strings strs;
+ struct usb_function **funcs;
+};
+
+struct usb_gadget_attrs {
+ uint8_t bDeviceClass;
+ uint8_t bDeviceSubClass;
+ uint8_t bDeviceProtocol;
+ uint16_t idVendor;
+ uint16_t idProduct;
+ uint16_t bcdDevice;
+};
+
+struct usb_gadget_strings {
+ uint16_t lang_code;
+ char *manufacturer;
+ char *product;
+ char *serial;
+};
+
+struct usb_gadget {
+ struct usb_gadget_attrs attrs;
+ struct usb_gadget_strings strs;
+ struct usb_configuration **configs;
+};
+
+struct usb_gadget_id {
+ unsigned int function_mask;
+};
+
+enum {
+ USB_GADGET_FUNC_IDX_MTP = 0,
+ USB_GADGET_FUNC_IDX_ACM = 1,
+ USB_GADGET_FUNC_IDX_SDB = 2,
+ USB_GADGET_FUNC_IDX_RNDIS = 3,
+ USB_GADGET_FUNC_IDX_DIAG = 4,
+ USB_GADGET_FUNC_IDX_CONN_GADGET = 5,
+ USB_GADGET_FUNC_IDX_DM = 6,
+ USB_GADGET_FUNC_IDX_RMNET = 7,
+ USB_GADGET_FUNC_IDX_MAX = USB_GADGET_FUNC_IDX_RMNET + 1
+};
+
+
+struct usb_function *find_usb_function_by_name(const char *name);
+struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance);
+
+int usb_gadget_ops_init(void);
+int usb_gadget_ops_exit(void);
+
+int usb_gadget_enable(void);
+int usb_gadget_disable(void);
+int usb_gadget_change_mode(unsigned int mode);
+
+#endif //__USB_GADGET_OPS_H__
-#include <stdlib.h>
-#include <string.h>
-
+/*
+ * deviced
+ *
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include <vconf.h>
#include <libsyscommon/libsystemd.h>
-#include <libsyscommon/list.h>
-#include <vconf-internal-usb-keys.h>
-#include <shared/log.h>
+#include "core/log.h"
+#include "core/udev.h"
+#include "display/poll.h"
+#include "display/display-ops.h"
+#include "shared/plugin.h"
#include "usb-gadget.h"
+#include "usb-gadget-ops.h"
+#include "usb-debug.h"
+#include "usb-tethering.h"
-static int (*__usb_gadget_open) (void);
-static int (*__usb_gadget_close) (void);
-static int (*__usb_gadget_enable) (void);
-static int (*__usb_gadget_disable) (void);
-static int (*__usb_gadget_reconfigure) (struct usb_gadget *gadget);
-
-/* temporary extern access */
-struct _usb_mode_mapping_table {
- int mode_v; /* Integer defined by vconf */
- unsigned int mode; /* Bitmap of usb function combination */
- struct usb_gadget_attrs attrs;
-};
-extern GList *usb_mode_mapping_table_custom;
+static int usb_change_gadget(unsigned mode);
-static void rndis_handler(int enable)
-{
- if (enable)
- (void)systemd_start_unit_wait_started("rndis.service", NULL, -1);
- else
- (void)systemd_stop_unit_wait_stopped("rndis.service", NULL, -1);
-}
+static struct display_plugin *disp_plgn;
-#define DEFINE_USB_FUNCTION(_id, _name, _is_functionfs, _service, _handler) \
- static struct usb_function _##_name##_function = { \
- .id = _id, \
- .name = #_name, \
- .instance = "default", \
- .is_functionfs = _is_functionfs, \
- .service = _service, \
- .remain_after_disable = 0, \
- .handler = _handler, \
- }
+static int usb_config_init(void)
+{
+ unsigned int mode = usb_state_get_selected_mode();
-DEFINE_USB_FUNCTION(USB_FUNCTION_MTP, mtp, 1, "mtp-responder", NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_ACM, acm, 0, "data-router", NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_SDB, sdb, 1, "sdbd", NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_RNDIS, rndis, 0, "sshd", rndis_handler);
-DEFINE_USB_FUNCTION(USB_FUNCTION_DIAG, diag, 1, "diag", NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_CONN_GADGET, conn_gadget, 0, NULL, NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_DM, dm, 0, NULL, NULL);
-DEFINE_USB_FUNCTION(USB_FUNCTION_RMNET, rmnet, 0, NULL, NULL);
-
-#undef DEFINE_USB_FUNCTION
-
-/* Caution: index order of arrary is important, because simple_translator_open() uses it. */
-static struct usb_function *_available_funcs[] = {
- [USB_FUNCTION_IDX_MTP] = &_mtp_function,
- [USB_FUNCTION_IDX_ACM] = &_acm_function,
- [USB_FUNCTION_IDX_SDB] = &_sdb_function,
- [USB_FUNCTION_IDX_RNDIS] = &_rndis_function,
- [USB_FUNCTION_IDX_DIAG] = &_diag_function,
- [USB_FUNCTION_IDX_CONN_GADGET] = &_conn_gadget_function,
- [USB_FUNCTION_IDX_DM] = &_dm_function,
- [USB_FUNCTION_IDX_RMNET] = &_rmnet_function,
- [USB_FUNCTION_IDX_MAX] = NULL /* An indicator to end the array */
-};
-static struct usb_function *find_usb_function_by_id(int id);
+ return usb_change_gadget(mode);
+}
-struct usb_function *find_usb_function_by_name(const char *name)
+/* Precondition: USB_GADGET_FUNC_NONE */
+static int usb_change_gadget(unsigned mode)
{
- int i;
+ int ret;
- if(!name || !name[0])
- return NULL;
+ ret = usb_gadget_change_mode(mode);
+ if (ret) {
+ /* because usb does not work properly */
+ (void)usb_state_set_current_mode(USB_GADGET_FUNC_NONE);
+ return ret;
+ }
- for (i = 0; _available_funcs[i]; i++)
- if (!strcmp(name, _available_funcs[i]->name))
- return _available_funcs[i];
+ _I("USB gadget changed to (%#x)", mode);
- return NULL;
+ return 0;
}
-struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance)
+/* Precondition: USB_CONNECTED, USB_GADGET_FUNC_NONE */
+static int usb_enable(unsigned int mode)
{
- int i;
+ int ret;
- if(!name || !name[0] || !instance || !instance[0])
- return NULL;
+ ret = usb_gadget_enable();
+ if (ret < 0) {
+ _E("Failed to enable USB config: %d", ret);
+ goto out;
+ }
- for (i = 0; _available_funcs[i]; ++i)
- if (!strcmp(name, _available_funcs[i]->name) && !strcmp(instance, _available_funcs[i]->instance))
- return _available_funcs[i];
+ send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_AVAILABLE);
+ (void)usb_state_set_current_mode(mode);
+ change_usb_state_notification_handler(mode);
- return NULL;
+ return 0;
+
+out:
+ /* Although this function has a USB_GADGET_FUNC_NONE precondition, it is to protect against coding mistakes. */
+ (void)usb_state_set_current_mode(USB_GADGET_FUNC_NONE); /* because usb does not work properly */
+ return ret;
}
-static void simple_cleanup_config(struct usb_configuration *config)
+/* Precondition: N/A */
+static int usb_disable(void)
{
- if (!config)
- return;
+ int ret;
- free(config->strs.config_str);
+ (void)usb_state_set_current_mode(USB_GADGET_FUNC_NONE);
+ change_usb_state_notification_handler(USB_GADGET_FUNC_NONE);
- if (config->funcs)
- free(config->funcs);
+ ret = usb_gadget_disable();
+ if (ret < 0) {
+ _E("Failed to disable USB config: %d", ret);
+ return ret;
+ }
- free(config);
+ return 0;
}
-static void cleanup_gadget(struct usb_gadget *gadget)
+/* Precondition: USB_DISCONNECTED (Implicitly contains USB_GADGET_FUNC_NONE) */
+static int usb_connected(void)
{
- int i;
-
- if (!gadget)
- return;
+ int ret;
+ unsigned int mode = usb_state_get_selected_mode();
- free(gadget->strs.manufacturer);
- free(gadget->strs.product);
- free(gadget->strs.serial);
+ if (disp_plgn->pm_lock_internal)
+ disp_plgn->pm_lock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE, 0);
- if (gadget->configs) {
- for (i = 0; gadget->configs[i]; ++i)
- simple_cleanup_config(gadget->configs[i]);
+ usb_state_set_connection(USB_CONNECTED);
+ send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_CONNECTED);
- free(gadget->configs);
+ ret = usb_enable(mode);
+ if (ret < 0) {
+ _E("Failed to enable USB gadget(%#x): %d", mode, ret);
+ return ret;
}
- free(gadget);
+ return 0;
}
-static int alloc_default_config(struct usb_configuration **_config)
+/* Precondition: USB_CONNECTED */
+static int usb_disconnected(void)
{
- struct usb_configuration *config;
-
- config = calloc(1, sizeof(*config));
- if (!config)
- return -ENOMEM;
+ int ret;
- config->attrs.bmAttributs = DEFAULT_BMATTRIBUTES;
- config->attrs.MaxPower = DEFAULT_MAX_POWER;
+ usb_state_set_connection(USB_DISCONNECTED);
+ send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED);
- /* TODO. Here is where to set the string used in config of configfs */
+ ret = usb_disable();
+ if(ret < 0) {
+ _E("Failed to disable USB gadget: %d", ret);
+ /* Important: You have to keep going to unlock disp_plgn->pm_unlock_internal */
+ }
- *_config = config;
+ if (disp_plgn->pm_unlock_internal)
+ disp_plgn->pm_unlock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE);
- return 0;
+ return ret;
}
-static int alloc_default_gadget(char *serial, struct usb_gadget **_gadget)
+/* Called by dbus signal and vconf change(tethering, debug mode) */
+int usb_change_mode(unsigned int new_mode, bool change_debug_mode)
{
- struct usb_gadget *gadget;
- struct usb_configuration **configs;
+ int ret;
+ const unsigned int curr_mode = usb_state_get_current_mode();
+ const unsigned int prev_mode = usb_state_get_selected_mode();
- gadget = calloc(1, sizeof(*gadget));
- if (!gadget)
- goto out;
+ if (prev_mode == new_mode) {
+ _I("already using USB mode(%#x), current running mode (%#x)", prev_mode, curr_mode);
+ return 0;
+ }
- gadget->attrs.idVendor = DEFAULT_VID;
- gadget->attrs.idProduct = DEFAULT_PID;
- gadget->attrs.bcdDevice = DEFAULT_BCD_DEVICE;
+ _I("USB change mode (%#x) -> (%#x), current running mode (%#x)", prev_mode, new_mode, curr_mode);
- gadget->strs.lang_code = DEFAULT_LANG;
- gadget->strs.manufacturer = strdup(DEFAULT_MANUFACTURER);
- gadget->strs.product = strdup(DEFAULT_PRODUCT);
- gadget->strs.serial = strdup(serial);
+ /*
+ * When you change the gadget(usb_change_gadget) for new usb mode, you must disable the gadget first.
+ *
+ * Even if the usb cable is plugged in, the current mode may be NULL due to usb fail.
+ * So to find out which mode you are in, you should use the current mode, not the selected mode.
+ */
+ if (curr_mode != USB_GADGET_FUNC_NONE) {
+ /* Special case: Because usb gadget is disabled, temporarily switch usb state from available to connected. */
+ send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_CONNECTED);
+
+ ret = usb_disable();
+ if (ret < 0) {
+ _E("Failed to disable current USB mode.");
+ return ret;
+ }
+ }
+
+ /*
+ * You should always change the gadget once when the usb mode is changed.
+ * In this state, usb_enable() is called when the usb cable is connected
+ * and usb_disable() is called when the usb cable is disconnected.
+ */
+ ret = usb_change_gadget(new_mode);
+ if (ret < 0) {
+ _E("Failed to change USB gadget: %d", ret);
+ return ret;
+ }
- if (!gadget->strs.manufacturer || !gadget->strs.product || !gadget->strs.serial)
- goto free_strs;
+ if (usb_state_get_connection() == USB_CONNECTED) {
+ ret = usb_enable(new_mode);
+ if (ret < 0) {
+ _E("Failed to enable USB mode: %d", ret);
+ return ret;
+ }
+ }
- /* slp-gadget use max 2 confiuration and NULL termination */
- configs = calloc(3, sizeof(*configs));
- if (!configs)
- goto free_strs;
+ /* If you success to change the runtime usb configuration, do change the selected mode. */
+ (void)usb_state_set_selected_mode(new_mode);
- gadget->configs = configs;
- *_gadget = gadget;
+ /* To sync with vconf for debug mode */
+ if (change_debug_mode)
+ set_usb_debug_state((bool)(new_mode & USB_GADGET_FUNC_SDB));
return 0;
-
-free_strs:
- free(gadget->strs.manufacturer);
- free(gadget->strs.product);
- free(gadget->strs.serial);
- free(gadget);
-out:
- return -ENOMEM;
}
-static int id_to_gadget(struct usb_gadget_id *gadget_id, char *serial, struct usb_gadget **_gadget)
+/* Called by extcon udev event */
+static int usb_state_changed(const char *index, int new_status)
{
- int ret;
- int i, j;
- int n_configs;
- struct usb_gadget *gadget;
- int functions[2][sizeof(gadget_id->function_mask)*8]; /* zero terminates */
+ int ret = -1;
+ static int old_status = -1; /* -1: Uninitialized, 0: disconnected, 1: connected */
- GList *elem;
- const struct _usb_mode_mapping_table *cm = NULL;
+ /* For debugging. Do not move the location. */
+ _I("USB state is changed by extcon from (%d) to (%d).", old_status, new_status);
- if (!gadget_id || !serial || !_gadget)
- return -EINVAL;
+ if (old_status == new_status)
+ return 0;
- ret = alloc_default_gadget(serial, &gadget);
- if (ret)
- goto out;
+ switch (new_status) {
+ case USB_CONNECTED:
+ ret = usb_connected();
+ break;
- /* find custom mode */
- SYS_G_LIST_FOREACH(usb_mode_mapping_table_custom, elem, cm) {
- if (cm->mode == gadget_id->function_mask)
- break;
- }
+ case USB_DISCONNECTED:
+ if (old_status == -1) {
+ /* only initialize the internal data state and skip usb hal operation. */
+ _I("USB is initialized without USB connection");
- if (cm) {
- int i, j;
-
- j = 0;
- n_configs = 1;
- for (i = 0; i < USB_FUNCTION_IDX_MAX; ++i) {
- if (cm->mode & (1 << i))
- functions[0][j++] = (1 << i);
- }
- functions[0][j] = 0;
-
- if (cm->attrs.idVendor)
- gadget->attrs.idVendor = cm->attrs.idVendor;
- if (cm->attrs.idProduct)
- gadget->attrs.idProduct = cm->attrs.idProduct;
-
- } else {
- /*
- * Currently all gadgets use inly single configuration but
- * slp-gadget is capable to handle two of them
- *
- * Order of interfaces in configuration is significant
- * so in this switch we sort our functions in a correct order
- */
- switch (gadget_id->function_mask) {
- /* MTP, ACM, SDB */
- case USB_FUNCTION_MTP | USB_FUNCTION_ACM:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_MTP;
- functions[0][1] = USB_FUNCTION_ACM;
- functions[0][2] = 0;
- gadget->attrs.idProduct = 0x6860;
- break;
-
- case USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_MTP;
- functions[0][1] = USB_FUNCTION_ACM;
- functions[0][2] = USB_FUNCTION_SDB;
- functions[0][3] = 0;
- gadget->attrs.idProduct = 0x6860;
- break;
-
- /* DIAG */
- case USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_DIAG:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_MTP;
- functions[0][1] = USB_FUNCTION_ACM;
- functions[0][2] = USB_FUNCTION_SDB;
- functions[0][3] = USB_FUNCTION_DIAG;
- functions[0][4] = 0;
- gadget->attrs.idProduct = 0x6860;
- break;
-
- /* RNDIS */
- case USB_FUNCTION_RNDIS:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_RNDIS;
- functions[0][1] = 0;
- gadget->attrs.idProduct = 0x6863;
- break;
-
- case USB_FUNCTION_RNDIS | USB_FUNCTION_DIAG:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_RNDIS;
- functions[0][1] = USB_FUNCTION_DIAG;
- functions[0][2] = 0;
- gadget->attrs.idProduct = 0x6864;
- break;
-
- case USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_RNDIS;
- functions[0][1] = USB_FUNCTION_ACM;
- functions[0][2] = USB_FUNCTION_SDB;
- functions[0][3] = 0;
- gadget->attrs.idProduct = 0x6864;
- break;
-
- /* RMNET */
- case USB_FUNCTION_DIAG | USB_FUNCTION_RMNET:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_DIAG;
- functions[0][1] = USB_FUNCTION_RMNET;
- functions[0][2] = 0;
- gadget->attrs.idProduct = 0x685d;
- break;
-
- /* DM */
- case USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_DM:
- n_configs = 1;
- functions[0][0] = USB_FUNCTION_ACM;
- functions[0][1] = USB_FUNCTION_SDB;
- functions[0][2] = USB_FUNCTION_DM;
- functions[0][3] = 0;
- gadget->attrs.idProduct = 0x6860;
- break;
-
- default:
- ret = -EINVAL;
- goto free_gadget;
- };
- }
+ /* From usb_disconnected() */
+ usb_state_set_connection(USB_DISCONNECTED);
+ send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED);
- for (j = 0; j < n_configs; ++j) {
- int n_funcs_in_config;
- struct usb_configuration *config;
+ /* From usb_disable() */
+ (void)usb_state_set_current_mode(USB_GADGET_FUNC_NONE);
+ change_usb_state_notification_handler(USB_GADGET_FUNC_NONE);
- for (i = 0; functions[j][i]; ++i);
- n_funcs_in_config = i;
+ ret = 0;
+ } else
+ ret = usb_disconnected();
+ break;
- ret = alloc_default_config(&config);
- if (ret)
- goto free_configs;
-
- gadget->configs[j] = config;
- config->funcs = calloc(n_funcs_in_config + 1, sizeof(void *));
- if (!config->funcs)
- goto free_configs;
+ default:
+ _E("Invalid USB state(%d).", new_status);
+ return -EINVAL;
+ }
- for (i = 0; functions[j][i]; ++i) {
- config->funcs[i] = find_usb_function_by_id(functions[j][i]);
- if (!config->funcs[i])
- goto free_configs;
- }
+ if (ret < 0) {
+ _E("Failed to operate USB connection: %d", ret);
+ return ret;
}
- *_gadget = gadget;
+ old_status = new_status;
- return 0;
-
-free_configs:
-free_gadget:
- cleanup_gadget(gadget);
-out:
return ret;
}
-
-static struct usb_function *find_usb_function_by_id(int id)
+static void uevent_usb_mode_handler(struct udev_device *dev)
{
- int i;
+ const char *state = NULL;
- for (i = 0; _available_funcs[i]; i++)
- if (_available_funcs[i]->id == id)
- return _available_funcs[i];
-
- return NULL;
-}
+ state = udev_device_get_property_value(dev, "USB_STATE");
+ if (!state)
+ return;
+ if (strncmp(state, "CONFIGURED", strlen(state) + 1))
+ return;
-int usb_gadget_enable(void)
-{
- if (!__usb_gadget_enable) {
- _E("Not supported usb_gadget_enable.");
- return -ENOTSUP;
- }
+ _I("USB udev event happend : CONFIGURED USB_STATE");
- return __usb_gadget_enable();
-}
+ if (usb_state_get_selected_mode() & USB_GADGET_FUNC_ACM)
+ systemd_start_unit_wait_started ("data-router.service", NULL, -1);
-int usb_gadget_disable(void)
-{
- if (!__usb_gadget_disable) {
- _E("Not supported usb_gadget_disable.");
- return -ENOTSUP;
- }
+ if (usb_state_get_selected_mode() & USB_GADGET_FUNC_SDB)
+ systemd_start_unit_wait_started ("sdbd.service", NULL, -1);
- return __usb_gadget_disable();
+ if (usb_state_get_selected_mode() & USB_GADGET_FUNC_MTP)
+ systemd_start_unit_wait_started ("mtp-responder.service", NULL, -1);
}
-int usb_gadget_change_mode(unsigned int mode)
+static struct uevent_handler uh = {
+ .subsystem = "usb_mode",
+ .uevent_func = uevent_usb_mode_handler,
+};
+
+static void usb_init(void *data)
{
- struct usb_gadget *gadget;
- struct usb_gadget_id gadget_id = {0, };
- char serial[] = "123456";
int ret;
- if (!__usb_gadget_reconfigure) {
- _E("Not supported usb_gadget_reconfigure.");
- return -ENOTSUP;
+ usb_state_retrieve_selected_mode();
+
+ ret = usb_gadget_ops_init();
+ if (ret < 0) {
+ CRITICAL_LOG("USB client cannot be used: %d", ret);
+ return;
}
- gadget_id.function_mask = mode;
+ ret = usb_config_init();
+ if (ret < 0)
+ _E("Failed to initialize USB configuation.");
- ret = id_to_gadget(&gadget_id, serial, &gadget);
- if (ret) {
- _E("Failed to id_to_gadget, %d", ret);
- return ret;
- }
+ ret = register_udev_uevent_control(&uh);
+ if (ret < 0)
+ _E("Failed to register udev event(%d) for USB", ret);
- ret = __usb_gadget_reconfigure(gadget);
- cleanup_gadget(gadget);
- if (ret) {
- _E("Failed to reconfigure gadget, %d", ret);
- return ret;
- }
+ ret = usb_dbus_init();
+ if (ret < 0)
+ _E("Failed to init dbus(%d) for USB", ret);
- return 0;
+ add_usb_tethering_handler();
+ add_usb_debug_handler();
}
-int usb_gadget_init(void)
+static void usb_exit(void *data)
{
- int ret;
+ remove_usb_debug_handler();
+ remove_usb_tethering_handler();
- if (usb_gadget_legacy_supported()) {
- /* legacy samsung gadget */
- usb_gadget_bind_legacy_gadget(&__usb_gadget_open,
- &__usb_gadget_close,
- &__usb_gadget_enable,
- &__usb_gadget_disable,
- &__usb_gadget_reconfigure);
- } else if (usb_gadget_cfs_supported()) {
- /* configfs/functionfs gadget */
- usb_gadget_bind_cfs_gadget(&__usb_gadget_open,
- &__usb_gadget_close,
- &__usb_gadget_enable,
- &__usb_gadget_disable,
- &__usb_gadget_reconfigure);
- } else {
- CRITICAL_LOG("Usb-gadget is not supported.");
- return -ENOTSUP;
- }
+ usb_state_set_connection(USB_DISCONNECTED);
+ send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED);
+ (void)usb_state_set_current_mode(USB_GADGET_FUNC_NONE);
+ change_usb_state_notification_handler(USB_GADGET_FUNC_NONE);
- /* open supported usb-gadget */
- if (__usb_gadget_open) {
- ret = __usb_gadget_open();
- if (ret != 0) {
- CRITICAL_LOG("Failed to open usb-gadget, %d", ret);
- return ret;
- }
- }
-
- /* Use mtp-responder-dummy.socket when there is no mtp-responser.socket.
- *
- * The mtp-responder.socket is special in the configfs environment.
- * If mtp-responder.socket is missing, gadget configuration will fail.
- * As a result, all usb operations do not work properly.
- * So in environments that mtp doesn't support, use dummy mtp.
- */
- if (access("/usr/lib/systemd/system/mtp-responder.socket", F_OK)) {
- _available_funcs[USB_FUNCTION_IDX_MTP]->service = "mtp-responder-dummy";
- }
+ unregister_udev_uevent_control(&uh);
+ usb_gadget_disable();
+ usb_gadget_ops_exit();
- return 0;
}
-int usb_gadget_exit(void)
-{
- if (__usb_gadget_close)
- __usb_gadget_close();
+static struct extcon_ops extcon_usb_ops = {
+ .name = EXTCON_CABLE_USB,
+ .init = usb_init,
+ .exit = usb_exit,
+ .update = usb_state_changed,
+};
- __usb_gadget_open = NULL;
- __usb_gadget_close = NULL;
- __usb_gadget_enable = NULL;
- __usb_gadget_disable = NULL;
- __usb_gadget_reconfigure = NULL;
+EXTCON_OPS_REGISTER(extcon_usb_ops)
- return 0;
+static void __CONSTRUCTOR__ initialize(void)
+{
+ disp_plgn = get_var_display_plugin();
+ if (!disp_plgn)
+ _E("Failed to get display plugin variable.");
}
-#ifndef __USB_GADGET_H__
-#define __USB_GADGET_H__
-
-#include <stdint.h>
-#include <device/usb-gadget.h>
-
-/*The default USB configuration */
-#define DEFAULT_VID 0x04e8
-#define DEFAULT_PID 0x6860
-#define DEFAULT_BCD_DEVICE 0x0100
-
-#define DEFAULT_LANG 0x409 /* US_en */
-#define DEFAULT_MANUFACTURER "Samsung"
-#define DEFAULT_PRODUCT "TIZEN"
-#define DEFAULT_SERIAL "01234TEST"
-
-#define DEFAULT_BMATTRIBUTES ((1 << 7) | (1 << 6))
-#define DEFAULT_MAX_POWER 500
-
-struct usb_function {
- int id;
- const char *name;
- const char *instance;
-
- int is_functionfs;
- const char *service;
-
- /* do not stop the service on disabling usb-gadget function */
- int remain_after_disable;
-
- void (*handler)(int enable);
-};
-
-struct usb_configuration_attributes {
- uint8_t bmAttributs;
- int MaxPower;
-};
-
-struct usb_configuration_strings {
- uint16_t lang_code;
- char *config_str;
-};
-
-struct usb_configuration {
- struct usb_configuration_attributes attrs;
- struct usb_configuration_strings strs;
- struct usb_function **funcs;
-};
-
-struct usb_gadget_attrs {
- uint8_t bDeviceClass;
- uint8_t bDeviceSubClass;
- uint8_t bDeviceProtocol;
- uint16_t idVendor;
- uint16_t idProduct;
- uint16_t bcdDevice;
-};
-
-struct usb_gadget_strings {
- uint16_t lang_code;
- char *manufacturer;
- char *product;
- char *serial;
-};
-
-struct usb_gadget {
- struct usb_gadget_attrs attrs;
- struct usb_gadget_strings strs;
- struct usb_configuration **configs;
-};
-
-struct usb_gadget_id {
- unsigned int function_mask;
-};
-
-enum {
- USB_FUNCTION_IDX_MTP = 0,
- USB_FUNCTION_IDX_ACM = 1,
- USB_FUNCTION_IDX_SDB = 2,
- USB_FUNCTION_IDX_RNDIS = 3,
- USB_FUNCTION_IDX_DIAG = 4,
- USB_FUNCTION_IDX_CONN_GADGET = 5,
- USB_FUNCTION_IDX_DM = 6,
- USB_FUNCTION_IDX_RMNET = 7,
- USB_FUNCTION_IDX_MAX = USB_FUNCTION_IDX_RMNET + 1
-};
-
-
-struct usb_function *find_usb_function_by_name(const char *name);
-struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance);
-
-int usb_gadget_enable(void);
-int usb_gadget_disable(void);
-int usb_gadget_change_mode(unsigned int mode);
-int usb_gadget_init(void);
-int usb_gadget_exit(void);
-
-/* legacy samsung gadget */
-int usb_gadget_legacy_supported(void);
-void usb_gadget_bind_legacy_gadget(int (**open) (void),
- int (**close) (void),
- int (**enable) (void),
- int (**disable) (void),
- int (**reconfigure) (struct usb_gadget *));
-
-/* configfs/functionfs gadget */
-int usb_gadget_cfs_supported(void);
-void usb_gadget_bind_cfs_gadget(int (**open) (void),
- int (**close) (void),
- int (**enable) (void),
- int (**disable) (void),
- int (**reconfigure) (struct usb_gadget *));
-
-#endif //__USB_GADGET_H__
+/*
+ * deviced
+ *
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __DEVICED_USB_GADGET_H___
+#define __DEVICED_USB_GADGET_H___
+
+#include <limits.h>
+#include <stdbool.h>
+#include "extcon/extcon.h"
+
+#define USB_GADGET_FUNC_INVALID UINT_MAX
+#define SET_USB_INVALID INT_MAX
+
+int usb_dbus_init(void);
+
+void usb_state_load_custom_mode(void);
+
+int usb_change_mode(unsigned int mode, bool change_debug_mode);
+void usb_state_retrieve_selected_mode(void);
+unsigned int get_mode_bitmap_from_vconf(int mode_v);
+
+void broadcast_usb_state_changed(void);
+void send_usb_state_changed_event(int status);
+void change_usb_state_notification_handler(unsigned int mode);
+
+unsigned int usb_state_get_selected_mode(void);
+int usb_state_set_selected_mode(unsigned int mode);
+
+unsigned int usb_state_get_current_mode(void);
+int usb_state_set_current_mode(unsigned int mode);
+
+extcon_usb_state_e usb_state_get_connection(void);
+void usb_state_set_connection(extcon_usb_state_e conn);
+
+#endif /* __DEVICED_USB_GADGET_H__ */
#include <vconf.h>
#include <bundle.h>
#include <eventsystem.h>
-#include <libsyscommon/ini-parser.h>
#include "core/log.h"
#include "apps/apps.h"
#include "extcon/extcon.h"
-#include "usb.h"
-#include "usb-debug.h"
#include "usb-gadget.h"
-
-#define PATH_USB_GADGET_CONF "/hal/etc/deviced/usb_gadget.conf"
+#include "usb-debug.h"
+#include "usb-gadget-ops.h"
static int noti_id = -1;
-static unsigned int usb_current_mode = USB_FUNCTION_NONE;
-static unsigned int usb_selected_mode = USB_FUNCTION_NONE;
+static unsigned int usb_current_mode = USB_GADGET_FUNC_NONE;
+static unsigned int usb_selected_mode = USB_GADGET_FUNC_NONE;
static extcon_usb_state_e usb_connection = USB_DISCONNECTED;
-/**
- * Important
- * You must keep the order for the specific items.
- *
- * There may be several mode_v for a mode.
- * In this case, the representative value should be placed on top.
- *
- * In case of SET_USB_SDB_DIAG and SET_USB_DIAG_SDB, the SET_USB_SDB_DIAG should be above.
- * As another example, SET_USB_RNDIS_SDB should be at the top of them.
- */
-static const struct _usb_mode_mapping_table {
- int mode_v; /* Integer defined by vconf */
- unsigned int mode; /* Bitmap of usb function combination */
- struct usb_gadget_attrs attrs;
-} usb_mode_mapping_table[] = {
- /* Hack for performance. In most cases this is the value. */
- {SET_USB_DEFAULT, USB_FUNCTION_MTP | USB_FUNCTION_ACM},
- {SET_USB_SDB, USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB},
-
- {SET_USB_NONE, USB_FUNCTION_NONE},
- {SET_USB_RNDIS, USB_FUNCTION_RNDIS},
- {SET_USB_RNDIS_DIAG, USB_FUNCTION_RNDIS | USB_FUNCTION_DIAG},
- {SET_USB_DIAG_RMNET, USB_FUNCTION_DIAG | USB_FUNCTION_RMNET},
- {SET_USB_ACM_SDB_DM, USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_DM},
-
- {SET_USB_SDB_DIAG, USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_DIAG},
- {SET_USB_DIAG_SDB, USB_FUNCTION_MTP | USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_DIAG},
-
- {SET_USB_RNDIS_SDB, USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS},
- {SET_USB_RNDIS_TETHERING, USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS},
- {SET_USB_RNDIS_SDB_ACM, USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS},
-};
-
-static const int mapping_table_len = sizeof(usb_mode_mapping_table) / sizeof(usb_mode_mapping_table[0]);
-
-/*
- * Private, custom defined mode mapping.
- * You can define custom mode_v with mode from /hal/etc/deviced/usb_gadget.conf
- * A custom mapping precedes the above default mapping if the custom mode_v overlaps
- * default mode_v.
- *
- * Do not declare it with static storage class as it will be exported as dynamic symbol
- * so that dlopen-ed device-common can refer it.
- */
-GList *usb_mode_mapping_table_custom;
-
-/*
- * Do not declare it with static storage class as it will be exported as dynamic symbol
- * so that dlopen-ed device-common can refer it.
- */
-struct service_config {
- char name[128];
- int remain_after_disable; /* do not stop the service on disabling usb-gadget function */
-};
-GList *service_config_list;
-
-unsigned int get_mode_bitmap_from_vconf(int mode_v)
-{
- int i;
- GList *elem;
- struct _usb_mode_mapping_table *table;
-
- /* lookup from custom mapping table first */
- SYS_G_LIST_FOREACH(usb_mode_mapping_table_custom, elem, table) {
- if (table->mode_v == mode_v)
- return table->mode;
- }
-
- /* and then lookup the default mapping table */
- for (i = 0; i < mapping_table_len; i++) {
- if (usb_mode_mapping_table[i].mode_v == mode_v)
- return usb_mode_mapping_table[i].mode;
- }
-
- return USB_FUNCTION_INVALID;
-}
-
-static int get_mode_vconf_from_bitmap(unsigned int mode)
-{
- int i;
- GList *elem;
- struct _usb_mode_mapping_table *table;
-
- /* lookup from custom mapping table first */
- SYS_G_LIST_FOREACH(usb_mode_mapping_table_custom, elem, table) {
- if (table->mode == mode)
- return table->mode_v;
- }
-
- /* Caution: The order to search the usb_mode_mapping_table must start with a low index. */
- for (i = 0; i < mapping_table_len; i++) {
- if (usb_mode_mapping_table[i].mode == mode)
- return usb_mode_mapping_table[i].mode_v;
- }
-
- return SET_USB_INVALID;
-}
-
static void usb_state_send_system_event(int status)
{
bundle *b;
bundle_free(b);
}
-static void parse_property_systemd_unit(gpointer data, gpointer udata)
-{
- struct section_property *prop = (struct section_property *) data;
- struct service_config *svc = (struct service_config *) udata;
-
- if (MATCH(prop->key, "Service")) {
- strncpy(svc->name, prop->value, sizeof(svc->name) - 1);
- svc->name[sizeof(svc->name) - 1] = '\0';
- } else if (MATCH(prop->key, "RemainAfterDisable")) {
- svc->remain_after_disable = MATCH(prop->value, "yes");
- }
-}
-
-static unsigned int parse_usb_function(char *fstr)
-{
- // Parse Function=, e.g., "Function=diag,acm"
- unsigned int ret = USB_FUNCTION_NONE;
- char *p, *saveptr;
-
- if (!fstr)
- return ret;
-
- p = strtok_r(fstr, ",", &saveptr);
- if (!p)
- return ret;
-
- do {
- if (strncasecmp(p, "mtp", sizeof("mtp")) == 0) {
- ret |= USB_FUNCTION_MTP;
- } else if (strncasecmp(p, "acm", sizeof("acm")) == 0) {
- ret |= USB_FUNCTION_ACM;
- } else if(strncasecmp(p, "sdb", sizeof("sdb")) == 0) {
- ret |= USB_FUNCTION_SDB;
- } else if (strncasecmp(p, "rndis", sizeof("rndis")) == 0) {
- ret |= USB_FUNCTION_RNDIS;
- } else if (strncasecmp(p, "diag", sizeof("diag")) == 0) {
- ret |= USB_FUNCTION_DIAG;
- } else if (strncasecmp(p, "conngadget", sizeof("conngadget")) == 0) {
- ret |= USB_FUNCTION_CONN_GADGET;
- } else if (strncasecmp(p, "dm", sizeof("dm")) == 0) {
- ret |= USB_FUNCTION_DM;
- } else if (strncasecmp(p, "rmnet", sizeof("rmnet")) == 0) {
- ret |= USB_FUNCTION_RMNET;
- }
- } while ((p = strtok_r(NULL, ",", &saveptr)));
-
- return ret;
-}
-
-static void parse_property_attribute(gpointer data, gpointer udata)
-{
- struct section_property *prop = (struct section_property *) data;
- struct _usb_mode_mapping_table *table = (struct _usb_mode_mapping_table *) udata;
- unsigned int value;
-
- if (MATCH(prop->key, "Mode")) {
- sscanf(prop->value, "%d", &table->mode_v);
- } else if (MATCH(prop->key, "Function")) {
- table->mode = parse_usb_function(prop->value);
- } else if (MATCH(prop->key, "bDeviceClass")) {
- sscanf(prop->value, "%x", &value);
- table->attrs.bDeviceClass = (uint8_t) value;
- } else if (MATCH(prop->key, "bDeviceSubClass")) {
- sscanf(prop->value, "%x", &value);
- table->attrs.bDeviceSubClass = (uint8_t) value;
- } else if (MATCH(prop->key, "bDeviceProtocol")) {
- sscanf(prop->value, "%x", &value);
- table->attrs.bDeviceProtocol = (uint8_t) value;
- } else if (MATCH(prop->key, "idVendor")) {
- sscanf(prop->value, "%x", &value);
- table->attrs.idVendor = (uint16_t) value;
- } else if (MATCH(prop->key, "idProduct")) {
- sscanf(prop->value, "%x", &value);
- table->attrs.idProduct = (uint16_t) value;
- } else if (MATCH(prop->key, "bcdDevice")) {
- sscanf(prop->value, "%x", &value);
- table->attrs.bcdDevice = (uint16_t) value;
- }
-}
-
-static int load_usb_gadget_config(const struct parse_result *result, void *data)
-{
- if (MATCH(result->section, "SystemdUnit")) {
- struct service_config svc = { 0, };
- void *entry = NULL;
-
- g_list_foreach(result->props, parse_property_systemd_unit, &svc);
-
- entry = malloc(sizeof(struct service_config));
- if (!entry) {
- _E("Failed to alloc service config");
- return 0;
- }
-
- _I("usb-gadget service config: name=%s, remain_after_disable=%d",
- svc.name, svc.remain_after_disable);
- service_config_list = g_list_prepend(service_config_list, memcpy(entry, &svc, sizeof(svc)));
-
- } else if (MATCH(result->section, "Attribute")) {
- struct _usb_mode_mapping_table table = { 0, };
- void *entry = NULL;
-
- g_list_foreach(result->props, parse_property_attribute, &table);
-
- // if it hasn't defined mode, use default or pre-defined one
- if (table.mode == USB_FUNCTION_NONE)
- table.mode = get_mode_bitmap_from_vconf(table.mode_v);
-
- if (table.mode == USB_FUNCTION_INVALID)
- return 0;
-
- if (table.mode_v >= SET_USB_NONE && table.mode_v <= SET_USB_RNDIS_SDB_ACM)
- _W("The custom mode=%d replaces the predefined usb-gadget configuration", table.mode_v);
-
- entry = malloc(sizeof(struct _usb_mode_mapping_table));
- if (!entry) {
- _E("Failed to alloc mapping table");
- return 0;
- }
-
- _I("Custom usb-gadget: mode=%d, function=%#x, idVendor=%#x, idProduct=%#x",
- table.mode_v, table.mode, table.attrs.idVendor, table.attrs.idProduct);
-
- usb_mode_mapping_table_custom = g_list_prepend(usb_mode_mapping_table_custom, memcpy(entry, &table, sizeof(table)));
- }
-
- return 0;
-}
-
-void usb_state_load_custom_mode(void)
-{
- libsys_config_parse_by_section(PATH_USB_GADGET_CONF, load_usb_gadget_config, NULL);
-}
-
void usb_state_retrieve_selected_mode(void)
{
- int mode_v;
- unsigned int mode;
- const unsigned int default_mode = USB_FUNCTION_MTP | USB_FUNCTION_ACM;
+ int mode;
+ const unsigned int default_mode = USB_GADGET_FUNC_MTP | USB_GADGET_FUNC_ACM;
- /* Never return here because deviced must never run in USB_FUNCTION_NONE mode */
- if (vconf_get_int(VCONFKEY_USB_SEL_MODE, &mode_v) != VCONF_OK) {
- mode_v = SET_USB_INVALID;
+ /* Never return here because deviced must never run in USB_GADGET_FUNC_NONE mode */
+ if (vconf_get_int(VCONFKEY_USB_SEL_MODE, &mode) != VCONF_OK) {
+ mode = USB_GADGET_FUNC_INVALID;
_E("Failed to get vconf value for USB sel mode: %d", vconf_get_ext_errno());
}
- mode = get_mode_bitmap_from_vconf(mode_v);
-
/*
- * Deviced must never run in USB_FUNCTION_NONE mode.
+ * Deviced must never run in USB_GADGET_FUNC_NONE mode.
* So if vconf value is invalid, deviced uses the default usb mode internally.
* Keep the problematic vconf values in order to define the problem correctly.
*/
switch (mode) {
- case USB_FUNCTION_INVALID: /* Invalid vconf */
+ case USB_GADGET_FUNC_INVALID: /* Invalid vconf */
usb_selected_mode = default_mode;
- _E("Failed to convert vconf to USB mode. There is no mode matches up with vconf %d. Use default mode=%#x.", mode_v, default_mode);
+ _E("Failed to convert vconf to USB mode. There is no mode matches up with vconf %#x. Use default mode=%#x.", mode, default_mode);
break;
- case USB_FUNCTION_NONE: /* Invalid vconf */
+ case USB_GADGET_FUNC_NONE: /* Invalid vconf */
usb_selected_mode = default_mode;
- _E("There is USB_FUNCTION_NONE USB mode matches up with vconf %d. Use default mode=%#x.", mode_v, default_mode);
+ _E("There is USB_GADGET_FUNC_NONE USB mode matches up with vconf %#x. Use default mode=%#x.", mode, default_mode);
break;
default: /* Success */
usb_selected_mode = mode;
- _I("Succeeded to convert vconf to USB mode. vconf=%d, mode=%#x.", mode_v, mode);
+ _I("Succeeded to convert vconf to USB mode=%#x", mode);
break;
}
/* To sync with vconf for debug mode */
- set_usb_debug_state((bool)(usb_selected_mode & USB_FUNCTION_SDB));
+ set_usb_debug_state((bool)(usb_selected_mode & USB_GADGET_FUNC_SDB));
}
/* Cache of vconf_get_int(VCONFKEY_USB_SEL_MODE) */
int usb_state_set_selected_mode(unsigned int mode)
{
int ret;
- int mode_v;
usb_selected_mode = mode;
- mode_v = get_mode_vconf_from_bitmap(mode);
- if (mode_v == SET_USB_INVALID) {
- _E("Failed to convert USB selected_mode to vconf. There is no vconf matches up with USB mode %#x", mode);
- return -EINVAL;
- }
-
- ret = vconf_set_int(VCONFKEY_USB_SEL_MODE, mode_v);
+ ret = vconf_set_int(VCONFKEY_USB_SEL_MODE, mode);
if (ret != VCONF_OK)
_E("Failed to set vconf value for USB selected mode: %d", vconf_get_ext_errno());
int usb_state_set_current_mode(unsigned int mode)
{
int ret = 0;
- int mode_v;
- static int old_mode_v = -1;
+ static int old_mode = -1;
usb_current_mode = mode;
- mode_v = get_mode_vconf_from_bitmap(mode);
- if (mode_v == SET_USB_INVALID) {
- _E("Failed to convert USB current_mode to vconf. There is no vconf matches up with mode %#x", mode);
- return -EINVAL;
- }
-
- if (old_mode_v != mode_v) {
- old_mode_v = mode_v;
- ret = vconf_set_int(VCONFKEY_USB_CUR_MODE, mode_v);
+ if (old_mode != mode) {
+ old_mode = mode;
+ ret = vconf_set_int(VCONFKEY_USB_CUR_MODE, mode);
if (ret < 0)
- _E("Failed to set vconf value for USB current mode: %d", vconf_get_ext_errno());
+ _E("Failed to set vconf value for USB current mode=%#x, error=%d", mode, vconf_get_ext_errno());
}
+
return ret;
}
void change_usb_state_notification_handler(unsigned int mode)
{
- if (mode & USB_FUNCTION_MTP)
+ if (mode & USB_GADGET_FUNC_MTP)
add_notification_handler();
- else if (mode == USB_FUNCTION_NONE)
+ else if (mode == USB_GADGET_FUNC_NONE)
remove_notification_handler();
}
/*
* USB_DISCONNECTED : VCONFKEY_SYSMAN_USB_DISCONNECTED
- * USB_CONNECTED + USB_FUNCTION_NONE : VCONFKEY_SYSMAN_USB_CONNECTED
- * USB_CONNECTED + USB_FUNCTION_OOOO : VCONFKEY_SYSMAN_USB_AVAILABLE
+ * USB_CONNECTED + USB_GADGET_FUNC_NONE : VCONFKEY_SYSMAN_USB_CONNECTED
+ * USB_CONNECTED + USB_GADGET_FUNC_OOOO : VCONFKEY_SYSMAN_USB_AVAILABLE
*
* When connecting the usb cable : "disconnected"(previous value) -> "connected" -> "available"
- * When disconnecting the usb cable : "available"(previous value) -> "dosconnected"
+ * When disconnecting the usb cable : "available"(previous value) -> "disconnected"
* When changing usb mode : "available"(previous value) -> "connected" -> "available"
*
* When USB cable is connected but USB initialization fails : It stays "connected" without going to "available"
#include "core/log.h"
#include "shared/device-notifier.h"
-#include "usb.h"
#include "usb-gadget.h"
+#include "usb-gadget-ops.h"
static bool tethering_state;
{
int ret;
unsigned new_mode;
- static unsigned int saved_prev_mode = USB_FUNCTION_NONE;
const unsigned int curr_mode = usb_state_get_selected_mode();
- const unsigned int predefined_rndis_mode_off = USB_FUNCTION_ACM | USB_FUNCTION_MTP;
- const unsigned int predefined_rndis_mode_on = USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS;
/*
- * On: Use predefined mode (USB_FUNCTION_ACM | USB_FUNCTION_SDB | USB_FUNCTION_RNDIS)
- * Off: Use predefined mode (USB_FUNCTION_MTP | USB_FUNCTION_ACM) + USB_FUNCTION_SDB(conditional)
+ * On: Use predefined mode (USB_GADGET_FUNC_ACM | USB_GADGET_FUNC_SDB | USB_GADGET_FUNC_RNDIS)
+ * Off: Use predefined mode (USB_GADGET_FUNC_MTP | USB_GADGET_FUNC_ACM) + USB_GADGET_FUNC_SDB(conditional)
*
- * Caution: If usb debug menu was enabled, add USB_FUNCTION_SDB to new mode to use.
+ * Caution: If usb debug menu was enabled, add USB_GADGET_FUNC_SDB to new mode to use.
*
- * When tethering is enabled, additionally enable usb debug(USB_FUNCTION_SDB) unconditionally.
+ * When tethering is enabled, additionally enable usb debug(USB_GADGET_FUNC_SDB) unconditionally.
* Since usb debug is always enabled when you turn off usb tethering, there is no way to know if usb debug was enabled or not.
* So before enabling usb tethering, save the previous state.
* This saved value is used to check if the debug menu was enabled or not when you turn off usb tethering.
* In addition, if someone changes the usb mode while tethering on,
* use the latest usb mode to determine the usb debug mode instead of using the saved value.
*
+ * ===================================
+ *
+ * The above policy, which is forcing combination of usb-gadget functions to a predefined set, is not
+ * mandatory anymore since introduction of usb-gadget bitmap. The usb-gadget bitmap would rather allow
+ * user to combine any kind of usb-gadget functions. This works in assumption that user knows validity
+ * of the combination, e.g., mtp would not work properly with rndis. Therefore, the deviced exactly
+ * does what a user did, not caring about validity of the combination.
+ *
*/
- if ((bool)on) {
- new_mode = predefined_rndis_mode_on;
- saved_prev_mode = curr_mode;
- } else {
- new_mode = predefined_rndis_mode_off;
-
- if (curr_mode != predefined_rndis_mode_on) /* someone changes the usb mode while tethering on, use latest usb mode */
- saved_prev_mode = curr_mode;
-
- if(saved_prev_mode & USB_FUNCTION_SDB)
- new_mode |= USB_FUNCTION_SDB;
-
- saved_prev_mode = USB_FUNCTION_NONE;
- }
+ if ((int)(intptr_t) on)
+ new_mode = curr_mode | USB_GADGET_FUNC_RNDIS;
+ else
+ new_mode = curr_mode & (~USB_GADGET_FUNC_RNDIS);
ret = usb_change_mode(new_mode, true);
if (ret < 0)
+++ /dev/null
-/*
- * deviced
- *
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-#include <vconf.h>
-#include <libsyscommon/libsystemd.h>
-
-#include "core/log.h"
-#include "core/udev.h"
-#include "display/poll.h"
-#include "display/display-ops.h"
-#include "shared/plugin.h"
-
-#include "usb.h"
-#include "usb-gadget.h"
-#include "usb-debug.h"
-#include "usb-tethering.h"
-
-static int usb_change_gadget(unsigned mode);
-
-static struct display_plugin *disp_plgn;
-
-static int usb_config_init(void)
-{
- unsigned int mode = usb_state_get_selected_mode();
-
- return usb_change_gadget(mode);
-}
-
-/* Precondition: USB_FUNCTION_NONE */
-static int usb_change_gadget(unsigned mode)
-{
- int ret;
-
- ret = usb_gadget_change_mode(mode);
- if (ret) {
- /* because usb does not work properly */
- (void)usb_state_set_current_mode(USB_FUNCTION_NONE);
- return ret;
- }
-
- _I("USB gadget changed to (%#x)", mode);
-
- return 0;
-}
-
-/* Precondition: USB_CONNECTED, USB_FUNCTION_NONE */
-static int usb_enable(unsigned int mode)
-{
- int ret;
-
- ret = usb_gadget_enable();
- if (ret < 0) {
- _E("Failed to enable USB config: %d", ret);
- goto out;
- }
-
- send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_AVAILABLE);
- (void)usb_state_set_current_mode(mode);
- change_usb_state_notification_handler(mode);
-
- return 0;
-
-out:
- /* Although this function has a USB_FUNCTION_NONE precondition, it is to protect against coding mistakes. */
- (void)usb_state_set_current_mode(USB_FUNCTION_NONE); /* because usb does not work properly */
- return ret;
-}
-
-/* Precondition: N/A */
-static int usb_disable(void)
-{
- int ret;
-
- (void)usb_state_set_current_mode(USB_FUNCTION_NONE);
- change_usb_state_notification_handler(USB_FUNCTION_NONE);
-
- ret = usb_gadget_disable();
- if (ret < 0) {
- _E("Failed to disable USB config: %d", ret);
- return ret;
- }
-
- return 0;
-}
-
-/* Precondition: USB_DISCONNECTED (Implicitly contains USB_FUNCTION_NONE) */
-static int usb_connected(void)
-{
- int ret;
- unsigned int mode = usb_state_get_selected_mode();
-
- if (disp_plgn->pm_lock_internal)
- disp_plgn->pm_lock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE, 0);
-
- usb_state_set_connection(USB_CONNECTED);
- send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_CONNECTED);
-
- ret = usb_enable(mode);
- if (ret < 0) {
- _E("Failed to enable USB gadget(%#x): %d", mode, ret);
- return ret;
- }
-
- return 0;
-}
-
-/* Precondition: USB_CONNECTED */
-static int usb_disconnected(void)
-{
- int ret;
-
- usb_state_set_connection(USB_DISCONNECTED);
- send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED);
-
- ret = usb_disable();
- if(ret < 0) {
- _E("Failed to disable USB gadget: %d", ret);
- /* Important: You have to keep going to unlock disp_plgn->pm_unlock_internal */
- }
-
- if (disp_plgn->pm_unlock_internal)
- disp_plgn->pm_unlock_internal(INTERNAL_LOCK_USB, LCD_OFF, STAY_CUR_STATE);
-
- return ret;
-}
-
-/* Called by dbus signal and vconf change(tethering, debug mode) */
-int usb_change_mode(unsigned int new_mode, bool change_debug_mode)
-{
- int ret;
- const unsigned int curr_mode = usb_state_get_current_mode();
- const unsigned int prev_mode = usb_state_get_selected_mode();
-
- if (prev_mode == new_mode) {
- _I("already using USB mode(%#x), current running mode (%#x)", prev_mode, curr_mode);
- return 0;
- }
-
- _I("USB change mode (%#x) -> (%#x), current running mode (%#x)", prev_mode, new_mode, curr_mode);
-
- /*
- * When you change the gadget(usb_change_gadget) for new usb mode, you must disable the gadget first.
- *
- * Even if the usb cable is plugged in, the current mode may be NULL due to usb fail.
- * So to find out which mode you are in, you should use the current mode, not the selected mode.
- */
- if (curr_mode != USB_FUNCTION_NONE) {
- /* Special case: Because usb gadget is disabled, temporarily switch usb state from available to connected. */
- send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_CONNECTED);
-
- ret = usb_disable();
- if (ret < 0) {
- _E("Failed to disable current USB mode.");
- return ret;
- }
- }
-
- /*
- * You should always change the gadget once when the usb mode is changed.
- * In this state, usb_enable() is called when the usb cable is connected
- * and usb_disable() is called when the usb cable is disconnected.
- */
- ret = usb_change_gadget(new_mode);
- if (ret < 0) {
- _E("Failed to change USB gadget: %d", ret);
- return ret;
- }
-
- if (usb_state_get_connection() == USB_CONNECTED) {
- ret = usb_enable(new_mode);
- if (ret < 0) {
- _E("Failed to enable USB mode: %d", ret);
- return ret;
- }
- }
-
- /* If you success to change the runtime usb configuration, do change the selected mode. */
- (void)usb_state_set_selected_mode(new_mode);
-
- /* To sync with vconf for debug mode */
- if (change_debug_mode)
- set_usb_debug_state((bool)(new_mode & USB_FUNCTION_SDB));
-
- return 0;
-}
-
-/* Called by extcon udev event */
-static int usb_state_changed(const char *index, int new_status)
-{
- int ret = -1;
- static int old_status = -1; /* -1: Uninitialized, 0: disconnected, 1: connected */
-
- /* For debugging. Do not move the location. */
- _I("USB state is changed by extcon from (%d) to (%d).", old_status, new_status);
-
- if (old_status == new_status)
- return 0;
-
- switch (new_status) {
- case USB_CONNECTED:
- ret = usb_connected();
- break;
-
- case USB_DISCONNECTED:
- if (old_status == -1) {
- /* only initialize the internal data state and skip usb hal operation. */
- _I("USB is initialized without USB connection");
-
- /* From usb_disconnected() */
- usb_state_set_connection(USB_DISCONNECTED);
- send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED);
-
- /* From usb_disable() */
- (void)usb_state_set_current_mode(USB_FUNCTION_NONE);
- change_usb_state_notification_handler(USB_FUNCTION_NONE);
-
- ret = 0;
- } else
- ret = usb_disconnected();
- break;
-
- default:
- _E("Invalid USB state(%d).", new_status);
- return -EINVAL;
- }
-
- if (ret < 0) {
- _E("Failed to operate USB connection: %d", ret);
- return ret;
- }
-
- old_status = new_status;
-
- return ret;
-}
-
-static void uevent_usb_mode_handler(struct udev_device *dev)
-{
- const char *state = NULL;
-
- state = udev_device_get_property_value(dev, "USB_STATE");
- if (!state)
- return;
-
- if (strncmp(state, "CONFIGURED", strlen(state) + 1))
- return;
-
- _I("USB udev event happend : CONFIGURED USB_STATE");
-
- if (usb_state_get_selected_mode() & USB_FUNCTION_ACM)
- systemd_start_unit_wait_started ("data-router.service", NULL, -1);
-
- if (usb_state_get_selected_mode() & USB_FUNCTION_SDB)
- systemd_start_unit_wait_started ("sdbd.service", NULL, -1);
-
- if (usb_state_get_selected_mode() & USB_FUNCTION_MTP)
- systemd_start_unit_wait_started ("mtp-responder.service", NULL, -1);
-}
-
-static struct uevent_handler uh = {
- .subsystem = "usb_mode",
- .uevent_func = uevent_usb_mode_handler,
-};
-
-static void usb_init(void *data)
-{
- int ret;
-
- usb_state_load_custom_mode();
- usb_state_retrieve_selected_mode();
-
- ret = usb_gadget_init();
- if (ret < 0) {
- CRITICAL_LOG("USB client cannot be used: %d", ret);
- return;
- }
-
- ret = usb_config_init();
- if (ret < 0)
- _E("Failed to initialize USB configuation.");
-
- ret = register_udev_uevent_control(&uh);
- if (ret < 0)
- _E("Failed to register udev event(%d) for USB", ret);
-
- ret = usb_dbus_init();
- if (ret < 0)
- _E("Failed to init dbus(%d) for USB", ret);
-
- add_usb_tethering_handler();
- add_usb_debug_handler();
-}
-
-static void usb_exit(void *data)
-{
- remove_usb_debug_handler();
- remove_usb_tethering_handler();
-
- usb_state_set_connection(USB_DISCONNECTED);
- send_usb_state_changed_event(VCONFKEY_SYSMAN_USB_DISCONNECTED);
- (void)usb_state_set_current_mode(USB_FUNCTION_NONE);
- change_usb_state_notification_handler(USB_FUNCTION_NONE);
-
- unregister_udev_uevent_control(&uh);
- usb_gadget_disable();
- usb_gadget_exit();
-
-}
-
-static struct extcon_ops extcon_usb_ops = {
- .name = EXTCON_CABLE_USB,
- .init = usb_init,
- .exit = usb_exit,
- .update = usb_state_changed,
-};
-
-EXTCON_OPS_REGISTER(extcon_usb_ops)
-
-static void __CONSTRUCTOR__ initialize(void)
-{
- disp_plgn = get_var_display_plugin();
- if (!disp_plgn)
- _E("Failed to get display plugin variable.");
-}
+++ /dev/null
-/*
- * deviced
- *
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __DEVICED_USB_H__
-#define __DEVICED_USB_H__
-
-#include <limits.h>
-#include <stdbool.h>
-#include "extcon/extcon.h"
-
-#define USB_FUNCTION_INVALID UINT_MAX
-#define SET_USB_INVALID INT_MAX
-
-int usb_dbus_init(void);
-
-void usb_state_load_custom_mode(void);
-
-int usb_change_mode(unsigned int mode, bool change_debug_mode);
-void usb_state_retrieve_selected_mode(void);
-unsigned int get_mode_bitmap_from_vconf(int mode_v);
-
-void broadcast_usb_state_changed(void);
-void send_usb_state_changed_event(int status);
-void change_usb_state_notification_handler(unsigned int mode);
-
-unsigned int usb_state_get_selected_mode(void);
-int usb_state_set_selected_mode(unsigned int mode);
-
-unsigned int usb_state_get_current_mode(void);
-int usb_state_set_current_mode(unsigned int mode);
-
-extcon_usb_state_e usb_state_get_connection(void);
-void usb_state_set_connection(extcon_usb_state_e conn);
-
-#endif /* __USB_CLIENT_H__ */