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 <hal/device/hal-device-power.h>
45 #include "ambient-mode.h"
49 #include "lock-detector.h"
50 #include "display-ops.h"
51 #include "shared/devices.h"
52 #include "shared/device-notifier.h"
53 #include "core/udev.h"
54 #include "shared/common.h"
55 #include "shared/apps.h"
56 #include "extcon/extcon.h"
57 #include "battery/power-supply.h"
58 #include "power/power.h"
59 #include "power/power-off.h"
60 #include "power/power-suspend.h"
61 #include "power/power-boot.h"
62 #include "power/power-doze.h"
63 #include "device-interface.h"
64 #include "display-plugin.h"
65 #include "proximity.h"
66 #include "display-info.h"
67 #include "display-signal.h"
68 #include "display-lock.h"
69 #include "display-backlight.h"
70 #include "display-misc.h"
71 #include "display-panel.h"
72 #include "display-config.h"
73 #include "display-state-transition.h"
74 #include "shared/plugin.h"
77 * @addtogroup POWER_MANAGER
81 #define LOCK_SCREEN_INPUT_TIMEOUT 10000
82 #define LOCK_SCREEN_CONTROL_TIMEOUT 5000
83 #define ALWAYS_ON_TIMEOUT 360000000
85 #define GESTURE_STR "gesture"
86 #define POWER_KEY_STR "powerkey"
87 #define TOUCH_STR "touch"
88 #define EVENT_STR "event"
89 #define TIMEOUT_STR "timeout"
90 #define PROXI_STR "proximity"
91 #define PALM_STR "palm"
92 #define UNKNOWN_STR "unknown"
94 extern void init_save_userlock(void);
96 static struct display_plugin *disp_plgn;
97 static struct display_backlight_ops *backlight_ops;
98 static struct battery_plugin *battery_plgn;
99 static int (*fp_get_charging_status) (int *val);
101 static void (*power_saving_func) (int onoff);
102 static enum device_ops_status status = DEVICE_OPS_STATUS_UNINIT;
104 static guint timeout_src_id;
105 static int system_wakeup_flag = false;
106 static unsigned int custom_normal_timeout = 0;
107 static unsigned int custom_dim_timeout = 0;
108 static char *custom_change_name;
109 static guint lock_timeout_id;
110 static int lock_screen_timeout = LOCK_SCREEN_INPUT_TIMEOUT;
111 static struct timeval lcdon_tv;
113 * The two variables(lcdon_broadcast, pmstate_suspend) must be set initial
114 * state because it should be sent from previous state at booting time.
116 static bool lcdon_broadcast = true;
118 static bool touch_blocked = false;
120 /* default transition, action fuctions */
121 static int default_trans(int evt);
122 static int default_action(int timeout);
123 static int default_check(int curr, int next);
125 static gboolean del_normal_cond(void *data);
126 static gboolean del_dim_cond(void *data);
127 static gboolean del_off_cond(void *data);
129 static int default_proc_change_state(unsigned int cond, pid_t pid);
130 static int (*proc_change_state)(unsigned int cond, pid_t pid) = default_proc_change_state;
132 static struct state states[S_END] = {
133 { S_START, "S_START", NULL, NULL, NULL, NULL },
134 { S_NORMAL, "S_NORMAL", default_trans, default_action, default_check, del_normal_cond },
135 { S_LCDDIM, "S_LCDDIM", default_trans, default_action, default_check, del_dim_cond },
136 { S_LCDOFF, "S_LCDOFF", default_trans, default_action, default_check, del_off_cond },
137 { S_STANDBY, "S_STANDBY", NULL, NULL, NULL, NULL },
138 { S_SLEEP, "S_SLEEP", default_trans, default_action, default_check, NULL },
139 { S_POWEROFF, "S_POWEROFF", NULL, NULL, NULL, NULL },
142 #define SHIFT_UNLOCK 4
143 #define SHIFT_CHANGE_STATE 7
144 #define CHANGE_STATE_BIT 0xF00 /* 1111 0000 0000 */
145 #define SHIFT_LOCK_FLAG 16
146 #define SHIFT_CHANGE_TIMEOUT 20
147 #define CUSTOM_TIMEOUT_BIT 0x1
148 #define CUSTOM_HOLDKEY_BIT 0x2
149 #define HOLD_KEY_BLOCK_BIT 0x1
150 #define TIMEOUT_NONE (-1)
152 #define S_COVER_TIMEOUT 8000
153 #define GET_HOLDKEY_BLOCK_STATE(x) ((x >> SHIFT_LOCK_FLAG) & HOLD_KEY_BLOCK_BIT)
154 #define DELAYED_INIT_WATING_TIME 60000 /* 1 minute */
156 #define LOCK_SCREEN_WATING_TIME 300 /* 0.3 second */
157 #define LONG_PRESS_INTERVAL 400 /* 0.4 seconds */
158 #define SAMPLING_INTERVAL 1 /* 1 sec */
159 #define BRIGHTNESS_CHANGE_STEP 10
160 #define LCD_ALWAYS_ON 0
161 #define ACCEL_SENSOR_ON 1
162 #define CONTINUOUS_SAMPLING 1
163 #define LCDOFF_TIMEOUT 300 /* milli second */
165 #define DIFF_TIMEVAL_MS(a, b) \
166 (((a.tv_sec * 1000000 + a.tv_usec) - \
167 (b.tv_sec * 1000000 + b.tv_usec)) \
170 static struct display_config display_conf = {
171 .lock_wait_time = LOCK_SCREEN_WATING_TIME,
172 .longpress_interval = LONG_PRESS_INTERVAL,
173 .lightsensor_interval = SAMPLING_INTERVAL,
174 .lcdoff_timeout = LCDOFF_TIMEOUT,
175 .pm_default_brightness = 80,
176 .brightness_change_step = BRIGHTNESS_CHANGE_STEP,
177 .lcd_always_on = LCD_ALWAYS_ON,
179 .framerate_app = {0, 0, 0, 0},
180 .control_display = 0,
181 .powerkey_doublepress = 0,
182 .accel_sensor_on = ACCEL_SENSOR_ON,
183 .continuous_sampling = CONTINUOUS_SAMPLING,
184 .timeout_enable = true,
185 .input_support = true,
186 .lockcheck_timeout = 600,
187 .display_init_direction = DISPLAY_INIT_DIRECTION_HORIZONTAL,
188 .aod_enter_level = 40,
190 .touch_wakeup = false,
191 .display_on_usb_conn_changed = true,
192 .display_dpms_type = DISPLAY_DPMS_TYPE_WINDOW_MANAGER,
195 struct display_function_info display_info = {
196 .update_auto_brightness = NULL,
197 .set_autobrightness_min = NULL,
198 .reset_autobrightness_min = NULL,
199 .face_detection = NULL,
202 inline const struct display_config* get_var_display_config()
204 return &display_conf;
207 inline struct state* state_st(enum state_t state)
209 return &states[state];
212 guint get_transition_timer(void)
214 return timeout_src_id;
217 static const char* __device_flags_to_string(enum device_flags flags)
220 if (flags & (LCD_ON_BY_GESTURE | LCD_OFF_BY_GESTURE))
222 else if (flags & (LCD_ON_BY_POWER_KEY | LCD_OFF_BY_POWER_KEY))
223 return POWER_KEY_STR;
224 else if (flags & (LCD_ON_BY_EVENT | LCD_OFF_BY_EVENT))
226 else if (flags & LCD_ON_BY_TOUCH)
228 else if (flags & LCD_OFF_BY_TIMEOUT)
230 else if (flags & LCD_OFF_BY_PROXIMITY)
232 else if (flags & LCD_OFF_BY_PALM)
238 static unsigned long get_lcd_on_flags(void)
240 unsigned long flags = NORMAL_MODE;
241 bool lcd_paneloff_mode = false;
243 display_panel_get_lcd_paneloff_mode(&lcd_paneloff_mode);
244 if (lcd_paneloff_mode)
245 flags |= LCD_PANEL_OFF_MODE;
250 bool touch_event_blocked(void)
252 return touch_blocked;
255 void lcd_on_procedure(int state, enum device_flags flag)
257 unsigned long flags = get_lcd_on_flags();
261 * Display on procedure
263 * step 2. broadcast lcd on signal with cause
264 * step 3. set brightness
265 * step 4. set pmstate of vconf
266 * step 5. display on operate
268 * - b. TSP(touch screen) and touchkey enable
269 * step 6. broadcast lcd on complete signal
270 * step 7. key backlight enable
274 _I("[lcdstep] 0x%lx", flags);
276 /* send LCDOn dbus signal */
277 if (!lcdon_broadcast)
278 broadcast_lcd_on(SIGNAL_PRE, flags);
280 /* Update brightness level */
281 if (state == LCD_DIM)
282 display_backlight_set_brightness_by_dim_brightness();
283 else if (state == LCD_NORMAL)
284 display_backlight_update_by_default_brightness();
286 if (state == LCD_NORMAL)
287 set_setting_pmstate(S_NORMAL);
288 else if (state == LCD_DIM)
289 set_setting_pmstate(S_LCDDIM);
291 display_start_dependent_device(flags);
293 if (!lcdon_broadcast) {
294 broadcast_lcd_on(SIGNAL_POST, flags);
295 lcdon_broadcast = true;
298 if (CHECK_OPS(keyfilter_ops, backlight_enable))
299 keyfilter_ops->backlight_enable(true);
301 touch_blocked = false;
304 static unsigned long get_lcd_off_flags(void)
306 unsigned long flags = NORMAL_MODE;
307 bool stay_touchscreen_off = false;
309 display_misc_get_stay_touchscreen_off(&stay_touchscreen_off);
310 if (stay_touchscreen_off)
311 flags |= TOUCH_SCREEN_OFF_MODE;
316 inline void lcd_off_procedure(enum device_flags flag)
318 unsigned long flags = get_lcd_off_flags();
322 * Display off procedure
323 * step 0. enhance mode off using nofity (e.g mdnie, HBM, LBM)
324 * step 1. broadcast lcd off signal with cause
325 * step 2. set pmstate of vconf
326 * step 3. display off operate
328 * - b. TSP(touch screen) and touchkey disable
329 * step 4. broadcast lcd off complete siganl
330 * step 5. enter doze mode if it is enabled
332 _I("[lcdstep] 0x%lx", flags);
336 device_notify(DEVICE_NOTIFIER_LCD_OFF, NULL);
338 if (lcdon_broadcast) {
339 broadcast_lcd_off(SIGNAL_PRE, flags);
340 lcdon_broadcast = false;
345 touch_blocked = true;
347 set_setting_pmstate(S_LCDOFF);
349 if (CHECK_OPS(keyfilter_ops, backlight_enable))
350 keyfilter_ops->backlight_enable(false);
352 display_stop_dependent_device(flags);
354 broadcast_lcd_off(SIGNAL_POST, flags);
355 device_notify(DEVICE_NOTIFIER_LCD_OFF_COMPLETE, NULL);
360 static void del_state_cond(void *data, enum state_t state)
362 PmLockNode *tmp = NULL;
368 /* A passed data is a pid_t type data, not a 64bit data. */
369 pid = (pid_t)((intptr_t)data);
370 _I("delete prohibit %s condition by timeout (%d)", states[state].name, pid);
372 if (pid == INTERNAL_LOCK_AMBIENT)
373 ambient_check_invalid_state(pid);
375 tmp = find_node(state, pid);
376 del_node(state, tmp);
377 set_unlock_time(pid, state);
379 /* Change state only when the two conditions below are satisfied.
380 * 1. There should be no running state-transition timer
381 * 2. Released lock is one of the pm_cur_state's lock
382 * This emulates already expired transition timer */
383 if (!timeout_src_id && get_pm_cur_state() == state)
384 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
386 if (state == S_LCDOFF)
387 set_process_active(false, pid);
390 static gboolean del_normal_cond(void *data)
392 del_state_cond(data, S_NORMAL);
393 return G_SOURCE_REMOVE;
396 static gboolean del_dim_cond(void *data)
398 del_state_cond(data, S_LCDDIM);
399 return G_SOURCE_REMOVE;
402 static gboolean del_off_cond(void *data)
404 del_state_cond(data, S_LCDOFF);
405 return G_SOURCE_REMOVE;
408 /* timeout handler */
409 gboolean timeout_handler(void *data)
411 _I("Time out state %s", states[get_pm_cur_state()].name);
413 if (timeout_src_id) {
414 g_source_remove(timeout_src_id);
418 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
419 return G_SOURCE_REMOVE;
422 void reset_timeout(int timeout)
424 enum state_t next_state;
426 if (!display_conf.timeout_enable)
429 if ((get_pm_cur_state() == S_LCDOFF)
430 && (is_emulator() == true || timeout_sleep_support == false))
433 _I("Reset timeout(%d ms).", timeout);
434 if (timeout_src_id != 0) {
435 g_source_remove(timeout_src_id);
439 display_state_transition_get_next_transition_display_state(get_pm_cur_state(), &next_state, EVENT_TIMEOUT);
440 if (next_state == get_pm_cur_state())
444 timeout_src_id = g_timeout_add(timeout,
445 timeout_handler, NULL);
446 else if (timeout == 0)
447 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
450 /* get configurations from setting */
451 static int get_lcd_timeout_from_settings(void)
456 for (i = 0; i < S_END; i++) {
457 switch (states[i].state) {
459 get_run_timeout(&val);
462 get_dim_timeout(&val);
465 val = display_conf.lcdoff_timeout;
468 /* This state doesn't need to set time out. */
473 states[i].timeout = val;
475 _I("State(%s) timeout(%d) ms", states[i].name,
482 static void update_display_time(void)
484 int run_timeout, val;
486 /* first priority : custom timeout */
487 if (custom_normal_timeout > 0) {
488 states[S_NORMAL].timeout = custom_normal_timeout;
489 states[S_LCDDIM].timeout = custom_dim_timeout;
490 _I("CUSTOM : timeout is set by normal(%d ms), dim(%d ms)",
491 custom_normal_timeout, custom_dim_timeout);
495 /* second priority : lock state */
496 if ((__get_lock_screen_state() == VCONFKEY_IDLE_LOCK) &&
497 !get_lock_screen_bg_state()) {
498 /* timeout is different according to key or event. */
499 states[S_NORMAL].timeout = lock_screen_timeout;
500 _I("LOCK: Timeout(%d ms) is set by normal.",
501 lock_screen_timeout);
505 /* default setting */
506 get_run_timeout(&run_timeout);
509 * if the run_timeout is zero, it regards AlwaysOn state
511 if (run_timeout == 0 || display_conf.lcd_always_on) {
512 run_timeout = ALWAYS_ON_TIMEOUT;
513 _I("LCD always on.");
516 states[S_NORMAL].timeout = run_timeout;
518 get_dim_timeout(&val);
519 states[S_LCDDIM].timeout = val;
521 _I("Normal: NORMAL timeout is set by %d ms", states[S_NORMAL].timeout);
522 _I("Normal: DIM timeout is set by %d ms", states[S_LCDDIM].timeout);
525 static void update_display_locktime(int time)
527 lock_screen_timeout = time;
528 update_display_time();
531 void set_dim_state(bool on)
533 _I("Dim state is %d.", on);
534 update_display_time();
535 states[get_pm_cur_state()].trans(EVENT_INPUT);
538 void lcd_on_direct(enum device_flags flags)
540 enum hal_device_power_transition_reason reason;
542 if (flags & LCD_ON_BY_POWER_KEY)
543 reason = HAL_DEVICE_POWER_TRANSITION_REASON_POWER_KEY;
544 else if (flags & LCD_ON_BY_TOUCH)
545 reason = HAL_DEVICE_POWER_TRANSITION_REASON_TOUCH_SCREEN;
547 reason = HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN;
549 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL, reason, NULL);
550 set_pm_cur_state(S_NORMAL);
552 _D("lcd is on directly");
553 gettimeofday(&lcdon_tv, NULL);
554 lcd_on_procedure(LCD_NORMAL, flags);
556 update_display_locktime(LOCK_SCREEN_INPUT_TIMEOUT);
559 static inline bool check_lcd_is_on(void)
561 if (display_panel_get_dpms_cached_state() != DPMS_ON)
567 static gboolean timer_refresh_cb(gpointer data)
571 /* state transition */
572 set_pm_old_state(get_pm_cur_state());
573 set_pm_cur_state(S_NORMAL);
574 st = &states[get_pm_cur_state()];
577 st->action(st->timeout);
582 int custom_lcdon(int timeout)
589 if (check_lcd_is_on() == false)
590 lcd_on_direct(LCD_ON_BY_GESTURE);
592 _I("Custom lcd on timeout(%d ms).", timeout);
593 if (set_custom_lcdon_timeout(timeout) == true)
594 update_display_time();
596 /* state transition */
597 set_pm_old_state(get_pm_cur_state());
598 set_pm_cur_state(S_NORMAL);
599 st = &states[get_pm_cur_state()];
603 st->action(st->timeout);
605 g_idle_add(timer_refresh_cb, NULL);
610 int custom_lcdoff(enum device_flags flag)
614 check_processes(S_NORMAL);
615 check_processes(S_LCDDIM);
617 /* check holdkey block flag in lock node */
618 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
620 * When another proccess is normal lock, device is received call then,
621 * call app can be changed to lcd state by proximity.
622 * If proximity is near then normal lock will be unlocked.
624 if (flag & LCD_OFF_BY_PROXIMITY) {
625 _I("custom lcd off by proximity, delete normal lock");
626 delete_condition(S_NORMAL);
628 _I("skip custom lcd off");
633 _I("custom lcd off by flag(%d)", flag);
634 if (display_panel_get_dpms_cached_state() == DPMS_ON)
635 lcd_off_procedure(flag);
637 if (set_custom_lcdon_timeout(0) == true)
638 update_display_time();
640 /* state transition */
641 set_pm_old_state(get_pm_cur_state());
642 set_pm_cur_state(S_LCDOFF);
643 st = &states[get_pm_cur_state()];
647 st->action(st->timeout);
652 int display_on_by_reason(const char *reason, int timeout)
658 str_len = strlen(reason);
660 if (!strncmp(reason, GESTURE_STR, str_len))
661 flag = LCD_ON_BY_GESTURE;
662 else if (!strncmp(reason, EVENT_STR, str_len))
663 flag = LCD_ON_BY_EVENT;
665 _E("Reason is unknown(%s)", reason);
670 _E("Cannot setting timeout %d", timeout);
674 if (check_lcd_is_on() == false)
677 _I("platform lcd on by %s (%d ms)", reason, timeout);
678 if (set_custom_lcdon_timeout(timeout) == true)
679 update_display_time();
681 /* state transition */
682 set_pm_old_state(get_pm_cur_state());
683 set_pm_cur_state(S_NORMAL);
684 st = &states[get_pm_cur_state()];
688 st->action(st->timeout);
693 int display_off_by_reason(const char *reason)
699 str_len = strlen(reason);
701 if (!strncmp(reason, GESTURE_STR, str_len)) {
702 check_processes(S_NORMAL);
703 check_processes(S_LCDDIM);
705 /* check holdkey block flag in lock node */
706 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
707 _I("skip platform lcd off by gesture");
710 flag = LCD_OFF_BY_GESTURE;
711 } else if (!strncmp(reason, PALM_STR, str_len)) {
712 delete_condition(S_NORMAL);
713 delete_condition(S_LCDDIM);
715 flag = LCD_OFF_BY_PALM;
717 _E("Reason is unknown(%s)", reason);
721 _I("platform lcd off by %s", reason);
722 if (display_panel_get_dpms_cached_state() == DPMS_ON)
723 lcd_off_procedure(flag);
725 /* state transition */
726 set_pm_old_state(get_pm_cur_state());
727 set_pm_cur_state(S_LCDOFF);
728 st = &states[get_pm_cur_state()];
732 st->action(st->timeout);
737 static void default_proc_change_state_action(enum state_t next, int timeout)
741 set_pm_old_state(get_pm_cur_state());
742 set_pm_cur_state(next);
744 st = &states[get_pm_cur_state()];
746 if (st && st->action) {
748 st->action(st->timeout);
754 static int default_proc_change_state(unsigned int cond, pid_t pid)
758 next = GET_COND_STATE(cond);
759 _I("Change process(%d) state to %s.", pid, states[next].name);
763 if (check_lcd_is_on() == false)
764 lcd_on_direct(LCD_ON_BY_EVENT);
765 update_display_locktime(LOCK_SCREEN_CONTROL_TIMEOUT);
766 default_proc_change_state_action(next, -1);
769 default_proc_change_state_action(next, -1);
772 if (display_panel_get_dpms_cached_state() == DPMS_ON) {
773 if (get_proximity_state() == SENSOR_PROXIMITY_NEAR)
774 lcd_off_procedure(LCD_OFF_BY_PROXIMITY);
776 lcd_off_procedure(LCD_OFF_BY_EVENT);
778 if (set_custom_lcdon_timeout(0))
779 update_display_time();
780 default_proc_change_state_action(next, -1);
783 _I("Dangerous requests.");
784 /* at first LCD_OFF and then goto sleep */
785 /* state transition */
786 default_proc_change_state_action(S_LCDOFF, TIMEOUT_NONE);
787 delete_condition(S_NORMAL);
788 delete_condition(S_LCDDIM);
789 delete_condition(S_LCDOFF);
790 if (lcdon_broadcast) {
791 _I("broadcast lcd off signal at non-lcd device");
792 broadcast_lcd_off(SIGNAL_PRE, 0);
793 broadcast_lcd_off(SIGNAL_POST, 0);
795 default_proc_change_state_action(S_SLEEP, TIMEOUT_NONE);
805 static void proc_condition_lock(PMMsg *data)
808 guint cond_timeout_id = 0;
809 char pname[PATH_MAX];
810 pid_t pid = data->pid;
812 int holdkey_block, ret;
815 const char *lock_type = NULL;
817 state = GET_COND_STATE(data->cond);
818 if (state == S_START)
821 flags = GET_COND_FLAG(data->cond);
822 display_misc_get_process_name(pid, pname);
824 if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) &&
825 (pm_get_power_lock() == POWER_UNLOCK))
826 proc_change_state(data->cond, INTERNAL_LOCK_PM);
828 if (data->timeout > 0) {
829 /* To pass a pid_t data through the timer infrastructure
830 * without memory allocation, a pid_t data becomes typecast
831 * to intptr_t and void *(64bit) type. */
832 cond_timeout_id = g_timeout_add(
834 states[state].timeout_cb,
835 (void*)((intptr_t)pid));
836 if (!cond_timeout_id)
837 _E("Failed to register display timer.");
840 holdkey_block = GET_COND_FLAG(data->cond) & PM_FLAG_BLOCK_HOLDKEY;
842 tmp = find_node(state, pid);
844 tmp = add_node(state, pid, cond_timeout_id, holdkey_block);
846 _E("Failed to acquire lock, state: %d, pid: %d.", state, pid);
850 update_lock_timer(data, tmp, cond_timeout_id);
851 tmp->holdkey_block = holdkey_block;
854 if (state == S_LCDOFF)
855 set_process_active(true, pid);
857 _I("[%s] locked by %5d with %u ms", states[state].name, pid, data->timeout);
859 if (pid < INTERNAL_LOCK_BASE) {
861 if (state == S_LCDOFF)
862 lock_type = PM_LCDOFF_STR;
863 else if (state == S_LCDDIM)
864 lock_type = PM_LCDDIM_STR;
865 else if (state == S_NORMAL)
866 lock_type = PM_LCDON_STR;
869 /* power lock signal */
870 ret = gdbus_signal_emit(NULL,
871 DEVICED_PATH_DISPLAY,
872 DEVICED_INTERFACE_DISPLAY,
874 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
876 _E("Failed to send dbus signal PowerLock.");
879 _SD("be requested LOCK info pname(%s), holdkeyblock(%d) flags(%d)",
880 pname, holdkey_block, flags);
881 set_lock_time(pid, pname, state);
883 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
886 static void proc_condition_unlock(PMMsg *data)
888 pid_t pid = data->pid;
891 char pname[PATH_MAX];
895 const char *lock_type = NULL;
897 state = GET_COND_STATE(data->cond);
901 flags = GET_COND_FLAG(data->cond);
902 display_misc_get_process_name(pid, pname);
904 tmp = find_node(state, pid);
905 del_node(state, tmp);
907 if (state == S_LCDOFF)
908 set_process_active(false, pid);
910 _I("[%s] unlocked by %5d", states[state].name, pid);
912 if (pid < INTERNAL_LOCK_BASE) {
914 if (state == S_LCDOFF)
915 lock_type = PM_LCDOFF_STR;
916 else if (state == S_LCDDIM)
917 lock_type = PM_LCDDIM_STR;
918 else if (state == S_NORMAL)
919 lock_type = PM_LCDON_STR;
922 /* power unlock signal */
923 ret = gdbus_signal_emit(NULL,
924 DEVICED_PATH_DISPLAY,
925 DEVICED_INTERFACE_DISPLAY,
927 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
929 _E("Failed to send dbus signal PowerUnlock.");
933 _SD("be requested UNLOCK info pname(%s) flag(%d)", pname, flags);
934 set_unlock_time(pid, state);
936 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
939 static int proc_condition(PMMsg *data)
943 if (IS_COND_REQUEST_LOCK(data->cond))
944 proc_condition_lock(data);
946 if (IS_COND_REQUEST_UNLOCK(data->cond))
947 proc_condition_unlock(data);
949 if (!display_conf.timeout_enable)
952 flags = GET_COND_FLAG(data->cond);
954 /* guard time for suspend */
955 if (get_pm_cur_state() == S_LCDOFF)
956 reset_timeout(states[S_LCDOFF].timeout);
958 if (flags & PM_FLAG_RESET_TIMER)
959 reset_timeout(states[get_pm_cur_state()].timeout);
963 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
968 void update_lcdoff_source(int source)
973 case VCONFKEY_PM_LCDOFF_BY_TIMEOUT:
974 _I("LCD OFF by timeout.");
976 case VCONFKEY_PM_LCDOFF_BY_POWERKEY:
977 _I("LCD OFF by powerkey.");
980 _E("Invalid value(%d).", source);
983 ret = vconf_set_int(VCONFKEY_PM_LCDOFF_SOURCE, source);
985 _E("Failed to set vconf value for lcd off source: %d", vconf_get_ext_errno());
988 void print_info(int fd)
991 char buf[PATH_MAX + 255];
994 char pname[PATH_MAX];
1002 snprintf(buf, sizeof(buf),
1003 "\n==========================================="
1004 "===========================\n");
1005 ret = write(fd, buf, strlen(buf));
1007 _E("Write() failed: %d", errno);
1008 snprintf(buf, sizeof(buf), "Timeout Info: Run[%dms] Dim[%dms] Off[%dms]\n",
1009 states[S_NORMAL].timeout,
1010 states[S_LCDDIM].timeout, states[S_LCDOFF].timeout);
1011 ret = write(fd, buf, strlen(buf));
1013 _E("Write() failed: %d", errno);
1015 snprintf(buf, sizeof(buf), "Tran. Locked : %s %s %s\n",
1016 (get_trans_condition() & MASK_NORMAL) ? states[S_NORMAL].name : "-",
1017 (get_trans_condition() & MASK_DIM) ? states[S_LCDDIM].name : "-",
1018 (get_trans_condition() & MASK_OFF) ? states[S_LCDOFF].name : "-");
1019 ret = write(fd, buf, strlen(buf));
1021 _E("Write() failed: %d", errno);
1023 snprintf(buf, sizeof(buf), "Current State: %s\n",
1024 states[get_pm_cur_state()].name);
1025 ret = write(fd, buf, strlen(buf));
1027 _E("Write() failed: %d", errno);
1029 snprintf(buf, sizeof(buf), "Current Lock Conditions: \n");
1030 ret = write(fd, buf, strlen(buf));
1032 _E("Write() failed: %d", errno);
1034 for (s_index = S_NORMAL; s_index < S_END; s_index++) {
1035 SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) {
1036 display_misc_get_process_name((pid_t)t->pid, pname);
1037 ctime_r(&t->time, time_buf);
1038 time_buf[strlen(time_buf) - 1] = 0;
1039 snprintf(buf, sizeof(buf),
1040 " %d: [%s] locked by pid %d %s %s\n",
1041 i++, states[s_index].name, t->pid, pname, time_buf);
1042 ret = write(fd, buf, strlen(buf));
1044 _E("Write() failed: %d", errno);
1048 print_lock_info_list(fd);
1050 #ifdef ENABLE_PM_LOG
1051 pm_history_print(fd, 250);
1055 void save_display_log(const char *path)
1062 _D("internal state is saved!");
1065 ctime_r(&now_time, time_buf);
1066 time_buf[strlen(time_buf) - 1] = 0;
1068 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
1070 snprintf(buf, sizeof(buf),
1071 "\npm_state_log now-time : %d(s) %s\n\n",
1072 (int)now_time, time_buf);
1073 ret = write(fd, buf, strlen(buf));
1075 _E("write() failed (%d)", errno);
1077 snprintf(buf, sizeof(buf), "pm_status_flag: %#x\n", get_pm_status_flag());
1078 ret = write(fd, buf, strlen(buf));
1080 _E("write() failed (%d)", errno);
1082 if (disp_plgn->get_lock_screen_state ) {
1083 snprintf(buf, sizeof(buf), "screen lock status : %d\n",
1084 disp_plgn->get_lock_screen_state());
1085 ret = write(fd, buf, strlen(buf));
1087 _E("write() failed (%d)", errno);
1093 fd = open("/dev/console", O_WRONLY);
1100 /* SIGHUP signal handler
1101 * For debug... print info to syslog
1103 static void sig_hup(int signo)
1105 _I("received sig hub %d", signo);
1110 int check_lcdoff_direct(void)
1112 int ret, lock, cradle;
1115 if (get_pm_old_state() != S_NORMAL)
1118 if (get_pm_cur_state() != S_LCDDIM)
1121 if (!display_conf.dimming)
1124 lock = __get_lock_screen_state();
1125 if (lock != VCONFKEY_IDLE_LOCK)
1128 hdmi_state = extcon_get_status(EXTCON_CABLE_HDMI);
1132 ret = vconf_get_int(VCONFKEY_SYSMAN_CRADLE_STATUS, &cradle);
1133 if (ret >= 0 && cradle == DOCK_SOUND)
1136 _E("Failed to get vconf value for cradle status: %d", vconf_get_ext_errno());
1138 _D("Goto LCDOFF direct: lock(%d) hdmi(%d) cradle(%d).", lock, hdmi_state, cradle);
1144 * default transition function
1147 * 3. call enter action function
1149 static int default_trans(int evt)
1151 struct state *st = &states[get_pm_cur_state()];
1152 enum state_t next_state;
1154 display_state_transition_get_next_transition_display_state(get_pm_cur_state(), &next_state, evt);
1156 /* check conditions */
1157 if (st->check && !st->check(get_pm_cur_state(), next_state)) {
1158 /* There is a condition. */
1159 _I("%s locked. Trans to %s failed.", states[get_pm_cur_state()].name,
1160 states[next_state].name);
1164 /* state transition */
1165 set_pm_old_state(get_pm_cur_state());
1166 set_pm_cur_state(next_state);
1167 st = &states[get_pm_cur_state()];
1171 if (get_pm_cur_state() == S_LCDOFF)
1172 update_lcdoff_source(VCONFKEY_PM_LCDOFF_BY_TIMEOUT);
1174 if ((get_pm_cur_state() == S_NORMAL) || (get_pm_cur_state() == S_LCDOFF))
1175 if (set_custom_lcdon_timeout(0) == true)
1176 update_display_time();
1178 if (check_lcdoff_direct() == true) {
1179 /* enter next state directly */
1180 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
1182 if ((get_pm_cur_state() == S_SLEEP)
1183 && (is_emulator() == true || timeout_sleep_support == false))
1186 st->action(st->timeout);
1193 static gboolean lcd_on_expired(void *data)
1195 int lock_state, ret;
1197 if (lock_timeout_id)
1198 lock_timeout_id = 0;
1200 /* check state of lock */
1201 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1202 if (ret > 0 && lock_state == VCONFKEY_IDLE_LOCK)
1203 return G_SOURCE_REMOVE;
1205 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
1207 /* lock screen is not launched yet, but lcd is on */
1208 if (check_lcd_is_on() == false)
1209 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
1211 return G_SOURCE_REMOVE;
1214 static inline void stop_lock_timer(void)
1216 if (lock_timeout_id) {
1217 g_source_remove(lock_timeout_id);
1218 lock_timeout_id = 0;
1222 static void check_lock_screen(void)
1224 int lock_state, ret;
1228 /* check state of lock */
1229 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1230 if (ret < 0 || lock_state == VCONFKEY_IDLE_LOCK)
1233 /* Use time to check lock is done. */
1234 lock_timeout_id = g_timeout_add(display_conf.lock_wait_time,
1235 lcd_on_expired, NULL);
1238 /* default enter action function */
1239 static int default_action(int timeout)
1241 int wakeup_count = -1, pm_cur_state;
1244 static time_t last_update_time = 0;
1245 static int last_timeout = 0;
1246 struct timeval now_tv;
1249 bool lcd_paneloff_mode = false;
1251 if (status != DEVICE_OPS_STATUS_START) {
1252 _E("Display is not started.");
1256 if (get_pm_cur_state() != S_SLEEP) {
1257 if ((get_pm_cur_state() == S_NORMAL) &&
1258 lcdon_tv.tv_sec != 0) {
1259 gettimeofday(&now_tv, NULL);
1260 timeout -= DIFF_TIMEVAL_MS(now_tv, lcdon_tv);
1261 lcdon_tv.tv_sec = 0;
1263 /* set timer with current state timeout */
1264 reset_timeout(timeout);
1266 if (get_pm_cur_state() == S_NORMAL) {
1267 time(&last_update_time);
1268 last_timeout = timeout;
1270 _I("Timout set: %s state %d ms",
1271 states[get_pm_cur_state()].name, timeout);
1275 if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) {
1276 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
1277 power_get_wakeup_reason(), NULL);
1278 set_setting_pmstate(get_pm_cur_state());
1279 pm_cur_state = get_pm_cur_state();
1280 device_notify(DEVICE_NOTIFIER_LCD, (void *)&pm_cur_state);
1283 if ((get_pm_old_state() == S_NORMAL) && (get_pm_cur_state() != S_NORMAL)) {
1285 diff = difftime(now, last_update_time);
1286 _I("S_NORMAL is changed to %s (timeout=%d ms diff=%.0f s).",
1287 states[get_pm_cur_state()].name, last_timeout, diff);
1290 switch (get_pm_cur_state()) {
1293 * normal state : backlight on and restore
1294 * the previous brightness
1296 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
1297 check_lock_screen();
1298 else if (get_pm_old_state() == S_LCDDIM)
1299 display_backlight_update_by_default_brightness();
1301 if (check_lcd_is_on() == false)
1302 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
1306 display_backlight_get_custom_status(&custom_status);
1307 if ((get_pm_old_state() == S_NORMAL) && custom_status) {
1308 display_backlight_get_brightness(&brightness);
1309 display_backlight_set_custom_brightness(brightness);
1311 /* lcd dim state : dim the brightness */
1312 display_backlight_set_brightness_by_dim_brightness();
1314 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
1315 lcd_on_procedure(LCD_DIM, NORMAL_MODE);
1319 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF)) {
1321 /* lcd off state : turn off the backlight */
1322 if (display_panel_get_dpms_cached_state() == DPMS_ON)
1323 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1326 display_panel_get_lcd_paneloff_mode(&lcd_paneloff_mode);
1327 if (display_panel_get_dpms_cached_state() == DPMS_ON
1328 || lcd_paneloff_mode)
1329 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1333 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF))
1336 if (display_panel_get_dpms_cached_state() == DPMS_ON)
1337 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1339 if (!pm_get_power_lock_support()) {
1340 /* sleep state : set system mode to SUSPEND */
1341 if (get_wakeup_count(&wakeup_count) < 0)
1342 _E("Wakeup count read error.");
1344 if (wakeup_count < 0) {
1345 _I("Wakup Event. Can not enter suspend mode.");
1349 if (set_wakeup_count(wakeup_count) < 0) {
1350 _E("Wakeup count write error.");
1360 #ifdef ENABLE_PM_LOG
1361 pm_history_save(PM_LOG_SLEEP, get_pm_cur_state());
1363 power_request_change_state(DEVICED_POWER_STATE_SLEEP, HAL_DEVICE_POWER_TRANSITION_REASON_DISPLAY_OFF_TIMEOUT);
1367 if (!pm_get_power_lock_support()) {
1369 states[get_pm_cur_state()].trans(EVENT_DEVICE);
1375 * default check function
1377 * 0 : can't transit, others : transitable
1379 static int default_check(int curr, int next)
1383 makeup_trans_condition();
1385 trans_cond = get_trans_condition() & MASK_BIT;
1387 if (next == S_NORMAL) /* S_NORMAL is exceptional */
1392 trans_cond = trans_cond & MASK_NORMAL;
1395 trans_cond = trans_cond & MASK_DIM;
1398 trans_cond = trans_cond & MASK_OFF;
1405 if (trans_cond != 0) {
1410 return 1; /* transitable */
1413 static void default_saving_mode(int onoff)
1416 set_pm_status_flag(PWRSV_FLAG);
1418 clear_pm_status_flag(PWRSV_FLAG);
1420 if (get_pm_cur_state() == S_NORMAL)
1421 display_backlight_update_by_default_brightness();
1424 int poll_callback(int condition, PMMsg *data)
1426 static time_t last_t;
1429 if (status != DEVICE_OPS_STATUS_START) {
1430 _E("Display logic is not started.");
1434 if (condition == INPUT_POLL_EVENT) {
1435 if ((get_pm_cur_state() == S_LCDOFF) || (get_pm_cur_state() == S_SLEEP))
1436 _I("Input event signal at Display Off");
1438 if ((last_t != now) ||
1439 (get_pm_cur_state() == S_LCDOFF) ||
1440 (get_pm_cur_state() == S_SLEEP)) {
1441 states[get_pm_cur_state()].trans(EVENT_INPUT);
1446 if (condition == PM_CONTROL_EVENT) {
1447 proc_condition(data);
1449 if (IS_COND_REQUEST_CHANGE(data->cond))
1450 proc_change_state(data->cond, data->pid);
1456 static int update_setting(int key_idx, int val)
1461 case SETTING_TO_NORMAL:
1462 update_display_time();
1463 states[get_pm_cur_state()].trans(EVENT_INPUT);
1465 case SETTING_LOW_BATT:
1466 if (display_misc_is_low_battery_state(val)) {
1467 if (!(get_pm_status_flag() & CHRGR_FLAG))
1468 power_saving_func(true);
1469 set_pm_status_flag(LOWBT_FLAG);
1471 if (get_pm_status_flag() & PWRSV_FLAG)
1472 power_saving_func(false);
1473 clear_pm_status_flag(LOWBT_FLAG);
1474 clear_pm_status_flag(BRTCH_FLAG);
1475 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, false);
1477 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
1480 case SETTING_CHARGING:
1482 if (get_pm_status_flag() & LOWBT_FLAG) {
1483 power_saving_func(false);
1484 clear_pm_status_flag(LOWBT_FLAG);
1486 set_pm_status_flag(CHRGR_FLAG);
1489 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
1492 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
1493 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
1495 if (display_misc_is_low_battery_state(bat_state)) {
1496 power_saving_func(true);
1497 set_pm_status_flag(LOWBT_FLAG);
1499 clear_pm_status_flag(CHRGR_FLAG);
1502 case SETTING_BRT_LEVEL:
1503 if (get_pm_status_flag() & PWRSV_FLAG) {
1504 set_pm_status_flag(BRTCH_FLAG);
1505 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, true);
1507 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
1508 _I("Brightness changed in low battery,"
1509 "escape dim state.");
1511 display_backlight_set_default_brightness(val);
1513 case SETTING_LOCK_SCREEN:
1514 set_lock_screen_state(val);
1515 if (val == VCONFKEY_IDLE_UNLOCK) {
1516 if (CHECK_OPS(keyfilter_ops, backlight_enable))
1517 keyfilter_ops->backlight_enable(false);
1520 /* LCD on if lock screen show before waiting time */
1521 if ((get_pm_cur_state() == S_NORMAL) &&
1522 val == VCONFKEY_IDLE_LOCK &&
1523 display_panel_get_dpms_cached_state() != DPMS_ON &&
1524 is_lcdon_blocked() == LCDON_BLOCK_NONE)
1525 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1527 update_display_time();
1528 if (get_pm_cur_state() == S_NORMAL)
1529 states[get_pm_cur_state()].trans(EVENT_INPUT);
1531 case SETTING_LOCK_SCREEN_BG:
1532 set_lock_screen_bg_state(val);
1533 update_display_time();
1534 if (get_pm_cur_state() == S_NORMAL)
1535 states[get_pm_cur_state()].trans(EVENT_INPUT);
1537 case SETTING_POWER_CUSTOM_BRIGHTNESS:
1538 if (val == VCONFKEY_PM_CUSTOM_BRIGHTNESS_ON)
1539 display_backlight_set_custom_status(true);
1541 display_backlight_set_custom_status(false);
1550 static void check_seed_status(void)
1558 /* Charging check */
1559 if (fp_get_charging_status && (fp_get_charging_status(&tmp) == 0) && (tmp > 0))
1560 set_pm_status_flag(CHRGR_FLAG);
1562 ret = get_setting_brightness(&tmp);
1563 if (ret != 0 || (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS)) {
1564 _I("Failed to read vconf value for brightness.");
1565 brt = display_conf.pm_default_brightness;
1566 if (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS) {
1567 ret = vconf_set_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, brt);
1569 _E("Failed to set vconf value for lcd brightness: %d", vconf_get_ext_errno());
1573 _I("Set brightness(%d) from setting app.", tmp);
1574 display_backlight_set_default_brightness(tmp);
1575 display_backlight_set_brightness(tmp);
1577 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat_state);
1579 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
1580 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
1582 if (display_misc_is_low_battery_state(bat_state)) {
1583 if (!(get_pm_status_flag() & CHRGR_FLAG)) {
1584 power_saving_func(true);
1585 set_pm_status_flag(LOWBT_FLAG);
1589 /* lock screen check */
1590 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1593 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
1595 set_lock_screen_state(lock_state);
1596 if (lock_state == VCONFKEY_IDLE_LOCK) {
1597 states[S_NORMAL].timeout = lock_screen_timeout;
1598 _I("LCD NORMAL timeout(%d ms) is set"
1599 " for lock screen.", lock_screen_timeout);
1605 static void init_lcd_operation(void)
1607 const struct device_ops *ops = NULL;
1609 ops = find_device("display");
1610 if (!check_default(ops))
1611 display_register_dependent_device(ops);
1613 ops = find_device("touchkey");
1614 if (!check_default(ops))
1615 display_register_dependent_device(ops);
1617 ops = find_device("touchscreen");
1618 if (!check_default(ops))
1619 display_register_dependent_device(ops);
1622 static void exit_lcd_operation(void)
1624 display_unregister_dependent_device();
1636 static const char *errMSG[INIT_END] = {
1637 [INIT_SETTING] = "setting init error",
1638 [INIT_INTERFACE] = "lowlevel interface(sysfs or others) init error",
1639 [INIT_POLL] = "input devices poll init error",
1640 [INIT_FIFO] = "FIFO poll init error",
1641 [INIT_DBUS] = "d-bus init error",
1644 int set_lcd_timeout(int on, int dim, int holdkey_block, const char *name)
1646 if (on == 0 && dim == 0) {
1647 _I("LCD timeout changed: default setting");
1648 custom_normal_timeout = custom_dim_timeout = 0;
1649 } else if (on < 0 || dim < 0) {
1650 _E("Failed to set value(on=%d dim=%d).", on, dim);
1653 _I("LCD timeout changed: on=%ds dim=%ds", on, dim);
1654 custom_normal_timeout = SEC_TO_MSEC(on);
1655 custom_dim_timeout = SEC_TO_MSEC(dim);
1657 /* Apply new backlight time */
1658 update_display_time();
1659 if (get_pm_cur_state() == S_NORMAL)
1660 states[get_pm_cur_state()].trans(EVENT_INPUT);
1662 if (holdkey_block) {
1663 display_lock_set_custom_holdkey_block(true);
1664 _I("Hold key disabled.");
1666 display_lock_set_custom_holdkey_block(false);
1667 _I("Hold key enabled.");
1670 if (custom_change_name) {
1671 free(custom_change_name);
1672 custom_change_name = 0;
1675 if (custom_normal_timeout == 0 &&
1676 custom_dim_timeout == 0 &&
1680 custom_change_name = strndup(name, strlen(name));
1681 if (!custom_change_name) {
1682 _E("Failed to malloc.");
1683 custom_normal_timeout = custom_dim_timeout = 0;
1684 display_lock_set_custom_holdkey_block(false);
1691 void reset_lcd_timeout(GDBusConnection *conn,
1692 const gchar *sender,
1693 const gchar *unique_name,
1699 if (!custom_change_name)
1702 if (strcmp(sender, custom_change_name))
1705 _I("reset lcd timeout: Set default timeout. sender=%s", sender);
1707 free(custom_change_name);
1708 custom_change_name = 0;
1709 custom_normal_timeout = custom_dim_timeout = 0;
1710 display_lock_set_custom_holdkey_block(false);
1712 update_display_time();
1713 if (get_pm_cur_state() == S_NORMAL)
1714 states[get_pm_cur_state()].trans(EVENT_INPUT);
1717 static int delayed_init_done(void *data)
1719 static bool done = false;
1728 _I("Booting done, release booting lock.");
1729 if (disp_plgn->pm_unlock_internal) {
1730 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL, PM_SLEEP_MARGIN);
1731 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF, PM_SLEEP_MARGIN);
1737 static gboolean delayed_dpms_init_done(gpointer data)
1741 if (!display_panel_init_dpms())
1742 return G_SOURCE_CONTINUE;
1744 switch (get_pm_cur_state()) {
1747 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1748 if (display_conf.timeout_enable) {
1749 timeout = states[S_NORMAL].timeout;
1750 /* check minimun lcd on time */
1751 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
1752 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
1753 reset_timeout(timeout);
1757 lcd_off_procedure(LCD_OFF_BY_EVENT);
1758 timeout = display_conf.lcdoff_timeout;
1759 reset_timeout(timeout);
1765 return G_SOURCE_REMOVE;
1768 static void add_timer_for_dpms_init(void)
1770 guint id = g_timeout_add(500/* milliseconds */, delayed_dpms_init_done, NULL);
1772 _E("Failed to add display_panel_init_dpms timeout.");
1775 static void init_display_states(void *data)
1777 struct display_plugin *dp = (struct display_plugin *) data;
1778 for(int i = 0; i < S_END; i++)
1779 dp->display_states[i] = &states[i];
1782 * Power manager Main
1785 static int display_probe(void *data)
1788 struct display_plugin *dp = (struct display_plugin *) data;
1791 dp->config = &display_conf;
1792 init_display_states(dp);
1793 setup_display_plugin_backlight_ops(dp);
1794 dp->delete_state_cond = del_state_cond;
1797 * load display service
1798 * if there is no display shared library,
1799 * deviced does not provide any method and function of display.
1801 ret = display_service_load();
1805 /* display_plugin instance initialization */
1806 init_pm_internal(data);
1807 disp_plgn->device_flags_to_string = __device_flags_to_string;
1809 if (battery_plgn->handle) {
1810 fp_get_charging_status = dlsym(battery_plgn->handle, "get_charging_status");
1811 if (!fp_get_charging_status)
1812 _E("Failed to obtain address of get_charging_status");
1814 _I("There is no battery module.");
1819 static int input_init_handler(void)
1821 if (!display_conf.input_support)
1822 remove_device_by_devname("input");
1827 static int power_resume_from_echomem_callback(void *data)
1829 system_wakeup_flag = true;
1830 if (check_wakeup_src() == EVENT_DEVICE)
1831 /* system waked up by devices */
1832 states[get_pm_cur_state()].trans(EVENT_DEVICE);
1834 /* system waked up by user input */
1835 states[get_pm_cur_state()].trans(EVENT_INPUT);
1840 static int poweroff_triggered_callback(void *udata)
1842 int val = (int)(intptr_t) udata;
1845 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
1846 clear_pm_status_flag(PWROFF_FLAG);
1848 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
1849 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
1850 set_pm_status_flag(PWROFF_FLAG);
1857 static void esd_action(void)
1859 const struct device_ops *touchscreen_ops = NULL;
1863 touchscreen_ops = find_device("touchscreen");
1865 if (!check_default(touchscreen_ops))
1866 touchscreen_ops->stop(NORMAL_MODE);
1867 display_panel_set_panel_state_by_off_state(NORMAL_MODE);
1868 display_panel_set_panel_state_by_on_state(NORMAL_MODE);
1869 if (!check_default(touchscreen_ops))
1870 touchscreen_ops->start(NORMAL_MODE);
1873 static void lcd_uevent_changed(struct udev_device *dev)
1875 const char *devpath;
1878 devpath = udev_device_get_devpath(dev);
1882 if (!fnmatch(LCD_ESD_PATH, devpath, 0)) {
1883 action = udev_device_get_action(dev);
1884 if (!strcmp(action, UDEV_CHANGE))
1889 static const struct uevent_handler lcd_uevent_ops = {
1890 .subsystem = LCD_EVENT_SUBSYSTEM,
1891 .uevent_func = lcd_uevent_changed,
1895 static void display_init(void *data)
1898 unsigned int flags = (WITHOUT_STARTNOTI | FLAG_X_DPMS);
1901 _I("Start power manager.");
1903 signal(SIGHUP, sig_hup);
1905 power_saving_func = default_saving_mode;
1907 /* load configutation */
1908 ret = display_load_config(&display_conf);
1910 _W("Failed to load '%s', use default value: %d",
1911 DISPLAY_CONF_FILE, ret);
1913 register_kernel_uevent_control(&lcd_uevent_ops);
1915 register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
1916 register_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
1917 register_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
1918 register_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
1919 register_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
1920 register_notifier(DEVICE_NOTIFIER_POWEROFF_TRIGGERED, poweroff_triggered_callback);
1922 init_save_userlock();
1924 for (i = INIT_SETTING; i < INIT_END; i++) {
1927 ret = init_setting(update_setting);
1929 case INIT_INTERFACE:
1930 if (display_conf.timeout_enable)
1931 get_lcd_timeout_from_settings();
1932 ret = init_sysfs(flags);
1936 ret = input_init_handler();
1938 pm_lock_detector_init();
1942 ret = init_pm_dbus();
1946 _E("Failed to init: %s", errMSG[i]);
1951 if (i == INIT_END) {
1952 display_ops_init(NULL);
1953 #ifdef ENABLE_PM_LOG
1956 init_lcd_operation();
1957 check_seed_status();
1959 /* In smd test, TSP should be turned off if display panel is not existed. */
1960 if (display_panel_get_dpms_cached_state() == -ENOENT) {
1961 _I("Display panel is not existed.");
1962 lcd_direct_control(DPMS_OFF, NORMAL_MODE);
1963 exit_lcd_operation();
1966 /* wm_ready needs to be checked
1967 * since display manager can be launched later than deviced.
1968 * In the case, display cannot be turned on at the first booting */
1969 // wm_ready = check_wm_ready();
1970 if (display_panel_init_dpms()) {
1971 if (is_lcdon_blocked() != LCDON_BLOCK_NONE)
1972 lcd_off_procedure(LCD_OFF_BY_EVENT);
1974 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1976 add_timer_for_dpms_init();
1979 if (display_conf.lcd_always_on) {
1980 _I("LCD always on.");
1981 display_state_transition_set_transition_table_display_state(S_NORMAL, S_NORMAL, EVENT_TIMEOUT);
1984 if (flags & WITHOUT_STARTNOTI) { /* start without noti */
1985 _I("Start Power managing without noti");
1986 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
1987 HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN, NULL);
1989 * Lock lcd off until booting is done.
1990 * deviced guarantees all booting script is executing.
1991 * Last script of booting unlocks this suspend blocking state.
1993 if (disp_plgn->pm_lock_internal)
1994 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF,
1995 STAY_CUR_STATE, DELAYED_INIT_WATING_TIME);
1997 /* Initial display state right after the booting done */
1998 if (is_lcdon_blocked())
1999 set_pm_cur_state(S_LCDOFF);
2001 set_pm_cur_state(S_NORMAL);
2002 ret = vconf_set_int(VCONFKEY_PM_STATE, get_pm_cur_state());
2004 _E("Failed to set vconf value for pm cur state: %d", vconf_get_ext_errno());
2006 status = DEVICE_OPS_STATUS_START;
2007 if (display_conf.timeout_enable) {
2008 timeout = states[S_NORMAL].timeout;
2009 /* check minimun lcd on time */
2010 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
2011 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
2013 if (disp_plgn->pm_lock_internal)
2014 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL,
2015 STAY_CUR_STATE, timeout);
2020 if (display_conf.input_support)
2021 if (CHECK_OPS(keyfilter_ops, init))
2022 keyfilter_ops->init();
2024 set_display_init_direction(display_conf.display_init_direction);
2028 static void display_exit(void *data)
2032 status = DEVICE_OPS_STATUS_STOP;
2034 /* Set current state to S_NORMAL */
2035 set_pm_cur_state(S_NORMAL);
2036 set_setting_pmstate(get_pm_cur_state());
2037 /* timeout is not needed */
2038 reset_timeout(TIMEOUT_NONE);
2040 if (CHECK_OPS(keyfilter_ops, exit))
2041 keyfilter_ops->exit();
2043 unregister_kernel_uevent_control(&lcd_uevent_ops);
2045 display_ops_exit(NULL);
2047 for (i = i - 1; i >= INIT_SETTING; i--) {
2052 case INIT_INTERFACE:
2056 unregister_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
2057 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
2058 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
2059 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
2060 unregister_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
2066 exit_lcd_operation();
2067 free_lock_info_list();
2069 /* free display service */
2070 display_service_free();
2072 _I("Stop power manager.");
2075 static int display_start(enum device_flags flags)
2077 const struct display_ops *enhance_ops = NULL;
2081 if (flags & NORMAL_MODE) {
2082 if (flags & LCD_PANEL_OFF_MODE)
2084 display_panel_set_panel_state_by_standby_state(true);
2087 display_panel_set_panel_state_by_on_state(flags);
2088 FIND_DISPLAY(enhance_ops, "enhance");
2089 if (enhance_ops && enhance_ops->func)
2090 enhance_ops->func(RESTORE_ENHANCE_OUTDOOR, &on);
2094 /* CORE LOGIC MODE */
2095 if (!(flags & CORE_LOGIC_MODE))
2098 if (status == DEVICE_OPS_STATUS_START)
2101 if (display_probe(NULL) < 0)
2109 static int display_stop(enum device_flags flags)
2112 if (flags & NORMAL_MODE || flags & FORCE_OFF_MODE) {
2113 display_panel_set_panel_state_by_off_state(flags);
2117 /* CORE LOGIC MODE */
2118 if (!(flags & CORE_LOGIC_MODE))
2121 if (status == DEVICE_OPS_STATUS_STOP)
2129 static int display_status(void)
2134 static const struct device_ops display_plugin_device_ops = {
2135 .disable_auto_init = true,
2136 DECLARE_NAME_LEN("display-plugin"),
2137 .probe = display_probe,
2138 .init = display_init,
2139 .exit = display_exit,
2140 .start = display_start,
2141 .stop = display_stop,
2142 .status = display_status,
2145 DEVICE_OPS_REGISTER(&display_plugin_device_ops)
2147 static void __CONSTRUCTOR__ initialize(void)
2149 disp_plgn = get_var_display_plugin();
2151 _E("Failed to get display plugin variable.");
2153 backlight_ops = get_var_backlight_ops();
2155 _E("Failed to get backlight operator variable.");
2157 battery_plgn = get_var_battery_plugin();
2159 _E("Failed to get battery plugin variable.");