4 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <linux/limits.h>
27 #include <hw/touchscreen.h>
28 #include "../shared.h"
30 #define INPUT_PATH "/sys/class/input/"
31 #define KEY_CAPABILITIES_PATH "/device/capabilities/key"
32 #define ENABLED_PATH "/device/enabled"
33 #define TOUCHSCREEN_CAPABILITY 400
35 #define TURNON_TOUCHSCREEN 1
36 #define TURNOFF_TOUCHSCREEN 0
38 static char *touchscreen_node;
40 static int touchscreen_probe(void)
45 int val, ret = -ENOTSUP;
47 d = opendir(INPUT_PATH);
51 while ((dir = readdir(d))) {
52 if (dir->d_name[0] == '.')
54 snprintf(buf, sizeof(buf), "%s%s%s", INPUT_PATH,
55 dir->d_name, KEY_CAPABILITIES_PATH);
57 ret = sys_get_int(buf, &val);
58 if (ret < 0 || val != TOUCHSCREEN_CAPABILITY)
61 snprintf(buf, sizeof(buf), "%s%s%s", INPUT_PATH,
62 dir->d_name, ENABLED_PATH);
64 touchscreen_node = strndup(buf, strlen(buf));
65 if (touchscreen_node) {
66 _I("touchscreen node (%s)", touchscreen_node);
69 _E("strndup() failed");
79 static int touchscreen_get_state(enum touchscreen_state *state)
83 if (!touchscreen_node || !(*touchscreen_node))
89 ret = sys_get_int(touchscreen_node, &val);
91 _E("Failed to get touchscreen state (%d)", ret);
96 case TURNOFF_TOUCHSCREEN:
97 *state = TOUCHSCREEN_OFF;
99 case TURNON_TOUCHSCREEN:
100 *state = TOUCHSCREEN_ON;
103 _E("Failed to get touchscreen state (%d)", val);
110 static int touchscreen_set_state(enum touchscreen_state state)
114 if (!touchscreen_node || !(*touchscreen_node))
118 case TOUCHSCREEN_OFF:
119 val = TURNOFF_TOUCHSCREEN;
122 val = TURNON_TOUCHSCREEN;
125 _E("Invalid input (%d)", state);
129 ret = sys_set_int(touchscreen_node, val);
131 _E("Failed to change touchscreen state (%d)", ret);
136 static int touchscreen_open(struct hw_info *info,
137 const char *id, struct hw_common **common)
139 struct touchscreen_device *touchscreen_dev;
141 if (!info || !common)
144 if (touchscreen_probe() < 0)
147 touchscreen_dev = calloc(1, sizeof(struct touchscreen_device));
148 if (!touchscreen_dev)
151 touchscreen_dev->common.info = info;
152 touchscreen_dev->get_state = touchscreen_get_state;
153 touchscreen_dev->set_state = touchscreen_set_state;
155 *common = (struct hw_common *)touchscreen_dev;
159 static int touchscreen_close(struct hw_common *common)
165 free(touchscreen_node);
169 HARDWARE_MODULE_STRUCTURE = {
170 .magic = HARDWARE_INFO_TAG,
171 .hal_version = HARDWARE_INFO_VERSION,
172 .device_version = TOUCHSCREEN_HARDWARE_DEVICE_VERSION,
173 .id = TOUCHSCREEN_HARDWARE_DEVICE_ID,
174 .name = "touchscreen",
175 .open = touchscreen_open,
176 .close = touchscreen_close,