summaryrefslogtreecommitdiff
path: root/hw/usb_cfs_client_common.c
blob: 029988b3f6a6398417e49b313565931e9d333641 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 * 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 <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>

#include <usbg/usbg.h>
#include <usbg/function/net.h>
#include <libsyscommon/dbus-systemd.h>

#include <hw/usb_client.h>

#define CONFIGFS_PATH "/sys/kernel/config"

#define CONFIGFS_GADGET_NAME "hal-gadget"
#define CONFIGFS_CONFIG_LABEL "hal-config"

#define NAME_INSTANCE_SEP '.'
#define MAX_INSTANCE_LEN 512

#define USB_FUNCS_PATH "/dev/usb-funcs"

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

enum cfs_function_service_operation {
	CFS_FUNCTION_SERVICE_START,
	CFS_FUNCTION_SERVICE_STOP,
	CFS_FUNCTION_SERVICE_POST_STOP,
};

struct cfs_client {
	struct usb_client client;
	usbg_state *ctx;
	usbg_gadget *gadget;
	usbg_udc *udc;
};

/* Based on values in slp-gadget kernel module */
struct usbg_gadget_attrs default_g_attrs = {
	.bcdUSB = 0x0200,
	.idVendor = 0x04e8,
	.idProduct = 0x6860,
	.bcdDevice = 0x0100,
};

struct usbg_gadget_strs default_g_strs = {
	.manufacturer = "Samsung",
	.product = "TIZEN",
	.serial = "01234TEST",
};

static struct usb_function *cfs_find_usb_function(usbg_function *function)
{
	char *sep;
	char buf[MAX_INSTANCE_LEN];
	const char *instance = usbg_get_function_instance(function);
	const char *name = usbg_get_function_type_str(usbg_get_function_type(function));

	/* Ex. name:"ffs",  instance: "sdb.default" */
	if (strcmp(name, usbg_get_function_type_str(USBG_F_FFS)) == 0) {
		strncpy(buf, instance, sizeof(buf) - 1);
		buf[sizeof(buf) - 1] = '\0';

		/* Ex. "sdb.default" ==> "sdb" + "default" */
		sep = strchr(buf, NAME_INSTANCE_SEP);
		if (!sep || !sep[1])
			return NULL;
		*sep = '\0';

		name = buf;
		instance = sep + 1;
	}

	return find_usb_function_by_name_instance(name, instance);
}

static bool cfs_is_function_supported(struct usb_client *usb,
					 struct usb_function *func)
{
	bool res;
	int ret;

	if (!func->is_functionfs) {
		ret = usbg_lookup_function_type(func->name);
		res = ret >= 0;
	} else {
		/* TODO: Check if socket is available */
		res = true;
	}

	return res;
}

static bool cfs_is_gadget_supported(struct usb_client *usb,
				       struct usb_gadget *gadget)
{
	int i, j;

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

	/* No real restrictions for strings */
	for (j = 0; gadget->configs && gadget->configs[j]; ++j) {
		struct usb_configuration *config = gadget->configs[j];

		if (!config->funcs)
			return false;

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

	if (j == 0)
		return false;

	return true;
}

static int cfs_set_gadget_attrs(struct cfs_client *cfs_client,
				struct usb_gadget_attrs *attrs)
{
	int ret;
	struct usbg_gadget_attrs gadget_attrs;

	ret = usbg_get_gadget_attrs(cfs_client->gadget, &gadget_attrs);
	if (ret)
		return ret;

	gadget_attrs.bDeviceClass = attrs->bDeviceClass;
	gadget_attrs.bDeviceSubClass = attrs->bDeviceSubClass;
	gadget_attrs.bDeviceProtocol = attrs->bDeviceProtocol;
	gadget_attrs.idVendor = attrs->idVendor;
	gadget_attrs.idProduct = attrs->idProduct;
	gadget_attrs.bcdDevice = attrs->bcdDevice;

	ret = usbg_set_gadget_attrs(cfs_client->gadget, &gadget_attrs);

	return ret;
}

static int cfs_set_gadget_strs(struct cfs_client *cfs_client,
				  struct usb_gadget_strings *strs)
{
	int ret = 0;

	/*
	 * TODO
	 * Here is a good place to ensure that serial is immutable
	 */
#define SET_STR(FIELD, STR_ID)				\
	if (strs->FIELD) {				\
		ret = usbg_set_gadget_str(cfs_client->gadget,	\
					  STR_ID,		\
					  strs->lang_code,	\
					  strs->FIELD);		\
		if (ret)					\
			return ret;				\
	}

