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 "display-signal.h"
66 #include "display-lock.h"
67 #include "display-backlight.h"
68 #include "display-misc.h"
69 #include "display-panel.h"
70 #include "display-config.h"
71 #include "display-state-transition.h"
72 #include "shared/plugin.h"
75 * @addtogroup POWER_MANAGER
79 #define LOCK_SCREEN_INPUT_TIMEOUT 10000
80 #define LOCK_SCREEN_CONTROL_TIMEOUT 5000
81 #define ALWAYS_ON_TIMEOUT 360000000
83 #define GESTURE_STR "gesture"
84 #define POWER_KEY_STR "powerkey"
85 #define TOUCH_STR "touch"
86 #define EVENT_STR "event"
87 #define TIMEOUT_STR "timeout"
88 #define PROXI_STR "proximity"
89 #define PALM_STR "palm"
90 #define UNKNOWN_STR "unknown"
92 extern void init_save_userlock(void);
94 static struct display_plugin *disp_plgn;
95 static struct display_backlight_ops *backlight_ops;
96 static struct battery_plugin *battery_plgn;
97 static int (*fp_get_charging_status) (int *val);
99 static void (*power_saving_func) (int onoff);
100 static enum device_ops_status status = DEVICE_OPS_STATUS_UNINIT;
102 static guint timeout_src_id;
103 static int system_wakeup_flag = false;
104 static unsigned int custom_normal_timeout = 0;
105 static unsigned int custom_dim_timeout = 0;
106 static char *custom_change_name;
107 static guint lock_timeout_id;
108 static int lock_screen_timeout = LOCK_SCREEN_INPUT_TIMEOUT;
109 static struct timeval lcdon_tv;
111 * The two variables(lcdon_broadcast, pmstate_suspend) must be set initial
112 * state because it should be sent from previous state at booting time.
114 static bool lcdon_broadcast = true;
116 static bool touch_blocked = false;
118 /* default transition, action fuctions */
119 static int default_trans(int evt);
120 static int default_action(int timeout);
121 static int default_check(int curr, int next);
123 static gboolean del_normal_cond(void *data);
124 static gboolean del_dim_cond(void *data);
125 static gboolean del_off_cond(void *data);
127 static int default_proc_change_state(unsigned int cond, pid_t pid);
128 static int (*proc_change_state)(unsigned int cond, pid_t pid) = default_proc_change_state;
130 static struct state states[S_END] = {
131 { S_START, "S_START", NULL, NULL, NULL, NULL },
132 { S_NORMAL, "S_NORMAL", default_trans, default_action, default_check, del_normal_cond },
133 { S_LCDDIM, "S_LCDDIM", default_trans, default_action, default_check, del_dim_cond },
134 { S_LCDOFF, "S_LCDOFF", default_trans, default_action, default_check, del_off_cond },
135 { S_STANDBY, "S_STANDBY", NULL, NULL, NULL, NULL },
136 { S_SLEEP, "S_SLEEP", default_trans, default_action, default_check, NULL },
137 { S_POWEROFF, "S_POWEROFF", NULL, NULL, NULL, NULL },
140 #define SHIFT_UNLOCK 4
141 #define SHIFT_CHANGE_STATE 7
142 #define CHANGE_STATE_BIT 0xF00 /* 1111 0000 0000 */
143 #define SHIFT_LOCK_FLAG 16
144 #define SHIFT_CHANGE_TIMEOUT 20
145 #define CUSTOM_TIMEOUT_BIT 0x1
146 #define CUSTOM_HOLDKEY_BIT 0x2
147 #define HOLD_KEY_BLOCK_BIT 0x1
148 #define TIMEOUT_NONE (-1)
150 #define S_COVER_TIMEOUT 8000
151 #define GET_HOLDKEY_BLOCK_STATE(x) ((x >> SHIFT_LOCK_FLAG) & HOLD_KEY_BLOCK_BIT)
152 #define DELAYED_INIT_WATING_TIME 60000 /* 1 minute */
154 #define LOCK_SCREEN_WATING_TIME 300 /* 0.3 second */
155 #define LONG_PRESS_INTERVAL 2 /* 2 seconds */
156 #define SAMPLING_INTERVAL 1 /* 1 sec */
157 #define BRIGHTNESS_CHANGE_STEP 10
158 #define LCD_ALWAYS_ON 0
159 #define ACCEL_SENSOR_ON 1
160 #define CONTINUOUS_SAMPLING 1
161 #define LCDOFF_TIMEOUT 300 /* milli second */
163 #define DIFF_TIMEVAL_MS(a, b) \
164 (((a.tv_sec * 1000000 + a.tv_usec) - \
165 (b.tv_sec * 1000000 + b.tv_usec)) \
168 static struct display_config display_conf = {
169 .lock_wait_time = LOCK_SCREEN_WATING_TIME,
170 .longpress_interval = LONG_PRESS_INTERVAL,
171 .lightsensor_interval = SAMPLING_INTERVAL,
172 .lcdoff_timeout = LCDOFF_TIMEOUT,
173 .pm_default_brightness = 80,
174 .brightness_change_step = BRIGHTNESS_CHANGE_STEP,
175 .lcd_always_on = LCD_ALWAYS_ON,
177 .framerate_app = {0, 0, 0, 0},
178 .control_display = 0,
179 .powerkey_doublepress = 0,
180 .accel_sensor_on = ACCEL_SENSOR_ON,
181 .continuous_sampling = CONTINUOUS_SAMPLING,
182 .timeout_enable = true,
183 .input_support = true,
184 .display_init_direction = DISPLAY_INIT_DIRECTION_HORIZONTAL,
185 .lockcheck_timeout = 600,
186 .aod_enter_level = 40,
188 .touch_wakeup = false,
189 .display_on_usb_conn_changed = true,
190 .display_dpms_type = DISPLAY_DPMS_TYPE_WINDOW_MANAGER,
193 struct display_function_info display_info = {
194 .update_auto_brightness = NULL,
195 .set_autobrightness_min = NULL,
196 .reset_autobrightness_min = NULL,
197 .face_detection = NULL,
200 inline const struct display_config* get_var_display_config()
202 return &display_conf;
205 inline struct state* state_st(enum state_t state)
207 return &states[state];
210 guint get_transition_timer(void)
212 return timeout_src_id;
215 static const char* __device_flags_to_string(enum device_flags flags)
217 if (flags & LCD_ON_BY_GESTURE)
219 else if (flags & (LCD_ON_BY_POWER_KEY | LCD_OFF_BY_POWER_KEY))
220 return POWER_KEY_STR;
221 else if (flags & (LCD_ON_BY_EVENT | LCD_OFF_BY_EVENT))
223 else if (flags & LCD_ON_BY_TOUCH)
225 else if (flags & LCD_OFF_BY_TIMEOUT)
231 static unsigned long get_lcd_on_flags(void)
233 unsigned long flags = NORMAL_MODE;
234 bool lcd_paneloff_mode = false;
236 display_panel_get_lcd_paneloff_mode(&lcd_paneloff_mode);
237 if (lcd_paneloff_mode)
238 flags |= LCD_PANEL_OFF_MODE;
243 bool touch_event_blocked(void)
245 return touch_blocked;
248 void lcd_on_procedure(int state, enum device_flags flag)
250 unsigned long flags = get_lcd_on_flags();
254 * Display on procedure
256 * step 2. broadcast lcd on signal with cause
257 * step 3. set brightness
258 * step 4. set pmstate of vconf
259 * step 5. display on operate
261 * - b. TSP(touch screen) and touchkey enable
262 * step 6. broadcast lcd on complete signal
263 * step 7. key backlight enable
267 _I("[lcdstep] 0x%lx", flags);
269 /* send LCDOn dbus signal */
270 if (!lcdon_broadcast)
271 broadcast_lcd_on(SIGNAL_PRE, flags);
273 /* Update brightness level */
274 if (state == LCD_DIM)
275 display_backlight_set_brightness_by_dim_brightness();
276 else if (state == LCD_NORMAL)
277 display_backlight_update_by_default_brightness();
279 if (state == LCD_NORMAL)
280 set_setting_pmstate(S_NORMAL);
281 else if (state == LCD_DIM)
282 set_setting_pmstate(S_LCDDIM);
284 display_start_dependent_device(flags);
286 if (!lcdon_broadcast) {
287 broadcast_lcd_on(SIGNAL_POST, flags);
288 lcdon_broadcast = true;
291 if (CHECK_OPS(keyfilter_ops, backlight_enable))
292 keyfilter_ops->backlight_enable(true);
294 touch_blocked = false;
297 static unsigned long get_lcd_off_flags(void)
299 unsigned long flags = NORMAL_MODE;
300 bool stay_touchscreen_off = false;
302 display_misc_get_stay_touchscreen_off(&stay_touchscreen_off);
303 if (stay_touchscreen_off)
304 flags |= TOUCH_SCREEN_OFF_MODE;
309 inline void lcd_off_procedure(enum device_flags flag)
311 unsigned long flags = get_lcd_off_flags();
315 * Display off procedure
316 * step 0. enhance mode off using nofity (e.g mdnie, HBM, LBM)
317 * step 1. broadcast lcd off signal with cause
318 * step 2. set pmstate of vconf
319 * step 3. display off operate
321 * - b. TSP(touch screen) and touchkey disable
322 * step 4. broadcast lcd off complete siganl
323 * step 5. enter doze mode if it is enabled
325 _I("[lcdstep] 0x%lx", flags);
329 device_notify(DEVICE_NOTIFIER_LCD_OFF, NULL);
331 if (lcdon_broadcast) {
332 broadcast_lcd_off(SIGNAL_PRE, flags);
333 lcdon_broadcast = false;
338 touch_blocked = true;
340 set_setting_pmstate(S_LCDOFF);
342 if (CHECK_OPS(keyfilter_ops, backlight_enable))
343 keyfilter_ops->backlight_enable(false);
345 display_stop_dependent_device(flags);
347 broadcast_lcd_off(SIGNAL_POST, flags);
348 device_notify(DEVICE_NOTIFIER_LCD_OFF_COMPLETE, NULL);
353 static void del_state_cond(void *data, enum state_t state)
355 PmLockNode *tmp = NULL;
361 /* A passed data is a pid_t type data, not a 64bit data. */
362 pid = (pid_t)((intptr_t)data);
363 _I("delete prohibit %s condition by timeout (%d)", states[state].name, pid);
365 if (pid == INTERNAL_LOCK_AMBIENT)
366 ambient_check_invalid_state(pid);
368 tmp = find_node(state, pid);
369 del_node(state, tmp);
370 set_unlock_time(pid, state);
372 /* Change state only when the two conditions below are satisfied.
373 * 1. There should be no running state-transition timer
374 * 2. Released lock is one of the pm_cur_state's lock
375 * This emulates already expired transition timer */
376 if (!timeout_src_id && get_pm_cur_state() == state)
377 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
379 if (state == S_LCDOFF)
380 set_process_active(false, pid);
383 static gboolean del_normal_cond(void *data)
385 del_state_cond(data, S_NORMAL);
386 return G_SOURCE_REMOVE;
389 static gboolean del_dim_cond(void *data)
391 del_state_cond(data, S_LCDDIM);
392 return G_SOURCE_REMOVE;
395 static gboolean del_off_cond(void *data)
397 del_state_cond(data, S_LCDOFF);
398 return G_SOURCE_REMOVE;
401 /* timeout handler */
402 gboolean timeout_handler(void *data)
404 _I("Time out state %s", states[get_pm_cur_state()].name);
406 if (timeout_src_id) {
407 g_source_remove(timeout_src_id);
411 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
412 return G_SOURCE_REMOVE;
415 void reset_timeout(int timeout)
417 enum state_t next_state;
419 if (!display_conf.timeout_enable)
422 if ((get_pm_cur_state() == S_LCDOFF)
423 && (is_emulator() == true || timeout_sleep_support == false))
426 _I("Reset timeout(%d ms) pm_cur_state(%d).", timeout, get_pm_cur_state());
427 if (timeout_src_id != 0) {
428 g_source_remove(timeout_src_id);
432 display_state_transition_get_next_transition_display_state(get_pm_cur_state(), &next_state, EVENT_TIMEOUT);
433 if (next_state == get_pm_cur_state())
437 timeout_src_id = g_timeout_add(timeout,
438 timeout_handler, NULL);
439 else if (timeout == 0)
440 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
443 /* get configurations from setting */
444 static int get_lcd_timeout_from_settings(void)
449 for (i = 0; i < S_END; i++) {
450 switch (states[i].state) {
452 get_run_timeout(&val);
455 get_dim_timeout(&val);
458 val = display_conf.lcdoff_timeout;
461 /* This state doesn't need to set time out. */
466 states[i].timeout = val;
468 _I("State(%s) timeout(%d) ms", states[i].name,
475 static void update_display_time(void)
477 int run_timeout, val;
479 /* first priority : custom timeout */
480 if (custom_normal_timeout > 0) {
481 states[S_NORMAL].timeout = custom_normal_timeout;
482 states[S_LCDDIM].timeout = custom_dim_timeout;
483 _I("CUSTOM : timeout is set by normal(%d ms), dim(%d ms)",
484 custom_normal_timeout, custom_dim_timeout);
488 /* second priority : lock state */
489 if ((__get_lock_screen_state() == VCONFKEY_IDLE_LOCK) &&
490 !get_lock_screen_bg_state()) {
491 /* timeout is different according to key or event. */
492 states[S_NORMAL].timeout = lock_screen_timeout;
493 _I("LOCK: Timeout(%d ms) is set by normal.",
494 lock_screen_timeout);
498 /* default setting */
499 get_run_timeout(&run_timeout);
502 * if the run_timeout is zero, it regards AlwaysOn state
504 if (run_timeout == 0 || display_conf.lcd_always_on) {
505 run_timeout = ALWAYS_ON_TIMEOUT;
506 _I("LCD always on.");
509 states[S_NORMAL].timeout = run_timeout;
511 get_dim_timeout(&val);
512 states[S_LCDDIM].timeout = val;
514 _I("Normal: NORMAL timeout is set by %d ms", states[S_NORMAL].timeout);
515 _I("Normal: DIM timeout is set by %d ms", states[S_LCDDIM].timeout);
518 static void update_display_locktime(int time)
520 lock_screen_timeout = time;
521 update_display_time();
524 void set_dim_state(bool on)
526 _I("Dim state is %d.", on);
527 update_display_time();
528 states[get_pm_cur_state()].trans(EVENT_INPUT);
531 void lcd_on_direct(enum device_flags flags)
533 enum hal_device_power_transition_reason reason;
535 if (flags & LCD_ON_BY_POWER_KEY)
536 reason = HAL_DEVICE_POWER_TRANSITION_REASON_POWER_KEY;
537 else if (flags & LCD_ON_BY_TOUCH)
538 reason = HAL_DEVICE_POWER_TRANSITION_REASON_TOUCH_SCREEN;
540 reason = HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN;
542 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL, reason, NULL);
543 set_pm_cur_state(S_NORMAL);
545 _D("lcd is on directly");
546 gettimeofday(&lcdon_tv, NULL);
547 lcd_on_procedure(LCD_NORMAL, flags);
549 update_display_locktime(LOCK_SCREEN_INPUT_TIMEOUT);
552 static inline bool check_lcd_is_on(void)
554 if (display_panel_get_dpms_cached_state() != DPMS_ON)
560 static gboolean timer_refresh_cb(gpointer data)
564 /* state transition */
565 set_pm_old_state(get_pm_cur_state());
566 set_pm_cur_state(S_NORMAL);
567 st = &states[get_pm_cur_state()];
570 st->action(st->timeout);
575 int custom_lcdon(int timeout)
582 if (check_lcd_is_on() == false)
583 lcd_on_direct(LCD_ON_BY_GESTURE);
585 _I("Custom lcd on timeout(%d ms).", timeout);
586 if (set_custom_lcdon_timeout(timeout) == true)
587 update_display_time();
589 /* state transition */
590 set_pm_old_state(get_pm_cur_state());
591 set_pm_cur_state(S_NORMAL);
592 st = &states[get_pm_cur_state()];
596 st->action(st->timeout);
598 g_idle_add(timer_refresh_cb, NULL);
603 int custom_lcdoff(enum device_flags flag)
607 check_processes(S_NORMAL);
608 check_processes(S_LCDDIM);
610 /* check holdkey block flag in lock node */
611 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
613 * When another proccess is normal lock, device is received call then,
614 * call app can be changed to lcd state by proximity.
615 * If proximity is near then normal lock will be unlocked.
617 if (flag & LCD_OFF_BY_PROXIMITY) {
618 _I("custom lcd off by proximity, delete normal lock");
619 delete_condition(S_NORMAL);
621 _I("skip custom lcd off");
626 _I("custom lcd off by flag(%d)", flag);
627 if (display_panel_get_dpms_cached_state() == DPMS_ON)
628 lcd_off_procedure(flag);
630 if (set_custom_lcdon_timeout(0) == true)
631 update_display_time();
633 /* state transition */
634 set_pm_old_state(get_pm_cur_state());
635 set_pm_cur_state(S_LCDOFF);
636 st = &states[get_pm_cur_state()];
640 st->action(st->timeout);
645 int display_on_by_reason(const char *reason, int timeout)
651 str_len = strlen(reason);
653 if (!strncmp(reason, GESTURE_STR, str_len))
654 flag = LCD_ON_BY_GESTURE;
655 else if (!strncmp(reason, EVENT_STR, str_len))
656 flag = LCD_ON_BY_EVENT;
658 _E("Reason is unknown(%s)", reason);
663 _E("Cannot setting timeout %d", timeout);
667 if (check_lcd_is_on() == false)
670 _I("platform lcd on by %s (%d ms)", reason, timeout);
671 if (set_custom_lcdon_timeout(timeout) == true)
672 update_display_time();
674 /* state transition */
675 set_pm_old_state(get_pm_cur_state());
676 set_pm_cur_state(S_NORMAL);
677 st = &states[get_pm_cur_state()];
681 st->action(st->timeout);
686 int display_off_by_reason(const char *reason)
692 str_len = strlen(reason);
694 if (!strncmp(reason, GESTURE_STR, str_len)) {
695 check_processes(S_NORMAL);
696 check_processes(S_LCDDIM);
698 /* check holdkey block flag in lock node */
699 if (check_lock_condition(S_NORMAL) || check_lock_condition(S_LCDDIM)) {
700 _I("skip platform lcd off by gesture");
703 flag = LCD_OFF_BY_GESTURE;
704 } else if (!strncmp(reason, PALM_STR, str_len)) {
705 delete_condition(S_NORMAL);
706 delete_condition(S_LCDDIM);
708 flag = LCD_OFF_BY_PALM;
710 _E("Reason is unknown(%s)", reason);
714 _I("platform lcd off by %s", reason);
715 if (display_panel_get_dpms_cached_state() == DPMS_ON)
716 lcd_off_procedure(flag);
718 /* state transition */
719 set_pm_old_state(get_pm_cur_state());
720 set_pm_cur_state(S_LCDOFF);
721 st = &states[get_pm_cur_state()];
725 st->action(st->timeout);
730 static void default_proc_change_state_action(enum state_t next, int timeout)
734 set_pm_old_state(get_pm_cur_state());
735 set_pm_cur_state(next);
737 st = &states[get_pm_cur_state()];
739 if (st && st->action) {
741 st->action(st->timeout);
747 static int default_proc_change_state(unsigned int cond, pid_t pid)
751 next = GET_COND_STATE(cond);
752 _I("Change process(%d) state to %s.", pid, states[next].name);
756 if (check_lcd_is_on() == false)
757 lcd_on_direct(LCD_ON_BY_EVENT);
758 update_display_locktime(LOCK_SCREEN_CONTROL_TIMEOUT);
759 default_proc_change_state_action(next, -1);
762 default_proc_change_state_action(next, -1);
765 if (display_panel_get_dpms_cached_state() == DPMS_ON)
766 lcd_off_procedure(LCD_OFF_BY_EVENT);
767 if (set_custom_lcdon_timeout(0))
768 update_display_time();
769 default_proc_change_state_action(next, -1);
772 _I("Dangerous requests.");
773 /* at first LCD_OFF and then goto sleep */
774 /* state transition */
775 default_proc_change_state_action(S_LCDOFF, TIMEOUT_NONE);
776 delete_condition(S_NORMAL);
777 delete_condition(S_LCDDIM);
778 delete_condition(S_LCDOFF);
779 if (lcdon_broadcast) {
780 _I("broadcast lcd off signal at non-lcd device");
781 broadcast_lcd_off(SIGNAL_PRE, 0);
782 broadcast_lcd_off(SIGNAL_POST, 0);
784 default_proc_change_state_action(S_SLEEP, TIMEOUT_NONE);
794 static void proc_condition_lock(PMMsg *data)
797 guint cond_timeout_id = 0;
798 char pname[PATH_MAX];
799 pid_t pid = data->pid;
801 int holdkey_block, ret;
804 const char *lock_type = NULL;
806 state = GET_COND_STATE(data->cond);
807 if (state == S_START)
810 flags = GET_COND_FLAG(data->cond);
811 display_misc_get_process_name(pid, pname);
813 if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) &&
814 (pm_get_power_lock() == POWER_UNLOCK))
815 proc_change_state(data->cond, INTERNAL_LOCK_PM);
817 if (data->timeout > 0) {
818 /* To pass a pid_t data through the timer infrastructure
819 * without memory allocation, a pid_t data becomes typecast
820 * to intptr_t and void *(64bit) type. */
821 cond_timeout_id = g_timeout_add(
823 states[state].timeout_cb,
824 (void*)((intptr_t)pid));
825 if (!cond_timeout_id)
826 _E("Failed to register display timer.");
829 holdkey_block = GET_COND_FLAG(data->cond) & PM_FLAG_BLOCK_HOLDKEY;
831 tmp = find_node(state, pid);
833 tmp = add_node(state, pid, cond_timeout_id, holdkey_block);
835 _E("Failed to acquire lock, state: %d, pid: %d.", state, pid);
839 update_lock_timer(data, tmp, cond_timeout_id);
840 tmp->holdkey_block = holdkey_block;
843 if (state == S_LCDOFF)
844 set_process_active(true, pid);
846 _I("[%s] locked by %5d with %u ms", states[state].name, pid, data->timeout);
848 if (pid < INTERNAL_LOCK_BASE) {
850 if (state == S_LCDOFF)
851 lock_type = PM_LCDOFF_STR;
852 else if (state == S_LCDDIM)
853 lock_type = PM_LCDDIM_STR;
854 else if (state == S_NORMAL)
855 lock_type = PM_LCDON_STR;
858 /* power lock signal */
859 ret = gdbus_signal_emit(NULL,
860 DEVICED_PATH_DISPLAY,
861 DEVICED_INTERFACE_DISPLAY,
863 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
865 _E("Failed to send dbus signal PowerLock.");
869 _SD("be requested LOCK info pname(%s), holdkeyblock(%d) flags(%d)",
870 pname, holdkey_block, flags);
871 set_lock_time(pid, pname, state);
873 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
876 static void proc_condition_unlock(PMMsg *data)
878 pid_t pid = data->pid;
881 char pname[PATH_MAX];
885 const char *lock_type = NULL;
887 state = GET_COND_STATE(data->cond);
891 flags = GET_COND_FLAG(data->cond);
892 display_misc_get_process_name(pid, pname);
894 tmp = find_node(state, pid);
895 del_node(state, tmp);
897 if (state == S_LCDOFF)
898 set_process_active(false, pid);
900 _I("[%s] unlocked by %5d", states[state].name, pid);
902 if (pid < INTERNAL_LOCK_BASE) {
904 if (state == S_LCDOFF)
905 lock_type = PM_LCDOFF_STR;
906 else if (state == S_LCDDIM)
907 lock_type = PM_LCDDIM_STR;
908 else if (state == S_NORMAL)
909 lock_type = PM_LCDON_STR;
912 /* power unlock signal */
913 ret = gdbus_signal_emit(NULL,
914 DEVICED_PATH_DISPLAY,
915 DEVICED_INTERFACE_DISPLAY,
917 g_variant_new("(sii)", lock_type, pid, (int)data->timeout));
919 _E("Failed to send dbus signal PowerUnlock.");
923 _SD("be requested UNLOCK info pname(%s) flag(%d)", pname, flags);
924 set_unlock_time(pid, state);
926 device_notify(DEVICE_NOTIFIER_DISPLAY_LOCK, (void *)&value);
929 static int proc_condition(PMMsg *data)
933 if (IS_COND_REQUEST_LOCK(data->cond))
934 proc_condition_lock(data);
936 if (IS_COND_REQUEST_UNLOCK(data->cond))
937 proc_condition_unlock(data);
939 if (!display_conf.timeout_enable)
942 flags = GET_COND_FLAG(data->cond);
944 /* guard time for suspend */
945 if (get_pm_cur_state() == S_LCDOFF)
946 reset_timeout(states[S_LCDOFF].timeout);
948 if (flags & PM_FLAG_RESET_TIMER)
949 reset_timeout(states[get_pm_cur_state()].timeout);
953 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
958 void update_lcdoff_source(int source)
963 case VCONFKEY_PM_LCDOFF_BY_TIMEOUT:
964 _I("LCD OFF by timeout.");
966 case VCONFKEY_PM_LCDOFF_BY_POWERKEY:
967 _I("LCD OFF by powerkey.");
970 _E("Invalid value(%d).", source);
973 ret = vconf_set_int(VCONFKEY_PM_LCDOFF_SOURCE, source);
975 _E("Failed to set vconf value for lcd off source: %d", vconf_get_ext_errno());
978 void print_info(int fd)
981 char buf[PATH_MAX + 255];
984 char pname[PATH_MAX];
992 snprintf(buf, sizeof(buf),
993 "\n==========================================="
994 "===========================\n");
995 ret = write(fd, buf, strlen(buf));
997 _E("Write() failed: %d", errno);
998 snprintf(buf, sizeof(buf), "Timeout Info: Run[%dms] Dim[%dms] Off[%dms]\n",
999 states[S_NORMAL].timeout,
1000 states[S_LCDDIM].timeout, states[S_LCDOFF].timeout);
1001 ret = write(fd, buf, strlen(buf));
1003 _E("Write() failed: %d", errno);
1005 snprintf(buf, sizeof(buf), "Tran. Locked : %s %s %s\n",
1006 (get_trans_condition() & MASK_NORMAL) ? states[S_NORMAL].name : "-",
1007 (get_trans_condition() & MASK_DIM) ? states[S_LCDDIM].name : "-",
1008 (get_trans_condition() & MASK_OFF) ? states[S_LCDOFF].name : "-");
1009 ret = write(fd, buf, strlen(buf));
1011 _E("Write() failed: %d", errno);
1013 snprintf(buf, sizeof(buf), "Current State: %s\n",
1014 states[get_pm_cur_state()].name);
1015 ret = write(fd, buf, strlen(buf));
1017 _E("Write() failed: %d", errno);
1019 snprintf(buf, sizeof(buf), "Current Lock Conditions: \n");
1020 ret = write(fd, buf, strlen(buf));
1022 _E("Write() failed: %d", errno);
1024 for (s_index = S_NORMAL; s_index < S_END; s_index++) {
1025 SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) {
1026 display_misc_get_process_name((pid_t)t->pid, pname);
1027 ctime_r(&t->time, time_buf);
1028 time_buf[strlen(time_buf) - 1] = 0;
1029 snprintf(buf, sizeof(buf),
1030 " %d: [%s] locked by pid %d %s %s\n",
1031 i++, states[s_index].name, t->pid, pname, time_buf);
1032 ret = write(fd, buf, strlen(buf));
1034 _E("Write() failed: %d", errno);
1038 print_lock_info_list(fd);
1040 #ifdef ENABLE_PM_LOG
1041 pm_history_print(fd, 250);
1045 void save_display_log(const char *path)
1052 _D("internal state is saved!");
1055 ctime_r(&now_time, time_buf);
1056 time_buf[strlen(time_buf) - 1] = 0;
1058 fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
1060 snprintf(buf, sizeof(buf),
1061 "\npm_state_log now-time : %d(s) %s\n\n",
1062 (int)now_time, time_buf);
1063 ret = write(fd, buf, strlen(buf));
1065 _E("write() failed (%d)", errno);
1067 snprintf(buf, sizeof(buf), "pm_status_flag: %#x\n", get_pm_status_flag());
1068 ret = write(fd, buf, strlen(buf));
1070 _E("write() failed (%d)", errno);
1072 if (disp_plgn->get_lock_screen_state ) {
1073 snprintf(buf, sizeof(buf), "screen lock status : %d\n",
1074 disp_plgn->get_lock_screen_state());
1075 ret = write(fd, buf, strlen(buf));
1077 _E("write() failed (%d)", errno);
1083 fd = open("/dev/console", O_WRONLY);
1090 /* SIGHUP signal handler
1091 * For debug... print info to syslog
1093 static void sig_hup(int signo)
1095 _I("received sig hub %d", signo);
1100 int check_lcdoff_direct(void)
1102 int ret, lock, cradle;
1105 if (get_pm_old_state() != S_NORMAL)
1108 if (get_pm_cur_state() != S_LCDDIM)
1111 if (!display_conf.dimming)
1114 lock = __get_lock_screen_state();
1115 if (lock != VCONFKEY_IDLE_LOCK)
1118 hdmi_state = extcon_get_status(EXTCON_CABLE_HDMI);
1122 ret = vconf_get_int(VCONFKEY_SYSMAN_CRADLE_STATUS, &cradle);
1123 if (ret >= 0 && cradle == DOCK_SOUND)
1126 _E("Failed to get vconf value for cradle status: %d", vconf_get_ext_errno());
1128 _D("Goto LCDOFF direct: lock(%d) hdmi(%d) cradle(%d).", lock, hdmi_state, cradle);
1134 * default transition function
1137 * 3. call enter action function
1139 static int default_trans(int evt)
1141 struct state *st = &states[get_pm_cur_state()];
1142 enum state_t next_state;
1144 display_state_transition_get_next_transition_display_state(get_pm_cur_state(), &next_state, evt);
1146 /* check conditions */
1147 if (st->check && !st->check(get_pm_cur_state(), next_state)) {
1148 /* There is a condition. */
1149 _I("%s locked. Trans to %s failed.", states[get_pm_cur_state()].name,
1150 states[next_state].name);
1154 /* state transition */
1155 set_pm_old_state(get_pm_cur_state());
1156 set_pm_cur_state(next_state);
1157 st = &states[get_pm_cur_state()];
1161 if (get_pm_cur_state() == S_LCDOFF)
1162 update_lcdoff_source(VCONFKEY_PM_LCDOFF_BY_TIMEOUT);
1164 if ((get_pm_cur_state() == S_NORMAL) || (get_pm_cur_state() == S_LCDOFF))
1165 if (set_custom_lcdon_timeout(0) == true)
1166 update_display_time();
1168 if (check_lcdoff_direct() == true) {
1169 /* enter next state directly */
1170 states[get_pm_cur_state()].trans(EVENT_TIMEOUT);
1172 if ((get_pm_cur_state() == S_SLEEP)
1173 && (is_emulator() == true || timeout_sleep_support == false))
1176 st->action(st->timeout);
1183 static gboolean lcd_on_expired(void *data)
1185 int lock_state, ret;
1187 if (lock_timeout_id)
1188 lock_timeout_id = 0;
1190 /* check state of lock */
1191 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1192 if (ret > 0 && lock_state == VCONFKEY_IDLE_LOCK)
1193 return G_SOURCE_REMOVE;
1195 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
1197 /* lock screen is not launched yet, but lcd is on */
1198 if (check_lcd_is_on() == false)
1199 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
1201 return G_SOURCE_REMOVE;
1204 static inline void stop_lock_timer(void)
1206 if (lock_timeout_id) {
1207 g_source_remove(lock_timeout_id);
1208 lock_timeout_id = 0;
1212 static void check_lock_screen(void)
1214 int lock_state, ret;
1218 /* check state of lock */
1219 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1220 if (ret < 0 || lock_state == VCONFKEY_IDLE_LOCK)
1223 /* Use time to check lock is done. */
1224 lock_timeout_id = g_timeout_add(display_conf.lock_wait_time,
1225 lcd_on_expired, NULL);
1228 /* default enter action function */
1229 static int default_action(int timeout)
1231 int wakeup_count = -1, pm_cur_state;
1234 static time_t last_update_time = 0;
1235 static int last_timeout = 0;
1236 struct timeval now_tv;
1239 bool lcd_paneloff_mode = false;
1241 if (status != DEVICE_OPS_STATUS_START) {
1242 _E("Display is not started.");
1246 if (get_pm_cur_state() != S_SLEEP) {
1247 if ((get_pm_cur_state() == S_NORMAL) &&
1248 lcdon_tv.tv_sec != 0) {
1249 gettimeofday(&now_tv, NULL);
1250 timeout -= DIFF_TIMEVAL_MS(now_tv, lcdon_tv);
1251 lcdon_tv.tv_sec = 0;
1253 /* set timer with current state timeout */
1254 reset_timeout(timeout);
1256 if (get_pm_cur_state() == S_NORMAL) {
1257 time(&last_update_time);
1258 last_timeout = timeout;
1260 _I("Timout set: %s state %d ms",
1261 states[get_pm_cur_state()].name, timeout);
1265 if ((get_pm_cur_state() != get_pm_old_state()) && (get_pm_cur_state() != S_SLEEP)) {
1266 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
1267 power_get_wakeup_reason(), NULL);
1268 set_setting_pmstate(get_pm_cur_state());
1269 pm_cur_state = get_pm_cur_state();
1270 device_notify(DEVICE_NOTIFIER_LCD, (void *)&pm_cur_state);
1273 if ((get_pm_old_state() == S_NORMAL) && (get_pm_cur_state() != S_NORMAL)) {
1275 diff = difftime(now, last_update_time);
1276 _I("S_NORMAL is changed to %s (timeout=%d ms diff=%.0f s).",
1277 states[get_pm_cur_state()].name, last_timeout, diff);
1280 switch (get_pm_cur_state()) {
1283 * normal state : backlight on and restore
1284 * the previous brightness
1286 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
1287 check_lock_screen();
1288 else if (get_pm_old_state() == S_LCDDIM)
1289 display_backlight_update_by_default_brightness();
1291 if (check_lcd_is_on() == false)
1292 lcd_on_procedure(LCD_NORMAL, NORMAL_MODE);
1296 display_backlight_get_custom_status(&custom_status);
1297 if ((get_pm_old_state() == S_NORMAL) && custom_status) {
1298 display_backlight_get_brightness(&brightness);
1299 display_backlight_set_custom_brightness(brightness);
1301 /* lcd dim state : dim the brightness */
1302 display_backlight_set_brightness_by_dim_brightness();
1304 if ((get_pm_old_state() == S_LCDOFF) || (get_pm_old_state() == S_SLEEP))
1305 lcd_on_procedure(LCD_DIM, NORMAL_MODE);
1309 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF)) {
1311 /* lcd off state : turn off the backlight */
1312 if (display_panel_get_dpms_cached_state() == DPMS_ON)
1313 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1316 display_panel_get_lcd_paneloff_mode(&lcd_paneloff_mode);
1317 if (display_panel_get_dpms_cached_state() == DPMS_ON
1318 || lcd_paneloff_mode)
1319 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1323 if ((get_pm_old_state() != S_SLEEP) && (get_pm_old_state() != S_LCDOFF))
1326 if (display_panel_get_dpms_cached_state() == DPMS_ON)
1327 lcd_off_procedure(LCD_OFF_BY_TIMEOUT);
1329 if (!pm_get_power_lock_support()) {
1330 /* sleep state : set system mode to SUSPEND */
1331 if (get_wakeup_count(&wakeup_count) < 0)
1332 _E("Wakeup count read error.");
1334 if (wakeup_count < 0) {
1335 _I("Wakup Event. Can not enter suspend mode.");
1339 if (set_wakeup_count(wakeup_count) < 0) {
1340 _E("Wakeup count write error.");
1350 #ifdef ENABLE_PM_LOG
1351 pm_history_save(PM_LOG_SLEEP, get_pm_cur_state());
1353 power_request_change_state(DEVICED_POWER_STATE_SLEEP, HAL_DEVICE_POWER_TRANSITION_REASON_DISPLAY_OFF_TIMEOUT);
1357 if (!pm_get_power_lock_support()) {
1359 states[get_pm_cur_state()].trans(EVENT_DEVICE);
1365 * default check function
1367 * 0 : can't transit, others : transitable
1369 static int default_check(int curr, int next)
1373 makeup_trans_condition();
1375 trans_cond = get_trans_condition() & MASK_BIT;
1377 if (next == S_NORMAL) /* S_NORMAL is exceptional */
1382 trans_cond = trans_cond & MASK_NORMAL;
1385 trans_cond = trans_cond & MASK_DIM;
1388 trans_cond = trans_cond & MASK_OFF;
1395 if (trans_cond != 0) {
1400 return 1; /* transitable */
1403 static void default_saving_mode(int onoff)
1406 set_pm_status_flag(PWRSV_FLAG);
1408 clear_pm_status_flag(PWRSV_FLAG);
1410 if (get_pm_cur_state() == S_NORMAL)
1411 display_backlight_update_by_default_brightness();
1414 int poll_callback(int condition, PMMsg *data)
1416 static time_t last_t;
1419 if (status != DEVICE_OPS_STATUS_START) {
1420 _E("Display logic is not started.");
1424 if (condition == INPUT_POLL_EVENT) {
1425 if ((get_pm_cur_state() == S_LCDOFF) || (get_pm_cur_state() == S_SLEEP))
1426 _I("Input event signal at Display Off");
1428 if ((last_t != now) ||
1429 (get_pm_cur_state() == S_LCDOFF) ||
1430 (get_pm_cur_state() == S_SLEEP)) {
1431 states[get_pm_cur_state()].trans(EVENT_INPUT);
1436 if (condition == PM_CONTROL_EVENT) {
1437 proc_condition(data);
1439 if (IS_COND_REQUEST_CHANGE(data->cond))
1440 proc_change_state(data->cond, data->pid);
1446 static int update_setting(int key_idx, int val)
1451 case SETTING_TO_NORMAL:
1452 update_display_time();
1453 states[get_pm_cur_state()].trans(EVENT_INPUT);
1455 case SETTING_LOW_BATT:
1456 if (display_misc_is_low_battery_state(val)) {
1457 if (!(get_pm_status_flag() & CHRGR_FLAG))
1458 power_saving_func(true);
1459 set_pm_status_flag(LOWBT_FLAG);
1461 if (get_pm_status_flag() & PWRSV_FLAG)
1462 power_saving_func(false);
1463 clear_pm_status_flag(LOWBT_FLAG);
1464 clear_pm_status_flag(BRTCH_FLAG);
1465 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, false);
1467 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
1470 case SETTING_CHARGING:
1472 if (get_pm_status_flag() & LOWBT_FLAG) {
1473 power_saving_func(false);
1474 clear_pm_status_flag(LOWBT_FLAG);
1476 set_pm_status_flag(CHRGR_FLAG);
1479 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
1482 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
1483 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
1485 if (display_misc_is_low_battery_state(bat_state)) {
1486 power_saving_func(true);
1487 set_pm_status_flag(LOWBT_FLAG);
1489 clear_pm_status_flag(CHRGR_FLAG);
1492 case SETTING_BRT_LEVEL:
1493 if (get_pm_status_flag() & PWRSV_FLAG) {
1494 set_pm_status_flag(BRTCH_FLAG);
1495 ret = vconf_set_bool(VCONFKEY_PM_BRIGHTNESS_CHANGED_IN_LPM, true);
1497 _E("Failed to set vconf value for brightness changed in lpm: %d", vconf_get_ext_errno());
1498 _I("Brightness changed in low battery,"
1499 "escape dim state.");
1501 display_backlight_set_default_brightness(val);
1503 case SETTING_LOCK_SCREEN:
1504 set_lock_screen_state(val);
1505 if (val == VCONFKEY_IDLE_UNLOCK) {
1506 if (CHECK_OPS(keyfilter_ops, backlight_enable))
1507 keyfilter_ops->backlight_enable(false);
1510 /* LCD on if lock screen show before waiting time */
1511 if ((get_pm_cur_state() == S_NORMAL) &&
1512 val == VCONFKEY_IDLE_LOCK &&
1513 display_panel_get_dpms_cached_state() != DPMS_ON &&
1514 is_lcdon_blocked() == LCDON_BLOCK_NONE)
1515 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1517 update_display_time();
1518 if (get_pm_cur_state() == S_NORMAL)
1519 states[get_pm_cur_state()].trans(EVENT_INPUT);
1521 case SETTING_LOCK_SCREEN_BG:
1522 set_lock_screen_bg_state(val);
1523 update_display_time();
1524 if (get_pm_cur_state() == S_NORMAL)
1525 states[get_pm_cur_state()].trans(EVENT_INPUT);
1527 case SETTING_POWER_CUSTOM_BRIGHTNESS:
1528 if (val == VCONFKEY_PM_CUSTOM_BRIGHTNESS_ON)
1529 display_backlight_set_custom_status(true);
1531 display_backlight_set_custom_status(false);
1540 static void check_seed_status(void)
1548 /* Charging check */
1549 if (fp_get_charging_status && (fp_get_charging_status(&tmp) == 0) && (tmp > 0))
1550 set_pm_status_flag(CHRGR_FLAG);
1552 ret = get_setting_brightness(&tmp);
1553 if (ret != 0 || (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS)) {
1554 _I("Failed to read vconf value for brightness.");
1555 brt = display_conf.pm_default_brightness;
1556 if (tmp < PM_MIN_BRIGHTNESS || tmp > PM_MAX_BRIGHTNESS) {
1557 ret = vconf_set_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, brt);
1559 _E("Failed to set vconf value for lcd brightness: %d", vconf_get_ext_errno());
1563 _I("Set brightness(%d) from setting app.", tmp);
1564 display_backlight_set_default_brightness(tmp);
1565 display_backlight_set_brightness(tmp);
1567 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat_state);
1569 bat_state = VCONFKEY_SYSMAN_BAT_NORMAL;
1570 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
1572 if (display_misc_is_low_battery_state(bat_state)) {
1573 if (!(get_pm_status_flag() & CHRGR_FLAG)) {
1574 power_saving_func(true);
1575 set_pm_status_flag(LOWBT_FLAG);
1579 /* lock screen check */
1580 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state);
1583 _E("Failed to get vconf value for idle lock state: %d", vconf_get_ext_errno());
1585 set_lock_screen_state(lock_state);
1586 if (lock_state == VCONFKEY_IDLE_LOCK) {
1587 states[S_NORMAL].timeout = lock_screen_timeout;
1588 _I("LCD NORMAL timeout(%d ms) is set"
1589 " for lock screen.", lock_screen_timeout);
1595 static void init_lcd_operation(void)
1597 const struct device_ops *ops = NULL;
1599 ops = find_device("display");
1600 if (!check_default(ops))
1601 display_register_dependent_device(ops);
1603 ops = find_device("touchkey");
1604 if (!check_default(ops))
1605 display_register_dependent_device(ops);
1607 ops = find_device("touchscreen");
1608 if (!check_default(ops))
1609 display_register_dependent_device(ops);
1612 static void exit_lcd_operation(void)
1614 display_unregister_dependent_device();
1626 static const char *errMSG[INIT_END] = {
1627 [INIT_SETTING] = "setting init error",
1628 [INIT_INTERFACE] = "lowlevel interface(sysfs or others) init error",
1629 [INIT_POLL] = "input devices poll init error",
1630 [INIT_FIFO] = "FIFO poll init error",
1631 [INIT_DBUS] = "d-bus init error",
1634 int set_lcd_timeout(int on, int dim, int holdkey_block, const char *name)
1636 if (on == 0 && dim == 0) {
1637 _I("LCD timeout changed: default setting");
1638 custom_normal_timeout = custom_dim_timeout = 0;
1639 } else if (on < 0 || dim < 0) {
1640 _E("Failed to set value(on=%d dim=%d).", on, dim);
1643 _I("LCD timeout changed: on=%ds dim=%ds", on, dim);
1644 custom_normal_timeout = SEC_TO_MSEC(on);
1645 custom_dim_timeout = SEC_TO_MSEC(dim);
1647 /* Apply new backlight time */
1648 update_display_time();
1649 if (get_pm_cur_state() == S_NORMAL)
1650 states[get_pm_cur_state()].trans(EVENT_INPUT);
1652 if (holdkey_block) {
1653 display_lock_set_custom_holdkey_block(true);
1654 _I("Hold key disabled.");
1656 display_lock_set_custom_holdkey_block(false);
1657 _I("Hold key enabled.");
1660 if (custom_change_name) {
1661 free(custom_change_name);
1662 custom_change_name = 0;
1665 if (custom_normal_timeout == 0 &&
1666 custom_dim_timeout == 0 &&
1670 custom_change_name = strndup(name, strlen(name));
1671 if (!custom_change_name) {
1672 _E("Failed to malloc.");
1673 custom_normal_timeout = custom_dim_timeout = 0;
1674 display_lock_set_custom_holdkey_block(false);
1681 void reset_lcd_timeout(GDBusConnection *conn,
1682 const gchar *sender,
1683 const gchar *unique_name,
1689 if (!custom_change_name)
1692 if (strcmp(sender, custom_change_name))
1695 _I("reset lcd timeout: Set default timeout. sender=%s", sender);
1697 free(custom_change_name);
1698 custom_change_name = 0;
1699 custom_normal_timeout = custom_dim_timeout = 0;
1700 display_lock_set_custom_holdkey_block(false);
1702 update_display_time();
1703 if (get_pm_cur_state() == S_NORMAL)
1704 states[get_pm_cur_state()].trans(EVENT_INPUT);
1707 static int delayed_init_done(void *data)
1709 static bool done = false;
1718 _I("Booting done, release booting lock.");
1719 if (disp_plgn->pm_unlock_internal) {
1720 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL, PM_SLEEP_MARGIN);
1721 disp_plgn->pm_unlock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF, PM_SLEEP_MARGIN);
1727 static gboolean delayed_dpms_init_done(gpointer data)
1731 if (!display_panel_init_dpms())
1732 return G_SOURCE_CONTINUE;
1734 switch (get_pm_cur_state()) {
1737 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1738 if (display_conf.timeout_enable) {
1739 timeout = states[S_NORMAL].timeout;
1740 /* check minimun lcd on time */
1741 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
1742 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
1743 reset_timeout(timeout);
1747 lcd_off_procedure(LCD_OFF_BY_EVENT);
1748 timeout = display_conf.lcdoff_timeout;
1749 reset_timeout(timeout);
1755 return G_SOURCE_REMOVE;
1758 static void add_timer_for_dpms_init(void)
1760 guint id = g_timeout_add(500/* milliseconds */, delayed_dpms_init_done, NULL);
1762 _E("Failed to add display_panel_init_dpms timeout.");
1765 static void init_display_states(void *data)
1767 struct display_plugin *dp = (struct display_plugin *) data;
1768 for(int i = 0; i < S_END; i++)
1769 dp->display_states[i] = &states[i];
1772 * Power manager Main
1776 static int display_probe(void *data)
1779 struct display_plugin *dp = (struct display_plugin *) data;
1782 dp->config = &display_conf;
1783 init_display_states(dp);
1784 setup_display_plugin_backlight_ops(dp);
1785 dp->delete_state_cond = del_state_cond;
1788 * load display service
1789 * if there is no display shared library,
1790 * deviced does not provide any method and function of display.
1792 ret = display_service_load();
1796 /* display_plugin instance initialization */
1797 init_pm_internal(data);
1798 disp_plgn->device_flags_to_string = __device_flags_to_string;
1800 if (battery_plgn->handle) {
1801 fp_get_charging_status = dlsym(battery_plgn->handle, "get_charging_status");
1802 if (!fp_get_charging_status)
1803 _E("Failed to obtain address of get_charging_status");
1805 _I("There is no battery module.");
1810 static int input_init_handler(void)
1812 if (!display_conf.input_support)
1813 remove_device_by_devname("input");
1818 static int power_resume_from_echomem_callback(void *data)
1820 system_wakeup_flag = true;
1821 if (check_wakeup_src() == EVENT_DEVICE)
1822 /* system waked up by devices */
1823 states[get_pm_cur_state()].trans(EVENT_DEVICE);
1825 /* system waked up by user input */
1826 states[get_pm_cur_state()].trans(EVENT_INPUT);
1831 static int poweroff_triggered_callback(void *udata)
1833 int val = (int)(intptr_t) udata;
1836 case VCONFKEY_SYSMAN_POWER_OFF_NONE:
1837 clear_pm_status_flag(PWROFF_FLAG);
1839 case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
1840 case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
1841 set_pm_status_flag(PWROFF_FLAG);
1848 static void esd_action(void)
1850 const struct device_ops *touchscreen_ops = NULL;
1854 touchscreen_ops = find_device("touchscreen");
1856 if (!check_default(touchscreen_ops))
1857 touchscreen_ops->stop(NORMAL_MODE);
1858 display_panel_set_panel_state_by_off_state(NORMAL_MODE);
1859 display_panel_set_panel_state_by_on_state(NORMAL_MODE);
1860 if (!check_default(touchscreen_ops))
1861 touchscreen_ops->start(NORMAL_MODE);
1864 static void lcd_uevent_changed(struct udev_device *dev)
1866 const char *devpath;
1869 devpath = udev_device_get_devpath(dev);
1873 if (!fnmatch(LCD_ESD_PATH, devpath, 0)) {
1874 action = udev_device_get_action(dev);
1875 if (!strcmp(action, UDEV_CHANGE))
1880 static const struct uevent_handler lcd_uevent_ops = {
1881 .subsystem = LCD_EVENT_SUBSYSTEM,
1882 .uevent_func = lcd_uevent_changed,
1886 static void display_init(void *data)
1889 unsigned int flags = (WITHOUT_STARTNOTI | FLAG_X_DPMS);
1892 _I("Start power manager.");
1894 signal(SIGHUP, sig_hup);
1896 power_saving_func = default_saving_mode;
1898 /* load configutation */
1899 ret = display_load_config(&display_conf);
1901 _W("Failed to load '%s', use default value: %d",
1902 DISPLAY_CONF_FILE, ret);
1904 register_kernel_uevent_control(&lcd_uevent_ops);
1906 register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
1907 register_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
1908 register_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
1909 register_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
1910 register_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
1911 register_notifier(DEVICE_NOTIFIER_POWEROFF_TRIGGERED, poweroff_triggered_callback);
1913 init_save_userlock();
1915 for (i = INIT_SETTING; i < INIT_END; i++) {
1918 ret = init_setting(update_setting);
1920 case INIT_INTERFACE:
1921 if (display_conf.timeout_enable)
1922 get_lcd_timeout_from_settings();
1923 ret = init_sysfs(flags);
1927 ret = input_init_handler();
1929 pm_lock_detector_init();
1933 ret = init_pm_dbus();
1937 _E("Failed to init: %s", errMSG[i]);
1942 if (i == INIT_END) {
1943 display_ops_init(NULL);
1944 #ifdef ENABLE_PM_LOG
1947 init_lcd_operation();
1948 check_seed_status();
1950 /* In smd test, TSP should be turned off if display panel is not existed. */
1951 if (display_panel_get_dpms_cached_state() == -ENOENT) {
1952 _I("Display panel is not existed.");
1953 lcd_direct_control(DPMS_OFF, NORMAL_MODE);
1954 exit_lcd_operation();
1957 /* wm_ready needs to be checked
1958 * since display manager can be launched later than deviced.
1959 * In the case, display cannot be turned on at the first booting */
1960 // wm_ready = check_wm_ready();
1961 if (display_panel_init_dpms()) {
1962 if (is_lcdon_blocked() != LCDON_BLOCK_NONE)
1963 lcd_off_procedure(LCD_OFF_BY_EVENT);
1965 lcd_on_procedure(LCD_NORMAL, LCD_ON_BY_EVENT);
1967 add_timer_for_dpms_init();
1970 if (display_conf.lcd_always_on) {
1971 _I("LCD always on.");
1972 display_state_transition_set_transition_table_display_state(S_NORMAL, S_NORMAL, EVENT_TIMEOUT);
1975 if (flags & WITHOUT_STARTNOTI) { /* start without noti */
1976 _I("Start Power managing without noti");
1977 power_request_change_state_strict(DEVICED_POWER_STATE_SLEEP, DEVICED_POWER_STATE_NORMAL,
1978 HAL_DEVICE_POWER_TRANSITION_REASON_UNKNOWN, NULL);
1980 * Lock lcd off until booting is done.
1981 * deviced guarantees all booting script is executing.
1982 * Last script of booting unlocks this suspend blocking state.
1984 if (disp_plgn->pm_lock_internal)
1985 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_OFF,
1986 STAY_CUR_STATE, DELAYED_INIT_WATING_TIME);
1988 /* Initial display state right after the booting done */
1989 if (is_lcdon_blocked())
1990 set_pm_cur_state(S_LCDOFF);
1992 set_pm_cur_state(S_NORMAL);
1993 ret = vconf_set_int(VCONFKEY_PM_STATE, get_pm_cur_state());
1995 _E("Failed to set vconf value for pm cur state: %d", vconf_get_ext_errno());
1997 status = DEVICE_OPS_STATUS_START;
1998 if (display_conf.timeout_enable) {
1999 timeout = states[S_NORMAL].timeout;
2000 /* check minimun lcd on time */
2001 if (timeout < SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT))
2002 timeout = SEC_TO_MSEC(DEFAULT_NORMAL_TIMEOUT);
2004 if (disp_plgn->pm_lock_internal)
2005 disp_plgn->pm_lock_internal(INTERNAL_LOCK_BOOTING, LCD_NORMAL,
2006 STAY_CUR_STATE, timeout);
2011 if (display_conf.input_support)
2012 if (CHECK_OPS(keyfilter_ops, init))
2013 keyfilter_ops->init();
2015 set_display_init_direction(display_conf.display_init_direction);
2019 static void display_exit(void *data)
2023 status = DEVICE_OPS_STATUS_STOP;
2025 /* Set current state to S_NORMAL */
2026 set_pm_cur_state(S_NORMAL);
2027 set_setting_pmstate(get_pm_cur_state());
2028 /* timeout is not needed */
2029 reset_timeout(TIMEOUT_NONE);
2031 if (CHECK_OPS(keyfilter_ops, exit))
2032 keyfilter_ops->exit();
2034 unregister_kernel_uevent_control(&lcd_uevent_ops);
2036 display_ops_exit(NULL);
2038 for (i = i - 1; i >= INIT_SETTING; i--) {
2043 case INIT_INTERFACE:
2047 unregister_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
2048 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_BACKGROUND, display_app_background);
2049 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_FOREGROUND, display_app_foreground);
2050 unregister_notifier(DEVICE_NOTIFIER_APPLICATION_TERMINATED, display_app_terminated);
2051 unregister_notifier(DEVICE_NOTIFIER_POWER_RESUME_FROM_ECHO_MEM, power_resume_from_echomem_callback);
2057 exit_lcd_operation();
2058 free_lock_info_list();
2060 /* free display service */
2061 display_service_free();
2063 _I("Stop power manager.");
2066 static int display_start(enum device_flags flags)
2069 if (flags & NORMAL_MODE) {
2070 if (flags & LCD_PANEL_OFF_MODE)
2072 display_panel_set_panel_state_by_standby_state(true);
2075 display_panel_set_panel_state_by_on_state(flags);
2080 /* CORE LOGIC MODE */
2081 if (!(flags & CORE_LOGIC_MODE))
2084 if (status == DEVICE_OPS_STATUS_START)
2087 if (display_probe(NULL) < 0)
2095 static int display_stop(enum device_flags flags)
2098 if (flags & NORMAL_MODE || flags & FORCE_OFF_MODE) {
2099 display_panel_set_panel_state_by_off_state(flags);
2103 /* CORE LOGIC MODE */
2104 if (!(flags & CORE_LOGIC_MODE))
2107 if (status == DEVICE_OPS_STATUS_STOP)
2115 static int display_status(void)
2120 static const struct device_ops display_plugin_device_ops = {
2121 .disable_auto_init = true,
2122 DECLARE_NAME_LEN("display-plugin"),
2123 .probe = display_probe,
2124 .init = display_init,
2125 .exit = display_exit,
2126 .start = display_start,
2127 .stop = display_stop,
2128 .status = display_status,
2131 DEVICE_OPS_REGISTER(&display_plugin_device_ops)
2133 static void __CONSTRUCTOR__ initialize(void)
2135 disp_plgn = get_var_display_plugin();
2137 _E("Failed to get display plugin variable.");
2139 backlight_ops = get_var_backlight_ops();
2141 _E("Failed to get backlight operator variable.");
2143 battery_plgn = get_var_battery_plugin();
2145 _E("Failed to get battery plugin variable.");