tizen 2.3 release
[framework/system/deviced.git] / src / ta / ta-handler.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19
20 #include <vconf.h>
21 #include <device-node.h>
22
23 #include "core/log.h"
24 #include "core/devices.h"
25 #include "display/poll.h"
26 #include "core/common.h"
27
28 #define RETRY   3
29
30 enum ta_connect_status{
31         TA_OFFLINE = 0,
32         TA_ONLINE = 1,
33 };
34
35 static int __check_insuspend_charging(void)
36 {
37         int val, ret;
38
39         ret = device_get_property(DEVICE_TYPE_POWER, PROP_POWER_INSUSPEND_CHARGING_SUPPORT, &val);
40         if (ret != 0)
41                 val = 0;
42         if (val == 0)
43                 ret = pm_lock_internal(INTERNAL_LOCK_TA, LCD_OFF, STAY_CUR_STATE, 0);
44         else
45                 ret = 0;
46         return ret;
47 }
48
49 static void ta_init(void *data)
50 {
51         int val, i = 0;
52         int ret;
53
54         if (device_get_property(DEVICE_TYPE_EXTCON, PROP_EXTCON_TA_ONLINE, &val) != 0)
55                 val = -EINVAL;
56
57         if (val == TA_ONLINE) {
58                 vconf_set_int(VCONFKEY_SYSMAN_CHARGER_STATUS,
59                                 VCONFKEY_SYSMAN_CHARGER_CONNECTED);
60                 while (i < RETRY
61                            && __check_insuspend_charging() == -1) {
62                         i++;
63                         sleep(1);
64                 }
65         } else if (val == TA_OFFLINE) {
66                 vconf_set_int(VCONFKEY_SYSMAN_CHARGER_STATUS,
67                                 VCONFKEY_SYSMAN_CHARGER_DISCONNECTED);
68         }
69 }
70
71 static const struct device_ops ta_device_ops = {
72         .priority = DEVICE_PRIORITY_NORMAL,
73         .name     = "ta",
74         .init     = ta_init,
75 };
76
77 DEVICE_OPS_REGISTER(&ta_device_ops)