blob: d439edad9f4cfe0cbcc1c567bf384a012f3e7d1c [file] [log] [blame]
Christopher N. Hesse79a9b152016-01-26 12:17:55 +01001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * Copyright (C) 2015 The CyanogenMod Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "SamsungLightsHAL"
19/* #define LOG_NDEBUG 0 */
20
21#include <cutils/log.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <pthread.h>
28
29#include <sys/ioctl.h>
30#include <sys/types.h>
31
32#include <hardware/lights.h>
33
34#define PANEL_FILE "/sys/class/backlight/panel/brightness"
35#define BUTTON_FILE "/sys/class/sec/sec_touchkey/brightness"
36#define LED_BLINK "/sys/class/sec/led/led_blink"
37
38#define COLOR_MASK 0x00ffffff
39
40static pthread_once_t g_init = PTHREAD_ONCE_INIT;
41static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
42
43struct led_config {
44 unsigned int color;
45 int delay_on, delay_off;
46};
47
48static struct led_config g_leds[3]; // For battery, notifications, and attention.
49static int g_cur_led = -1; // Presently showing LED of the above.
50
51void init_g_lock(void)
52{
53 pthread_mutex_init(&g_lock, NULL);
54}
55
56static int write_int(char const *path, int value)
57{
58 int fd;
59 static int already_warned;
60
61 already_warned = 0;
62
63 ALOGV("write_int: path %s, value %d", path, value);
64 fd = open(path, O_RDWR);
65
66 if (fd >= 0) {
67 char buffer[20];
68 int bytes = sprintf(buffer, "%d\n", value);
69 int amt = write(fd, buffer, bytes);
70 close(fd);
71 return amt == -1 ? -errno : 0;
72 } else {
73 if (already_warned == 0) {
74 ALOGE("write_int failed to open %s\n", path);
75 already_warned = 1;
76 }
77 return -errno;
78 }
79}
80
81static int write_str(char const *path, const char* value)
82{
83 int fd;
84 static int already_warned;
85
86 already_warned = 0;
87
88 ALOGV("write_str: path %s, value %s", path, value);
89 fd = open(path, O_RDWR);
90
91 if (fd >= 0) {
92 int amt = write(fd, value, strlen(value));
93 close(fd);
94 return amt == -1 ? -errno : 0;
95 } else {
96 if (already_warned == 0) {
97 ALOGE("write_str failed to open %s\n", path);
98 already_warned = 1;
99 }
100 return -errno;
101 }
102}
103
104static int rgb_to_brightness(struct light_state_t const *state)
105{
106 int color = state->color & COLOR_MASK;
107
108 return ((77*((color>>16) & 0x00ff))
109 + (150*((color>>8) & 0x00ff)) + (29*(color & 0x00ff))) >> 8;
110}
111
112static int set_light_backlight(struct light_device_t *dev __unused,
113 struct light_state_t const *state)
114{
115 int err = 0;
116 int brightness = rgb_to_brightness(state);
117
118 pthread_mutex_lock(&g_lock);
119 err = write_int(PANEL_FILE, brightness);
120
121 pthread_mutex_unlock(&g_lock);
122 return err;
123}
124
125static int set_light_buttons(struct light_device_t* dev __unused,
126 struct light_state_t const* state)
127{
128 int err = 0;
129 int on = (state->color & COLOR_MASK);
130
131 pthread_mutex_lock(&g_lock);
132
133 err = write_int(BUTTON_FILE, on ? 1 : 0);
134
135 pthread_mutex_unlock(&g_lock);
136
137 return err;
138}
139
140static int close_lights(struct light_device_t *dev)
141{
142 ALOGV("close_light is called");
143 if (dev)
144 free(dev);
145
146 return 0;
147}
148
149/* LEDs */
150static int write_leds(const struct led_config *led)
151{
152 static const struct led_config led_off = {0, 0, 0};
153
154 char blink[32];
155 int count, err;
156 int color;
157
158 if (led == NULL)
159 led = &led_off;
160
161 count = snprintf(blink,
162 sizeof(blink) - 1,
163 "0x%08x %d %d",
164 led->color,
165 led->delay_on,
166 led->delay_off);
167 if (count < 0) {
168 return -errno;
169 } else if ((unsigned int)count >= sizeof(blink)-1) {
170 ALOGE("%s: Truncated string: blink=\"%s\".", __func__, blink);
171 return -EINVAL;
172 }
173
174 ALOGV("%s: color=0x%08x, delay_on=%d, delay_off=%d, blink=\"%s\".",
175 __func__, led->color, led->delay_on, led->delay_off, blink);
176
177 /* Add '\n' here to make the above log message clean. */
178 blink[count] = '\n';
179 blink[count+1] = '\0';
180
181 pthread_mutex_lock(&g_lock);
182 err = write_str(LED_BLINK, blink);
183 pthread_mutex_unlock(&g_lock);
184
185 return err;
186}
187
188static int set_light_leds(struct light_state_t const *state, int type)
189{
190 struct led_config *led;
191 int err = 0;
192
193 ALOGV("%s: type=%d, color=0x%010x, fM=%d, fOnMS=%d, fOffMs=%d.", __func__,
194 type, state->color,state->flashMode, state->flashOnMS, state->flashOffMS);
195
196 if (type < 0 || (size_t)type >= sizeof(g_leds)/sizeof(g_leds[0])) {
197 return -EINVAL;
198 }
199
200 /* type is one of:
201 * 0. battery
202 * 1. notifications
203 * 2. attention
204 * which are multiplexed onto the same physical LED in the above order. */
205 led = &g_leds[type];
206
207 switch (state->flashMode) {
208 case LIGHT_FLASH_NONE:
209 /* Set LED to a solid color, spec is unclear on the exact behavior here. */
210 led->delay_on = led->delay_off = 0;
211 break;
212 case LIGHT_FLASH_TIMED:
213 case LIGHT_FLASH_HARDWARE:
214 led->delay_on = state->flashOnMS;
215 led->delay_off = state->flashOffMS;
216 break;
217 default:
218 return -EINVAL;
219 }
220
221 led->color = state->color & COLOR_MASK;
222
223 if (led->color > 0) {
224 /* This LED is lit. */
225 if (type >= g_cur_led) {
226 /* And it has the highest priority, so show it. */
227 err = write_leds(led);
228 g_cur_led = type;
229 }
230 } else {
231 /* This LED is not (any longer) lit. */
232 if (type == g_cur_led) {
233 /* But it is currently showing, switch to a lower-priority LED. */
234 int i;
235
236 for (i = type-1; i >= 0; i--) {
237 if (g_leds[i].color > 0) {
238 /* Found a lower-priority LED to switch to. */
239 err = write_leds(&g_leds[i]);
240 goto switched;
241 }
242 }
243
244 /* No LEDs are lit, turn off. */
245 err = write_leds(NULL);
246switched:
247 g_cur_led = i;
248 }
249 }
250
251 return err;
252}
253
254static int set_light_leds_battery(struct light_device_t *dev __unused,
255 struct light_state_t const *state)
256{
257 return set_light_leds(state, 0);
258}
259
260static int set_light_leds_notifications(struct light_device_t *dev __unused,
261 struct light_state_t const *state)
262{
263 return set_light_leds(state, 1);
264}
265
266static int set_light_leds_attention(struct light_device_t *dev __unused,
267 struct light_state_t const *state)
268{
269 struct light_state_t fixed;
270
271 memcpy(&fixed, state, sizeof(fixed));
272
273 /* The framework does odd things with the attention lights, fix them up to
274 * do something sensible here. */
275 switch (fixed.flashMode) {
276 case LIGHT_FLASH_NONE:
277 /* LightsService.Light::stopFlashing calls with non-zero color. */
278 fixed.color = 0;
279 break;
280 case LIGHT_FLASH_HARDWARE:
281 /* PowerManagerService::setAttentionLight calls with onMS=3, offMS=0, which
282 * just makes for a slightly-dimmer LED. */
283 if (fixed.flashOnMS > 0 && fixed.flashOffMS == 0)
284 fixed.flashMode = LIGHT_FLASH_NONE;
285 fixed.color = 0x000000ff;
286 break;
287 }
288
289 return set_light_leds(&fixed, 2);
290}
291
292static int open_lights(const struct hw_module_t *module, char const *name,
293 struct hw_device_t **device)
294{
295 int (*set_light)(struct light_device_t *dev,
296 struct light_state_t const *state);
297
298 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
299 set_light = set_light_backlight;
300 else if (0 == strcmp(LIGHT_ID_BUTTONS, name))
301 set_light = set_light_buttons;
302 else if (0 == strcmp(LIGHT_ID_BATTERY, name))
303 set_light = set_light_leds_battery;
304 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
305 set_light = set_light_leds_notifications;
306 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
307 set_light = set_light_leds_attention;
308 else
309 return -EINVAL;
310
311 pthread_once(&g_init, init_g_lock);
312
313 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
314 memset(dev, 0, sizeof(*dev));
315
316 dev->common.tag = HARDWARE_DEVICE_TAG;
317 dev->common.version = 0;
318 dev->common.module = (struct hw_module_t *)module;
319 dev->common.close = (int (*)(struct hw_device_t *))close_lights;
320 dev->set_light = set_light;
321
322 *device = (struct hw_device_t *)dev;
323
324 return 0;
325}
326
327static struct hw_module_methods_t lights_module_methods = {
328 .open = open_lights,
329};
330
331struct hw_module_t HAL_MODULE_INFO_SYM = {
332 .tag = HARDWARE_MODULE_TAG,
333 .version_major = 1,
334 .version_minor = 0,
335 .id = LIGHTS_HARDWARE_MODULE_ID,
336 .name = "Samsung Lights Module",
337 .author = "The CyanogenMod Project",
338 .methods = &lights_module_methods,
339};