2002-06-29 22:41:39 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
2006-01-07 00:43:48 +00:00
|
|
|
* iPod driver based on code from the ipodlinux project - http://ipodlinux.org
|
|
|
|
* Adapted for Rockbox in January 2006
|
|
|
|
* Original file: podzilla/usb.c
|
|
|
|
* Copyright (C) 2005 Adam Johnston
|
|
|
|
*
|
2002-06-29 22:41:39 +00:00
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
2004-11-02 22:57:51 +00:00
|
|
|
#include "cpu.h"
|
2002-06-29 22:41:39 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "debug.h"
|
2002-06-30 13:13:13 +00:00
|
|
|
#include "ata.h"
|
|
|
|
#include "fat.h"
|
|
|
|
#include "disk.h"
|
|
|
|
#include "panic.h"
|
2002-06-30 14:11:28 +00:00
|
|
|
#include "lcd.h"
|
2002-06-30 20:27:26 +00:00
|
|
|
#include "adc.h"
|
2002-07-01 12:19:12 +00:00
|
|
|
#include "usb.h"
|
2002-07-02 18:30:39 +00:00
|
|
|
#include "button.h"
|
|
|
|
#include "sprintf.h"
|
2006-01-07 00:43:48 +00:00
|
|
|
#include "string.h"
|
2003-05-17 22:12:11 +00:00
|
|
|
#include "hwcompat.h"
|
2004-10-10 19:51:11 +00:00
|
|
|
#ifdef HAVE_MMC
|
|
|
|
#include "ata_mmc.h"
|
|
|
|
#endif
|
2002-06-30 13:13:13 +00:00
|
|
|
|
2002-07-23 15:02:25 +00:00
|
|
|
extern void dbg_ports(void); /* NASTY! defined in apps/ */
|
2002-06-30 13:13:13 +00:00
|
|
|
|
2004-06-30 13:31:14 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
bool do_screendump_instead_of_usb = false;
|
|
|
|
void screen_dump(void); /* Nasty again. Defined in apps/ too */
|
|
|
|
#endif
|
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
#define USB_REALLY_BRAVE
|
|
|
|
|
2003-12-12 13:30:15 +00:00
|
|
|
#if !defined(SIMULATOR) && !defined(USB_NONE)
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2005-06-23 02:18:29 +00:00
|
|
|
/* Messages from usb_tick and thread states */
|
2002-06-29 22:41:39 +00:00
|
|
|
#define USB_INSERTED 1
|
2006-01-26 22:53:05 +00:00
|
|
|
#define USB_EXTRACTED 2
|
2004-10-10 19:51:11 +00:00
|
|
|
#ifdef HAVE_MMC
|
|
|
|
#define USB_REENABLE 3
|
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
|
|
|
#define USB_POWERED 4
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2005-06-23 02:18:29 +00:00
|
|
|
#if CONFIG_KEYPAD == RECORDER_PAD
|
|
|
|
#define USBPOWER_BUTTON BUTTON_F1
|
2005-10-28 18:47:25 +00:00
|
|
|
#define USBPOWER_BTN_IGNORE BUTTON_ON
|
2005-06-23 02:18:29 +00:00
|
|
|
#elif CONFIG_KEYPAD == ONDIO_PAD
|
|
|
|
#define USBPOWER_BUTTON BUTTON_MENU
|
2005-10-28 18:47:25 +00:00
|
|
|
#define USBPOWER_BTN_IGNORE BUTTON_OFF
|
2006-02-24 20:54:09 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IPOD_4G_PAD)
|
2006-01-07 00:43:48 +00:00
|
|
|
#define USBPOWER_BUTTON BUTTON_MENU
|
|
|
|
#define USBPOWER_BTN_IGNORE BUTTON_PLAY
|
2006-01-26 22:53:05 +00:00
|
|
|
#elif CONFIG_KEYPAD == IRIVER_H300_PAD
|
|
|
|
#define USBPOWER_BUTTON BUTTON_REC
|
|
|
|
#define USBPOWER_BTN_IGNORE BUTTON_ON
|
2005-06-23 02:18:29 +00:00
|
|
|
#endif
|
|
|
|
#endif /* HAVE_USB_POWER */
|
2002-07-01 19:32:54 +00:00
|
|
|
|
|
|
|
/* The ADC tick reads one channel per tick, and we want to check 3 successive
|
|
|
|
readings on the USB voltage channel. This doesn't apply to the Player, but
|
|
|
|
debouncing the USB detection port won't hurt us either. */
|
|
|
|
#define NUM_POLL_READINGS (NUM_ADC_CHANNELS * 3)
|
|
|
|
static int countdown;
|
|
|
|
|
2002-07-01 11:02:54 +00:00
|
|
|
static int usb_state;
|
|
|
|
|
2006-03-18 19:43:35 +00:00
|
|
|
#if defined(HAVE_MMC) && !defined(BOOTLOADER)
|
2004-10-10 19:51:11 +00:00
|
|
|
static int usb_mmc_countdown = 0;
|
|
|
|
#endif
|
|
|
|
|
2005-02-11 14:34:32 +00:00
|
|
|
/* FIXME: The extra 0x800 is consumed by fat_mount() when the fsinfo
|
2004-03-25 14:08:58 +00:00
|
|
|
needs updating */
|
2005-12-06 12:12:29 +00:00
|
|
|
#ifndef BOOTLOADER
|
2005-02-11 14:34:32 +00:00
|
|
|
static long usb_stack[(DEFAULT_STACK_SIZE + 0x800)/sizeof(long)];
|
2004-08-03 19:22:56 +00:00
|
|
|
static const char usb_thread_name[] = "usb";
|
2005-12-06 12:12:29 +00:00
|
|
|
#endif
|
2002-06-29 22:41:39 +00:00
|
|
|
static struct event_queue usb_queue;
|
2002-06-30 13:13:13 +00:00
|
|
|
static bool last_usb_status;
|
|
|
|
static bool usb_monitor_enabled;
|
|
|
|
|
2005-12-06 12:12:29 +00:00
|
|
|
void usb_enable(bool on)
|
2002-06-30 13:13:13 +00:00
|
|
|
{
|
2004-09-11 02:41:58 +00:00
|
|
|
#ifdef USB_ENABLE_ONDIOSTYLE
|
2004-10-14 21:58:33 +00:00
|
|
|
PACR2 &= ~0x04C0; /* use PA3, PA5 as GPIO */
|
2004-09-11 02:41:58 +00:00
|
|
|
if(on)
|
|
|
|
{
|
2004-10-10 19:51:11 +00:00
|
|
|
#ifdef HAVE_MMC
|
2005-05-22 00:42:00 +00:00
|
|
|
mmc_enable_int_flash_clock(!mmc_detect());
|
2004-10-10 19:51:11 +00:00
|
|
|
#endif
|
2004-11-17 20:14:43 +00:00
|
|
|
if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
|
|
|
|
and_b(~0x20, &PBDRH); /* old circuit needs SCK1 = low while on USB */
|
2004-09-11 02:41:58 +00:00
|
|
|
or_b(0x20, &PADRL); /* enable USB */
|
2004-09-13 07:39:00 +00:00
|
|
|
and_b(~0x08, &PADRL); /* assert card detect */
|
2004-09-11 02:41:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-17 20:14:43 +00:00
|
|
|
if (!(read_hw_mask() & MMC_CLOCK_POLARITY))
|
|
|
|
or_b(0x20, &PBDRH); /* reset SCK1 = high for old circuit */
|
2004-09-11 09:06:58 +00:00
|
|
|
and_b(~0x20, &PADRL); /* disable USB */
|
2004-09-13 07:39:00 +00:00
|
|
|
or_b(0x08, &PADRL); /* deassert card detect */
|
2004-09-11 02:41:58 +00:00
|
|
|
}
|
2004-09-13 07:39:00 +00:00
|
|
|
or_b(0x28, &PAIORL); /* output for USB enable and card detect */
|
2005-02-10 23:20:34 +00:00
|
|
|
#elif defined(USB_GMINISTYLE)
|
2005-02-02 17:14:53 +00:00
|
|
|
{
|
2005-02-19 17:49:58 +00:00
|
|
|
int smsc_ver = smsc_version();
|
2005-02-02 17:14:53 +00:00
|
|
|
if (on) {
|
2005-02-19 17:49:58 +00:00
|
|
|
if (smsc_ver < 4) {
|
2005-02-02 17:14:53 +00:00
|
|
|
P6 &= ~0x04;
|
|
|
|
P10 &= ~0x20;
|
|
|
|
|
2005-02-19 17:49:58 +00:00
|
|
|
smsc_delay();
|
2005-02-02 17:14:53 +00:00
|
|
|
|
|
|
|
P6 |= 0x08;
|
|
|
|
P10 |= 0x20;
|
2005-02-19 17:49:58 +00:00
|
|
|
|
|
|
|
smsc_delay();
|
2005-02-02 17:14:53 +00:00
|
|
|
}
|
|
|
|
P6 |= 0x10;
|
|
|
|
} else {
|
|
|
|
P6 &= ~0x10;
|
2005-02-19 17:49:58 +00:00
|
|
|
if (smsc_ver < 4) {
|
2005-02-02 17:14:53 +00:00
|
|
|
P6 &= ~0x04;
|
|
|
|
P10 &= ~0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-02-10 23:20:34 +00:00
|
|
|
#elif defined(USB_IRIVERSTYLE)
|
|
|
|
if(on)
|
|
|
|
{
|
|
|
|
/* Power on the Cypress chip */
|
2005-12-06 10:06:40 +00:00
|
|
|
#ifdef IRIVER_H100_SERIES
|
|
|
|
or_l(0x01000040, &GPIO_OUT);
|
2006-02-16 20:44:38 +00:00
|
|
|
#else
|
|
|
|
and_l(~0x00000008,&GPIO1_OUT);
|
2005-12-06 10:06:40 +00:00
|
|
|
#endif
|
2005-02-10 23:20:34 +00:00
|
|
|
sleep(2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Power off the Cypress chip */
|
2005-12-06 10:06:40 +00:00
|
|
|
#ifdef IRIVER_H100_SERIES
|
|
|
|
and_l(~0x01000040, &GPIO_OUT);
|
2006-02-16 20:44:38 +00:00
|
|
|
#else
|
|
|
|
or_l(0x00000008,&GPIO1_OUT);
|
2005-12-06 10:06:40 +00:00
|
|
|
#endif
|
2005-02-10 23:20:34 +00:00
|
|
|
}
|
|
|
|
|
2006-02-24 20:54:09 +00:00
|
|
|
#elif defined(USB_IPODSTYLE)
|
2006-01-07 00:43:48 +00:00
|
|
|
/* For the ipod, we can only do one thing with USB mode - reboot
|
|
|
|
into Apple's flash-based disk-mode. This does not return. */
|
|
|
|
if (on)
|
|
|
|
{
|
2006-01-19 03:01:34 +00:00
|
|
|
/* The following code is copied from ipodlinux */
|
2006-02-21 15:01:25 +00:00
|
|
|
#if defined(APPLE_IPODCOLOR) || defined(APPLE_IPOD3G) || \
|
|
|
|
defined(APPLE_IPOD4G) || defined(APPLE_IPODMINI)
|
2006-01-07 00:43:48 +00:00
|
|
|
unsigned char* storage_ptr = (unsigned char *)0x40017F00;
|
2006-01-19 03:01:34 +00:00
|
|
|
#elif defined(APPLE_IPODNANO) || defined(APPLE_IPODVIDEO)
|
|
|
|
unsigned char* storage_ptr = (unsigned char *)0x4001FF00;
|
|
|
|
#endif
|
|
|
|
memcpy(storage_ptr, "diskmode\0\0hotstuff\0\0\1", 21);
|
|
|
|
DEV_RS |= 4; /* Reboot */
|
2006-01-07 00:43:48 +00:00
|
|
|
}
|
2006-02-24 20:54:09 +00:00
|
|
|
|
2006-01-12 00:35:50 +00:00
|
|
|
#elif defined(USB_ISP1582)
|
|
|
|
/* TODO: Implement USB_ISP1582 */
|
|
|
|
(void) on;
|
2006-02-04 00:01:15 +00:00
|
|
|
#elif defined(USB_X5STYLE)
|
|
|
|
/* TODO X5 */
|
2005-02-02 17:14:53 +00:00
|
|
|
#else
|
2004-09-11 15:18:10 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
if(read_hw_mask() & USB_ACTIVE_HIGH)
|
|
|
|
on = !on;
|
|
|
|
#endif
|
2002-06-30 13:13:13 +00:00
|
|
|
if(on)
|
2003-11-07 21:03:41 +00:00
|
|
|
{
|
|
|
|
and_b(~0x04, &PADRH); /* enable USB */
|
|
|
|
}
|
2002-06-30 13:13:13 +00:00
|
|
|
else
|
2003-11-07 21:03:41 +00:00
|
|
|
{
|
|
|
|
or_b(0x04, &PADRH);
|
|
|
|
}
|
|
|
|
or_b(0x04, &PAIORH);
|
2004-09-11 02:41:58 +00:00
|
|
|
#endif
|
2002-06-30 13:13:13 +00:00
|
|
|
}
|
|
|
|
|
2005-12-06 12:12:29 +00:00
|
|
|
#ifndef BOOTLOADER
|
2002-06-30 13:13:13 +00:00
|
|
|
static void usb_slave_mode(bool on)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if(on)
|
|
|
|
{
|
2002-07-01 11:02:54 +00:00
|
|
|
DEBUGF("Entering USB slave mode\n");
|
2002-07-23 15:02:25 +00:00
|
|
|
ata_soft_reset();
|
2002-07-24 17:09:50 +00:00
|
|
|
ata_init();
|
2002-07-01 11:02:54 +00:00
|
|
|
ata_enable(false);
|
|
|
|
usb_enable(true);
|
2002-06-30 13:13:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-07-01 11:02:54 +00:00
|
|
|
DEBUGF("Leaving USB slave mode\n");
|
|
|
|
|
|
|
|
/* Let the ISDx00 settle */
|
|
|
|
sleep(HZ*1);
|
|
|
|
|
|
|
|
usb_enable(false);
|
2002-06-30 13:13:13 +00:00
|
|
|
|
2002-07-01 11:02:54 +00:00
|
|
|
rc = ata_init();
|
|
|
|
if(rc)
|
2002-07-02 17:49:05 +00:00
|
|
|
{
|
2004-12-29 22:10:24 +00:00
|
|
|
/* fixme: can we remove this? (already such in main.c) */
|
2002-07-02 17:49:05 +00:00
|
|
|
char str[32];
|
|
|
|
lcd_clear_display();
|
|
|
|
snprintf(str, 31, "ATA error: %d", rc);
|
2002-07-23 15:02:25 +00:00
|
|
|
lcd_puts(0, 0, str);
|
|
|
|
lcd_puts(0, 1, "Press ON to debug");
|
2002-07-02 17:49:05 +00:00
|
|
|
lcd_update();
|
2004-09-19 21:58:37 +00:00
|
|
|
while(!(button_get(true) & BUTTON_REL)) {};
|
2002-07-02 17:49:05 +00:00
|
|
|
dbg_ports();
|
2002-07-01 11:02:54 +00:00
|
|
|
panicf("ata: %d",rc);
|
2002-07-02 17:49:05 +00:00
|
|
|
}
|
2002-07-01 11:02:54 +00:00
|
|
|
|
2004-12-29 22:10:24 +00:00
|
|
|
rc = disk_mount_all();
|
|
|
|
if (rc <= 0) /* no partition */
|
2002-07-01 11:02:54 +00:00
|
|
|
panicf("mount: %d",rc);
|
2004-12-28 22:16:07 +00:00
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
2002-06-29 22:41:39 +00:00
|
|
|
|
|
|
|
static void usb_thread(void)
|
|
|
|
{
|
|
|
|
int num_acks_to_expect = -1;
|
|
|
|
bool waiting_for_ack;
|
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
waiting_for_ack = false;
|
2002-07-01 19:32:54 +00:00
|
|
|
|
2002-06-29 22:41:39 +00:00
|
|
|
while(1)
|
|
|
|
{
|
2002-07-01 11:02:54 +00:00
|
|
|
queue_wait(&usb_queue, &ev);
|
|
|
|
switch(ev.id)
|
|
|
|
{
|
|
|
|
case USB_INSERTED:
|
2004-06-30 13:31:14 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
if(do_screendump_instead_of_usb)
|
|
|
|
{
|
|
|
|
screen_dump();
|
|
|
|
}
|
|
|
|
else
|
2005-06-23 02:18:29 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_USB_POWER
|
2005-10-28 18:47:25 +00:00
|
|
|
if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
|
2004-06-30 13:31:14 +00:00
|
|
|
{
|
2005-06-23 02:18:29 +00:00
|
|
|
usb_state = USB_POWERED;
|
|
|
|
}
|
|
|
|
else
|
2004-06-30 13:31:14 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
{
|
2004-06-30 13:31:14 +00:00
|
|
|
/* Tell all threads that they have to back off the ATA.
|
|
|
|
We subtract one for our own thread. */
|
|
|
|
num_acks_to_expect =
|
|
|
|
queue_broadcast(SYS_USB_CONNECTED, NULL) - 1;
|
|
|
|
waiting_for_ack = true;
|
|
|
|
DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
|
|
|
|
num_acks_to_expect);
|
|
|
|
}
|
2002-07-01 11:02:54 +00:00
|
|
|
break;
|
2005-06-23 02:18:29 +00:00
|
|
|
|
2002-07-01 11:02:54 +00:00
|
|
|
case SYS_USB_CONNECTED_ACK:
|
|
|
|
if(waiting_for_ack)
|
|
|
|
{
|
|
|
|
num_acks_to_expect--;
|
|
|
|
if(num_acks_to_expect == 0)
|
|
|
|
{
|
2002-07-01 19:32:54 +00:00
|
|
|
DEBUGF("All threads have acknowledged the connect.\n");
|
2002-06-30 13:13:13 +00:00
|
|
|
#ifdef USB_REALLY_BRAVE
|
2002-07-01 11:02:54 +00:00
|
|
|
usb_slave_mode(true);
|
|
|
|
usb_state = USB_INSERTED;
|
2005-07-05 08:02:48 +00:00
|
|
|
cpu_idle_mode(true);
|
2002-06-30 13:13:13 +00:00
|
|
|
#else
|
2002-07-01 11:02:54 +00:00
|
|
|
system_reboot();
|
2002-06-30 13:13:13 +00:00
|
|
|
#endif
|
2002-07-01 11:02:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-07-01 19:32:54 +00:00
|
|
|
DEBUGF("usb: got ack, %d to go...\n",
|
|
|
|
num_acks_to_expect);
|
2002-07-01 11:02:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_EXTRACTED:
|
2004-06-30 13:31:14 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
2005-06-23 02:18:29 +00:00
|
|
|
if(do_screendump_instead_of_usb)
|
|
|
|
break;
|
2004-06-30 13:31:14 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
|
|
|
if(usb_state == USB_POWERED)
|
|
|
|
{
|
2004-06-30 13:31:14 +00:00
|
|
|
usb_state = USB_EXTRACTED;
|
2005-06-23 02:18:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-06-30 13:31:14 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
if(usb_state == USB_INSERTED)
|
|
|
|
{
|
|
|
|
/* Only disable the USB mode if we really have enabled it
|
|
|
|
some threads might not have acknowledged the
|
|
|
|
insertion */
|
|
|
|
usb_slave_mode(false);
|
2005-07-05 08:02:48 +00:00
|
|
|
cpu_idle_mode(false);
|
2004-06-30 13:31:14 +00:00
|
|
|
}
|
2005-06-23 02:18:29 +00:00
|
|
|
|
|
|
|
usb_state = USB_EXTRACTED;
|
|
|
|
|
|
|
|
/* Tell all threads that we are back in business */
|
|
|
|
num_acks_to_expect =
|
|
|
|
queue_broadcast(SYS_USB_DISCONNECTED, NULL) - 1;
|
|
|
|
waiting_for_ack = true;
|
|
|
|
DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
|
|
|
|
num_acks_to_expect);
|
|
|
|
#ifdef HAVE_LCD_CHARCELLS
|
|
|
|
lcd_icon(ICON_USB, false);
|
2002-08-23 02:17:00 +00:00
|
|
|
#endif
|
2002-07-01 11:02:54 +00:00
|
|
|
break;
|
2005-06-23 02:18:29 +00:00
|
|
|
|
2002-07-01 11:02:54 +00:00
|
|
|
case SYS_USB_DISCONNECTED_ACK:
|
|
|
|
if(waiting_for_ack)
|
|
|
|
{
|
|
|
|
num_acks_to_expect--;
|
|
|
|
if(num_acks_to_expect == 0)
|
|
|
|
{
|
2002-07-01 19:32:54 +00:00
|
|
|
DEBUGF("All threads have acknowledged. "
|
|
|
|
"We're in business.\n");
|
2002-07-01 11:02:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-07-01 19:32:54 +00:00
|
|
|
DEBUGF("usb: got ack, %d to go...\n",
|
|
|
|
num_acks_to_expect);
|
2002-07-01 11:02:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-10-10 19:51:11 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MMC
|
|
|
|
case SYS_MMC_INSERTED:
|
|
|
|
case SYS_MMC_EXTRACTED:
|
|
|
|
if(usb_state == USB_INSERTED)
|
|
|
|
{
|
|
|
|
usb_enable(false);
|
|
|
|
usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REENABLE:
|
|
|
|
if(usb_state == USB_INSERTED)
|
|
|
|
usb_enable(true); /* reenable only if still inserted */
|
|
|
|
break;
|
|
|
|
#endif
|
2002-07-01 11:02:54 +00:00
|
|
|
}
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-06 12:12:29 +00:00
|
|
|
#endif
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2003-07-03 22:11:14 +00:00
|
|
|
bool usb_detect(void)
|
2002-06-29 22:41:39 +00:00
|
|
|
{
|
2002-06-29 22:59:12 +00:00
|
|
|
bool current_status;
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2003-11-27 21:27:37 +00:00
|
|
|
#ifdef USB_RECORDERSTYLE
|
|
|
|
current_status = (adc_read(ADC_USB_POWER) > 500)?true:false;
|
|
|
|
#endif
|
|
|
|
#ifdef USB_FMRECORDERSTYLE
|
|
|
|
current_status = (adc_read(ADC_USB_POWER) <= 512)?true:false;
|
2003-01-16 15:11:29 +00:00
|
|
|
#endif
|
2003-11-27 21:27:37 +00:00
|
|
|
#ifdef USB_PLAYERSTYLE
|
|
|
|
current_status = (PADR & 0x8000)?false:true;
|
2002-06-29 22:41:39 +00:00
|
|
|
#endif
|
2005-07-08 15:00:31 +00:00
|
|
|
#ifdef USB_IRIVERSTYLE
|
2004-11-02 22:57:51 +00:00
|
|
|
current_status = (GPIO1_READ & 0x80)?true:false;
|
2005-02-02 17:14:53 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USB_GMINISTYLE
|
2005-02-19 17:49:58 +00:00
|
|
|
current_status = (P5 & 0x10)?true:false;
|
2004-11-02 22:57:51 +00:00
|
|
|
#endif
|
2005-11-11 17:51:35 +00:00
|
|
|
#ifdef USB_IPODSTYLE
|
2006-01-07 00:43:48 +00:00
|
|
|
/* The following check is in the ipodlinux source, with the
|
|
|
|
comment "USB2D_IDENT is bad" if USB2D_IDENT != 0x22FA05 */
|
|
|
|
if (USB2D_IDENT != 0x22FA05) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
current_status = (USB_STATUS & 0x800)?true:false;
|
2006-01-12 00:35:50 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USB_ISP1582
|
|
|
|
/* TODO: Implement USB_ISP1582 */
|
|
|
|
current_status = false;
|
2005-11-11 17:51:35 +00:00
|
|
|
#endif
|
2003-07-03 22:11:14 +00:00
|
|
|
return current_status;
|
|
|
|
}
|
|
|
|
|
2005-12-06 12:12:29 +00:00
|
|
|
#ifndef BOOTLOADER
|
2003-07-03 22:11:14 +00:00
|
|
|
static void usb_tick(void)
|
|
|
|
{
|
|
|
|
bool current_status;
|
|
|
|
|
2005-02-19 21:34:03 +00:00
|
|
|
#ifdef USB_GMINISTYLE
|
|
|
|
/* Keep usb chip in usb state (?) */
|
|
|
|
if (P5 & 0x10) {
|
|
|
|
if ((P10 & 0x20) == 0 || (P6 & 0x08) == 0) {
|
|
|
|
if (smsc_version() < 4) {
|
|
|
|
P6 |= 0x08;
|
|
|
|
P10 |= 0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-03 22:11:14 +00:00
|
|
|
if(usb_monitor_enabled)
|
|
|
|
{
|
|
|
|
current_status = usb_detect();
|
2002-07-01 11:02:54 +00:00
|
|
|
|
|
|
|
/* Only report when the status has changed */
|
|
|
|
if(current_status != last_usb_status)
|
|
|
|
{
|
|
|
|
last_usb_status = current_status;
|
2002-07-01 19:32:54 +00:00
|
|
|
countdown = NUM_POLL_READINGS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Count down until it gets negative */
|
|
|
|
if(countdown >= 0)
|
|
|
|
countdown--;
|
|
|
|
|
|
|
|
/* Report to the thread if we have had 3 identical status
|
|
|
|
readings in a row */
|
|
|
|
if(countdown == 0)
|
|
|
|
{
|
|
|
|
if(current_status)
|
|
|
|
queue_post(&usb_queue, USB_INSERTED, NULL);
|
|
|
|
else
|
|
|
|
queue_post(&usb_queue, USB_EXTRACTED, NULL);
|
|
|
|
}
|
2002-07-01 11:02:54 +00:00
|
|
|
}
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
2004-10-10 19:51:11 +00:00
|
|
|
#ifdef HAVE_MMC
|
|
|
|
if(usb_mmc_countdown > 0)
|
|
|
|
{
|
|
|
|
usb_mmc_countdown--;
|
|
|
|
if (usb_mmc_countdown == 0)
|
|
|
|
queue_post(&usb_queue, USB_REENABLE, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
2005-12-06 12:12:29 +00:00
|
|
|
#endif
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2005-01-30 15:16:58 +00:00
|
|
|
void usb_acknowledge(long id)
|
2002-06-29 22:41:39 +00:00
|
|
|
{
|
2002-07-23 15:02:25 +00:00
|
|
|
queue_post(&usb_queue, id, NULL);
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init(void)
|
|
|
|
{
|
2002-07-01 11:02:54 +00:00
|
|
|
usb_state = USB_EXTRACTED;
|
2002-06-30 13:13:13 +00:00
|
|
|
usb_monitor_enabled = false;
|
2002-07-01 19:32:54 +00:00
|
|
|
countdown = -1;
|
2002-07-03 14:16:20 +00:00
|
|
|
|
2005-07-08 15:00:31 +00:00
|
|
|
#ifdef USB_IRIVERSTYLE
|
2005-07-12 07:25:01 +00:00
|
|
|
or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */
|
2005-12-06 10:06:40 +00:00
|
|
|
|
|
|
|
#ifdef IRIVER_H300_SERIES
|
2006-02-16 20:44:38 +00:00
|
|
|
/* ISD300 3.3V ON */
|
|
|
|
or_l(8,&GPIO1_FUNCTION);
|
|
|
|
or_l(8,&GPIO1_OUT);
|
|
|
|
or_l(8,&GPIO1_ENABLE);
|
|
|
|
|
2005-12-06 10:06:40 +00:00
|
|
|
/* Tristate the SCK/SDA to the ISD300 config EEPROM */
|
|
|
|
and_l(~0x03000000, &GPIO_ENABLE);
|
|
|
|
or_l(0x03000000, &GPIO_FUNCTION);
|
|
|
|
#else
|
|
|
|
and_l(~0x01000040, &GPIO_OUT); /* GPIO24 is the Cypress chip power */
|
|
|
|
or_l(0x01000040, &GPIO_ENABLE);
|
|
|
|
or_l(0x01000040, &GPIO_FUNCTION);
|
|
|
|
#endif
|
|
|
|
|
2006-01-07 00:43:48 +00:00
|
|
|
#elif defined(USB_IPODSTYLE)
|
|
|
|
int r0;
|
|
|
|
outl(inl(0x70000084) | 0x200, 0x70000084);
|
|
|
|
|
|
|
|
outl(inl(0x7000002C) | 0x3000000, 0x7000002C);
|
|
|
|
outl(inl(0x6000600C) | 0x400000, 0x6000600C);
|
|
|
|
|
|
|
|
outl(inl(0x60006004) | 0x400000, 0x60006004); /* reset usb start */
|
|
|
|
outl(inl(0x60006004) & ~0x400000, 0x60006004); /* reset usb end */
|
|
|
|
|
|
|
|
outl(inl(0x70000020) | 0x80000000, 0x70000020);
|
|
|
|
while ((inl(0x70000028) & 0x80) == 0);
|
|
|
|
|
|
|
|
outl(inl(0xc5000184) | 0x100, 0xc5000184);
|
|
|
|
while ((inl(0xc5000184) & 0x100) != 0);
|
|
|
|
|
|
|
|
outl(inl(0xc50001A4) | 0x5F000000, 0xc50001A4);
|
|
|
|
if ((inl(0xc50001A4) & 0x100) == 0) {
|
|
|
|
outl(inl(0xc50001A8) & ~0x3, 0xc50001A8);
|
|
|
|
outl(inl(0xc50001A8) | 0x2, 0xc50001A8);
|
|
|
|
outl(inl(0x70000028) | 0x4000, 0x70000028);
|
|
|
|
outl(inl(0x70000028) | 0x2, 0x70000028);
|
|
|
|
} else {
|
|
|
|
outl(inl(0xc50001A8) | 0x3, 0xc50001A8);
|
|
|
|
outl(inl(0x70000028) &~0x4000, 0x70000028);
|
|
|
|
outl(inl(0x70000028) | 0x2, 0x70000028);
|
|
|
|
}
|
|
|
|
outl(inl(0xc5000140) | 0x2, 0xc5000140);
|
|
|
|
while((inl(0xc5000140) & 0x2) != 0);
|
|
|
|
r0 = inl(0xc5000184);
|
|
|
|
|
|
|
|
/* Note from IPL source (referring to next 5 lines of code:
|
|
|
|
THIS NEEDS TO BE CHANGED ONCE THERE IS KERNEL USB */
|
|
|
|
outl(inl(0x70000020) | 0x80000000, 0x70000020);
|
|
|
|
outl(inl(0x6000600C) | 0x400000, 0x6000600C);
|
|
|
|
while ((inl(0x70000028) & 0x80) == 0);
|
|
|
|
outl(inl(0x70000028) | 0x2, 0x70000028);
|
|
|
|
|
|
|
|
udelay(0x186A0);
|
2004-11-02 22:57:51 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
usb_enable(false);
|
|
|
|
|
|
|
|
/* We assume that the USB cable is extracted */
|
|
|
|
last_usb_status = false;
|
2004-10-14 21:58:33 +00:00
|
|
|
|
2005-12-06 12:12:29 +00:00
|
|
|
#ifndef BOOTLOADER
|
2002-06-29 22:41:39 +00:00
|
|
|
queue_init(&usb_queue);
|
2002-07-15 22:19:49 +00:00
|
|
|
create_thread(usb_thread, usb_stack, sizeof(usb_stack), usb_thread_name);
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
tick_add_task(usb_tick);
|
2005-12-06 12:12:29 +00:00
|
|
|
#endif
|
2006-02-24 20:54:09 +00:00
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_wait_for_disconnect(struct event_queue *q)
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
/* Don't return until we get SYS_USB_DISCONNECTED */
|
|
|
|
while(1)
|
|
|
|
{
|
2002-07-01 11:02:54 +00:00
|
|
|
queue_wait(q, &ev);
|
|
|
|
if(ev.id == SYS_USB_DISCONNECTED)
|
|
|
|
{
|
|
|
|
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
|
|
|
|
return;
|
|
|
|
}
|
2002-06-30 13:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-11 08:56:23 +00:00
|
|
|
int usb_wait_for_disconnect_w_tmo(struct event_queue *q, int ticks)
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
/* Don't return until we get SYS_USB_DISCONNECTED or SYS_TIMEOUT */
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
queue_wait_w_tmo(q, &ev, ticks);
|
|
|
|
switch(ev.id)
|
|
|
|
{
|
|
|
|
case SYS_USB_DISCONNECTED:
|
|
|
|
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case SYS_TIMEOUT:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
void usb_start_monitoring(void)
|
|
|
|
{
|
|
|
|
usb_monitor_enabled = true;
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-25 19:25:10 +00:00
|
|
|
bool usb_inserted(void)
|
|
|
|
{
|
2006-02-19 12:27:21 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
2006-02-19 11:37:30 +00:00
|
|
|
return usb_state == USB_INSERTED || usb_state == USB_POWERED;
|
2006-02-19 12:27:21 +00:00
|
|
|
#else
|
|
|
|
return usb_state == USB_INSERTED;
|
|
|
|
#endif
|
2002-09-25 19:25:10 +00:00
|
|
|
}
|
|
|
|
|
2005-06-23 02:18:29 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
|
|
|
bool usb_powered(void)
|
|
|
|
{
|
|
|
|
return usb_state == USB_POWERED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-29 22:41:39 +00:00
|
|
|
#else
|
|
|
|
|
2003-12-12 13:30:15 +00:00
|
|
|
#ifdef USB_NONE
|
|
|
|
bool usb_inserted(void)
|
|
|
|
{
|
2003-12-15 11:44:06 +00:00
|
|
|
return false;
|
2003-12-12 13:30:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-29 22:41:39 +00:00
|
|
|
/* Dummy simulator functions */
|
2005-01-30 15:16:58 +00:00
|
|
|
void usb_acknowledge(long id)
|
2002-06-29 22:41:39 +00:00
|
|
|
{
|
|
|
|
id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
void usb_start_monitoring(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-07-20 00:08:09 +00:00
|
|
|
bool usb_detect(void)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-24 20:54:09 +00:00
|
|
|
void usb_wait_for_disconnect(struct event_queue *q)
|
|
|
|
{
|
2006-02-24 21:09:34 +00:00
|
|
|
(void)q;
|
2006-02-24 20:54:09 +00:00
|
|
|
}
|
|
|
|
|
2003-12-12 13:30:15 +00:00
|
|
|
#endif /* USB_NONE or SIMULATOR */
|