/* Return USB_HANDLED if session took place else return USB_EXTRACTED */ static int handle_usb(int connect_timeout) { static struct event_queue q SHAREDBSS_ATTR; struct queue_event ev; int usb = USB_EXTRACTED; long end_tick = 0; if (!usb_plugged()) return USB_EXTRACTED; queue_init(&q, true); usb_init(); usb_start_monitoring(); printf("USB: Connecting"); if (connect_timeout != TIMEOUT_BLOCK) end_tick = current_tick + connect_timeout; while (1) { /* Sleep no longer than 1/2s */ queue_wait_w_tmo(&q, &ev, HZ/2); if (ev.id == SYS_USB_CONNECTED) { /* Switch to verbose mode if not in it so that the status updates * are shown */ verbose = true; /* Got the message - wait for disconnect */ printf("Bootloader USB mode"); usb = USB_HANDLED; usb_acknowledge(SYS_USB_CONNECTED_ACK); usb_wait_for_disconnect(&q); break; } if (connect_timeout != TIMEOUT_BLOCK && TIME_AFTER(current_tick, end_tick)) { /* Timed out waiting for the connect - will happen when connected * to a charger instead of a host port and the charging pin is * the same as the USB pin */ printf("USB: Timed out"); break; } if (!usb_plugged()) break; /* Cable pulled */ } usb_close(); queue_delete(&q); return usb; }
void usb_connect_event(void) { int status = usb_plugged() ? USB_INSERTED : USB_EXTRACTED; usb_status = status; /* Notify power that USB charging is potentially available */ charger_usb_detect_event(status); usb_status_event((status == USB_INSERTED) ? USB_POWERED : USB_UNPOWERED); }
int usb_detect(void) { return usb_plugged() ? USB_INSERTED : USB_EXTRACTED; }