• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
50 
51 enum UsbdCmd {
52     CMD_NOTIFY_DEVICE_DOWN,
53     CMD_NOTIFY_DEVICE_FOUND,
54     CMD_CLASS_CTRL_SYNC,
55     CMD_FUN_ADD_FUNCTION,
56     CMD_FUN_ADD_INTERFACE,
57     CMD_FUN_CONNECT_DEVICE,
58     CMD_FUN_DELETE_INTERFACE,
59     CMD_FUN_GET_INTERFACE_DESCRIPTOR,
60     CMD_FUN_GET_ENDPOINT_DESCRIPTOR,
61     CMD_FUN_HANDLE_REQUEST,
62     CMD_FUN_REMOVE_FUNCTION,
63     CMD_FUN_REMOVE_INTERFACE,
64     CMD_FUN_SEND_CTRL_READ_ASYNC,
65     CMD_FUN_SEND_CTRL_READ_SYNC,
66     CMD_FUN_SEND_CTRL_REQUEST_ASYNC,
67     CMD_FUN_SEND_CTRL_WRITE_ASYNC,
68     CMD_FUN_SEND_CTRL_WRITE_SYNC,
69     CMD_FUN_SEND_INTERRUPT_READ_ASYNC,
70     CMD_FUN_SEND_INTERRUPT_WRITE_ASYNC,
71     CMD_FUN_SEND_ISO_READ_ASYNC,
72     CMD_FUN_SEND_ISO_WRITE_ASYNC,
73     CMD_ADD_INTERFACE,
74     CMD_REMOVE_INTERFACE,
75     CMD_GET_CHARGE_STATE,
76     CMD_GET_DEVICES,
77     CMD_HAS_RIGHT,
78     CMD_READ_DATA_SYNC,
79     CMD_READ_PARM,
80     CMD_SET_BAUDRATE,
81     CMD_STD_CTRL_GET_CONFIGURATION,
82     CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC,
83     CMD_STD_CTRL_GET_DESCRIPTOR_CMD,
84     CMD_STD_CTRL_GET_INTERFACE,
85     CMD_STD_CTRL_GET_STATUS_CMD,
86 };
87 
88 struct UsbdDevice {
89     uint8_t busNum;
90     uint8_t devAddr;
91     uint8_t interface;
92     uint8_t endPoint;
93 };
94 
95 struct UsbdPeripheral {
96     uint8_t busNum;
97     uint8_t devAddr;
98     uint8_t iSerialNumber;
99 };
100 
101 struct UsbdPort {
102     uint8_t modes;
103     uint8_t roles;
104 };
105 
106 struct UsbdRequestDataSync {
107     uint8_t endPoint;
108     int32_t *requested;
109     uint8_t *data;
110     int32_t length;
111     uint32_t timeout;
112 };
113 
114 struct UsbdRequestDataAsync {
115     unsigned char endPoint;
116     unsigned char *buffer;
117     int32_t length;
118     int32_t numIsoPackets;
119     UsbdRequestCallback callback;
120     uint8_t *userData;
121     uint32_t timeout;
122 };
123 
124 struct UsbdControlRequestSetupData {
125     uint8_t requestType;
126     uint8_t requestCmd;
127     uint16_t value;
128     uint16_t index;
129     uint8_t *data;
130     uint16_t length;
131     uint32_t timeout;
132 };
133 
134 struct UsbdDescriptorParam {
135     uint8_t descType;
136     uint8_t descIndex;
137     int32_t length;
138 };
139 
140 struct UsbdDescriptorHeader {
141     uint8_t bLength;
142     uint8_t bDescriptorType;
143 } __attribute__((packed));
144 
145 struct UsbdDeviceDescriptor {
146     uint8_t bLength;
147     uint8_t bDescriptorType;
148     uint16_t bcdUSB;
149     uint8_t bDeviceClass;
150     uint8_t bDeviceSubClass;
151     uint8_t bDeviceProtocol;
152     uint8_t bMaxPacketSize0;
153     uint16_t idVendor;
154     uint16_t idProduct;
155     uint16_t bcdDevice;
156     uint8_t iManufacturer;
157     uint8_t iProduct;
158     uint8_t iSerialNumber;
159     uint8_t bNumConfigurations;
160 } __attribute__((packed));
161 
162 struct UsbdConfigDescriptor {
163     uint8_t bLength;
164     uint8_t bDescriptorType;
165     uint16_t wTotalLength;
166     uint8_t bNumInterfaces;
167     uint8_t bConfigurationValue;
168     uint8_t iConfiguration;
169     uint8_t bmAttributes;
170     uint8_t bMaxPower;
171 } __attribute__((packed));
172 
173 struct UsbdInterfaceDescriptor {
174     uint8_t bLength;
175     uint8_t bDescriptorType;
176     uint8_t bInterfaceNumber;
177     uint8_t bAlternateSetting;
178     uint8_t bNumEndpoints;
179     uint8_t bInterfaceClass;
180     uint8_t bInterfaceSubClass;
181     uint8_t bInterfaceProtocol;
182     uint8_t iInterface;
183 } __attribute__((packed));
184 
185 struct UsbdEndpointDescriptor {
186     uint8_t bLength;
187     uint8_t bDescriptorType;
188     uint8_t bEndpointAddress;
189     uint8_t bmAttributes;
190     uint16_t wMaxPacketSize;
191     uint8_t bInterval;
192 } __attribute__((packed));
193 
194 struct UsbNotifyServiceInfo {
195     uint32_t length;
196     int32_t devNum;
197     int32_t busNum;
198     int32_t interfaceLength;
199     uint8_t interfaceNumber[USB_MAX_INTERFACES];
200 } __attribute__((packed));
201 
202 enum PortRoleType { DATA_ROLE, POWER_ROLE, MODE };
203 
204 enum PortPowerRole { SOURCE, SINK };
205 
206 enum PortMode { HOST, DEVICE, OTG };
207 
208 struct UsbdInfo {
209     int32_t capacity;
210     int32_t voltage;
211     int32_t temperature;
212     int32_t healthState;
213     int32_t pluggedType;
214     int32_t pluggedMaxCurrent;
215     int32_t pluggedMaxVoltage;
216     int32_t chargeState;
217     int32_t chargeCounter;
218     int8_t present;
219     const char *technology;
220 };
221 
222 #endif // USBD_TYPE_H
223