1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * iPod driver based on code from the ipodlinux project - http://ipodlinux.org
13 * Adapted for Rockbox in January 2006
14 * Original file: podzilla/usb.c
15 * Copyright (C) 2005 Adam Johnston
17 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
45 #include "usb-target.h"
48 extern void dbg_ports(void); /* NASTY! defined in apps/ */
50 #ifdef HAVE_LCD_BITMAP
51 bool do_screendump_instead_of_usb
= false;
52 void screen_dump(void); /* Nasty again. Defined in apps/ too */
55 #define USB_REALLY_BRAVE
57 #if !defined(SIMULATOR) && !defined(USB_NONE)
59 /* Messages from usb_tick and thread states */
60 #define USB_INSERTED 1
61 #define USB_EXTRACTED 2
63 #define USB_REENABLE 3
68 #if CONFIG_KEYPAD == RECORDER_PAD
69 #define USBPOWER_BUTTON BUTTON_F1
70 #define USBPOWER_BTN_IGNORE BUTTON_ON
71 #elif CONFIG_KEYPAD == ONDIO_PAD
72 #define USBPOWER_BUTTON BUTTON_MENU
73 #define USBPOWER_BTN_IGNORE BUTTON_OFF
74 #elif (CONFIG_KEYPAD == IPOD_4G_PAD)
75 #define USBPOWER_BUTTON BUTTON_MENU
76 #define USBPOWER_BTN_IGNORE BUTTON_PLAY
77 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
78 #define USBPOWER_BUTTON BUTTON_REC
79 #define USBPOWER_BTN_IGNORE BUTTON_ON
80 #elif CONFIG_KEYPAD == GIGABEAT_PAD
81 #define USBPOWER_BUTTON BUTTON_MENU
82 #define USBPOWER_BTN_IGNORE BUTTON_POWER
83 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
84 #define USBPOWER_BUTTON BUTTON_NONE
85 #define USBPOWER_BTN_IGNORE BUTTON_POWER
86 #elif CONFIG_KEYPAD == SANSA_E200_PAD
87 #define USBPOWER_BUTTON BUTTON_SELECT
88 #define USBPOWER_BTN_IGNORE BUTTON_POWER
90 #endif /* HAVE_USB_POWER */
92 /* The ADC tick reads one channel per tick, and we want to check 3 successive
93 readings on the USB voltage channel. This doesn't apply to the Player, but
94 debouncing the USB detection port won't hurt us either. */
95 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
100 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
101 static int usb_mmc_countdown
= 0;
104 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
107 static long usb_stack
[(DEFAULT_STACK_SIZE
+ 0x800)/sizeof(long)];
108 static const char usb_thread_name
[] = "usb";
110 static struct event_queue usb_queue
;
111 static bool last_usb_status
;
112 static bool usb_monitor_enabled
;
115 void usb_enable(bool on
)
117 #ifdef USB_ENABLE_ONDIOSTYLE
118 PACR2
&= ~0x04C0; /* use PA3, PA5 as GPIO */
122 mmc_enable_int_flash_clock(!mmc_detect());
124 if (!(read_hw_mask() & MMC_CLOCK_POLARITY
))
125 and_b(~0x20, &PBDRH
); /* old circuit needs SCK1 = low while on USB */
126 or_b(0x20, &PADRL
); /* enable USB */
127 and_b(~0x08, &PADRL
); /* assert card detect */
131 if (!(read_hw_mask() & MMC_CLOCK_POLARITY
))
132 or_b(0x20, &PBDRH
); /* reset SCK1 = high for old circuit */
133 and_b(~0x20, &PADRL
); /* disable USB */
134 or_b(0x08, &PADRL
); /* deassert card detect */
136 or_b(0x28, &PAIORL
); /* output for USB enable and card detect */
137 #elif defined(USB_ISP1582)
138 /* TODO: Implement USB_ISP1582 */
140 #elif defined(USB_X5STYLE)
142 #elif defined(USB_GIGABEAT_STYLE)
145 #ifdef HAVE_LCD_BITMAP
146 if(read_hw_mask() & USB_ACTIVE_HIGH
)
151 and_b(~0x04, &PADRH
); /* enable USB */
163 static void usb_slave_mode(bool on
)
169 DEBUGF("Entering USB slave mode\n");
177 DEBUGF("Leaving USB slave mode\n");
179 /* Let the ISDx00 settle */
187 /* fixme: can we remove this? (already such in main.c) */
190 snprintf(str
, 31, "ATA error: %d", rc
);
192 lcd_puts(0, 1, "Press ON to debug");
194 while(!(button_get(true) & BUTTON_REL
)) {};
196 panicf("ata: %d",rc
);
199 rc
= disk_mount_all();
200 if (rc
<= 0) /* no partition */
201 panicf("mount: %d",rc
);
206 static void usb_thread(void)
208 int num_acks_to_expect
= -1;
209 bool waiting_for_ack
;
212 waiting_for_ack
= false;
216 queue_wait(&usb_queue
, &ev
);
220 #ifdef HAVE_LCD_BITMAP
221 if(do_screendump_instead_of_usb
)
227 #ifdef HAVE_USB_POWER
228 if((button_status() & ~USBPOWER_BTN_IGNORE
) == USBPOWER_BUTTON
)
230 usb_state
= USB_POWERED
;
235 /* Tell all threads that they have to back off the ATA.
236 We subtract one for our own thread. */
238 queue_broadcast(SYS_USB_CONNECTED
, 0) - 1;
239 waiting_for_ack
= true;
240 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
245 case SYS_USB_CONNECTED_ACK
:
248 num_acks_to_expect
--;
249 if(num_acks_to_expect
== 0)
251 DEBUGF("All threads have acknowledged the connect.\n");
252 #ifdef USB_REALLY_BRAVE
253 usb_slave_mode(true);
254 usb_state
= USB_INSERTED
;
262 DEBUGF("usb: got ack, %d to go...\n",
269 #ifdef HAVE_LCD_BITMAP
270 if(do_screendump_instead_of_usb
)
273 #ifdef HAVE_USB_POWER
274 if(usb_state
== USB_POWERED
)
276 usb_state
= USB_EXTRACTED
;
280 if(usb_state
== USB_INSERTED
)
282 /* Only disable the USB mode if we really have enabled it
283 some threads might not have acknowledged the
285 usb_slave_mode(false);
286 cpu_idle_mode(false);
289 usb_state
= USB_EXTRACTED
;
291 /* Tell all threads that we are back in business */
293 queue_broadcast(SYS_USB_DISCONNECTED
, 0) - 1;
294 waiting_for_ack
= true;
295 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
297 #ifdef HAVE_LCD_CHARCELLS
298 lcd_icon(ICON_USB
, false);
302 case SYS_USB_DISCONNECTED_ACK
:
305 num_acks_to_expect
--;
306 if(num_acks_to_expect
== 0)
308 DEBUGF("All threads have acknowledged. "
309 "We're in business.\n");
313 DEBUGF("usb: got ack, %d to go...\n",
320 case SYS_MMC_INSERTED
:
321 case SYS_MMC_EXTRACTED
:
322 if(usb_state
== USB_INSERTED
)
325 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
330 if(usb_state
== USB_INSERTED
)
331 usb_enable(true); /* reenable only if still inserted */
340 bool usb_detect(void)
344 #ifdef USB_RECORDERSTYLE
345 current_status
= (adc_read(ADC_USB_POWER
) > 500)?true:false;
347 #ifdef USB_FMRECORDERSTYLE
348 current_status
= (adc_read(ADC_USB_POWER
) <= 512)?true:false;
350 #ifdef USB_PLAYERSTYLE
351 current_status
= (PADR
& 0x8000)?false:true;
354 /* TODO: Implement USB_ISP1582 */
355 current_status
= false;
357 return current_status
;
362 static void usb_tick(void)
366 if(usb_monitor_enabled
)
368 current_status
= usb_detect();
370 /* Only report when the status has changed */
371 if(current_status
!= last_usb_status
)
373 last_usb_status
= current_status
;
374 countdown
= NUM_POLL_READINGS
;
378 /* Count down until it gets negative */
382 /* Report to the thread if we have had 3 identical status
387 queue_post(&usb_queue
, USB_INSERTED
, 0);
389 queue_post(&usb_queue
, USB_EXTRACTED
, 0);
394 if(usb_mmc_countdown
> 0)
397 if (usb_mmc_countdown
== 0)
398 queue_post(&usb_queue
, USB_REENABLE
, 0);
404 void usb_acknowledge(long id
)
406 queue_post(&usb_queue
, id
, 0);
411 usb_state
= USB_EXTRACTED
;
412 usb_monitor_enabled
= false;
421 /* We assume that the USB cable is extracted */
422 last_usb_status
= false;
425 queue_init(&usb_queue
, true);
426 create_thread(usb_thread
, usb_stack
, sizeof(usb_stack
),
427 usb_thread_name
IF_PRIO(, PRIORITY_SYSTEM
));
429 tick_add_task(usb_tick
);
434 void usb_wait_for_disconnect(struct event_queue
*q
)
438 /* Don't return until we get SYS_USB_DISCONNECTED */
442 if(ev
.id
== SYS_USB_DISCONNECTED
)
444 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
450 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
454 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
457 queue_wait_w_tmo(q
, &ev
, ticks
);
460 case SYS_USB_DISCONNECTED
:
461 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
471 void usb_start_monitoring(void)
473 usb_monitor_enabled
= true;
476 bool usb_inserted(void)
478 #ifdef HAVE_USB_POWER
479 return usb_state
== USB_INSERTED
|| usb_state
== USB_POWERED
;
481 return usb_state
== USB_INSERTED
;
485 #ifdef HAVE_USB_POWER
486 bool usb_powered(void)
488 return usb_state
== USB_POWERED
;
495 bool usb_inserted(void)
501 /* Dummy simulator functions */
502 void usb_acknowledge(long id
)
511 void usb_start_monitoring(void)
515 bool usb_detect(void)
520 void usb_wait_for_disconnect(struct event_queue
*q
)
525 #endif /* USB_NONE or SIMULATOR */