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
87 #endif /* HAVE_USB_POWER */
89 /* The ADC tick reads one channel per tick, and we want to check 3 successive
90 readings on the USB voltage channel. This doesn't apply to the Player, but
91 debouncing the USB detection port won't hurt us either. */
92 #define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
97 #if defined(HAVE_MMC) && !defined(BOOTLOADER)
98 static int usb_mmc_countdown
= 0;
101 /* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
104 static long usb_stack
[(DEFAULT_STACK_SIZE
+ 0x800)/sizeof(long)];
105 static const char usb_thread_name
[] = "usb";
107 static struct event_queue usb_queue
;
108 static bool last_usb_status
;
109 static bool usb_monitor_enabled
;
112 void usb_enable(bool on
)
114 #ifdef USB_ENABLE_ONDIOSTYLE
115 PACR2
&= ~0x04C0; /* use PA3, PA5 as GPIO */
119 mmc_enable_int_flash_clock(!mmc_detect());
121 if (!(read_hw_mask() & MMC_CLOCK_POLARITY
))
122 and_b(~0x20, &PBDRH
); /* old circuit needs SCK1 = low while on USB */
123 or_b(0x20, &PADRL
); /* enable USB */
124 and_b(~0x08, &PADRL
); /* assert card detect */
128 if (!(read_hw_mask() & MMC_CLOCK_POLARITY
))
129 or_b(0x20, &PBDRH
); /* reset SCK1 = high for old circuit */
130 and_b(~0x20, &PADRL
); /* disable USB */
131 or_b(0x08, &PADRL
); /* deassert card detect */
133 or_b(0x28, &PAIORL
); /* output for USB enable and card detect */
134 #elif defined(USB_GMINISTYLE)
136 int smsc_ver
= smsc_version();
158 #elif defined(USB_ISP1582)
159 /* TODO: Implement USB_ISP1582 */
161 #elif defined(USB_X5STYLE)
163 #elif defined(USB_GIGABEAT_STYLE)
166 #ifdef HAVE_LCD_BITMAP
167 if(read_hw_mask() & USB_ACTIVE_HIGH
)
172 and_b(~0x04, &PADRH
); /* enable USB */
184 static void usb_slave_mode(bool on
)
190 DEBUGF("Entering USB slave mode\n");
198 DEBUGF("Leaving USB slave mode\n");
200 /* Let the ISDx00 settle */
208 /* fixme: can we remove this? (already such in main.c) */
211 snprintf(str
, 31, "ATA error: %d", rc
);
213 lcd_puts(0, 1, "Press ON to debug");
215 while(!(button_get(true) & BUTTON_REL
)) {};
217 panicf("ata: %d",rc
);
220 rc
= disk_mount_all();
221 if (rc
<= 0) /* no partition */
222 panicf("mount: %d",rc
);
227 static void usb_thread(void)
229 int num_acks_to_expect
= -1;
230 bool waiting_for_ack
;
233 waiting_for_ack
= false;
237 queue_wait(&usb_queue
, &ev
);
241 #ifdef HAVE_LCD_BITMAP
242 if(do_screendump_instead_of_usb
)
248 #ifdef HAVE_USB_POWER
249 if((button_status() & ~USBPOWER_BTN_IGNORE
) == USBPOWER_BUTTON
)
251 usb_state
= USB_POWERED
;
256 /* Tell all threads that they have to back off the ATA.
257 We subtract one for our own thread. */
259 queue_broadcast(SYS_USB_CONNECTED
, NULL
) - 1;
260 waiting_for_ack
= true;
261 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
266 case SYS_USB_CONNECTED_ACK
:
269 num_acks_to_expect
--;
270 if(num_acks_to_expect
== 0)
272 DEBUGF("All threads have acknowledged the connect.\n");
273 #ifdef USB_REALLY_BRAVE
274 usb_slave_mode(true);
275 usb_state
= USB_INSERTED
;
283 DEBUGF("usb: got ack, %d to go...\n",
290 #ifdef HAVE_LCD_BITMAP
291 if(do_screendump_instead_of_usb
)
294 #ifdef HAVE_USB_POWER
295 if(usb_state
== USB_POWERED
)
297 usb_state
= USB_EXTRACTED
;
301 if(usb_state
== USB_INSERTED
)
303 /* Only disable the USB mode if we really have enabled it
304 some threads might not have acknowledged the
306 usb_slave_mode(false);
307 cpu_idle_mode(false);
310 usb_state
= USB_EXTRACTED
;
312 /* Tell all threads that we are back in business */
314 queue_broadcast(SYS_USB_DISCONNECTED
, NULL
) - 1;
315 waiting_for_ack
= true;
316 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
318 #ifdef HAVE_LCD_CHARCELLS
319 lcd_icon(ICON_USB
, false);
323 case SYS_USB_DISCONNECTED_ACK
:
326 num_acks_to_expect
--;
327 if(num_acks_to_expect
== 0)
329 DEBUGF("All threads have acknowledged. "
330 "We're in business.\n");
334 DEBUGF("usb: got ack, %d to go...\n",
341 case SYS_MMC_INSERTED
:
342 case SYS_MMC_EXTRACTED
:
343 if(usb_state
== USB_INSERTED
)
346 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
351 if(usb_state
== USB_INSERTED
)
352 usb_enable(true); /* reenable only if still inserted */
361 bool usb_detect(void)
365 #ifdef USB_RECORDERSTYLE
366 current_status
= (adc_read(ADC_USB_POWER
) > 500)?true:false;
368 #ifdef USB_FMRECORDERSTYLE
369 current_status
= (adc_read(ADC_USB_POWER
) <= 512)?true:false;
371 #ifdef USB_PLAYERSTYLE
372 current_status
= (PADR
& 0x8000)?false:true;
374 #ifdef USB_GMINISTYLE
375 current_status
= (P5
& 0x10)?true:false;
378 /* The following check is in the ipodlinux source, with the
379 comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
380 if (USB2D_IDENT
!= 0x22FA05) {
383 current_status
= (USB_STATUS
& 0x800)?true:false;
386 /* TODO: Implement USB_ISP1582 */
387 current_status
= false;
389 return current_status
;
394 static void usb_tick(void)
398 #ifdef USB_GMINISTYLE
399 /* Keep usb chip in usb state (?) */
401 if ((P10
& 0x20) == 0 || (P6
& 0x08) == 0) {
402 if (smsc_version() < 4) {
410 if(usb_monitor_enabled
)
412 current_status
= usb_detect();
414 /* Only report when the status has changed */
415 if(current_status
!= last_usb_status
)
417 last_usb_status
= current_status
;
418 countdown
= NUM_POLL_READINGS
;
422 /* Count down until it gets negative */
426 /* Report to the thread if we have had 3 identical status
431 queue_post(&usb_queue
, USB_INSERTED
, NULL
);
433 queue_post(&usb_queue
, USB_EXTRACTED
, NULL
);
438 if(usb_mmc_countdown
> 0)
441 if (usb_mmc_countdown
== 0)
442 queue_post(&usb_queue
, USB_REENABLE
, NULL
);
448 void usb_acknowledge(long id
)
450 queue_post(&usb_queue
, id
, NULL
);
455 usb_state
= USB_EXTRACTED
;
456 usb_monitor_enabled
= false;
465 /* We assume that the USB cable is extracted */
466 last_usb_status
= false;
469 queue_init(&usb_queue
, true);
470 create_thread(usb_thread
, usb_stack
, sizeof(usb_stack
),
471 usb_thread_name
IF_PRIO(, PRIORITY_SYSTEM
));
473 tick_add_task(usb_tick
);
478 void usb_wait_for_disconnect(struct event_queue
*q
)
482 /* Don't return until we get SYS_USB_DISCONNECTED */
486 if(ev
.id
== SYS_USB_DISCONNECTED
)
488 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
494 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
498 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
501 queue_wait_w_tmo(q
, &ev
, ticks
);
504 case SYS_USB_DISCONNECTED
:
505 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
515 void usb_start_monitoring(void)
517 usb_monitor_enabled
= true;
520 bool usb_inserted(void)
522 #ifdef HAVE_USB_POWER
523 return usb_state
== USB_INSERTED
|| usb_state
== USB_POWERED
;
525 return usb_state
== USB_INSERTED
;
529 #ifdef HAVE_USB_POWER
530 bool usb_powered(void)
532 return usb_state
== USB_POWERED
;
539 bool usb_inserted(void)
545 /* Dummy simulator functions */
546 void usb_acknowledge(long id
)
555 void usb_start_monitoring(void)
559 bool usb_detect(void)
564 void usb_wait_for_disconnect(struct event_queue
*q
)
569 #endif /* USB_NONE or SIMULATOR */