summaryrefslogtreecommitdiff
path: root/hw/usb_client_common.c
blob: e8f8473f1aee68720b2b8485f88981fe08c02a03 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * libdevice-node
 *
 * Copyright (c) 2018 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 <errno.h>
#include <string.h>
#include <stdbool.h>

#include <libsyscommon/dbus-systemd.h>

#include <hw/shared.h>
#include <hw/usb_gadget.h>
#include <hw/usb_client.h>

#define MAX_GADGET_STR_LEN 256

#define LEGACY_ROOTPATH        "/sys/class/usb_mode/usb0"

/* Device descriptor values */
#define LEGACY_ID_VENDOR_PATH       LEGACY_ROOTPATH"/idVendor"
#define LEGACY_ID_PRODUCT_PATH      LEGACY_ROOTPATH"/idProduct"
#define LEGACY_BCD_DEVICE_PATH      LEGACY_ROOTPATH"/bcdDevice"
#define LEGACY_CLASS_PATH           LEGACY_ROOTPATH"/bDeviceClass"
#define LEGACY_SUBCLASS_PATH        LEGACY_ROOTPATH"/bDeviceSubClass"
#define LEGACY_PROTOCOL_PATH        LEGACY_ROOTPATH"/bDeviceProtocol"

/* Strings */
#define LEGACY_IMANUFACTURER_PATH   LEGACY_ROOTPATH"/iManufacturer"
#define LEGACY_IPRODUCT_PATH        LEGACY_ROOTPATH"/iProduct"

/* Functions in each config */
#define LEGACY_CONFIG_1_PATH        LEGACY_ROOTPATH"/funcs_fconf"
#define LEGACY_CONFIG_2_PATH        LEGACY_ROOTPATH"/funcs_sconf"

/* should be single char */
#define LEGACY_FUNC_SEP             ","

/* ON/OFF switch */
#define LEGACY_ENABLE_PATH          LEGACY_ROOTPATH"/enable"
#define LEGACY_ENABLE               "1"
#define LEGACY_DISABLE              "0"

#ifndef EXPORT
#define EXPORT	__attribute__ ((visibility("default")))
#endif

static bool legacy_is_function_supported(struct usb_function *func)
{
	char buf[PATH_MAX];

	snprintf (buf, sizeof(buf), "%s/f_%s", LEGACY_ROOTPATH, func->name);
	if (access(buf, F_OK))
		return false;

	return true;
}

static bool legacy_is_gadget_supported(struct usb_gadget *gadget)
{
	int i, j;
	struct usb_configuration *config;

	if (!gadget || !gadget->configs || !gadget->configs[0] || !gadget->configs[0]->funcs[0])
		return false;

	if (!gadget->attrs.idVendor || !gadget->attrs.idProduct || !gadget->attrs.bcdDevice)
			return false;

	/* only strings in US_en are allowed */
	if (gadget->strs.lang_code != DEFAULT_LANG)
			return false;

	if (!gadget->strs.manufacturer || !gadget->strs.product)
		return false;

	for (j = 0; gadget->configs[j]; ++j) {
		config = gadget->configs[j];

		if (!config->funcs)
			return false;

		for (i = 0; config->funcs[i]; ++i)
			if (!legacy_is_function_supported(config->funcs[i]))
				return false;
	}

	return true;
}

/* TODO. Maybe move this to sys ? */
static int legacy_set_int_hex(char *path, int val)
{
	int r;
	char buf[MAX_GADGET_STR_LEN];

	if (!path)
		return -EINVAL;

	snprintf(buf, sizeof(buf), "%x", val);
	r = sys_set_str(path, buf);
	if (r < 0)
		return r;

	return 0;
}

static int legacy_set_gadget_attrs(struct usb_gadget_attrs *attrs)
{
	int ret;

	ret = sys_set_int(LEGACY_CLASS_PATH, attrs->bDeviceClass);
	if (ret)
		return ret;

	ret = sys_set_int(LEGACY_SUBCLASS_PATH, attrs->bDeviceSubClass);
	if (ret)
		return ret;

	ret = sys_set_int(LEGACY_PROTOCOL_PATH, attrs->bDeviceProtocol);
	if (ret)
		return ret;

	ret = legacy_set_int_hex(LEGACY_ID_VENDOR_PATH, attrs->idVendor);
	if (ret)
		return ret;

	ret = legacy_set_int_hex(LEGACY_ID_PRODUCT_PATH, attrs->idProduct);
	if (ret)
		return ret;

	ret = legacy_set_int_hex(LEGACY_BCD_DEVICE_PATH, attrs->bcdDevice);
	if (ret)
		return ret;

	return 0;
}

static int legacy_set_gadget_strs(struct usb_gadget_strings *strs)
{
	int ret;

	if (!strs->manufacturer || !strs->product)
		return -EINVAL;

	ret = sys_set_str(LEGACY_IMANUFACTURER_PATH, strs->manufacturer);
	if (ret)
		return ret;

	ret = sys_set_str(LEGACY_IPRODUCT_PATH, strs->product);
	if (ret)
		return ret;

	/* The serial is written by the slp gadget kernel driver */

	return 0;
}

static int legacy_set_gadget_config(char *cpath,
				    struct usb_configuration *config)
{
	char buf[MAX_GADGET_STR_LEN];
	int left = sizeof(buf);
	char *pos = buf;
	int ret;
	int i;

