summaryrefslogtreecommitdiffstats
path: root/drivers/usb/otg/otgdev.c
blob: 7017796e8cb18f97796960c9ea726bfa6404b8b8 (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
// SPDX-License-Identifier: GPL-2.0

#include <common.h>
#include <driver.h>
#include <usb/usb.h>

static int (*set_mode_callback)(void *ctx, enum usb_dr_mode mode);
static unsigned int otg_mode;

static int otg_set_mode(struct param_d *param, void *ctx)
{
	static int cur_mode = USB_DR_MODE_OTG;
	int ret;

	if (otg_mode == USB_DR_MODE_UNKNOWN)
		return -EINVAL;

	if (otg_mode == cur_mode)
		return 0;

	if (cur_mode != USB_DR_MODE_OTG)
		return -EBUSY;

	ret = set_mode_callback(ctx, otg_mode);
	if (ret)
		return ret;

	cur_mode = otg_mode;

	return 0;
}

static const char *otg_mode_names[] = {
	[USB_DR_MODE_UNKNOWN] = "unknown",
	[USB_DR_MODE_HOST] = "host",
	[USB_DR_MODE_PERIPHERAL] = "peripheral",
	[USB_DR_MODE_OTG] = "otg",
};

static struct device_d otg_device = {
	.name = "otg",
	.id = DEVICE_ID_SINGLE,
};

int usb_register_otg_device(struct device_d *parent,
			    int (*set_mode)(void *ctx, enum usb_dr_mode mode), void *ctx)
{
	int ret;
	struct param_d *param_mode;

	if (otg_device.parent)
		return -EBUSY;

	otg_device.parent = parent;
	set_mode_callback = set_mode;
	otg_mode = USB_DR_MODE_OTG;

	ret = register_device(&otg_device);
	if (ret)
		return ret;

	param_mode = dev_add_param_enum(&otg_device, "mode",
			otg_set_mode, NULL, &otg_mode,
			otg_mode_names, ARRAY_SIZE(otg_mode_names), ctx);
	if (IS_ERR(param_mode))
		return PTR_ERR(param_mode);

	return 0;
}