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 HDF_USB_SERIAL_H
17 #define HDF_USB_SERIAL_H
18 
19 #include "data_fifo.h"
20 #include "hdf_base.h"
21 #include "hdf_device_desc.h"
22 #include "usb_ddk.h"
23 #include "usb_ddk_interface.h"
24 
25 #define USB_MAX_INTERFACES      32
26 #define DATARATE                9600
27 #define DATA_BITS_LENGTH        8
28 #define ACM_NW                  16
29 #define ACM_NR                  16
30 #define READ_BUF_SIZE           8192
31 #define DIRECTION_MASK          0x1
32 #define USB_CTRL_SET_TIMEOUT    5000
33 #define USB_PIPE_DIR_OFFSET     7
34 
35 typedef enum {
36     CMD_OPEN_PARM = 0,
37     CMD_CLOSE_PARM,
38     CMD_WRITE_PARM,
39     CMD_READ_PARM,
40     CMD_GET_BAUDRATE,
41     CMD_SET_BAUDRATE,
42     CMD_WRITE_DATA_SYNC,
43     CMD_READ_DATA_SYNC,
44     CMD_CLASS_CTRL_SYNC,
45     CMD_STD_CTRL_GET_DESCRIPTOR_CMD,
46     CMD_STD_CTRL_GET_STATUS_CMD,
47     CMD_STD_CTRL_GET_CONFIGURATION,
48     CMD_STD_CTRL_GET_INTERFACE,
49     CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC,
50     CMD_ADD_INTERFACE,
51     CMD_REMOVE_INTERFACE,
52 } SerialOPCmd;
53 
54 typedef enum {
55     HOST_ACM_SYNC_READ = 1,
56     HOST_ACM_SYNC_WRITE,
57     HOST_ACM_ASYNC_READ,
58     HOST_ACM_ASYNC_WRITE,
59     HOST_ACM_CTRL_READ,
60     HOST_ACM_CTRL_WRITE,
61     HOST_ACM_CTRL_CLASS_SYNC,
62     HOST_ACM_CTRL_GET_STATUS,
63     HOST_ACM_CTRL_SYNC_DESCRIPTOR,
64     HOST_ACM_CTRL_ASYNC_DESCRIPTOR,
65     HOST_ACM_CTRL_GET_CONFIGURATION,
66     HOST_ACM_CTRL_GET_INTERFACE,
67     HOST_ACM_SPEED_TEST,
68     HOST_ACM_SET_BAUDRATE,
69     HOST_ACM_GET_BAUDRATE,
70     HOST_ACM_ADD_INTERFACE,
71     HOST_ACM_REMOVE_INTERFACE,
72 } AcmModuleTestCmdType;
73 
74 struct AcmDevice;
75 
76 struct AcmWb {
77     struct UsbRequest *request;
78     struct AcmDevice *instance;
79     uint8_t *buf;
80     uint32_t len;
81     int32_t use;
82 };
83 
84 struct AcmRb {
85     uint8_t *base;
86     int32_t size;
87     int32_t index;
88     int32_t use;
89     struct AcmDevice *instance;
90 };
91 
92 struct SerialDevice {
93     struct AcmDevice *acm;
94     struct UsbCdcLineCoding lineCoding;
95     struct OsalMutex lock;
96     struct DataFifo readFifo;
97 };
98 
99 struct AcmDevice {
100     struct IDeviceIoService service;
101     struct HdfDeviceObject *device;
102     struct UsbInterface *ctrIface;
103     struct UsbPipeInfo  *ctrPipe;
104     struct UsbPipeInfo  *intPipe;
105     struct UsbPipeInfo  *dataInPipe;
106     struct UsbPipeInfo  *dataOutPipe;
107     struct AcmWb wb[ACM_NW];
108     struct AcmRb rb[ACM_NR];
109     struct UsbPipeInfo wPipeInfo;
110     struct OsalMutex writeLock;
111     struct OsalMutex readLock;
112     struct UsbRequest *notifyReq;
113     struct UsbRequest *readReq[ACM_NR];
114     struct UsbRequest *writeReq;
115     struct UsbRequest *ctrlReq;
116     struct OsalMutex lock;
117     struct UsbInterface *itface;
118     UsbInterfaceHandle *devHandle[USB_MAX_INTERFACES];
119     UsbInterfaceHandle *ctrDevHandle;
120     struct UsbSession *session;
121     struct SerialDevice  *port;
122     uint32_t nbIndex;
123     uint32_t nbSize;
124     int32_t transmitting;
125     int32_t ctrlReqNum;
126     uint8_t busNum;
127     uint8_t devAddr;
128     uint8_t interfaceCnt;
129     uint8_t *notificationBuffer;
130     uint8_t interfaceIndex[USB_MAX_INTERFACES];
131     struct UsbInterface *iface[USB_MAX_INTERFACES];
132     uint32_t ctrlSize;
133     uint32_t intSize;
134     uint32_t writeSize;
135     uint32_t readSize;
136     struct UsbCdcLineCoding lineCoding;
137     bool initFlag;
138 };
139 
140 struct UsbControlParams {
141     uint8_t request;
142     UsbRequestTargetType target;
143     UsbControlRequestType reqType;
144     UsbRequestDirection directon;
145     uint16_t value;
146     uint16_t index;
147     void *data;
148     uint16_t size;
149 };
150 
151 struct UsbDescriptorParams {
152     UsbInterfaceHandle *devHandle;
153     struct UsbRequest *request;
154     uint8_t type;
155     uint8_t index;
156     void *buf;
157     uint16_t size;
158 };
159 #endif
160