external connection: add HAL for external connection
authorTaeyoung Kim <ty317.kim@samsung.com>
Thu, 17 Dec 2015 11:38:20 +0000 (20:38 +0900)
committerTaeyoung Kim <ty317.kim@samsung.com>
Thu, 17 Dec 2015 11:41:39 +0000 (20:41 +0900)
- The HAL will exchange extcon operation of kernel.
- Currently, USB, earjack, and Dock are available.
- For TM1 target, external connections are handled by switch subsystem,
  thus the HAL should work well.

Change-Id: I5ae3b90b3089a66890fe9b381db072b3cd58fc7e
Signed-off-by: Taeyoung Kim <ty317.kim@samsung.com>
CMakeLists.txt
hw/external_connection/CMakeLists.txt [new file with mode: 0644]
hw/external_connection/external_connection.c [new file with mode: 0644]
hw/udev.c [new file with mode: 0644]
hw/udev.h [new file with mode: 0644]
packaging/device-manager-plugin-sc7730.spec

index cd7816c..ad2e1fa 100644 (file)
@@ -5,4 +5,5 @@ SET(PREFIX ${CMAKE_INSTALL_PREFIX})
 
 ADD_SUBDIRECTORY(hw/display)
 ADD_SUBDIRECTORY(hw/led)
+ADD_SUBDIRECTORY(hw/external_connection)
 ADD_SUBDIRECTORY(hw/touchscreen)
