1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-15 09:50:57 +00:00

usb: Avoid segfault in usbSleep(), usbReconnect() and usbPoll() when USB is not initialized.

This commit is contained in:
Andrew Kozlik 2019-03-13 17:10:01 +01:00
parent 1dada5a6fd
commit d6d6416b50

View File

@ -332,7 +332,7 @@ static void set_config(usbd_device *dev, uint16_t wValue)
hid_control_request); 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 uint8_t usbd_control_buffer[256] __attribute__ ((aligned (2)));
static const struct usb_device_capability_descriptor* capabilities[] = { static const struct usb_device_capability_descriptor* capabilities[] = {
@ -360,6 +360,10 @@ void usbInit(void)
void usbPoll(void) void usbPoll(void)
{ {
if (usbd_dev == NULL) {
return;
}
static const uint8_t *data; static const uint8_t *data;
// poll read buffer // poll read buffer
usbd_poll(usbd_dev); usbd_poll(usbd_dev);
@ -383,9 +387,11 @@ void usbPoll(void)
void usbReconnect(void) void usbReconnect(void)
{ {
usbd_disconnect(usbd_dev, 1); if (usbd_dev != NULL) {
delay(1000); usbd_disconnect(usbd_dev, 1);
usbd_disconnect(usbd_dev, 0); delay(1000);
usbd_disconnect(usbd_dev, 0);
}
} }
char usbTiny(char set) char usbTiny(char set)
@ -400,6 +406,8 @@ void usbSleep(uint32_t millis)
uint32_t start = timer_ms(); uint32_t start = timer_ms();
while ((timer_ms() - start) < millis) { while ((timer_ms() - start) < millis) {
usbd_poll(usbd_dev); if (usbd_dev != NULL) {
usbd_poll(usbd_dev);
}
} }
} }