update(port/template): update template

Signed-off-by: sakumisu <1203593632@qq.com>
This commit is contained in:
sakumisu 2025-01-23 23:04:34 +08:00
parent f447de38dc
commit efbfc9d70f
2 changed files with 18 additions and 175 deletions

View File

@ -48,6 +48,11 @@ int usbd_set_remote_wakeup(uint8_t busid)
return -1;
}
uint8_t usbd_get_port_speed(uint8_t busid)
{
return USB_SPEED_FULL;
}
int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep)
{
uint8_t ep_idx = USB_EP_GET_IDX(ep->bEndpointAddress);
@ -55,9 +60,11 @@ int usbd_ep_open(uint8_t busid, const struct usb_endpoint_descriptor *ep)
if (USB_EP_DIR_IS_OUT(ep->bEndpointAddress)) {
g_xxx_udc.out_ep[ep_idx].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
g_xxx_udc.out_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
g_xxx_udc.out_ep[ep_idx].ep_enable = true;
} else {
g_xxx_udc.in_ep[ep_idx].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
g_xxx_udc.in_ep[ep_idx].ep_type = USB_GET_ENDPOINT_TYPE(ep->bmAttributes);
g_xxx_udc.in_ep[ep_idx].ep_enable = true;
}
return 0;
}

View File

@ -1,79 +1,27 @@
/*
* Copyright (c) 2022, sakumisu
* Copyright (c) 2025, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usbh_core.h"
#include "usbh_hub.h"
#include "usb_xxx_reg.h"
struct dwc2_pipe {
bool inuse;
uint32_t xfrd;
volatile bool waiter;
usb_osal_sem_t waitsem;
struct usbh_hubport *hport;
struct usbh_urb *urb;
};
struct dwc2_hcd {
struct dwc2_pipe pipe_pool[CONFIG_USBHOST_PIPE_NUM];
} g_dwc2_hcd;
static int dwc2_chan_alloc(void)
{
int chidx;
for (chidx = 0; chidx < CONFIG_USBHOST_PIPE_NUM; chidx++) {
if (!g_dwc2_hcd.pipe_pool[chidx].inuse) {
g_dwc2_hcd.pipe_pool[chidx].inuse = true;
return chidx;
}
}
return -1;
}
static void dwc2_chan_free(struct dwc2_pipe *pipe)
{
pipe->inuse = false;
}
static int usbh_reset_port(const uint8_t port)
int usb_hc_init(struct usbh_bus *bus)
{
return 0;
}
static uint8_t usbh_get_port_speed(const uint8_t port)
{
return USB_SPEED_UNKNOWN;
}
__WEAK void usb_hc_low_level_init(void)
{
}
int usb_hc_init(void)
{
int ret;
memset(&g_dwc2_hcd, 0, sizeof(struct dwc2_hcd));
for (uint8_t chidx = 0; chidx < CONFIG_USBHOST_PIPE_NUM; chidx++) {
g_dwc2_hcd.pipe_pool[chidx].waitsem = usb_osal_sem_create(0);
}
usb_hc_low_level_init();
return 0;
}
uint16_t usbh_get_frame_number(void)
int usb_hc_deinit(struct usbh_bus *bus)
{
return 0;
}
int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf)
uint16_t usbh_get_frame_number(struct usbh_bus *bus)
{
return 0;
}
int usbh_roothub_control(struct usbh_bus *bus, struct usb_setup_packet *setup, uint8_t *buf)
{
uint8_t nports;
uint8_t port;
@ -150,7 +98,6 @@ int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf)
case HUB_PORT_FEATURE_POWER:
break;
case HUB_PORT_FEATURE_RESET:
usbh_reset_port(port);
break;
default:
@ -173,123 +120,12 @@ int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf)
int usbh_submit_urb(struct usbh_urb *urb)
{
struct dwc2_chan *chan;
size_t flags;
int ret = 0;
int chidx;
if (!urb || !urb->hport || !urb->ep) {
return -USB_ERR_INVAL;
}
if (!urb->hport->connected) {
return -USB_ERR_NOTCONN;
}
if (urb->errorcode == -USB_ERR_BUSY) {
return -USB_ERR_BUSY;
}
flags = usb_osal_enter_critical_section();
chidx = dwc2_chan_alloc();
if (chidx == -1) {
usb_osal_leave_critical_section(flags);
return -USB_ERR_NOMEM;
}
chan = &g_dwc2_hcd.chan_pool[chidx];
chan->chidx = chidx;
chan->urb = urb;
urb->hcpriv = chan;
urb->errorcode = -USB_ERR_BUSY;
urb->actual_length = 0;
usb_osal_leave_critical_section(flags);
switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
case USB_ENDPOINT_TYPE_CONTROL:
break;
case USB_ENDPOINT_TYPE_BULK:
break;
case USB_ENDPOINT_TYPE_INTERRUPT:
break;
case USB_ENDPOINT_TYPE_ISOCHRONOUS:
break;
default:
break;
}
if (urb->timeout > 0) {
/* wait until timeout or sem give */
ret = usb_osal_sem_take(chan->waitsem, urb->timeout);
if (ret < 0) {
goto errout_timeout;
}
urb->timeout = 0;
ret = urb->errorcode;
/* we can free chan when waitsem is done */
dwc2_chan_free(chan);
}
return ret;
errout_timeout:
urb->timeout = 0;
usbh_kill_urb(urb);
return ret;
return -USB_ERR_NOTSUPP;
}
int usbh_kill_urb(struct usbh_urb *urb)
{
struct dwc2_chan *chan;
size_t flags;
if (!urb || !urb->hcpriv) {
return -USB_ERR_INVAL;
}
flags = usb_osal_enter_critical_section();
chan = (struct dwc2_chan *)urb->hcpriv;
chan->urb = NULL;
urb->hcpriv = NULL;
if (urb->timeout) {
urb->timeout = 0;
urb->errorcode = -USB_ERR_SHUTDOWN;
usb_osal_sem_give(chan->waitsem);
} else {
dwc2_chan_free(chan);
}
usb_osal_leave_critical_section(flags);
return 0;
}
static inline void dwc2_urb_waitup(struct usbh_urb *urb)
{
struct dwc2_chan *chan;
chan = (struct dwc2_chan *)urb->hcpriv;
chan->urb = NULL;
urb->hcpriv = NULL;
if (urb->timeout) {
urb->timeout = 0;
usb_osal_sem_give(chan->waitsem);
} else {
dwc2_chan_free(chan);
}
if (urb->complete) {
if (urb->errorcode < 0) {
urb->complete(urb->arg, urb->errorcode);
} else {
urb->complete(urb->arg, urb->actual_length);
}
}
return -USB_ERR_NOTSUPP;
}
void USBH_IRQHandler(uint8_t busid)