summaryrefslogtreecommitdiff
path: root/hw/usb_gadget.h
blob: 95ad8592428afa2ed61f55b956e366b2249577b0 (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
/*
 * 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 <stdint.h>

#include <hw/common.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)

/*The default USB configuration */
#define DEFAULT_VID 0x04e8
#define DEFAULT_PID 0x6860
#define DEFAULT_BCD_DEVICE 0x0100

#define DEFAULT_LANG 0x409 /* US_en */
#define DEFAULT_MANUFACTURER "Samsung"
#define DEFAULT_PRODUCT "TIZEN"
#define DEFAULT_SERIAL "01234TEST"

#define DEFAULT_BMATTRIBUTES ((1 << 7) | (1 << 6))
#define DEFAULT_MAX_POWER 500

typedef enum {
	USB_FUNCTION_IDX_MTP         = 0,
	USB_FUNCTION_IDX_ACM         = 1,
	USB_FUNCTION_IDX_SDB         = 2,
	USB_FUNCTION_IDX_RNDIS       = 3,
	USB_FUNCTION_IDX_DIAG        = 4,
	USB_FUNCTION_IDX_CONN_GADGET = 5,
	USB_FUNCTION_IDX_DM          = 6,
	USB_FUNCTION_IDX_RMNET       = 7,
	USB_FUNCTION_IDX_MAX         = USB_FUNCTION_IDX_RMNET + 1
} usb_function_idx_e;

typedef enum {
	USB_FUNCTION_NONE        = 0,
	USB_FUNCTION_MTP         = 1 << USB_FUNCTION_IDX_MTP,
	USB_FUNCTION_ACM         = 1 << USB_FUNCTION_IDX_ACM,
	USB_FUNCTION_SDB         = 1 << USB_FUNCTION_IDX_SDB,
	USB_FUNCTION_RNDIS       = 1 << USB_FUNCTION_IDX_RNDIS,
	USB_FUNCTION_DIAG        = 1 << USB_FUNCTION_IDX_DIAG,
	USB_FUNCTION_CONN_GADGET = 1 << USB_FUNCTION_IDX_CONN_GADGET,
	USB_FUNCTION_DM          = 1 << USB_FUNCTION_IDX_DM,
	USB_FUNCTION_RMNET       = 1 << USB_FUNCTION_IDX_RMNET
} usb_function_e;


/*
 * legacy enable(usb plug)    : enable gadget -> handler(1) -> service start
 * legacy disable(usb unplug) : service stop ->  handler(0) -> disable gadget
 *
 * configfs init(booting)       : service.socket start
 * configfs enable(usb plug)*   : enable gadget -> handler(1)
 * configfs disable(usb unplug) : handler(0) -> disable gadget -> service.service stop
 * configfs deinit              : service.socket stop
 *
 * Since functionfs of configfs works by socket activation,
 * it will be started automatically when data is enqueued to the usb socket.
 * So when enabling configfs gadget, it doesn't start the service for functionfs.
 */
struct usb_function {
	int id;
	const char *name;
	const char *instance;

	int is_functionfs;
	const char *service;

	void (*handler)(int enable);
};

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;
};

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);

struct usb_function *find_usb_function_by_name(const char *name);
struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance);

#endif