	SET_STR(manufacturer, USBG_STR_MANUFACTURER);
	SET_STR(product, USBG_STR_PRODUCT);
	SET_STR(serial, USBG_STR_SERIAL_NUMBER);
#undef SET_STR
	return ret;
}

static int cfs_ensure_dir(char *path)
{
	int ret;

	ret = mkdir(path, 0770);
	if (ret < 0)
		ret = errno == EEXIST ? 0 : errno;

	return ret;
}


static int cfs_prep_ffs_service(struct usb_function *usb_func, usbg_function *function)
{
	int ret;
	const char *name;
	const char *service;
	const char *instance;
	const char *dev_name;
	char buf[MAX_INSTANCE_LEN];

	if (!usb_func || !function)
		return -EINVAL;

	if (usbg_get_function_type(function) != USBG_F_FFS)
		return -EINVAL;

	name = usb_func->name;
	service = usb_func->service;
	instance = usb_func->instance;
	dev_name = usbg_get_function_instance(function);

	/* "/dev/usb-funcs" + "/" + "sdb" + "/" + "default"  + '0' */
	if (strlen(USB_FUNCS_PATH) + strlen(name) + strlen(instance) + 3  > sizeof(buf))
		return -ENAMETOOLONG;

	/* mkdir /dev/usb-funcs */
	ret = cfs_ensure_dir(USB_FUNCS_PATH);
	if (ret < 0)
		return ret;

	/* mkdir /dev/usb-funcs/sdb */
	snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
	ret = cfs_ensure_dir(buf);
	if (ret < 0)
		goto out_rmdir;

	/* mkdir /dev/usb-funcs/sdb/default */
	snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
	ret = cfs_ensure_dir(buf);
	if (ret < 0)
		goto out_rmdir;

	/* mount -t functionfs sdb.default /dev/usb-funcs/sdb/default */
	ret = mount(dev_name, buf, "functionfs", 0, NULL);
	if (ret < 0)
		goto out_rmdir;

	/* start sdbd.socket */
	ret = systemd_start_unit_wait_started(service, ".socket", -1);
	if (ret < 0)
		goto out_unmount;

	return 0;

out_unmount:
	umount(buf);

out_rmdir:
	snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, name, instance);
	rmdir(buf);

	snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, name);
	rmdir(buf);

	rmdir(USB_FUNCS_PATH);

	return ret;
}

