• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 USBD_H
17 #define USBD_H
18 
19 #include "data_fifo.h"
20 #include "hdf_base.h"
21 #include "hdf_device_desc.h"
22 #include "osal_atomic.h"
23 #include "osal_sem.h"
24 #include "usb_ddk.h"
25 #include "usb_ddk_interface.h"
26 #include "usb_session.h"
27 #include "usbd_publisher.h"
28 #include "usbd_type.h"
29 
30 #define USB_MAX_INTERFACES 32
31 #define USB_MAX_DEVICE_NUMBER 127
32 #define DIRECTION_MASK 0x1
33 #define USB_CTRL_SET_TIMEOUT 5000
34 #define USB_PIPE_DIR_OFFSET 7
35 
36 struct UsbdService;
37 
38 #define USBD_BULKASYNCREQ_NUM_MAX 64
39 
40 struct UsbdBulkASyncReqList;
41 struct UsbdBulkASyncList;
42 
43 struct UsbdBufferHandle {
44     int32_t fd;   /**< buffer fd, -1 if not supported */
45     int32_t size; /* < size of memory */
46     uint8_t *starAddr;
47     int32_t cur;
48     int32_t rcur;
49     uint8_t cbflg;
50     struct OsalMutex lock;
51 };
52 
53 struct UsbdBulkASyncReqNode {
54     struct DListHead node;
55     struct UsbRequest *request;
56     struct UsbdBulkASyncReqList *list;
57     int32_t use;
58     int32_t id;
59 };
60 
61 struct UsbdBulkASyncReqList {
62     struct UsbdBulkASyncReqNode node[USBD_BULKASYNCREQ_NUM_MAX];
63     struct UsbdBulkASyncList *pList;
64     struct DListHead eList;
65     struct DListHead uList;
66     struct OsalMutex elock;
67     struct OsalMutex ulock;
68 };
69 
70 struct UsbdBulkASyncList {
71     struct HostDevice *instance;
72     struct UsbdBulkASyncList *next;
73     UsbInterfaceHandle *ifHandle;
74     struct HdfRemoteService *cb;
75     struct UsbdBulkASyncReqList rList;
76     struct UsbPipeInfo pipe;
77     struct UsbRequestParams params;
78     struct UsbdBufferHandle asmHandle;
79     uint8_t ifId;
80     uint8_t epId;
81 };
82 
83 struct HostDevice {
84     struct HdfSListNode node;
85     struct UsbdService *service;
86     struct DataFifo readFifo;
87     struct HdfSList requestQueue;
88     struct OsalMutex requestLock;
89     uint8_t interfaceIndex[USB_MAX_INTERFACES];
90     uint8_t interfaceCnt;
91     struct UsbInterface *iface[USB_MAX_INTERFACES];
92     UsbInterfaceHandle *ctrDevHandle;
93     UsbInterfaceHandle *devHandle[USB_MAX_INTERFACES];
94     struct OsalMutex writeLock;
95     struct OsalMutex readLock;
96     struct OsalMutex lock;
97     struct UsbRequest *ctrlReq;
98     struct UsbInterface *ctrIface;
99     struct UsbPipeInfo *ctrPipe;
100     uint8_t busNum;
101     uint8_t devAddr;
102     bool initFlag;
103     struct HdfSList reqSyncList;
104     struct OsalMutex reqSyncLock;
105     struct HdfSList reqASyncList;
106     struct OsalMutex reqASyncLock;
107     struct UsbdBulkASyncList *bulkASyncList;
108 };
109 
110 struct RequestMsg {
111     struct UsbRequest *request;
112     void *clientData;
113     uint32_t clientLength;
114     void *buffer;
115     uint32_t length;
116 };
117 
118 struct UsbControlParams {
119     uint8_t request;
120     UsbRequestTargetType target;
121     UsbControlRequestType reqType;
122     UsbRequestDirection directon;
123     uint16_t value;
124     uint16_t index;
125     void *data;
126     uint16_t size;
127 };
128 
129 struct UsbDescriptorParams {
130     UsbInterfaceHandle *devHandle;
131     struct UsbRequest *request;
132     uint8_t type;
133     uint8_t index;
134     void *buf;
135     uint16_t size;
136 };
137 
138 struct UsbdService {
139     struct IDeviceIoService service;
140     struct HdfDeviceObject *device;
141     struct UsbdSubscriber *subscriber;
142     struct UsbSession *session;
143     struct HdfSList devList;
144     struct OsalMutex lock;
145 };
146 
147 struct UsbdRequestSync {
148     struct HdfSListNode node;
149     struct UsbRequest *request;
150     UsbInterfaceHandle *ifHandle;
151     struct OsalMutex lock;
152     struct UsbPipeInfo pipe;
153     struct UsbRequestParams params;
154     uint8_t endPointAddr;
155 };
156 
157 struct UsbdRequestASync {
158     struct HdfSListNode node;
159     struct HdfSListNode qNode;
160     UsbInterfaceHandle *ifHandle;
161     struct RequestMsg reqMsg;
162     struct OsalMutex lock;
163     struct UsbPipeInfo pipe;
164     struct UsbRequestParams params;
165     uint8_t endPointAddr;
166     uint8_t status;
167 };
168 
169 struct UsbdSubscriber;
170 int32_t BindUsbSubscriber(struct UsbdService *service, struct UsbdSubscriber *subscriber);
171 int32_t UnbindUsbSubscriber(struct UsbdService *service);
172 
173 #endif // USBD_H
174