From 1cb56fe0efcfe46b3008992ff73256759a7a991d Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Mon, 7 Feb 2022 17:32:36 +0800 Subject: [PATCH] replace ssize_t with int --- class/cdc/usbh_cdc_acm.c | 12 ++++- class/hid/usbh_hid.c | 7 ++- class/hub/usbh_hub.c | 11 +++- class/msc/usbh_msc.c | 107 ++++++++++++++++++++++++++++++++++----- 4 files changed, 119 insertions(+), 18 deletions(-) diff --git a/class/cdc/usbh_cdc_acm.c b/class/cdc/usbh_cdc_acm.c index 27d51584..fdc020a3 100644 --- a/class/cdc/usbh_cdc_acm.c +++ b/class/cdc/usbh_cdc_acm.c @@ -180,6 +180,7 @@ int usbh_cdc_acm_connect(struct usbh_hubport *hport, uint8_t intf) struct usbh_cdc_acm *cdc_acm_class = usb_malloc(sizeof(struct usbh_cdc_acm)); if (cdc_acm_class == NULL) { + USB_LOG_ERR("Fail to alloc cdc_acm_class\r\n"); return -ENOMEM; } @@ -192,8 +193,15 @@ int usbh_cdc_acm_connect(struct usbh_hubport *hport, uint8_t intf) hport->config.intf[intf + 1].priv = cdc_acm_class; cdc_acm_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); + if (cdc_acm_class->setup == NULL) { + USB_LOG_ERR("Fail to alloc setup\r\n"); + return -ENOMEM; + } cdc_acm_class->linecoding = usb_iomalloc(sizeof(struct cdc_line_coding)); - + if (cdc_acm_class->linecoding == NULL) { + USB_LOG_ERR("Fail to alloc linecoding\r\n"); + return -ENOMEM; + } cdc_acm_class->ctrl_intf = intf; cdc_acm_class->data_intf = intf + 1; @@ -316,7 +324,7 @@ int usbh_cdc_acm_disconnect(struct usbh_hubport *hport, uint8_t intf) return ret; } -void usb_cdc_acm_callback(void *arg, int result) +void usbh_cdc_acm_callback(void *arg, int result) { printf("result:%d\r\n", result); } diff --git a/class/hid/usbh_hid.c b/class/hid/usbh_hid.c index b7aff16f..0a240318 100644 --- a/class/hid/usbh_hid.c +++ b/class/hid/usbh_hid.c @@ -175,7 +175,8 @@ int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf) struct usbh_hid *hid_class = usb_malloc(sizeof(struct usbh_hid)); if (hid_class == NULL) { - return -1; + USB_LOG_ERR("Fail to alloc hid_class\r\n"); + return -ENOMEM; } memset(hid_class, 0, sizeof(struct usbh_hid)); @@ -185,6 +186,10 @@ int usbh_hid_connect(struct usbh_hubport *hport, uint8_t intf) hport->config.intf[intf].priv = hid_class; hid_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); + if (hid_class->setup == NULL) { + USB_LOG_ERR("Fail to alloc setup\r\n"); + return -ENOMEM; + } hid_class->intf = intf; ret = usbh_hid_set_idle(hport, intf, 0, 0); diff --git a/class/hub/usbh_hub.c b/class/hub/usbh_hub.c index 9db9c0b2..5b010c58 100644 --- a/class/hub/usbh_hub.c +++ b/class/hub/usbh_hub.c @@ -215,11 +215,20 @@ int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf) hub_class = usb_malloc(sizeof(struct usbh_hub)); if (hub_class == NULL) { - return -1; + USB_LOG_ERR("Fail to alloc hub_class\r\n"); + return -ENOMEM; } memset(hub_class, 0, sizeof(struct usbh_hub)); hub_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); + if (hub_class->setup == NULL) { + USB_LOG_ERR("Fail to alloc setup\r\n"); + return -ENOMEM; + } hub_class->port_status = usb_iomalloc(sizeof(struct hub_port_status)); + if (hub_class->port_status == NULL) { + USB_LOG_ERR("Fail to alloc port_status\r\n"); + return -ENOMEM; + } hub_desc_buffer = usb_iomalloc(32); diff --git a/class/msc/usbh_msc.c b/class/msc/usbh_msc.c index 90da54f1..a7498fa8 100644 --- a/class/msc/usbh_msc.c +++ b/class/msc/usbh_msc.c @@ -21,6 +21,7 @@ */ #include "usbh_core.h" #include "usbh_msc.h" +#include "scsi.h" #define DEV_FORMAT "/dev/sd%c" #define DEV_NAMELEN 16 @@ -147,8 +148,9 @@ static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class) memset(cbw, 0, USB_SIZEOF_MSC_CBW); cbw->dSignature = MSC_CBW_Signature; - cbw->bCBLength = 0x06; - cbw->CB[0] = SCSI_TEST_UNIT_READY; + cbw->bCBLength = SCSICMD_TESTUNITREADY_SIZEOF; + cbw->CB[0] = SCSI_CMD_TESTUNITREADY; + /* Send the CBW */ nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); if (nbytes >= 0) { @@ -172,15 +174,15 @@ static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class) cbw->dSignature = MSC_CBW_Signature; cbw->bmFlags = 0x80; - cbw->bCBLength = 0x06; - cbw->dDataLength = 18; + cbw->bCBLength = SCSIRESP_FIXEDSENSEDATA_SIZEOF; + cbw->dDataLength = SCSICMD_REQUESTSENSE_SIZEOF; cbw->CB[0] = SCSI_REQUEST_SENSE; - cbw->CB[4] = 18; + cbw->CB[4] = SCSIRESP_FIXEDSENSEDATA_SIZEOF; /* Send the CBW */ nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); if (nbytes >= 0) { /* Receive the sense data response */ - nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, 18); + nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_FIXEDSENSEDATA_SIZEOF); if (nbytes >= 0) { /* Receive the CSW */ nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); @@ -202,17 +204,17 @@ static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class) memset(cbw, 0, USB_SIZEOF_MSC_CBW); cbw->dSignature = MSC_CBW_Signature; - cbw->dDataLength = 36; + cbw->dDataLength = SCSIRESP_INQUIRY_SIZEOF; cbw->bmFlags = 0x80; - cbw->bCBLength = 6; + cbw->bCBLength = SCSICMD_INQUIRY_SIZEOF; cbw->CB[0] = SCSI_INQUIRY; - cbw->CB[4] = 36; + cbw->CB[4] = SCSIRESP_INQUIRY_SIZEOF; /* Send the CBW */ nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); if (nbytes >= 0) { /* Receive the sense data response */ - nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, 36); + nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_INQUIRY_SIZEOF); if (nbytes >= 0) { /* Receive the CSW */ nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); @@ -234,16 +236,16 @@ static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class) memset(cbw, 0, USB_SIZEOF_MSC_CBW); cbw->dSignature = MSC_CBW_Signature; - cbw->dDataLength = 8; + cbw->dDataLength = SCSIRESP_READCAPACITY10_SIZEOF; cbw->bmFlags = 0x80; - cbw->bCBLength = 10; + cbw->bCBLength = SCSICMD_READCAPACITY10_SIZEOF; cbw->CB[0] = SCSI_READ_CAPACITY10; /* Send the CBW */ nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); if (nbytes >= 0) { /* Receive the sense data response */ - nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, 8); + nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, SCSIRESP_READCAPACITY10_SIZEOF); if (nbytes >= 0) { /* Save the capacity information */ msc_class->blocknum = GET_BE32(&msc_class->tx_buffer[0]) + 1; @@ -260,6 +262,73 @@ static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class) return nbytes < 0 ? (int)nbytes : 0; } +int usbh_msc_mem_write(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors) +{ + int nbytes; + struct CBW *cbw; + + /* Construct the CBW */ + cbw = (struct CBW *)msc_class->tx_buffer; + memset(cbw, 0, USB_SIZEOF_MSC_CBW); + cbw->dSignature = MSC_CBW_Signature; + + cbw->dDataLength = (msc_class->blocksize * nsectors); + cbw->bCBLength = SCSICMD_WRITE10_SIZEOF; + cbw->CB[0] = SCSI_WRITE10; + + SET_BE24(&cbw->CB[2], sector); + SET_BE24(&cbw->CB[7], nsectors); + + /* Send the CBW */ + nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); + if (nbytes >= 0) { + /* Send the user data */ + nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)buffer, msc_class->blocksize * nsectors); + if (nbytes >= 0) { + /* Receive the CSW */ + nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); + if (nbytes >= 0) { + usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); + } + } + } + return nbytes < 0 ? (int)nbytes : 0; +} + +int usbh_msc_mem_read(struct usbh_msc *msc_class, uint32_t sector, const uint8_t *buffer, uint32_t nsectors) +{ + int nbytes; + struct CBW *cbw; + + /* Construct the CBW */ + cbw = (struct CBW *)msc_class->tx_buffer; + memset(cbw, 0, USB_SIZEOF_MSC_CBW); + cbw->dSignature = MSC_CBW_Signature; + + cbw->dDataLength = (msc_class->blocksize * nsectors); + cbw->bmFlags = 0x80; + cbw->bCBLength = SCSICMD_READ10_SIZEOF; + cbw->CB[0] = SCSI_READ10; + + SET_BE24(&cbw->CB[2], sector); + SET_BE24(&cbw->CB[7], nsectors); + + /* Send the CBW */ + nbytes = usbh_ep_bulk_transfer(msc_class->bulkout, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW); + if (nbytes >= 0) { + /* Receive the user data */ + nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, (uint8_t *)buffer, msc_class->blocksize * nsectors); + if (nbytes >= 0) { + /* Receive the CSW */ + nbytes = usbh_ep_bulk_transfer(msc_class->bulkin, msc_class->tx_buffer, USB_SIZEOF_MSC_CSW); + if (nbytes >= 0) { + usbh_msc_csw_dump((struct CSW *)msc_class->tx_buffer); + } + } + } + return nbytes < 0 ? (int)nbytes : 0; +} + int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf) { struct usbh_endpoint_cfg ep_cfg = { 0 }; @@ -270,6 +339,7 @@ int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf) struct usbh_msc *msc_class = usb_malloc(sizeof(struct usbh_msc)); if (msc_class == NULL) { + USB_LOG_ERR("Fail to alloc msc_class\r\n"); return -ENOMEM; } @@ -281,8 +351,15 @@ int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf) hport->config.intf[intf].priv = msc_class; msc_class->setup = usb_iomalloc(sizeof(struct usb_setup_packet)); + if (msc_class->setup == NULL) { + USB_LOG_ERR("Fail to alloc setup\r\n"); + return -ENOMEM; + } msc_class->tx_buffer = usb_iomalloc(128); - + if (msc_class->tx_buffer == NULL) { + USB_LOG_ERR("Fail to alloc tx_buffer\r\n"); + return -ENOMEM; + } ret = usbh_msc_get_maxlun(hport, intf, msc_class->tx_buffer); if (ret < 0) { return ret; @@ -341,6 +418,8 @@ int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf) if (msc_class->setup) usb_iofree(msc_class->setup); + if (msc_class->tx_buffer) + usb_iofree(msc_class->tx_buffer); usb_free(msc_class);