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_RAW_API_LIBRARY_H
17#define USB_RAW_API_LIBRARY_H
18
19#include "hdf_device_desc.h"
20#include "hdf_usb_pnp_manage.h"
21#include "usb_session.h"
22#include "usb_ddk_device.h"
23#include "usb_ddk_request.h"
24#include "usb_raw_api.h"
25
26#define BYTE_LENGTH         8
27
28#define USB_HOST_PNP_SERVICE_NAME "hdf_usb_pnp_notify_service"
29
30#define USB_RAW_REQUEST_TIME_ZERO_MS        (0)
31#define USB_RAW_REQUEST_DEFAULT_TIMEOUT     (1000)
32#define USB_RAW_REQUEST_TIMEOUT_MAX         (0xFFFFFFFF)
33
34#define DESC_HEADER_LENGTH  2
35
36typedef pid_t UsbRawTidType;
37
38struct UsbRawControlSetup {
39    uint8_t requestType;
40    uint8_t request;
41    uint16_t value;
42    uint16_t index;
43    uint16_t length;
44} __attribute__((packed));
45
46#define USB_RAW_CONTROL_SETUP_SIZE (sizeof(struct UsbRawControlSetup))
47
48union UsbiConfigDescBuf {
49    struct UsbConfigDescriptor desc;
50    uint8_t buf[USB_DDK_DT_CONFIG_SIZE];
51    uint16_t align;     /* Force 2-byte alignment */
52};
53
54enum UsbRawDescriptorType {
55    USB_RAW_CONFIG_DESCRIPTOR_TYPE,
56    USB_RAW_INTERFACE_DESCRIPTOR_TYPE,
57    USB_RAW_ENDPOINT_DESCRIPTOR_TYPE,
58    USB_RAW_AUDIO_ENDPOINT_DESCRIPTOR_TYPE,
59};
60
61enum RawRequestTimeoutFlags {
62    RAW_REQUEST_OS_HANDLES_TIMEOUT = 1U << 0,
63    RAW_REQUEST_TIMEOUT_HANDLED = 1U << 1,
64    RAW_REQUEST_TIMED_OUT = 1U << 2,
65};
66
67struct UsbMessageQueue {
68    struct DListHead entry;
69    struct OsalMutex mutex;
70    struct OsalSem sem;
71};
72
73struct RawUsbRamTestList {
74    uintptr_t address;
75    uint32_t size;
76    struct DListHead list;
77    struct OsalMutex lock;
78};
79#ifdef __cplusplus
80extern "C" {
81#endif
82
83struct UsbSession *RawGetSession(const struct UsbSession *session);
84int32_t RawInit(struct UsbSession **session);
85int32_t RawExit(const struct UsbSession *session);
86struct UsbDeviceHandle *RawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr);
87int32_t RawCloseDevice(const struct UsbDeviceHandle *devHandle);
88int32_t RawClaimInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber);
89struct UsbHostRequest *AllocRequest(const struct UsbDeviceHandle *devHandle,  int32_t isoPackets, size_t length);
90int32_t FreeRequest(const struct UsbHostRequest *request);
91int32_t RawFillBulkRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
92    const struct UsbFillRequestData *fillRequestData);
93int32_t RawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData);
94int32_t RawFillControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
95    const struct UsbFillRequestData *fillRequestData);
96int32_t RawFillInterruptRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
97    const struct UsbFillRequestData *fillRequestData);
98int32_t RawFillInterruptRequestByMmap(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
99    const struct UsbFillRequestData *fillRequestData);
100int32_t RawFillIsoRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
101    const struct UsbFillRequestData *fillRequestData);
102int32_t RawSendControlRequest(struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
103    const struct UsbControlRequestData *requestData);
104int32_t RawSendBulkRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
105    const struct UsbRequestData *requestData);
106int32_t RawSendInterruptRequest(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
107    const struct UsbRequestData *requestData);
108struct UsbHostRequest *RawAllocRequest(const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length);
109struct UsbHostRequest *RawAllocRequestByMmap(
110    const struct UsbDeviceHandle *devHandle, int32_t isoPackets, int32_t length);
111int32_t RawFreeRequest(const struct UsbHostRequest *request);
112int32_t RawFreeRequestByMmap(const struct UsbHostRequest *request);
113int32_t RawGetConfigDescriptor(const struct UsbDevice *dev, uint8_t configIndex,
114    struct UsbRawConfigDescriptor ** const config);
115void RawClearConfiguration(struct UsbRawConfigDescriptor *config);
116int32_t RawGetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t *config);
117int32_t RawSetConfiguration(const struct UsbDeviceHandle *devHandle, int32_t configuration);
118int32_t RawGetDescriptor(const struct UsbHostRequest *request, const struct UsbDeviceHandle *devHandle,
119    const struct UsbRawDescriptorParam *param, const unsigned char *data);
120struct UsbDevice *RawGetDevice(const struct UsbDeviceHandle *devHandle);
121int32_t RawGetDeviceDescriptor(const struct UsbDevice *dev, struct UsbDeviceDescriptor *desc);
122int32_t RawReleaseInterface(struct UsbDeviceHandle *devHandle, int32_t interfaceNumber);
123int32_t RawResetDevice(const struct UsbDeviceHandle *devHandle);
124int32_t RawSubmitRequest(const struct UsbHostRequest *request);
125int32_t RawCancelRequest(const struct UsbHostRequest *request);
126int32_t RawHandleRequest(const struct UsbDeviceHandle *devHandle);
127int32_t RawClearHalt(const struct UsbDeviceHandle *devHandle, uint8_t pipeAddress);
128int32_t RawHandleRequestCompletion(struct UsbHostRequest *request, UsbRequestStatus status);
129int32_t RawSetInterfaceAltsetting(
130    const struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber, uint8_t settingIndex);
131UsbRawTidType RawGetTid(void);
132int32_t RawRegisterSignal(void);
133int32_t RawKillSignal(struct UsbDeviceHandle *devHandle, UsbRawTidType tid);
134int32_t RawInitPnpService(enum UsbPnpNotifyServiceCmd cmdType, struct UsbPnpAddRemoveInfo infoData);
135void RawRequestListInit(struct UsbDevice *deviceObj);
136void *RawUsbMemAlloc(size_t size);
137void *RawUsbMemCalloc(size_t size);
138void RawUsbMemFree(void *mem);
139int32_t RawClaimInterfaceForce(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber);
140void RawAttachKernelDriver(struct UsbDeviceHandle *devHandle, uint8_t interfaceNumber);
141int32_t RawAttachInterface(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber);
142int32_t RawDetachInterface(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber);
143
144int32_t RawUsbControlMsg(const struct UsbDeviceHandle *devHandle,  struct UsbControlRequestData *ctrlData);
145int32_t RawUsbGetUsbSpeed(const struct UsbDeviceHandle *devHandle);
146bool RawGetInterfaceActiveStatus(struct UsbDeviceHandle *devHandle, uint32_t interfaceNumber);
147int32_t RawUsbCloseCtlProcess(const UsbInterfaceHandle *interfaceHandle);
148
149#ifdef __cplusplus
150}
151#endif
152#endif /* USB_RAW_API_LIBRARY_H */
153