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 USBFN_ADAPTER_H
17#define USBFN_ADAPTER_H
18
19#include <stdbool.h>
20#include <sys/mman.h>
21#include "hdf_slist.h"
22#include "hdf_dlist.h"
23#include "osal_mutex.h"
24#include "osal_file.h"
25#include "osal_mem.h"
26#include "securec.h"
27#include "hdf_log.h"
28#include "usbfn_interface.h"
29#include "usbfn_device.h"
30#include "usb_ddk.h"
31
32#define FCONFIG_IOC_MAGIC                'c'
33#define FCONFIG_CMD_MAKE_GADGET          _IO(FCONFIG_IOC_MAGIC, 1)
34#define FCONFIG_CMD_DROP_GADGET          _IO(FCONFIG_IOC_MAGIC, 2)
35#define FCONFIG_CMD_WRITE_DEV_DESC       _IO(FCONFIG_IOC_MAGIC, 3)
36#define FCONFIG_CMD_ADD_CONFIG           _IO(FCONFIG_IOC_MAGIC, 4)
37#define FCONFIG_CMD_REMOVE_CONFIG        _IO(FCONFIG_IOC_MAGIC, 5)
38#define FCONFIG_CMD_WRITE_STRINGS        _IO(FCONFIG_IOC_MAGIC, 6)
39#define FCONFIG_CMD_MAKE_FUNCTION        _IO(FCONFIG_IOC_MAGIC, 7)
40#define FCONFIG_CMD_DROP_FUNCTION        _IO(FCONFIG_IOC_MAGIC, 8)
41#define FCONFIG_CMD_ENABLE_UDC           _IO(FCONFIG_IOC_MAGIC, 9)
42#define FCONFIG_CMD_DISABLE_UDC          _IO(FCONFIG_IOC_MAGIC, 10)
43#define FCONFIG_CMD_CHAGE_DEVINFO        _IO(FCONFIG_IOC_MAGIC, 11)
44#define FCONFIG_CMD_CHAGE_DEVSTRING      _IO(FCONFIG_IOC_MAGIC, 12)
45
46#define GENERIC_IOC_MAGIC               'g'
47#define GENERIC_CMD_FREE_MEM            _IO(GENERIC_IOC_MAGIC, 1)
48#define GENERIC_CMD_CANCEL_REQUEST      _IO(GENERIC_IOC_MAGIC, 2)
49#define GENERIC_CMD_GET_PIPE_INFO       _IO(GENERIC_IOC_MAGIC, 3)
50
51#define GENERIC_CMD_GET_EP0_EVENT       _IO(GENERIC_IOC_MAGIC, 4)
52#define GENERIC_CMD_ENDPOINT_IO         _IO(GENERIC_IOC_MAGIC, 5)
53#define GENERIC_CMD_GET_REQ_STATUS      _IO(GENERIC_IOC_MAGIC, 6)
54
55struct FconfigString {
56    uint32_t                      len;
57    char                          *s;
58};
59
60struct FconfigUsbString {
61    uint8_t                       id;
62    struct FconfigString         str;
63};
64
65struct FconfigDevStrings {
66    struct FconfigString    gadgetName;
67    uint16_t                language;
68    uint32_t                strCount;
69    struct FconfigUsbString *strings;
70} __attribute__((packed));
71
72struct FconfigDevDesc {
73    struct FconfigString        gadgetName;
74    struct UsbDeviceDescriptor  devDesc;
75} __attribute__((packed));
76
77struct FconfigCfgDesc {
78    struct FconfigString        gadgetName;
79    struct FconfigString        configName;
80    struct UsbConfigDescriptor  cfgDesc;
81} __attribute__((packed));
82
83struct FconfigFuncInfo {
84    struct FconfigString        gadgetName;
85    struct FconfigString        configName;
86    struct FconfigString        funcName;
87};
88
89struct FconfigUdcInfo {
90    struct FconfigString      gadgetName;
91    struct FconfigString      udcName;
92};
93
94struct FconfigProp {
95    const char  *propName;
96    uint16_t    propValue;
97};
98
99struct FconfigPropSting {
100    uint16_t    lang;
101    const char  *propName;
102    const char  *propValue;
103};
104
105struct FconfigDevdescInfo {
106    struct FconfigString      gadgetName;
107    struct FconfigProp        prop;
108};
109
110struct FconfigDevDescString {
111    struct FconfigString      gadgetName;
112    struct FconfigPropSting   prop;
113};
114
115struct FconfigPollFd {
116    int32_t fd;
117    uint32_t revents;
118    int32_t events;
119};
120
121#define FUNCTIONFS_ENDPOINT_DESC             _IOR('g', 130, struct UsbEndpointDescriptor)
122
123#define FUNCTIONFS_NEWFN                     _IOW('g', 60, struct FuncNew)
124#define FUNCTIONFS_DELFN                     _IOW('g', 61, struct FuncNew)
125#define FUNCTIONFS_ENDPOINT_GET_REQ_STATUS   _IOW('g', 48, struct IoData)
126#define FUNCTIONFS_ENDPOINT_WRITE            _IOW('g', 49, struct IoData)
127#define FUNCTIONFS_ENDPOINT_READ             _IOW('g', 50, struct IoData)
128#define FUNCTIONFS_ENDPOINT_RW_CANCEL        _IOW('g', 51, struct IoData)
129#define FUNCTIONFS_ENDPOINT_QUEUE_INIT       _IO('g', 52)
130#define FUNCTIONFS_ENDPOINT_QUEUE_DEL        _IO('g', 53)
131#define FUNCTIONFS_ENDPOINT_RELEASE_BUF      _IOR('g', 54, struct GenericMemory)
132#define FUNCTIONFS_ENDPOINT_GET_EP0_EVENT    _IOR('g', 56, struct UsbFnReqEvent)
133
134#define FUNCTION_GENERIC "f_generic"
135#define CONFIGFS_DIR "/config/usb_gadget"
136#define USBFN_DEV "/dev/usbfn"
137#define MAX_REQUEST         64
138#define MAX_NAMELEN         64
139#define MAX_PATHLEN         128
140
141#define USB_EVENT_COUNT 10
142#define MAX_EP          16
143#define MAX_BUFLEN      2048
144
145struct UsbDeviceFunctionsInfo {
146    const char *functionName;
147    uint32_t numberMask;
148};
149
150struct UsbFnCtrlEvent {
151    union {
152        struct UsbFnCtrlRequest  setup;
153    } __attribute__((packed))    u;
154    uint8_t                      type;
155    uint8_t                      pad[3];
156} __attribute__((packed));
157
158typedef enum {
159    USB_EP0_INVALID,
160    USB_EP0_CTRL_EVENT,
161    USB_EP0_IO_COMPLETED,
162} UsbEp0EventType;
163
164struct FuncNew {
165    uint32_t nameLen;
166    char     name[MAX_NAMELEN];
167};
168
169struct IoData {
170    uint32_t aio;       /* 0 for sync ,1 for async */
171    uint32_t read;      /* 0 for write ,1 for read */
172    uint32_t len;       /* the len of this io request */
173    uint32_t timeout;   /* sync timeout */
174    uint64_t buf;       /* the address of map buf */
175};
176
177struct GenericMemory {
178    uint32_t size;
179    uint64_t buf;
180};
181
182struct UsbFnReqEvent {
183    uint64_t buf;
184    uint32_t actual;
185    int32_t      status;
186};
187
188struct UsbEp0Event {
189    union {
190        struct UsbFnCtrlEvent ctrlEvent;
191        struct UsbFnReqEvent reqEvent;
192    };
193    UsbEp0EventType type;
194};
195#define MAX_EP0_NUM 5
196struct UsbFnEventAll {
197    int32_t ep0[MAX_EP0_NUM];
198    uint8_t ep0Num;
199    struct UsbEp0Event ep0Event[MAX_EP0_NUM];
200    int32_t epx[MAX_EP];
201    uint8_t epNum;
202    struct UsbFnReqEvent *reqEvent[MAX_EP];
203    uint8_t numEvent[MAX_EP];
204};
205
206struct UsbFnAdapterOps {
207    int32_t (*createDevice)(const char *udcName, const char *devName, struct UsbFnDeviceDesc *descriptor);
208    int32_t (*delDevice)(const char *devName, const char *udcName, struct UsbFnDeviceDesc *descriptor);
209
210    int32_t (*openPipe)(const char *interfaceName, int32_t epIndex);
211    int32_t (*closePipe)(int32_t ep);
212    int32_t (*getPipeInfo)(int32_t ep, struct UsbFnPipeInfo *pipeInfo);
213
214    int32_t (*queueInit)(int32_t ep);
215    int32_t (*queueDel)(int32_t ep);
216    int32_t (*releaseBuf)(int32_t ep, const struct GenericMemory *mem);
217    int32_t (*pipeIo)(int32_t ep, struct IoData *ioData);
218    int32_t (*cancelIo)(int32_t ep, const struct IoData *ioData);
219    int32_t (*getReqStatus)(int32_t ep, const struct IoData *ioData);
220    uint8_t *(*mapAddr)(int32_t ep, uint32_t len);
221    int32_t (*unmapAddr)(uint8_t *mapAddr, uint32_t len);
222    int32_t (*pollEvent)(struct UsbFnEventAll *event, int32_t timeout);
223
224    int32_t (*writeUDC)(const char *deviceName, const char *udcName, int32_t enable);
225    int32_t (*writeProp)(const char *deviceName, const char *propName, uint32_t propValue);
226    int32_t (*writeDesString)(const char *deviceName,
227        uint16_t lang, const char *stringName, const char *stringValue);
228};
229
230struct RawUsbRamTestList {
231    uintptr_t address;
232    uint32_t size;
233    struct DListHead list;
234    struct OsalMutex lock;
235};
236
237struct UsbFnAdapterOps *UsbFnAdapterGetOps(void);
238void *UsbFnMemAlloc(size_t size);
239void *UsbFnMemCalloc(size_t size);
240void UsbFnMemFree(const void *mem);
241
242#endif /* USBFN_ADAPTER_H */
243