static int cfs_cleanup_ffs_service(usbg_function *function)
{
	int ret;
	char buf[MAX_INSTANCE_LEN];
	struct usb_function *usb_function;

	if (!function)
		return -EINVAL;

	usb_function = cfs_find_usb_function(function);
	if (!usb_function)
		return -ENOENT;

	/* stop .socket first and stop .service later becuase of socket activation */
	if (usb_function->service) {
		(void)systemd_stop_unit_wait_stopped(usb_function->service, ".socket", -1);
		(void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
	}

	/* umount /dev/usb-funcs/[sdb|mtp]/default and remove it's directory */
	ret = snprintf(buf, sizeof(buf), "%s/%s/%s", USB_FUNCS_PATH, usb_function->name, usb_function->instance);
	if (ret < 0)
		return ret;

	ret = umount(buf);
	if (ret < 0)
		return ret;

	ret = rmdir(buf);
	if (ret < 0)
		return ret;

	/* remove /dev/usb-funcs/[sdb|mtp] directory */
	ret = snprintf(buf, sizeof(buf), "%s/%s", USB_FUNCS_PATH, usb_function->name);
	if (ret < 0)
		return ret;

	ret = rmdir(buf);
	if (ret < 0 && errno != ENOTEMPTY)
		return ret;

	/* remove /dev/usb-funcs/ directory */
	ret = rmdir(USB_FUNCS_PATH);
	if (ret < 0 && errno != ENOTEMPTY)
		return ret;

	return 0;
}


static int cfs_set_rndis_mac_addr(usbg_gadget *gadget, usbg_function *func)
{
	int i, ret;
	struct ether_addr ethaddr;
	struct usbg_gadget_strs strs;
	struct usbg_f_net *nf = usbg_to_net_function(func);

	if (!nf)
		return -EINVAL;

	ret = usbg_get_gadget_strs(gadget, LANG_US_ENG, &strs);
	if (ret != USBG_SUCCESS)
		return ret;

	for (i = 0; i < ETHER_ADDR_LEN; i++)
		ethaddr.ether_addr_octet[i] = 0;

	for (i = 0; (i < 256) && strs.serial[i]; i++) {
		ethaddr.ether_addr_octet[i % (ETHER_ADDR_LEN - 1) + 1] ^= strs.serial[i];
	}
	ethaddr.ether_addr_octet[0] &= 0xfe;     /* clear multicast bit */
	ethaddr.ether_addr_octet[0] |= 0x02;     /* set local assignment bit (IEEE802) */

	usbg_free_gadget_strs(&strs);

	/* host_addr changes mac address */
	ret = usbg_f_net_set_host_addr(nf, &ethaddr);

	return ret;
}

static int cfs_cleanup_all_config_and_function(struct cfs_client *cfs_client)
{
	int ret;
	usbg_config *config;
	usbg_function *function;

	/* delete all configs */
restart_rm_config:
	usbg_for_each_config(config, cfs_client->gadget) {
		ret = usbg_rm_config(config, USBG_RM_RECURSE);
		if (ret)
			return ret;

		goto restart_rm_config; /* You cannot delete a config directly in an iterator. */
	}

	/* delete all functions */
restart_rm_function:
	usbg_for_each_function(function, cfs_client->gadget) {
		if (usbg_get_function_type(function) == USBG_F_FFS) {
			ret = cfs_cleanup_ffs_service(function);
			if (ret)
				return ret;
		}

		ret = usbg_rm_function(function, USBG_RM_RECURSE);
		if (ret)
			return ret;

		goto restart_rm_function; /* You cannot delete a function directly in an iterator. */
	}

	return 0;
}

static int cfs_set_gadget_config(struct cfs_client *cfs_client, int config_id, struct usb_configuration *usb_config)
{
	int i;
	int ret;
	int function_type;
	usbg_config *config;
	usbg_function *function;
	struct usb_function *usb_func;
	char instance[MAX_INSTANCE_LEN];
	struct usbg_config_attrs cattrs = {
		.bmAttributes = usb_config->attrs.bmAttributs,
		.bMaxPower = usb_config->attrs.MaxPower/2,
	};

	if (!usb_config->funcs || !usb_config->funcs[0])
		return -EINVAL;

	ret = usbg_create_config(cfs_client->gadget, config_id, CONFIGFS_CONFIG_LABEL, &cattrs, NULL, &config);
	if (ret)
		return ret;

	for (i = 0; usb_config->strs && usb_config->strs[i].lang_code; ++i) {
		ret = usbg_set_config_string(config, usb_config->strs[i].lang_code, usb_config->strs[i].config_str);
		if (ret)
			return ret;
	}

	for (i = 0; usb_config->funcs[i]; ++i) {
		usb_func = usb_config->funcs[i];

		/* name("sdb") + NAME_INSTANCE_SEP(".") + instance("default") + '\0' */
		if (strlen(usb_func->name) + strlen(usb_func->instance) + 2 > sizeof(instance))
			return -ENAMETOOLONG;

		/* In functionfs, the instance is used in the format "[sdb|mtp].default" instead of "default" */
		if (usb_func->is_functionfs) {
			function_type = USBG_F_FFS;
			snprintf(instance, sizeof(instance), "%s%c%s", usb_func->name, NAME_INSTANCE_SEP, usb_func->instance);
		} else {
			function_type = usbg_lookup_function_type(usb_func->name);
			strncpy(instance, usb_func->instance, sizeof(instance) - 1);
			instance[sizeof(instance) - 1] = '\0';
		}

		ret = usbg_create_function(cfs_client->gadget, function_type, instance, NULL, &function);
		if (ret)
			return ret;

		/* Setting rndis mac address. This should be done at this point,
		 * since the node host_addr changes to read only after the function
		 * is added to config. */
		if (usbg_get_function_type(function) == USBG_F_RNDIS)
			(void)cfs_set_rndis_mac_addr(cfs_client->gadget, function); /* A random value is used if fails */

		if (usbg_get_function_type(function) == USBG_F_FFS) {
			ret = cfs_prep_ffs_service(usb_func, function);
			if (ret)
				return ret;
		}

		ret = usbg_add_config_function(config, NULL, function);
		if (ret)
			return ret;
	}

	return 0;
}

static int cfs_reconfigure_gadget(struct usb_client *usb,
				  struct usb_gadget *gadget)
{
	int i;
	int ret;
	struct cfs_client *cfs_client;

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

	cfs_client = container_of(usb, struct cfs_client, client);

	ret = cfs_set_gadget_attrs(cfs_client, &gadget->attrs);
	if (ret)
		return ret;

	for (i = 0; gadget->strs && gadget->strs[i].lang_code > 0; ++i) {
		ret = cfs_set_gadget_strs(cfs_client, gadget->strs + i);
		if (ret)
			return ret;
	}

	ret = cfs_cleanup_all_config_and_function(cfs_client);
	if (ret)
		return ret;

	for (i = 0; gadget->configs && gadget->configs[i]; ++i) {
		ret = cfs_set_gadget_config(cfs_client, i + 1, gadget->configs[i]);
		if (ret)
			return ret;
	}

	return 0;
}

static void cfs_start_stop_service_and_handler(usbg_gadget *gadget, enum cfs_function_service_operation operation)
{
	usbg_function *function;
	struct usb_function *usb_function;

	usbg_for_each_function(function, gadget) {
		usb_function = cfs_find_usb_function(function);
		if (!usb_function)
			continue;

		switch(operation) {
		case CFS_FUNCTION_SERVICE_START:
			if (usb_function->handler)
				usb_function->handler(1);

			/* functionfs service is automatically started by socket activation */
			if (!usb_function->is_functionfs && usb_function->service)
				(void)systemd_start_unit_wait_started(usb_function->service, ".service", -1);
			break;

		case CFS_FUNCTION_SERVICE_STOP:
			if (!usb_function->is_functionfs && usb_function->service)
				(void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);

			if (usb_function->handler)
				usb_function->handler(0);
			break;

		case CFS_FUNCTION_SERVICE_POST_STOP:
			if (usb_function->is_functionfs && usb_function->service)
				(void)systemd_stop_unit_wait_stopped(usb_function->service, ".service", -1);
			break;

		default:
			break;
		}
	}
}

static int cfs_enable(struct usb_client *usb)
{
	int ret;
	struct cfs_client *cfs_client;

	if (!usb)
		return -EINVAL;

	cfs_client = container_of(usb, struct cfs_client, client);

	ret = usbg_enable_gadget(cfs_client->gadget, cfs_client->udc);
	if (ret)
		return ret;

	cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_START);

	return 0;
}

