4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 * @brief Power manager main loop.
24 * This file includes Main loop, the FSM, signal processing.
35 #include <sys/types.h>
40 #include <vconf-keys.h>
42 #include <libsyscommon/list.h>
43 #include <device/display-internal.h>
45 #include "ambient-mode.h"
48 #include "lock-detector.h"
49 #include "display-ops.h"
50 #include "shared/devices.h"
51 #include "shared/device-notifier.h"
52 #include "core/udev.h"
53 #include "shared/common.h"
54 #include "shared/apps.h"
55 #include "extcon/extcon.h"
56 #include "battery/power-supply.h"
57 #include "power/power-off.h"
58 #include "power/power-suspend.h"
59 #include "power/power-boot.h"
60 #include "power/power-doze.h"
61 #include "display-dpms.h"
62 #include "display-signal.h"
63 #include "display-lock.h"
64 #include "shared/plugin.h"
66 #define DISPLAY_CONF_FILE "/etc/deviced/display.conf"
69 * @addtogroup POWER_MANAGER
73 #define LOCK_SCREEN_INPUT_TIMEOUT 10000
74 #define LOCK_SCREEN_CONTROL_TIMEOUT 5000
75 #define ALWAYS_ON_TIMEOUT 360000000
76 #define LATE_LCD_TRANSIT 1
78 #define GESTURE_STR "gesture"
79 #define POWER_KEY_STR "powerkey"
80 #define TOUCH_STR "touch"
81 #define EVENT_STR "event"
82 #define TIMEOUT_STR "timeout"
83 #define PROXI_STR "proximity"
84 #define PALM_STR "palm"
85 #define UNKNOWN_STR "unknown"
90 extern void init_pm_internal();
91 extern void init_save_userlock(void);
93 static struct display_plugin *disp_plgn;
94 static struct _backlight_ops *backlight_ops;
95 static struct _display_white_balance_ops *display_white_balance_ops;
96 static struct battery_plugin *battery_plgn;
97 static int (*fp_get_charging_status) (int *val);
98 static void (*power_saving_func) (int onoff);
99 static enum device_ops_status status = DEVICE_OPS_STATUS_UNINIT;
101 static guint timeout_src_id;
102 static int system_wakeup_flag = false;
103 static unsigned int custom_normal_timeout = 0;
104 static unsigned int custom_dim_timeout = 0;
105 int custom_holdkey_block = false;
106 static char *custom_change_name;
107 static bool hallic_open = true;
108 static guint lock_timeout_id;
109 static guint transit_timer;
110 static int lock_screen_timeout = LOCK_SCREEN_INPUT_TIMEOUT;
111 static struct timeval lcdon_tv;
112 static int lcd_paneloff_mode = false;
113 static int stay_touchscreen_off = false;
116 * The two variables(lcdon_broadcast, pmstate_suspend) must be set initial
117 * state because it should be sent from previous state at booting time.
119 static bool lcdon_broadcast = true;
121 static bool touch_blocked = false;
123 /* default transition, action fuctions */
124 static int default_trans(int evt);
125 static int default_action(int timeout);
126 static int default_check(int curr, int next);
128 static gboolean del_normal_cond(void *data);
129 static gboolean del_dim_cond(void *data);
130 static gboolean del_off_cond(void *data);
132 static int default_proc_change_state(unsigned int cond, pid_t pid);
133 static int (*proc_change_state)(unsigned int cond, pid_t pid) = default_proc_change_state;
135 static struct state states[S_END] = {
136 { S_START, "S_START", NULL, NULL, NULL, NULL },
137 { S_NORMAL, "S_NORMAL", default_trans, default_action, default_check, del_normal_cond },
138 { S_LCDDIM, "S_LCDDIM", default_trans, default_action, default_check, del_dim_cond },
139 { S_LCDOFF, "S_LCDOFF", default_trans, default_action, default_check, del_off_cond },
140 { S_STANDBY, "S_STANDBY", NULL, NULL, NULL, NULL },
141 { S_SLEEP, "S_SLEEP", default_trans, default_action, default_check, NULL },
142 { S_POWEROFF, "S_POWEROFF", NULL, NULL, NULL, NULL },
145 static int trans_table[S_END][EVENT_END] = {
147 { S_START, S_START }, /* S_START */
148 { S_LCDDIM, S_NORMAL }, /* S_NORMAL */
149 { S_LCDOFF, S_NORMAL }, /* S_LCDDIM */
150 { S_SLEEP, S_NORMAL }, /* S_LCDOFF */
151 { S_SLEEP, S_STANDBY }, /* S_STANDBY */
152 { S_LCDOFF, S_NORMAL }, /* S_SLEEP */
153 { S_POWEROFF, S_POWEROFF }, /* S_POWEROFF */
156 #define SHIFT_UNLOCK 4
157 #define SHIFT_CHANGE_STATE 7
158 #define CHANGE_STATE_BIT 0xF00 /* 1111 0000 0000 */
159 #define SHIFT_LOCK_FLAG 16
160 #define SHIFT_CHANGE_TIMEOUT 20
161 #define CUSTOM_TIMEOUT_BIT 0x1
162 #define CUSTOM_HOLDKEY_BIT 0x2
163 #define HOLD_KEY_BLOCK_BIT 0x1
164 #define TIMEOUT_NONE (-1)
166 #define S_COVER_TIMEOUT 8000
167 #define GET_HOLDKEY_BLOCK_STATE(x) ((x >> SHIFT_LOCK_FLAG) & HOLD_KEY_BLOCK_BIT)
168 #define DELAYED_INIT_WATING_TIME 60000 /* 1 minute */
170 #define LOCK_SCREEN_WATING_TIME 300 /* 0.3 second */
171 #define LONG_PRESS_INTERVAL 2 /* 2 seconds */
172 #define SAMPLING_INTERVAL 1 /* 1 sec */
173 #define BRIGHTNESS_CHANGE_STEP 10
174 #define LCD_ALWAYS_ON 0
175 #define ACCEL_SENSOR_ON 1
176 #define CONTINUOUS_SAMPLING 1
177 #define LCDOFF_TIMEOUT 300 /* milli second */
179 #define DIFF_TIMEVAL_MS(a, b) \
180 (((a.tv_sec * 1000000 + a.tv_usec) - \
181 (b.tv_sec * 1000000 + b.tv_usec)) \
184 static struct display_config display_conf = {
185 .lock_wait_time = LOCK_SCREEN_WATING_TIME,
186 .longpress_interval = LONG_PRESS_INTERVAL,
187 .lightsensor_interval = SAMPLING_INTERVAL,
188 .lcdoff_timeout = LCDOFF_TIMEOUT,
189 .pm_default_brightness = 80,
190 .brightness_change_step = BRIGHTNESS_CHANGE_STEP,
191 .lcd_always_on = LCD_ALWAYS_ON,
193 .framerate_app = {0, 0, 0, 0},
194 .control_display = 0,
195 .powerkey_doublepress = 0,
196 .accel_sensor_on = ACCEL_SENSOR_ON,
197 .continuous_sampling = CONTINUOUS_SAMPLING,
198 .timeout_enable = true,
199 .input_support = true,
200 .lockcheck_timeout = 600,
201 .aod_enter_level = 40,
203 .touch_wakeup = false,
204 .display_on_usb_conn_changed = true,
207 struct display_function_info display_info = {
208 .update_auto_brightness = NULL,
209 .set_autobrightness_min = NULL,
210 .reset_autobrightness_min = NULL,
211 .face_detection = NULL,
214 inline struct display_config* get_var_display_config()
216 return &display_conf;
219 inline struct state* state_st(enum state_t state)
221 return &states[state];
224 guint get_transition_timer(void)
226 return timeout_src_id;
229 void change_state_action(enum state_t state, int (*func)(int timeout))
231 _I("[%s] 'action' is changed.", states[state].name);
232 states[state].action = func;
235 void change_state_trans(enum state_t state, int (*func)(int evt))
237 _I("[%s] 'trans' is changed.", states[state].name);
238 states[state].trans = func;
241 void change_state_check(enum state_t state, int (*func)(int curr, int next))
243 _I("[%s] 'check' is changed.", states[state].name);
244 states[state].check = func;
247 void change_state_name(enum state_t state, const char *name)
249 _I("[%s] 'name' is changed.", states[state].name);
250 states[state].name = name;
253 void change_trans_table(enum state_t state, enum state_t next)
255 _I("[%s] 'timeout trans table' is changed.", states[state].name);
256 trans_table[state][EVENT_TIMEOUT] = next;
259 void change_proc_change_state(int (*func)(unsigned int cond, pid_t pid))
261 _I("'proc change state' is changed.");
262 proc_change_state = func;
265 static int display_brightness_changed(void *data)
269 brt = DATA_VALUE_INT(data);
271 ret = gdbus_signal_emit(NULL,
272 DEVICED_PATH_DISPLAY,
273 DEVICED_INTERFACE_DISPLAY,
275 g_variant_new("(i)", brt));
277 _E("Failed to send dbus signal Brightness.");
282 static int display_auto_brightness_sensing(void *data)
287 g_source_remove(transit_timer);
293 static const char* __device_flags_to_string(enum device_flags flags)
295 if (flags & LCD_ON_BY_GESTURE)
297 else if (flags & (LCD_ON_BY_POWER_KEY | LCD_OFF_BY_POWER_KEY))
298 return POWER_KEY_STR;
299 else if (flags & (LCD_ON_BY_EVENT | LCD_OFF_BY_EVENT))
301 else if (flags & LCD_ON_BY_TOUCH)
303 else if (flags & LCD_OFF_BY_TIMEOUT)
309 static unsigned long get_lcd_on_flags(void)
311 unsigned long flags = NORMAL_MODE;
313 if (lcd_paneloff_mode)
314 flags |= LCD_PANEL_OFF_MODE;
316 if (ambient_get_condition() == true) {
317 flags |= AMBIENT_MODE;
318 flags |= LCD_PHASED_TRANSIT_MODE;
324 bool touch_event_blocked(void)
326 return touch_blocked;
329 static gboolean late_transit_on(void *data)
332 return G_SOURCE_REMOVE;
334 g_source_remove(transit_timer);
337 backlight_ops->transit_state(DPMS_ON);
338 return G_SOURCE_REMOVE;
341 void lcd_on_procedure(int state, enum device_flags flag)
344 const struct device_ops *ops = NULL;
345 unsigned long flags = get_lcd_on_flags();
349 * Display on procedure
351 * step 2. broadcast lcd on signal with cause
352 * step 3. set brightness
353 * step 4. set pmstate of vconf
354 * step 5. display on operate
356 * - b. TSP(touch screen) and touchkey enable
357 * step 6. broadcast lcd on complete signal
358 * step 7. key backlight enable
362 _I("[lcdstep] 0x%lx", flags);
364 if (flags & AMBIENT_MODE) {
365 if (ambient_get_state() == false && backlight_ops->get_lcd_power() == DPMS_ON)
367 ambient_set_state(false);
370 /* send LCDOn dbus signal */
371 if (!lcdon_broadcast)
372 broadcast_lcd_on(SIGNAL_PRE, flags);
374 if (!(flags & LCD_PHASED_TRANSIT_MODE)) {
375 /* Update brightness level */
376 if (state == LCD_DIM)
377 backlight_ops->dim();
378 else if (state == LCD_NORMAL)
379 backlight_ops->update();
382 if (state == LCD_NORMAL)
383 set_setting_pmstate(S_NORMAL);
384 else if (state == LCD_DIM)
385 set_setting_pmstate(S_LCDDIM);
387 SYS_G_LIST_FOREACH(lcdon_ops, l, ops)
390 if (!lcdon_broadcast) {
391 broadcast_lcd_on(SIGNAL_POST, flags);
392 if (flags & LCD_PHASED_TRANSIT_MODE)
393 transit_timer = g_timeout_add_seconds(LATE_LCD_TRANSIT,
394 late_transit_on, NULL);
395 lcdon_broadcast = true;
398 if (CHECK_OPS(keyfilter_ops, backlight_enable))
399 keyfilter_ops->backlight_enable(true);
401 touch_blocked = false;
404 static unsigned long get_lcd_off_flags(void)
406 unsigned long flags = NORMAL_MODE;
408 if (ambient_get_condition() == true) {
409 flags |= AMBIENT_MODE;
410 flags |= LCD_PHASED_TRANSIT_MODE;
413 if (stay_touchscreen_off)
414 flags |= TOUCH_SCREEN_OFF_MODE;
419 inline void lcd_off_procedure(enum device_flags flag)
422 const struct device_ops *ops = NULL;
423 unsigned long flags = get_lcd_off_flags();
427 * Display off procedure
428 * step 0. enhance mode off using nofity (e.g mdnie, HBM, LBM)
429 * step 1. broadcast lcd off signal with cause
430 * step 2. set pmstate of vconf
431 * step 3. display off operate
433 * - b. TSP(touch screen) and touchkey disable
434 * step 4. broadcast lcd off complete siganl
435 * step 5. enter doze mode if it is enabled
437 _I("[lcdstep] 0x%lx", flags);
441 device_notify(DEVICE_NOTIFIER_LCD_OFF, NULL);
443 if (lcdon_broadcast) {
444 broadcast_lcd_off(SIGNAL_PRE, flags);
445 lcdon_broadcast = false;
450 touch_blocked = true;
452 set_setting_pmstate(S_LCDOFF);
454 if (CHECK_OPS(keyfilter_ops, backlight_enable))
455 keyfilter_ops->backlight_enable(false);
458 g_source_remove(transit_timer);
462 if (flags & LCD_PHASED_TRANSIT_MODE)
463 backlight_ops->transit_state(DPMS_OFF);
465 SYS_G_LIST_FOREACH(lcdon_ops, l, ops)
468 if (flags & AMBIENT_MODE) {
469 broadcast_lcd_off_late(flags);
471 broadcast_lcd_off(SIGNAL_POST, flags);
472 device_notify(DEVICE_NOTIFIER_LCD_OFF_COMPLETE, NULL);
478 void set_stay_touchscreen_off(int val)
480 _I("Stay touch screen off: %d", val);
481 stay_touchscreen_off = val;
483 if (disp_plgn->pm_change_internal)
484 disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL);
487 void set_lcd_paneloff_mode(int val)
489 _I("Lcd paneloff mode: %d", val);
490 lcd_paneloff_mode = val;
492 if (disp_plgn->pm_change_internal)
493 disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL);
496 int low_battery_state(int val)
499 case VCONFKEY_SYSMAN_BAT_POWER_OFF:
500 case VCONFKEY_SYSMAN_BAT_CRITICAL_LOW:
501 case VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF:
507 int get_hallic_open(void)
512 void get_pname(pid_t pid, char *pname)
517 if (pid >= INTERNAL_LOCK_BASE)
518 snprintf(buf, PATH_MAX, "/proc/%d/cmdline", getpid());
520 snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid);
522 cmdline = open(buf, O_RDONLY);
525 _E("Process(%d) does not exist now(may be dead without unlock).", pid);
529 r = read(cmdline, pname, PATH_MAX);
530 if ((r >= 0) && (r < PATH_MAX))
538 static void del_state_cond(void *data, enum state_t state)
540 PmLockNode *tmp = NULL;
546 /* A passed data is a pid_t type data, not a 64bit data. */
547 pid = (pid_t)((intptr_t)data);
548 _I("delete prohibit %s condition by timeout (%d)", states[state].name, pid);
550 if (pid == INTERNAL_LOCK_AMBIENT)
551 ambient_check_invalid_state(pid);
553 tmp = find_node(state, pid);
554 del_node(state, tmp);
555 set_unlock_time(pid, state);
557 /* Change state only when the two conditions below are satisfied.
558 * 1. There should be no running state-transition timer
559 * 2. Released lock is one of the pm_cur_state's lock
560 * This emulates already expired transition timer */
561 if (!timeout_src_id && get_pm_cur_state() == state)
562 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
564 if (state == S_LCDOFF)
565 set_process_active(false, pid);
568 static gboolean del_normal_cond(void *data)
570 del_state_cond(data, S_NORMAL);
571 return G_SOURCE_REMOVE;
574 static gboolean del_dim_cond(void *data)
576 del_state_cond(data, S_LCDDIM);
577 return G_SOURCE_REMOVE;
580 static gboolean del_off_cond(void *data)
582 del_state_cond(data, S_LCDOFF);
583 return G_SOURCE_REMOVE;
586 /* timeout handler */
587 gboolean timeout_handler(void *data)
589 _I("Time out state %s", states[get_pm_cur_state()].name);
591 if (timeout_src_id) {
592 g_source_remove(timeout_src_id);
596 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
597 return G_SOURCE_REMOVE;
600 void reset_timeout(int timeout)
602 if (!display_conf.timeout_enable)
605 if ((get_pm_cur_state() == S_LCDOFF)
606 && (is_emulator() == true || timeout_sleep_support == false))
609 _I("Reset timeout(%d ms).", timeout);
610 if (timeout_src_id != 0) {
611 g_source_remove(timeout_src_id);
615 if (trans_table[get_pm_cur_state()][EVENT_TIMEOUT] == get_pm_cur_state())
619 timeout_src_id = g_timeout_add(timeout,
620 timeout_handler, NULL);
621 else if (timeout == 0)
622 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
625 /* get configurations from setting */
626 static int get_lcd_timeout_from_settings(void)
631 for (i = 0; i < S_END; i++) {
632 switch (states[i].state) {
634 get_run_timeout(&val);
637 get_dim_timeout(&val);
640 val = display_conf.lcdoff_timeout;
643 /* This state doesn't need to set time out. */
648 states[i].timeout = val;
650 _I("State(%s) timeout(%d) ms", states[i].name,
657 static void update_display_time(void)
659 int run_timeout, val;
661 /* first priority : s cover */
663 states[S_NORMAL].timeout = S_COVER_TIMEOUT;
664 _I("S cover closed: Timeout(%d ms) is set by normal.",
669 /* second priority : custom timeout */
670 if (custom_normal_timeout > 0) {
671 states[S_NORMAL].timeout = custom_normal_timeout;
672 states[S_LCDDIM].timeout = custom_dim_timeout;
673 _I("CUSTOM : timeout is set by normal(%d ms), dim(%d ms)",
674 custom_normal_timeout, custom_dim_timeout);
678 /* third priority : lock state */
679 if ((__get_lock_screen_state() == VCONFKEY_IDLE_LOCK) &&
680 !get_lock_screen_bg_state()) {
681 /* timeout is different according to key or event. */
682 states[S_NORMAL].timeout = lock_screen_timeout;
683 _I("LOCK: Timeout(%d ms) is set by normal.",
684 lock_screen_timeout);
688 /* default setting */
689 get_run_timeout(&run_timeout);
692 * if the run_timeout is zero, it regards AlwaysOn state
694 if (run_timeout == 0 || display_conf.lcd_always_on) {
695 run_timeout = ALWAYS_ON_TIMEOUT;
696 _I("LCD always on.");
699 states[S_NORMAL].timeout = run_timeout;
701 get_dim_timeout(&val);
702 states[S_LCDDIM].timeout = val;
704 _I("Normal: NORMAL timeout is set by %d ms", states[S_NORMAL].timeout);
705 _I("Normal: DIM timeout is set by %d ms", states[S_LCDDIM].timeout);
708 static void update_display_locktime(int time)
710 lock_screen_timeout = time;
711 update_display_time();
714 void set_dim_state(bool on)
716 _I("Dim state is %d.", on);
717 update_display_time();
718 states[get_pm_cur_state()].trans(EVENT_INPUT);
721 void lcd_on_direct(enum device_flags flags)
723 if (pm_get_power_lock_support()
724 && (get_pm_cur_state() == S_SLEEP)) {
725 broadcast_pm_wakeup();
726 power_acquire_wakelock();
727 set_pm_cur_state(S_NORMAL);
730 _D("lcd is on directly");
731 gettimeofday(&lcdon_tv, NULL);
732 lcd_on_procedure(LCD_NORMAL, flags);
734 update_display_locktime(LOCK_SCREEN_INPUT_TIMEOUT);
737 static inline bool check_lcd_is_on(void)
739 if (backlight_ops->get_lcd_power() != DPMS_ON)
745 static gboolean timer_refresh_cb(gpointer data)
749 /* state transition */
750 set_pm_old_state(get_pm_cur_state());
751 set_pm_cur_state(S_NORMAL);
752 st = &states[get_pm_cur_state()];
755 st->action(st->timeout);
760 int custom_lcdon(int timeout)
767 if (check_lcd_is_on() == false)
768 lcd_on_direct(LCD_ON_BY_GESTURE);
770 _I("Custom lcd on timeout(%d ms).", timeout);
771 if (set_custom_lcdon_timeout(timeout) == true)
772 update_display_time();
774 /* state transition */
775 set_pm_old_state(get_pm_cur_state());
776 set_pm_cur_state(S_NORMAL);
777 st = &states[get_pm_cur_state()];
781 st->action(st->timeout);
783 g_idle_add(timer_refresh_cb, NULL);
788 int custom_lcdoff(enum device_flags flag)
792 check_processes(S_NORMAL);
793 check_processes(S_LCDDIM);
795 /* check holdkey block flag in lock node */
796 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
798 * When another proccess is normal lock, device is received call then,
799 * call app can be changed to lcd state by proximity.
800 * If proximity is near then normal lock will be unlocked.
802 if (flag & LCD_OFF_BY_PROXIMITY) {
803 _I("custom lcd off by proximity, delete normal lock");
804 delete_condition(S_NORMAL);
806 _I("skip custom lcd off");
811 _I("custom lcd off by flag(%d)", flag);
812 if (backlight_ops->get_lcd_power() == DPMS_ON)
813 lcd_off_procedure(flag);
815 if (set_custom_lcdon_timeout(0) == true)
816 update_display_time();
818 /* state transition */
819 set_pm_old_state(get_pm_cur_state());
820 set_pm_cur_state(S_LCDOFF);
821 st = &states[get_pm_cur_state()];
825 st->action(st->timeout);
830 int display_on_by_reason(const char *reason, int timeout)
836 str_len = strlen(reason);
838 if (!strncmp(reason, GESTURE_STR, str_len))
839 flag = LCD_ON_BY_GESTURE;
840 else if (!strncmp(reason, EVENT_STR, str_len))
841 flag = LCD_ON_BY_EVENT;
843 _E("Reason is unknown(%s)", reason);
848 _E("Cannot setting timeout %d", timeout);
852 if (check_lcd_is_on() == false)
855 _I("platform lcd on by %s (%d ms)", reason, timeout);
856 if (set_custom_lcdon_timeout(timeout) == true)
857 update_display_time();
859 /* state transition */
860 set_pm_old_state(get_pm_cur_state());
861 set_pm_cur_state(S_NORMAL);
862 st = &states[get_pm_cur_state()];
866 st->action(st->timeout);
871 int display_off_by_reason(const char *reason)
877 str_len = strlen(reason);
879 if (!strncmp(reason, GESTURE_STR, str_len)) {
880 check_processes(S_NORMAL);
881 check_processes(S_LCDDIM);
883 /* check holdkey block flag in lock node */
884 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
885 _I("skip platform lcd off by gesture");
888 flag = LCD_OFF_BY_GESTURE;
889 } else if (!strncmp(reason, PALM_STR, str_len)) {
890 delete_condition(S_NORMAL);
891 delete_condition(S_LCDDIM);
893 flag = LCD_OFF_BY_PALM;
895 _E("Reason is unknown(%s)", reason);
899 _I("platform lcd off by %s", reason);
900 if (backlight_ops->get_lcd_power() == DPMS_ON)
901 lcd_off_procedure(flag);
903 /* state transition */
904 set_pm_old_state(get_pm_cur_state());
905 set_pm_cur_state(S_LCDOFF);
906 st = &states[get_pm_cur_state()];
910 st->action(st->timeout);
915 static void default_proc_change_state_action(enum state_t next, int timeout)
919 set_pm_old_state(get_pm_cur_state());
920 set_pm_cur_state(next);
922 st = &states[get_pm_cur_state()];
924 if (st && st->action) {
926 st->action(st->timeout);
932 static int default_proc_change_state(unsigned int cond, pid_t pid)
936 next = GET_COND_STATE(cond);
937 _I("Change process(%d) state to %s.", pid, states[next].name);
941 if (check_lcd_is_on() == false)
942 lcd_on_direct(LCD_ON_BY_EVENT);
943 update_display_locktime(LOCK_SCREEN_CONTROL_TIMEOUT);
944 default_proc_change_state_action(next, -1);
947 default_proc_change_state_action(next, -1);
950 if (backlight_ops->get_lcd_power() == DPMS_ON)
951 lcd_off_procedure(LCD_OFF_BY_EVENT);
952 if (set_custom_lcdon_timeout(0))
953 update_display_time();
954 default_proc_change_state_action(next, -1);
957 _I("Dangerous requests.");
958 /* at first LCD_OFF and then goto sleep */
959 /* state transition */
960 default_proc_change_state_action(S_LCDOFF, TIMEOUT_NONE);
961 delete_condition(S_NORMAL);
962 delete_condition(S_LCDDIM);
963 delete_condition(S_LCDOFF);
964 if (lcdon_broadcast) {
965 _I("broadcast lcd off signal at non-lcd device");
966 broadcast_lcd_off(SIGNAL_PRE, 0);
967 broadcast_lcd_off(SIGNAL_POST, 0);
969 default_proc_change_state_action(S_SLEEP, TIMEOUT_NONE);
979 static void proc_condition_lock(PMMsg *data)
982 guint cond_timeout_id = 0;
983 char pname[PATH_MAX];
984 pid_t pid = data->pid;
986 int holdkey_block, ret;
989 const char *lock_type = NULL;
991 state = GET_COND_STATE(data->cond);
992 if (state == S_START)
995 flags = GET_COND_FLAG(data->cond);
996 get_pname(pid, pname);
998 if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) &&
999 (pm_get_power_lock() == POWER_UNLOCK))
1000 proc_change_state(data->cond, INTERNAL_LOCK_PM);
1002 if (data->timeout > 0) {
1003 /* To pass a pid_t data through the timer infrastructure
1004 * without memory allocation, a pid_t data becomes typecast
1005 * to intptr_t and void *(64bit) type. */
1006 cond_timeout_id = g_timeout_add(
1008 states[state].timeout_cb,
1009 (void*)((intptr_t)pid));
1010 if (!cond_timeout_id)
1011 _E("Failed to register display timer.");
1014 holdkey_block = GET_COND_FLAG(data->cond) & PM_FLAG_BLOCK_HOLDKEY;
1016 tmp = find_node(state, pid);
1018 tmp = add_node(state, pid, cond_timeout_id, holdkey_block);
1020 _E("Failed to acquire lock, state: %d, pid: %d.", state, pid);
1024 update_lock_timer(data, tmp, cond_timeout_id);
1025 tmp->holdkey_block = holdkey_block;
1028 if (state == S_LCDOFF)
1029 set_process_active(true, pid);
1031 _I("[%s] locked by %5d with %u ms", states[state].name, pid, data->timeout);
1033 if (pid < INTERNAL_LOCK_BASE) {
1035 if (state == S_LCDOFF)
1036 lock_type = PM_LCDOFF_STR;
1037 else if (state == S_LCDDIM)
1038 lock_type = PM_LCDDIM_STR;
1039 else if (state == S_NORMAL)
1040 lock_type = PM_LCDON_STR;
1043 /* power lock signal */
1044 ret = gdbus_signal_emit(NULL,
1045 DEVICED_PATH_DISPLAY,
1046 DEVICED_INTERFACE_DISPLAY,
1048 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
1050 _E("Failed to send dbus signal PowerLock.");
1054 _SD("be requested LOCK info pname(%s), holdkeyblock(%d) flags(%d)",
1055 pname, holdkey_block, flags);
1056 set_lock_time(pid, pname, state);
1058 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
1061 static void proc_condition_unlock(PMMsg *data)
1063 pid_t pid = data->pid;
1066 char pname[PATH_MAX];
1070 const char *lock_type = NULL;
1072 state = GET_COND_STATE(data->cond);
1076 flags = GET_COND_FLAG(data->cond);
1077 get_pname(pid, pname);
1079 tmp = find_node(state, pid);
1080 del_node(state, tmp);
1082 if (state == S_LCDOFF)
1083 set_process_active(false, pid);
1085 _I("[%s] unlocked by %5d", states[state].name, pid);
1087 if (pid < INTERNAL_LOCK_BASE) {
1089 if (state == S_LCDOFF)
1090 lock_type = PM_LCDOFF_STR;
1091 else if (state == S_LCDDIM)
1092 lock_type = PM_LCDDIM_STR;
1093 else if (state == S_NORMAL)
1094 lock_type = PM_LCDON_STR;
1097 /* power unlock signal */
1098 ret = gdbus_signal_emit(NULL,
1099 DEVICED_PATH_DISPLAY,
1100 DEVICED_INTERFACE_DISPLAY,
1102 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
1104 _E("Failed to send dbus signal PowerUnlock.");
1108 _SD("be requested UNLOCK info pname(%s) flag(%d)", pname, flags);
1109 set_unlock_time(pid, state);
1111 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
1114 static int proc_condition(PMMsg *data)
1118 if (IS_COND_REQUEST_LOCK(data->cond))
1119 proc_condition_lock(data);
1121 if (IS_COND_REQUEST_UNLOCK(data->cond))
1122 proc_condition_unlock(data);
1124 if (!display_conf.timeout_enable)
1127 flags = GET_COND_FLAG(data->cond);
1129 /* guard time for suspend */
1130 if (get_pm_cur_state() == S_LCDOFF)
1131 reset_timeout(states[S_LCDOFF].timeout);
1133 if (flags & PM_FLAG_RESET_TIMER)
1134 reset_timeout(states[get_pm_cur_state()].timeout);
1137 if (!timeout_src_id)
1138 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
1143 void update_lcdoff_source(int source)
1148 case VCONFKEY_PM_LCDOFF_BY_TIMEOUT:
1149 _I("LCD OFF by timeout.");
1151 case VCONFKEY_PM_LCDOFF_BY_POWERKEY:
1152 _I("LCD OFF by powerkey.");
1155 _E("Invalid value(%d).", source);
1158 ret = vconf_set_int(VCONFKEY_PM_LCDOFF_SOURCE, source);
1160 _E("Failed to set vconf value for lcd off source: %d", vconf_get_ext_errno());
1163 void print_info(int fd)
1166 char buf[PATH_MAX + 255];
1169 char pname[PATH_MAX];
1177 snprintf(buf, sizeof(buf),
1178 "\n==========================================="
1179 "===========================\n");
1180 ret = write(fd, buf, strlen(buf));
1182 _E("Write() failed: %d", errno);
1183 snprintf(buf, sizeof(buf), "Timeout Info: Run[%dms] Dim[%dms] Off[%dms]\n",
1184 states[S_NORMAL].timeout,
1185 states[S_LCDDIM].timeout, states[S_LCDOFF].timeout);
1186 ret = write(fd, buf, strlen(buf));
1188 _E("Write() failed: %d", errno);
1190 snprintf(buf, sizeof(buf), "Tran. Locked : %s %s %s\n",
1191 (get_trans_condition() & MASK_NORMAL) ? states[S_NORMAL].name : "-",
1192 (get_trans_condition() & MASK_DIM) ? states[S_LCDDIM].name : "-",
1193 (get_trans_condition() & MASK_OFF) ? states[S_LCDOFF].name : "-");
1194 ret = write(fd, buf, strlen(buf));
1196 _E("Write() failed: %d", errno);
1198 snprintf(buf, sizeof(buf), "Current State: %s\n",
1199 states[get_pm_cur_state()].name);
1200 ret = write(fd, buf, strlen(buf));
1202 _E("Write() failed: %d", errno);
1204 snprintf(buf, sizeof(buf), "Current Lock Conditions: \n");
1205 ret = write(fd, buf, strlen(buf));
1207 _E("Write() failed: %d", errno);
1209 for (s_index = S_NORMAL; s_index < S_END; s_index++) {
1210 SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) {
1211 get_pname((pid_t)t->pid, pname);
1212 ctime_r(&t->time, time_buf);
1213 time_buf[strlen(time_buf) - 1] = 0;
1214 snprintf(buf, sizeof(buf),
1215 " %d: [%s] locked by pid %d %s %s\n",
1216 i++, states[s_index].name, t->pid, pname, time_buf);
1217 ret = write(fd, buf, strlen(buf));
1219 _E("Write() failed: %d", errno);
1223 print_lock_info_list(fd);
1225 #ifdef ENABLE_PM_LOG
1226 pm_history_print(fd, 250);
1230 void save_display_log(const char *path)
1237 _D("internal state is saved!");
1240 ctime_r(&now_time, time_buf);
1241 time_buf[strlen(time_buf) - 1] = 0;
1243 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
1245 snprintf(buf, sizeof(buf),
1246 "\npm_state_log now-time : %d(s) %s\n\n",
1247 (int)now_time, time_buf);
1248 ret = write(fd, buf, strlen(buf));
1250 _E("write() failed (%d)", errno);
1252 snprintf(buf, sizeof(buf), "pm_status_flag: %#x\n", get_pm_status_flag());
1253 ret = write(fd, buf, strlen(buf));
1255 _E("write() failed (%d)", errno);
1257 if (disp_plgn->get_lock_screen_state ) {
1258 snprintf(buf, sizeof(buf), "screen lock status : %d\n",
1259 disp_plgn->get_lock_screen_state());
1260 ret = write(fd, buf, strlen(buf));
1262 _E("write() failed (%d)", errno);
1268 fd = open("/dev/console", O_WRONLY);
1275 /* SIGHUP signal handler
1276 * For debug... print info to syslog
1278 static void sig_hup(int signo)
1280 _I("received sig hub %d", signo);
1285 int check_lcdoff_direct(void)
1287 int ret, lock, cradle;
1290 if (get_pm_old_state() != S_NORMAL)
1293 if (get_pm_cur_state() != S_LCDDIM)
1296 if (!display_conf.dimming)
1299 lock = __get_lock_screen_state();
1300 if (lock != VCONFKEY_IDLE_LOCK && hallic_open)
1303 hdmi_state = extcon_get_status(EXTCON_CABLE_HDMI);
1307 ret = vconf_get_int(VCONFKEY_SYSMAN_CRADLE_STATUS, &cradle);
1308 if (ret >= 0 && cradle == DOCK_SOUND)
1311 _E("Failed to get vconf value for cradle status: %d", vconf_get_ext_errno());
1313 _D("Goto LCDOFF direct: lock(%d) hdmi(%d) cradle(%d).", lock, hdmi_state, cradle);
1319 * default transition function
1322 * 3. call enter action function
1324 static int default_trans(int evt)
1326 struct state *st = &states[get_pm_cur_state()];
1329 next_state = (enum state_t)trans_table[get_pm_cur_state()][evt];
1331 /* check conditions */
1332 if (st->check && !st->check(get_pm_cur_state(), next_state)) {
1333 /* There is a condition. */
1334 _I("%s locked. Trans to %s failed.", states[get_pm_cur_state()].name,
1335 states[next_state].name);
1339 /* state transition */
1340 set_pm_old_state(get_pm_cur_state());
1341 set_pm_cur_state(next_state);
1342 st = &states[get_pm_cur_state()];
1346 if (get_pm_cur_state() == S_LCDOFF)
1347 update_lcdoff_source(VCONFKEY_PM_LCDOFF_BY_TIMEOUT);
1349 if ((get_pm_cur_state() == S_NORMAL) || (get_pm_cur_state() == S_LCDOFF))
1350 if (set_custom_lcdon_timeout(0) == true)
1351 update_display_time();
1353 if (check_lcdoff_direct() == true) {
1354 /* enter next state directly */
1355 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
1357 if ((get_pm_cur_state() == S_SLEEP)
1358 && (is_emulator() == true || timeout_sleep_support == false))
1361 st->action(st->timeout);
1368 static gboolean lcd_on_expired(void *data)
1370 int lock_state, ret;
1372 if (lock_timeout_id)
1373 lock_timeout_id = 0;
1375 /* check state of lock */
1376 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1377 if (ret > 0 && lock_state == VCONFKEY_IDLE_LOCK)
1378 return G_SOURCE_REMOVE;
1380 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
1382 /* lock screen is not launched yet, but lcd is on */
1383 if (check_lcd_is_on() == false)
1384 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
1386 return G_SOURCE_REMOVE;
1389 static inline void stop_lock_timer(void)
1391 if (lock_timeout_id) {
1392 g_source_remove(lock_timeout_id);
1393 lock_timeout_id = 0;
1397 static void check_lock_screen(void)
1399 int lock_state, ret;
1403 /* check state of lock */
1404 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1405 if (ret < 0 || lock_state == VCONFKEY_IDLE_LOCK)
1408 /* Use time to check lock is done. */
1409 lock_timeout_id = g_timeout_add(display_conf.lock_wait_time,
1410 lcd_on_expired, NULL);
1413 /* default enter action function */
1414 static int default_action(int timeout)
1416 int wakeup_count = -1, pm_cur_state;
1419 static time_t last_update_time = 0;
1420 static int last_timeout = 0;
1421 struct timeval now_tv;
1423 if (status != DEVICE_OPS_STATUS_START) {
1424 _E("Display is not started.");
1428 if (get_pm_cur_state() != S_SLEEP) {
1429 if ((get_pm_cur_state() == S_NORMAL) &&
1430 lcdon_tv.tv_sec != 0) {
1431 gettimeofday(&now_tv, NULL);
1432 timeout -= DIFF_TIMEVAL_MS(now_tv, lcdon_tv);
1433 lcdon_tv.tv_sec = 0;
1435 /* set timer with current state timeout */
1436 reset_timeout(timeout);
1438 if (get_pm_cur_state() == S_NORMAL) {
1439 time(&last_update_time);
1440 last_timeout = timeout;
1442 _I("Timout set: %s state %d ms",
1443 states[get_pm_cur_state()].name, timeout);
1447 if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) {
1448 if (pm_get_power_lock_support()) {
1449 broadcast_pm_wakeup();
1450 power_acquire_wakelock();
1452 set_setting_pmstate(get_pm_cur_state());
1453 pm_cur_state = get_pm_cur_state();
1454 device_notify(DEVICE_NOTIFIER_LCD, (void *)&pm_cur_state);
1457 if ((get_pm_old_state() == S_NORMAL) && (get_pm_cur_state() != S_NORMAL)) {
1459 diff = difftime(now, last_update_time);
1460 _I("S_NORMAL is changed to %s (timeout=%d ms diff=%.0f s).",
1461 states[get_pm_cur_state()].name, last_timeout, diff);
1464 switch (get_pm_cur_state()) {
1467 * normal state : backlight on and restore
1468 * the previous brightness
1470 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
1471 check_lock_screen();
1472 else if (get_pm_old_state() == S_LCDDIM)
1473 backlight_ops->update();
1475 if (check_lcd_is_on() == false)
1476 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
1480 if ((get_pm_old_state() == S_NORMAL) &&
1481 backlight_ops->get_custom_status())
1482 backlight_ops->save_custom_brightness();
1483 /* lcd dim state : dim the brightness */
1484 backlight_ops->dim();
1486 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
1487 lcd_on_procedure(LCD_DIM, NORMAL_MODE);
1491 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF)) {
1493 /* lcd off state : turn off the backlight */
1494 if (backlight_ops->get_lcd_power() == DPMS_ON)
1495 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1498 if (backlight_ops->get_lcd_power() == DPMS_ON
1499 || lcd_paneloff_mode)
1500 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1504 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF))
1507 if (backlight_ops->get_lcd_power() == DPMS_ON)
1508 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1510 if (!pm_get_power_lock_support()) {
1511 /* sleep state : set system mode to SUSPEND */
1512 if (get_wakeup_count(&wakeup_count) < 0)
1513 _E("Wakeup count read error.");
1515 if (wakeup_count < 0) {
1516 _I("Wakup Event. Can not enter suspend mode.");
1520 if (set_wakeup_count(wakeup_count) < 0) {
1521 _E("Wakeup count write error.");
1531 #ifdef ENABLE_PM_LOG
1532 pm_history_save(PM_LOG_SLEEP, get_pm_cur_state());
1534 broadcast_pm_suspend();
1535 if (pm_get_power_lock_support()) {
1536 power_enable_autosleep();
1538 if (power_release_wakelock() < 0)
1539 _E("Power unlock state error.");
1542 _I("system wakeup!!");
1543 system_wakeup_flag = true;
1545 if (check_wakeup_src() == EVENT_DEVICE)
1546 /* system waked up by devices */
1547 states[get_pm_cur_state()].trans(EVENT_DEVICE);
1549 /* system waked up by user input */
1550 states[get_pm_cur_state()].trans(EVENT_INPUT);
1555 if (!pm_get_power_lock_support()) {
1557 states[get_pm_cur_state()].trans(EVENT_DEVICE);
1563 * default check function
1565 * 0 : can't transit, others : transitable
1567 static int default_check(int curr, int next)
1571 makeup_trans_condition();
1573 trans_cond = get_trans_condition() & MASK_BIT;
1575 if (next == S_NORMAL) /* S_NORMAL is exceptional */
1580 trans_cond = trans_cond & MASK_NORMAL;
1583 trans_cond = trans_cond & MASK_DIM;
1586 trans_cond = trans_cond & MASK_OFF;
1593 if (trans_cond != 0) {
1598 return 1; /* transitable */
1601 static void default_saving_mode(int onoff)
1604 set_pm_status_flag(PWRSV_FLAG);
1606 clear_pm_status_flag(PWRSV_FLAG);
1608 if (get_pm_cur_state() == S_NORMAL)
1609 backlight_ops->update();
1612 int poll_callback(int condition, PMMsg *data)
1614 static time_t last_t;
1617 if (status != DEVICE_OPS_STATUS_START) {
1618 _E("Display logic is not started.");
1622 if (condition == INPUT_POLL_EVENT) {
1623 if ((get_pm_cur_state() == S_LCDOFF) || (get_pm_cur_state() == S_SLEEP))
1624 _I("Input event signal at Display Off");
1626 if ((last_t != now) ||
1627 (get_pm_cur_state() == S_LCDOFF) ||
1628 (get_pm_cur_state() == S_SLEEP)) {
1629 states[get_pm_cur_state()].trans(EVENT_INPUT);
1634 if (condition == PM_CONTROL_EVENT) {
1635 proc_condition(data);
1637 if (IS_COND_REQUEST_CHANGE(data->cond))
1638 proc_change_state(data->cond, data->pid);
1644 static int update_setting(int key_idx, int val)
1649 case SETTING_TO_NORMAL:
1650 update_display_time();
1651 states[get_pm_cur_state()].trans(EVENT_INPUT);
1653 case SETTING_HALLIC_OPEN:
1655 update_display_time();
1656 if ((get_pm_cur_state() == S_NORMAL) || (get_pm_cur_state() == S_LCDDIM))
1657 states[get_pm_cur_state()].trans(EVENT_INPUT);
1658 else if ((get_pm_cur_state() == S_SLEEP) && hallic_open)
1659 proc_change_state(S_LCDOFF, INTERNAL_LOCK_HALLIC);
1661 case SETTING_LOW_BATT:
1662 if (low_battery_state(val)) {
1663 if (!(get_pm_status_flag() & CHRGR_FLAG))
1664 power_saving_func(true);
1665 set_pm_status_flag(LOWBT_FLAG);
1667 if (get_pm_status_flag() & PWRSV_FLAG)
1668 power_saving_func(false);
1669 clear_pm_status_flag(LOWBT_FLAG);
1670 clear_pm_status_flag(BRTCH_FLAG);
1671 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, false);
1673 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
1676 case SETTING_CHARGING:
1678 if (get_pm_status_flag() & LOWBT_FLAG) {
1679 power_saving_func(false);
1680 clear_pm_status_flag(LOWBT_FLAG);
1682 set_pm_status_flag(CHRGR_FLAG);
1685 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
1688 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
1689 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
1691 if (low_battery_state(bat_state)) {
1692 power_saving_func(true);
1693 set_pm_status_flag(LOWBT_FLAG);
1695 clear_pm_status_flag(CHRGR_FLAG);
1698 case SETTING_BRT_LEVEL:
1699 if (get_pm_status_flag() & PWRSV_FLAG) {
1700 set_pm_status_flag(BRTCH_FLAG);
1701 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, true);
1703 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
1704 _I("Brightness changed in low battery,"
1705 "escape dim state.");
1707 backlight_ops->set_default_brt(val);
1709 case SETTING_LOCK_SCREEN:
1710 set_lock_screen_state(val);
1711 if (val == VCONFKEY_IDLE_UNLOCK) {
1712 if (CHECK_OPS(keyfilter_ops, backlight_enable))
1713 keyfilter_ops->backlight_enable(false);
1716 /* LCD on if lock screen show before waiting time */
1717 if ((get_pm_cur_state() == S_NORMAL) &&
1718 val == VCONFKEY_IDLE_LOCK &&
1719 backlight_ops->get_lcd_power() != DPMS_ON &&
1720 is_lcdon_blocked() == LCDON_BLOCK_NONE)
1721 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1723 update_display_time();
1724 if (get_pm_cur_state() == S_NORMAL)
1725 states[get_pm_cur_state()].trans(EVENT_INPUT);
1727 case SETTING_LOCK_SCREEN_BG:
1728 set_lock_screen_bg_state(val);
1729 update_display_time();
1730 if (get_pm_cur_state() == S_NORMAL)
1731 states[get_pm_cur_state()].trans(EVENT_INPUT);
1733 case SETTING_POWER_CUSTOM_BRIGHTNESS:
1734 if (val == VCONFKEY_PM_CUSTOM_BRIGHTNESS_ON)
1735 backlight_ops->set_custom_status(true);
1737 backlight_ops->set_custom_status(false);
1746 static void check_seed_status(void)
1754 /* Charging check */
1755 if (fp_get_charging_status && (fp_get_charging_status(&tmp) == 0) && (tmp > 0))
1756 set_pm_status_flag(CHRGR_FLAG);
1758 ret = get_setting_brightness(&tmp);
1759 if (ret != 0 || (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS)) {
1760 _I("Failed to read vconf value for brightness.");
1761 brt = display_conf.pm_default_brightness;
1762 if (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS) {
1763 ret = vconf_set_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, brt);
1765 _E("Failed to set vconf value for lcd brightness: %d", vconf_get_ext_errno());
1769 _I("Set brightness(%d) from setting app.", tmp);
1770 backlight_ops->set_default_brt(tmp);
1771 backlight_ops->set_brightness(tmp);
1773 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat_state);
1775 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
1776 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
1778 if (low_battery_state(bat_state)) {
1779 if (!(get_pm_status_flag() & CHRGR_FLAG)) {
1780 power_saving_func(true);
1781 set_pm_status_flag(LOWBT_FLAG);
1785 /* lock screen check */
1786 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1789 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
1791 set_lock_screen_state(lock_state);
1792 if (lock_state == VCONFKEY_IDLE_LOCK) {
1793 states[S_NORMAL].timeout = lock_screen_timeout;
1794 _I("LCD NORMAL timeout(%d ms) is set"
1795 " for lock screen.", lock_screen_timeout);
1801 static void init_lcd_operation(void)
1803 const struct device_ops *ops = NULL;
1805 ops = find_device("display");
1806 if (!check_default(ops))
1807 SYS_G_LIST_APPEND(lcdon_ops, ops);
1809 ops = find_device("touchkey");
1810 if (!check_default(ops))
1811 SYS_G_LIST_APPEND(lcdon_ops, ops);
1813 ops = find_device("touchscreen");
1814 if (!check_default(ops))
1815 SYS_G_LIST_APPEND(lcdon_ops, ops);
1818 static void exit_lcd_operation(void)
1821 GList *l_next = NULL;
1822 const struct device_ops *ops = NULL;
1824 SYS_G_LIST_FOREACH_SAFE(lcdon_ops, l, l_next, ops)
1825 SYS_G_LIST_REMOVE_LIST(lcdon_ops, l);
1837 static const char *errMSG[INIT_END] = {
1838 [INIT_SETTING] = "setting init error",
1839 [INIT_INTERFACE] = "lowlevel interface(sysfs or others) init error",
1840 [INIT_POLL] = "input devices poll init error",
1841 [INIT_FIFO] = "FIFO poll init error",
1842 [INIT_DBUS] = "d-bus init error",
1845 int set_lcd_timeout(int on, int dim, int holdkey_block, const char *name)
1847 if (on == 0 && dim == 0) {
1848 _I("LCD timeout changed: default setting");
1849 custom_normal_timeout = custom_dim_timeout = 0;
1850 } else if (on < 0 || dim < 0) {
1851 _E("Failed to set value(on=%d dim=%d).", on, dim);
1854 _I("LCD timeout changed: on=%ds dim=%ds", on, dim);
1855 custom_normal_timeout = SEC_TO_MSEC(on);
1856 custom_dim_timeout = SEC_TO_MSEC(dim);
1858 /* Apply new backlight time */
1859 update_display_time();
1860 if (get_pm_cur_state() == S_NORMAL)
1861 states[get_pm_cur_state()].trans(EVENT_INPUT);
1863 if (holdkey_block) {
1864 custom_holdkey_block = true;
1865 _I("Hold key disabled.");
1867 custom_holdkey_block = false;
1868 _I("Hold key enabled.");
1871 if (custom_change_name) {
1872 free(custom_change_name);
1873 custom_change_name = 0;
1876 if (custom_normal_timeout == 0 &&
1877 custom_dim_timeout == 0 &&
1881 custom_change_name = strndup(name, strlen(name));
1882 if (!custom_change_name) {
1883 _E("Failed to malloc.");
1884 custom_normal_timeout = custom_dim_timeout = 0;
1885 custom_holdkey_block = false;
1892 void reset_lcd_timeout(GDBusConnection *conn,
1893 const gchar *sender,
1894 const gchar *unique_name,
1900 if (!custom_change_name)
1903 if (strcmp(sender, custom_change_name))
1906 _I("reset lcd timeout: Set default timeout. sender=%s", sender);
1908 free(custom_change_name);
1909 custom_change_name = 0;
1910 custom_normal_timeout = custom_dim_timeout = 0;
1911 custom_holdkey_block = false;
1913 update_display_time();
1914 if (get_pm_cur_state() == S_NORMAL)
1915 states[get_pm_cur_state()].trans(EVENT_INPUT);
1918 static int delayed_init_done(void *data)
1920 static bool done = false;
1929 _I("Booting done, release booting lock.");
1930 if (disp_plgn->pm_unlock_internal) {
1931 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL, PM_SLEEP_MARGIN);
1932 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF, PM_SLEEP_MARGIN);
1938 static int battery_health_changed(void *data)
1940 int health = DATA_VALUE_INT(data);
1942 _I("battery health change %d", health);
1944 if (health == HEALTH_GOOD) {
1945 clear_pm_status_flag(BATTERY_FLAG);
1946 clear_pm_status_flag(DIMSTAY_FLAG);
1947 } else if (health == HEALTH_LOW || health == HEALTH_HIGH || health == HEALTH_OVP) {
1948 set_pm_status_flag(BATTERY_FLAG);
1949 set_pm_status_flag(DIMSTAY_FLAG);
1952 if (backlight_ops->get_lcd_power() == DPMS_ON)
1953 backlight_ops->update();
1958 static gboolean delayed_init_done_dpms(gpointer data)
1963 return G_SOURCE_CONTINUE;
1965 switch (get_pm_cur_state()) {
1968 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1969 if (display_conf.timeout_enable) {
1970 timeout = states[S_NORMAL].timeout;
1971 /* check minimun lcd on time */
1972 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
1973 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
1974 reset_timeout(timeout);
1978 lcd_off_procedure(LCD_OFF_BY_EVENT);
1979 timeout = display_conf.lcdoff_timeout;
1980 reset_timeout(timeout);
1986 return G_SOURCE_REMOVE;
1989 static void add_timer_for_init_dpms(void)
1991 guint id = g_timeout_add(500/* milliseconds */, delayed_init_done_dpms, NULL);
1993 _E("Failed to add init_dpms timeout.");
1997 * Power manager Main
2000 static int display_probe(void *data)
2004 /* check display feature */
2005 if (!is_feature_display_supported())
2009 * load display service
2010 * if there is no display shared library,
2011 * deviced does not provide any method and function of display.
2013 ret = display_service_load();
2017 /* display_plugin instance initialization */
2019 disp_plgn->device_flags_to_string = __device_flags_to_string;
2021 if (battery_plgn->handle) {
2022 fp_get_charging_status = dlsym(battery_plgn->handle, "get_charging_status");
2023 if (!fp_get_charging_status)
2024 _E("Failed to obtain address of get_charging_status");
2026 _I("There is no battery module.");
2031 static int input_init_handler(void)
2033 if (!display_conf.input_support)
2034 remove_device_by_devname("input");
2039 static int poweroff_triggered_callback(void *udata)
2041 int val = (int)(intptr_t) udata;
2044 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
2045 clear_pm_status_flag(PWROFF_FLAG);
2047 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
2048 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
2049 set_pm_status_flag(PWROFF_FLAG);
2056 static void esd_action(void)
2058 const struct device_ops *touchscreen_ops = NULL;
2062 touchscreen_ops = find_device("touchscreen");
2064 if (!check_default(touchscreen_ops))
2065 touchscreen_ops->stop(NORMAL_MODE);
2066 backlight_ops->off(NORMAL_MODE);
2067 backlight_ops->on(NORMAL_MODE);
2068 if (!check_default(touchscreen_ops))
2069 touchscreen_ops->start(NORMAL_MODE);
2072 static void lcd_uevent_changed(struct udev_device *dev)
2074 const char *devpath;
2077 devpath = udev_device_get_devpath(dev);
2081 if (!fnmatch(LCD_ESD_PATH, devpath, 0)) {
2082 action = udev_device_get_action(dev);
2083 if (!strcmp(action, UDEV_CHANGE))
2088 static const struct uevent_handler lcd_uevent_ops = {
2089 .subsystem = LCD_EVENT_SUBSYSTEM,
2090 .uevent_func = lcd_uevent_changed,
2094 static void display_init(void *data)
2097 unsigned int flags = (WITHOUT_STARTNOTI | FLAG_X_DPMS);
2100 _I("Start power manager.");
2102 signal(SIGHUP, sig_hup);
2104 power_saving_func = default_saving_mode;
2106 /* load configutation */
2107 ret = config_parse(DISPLAY_CONF_FILE, display_load_config, &display_conf);
2109 _W("Failed to load '%s', use default value: %d",
2110 DISPLAY_CONF_FILE, ret);
2112 register_kernel_uevent_control(&lcd_uevent_ops);
2114 register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
2115 register_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
2116 register_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
2117 register_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
2118 register_notifier(DEVICE_NOTIFIER_BATTERY_HEALTH, battery_health_changed);
2119 register_notifier(DEVICE_NOTIFIER_DISPLAY_BRIGHTNESS, display_brightness_changed);
2120 register_notifier(DEVICE_NOTIFIER_LCD_AUTOBRT_SENSING, display_auto_brightness_sensing);
2121 register_notifier(DEVICE_NOTIFIER_POWEROFF_TRIGGERED, poweroff_triggered_callback);
2123 init_save_userlock();
2125 for (i = INIT_SETTING; i < INIT_END; i++) {
2128 ret = init_setting(update_setting);
2130 case INIT_INTERFACE:
2131 if (display_conf.timeout_enable)
2132 get_lcd_timeout_from_settings();
2133 ret = init_sysfs(flags);
2137 ret = input_init_handler();
2139 pm_lock_detector_init();
2143 ret = init_pm_dbus();
2147 _E("Failed to init: %s", errMSG[i]);
2152 if (i == INIT_END) {
2153 display_ops_init(NULL);
2154 #ifdef ENABLE_PM_LOG
2157 init_lcd_operation();
2158 check_seed_status();
2160 /* In smd test, TSP should be turned off if display panel is not existed. */
2161 if (backlight_ops->get_lcd_power() == -ENOENT) {
2162 _I("Display panel is not existed.");
2163 lcd_direct_control(DPMS_OFF, NORMAL_MODE);
2164 exit_lcd_operation();
2167 /* wm_ready needs to be checked
2168 * since display manager can be launched later than deviced.
2169 * In the case, display cannot be turned on at the first booting */
2170 // wm_ready = check_wm_ready();
2172 if (is_lcdon_blocked() != LCDON_BLOCK_NONE)
2173 lcd_off_procedure(LCD_OFF_BY_EVENT);
2175 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
2177 add_timer_for_init_dpms();
2180 if (display_conf.lcd_always_on) {
2181 _I("LCD always on.");
2182 trans_table[S_NORMAL][EVENT_TIMEOUT] = S_NORMAL;
2185 if (flags & WITHOUT_STARTNOTI) { /* start without noti */
2186 _I("Start Power managing without noti");
2187 if (pm_get_power_lock_support()) {
2188 broadcast_pm_wakeup();
2189 power_acquire_wakelock();
2192 * Lock lcd off until booting is done.
2193 * deviced guarantees all booting script is executing.
2194 * Last script of booting unlocks this suspend blocking state.
2196 if (disp_plgn->pm_lock_internal)
2197 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF,
2198 STAY_CUR_STATE, DELAYED_INIT_WATING_TIME);
2200 /* Initial display state right after the booting done */
2201 if (is_lcdon_blocked())
2202 set_pm_cur_state(S_LCDOFF);
2204 set_pm_cur_state(S_NORMAL);
2205 ret = vconf_set_int(VCONFKEY_PM_STATE, get_pm_cur_state());
2207 _E("Failed to set vconf value for pm cur state: %d", vconf_get_ext_errno());
2209 status = DEVICE_OPS_STATUS_START;
2210 if (display_conf.timeout_enable) {
2211 timeout = states[S_NORMAL].timeout;
2212 /* check minimun lcd on time */
2213 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
2214 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
2216 if (disp_plgn->pm_lock_internal)
2217 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL,
2218 STAY_CUR_STATE, timeout);
2223 if (display_conf.input_support)
2224 if (CHECK_OPS(keyfilter_ops, init))
2225 keyfilter_ops->init();
2229 static void display_exit(void *data)
2233 status = DEVICE_OPS_STATUS_STOP;
2235 /* Set current state to S_NORMAL */
2236 set_pm_cur_state(S_NORMAL);
2237 set_setting_pmstate(get_pm_cur_state());
2238 /* timeout is not needed */
2239 reset_timeout(TIMEOUT_NONE);
2241 if (CHECK_OPS(keyfilter_ops, exit))
2242 keyfilter_ops->exit();
2244 unregister_kernel_uevent_control(&lcd_uevent_ops);
2246 display_ops_exit(NULL);
2248 for (i = i - 1; i >= INIT_SETTING; i--) {
2253 case INIT_INTERFACE:
2257 unregister_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
2258 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
2259 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
2260 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
2261 unregister_notifier(DEVICE_NOTIFIER_BATTERY_HEALTH, battery_health_changed);
2262 unregister_notifier(DEVICE_NOTIFIER_DISPLAY_BRIGHTNESS, display_brightness_changed);
2268 exit_lcd_operation();
2269 free_lock_info_list();
2271 /* free display service */
2272 display_service_free();
2274 _I("Stop power manager.");
2277 static int display_start(enum device_flags flags)
2280 if (flags & NORMAL_MODE) {
2281 if (flags & LCD_PANEL_OFF_MODE)
2283 backlight_ops->standby(true);
2286 backlight_ops->on(flags);
2291 /* CORE LOGIC MODE */
2292 if (!(flags & CORE_LOGIC_MODE))
2295 if (status == DEVICE_OPS_STATUS_START)
2298 if (display_probe(NULL) < 0)
2306 static int display_stop(enum device_flags flags)
2309 if (flags & NORMAL_MODE || flags & FORCE_OFF_MODE) {
2310 backlight_ops->off(flags);
2314 /* CORE LOGIC MODE */
2315 if (!(flags & CORE_LOGIC_MODE))
2318 if (status == DEVICE_OPS_STATUS_STOP)
2326 static int display_status(void)
2331 static const struct device_ops display_device_ops = {
2332 .priority = DEVICE_PRIORITY_HIGH,
2333 DECLARE_NAME_LEN("display"),
2334 .probe = display_probe,
2335 .init = display_init,
2336 .exit = display_exit,
2337 .start = display_start,
2338 .stop = display_stop,
2339 .status = display_status,
2342 DEVICE_OPS_REGISTER(&display_device_ops)
2344 static void __CONSTRUCTOR__ initialize(void)
2346 disp_plgn = get_var_display_plugin();
2348 _E("Failed to get display plugin variable.");
2350 backlight_ops = get_var_backlight_ops();
2352 _E("Failed to get backlight operator variable.");
2354 display_white_balance_ops = get_var_display_white_balance_ops();
2355 if (!display_white_balance_ops)
2356 _E("Failed to get display white balance operator variable.");
2358 battery_plgn = get_var_battery_plugin();
2360 _E("Failed to get battery plugin variable.");