1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "lights"
18 #define DEBUG 0
19 
20 #include <cutils/log.h>
21 
22 #include <malloc.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 
33 #include <hardware/lights.h>
34 
35 /******************************************************************************/
36 
37 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
38 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
39 
40 char const *const LCD_FILE           = "/sys/class/leds/lcd-backlight/brightness";
41 char const *const RED_LED_FILE       = "/sys/class/leds/red/brightness";
42 char const *const GREEN_LED_FILE     = "/sys/class/leds/green/brightness";
43 char const *const BLUE_LED_FILE      = "/sys/class/leds/blue/brightness";
44 char const *const RED_TIMEOUT_FILE   = "/sys/class/leds/red/on_off_ms";
45 char const *const GREEN_TIMEOUT_FILE = "/sys/class/leds/green/on_off_ms";
46 char const *const BLUE_TIMEOUT_FILE  = "/sys/class/leds/blue/on_off_ms";
47 char const *const RGB_LOCKED_FILE    = "/sys/class/leds/red/rgb_start";
48 
49 /**
50  * device methods
51  */
52 
init_globals(void)53 void init_globals(void)
54 {
55     // init the mutex
56     pthread_mutex_init(&g_lock, NULL);
57 }
58 
write_int(char const * path,int value)59 static int write_int(char const* path, int value)
60 {
61     int fd;
62     static int already_warned = 0;
63 
64     fd = open(path, O_RDWR);
65     if (fd >= 0) {
66         char buffer[32] = {0,};
67         int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
68         int amt = write(fd, buffer, bytes);
69         close(fd);
70         return amt == -1 ? -errno : 0;
71     } else {
72         if (already_warned == 0) {
73             ALOGE("write_int failed to open %s\n", path);
74             already_warned = 1;
75         }
76         return -errno;
77     }
78 }
79 
write_on_off(char const * path,int on,int off)80 static int write_on_off(char const* path, int on, int off)
81 {
82     int fd;
83     static int already_warned = 0;
84 
85     fd = open(path, O_RDWR);
86     if (fd >= 0) {
87         char buffer[32] = {0,};
88         int bytes = snprintf(buffer, sizeof(buffer), "%d %d\n", on, off);
89         int amt = write(fd, buffer, bytes);
90         close(fd);
91         return amt == -1 ? -errno : 0;
92     } else {
93         if (already_warned == 0) {
94             ALOGE("write_int failed to open %s\n", path);
95             already_warned = 1;
96         }
97         return -errno;
98     }
99 }
100 
rgb_to_brightness(struct light_state_t const * state)101 static int rgb_to_brightness(struct light_state_t const* state)
102 {
103     int color = state->color & 0x00ffffff;
104 
105     return ((77 * ((color >> 16) & 0x00ff))
106             + (150 * ((color >> 8) & 0x00ff)) + (29 * (color & 0x00ff))) >> 8;
107 }
108 
set_light_backlight(struct light_device_t * dev __unused,struct light_state_t const * state)109 static int set_light_backlight(struct light_device_t* dev __unused,
110         struct light_state_t const* state)
111 {
112     int err = 0;
113     int brightness = rgb_to_brightness(state);
114 
115     pthread_mutex_lock(&g_lock);
116     err = write_int(LCD_FILE, brightness);
117     pthread_mutex_unlock(&g_lock);
118 
119     return err;
120 }
121 
set_light_locked(struct light_state_t const * state,int type __unused)122 static int set_light_locked(struct light_state_t const* state, int type __unused)
123 {
124     int len;
125     int red, green, blue;
126     int onMS, offMS;
127     unsigned int colorRGB;
128 
129     switch (state->flashMode) {
130     case LIGHT_FLASH_TIMED:
131     case LIGHT_FLASH_HARDWARE:
132         onMS = state->flashOnMS;
133         offMS = state->flashOffMS;
134         break;
135     case LIGHT_FLASH_NONE:
136     default:
137         onMS = 0;
138         offMS = 0;
139         break;
140     }
141 
142     colorRGB = state->color;
143 
144 #if DEBUG
145     ALOGD("set_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
146             state->flashMode, colorRGB, onMS, offMS);
147 #endif
148 
149     red = (colorRGB >> 16) & 0xFF;
150     green = (colorRGB >> 8) & 0xFF;
151     blue = colorRGB & 0xFF;
152 
153     // due to limitation of driver
154     if (onMS == 0) {
155         red = 0;
156         green = 0;
157         blue = 0;
158     }
159 
160     write_int(RGB_LOCKED_FILE, 0);
161 
162     write_int(RED_LED_FILE, red);
163     write_int(GREEN_LED_FILE, green);
164     write_int(BLUE_LED_FILE, blue);
165 
166     write_on_off(RED_TIMEOUT_FILE, onMS, offMS);
167     write_on_off(GREEN_TIMEOUT_FILE, onMS, offMS);
168     write_on_off(BLUE_TIMEOUT_FILE, onMS, offMS);
169 
170     write_int(RGB_LOCKED_FILE, 1);
171 
172     return 0;
173 }
174 
set_light_notifications(struct light_device_t * dev __unused,struct light_state_t const * state)175 static int set_light_notifications(struct light_device_t* dev __unused,
176         struct light_state_t const* state)
177 {
178     pthread_mutex_lock(&g_lock);
179     set_light_locked(state, 0);
180     pthread_mutex_unlock(&g_lock);
181 
182     return 0;
183 }
184 
set_light_attention(struct light_device_t * dev __unused,struct light_state_t const * state)185 static int set_light_attention(struct light_device_t* dev __unused,
186         struct light_state_t const* state)
187 {
188     pthread_mutex_lock(&g_lock);
189     set_light_locked(state, 1);
190     pthread_mutex_unlock(&g_lock);
191 
192     return 0;
193 }
194 
195 
196 /** Close the lights device */
close_lights(struct light_device_t * dev)197 static int close_lights(struct light_device_t *dev)
198 {
199     if (dev)
200         free(dev);
201 
202     return 0;
203 }
204 
205 /******************************************************************************/
206 
207 /**
208  * module methods
209  */
210 
211 /** Open a new instance of a lights device using name */
open_lights(const struct hw_module_t * module,char const * name,struct hw_device_t ** device)212 static int open_lights(const struct hw_module_t* module, char const* name,
213         struct hw_device_t** device)
214 {
215     int (*set_light)(struct light_device_t* dev,
216             struct light_state_t const* state);
217 
218     if (!strcmp(LIGHT_ID_BACKLIGHT, name))
219         set_light = set_light_backlight;
220     else if (!strcmp(LIGHT_ID_NOTIFICATIONS, name))
221         set_light = set_light_notifications;
222     else if (!strcmp(LIGHT_ID_ATTENTION, name))
223         set_light = set_light_attention;
224     else
225         return -EINVAL;
226 
227     pthread_once(&g_init, init_globals);
228 
229     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
230 
231     if(!dev)
232         return -ENOMEM;
233 
234     memset(dev, 0, sizeof(*dev));
235 
236     dev->common.tag = HARDWARE_DEVICE_TAG;
237     dev->common.version = 0;
238     dev->common.module = (struct hw_module_t*)module;
239     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
240     dev->set_light = set_light;
241 
242     *device = (struct hw_device_t*)dev;
243 
244     return 0;
245 }
246 
247 static struct hw_module_methods_t lights_module_methods = {
248     .open =  open_lights,
249 };
250 
251 /*
252  * The lights Module
253  */
254 struct hw_module_t HAL_MODULE_INFO_SYM = {
255     .tag = HARDWARE_MODULE_TAG,
256     .version_major = 1,
257     .version_minor = 0,
258     .id = LIGHTS_HARDWARE_MODULE_ID,
259     .name = "lights Module",
260     .author = "Google, Inc.",
261     .methods = &lights_module_methods,
262 };
263