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_IRIVERSTYLE)
161 /* Power on the Cypress chip */
162 #ifdef IRIVER_H100_SERIES
163 or_l(0x01000040, &GPIO_OUT
);
165 and_l(~0x00000008,&GPIO1_OUT
);
171 /* Power off the Cypress chip */
172 #ifdef IRIVER_H100_SERIES
173 and_l(~0x01000040, &GPIO_OUT
);
175 or_l(0x00000008,&GPIO1_OUT
);
178 #elif defined(USB_ISP1582)
179 /* TODO: Implement USB_ISP1582 */
181 #elif defined(USB_X5STYLE)
183 #elif defined(USB_GIGABEAT_STYLE)
186 #ifdef HAVE_LCD_BITMAP
187 if(read_hw_mask() & USB_ACTIVE_HIGH
)
192 and_b(~0x04, &PADRH
); /* enable USB */
204 static void usb_slave_mode(bool on
)
210 DEBUGF("Entering USB slave mode\n");
218 DEBUGF("Leaving USB slave mode\n");
220 /* Let the ISDx00 settle */
228 /* fixme: can we remove this? (already such in main.c) */
231 snprintf(str
, 31, "ATA error: %d", rc
);
233 lcd_puts(0, 1, "Press ON to debug");
235 while(!(button_get(true) & BUTTON_REL
)) {};
237 panicf("ata: %d",rc
);
240 rc
= disk_mount_all();
241 if (rc
<= 0) /* no partition */
242 panicf("mount: %d",rc
);
247 static void usb_thread(void)
249 int num_acks_to_expect
= -1;
250 bool waiting_for_ack
;
253 waiting_for_ack
= false;
257 queue_wait(&usb_queue
, &ev
);
261 #ifdef HAVE_LCD_BITMAP
262 if(do_screendump_instead_of_usb
)
268 #ifdef HAVE_USB_POWER
269 if((button_status() & ~USBPOWER_BTN_IGNORE
) == USBPOWER_BUTTON
)
271 usb_state
= USB_POWERED
;
276 /* Tell all threads that they have to back off the ATA.
277 We subtract one for our own thread. */
279 queue_broadcast(SYS_USB_CONNECTED
, NULL
) - 1;
280 waiting_for_ack
= true;
281 DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
286 case SYS_USB_CONNECTED_ACK
:
289 num_acks_to_expect
--;
290 if(num_acks_to_expect
== 0)
292 DEBUGF("All threads have acknowledged the connect.\n");
293 #ifdef USB_REALLY_BRAVE
294 usb_slave_mode(true);
295 usb_state
= USB_INSERTED
;
303 DEBUGF("usb: got ack, %d to go...\n",
310 #ifdef HAVE_LCD_BITMAP
311 if(do_screendump_instead_of_usb
)
314 #ifdef HAVE_USB_POWER
315 if(usb_state
== USB_POWERED
)
317 usb_state
= USB_EXTRACTED
;
321 if(usb_state
== USB_INSERTED
)
323 /* Only disable the USB mode if we really have enabled it
324 some threads might not have acknowledged the
326 usb_slave_mode(false);
327 cpu_idle_mode(false);
330 usb_state
= USB_EXTRACTED
;
332 /* Tell all threads that we are back in business */
334 queue_broadcast(SYS_USB_DISCONNECTED
, NULL
) - 1;
335 waiting_for_ack
= true;
336 DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
338 #ifdef HAVE_LCD_CHARCELLS
339 lcd_icon(ICON_USB
, false);
343 case SYS_USB_DISCONNECTED_ACK
:
346 num_acks_to_expect
--;
347 if(num_acks_to_expect
== 0)
349 DEBUGF("All threads have acknowledged. "
350 "We're in business.\n");
354 DEBUGF("usb: got ack, %d to go...\n",
361 case SYS_MMC_INSERTED
:
362 case SYS_MMC_EXTRACTED
:
363 if(usb_state
== USB_INSERTED
)
366 usb_mmc_countdown
= HZ
/2; /* re-enable after 0.5 sec */
371 if(usb_state
== USB_INSERTED
)
372 usb_enable(true); /* reenable only if still inserted */
381 bool usb_detect(void)
385 #ifdef USB_RECORDERSTYLE
386 current_status
= (adc_read(ADC_USB_POWER
) > 500)?true:false;
388 #ifdef USB_FMRECORDERSTYLE
389 current_status
= (adc_read(ADC_USB_POWER
) <= 512)?true:false;
391 #ifdef USB_PLAYERSTYLE
392 current_status
= (PADR
& 0x8000)?false:true;
394 #ifdef USB_IRIVERSTYLE
395 current_status
= (GPIO1_READ
& 0x80)?true:false;
397 #ifdef USB_GMINISTYLE
398 current_status
= (P5
& 0x10)?true:false;
401 /* The following check is in the ipodlinux source, with the
402 comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
403 if (USB2D_IDENT
!= 0x22FA05) {
406 current_status
= (USB_STATUS
& 0x800)?true:false;
409 /* TODO: Implement USB_ISP1582 */
410 current_status
= false;
412 return current_status
;
417 static void usb_tick(void)
421 #ifdef USB_GMINISTYLE
422 /* Keep usb chip in usb state (?) */
424 if ((P10
& 0x20) == 0 || (P6
& 0x08) == 0) {
425 if (smsc_version() < 4) {
433 if(usb_monitor_enabled
)
435 current_status
= usb_detect();
437 /* Only report when the status has changed */
438 if(current_status
!= last_usb_status
)
440 last_usb_status
= current_status
;
441 countdown
= NUM_POLL_READINGS
;
445 /* Count down until it gets negative */
449 /* Report to the thread if we have had 3 identical status
454 queue_post(&usb_queue
, USB_INSERTED
, NULL
);
456 queue_post(&usb_queue
, USB_EXTRACTED
, NULL
);
461 if(usb_mmc_countdown
> 0)
464 if (usb_mmc_countdown
== 0)
465 queue_post(&usb_queue
, USB_REENABLE
, NULL
);
471 void usb_acknowledge(long id
)
473 queue_post(&usb_queue
, id
, NULL
);
478 usb_state
= USB_EXTRACTED
;
479 usb_monitor_enabled
= false;
484 #elif defined USB_IRIVERSTYLE
485 or_l(0x00000080, &GPIO1_FUNCTION
); /* GPIO39 is the USB detect input */
487 #ifdef IRIVER_H300_SERIES
489 or_l(8,&GPIO1_FUNCTION
);
491 or_l(8,&GPIO1_ENABLE
);
493 /* Tristate the SCK/SDA to the ISD300 config EEPROM */
494 and_l(~0x03000000, &GPIO_ENABLE
);
495 or_l(0x03000000, &GPIO_FUNCTION
);
497 and_l(~0x01000040, &GPIO_OUT
); /* GPIO24 is the Cypress chip power */
498 or_l(0x01000040, &GPIO_ENABLE
);
499 or_l(0x01000040, &GPIO_FUNCTION
);
506 /* We assume that the USB cable is extracted */
507 last_usb_status
= false;
510 queue_init(&usb_queue
, true);
511 create_thread(usb_thread
, usb_stack
, sizeof(usb_stack
),
512 usb_thread_name
IF_PRIO(, PRIORITY_SYSTEM
));
514 tick_add_task(usb_tick
);
519 void usb_wait_for_disconnect(struct event_queue
*q
)
523 /* Don't return until we get SYS_USB_DISCONNECTED */
527 if(ev
.id
== SYS_USB_DISCONNECTED
)
529 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
535 int usb_wait_for_disconnect_w_tmo(struct event_queue
*q
, int ticks
)
539 /* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
542 queue_wait_w_tmo(q
, &ev
, ticks
);
545 case SYS_USB_DISCONNECTED
:
546 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
556 void usb_start_monitoring(void)
558 usb_monitor_enabled
= true;
561 bool usb_inserted(void)
563 #ifdef HAVE_USB_POWER
564 return usb_state
== USB_INSERTED
|| usb_state
== USB_POWERED
;
566 return usb_state
== USB_INSERTED
;
570 #ifdef HAVE_USB_POWER
571 bool usb_powered(void)
573 return usb_state
== USB_POWERED
;
580 bool usb_inserted(void)
586 /* Dummy simulator functions */
587 void usb_acknowledge(long id
)
596 void usb_start_monitoring(void)
600 bool usb_detect(void)
605 void usb_wait_for_disconnect(struct event_queue
*q
)
610 #endif /* USB_NONE or SIMULATOR */