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