From 09c6f2b655c233fa9bdbd1369b52b79536415468 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Thu, 15 Sep 2022 20:46:14 +0800 Subject: [PATCH] add urb for msc class --- class/hub/usbh_hub.c | 6 ++--- class/msc/usbh_msc.c | 62 ++++++++++++++++++++++++++++++++------------ class/msc/usbh_msc.h | 10 ++++--- core/usbh_core.h | 2 +- 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/class/hub/usbh_hub.c b/class/hub/usbh_hub.c index 892a3eef..2f2ccf69 100644 --- a/class/hub/usbh_hub.c +++ b/class/hub/usbh_hub.c @@ -255,7 +255,7 @@ static void hub_int_complete_callback(void *arg, int nbytes) if (nbytes > 0) { usbh_hub_thread_wakeup(hub); - usbh_submit_urb(&hub->inturb); + usbh_submit_urb(&hub->intin_urb); } } @@ -319,8 +319,8 @@ static int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf) usbh_hub_register(hub); USB_LOG_INFO("Register HUB Class:%s\r\n", hport->config.intf[intf].devname); - usbh_int_urb_fill(&hub->inturb, hub->intin, hub->int_buffer, 1, 0, hub_int_complete_callback, hub); - usbh_submit_urb(&hub->inturb); + usbh_int_urb_fill(&hub->intin_urb, hub->intin, hub->int_buffer, 1, 0, hub_int_complete_callback, hub); + usbh_submit_urb(&hub->intin_urb); return 0; } diff --git a/class/msc/usbh_msc.c b/class/msc/usbh_msc.c index 54abe2e4..d85fbf8c 100644 --- a/class/msc/usbh_msc.c +++ b/class/msc/usbh_msc.c @@ -107,6 +107,34 @@ static void usbh_msc_csw_dump(struct CSW *csw) #endif } +static inline int usbh_msc_bulk_in_transfer(struct usbh_msc *msc_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout) +{ + int ret; + struct usbh_urb *urb = &msc_class->bulkin_urb; + memset(urb, 0, sizeof(struct usbh_urb)); + + usbh_bulk_urb_fill(urb, msc_class->bulkin, buffer, buflen, timeout, NULL, NULL); + ret = usbh_submit_urb(urb); + if (ret == 0) { + ret = urb->actual_length; + } + return ret; +} + +static inline int usbh_msc_bulk_out_transfer(struct usbh_msc *msc_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout) +{ + int ret; + struct usbh_urb *urb = &msc_class->bulkout_urb; + memset(urb, 0, sizeof(struct usbh_urb)); + + usbh_bulk_urb_fill(urb, msc_class->bulkout, buffer, buflen, timeout, NULL, NULL); + ret = usbh_submit_urb(urb); + if (ret == 0) { + ret = urb->actual_length; + } + return ret; +} + static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class) { int nbytes; @@ -122,10 +150,10 @@ static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class) usbh_msc_cbw_dump(cbw); /* Send the CBW */ - nbytes = usbh_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the CSW */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { usbh_msc_csw_dump((struct CSW *)g_msc_buf); } @@ -151,13 +179,13 @@ static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class) usbh_msc_cbw_dump(cbw); /* Send the CBW */ - nbytes = usbh_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the sense data response */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, SCSIRESP_FIXEDSENSEDATA_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, SCSIRESP_FIXEDSENSEDATA_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the CSW */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { usbh_msc_csw_dump((struct CSW *)g_msc_buf); } @@ -184,13 +212,13 @@ static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class) usbh_msc_cbw_dump(cbw); /* Send the CBW */ - nbytes = usbh_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the sense data response */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, SCSIRESP_INQUIRY_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, SCSIRESP_INQUIRY_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the CSW */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { usbh_msc_csw_dump((struct CSW *)g_msc_buf); } @@ -216,16 +244,16 @@ static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class) usbh_msc_cbw_dump(cbw); /* Send the CBW */ - nbytes = usbh_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the sense data response */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, SCSIRESP_READCAPACITY10_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, SCSIRESP_READCAPACITY10_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Save the capacity information */ msc_class->blocknum = GET_BE32(&g_msc_buf[0]) + 1; msc_class->blocksize = GET_BE32(&g_msc_buf[4]); /* Receive the CSW */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { usbh_msc_csw_dump((struct CSW *)g_msc_buf); } @@ -253,13 +281,13 @@ int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, con usbh_msc_cbw_dump(cbw); /* Send the CBW */ - nbytes = usbh_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Send the user data */ - nbytes = usbh_bulk_transfer(msc_class->bulkout, (uint8_t *)buffer, msc_class->blocksize * nsectors, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)buffer, msc_class->blocksize * nsectors, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the CSW */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { usbh_msc_csw_dump((struct CSW *)g_msc_buf); } @@ -288,13 +316,13 @@ int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, cons usbh_msc_cbw_dump(cbw); /* Send the CBW */ - nbytes = usbh_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the user data */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, (uint8_t *)buffer, msc_class->blocksize * nsectors, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, (uint8_t *)buffer, msc_class->blocksize * nsectors, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { /* Receive the CSW */ - nbytes = usbh_bulk_transfer(msc_class->bulkin, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); + nbytes = usbh_msc_bulk_in_transfer(msc_class, g_msc_buf, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); if (nbytes >= 0) { usbh_msc_csw_dump((struct CSW *)g_msc_buf); } diff --git a/class/msc/usbh_msc.h b/class/msc/usbh_msc.h index cb13a3b2..5a61de7d 100644 --- a/class/msc/usbh_msc.h +++ b/class/msc/usbh_msc.h @@ -14,10 +14,12 @@ struct usbh_msc { uint8_t intf; /* Data interface number */ uint8_t sdchar; - usbh_pipe_t bulkin; /* Bulk IN endpoint */ - usbh_pipe_t bulkout; /* Bulk OUT endpoint */ - uint32_t blocknum; /* Number of blocks on the USB mass storage device */ - uint16_t blocksize; /* Block size of USB mass storage device */ + usbh_pipe_t bulkin; /* Bulk IN endpoint */ + usbh_pipe_t bulkout; /* Bulk OUT endpoint */ + struct usbh_urb bulkin_urb; /* Bulk IN urb */ + struct usbh_urb bulkout_urb; /* Bulk OUT urb */ + uint32_t blocknum; /* Number of blocks on the USB mass storage device */ + uint16_t blocksize; /* Block size of USB mass storage device */ }; int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors); diff --git a/core/usbh_core.h b/core/usbh_core.h index 8d9ee453..dab1e4fb 100644 --- a/core/usbh_core.h +++ b/core/usbh_core.h @@ -175,7 +175,7 @@ struct usbh_hub { uint8_t hub_addr; usbh_pipe_t intin; USB_MEM_ALIGNX uint8_t int_buffer[1]; - struct usbh_urb inturb; + struct usbh_urb intin_urb; struct usb_hub_descriptor hub_desc; struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS]; struct usbh_hubport *parent;