diff --git a/hw/external_connection/CMakeLists.txt b/hw/external_connection/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4ffccfd
--- /dev/null
@@ -0,0 +1,19 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(external_connection C)
+
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(external_connection_pkgs REQUIRED hwcommon dlog glib-2.0)
+
+FOREACH(flag ${external_connection_pkgs_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+
+ADD_LIBRARY(${PROJECT_NAME} MODULE external_connection.c ../shared.c ../udev.c)
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${external_connection_pkgs_LDFLAGS})
+SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries)
diff --git a/hw/external_connection/external_connection.c b/hw/external_connection/external_connection.c
new file mode 100644 (file)
index 0000000..0a94629
--- /dev/null
@@ -0,0 +1,216 @@
+/*
+ * device-node
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <linux/limits.h>
+#include <dirent.h>
+
+#include <hw/external_connection.h>
+#include "../shared.h"
+#include "../udev.h"
+
+#define SWITCH_ROOT_PATH "/sys/devices/virtual/switch"
+
+static struct switch_device {
+       char *type;
+       char *name;
+       int state;
+} switch_devices[] = {
+       { EXTERNAL_CONNECTION_USB,       "usb_cable", 0 },
+       { EXTERNAL_CONNECTION_DOCK,      "dock"     , 0 },
+       { EXTERNAL_CONNECTION_HEADPHONE, "earjack"  , 0 },
+};
+
+static struct uevent_data {
+       ConnectionUpdated updated_cb;
+       void *data;
+} udata = { 0, };
+
+static void uevent_delivered(struct udev_device *dev)
+{
+       struct connection_info info;
+       char *name, *state;
+       int i;
+
+       _I("Switch uevent is delivered");
+
+       name = (char *)udev_device_get_property_value(dev, "SWITCH_NAME");
+       if (!name)
+               return;
+
+       state = (char *)udev_device_get_property_value(dev, "SWITCH_STATE");
+       if (!state)
+               return;
+
+       for (i = 0 ; i < ARRAY_SIZE(switch_devices) ; i++) {
+               if (strncmp(name, switch_devices[i].name, strlen(name) + 1))
+                       continue;
+
+               switch_devices[i].state = atoi(state);
+
+               info.name = switch_devices[i].type;
+               info.state = state;
+               info.flags = 0;
+
+               if (udata.updated_cb)
+                       udata.updated_cb(&info, udata.data);
+               else
+                       _E("callback is NULL");
+       }
+}
+
+static struct uevent_handler uh = {
+       .subsystem = "switch",
+       .uevent_func = uevent_delivered,
+};
+
+static int external_connection_register_changed_event(
+               ConnectionUpdated updated_cb, void *data)
+{
+       int ret;
+
+       ret = uevent_control_kernel_start();
+       if (ret < 0) {
+               _E("Failed to register uevent handler (%d)", ret);
+               return ret;
+       }
+
+       ret = register_kernel_event_control(&uh);
+       if (ret < 0)
+               _E("Failed to register kernel event control (%d)", ret);
+
+       if (udata.updated_cb == NULL) {
+               udata.updated_cb = updated_cb;
+               udata.data = data;
+       } else
+               _E("update callback is already registered");
+
+       return ret;
+}
+
+static void external_connection_unregister_changed_event(
+               ConnectionUpdated updated_cb)
+{
+       unregister_kernel_event_control(&uh);
+       uevent_control_kernel_stop();
+       udata.updated_cb = NULL;
+       udata.data = NULL;
+}
+
+static int read_switch_state(char *path)
+{
+       char node[128], val[8];
+       FILE *fp;
+
+       if (!path)
+               return -EINVAL;
+
+       snprintf(node, sizeof(node), "%s/%s/state",
+                       SWITCH_ROOT_PATH, path);
+
+       fp = fopen(node, "r");
+       if (!fp) {
+               _E("Failed to open (%s)", path);
+               return -ENOMEM;
+       }
+
+       if (!fgets(val, sizeof(val), fp)) {
+               _E("Failed to read (%s)", path);
+               fclose(fp);
+               return -ENOENT;
+       }
+
+       fclose(fp);
+
+       return atoi(val);
+}
+
+static int external_connection_get_current_state(
+               ConnectionUpdated updated_cb, void *data)
+{
+       int ret, i;
+       struct connection_info info;
+       char buf[8];
+
+       if (!updated_cb)
+               return -EINVAL;
+
+       for (i = 0 ; i < ARRAY_SIZE(switch_devices) ; i++) {
+               ret = read_switch_state(switch_devices[i].name);
+               if (ret < 0) {
+                       _E("Failed to get value of (%s, ret:%d)",
+                                       switch_devices[i].name, ret);
+                       continue;
+               }
+
+               info.name = switch_devices[i].type;
+               snprintf(buf, sizeof(buf), "%d", ret);
+               info.state = buf;
+
+               updated_cb(&info, data);
+       }
+
+       return 0;
+}
+
+static int external_connection_open(struct hw_info *info,
+               const char *id, struct hw_common **common)
+{
+       struct external_connection_device *external_connection_dev;
+
+       if (!info || !common)
+               return -EINVAL;
+
+       external_connection_dev = calloc(1, sizeof(struct external_connection_device));
+       if (!external_connection_dev)
+               return -ENOMEM;
+
+       external_connection_dev->common.info = info;
+       external_connection_dev->register_changed_event
+               = external_connection_register_changed_event;
+       external_connection_dev->unregister_changed_event
+               = external_connection_unregister_changed_event;
+       external_connection_dev->get_current_state
+               = external_connection_get_current_state;
+
+       *common = (struct hw_common *)external_connection_dev;
+       return 0;
+}
+
+static int external_connection_close(struct hw_common *common)
+{
+       if (!common)
+               return -EINVAL;
+
+       free(common);
+       return 0;
+}
+
+HARDWARE_MODULE_STRUCTURE = {
+       .magic = HARDWARE_INFO_TAG,
+       .hal_version = HARDWARE_INFO_VERSION,
+       .device_version = EXTERNAL_CONNECTION_HARDWARE_DEVICE_VERSION,
+       .id = EXTERNAL_CONNECTION_HARDWARE_DEVICE_ID,
+       .name = "external_connection",
+       .open = external_connection_open,
+       .close = external_connection_close,
+};
diff --git a/hw/udev.c b/hw/udev.c
new file mode 100644 (file)
index 0000000..562725b
--- /dev/null
+++ b/hw/udev.c
@@ -0,0 +1,298 @@
+/*
+ * device-manager
+ *
+ * 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 <stdio.h>
+#include <stdbool.h>
+#include <errno.h>
+#include <libudev.h>
+#include <glib.h>
+#include <string.h>
+#include "shared.h"
+#include "udev.h"
+
+#define EVENT_KERNEL       "kernel"
+#define EVENT_UDEV         "udev"
+
+#define UDEV_MONITOR_SIZE   (10*1024)
+
+struct uevent_info {
+       struct udev_monitor *mon;
+       GIOChannel *ch;
+       guint eventid;
+       GList *event_list;
+};
+
+
+/* Uevent */
+static struct udev *udev;
+static struct uevent_info kevent; /* kernel */
+static struct uevent_info uevent; /* udev */
+
+static gboolean uevent_control_cb(GIOChannel *channel,
+               GIOCondition cond, void *data)
+{
+       struct uevent_info *info = data;
+       struct udev_device *dev;
+       struct uevent_handler *l;
+       GList *elem;
+       const char *subsystem;
+       int len;
+
+       if (!info) {
+               _E("data is invalid");
+               return TRUE;
+       }
+
+       dev = udev_monitor_receive_device(info->mon);
+       if (!dev)
+               return TRUE;
+
+       subsystem = udev_device_get_subsystem(dev);
+       if (!subsystem)
+               goto out;
+
+       len = strlen(subsystem);
+
+       for (elem = info->event_list ; elem ; elem = g_list_next(elem)) {
+               l = elem->data;
+               if (!l)
+                       continue;
+               if (!strncmp(l->subsystem, subsystem, len) &&
+                   l->uevent_func)
+                       l->uevent_func(dev);
+       }
+
+out:
+       udev_device_unref(dev);
+       return TRUE;
+}
+
+static int uevent_control_stop(struct uevent_info *info)
+{
+       struct udev_device *dev;
+
+       if (!info)
+               return -EINVAL;
+
+       if (info->eventid) {
+               g_source_remove(info->eventid);
+               info->eventid = 0;
+       }
+       if (info->ch) {
+               g_io_channel_unref(info->ch);
+               info->ch = NULL;
+       }
+       if (info->mon) {
+               dev = udev_monitor_receive_device(info->mon);
+               if (dev)
+                       udev_device_unref(dev);
+               udev_monitor_unref(info->mon);
+               info->mon = NULL;
+       }
+       if (udev)
+               udev = udev_unref(udev);
+       return 0;
+}
+
+static int uevent_control_start(const char *type,
+               struct uevent_info *info)
+{
+       struct uevent_handler *l;
+       GList *elem;
+       int fd;
+       int ret;
+
+       if (!info)
+               return -EINVAL;
+
+       if (info->mon) {
+               _E("%s uevent control routine is alreay started", type);
+               return -EINVAL;
+       }
+
+       if (!udev) {
+               udev = udev_new();
+               if (!udev) {
+                       _E("error create udev");
+                       return -EINVAL;
+               }
+       } else
+               udev = udev_ref(udev);
+
+       info->mon = udev_monitor_new_from_netlink(udev, type);
+       if (info->mon == NULL) {
+               _E("error udev_monitor create");
+               goto stop;
+       }
+
+       ret = udev_monitor_set_receive_buffer_size(info->mon,
+                       UDEV_MONITOR_SIZE);
+       if (ret != 0) {
+               _E("fail to set receive buffer size");
+               goto stop;
+       }
+
+       for (elem = info->event_list ; elem ; elem = g_list_next(elem)) {
+               l = elem->data;
+               ret = udev_monitor_filter_add_match_subsystem_devtype(
+                               info->mon,
+                               l->subsystem, NULL);
+               if (ret < 0) {
+                       _E("error apply subsystem filter");
+                       goto stop;
+               }
+       }
+
+       ret = udev_monitor_filter_update(info->mon);
+       if (ret < 0)
+               _E("error udev_monitor_filter_update");
+
+       fd = udev_monitor_get_fd(info->mon);
+       if (fd == -1) {
+               _E("error udev_monitor_get_fd");
+               goto stop;
+       }
+
+       info->ch = g_io_channel_unix_new(fd);
+       info->eventid = g_io_add_watch(info->ch,
+                       G_IO_IN, uevent_control_cb, info);
+       if (info->eventid == 0) {
+               _E("Failed to add channel watch");
+               goto stop;
+       }
+
+       if (udev_monitor_enable_receiving(info->mon) < 0) {
+               _E("error unable to subscribe to udev events");
+               goto stop;
+       }
+
+       return 0;
+stop:
+       uevent_control_stop(info);
+       return -EINVAL;
+}
+
+int uevent_control_kernel_start(void)
+{
+       return uevent_control_start(EVENT_KERNEL, &kevent);
+}
+
+void uevent_control_kernel_stop(void)
+{
+       uevent_control_stop(&kevent);
+}
+
+int uevent_control_udev_start(void)
+{
+       return uevent_control_start(EVENT_UDEV, &uevent);
+}
+
+void uevent_control_udev_stop(void)
+{
+       uevent_control_stop(&uevent);
+}
+
+static int register_uevent_control(struct uevent_info *info,
+               struct uevent_handler *uh)
+{
+       struct uevent_handler *l;
+       GList *elem;
+       int r;
+       bool matched = false;
+       int len;
+
+       if (!info || !uh || !uh->subsystem)
+               return -EINVAL;
+
+       /* if udev is not initialized, it just will be added list */
+       if (!udev || !info->mon)
+               goto add_list;
+
+       len = strlen(uh->subsystem);
+       /* check if the same subsystem is already added */
+       for (elem = info->event_list; elem ; elem = g_list_next(elem)) {
+               l = elem->data;
+               if (!strncmp(l->subsystem, uh->subsystem, len)) {
+                       matched = true;
+                       break;
+               }
+       }
+
+       /* the first request to add subsystem */
+       if (!matched) {
+               r = udev_monitor_filter_add_match_subsystem_devtype(info->mon,
+                               uh->subsystem, NULL);
+               if (r < 0) {
+                       _E("fail to add %s subsystem : %d", uh->subsystem, r);
+                       return -EPERM;
+               }
+       }
+
+       r = udev_monitor_filter_update(info->mon);
+       if (r < 0)
+               _E("fail to update udev monitor filter : %d", r);
+
+add_list:
+       info->event_list = g_list_append(info->event_list, uh);
+       return 0;
+}
+
+static int unregister_uevent_control(struct uevent_info *info,
+               const struct uevent_handler *uh)
+{
+       struct uevent_handler *l;
+       GList *n, *next;
+       int len;
+
+       if (!info || !uh || !uh->subsystem)
+               return -EINVAL;
+
+       len = strlen(uh->subsystem);
+       for (n = info->event_list, next = g_list_next(n) ;
+                       n ; n = next, next = g_list_next(n)) {
+               l = n->data;
+               if (!strncmp(l->subsystem, uh->subsystem, len) &&
+                   l->uevent_func == uh->uevent_func) {
+                       info->event_list = g_list_delete_link(info->event_list, n);
+                       return 0;
+               }
+       }
+
+       return -ENOENT;
+}
+
+int register_kernel_event_control(struct uevent_handler *uh)
+{
+       return register_uevent_control(&kevent, uh);
+}
+
+void unregister_kernel_event_control(struct uevent_handler *uh)
+{
+       unregister_uevent_control(&kevent, uh);
+}
+
+int register_udev_event_control(struct uevent_handler *uh)
+{
+       return register_uevent_control(&uevent, uh);
+}
+
+void unregister_udev_event_control(struct uevent_handler *uh)
+{
+       unregister_uevent_control(&uevent, uh);
+}
diff --git a/hw/udev.h b/hw/udev.h
new file mode 100644 (file)
index 0000000..d2aeff1
--- /dev/null
+++ b/hw/udev.h
@@ -0,0 +1,43 @@
+/*
+ * device-manager
+ *
+ * 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 __UDEV_H__
+#define __UDEV_H__
+
+#include <libudev.h>
+
+struct uevent_handler {
+       const char *subsystem;
+       void (*uevent_func)(struct udev_device *dev);
+       void *data;
+};
+
+int uevent_control_kernel_start(void);
+void uevent_control_kernel_stop(void);
+
+int uevent_control_udev_start(void);
+void uevent_control_udev_stop(void);
+
+int register_kernel_event_control(struct uevent_handler *uh);
+void unregister_kernel_event_control(struct uevent_handler *uh);
+
+int register_udev_event_control(struct uevent_handler *uh);
+void  unregister_udev_event_control(struct uevent_handler *uh);
+
+#endif /* __UDEV_H__ */
index 953da39..86e1ca0 100644 (file)
@@ -11,6 +11,7 @@ Requires(postun): /sbin/ldconfig
 BuildRequires:  cmake
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(hwcommon)
+BuildRequires:  pkgconfig(glib-2.0)
 
 %description
 Device manager plugin sc7730