From d6d6416b50bae8544a4294ba12416cdd381c8412 Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Wed, 13 Mar 2019 17:10:01 +0100 Subject: [PATCH] usb: Avoid segfault in usbSleep(), usbReconnect() and usbPoll() when USB is not initialized. --- firmware/usb.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/firmware/usb.c b/firmware/usb.c index 356bd03650..4830da6628 100644 --- a/firmware/usb.c +++ b/firmware/usb.c @@ -332,7 +332,7 @@ static void set_config(usbd_device *dev, uint16_t wValue) hid_control_request); } -static usbd_device *usbd_dev; +static usbd_device *usbd_dev = NULL; static uint8_t usbd_control_buffer[256] __attribute__ ((aligned (2))); static const struct usb_device_capability_descriptor* capabilities[] = { @@ -360,6 +360,10 @@ void usbInit(void) void usbPoll(void) { + if (usbd_dev == NULL) { + return; + } + static const uint8_t *data; // poll read buffer usbd_poll(usbd_dev); @@ -383,9 +387,11 @@ void usbPoll(void) void usbReconnect(void) { - usbd_disconnect(usbd_dev, 1); - delay(1000); - usbd_disconnect(usbd_dev, 0); + if (usbd_dev != NULL) { + usbd_disconnect(usbd_dev, 1); + delay(1000); + usbd_disconnect(usbd_dev, 0); + } } char usbTiny(char set) @@ -400,6 +406,8 @@ void usbSleep(uint32_t millis) uint32_t start = timer_ms(); while ((timer_ms() - start) < millis) { - usbd_poll(usbd_dev); + if (usbd_dev != NULL) { + usbd_poll(usbd_dev); + } } }