• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
55 struct FconfigString {
56     uint32_t                      len;
57     char                          *s;
58 };
59 
60 struct FconfigUsbString {
61     uint8_t                       id;
62     struct FconfigString         str;
63 };
64 
65 struct FconfigDevStrings {
66     struct FconfigString    gadgetName;
67     uint16_t                language;
68     uint32_t                strCount;
69     struct FconfigUsbString	*strings;
70 } __attribute__((packed));
71 
72 struct FconfigDevDesc {
73     struct FconfigString        gadgetName;
74     struct UsbDeviceDescriptor  devDesc;
75 } __attribute__((packed));
76 
77 struct FconfigCfgDesc {
78     struct FconfigString        gadgetName;
79     struct FconfigString        configName;
80     struct UsbConfigDescriptor  cfgDesc;
81 } __attribute__((packed));
82 
83 struct FconfigFuncInfo {
84     struct FconfigString        gadgetName;
85     struct FconfigString        configName;
86     struct FconfigString        funcName;
87 };
88 
89 struct FconfigUdcInfo {
90     struct FconfigString      gadgetName;
91     struct FconfigString      udcName;
92 };
93 
94 struct FconfigProp {
95     const char  *propName;
96     uint16_t    propValue;
97 };
98 
99 struct FconfigPropSting {
100     uint16_t    lang;
101     const char  *propName;
102     const char  *propValue;
103 };
104 
105 struct FconfigDevdescInfo {
106     struct FconfigString      gadgetName;
107     struct FconfigProp        Prop;
108 };
109 
110 struct FconfigDevDescString {
111     struct FconfigString      gadgetName;
112     struct FconfigPropSting   Prop;
113 };
114 
115 struct FconfigPollFd {
116     int fd;
117     int revents;
118     int 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 
145 struct UsbFnCtrlEvent {
146     union {
147         struct UsbFnCtrlRequest	 setup;
148     } __attribute__((packed))    u;
149     uint8_t                      type;
150     uint8_t                      pad[3];
151 } __attribute__((packed));
152 
153 typedef enum {
154     USB_EP0_INVALID,
155     USB_EP0_CTRL_EVENT,
156     USB_EP0_IO_COMPLETED,
157 } UsbEp0EventType;
158 
159 struct FuncNew {
160     uint32_t nameLen;
161     char     name[MAX_NAMELEN];
162 };
163 
164 struct IoData {
165     uint32_t aio;       /* 0 for sync ,1 for async */
166     uint32_t read;      /* 0 for write ,1 for read */
167     uint32_t len;       /* the len of this io request */
168     uint32_t timeout;   /* sync timeout */
169     uint32_t buf;       /* the address of map buf */
170 };
171 
172 struct GenericMemory {
173     size_t size;
174     uint32_t buf;
175     char storage[];
176 };
177 
178 struct UsbFnReqEvent {
179     uint32_t buf;
180     uint32_t actual;
181     int      status;
182 };
183 
184 struct UsbEp0Event {
185     union {
186         struct UsbFnCtrlEvent ctrlEvent;
187         struct UsbFnReqEvent reqEvent;
188     };
189     UsbEp0EventType type;
190 };
191 #define MAX_EP0_NUM 5
192 struct UsbFnEventAll {
193     int ep0[MAX_EP0_NUM];
194     uint8_t ep0Num;
195     struct UsbEp0Event ep0Event[MAX_EP0_NUM];
196     int epx[MAX_EP];
197     uint8_t epNum;
198     struct UsbFnReqEvent *reqEvent[MAX_EP];
199     uint8_t numEvent[MAX_EP];
200 };
201 
202 struct UsbFnAdapterOps {
203     int (*createDevice)(const char *udcName, const char *devName, struct UsbFnDeviceDesc *descriptor);
204     int (*delDevice)(const char *devName, const char *udcName, struct UsbFnDeviceDesc *descriptor);
205 
206     int (*openPipe)(const char *interfaceName, int epIndex);
207     int (*closePipe)(int ep);
208     int (*getPipeInfo)(int ep, struct UsbFnPipeInfo *pipeInfo);
209 
210     int (*queueInit)(int ep);
211     int (*queueDel)(int ep);
212     int (*releaseBuf)(int ep, const struct GenericMemory *mem);
213     int (*pipeIo)(int ep, struct IoData *ioData);
214     int (*cancelIo)(int ep, const struct IoData *ioData);
215     int (*getReqStatus)(int ep, const struct IoData *ioData);
216     uint8_t *(*mapAddr)(int ep, uint32_t len);
217     int (*unmapAddr)(uint8_t *mapAddr, uint32_t len);
218     int (*pollEvent)(struct UsbFnEventAll *event, int timeout);
219 
220     int (*writeUDC)(const char *deviceName, const char *udcName, int enable);
221     int (*writeProp)(const char *deviceName, const char *propName, uint32_t propValue);
222     int (*writeDesString)(const char *deviceName,
223         uint16_t lang, const char *stringName, const char *stringValue);
224 };
225 
226 struct RawUsbRamTestList {
227     uint32_t address;
228     uint32_t size;
229     struct DListHead list;
230     struct OsalMutex lock;
231 };
232 
233 struct UsbFnAdapterOps *UsbFnAdapterGetOps(void);
234 void *UsbFnMemAlloc(size_t size);
235 void *UsbFnMemCalloc(size_t size);
236 void UsbFnMemFree(void *mem);
237 int UsbFnAdpMemTestTrigger(bool enable);
238 
239 #endif /* USBFN_ADAPTER_H */
240