static int cfs_disable(struct usb_client *usb)
{
	int ret;
	struct cfs_client *cfs_client;

	if (!usb)
		return -EINVAL;

	cfs_client = container_of(usb, struct cfs_client, client);

	cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_STOP);

	ret = usbg_disable_gadget(cfs_client->gadget); /* ignore error checking */

	/*
	 * Since functionfs service works with socket activation, you must stop it after disabling gadget.
	 * If usb data may come in after stopping functionfs service and before disabling gadget,
	 * functionfs service wakes up again by socket activation.
	 */
	cfs_start_stop_service_and_handler(cfs_client->gadget, CFS_FUNCTION_SERVICE_POST_STOP);

	return ret;
}

EXPORT
int hw_cfs_gadget_open(struct hw_info *info,
		const char *id, struct hw_common **common)
{
	struct cfs_client *cfs_client;
	int ret;

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

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

	ret = usbg_init(CONFIGFS_PATH, &cfs_client->ctx);
	if (ret)
		goto err_usbg_init;

	cfs_client->udc = usbg_get_first_udc(cfs_client->ctx);
	if (!cfs_client->udc) {
		ret = -ENODEV;
		goto err_no_udc;
	}

	ret = usbg_create_gadget(cfs_client->ctx, CONFIGFS_GADGET_NAME,
				 &default_g_attrs, &default_g_strs,
				 &cfs_client->gadget);
	if (ret)
		goto err_create_gadget;

	cfs_client->client.common.info = info;

	cfs_client->client.reconfigure_gadget = cfs_reconfigure_gadget;
	cfs_client->client.enable = cfs_enable;
	cfs_client->client.disable = cfs_disable;

	*common = &cfs_client->client.common;

	return 0;

err_create_gadget:
err_no_udc:
	usbg_cleanup(cfs_client->ctx);
err_usbg_init:
	free(cfs_client);

	return ret;
}

EXPORT
int hw_cfs_gadget_close(struct hw_common *common)
{
	usbg_function *function;
	struct cfs_client *cfs_client;
	struct usb_function *usb_func;

	if (!common)
		return -EINVAL;

	cfs_client = container_of(common, struct cfs_client, client.common);

	usbg_for_each_function(function, cfs_client->gadget) {
		usb_func = cfs_find_usb_function(function);
		if (!usb_func)
			continue;

		if (usb_func->is_functionfs && usb_func->service) {
			(void)systemd_stop_unit_wait_stopped(usb_func->service, ".socket", -1);
			(void)systemd_stop_unit_wait_stopped(usb_func->service, ".service", -1);
		}
	}

	/*
	 * For now we don't check for errors
	 * but we should somehow handle them
	 */
	usbg_rm_gadget(cfs_client->gadget, USBG_RM_RECURSE);
	usbg_cleanup(cfs_client->ctx);
	free(cfs_client);

	return 0;
}