summaryrefslogtreecommitdiff
path: root/hw/usb_gadget.h
blob: 8533228dd84731ee95150e6e0d44e96cb083d0ca (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
/*
 * libdevice-node
 *
 * Copyright (c) 2016 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.
 */

#ifndef __HW_USB_GADGET_H__
#define __HW_USB_GADGET_H__

#include <hw/common.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

/**
 * The id of this device
 */
#define USB_GADGET_DEVICE_ID	"usb_gadget"

/**
 * The version of this device
 */
#define USB_GADGET_DEVICE_VERSION	MAKE_VERSION(0,1)

#define _HELPER_Y(x) ((x) & -(x))

/* Count number of trailing zeros using Dean Gaudet's algorithm */
#define _HELPER_CTZ(mask)					\
	((_HELPER_Y(mask) ? 0 : 1) +				\
	((_HELPER_Y(mask) & 0x0000FFFF) ? 0 : 16) +		\
	((_HELPER_Y(mask) & 0x00FF00FF) ? 0 :  8) +		\
	((_HELPER_Y(mask) & 0x0F0F0F0F) ? 0 :  4) +		\
	((_HELPER_Y(mask) & 0x33333333) ? 0 :  2) +		\
	((_HELPER_Y(mask) & 0x55555555) ? 0 :  1))

/* Function IDX in array is number of trailing zeros */
#define FUNC_IDX_FROM_MASK(mask) _HELPER_CTZ(mask)

/*
 * legacy enable  : enable gadget -> handler(1) -> ffs_service start -> service start
 * legacy disable : service stop -> ffs_service stop -> handler(0) -> disable gadget
 *
 * configfs enable  : ffs_service start -> enable gadget -> handler(1) -> service start
 * configfs disable : service stop -> handler(0) -> disable gadget  (ffs_service is never stopped until changing usb mode)
 */
struct usb_function {
	int id;
	const char *name;
	const char *instance;

	const char *ffs_service; /* only used in configfs */
	const char *service;

	void (*handler)(int enable);

	int (*clone)(struct usb_function *func, struct usb_function **_clone);
	void (*free_func)(struct usb_function *func);
};

struct usb_configuration_attributes {
	uint8_t bmAttributs;
	int MaxPower;
};

struct usb_configuration_strings {
	uint16_t lang_code;
	char *config_str;
};

struct usb_configuration {
	struct usb_configuration_attributes attrs;
	struct usb_configuration_strings *strs;
	struct usb_function **funcs;
};

struct usb_gadget_attrs {
	uint8_t bDeviceClass;
	uint8_t bDeviceSubClass;
	uint8_t bDeviceProtocol;
	uint16_t idVendor;
	uint16_t idProduct;
	uint16_t bcdDevice;
};

struct usb_gadget_strings {
	uint16_t lang_code;
	char *manufacturer;
	char *product;
	char *serial;
};

struct usb_gadget {
	struct usb_gadget_attrs attrs;
	struct usb_gadget_strings *strs;
	struct usb_function **funcs;
	struct usb_configuration **configs;
};

typedef enum {
	USB_FUNCTION_NONE               = 0,
	USB_FUNCTION_MTP                = 1 << 0,
	USB_FUNCTION_ACM                = 1 << 1,
	USB_FUNCTION_SDB                = 1 << 2,
	USB_FUNCTION_RNDIS              = 1 << 3,
	USB_FUNCTION_DIAG               = 1 << 4,
	USB_FUNCTION_CONN_GADGET        = 1 << 5,
	USB_FUNCTION_DM                 = 1 << 6,
	USB_FUNCTION_RMNET              = 1 << 7,
} usb_function_e;

static void free_simple_func(struct usb_function *func)
{
	if (func) {
		free((void *)func->name);
		free((void *)func->instance);
		free((void *)func->service);
		free(func);
	}
}

static int clone_simple_func(struct usb_function *func,
			      struct usb_function **clone)
{
	struct usb_function *other;

	if (!func || !clone)
		return -EINVAL;

	other = (struct usb_function *)calloc(1, sizeof(struct usb_function));
	if (!other)
		return -ENOMEM;

	*other = *func;

	other->name = strdup(func->name);
	other->instance = strdup(func->instance);
	if (!other->name || !other->instance)
		goto out_nomem;

	if (func->service) {
		other->service = strdup(func->service);
		if (!other->service)
			goto out_nomem;
	}

	*clone = other;
	return 0;

out_nomem:
	free_simple_func(other);
	return -ENOMEM;
}

#define DEFINE_USB_FUNCTION(_id, _name, _ffs_service, _service, _handler)	\
	static struct usb_function _##_name##_function = {                      \
		.id = _id,                                                      \
		.name = #_name,                                                 \
		.instance = "default",                                          \
		.ffs_service = _ffs_service,                                    \
		.service = _service,                                            \
		.handler = _handler,                                            \
		.clone = clone_simple_func,                                     \
		.free_func = free_simple_func,                                  \
	}

DEFINE_USB_FUNCTION(USB_FUNCTION_DIAG,        diag,        NULL, NULL,            NULL);
DEFINE_USB_FUNCTION(USB_FUNCTION_RMNET,       rmnet,       NULL, NULL,            NULL);
DEFINE_USB_FUNCTION(USB_FUNCTION_DM,          dm,          NULL, NULL,            NULL);
DEFINE_USB_FUNCTION(USB_FUNCTION_CONN_GADGET, conn_gadget, NULL, NULL,            NULL);
DEFINE_USB_FUNCTION(USB_FUNCTION_SDB,         sdb,         "sdbd", NULL,          NULL);
DEFINE_USB_FUNCTION(USB_FUNCTION_MTP,         mtp,         "mtp-responder", NULL, NULL);
DEFINE_USB_FUNCTION(USB_FUNCTION_ACM,         acm,         NULL, "data-router",   NULL);
DEFINE_USB_FUNCTION(USB_FUNCTION_RNDIS,       rndis,       NULL, "rndis",         NULL);
#undef DEFINE_USB_FUNCTION

#define MAKE_FUNC_AVAILABLE(_name, _vname)  \
	[FUNC_IDX_FROM_MASK(USB_FUNCTION_##_name)] = &_##_vname##_function

static struct usb_function *_available_funcs[] = {
	MAKE_FUNC_AVAILABLE(MTP, mtp),
	MAKE_FUNC_AVAILABLE(ACM, acm),
	MAKE_FUNC_AVAILABLE(SDB, sdb),
	MAKE_FUNC_AVAILABLE(RNDIS, rndis),
	MAKE_FUNC_AVAILABLE(DIAG, diag),
	MAKE_FUNC_AVAILABLE(CONN_GADGET, conn_gadget),
	MAKE_FUNC_AVAILABLE(DM, dm),
	MAKE_FUNC_AVAILABLE(RMNET, rmnet),
};
#undef MAKE_FUNC_AVAILABLE

struct usb_gadget_id {
	unsigned int function_mask;
};

struct usb_gadget_translator {
	struct hw_common common;

	int (*id_to_gadget)(struct usb_gadget_id *gadget_id,
			 struct usb_gadget **gadget);

	void (*cleanup_gadget)(struct usb_gadget *gadget);
};

int simple_translator_open(struct hw_info *info,
		const char *id, struct hw_common **common);
int simple_translator_close(struct hw_common *common);

#endif