summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/main.c2
-rw-r--r--bootloader/gigabeat.c4
-rw-r--r--bootloader/iriver_h300.c11
-rw-r--r--bootloader/main-pp.c2
-rw-r--r--bootloader/main.c10
-rw-r--r--firmware/export/usb.h37
-rw-r--r--firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c4
-rw-r--r--firmware/target/arm/s3c2440/gigabeat-fx/usb-meg-fx.c8
-rw-r--r--firmware/target/arm/s3c2440/gigabeat-fx/usb-target.h1
-rw-r--r--firmware/target/arm/usb-fw-pp5002.c6
-rw-r--r--firmware/target/arm/usb-fw-pp502x.c88
-rw-r--r--firmware/target/coldfire/iaudio/usb-iaudio.c5
-rw-r--r--firmware/target/coldfire/iriver/h100/usb-h100.c5
-rw-r--r--firmware/target/coldfire/iriver/h300/usb-h300.c5
-rw-r--r--firmware/target/sh/archos/fm_v2/usb-fm_v2.c5
-rw-r--r--firmware/target/sh/archos/ondio/usb-ondio.c5
-rw-r--r--firmware/target/sh/archos/player/usb-player.c5
-rw-r--r--firmware/target/sh/archos/recorder/usb-recorder.c5
-rw-r--r--firmware/usb.c51
19 files changed, 160 insertions, 99 deletions
diff --git a/apps/main.c b/apps/main.c
index 3d419407f4..86f651f775 100644
--- a/apps/main.c
+++ b/apps/main.c
@@ -444,7 +444,7 @@ static void init(void)
444#endif 444#endif
445 445
446 usb_start_monitoring(); 446 usb_start_monitoring();
447 while (usb_detect()) 447 while (usb_detect() == USB_INSERTED)
448 { 448 {
449#ifdef HAVE_EEPROM_SETTINGS 449#ifdef HAVE_EEPROM_SETTINGS
450 firmware_settings.disk_clean = false; 450 firmware_settings.disk_clean = false;
diff --git a/bootloader/gigabeat.c b/bootloader/gigabeat.c
index e482c70d16..704548b65a 100644
--- a/bootloader/gigabeat.c
+++ b/bootloader/gigabeat.c
@@ -66,7 +66,7 @@ void main(void)
66 usb_init(); 66 usb_init();
67 67
68 /* Enter USB mode without USB thread */ 68 /* Enter USB mode without USB thread */
69 if(usb_detect()) 69 if(usb_detect() == USB_INSERTED)
70 { 70 {
71 const char msg[] = "Bootloader USB mode"; 71 const char msg[] = "Bootloader USB mode";
72 reset_screen(); 72 reset_screen();
@@ -78,7 +78,7 @@ void main(void)
78 sleep(HZ/20); 78 sleep(HZ/20);
79 usb_enable(true); 79 usb_enable(true);
80 80
81 while (usb_detect()) 81 while (usb_detect() == USB_INSERTED)
82 sleep(HZ); 82 sleep(HZ);
83 83
84 usb_enable(false); 84 usb_enable(false);
diff --git a/bootloader/iriver_h300.c b/bootloader/iriver_h300.c
index dce178fcec..a3a15bd4b5 100644
--- a/bootloader/iriver_h300.c
+++ b/bootloader/iriver_h300.c
@@ -169,7 +169,7 @@ void main(void)
169 169
170 /* Turn off if we believe the start was accidental */ 170 /* Turn off if we believe the start was accidental */
171 if(!(rtc_alarm || on_button || rc_on_button || 171 if(!(rtc_alarm || on_button || rc_on_button ||
172 usb_detect() || charger_inserted())) { 172 (usb_detect() == USB_INSERTED) || charger_inserted())) {
173 __reset_cookie(); 173 __reset_cookie();
174 power_off(); 174 power_off();
175 } 175 }
@@ -222,7 +222,8 @@ void main(void)
222 { 222 {
223 hold_status = true; 223 hold_status = true;
224 } 224 }
225 if (hold_status && !rtc_alarm && !usb_detect() && !charger_inserted()) 225 if (hold_status && !rtc_alarm && (usb_detect() != USB_INSERTED) &&
226 !charger_inserted())
226 { 227 {
227 if (detect_original_firmware()) 228 if (detect_original_firmware())
228 { 229 {
@@ -282,7 +283,7 @@ void main(void)
282 break; 283 break;
283 } 284 }
284 285
285 if(usb_detect()) 286 if(usb_detect() == USB_INSERTED)
286 request_start = true; 287 request_start = true;
287 } 288 }
288 if(!request_start) 289 if(!request_start)
@@ -297,7 +298,7 @@ void main(void)
297 usb_init(); 298 usb_init();
298 299
299 /* A hack to enter USB mode without using the USB thread */ 300 /* A hack to enter USB mode without using the USB thread */
300 if(usb_detect()) 301 if(usb_detect() == USB_INSERTED)
301 { 302 {
302 const char msg[] = "Bootloader USB mode"; 303 const char msg[] = "Bootloader USB mode";
303 int w, h; 304 int w, h;
@@ -314,7 +315,7 @@ void main(void)
314 sleep(HZ/20); 315 sleep(HZ/20);
315 usb_enable(true); 316 usb_enable(true);
316 cpu_idle_mode(true); 317 cpu_idle_mode(true);
317 while (usb_detect()) 318 while (usb_detect() == USB_INSERTED)
318 { 319 {
319 /* Print the battery status. */ 320 /* Print the battery status. */
320 line = 0; 321 line = 0;
diff --git a/bootloader/main-pp.c b/bootloader/main-pp.c
index 0d377fd798..8b851394ac 100644
--- a/bootloader/main-pp.c
+++ b/bootloader/main-pp.c
@@ -464,7 +464,7 @@ void* main(void)
464 { 464 {
465 usb_retry++; 465 usb_retry++;
466 sleep(HZ/4); 466 sleep(HZ/4);
467 usb = usb_detect(); 467 usb = (usb_detect() == USB_INSERTED);
468 } 468 }
469 if (usb) 469 if (usb)
470 btn |= BOOTLOADER_BOOT_OF; 470 btn |= BOOTLOADER_BOOT_OF;
diff --git a/bootloader/main.c b/bootloader/main.c
index 0306eea5dc..e60799fa65 100644
--- a/bootloader/main.c
+++ b/bootloader/main.c
@@ -388,7 +388,7 @@ void main(void)
388 power_init(); 388 power_init();
389 389
390 /* Turn off if neither ON button is pressed */ 390 /* Turn off if neither ON button is pressed */
391 if(!(on_button || rc_on_button || usb_detect())) 391 if(!(on_button || rc_on_button || (usb_detect() == USB_INSERTED)))
392 { 392 {
393 __reset_cookie(); 393 __reset_cookie();
394 power_off(); 394 power_off();
@@ -439,7 +439,7 @@ void main(void)
439 } 439 }
440 440
441# ifdef EEPROM_SETTINGS 441# ifdef EEPROM_SETTINGS
442 if (!hold_status && !usb_detect() && !recovery_mode) 442 if (!hold_status && (usb_detect() != USB_INSERTED) && !recovery_mode)
443 try_flashboot(); 443 try_flashboot();
444# endif 444# endif
445 445
@@ -467,7 +467,7 @@ void main(void)
467 467
468 /* Don't start if the Hold button is active on the device you 468 /* Don't start if the Hold button is active on the device you
469 are starting with */ 469 are starting with */
470 if (!usb_detect() && (hold_status 470 if ((usb_detect() != USB_INSERTED) && (hold_status
471#ifdef HAVE_EEPROM_SETTINGS 471#ifdef HAVE_EEPROM_SETTINGS
472 || recovery_mode 472 || recovery_mode
473#endif 473#endif
@@ -494,7 +494,7 @@ void main(void)
494 usb_init(); 494 usb_init();
495 495
496 /* A hack to enter USB mode without using the USB thread */ 496 /* A hack to enter USB mode without using the USB thread */
497 if(usb_detect()) 497 if(usb_detect() == USB_INSERTED)
498 { 498 {
499 const char msg[] = "Bootloader USB mode"; 499 const char msg[] = "Bootloader USB mode";
500 int w, h; 500 int w, h;
@@ -520,7 +520,7 @@ void main(void)
520 sleep(HZ/20); 520 sleep(HZ/20);
521 usb_enable(true); 521 usb_enable(true);
522 cpu_idle_mode(true); 522 cpu_idle_mode(true);
523 while (usb_detect()) 523 while (usb_detect() == USB_INSERTED)
524 { 524 {
525 /* Print the battery status. */ 525 /* Print the battery status. */
526 line = 0; 526 line = 0;
diff --git a/firmware/export/usb.h b/firmware/export/usb.h
index 622db35543..82b9d36d4a 100644
--- a/firmware/export/usb.h
+++ b/firmware/export/usb.h
@@ -20,6 +20,41 @@
20#define _USB_H_ 20#define _USB_H_
21 21
22#include "kernel.h" 22#include "kernel.h"
23#include "button.h"
24
25/* Messages from usb_tick and thread states */
26#define USB_INSERTED 1
27#define USB_EXTRACTED 2
28#ifdef HAVE_MMC
29#define USB_REENABLE 3
30#endif
31
32#ifdef HAVE_USB_POWER
33#define USB_POWERED 4
34
35#if CONFIG_KEYPAD == RECORDER_PAD
36#define USBPOWER_BUTTON BUTTON_F1
37#define USBPOWER_BTN_IGNORE BUTTON_ON
38#elif CONFIG_KEYPAD == ONDIO_PAD
39#define USBPOWER_BUTTON BUTTON_MENU
40#define USBPOWER_BTN_IGNORE BUTTON_OFF
41#elif (CONFIG_KEYPAD == IPOD_4G_PAD)
42#define USBPOWER_BUTTON BUTTON_MENU
43#define USBPOWER_BTN_IGNORE BUTTON_PLAY
44#elif CONFIG_KEYPAD == IRIVER_H300_PAD
45#define USBPOWER_BUTTON BUTTON_REC
46#define USBPOWER_BTN_IGNORE BUTTON_ON
47#elif CONFIG_KEYPAD == GIGABEAT_PAD
48#define USBPOWER_BUTTON BUTTON_MENU
49#define USBPOWER_BTN_IGNORE BUTTON_POWER
50#elif CONFIG_KEYPAD == IRIVER_H10_PAD
51#define USBPOWER_BUTTON BUTTON_NONE
52#define USBPOWER_BTN_IGNORE BUTTON_POWER
53#elif CONFIG_KEYPAD == SANSA_E200_PAD
54#define USBPOWER_BUTTON BUTTON_SELECT
55#define USBPOWER_BTN_IGNORE BUTTON_POWER
56#endif
57#endif /* HAVE_USB_POWER */
23 58
24void usb_init(void); 59void usb_init(void);
25void usb_enable(bool on); 60void usb_enable(bool on);
@@ -28,7 +63,7 @@ void usb_acknowledge(long id);
28void usb_wait_for_disconnect(struct event_queue *q); 63void usb_wait_for_disconnect(struct event_queue *q);
29int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks); 64int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks);
30bool usb_inserted(void); /* return the official value, what's been reported to the threads */ 65bool usb_inserted(void); /* return the official value, what's been reported to the threads */
31bool usb_detect(void); /* return the raw hardware value */ 66int usb_detect(void); /* return the raw hardware value - nothing/pc/charger */
32#ifdef HAVE_USB_POWER 67#ifdef HAVE_USB_POWER
33bool usb_powered(void); 68bool usb_powered(void);
34#ifdef CONFIG_CHARGING 69#ifdef CONFIG_CHARGING
diff --git a/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c b/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c
index a22a227b15..3761c84a50 100644
--- a/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c
+++ b/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c
@@ -37,10 +37,10 @@ void usb_init_device(void)
37{ 37{
38} 38}
39 39
40bool usb_detect(void) 40int usb_detect(void)
41{ 41{
42 /* TODO: Implement USB_ISP1582 */ 42 /* TODO: Implement USB_ISP1582 */
43 return false; 43 return USB_EXTRACTED;
44} 44}
45 45
46void usb_enable(bool on) 46void usb_enable(bool on)
diff --git a/firmware/target/arm/s3c2440/gigabeat-fx/usb-meg-fx.c b/firmware/target/arm/s3c2440/gigabeat-fx/usb-meg-fx.c
index 566d25eecb..217a7d3cef 100644
--- a/firmware/target/arm/s3c2440/gigabeat-fx/usb-meg-fx.c
+++ b/firmware/target/arm/s3c2440/gigabeat-fx/usb-meg-fx.c
@@ -21,6 +21,7 @@
21#include "system.h" 21#include "system.h"
22#include "kernel.h" 22#include "kernel.h"
23#include "ata.h" 23#include "ata.h"
24#include "usb.h"
24 25
25#define USB_RST_ASSERT GPBDAT &= ~(1 << 4) 26#define USB_RST_ASSERT GPBDAT &= ~(1 << 4)
26#define USB_RST_DEASSERT GPBDAT |= (1 << 4) 27#define USB_RST_DEASSERT GPBDAT |= (1 << 4)
@@ -35,9 +36,12 @@
35#define USB_CRADLE_BUS_DISABLE GPHDAT &= ~(1 << 8) 36#define USB_CRADLE_BUS_DISABLE GPHDAT &= ~(1 << 8)
36 37
37/* The usb detect is one pin to the cpu active low */ 38/* The usb detect is one pin to the cpu active low */
38inline bool usb_detect(void) 39int usb_detect(void)
39{ 40{
40 return USB_UNIT_IS_PRESENT | USB_CRADLE_IS_PRESENT; 41 if (USB_UNIT_IS_PRESENT | USB_CRADLE_IS_PRESENT)
42 return USB_INSERTED;
43 else
44 return USB_EXTRACTED;
41} 45}
42 46
43void usb_init_device(void) 47void usb_init_device(void)
diff --git a/firmware/target/arm/s3c2440/gigabeat-fx/usb-target.h b/firmware/target/arm/s3c2440/gigabeat-fx/usb-target.h
index baeb539b38..65690dc86b 100644
--- a/firmware/target/arm/s3c2440/gigabeat-fx/usb-target.h
+++ b/firmware/target/arm/s3c2440/gigabeat-fx/usb-target.h
@@ -20,7 +20,6 @@
20#define USB_TARGET_H 20#define USB_TARGET_H
21 21
22bool usb_init_device(void); 22bool usb_init_device(void);
23bool usb_detect(void);
24void usb_enable(bool on); 23void usb_enable(bool on);
25 24
26#endif 25#endif
diff --git a/firmware/target/arm/usb-fw-pp5002.c b/firmware/target/arm/usb-fw-pp5002.c
index e64d4f8f81..2a216c4d9b 100644
--- a/firmware/target/arm/usb-fw-pp5002.c
+++ b/firmware/target/arm/usb-fw-pp5002.c
@@ -56,15 +56,15 @@ void usb_enable(bool on)
56 } 56 }
57} 57}
58 58
59bool usb_detect(void) 59int usb_detect(void)
60{ 60{
61#if defined(IPOD_1G2G) || defined(IPOD_3G) 61#if defined(IPOD_1G2G) || defined(IPOD_3G)
62 /* GPIO C bit 7 is firewire detect */ 62 /* GPIO C bit 7 is firewire detect */
63 if (!(GPIOC_INPUT_VAL & 0x80)) 63 if (!(GPIOC_INPUT_VAL & 0x80))
64 return true; 64 return USB_INSERTED;
65#endif 65#endif
66 66
67 /* TODO: add USB detection for iPod 3rd gen */ 67 /* TODO: add USB detection for iPod 3rd gen */
68 68
69 return false; 69 return USB_EXTRACTED;
70} 70}
diff --git a/firmware/target/arm/usb-fw-pp502x.c b/firmware/target/arm/usb-fw-pp502x.c
index 76bd7281f5..ad2a10599f 100644
--- a/firmware/target/arm/usb-fw-pp502x.c
+++ b/firmware/target/arm/usb-fw-pp502x.c
@@ -92,7 +92,7 @@ void usb_init_device(void)
92void usb_enable(bool on) 92void usb_enable(bool on)
93{ 93{
94#ifdef HAVE_USBSTACK 94#ifdef HAVE_USBSTACK
95 (void)on; 95 (void)on;
96#else 96#else
97 /* This device specific code will eventually give way to proper USB 97 /* This device specific code will eventually give way to proper USB
98 handling, which should be the same for all PP502x targets. */ 98 handling, which should be the same for all PP502x targets. */
@@ -124,18 +124,42 @@ void usb_enable(bool on)
124#endif /* !HAVE_USBSTACK */ 124#endif /* !HAVE_USBSTACK */
125} 125}
126 126
127bool usb_detect(void) 127int usb_detect(void)
128{ 128{
129 static int countdown = 0;
130 static int status = USB_EXTRACTED;
129 static bool prev_usbstatus1 = false; 131 static bool prev_usbstatus1 = false;
130 bool usbstatus1,usbstatus2; 132 bool usbstatus1, usbstatus2;
131 133
132#if defined(IPOD_COLOR) || defined(IPOD_4G) \ 134#if defined(IPOD_COLOR) || defined(IPOD_4G) \
133 || defined(IPOD_MINI) || defined(IPOD_MINI2G) 135 || defined(IPOD_MINI) || defined(IPOD_MINI2G)
134 /* GPIO C bit 1 is firewire detect */ 136 /* GPIO C bit 1 is firewire detect */
135 if (!(GPIOC_INPUT_VAL & 0x02)) 137 if (!(GPIOC_INPUT_VAL & 0x02))
136 return true; 138 return USB_INSERTED;
137#endif 139#endif
138 140
141 if (countdown > 0)
142 {
143 countdown--;
144
145 usbstatus2 = (UDC_PORTSC1 & PORTSCX_CURRENT_CONNECT_STATUS) ? true : false;
146 if ((countdown == 0) || usbstatus2)
147 {
148 countdown = 0;
149 status = usbstatus2 ? USB_INSERTED : USB_POWERED;
150 dr_controller_stop();
151
152#ifdef HAVE_USBSTACK
153 /* TODO: Move this call - it shouldn't be done in this function */
154 if (status == USB_INSERTED)
155 {
156 usb_stack_start();
157 }
158#endif
159 }
160 return status;
161 }
162
139 /* UDC_ID should have the bit format: 163 /* UDC_ID should have the bit format:
140 [31:24] = 0x0 164 [31:24] = 0x0
141 [23:16] = 0x22 (Revision number) 165 [23:16] = 0x22 (Revision number)
@@ -144,29 +168,51 @@ bool usb_detect(void)
144 [7:6] = 0x0 (Reserved) 168 [7:6] = 0x0 (Reserved)
145 [5:0] = 0x05 (ID) */ 169 [5:0] = 0x05 (ID) */
146 if (UDC_ID != 0x22FA05) { 170 if (UDC_ID != 0x22FA05) {
147 return false; 171 /* This should never occur - do we even need to test? */
172 return USB_EXTRACTED;
148 } 173 }
149 174
150 usbstatus1 = (UDC_OTGSC & 0x800) ? true : false; 175 usbstatus1 = (UDC_OTGSC & 0x800) ? true : false;
176
177 if (usbstatus1 == prev_usbstatus1)
178 {
179 /* Nothing has changed, so just return previous status */
180 return status;
181 }
182 prev_usbstatus1 = usbstatus1;
183
184 if (!usbstatus1)
185 { /* We have just been disconnected */
186 status = USB_EXTRACTED;
151#ifdef HAVE_USBSTACK 187#ifdef HAVE_USBSTACK
152 if ((usbstatus1 == true) && (prev_usbstatus1 == false)) { 188 /* TODO: Move this call - it shouldn't be done in this function */
153 usb_stack_start();
154 } else if ((usbstatus1 == false) && (prev_usbstatus1 == true)) {
155 usb_stack_stop(); 189 usb_stack_stop();
156 }
157#else
158 if ((usbstatus1 == true) && (prev_usbstatus1 == false)) {
159 dr_controller_run();
160 } else if ((usbstatus1 == false) && (prev_usbstatus1 == true)) {
161 dr_controller_stop();
162 }
163#endif 190#endif
164 prev_usbstatus1 = usbstatus1; 191 return status;
165 usbstatus2 = (UDC_PORTSC1 & PORTSCX_CURRENT_CONNECT_STATUS) ? true : false; 192 }
193
194 /* We now know that we have just been connected to either a charger
195 or a computer */
166 196
167 if (usbstatus1 && usbstatus2) { 197 if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
168 return true; 198 {
169 } else { 199 /* The user wants to charge, so it doesn't matter what we are
170 return false; 200 connected to. */
201
202 status = USB_POWERED;
203 return status;
171 } 204 }
205
206 /* Run the USB controller for long enough to detect if we're connected
207 to a computer, then stop it again. */
208
209 dr_controller_run();
210
211 /* Wait for 50 ticks (500ms) before deciding there is no computer
212 attached. The required value varied a lot between different users
213 when this feature was being tested. */
214
215 countdown = 50;
216
217 return status;
172} 218}
diff --git a/firmware/target/coldfire/iaudio/usb-iaudio.c b/firmware/target/coldfire/iaudio/usb-iaudio.c
index 3bd1a7a458..21d69611b7 100644
--- a/firmware/target/coldfire/iaudio/usb-iaudio.c
+++ b/firmware/target/coldfire/iaudio/usb-iaudio.c
@@ -20,6 +20,7 @@
20#include <stdbool.h> 20#include <stdbool.h>
21#include "cpu.h" 21#include "cpu.h"
22#include "system.h" 22#include "system.h"
23#include "usb.h"
23 24
24void usb_init_device(void) 25void usb_init_device(void)
25{ 26{
@@ -30,9 +31,9 @@ void usb_init_device(void)
30 or_l(0x00800000, &GPIO1_FUNCTION); /* USB detect */ 31 or_l(0x00800000, &GPIO1_FUNCTION); /* USB detect */
31} 32}
32 33
33bool usb_detect(void) 34int usb_detect(void)
34{ 35{
35 return (GPIO1_READ & 0x00800000)?true:false; 36 return (GPIO1_READ & 0x00800000) ? USB_INSERTED : USB_EXTRACTED;
36} 37}
37 38
38void usb_enable(bool on) 39void usb_enable(bool on)
diff --git a/firmware/target/coldfire/iriver/h100/usb-h100.c b/firmware/target/coldfire/iriver/h100/usb-h100.c
index 3b00e967da..5a2f7f500b 100644
--- a/firmware/target/coldfire/iriver/h100/usb-h100.c
+++ b/firmware/target/coldfire/iriver/h100/usb-h100.c
@@ -21,6 +21,7 @@
21#include "cpu.h" 21#include "cpu.h"
22#include "system.h" 22#include "system.h"
23#include "kernel.h" 23#include "kernel.h"
24#include "usb.h"
24 25
25void usb_init_device(void) 26void usb_init_device(void)
26{ 27{
@@ -30,9 +31,9 @@ void usb_init_device(void)
30 or_l(0x01000040, &GPIO_FUNCTION); 31 or_l(0x01000040, &GPIO_FUNCTION);
31} 32}
32 33
33bool usb_detect(void) 34int usb_detect(void)
34{ 35{
35 return (GPIO1_READ & 0x80)?true:false; 36 return (GPIO1_READ & 0x80) ? USB_INSERTED : USB_EXTRACTED;
36} 37}
37 38
38void usb_enable(bool on) 39void usb_enable(bool on)
diff --git a/firmware/target/coldfire/iriver/h300/usb-h300.c b/firmware/target/coldfire/iriver/h300/usb-h300.c
index d08cc24dba..d50b7bc808 100644
--- a/firmware/target/coldfire/iriver/h300/usb-h300.c
+++ b/firmware/target/coldfire/iriver/h300/usb-h300.c
@@ -21,6 +21,7 @@
21#include "cpu.h" 21#include "cpu.h"
22#include "system.h" 22#include "system.h"
23#include "kernel.h" 23#include "kernel.h"
24#include "usb.h"
24 25
25void usb_init_device(void) 26void usb_init_device(void)
26{ 27{
@@ -35,9 +36,9 @@ void usb_init_device(void)
35 or_l(0x03000000, &GPIO_FUNCTION); 36 or_l(0x03000000, &GPIO_FUNCTION);
36} 37}
37 38
38bool usb_detect(void) 39int usb_detect(void)
39{ 40{
40 return (GPIO1_READ & 0x80)?true:false; 41 return (GPIO1_READ & 0x80) ? USB_INSERTED : USB_EXTRACTED;
41} 42}
42 43
43void usb_enable(bool on) 44void usb_enable(bool on)
diff --git a/firmware/target/sh/archos/fm_v2/usb-fm_v2.c b/firmware/target/sh/archos/fm_v2/usb-fm_v2.c
index 3dcc3559a3..9c641b925a 100644
--- a/firmware/target/sh/archos/fm_v2/usb-fm_v2.c
+++ b/firmware/target/sh/archos/fm_v2/usb-fm_v2.c
@@ -22,10 +22,11 @@
22#include "cpu.h" 22#include "cpu.h"
23#include "hwcompat.h" 23#include "hwcompat.h"
24#include "system.h" 24#include "system.h"
25#include "usb.h"
25 26
26bool usb_detect(void) 27int usb_detect(void)
27{ 28{
28 return (adc_read(ADC_USB_POWER) <= 512) ? true : false; 29 return (adc_read(ADC_USB_POWER) <= 512) ? USB_INSERTED : USB_EXTRACTED;
29} 30}
30 31
31void usb_enable(bool on) 32void usb_enable(bool on)
diff --git a/firmware/target/sh/archos/ondio/usb-ondio.c b/firmware/target/sh/archos/ondio/usb-ondio.c
index c856f3ae2c..b370fec3d9 100644
--- a/firmware/target/sh/archos/ondio/usb-ondio.c
+++ b/firmware/target/sh/archos/ondio/usb-ondio.c
@@ -23,10 +23,11 @@
23#include "cpu.h" 23#include "cpu.h"
24#include "hwcompat.h" 24#include "hwcompat.h"
25#include "system.h" 25#include "system.h"
26#include "usb.h"
26 27
27bool usb_detect(void) 28int usb_detect(void)
28{ 29{
29 return (adc_read(ADC_USB_POWER) <= 512) ? true : false; 30 return (adc_read(ADC_USB_POWER) <= 512) ? USB_INSERTED : USB_EXTRACTED;
30} 31}
31 32
32void usb_enable(bool on) 33void usb_enable(bool on)
diff --git a/firmware/target/sh/archos/player/usb-player.c b/firmware/target/sh/archos/player/usb-player.c
index c10e222f0a..e86003f2c2 100644
--- a/firmware/target/sh/archos/player/usb-player.c
+++ b/firmware/target/sh/archos/player/usb-player.c
@@ -20,10 +20,11 @@
20#include <stdbool.h> 20#include <stdbool.h>
21#include "cpu.h" 21#include "cpu.h"
22#include "system.h" 22#include "system.h"
23#include "usb.h"
23 24
24bool usb_detect(void) 25int usb_detect(void)
25{ 26{
26 return (PADR & 0x8000) ? false : true; 27 return (PADR & 0x8000) ? USB_INSERTED : USB_EXTRACTED;
27} 28}
28 29
29void usb_enable(bool on) 30void usb_enable(bool on)
diff --git a/firmware/target/sh/archos/recorder/usb-recorder.c b/firmware/target/sh/archos/recorder/usb-recorder.c
index 7ed237068e..dfa8462203 100644
--- a/firmware/target/sh/archos/recorder/usb-recorder.c
+++ b/firmware/target/sh/archos/recorder/usb-recorder.c
@@ -22,10 +22,11 @@
22#include "cpu.h" 22#include "cpu.h"
23#include "hwcompat.h" 23#include "hwcompat.h"
24#include "system.h" 24#include "system.h"
25#include "usb.h"
25 26
26bool usb_detect(void) 27int usb_detect(void)
27{ 28{
28 return (adc_read(ADC_USB_POWER) > 500) ? true : false; 29 return (adc_read(ADC_USB_POWER) > 500) ? USB_INSERTED : USB_EXTRACTED;
29} 30}
30 31
31void usb_enable(bool on) 32void usb_enable(bool on)
diff --git a/firmware/usb.c b/firmware/usb.c
index 9e9cb77919..876a5e4ec2 100644
--- a/firmware/usb.c
+++ b/firmware/usb.c
@@ -51,39 +51,6 @@ void screen_dump(void); /* Nasty again. Defined in apps/ too */
51 51
52#if !defined(SIMULATOR) && !defined(USB_NONE) 52#if !defined(SIMULATOR) && !defined(USB_NONE)
53 53
54/* Messages from usb_tick and thread states */
55#define USB_INSERTED 1
56#define USB_EXTRACTED 2
57#ifdef HAVE_MMC
58#define USB_REENABLE 3
59#endif
60#ifdef HAVE_USB_POWER
61#define USB_POWERED 4
62
63#if CONFIG_KEYPAD == RECORDER_PAD
64#define USBPOWER_BUTTON BUTTON_F1
65#define USBPOWER_BTN_IGNORE BUTTON_ON
66#elif CONFIG_KEYPAD == ONDIO_PAD
67#define USBPOWER_BUTTON BUTTON_MENU
68#define USBPOWER_BTN_IGNORE BUTTON_OFF
69#elif (CONFIG_KEYPAD == IPOD_4G_PAD)
70#define USBPOWER_BUTTON BUTTON_MENU
71#define USBPOWER_BTN_IGNORE BUTTON_PLAY
72#elif CONFIG_KEYPAD == IRIVER_H300_PAD
73#define USBPOWER_BUTTON BUTTON_REC
74#define USBPOWER_BTN_IGNORE BUTTON_ON
75#elif CONFIG_KEYPAD == GIGABEAT_PAD
76#define USBPOWER_BUTTON BUTTON_MENU
77#define USBPOWER_BTN_IGNORE BUTTON_POWER
78#elif CONFIG_KEYPAD == IRIVER_H10_PAD
79#define USBPOWER_BUTTON BUTTON_NONE
80#define USBPOWER_BTN_IGNORE BUTTON_POWER
81#elif CONFIG_KEYPAD == SANSA_E200_PAD
82#define USBPOWER_BUTTON BUTTON_SELECT
83#define USBPOWER_BTN_IGNORE BUTTON_POWER
84#endif
85#endif /* HAVE_USB_POWER */
86
87#define NUM_POLL_READINGS (HZ/5) 54#define NUM_POLL_READINGS (HZ/5)
88static int countdown; 55static int countdown;
89 56
@@ -100,7 +67,7 @@ static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
100static const char usb_thread_name[] = "usb"; 67static const char usb_thread_name[] = "usb";
101#endif 68#endif
102static struct event_queue usb_queue; 69static struct event_queue usb_queue;
103static bool last_usb_status; 70static int last_usb_status;
104static bool usb_monitor_enabled; 71static bool usb_monitor_enabled;
105 72
106 73
@@ -161,6 +128,11 @@ static void usb_thread(void)
161 queue_wait(&usb_queue, &ev); 128 queue_wait(&usb_queue, &ev);
162 switch(ev.id) 129 switch(ev.id)
163 { 130 {
131#ifdef HAVE_USB_POWER
132 case USB_POWERED:
133 usb_state = USB_POWERED;
134 break;
135#endif
164 case USB_INSERTED: 136 case USB_INSERTED:
165#ifdef HAVE_LCD_BITMAP 137#ifdef HAVE_LCD_BITMAP
166 if(do_screendump_instead_of_usb) 138 if(do_screendump_instead_of_usb)
@@ -278,7 +250,7 @@ static void usb_thread(void)
278#ifndef BOOTLOADER 250#ifndef BOOTLOADER
279static void usb_tick(void) 251static void usb_tick(void)
280{ 252{
281 bool current_status; 253 int current_status;
282 254
283 if(usb_monitor_enabled) 255 if(usb_monitor_enabled)
284 { 256 {
@@ -300,10 +272,7 @@ static void usb_tick(void)
300 readings in a row */ 272 readings in a row */
301 if(countdown == 0) 273 if(countdown == 0)
302 { 274 {
303 if(current_status) 275 queue_post(&usb_queue, current_status, 0);
304 queue_post(&usb_queue, USB_INSERTED, 0);
305 else
306 queue_post(&usb_queue, USB_EXTRACTED, 0);
307 } 276 }
308 } 277 }
309 } 278 }
@@ -463,9 +432,9 @@ void usb_start_monitoring(void)
463{ 432{
464} 433}
465 434
466bool usb_detect(void) 435int usb_detect(void)
467{ 436{
468 return false; 437 return USB_EXTRACTED;
469} 438}
470 439
471void usb_wait_for_disconnect(struct event_queue *q) 440void usb_wait_for_disconnect(struct event_queue *q)