1 /*
2  * Copyright (c) 2021 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 USB_RAW_SDK_IF_TEST_H
17 #define USB_RAW_SDK_IF_TEST_H
18 
19 #include "hdf_base.h"
20 #include "hdf_device_desc.h"
21 #include "osal_atomic.h"
22 #include "usb_raw_api.h"
23 #include "data_fifo.h"
24 
25 #define USB_MAX_INTERFACES     32
26 #define USB_CTRL_REQ_SIZE          64
27 
28 typedef enum {
29     CMD_OPEN_PARM = 0,
30     CMD_CLOSE_PARM,
31     CMD_WRITE_PARM,
32     CMD_READ_PARM,
33     CMD_GET_BAUDRATE,
34     CMD_SET_BAUDRATE,
35     CMD_WRITE_DATA_SYNC,
36     CMD_READ_DATA_SYNC,
37     CMD_CLASS_CTRL_SYNC,
38     CMD_STD_CTRL_GET_DESCRIPTOR_CMD,
39     CMD_STD_CTRL_GET_STATUS_CMD,
40     CMD_STD_CTRL_GET_CONFIGURATION,
41     CMD_STD_CTRL_GET_INTERFACE,
42     CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC,
43 } SerialOPCmd;
44 
45 #define DATARATE                     9600
46 #define DATA_BITS_LENGTH             8
47 #define ACM_NW                       16
48 #define ACM_NR                       16
49 #define READ_BUF_SIZE                8192
50 #define USB_CTRL_SET_TIMEOUT         0
51 #define USB_PIPE_DIR_OFFSET          7
52 
53 struct AcmDevice;
54 struct AcmWb {
55     struct UsbRawRequest *request;
56     struct AcmDevice *instance;
57     uint8_t *buf;
58     uint32_t len;
59     int32_t use;
60 };
61 
62 struct AcmRb {
63     uint8_t *base;
64     int32_t size;
65     int32_t index;
66     int32_t use;
67     struct AcmDevice *instance;
68 };
69 
70 struct SerialDevice {
71     struct AcmDevice *acm;
72     struct UsbCdcLineCoding lineCoding;
73     struct OsalMutex lock;
74     struct DataFifo readFifo;
75 };
76 
77 struct UsbEndpoint {
78     uint8_t addr;
79     uint8_t interval;
80     uint16_t maxPacketSize;
81 };
82 
83 struct AcmDevice {
84     struct IDeviceIoService service;
85     struct HdfDeviceObject *device;
86     uint8_t ctrlIface;
87     uint8_t dataIface;
88     struct UsbEndpoint *notifyEp;
89     struct UsbEndpoint *dataInEp;
90     struct UsbEndpoint *dataOutEp;
91     struct UsbRawConfigDescriptor *config;
92     struct AcmWb wb[ACM_NW];
93     struct AcmRb rb[ACM_NR];
94     struct OsalMutex writeLock;
95     struct OsalMutex readLock;
96     struct UsbRawRequest *notifyReq;
97     struct UsbRawRequest *readReq[ACM_NR];
98     struct UsbRawRequest *writeReq;
99     struct UsbRawRequest *ctrlReq;
100     struct OsalMutex lock;
101     UsbRawHandle *devHandle;
102     struct UsbSession *session;
103     struct SerialDevice *port;
104     uint32_t nbIndex;
105     uint32_t nbSize;
106     int32_t transmitting;
107     uint8_t busNum;
108     uint8_t devAddr;
109     uint8_t interfaceCnt;
110     uint8_t *notificationBuffer;
111     uint8_t interfaceIndex[USB_MAX_INTERFACES];
112     struct UsbCdcLineCoding lineCoding;
113     struct OsalThread ioThread;
114 };
115 
116 typedef void (*UsbRequestCallback)(struct UsbRequest *request);
117 
118 typedef enum {
119     /* select a configuration for the device */
120     USB_REQUEST_SELECT_CONFIGURATION,
121     /* select an alternate setting for an interface */
122     USB_REQUEST_SELECT_INTERFACE,
123     /* get the device descriptor from a specific USB device/interface/endpoint */
124     USB_REQUEST_GET_DESCRIPTOR,
125     /* set descriptor on a device/interface/endpoint */
126     USB_REQUEST_SET_DESCRIPTOR,
127     /* set a feature on a device/interface/endpoint/other */
128     USB_REQUEST_SET_FEATURE,
129     /* clear a USB-defined feature on a device/interface/endpoint/other */
130     USB_REQUEST_CLEAR_FEATURE,
131     /* get status form a USB device/interface/endpoint/other */
132     USB_REQUEST_GET_STATUS,
133     /* get the current configuration on a USB device */
134     USB_REQUEST_GET_CONFIGURATION = 0x08,
135     /* get the current setting for an interface */
136     USB_REQUEST_GET_INTERFACE,
137 } UsbStandardRequest;
138 
139 typedef enum {
140     USB_REQUEST_TYPE_CONTROL = 0U,
141     USB_REQUEST_TYPE_ISOCHRONOUS = 1U,
142     USB_REQUEST_TYPE_BULK = 2U,
143     USB_REQUEST_TYPE_INTERRUPT = 3U,
144 } UsbRequestType;
145 
146 struct UsbIsoPacketDesc {
147     unsigned int length;
148     unsigned int actualLength;
149     unsigned int status;
150 };
151 
152 struct UsbAdapterUrb {
153     unsigned char type;
154     unsigned char endPoint;
155     int32_t status;
156     unsigned int flags;
157     void *buffer;
158     int32_t bufferLength;
159     int32_t actualLength;
160     int32_t startFrame;
161     int32_t numberOfPackets;
162     int32_t errorCount;
163     unsigned int signr;
164     void *userContext;
165     struct UsbIsoPacketDesc isoFrameDesc[0];
166 };
167 
168 /* Note:The first five fields of the definition must be consistent with the UsbRawRequest structure definition. */
169 struct UsbHostRequest {
170     /* the address of data buffer */
171     unsigned char *buffer;
172     /* the length of data buffer */
173     uint32_t length;
174     /* the actual length of the requested data */
175     int32_t actualLength;
176     /* the status of the request */
177     UsbRequestStatus status;
178     /* private user data */
179     void *userData;
180 
181     /* the following fields are not visible to user */
182     struct UsbDeviceHandle *devHandle;
183     UsbRawRequestCallback callback;
184     UsbRequestCallback userCallback;
185     /* the length of data buffer */
186     int32_t bufLen;
187     struct OsalSem sem;
188     unsigned char endPoint;
189     unsigned int timeout;
190     unsigned char requestType;
191     union {
192         void *urbs;
193         void **isoUrbs;
194     };
195     int32_t numUrbs;
196     int32_t numRetired;
197     UsbRequestStatus reqStatus;
198     int32_t isoPacketOffset;
199     struct OsalMutex lock;
200     void *privateObj;
201     int32_t numIsoPackets;
202     /* isopacketdesc ptr */
203     struct UsbIsoPacketDesc isoPacketDesc[0];
204 }__attribute__((aligned(4)));
205 
206 /* Note:The first seven fields of this definition must be consistent with the UsbRawFillRequestData
207   * structure definition.
208   */
209 struct UsbFillRequestData {
210     unsigned char endPoint;
211     unsigned char *buffer;
212     uint32_t length;
213     int32_t numIsoPackets;
214     UsbRawRequestCallback callback;
215     void *userData;
216     unsigned int timeout;
217     UsbRequestCallback userCallback;
218 };
219 
220 #endif /* USB_DDK_RAW_SDK_IF_TEST_H */
221