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>
28 #include <sys/syscall.h>
29 #include <sys/types.h>
31 #include <sys/utsname.h>
33 #include <hw/touchscreen.h>
34 #include "../shared.h"
36 #define TURNON_TOUCHSCREEN 1
37 #define TURNOFF_TOUCHSCREEN 0
39 #define MODULES_FILE_NAME "/proc/modules"
40 #define MODULE_KO_NAME "rpi-ft5406.ko"
41 #define MODULE_NAME "rpi_ft5406"
43 #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
44 #define delete_module(name, flags) syscall(__NR_delete_module, name, flags)
46 static int find_module(char *name)
48 FILE *fp = fopen(MODULES_FILE_NAME, "rt");
50 _E("Can not open file(%s).\n", MODULES_FILE_NAME);
57 for (; fgets(mod_str, sizeof(mod_str), fp); ) {
58 sscanf(mod_str, "%s ", mod_name);
59 if (strcmp(mod_name, name) == 0) {
60 _E("[%s]\t%s", mod_name, mod_str);
71 static int get_module_path(char *name, char *path)
75 int ret = uname(&utsn);
79 sprintf(path, "/lib/modules/%s/%s", utsn.release, name);
84 static int ins_module(char *name)
88 if (get_module_path(name, path) != 0)
91 int fd = open(path, O_RDONLY);
93 _E("Can not open module(%s).\n", path);
98 int ret = fstat(fd, &st);
100 _E("Module fstat error(%s).\n", path);
105 size_t image_size = st.st_size;
106 void *image = malloc(image_size);
108 _E("Memory allocation errorr.\n");
113 ssize_t size = read(fd, image, image_size);
114 if (size != image_size) {
115 _E("Module file read error(%d).\n", size);
122 ret = init_module(image, image_size, "");
124 _E("init_module error(%d).\n", ret);
133 static int rm_module(char *name)
135 int ret = delete_module(name, O_NONBLOCK);
137 _E("delete_module error(%d).\n", ret);
145 static int touchscreen_get_state(enum touchscreen_state *state)
152 ret = find_module(MODULE_NAME);
154 _E("Failed to get touchscreen state (%d)", ret);
159 case TURNOFF_TOUCHSCREEN:
160 *state = TOUCHSCREEN_OFF;
162 case TURNON_TOUCHSCREEN:
163 *state = TOUCHSCREEN_ON;
170 static int touchscreen_set_state(enum touchscreen_state state)
175 case TOUCHSCREEN_OFF:
176 ret = rm_module(MODULE_NAME);
179 ret = ins_module(MODULE_KO_NAME);
182 _E("Invalid input (%d)", state);
189 static int touchscreen_open(struct hw_info *info,
190 const char *id, struct hw_common **common)
192 struct touchscreen_device *touchscreen_dev;
194 if (!info || !common)
197 touchscreen_dev = calloc(1, sizeof(struct touchscreen_device));
198 if (!touchscreen_dev)
201 touchscreen_dev->common.info = info;
202 touchscreen_dev->get_state = touchscreen_get_state;
203 touchscreen_dev->set_state = touchscreen_set_state;
205 *common = (struct hw_common *)touchscreen_dev;
209 static int touchscreen_close(struct hw_common *common)
218 HARDWARE_MODULE_STRUCTURE = {
219 .magic = HARDWARE_INFO_TAG,
220 .hal_version = HARDWARE_INFO_VERSION,
221 .device_version = TOUCHSCREEN_HARDWARE_DEVICE_VERSION,
222 .id = TOUCHSCREEN_HARDWARE_DEVICE_ID,
223 .name = "touchscreen",
224 .open = touchscreen_open,
225 .close = touchscreen_close,