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 USB_INTERFACE_POOL_H
17 #define USB_INTERFACE_POOL_H
18 
19 #include "usb_session.h"
20 #include "usb_ddk_device.h"
21 #include "usb_ddk_interface.h"
22 #include "usb_ddk_request.h"
23 #include "usb_raw_api_library.h"
24 
25 #define INTERFACE_POOL_ID_MAX   (128)
26 #define INTERFACE_REFCOUNT_UNFORCE  (1)
27 
28 typedef enum {
29     USB_PIPE_INDEX_TYPE,
30     USB_PIPE_DIRECTION_TYPE
31 } UsbPipeQueryParaType;
32 
33 typedef enum {
34     USB_INTERFACE_INTERFACE_INDEX_TYPE,
35     USB_INTERFACE_ALT_SETTINGS_TYPE
36 } UsbInterfaceQueryParaType;
37 
38 typedef enum {
39     USB_POOL_NORMAL_TYPE,
40     USB_POOL_OBJECT_ID_TYPE
41 } UsbPoolQueryParaType;
42 
43 typedef enum {
44     USB_POOL_PROCESS_RUNNING,
45     USB_POOL_PROCESS_STOP,
46     USB_POOL_PROCESS_STOPED
47 } UsbPoolProcessStatusType;
48 
49 struct UsbInterfaceHandleEntity {
50     struct UsbDeviceHandle *devHandle;
51     uint8_t interfaceIndex;
52 };
53 struct UsbSdkInterface {
54     struct UsbInterface interface;
55     int32_t parentObjectId;
56     struct DListHead pipeList;
57     struct OsalMutex listLock;
58     UsbInterfaceStatus status;
59     uint8_t altSettingId;
60     struct UsbSession *session;
61     OsalAtomic refCount;
62     bool forceDetachKernelDriver;
63 };
64 
65 struct UsbInterfacePool {
66     struct UsbObject object;
67     struct UsbSession *session;
68     struct OsalMutex mutex;
69     struct DListHead interfaceList;
70     struct OsalMutex interfaceLock;
71     OsalAtomic refCount;
72     uint8_t busNum;
73     uint8_t devAddr;
74     OsalAtomic ioRefCount;
75     struct OsalThread ioSendProcess;
76     struct OsalThread ioAsyncReceiveProcess;
77     struct UsbMessageQueue submitRequestQueue;
78     UsbRawTidType ioProcessTid;
79     UsbPoolProcessStatusType ioProcessStopStatus;
80     UsbPoolProcessStatusType ioRecvProcessStopStatus;
81     struct OsalMutex ioStopLock;
82     struct UsbDevice *device;
83     struct OsalSem ioSem;
84 };
85 
86 struct UsbPipeQueryPara {
87     UsbPipeQueryParaType type;
88     union {
89         uint8_t pipeId;
90         UsbPipeDirection pipeDirection;
91     };
92 };
93 
94 struct UsbInterfaceQueryPara {
95     UsbInterfaceQueryParaType type;
96     uint8_t interfaceIndex;
97     uint8_t altSettingId;
98 };
99 
100 struct UsbPoolQueryPara {
101     UsbPoolQueryParaType type;
102     union {
103         struct {
104             uint8_t busNum;
105             uint8_t usbAddr;
106         };
107         int32_t objectId;
108     };
109 };
110 
111 struct UsbIfRequest {
112     struct UsbRequest request;
113     struct UsbHostRequest *hostRequest;
114     bool isSyncReq;
115 }__attribute__((aligned(4)));
116 
117 int32_t UsbIfCreatPipeObj(const struct UsbSdkInterface *interfaceObj, struct UsbPipe **pipeObj);
118 int32_t UsbIfCreatInterfaceObj(const struct UsbInterfacePool *interfacePool, struct UsbSdkInterface **interfaceObj);
119 HDF_STATUS UsbIfDestroyInterfaceObj(
120     const struct UsbInterfacePool *interfacePool, const struct UsbSdkInterface *interfaceObj);
121 int32_t UsbIfCreatInterfacePool(const struct UsbSession *session, uint8_t busNum, uint8_t devAddr,
122     struct UsbInterfacePool **interfacePool);
123 int32_t UsbCloseCtlProcess(const UsbInterfaceHandle *interfaceHandle);
124 
125 #endif /* USB_INTERFACE_POOL_H */
126