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