diff --git a/components/usb/cherryusb/class/audio/usb_audio.h b/components/usb/cherryusb/class/audio/usb_audio.h index e9d4ff49..cdb0bd98 100644 --- a/components/usb/cherryusb/class/audio/usb_audio.h +++ b/components/usb/cherryusb/class/audio/usb_audio.h @@ -132,6 +132,12 @@ #define AUDIO_FORMAT_ALAW 0x0004 #define AUDIO_FORMAT_MULAW 0x0005 +#define AUDIO_V2_FORMAT_PCM 0x00000001 +#define AUDIO_V2_FORMAT_PCM8 0x00000002 +#define AUDIO_V2_FORMAT_IEEE_FLOAT 0x00000004 +#define AUDIO_V2_FORMAT_ALAW 0x00000008 +#define AUDIO_V2_FORMAT_MULAW 0x00000010 + /* bmChannelConfig: a bitmap field that indicates which spatial locations * are occupied by the channels present in the cluster. The bit allocations * are as follows: @@ -640,7 +646,7 @@ struct audio_cs_if_ac_feature_unit_descriptor { uint8_t iFeature; } __PACKED; -#define AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(ch, n) (7 + ch * n) +#define AUDIO_SIZEOF_AC_FEATURE_UNIT_DESC(ch, n) (7 + (ch + 1) * n) struct audio_cs_if_as_general_descriptor { uint8_t bLength; @@ -754,7 +760,7 @@ struct audio_cs_ep_ep_general_descriptor { __VA_ARGS__, /* bmaControls(0) Mute */ \ 0x00 /* iTerminal */ -#define AUDIO_AS_DESCRIPTOR_INIT(bInterfaceNumber, bTerminalLink, bNrChannels, bEndpointAddress, wMaxPacketSize, bInterval, ...) \ +#define AUDIO_AS_DESCRIPTOR_INIT(bInterfaceNumber, bTerminalLink, bNrChannels, bSubFrameSize, bBitResolution, bEndpointAddress, wMaxPacketSize, bInterval, ...) \ 0x09, /* bLength */ \ USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \ bInterfaceNumber, /* bInterfaceNumber */ \ @@ -784,8 +790,8 @@ struct audio_cs_ep_ep_general_descriptor { AUDIO_STREAMING_FORMAT_TYPE, /* bDescriptorSubtype */ \ AUDIO_FORMAT_TYPE_I, /* bFormatType */ \ bNrChannels, /* bNrChannels */ \ - 0x02, /* bSubFrameSize : 2 Bytes per audio subframe */ \ - 0x10, /* bBitResolution : 16 bits per sample */ \ + bSubFrameSize, /* bSubFrameSize : Bytes per audio subframe */ \ + bBitResolution, /* bBitResolution : bits per sample */ \ (PP_NARG(__VA_ARGS__)/3), /* bSamFreqType : only one frequency supported */ \ __VA_ARGS__, /* tSamFreq : Audio sampling frequency coded on 3 bytes */ \ 0x09, /* bLength */ \ @@ -905,7 +911,7 @@ struct audio_v2_cs_if_ac_feature_unit_descriptor { uint8_t iFeature; } __PACKED; -#define AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(ch) (6 + ch * 4) +#define AUDIO_V2_SIZEOF_AC_FEATURE_UNIT_DESC(ch) (6 + (ch + 1) * 4) struct audio_v2_cs_if_as_general_descriptor { uint8_t bLength; @@ -959,7 +965,7 @@ struct audio_v2_control_range3_param_block { bFirstInterface, \ bInterfaceCount, \ USB_DEVICE_CLASS_AUDIO, \ - AUDIO_SUBCLASS_AUDIOCONTROL, \ + AUDIO_SUBCLASS_UNDEFINED, \ AUDIO_PROTOCOLv20, \ 0x00, \ /* ------------------ AudioControl Interface ------------------ */\ @@ -1049,10 +1055,10 @@ struct audio_v2_control_range3_param_block { 0x10, /* bLength */ \ AUDIO_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */ \ AUDIO_STREAMING_GENERAL, /* bDescriptorSubtype */ \ - bTerminalLink, /* bTerminalLink : Unit ID of the Output Terminal*/ \ + bTerminalLink, /* bTerminalLink : Unit ID of the Output or Input Terminal*/ \ 0x00, /* bmControls */ \ AUDIO_FORMAT_TYPE_I, /* bFormatType : AUDIO_FORMAT_TYPE_I */ \ - DBVAL(AUDIO_FORMAT_PCM), /* bmFormats PCM */ \ + DBVAL(AUDIO_V2_FORMAT_PCM), /* bmFormats PCM */ \ bNrChannels, /* bNrChannels */ \ DBVAL(bmChannelConfig), /* bmChannelConfig */ \ 0x00, /* iChannelNames */ \ @@ -1063,10 +1069,10 @@ struct audio_v2_control_range3_param_block { bSubslotSize, /* bSubslotSize */ \ bBitResolution, /* bBitResolution */ \ 0x07, /* bLength */ \ - 0x05, /* bDescriptorType */ \ + USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \ bEndpointAddress, /* bEndpointAddress 3 out endpoint for Audio */ \ 0x01, /* bmAttributes */ \ - WBVAL(wMaxPacketSize), /* XXXX wMaxPacketSize in Bytes (Freq(Samples)*2(Stereo)*2(HalfWord)) */ \ + WBVAL(wMaxPacketSize), /* XXXX wMaxPacketSize in Bytes (SampleRate * SlotByteSize * NumChannels) */ \ bInterval, /* bInterval */ \ 0x08, /* bLength */ \ AUDIO_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */ \ diff --git a/components/usb/cherryusb/class/audio/usbd_audio.c b/components/usb/cherryusb/class/audio/usbd_audio.c index 4baab5a4..0e432055 100644 --- a/components/usb/cherryusb/class/audio/usbd_audio.c +++ b/components/usb/cherryusb/class/audio/usbd_audio.c @@ -46,36 +46,6 @@ struct audio_entity_info { static usb_slist_t usbd_audio_entity_info_head = USB_SLIST_OBJECT_INIT(usbd_audio_entity_info_head); -#if CONFIG_USBDEV_AUDIO_VERSION >= 0x0200 -const uint8_t default_sampling_freq_table[] = { - AUDIO_SAMPLE_FREQ_NUM(1), - // AUDIO_SAMPLE_FREQ_4B(8000), - // AUDIO_SAMPLE_FREQ_4B(8000), - // AUDIO_SAMPLE_FREQ_4B(0x00), - AUDIO_SAMPLE_FREQ_4B(16000), - AUDIO_SAMPLE_FREQ_4B(16000), - AUDIO_SAMPLE_FREQ_4B(0x00), - AUDIO_SAMPLE_FREQ_4B(32000), - AUDIO_SAMPLE_FREQ_4B(32000), - AUDIO_SAMPLE_FREQ_4B(0x00), - AUDIO_SAMPLE_FREQ_4B(44100), - AUDIO_SAMPLE_FREQ_4B(44100), - AUDIO_SAMPLE_FREQ_4B(0x00), - AUDIO_SAMPLE_FREQ_4B(48000), - AUDIO_SAMPLE_FREQ_4B(48000), - AUDIO_SAMPLE_FREQ_4B(0x00), - AUDIO_SAMPLE_FREQ_4B(88200), - AUDIO_SAMPLE_FREQ_4B(88200), - AUDIO_SAMPLE_FREQ_4B(0x00), - AUDIO_SAMPLE_FREQ_4B(96000), - AUDIO_SAMPLE_FREQ_4B(96000), - AUDIO_SAMPLE_FREQ_4B(0x00), - AUDIO_SAMPLE_FREQ_4B(192000), - AUDIO_SAMPLE_FREQ_4B(192000), - AUDIO_SAMPLE_FREQ_4B(0x00), -}; -#endif - #if CONFIG_USBDEV_AUDIO_VERSION < 0x0200 static int audio_class_endpoint_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) { @@ -109,6 +79,7 @@ static int audio_class_endpoint_request_handler(struct usb_setup_packet *setup, sampling_freq = 16000; memcpy(*data, &sampling_freq, 4); *len = 4; + USB_LOG_DBG("Get ep:%02x %d Hz\r\n", ep, (int)sampling_freq); break; default: USB_LOG_WRN("Unhandled Audio Class bRequest 0x%02x\r\n", setup->bRequest); @@ -246,6 +217,7 @@ static int audio_class_interface_request_handler(struct usb_setup_packet *setup, *len = 1; } else { mute = (*data)[0]; + current_feature_control->mute_bCUR = mute; USB_LOG_DBG("Set UnitId:%d ch[%d] mute %s\r\n", entity_id, ch, mute_string[mute]); usbd_audio_set_mute(entity_id, ch, mute); } @@ -299,6 +271,7 @@ static int audio_class_interface_request_handler(struct usb_setup_packet *setup, case AUDIO_CS_CONTROL_SAM_FREQ: if (setup->bmRequestType & USB_REQUEST_DIR_MASK) { memcpy(*data, &sampling_freq[ch], sizeof(uint32_t)); + USB_LOG_DBG("Get ClockId:%d ch[%d] %d Hz\r\n", entity_id, ch, (int)sampling_freq[ch]); *len = 4; } else { memcpy(&sampling_freq[ch], *data, setup->wLength); @@ -327,8 +300,9 @@ static int audio_class_interface_request_handler(struct usb_setup_packet *setup, usbd_audio_get_sampling_freq_table(entity_id, &sampling_freq_table); num = (uint16_t)((uint16_t)(sampling_freq_table[1] << 8) | ((uint16_t)sampling_freq_table[0])); - *data = sampling_freq_table; + memcpy(*data, sampling_freq_table, (12 * num + 2)); *len = (12 * num + 2); + USB_LOG_DBG("Get sampling_freq_table entity_id:%d ch[%d] addr:%x\r\n", entity_id, ch, (uint32_t)sampling_freq_table); } else { } break; @@ -415,7 +389,7 @@ void usbd_audio_add_entity(uint8_t entity_id, uint16_t bDescriptorSubtype) control->volume.subrange[ch].wMax = 100; control->volume.subrange[ch].wRes = 1; control->mute[ch] = 0; - control->volume_bCUR = 50; + control->volume_bCUR = 0; control->mute_bCUR = 0; } #endif @@ -446,12 +420,13 @@ __WEAK void usbd_audio_set_mute(uint8_t entity_id, uint8_t ch, uint8_t enable) __WEAK void usbd_audio_set_sampling_freq(uint8_t entity_id, uint8_t ep_ch, uint32_t sampling_freq) { } + #if CONFIG_USBDEV_AUDIO_VERSION >= 0x0200 __WEAK void usbd_audio_get_sampling_freq_table(uint8_t entity_id, uint8_t **sampling_freq_table) { - *sampling_freq_table = (uint8_t *)default_sampling_freq_table; } #endif + __WEAK void usbd_audio_set_pitch(uint8_t ep, bool enable) { -} \ No newline at end of file +} diff --git a/components/usb/cherryusb/class/hid/usb_hid.h b/components/usb/cherryusb/class/hid/usb_hid.h index 19539e7f..5a88a34b 100644 --- a/components/usb/cherryusb/class/hid/usb_hid.h +++ b/components/usb/cherryusb/class/hid/usb_hid.h @@ -387,7 +387,7 @@ #define HID_KBD_USAGE_PAGEDOWN 0x4e /* Keyboard PageDown */ #define HID_KBD_USAGE_RIGHT 0x4f /* eyboard RightArrow */ #define HID_KBD_USAGE_LEFT 0x50 /* Keyboard LeftArrow */ -#define HID_KBD_USAGE_DOWN 0x5a /* Keyboard DownArrow */ +#define HID_KBD_USAGE_DOWN 0x51 /* Keyboard DownArrow */ #define HID_KBD_USAGE_UP 0x52 /* Keyboard UpArrow */ #define HID_KBD_USAGE_KPDNUMLOCK 0x53 /* Keypad Num Lock and Clear */ #define HID_KBD_USAGE_KPDNUMLOCKCLEAR 0x53 /* Keypad Num Lock and Clear */ diff --git a/components/usb/cherryusb/class/hub/usbh_hub.c b/components/usb/cherryusb/class/hub/usbh_hub.c index 0f185549..cf99f6f6 100644 --- a/components/usb/cherryusb/class/hub/usbh_hub.c +++ b/components/usb/cherryusb/class/hub/usbh_hub.c @@ -14,7 +14,9 @@ #define EXTHUB_FIRST_INDEX 2 +#if CONFIG_USBHOST_MAX_EXTHUBS > 0 static uint32_t g_devinuse = 0; +#endif USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hub_buf[32]; USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hub_intbuf[CONFIG_USBHOST_MAX_EXTHUBS + 1][1]; @@ -65,6 +67,7 @@ static void usbh_hub_register(struct usbh_hub *hub) usb_slist_add_tail(&hub_class_head, &hub->list); } +#if CONFIG_USBHOST_MAX_EXTHUBS > 0 static void usbh_hub_unregister(struct usbh_hub *hub) { usb_slist_remove(&hub_class_head, &hub->list); @@ -111,6 +114,7 @@ static int _usbh_hub_get_status(struct usbh_hub *hub, uint8_t *buffer) memcpy(buffer, g_hub_buf, 2); return ret; } +#endif static int _usbh_hub_get_portstatus(struct usbh_hub *hub, uint8_t port, struct hub_port_status *port_status) { @@ -163,6 +167,7 @@ static int _usbh_hub_clear_feature(struct usbh_hub *hub, uint8_t port, uint8_t f return usbh_control_transfer(hub->parent->ep0, setup, NULL); } +#if CONFIG_USBHOST_MAX_EXTHUBS > 0 static int parse_hub_descriptor(struct usb_hub_descriptor *desc, uint16_t length) { if (desc->bLength != USB_SIZEOF_HUB_DESC) { @@ -184,6 +189,7 @@ static int parse_hub_descriptor(struct usb_hub_descriptor *desc, uint16_t length } return 0; } +#endif static int usbh_hub_get_portstatus(struct usbh_hub *hub, uint8_t port, struct hub_port_status *port_status) { diff --git a/components/usb/cherryusb/class/msc/usbh_msc.c b/components/usb/cherryusb/class/msc/usbh_msc.c index 592efa62..8094df9a 100644 --- a/components/usb/cherryusb/class/msc/usbh_msc.c +++ b/components/usb/cherryusb/class/msc/usbh_msc.c @@ -53,36 +53,32 @@ static int usbh_msc_get_maxlun(struct usbh_msc *msc_class, uint8_t *buffer) static void usbh_msc_cbw_dump(struct CBW *cbw) { -#if 0 int i; - USB_LOG_INFO("CBW:\r\n"); - USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)cbw->dSignature); - USB_LOG_INFO(" tag: 0x%08x\r\n", (unsigned int)cbw->dTag); - USB_LOG_INFO(" datlen: 0x%08x\r\n", (unsigned int)cbw->dDataLength); - USB_LOG_INFO(" flags: 0x%02x\r\n", cbw->bmFlags); - USB_LOG_INFO(" lun: 0x%02x\r\n", cbw->bLUN); - USB_LOG_INFO(" cblen: 0x%02x\r\n", cbw->bCBLength); + USB_LOG_DBG("CBW:\r\n"); + USB_LOG_DBG(" signature: 0x%08x\r\n", (unsigned int)cbw->dSignature); + USB_LOG_DBG(" tag: 0x%08x\r\n", (unsigned int)cbw->dTag); + USB_LOG_DBG(" datlen: 0x%08x\r\n", (unsigned int)cbw->dDataLength); + USB_LOG_DBG(" flags: 0x%02x\r\n", cbw->bmFlags); + USB_LOG_DBG(" lun: 0x%02x\r\n", cbw->bLUN); + USB_LOG_DBG(" cblen: 0x%02x\r\n", cbw->bCBLength); - USB_LOG_INFO("CB:\r\n"); + USB_LOG_DBG("CB:\r\n"); for (i = 0; i < cbw->bCBLength; i += 8) { - USB_LOG_INFO(" 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n", + USB_LOG_DBG(" 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n", cbw->CB[i], cbw->CB[i + 1], cbw->CB[i + 2], cbw->CB[i + 3], cbw->CB[i + 4], cbw->CB[i + 5], cbw->CB[i + 6], cbw->CB[i + 7]); } -#endif } static void usbh_msc_csw_dump(struct CSW *csw) { -#if 0 - USB_LOG_INFO("CSW:\r\n"); - USB_LOG_INFO(" signature: 0x%08x\r\n", (unsigned int)csw->dSignature); - USB_LOG_INFO(" tag: 0x%08x\r\n", (unsigned int)csw->dTag); - USB_LOG_INFO(" residue: 0x%08x\r\n", (unsigned int)csw->dDataResidue); - USB_LOG_INFO(" status: 0x%02x\r\n", csw->bStatus); -#endif + USB_LOG_DBG("CSW:\r\n"); + USB_LOG_DBG(" signature: 0x%08x\r\n", (unsigned int)csw->dSignature); + USB_LOG_DBG(" tag: 0x%08x\r\n", (unsigned int)csw->dTag); + USB_LOG_DBG(" residue: 0x%08x\r\n", (unsigned int)csw->dDataResidue); + USB_LOG_DBG(" status: 0x%02x\r\n", csw->bStatus); } static inline int usbh_msc_bulk_in_transfer(struct usbh_msc *msc_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout) @@ -113,9 +109,65 @@ static inline int usbh_msc_bulk_out_transfer(struct usbh_msc *msc_class, uint8_t return ret; } -static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class) +int usbh_bulk_cbw_csw_xfer(struct usbh_msc *msc_class, struct CBW *cbw, struct CSW *csw, uint8_t *buffer) { int nbytes; + + usbh_msc_cbw_dump(cbw); + + /* Send the CBW */ + nbytes = usbh_msc_bulk_out_transfer(msc_class, (uint8_t *)cbw, USB_SIZEOF_MSC_CBW, CONFIG_USBHOST_MSC_TIMEOUT); + if (nbytes < 0) { + USB_LOG_ERR("cbw transfer error\n"); + goto __err_exit; + } + + if (cbw->dDataLength != 0) { + if (cbw->CB[0] == SCSI_CMD_WRITE10) { + nbytes = usbh_msc_bulk_out_transfer(msc_class, buffer, cbw->dDataLength, CONFIG_USBHOST_MSC_TIMEOUT); + } else if (cbw->CB[0] == SCSI_CMD_READCAPACITY10) { + nbytes = usbh_msc_bulk_in_transfer(msc_class, buffer, cbw->dDataLength, CONFIG_USBHOST_MSC_TIMEOUT); + if (nbytes >= 0) { + /* Save the capacity information */ + msc_class->blocknum = GET_BE32(&buffer[0]) + 1; + msc_class->blocksize = GET_BE32(&buffer[4]); + } + } else { + nbytes = usbh_msc_bulk_in_transfer(msc_class, buffer, cbw->dDataLength, CONFIG_USBHOST_MSC_TIMEOUT); + } + + if (nbytes < 0) { + USB_LOG_ERR("csw cbw data response error\n"); + goto __err_exit; + } + } + + /* Receive the CSW */ + memset(csw, 0, USB_SIZEOF_MSC_CSW); + nbytes = usbh_msc_bulk_in_transfer(msc_class, (uint8_t *)csw, USB_SIZEOF_MSC_CSW, CONFIG_USBHOST_MSC_TIMEOUT); + if (nbytes < 0) { + USB_LOG_ERR("csw transfer error\r\n"); + goto __err_exit; + } + + usbh_msc_csw_dump(csw); + + /* check csw status */ + if (csw->dSignature != MSC_CSW_Signature) { + USB_LOG_ERR("csw signature error\r\n"); + return -EINVAL; + } + + if (csw->bStatus != 0) { + USB_LOG_ERR("csw bStatus %d\r\n", csw->bStatus); + return -EINVAL; + } +__err_exit: + return nbytes < 0 ? (int)nbytes : 0; +} + +static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class) +{ struct CBW *cbw; /* Construct the CBW */ @@ -126,22 +178,11 @@ static inline int usbh_msc_scsi_testunitready(struct usbh_msc *msc_class) cbw->bCBLength = SCSICMD_TESTUNITREADY_SIZEOF; cbw->CB[0] = SCSI_CMD_TESTUNITREADY; - usbh_msc_cbw_dump(cbw); - /* Send the CBW */ - 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_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); - } - } - return nbytes < 0 ? (int)nbytes : 0; + return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_buf, NULL); } static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class) { - int nbytes; struct CBW *cbw; /* Construct the CBW */ @@ -155,26 +196,11 @@ static inline int usbh_msc_scsi_requestsense(struct usbh_msc *msc_class) cbw->CB[0] = SCSI_CMD_REQUESTSENSE; cbw->CB[4] = SCSIRESP_FIXEDSENSEDATA_SIZEOF; - usbh_msc_cbw_dump(cbw); - /* Send the CBW */ - 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_msc_bulk_in_transfer(msc_class, g_msc_buf, SCSIRESP_FIXEDSENSEDATA_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); - if (nbytes >= 0) { - /* Receive the CSW */ - 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); - } - } - } - return nbytes < 0 ? (int)nbytes : 0; + return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_buf, NULL); } static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class) { - int nbytes; struct CBW *cbw; /* Construct the CBW */ @@ -188,21 +214,7 @@ static inline int usbh_msc_scsi_inquiry(struct usbh_msc *msc_class) cbw->CB[0] = SCSI_CMD_INQUIRY; cbw->CB[4] = SCSIRESP_INQUIRY_SIZEOF; - usbh_msc_cbw_dump(cbw); - /* Send the CBW */ - 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_msc_bulk_in_transfer(msc_class, g_msc_buf, SCSIRESP_INQUIRY_SIZEOF, CONFIG_USBHOST_MSC_TIMEOUT); - if (nbytes >= 0) { - /* Receive the CSW */ - 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); - } - } - } - return nbytes < 0 ? (int)nbytes : 0; + return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_buf, g_msc_buf); } static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class) @@ -220,29 +232,11 @@ static inline int usbh_msc_scsi_readcapacity10(struct usbh_msc *msc_class) cbw->bCBLength = SCSICMD_READCAPACITY10_SIZEOF; cbw->CB[0] = SCSI_CMD_READCAPACITY10; - usbh_msc_cbw_dump(cbw); - /* Send the CBW */ - 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_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_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); - } - } - } - return nbytes < 0 ? (int)nbytes : 0; + return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_buf, g_msc_buf); } int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors) { - int nbytes; struct CBW *cbw; /* Construct the CBW */ @@ -257,26 +251,11 @@ int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, con SET_BE32(&cbw->CB[2], start_sector); SET_BE16(&cbw->CB[7], nsectors); - usbh_msc_cbw_dump(cbw); - /* Send the CBW */ - 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_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_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); - } - } - } - return nbytes < 0 ? (int)nbytes : 0; + return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_buf, (uint8_t *)buffer); } int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors) { - int nbytes; struct CBW *cbw; /* Construct the CBW */ @@ -292,21 +271,7 @@ int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, cons SET_BE32(&cbw->CB[2], start_sector); SET_BE16(&cbw->CB[7], nsectors); - usbh_msc_cbw_dump(cbw); - /* Send the CBW */ - 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_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_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); - } - } - } - return nbytes < 0 ? (int)nbytes : 0; + return usbh_bulk_cbw_csw_xfer(msc_class, cbw, (struct CSW *)g_msc_buf, (uint8_t *)buffer); } static int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf) @@ -345,16 +310,17 @@ static int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf) ret = usbh_msc_scsi_testunitready(msc_class); if (ret < 0) { - USB_LOG_ERR("Fail to scsi_testunitready\r\n"); - return ret; + ret = usbh_msc_scsi_requestsense(msc_class); + if (ret < 0) { + USB_LOG_ERR("Fail to scsi_testunitready\r\n"); + return ret; + } } - ret = usbh_msc_scsi_inquiry(msc_class); if (ret < 0) { USB_LOG_ERR("Fail to scsi_inquiry\r\n"); return ret; } - ret = usbh_msc_scsi_readcapacity10(msc_class); if (ret < 0) { USB_LOG_ERR("Fail to scsi_readcapacity10\r\n"); @@ -407,12 +373,10 @@ static int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf) __WEAK void usbh_msc_run(struct usbh_msc *msc_class) { - } __WEAK void usbh_msc_stop(struct usbh_msc *msc_class) { - } const struct usbh_class_driver msc_class_driver = { diff --git a/components/usb/cherryusb/class/video/usbd_video.c b/components/usb/cherryusb/class/video/usbd_video.c index be27bba5..62982d20 100644 --- a/components/usb/cherryusb/class/video/usbd_video.c +++ b/components/usb/cherryusb/class/video/usbd_video.c @@ -576,10 +576,10 @@ static int usbd_video_stream_request_handler(struct usb_setup_packet *setup, uin case VIDEO_VS_PROBE_CONTROL: switch (setup->bRequest) { case VIDEO_REQUEST_SET_CUR: - //memcpy((uint8_t *)usbd_video_cfg.probe, *data, setup->wLength); + //memcpy((uint8_t *)&usbd_video_cfg.probe, *data, setup->wLength); break; case VIDEO_REQUEST_GET_CUR: - *data = (uint8_t *)&usbd_video_cfg.probe; + memcpy(*data, (uint8_t *)&usbd_video_cfg.probe, setup->wLength); *len = sizeof(struct video_probe_and_commit_controls); break; @@ -587,7 +587,7 @@ static int usbd_video_stream_request_handler(struct usb_setup_packet *setup, uin case VIDEO_REQUEST_GET_MAX: case VIDEO_REQUEST_GET_RES: case VIDEO_REQUEST_GET_DEF: - *data = (uint8_t *)&usbd_video_cfg.probe; + memcpy(*data, (uint8_t *)&usbd_video_cfg.probe, setup->wLength); *len = sizeof(struct video_probe_and_commit_controls); break; case VIDEO_REQUEST_GET_LEN: @@ -608,17 +608,17 @@ static int usbd_video_stream_request_handler(struct usb_setup_packet *setup, uin case VIDEO_VS_COMMIT_CONTROL: switch (setup->bRequest) { case VIDEO_REQUEST_SET_CUR: - //memcpy((uint8_t *)usbd_video_cfg.commit, *data, setup->wLength); + //memcpy((uint8_t *)&usbd_video_cfg.commit, *data, setup->wLength); break; case VIDEO_REQUEST_GET_CUR: - *data = (uint8_t *)&usbd_video_cfg.commit; + memcpy(*data, (uint8_t *)&usbd_video_cfg.commit, setup->wLength); *len = sizeof(struct video_probe_and_commit_controls); break; case VIDEO_REQUEST_GET_MIN: case VIDEO_REQUEST_GET_MAX: case VIDEO_REQUEST_GET_RES: case VIDEO_REQUEST_GET_DEF: - *data = (uint8_t *)&usbd_video_cfg.commit; + memcpy(*data, (uint8_t *)&usbd_video_cfg.commit, setup->wLength); *len = sizeof(struct video_probe_and_commit_controls); break; @@ -640,7 +640,7 @@ static int usbd_video_stream_request_handler(struct usb_setup_packet *setup, uin case VIDEO_VS_STREAM_ERROR_CODE_CONTROL: switch (setup->bRequest) { case VIDEO_REQUEST_GET_CUR: - *data = &usbd_video_cfg.error_code; + (*data)[0] = usbd_video_cfg.error_code; *len = 1; break; case VIDEO_REQUEST_GET_INFO: diff --git a/components/usb/cherryusb/common/usb_hc.h b/components/usb/cherryusb/common/usb_hc.h index 880149fe..dfe20703 100644 --- a/components/usb/cherryusb/common/usb_hc.h +++ b/components/usb/cherryusb/common/usb_hc.h @@ -85,15 +85,15 @@ uint16_t usbh_get_frame_number(void); int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf); /** - * @brief reconfig control endpoint pipe. + * @brief reconfig endpoint pipe. * * @param pipe A memory allocated for pipe. * @param dev_addr device address. - * @param ep_mps control endpoint max packet size. - * @param speed port speed + * @param ep_mps endpoint max packet size. + * @param mult endpoint additional transcation * @return On success will return 0, and others indicate fail. */ -int usbh_ep0_pipe_reconfigure(usbh_pipe_t pipe, uint8_t dev_addr, uint8_t ep_mps, uint8_t speed); +int usbh_ep_pipe_reconfigure(usbh_pipe_t pipe, uint8_t dev_addr, uint8_t ep_mps, uint8_t mult); /** * @brief Allocate pipe for endpoint diff --git a/components/usb/cherryusb/core/usbd_core.c b/components/usb/cherryusb/core/usbd_core.c index 1d4e57b3..b73a5caf 100644 --- a/components/usb/cherryusb/core/usbd_core.c +++ b/components/usb/cherryusb/core/usbd_core.c @@ -6,7 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "usbd_core.h" -#ifdef CONFIG_USBDEV_TX_RX_THREAD +#if defined(CONFIG_USBDEV_TX_THREAD) || defined(CONFIG_USBDEV_RX_THREAD) #include "usb_osal.h" #endif @@ -44,7 +44,7 @@ USB_NOCACHE_RAM_SECTION struct usbd_core_cfg_priv { /** Zero length packet flag of control transfer */ bool zlp_flag; /** Pointer to registered descriptors */ -#if defined(CHERRYUSB_VERSION) && (CHERRYUSB_VERSION > 0x000700) +#ifdef CONFIG_USBDEV_ADVANCE_DESC struct usb_descriptor *descriptors; #else const uint8_t *descriptors; @@ -72,9 +72,13 @@ static struct usb_bos_descriptor *bos_desc; struct usbd_tx_rx_msg tx_msg[USB_EP_IN_NUM]; struct usbd_tx_rx_msg rx_msg[USB_EP_OUT_NUM]; -#ifdef CONFIG_USBDEV_TX_RX_THREAD -usb_osal_mq_t usbd_tx_rx_mq; -usb_osal_thread_t usbd_tx_rx_thread; +#if defined(CONFIG_USBDEV_TX_THREAD) +usb_osal_mq_t usbd_tx_mq; +usb_osal_thread_t usbd_tx_thread; +#endif +#if defined(CONFIG_USBDEV_RX_THREAD) +usb_osal_mq_t usbd_rx_mq; +usb_osal_thread_t usbd_rx_thread; #endif static void usbd_class_event_notify_handler(uint8_t event, void *arg); @@ -113,7 +117,7 @@ static bool usbd_set_endpoint(const struct usb_endpoint_descriptor *ep_desc) ep_cfg.ep_mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK; - USB_LOG_INFO("Open endpoint:0x%x type:%u mps:%u\r\n", + USB_LOG_INFO("Open ep:0x%02x type:%u mps:%u\r\n", ep_cfg.ep_addr, ep_cfg.ep_type, ep_cfg.ep_mps); return usbd_ep_open(&ep_cfg) == 0 ? true : false; @@ -136,7 +140,7 @@ static bool usbd_reset_endpoint(const struct usb_endpoint_descriptor *ep_desc) ep_cfg.ep_mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK; ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK; - USB_LOG_INFO("Close endpoint:0x%x type:%u\r\n", + USB_LOG_INFO("Close ep:0x%02x type:%u\r\n", ep_cfg.ep_addr, ep_cfg.ep_type); return usbd_ep_close(ep_cfg.ep_addr) == 0 ? true : false; @@ -154,7 +158,7 @@ static bool usbd_reset_endpoint(const struct usb_endpoint_descriptor *ep_desc) * * @return true if the descriptor was found, false otherwise */ -#if defined(CHERRYUSB_VERSION) && (CHERRYUSB_VERSION > 0x000700) +#ifdef CONFIG_USBDEV_ADVANCE_DESC static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *len) { uint8_t type = 0U; @@ -173,18 +177,18 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l case USB_DESCRIPTOR_TYPE_CONFIGURATION: usbd_core_cfg.speed = usbd_get_port_speed(0); if (usbd_core_cfg.speed == USB_SPEED_HIGH) { - if (usbd_core_cfg.descriptors->hs_config_descriptor[index]) { - *data = (uint8_t *)usbd_core_cfg.descriptors->hs_config_descriptor[index]; - *len = (usbd_core_cfg.descriptors->hs_config_descriptor[index][CONF_DESC_wTotalLength] | - (usbd_core_cfg.descriptors->hs_config_descriptor[index][CONF_DESC_wTotalLength + 1] << 8)); + if (usbd_core_cfg.descriptors->hs_config_descriptor) { + *data = (uint8_t *)usbd_core_cfg.descriptors->hs_config_descriptor; + *len = (usbd_core_cfg.descriptors->hs_config_descriptor[CONF_DESC_wTotalLength] | + (usbd_core_cfg.descriptors->hs_config_descriptor[CONF_DESC_wTotalLength + 1] << 8)); } else { found = false; } } else { - if (usbd_core_cfg.descriptors->fs_config_descriptor[index]) { - *data = (uint8_t *)usbd_core_cfg.descriptors->fs_config_descriptor[index]; - *len = (usbd_core_cfg.descriptors->fs_config_descriptor[index][CONF_DESC_wTotalLength] | - (usbd_core_cfg.descriptors->fs_config_descriptor[index][CONF_DESC_wTotalLength + 1] << 8)); + if (usbd_core_cfg.descriptors->fs_config_descriptor) { + *data = (uint8_t *)usbd_core_cfg.descriptors->fs_config_descriptor; + *len = (usbd_core_cfg.descriptors->fs_config_descriptor[CONF_DESC_wTotalLength] | + (usbd_core_cfg.descriptors->fs_config_descriptor[CONF_DESC_wTotalLength + 1] << 8)); } else { found = false; } @@ -332,9 +336,6 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l } if (found) { - /* found descriptor, save descriptor premiere address */ - *data = p; - if ((type == USB_DESCRIPTOR_TYPE_CONFIGURATION) || ((type == USB_DESCRIPTOR_TYPE_OTHER_SPEED))) { /* configuration or other speed descriptor is an * exception, length is at offset 2 and 3 @@ -345,6 +346,7 @@ static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *l /* normally length is at offset 0 */ *len = p[DESC_bLength]; } + memcpy(*data, p, *len); } else { /* nothing found */ USB_LOG_ERR("descriptor not found!\r\n", type, index); @@ -372,11 +374,11 @@ static bool usbd_set_configuration(uint8_t config_index, uint8_t alt_setting) uint8_t cur_config = 0xFF; bool found = false; uint8_t *p; -#if defined(CHERRYUSB_VERSION) && (CHERRYUSB_VERSION > 0x000700) +#ifdef CONFIG_USBDEV_ADVANCE_DESC if (usbd_core_cfg.speed == USB_SPEED_HIGH) { - p = (uint8_t *)usbd_core_cfg.descriptors->hs_config_descriptor[0]; + p = (uint8_t *)usbd_core_cfg.descriptors->hs_config_descriptor; } else { - p = (uint8_t *)usbd_core_cfg.descriptors->fs_config_descriptor[0]; + p = (uint8_t *)usbd_core_cfg.descriptors->fs_config_descriptor; } #else p = (uint8_t *)usbd_core_cfg.descriptors; @@ -436,11 +438,11 @@ static bool usbd_set_interface(uint8_t iface, uint8_t alt_setting) uint8_t cur_iface = 0xFF; bool ret = false; uint8_t *p; -#if defined(CHERRYUSB_VERSION) && (CHERRYUSB_VERSION > 0x000700) +#ifdef CONFIG_USBDEV_ADVANCE_DESC if (usbd_core_cfg.speed == USB_SPEED_HIGH) { - p = (uint8_t *)usbd_core_cfg.descriptors->hs_config_descriptor[0]; + p = (uint8_t *)usbd_core_cfg.descriptors->hs_config_descriptor; } else { - p = (uint8_t *)usbd_core_cfg.descriptors->fs_config_descriptor[0]; + p = (uint8_t *)usbd_core_cfg.descriptors->fs_config_descriptor; } #else p = (uint8_t *)usbd_core_cfg.descriptors; @@ -787,7 +789,7 @@ static int usbd_class_request_handler(struct usb_setup_packet *setup, uint8_t ** */ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) { -#if defined(CHERRYUSB_VERSION) && (CHERRYUSB_VERSION > 0x000700) +#ifdef CONFIG_USBDEV_ADVANCE_DESC if (usbd_core_cfg.descriptors->msosv1_descriptor) { if (setup->bRequest == usbd_core_cfg.descriptors->msosv1_descriptor->vendor_code) { switch (setup->wIndex) { @@ -819,6 +821,19 @@ static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t * return -1; } } + } else if (usbd_core_cfg.descriptors->webusb_url_descriptor) { + if (setup->bRequest == usbd_core_cfg.descriptors->webusb_url_descriptor->vendor_code) { + switch (setup->wIndex) { + case WINUSB_REQUEST_GET_DESCRIPTOR_SET: + USB_LOG_INFO("GET Webusb url Descriptor\r\n"); + *data = (uint8_t *)usbd_core_cfg.descriptors->webusb_url_descriptor->string; + *len = usbd_core_cfg.descriptors->webusb_url_descriptor->string_len; + return 0; + default: + USB_LOG_ERR("unknown vendor code\r\n"); + return -1; + } + } } #else if (msosv1_desc) { @@ -884,6 +899,12 @@ static bool usbd_setup_request_handler(struct usb_setup_packet *setup, uint8_t * switch (setup->bmRequestType & USB_REQUEST_TYPE_MASK) { case USB_REQUEST_STANDARD: if (usbd_standard_request_handler(setup, data, len) < 0) { +#ifndef CONFIG_USB_HS + if ((setup->bRequest == 0x06) && (setup->wValue == 0x0600)) { + USB_LOG_WRN("Ignore DQD in fs\r\n"); /* Device Qualifier Descriptor */ + return false; + } +#endif USB_LOG_ERR("standard request error\r\n"); usbd_print_setup(setup); return false; @@ -1008,22 +1029,11 @@ void usbd_event_ep0_setup_complete_handler(uint8_t *psetup) #endif /* Send smallest of requested and offered length */ usbd_core_cfg.ep0_data_buf_residue = MIN(usbd_core_cfg.ep0_data_buf_len, setup->wLength); - -#if defined(CHERRYUSB_VERSION) && (CHERRYUSB_VERSION > 0x000700) -#else - /* check if the data buf addr matches align size,if not, copy into align buf */ -#ifndef CONFIG_USBDEV_ALIGN_CHECK_DISABLE - if (((unsigned long)usbd_core_cfg.ep0_data_buf) & (CONFIG_USB_ALIGN_SIZE - 1)) { - if (usbd_core_cfg.ep0_data_buf_residue > CONFIG_USBDEV_REQUEST_BUFFER_LEN) { - USB_LOG_ERR("Request buffer too small\r\n"); - return; - } - /* copy data buf from misalignx addr to alignx addr */ - memcpy(usbd_core_cfg.req_data, usbd_core_cfg.ep0_data_buf, usbd_core_cfg.ep0_data_buf_residue); - usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data; + if (usbd_core_cfg.ep0_data_buf_residue > CONFIG_USBDEV_REQUEST_BUFFER_LEN) { + USB_LOG_ERR("Request buffer too small\r\n"); + return; } -#endif -#endif + /* Send data or status to host */ usbd_ep_start_write(USB_CONTROL_IN_EP0, usbd_core_cfg.ep0_data_buf, usbd_core_cfg.ep0_data_buf_residue); /* @@ -1101,36 +1111,36 @@ void usbd_event_ep0_out_complete_handler(uint8_t ep, uint32_t nbytes) void usbd_event_ep_in_complete_handler(uint8_t ep, uint32_t nbytes) { -#ifndef CONFIG_USBDEV_TX_RX_THREAD +#ifndef CONFIG_USBDEV_TX_THREAD if (tx_msg[ep & 0x7f].cb) { tx_msg[ep & 0x7f].cb(ep, nbytes); } #else tx_msg[ep & 0x7f].nbytes = nbytes; - usb_osal_mq_send(usbd_tx_rx_mq, (uint32_t)&tx_msg[ep & 0x7f]); + usb_osal_mq_send(usbd_tx_mq, (uint32_t)&tx_msg[ep & 0x7f]); #endif } void usbd_event_ep_out_complete_handler(uint8_t ep, uint32_t nbytes) { -#ifndef CONFIG_USBDEV_TX_RX_THREAD +#ifndef CONFIG_USBDEV_RX_THREAD if (rx_msg[ep & 0x7f].cb) { rx_msg[ep & 0x7f].cb(ep, nbytes); } #else rx_msg[ep & 0x7f].nbytes = nbytes; - usb_osal_mq_send(usbd_tx_rx_mq, (uint32_t)&rx_msg[ep & 0x7f]); + usb_osal_mq_send(usbd_rx_mq, (uint32_t)&rx_msg[ep & 0x7f]); #endif } -#ifdef CONFIG_USBDEV_TX_RX_THREAD -static void usbdev_tx_rx_thread(void *argument) +#ifdef CONFIG_USBDEV_TX_THREAD +static void usbdev_tx_thread(void *argument) { struct usbd_tx_rx_msg *msg; int ret; while (1) { - ret = usb_osal_mq_recv(usbd_tx_rx_mq, (uint32_t *)&msg, 0xffffffff); + ret = usb_osal_mq_recv(usbd_tx_mq, (uint32_t *)&msg, 0xffffffff); if (ret < 0) { continue; } @@ -1142,7 +1152,26 @@ static void usbdev_tx_rx_thread(void *argument) } #endif -#if defined(CHERRYUSB_VERSION) && (CHERRYUSB_VERSION > 0x000700) +#ifdef CONFIG_USBDEV_RX_THREAD +static void usbdev_rx_thread(void *argument) +{ + struct usbd_tx_rx_msg *msg; + int ret; + + while (1) { + ret = usb_osal_mq_recv(usbd_rx_mq, (uint32_t *)&msg, 0xffffffff); + if (ret < 0) { + continue; + } + + if (msg->cb) { + msg->cb(msg->ep, msg->nbytes); + } + } +} +#endif + +#ifdef CONFIG_USBDEV_ADVANCE_DESC void usbd_desc_register(struct usb_descriptor *desc) { usbd_core_cfg.descriptors = desc; @@ -1206,13 +1235,23 @@ bool usb_device_is_configured(void) int usbd_initialize(void) { -#ifdef CONFIG_USBDEV_TX_RX_THREAD - usbd_tx_rx_mq = usb_osal_mq_create(32); - if (usbd_tx_rx_mq == NULL) { +#ifdef CONFIG_USBDEV_TX_THREAD + usbd_tx_mq = usb_osal_mq_create(16); + if (usbd_tx_mq == NULL) { return -1; } - usbd_tx_rx_thread = usb_osal_thread_create("usbd_tx_rx", CONFIG_USBDEV_TX_RX_STACKSIZE, CONFIG_USBDEV_TX_RX_PRIO, usbdev_tx_rx_thread, NULL); - if (usbd_tx_rx_thread == NULL) { + usbd_tx_thread = usb_osal_thread_create("usbd_tx", CONFIG_USBDEV_TX_STACKSIZE, CONFIG_USBDEV_TX_PRIO, usbdev_tx_thread, NULL); + if (usbd_tx_thread == NULL) { + return -1; + } +#endif +#ifdef CONFIG_USBDEV_RX_THREAD + usbd_rx_mq = usb_osal_mq_create(16); + if (usbd_rx_mq == NULL) { + return -1; + } + usbd_rx_thread = usb_osal_thread_create("usbd_rx", CONFIG_USBDEV_RX_STACKSIZE, CONFIG_USBDEV_RX_PRIO, usbdev_rx_thread, NULL); + if (usbd_rx_thread == NULL) { return -1; } #endif @@ -1224,7 +1263,7 @@ int usbd_deinitialize(void) usbd_core_cfg.intf_offset = 0; usb_slist_init(&usbd_intf_head); usb_dc_deinit(); -#ifdef CONFIG_USBDEV_TX_RX_THREAD +#if defined(CONFIG_USBDEV_TX_THREAD) || defined(CONFIG_USBDEV_RX_THREAD) #endif return 0; } diff --git a/components/usb/cherryusb/core/usbh_core.c b/components/usb/cherryusb/core/usbh_core.c index 3aea18d7..93300030 100644 --- a/components/usb/cherryusb/core/usbh_core.c +++ b/components/usb/cherryusb/core/usbh_core.c @@ -438,7 +438,7 @@ int usbh_enumerate(struct usbh_hubport *hport) ep_mps = ((struct usb_device_descriptor *)ep0_request_buffer)->bMaxPacketSize0; /* Reconfigure EP0 with the correct maximum packet size */ - usbh_ep0_pipe_reconfigure(hport->ep0, 0, ep_mps, hport->speed); + usbh_ep_pipe_reconfigure(hport->ep0, 0, ep_mps, 0); #ifdef CONFIG_USBHOST_XHCI extern int usbh_get_xhci_devaddr(usbh_pipe_t * pipe); @@ -478,7 +478,7 @@ int usbh_enumerate(struct usbh_hubport *hport) hport->dev_addr = dev_addr; /* And reconfigure EP0 with the correct address */ - usbh_ep0_pipe_reconfigure(hport->ep0, dev_addr, ep_mps, hport->speed); + usbh_ep_pipe_reconfigure(hport->ep0, dev_addr, ep_mps, 0); /* Read the full device descriptor */ setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE; diff --git a/components/usb/cherryusb/port/ehci/usb_hc_ehci.c b/components/usb/cherryusb/port/ehci/usb_hc_ehci.c index 96f667ae..79e5e851 100644 --- a/components/usb/cherryusb/port/ehci/usb_hc_ehci.c +++ b/components/usb/cherryusb/port/ehci/usb_hc_ehci.c @@ -959,13 +959,13 @@ int usbh_roothub_control(struct usb_setup_packet *setup, uint8_t *buf) return 0; } -int usbh_ep0_pipe_reconfigure(usbh_pipe_t pipe, uint8_t dev_addr, uint8_t ep_mps, uint8_t speed) +int usbh_ep_pipe_reconfigure(usbh_pipe_t pipe, uint8_t dev_addr, uint8_t ep_mps, uint8_t mult) { struct ehci_pipe *ppipe = (struct ehci_pipe *)pipe; ppipe->dev_addr = dev_addr; ppipe->ep_mps = ep_mps; - ppipe->speed = speed; + ppipe->mult = mult; return 0; } diff --git a/components/usb/cherryusb/port/ehci/usb_hc_ehci.h b/components/usb/cherryusb/port/ehci/usb_hc_ehci.h index 243a7e63..dfe3575b 100644 --- a/components/usb/cherryusb/port/ehci/usb_hc_ehci.h +++ b/components/usb/cherryusb/port/ehci/usb_hc_ehci.h @@ -52,7 +52,7 @@ #define EHCI_USBCMD_IAAD (1 << 6) /* Bit 6: Interrupt on Async Advance Doorbell */ #define EHCI_USBCMD_LRESET (1 << 7) /* Bit 7: Light Host Controller Reset */ #define EHCI_USBCMD_ASYNC_PARKCNT_SHIFT (8) /* Bits 8-9: Asynchronous Schedule Park Mode Count */ -#define EHCI_USBCMD_ASYNC_PARKCNT_MASK (3 << EHCI_USBCMD_PARKCNT_SHIFT) +#define EHCI_USBCMD_ASYNC_PARKCNT_MASK (3 << EHCI_USBCMD_ASYNC_PARKCNT_SHIFT) #define EHCI_USBCMD_ASYNC_PARK (1 << 11) /* Bit 11: Asynchronous Schedule Park Mode Enable */ #define EHCI_USBCMD_ITHRE_SHIFT (16) /* Bits 16-23: Interrupt Threshold Control */ #define EHCI_USBCMD_ITHRE_MASK (0xff << EHCI_USBCMD_ITHRE_SHIFT) @@ -247,7 +247,7 @@ #define ITD_TSCL_STATUS_XACTERR (1 << 28) /* Bit 28: Transaction error */ #define ITD_TSCL_STATUS_BABBLE (1 << 29) /* Bit 29: Babble Detected */ #define ITD_TSCL_STATUS_DBERROR (1 << 30) /* Bit 30: Data Buffer Error */ -#define ITD_TSCL_STATUS_ACTIVE (1 << 31) /* Bit 28: Transaction error */ +#define ITD_TSCL_STATUS_ACTIVE (1 << 31) /* Bit 31: Active error */ /* iTD Buffer Page Pointer List. Paragraph 3.3.4 */ diff --git a/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/README.md b/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/README.md index d3cbfb23..79cbaba2 100644 --- a/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/README.md +++ b/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/README.md @@ -5,18 +5,11 @@ | CHIP | Remark | |:----------------:|:------:| -|BL702/BL704/BL706 | | |BL616/BL618 | | |BL808 | D0 has no irq | ## Compile -- BL702/BL704/BL706 - -``` -make CHIP=bl702 BOARD=bl702dk -``` - - BL616/BL618 ``` diff --git a/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/audio_v1_mic_speaker_multichan_template.c b/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/audio_v1_mic_speaker_multichan_template.c index 4c5b86d5..bbbba9f4 100644 --- a/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/audio_v1_mic_speaker_multichan_template.c +++ b/examples/peripherals/usbdev/usbd_audio_v1_mic_speaker_multichan/audio_v1_mic_speaker_multichan_template.c @@ -16,14 +16,19 @@ #define AUDIO_OUT_EP 0x02 /* AUDIO Class Config */ -#define AUDIO_FREQ 16000U +#define AUDIO_SPEAKER_FREQ 16000U +#define AUDIO_SPEAKER_FRAME_SIZE_BYTE 2u +#define AUDIO_SPEAKER_RESOLUTION_BIT 16u +#define AUDIO_MIC_FREQ 16000U +#define AUDIO_MIC_FRAME_SIZE_BYTE 2u +#define AUDIO_MIC_RESOLUTION_BIT 16u #define AUDIO_SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16)) /* AudioFreq * DataSize (2 bytes) * NumChannels (Stereo: 2) */ -#define AUDIO_OUT_PACKET ((uint32_t)((AUDIO_FREQ * 2 * 2) / 1000)) +#define AUDIO_OUT_PACKET ((uint32_t)((AUDIO_SPEAKER_FREQ * AUDIO_SPEAKER_FRAME_SIZE_BYTE * 2) / 1000)) /* 16bit(2 Bytes) 双声道(Mono:2) */ -#define AUDIO_IN_PACKET ((uint32_t)((AUDIO_FREQ * 2 * 2) / 1000)) +#define AUDIO_IN_PACKET ((uint32_t)((AUDIO_MIC_FREQ * AUDIO_MIC_FRAME_SIZE_BYTE * 2) / 1000)) #define USB_AUDIO_CONFIG_DESC_SIZ (unsigned long)(9 + \ AUDIO_AC_DESCRIPTOR_INIT_LEN(2) + \ @@ -49,13 +54,15 @@ const uint8_t audio_descriptor[] = { USB_CONFIG_DESCRIPTOR_INIT(USB_AUDIO_CONFIG_DESC_SIZ, 0x03, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), AUDIO_AC_DESCRIPTOR_INIT(0x00, 0x03, AUDIO_AC_SIZ, 0x00, 0x01, 0x02), AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x01, AUDIO_INTERM_MIC, 0x02, 0x0003), - AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00), + AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x02, 0x01, 0x01, 0x03, 0x00, 0x00), AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x03, AUDIO_TERMINAL_STREAMING, 0x02), AUDIO_AC_INPUT_TERMINAL_DESCRIPTOR_INIT(0x04, AUDIO_TERMINAL_STREAMING, 0x02, 0x0003), - AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00), + AUDIO_AC_FEATURE_UNIT_DESCRIPTOR_INIT(0x05, 0x04, 0x01, 0x03, 0x00, 0x00), AUDIO_AC_OUTPUT_TERMINAL_DESCRIPTOR_INIT(0x06, AUDIO_OUTTERM_SPEAKER, 0x05), - AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_OUT_EP, AUDIO_OUT_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_FREQ)), - AUDIO_AS_DESCRIPTOR_INIT(0x02, 0x03, 0x02, AUDIO_IN_EP, AUDIO_IN_PACKET, EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_FREQ)), + AUDIO_AS_DESCRIPTOR_INIT(0x01, 0x04, 0x02, AUDIO_SPEAKER_FRAME_SIZE_BYTE, AUDIO_SPEAKER_RESOLUTION_BIT, AUDIO_OUT_EP, AUDIO_OUT_PACKET,\ + EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_SPEAKER_FREQ)), + AUDIO_AS_DESCRIPTOR_INIT(0x02, 0x03, 0x02, AUDIO_MIC_FRAME_SIZE_BYTE, AUDIO_MIC_RESOLUTION_BIT, AUDIO_IN_EP, AUDIO_IN_PACKET,\ + EP_INTERVAL, AUDIO_SAMPLE_FREQ_3B(AUDIO_MIC_FREQ)), /////////////////////////////////////// /// string0 descriptor /////////////////////////////////////// @@ -130,6 +137,9 @@ const uint8_t audio_descriptor[] = { 0x00 }; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048]; +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t out_buffer[AUDIO_OUT_PACKET]; + volatile bool tx_flag = 0; volatile bool rx_flag = 0; @@ -137,6 +147,8 @@ void usbd_audio_open(uint8_t intf) { if (intf == 1) { rx_flag = 1; + /* setup first out ep read transfer */ + usbd_ep_start_read(AUDIO_OUT_EP, out_buffer, AUDIO_OUT_PACKET); printf("OPEN1\r\n"); } else { tx_flag = 1; @@ -160,15 +172,10 @@ void usbd_audio_close(uint8_t intf) #define AUDIO_OUT_EP_MPS 64 #endif -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048]; -USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t out_buffer[AUDIO_OUT_PACKET]; - volatile bool ep_tx_busy_flag = false; void usbd_configure_done_callback(void) { - /* setup first out ep read transfer */ - usbd_ep_start_read(AUDIO_OUT_EP, out_buffer, AUDIO_OUT_PACKET); } void usbd_audio_out_callback(uint8_t ep, uint32_t nbytes) @@ -216,11 +223,11 @@ void audio_test() { while (1) { if (tx_flag) { - memset(write_buffer, 'a', 2048); - ep_tx_busy_flag = true; - usbd_ep_start_write(AUDIO_IN_EP, write_buffer, 2048); - while (ep_tx_busy_flag) { - } + // memset(write_buffer, 'a', 2048); + // ep_tx_busy_flag = true; + // usbd_ep_start_write(AUDIO_IN_EP, write_buffer, 2048); + // while (ep_tx_busy_flag) { + // } } } } diff --git a/examples/peripherals/usbdev/usbd_static_video/README.md b/examples/peripherals/usbdev/usbd_static_video/README.md index bd0f4ef3..55d558dc 100644 --- a/examples/peripherals/usbdev/usbd_static_video/README.md +++ b/examples/peripherals/usbdev/usbd_static_video/README.md @@ -5,18 +5,11 @@ | CHIP | Remark | |:----------------:|:------:| -|BL702/BL704/BL706 | | |BL616/BL618 | | |BL808 | D0 has no irq | ## Compile -- BL702/BL704/BL706 - -``` -make CHIP=bl702 BOARD=bl702dk -``` - - BL616/BL618 ```