	if (!config) {
		buf[0] = '\n';
		buf[1] = '\0';
		goto empty_config;
	}

	for (i = 0; config->funcs[i]; ++i) {
		ret = snprintf(pos, left, "%s" LEGACY_FUNC_SEP,
			       config->funcs[i]->name);
		if (ret >= left)
			return -EOVERFLOW;

		pos += ret;
		left -= ret;
	}

	/* eliminate last separator */
	*(pos - 1) = '\0';

empty_config:
	return sys_set_str(cpath, buf);
}

static int legacy_reconfigure_gadget(struct usb_client *usb, struct usb_gadget *gadget)
{
	int ret;

	if (!usb || !gadget)
		return -EINVAL;

	/* Verify the gadget and check if function is supported */
	if (!legacy_is_gadget_supported(gadget))
		return -ENOTSUP;

	ret = legacy_set_gadget_attrs(&gadget->attrs);
	if (ret)
		return ret;

	ret = legacy_set_gadget_strs(&gadget->strs);
	if (ret)
		return ret;

	ret = legacy_set_gadget_config(LEGACY_CONFIG_1_PATH, gadget->configs[0]);
	if (ret)
		return ret;

	ret = legacy_set_gadget_config(LEGACY_CONFIG_2_PATH, gadget->configs[1]);
	if (ret)
		return ret;

	return 0;
}

static void legacy_start_stop_service_and_handler(bool start)
{
	int i;
	int ret;
	int n_func = 0;
	char *ptr;
	char *begin;
	char *fname;
	char *sep = LEGACY_FUNC_SEP;
	char buf[MAX_GADGET_STR_LEN];
	struct usb_function *func;
	struct usb_function *funcs[USB_FUNCTION_IDX_MAX];

	/* SLP gadget uses two USB configuration.
	 * (/sys/class/usb_mode/usb0/funcs_fconf and /sys/class/usb_mode/usb0/funcs_sconf)
	 *
	 * One usb function can be included in two configurations simultaneously.
	 * In this situation, a handler associated with function can be called twice for a usb function.
	 * To prevent duplicate calls,
	 * Collect all functions and remove duplicates and process them.
	 */

	/* First configuration */
	ret = sys_get_str(LEGACY_CONFIG_1_PATH, buf, sizeof(buf));
	if (ret)
		goto second_configuration;

	/* Caution: buf ends with '\n' */
	ptr = strchr(buf, '\n');
	if (ptr)
		*ptr = 0;

	begin = buf;
	for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
		func = find_usb_function_by_name(fname);
		if (!func)
			continue;

		for (i = 0; i < n_func; i++)
			if (funcs[i] == func)
				continue;

		if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
			break;

		funcs[n_func] = func;
		n_func++;
	}

	/* Second configuration */
second_configuration:
	ret = sys_get_str(LEGACY_CONFIG_2_PATH, buf, sizeof(buf));
	if (ret)
		return;

	/* Caution: buf ends with '\n' */
	ptr = strchr(buf, '\n');
	if (ptr)
		*ptr = 0;

	begin = buf;
	for (fname = strsep(&begin, sep); fname; fname = strsep(&begin, sep)) {
		func = find_usb_function_by_name(fname);
		if (!func)
			continue;

		for (i = 0; i < n_func; i++)
			if (funcs[i] == func)
				continue;

		if(n_func >= USB_FUNCTION_IDX_MAX) /* What happen */
			break;

		funcs[n_func] = func;
		n_func++;
	}

	for (i = 0; i < n_func; i++) {
		if (start) {
			if (funcs[i]->handler)
				funcs[i]->handler(1);

			if (funcs[i]->service)
				(void)systemd_start_unit_wait_started(funcs[i]->service, ".service", -1);
		} else {
			if (funcs[i]->service)
				(void)systemd_stop_unit_wait_stopped(funcs[i]->service, ".service", -1);

			if (funcs[i]->handler)
				funcs[i]->handler(0);
		}
	}
}

static int legacy_enable(struct usb_client *usb)
{
	int ret;

	ret = sys_set_str(LEGACY_ENABLE_PATH, LEGACY_ENABLE);
	if (ret < 0)
		return ret;

	legacy_start_stop_service_and_handler(true);

	return 0;
}

static int legacy_disable(struct usb_client *usb)
{

	legacy_start_stop_service_and_handler(false);

	return sys_set_str(LEGACY_ENABLE_PATH, LEGACY_DISABLE);
}

EXPORT
int hw_legacy_gadget_open(struct hw_info *info,
		const char *id, struct hw_common **common)
{
	struct usb_client *legacy;

	if (!info || !common)
		return -EINVAL;

	/* check if slp usb gadget exists */
	if (access("/sys/class/usb_mode/usb0/enable", F_OK))
		return -ENOENT;

	legacy = calloc(1, sizeof(*legacy));
	if (!legacy)
		return -ENOMEM;

	legacy->common.info = info;

	legacy->reconfigure_gadget = legacy_reconfigure_gadget;
	legacy->enable = legacy_enable;
	legacy->disable = legacy_disable;

	*common = &legacy->common;
	return 0;
}

EXPORT
int hw_legacy_gadget_close(struct hw_common *common)
{
	struct usb_client *legacy;

	if (!common)
		return -EINVAL;

	legacy = container_of(common, struct usb_client,
					 common);

	free(legacy);
	return 0;
}