1 /*
2  * Copyright (c) 2024 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_NET_HOST_H
17 #define USB_NET_HOST_H
18 
19 #include "hdf_log.h"
20 #include "hdf_usb_net_manager.h"
21 #include "hdf_device_desc.h"
22 
23 #include "data_fifo.h"
24 #include "usb_raw_api.h"
25 #include "usb_net_net.h"
26 
27 #define USBNET_NW                  16
28 #define USBNET_NR                  44
29 #define USB_CTRL_REQ_SIZE          256
30 #define IFF_NOARP                  1 << 7
31 
32 #define USB_IO_THREAD_STACK_SIZE      8192
33 #define USB_RAW_IO_SLEEP_MS_TIME      100
34 #define USB_RAW_IO_STOP_WAIT_MAX_TIME 3
35 #define HARCH_LOG_TAG                 "[-harch-hdf-]"
36 
37 #define HARCH_INFO_PRINT(fmt, ...) \
38 do { \
39     if (0){ \
40         HDF_LOGI(HARCH_LOG_TAG"[%{public}s][%{public}d]:" fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);} \
41 }while (0)
42 
43 enum UsbnetHostDeviceSpeed {
44     USB_SPEED_UNKNOWN = 0,        /* enumerating */
45     USB_SPEED_LOW,                /* usb 1.1  1.5M */
46     USB_SPEED_FULL,               /* usb 1.1  12M */
47     USB_SPEED_HIGH,               /* usb 2.0  480M */
48     USB_SPEED_WIRELESS,
49     USB_SPEED_SUPER,              /* usb 3.0 "5G" */
50     USB_SPEED_SUPER_PLUS,         /* usb 3.0 "10G" */
51 };
52 
53 //usb process
54 struct UsbnetHost;
55 struct UsbHostWb {
56     struct UsbRawRequest *request;
57     struct UsbnetHost *nNet;
58     uint8_t *buf;
59     uint32_t len;
60     int32_t use;
61 };
62 
63 struct UsbHostRb {
64     uint8_t *base;
65     int32_t size;
66     int32_t index;
67     int32_t use;
68     struct UsbnetHost *nNet;
69 };
70 
71 struct UsbEndpoint {
72     uint8_t addr;
73     uint8_t interval;
74     uint16_t maxPacketSize;
75 };
76 
77 typedef enum {
78     USB_RAW_IO_PROCESS_RUNNING,
79     USB_RAW_IO_PROCESS_STOP,
80     USB_RAW_IO_PROCESS_STOPED
81 } UsbRawIoProcessStatusType;
82 
83 /* interface from UsbnetHost core to each USB networking link we handle */
84 struct UsbnetHost {
85     //usb info
86     struct IDeviceIoService service;
87     struct HdfDeviceObject *deviceObject;
88 
89     uint8_t curInterfaceNumber;
90     uint8_t busNum;
91     uint8_t devAddr;
92     struct UsbSession *session;
93     UsbRawHandle *devHandle;
94     struct UsbRawConfigDescriptor *config;
95     struct UsbRawInterfaceDescriptor *intf;
96     struct UsbRawEndpointDescriptor *status;
97     bool initFlag;
98     int32_t flags;
99     uint8_t ctrlIface;
100     uint8_t dataIface;
101     struct UsbEndpoint *statusEp;
102     struct UsbEndpoint *dataInEp;
103     struct UsbEndpoint *dataOutEp;
104     uint32_t xid;
105     //data process
106     uint16_t rxQlen, txQlen;
107     uint32_t canDmaSg : 1;
108     uint8_t  *paddingPkt;
109     struct DListHead  readPool;
110     struct DListHead  readQueue;
111     struct DListHead  writePool;
112     struct DListHead  writeQueue;
113 
114     struct UsbHostWb wb[USBNET_NW];
115     struct OsalMutex writeLock;
116     struct OsalMutex readLock;
117     struct UsbRawRequest *readReq[USBNET_NR];
118     struct UsbRawRequest *statusReq;
119     bool allocFlag;
120 
121     struct DataFifo readFifo;
122 
123     uint32_t nbIndex;
124     uint32_t nbSize;
125     int32_t transmitting;
126 
127     uint8_t *notificationBuffer;
128     uint8_t readReqNum;
129 
130     struct UsbRawRequest *ctrlWriteReqSync;
131     struct UsbRawRequest *ctrlReadReqSync;
132     struct UsbRawRequest *ctrlWriteReqAsync;
133 
134     struct OsalMutex usbIoLock;
135     UsbRawIoProcessStatusType usbIoStatus;
136     struct OsalThread ioThread;
137     //net info
138     struct HdfIoService *hdfNetIoServ;
139     struct HdfDevEventlistener *hdfNetListener;
140     struct UsbnetTransInfo net;
141     struct OsalMutex sendNetLock;
142     //device info
143     struct UsbnetHostDriverInfo *driverInfo;
144 };
145 
146 /* interface from the device/framing level "minidriver" to core */
147 struct UsbnetHostDriverInfo {
148     char    *description;
149     int32_t flags;
150     /* init device ... can sleep, or cause probe() failure */
151     int32_t (*bind)(struct UsbnetHost *);
152     void    (*unbind)(struct UsbnetHost *);
153 };
154 
155 /* write or read command need param */
156 struct UsbnetHostCmdParam {
157     uint8_t cmd;
158     uint8_t reqtype;
159     uint16_t value;
160     uint16_t index;
161     const void *data;
162     uint16_t size;
163 };
164 
165 void UsbnetWriteLog(char *buff, int size, int tag);
166 
167 //net process
168 int32_t UsbnetHostProbe(struct UsbnetHost *uNet);
169 
170 void UsbnetHostRelease(struct UsbnetHost *uNet);
171 
172 //usb process
173 int32_t UsbnetHostWriteCmdSync(struct UsbnetHost *usbNet, struct UsbnetHostCmdParam cmdParm);
174 
175 #endif
176