1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef USBD_TYPE_H
17 #define USBD_TYPE_H
18 
19 #include <stdint.h>
20 #include <cstdlib>
21 
22 #define USB_MAX_INTERFACES 32
23 
24 #define HDF_USB_USBD_DESC "hdf.usb.usbd"
25 #define HDF_USB_USBFN_DESC "hdf.usb.usbfn"
26 
27 /**
28  * Bitmask used for extracting the USBEndpoint direction from it's address
29  */
30 const int32_t USB_ENDPOINT_DIR_MASK = 0x80;
31 
32 /**
33  * Used to signify direction of data for USBEndpoint is IN, device to host
34  */
35 const int32_t USB_ENDPOINT_DIR_IN = 0x80;
36 
37 /**
38  * Used to signify direction of data for USBEndpoint is OUT, host to device
39  */
40 const int32_t USB_ENDPOINT_DIR_OUT = 0;
41 
42 typedef void (*UsbdRequestCallback)(uint8_t *requestArg);
43 
44 enum UsbdDeviceAction {
45     ACT_DEVUP = 0,
46     ACT_DEVDOWN,
47     ACT_UPDEVICE,
48     ACT_DOWNDEVICE,
49     ACT_ACCESSORYUP,
50     ACT_ACCESSORYDOWN,
51     ACT_ACCESSORYSEND
52 };
53 
54 enum UsbdCmd {
55     CMD_NOTIFY_DEVICE_DOWN,
56     CMD_NOTIFY_DEVICE_FOUND,
57     CMD_CLASS_CTRL_SYNC,
58     CMD_FUN_ADD_FUNCTION,
59     CMD_FUN_ADD_INTERFACE,
60     CMD_FUN_CONNECT_DEVICE,
61     CMD_FUN_DELETE_INTERFACE,
62     CMD_FUN_GET_INTERFACE_DESCRIPTOR,
63     CMD_FUN_GET_ENDPOINT_DESCRIPTOR,
64     CMD_FUN_HANDLE_REQUEST,
65     CMD_FUN_REMOVE_FUNCTION,
66     CMD_FUN_REMOVE_INTERFACE,
67     CMD_FUN_SEND_CTRL_READ_ASYNC,
68     CMD_FUN_SEND_CTRL_READ_SYNC,
69     CMD_FUN_SEND_CTRL_REQUEST_ASYNC,
70     CMD_FUN_SEND_CTRL_WRITE_ASYNC,
71     CMD_FUN_SEND_CTRL_WRITE_SYNC,
72     CMD_FUN_SEND_INTERRUPT_READ_ASYNC,
73     CMD_FUN_SEND_INTERRUPT_WRITE_ASYNC,
74     CMD_FUN_SEND_ISO_READ_ASYNC,
75     CMD_FUN_SEND_ISO_WRITE_ASYNC,
76     CMD_ADD_INTERFACE,
77     CMD_REMOVE_INTERFACE,
78     CMD_GET_CHARGE_STATE,
79     CMD_GET_DEVICES,
80     CMD_HAS_RIGHT,
81     CMD_READ_DATA_SYNC,
82     CMD_READ_PARM,
83     CMD_SET_BAUDRATE,
84     CMD_STD_CTRL_GET_CONFIGURATION,
85     CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC,
86     CMD_STD_CTRL_GET_DESCRIPTOR_CMD,
87     CMD_STD_CTRL_GET_INTERFACE,
88     CMD_STD_CTRL_GET_STATUS_CMD,
89 };
90 
91 struct UsbdDevice {
92     uint8_t busNum;
93     uint8_t devAddr;
94     uint8_t interface;
95     uint8_t endPoint;
96 };
97 
98 struct UsbdPeripheral {
99     uint8_t busNum;
100     uint8_t devAddr;
101     uint8_t iSerialNumber;
102 };
103 
104 struct UsbdPort {
105     uint8_t modes;
106     uint8_t roles;
107 };
108 
109 struct UsbdRequestDataSync {
110     uint8_t endPoint;
111     int32_t *requested;
112     uint8_t *data;
113     int32_t length;
114     uint32_t timeout;
115 };
116 
117 struct UsbdRequestDataAsync {
118     unsigned char endPoint;
119     unsigned char *buffer;
120     int32_t length;
121     int32_t numIsoPackets;
122     UsbdRequestCallback callback;
123     uint8_t *userData;
124     uint32_t timeout;
125 };
126 
127 struct UsbdControlRequestSetupData {
128     uint8_t requestType;
129     uint8_t requestCmd;
130     uint16_t value;
131     uint16_t index;
132     uint8_t *data;
133     uint16_t length;
134     uint32_t timeout;
135 };
136 
137 struct UsbdDescriptorParam {
138     uint8_t descType;
139     uint8_t descIndex;
140     int32_t length;
141 };
142 
143 struct UsbdDescriptorHeader {
144     uint8_t bLength;
145     uint8_t bDescriptorType;
146 } __attribute__((packed));
147 
148 struct UsbdDeviceDescriptor {
149     uint8_t bLength;
150     uint8_t bDescriptorType;
151     uint16_t bcdUSB;
152     uint8_t bDeviceClass;
153     uint8_t bDeviceSubClass;
154     uint8_t bDeviceProtocol;
155     uint8_t bMaxPacketSize0;
156     uint16_t idVendor;
157     uint16_t idProduct;
158     uint16_t bcdDevice;
159     uint8_t iManufacturer;
160     uint8_t iProduct;
161     uint8_t iSerialNumber;
162     uint8_t bNumConfigurations;
163 } __attribute__((packed));
164 
165 struct UsbdConfigDescriptor {
166     uint8_t bLength;
167     uint8_t bDescriptorType;
168     uint16_t wTotalLength;
169     uint8_t bNumInterfaces;
170     uint8_t bConfigurationValue;
171     uint8_t iConfiguration;
172     uint8_t bmAttributes;
173     uint8_t bMaxPower;
174 } __attribute__((packed));
175 
176 struct UsbdInterfaceDescriptor {
177     uint8_t bLength;
178     uint8_t bDescriptorType;
179     uint8_t bInterfaceNumber;
180     uint8_t bAlternateSetting;
181     uint8_t bNumEndpoints;
182     uint8_t bInterfaceClass;
183     uint8_t bInterfaceSubClass;
184     uint8_t bInterfaceProtocol;
185     uint8_t iInterface;
186 } __attribute__((packed));
187 
188 struct UsbdEndpointDescriptor {
189     uint8_t bLength;
190     uint8_t bDescriptorType;
191     uint8_t bEndpointAddress;
192     uint8_t bmAttributes;
193     uint16_t wMaxPacketSize;
194     uint8_t bInterval;
195 } __attribute__((packed));
196 
197 struct UsbNotifyServiceInfo {
198     uint32_t length;
199     int32_t devNum;
200     int32_t busNum;
201     int32_t interfaceLength;
202     uint8_t interfaceNumber[USB_MAX_INTERFACES];
203 } __attribute__((packed));
204 
205 enum PortRoleType { DATA_ROLE, POWER_ROLE, MODE };
206 
207 enum PortPowerRole { SOURCE, SINK };
208 
209 enum PortMode { HOST, DEVICE, OTG };
210 
211 struct UsbdInfo {
212     int32_t capacity;
213     int32_t voltage;
214     int32_t temperature;
215     int32_t healthState;
216     int32_t pluggedType;
217     int32_t pluggedMaxCurrent;
218     int32_t pluggedMaxVoltage;
219     int32_t chargeState;
220     int32_t chargeCounter;
221     int8_t present;
222     const char *technology;
223 };
224 
225 #endif // USBD_TYPE_H
226