4dd3be909adb5879f98cd55406e3424333465e63
[platform/core/system/libdevice-node.git] / hw / usb_gadget.h
1 /*
2  * libdevice-node
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifndef __HW_USB_GADGET_H__
20 #define __HW_USB_GADGET_H__
21
22 #include <stdint.h>
23
24 #include <hw/common.h>
25
26 /* The id of this device */
27 #define USB_GADGET_DEVICE_ID    "usb_gadget"
28
29 /*The version of this device */
30 #define USB_GADGET_DEVICE_VERSION       MAKE_VERSION(0,1)
31
32 /*The default USB configuration */
33 #define DEFAULT_VID 0x04e8
34 #define DEFAULT_PID 0x6860
35 #define DEFAULT_BCD_DEVICE 0x0100
36
37 #define DEFAULT_LANG 0x409 /* US_en */
38 #define DEFAULT_MANUFACTURER "Samsung"
39 #define DEFAULT_PRODUCT "TIZEN"
40 #define DEFAULT_SERIAL "01234TEST"
41
42 #define DEFAULT_BMATTRIBUTES ((1 << 7) | (1 << 6))
43 #define DEFAULT_MAX_POWER 500
44
45 typedef enum {
46         USB_FUNCTION_IDX_MTP         = 0,
47         USB_FUNCTION_IDX_ACM         = 1,
48         USB_FUNCTION_IDX_SDB         = 2,
49         USB_FUNCTION_IDX_RNDIS       = 3,
50         USB_FUNCTION_IDX_DIAG        = 4,
51         USB_FUNCTION_IDX_CONN_GADGET = 5,
52         USB_FUNCTION_IDX_DM          = 6,
53         USB_FUNCTION_IDX_RMNET       = 7,
54         USB_FUNCTION_IDX_MAX         = USB_FUNCTION_IDX_RMNET + 1
55 } usb_function_idx_e;
56
57 typedef enum {
58         USB_FUNCTION_NONE        = 0,
59         USB_FUNCTION_MTP         = 1 << USB_FUNCTION_IDX_MTP,
60         USB_FUNCTION_ACM         = 1 << USB_FUNCTION_IDX_ACM,
61         USB_FUNCTION_SDB         = 1 << USB_FUNCTION_IDX_SDB,
62         USB_FUNCTION_RNDIS       = 1 << USB_FUNCTION_IDX_RNDIS,
63         USB_FUNCTION_DIAG        = 1 << USB_FUNCTION_IDX_DIAG,
64         USB_FUNCTION_CONN_GADGET = 1 << USB_FUNCTION_IDX_CONN_GADGET,
65         USB_FUNCTION_DM          = 1 << USB_FUNCTION_IDX_DM,
66         USB_FUNCTION_RMNET       = 1 << USB_FUNCTION_IDX_RMNET
67 } usb_function_e;
68
69
70 /*
71  * legacy enable(usb plug)    : enable gadget -> handler(1) -> service start
72  * legacy disable(usb unplug) : service stop ->  handler(0) -> disable gadget
73  *
74  * configfs init(booting)       : service.socket start
75  * configfs enable(usb plug)*   : enable gadget -> handler(1)
76  * configfs disable(usb unplug) : handler(0) -> disable gadget -> service.service stop
77  * configfs deinit              : service.socket stop
78  *
79  * Since functionfs of configfs works by socket activation,
80  * it will be started automatically when data is enqueued to the usb socket.
81  * So when enabling configfs gadget, it doesn't start the service for functionfs.
82  */
83 struct usb_function {
84         int id;
85         const char *name;
86         const char *instance;
87
88         int is_functionfs;
89         const char *service;
90
91         void (*handler)(int enable);
92 };
93
94 struct usb_configuration_attributes {
95         uint8_t bmAttributs;
96         int MaxPower;
97 };
98
99 struct usb_configuration_strings {
100         uint16_t lang_code;
101         char *config_str;
102 };
103
104 struct usb_configuration {
105         struct usb_configuration_attributes attrs;
106         struct usb_configuration_strings strs;
107         struct usb_function **funcs;
108 };
109
110 struct usb_gadget_attrs {
111         uint8_t bDeviceClass;
112         uint8_t bDeviceSubClass;
113         uint8_t bDeviceProtocol;
114         uint16_t idVendor;
115         uint16_t idProduct;
116         uint16_t bcdDevice;
117 };
118
119 struct usb_gadget_strings {
120         uint16_t lang_code;
121         char *manufacturer;
122         char *product;
123         char *serial;
124 };
125
126 struct usb_gadget {
127         struct usb_gadget_attrs attrs;
128         struct usb_gadget_strings strs;
129         struct usb_function **funcs;
130         struct usb_configuration **configs;
131 };
132
133 struct usb_gadget_id {
134         unsigned int function_mask;
135 };
136
137 struct usb_gadget_translator {
138         struct hw_common common;
139
140         int (*id_to_gadget)(struct usb_gadget_id *gadget_id,  struct usb_gadget **gadget);
141         void (*cleanup_gadget)(struct usb_gadget *gadget);
142 };
143
144 int simple_translator_open(struct hw_info *info, const char *id, struct hw_common **common);
145 int simple_translator_close(struct hw_common *common);
146
147 struct usb_function *find_usb_function_by_name(const char *name);
148 struct usb_function *find_usb_function_by_name_instance(const char *name, const char *instance);
149
150 #endif