diff options
author | taeyoung <ty317.kim@samsung.com> | 2015-02-10 16:54:30 +0900 |
---|---|---|
committer | Jiyoung Yun <jy910.yun@samsung.com> | 2015-02-12 20:57:09 -0800 |
commit | 54e4cc087ea8ce909f9b0179455b4e6d27ea3a49 (patch) | |
tree | 3995d4a475a5eac5d2720c0287fe391d8f3a951a | |
parent | 9603477d906917419b0402b7498011659526f6af (diff) | |
download | deviced-54e4cc087ea8ce909f9b0179455b4e6d27ea3a49.tar.gz deviced-54e4cc087ea8ce909f9b0179455b4e6d27ea3a49.tar.bz2 deviced-54e4cc087ea8ce909f9b0179455b4e6d27ea3a49.zip |
usb: add plugin interfaces for usb configuration
- USB configuration manner can be changed according to the Kernel or vendors.
Thus USB configuration setting codes should be implemented by plugin structure.
- This modification contains basic structure for the USB configuration plugin.
Change-Id: I4afb1033fb59ba132182ae16ffef9fbb34192f13
Signed-off-by: taeyoung <ty317.kim@samsung.com>
-rw-r--r-- | src/usb/usb.c | 138 | ||||
-rw-r--r-- | src/usb/usb.h | 50 |
2 files changed, 183 insertions, 5 deletions
diff --git a/src/usb/usb.c b/src/usb/usb.c index 3d2830ce..02fd197f 100644 --- a/src/usb/usb.c +++ b/src/usb/usb.c @@ -20,21 +20,133 @@ #include <stdio.h> #include "core/log.h" +#include "core/list.h" #include "core/common.h" #include "core/device-notifier.h" #include "extcon/extcon.h" +#include "usb.h" enum usb_state { USB_DISCONNECTED, USB_CONNECTED, }; +static dd_list *config_list; struct extcon_ops extcon_usb_ops; +static const struct usb_config_plugin_ops *config_plugin; + +void add_usb_config(const struct usb_config_ops *ops) +{ + DD_LIST_APPEND(config_list, ops); +} + +void remove_usb_config(const struct usb_config_ops *ops) +{ + DD_LIST_REMOVE(config_list, ops); +} + +static int usb_config_module_load(void) +{ + dd_list *l; + struct usb_config_ops *ops; + + DD_LIST_FOREACH(config_list, l, ops) { + if (ops->is_valid && ops->is_valid()) { + if (ops->load) + config_plugin = ops->load(); + return 0; + } + } + return -ENOENT; +} + +static void usb_config_module_unload(void) +{ + dd_list *l; + struct usb_config_ops *ops; + + config_plugin = NULL; + + DD_LIST_FOREACH(config_list, l, ops) { + if (ops->is_valid && ops->is_valid()) { + if (ops->release) + ops->release(); + } + } +} + +static int usb_config_init(void) +{ + if (!config_plugin) { + _E("There is no usb config plugin"); + return -ENOENT; + } + + if (config_plugin->init == NULL) { + _E("There is no usb config init function"); + return -ENOENT; + } + + /* TODO: + * parameter "DEFAULT" can be changed */ + return config_plugin->init("DEFAULT"); +} + +static void usb_config_deinit(void) +{ + if (!config_plugin) { + _E("There is no usb config plugin"); + return; + } + + if (config_plugin->deinit == NULL) { + _E("There is no usb config deinit function"); + return; + } + + /* TODO: + * parameter "DEFAULT" can be changed */ + config_plugin->deinit("DEFAULT"); +} + +static int usb_config_enable(void) +{ + if (!config_plugin) { + _E("There is no usb config plugin"); + return -ENOENT; + } + + if (config_plugin->enable == NULL) { + _E("There is no usb config enable function"); + return -ENOENT; + } + + /* TODO: + * parameter "DEFAULT" can be changed */ + return config_plugin->enable("DEFAULT"); +} + +static int usb_config_disable(void) +{ + if (!config_plugin) { + _E("There is no usb config plugin"); + return -ENOENT; + } + + if (config_plugin->disable == NULL) { + _E("There is no usb config disable function"); + return -ENOENT; + } + + /* TODO: + * parameter "DEFAULT" can be changed */ + return config_plugin->disable("DEFAULT"); +} static int usb_state_changed(void *data) { static int state = USB_DISCONNECTED; - int input; + int input, ret; if (!data) return -EINVAL; @@ -49,26 +161,40 @@ static int usb_state_changed(void *data) switch (input) { case USB_CONNECTED: _I("USB cable is connected"); + ret = usb_config_enable(); break; case USB_DISCONNECTED: _I("USB cable is disconnected"); + ret = usb_config_disable(); break; default: _E("Invalid USB state(%d)", state); return -EINVAL; } + if (ret < 0) + _E("Failed to operate usb connection(%d)", ret); + else + state = input; - state = input; - - return 0; + return ret; } static void usb_init(void *data) { - int ret, status; + int ret; register_notifier(DEVICE_NOTIFIER_USB, usb_state_changed); + ret = usb_config_module_load(); + if (ret < 0) { + _E("Failed to get config module (%d)", ret); + return; + } + + ret = usb_config_init(); + if (ret < 0) + _E("Failed to initialize usb configuation"); + ret = usb_state_changed(&(extcon_usb_ops.status)); if (ret < 0) _E("Failed to update usb status(%d)", ret); @@ -77,6 +203,8 @@ static void usb_init(void *data) static void usb_exit(void *data) { unregister_notifier(DEVICE_NOTIFIER_USB, usb_state_changed); + usb_config_deinit(); + usb_config_module_unload(); } struct extcon_ops extcon_usb_ops = { diff --git a/src/usb/usb.h b/src/usb/usb.h new file mode 100644 index 00000000..5c3276d0 --- /dev/null +++ b/src/usb/usb.h @@ -0,0 +1,50 @@ +/* + * 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 __USB_CLIENT_H__ +#define __USB_CLIENT_H__ + +#define USB_CONFIG_OPS_REGISTER(dev) \ +static void __CONSTRUCTOR__ usb_config_init(void) \ +{ \ + add_usb_config(dev); \ +} \ +static void __DESTRUCTOR__ usb_config_exit(void) \ +{ \ + remove_usb_config(dev); \ +} + +struct usb_config_ops { + bool (*is_valid)(void); + const struct usb_config_plugin_ops *(*load)(void); + void (*release)(void); +}; + +/* TODO + * move it to proper location */ +struct usb_config_plugin_ops { + int (*init)(char *name); + void (*deinit)(char *name); + int (*enable)(char *name); + int (*disable)(char *name); +}; + +void add_usb_config(const struct usb_config_ops *ops); +void remove_usb_config(const struct usb_config_ops *ops); + +#endif /* __USB_CLIENT_H__ */ |