From 47bade1437d5281a5aca0ec8b2b9a3956f88f685 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Thu, 15 Dec 2011 00:14:36 +0000 Subject: Simplify and neaten-up usb.c a bit. USB_INSERTED and USB_EXTRACTED are always used as events to indicate cable state. USB_HOSTED is posted to indicated that a host was detected. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@31263 a1c6a512-1295-4272-9138-f99709370657 --- firmware/export/usb.h | 14 +- firmware/target/arm/as3525/usb-as3525.c | 6 +- firmware/target/arm/imx233/usb-imx233.c | 6 +- .../target/arm/imx31/gigabeat-s/usb-gigabeat-s.c | 6 +- firmware/target/arm/usb-fw-pp502x.c | 4 +- firmware/usb.c | 151 +++++++++------------ 6 files changed, 83 insertions(+), 104 deletions(-) (limited to 'firmware') diff --git a/firmware/export/usb.h b/firmware/export/usb.h index d68ac7d910..3baacc5420 100644 --- a/firmware/export/usb.h +++ b/firmware/export/usb.h @@ -30,15 +30,16 @@ #endif /* Messages from usb_tick and thread states */ -enum { +enum +{ +#ifdef HAVE_LCD_BITMAP + USB_SCREENDUMP = -1, /* State */ +#endif USB_EXTRACTED = 0, /* Event+State */ USB_INSERTED, /* Event+State */ - USB_POWERED, /* Event+State - transitional indicator if no power */ + USB_POWERED, /* State - transitional indicator if no power */ #ifdef USB_DETECT_BY_CORE - USB_UNPOWERED, /* Event */ -#endif -#ifdef HAVE_LCD_BITMAP - USB_SCREENDUMP, /* State */ + USB_HOSTED, /* Event - host presence was detected */ #endif #if (CONFIG_STORAGE & STORAGE_MMC) USB_REENABLE, /* Event */ @@ -57,6 +58,7 @@ enum { USB_HANDLED, /* Bootloader status code */ #endif }; + #ifdef HAVE_USB_POWER #if CONFIG_KEYPAD == RECORDER_PAD #define USBPOWER_BUTTON BUTTON_F1 diff --git a/firmware/target/arm/as3525/usb-as3525.c b/firmware/target/arm/as3525/usb-as3525.c index 6ff235dfce..6c39150c28 100644 --- a/firmware/target/arm/as3525/usb-as3525.c +++ b/firmware/target/arm/as3525/usb-as3525.c @@ -46,7 +46,7 @@ void usb_insert_int(void) { usb_status = USB_INSERTED; #ifdef USB_STATUS_BY_EVENT - usb_status_event(USB_POWERED); + usb_status_event(USB_INSERTED); #endif } @@ -54,7 +54,7 @@ void usb_remove_int(void) { usb_status = USB_EXTRACTED; #ifdef USB_STATUS_BY_EVENT - usb_status_event(USB_UNPOWERED); + usb_status_event(USB_EXTRACTED); #endif } @@ -68,7 +68,7 @@ void usb_drv_usb_detect_event(void) int oldstatus = disable_irq_save(); /* May come via USB thread */ if (usb_status == USB_INSERTED) - usb_status_event(USB_INSERTED); + usb_status_event(USB_HOSTED); restore_irq(oldstatus); #endif diff --git a/firmware/target/arm/imx233/usb-imx233.c b/firmware/target/arm/imx233/usb-imx233.c index c275f76916..91fb770190 100644 --- a/firmware/target/arm/imx233/usb-imx233.c +++ b/firmware/target/arm/imx233/usb-imx233.c @@ -33,17 +33,17 @@ void usb_insert_int(void) { - usb_status_event(USB_POWERED); + usb_status_event(USB_INSERTED); } void usb_remove_int(void) { - usb_status_event(USB_UNPOWERED); + usb_status_event(USB_EXTRACTED); } void usb_drv_usb_detect_event() { - usb_status_event(USB_INSERTED); + usb_status_event(USB_HOSTED); } void usb_attach(void) diff --git a/firmware/target/arm/imx31/gigabeat-s/usb-gigabeat-s.c b/firmware/target/arm/imx31/gigabeat-s/usb-gigabeat-s.c index c52a9a6dec..19c3b3173f 100644 --- a/firmware/target/arm/imx31/gigabeat-s/usb-gigabeat-s.c +++ b/firmware/target/arm/imx31/gigabeat-s/usb-gigabeat-s.c @@ -66,7 +66,7 @@ void usb_connect_event(void) 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); + usb_status_event(status); } int usb_detect(void) @@ -138,11 +138,11 @@ void usb_drv_int_enable(bool enable) } } -/* Called during the bus reset interrupt when in detect mode */ +/* Called during the setup packet request by the host */ void usb_drv_usb_detect_event(void) { if (usb_drv_powered()) - usb_status_event(USB_INSERTED); + usb_status_event(USB_HOSTED); } /* Called when reading the MBR */ diff --git a/firmware/target/arm/usb-fw-pp502x.c b/firmware/target/arm/usb-fw-pp502x.c index f1d250d3ec..2ba537f286 100644 --- a/firmware/target/arm/usb-fw-pp502x.c +++ b/firmware/target/arm/usb-fw-pp502x.c @@ -212,7 +212,7 @@ static int usb_status = USB_EXTRACTED; static int usb_timeout_event(struct timeout *tmo) { - usb_status_event(tmo->data == USB_GPIO_VAL ? USB_POWERED : USB_UNPOWERED); + usb_status_event(tmo->data == USB_GPIO_VAL ? USB_INSERTED : USB_EXTRACTED); return 0; } @@ -231,7 +231,7 @@ void usb_drv_usb_detect_event(void) { /* Filter for invalid bus reset when unplugging by checking the pin state. */ if(usb_plugged()) { - usb_status_event(USB_INSERTED); + usb_status_event(USB_HOSTED); } } #endif /* USB_STATUS_BY_EVENT */ diff --git a/firmware/usb.c b/firmware/usb.c index c7326c548b..ec81ba6f58 100644 --- a/firmware/usb.c +++ b/firmware/usb.c @@ -110,29 +110,46 @@ static void try_reboot(void) } #endif /* USB_FIRWIRE_HANDLING || (HAVE_USBSTACK && !USE_ROCKBOX_USB) */ - -#ifdef HAVE_LCD_BITMAP +/* Screen dump */ static inline bool usb_do_screendump(void) { +#ifdef HAVE_LCD_BITMAP if(do_screendump_instead_of_usb) { usb_state = USB_SCREENDUMP; screen_dump(); #ifdef HAVE_REMOTE_LCD remote_screen_dump(); -#endif +#endif /* HAVE_REMOTE_LCD */ return true; } +#endif /* HAVE_LCD_BITMAP */ + return false; +} +/* Power (charging-only) button */ +static inline bool usb_power_button(void) +{ +#ifdef HAVE_USB_POWER + return (button_status() & ~USBPOWER_BTN_IGNORE); +#else return false; +#endif } -#else /* !HAVE_LCD_BITMAP */ -static inline bool usb_do_screendump(void) + +#ifdef USB_FIREWIRE_HANDLING +static inline bool usb_reboot_button(void) { +#ifdef HAVE_USB_POWER + return (button_status() & ~USBPOWER_BTN_IGNORE); +#else return false; +#endif } -#endif /* HAVE_LCD_BITMAP */ +#endif /* USB_FIREWIRE_HANDLING */ + +/*--- Routines that differ depending upon the presence of a USB stack ---*/ #ifdef HAVE_USBSTACK /* Enable / disable USB when the stack is enabled - otherwise a noop */ @@ -241,12 +258,10 @@ static inline void usb_slave_mode(bool on) #else /* !USE_ROCKBOX_USB */ static inline void usb_slave_mode(bool on) { + /* Until we have native mass-storage mode, we want to reboot on USB host + * connect */ if(on) - { - /* until we have native mass-storage mode, we want to reboot on - usb host connect */ try_reboot(); - } } #endif /* USE_ROCKBOX_USB */ @@ -343,68 +358,34 @@ static inline void usb_slave_mode(bool on) } #endif /* HAVE_USBSTACK */ -#ifdef HAVE_USB_POWER -static inline bool usb_power_button(void) -{ - return (button_status() & ~USBPOWER_BTN_IGNORE); -} -#ifdef USB_FIREWIRE_HANDLING -static inline bool usb_reboot_button(void) +/*--- General driver code ---*/ +static void NORETURN_ATTR usb_thread(void) { - return (button_status() & ~USBPOWER_BTN_IGNORE); -} -#endif -#else /* !HAVE_USB_POWER */ -static inline bool usb_power_button(void) -{ - return false; -} - -#ifdef USB_FIREWIRE_HANDLING -static inline bool usb_reboot_button(void) -{ - return false; -} -#endif -#endif /* HAVE_USB_POWER */ - -#ifndef USB_DRIVER_CLOSE -static void usb_thread(void) NORETURN_ATTR; -#endif -static void usb_thread(void) -{ -#ifdef USB_DETECT_BY_CORE - bool host_detected = false; -#endif int num_acks_to_expect = 0; long last_broadcast_tick = current_tick; + bool host_detected = false; struct queue_event ev; while(1) { queue_wait(&usb_queue, &ev); + switch(ev.id) { /*** Main USB thread duties ***/ #ifdef HAVE_USBSTACK case USB_TRANSFER_COMPLETION: + if(usb_state <= USB_EXTRACTED) + break; + usb_core_handle_transfer_completion( (struct usb_transfer_completion_event_data*)ev.data); break; #endif /* HAVE_USBSTACK */ case USB_INSERTED: -#ifdef USB_DETECT_BY_CORE - if(usb_state != USB_POWERED) - break; - - if (host_detected) - break; /* Drivers configured but we're still USB_POWERED */ - - host_detected = true; -#else /* !USB_DETECT_BY_CORE */ if(usb_state != USB_EXTRACTED) break; @@ -413,8 +394,21 @@ static void usb_thread(void) usb_state = USB_POWERED; usb_stack_enable(true); + +#ifdef USB_DETECT_BY_CORE + /* Wait for USB core to detect the host */ + break; + + case USB_HOSTED: + if(usb_state != USB_POWERED) + break; #endif /* USB_DETECT_BY_CORE */ + if(host_detected) + break; + + host_detected = true; + if(usb_power_button()) { /* Only charging is desired */ @@ -440,19 +434,18 @@ static void usb_thread(void) } num_acks_to_expect += queue_broadcast(SYS_USB_CONNECTED, 0) - 1; - DEBUGF("USB inserted. Waiting for %d acks...\n", - num_acks_to_expect); + DEBUGF("usb: waiting for %d acks...\n", num_acks_to_expect); /* Leave the state as USB_POWERED until the expected number of ACKS are received. */ break; - /* USB_INSERTED: */ + /* USB_INSERTED: or USB_HOSTED: */ case SYS_USB_CONNECTED_ACK: if(num_acks_to_expect > 0 && --num_acks_to_expect == 0) { - DEBUGF("All threads have acknowledged the connect.\n"); - if(usb_state == USB_POWERED) + DEBUGF("usb: all threads have acknowledged the connect.\n"); + if(host_detected) { usb_slave_mode(true); usb_state = USB_INSERTED; @@ -460,29 +453,11 @@ static void usb_thread(void) } else { - DEBUGF("usb: got ack, %d to go...\n", - num_acks_to_expect); + DEBUGF("usb: got ack, %d to go...\n", num_acks_to_expect); } break; /* SYS_USB_CONNECTED_ACK */ -#ifdef USB_DETECT_BY_CORE - /* In this case, these events handle cable insertion. USB driver or - core determines USB_INSERTED. */ - case USB_POWERED: - if(usb_state != USB_EXTRACTED) - break; - - if(usb_do_screendump()) - break; - - usb_state = USB_POWERED; - usb_stack_enable(true); - break; - /* USB_POWERED: */ - - case USB_UNPOWERED: -#endif /* USB_DETECT_BY_CORE */ case USB_EXTRACTED: if(usb_state == USB_EXTRACTED) break; @@ -497,13 +472,15 @@ static void usb_thread(void) usb_state = USB_EXTRACTED; - /* Ok to broadcast disconnect now */ - usb_configure_drivers(USB_EXTRACTED); -#ifdef USB_DETECT_BY_CORE - host_detected = false; -#endif + if(host_detected) + { + /* Ok to broadcast disconnect now */ + usb_configure_drivers(USB_EXTRACTED); + host_detected = false; + } + break; - /* USB_UNPOWERED: USB_EXTRACTED: */ + /* USB_EXTRACTED: */ /*** Miscellaneous USB thread duties ***/ @@ -536,7 +513,8 @@ static void usb_thread(void) /* CLOSE */ #ifdef USB_DRIVER_CLOSE case USB_QUIT: - return; + thread_exit(); + break; #endif } /* switch */ } /* while */ @@ -554,9 +532,8 @@ void usb_status_event(int current_status) { /* Caller isn't expected to filter for changes in status. * current_status: - * USB_DETECT_BY_CORE: USB_POWERED, USB_UNPOWERED, - USB_INSERTED (core) - * else: USB_INSERTED, USB_EXTRACTED + * all: USB_INSERTED, USB_EXTRACTED + * USB_DETECT_BY_CORE: USB_HOSTED (from core) */ if(usb_monitor_enabled) { @@ -578,8 +555,8 @@ void usb_start_monitoring(void) * was enabled due to the connector already having been inserted before * before or during boot. */ #ifdef USB_DETECT_BY_CORE - /* Filter the status - USB_INSERTED may happen later */ - status = (status == USB_EXTRACTED) ? USB_UNPOWERED : USB_POWERED; + /* Filter the status - USB_HOSTED may happen later */ + status = (status == USB_INSERTED) ? : USB_EXTRACTED; #endif usb_status_event(status); -- cgit