usb: apply new enums for usb-gadget mode
[platform/core/connectivity/data-router.git] / src / dr-noti-handler.c
1 /*
2  * Data-Router
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Injun Yang <injun.yang@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *              http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24 #include <glib.h>
25 #include <sys/wait.h>
26 #include <device/usb-gadget.h>
27
28 #include "dr-main.h"
29 #include "dr-usb.h"
30 #include "dr-modem.h"
31 #include "dr-common.h"
32
33
34
35 #define ACM_WAIT_TIME 300000
36
37 extern dr_info_t dr_info;
38 volatile gboolean dsr_status = FALSE;
39
40 static gboolean acm_enabled(int mode)
41 {
42         return (mode & USB_GADGET_FUNC_ACM);
43 }
44
45 static void __usb_status_noti_handler(void *data)
46 {
47         int usb_state = -1;
48         int usb_mode = -1;
49         int ret;
50
51         if (_get_usb_state(&usb_state) < 0) {
52                 ERR(" Unable to get usb status !\n");
53                 return;
54         }
55
56         ret = vconf_get_int(VCONFKEY_USB_CUR_MODE, &usb_mode);
57         if (ret != 0) {
58                 DBG("Vconf get failed\n");
59                 return;
60         }
61
62         DBG("USB noti handler, USB state : %d, Mode : %d\n", usb_state, usb_mode);
63         DBG("usb_fd = 0x%x\n", dr_info.usb.usb_fd);
64
65
66         /*
67          * If USB driver builted in the Kernel, VCONFKEY_SYSMAN_USB_CONNECTED is not used.
68          * If USB driver loaded in DR/MTP then need to check VCONFKEY_SYSMAN_USB_CONNECTED.
69          * After usb driver loaded, MTP will be set vconf value as VCONFKEY_SYSMAN_USB_CONNECTED
70          */
71         if (usb_state != VCONFKEY_SYSMAN_USB_DISCONNECTED &&
72                         acm_enabled(usb_mode)) {
73                 _init_usb();
74         } else if (usb_state == VCONFKEY_SYSMAN_USB_DISCONNECTED) {
75                 _deinit_usb();
76                 _deinit_dr();
77         }
78         return;
79 }
80
81 static void __usb_mode_noti_handler(void *data)
82 {
83         int usb_mode = -1;
84         int ret;
85
86         ret = vconf_get_int(VCONFKEY_USB_CUR_MODE, &usb_mode);
87         if (ret != 0) {
88                 DBG("Vconf get failed\n");
89                 return;
90         }
91
92         DBG("USB Mode noti handler, USB  Mode : %d\n", usb_mode);
93         DBG("usb_fd = 0x%x\n", dr_info.usb.usb_fd);
94
95         if (!acm_enabled(usb_mode)) {
96                 _deinit_usb();
97                 _deinit_dr();
98         }
99
100         return;
101 }
102
103
104 gboolean _register_vconf_notification(void)
105 {
106         int ret;
107
108         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
109                         (vconf_callback_fn)__usb_status_noti_handler, NULL);
110         if (ret < 0)
111                 ERR("Error !!! VCONFKEY reg noti  : %s\n", VCONFKEY_SYSMAN_USB_STATUS);
112
113         ret = vconf_notify_key_changed(VCONFKEY_USB_CUR_MODE,
114                         (vconf_callback_fn) __usb_mode_noti_handler, NULL);
115         if (ret < 0)
116                 ERR("Error !!! VCONFKEY reg noti  : %s\n", VCONFKEY_USB_CUR_MODE);
117
118         return TRUE;
119 }
120
121 void _unregister_vconf_notification(void)
122 {
123         vconf_ignore_key_changed(VCONFKEY_SYSMAN_USB_STATUS,
124                         (vconf_callback_fn) __usb_status_noti_handler);
125         vconf_ignore_key_changed(VCONFKEY_USB_CUR_MODE,
126                         (vconf_callback_fn) __usb_mode_noti_handler);
127         return;
128 }
129
130 gboolean _register_telephony_event(void)
131 {
132         return TRUE;
133 }
134
135 void _unregister_telephony_event(void)
136 {
137         return;
138 }
139