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