Przeglądaj źródła

update: check class->hport validity

sakumisu 1 rok temu
rodzic
commit
755d067650

+ 24 - 4
class/audio/usbh_audio.c

@@ -57,7 +57,7 @@ static void usbh_audio_class_free(struct usbh_audio *audio_class)
 
 int usbh_audio_open(struct usbh_audio *audio_class, const char *name, uint32_t samp_freq)
 {
-    struct usb_setup_packet *setup = audio_class->hport->setup;
+    struct usb_setup_packet *setup;
     struct usb_endpoint_descriptor *ep_desc;
     uint8_t mult;
     uint16_t mps;
@@ -65,6 +65,11 @@ int usbh_audio_open(struct usbh_audio *audio_class, const char *name, uint32_t s
     uint8_t intf = 0xff;
     uint8_t altsetting = 1;
 
+    if (!audio_class || !audio_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = audio_class->hport->setup;
+
     if (audio_class->is_opened) {
         return 0;
     }
@@ -129,12 +134,17 @@ freq_found:
 
 int usbh_audio_close(struct usbh_audio *audio_class, const char *name)
 {
-    struct usb_setup_packet *setup = audio_class->hport->setup;
+    struct usb_setup_packet *setup;
     struct usb_endpoint_descriptor *ep_desc;
     int ret;
     uint8_t intf = 0xff;
     uint8_t altsetting = 1;
 
+    if (!audio_class || !audio_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = audio_class->hport->setup;
+
     for (size_t i = 0; i < audio_class->module_num; i++) {
         if (strcmp(name, audio_class->module[i].name) == 0) {
             intf = audio_class->module[i].data_intf;
@@ -172,12 +182,17 @@ int usbh_audio_close(struct usbh_audio *audio_class, const char *name)
 
 int usbh_audio_set_volume(struct usbh_audio *audio_class, const char *name, uint8_t ch, uint8_t volume)
 {
-    struct usb_setup_packet *setup = audio_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
     uint8_t intf = 0xff;
     uint8_t feature_id = 0xff;
     uint16_t volume_hex;
 
+    if (!audio_class || !audio_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = audio_class->hport->setup;
+
     for (size_t i = 0; i < audio_class->module_num; i++) {
         if (strcmp(name, audio_class->module[i].name) == 0) {
             intf = audio_class->ctrl_intf;
@@ -205,11 +220,16 @@ int usbh_audio_set_volume(struct usbh_audio *audio_class, const char *name, uint
 
 int usbh_audio_set_mute(struct usbh_audio *audio_class, const char *name, uint8_t ch, bool mute)
 {
-    struct usb_setup_packet *setup = audio_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
     uint8_t intf = 0xff;
     uint8_t feature_id = 0xff;
 
+    if (!audio_class || !audio_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = audio_class->hport->setup;
+
     for (size_t i = 0; i < audio_class->module_num; i++) {
         if (strcmp(name, audio_class->module[i].name) == 0) {
             intf = audio_class->ctrl_intf;

+ 18 - 3
class/cdc/usbh_cdc_acm.c

@@ -44,7 +44,12 @@ static void usbh_cdc_acm_class_free(struct usbh_cdc_acm *cdc_acm_class)
 
 int usbh_cdc_acm_set_line_coding(struct usbh_cdc_acm *cdc_acm_class, struct cdc_line_coding *line_coding)
 {
-    struct usb_setup_packet *setup = cdc_acm_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!cdc_acm_class || !cdc_acm_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cdc_acm_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
@@ -59,9 +64,14 @@ int usbh_cdc_acm_set_line_coding(struct usbh_cdc_acm *cdc_acm_class, struct cdc_
 
 int usbh_cdc_acm_get_line_coding(struct usbh_cdc_acm *cdc_acm_class, struct cdc_line_coding *line_coding)
 {
-    struct usb_setup_packet *setup = cdc_acm_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!cdc_acm_class || !cdc_acm_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cdc_acm_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
     setup->wValue = 0;
@@ -78,7 +88,12 @@ int usbh_cdc_acm_get_line_coding(struct usbh_cdc_acm *cdc_acm_class, struct cdc_
 
 int usbh_cdc_acm_set_line_state(struct usbh_cdc_acm *cdc_acm_class, bool dtr, bool rts)
 {
-    struct usb_setup_packet *setup = cdc_acm_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!cdc_acm_class || !cdc_acm_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cdc_acm_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;

+ 6 - 1
class/cdc/usbh_cdc_ecm.c

@@ -32,7 +32,12 @@ static struct usbh_cdc_ecm g_cdc_ecm_class;
 
 static int usbh_cdc_ecm_set_eth_packet_filter(struct usbh_cdc_ecm *cdc_ecm_class, uint16_t filter_value)
 {
-    struct usb_setup_packet *setup = cdc_ecm_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!cdc_ecm_class || !cdc_ecm_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cdc_ecm_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_SET_ETHERNET_PACKET_FILTER;

+ 6 - 1
class/cdc/usbh_cdc_ncm.c

@@ -33,9 +33,14 @@ static struct usbh_cdc_ncm g_cdc_ncm_class;
 
 static int usbh_cdc_ncm_get_ntb_parameters(struct usbh_cdc_ncm *cdc_ncm_class, struct cdc_ncm_ntb_parameters *param)
 {
-    struct usb_setup_packet *setup = cdc_ncm_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!cdc_ncm_class || !cdc_ncm_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cdc_ncm_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_GET_NTB_PARAMETERS;
     setup->wValue = 0;

+ 36 - 6
class/hid/usbh_hid.c

@@ -44,9 +44,14 @@ static void usbh_hid_class_free(struct usbh_hid *hid_class)
 
 static int usbh_hid_get_report_descriptor(struct usbh_hid *hid_class, uint8_t *buffer)
 {
-    struct usb_setup_packet *setup = hid_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!hid_class || !hid_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = hid_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
     setup->wValue = HID_DESCRIPTOR_TYPE_HID_REPORT << 8;
@@ -63,7 +68,12 @@ static int usbh_hid_get_report_descriptor(struct usbh_hid *hid_class, uint8_t *b
 
 int usbh_hid_set_idle(struct usbh_hid *hid_class, uint8_t report_id, uint8_t duration)
 {
-    struct usb_setup_packet *setup = hid_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!hid_class || !hid_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = hid_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = HID_REQUEST_SET_IDLE;
@@ -76,9 +86,14 @@ int usbh_hid_set_idle(struct usbh_hid *hid_class, uint8_t report_id, uint8_t dur
 
 int usbh_hid_get_idle(struct usbh_hid *hid_class, uint8_t *buffer)
 {
-    struct usb_setup_packet *setup = hid_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!hid_class || !hid_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = hid_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = HID_REQUEST_GET_IDLE;
     setup->wValue = 0;
@@ -95,7 +110,12 @@ int usbh_hid_get_idle(struct usbh_hid *hid_class, uint8_t *buffer)
 
 int usbh_hid_set_protocol(struct usbh_hid *hid_class, uint8_t protocol)
 {
-    struct usb_setup_packet *setup = hid_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!hid_class || !hid_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = hid_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = HID_REQUEST_SET_PROTOCOL;
@@ -108,7 +128,12 @@ int usbh_hid_set_protocol(struct usbh_hid *hid_class, uint8_t protocol)
 
 int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
 {
-    struct usb_setup_packet *setup = hid_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!hid_class || !hid_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = hid_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = HID_REQUEST_SET_REPORT;
@@ -121,7 +146,12 @@ int usbh_hid_set_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t
 
 int usbh_hid_get_report(struct usbh_hid *hid_class, uint8_t report_type, uint8_t report_id, uint8_t *buffer, uint32_t buflen)
 {
-    struct usb_setup_packet *setup = hid_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!hid_class || !hid_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = hid_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = HID_REQUEST_GET_REPORT;

+ 6 - 1
class/msc/usbh_msc.c

@@ -46,7 +46,12 @@ static void usbh_msc_class_free(struct usbh_msc *msc_class)
 
 static int usbh_msc_get_maxlun(struct usbh_msc *msc_class, uint8_t *buffer)
 {
-    struct usb_setup_packet *setup = msc_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!msc_class || !msc_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = msc_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = MSC_REQUEST_GET_MAX_LUN;

+ 12 - 2
class/vendor/net/usbh_asix.c

@@ -55,9 +55,14 @@ static int usbh_asix_read_cmd(struct usbh_asix *asix_class,
                               void *data,
                               uint16_t size)
 {
-    struct usb_setup_packet *setup = asix_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!asix_class || !asix_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = asix_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = cmd;
     setup->wValue = value;
@@ -80,7 +85,12 @@ static int usbh_asix_write_cmd(struct usbh_asix *asix_class,
                                void *data,
                                uint16_t size)
 {
-    struct usb_setup_packet *setup = asix_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!asix_class || !asix_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = asix_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = cmd;

+ 12 - 2
class/vendor/net/usbh_rtl8152.c

@@ -946,9 +946,14 @@ static int usbh_rtl8152_read_regs(struct usbh_rtl8152 *rtl8152_class,
                                   uint16_t size,
                                   void *data)
 {
-    struct usb_setup_packet *setup = rtl8152_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!rtl8152_class || !rtl8152_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = rtl8152_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = RTL8152_REQ_GET_REGS;
     setup->wValue = value;
@@ -970,7 +975,12 @@ static int usbh_rtl8152_write_regs(struct usbh_rtl8152 *rtl8152_class,
                                    uint16_t size,
                                    void *data)
 {
-    struct usb_setup_packet *setup = rtl8152_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!rtl8152_class || !rtl8152_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = rtl8152_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = RTL8152_REQ_SET_REGS;

+ 24 - 4
class/vendor/serial/usbh_ch34x.c

@@ -90,9 +90,14 @@ static int usbh_ch34x_get_baudrate_div(uint32_t baudrate, uint8_t *factor, uint8
 
 static int usbh_ch34x_get_version(struct usbh_ch34x *ch34x_class)
 {
-    struct usb_setup_packet *setup = ch34x_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!ch34x_class || !ch34x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ch34x_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = CH34X_READ_VERSION;
     setup->wValue = 0;
@@ -110,7 +115,12 @@ static int usbh_ch34x_get_version(struct usbh_ch34x *ch34x_class)
 
 static int usbh_ch34x_flow_ctrl(struct usbh_ch34x *ch34x_class)
 {
-    struct usb_setup_packet *setup = ch34x_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!ch34x_class || !ch34x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ch34x_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = CH34X_WRITE_REG;
@@ -123,12 +133,17 @@ static int usbh_ch34x_flow_ctrl(struct usbh_ch34x *ch34x_class)
 
 int usbh_ch34x_set_line_coding(struct usbh_ch34x *ch34x_class, struct cdc_line_coding *line_coding)
 {
-    struct usb_setup_packet *setup = ch34x_class->hport->setup;
+    struct usb_setup_packet *setup;
     uint16_t reg_value = 0;
     uint16_t value = 0;
     uint8_t factor = 0;
     uint8_t divisor = 0;
 
+    if (!ch34x_class || !ch34x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ch34x_class->hport->setup;
+
     memcpy((uint8_t *)&ch34x_class->line_coding, line_coding, sizeof(struct cdc_line_coding));
 
     /* refer to https://github.com/WCHSoftGroup/ch341ser_linux/blob/main/driver/ch341.c */
@@ -197,7 +212,12 @@ int usbh_ch34x_get_line_coding(struct usbh_ch34x *ch34x_class, struct cdc_line_c
 
 int usbh_ch34x_set_line_state(struct usbh_ch34x *ch34x_class, bool dtr, bool rts)
 {
-    struct usb_setup_packet *setup = ch34x_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!ch34x_class || !ch34x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ch34x_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = CH34X_MODEM_CTRL;

+ 36 - 6
class/vendor/serial/usbh_cp210x.c

@@ -42,7 +42,12 @@ static void usbh_cp210x_class_free(struct usbh_cp210x *cp210x_class)
 
 static int usbh_cp210x_enable(struct usbh_cp210x *cp210x_class)
 {
-    struct usb_setup_packet *setup = cp210x_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!cp210x_class || !cp210x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cp210x_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CP210X_IFC_ENABLE;
@@ -55,7 +60,12 @@ static int usbh_cp210x_enable(struct usbh_cp210x *cp210x_class)
 
 static int usbh_cp210x_set_flow(struct usbh_cp210x *cp210x_class)
 {
-    struct usb_setup_packet *setup = cp210x_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!cp210x_class || !cp210x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cp210x_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CP210X_SET_FLOW;
@@ -70,7 +80,12 @@ static int usbh_cp210x_set_flow(struct usbh_cp210x *cp210x_class)
 
 static int usbh_cp210x_set_chars(struct usbh_cp210x *cp210x_class)
 {
-    struct usb_setup_packet *setup = cp210x_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!cp210x_class || !cp210x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cp210x_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CP210X_SET_CHARS;
@@ -87,7 +102,12 @@ static int usbh_cp210x_set_chars(struct usbh_cp210x *cp210x_class)
 
 static int usbh_cp210x_set_baudrate(struct usbh_cp210x *cp210x_class, uint32_t baudrate)
 {
-    struct usb_setup_packet *setup = cp210x_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!cp210x_class || !cp210x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cp210x_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CP210X_SET_BAUDRATE;
@@ -101,9 +121,14 @@ static int usbh_cp210x_set_baudrate(struct usbh_cp210x *cp210x_class, uint32_t b
 
 static int usbh_cp210x_set_data_format(struct usbh_cp210x *cp210x_class, uint8_t databits, uint8_t parity, uint8_t stopbits)
 {
-    struct usb_setup_packet *setup = cp210x_class->hport->setup;
+    struct usb_setup_packet *setup;
     uint16_t value;
 
+    if (!cp210x_class || !cp210x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cp210x_class->hport->setup;
+
     value = ((databits & 0x0F) << 8) | ((parity & 0x0f) << 4) | ((stopbits & 0x03) << 0);
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_INTERFACE;
@@ -117,9 +142,14 @@ static int usbh_cp210x_set_data_format(struct usbh_cp210x *cp210x_class, uint8_t
 
 static int usbh_cp210x_set_mhs(struct usbh_cp210x *cp210x_class, uint8_t dtr, uint8_t rts, uint8_t dtr_mask, uint8_t rts_mask)
 {
-    struct usb_setup_packet *setup = cp210x_class->hport->setup;
+    struct usb_setup_packet *setup;
     uint16_t value;
 
+    if (!cp210x_class || !cp210x_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = cp210x_class->hport->setup;
+
     value = ((dtr & 0x01) << 0) | ((rts & 0x01) << 1) | ((dtr_mask & 0x01) << 8) | ((rts_mask & 0x01) << 9);
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_INTERFACE;

+ 43 - 8
class/vendor/serial/usbh_ftdi.c

@@ -71,7 +71,12 @@ static void usbh_ftdi_caculate_baudrate(uint32_t *itdf_divisor, uint32_t actual_
 
 int usbh_ftdi_reset(struct usbh_ftdi *ftdi_class)
 {
-    struct usb_setup_packet *setup = ftdi_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!ftdi_class || !ftdi_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ftdi_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = SIO_RESET_REQUEST;
@@ -84,7 +89,12 @@ int usbh_ftdi_reset(struct usbh_ftdi *ftdi_class)
 
 static int usbh_ftdi_set_modem(struct usbh_ftdi *ftdi_class, uint16_t value)
 {
-    struct usb_setup_packet *setup = ftdi_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!ftdi_class || !ftdi_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ftdi_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = SIO_SET_MODEM_CTRL_REQUEST;
@@ -97,11 +107,16 @@ static int usbh_ftdi_set_modem(struct usbh_ftdi *ftdi_class, uint16_t value)
 
 static int usbh_ftdi_set_baudrate(struct usbh_ftdi *ftdi_class, uint32_t baudrate)
 {
-    struct usb_setup_packet *setup = ftdi_class->hport->setup;
+    struct usb_setup_packet *setup;
     uint32_t itdf_divisor;
     uint16_t value;
     uint8_t baudrate_high;
 
+    if (!ftdi_class || !ftdi_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ftdi_class->hport->setup;
+
     usbh_ftdi_caculate_baudrate(&itdf_divisor, baudrate);
     value = itdf_divisor & 0xFFFF;
     baudrate_high = (itdf_divisor >> 16) & 0xff;
@@ -123,10 +138,15 @@ static int usbh_ftdi_set_data_format(struct usbh_ftdi *ftdi_class, uint8_t datab
      * D11-D12 		STOP_BIT_1=0, STOP_BIT_15=1, STOP_BIT_2=2
      * D14  		BREAK_OFF=0, BREAK_ON=1
      **/
+    struct usb_setup_packet *setup;
+    uint16_t value;
 
-    uint16_t value = ((isbreak & 0x01) << 14) | ((stopbits & 0x03) << 11) | ((parity & 0x0f) << 8) | (databits & 0x0f);
+    if (!ftdi_class || !ftdi_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ftdi_class->hport->setup;
 
-    struct usb_setup_packet *setup = ftdi_class->hport->setup;
+    value = ((isbreak & 0x01) << 14) | ((stopbits & 0x03) << 11) | ((parity & 0x0f) << 8) | (databits & 0x0f);
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = SIO_SET_DATA_REQUEST;
@@ -139,7 +159,12 @@ static int usbh_ftdi_set_data_format(struct usbh_ftdi *ftdi_class, uint8_t datab
 
 static int usbh_ftdi_set_latency_timer(struct usbh_ftdi *ftdi_class, uint16_t value)
 {
-    struct usb_setup_packet *setup = ftdi_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!ftdi_class || !ftdi_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ftdi_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = SIO_SET_LATENCY_TIMER_REQUEST;
@@ -152,7 +177,12 @@ static int usbh_ftdi_set_latency_timer(struct usbh_ftdi *ftdi_class, uint16_t va
 
 static int usbh_ftdi_set_flow_ctrl(struct usbh_ftdi *ftdi_class, uint16_t value)
 {
-    struct usb_setup_packet *setup = ftdi_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!ftdi_class || !ftdi_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ftdi_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = SIO_SET_FLOW_CTRL_REQUEST;
@@ -165,9 +195,14 @@ static int usbh_ftdi_set_flow_ctrl(struct usbh_ftdi *ftdi_class, uint16_t value)
 
 static int usbh_ftdi_read_modem_status(struct usbh_ftdi *ftdi_class)
 {
-    struct usb_setup_packet *setup = ftdi_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!ftdi_class || !ftdi_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = ftdi_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = SIO_POLL_MODEM_STATUS_REQUEST;
     setup->wValue = 0x0000;

+ 24 - 4
class/vendor/serial/usbh_pl2303.c

@@ -131,7 +131,12 @@ static int usbh_pl2303_do(struct usbh_pl2303 *pl2303_class,
                           uint8_t req_type, uint8_t request, uint16_t value, uint16_t index,
                           uint16_t length)
 {
-    struct usb_setup_packet *setup = pl2303_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!pl2303_class || !pl2303_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = pl2303_class->hport->setup;
 
     setup->bmRequestType = req_type;
     setup->bRequest = request;
@@ -144,7 +149,12 @@ static int usbh_pl2303_do(struct usbh_pl2303 *pl2303_class,
 
 int usbh_pl2303_set_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_line_coding *line_coding)
 {
-    struct usb_setup_packet *setup = pl2303_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!pl2303_class || !pl2303_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = pl2303_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
@@ -159,9 +169,14 @@ int usbh_pl2303_set_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_lin
 
 int usbh_pl2303_get_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_line_coding *line_coding)
 {
-    struct usb_setup_packet *setup = pl2303_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!pl2303_class || !pl2303_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = pl2303_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
     setup->wValue = 0;
@@ -178,7 +193,12 @@ int usbh_pl2303_get_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_lin
 
 int usbh_pl2303_set_line_state(struct usbh_pl2303 *pl2303_class, bool dtr, bool rts)
 {
-    struct usb_setup_packet *setup = pl2303_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!pl2303_class || !pl2303_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = pl2303_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;

+ 24 - 4
class/video/usbh_video.c

@@ -59,10 +59,15 @@ static void usbh_video_class_free(struct usbh_video *video_class)
 
 int usbh_video_get(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len)
 {
-    struct usb_setup_packet *setup = video_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
     uint8_t retry;
 
+    if (!video_class || !video_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = video_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = request;
     setup->wValue = cs << 8;
@@ -91,9 +96,14 @@ int usbh_video_get(struct usbh_video *video_class, uint8_t request, uint8_t intf
 
 int usbh_video_set(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len)
 {
-    struct usb_setup_packet *setup = video_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret;
 
+    if (!video_class || !video_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = video_class->hport->setup;
+
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
     setup->bRequest = request;
     setup->wValue = cs << 8;
@@ -136,7 +146,7 @@ int usbh_video_open(struct usbh_video *video_class,
                     uint16_t wHeight,
                     uint8_t altsetting)
 {
-    struct usb_setup_packet *setup = video_class->hport->setup;
+    struct usb_setup_packet *setup;
     struct usb_endpoint_descriptor *ep_desc;
     uint8_t mult;
     uint16_t mps;
@@ -146,6 +156,11 @@ int usbh_video_open(struct usbh_video *video_class,
     uint8_t frameidx = 0;
     uint8_t step;
 
+    if (!video_class || !video_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = video_class->hport->setup;
+
     if (video_class->is_opened) {
         return 0;
     }
@@ -265,9 +280,14 @@ errout:
 
 int usbh_video_close(struct usbh_video *video_class)
 {
-    struct usb_setup_packet *setup = video_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret = 0;
 
+    if (!video_class || !video_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = video_class->hport->setup;
+
     USB_LOG_INFO("Close video device\r\n");
 
     video_class->is_opened = false;

+ 6 - 1
class/wireless/usbh_bluetooth.c

@@ -234,7 +234,12 @@ delete :
 static int usbh_bluetooth_hci_cmd(uint8_t *buffer, uint32_t buflen)
 {
     struct usbh_bluetooth *bluetooth_class = &g_bluetooth_class;
-    struct usb_setup_packet *setup = bluetooth_class->hport->setup;
+    struct usb_setup_packet *setup;
+
+    if (!bluetooth_class || !bluetooth_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = bluetooth_class->hport->setup;
 
     setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
     setup->bRequest = 0x00;

+ 24 - 4
class/wireless/usbh_rndis.c

@@ -40,11 +40,16 @@ static int usbh_rndis_get_notification(struct usbh_rndis *rndis_class)
 
 static int usbh_rndis_init_msg_transfer(struct usbh_rndis *rndis_class)
 {
-    struct usb_setup_packet *setup = rndis_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret = 0;
     rndis_initialize_msg_t *cmd;
     rndis_initialize_cmplt_t *resp;
 
+    if (!rndis_class || !rndis_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = rndis_class->hport->setup;
+
     cmd = (rndis_initialize_msg_t *)g_rndis_buf;
 
     cmd->MessageType = REMOTE_NDIS_INITIALIZE_MSG;
@@ -92,11 +97,16 @@ static int usbh_rndis_init_msg_transfer(struct usbh_rndis *rndis_class)
 
 int usbh_rndis_query_msg_transfer(struct usbh_rndis *rndis_class, uint32_t oid, uint32_t query_len, uint8_t *info, uint32_t *info_len)
 {
-    struct usb_setup_packet *setup = rndis_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret = 0;
     rndis_query_msg_t *cmd;
     rndis_query_cmplt_t *resp;
 
+    if (!rndis_class || !rndis_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = rndis_class->hport->setup;
+
     cmd = (rndis_query_msg_t *)g_rndis_buf;
 
     cmd->MessageType = REMOTE_NDIS_QUERY_MSG;
@@ -143,11 +153,16 @@ int usbh_rndis_query_msg_transfer(struct usbh_rndis *rndis_class, uint32_t oid,
 
 static int usbh_rndis_set_msg_transfer(struct usbh_rndis *rndis_class, uint32_t oid, uint8_t *info, uint32_t info_len)
 {
-    struct usb_setup_packet *setup = rndis_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret = 0;
     rndis_set_msg_t *cmd;
     rndis_set_cmplt_t *resp;
 
+    if (!rndis_class || !rndis_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = rndis_class->hport->setup;
+
     cmd = (rndis_set_msg_t *)g_rndis_buf;
 
     cmd->MessageType = REMOTE_NDIS_SET_MSG;
@@ -210,11 +225,16 @@ int usbh_rndis_get_connect_status(struct usbh_rndis *rndis_class)
 
 int usbh_rndis_keepalive(struct usbh_rndis *rndis_class)
 {
-    struct usb_setup_packet *setup = rndis_class->hport->setup;
+    struct usb_setup_packet *setup;
     int ret = 0;
     rndis_keepalive_msg_t *cmd;
     rndis_keepalive_cmplt_t *resp;
 
+    if (!rndis_class || !rndis_class->hport) {
+        return -USB_ERR_INVAL;
+    }
+    setup = rndis_class->hport->setup;
+
     cmd = (rndis_keepalive_msg_t *)g_rndis_buf;
 
     cmd->MessageType = REMOTE_NDIS_KEEPALIVE_MSG;