summaryrefslogtreecommitdiff
path: root/src/usb/usb.c
blob: df8afdd52b65a20c2c5d36ce9c9584a7aad8b9c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * deviced
 *
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <vconf.h>

#include "core/log.h"
#include "core/list.h"
#include "core/common.h"
#include "core/device-notifier.h"
#include "display/poll.h"
#include "extcon/extcon.h"
#include "usb.h"
#include "usb-tethering.h"

enum usb_state {
	USB_DISCONNECTED,
	USB_CONNECTED,
};

static dd_list *config_list;
struct extcon_ops extcon_usb_ops;
static const struct usb_config_plugin_ops *config_plugin;

void add_usb_config(const struct usb_config_ops *ops)
{
	DD_LIST_APPEND(config_list, ops);
}

void remove_usb_config(const struct usb_config_ops *ops)
{
	DD_LIST_REMOVE(config_list, ops);
}

static int usb_config_module_load(void)
{
	dd_list *l;
	struct usb_config_ops *ops;

	DD_LIST_FOREACH(config_list, l, ops) {
		if (ops->is_valid && ops->is_valid()) {
			if (ops->load)
				config_plugin = ops->load();
			return 0;
		}
	}
	return -ENOENT;
}

static void usb_config_module_unload(void)
{
	dd_list *l;
	struct usb_config_ops *ops;

	config_plugin = NULL;

	DD_LIST_FOREACH(config_list, l, ops) {
		if (ops->is_valid && ops->is_valid()) {
			if (ops->release)
				ops->release();
		}
	}
}

static int usb_config_init(void)
{
	if (!config_plugin) {
		_E("There is no usb config plugin");
		return -ENOENT;
	}

	if (config_plugin->init == NULL) {
		_E("There is no usb config init function");
		return -ENOENT;
	}

	return config_plugin->init(NULL);
}

static void usb_config_deinit(void)
{
	if (!config_plugin) {
		_E("There is no usb config plugin");
		return;
	}

	if (config_plugin->deinit == NULL) {
		_E("There is no usb config deinit function");
		return;
	}

	config_plugin->deinit(NULL);
}

static int usb_config_enable(void)
{
	char *mode;

	if (!config_plugin) {
		_E("There is no usb config plugin");
		return -ENOENT;
	}

	if (config_plugin->enable == NULL) {
		_E("There is no usb config enable function");
		return -ENOENT;
	}

	if (usb_tethering_state())
		mode = MODE_TETHERING;
	else
		mode = MODE_DEFAULT;

	return config_plugin->enable(mode);
}

static int usb_config_disable(void)
{
	if (!config_plugin) {
		_E("There is no usb config plugin");
		return -ENOENT;
	}

	if (config_plugin->disable == NULL) {
		_E("There is no usb config disable function");
		return -ENOENT;
	}

	return config_plugin->disable(NULL);
}

static void usb_change_state(int val)
{
	static int old_status = -1;
	static int old_mode = -1;
	int mode, legacy_mode;

	switch (val) {
	case VCONFKEY_SYSMAN_USB_DISCONNECTED:
	case VCONFKEY_SYSMAN_USB_CONNECTED:
		mode = SET_USB_NONE;
		legacy_mode = SETTING_USB_NONE_MODE;
		break;
	case VCONFKEY_SYSMAN_USB_AVAILABLE:
		mode = SET_USB_DEFAULT;
		legacy_mode = SETTING_USB_DEFAULT_MODE;
		break;
	default:
		return;
	}

	if (old_status != val) {
		vconf_set_int(VCONFKEY_SYSMAN_USB_STATUS, val);
		old_status = val;
	}

	if (old_mode != mode) {
		vconf_set_int(VCONFKEY_USB_CUR_MODE, mode);
		vconf_set_int(VCONFKEY_SETAPPL_USB_MODE_INT, legacy_mode);
		old_mode = mode;
	}
}

int usb_change_mode(char *name)
{
	int ret;

	if (!name)
		return -EINVAL;

	if (!config_plugin) {
		_E("There is no usb config plugin");
		return -ENOENT;
	}

	if (config_plugin->change == NULL) {
		_E("There is no usb config change function");
		return -ENOENT;
	}

	_I("USB mode change to (%s) is requested", name);
	usb_change_state(VCONFKEY_SYSMAN_USB_CONNECTED);

	ret = config_plugin->change(name);
	if (ret < 0) {
		_E("Failed to change usb mode (%d)", ret);
		return ret;
	}

	usb_change_state(VCONFKEY_SYSMAN_USB_AVAILABLE);

	return 0;
}

static int usb_state_changed(int status)
{
	static int old = -1;	/* to update at the first time */
	int ret;

	_I("USB state is changed from (%d) to (%d)", old, status);

	if (old == status)
		return 0;

	switch (status) {
	case USB_CONNECTED:
		_I("USB cable is connected");
		usb_change_state(VCONFKEY_SYSMAN_USB_CONNECTED);
		ret = usb_config_enable();
		if (ret != 0) {
			_E("Failed to enable usb config (%d)", ret);
			break;
		}
		usb_change_state(VCONFKEY_SYSMAN_USB_AVAILABLE);
		pm_lock_internal(INTERNAL_LOCK_USB,
				LCD_OFF, STAY_CUR_STATE, 0);
		break;
	case USB_DISCONNECTED:
		_I("USB cable is disconnected");
		usb_change_state(VCONFKEY_SYSMAN_USB_DISCONNECTED);
		ret = usb_config_disable();
		if (ret != 0)
			_E("Failed to disable usb config (%d)", ret);
		pm_unlock_internal(INTERNAL_LOCK_USB,
				LCD_OFF, STAY_CUR_STATE);
		break;
	default:
		_E("Invalid USB state(%d)", status);
		return -EINVAL;
	}
	if (ret < 0)
		_E("Failed to operate usb connection(%d)", ret);
	else
		old = status;

	return ret;
}

static void usb_init(void *data)
{
	int ret;

	ret = usb_config_module_load();
	if (ret < 0) {
		_E("Failed to get config module (%d)", ret);
		return;
	}

	ret = usb_config_init();
	if (ret < 0)
		_E("Failed to initialize usb configuation");

	add_usb_tethering_handler();

	ret = usb_state_changed(extcon_usb_ops.status);
	if (ret < 0)
		_E("Failed to update usb status(%d)", ret);
}

static void usb_exit(void *data)
{
	remove_usb_tethering_handler();
	usb_config_deinit();
	usb_config_module_unload();
}

struct extcon_ops extcon_usb_ops = {
	.name	= EXTCON_CABLE_USB,
	.init	= usb_init,
	.exit	= usb_exit,
	.update = usb_state_changed,
};

EXTCON_OPS_REGISTER(extcon_usb_ops)