summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2022-09-04 20:17:32 +0800
committersakumisu <[email protected]>2022-09-09 19:09:22 +0800
commit62d000092661d61a660fa9ea3f52afd6e493db5b (patch)
treee9946a397559abc40b3b24f7cef5d6de05372d82 /common
parente54d88e0e1ca2f894621c704850bc7426a1a2753 (diff)
refactor usb host stack: hcd api, hub process and usb no cache ram process
Diffstat (limited to 'common')
-rw-r--r--common/usb_hc.h169
-rw-r--r--common/usb_list.h33
2 files changed, 59 insertions, 143 deletions
diff --git a/common/usb_hc.h b/common/usb_hc.h
index ac53f6b7..20a32945 100644
--- a/common/usb_hc.h
+++ b/common/usb_hc.h
@@ -12,8 +12,8 @@
extern "C" {
#endif
-typedef void (*usbh_asynch_callback_t)(void *arg, int nbytes);
-typedef void *usbh_epinfo_t;
+typedef void (*usbh_complete_callback_t)(void *arg, int nbytes);
+typedef void *usbh_pipe_t;
/**
* @brief USB Endpoint Configuration.
@@ -29,174 +29,87 @@ struct usbh_endpoint_cfg {
};
/**
- * @brief usb host software init, used for global reset.
+ * @brief USB Urb Configuration.
*
- * @return On success will return 0, and others indicate fail.
+ * Structure containing the USB Urb configuration.
*/
-int usb_hc_sw_init(void);
+struct usbh_urb {
+ usbh_pipe_t pipe;
+ struct usb_setup_packet *setup;
+ uint8_t *transfer_buffer;
+ uint32_t transfer_buffer_length;
+ int transfer_flags;
+ uint32_t actual_length;
+ uint32_t timeout;
+ int errorcode;
+ usbh_complete_callback_t complete;
+ void *arg;
+};
/**
* @brief usb host controller hardware init.
*
* @return On success will return 0, and others indicate fail.
*/
-int usb_hc_hw_init(void);
+int usb_hc_init(void);
/**
- * @brief get port connect status
+ * @brief control roothub.
*
- * @param port
+ * @param setup setup request buffer.
+ * @param buf buf for reading response or write data.
* @return On success will return 0, and others indicate fail.
*/
-bool usbh_get_port_connect_status(const uint8_t port);
-
-/**
- * @brief reset roothub port
- *
- * @param port port index
- * @return On success will return 0, and others indicate fail.
- */
-int usbh_reset_port(const uint8_t port);
-
-/**
- * @brief get roothub port speed
- *
- * @param port port index
- * @return return 1 means USB_SPEED_LOW, 2 means USB_SPEED_FULL and 3 means USB_SPEED_HIGH.
- */
-uint8_t usbh_get_port_speed(const uint8_t port);
+int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf);
/**
- * @brief reconfig control endpoint.
+ * @brief reconfig control endpoint pipe.
*
- * @param ep A memory location provided by the caller.
+ * @param pipe A memory allocated for pipe.
* @param dev_addr device address.
* @param ep_mps control endpoint max packet size.
* @param speed port speed
* @return On success will return 0, and others indicate fail.
*/
-int usbh_ep0_reconfigure(usbh_epinfo_t ep, uint8_t dev_addr, uint8_t ep_mps, uint8_t speed);
+int usbh_ep0_pipe_reconfigure(usbh_pipe_t pipe, uint8_t dev_addr, uint8_t ep_mps, uint8_t speed);
/**
- * @brief Allocate and config endpoint
+ * @brief Allocate pipe for endpoint
*
- * @param ep A memory location provided by the caller in which to save the allocated endpoint info.
+ * @param pipe A memory location provided by the caller in which to save the allocated pipe.
* @param ep_cfg Describes the endpoint info to be allocated.
* @return On success will return 0, and others indicate fail.
*/
-int usbh_ep_alloc(usbh_epinfo_t *ep, const struct usbh_endpoint_cfg *ep_cfg);
+int usbh_pipe_alloc(usbh_pipe_t *pipe, const struct usbh_endpoint_cfg *ep_cfg);
/**
- * @brief Free a memory in which saves endpoint info.
+ * @brief Free a pipe in which saves endpoint info.
*
- * @param ep A memory location provided by the caller in which to free the allocated endpoint info.
+ * @param pipe A memory location provided by the caller in which to free the allocated endpoint info.
* @return On success will return 0, and others indicate fail.
*/
-int usbh_ep_free(usbh_epinfo_t ep);
+int usbh_pipe_free(usbh_pipe_t pipe);
/**
- * @brief Perform a control transfer.
- * This is a blocking method; this method will not return until the transfer has completed.
+ * @brief Submit a usb transfer request to an endpoint.
*
- * @param ep The control endpoint to send/receive the control request.
- * @param setup Setup packet to be sent.
- * @param buffer buffer used for sending the request and for returning any responses. This buffer must be large enough to hold the length value
- * in the request description.
- * @return On success will return 0, and others indicate fail.
- */
-int usbh_control_transfer(usbh_epinfo_t ep, struct usb_setup_packet *setup, uint8_t *buffer);
-
-/**
- * @brief Process a request to handle a transfer descriptor. This method will
- * enqueue the transfer request and wait for it to complete. Only one transfer may be queued;
- * This is a blocking method; this method will not return until the transfer has completed.
- *
- * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
- * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
- * @param buflen The length of the data to be sent or received.
- * @param timeout Timeout for transfer, unit is ms.
- * @return On success, a non-negative value is returned that indicates the number
- * of bytes successfully transferred. On a failure, a negated errno value
- * is returned that indicates the nature of the failure:
- *
- * -EAGAIN - If devices NAKs the transfer (or NYET or other error where
- * it may be appropriate to restart the entire transaction).
- * -EPERM - If the endpoint stalls
- * -EIO - On a TX or data toggle error
- * -EPIPE - Overrun errors
- * -ETIMEDOUT - Sem wait timeout
- *
- */
-int usbh_ep_bulk_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
-
-/**
- * @brief Process a request to handle a transfer descriptor. This method will
- * enqueue the transfer request and wait for it to complete. Only one transfer may be queued;
- * This is a blocking method; this method will not return until the transfer has completed.
+ * If timeout is not zero, this function will be in poll transfer mode,
+ * otherwise will be in async transfer mode.
*
- * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
- * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
- * @param buflen The length of the data to be sent or received.
- * @param timeout Timeout for transfer, unit is ms.
- * @return On success, a non-negative value is returned that indicates the number
- * of bytes successfully transferred. On a failure, a negated errno value
- * is returned that indicates the nature of the failure:
- *
- * -EAGAIN - If devices NAKs the transfer (or NYET or other error where
- * it may be appropriate to restart the entire transaction).
- * -EPERM - If the endpoint stalls
- * -EIO - On a TX or data toggle error
- * -EPIPE - Overrun errors
- * -ETIMEDOUT - Sem wait timeout
- *
- */
-int usbh_ep_intr_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
-
-/**
- * @brief Process a request to handle a transfer asynchronously. This method
- * will enqueue the transfer request and return immediately. Only one transfer may be queued on a given endpoint
- * When the transfer completes, the callback will be invoked with the provided argument.
- *
- * This method is useful for receiving interrupt transfers which may come infrequently.
- *
- * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
- * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
- * @param buflen The length of the data to be sent or received.
- * @param callback This function will be called when the transfer completes.
- * @param arg The arbitrary parameter that will be passed to the callback function when the transfer completes.
- *
- * @return On success will return 0, and others indicate fail.
+ * @param urb Usb request block.
+ * @return On success will return 0, and others indicate fail.
*/
-int usbh_ep_bulk_async_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, usbh_asynch_callback_t callback, void *arg);
+int usbh_submit_urb(struct usbh_urb *urb);
/**
- * @brief Process a request to handle a transfer asynchronously. This method
- * will enqueue the transfer request and return immediately. Only one transfer may be queued on a given endpoint
- * When the transfer completes, the callback will be invoked with the provided argument.
+ * @brief Cancel a transfer request.
*
- * This method is useful for receiving interrupt transfers which may come infrequently.
+ * This function will call When calls usbh_submit_urb and return -ETIMEOUT or -ESHUTDOWN.
*
- * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to perform the transfer.
- * @param buffer A buffer containing the data to be sent (OUT endpoint) or received (IN endpoint).
- * @param buflen The length of the data to be sent or received.
- * @param callback This function will be called when the transfer completes.
- * @param arg The arbitrary parameter that will be passed to the callback function when the transfer completes.
- *
- * @return On success will return 0, and others indicate fail.
- */
-int usbh_ep_intr_async_transfer(usbh_epinfo_t ep, uint8_t *buffer, uint32_t buflen, usbh_asynch_callback_t callback, void *arg);
-
-/**
- * @brief Cancel any pending syncrhonous or asynchronous transfer on an endpoint.
- *
- * @param ep The IN or OUT endpoint descriptor for the device endpoint on which to cancel.
- * @return On success will return 0, and others indicate fail.
+ * @param urb Usb request block.
+ * @return On success will return 0, and others indicate fail.
*/
-int usb_ep_cancel(usbh_epinfo_t ep);
-
-/* usb hcd irq callback */
-
-void usbh_event_notify_handler(uint8_t event, uint8_t rhport);
+int usbh_kill_urb(struct usbh_urb *urb);
#ifdef __cplusplus
}
diff --git a/common/usb_list.h b/common/usb_list.h
index 228dc6f3..3078a1f2 100644
--- a/common/usb_list.h
+++ b/common/usb_list.h
@@ -46,12 +46,14 @@ static inline void usb_slist_add_head(usb_slist_t *l, usb_slist_t *n)
static inline void usb_slist_add_tail(usb_slist_t *l, usb_slist_t *n)
{
- while (l->next) {
- l = l->next;
+ usb_slist_t *tmp = l;
+
+ while (tmp->next) {
+ tmp = tmp->next;
}
/* append the node to the tail */
- l->next = n;
+ tmp->next = n;
n->next = NULL;
}
@@ -74,14 +76,15 @@ static inline void usb_slist_insert(usb_slist_t *l, usb_slist_t *next, usb_slist
static inline usb_slist_t *usb_slist_remove(usb_slist_t *l, usb_slist_t *n)
{
+ usb_slist_t *tmp = l;
/* remove slist head */
- while (l->next && l->next != n) {
- l = l->next;
+ while (tmp->next && tmp->next != n) {
+ tmp = tmp->next;
}
/* remove node */
- if (l->next != (usb_slist_t *)0) {
- l->next = l->next->next;
+ if (tmp->next != (usb_slist_t *)0) {
+ tmp->next = tmp->next->next;
}
return l;
@@ -333,8 +336,8 @@ static inline unsigned int usb_dlist_len(const usb_dlist_t *l)
* @brief initialize a dlist object
*/
#define USB_DLIST_OBJECT_INIT(object) \
- { \
- &(object), &(object) \
+ { \
+ &(object), &(object) \
}
/**
* @brief initialize a dlist object
@@ -394,11 +397,11 @@ static inline unsigned int usb_dlist_len(const usb_dlist_t *l)
* @n: another dlist_t * to use as temporary storage
* @head: the head for your list.
*/
-#define usb_dlist_for_each_safe(pos, n, head) \
+#define usb_dlist_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
-#define usb_dlist_for_each_prev_safe(pos, n, head) \
+#define usb_dlist_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; pos != (head); \
pos = n, n = pos->prev)
/**
@@ -409,7 +412,7 @@ static inline unsigned int usb_dlist_len(const usb_dlist_t *l)
*/
#define usb_dlist_for_each_entry(pos, head, member) \
for (pos = usb_dlist_entry((head)->next, typeof(*pos), member); \
- &pos->member != (head); \
+ &pos->member != (head); \
pos = usb_dlist_entry(pos->member.next, typeof(*pos), member))
/**
@@ -420,7 +423,7 @@ static inline unsigned int usb_dlist_len(const usb_dlist_t *l)
*/
#define usb_dlist_for_each_entry_reverse(pos, head, member) \
for (pos = usb_dlist_entry((head)->prev, typeof(*pos), member); \
- &pos->member != (head); \
+ &pos->member != (head); \
pos = usb_dlist_entry(pos->member.prev, typeof(*pos), member))
/**
@@ -433,7 +436,7 @@ static inline unsigned int usb_dlist_len(const usb_dlist_t *l)
#define usb_dlist_for_each_entry_safe(pos, n, head, member) \
for (pos = usb_dlist_entry((head)->next, typeof(*pos), member), \
n = usb_dlist_entry(pos->member.next, typeof(*pos), member); \
- &pos->member != (head); \
+ &pos->member != (head); \
pos = n, n = usb_dlist_entry(n->member.next, typeof(*n), member))
/**
@@ -446,7 +449,7 @@ static inline unsigned int usb_dlist_len(const usb_dlist_t *l)
#define usb_dlist_for_each_entry_safe_reverse(pos, n, head, member) \
for (pos = usb_dlist_entry((head)->prev, typeof(*pos), field), \
n = usb_dlist_entry(pos->member.prev, typeof(*pos), member); \
- &pos->member != (head); \
+ &pos->member != (head); \
pos = n, n = usb_dlist_entry(pos->member.prev, typeof(*pos), member))
#ifdef __cplusplus