summaryrefslogtreecommitdiffstats
path: root/backend/devicelock_n810.c
blob: ea8226407a723ab1ef8cb8a771e250b2a4cce328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 *   Copyright (C) 2010 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "devicelock_n810.h"
#include "util.h"
#include "log.h"
#include "main.h"

#include <unistd.h>
#include <fcntl.h>
#include <signal.h>


#define KB_BASEPATH	"/devices/platform/i2c_omap.2/i2c-2/2-0045"
#define TS_BASEPATH	"/devices/platform/omap2_mcspi.1/spi1.0"
#define INPUT_BASEPATH	"/devices/virtual/input"
#define GPIOSW_BASEPATH	"/devices/platform/gpio-switch"


static void devicelock_n810_toggle(struct devicelock *s)
{
	struct devicelock_n810 *sn = container_of(s, struct devicelock_n810, devicelock);
	int err, lock;

	lock = !(backend.backlight->screen_is_locked(backend.backlight));
	logdebug("devicelock: %s the device\n", lock ? "Locking" : "Unlocking");

	if (lock)
		autodim_suspend(backend.autodim);

	/* Set backlight lock state */
	err = backlight_screen_lock(backend.backlight, lock);
	if (err)
		logerr("Failed to set backlight lock state\n");
	/* Set touchscreen lock state */
	err = file_write_int(sn->ts_disable_file, lock, 10);
	if (err)
		logerr("Failed to write touchscreen disable file\n");
	/* Set keyboard lock state */
	err = file_write_int(sn->kb_disable_file, lock, 10);
	if (err)
		logerr("Failed to write keyboard disable file\n");

	if (!lock)
		autodim_resume(backend.autodim);
}

static void devicelock_n810_event(struct devicelock *s)
{
	struct devicelock_n810 *sn = container_of(s, struct devicelock_n810, devicelock);
	char buf[32], *bufp;
	int count, switch_state;

	count = file_read_string(sn->kb_lock_state_file, buf, sizeof(buf));
	if (count <= 0)
		return;
	bufp = string_strip(buf);
	if (strcmp(bufp, "open") == 0) {
		switch_state = 0;
	} else if (strcmp(bufp, "closed") == 0) {
		switch_state = 1;
	} else {
		logerr("devicelock: Invalid state\n");
		return;
	}
	if (switch_state == sn->switch_state)
		return;
	sn->switch_state = switch_state;
	if (switch_state)
		devicelock_n810_toggle(s);
}

static void devicelock_n810_destroy(struct devicelock *s)
{
	struct devicelock_n810 *sn = container_of(s, struct devicelock_n810, devicelock);

	close(sn->kb_lock_evdev_fd);
	file_close(sn->kb_lock_state_file);
	file_close(sn->kb_disable_file);
	file_close(sn->ts_disable_file);
	free(sn);
}

static struct devicelock * devicelock_n810_probe(void)
{
	struct devicelock_n810 *sn;
	struct fileaccess *ts_disable = NULL;
	struct fileaccess *kb_disable = NULL;
	struct fileaccess *kb_lock_state = NULL;
	struct fileaccess *file;
	int kb_lock_evdev = -1;
	int kb_lock_evdev_fd = -1;
	struct dir_entry *dir_entry;
	LIST_HEAD(dir_entries);
	int err, count;
	unsigned int i;
	char buf[PATH_MAX + 1];

	ts_disable = sysfs_file_open(O_RDWR, TS_BASEPATH "/disable_ts");
	if (!ts_disable)
		goto err_close;
	kb_disable = sysfs_file_open(O_RDWR, KB_BASEPATH "/disable_kp");
	if (!kb_disable)
		goto err_close;
	kb_lock_state = sysfs_file_open(O_RDONLY, GPIOSW_BASEPATH "/kb_lock/state");
	if (!kb_lock_state)
		goto err_close;
	count = list_sysfs_directory(&dir_entries, INPUT_BASEPATH);
	if (count <= 0)
		goto err_close;
	list_for_each_entry(dir_entry, &dir_entries, list) {
		if (sscanf(dir_entry->name, "input%u", &i) != 1)
			continue;
		file = sysfs_file_open(O_RDONLY, "%s/%s/name",
				       INPUT_BASEPATH, dir_entry->name);
		if (!file)
			continue;
		count = file_read_string(file, buf, sizeof(buf));
		file_close(file);
		if (count <= 0)
			continue;
		if (strcmp(string_strip(buf), "kb_lock") == 0) {
			kb_lock_evdev = i;
			break;
		}
	}
	dir_entries_free(&dir_entries);
	if (kb_lock_evdev < 0)
		goto err_close;
	snprintf(buf, sizeof(buf), "/dev/input/event%u", kb_lock_evdev);
	kb_lock_evdev_fd = open(buf, O_RDONLY);
	if (kb_lock_evdev_fd < 0)
		goto err_close;
	err = fcntl(kb_lock_evdev_fd, F_SETFL, O_ASYNC | O_NONBLOCK);
	err |= fcntl(kb_lock_evdev_fd, F_SETSIG, SIGUSR2);
	err |= fcntl(kb_lock_evdev_fd, F_SETOWN, getpid());
	if (err)
		goto err_close;

	sn = zalloc(sizeof(*sn));
	if (!sn)
		goto err_close;
	sn->kb_lock_evdev_fd = kb_lock_evdev_fd;
	sn->kb_lock_state_file = kb_lock_state;
	sn->ts_disable_file = ts_disable;
	sn->kb_disable_file = kb_disable;

	devicelock_init(&sn->devicelock, "n810");
	sn->devicelock.event = devicelock_n810_event;
	sn->devicelock.destroy = devicelock_n810_destroy;

	logdebug("Screenlock toggle on %s\n", buf);

	return &sn->devicelock;

err_close:
	if (kb_lock_evdev_fd)
		close(kb_lock_evdev_fd);
	file_close(kb_lock_state);
	file_close(ts_disable);
	file_close(kb_disable);
	return NULL;
}

DEVICELOCK_PROBE(n810, devicelock_n810_probe);
bues.ch cgit interface