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