summaryrefslogtreecommitdiff
path: root/components/drivers/usb
diff options
context:
space:
mode:
authorarmink <[email protected]>2019-12-17 20:11:46 +0800
committerarmink <[email protected]>2019-12-17 20:11:46 +0800
commit4875ee8a3ab95c4fbf1e09941d3f44d99a9a632b (patch)
tree15c596c04a842bac747bbcf05a703b8da76f8eac /components/drivers/usb
parentfa46d6ac861f26b472d62abdf1278c445431ce4c (diff)
Sync from master (75a3f3dd3a930eab1dc58fa0258a9d1605a0ac11)
Diffstat (limited to 'components/drivers/usb')
-rw-r--r--components/drivers/usb/usbdevice/SConscript6
-rw-r--r--components/drivers/usb/usbdevice/class/audio.h676
-rw-r--r--components/drivers/usb/usbdevice/class/audio_mic.c577
-rw-r--r--components/drivers/usb/usbdevice/class/audio_speaker.c578
-rw-r--r--components/drivers/usb/usbdevice/class/ecm.c50
-rw-r--r--components/drivers/usb/usbdevice/class/hid.c2
-rw-r--r--components/drivers/usb/usbdevice/class/rndis.c236
-rw-r--r--components/drivers/usb/usbdevice/class/rndis.h9
-rw-r--r--components/drivers/usb/usbdevice/core/core.c57
9 files changed, 2076 insertions, 115 deletions
diff --git a/components/drivers/usb/usbdevice/SConscript b/components/drivers/usb/usbdevice/SConscript
index be0b0c7b50..010b9837a4 100644
--- a/components/drivers/usb/usbdevice/SConscript
+++ b/components/drivers/usb/usbdevice/SConscript
@@ -24,6 +24,12 @@ if GetDepend('RT_USB_DEVICE_RNDIS'):
if GetDepend('RT_USB_DEVICE_WINUSB'):
src += Glob('class/winusb.c')
+
+if GetDepend('RT_USB_DEVICE_AUDIO_MIC'):
+ src += Glob('class/audio_mic.c')
+
+if GetDepend('RT_USB_DEVICE_AUDIO_SPEAKER'):
+ src += Glob('class/audio_speaker.c')
CPPPATH = [cwd]
diff --git a/components/drivers/usb/usbdevice/class/audio.h b/components/drivers/usb/usbdevice/class/audio.h
new file mode 100644
index 0000000000..4bb8994b47
--- /dev/null
+++ b/components/drivers/usb/usbdevice/class/audio.h
@@ -0,0 +1,676 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * <linux/usb/audio.h> -- USB Audio definitions.
+ *
+ * Copyright (C) 2006 Thumtronics Pty Ltd.
+ * Developed for Thumtronics by Grey Innovation
+ * Ben Williamson <[email protected]>
+ *
+ * This software is distributed under the terms of the GNU General Public
+ * License ("GPL") version 2, as published by the Free Software Foundation.
+ *
+ * This file holds USB constants and structures defined
+ * by the USB Device Class Definition for Audio Devices.
+ * Comments below reference relevant sections of that document:
+ *
+ * http://www.usb.org/developers/devclass_docs/audio10.pdf
+ *
+ * Types and defines in this file are either specific to version 1.0 of
+ * this standard or common for newer versions.
+ */
+
+#ifndef _UAPI__LINUX_USB_AUDIO_H
+#define _UAPI__LINUX_USB_AUDIO_H
+
+//#include <linux/types.h>
+#include <rtthread.h>
+
+typedef int8_t __s8;
+typedef uint8_t __u8;
+typedef int16_t __s16;
+typedef uint16_t __u16;
+typedef int32_t __s32;
+typedef uint32_t __u32;
+typedef int64_t __s64;
+typedef uint64_t __u64;
+
+#define UAC_USE_FEATURE_UNIT 0 /* Feature unit does not support */
+
+/*
+ * Below are truly Linux-specific types that should never collide with
+ * any application/library that wants linux/types.h.
+ */
+
+#ifdef __CHECKER__
+#define __bitwise__ __attribute__((bitwise))
+#else
+#define __bitwise__
+#endif
+#ifdef __CHECK_ENDIAN__
+#define __bitwise __bitwise__
+#else
+#define __bitwise
+#endif
+
+typedef __u16 __bitwise __le16;
+typedef __u16 __bitwise __be16;
+typedef __u32 __bitwise __le32;
+typedef __u32 __bitwise __be32;
+typedef __u64 __bitwise __le64;
+typedef __u64 __bitwise __be64;
+
+typedef __u16 __bitwise __sum16;
+typedef __u32 __bitwise __wsum;
+
+/* bInterfaceProtocol values to denote the version of the standard used */
+#define UAC_VERSION_1 0x00
+#define UAC_VERSION_2 0x20
+#define UAC_VERSION_3 0x30
+
+/* A.2 Audio Interface Subclass Codes */
+#define USB_SUBCLASS_AUDIOCONTROL 0x01
+#define USB_SUBCLASS_AUDIOSTREAMING 0x02
+#define USB_SUBCLASS_MIDISTREAMING 0x03
+
+/* A.5 Audio Class-Specific AC Interface Descriptor Subtypes */
+#define UAC_HEADER 0x01
+#define UAC_INPUT_TERMINAL 0x02
+#define UAC_OUTPUT_TERMINAL 0x03
+#define UAC_MIXER_UNIT 0x04
+#define UAC_SELECTOR_UNIT 0x05
+#define UAC_FEATURE_UNIT 0x06
+#define UAC1_PROCESSING_UNIT 0x07
+#define UAC1_EXTENSION_UNIT 0x08
+
+/* A.6 Audio Class-Specific AS Interface Descriptor Subtypes */
+#define UAC_AS_GENERAL 0x01
+#define UAC_FORMAT_TYPE 0x02
+#define UAC_FORMAT_SPECIFIC 0x03
+
+/* A.7 Processing Unit Process Types */
+#define UAC_PROCESS_UNDEFINED 0x00
+#define UAC_PROCESS_UP_DOWNMIX 0x01
+#define UAC_PROCESS_DOLBY_PROLOGIC 0x02
+#define UAC_PROCESS_STEREO_EXTENDER 0x03
+#define UAC_PROCESS_REVERB 0x04
+#define UAC_PROCESS_CHORUS 0x05
+#define UAC_PROCESS_DYN_RANGE_COMP 0x06
+
+/* A.8 Audio Class-Specific Endpoint Descriptor Subtypes */
+#define UAC_EP_GENERAL 0x01
+
+/* A.9 Audio Class-Specific Request Codes */
+#define UAC_SET_ 0x00
+#define UAC_GET_ 0x80
+
+#define UAC__CUR 0x1
+#define UAC__MIN 0x2
+#define UAC__MAX 0x3
+#define UAC__RES 0x4
+#define UAC__MEM 0x5
+
+#define UAC_SET_CUR (UAC_SET_ | UAC__CUR)
+#define UAC_GET_CUR (UAC_GET_ | UAC__CUR)
+#define UAC_SET_MIN (UAC_SET_ | UAC__MIN)
+#define UAC_GET_MIN (UAC_GET_ | UAC__MIN)
+#define UAC_SET_MAX (UAC_SET_ | UAC__MAX)
+#define UAC_GET_MAX (UAC_GET_ | UAC__MAX)
+#define UAC_SET_RES (UAC_SET_ | UAC__RES)
+#define UAC_GET_RES (UAC_GET_ | UAC__RES)
+#define UAC_SET_MEM (UAC_SET_ | UAC__MEM)
+#define UAC_GET_MEM (UAC_GET_ | UAC__MEM)
+
+#define UAC_GET_STAT 0xff
+
+/* A.10 Control Selector Codes */
+
+/* A.10.1 Terminal Control Selectors */
+#define UAC_TERM_COPY_PROTECT 0x01
+
+/* A.10.2 Feature Unit Control Selectors */
+#define UAC_FU_MUTE 0x01
+#define UAC_FU_VOLUME 0x02
+#define UAC_FU_BASS 0x03
+#define UAC_FU_MID 0x04
+#define UAC_FU_TREBLE 0x05
+#define UAC_FU_GRAPHIC_EQUALIZER 0x06
+#define UAC_FU_AUTOMATIC_GAIN 0x07
+#define UAC_FU_DELAY 0x08
+#define UAC_FU_BASS_BOOST 0x09
+#define UAC_FU_LOUDNESS 0x0a
+
+#define UAC_CONTROL_BIT(CS) (1 << ((CS) - 1))
+
+/* A.10.3.1 Up/Down-mix Processing Unit Controls Selectors */
+#define UAC_UD_ENABLE 0x01
+#define UAC_UD_MODE_SELECT 0x02
+
+/* A.10.3.2 Dolby Prologic (tm) Processing Unit Controls Selectors */
+#define UAC_DP_ENABLE 0x01
+#define UAC_DP_MODE_SELECT 0x02
+
+/* A.10.3.3 3D Stereo Extender Processing Unit Control Selectors */
+#define UAC_3D_ENABLE 0x01
+#define UAC_3D_SPACE 0x02
+
+/* A.10.3.4 Reverberation Processing Unit Control Selectors */
+#define UAC_REVERB_ENABLE 0x01
+#define UAC_REVERB_LEVEL 0x02
+#define UAC_REVERB_TIME 0x03
+#define UAC_REVERB_FEEDBACK 0x04
+
+/* A.10.3.5 Chorus Processing Unit Control Selectors */
+#define UAC_CHORUS_ENABLE 0x01
+#define UAC_CHORUS_LEVEL 0x02
+#define UAC_CHORUS_RATE 0x03
+#define UAC_CHORUS_DEPTH 0x04
+
+/* A.10.3.6 Dynamic Range Compressor Unit Control Selectors */
+#define UAC_DCR_ENABLE 0x01
+#define UAC_DCR_RATE 0x02
+#define UAC_DCR_MAXAMPL 0x03
+#define UAC_DCR_THRESHOLD 0x04
+#define UAC_DCR_ATTACK_TIME 0x05
+#define UAC_DCR_RELEASE_TIME 0x06
+
+/* A.10.4 Extension Unit Control Selectors */
+#define UAC_XU_ENABLE 0x01
+
+/* MIDI - A.1 MS Class-Specific Interface Descriptor Subtypes */
+#define UAC_MS_HEADER 0x01
+#define UAC_MIDI_IN_JACK 0x02
+#define UAC_MIDI_OUT_JACK 0x03
+
+/* MIDI - A.1 MS Class-Specific Endpoint Descriptor Subtypes */
+#define UAC_MS_GENERAL 0x01
+
+/* Terminals - 2.1 USB Terminal Types */
+#define UAC_TERMINAL_UNDEFINED 0x100
+#define UAC_TERMINAL_STREAMING 0x101
+#define UAC_TERMINAL_VENDOR_SPEC 0x1FF
+
+/* Terminal Control Selectors */
+/* 4.3.2 Class-Specific AC Interface Descriptor */
+struct uac1_ac_header_descriptor {
+ __u8 bLength; /* 8 + n */
+ __u8 bDescriptorType; /* USB_DT_CS_INTERFACE */
+ __u8 bDescriptorSubtype; /* UAC_MS_HEADER */
+ __le16 bcdADC; /* 0x0100 */
+ __le16 wTotalLength; /* includes Unit and Terminal desc. */
+ __u8 bInCollection; /* n */
+ __u8 baInterfaceNr[]; /* [n] */
+} __attribute__ ((packed));
+
+#define UAC_DT_AC_HEADER_SIZE(n) (8 + (n))
+
+/* As above, but more useful for defining your own descriptors: */
+#define DECLARE_UAC_AC_HEADER_DESCRIPTOR(n) \
+struct uac1_ac_header_descriptor_##n { \
+ __u8 bLength; \
+ __u8 bDescriptorType; \
+ __u8 bDescriptorSubtype; \
+ __le16 bcdADC; \
+ __le16 wTotalLength; \
+ __u8 bInCollection; \
+ __u8 baInterfaceNr[n]; \
+} __attribute__ ((packed))
+
+/* 4.3.2.1 Input Terminal Descriptor */
+struct uac_input_terminal_descriptor {
+ __u8 bLength; /* in bytes: 12 */
+ __u8 bDescriptorType; /* CS_INTERFACE descriptor type */
+ __u8 bDescriptorSubtype; /* INPUT_TERMINAL descriptor subtype */
+ __u8 bTerminalID; /* Constant uniquely terminal ID */
+ __le16 wTerminalType; /* USB Audio Terminal Types */
+ __u8 bAssocTerminal; /* ID of the Output Terminal associated */
+ __u8 bNrChannels; /* Number of logical output channels */
+ __le16 wChannelConfig;
+ __u8 iChannelNames;
+ __u8 iTerminal;
+} __attribute__ ((packed));
+
+#define UAC_DT_INPUT_TERMINAL_SIZE 12
+
+/* Terminals - 2.2 Input Terminal Types */
+#define UAC_INPUT_TERMINAL_UNDEFINED 0x200
+#define UAC_INPUT_TERMINAL_MICROPHONE 0x201
+#define UAC_INPUT_TERMINAL_DESKTOP_MICROPHONE 0x202
+#define UAC_INPUT_TERMINAL_PERSONAL_MICROPHONE 0x203
+#define UAC_INPUT_TERMINAL_OMNI_DIR_MICROPHONE 0x204
+#define UAC_INPUT_TERMINAL_MICROPHONE_ARRAY 0x205
+#define UAC_INPUT_TERMINAL_PROC_MICROPHONE_ARRAY 0x206
+
+/* Terminals - control selectors */
+
+#define UAC_TERMINAL_CS_COPY_PROTECT_CONTROL 0x01
+
+/* 4.3.2.2 Output Terminal Descriptor */
+struct uac1_output_terminal_descriptor {
+ __u8 bLength; /* in bytes: 9 */
+ __u8 bDescriptorType; /* CS_INTERFACE descriptor type */
+ __u8 bDescriptorSubtype; /* OUTPUT_TERMINAL descriptor subtype */
+ __u8 bTerminalID; /* Constant uniquely terminal ID */
+ __le16 wTerminalType; /* USB Audio Terminal Types */
+ __u8 bAssocTerminal; /* ID of the Input Terminal associated */
+ __u8 bSourceID; /* ID of the connected Unit or Terminal*/
+ __u8 iTerminal;
+} __attribute__ ((packed));
+
+#define UAC_DT_OUTPUT_TERMINAL_SIZE 9
+
+/* Terminals - 2.3 Output Terminal Types */
+#define UAC_OUTPUT_TERMINAL_UNDEFINED 0x300
+#define UAC_OUTPUT_TERMINAL_SPEAKER 0x301
+#define UAC_OUTPUT_TERMINAL_HEADPHONES 0x302
+#define UAC_OUTPUT_TERMINAL_HEAD_MOUNTED_DISPLAY_AUDIO 0x303
+#define UAC_OUTPUT_TERMINAL_DESKTOP_SPEAKER 0x304
+#define UAC_OUTPUT_TERMINAL_ROOM_SPEAKER 0x305
+#define UAC_OUTPUT_TERMINAL_COMMUNICATION_SPEAKER 0x306
+#define UAC_OUTPUT_TERMINAL_LOW_FREQ_EFFECTS_SPEAKER 0x307
+
+/* Terminals - 2.4 Bi-directional Terminal Types */
+#define UAC_BIDIR_TERMINAL_UNDEFINED 0x400
+#define UAC_BIDIR_TERMINAL_HANDSET 0x401
+#define UAC_BIDIR_TERMINAL_HEADSET 0x402
+#define UAC_BIDIR_TERMINAL_SPEAKER_PHONE 0x403
+#define UAC_BIDIR_TERMINAL_ECHO_SUPPRESSING 0x404
+#define UAC_BIDIR_TERMINAL_ECHO_CANCELING 0x405
+
+/* Set bControlSize = 2 as default setting */
+#define UAC_DT_FEATURE_UNIT_SIZE(ch) (7 + ((ch) + 1) * 2)
+
+/* As above, but more useful for defining your own descriptors: */
+#define DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(ch) \
+struct uac_feature_unit_descriptor_##ch { \
+ __u8 bLength; \
+ __u8 bDescriptorType; \
+ __u8 bDescriptorSubtype; \
+ __u8 bUnitID; \
+ __u8 bSourceID; \
+ __u8 bControlSize; \
+ __le16 bmaControls[ch + 1]; \
+ __u8 iFeature; \
+} __attribute__ ((packed))
+
+/* 4.3.2.3 Mixer Unit Descriptor */
+struct uac_mixer_unit_descriptor {
+ __u8 bLength;
+ __u8 bDescriptorType;
+ __u8 bDescriptorSubtype;
+ __u8 bUnitID;
+ __u8 bNrInPins;
+ __u8 baSourceID[];
+} __attribute__ ((packed));
+
+static inline __u8 uac_mixer_unit_bNrChannels(struct uac_mixer_unit_descriptor *desc)
+{
+ return desc->baSourceID[desc->bNrInPins];
+}
+
+static inline __u32 uac_mixer_unit_wChannelConfig(struct uac_mixer_unit_descriptor *desc,
+ int protocol)
+{
+ if (protocol == UAC_VERSION_1)
+ return (desc->baSourceID[desc->bNrInPins + 2] << 8) |
+ desc->baSourceID[desc->bNrInPins + 1];
+ else
+ return (desc->baSourceID[desc->bNrInPins + 4] << 24) |
+ (desc->baSourceID[desc->bNrInPins + 3] << 16) |
+ (desc->baSourceID[desc->bNrInPins + 2] << 8) |
+ (desc->baSourceID[desc->bNrInPins + 1]);
+}
+
+static inline __u8 uac_mixer_unit_iChannelNames(struct uac_mixer_unit_descriptor *desc,
+ int protocol)
+{
+ return (protocol == UAC_VERSION_1) ?
+ desc->baSourceID[desc->bNrInPins + 3] :
+ desc->baSourceID[desc->bNrInPins + 5];
+}
+
+static inline __u8 *uac_mixer_unit_bmControls(struct uac_mixer_unit_descriptor *desc,
+ int protocol)
+{
+ switch (protocol) {
+ case UAC_VERSION_1:
+ return &desc->baSourceID[desc->bNrInPins + 4];
+ case UAC_VERSION_2:
+ return &desc->baSourceID[desc->bNrInPins + 6];
+ case UAC_VERSION_3:
+ return &desc->baSourceID[desc->bNrInPins + 2];
+ default:
+ return NULL;
+ }
+}
+
+static inline __u16 uac3_mixer_unit_wClusterDescrID(struct uac_mixer_unit_descriptor *desc)
+{
+ return (desc->baSourceID[desc->bNrInPins + 1] << 8) |
+ desc->baSourceID[desc->bNrInPins];
+}
+
+static inline __u8 uac_mixer_unit_iMixer(struct uac_mixer_unit_descriptor *desc)
+{
+ __u8 *raw = (__u8 *) desc;
+ return raw[desc->bLength - 1];
+}
+
+/* 4.3.2.4 Selector Unit Descriptor */
+struct uac_selector_unit_descriptor {
+ __u8 bLength;
+ __u8 bDescriptorType;
+ __u8 bDescriptorSubtype;
+ __u8 bUintID;
+ __u8 bNrInPins;
+ __u8 baSourceID[];
+} __attribute__ ((packed));
+
+static inline __u8 uac_selector_unit_iSelector(struct uac_selector_unit_descriptor *desc)
+{
+ __u8 *raw = (__u8 *) desc;
+ return raw[desc->bLength - 1];
+}
+#if UAC_USE_FEATURE_UNIT
+/* 4.3.2.5 Feature Unit Descriptor */
+struct uac_feature_unit_descriptor {
+ __u8 bLength;
+ __u8 bDescriptorType;
+ __u8 bDescriptorSubtype;
+ __u8 bUnitID;
+ __u8 bSourceID;
+ __u8 bControlSize;
+ __u8 bmaControls[0]; /* variable length */
+} __attribute__((packed));
+
+static inline __u8 uac_feature_unit_iFeature(struct uac_feature_unit_descriptor *desc)
+{
+ __u8 *raw = (__u8 *) desc;
+ return raw[desc->bLength - 1];
+}
+#endif
+/* 4.3.2.6 Processing Unit Descriptors */
+struct uac_processing_unit_descriptor {
+ __u8 bLength;
+ __u8 bDescriptorType;
+ __u8 bDescriptorSubtype;
+ __u8 bUnitID;
+ __le16 wProcessType;
+ __u8 bNrInPins;
+ __u8 baSourceID[];
+} __attribute__ ((packed));
+
+static inline __u8 uac_processing_unit_bNrChannels(struct uac_processing_unit_descriptor *desc)
+{
+ return desc->baSourceID[desc->bNrInPins];
+}
+
+static inline __u32 uac_processing_unit_wChannelConfig(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ if (protocol == UAC_VERSION_1)
+ return (desc->baSourceID[desc->bNrInPins + 2] << 8) |
+ desc->baSourceID[desc->bNrInPins + 1];
+ else
+ return (desc->baSourceID[desc->bNrInPins + 4] << 24) |
+ (desc->baSourceID[desc->bNrInPins + 3] << 16) |
+ (desc->baSourceID[desc->bNrInPins + 2] << 8) |
+ (desc->baSourceID[desc->bNrInPins + 1]);
+}
+
+static inline __u8 uac_processing_unit_iChannelNames(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ return (protocol == UAC_VERSION_1) ?
+ desc->baSourceID[desc->bNrInPins + 3] :
+ desc->baSourceID[desc->bNrInPins + 5];
+}
+
+static inline __u8 uac_processing_unit_bControlSize(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ switch (protocol) {
+ case UAC_VERSION_1:
+ return desc->baSourceID[desc->bNrInPins + 4];
+ case UAC_VERSION_2:
+ return 2; /* in UAC2, this value is constant */
+ case UAC_VERSION_3:
+ return 4; /* in UAC3, this value is constant */
+ default:
+ return 1;
+ }
+}
+
+static inline __u8 *uac_processing_unit_bmControls(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ switch (protocol) {
+ case UAC_VERSION_1:
+ return &desc->baSourceID[desc->bNrInPins + 5];
+ case UAC_VERSION_2:
+ return &desc->baSourceID[desc->bNrInPins + 6];
+ case UAC_VERSION_3:
+ return &desc->baSourceID[desc->bNrInPins + 2];
+ default:
+ return NULL;
+ }
+}
+
+static inline __u8 uac_processing_unit_iProcessing(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ __u8 control_size = uac_processing_unit_bControlSize(desc, protocol);
+
+ switch (protocol) {
+ case UAC_VERSION_1:
+ case UAC_VERSION_2:
+ default:
+ return *(uac_processing_unit_bmControls(desc, protocol)
+ + control_size);
+ case UAC_VERSION_3:
+ return 0; /* UAC3 does not have this field */
+ }
+}
+
+static inline __u8 *uac_processing_unit_specific(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ __u8 control_size = uac_processing_unit_bControlSize(desc, protocol);
+
+ switch (protocol) {
+ case UAC_VERSION_1:
+ case UAC_VERSION_2:
+ default:
+ return uac_processing_unit_bmControls(desc, protocol)
+ + control_size + 1;
+ case UAC_VERSION_3:
+ return uac_processing_unit_bmControls(desc, protocol)
+ + control_size;
+ }
+}
+
+/*
+ * Extension Unit (XU) has almost compatible layout with Processing Unit, but
+ * on UAC2, it has a different bmControls size (bControlSize); it's 1 byte for
+ * XU while 2 bytes for PU. The last iExtension field is a one-byte index as
+ * well as iProcessing field of PU.
+ */
+static inline __u8 uac_extension_unit_bControlSize(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ switch (protocol) {
+ case UAC_VERSION_1:
+ return desc->baSourceID[desc->bNrInPins + 4];
+ case UAC_VERSION_2:
+ return 1; /* in UAC2, this value is constant */
+ case UAC_VERSION_3:
+ return 4; /* in UAC3, this value is constant */
+ default:
+ return 1;
+ }
+}
+
+static inline __u8 uac_extension_unit_iExtension(struct uac_processing_unit_descriptor *desc,
+ int protocol)
+{
+ __u8 control_size = uac_extension_unit_bControlSize(desc, protocol);
+
+ switch (protocol) {
+ case UAC_VERSION_1:
+ case UAC_VERSION_2:
+ default:
+ return *(uac_processing_unit_bmControls(desc, protocol)
+ + control_size);
+ case UAC_VERSION_3:
+ return 0; /* UAC3 does not have this field */
+ }
+}
+
+/* 4.5.2 Class-Specific AS Interface Descriptor */
+struct uac1_as_header_descriptor {
+ __u8 bLength; /* in bytes: 7 */
+ __u8 bDescriptorType; /* USB_DT_CS_INTERFACE */
+ __u8 bDescriptorSubtype; /* AS_GENERAL */
+ __u8 bTerminalLink; /* Terminal ID of connected Terminal */
+ __u8 bDelay; /* Delay introduced by the data path */
+ __le16 wFormatTag; /* The Audio Data Format */
+} __attribute__ ((packed));
+
+#define UAC_DT_AS_HEADER_SIZE 7
+
+/* Formats - A.1.1 Audio Data Format Type I Codes */
+#define UAC_FORMAT_TYPE_I_UNDEFINED 0x0
+#define UAC_FORMAT_TYPE_I_PCM 0x1
+#define UAC_FORMAT_TYPE_I_PCM8 0x2
+#define UAC_FORMAT_TYPE_I_IEEE_FLOAT 0x3
+#define UAC_FORMAT_TYPE_I_ALAW 0x4
+#define UAC_FORMAT_TYPE_I_MULAW 0x5
+
+struct uac_format_type_i_continuous_descriptor {
+ __u8 bLength; /* in bytes: 8 + (ns * 3) */
+ __u8 bDescriptorType; /* USB_DT_CS_INTERFACE */
+ __u8 bDescriptorSubtype; /* FORMAT_TYPE */
+ __u8 bFormatType; /* FORMAT_TYPE_1 */
+ __u8 bNrChannels; /* physical channels in the stream */
+ __u8 bSubframeSize; /* */
+ __u8 bBitResolution;
+ __u8 bSamFreqType;
+ __u8 tLowerSamFreq[3];
+ __u8 tUpperSamFreq[3];
+} __attribute__ ((packed));
+
+#define UAC_FORMAT_TYPE_I_CONTINUOUS_DESC_SIZE 14
+
+struct uac_format_type_i_discrete_descriptor {
+ __u8 bLength; /* in bytes: 8 + (ns * 3) */
+ __u8 bDescriptorType; /* USB_DT_CS_INTERFACE */
+ __u8 bDescriptorSubtype; /* FORMAT_TYPE */
+ __u8 bFormatType; /* FORMAT_TYPE_1 */
+ __u8 bNrChannels; /* physical channels in the stream */
+ __u8 bSubframeSize; /* */
+ __u8 bBitResolution;
+ __u8 bSamFreqType;
+ __u8 tSamFreq[][3];
+} __attribute__ ((packed));
+
+#define DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(n) \
+struct uac_format_type_i_discrete_descriptor_##n { \
+ __u8 bLength; \
+ __u8 bDescriptorType; \
+ __u8 bDescriptorSubtype; \
+ __u8 bFormatType; \
+ __u8 bNrChannels; \
+ __u8 bSubframeSize; \
+ __u8 bBitResolution; \
+ __u8 bSamFreqType; \
+ __u8 tSamFreq[n][3]; \
+} __attribute__ ((packed))
+
+#define UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(n) (8 + (n * 3))
+
+struct uac_format_type_i_ext_descriptor {
+ __u8 bLength;
+ __u8 bDescriptorType;
+ __u8 bDescriptorSubtype;
+ __u8 bFormatType;
+ __u8 bSubslotSize;
+ __u8 bBitResolution;
+ __u8 bHeaderLength;
+ __u8 bControlSize;
+ __u8 bSideBandProtocol;
+} __attribute__((packed));
+
+/* Formats - Audio Data Format Type I Codes */
+
+#define UAC_FORMAT_TYPE_II_MPEG 0x1001
+#define UAC_FORMAT_TYPE_II_AC3 0x1002
+
+struct uac_format_type_ii_discrete_descriptor {
+ __u8 bLength;
+ __u8 bDescriptorType;
+ __u8 bDescriptorSubtype;
+ __u8 bFormatType;
+ __le16 wMaxBitRate;
+ __le16 wSamplesPerFrame;
+ __u8 bSamFreqType;
+ __u8 tSamFreq[][3];
+} __attribute__((packed));
+
+struct uac_format_type_ii_ext_descriptor {
+ __u8 bLength;
+ __u8 bDescriptorType;
+ __u8 bDescriptorSubtype;
+ __u8 bFormatType;
+ __le16 wMaxBitRate;
+ __le16 wSamplesPerFrame;
+ __u8 bHeaderLength;
+ __u8 bSideBandProtocol;
+} __attribute__((packed));
+
+/* type III */
+#define UAC_FORMAT_TYPE_III_IEC1937_AC3 0x2001
+#define UAC_FORMAT_TYPE_III_IEC1937_MPEG1_LAYER1 0x2002
+#define UAC_FORMAT_TYPE_III_IEC1937_MPEG2_NOEXT 0x2003
+#define UAC_FORMAT_TYPE_III_IEC1937_MPEG2_EXT 0x2004
+#define UAC_FORMAT_TYPE_III_IEC1937_MPEG2_LAYER1_LS 0x2005
+#define UAC_FORMAT_TYPE_III_IEC1937_MPEG2_LAYER23_LS 0x2006
+
+/* Formats - A.2 Format Type Codes */
+#define UAC_FORMAT_TYPE_UNDEFINED 0x0
+#define UAC_FORMAT_TYPE_I 0x1
+#define UAC_FORMAT_TYPE_II 0x2
+#define UAC_FORMAT_TYPE_III 0x3
+#define UAC_EXT_FORMAT_TYPE_I 0x81
+#define UAC_EXT_FORMAT_TYPE_II 0x82
+#define UAC_EXT_FORMAT_TYPE_III 0x83
+
+struct uac_iso_endpoint_descriptor {
+ __u8 bLength; /* in bytes: 7 */
+ __u8 bDescriptorType; /* USB_DT_CS_ENDPOINT */
+ __u8 bDescriptorSubtype; /* EP_GENERAL */
+ __u8 bmAttributes;
+ __u8 bLockDelayUnits;
+ __le16 wLockDelay;
+} __attribute__((packed));
+#define UAC_ISO_ENDPOINT_DESC_SIZE 7
+
+#define UAC_EP_CS_ATTR_SAMPLE_RATE 0x01
+#define UAC_EP_CS_ATTR_PITCH_CONTROL 0x02
+#define UAC_EP_CS_ATTR_FILL_MAX 0x80
+
+/* status word format (3.7.1.1) */
+
+#define UAC1_STATUS_TYPE_ORIG_MASK 0x0f
+#define UAC1_STATUS_TYPE_ORIG_AUDIO_CONTROL_IF 0x0
+#define UAC1_STATUS_TYPE_ORIG_AUDIO_STREAM_IF 0x1
+#define UAC1_STATUS_TYPE_ORIG_AUDIO_STREAM_EP 0x2
+
+#define UAC1_STATUS_TYPE_IRQ_PENDING (1 << 7)
+#define UAC1_STATUS_TYPE_MEM_CHANGED (1 << 6)
+
+struct uac1_status_word {
+ __u8 bStatusType;
+ __u8 bOriginator;
+} __attribute__((packed));
+
+
+#endif /* _UAPI__LINUX_USB_AUDIO_H */
diff --git a/components/drivers/usb/usbdevice/class/audio_mic.c b/components/drivers/usb/usbdevice/class/audio_mic.c
new file mode 100644
index 0000000000..4ac85711ad
--- /dev/null
+++ b/components/drivers/usb/usbdevice/class/audio_mic.c
@@ -0,0 +1,577 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2019-09-07 flybreak the first version
+ */
+
+#include <rthw.h>
+#include <rtthread.h>
+#include <rtservice.h>
+#include <rtdevice.h>
+
+#include "drivers/usb_device.h"
+#include "audio.h"
+
+#define DBG_TAG "usbd.audio.mic"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
+#define RECORD_SAMPLERATE 16000
+#define RECORD_CHANNEL 1
+#define RESOLUTION_BITS 16
+
+#define RESOLUTION_BYTE (RESOLUTION_BITS / 8)
+#define RECORD_PER_MS_SZ ((RECORD_SAMPLERATE * RECORD_CHANNEL * RESOLUTION_BYTE) / 1000)
+#define RECORD_BUFFER_SZ (RECORD_PER_MS_SZ * 20) /* 20ms */
+
+#if defined(RT_USBD_MIC_DEVICE_NAME)
+ #define MIC_DEVICE_NAME RT_USBD_MIC_DEVICE_NAME
+#else
+ #define MIC_DEVICE_NAME "mic0"
+#endif
+
+#define EVENT_RECORD_START (1 << 0)
+#define EVENT_RECORD_STOP (1 << 1)
+#define EVENT_RECORD_DATA (1 << 2)
+
+/*
+ * uac mic descriptor define
+ */
+
+#define UAC_CS_INTERFACE 0x24
+#define UAC_CS_ENDPOINT 0x25
+
+#define UAC_MAX_PACKET_SIZE 64
+#define UAC_EP_MAX_PACKET_SIZE 32
+#define UAC_CHANNEL_NUM RECORD_CHANNEL
+#define UAC_INTR_NUM 1
+#define UAC_CH_NUM 1
+#define UAC_FORMAT_NUM 1
+
+struct uac_ac_descriptor
+{
+#ifdef RT_USB_DEVICE_COMPOSITE
+ struct uiad_descriptor iad_desc;
+#endif
+ struct uinterface_descriptor intf_desc;
+ DECLARE_UAC_AC_HEADER_DESCRIPTOR(UAC_INTR_NUM) hdr_desc;
+ struct uac_input_terminal_descriptor it_desc;
+ struct uac1_output_terminal_descriptor ot_desc;
+#if UAC_USE_FEATURE_UNIT
+ DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(UAC_CH_NUM) feature_unit_desc;
+#endif
+};
+
+struct uac_as_descriptor
+{
+ struct uinterface_descriptor intf_desc;
+ struct uac1_as_header_descriptor hdr_desc;
+ DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(UAC_FORMAT_NUM) format_type_desc;
+ struct uendpoint_descriptor ep_desc;
+ struct uac_iso_endpoint_descriptor as_ep_desc;
+};
+
+/*
+ * uac mic device type
+ */
+
+struct uac_audio_mic
+{
+ rt_device_t dev;
+ rt_event_t event;
+ rt_uint8_t open_count;
+
+ rt_uint8_t *buffer;
+ rt_uint32_t buffer_index;
+
+ uep_t ep;
+};
+static struct uac_audio_mic mic;
+
+ALIGN(4)
+static struct udevice_descriptor dev_desc =
+{
+ USB_DESC_LENGTH_DEVICE, //bLength;
+ USB_DESC_TYPE_DEVICE, //type;
+ USB_BCD_VERSION, //bcdUSB;
+ USB_CLASS_DEVICE, //bDeviceClass;
+ 0x00, //bDeviceSubClass;
+ 0x00, //bDeviceProtocol;
+ UAC_MAX_PACKET_SIZE, //bMaxPacketSize0;
+ _VENDOR_ID, //idVendor;
+ _PRODUCT_ID, //idProduct;
+ USB_BCD_DEVICE, //bcdDevice;
+ USB_STRING_MANU_INDEX, //iManufacturer;
+ USB_STRING_PRODUCT_INDEX, //iProduct;
+ USB_STRING_SERIAL_INDEX, //iSerialNumber;Unused.
+ USB_DYNAMIC, //bNumConfigurations;
+};
+
+//FS and HS needed
+ALIGN(4)
+static struct usb_qualifier_descriptor dev_qualifier =
+{
+ sizeof(dev_qualifier), //bLength
+ USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
+ 0x0200, //bcdUSB
+ USB_CLASS_AUDIO, //bDeviceClass
+ 0x00, //bDeviceSubClass
+ 0x00, //bDeviceProtocol
+ 64, //bMaxPacketSize0
+ 0x01, //bNumConfigurations
+ 0,
+};
+
+ALIGN(4)
+const static char *_ustring[] =
+{
+ "Language",
+ "RT-Thread Team.",
+ "Microphone",
+ "32021919830108",
+ "Configuration",
+ "Interface",
+};
+
+ALIGN(4)
+static struct uac_ac_descriptor ac_desc =
+{
+#ifdef RT_USB_DEVICE_COMPOSITE
+ /* Interface Association Descriptor */
+ {
+ USB_DESC_LENGTH_IAD,
+ USB_DESC_TYPE_IAD,
+ USB_DYNAMIC,
+ 0x02,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOSTREAMING,
+ 0x00,
+ 0x00,
+ },
+#endif
+ /* Interface Descriptor */
+ {
+ USB_DESC_LENGTH_INTERFACE,
+ USB_DESC_TYPE_INTERFACE,
+ USB_DYNAMIC,
+ 0x00,
+ 0x00,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOCONTROL,
+ 0x00,
+ 0x00,
+ },
+ /* Header Descriptor */
+ {
+ UAC_DT_AC_HEADER_SIZE(UAC_INTR_NUM),
+ UAC_CS_INTERFACE,
+ UAC_HEADER,
+ 0x0100, /* Version: 1.00 */
+ 0x001E, /* Total length: 30 */
+ 0x01, /* Total number of interfaces: 1 */
+ {0x01}, /* Interface number: 1 */
+ },
+ /* Input Terminal Descriptor */
+ {
+ UAC_DT_INPUT_TERMINAL_SIZE,
+ UAC_CS_INTERFACE,
+ UAC_INPUT_TERMINAL,
+ 0x01, /* Terminal ID: 1 */
+ 0x0201, /* Terminal Type: Microphone (0x0201) */
+ 0x00, /* Assoc Terminal: 0 */
+ 0x01, /* Number Channels: 1 */
+ 0x0000, /* Channel Config: 0x0000 */
+ 0x00, /* Channel Names: 0 */
+ 0x00, /* Terminal: 0 */
+ },
+ /* Output Terminal Descriptor */
+ {
+ UAC_DT_OUTPUT_TERMINAL_SIZE,
+ UAC_CS_INTERFACE,
+ UAC_OUTPUT_TERMINAL,
+ 0x02, /* Terminal ID: 2 */
+ 0x0101, /* Terminal Type: USB Streaming (0x0101) */
+ 0x00, /* Assoc Terminal: 0 */
+ 0x01, /* Source ID: 1 */
+ 0x00, /* Terminal: 0 */
+ },
+#if UAC_USE_FEATURE_UNIT
+ /* Feature unit Descriptor */
+ {
+ UAC_DT_FEATURE_UNIT_SIZE(UAC_CH_NUM),
+ UAC_CS_INTERFACE,
+ UAC_FEATURE_UNIT,
+ 0x02,
+ 0x0101,
+ 0x00,
+ 0x01,
+ },
+#endif
+};
+
+ALIGN(4)
+static struct uinterface_descriptor as_desc0 =
+{
+ USB_DESC_LENGTH_INTERFACE,
+ USB_DESC_TYPE_INTERFACE,
+ USB_DYNAMIC,
+ 0x00,
+ 0x00,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOSTREAMING,
+ 0x00,
+ 0x00,
+};
+
+ALIGN(4)
+static struct uac_as_descriptor as_desc =
+{
+ /* Interface Descriptor */
+ {
+ USB_DESC_LENGTH_INTERFACE,
+ USB_DESC_TYPE_INTERFACE,
+ USB_DYNAMIC,
+ 0x01,
+ 0x01,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOSTREAMING,
+ 0x00,
+ 0x00,
+ },
+ /* General AS Descriptor */
+ {
+ UAC_DT_AS_HEADER_SIZE,
+ UAC_CS_INTERFACE,
+ UAC_AS_GENERAL,
+ 0x02, /* Terminal ID: 2 */
+ 0x01, /* Interface delay in frames: 1 */
+ UAC_FORMAT_TYPE_I_PCM,
+ },
+ /* Format type i Descriptor */
+ {
+ UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(UAC_FORMAT_NUM),
+ UAC_CS_INTERFACE,
+ UAC_FORMAT_TYPE,
+ UAC_FORMAT_TYPE_I,
+ UAC_CHANNEL_NUM,
+ 2, /* Subframe Size: 2 */
+ RESOLUTION_BITS,
+ 0x01, /* Samples Frequence Type: 1 */
+ {0}, /* Samples Frequence */
+ },
+ /* Endpoint Descriptor */
+ {
+ USB_DESC_LENGTH_ENDPOINT,
+ USB_DESC_TYPE_ENDPOINT,
+ USB_DYNAMIC | USB_DIR_IN,
+ USB_EP_ATTR_ISOC,
+ UAC_EP_MAX_PACKET_SIZE,
+ 0x01,
+ },
+ /* AS Endpoint Descriptor */
+ {
+ UAC_ISO_ENDPOINT_DESC_SIZE,
+ UAC_CS_ENDPOINT,
+ UAC_MS_GENERAL,
+ },
+};
+
+void mic_entry(void *parameter)
+{
+ struct rt_audio_caps caps = {0};
+ rt_uint32_t e, index;
+
+ mic.buffer = rt_malloc(RECORD_BUFFER_SZ);
+ if (mic.buffer == RT_NULL)
+ {
+ LOG_E("malloc failed");
+ goto __exit;
+ }
+
+ mic.dev = rt_device_find(MIC_DEVICE_NAME);
+ if (mic.dev == RT_NULL)
+ {
+ LOG_E("can't find device:%s", MIC_DEVICE_NAME);
+ goto __exit;
+ }
+
+ while (1)
+ {
+ if (rt_event_recv(mic.event, EVENT_RECORD_START | EVENT_RECORD_STOP,
+ RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
+ 1000, &e) != RT_EOK)
+ {
+ continue;
+ }
+ if (mic.open_count == 0)
+ {
+ continue;
+ }
+ LOG_D("record start");
+
+ rt_device_open(mic.dev, RT_DEVICE_OFLAG_RDONLY);
+
+ caps.main_type = AUDIO_TYPE_INPUT;
+ caps.sub_type = AUDIO_DSP_PARAM;
+ caps.udata.config.samplerate = RECORD_SAMPLERATE;
+ caps.udata.config.channels = RECORD_CHANNEL;
+ caps.udata.config.samplebits = RESOLUTION_BITS;
+ rt_device_control(mic.dev, AUDIO_CTL_CONFIGURE, &caps);
+
+ while (1)
+ {
+ if (rt_event_recv(mic.event, EVENT_RECORD_DATA | EVENT_RECORD_STOP,
+ RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
+ 1000, &e) != RT_EOK)
+ {
+ if (mic.open_count > 0)
+ continue;
+ else
+ break;
+ }
+ if (e & EVENT_RECORD_DATA)
+ {
+ index = (mic.buffer_index >= RECORD_BUFFER_SZ / 2) ? 0 : (RECORD_BUFFER_SZ / 2);
+ rt_device_read(mic.dev, 0, mic.buffer + index, RECORD_BUFFER_SZ / 2);
+ }
+ else if (e & EVENT_RECORD_STOP)
+ {
+ break;
+ }
+ }
+ LOG_D("record stop");
+ rt_device_close(mic.dev);
+ }
+
+__exit:
+ if (mic.buffer)
+ rt_free(mic.buffer);
+}
+
+static rt_err_t _record_start(ufunction_t func)
+{
+ mic.ep->request.buffer = RT_NULL;
+ mic.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
+ mic.ep->request.req_type = UIO_REQUEST_WRITE;
+ rt_usbd_io_request(func->device, mic.ep, &mic.ep->request);
+
+ mic.open_count ++;
+ rt_event_send(mic.event, EVENT_RECORD_START);
+ return 0;
+}
+
+static rt_err_t _record_stop(ufunction_t func)
+{
+ mic.open_count --;
+ rt_event_send(mic.event, EVENT_RECORD_STOP);
+ return 0;
+}
+
+static rt_err_t _ep_data_in_handler(ufunction_t func, rt_size_t size)
+{
+ RT_ASSERT(func != RT_NULL);
+ LOG_D("_ep_data_in_handler");
+
+ mic.ep->request.buffer = mic.buffer + mic.buffer_index;
+ mic.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
+ mic.ep->request.req_type = UIO_REQUEST_WRITE;
+ rt_usbd_io_request(func->device, mic.ep, &mic.ep->request);
+
+ mic.buffer_index += UAC_EP_MAX_PACKET_SIZE;
+ if (mic.buffer_index >= RECORD_BUFFER_SZ)
+ {
+ mic.buffer_index = 0;
+ rt_event_send(mic.event, EVENT_RECORD_DATA);
+ }
+ else if (mic.buffer_index == RECORD_BUFFER_SZ / 2)
+ {
+ rt_event_send(mic.event, EVENT_RECORD_DATA);
+ }
+
+ return RT_EOK;
+}
+
+static rt_err_t _interface_as_handler(ufunction_t func, ureq_t setup)
+{
+ RT_ASSERT(func != RT_NULL);
+ RT_ASSERT(func->device != RT_NULL);
+ RT_ASSERT(setup != RT_NULL);
+
+ LOG_D("_interface_as_handler");
+
+ if ((setup->request_type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
+ {
+ switch (setup->bRequest)
+ {
+ case USB_REQ_GET_INTERFACE:
+ break;
+ case USB_REQ_SET_INTERFACE:
+ LOG_D("set interface handler");
+ if (setup->wValue == 1)
+ {
+ _record_start(func);
+ }
+ else if (setup->wValue == 0)
+ {
+ _record_stop(func);
+ }
+ break;
+ default:
+ LOG_D("unknown uac request 0x%x", setup->bRequest);
+ return -RT_ERROR;
+ }
+ }
+
+ return RT_EOK;
+}
+
+static rt_err_t _function_enable(ufunction_t func)
+{
+ RT_ASSERT(func != RT_NULL);
+
+ LOG_D("uac function enable");
+
+ return RT_EOK;
+}
+
+static rt_err_t _function_disable(ufunction_t func)
+{
+ RT_ASSERT(func != RT_NULL);
+
+ LOG_D("uac function disable");
+ _record_stop(func);
+ return RT_EOK;
+}
+
+static struct ufunction_ops ops =
+{
+ _function_enable,
+ _function_disable,
+ RT_NULL,
+};
+/**
+ * This function will configure uac descriptor.
+ *
+ * @param comm the communication interface number.
+ * @param data the data interface number.
+ *
+ * @return RT_EOK on successful.
+ */
+static rt_err_t _uac_descriptor_config(struct uac_ac_descriptor *ac,
+ rt_uint8_t cintf_nr, struct uac_as_descriptor *as, rt_uint8_t sintf_nr)
+{
+ ac->hdr_desc.baInterfaceNr[0] = sintf_nr;
+#ifdef RT_USB_DEVICE_COMPOSITE
+ ac->iad_desc.bFirstInterface = cintf_nr;
+#endif
+
+ return RT_EOK;
+}
+
+static rt_err_t _uac_samplerate_config(struct uac_as_descriptor *as, rt_uint32_t samplerate)
+{
+ as->format_type_desc.tSamFreq[0][2] = samplerate >> 16 & 0xff;
+ as->format_type_desc.tSamFreq[0][1] = samplerate >> 8 & 0xff;
+ as->format_type_desc.tSamFreq[0][0] = samplerate & 0xff;
+ return RT_EOK;
+}
+
+/**
+ * This function will create a uac function instance.
+ *
+ * @param device the usb device object.
+ *
+ * @return RT_EOK on successful.
+ */
+ufunction_t rt_usbd_function_uac_mic_create(udevice_t device)
+{
+ ufunction_t func;
+ uintf_t intf_ac, intf_as;
+ ualtsetting_t setting_as0;
+ ualtsetting_t setting_ac, setting_as;
+ struct uac_as_descriptor *as_desc_t;
+
+ /* parameter check */
+ RT_ASSERT(device != RT_NULL);
+
+ /* set usb device string description */
+ rt_usbd_device_set_string(device, _ustring);
+
+ /* create a uac function */
+ func = rt_usbd_function_new(device, &dev_desc, &ops);
+ //not support HS
+ //rt_usbd_device_set_qualifier(device, &dev_qualifier);
+
+ /* create interface */
+ intf_ac = rt_usbd_interface_new(device, RT_NULL);
+ intf_as = rt_usbd_interface_new(device, _interface_as_handler);
+
+ /* create alternate setting */
+ setting_ac = rt_usbd_altsetting_new(sizeof(struct uac_ac_descriptor));
+ setting_as0 = rt_usbd_altsetting_new(sizeof(struct uinterface_descriptor));
+ setting_as = rt_usbd_altsetting_new(sizeof(struct uac_as_descriptor));
+ /* config desc in alternate setting */
+ rt_usbd_altsetting_config_descriptor(setting_ac, &ac_desc,
+ (rt_off_t) & ((struct uac_ac_descriptor *)0)->intf_desc);
+ rt_usbd_altsetting_config_descriptor(setting_as0, &as_desc0, 0);
+ rt_usbd_altsetting_config_descriptor(setting_as, &as_desc,
+ (rt_off_t) & ((struct uac_as_descriptor *)0)->intf_desc);
+ /* configure the uac interface descriptor */
+ _uac_descriptor_config(setting_ac->desc, intf_ac->intf_num, setting_as->desc, intf_as->intf_num);
+ _uac_samplerate_config(setting_as->desc, RECORD_SAMPLERATE);
+
+ /* create endpoint */
+ as_desc_t = (struct uac_as_descriptor *)setting_as->desc;
+ mic.ep = rt_usbd_endpoint_new(&as_desc_t->ep_desc, _ep_data_in_handler);
+
+ /* add the endpoint to the alternate setting */
+ rt_usbd_altsetting_add_endpoint(setting_as, mic.ep);
+
+ /* add the alternate setting to the interface, then set default setting of the interface */
+ rt_usbd_interface_add_altsetting(intf_ac, setting_ac);
+ rt_usbd_set_altsetting(intf_ac, 0);
+ rt_usbd_interface_add_altsetting(intf_as, setting_as0);
+ rt_usbd_interface_add_altsetting(intf_as, setting_as);
+ rt_usbd_set_altsetting(intf_as, 0);
+
+ /* add the interface to the uac function */
+ rt_usbd_function_add_interface(func, intf_ac);
+ rt_usbd_function_add_interface(func, intf_as);
+
+ return func;
+}
+
+int audio_mic_init(void)
+{
+ rt_thread_t mic_tid;
+ mic.event = rt_event_create("mic_event", RT_IPC_FLAG_FIFO);
+
+ mic_tid = rt_thread_create("mic_thread",
+ mic_entry, RT_NULL,
+ 1024,
+ 5, 10);
+
+ if (mic_tid != RT_NULL)
+ rt_thread_startup(mic_tid);
+ return RT_EOK;
+}
+INIT_COMPONENT_EXPORT(audio_mic_init);
+
+/*
+ * register uac class
+ */
+struct udclass uac_class =
+{
+ .rt_usbd_function_create = rt_usbd_function_uac_mic_create
+};
+
+int rt_usbd_uac_mic_class_register(void)
+{
+ rt_usbd_class_register(&uac_class);
+ return 0;
+}
+INIT_PREV_EXPORT(rt_usbd_uac_mic_class_register);
diff --git a/components/drivers/usb/usbdevice/class/audio_speaker.c b/components/drivers/usb/usbdevice/class/audio_speaker.c
new file mode 100644
index 0000000000..84c36d2053
--- /dev/null
+++ b/components/drivers/usb/usbdevice/class/audio_speaker.c
@@ -0,0 +1,578 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2019-09-19 flybreak the first version
+ */
+
+#include <rthw.h>
+#include <rtthread.h>
+#include <rtservice.h>
+#include <rtdevice.h>
+
+#include "drivers/usb_device.h"
+#include "audio.h"
+
+#define DBG_TAG "usbd.audio.speaker"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
+#define AUDIO_SAMPLERATE 16000
+#define AUDIO_CHANNEL 1
+#define RESOLUTION_BITS 16
+
+#define RESOLUTION_BYTE (RESOLUTION_BITS / 8)
+#define AUDIO_PER_MS_SZ ((AUDIO_SAMPLERATE * AUDIO_CHANNEL * RESOLUTION_BYTE) / 1000)
+#define AUDIO_BUFFER_SZ (AUDIO_PER_MS_SZ * 20) /* 20ms */
+
+#if defined(RT_USBD_SPEAKER_DEVICE_NAME)
+ #define SPEAKER_DEVICE_NAME RT_USBD_SPEAKER_DEVICE_NAME
+#else
+ #define SPEAKER_DEVICE_NAME "sound0"
+#endif
+
+#define EVENT_AUDIO_START (1 << 0)
+#define EVENT_AUDIO_STOP (1 << 1)
+#define EVENT_AUDIO_DATA (1 << 2)
+
+/*
+ * uac speaker descriptor define
+ */
+
+#define UAC_CS_INTERFACE 0x24
+#define UAC_CS_ENDPOINT 0x25
+
+#define UAC_MAX_PACKET_SIZE 64
+#define UAC_EP_MAX_PACKET_SIZE 32
+#define UAC_CHANNEL_NUM AUDIO_CHANNEL
+#define UAC_INTR_NUM 1
+#define UAC_CH_NUM 1
+#define UAC_FORMAT_NUM 1
+
+struct uac_ac_descriptor
+{
+#ifdef RT_USB_DEVICE_COMPOSITE
+ struct uiad_descriptor iad_desc;
+#endif
+ struct uinterface_descriptor intf_desc;
+ DECLARE_UAC_AC_HEADER_DESCRIPTOR(UAC_INTR_NUM) hdr_desc;
+ struct uac_input_terminal_descriptor it_desc;
+ struct uac1_output_terminal_descriptor ot_desc;
+#if UAC_USE_FEATURE_UNIT
+ DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(UAC_CH_NUM) feature_unit_desc;
+#endif
+};
+
+struct uac_as_descriptor
+{
+ struct uinterface_descriptor intf_desc;
+ struct uac1_as_header_descriptor hdr_desc;
+ DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(UAC_FORMAT_NUM) format_type_desc;
+ struct uendpoint_descriptor ep_desc;
+ struct uac_iso_endpoint_descriptor as_ep_desc;
+};
+
+/*
+ * uac speaker device type
+ */
+
+struct uac_audio_speaker
+{
+ rt_device_t dev;
+ rt_event_t event;
+ rt_uint8_t open_count;
+
+ rt_uint8_t *buffer;
+ rt_uint32_t buffer_index;
+
+ uep_t ep;
+};
+static struct uac_audio_speaker speaker;
+
+ALIGN(4)
+static struct udevice_descriptor dev_desc =
+{
+ USB_DESC_LENGTH_DEVICE, //bLength;
+ USB_DESC_TYPE_DEVICE, //type;
+ USB_BCD_VERSION, //bcdUSB;
+ USB_CLASS_DEVICE, //bDeviceClass;
+ 0x00, //bDeviceSubClass;
+ 0x00, //bDeviceProtocol;
+ UAC_MAX_PACKET_SIZE, //bMaxPacketSize0;
+ _VENDOR_ID, //idVendor;
+ _PRODUCT_ID, //idProduct;
+ USB_BCD_DEVICE, //bcdDevice;
+ USB_STRING_MANU_INDEX, //iManufacturer;
+ USB_STRING_PRODUCT_INDEX, //iProduct;
+ USB_STRING_SERIAL_INDEX, //iSerialNumber;Unused.
+ USB_DYNAMIC, //bNumConfigurations;
+};
+
+//FS and HS needed
+ALIGN(4)
+static struct usb_qualifier_descriptor dev_qualifier =
+{
+ sizeof(dev_qualifier), //bLength
+ USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
+ 0x0200, //bcdUSB
+ USB_CLASS_AUDIO, //bDeviceClass
+ 0x00, //bDeviceSubClass
+ 0x00, //bDeviceProtocol
+ 64, //bMaxPacketSize0
+ 0x01, //bNumConfigurations
+ 0,
+};
+
+ALIGN(4)
+const static char *_ustring[] =
+{
+ "Language",
+ "RT-Thread Team.",
+ "RT-Thread Speaker",
+ "32021919830108",
+ "Configuration",
+ "Interface",
+};
+
+ALIGN(4)
+static struct uac_ac_descriptor ac_desc =
+{
+#ifdef RT_USB_DEVICE_COMPOSITE
+ /* Interface Association Descriptor */
+ {
+ USB_DESC_LENGTH_IAD,
+ USB_DESC_TYPE_IAD,
+ USB_DYNAMIC,
+ 0x02,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOSTREAMING,
+ 0x00,
+ 0x00,
+ },
+#endif
+ /* Interface Descriptor */
+ {
+ USB_DESC_LENGTH_INTERFACE,
+ USB_DESC_TYPE_INTERFACE,
+ USB_DYNAMIC,
+ 0x00,
+ 0x00,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOCONTROL,
+ 0x00,
+ 0x00,
+ },
+ /* Header Descriptor */
+ {
+ UAC_DT_AC_HEADER_SIZE(UAC_INTR_NUM),
+ UAC_CS_INTERFACE,
+ UAC_HEADER,
+ 0x0100, /* Version: 1.00 */
+ 0x0027, /* Total length: 39 */
+ 0x01, /* Total number of interfaces: 1 */
+ {0x01}, /* Interface number: 1 */
+ },
+ /* Input Terminal Descriptor */
+ {
+ UAC_DT_INPUT_TERMINAL_SIZE,
+ UAC_CS_INTERFACE,
+ UAC_INPUT_TERMINAL,
+ 0x01, /* Terminal ID: 1 */
+ 0x0101, /* Terminal Type: USB Streaming (0x0101) */
+ 0x00, /* Assoc Terminal: 0 */
+ 0x01, /* Number Channels: 1 */
+ 0x0000, /* Channel Config: 0x0000 */
+ 0x00, /* Channel Names: 0 */
+ 0x00, /* Terminal: 0 */
+ },
+ /* Output Terminal Descriptor */
+ {
+ UAC_DT_OUTPUT_TERMINAL_SIZE,
+ UAC_CS_INTERFACE,
+ UAC_OUTPUT_TERMINAL,
+ 0x02, /* Terminal ID: 2 */
+ 0x0302, /* Terminal Type: Headphones (0x0302) */
+ 0x00, /* Assoc Terminal: 0 */
+ 0x01, /* Source ID: 1 */
+ 0x00, /* Terminal: 0 */
+ },
+#if UAC_USE_FEATURE_UNIT
+ /* Feature unit Descriptor */
+ {
+ UAC_DT_FEATURE_UNIT_SIZE(UAC_CH_NUM),
+ UAC_CS_INTERFACE,
+ UAC_FEATURE_UNIT,
+ 0x02,
+ 0x0101,
+ 0x00,
+ 0x01,
+ },
+#endif
+};
+
+ALIGN(4)
+static struct uinterface_descriptor as_desc0 =
+{
+ USB_DESC_LENGTH_INTERFACE,
+ USB_DESC_TYPE_INTERFACE,
+ USB_DYNAMIC,
+ 0x00,
+ 0x00,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOSTREAMING,
+ 0x00,
+ 0x00,
+};
+
+ALIGN(4)
+static struct uac_as_descriptor as_desc =
+{
+ /* Interface Descriptor */
+ {
+ USB_DESC_LENGTH_INTERFACE,
+ USB_DESC_TYPE_INTERFACE,
+ USB_DYNAMIC,
+ 0x01,
+ 0x01,
+ USB_CLASS_AUDIO,
+ USB_SUBCLASS_AUDIOSTREAMING,
+ 0x00,
+ 0x00,
+ },
+ /* General AS Descriptor */
+ {
+ UAC_DT_AS_HEADER_SIZE,
+ UAC_CS_INTERFACE,
+ UAC_AS_GENERAL,
+ 0x01, /* Terminal ID: 1 */
+ 0x01, /* Interface delay in frames: 1 */
+ UAC_FORMAT_TYPE_I_PCM,
+ },
+ /* Format type i Descriptor */
+ {
+ UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(UAC_FORMAT_NUM),
+ UAC_CS_INTERFACE,
+ UAC_FORMAT_TYPE,
+ UAC_FORMAT_TYPE_I,
+ UAC_CHANNEL_NUM,
+ 2, /* Subframe Size: 2 */
+ RESOLUTION_BITS,
+ 0x01, /* Samples Frequence Type: 1 */
+ {0}, /* Samples Frequence */
+ },
+ /* Endpoint Descriptor */
+ {
+ USB_DESC_LENGTH_ENDPOINT,
+ USB_DESC_TYPE_ENDPOINT,
+ USB_DYNAMIC | USB_DIR_OUT,
+ USB_EP_ATTR_ISOC,
+ UAC_EP_MAX_PACKET_SIZE,
+ 0x01,
+ },
+ /* AS Endpoint Descriptor */
+ {
+ UAC_ISO_ENDPOINT_DESC_SIZE,
+ UAC_CS_ENDPOINT,
+ UAC_MS_GENERAL,
+ },
+};
+
+void speaker_entry(void *parameter)
+{
+ struct rt_audio_caps caps = {0};
+ rt_uint32_t e, index;
+
+ speaker.buffer = rt_malloc(AUDIO_BUFFER_SZ);
+ if (speaker.buffer == RT_NULL)
+ {
+ LOG_E("malloc failed");
+ goto __exit;
+ }
+
+ speaker.dev = rt_device_find(SPEAKER_DEVICE_NAME);
+ if (speaker.dev == RT_NULL)
+ {
+ LOG_E("can't find device:%s", SPEAKER_DEVICE_NAME);
+ goto __exit;
+ }
+
+ while (1)
+ {
+ if (rt_event_recv(speaker.event, EVENT_AUDIO_START | EVENT_AUDIO_STOP,
+ RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
+ 1000, &e) != RT_EOK)
+ {
+ continue;
+ }
+ if (speaker.open_count == 0)
+ {
+ continue;
+ }
+ LOG_D("play start");
+
+ rt_device_open(speaker.dev, RT_DEVICE_OFLAG_WRONLY);
+
+ caps.main_type = AUDIO_TYPE_OUTPUT;
+ caps.sub_type = AUDIO_DSP_PARAM;
+ caps.udata.config.samplerate = AUDIO_SAMPLERATE;
+ caps.udata.config.channels = AUDIO_CHANNEL;
+ caps.udata.config.samplebits = RESOLUTION_BITS;
+ rt_device_control(speaker.dev, AUDIO_CTL_CONFIGURE, &caps);
+
+ while (1)
+ {
+ if (rt_event_recv(speaker.event, EVENT_AUDIO_DATA | EVENT_AUDIO_STOP,
+ RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
+ 1000, &e) != RT_EOK)
+ {
+ if (speaker.open_count > 0)
+ continue;
+ else
+ break;
+ }
+ if (e & EVENT_AUDIO_DATA)
+ {
+ index = (speaker.buffer_index >= AUDIO_BUFFER_SZ / 2) ? 0 : (AUDIO_BUFFER_SZ / 2);
+ rt_device_write(speaker.dev, 0, speaker.buffer + index, AUDIO_BUFFER_SZ / 2);
+ }
+ else if (e & EVENT_AUDIO_STOP)
+ {
+ break;
+ }
+ }
+ LOG_D("play stop");
+ rt_device_close(speaker.dev);
+ }
+
+__exit:
+ if (speaker.buffer)
+ rt_free(speaker.buffer);
+}
+
+static rt_err_t _audio_start(ufunction_t func)
+{
+ speaker.ep->request.buffer = speaker.buffer;
+ speaker.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
+ speaker.ep->request.req_type = UIO_REQUEST_READ_FULL;
+ rt_usbd_io_request(func->device, speaker.ep, &speaker.ep->request);
+
+ speaker.open_count ++;
+ rt_event_send(speaker.event, EVENT_AUDIO_START);
+
+ return 0;
+}
+
+static rt_err_t _audio_stop(ufunction_t func)
+{
+ speaker.open_count --;
+ rt_event_send(speaker.event, EVENT_AUDIO_STOP);
+ return 0;
+}
+
+static rt_err_t _ep_data_handler(ufunction_t func, rt_size_t size)
+{
+ RT_ASSERT(func != RT_NULL);
+ LOG_D("_ep_data_handler");
+
+ speaker.ep->request.buffer = speaker.buffer + speaker.buffer_index;
+ speaker.ep->request.size = UAC_EP_MAX_PACKET_SIZE;
+ speaker.ep->request.req_type = UIO_REQUEST_READ_FULL;
+ rt_usbd_io_request(func->device, speaker.ep, &speaker.ep->request);
+
+ speaker.buffer_index += UAC_EP_MAX_PACKET_SIZE;
+ if (speaker.buffer_index >= AUDIO_BUFFER_SZ)
+ {
+ speaker.buffer_index = 0;
+ rt_event_send(speaker.event, EVENT_AUDIO_DATA);
+ }
+ else if (speaker.buffer_index == AUDIO_BUFFER_SZ / 2)
+ {
+ rt_event_send(speaker.event, EVENT_AUDIO_DATA);
+ }
+
+ return RT_EOK;
+}
+
+static rt_err_t _interface_as_handler(ufunction_t func, ureq_t setup)
+{
+ RT_ASSERT(func != RT_NULL);
+ RT_ASSERT(func->device != RT_NULL);
+ RT_ASSERT(setup != RT_NULL);
+
+ LOG_D("_interface_as_handler");
+
+ if ((setup->request_type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
+ {
+ switch (setup->bRequest)
+ {
+ case USB_REQ_GET_INTERFACE:
+ break;
+ case USB_REQ_SET_INTERFACE:
+ LOG_D("set interface handler");
+ if (setup->wValue == 1)
+ {
+ _audio_start(func);
+ }
+ else if (setup->wValue == 0)
+ {
+ _audio_stop(func);
+ }
+ break;
+ default:
+ LOG_D("unknown uac request 0x%x", setup->bRequest);
+ return -RT_ERROR;
+ }
+ }
+
+ return RT_EOK;
+}
+
+static rt_err_t _function_enable(ufunction_t func)
+{
+ RT_ASSERT(func != RT_NULL);
+
+ LOG_D("uac function enable");
+
+ return RT_EOK;
+}
+
+static rt_err_t _function_disable(ufunction_t func)
+{
+ RT_ASSERT(func != RT_NULL);
+
+ LOG_D("uac function disable");
+ _audio_stop(func);
+ return RT_EOK;
+}
+
+static struct ufunction_ops ops =
+{
+ _function_enable,
+ _function_disable,
+ RT_NULL,
+};
+/**
+ * This function will configure uac descriptor.
+ *
+ * @param comm the communication interface number.
+ * @param data the data interface number.
+ *
+ * @return RT_EOK on successful.
+ */
+static rt_err_t _uac_descriptor_config(struct uac_ac_descriptor *ac,
+ rt_uint8_t cintf_nr, struct uac_as_descriptor *as, rt_uint8_t sintf_nr)
+{
+ ac->hdr_desc.baInterfaceNr[0] = sintf_nr;
+#ifdef RT_USB_DEVICE_COMPOSITE
+ ac->iad_desc.bFirstInterface = cintf_nr;
+#endif
+
+ return RT_EOK;
+}
+
+static rt_err_t _uac_samplerate_config(struct uac_as_descriptor *as, rt_uint32_t samplerate)
+{
+ as->format_type_desc.tSamFreq[0][2] = samplerate >> 16 & 0xff;
+ as->format_type_desc.tSamFreq[0][1] = samplerate >> 8 & 0xff;
+ as->format_type_desc.tSamFreq[0][0] = samplerate & 0xff;
+ return RT_EOK;
+}
+
+/**
+ * This function will create a uac function instance.
+ *
+ * @param device the usb device object.
+ *
+ * @return RT_EOK on successful.
+ */
+ufunction_t rt_usbd_function_uac_speaker_create(udevice_t device)
+{
+ ufunction_t func;
+ uintf_t intf_ac, intf_as;
+ ualtsetting_t setting_as0;
+ ualtsetting_t setting_ac, setting_as;
+ struct uac_as_descriptor *as_desc_t;
+
+ /* parameter check */
+ RT_ASSERT(device != RT_NULL);
+
+ /* set usb device string description */
+ rt_usbd_device_set_string(device, _ustring);
+
+ /* create a uac function */
+ func = rt_usbd_function_new(device, &dev_desc, &ops);
+ //not support HS
+ //rt_usbd_device_set_qualifier(device, &dev_qualifier);
+
+ /* create interface */
+ intf_ac = rt_usbd_interface_new(device, RT_NULL);
+ intf_as = rt_usbd_interface_new(device, _interface_as_handler);
+
+ /* create alternate setting */
+ setting_ac = rt_usbd_altsetting_new(sizeof(struct uac_ac_descriptor));
+ setting_as0 = rt_usbd_altsetting_new(sizeof(struct uinterface_descriptor));
+ setting_as = rt_usbd_altsetting_new(sizeof(struct uac_as_descriptor));
+ /* config desc in alternate setting */
+ rt_usbd_altsetting_config_descriptor(setting_ac, &ac_desc,
+ (rt_off_t) & ((struct uac_ac_descriptor *)0)->intf_desc);
+ rt_usbd_altsetting_config_descriptor(setting_as0, &as_desc0, 0);
+ rt_usbd_altsetting_config_descriptor(setting_as, &as_desc,
+ (rt_off_t) & ((struct uac_as_descriptor *)0)->intf_desc);
+ /* configure the uac interface descriptor */
+ _uac_descriptor_config(setting_ac->desc, intf_ac->intf_num, setting_as->desc, intf_as->intf_num);
+ _uac_samplerate_config(setting_as->desc, AUDIO_SAMPLERATE);
+
+ /* create endpoint */
+ as_desc_t = (struct uac_as_descriptor *)setting_as->desc;
+ speaker.ep = rt_usbd_endpoint_new(&as_desc_t->ep_desc, _ep_data_handler);
+
+ /* add the endpoint to the alternate setting */
+ rt_usbd_altsetting_add_endpoint(setting_as, speaker.ep);
+
+ /* add the alternate setting to the interface, then set default setting of the interface */
+ rt_usbd_interface_add_altsetting(intf_ac, setting_ac);
+ rt_usbd_set_altsetting(intf_ac, 0);
+ rt_usbd_interface_add_altsetting(intf_as, setting_as0);
+ rt_usbd_interface_add_altsetting(intf_as, setting_as);
+ rt_usbd_set_altsetting(intf_as, 0);
+
+ /* add the interface to the uac function */
+ rt_usbd_function_add_interface(func, intf_ac);
+ rt_usbd_function_add_interface(func, intf_as);
+
+ return func;
+}
+
+int audio_speaker_init(void)
+{
+ rt_thread_t speaker_tid;
+ speaker.event = rt_event_create("speaker_event", RT_IPC_FLAG_FIFO);
+
+ speaker_tid = rt_thread_create("speaker_thread",
+ speaker_entry, RT_NULL,
+ 1024,
+ 5, 10);
+
+ if (speaker_tid != RT_NULL)
+ rt_thread_startup(speaker_tid);
+ return RT_EOK;
+}
+INIT_COMPONENT_EXPORT(audio_speaker_init);
+
+/*
+ * register uac class
+ */
+static struct udclass uac_speaker_class =
+{
+ .rt_usbd_function_create = rt_usbd_function_uac_speaker_create
+};
+
+int rt_usbd_uac_speaker_class_register(void)
+{
+ rt_usbd_class_register(&uac_speaker_class);
+ return 0;
+}
+INIT_PREV_EXPORT(rt_usbd_uac_speaker_class_register);
diff --git a/components/drivers/usb/usbdevice/class/ecm.c b/components/drivers/usb/usbdevice/class/ecm.c
index cda1a8bf1b..b98b5007f4 100644
--- a/components/drivers/usb/usbdevice/class/ecm.c
+++ b/components/drivers/usb/usbdevice/class/ecm.c
@@ -6,17 +6,16 @@
* Change Logs:
* Date Author Notes
* 2017-11-19 ZYH first version
+ * 2019-06-10 ZYH fix hotplug
*/
#include <rtdevice.h>
+#ifdef RT_USB_DEVICE_ECM
#include "cdc.h"
-
-#ifdef ECM_DEBUG
-#define ECM_PRINTF rt_kprintf("[ECM] "); rt_kprintf
-#else
-#define ECM_PRINTF(...)
-#endif /* ECM_DEBUG */
+#define DBG_LEVEL DBG_WARNING
+#define DBG_SECTION_NAME "ECM"
+#include <rtdbg.h>
/* RT-Thread LWIP ethernet interface */
#include <netif/ethernetif.h>
@@ -216,15 +215,21 @@ static rt_err_t _cdc_send_notifi(ufunction_t func,ucdc_notification_code_t notif
return RT_EOK;
}
+
static rt_err_t _ecm_set_eth_packet_filter(ufunction_t func, ureq_t setup)
{
rt_ecm_eth_t _ecm_eth = (rt_ecm_eth_t)func->user_data;
dcd_ep0_send_status(func->device->dcd);
-
/* send link up. */
eth_device_linkchange(&_ecm_eth->parent, RT_TRUE);
- _cdc_send_notifi(func,UCDC_NOTIFI_NETWORK_CONNECTION,1,0);
+ _cdc_send_notifi(func, UCDC_NOTIFI_NETWORK_CONNECTION, 1, 0);
+
+#ifdef LWIP_USING_DHCPD
+ extern void dhcpd_start(const char *netif_name);
+ dhcpd_start("u0");
+#endif
+
return RT_EOK;
}
/**
@@ -243,10 +248,11 @@ static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
switch(setup->bRequest)
{
case CDC_SET_ETH_PACKET_FILTER:
+ LOG_D("CDC_SET_ETH_PACKET_FILTER");
_ecm_set_eth_packet_filter(func, setup);
break;
default:
- rt_kprintf("setup->bRequest:0x%02X",setup->bRequest);
+ LOG_E("Unknow setup->bRequest: 0x%02X", setup->bRequest);
break;
}
return RT_EOK;
@@ -402,22 +408,26 @@ rt_err_t rt_ecm_eth_tx(rt_device_t dev, struct pbuf* p)
if(!ecm_eth_dev->parent.link_status)
{
- ECM_PRINTF("linkdown, drop pkg\r\n");
+ LOG_D("linkdown, drop pkg");
return RT_EOK;
}
- // RT_ASSERT(p->tot_len < USB_ETH_MTU);
if(p->tot_len > USB_ETH_MTU)
{
- ECM_PRINTF("RNDIS MTU is:%d, but the send packet size is %d\r\n",
+ LOG_W("ECM MTU is:%d, but the send packet size is %d",
USB_ETH_MTU, p->tot_len);
p->tot_len = USB_ETH_MTU;
}
- result = rt_sem_take(&ecm_eth_dev->tx_buffer_free, RT_WAITING_FOREVER);
+
+ result = rt_sem_take(&device->tx_buffer_free, rt_tick_from_millisecond(1000));
if(result != RT_EOK)
{
+ LOG_W("wait for buffer free timeout");
+ /* if cost 1s to wait send done it said that connection is close . drop it */
+ rt_sem_release(&device->tx_buffer_free);
return result;
}
+
pbuffer = (char *)&ecm_eth_dev->tx_buffer;
for (q = p; q != NULL; q = q->next)
{
@@ -461,8 +471,13 @@ static rt_err_t _function_enable(ufunction_t func)
{
cdc_eps_t eps;
rt_ecm_eth_t ecm_device = (rt_ecm_eth_t)func->user_data;
+
+ LOG_D("plugged in");
+
eps = (cdc_eps_t)&ecm_device->eps;
eps->ep_out->buffer = ecm_device->rx_pool;
+
+ /* reset eth rx tx */
ecm_device->rx_size = 0;
ecm_device->rx_offset = 0;
@@ -482,7 +497,14 @@ static rt_err_t _function_enable(ufunction_t func)
*/
static rt_err_t _function_disable(ufunction_t func)
{
+ LOG_D("plugged out");
+
eth_device_linkchange(&((rt_ecm_eth_t)func->user_data)->parent, RT_FALSE);
+
+ /* reset eth rx tx */
+ ((rt_ecm_eth_t)func->user_data)->rx_size = 0;
+ ((rt_ecm_eth_t)func->user_data)->rx_offset = 0;
+
return RT_EOK;
}
@@ -643,3 +665,5 @@ int rt_usbd_ecm_class_register(void)
return 0;
}
INIT_PREV_EXPORT(rt_usbd_ecm_class_register);
+
+#endif /* RT_USB_DEVICE_ECM */
diff --git a/components/drivers/usb/usbdevice/class/hid.c b/components/drivers/usb/usbdevice/class/hid.c
index aea6d802d1..a9b2e62d70 100644
--- a/components/drivers/usb/usbdevice/class/hid.c
+++ b/components/drivers/usb/usbdevice/class/hid.c
@@ -372,7 +372,7 @@ const static char* _ustring[] =
"Interface",
};
-static void dump_data(uint8_t *data, rt_size_t size)
+static void dump_data(rt_uint8_t *data, rt_size_t size)
{
rt_size_t i;
for (i = 0; i < size; i++)
diff --git a/components/drivers/usb/usbdevice/class/rndis.c b/components/drivers/usb/usbdevice/class/rndis.c
index 8996b728e6..93e0ba6ae2 100644
--- a/components/drivers/usb/usbdevice/class/rndis.c
+++ b/components/drivers/usb/usbdevice/class/rndis.c
@@ -13,21 +13,21 @@
* 2013-07-09 aozima support respone chain list.
* 2013-07-18 aozima re-initial respone chain list when RNDIS restart.
* 2017-11-25 ZYH fix it and add OS descriptor
+ * 2019-06-10 ZYH fix hot plug and delay linkup
*/
#include <rtdevice.h>
+#ifdef RT_USB_DEVICE_RNDIS
#include "cdc.h"
#include "rndis.h"
#include "ndis.h"
-//#define RNDIS_DEBUG
-//#define RNDIS_DELAY_LINK_UP
+/* define RNDIS_DELAY_LINK_UP by menuconfig for delay linkup */
+
+#define DBG_LEVEL DBG_WARNING
+#define DBG_SECTION_NAME "RNDIS"
+#include <rtdbg.h>
-#ifdef RNDIS_DEBUG
-#define RNDIS_PRINTF rt_kprintf("[RNDIS] "); rt_kprintf
-#else
-#define RNDIS_PRINTF(...)
-#endif /* RNDIS_DEBUG */
/* RT-Thread LWIP ethernet interface */
#include <netif/ethernetif.h>
@@ -314,7 +314,7 @@ static rt_err_t _rndis_init_response(ufunction_t func, rndis_init_msg_t msg)
if( (response == RT_NULL) || (resp == RT_NULL) )
{
- RNDIS_PRINTF("%d: no memory!\r\n", __LINE__);
+ LOG_E("%s,%d: no memory!", __func__, __LINE__);
if(response != RT_NULL)
rt_free(response);
@@ -360,7 +360,7 @@ static rndis_query_cmplt_t _create_resp(rt_size_t size)
if(resp == RT_NULL)
{
- RNDIS_PRINTF("%d: no memory!\r\n", __LINE__);
+ LOG_E("%s,%d: no memory!", __func__, __LINE__);
return RT_NULL;
}
@@ -421,7 +421,7 @@ static rt_err_t _rndis_query_response(ufunction_t func,rndis_query_msg_t msg)
case OID_GEN_LINK_SPEED:
resp = _create_resp(4);
if(resp == RT_NULL) break;
- _set_resp(resp, func->device->dcd->device_is_hs ? (480UL * 1000 *1000) : (12UL * 1000 * 1000) / 100);
+ _set_resp(resp, (func->device->dcd->device_is_hs ? (480UL * 1000 *1000) : (12UL * 1000 * 1000)) / 100);
break;
case OID_GEN_MEDIA_CONNECT_STATUS:
@@ -510,7 +510,7 @@ static rt_err_t _rndis_query_response(ufunction_t func,rndis_query_msg_t msg)
break;
default:
- RNDIS_PRINTF("OID %X\n", msg->Oid);
+ LOG_W("Not support OID %X", msg->Oid);
ret = -RT_ERROR;
break;
}
@@ -518,7 +518,7 @@ static rt_err_t _rndis_query_response(ufunction_t func,rndis_query_msg_t msg)
response = rt_malloc(sizeof(struct rt_rndis_response));
if( (response == RT_NULL) || (resp == RT_NULL) )
{
- RNDIS_PRINTF("%d: no memory!\r\n", __LINE__);
+ LOG_E("%s,%d: no memory!", __func__, __LINE__);
if(response != RT_NULL)
rt_free(response);
@@ -557,7 +557,7 @@ static rt_err_t _rndis_set_response(ufunction_t func,rndis_set_msg_t msg)
if( (response == RT_NULL) || (resp == RT_NULL) )
{
- RNDIS_PRINTF("%d: no memory!\r\n", __LINE__);
+ LOG_E("%s,%d: no memory!", __func__, __LINE__);
if(response != RT_NULL)
rt_free(response);
@@ -577,8 +577,10 @@ static rt_err_t _rndis_set_response(ufunction_t func,rndis_set_msg_t msg)
case OID_GEN_CURRENT_PACKET_FILTER:
oid_packet_filter = *((rt_uint32_t *)((rt_uint8_t *)&(msg->RequestId) + \
msg->InformationBufferOffset));
+ /* TODO: make complier happy */
oid_packet_filter = oid_packet_filter;
- RNDIS_PRINTF("OID_GEN_CURRENT_PACKET_FILTER\r\n");
+
+ LOG_D("OID_GEN_CURRENT_PACKET_FILTER");
#ifdef RNDIS_DELAY_LINK_UP
/* link up. */
@@ -592,6 +594,7 @@ static rt_err_t _rndis_set_response(ufunction_t func,rndis_set_msg_t msg)
break;
default:
+ LOG_W("Unknow rndis set 0x%02X", msg->Oid);
resp->Status = RNDIS_STATUS_FAILURE;
return RT_EOK;
}
@@ -609,6 +612,56 @@ static rt_err_t _rndis_set_response(ufunction_t func,rndis_set_msg_t msg)
return RT_EOK;
}
+static rt_err_t _rndis_reset_response(ufunction_t func,rndis_set_msg_t msg)
+{
+ struct rndis_reset_cmplt * resp;
+ struct rt_rndis_response * response;
+
+ response = rt_malloc(sizeof(struct rt_rndis_response));
+ resp = rt_malloc(sizeof(struct rndis_reset_cmplt));
+
+ if( (response == RT_NULL) || (resp == RT_NULL) )
+ {
+ LOG_E("%s,%d: no memory!", __func__, __LINE__);
+
+ if(response != RT_NULL)
+ rt_free(response);
+
+ if(resp != RT_NULL)
+ rt_free(resp);
+
+ return -RT_ENOMEM;
+ }
+
+ /* reset packet filter */
+
+ oid_packet_filter = 0x0000000;
+
+ /* link down eth */
+
+ eth_device_linkchange(&((rt_rndis_eth_t)func->user_data)->parent, RT_FALSE);
+
+ /* reset eth rx tx */
+ ((rt_rndis_eth_t)func->user_data)->rx_frist = RT_TRUE;
+ ((rt_rndis_eth_t)func->user_data)->rx_flag = RT_FALSE;
+
+
+ resp->MessageType = REMOTE_NDIS_RESET_CMPLT;
+ resp->MessageLength = sizeof(struct rndis_reset_cmplt);
+ resp->Status = RNDIS_STATUS_SUCCESS;
+ resp->AddressingReset = 1;
+
+ response->buffer = resp;
+
+ {
+ rt_base_t level = rt_hw_interrupt_disable();
+ rt_list_insert_before(&((rt_rndis_eth_t)func->user_data)->response_list, &response->list);
+ rt_hw_interrupt_enable(level);
+ }
+
+ return RT_EOK;
+}
+
static rt_err_t _rndis_keepalive_response(ufunction_t func,rndis_keepalive_msg_t msg)
{
rndis_keepalive_cmplt_t resp;
@@ -619,7 +672,7 @@ static rt_err_t _rndis_keepalive_response(ufunction_t func,rndis_keepalive_msg_t
if( (response == RT_NULL) || (resp == RT_NULL) )
{
- RNDIS_PRINTF("%d: no memory!\r\n", __LINE__);
+ LOG_E("%s,%d: no memory!", __func__, __LINE__);
if(response != RT_NULL)
rt_free(response);
@@ -652,34 +705,42 @@ static rt_err_t _rndis_msg_parser(ufunction_t func, rt_uint8_t *msg)
switch (((rndis_gen_msg_t) msg)->MessageType)
{
case REMOTE_NDIS_INITIALIZE_MSG:
+ LOG_D("REMOTE_NDIS_INITIALIZE_MSG");
ret = _rndis_init_response(func, (rndis_init_msg_t) msg);
break;
case REMOTE_NDIS_HALT_MSG:
- RNDIS_PRINTF("halt\n");
+ LOG_D("REMOTE_NDIS_HALT_MSG");
/* link down. */
eth_device_linkchange(&((rt_rndis_eth_t)func->user_data)->parent, RT_FALSE);
+
+ /* reset eth rx tx */
+ ((rt_rndis_eth_t)func->user_data)->rx_frist = RT_TRUE;
+ ((rt_rndis_eth_t)func->user_data)->rx_flag = RT_FALSE;
break;
case REMOTE_NDIS_QUERY_MSG:
+ LOG_D("REMOTE_NDIS_QUERY_MSG");
ret = _rndis_query_response(func,(rndis_query_msg_t) msg);
break;
case REMOTE_NDIS_SET_MSG:
+ LOG_D("REMOTE_NDIS_SET_MSG");
ret = _rndis_set_response(func,(rndis_set_msg_t) msg);
- RNDIS_PRINTF("set\n");
break;
case REMOTE_NDIS_RESET_MSG:
- RNDIS_PRINTF("reset\n");
+ LOG_D("REMOTE_NDIS_RESET_MSG");
+ ret = _rndis_reset_response(func,(rndis_set_msg_t) msg);
break;
case REMOTE_NDIS_KEEPALIVE_MSG:
+ LOG_D("REMOTE_NDIS_KEEPALIVE_MSG");
ret = _rndis_keepalive_response(func,(rndis_keepalive_msg_t) msg);
break;
default:
- RNDIS_PRINTF("msg %X\n", ((rndis_gen_msg_t) msg)->MessageType);
+ LOG_W("not support RNDIS msg %X", ((rndis_gen_msg_t) msg)->MessageType);
ret = -RT_ERROR;
break;
}
@@ -718,7 +779,7 @@ static rt_err_t _rndis_get_encapsulated_response(ufunction_t func, ureq_t setup)
if(rt_list_isempty(&((rt_rndis_eth_t)func->user_data)->response_list))
{
- RNDIS_PRINTF("response_list is empty!\r\n");
+ LOG_D("response_list is empty!");
((rt_rndis_eth_t)func->user_data)->need_notify = RT_TRUE;
return RT_EOK;
}
@@ -741,7 +802,7 @@ static rt_err_t _rndis_get_encapsulated_response(ufunction_t func, ureq_t setup)
{
rt_uint32_t * data;
- RNDIS_PRINTF("auto append next response!\r\n");
+ LOG_I("auto append next response!");
data = (rt_uint32_t *)((rt_rndis_eth_t)func->user_data)->eps.ep_cmd->buffer;
data[0] = RESPONSE_AVAILABLE;
data[1] = 0;
@@ -758,55 +819,6 @@ static rt_err_t _rndis_get_encapsulated_response(ufunction_t func, ureq_t setup)
return RT_EOK;
}
-#ifdef RNDIS_DELAY_LINK_UP
-/**
- * This function will set rndis connect status.
- *
- * @param device the usb device object.
- * @param status the connect status.
- *
- * @return RT_EOK on successful.
- */
-static rt_err_t _rndis_indicate_status_msg(ufunction_t func, rt_uint32_t status)
-{
- rndis_indicate_status_msg_t resp;
- struct rt_rndis_response * response;
-
- response = rt_malloc(sizeof(struct rt_rndis_response));
- resp = rt_malloc(sizeof(struct rndis_indicate_status_msg));
-
- if( (response == RT_NULL) || (resp == RT_NULL) )
- {
- RNDIS_PRINTF("%d: no memory!\r\n", __LINE__);
-
- if(response != RT_NULL)
- rt_free(response);
-
- if(resp != RT_NULL)
- rt_free(resp);
-
- return -RT_ENOMEM;
- }
-
- resp->MessageType = REMOTE_NDIS_INDICATE_STATUS_MSG;
- resp->MessageLength = 20; /* sizeof(struct rndis_indicate_status_msg) */
- resp->Status = status;
- resp->StatusBufferLength = 0;
- resp->StatusBufferOffset = 0;
-
- response->buffer = resp;
- {
- rt_base_t level = rt_hw_interrupt_disable();
- rt_list_insert_before(&((rt_rndis_eth_t)func->user_data)->response_list, &response->list);
- rt_hw_interrupt_enable(level);
- }
-
- _rndis_response_available(func);
-
- return RT_EOK;
-}
-#endif /* RNDIS_DELAY_LINK_UP */
-
/**
* This function will handle rndis interface request.
*
@@ -831,7 +843,7 @@ static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
break;
default:
- RNDIS_PRINTF("unkown setup->request!\r\n");
+ LOG_W("unkown setup->request 0x%02X !", setup->bRequest);
break;
}
@@ -933,6 +945,8 @@ static rt_err_t _function_enable(ufunction_t func)
{
cdc_eps_t eps;
+ LOG_I("plugged in");
+
eps = (cdc_eps_t)&((rt_rndis_eth_t)func->user_data)->eps;
eps->ep_in->buffer = ((rt_rndis_eth_t)func->user_data)->tx_pool;
eps->ep_out->buffer = ((rt_rndis_eth_t)func->user_data)->rx_pool;
@@ -945,6 +959,7 @@ static rt_err_t _function_enable(ufunction_t func)
((rt_rndis_eth_t)func->user_data)->rx_flag = RT_FALSE;
((rt_rndis_eth_t)func->user_data)->rx_frist = RT_TRUE;
+ // eth_device_ready(&(((rt_rndis_eth_t)func->user_data)->parent));
#ifdef RNDIS_DELAY_LINK_UP
/* stop link up timer. */
@@ -981,7 +996,7 @@ static rt_err_t _function_enable(ufunction_t func)
*/
static rt_err_t _function_disable(ufunction_t func)
{
- RNDIS_PRINTF("plugged out\n");
+ LOG_I("plugged out");
#ifdef RNDIS_DELAY_LINK_UP
/* stop link up timer. */
@@ -996,7 +1011,7 @@ static rt_err_t _function_disable(ufunction_t func)
while(!rt_list_isempty(&((rt_rndis_eth_t)func->user_data)->response_list))
{
response = (struct rt_rndis_response *)((rt_rndis_eth_t)func->user_data)->response_list.next;
- RNDIS_PRINTF("remove resp chain list!\r\n");
+ LOG_D("remove resp chain list!");
rt_list_remove(&response->list);
rt_free((void *)response->buffer);
@@ -1007,8 +1022,14 @@ static rt_err_t _function_disable(ufunction_t func)
rt_hw_interrupt_enable(level);
}
+
/* link down. */
eth_device_linkchange(&((rt_rndis_eth_t)func->user_data)->parent, RT_FALSE);
+
+ /* reset eth rx tx */
+ ((rt_rndis_eth_t)func->user_data)->rx_frist = RT_TRUE;
+ ((rt_rndis_eth_t)func->user_data)->rx_flag = RT_FALSE;
+
return RT_EOK;
}
@@ -1141,22 +1162,25 @@ rt_err_t rt_rndis_eth_tx(rt_device_t dev, struct pbuf* p)
if(!device->parent.link_status)
{
- RNDIS_PRINTF("linkdown, drop pkg\r\n");
+ LOG_I("linkdown, drop pkg");
return RT_EOK;
}
- RT_ASSERT(p->tot_len < sizeof(device->tx_buffer));
+ //RT_ASSERT(p->tot_len < sizeof(device->tx_buffer));
if(p->tot_len > sizeof(device->tx_buffer))
{
- RNDIS_PRINTF("RNDIS MTU is:%d, but the send packet size is %d\r\n",
+ LOG_W("RNDIS MTU is:%d, but the send packet size is %d",
sizeof(device->tx_buffer), p->tot_len);
p->tot_len = sizeof(device->tx_buffer);
}
/* wait for buffer free. */
- result = rt_sem_take(&device->tx_buffer_free, RT_WAITING_FOREVER);
+ result = rt_sem_take(&device->tx_buffer_free, rt_tick_from_millisecond(1000));
if(result != RT_EOK)
{
+ LOG_W("wait for buffer free timeout");
+ /* if cost 1s to wait send done it said that connection is close . drop it */
+ rt_sem_release(&device->tx_buffer_free);
return result;
}
@@ -1215,11 +1239,59 @@ const static struct rt_device_ops rndis_device_ops =
#endif /* RT_USING_LWIP */
#ifdef RNDIS_DELAY_LINK_UP
+
+/**
+ * This function will set rndis connect status.
+ *
+ * @param device the usb device object.
+ * @param status the connect status.
+ *
+ * @return RT_EOK on successful.
+ */
+static rt_err_t _rndis_indicate_status_msg(ufunction_t func, rt_uint32_t status)
+{
+ rndis_indicate_status_msg_t resp;
+ struct rt_rndis_response * response;
+
+ response = rt_malloc(sizeof(struct rt_rndis_response));
+ resp = rt_malloc(sizeof(struct rndis_indicate_status_msg));
+
+ if( (response == RT_NULL) || (resp == RT_NULL) )
+ {
+ LOG_E("%s,%d: no memory!", __func__, __LINE__);
+
+ if(response != RT_NULL)
+ rt_free(response);
+
+ if(resp != RT_NULL)
+ rt_free(resp);
+
+ return -RT_ENOMEM;
+ }
+
+ resp->MessageType = REMOTE_NDIS_INDICATE_STATUS_MSG;
+ resp->MessageLength = 20; /* sizeof(struct rndis_indicate_status_msg) */
+ resp->Status = status;
+ resp->StatusBufferLength = 0;
+ resp->StatusBufferOffset = 0;
+
+ response->buffer = resp;
+ {
+ rt_base_t level = rt_hw_interrupt_disable();
+ rt_list_insert_before(&((rt_rndis_eth_t)func->user_data)->response_list, &response->list);
+ rt_hw_interrupt_enable(level);
+ }
+
+ _rndis_response_available(func);
+
+ return RT_EOK;
+}
+
/* the delay linkup timer handler. */
static void timer_timeout(void* parameter)
{
- RNDIS_PRINTF("delay link up!\r\n");
- _rndis_indicate_status_msg(((rt_rndis_eth_t)parameter)->parent.parent.user_data,
+ LOG_I("delay link up!");
+ _rndis_indicate_status_msg(((rt_rndis_eth_t)parameter)->func,
RNDIS_STATUS_MEDIA_CONNECT);
eth_device_linkchange(&((rt_rndis_eth_t)parameter)->parent, RT_TRUE);
}
@@ -1319,7 +1391,7 @@ ufunction_t rt_usbd_function_rndis_create(udevice_t device)
timer_timeout,
_rndis,
RT_TICK_PER_SECOND * 2,
- RT_TIMER_FLAG_ONE_SHOT);
+ RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
#endif /* RNDIS_DELAY_LINK_UP */
/* OUI 00-00-00, only for test. */
@@ -1372,4 +1444,6 @@ int rt_usbd_rndis_class_register(void)
rt_usbd_class_register(&rndis_class);
return 0;
}
-INIT_PREV_EXPORT(rt_usbd_rndis_class_register); \ No newline at end of file
+INIT_PREV_EXPORT(rt_usbd_rndis_class_register);
+
+#endif /* RT_USB_DEVICE_RNDIS */
diff --git a/components/drivers/usb/usbdevice/class/rndis.h b/components/drivers/usb/usbdevice/class/rndis.h
index 000ea62baf..fcdd249984 100644
--- a/components/drivers/usb/usbdevice/class/rndis.h
+++ b/components/drivers/usb/usbdevice/class/rndis.h
@@ -180,6 +180,15 @@ struct rndis_reset_msg
rt_uint32_t Reserved;
};
+/* Remote NDIS Soft Reset Response */
+struct rndis_reset_cmplt
+{
+ rt_uint32_t MessageType;
+ rt_uint32_t MessageLength;
+ rt_uint32_t Status;
+ rt_uint32_t AddressingReset;
+};
+
/* Remote NDIS Indicate Status Message */
struct rndis_indicate_status_msg
{
diff --git a/components/drivers/usb/usbdevice/core/core.c b/components/drivers/usb/usbdevice/core/core.c
index 4e3a3e9280..584556303f 100644
--- a/components/drivers/usb/usbdevice/core/core.c
+++ b/components/drivers/usb/usbdevice/core/core.c
@@ -237,7 +237,8 @@ static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
{
rt_uint8_t value;
uintf_t intf;
-
+ ufunction_t func;
+
/* parameter check */
RT_ASSERT(device != RT_NULL);
RT_ASSERT(setup != RT_NULL);
@@ -251,12 +252,17 @@ static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
}
/* find the specified interface and its alternate setting */
- intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
+ intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
value = intf->curr_setting->intf_desc->bAlternateSetting;
/* send the interface alternate setting to endpoint 0*/
rt_usbd_ep0_write(device, &value, 1);
+ if (intf->handler)
+ {
+ intf->handler(func, setup);
+ }
+
return RT_EOK;
}
@@ -270,6 +276,7 @@ static rt_err_t _get_interface(struct udevice* device, ureq_t setup)
*/
static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
{
+ ufunction_t func;
uintf_t intf;
uep_t ep;
struct rt_list_node* i;
@@ -288,7 +295,7 @@ static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
}
/* find the specified interface */
- intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, RT_NULL);
+ intf = rt_usbd_find_interface(device, setup->wIndex & 0xFF, &func);
/* set alternate setting to the interface */
rt_usbd_set_altsetting(intf, setup->wValue & 0xFF);
@@ -303,6 +310,11 @@ static rt_err_t _set_interface(struct udevice* device, ureq_t setup)
}
dcd_ep0_send_status(device->dcd);
+ if (intf->handler)
+ {
+ intf->handler(func, setup);
+ }
+
return RT_EOK;
}
@@ -995,7 +1007,7 @@ udevice_t rt_usbd_device_new(void)
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_new\n"));
/* allocate memory for the object */
- udevice = rt_malloc(sizeof(struct udevice));
+ udevice = (udevice_t)rt_malloc(sizeof(struct udevice));
if(udevice == RT_NULL)
{
rt_kprintf("alloc memery failed\n");
@@ -1109,7 +1121,7 @@ uconfig_t rt_usbd_config_new(void)
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_config_new\n"));
/* allocate memory for the object */
- cfg = rt_malloc(sizeof(struct uconfig));
+ cfg = (uconfig_t)rt_malloc(sizeof(struct uconfig));
if(cfg == RT_NULL)
{
rt_kprintf("alloc memery failed\n");
@@ -1488,9 +1500,10 @@ uep_t rt_usbd_find_endpoint(udevice_t device, ufunction_t* pfunc, rt_uint8_t ep_
*/
rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
{
- struct rt_list_node *i, *j, *k;
+ struct rt_list_node *i, *j, *k, *m;
ufunction_t func;
uintf_t intf;
+ ualtsetting_t altsetting;
uep_t ep;
RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbd_device_add_config\n"));
@@ -1512,22 +1525,26 @@ rt_err_t rt_usbd_device_add_config(udevice_t device, uconfig_t cfg)
intf = (uintf_t)rt_list_entry(j, struct uinterface, list);
cfg->cfg_desc.bNumInterfaces++;
- /* allocate address for every endpoint in the interface alternate setting */
- for(k=intf->curr_setting->ep_list.next;
- k!=&intf->curr_setting->ep_list; k=k->next)
+ for(k=intf->setting_list.next; k!=&intf->setting_list;k=k->next)
{
- ep = (uep_t)rt_list_entry(k, struct uendpoint, list);
- if(rt_usbd_ep_assign(device, ep) != RT_EOK)
+ altsetting = (ualtsetting_t)rt_list_entry(k, struct ualtsetting, list);
+
+ /* allocate address for every endpoint in the interface alternate setting */
+ for(m=altsetting->ep_list.next; m!=&altsetting->ep_list; m=m->next)
{
- rt_kprintf("endpoint assign error\n");
+ ep = (uep_t)rt_list_entry(m, struct uendpoint, list);
+ if(rt_usbd_ep_assign(device, ep) != RT_EOK)
+ {
+ rt_kprintf("endpoint assign error\n");
+ }
}
- }
- /* construct complete configuration descriptor */
- rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG],
- (void*)intf->curr_setting->desc,
- intf->curr_setting->desc_size);
- cfg->cfg_desc.wTotalLength += intf->curr_setting->desc_size;
+ /* construct complete configuration descriptor */
+ rt_memcpy((void*)&cfg->cfg_desc.data[cfg->cfg_desc.wTotalLength - USB_DESC_LENGTH_CONFIG],
+ (void*)altsetting->desc,
+ altsetting->desc_size);
+ cfg->cfg_desc.wTotalLength += altsetting->desc_size;
+ }
}
}
@@ -2053,7 +2070,7 @@ rt_size_t rt_usbd_ep0_write(udevice_t device, void *buffer, rt_size_t size)
ep0 = &device->dcd->ep0;
ep0->request.size = size;
- ep0->request.buffer = buffer;
+ ep0->request.buffer = (rt_uint8_t *)buffer;
ep0->request.remain_size = size;
if(size >= ep0->id->maxpacket)
{
@@ -2079,7 +2096,7 @@ rt_size_t rt_usbd_ep0_read(udevice_t device, void *buffer, rt_size_t size,
RT_ASSERT(buffer != RT_NULL);
ep0 = &device->dcd->ep0;
- ep0->request.buffer = buffer;
+ ep0->request.buffer = (rt_uint8_t *)buffer;
ep0->request.remain_size = size;
ep0->rx_indicate = rx_ind;
if(size >= ep0->id->maxpacket)