1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci/**
17094332d3Sopenharmony_ci * @file usbfn_request.h
18094332d3Sopenharmony_ci *
19094332d3Sopenharmony_ci * @brief Declares APIs for managing USB data requests.
20094332d3Sopenharmony_ci *
21094332d3Sopenharmony_ci * @since 1.0
22094332d3Sopenharmony_ci * @version 1.0
23094332d3Sopenharmony_ci */
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ci#ifndef USBFN_REQUEST_H
26094332d3Sopenharmony_ci#define USBFN_REQUEST_H
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_ci#include "usb_object.h"
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ci/**
31094332d3Sopenharmony_ci * @brief Defines the USB request type.
32094332d3Sopenharmony_ci */
33094332d3Sopenharmony_citypedef enum {
34094332d3Sopenharmony_ci    /** Invalid request */
35094332d3Sopenharmony_ci    USB_REQUEST_TYPE_INVALID,
36094332d3Sopenharmony_ci    /** Write request */
37094332d3Sopenharmony_ci    USB_REQUEST_TYPE_PIPE_WRITE,
38094332d3Sopenharmony_ci    /** Read request */
39094332d3Sopenharmony_ci    USB_REQUEST_TYPE_PIPE_READ,
40094332d3Sopenharmony_ci} UsbFnRequestType;
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ci/**
43094332d3Sopenharmony_ci * @brief Defines a USB request.
44094332d3Sopenharmony_ci */
45094332d3Sopenharmony_cistruct UsbFnRequest {
46094332d3Sopenharmony_ci    /** USB object */
47094332d3Sopenharmony_ci    const struct UsbObject *obj;
48094332d3Sopenharmony_ci    /** Linked list header provided for users */
49094332d3Sopenharmony_ci    struct DListHead list;
50094332d3Sopenharmony_ci    /** Pointer to the request data */
51094332d3Sopenharmony_ci    void             *buf;
52094332d3Sopenharmony_ci    /** Length of the request data */
53094332d3Sopenharmony_ci    uint32_t          length;
54094332d3Sopenharmony_ci    /** Request type */
55094332d3Sopenharmony_ci    UsbFnRequestType type;
56094332d3Sopenharmony_ci    /** Request status */
57094332d3Sopenharmony_ci    UsbRequestStatus status;
58094332d3Sopenharmony_ci    /** Number of bytes transferred to or from the buffer */
59094332d3Sopenharmony_ci    uint32_t         actual;
60094332d3Sopenharmony_ci    /** Callback for request completion */
61094332d3Sopenharmony_ci    void (*complete)(uint8_t pipe, struct UsbFnRequest *req);
62094332d3Sopenharmony_ci    /** Pointer to the context for the callback */
63094332d3Sopenharmony_ci    void             *context;
64094332d3Sopenharmony_ci};
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ci#ifdef __cplusplus
67094332d3Sopenharmony_ciextern "C" {
68094332d3Sopenharmony_ci#endif
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci/**
71094332d3Sopenharmony_ci * @brief Defines a USB interface handle.
72094332d3Sopenharmony_ci */
73094332d3Sopenharmony_citypedef void *UsbFnInterfaceHandle;
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_ci/**
76094332d3Sopenharmony_ci * @brief Applies for a control (EP0) request.
77094332d3Sopenharmony_ci *
78094332d3Sopenharmony_ci * You can use this function to apply for a control (EP0)
79094332d3Sopenharmony_ci * request based on the specified USB interface handle and length.
80094332d3Sopenharmony_ci *
81094332d3Sopenharmony_ci * @param handle Indicates the USB interface handle. It can be set to any <b>UsbFnInterfaceHandle</b>.
82094332d3Sopenharmony_ci * @param len Indicates the request data length. The maximum value is <b>2048</b>. An error will be
83094332d3Sopenharmony_ci * thrown if the passed value is greater than <b>2048</b>.
84094332d3Sopenharmony_ci * You are advised to set the value to the <b>bMaxPacketSize0</b> defined by the device descriptor.
85094332d3Sopenharmony_ci * The data length set by <b>bMaxPacketSize0</b> prevails if a greater value is specified.
86094332d3Sopenharmony_ci *
87094332d3Sopenharmony_ci * @return Returns the pointer to the <b>UsbFnRequest</b> if the operation is successful;
88094332d3Sopenharmony_ci * returns <b>NULL</b> otherwise.
89094332d3Sopenharmony_ci */
90094332d3Sopenharmony_cistruct UsbFnRequest *UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle, uint32_t len);
91094332d3Sopenharmony_ci
92094332d3Sopenharmony_ci/**
93094332d3Sopenharmony_ci * @brief Allocates a USB request.
94094332d3Sopenharmony_ci *
95094332d3Sopenharmony_ci * You can use this function to allocate a USB request based on the
96094332d3Sopenharmony_ci * specified USB interface handle, pipe ID, and length.
97094332d3Sopenharmony_ci *
98094332d3Sopenharmony_ci * @param handle Indicates the USB interface handle.
99094332d3Sopenharmony_ci * @param pipe Indicates the pipe ID. The value ranges from 0 to the total number of pipes on the USB interface.
100094332d3Sopenharmony_ci * @param len Indicates the request data length. The maximum value is <b>2048</b>. An error will be thrown
101094332d3Sopenharmony_ci * if the passed value is greater than <b>2048</b>.
102094332d3Sopenharmony_ci * You are advised to set the value to the <b>wMaxPacketSize</b> defined by the endpoint descriptor.
103094332d3Sopenharmony_ci * The data length set by <b>wMaxPacketSize</b> prevails if a greater value is specified.
104094332d3Sopenharmony_ci *
105094332d3Sopenharmony_ci * @return Returns the pointer to the <b>UsbFnRequest</b> if the operation is successful;
106094332d3Sopenharmony_ci * returns <b>NULL</b> otherwise.
107094332d3Sopenharmony_ci */
108094332d3Sopenharmony_cistruct UsbFnRequest *UsbFnAllocRequest(UsbFnInterfaceHandle handle, uint8_t pipe, uint32_t len);
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci/**
111094332d3Sopenharmony_ci * @brief Releases a specified USB request.
112094332d3Sopenharmony_ci *
113094332d3Sopenharmony_ci * @param req Indicates the pointer to the USB request.
114094332d3Sopenharmony_ci *
115094332d3Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful;
116094332d3Sopenharmony_ci * returns a negative value defined in {@link UsbErrorType} otherwise.
117094332d3Sopenharmony_ci */
118094332d3Sopenharmony_ciint32_t UsbFnFreeRequest(struct UsbFnRequest *req);
119094332d3Sopenharmony_ci
120094332d3Sopenharmony_ci/**
121094332d3Sopenharmony_ci * Obtains the status of a specified USB request.
122094332d3Sopenharmony_ci *
123094332d3Sopenharmony_ci * @param req Indicates the pointer to the USB request.
124094332d3Sopenharmony_ci * @param status Indicates the pointer to the status of the USB request.
125094332d3Sopenharmony_ci *
126094332d3Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful;
127094332d3Sopenharmony_ci * returns a negative value defined in {@link UsbErrorType} otherwise.
128094332d3Sopenharmony_ci */
129094332d3Sopenharmony_ciint32_t UsbFnGetRequestStatus(struct UsbFnRequest *req, UsbRequestStatus *status);
130094332d3Sopenharmony_ci
131094332d3Sopenharmony_ci/**
132094332d3Sopenharmony_ci * @brief Sends a non-isochronous USB request based on the passed request.
133094332d3Sopenharmony_ci *
134094332d3Sopenharmony_ci * @param req Indicates the pointer to the USB request.
135094332d3Sopenharmony_ci *
136094332d3Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful;
137094332d3Sopenharmony_ci * returns a negative value defined in {@link UsbErrorType} otherwise.
138094332d3Sopenharmony_ci */
139094332d3Sopenharmony_ciint32_t UsbFnSubmitRequestAsync(struct UsbFnRequest *req);
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_ci/**
142094332d3Sopenharmony_ci * @brief Cancels a USB request based on the passed request.
143094332d3Sopenharmony_ci *
144094332d3Sopenharmony_ci *
145094332d3Sopenharmony_ci *
146094332d3Sopenharmony_ci * @param req Indicates the pointer to the USB request.
147094332d3Sopenharmony_ci *
148094332d3Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful;
149094332d3Sopenharmony_ci * returns a negative value defined in {@link UsbErrorType} otherwise.
150094332d3Sopenharmony_ci */
151094332d3Sopenharmony_ciint32_t UsbFnCancelRequest(struct UsbFnRequest *req);
152094332d3Sopenharmony_ci
153094332d3Sopenharmony_ci/**
154094332d3Sopenharmony_ci * @brief Sends an isochronous USB request with a timeout interval based on the passed request.
155094332d3Sopenharmony_ci *
156094332d3Sopenharmony_ci * @param req Indicates the pointer to the USB request.
157094332d3Sopenharmony_ci * @param timeout Indicates the timeout interval for canceling a USB request.
158094332d3Sopenharmony_ci * The value <b>0</b> indicates that the system waits until the USB request is complete.
159094332d3Sopenharmony_ci *
160094332d3Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful;
161094332d3Sopenharmony_ci * returns a negative value defined in {@link UsbErrorType} otherwise.
162094332d3Sopenharmony_ci */
163094332d3Sopenharmony_ciint32_t UsbFnSubmitRequestSync(struct UsbFnRequest *req, uint32_t timeout);
164094332d3Sopenharmony_ci
165094332d3Sopenharmony_ci#ifdef __cplusplus
166094332d3Sopenharmony_ci}
167094332d3Sopenharmony_ci#endif
168094332d3Sopenharmony_ci
169094332d3Sopenharmony_ci#endif /* USBFN_REQUEST_H */
170