From: INSUN PYO Date: Wed, 26 Feb 2020 10:32:25 +0000 (+0900) Subject: Cleanup usb code X-Git-Tag: submit/tizen/20200228.005656^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F66%2F226066%2F4;p=platform%2Fcore%2Fsystem%2Fdeviced.git Cleanup usb code Change-Id: I6bee810fb808b2caf6de69bcd17db2a9ea4b8677 --- diff --git a/src/usb/usb-dbus.c b/src/usb/usb-dbus.c index b697932..402c3e9 100644 --- a/src/usb/usb-dbus.c +++ b/src/usb/usb-dbus.c @@ -41,7 +41,7 @@ static int get_usb_state(void) int result = 0; ret = vconf_get_int(VCONFKEY_SYSMAN_USB_STATUS, &val); - if (ret < 0) { + if (ret != VCONF_OK) { _E("Failed to get vconf value for usb_status: %d", vconf_get_ext_errno()); return ret; } @@ -104,6 +104,7 @@ void broadcast_usb_mode_changed(void) mode = usb_state_get_current_mode(); if (mode == prev_mode) return; + prev_mode = mode; _I("USB mode(%u) changed.", mode); diff --git a/src/usb/usb-state.c b/src/usb/usb-state.c index 6d154b4..5349dd3 100644 --- a/src/usb/usb-state.c +++ b/src/usb/usb-state.c @@ -109,12 +109,15 @@ static void usb_state_send_system_event(int status) case VCONFKEY_SYSMAN_USB_DISCONNECTED: str = EVT_VAL_USB_DISCONNECTED; break; + case VCONFKEY_SYSMAN_USB_CONNECTED: str = EVT_VAL_USB_CONNECTED; break; + case VCONFKEY_SYSMAN_USB_AVAILABLE: str = EVT_VAL_USB_AVAILABLE; break; + default: return; } @@ -200,7 +203,7 @@ int usb_state_set_selected_mode(unsigned int mode) } ret = vconf_set_int(VCONFKEY_USB_SEL_MODE, mode_v); - if (ret < 0) + if (ret != VCONF_OK) _E("Failed to set vconf value for usb selected mode: %d", vconf_get_ext_errno()); return ret; @@ -247,38 +250,6 @@ static void usb_state_set_connection(extcon_usb_state_e conn) usb_connection = conn; } - -#if 0 -char *usb_state_get_mode_str(unsigned int mode, char *str, size_t len) -{ - int i; - int ret; - int flag = 0; - - if (mode == USB_FUNCTION_NONE) { - snprintf(str, len, "%s", ""); - } else { - for (i = 0; i < ARRAY_SIZE(_available_funcs); ++i) { - if (mode & _available_funcs[i]->id) { - ret = snprintf(str, len, "%s,", - _available_funcs[i]->name); - if (ret >= len) - return str; - str += ret; - len -= ret; - flag = 1; - } - } - if (flag == 1) { - /* eliminate trailing comma */ - *(str - 1) = '\0'; - } - } - - return str; -} -#endif - static void media_noti_cb(GVariant *var, void *user_data, GError *err) { int id = 0; @@ -335,7 +306,6 @@ static void change_usb_state_notification_handler(unsigned int mode) static void send_usb_state_changed_event(extcon_usb_state_e state, unsigned int mode) { - int ret; int status; static int old_status = -1; @@ -354,8 +324,7 @@ static void send_usb_state_changed_event(extcon_usb_state_e state, unsigned int usb_state_send_system_event(status); - ret = vconf_set_int(VCONFKEY_SYSMAN_USB_STATUS, status); - if (ret < 0) + if (vconf_set_int(VCONFKEY_SYSMAN_USB_STATUS, status) != VCONF_OK) _E("Failed to set vconf value for usb status: %d", vconf_get_ext_errno()); broadcast_usb_state_changed(); diff --git a/src/usb/usb-tethering.c b/src/usb/usb-tethering.c index 4ca4e06..b74e87c 100644 --- a/src/usb/usb-tethering.c +++ b/src/usb/usb-tethering.c @@ -59,8 +59,8 @@ static void usb_tethering_changed(keynode_t *key, void *data) static int usb_tethering_mode_changed(void *data) { bool on; - unsigned mode; int ret = 0; + unsigned mode; on = (bool)data; @@ -92,19 +92,15 @@ static int usb_tethering_mode_changed(void *data) void add_usb_tethering_handler(void) { - if (vconf_notify_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE, - usb_tethering_changed, NULL) != 0) + if (vconf_notify_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE, usb_tethering_changed, NULL) != VCONF_OK) _E("Failed to add usb tethering handler."); - register_notifier(DEVICE_NOTIFIER_USB_TETHERING_MODE, - usb_tethering_mode_changed); + register_notifier(DEVICE_NOTIFIER_USB_TETHERING_MODE, usb_tethering_mode_changed); } void remove_usb_tethering_handler(void) { - vconf_ignore_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE, - usb_tethering_changed); + vconf_ignore_key_changed(VCONFKEY_MOBILE_HOTSPOT_MODE, usb_tethering_changed); - unregister_notifier(DEVICE_NOTIFIER_USB_TETHERING_MODE, - usb_tethering_mode_changed); + unregister_notifier(DEVICE_NOTIFIER_USB_TETHERING_MODE, usb_tethering_mode_changed); } diff --git a/src/usb/usb.c b/src/usb/usb.c index b551378..5a84d95 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -162,8 +162,8 @@ static void usb_release() static int usb_config_init(void) { int ret; - struct usb_gadget_id gadget_id; struct usb_gadget *gadget; + struct usb_gadget_id gadget_id; if (!gadget_translator || !usb_client) goto no_hal; @@ -184,6 +184,7 @@ static int usb_config_init(void) _E("Unable to configure gadget: %d", ret); return ret; + no_hal: /* TODO. Maybe some default action here? */ return -ENODEV; @@ -197,6 +198,7 @@ static void usb_config_deinit(void) usb_client->disable(usb_client); return; + no_hal: /* TODO. Maybe some default action here? */ return; @@ -222,6 +224,7 @@ static int usb_config_disable(void) usb_client->disable(usb_client); return 0; + no_hal: /* TODO. Maybe some default action here? */ return -ENODEV; @@ -229,9 +232,9 @@ no_hal: static int usb_change_gadget(unsigned mode) { - struct usb_gadget_id gadget_id; - struct usb_gadget *gadget; int ret; + struct usb_gadget *gadget; + struct usb_gadget_id gadget_id; if (!gadget_translator || !usb_client) goto no_hal; @@ -250,13 +253,13 @@ static int usb_change_gadget(unsigned mode) gadget_translator->cleanup_gadget(gadget); if (ret) { _E("Unable to configure gadget: %d", ret); - goto out; + return ret; } _I("USB gadget changed to %d.", mode); -out: return ret; + no_hal: /* TODO. Maybe some default action here? */ return -ENODEV; @@ -309,6 +312,30 @@ static int usb_disable(void) return 0; } +static int usb_connected(void) +{ + int ret; + unsigned int mode = usb_state_get_selected_mode(); + + usb_state_update_state(USB_CONNECTED, USB_FUNCTION_NONE); + + ret = usb_enable(mode); + if (ret < 0) { + _E("Failed to enable usb gadget(%d): %d", mode, ret); + return ret; + } + + return 0; +} + +static int usb_disconnected(void) +{ + usb_state_update_state(USB_DISCONNECTED, USB_FUNCTION_NONE); + + return usb_disable(); +} + +/* Called by dbus signal and vconf change(tethering, debug mode) */ int usb_change_mode(unsigned mode) { int ret; @@ -341,56 +368,38 @@ int usb_change_mode(unsigned mode) return 0; } -static int usb_connected(void) -{ - int ret; - unsigned int mode = usb_state_get_selected_mode(); - - usb_state_update_state(USB_CONNECTED, USB_FUNCTION_NONE); - - ret = usb_enable(mode); - if (ret < 0) { - _E("Failed to enable usb gadget(%d): %d", mode, ret); - return ret; - } - - return 0; -} - -static int usb_disconnected(void) -{ - usb_state_update_state(USB_DISCONNECTED, USB_FUNCTION_NONE); - - return usb_disable(); -} - -static int usb_state_changed(int status) +/* Called by extcon udev event */ +static int usb_state_changed(int new_status) { - static int old = -1; /* to update at the first time */ - int ret; + int ret = -1; + static int old_status = -1; /* For debugging. Do not move the location. */ - _I("USB state is changed from (%d) to (%d).", old, status); + _I("USB state is changed from (%d) to (%d).", old_status, new_status); - if (old == status) + if (old_status == new_status) return 0; - switch (status) { + switch (new_status) { case USB_CONNECTED: ret = usb_connected(); break; + case USB_DISCONNECTED: ret = usb_disconnected(); break; + default: - _E("Invalid USB state(%d).", status); + _E("Invalid USB state(%d).", new_status); return -EINVAL; } - if (ret < 0) + if (ret < 0) { _E("Failed to operate usb connection: %d", ret); - else - old = status; + return ret; + } + + old_status = new_status; return ret; }