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
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-06-29 22:41:39 +00:00
|
|
|
*
|
|
|
|
* 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-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"
|
2006-03-22 11:06:45 +00:00
|
|
|
#include "usb-target.h"
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
#include "usb_core.h"
|
|
|
|
#endif
|
2007-02-01 10:47:22 +00:00
|
|
|
#ifdef IRIVER_H300_SERIES
|
|
|
|
#include "pcf50606.h" /* for pcf50606_usb_charging_... */
|
|
|
|
#endif
|
|
|
|
#include "logf.h"
|
2002-06-30 13:13:13 +00:00
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
/* Conditions under which we want the entire driver */
|
|
|
|
#if !defined(BOOTLOADER) || \
|
2008-04-24 20:08:28 +00:00
|
|
|
(defined(TOSHIBA_GIGABEAT_S) && defined(USE_ROCKBOX_USB) && defined(USB_STORAGE)) || \
|
2008-05-14 18:55:19 +00:00
|
|
|
(defined(CREATIVE_ZVx) && defined(HAVE_USBSTACK))
|
2008-04-18 16:42:50 +00:00
|
|
|
#define USB_FULL_INIT
|
|
|
|
#endif
|
|
|
|
|
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;
|
2008-04-18 16:42:50 +00:00
|
|
|
#if defined(USB_FULL_INIT) && defined(BOOTLOADER)
|
|
|
|
static void screen_dump(void) {}
|
|
|
|
#else
|
2004-06-30 13:31:14 +00:00
|
|
|
void screen_dump(void); /* Nasty again. Defined in apps/ too */
|
|
|
|
#endif
|
2008-04-18 16:42:50 +00:00
|
|
|
#endif
|
2004-06-30 13:31:14 +00:00
|
|
|
|
2003-12-12 13:30:15 +00:00
|
|
|
#if !defined(SIMULATOR) && !defined(USB_NONE)
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2007-04-11 20:44:32 +00:00
|
|
|
#define NUM_POLL_READINGS (HZ/5)
|
2002-07-01 19:32:54 +00:00
|
|
|
static int countdown;
|
|
|
|
|
2002-07-01 11:02:54 +00:00
|
|
|
static int usb_state;
|
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
#if defined(HAVE_MMC) && defined(USB_FULL_INIT)
|
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 */
|
2008-04-18 16:42:50 +00:00
|
|
|
#ifdef USB_FULL_INIT
|
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";
|
2008-02-27 19:08:30 +00:00
|
|
|
static struct thread_entry *usb_thread_entry;
|
2005-12-06 12:12:29 +00:00
|
|
|
#endif
|
2007-10-16 22:00:51 +00:00
|
|
|
static struct event_queue usb_queue;
|
2007-09-04 08:03:07 +00:00
|
|
|
static int last_usb_status;
|
2002-06-30 13:13:13 +00:00
|
|
|
static bool usb_monitor_enabled;
|
2008-03-02 00:43:45 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
static bool exclusive_ata_access;
|
|
|
|
#endif
|
2002-06-30 13:13:13 +00:00
|
|
|
|
|
|
|
|
2008-02-27 19:08:30 +00:00
|
|
|
#if defined(IPOD_COLOR) || defined(IPOD_4G) \
|
|
|
|
|| defined(IPOD_MINI) || defined(IPOD_MINI2G)
|
|
|
|
static int firewire_countdown;
|
|
|
|
static bool last_firewire_status;
|
|
|
|
#endif
|
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
#ifdef USB_FULL_INIT
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifndef HAVE_USBSTACK
|
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");
|
2007-11-22 20:51:00 +00:00
|
|
|
|
2002-07-01 11:02:54 +00:00
|
|
|
/* 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
|
|
|
}
|
|
|
|
}
|
2008-02-27 19:08:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void try_reboot(void)
|
|
|
|
{
|
|
|
|
#ifndef HAVE_FLASH_STORAGE
|
2008-03-21 20:22:04 +00:00
|
|
|
ata_sleepnow(); /* Immediately spindown the disk. */
|
|
|
|
sleep(HZ*2);
|
2008-02-27 19:08:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef IPOD_ARCH /* The following code is based on ipodlinux */
|
|
|
|
#if CONFIG_CPU == PP5020
|
2008-03-21 20:22:04 +00:00
|
|
|
memcpy((void *)0x40017f00, "diskmode\0\0hotstuff\0\0\1", 21);
|
2008-02-27 19:08:30 +00:00
|
|
|
#elif CONFIG_CPU == PP5022
|
2008-03-21 20:22:04 +00:00
|
|
|
memcpy((void *)0x4001ff00, "diskmode\0\0hotstuff\0\0\1", 21);
|
2008-02-27 19:08:30 +00:00
|
|
|
#endif /* CONFIG_CPU */
|
|
|
|
#endif /* IPOD_ARCH */
|
|
|
|
|
2008-03-21 20:22:04 +00:00
|
|
|
system_reboot(); /* Reboot */
|
2008-02-27 19:08:30 +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;
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2002-06-29 22:41:39 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-05-10 18:00:11 +00:00
|
|
|
#ifdef USB_DRIVER_CLOSE
|
|
|
|
case USB_QUIT:
|
|
|
|
return;
|
|
|
|
#endif
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
case USB_TRANSFER_COMPLETION:
|
|
|
|
usb_core_handle_transfer_completion((struct usb_transfer_completion_event_data*)ev.data);
|
|
|
|
break;
|
|
|
|
#endif
|
2007-09-04 08:03:07 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
|
|
|
case USB_POWERED:
|
|
|
|
usb_state = USB_POWERED;
|
|
|
|
break;
|
|
|
|
#endif
|
2002-07-01 11:02:54 +00:00
|
|
|
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
|
2008-03-21 20:22:04 +00:00
|
|
|
#if defined(IRIVER_H10) || defined (IRIVER_H10_5GB)
|
|
|
|
if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
|
|
|
|
#else
|
2005-10-28 18:47:25 +00:00
|
|
|
if((button_status() & ~USBPOWER_BTN_IGNORE) == USBPOWER_BUTTON)
|
2008-03-21 20:22:04 +00:00
|
|
|
#endif
|
2004-06-30 13:31:14 +00:00
|
|
|
{
|
2005-06-23 02:18:29 +00:00
|
|
|
usb_state = USB_POWERED;
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2008-03-06 21:25:09 +00:00
|
|
|
usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,false);
|
|
|
|
usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,true);
|
2008-02-27 19:08:30 +00:00
|
|
|
usb_enable(true);
|
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
}
|
|
|
|
else
|
2004-06-30 13:31:14 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
{
|
2008-03-02 00:15:02 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2008-03-22 22:37:26 +00:00
|
|
|
/* Set the state to USB_POWERED for now. if a real
|
|
|
|
connection is detected it will switch to USB_INSERTED */
|
|
|
|
usb_state = USB_POWERED;
|
2008-03-06 21:25:09 +00:00
|
|
|
usb_core_enable_driver(USB_DRIVER_MASS_STORAGE,true);
|
|
|
|
usb_core_enable_driver(USB_DRIVER_CHARGING_ONLY,false);
|
2008-03-02 00:15:02 +00:00
|
|
|
usb_enable(true);
|
|
|
|
#else
|
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 =
|
2006-12-19 16:50:07 +00:00
|
|
|
queue_broadcast(SYS_USB_CONNECTED, 0) - 1;
|
2004-06-30 13:31:14 +00:00
|
|
|
waiting_for_ack = true;
|
|
|
|
DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
|
|
|
|
num_acks_to_expect);
|
2008-03-02 00:15:02 +00:00
|
|
|
#endif
|
2004-06-30 13:31:14 +00:00
|
|
|
}
|
2002-07-01 11:02:54 +00:00
|
|
|
break;
|
2008-03-02 00:15:02 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
case USB_REQUEST_DISK:
|
|
|
|
if(!waiting_for_ack)
|
|
|
|
{
|
|
|
|
/* 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, 0) - 1;
|
|
|
|
waiting_for_ack = true;
|
|
|
|
DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
|
|
|
|
num_acks_to_expect);
|
|
|
|
}
|
|
|
|
break;
|
2008-03-10 20:55:24 +00:00
|
|
|
case USB_RELEASE_DISK:
|
|
|
|
if(!waiting_for_ack)
|
|
|
|
{
|
|
|
|
/* 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_DISCONNECTED, 0) - 1;
|
|
|
|
waiting_for_ack = true;
|
|
|
|
DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
|
|
|
|
num_acks_to_expect);
|
|
|
|
}
|
|
|
|
break;
|
2008-03-02 00:15:02 +00:00
|
|
|
#endif
|
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");
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2008-03-02 00:15:02 +00:00
|
|
|
#ifndef USE_ROCKBOX_USB
|
2008-02-27 19:08:30 +00:00
|
|
|
/* until we have native mass-storage mode, we want to reboot on
|
|
|
|
usb host connect */
|
2008-04-07 13:00:28 +00:00
|
|
|
usb_enable(true);
|
2008-02-27 19:08:30 +00:00
|
|
|
try_reboot();
|
|
|
|
#endif /* USE_ROCKBOX_USB */
|
2008-03-02 00:15:02 +00:00
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
thread_set_priority(usb_thread_entry,PRIORITY_REALTIME);
|
|
|
|
#endif
|
2008-03-21 19:34:46 +00:00
|
|
|
exclusive_ata_access = true;
|
2008-02-27 19:08:30 +00:00
|
|
|
|
|
|
|
#else
|
2002-07-01 11:02:54 +00:00
|
|
|
usb_slave_mode(true);
|
2005-07-05 08:02:48 +00:00
|
|
|
cpu_idle_mode(true);
|
2008-02-27 19:08:30 +00:00
|
|
|
#endif
|
|
|
|
usb_state = USB_INSERTED;
|
2008-03-02 00:15:02 +00:00
|
|
|
waiting_for_ack = false;
|
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:
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
usb_enable(false);
|
|
|
|
#ifdef HAVE_PRIORITY_SCHEDULING
|
|
|
|
thread_set_priority(usb_thread_entry,PRIORITY_SYSTEM);
|
|
|
|
#endif
|
|
|
|
#endif
|
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
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifndef HAVE_USBSTACK
|
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
|
|
|
}
|
2008-02-27 19:08:30 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
|
|
|
|
usb_state = USB_EXTRACTED;
|
2008-03-10 21:16:31 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2008-03-10 20:55:24 +00:00
|
|
|
if(exclusive_ata_access)
|
|
|
|
{
|
2008-08-20 20:32:12 +00:00
|
|
|
int rc = disk_mount_all();
|
|
|
|
if (rc <= 0) /* no partition */
|
|
|
|
panicf("mount: %d",rc);
|
2008-03-10 20:55:24 +00:00
|
|
|
exclusive_ata_access = false;
|
2008-03-10 21:16:31 +00:00
|
|
|
#endif
|
2008-03-10 20:55:24 +00:00
|
|
|
/* Tell all threads that we are back in business */
|
|
|
|
num_acks_to_expect =
|
|
|
|
queue_broadcast(SYS_USB_DISCONNECTED, 0) - 1;
|
|
|
|
waiting_for_ack = true;
|
|
|
|
DEBUGF("USB extracted. Waiting for ack from %d threads...\n",
|
|
|
|
num_acks_to_expect);
|
2008-03-10 21:16:31 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2008-03-10 20:55:24 +00:00
|
|
|
}
|
2008-03-10 21:16:31 +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");
|
2008-03-02 00:15:02 +00:00
|
|
|
waiting_for_ack = false;
|
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
|
|
|
|
2008-03-10 20:55:24 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
2007-06-30 02:08:27 +00:00
|
|
|
case SYS_HOTSWAP_INSERTED:
|
|
|
|
case SYS_HOTSWAP_EXTRACTED:
|
2008-03-10 20:55:24 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
usb_core_hotswap_event(1,ev.id == SYS_HOTSWAP_INSERTED);
|
|
|
|
#else
|
2004-10-10 19:51:11 +00:00
|
|
|
if(usb_state == USB_INSERTED)
|
|
|
|
{
|
|
|
|
usb_enable(false);
|
|
|
|
usb_mmc_countdown = HZ/2; /* re-enable after 0.5 sec */
|
|
|
|
}
|
|
|
|
break;
|
2008-03-10 20:55:24 +00:00
|
|
|
#endif
|
2004-10-10 19:51:11 +00:00
|
|
|
|
|
|
|
case USB_REENABLE:
|
|
|
|
if(usb_state == USB_INSERTED)
|
|
|
|
usb_enable(true); /* reenable only if still inserted */
|
|
|
|
break;
|
|
|
|
#endif
|
2008-02-27 19:08:30 +00:00
|
|
|
case USB_REQUEST_REBOOT:
|
2008-02-28 10:11:29 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
2008-02-27 19:08:30 +00:00
|
|
|
if((button_status() & ~USBPOWER_BTN_IGNORE) != USBPOWER_BUTTON)
|
2008-02-27 19:23:16 +00:00
|
|
|
#endif
|
2008-02-27 19:08:30 +00:00
|
|
|
try_reboot();
|
|
|
|
break;
|
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
|
|
|
|
2008-02-27 19:08:30 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
void usb_signal_transfer_completion(struct usb_transfer_completion_event_data* event_data)
|
|
|
|
{
|
|
|
|
queue_post(&usb_queue, USB_TRANSFER_COMPLETION, (intptr_t)event_data);
|
|
|
|
}
|
|
|
|
#endif
|
2003-07-03 22:11:14 +00:00
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
#ifdef USB_FULL_INIT
|
2003-07-03 22:11:14 +00:00
|
|
|
static void usb_tick(void)
|
|
|
|
{
|
2007-09-04 08:03:07 +00:00
|
|
|
int current_status;
|
2003-07-03 22:11:14 +00:00
|
|
|
|
|
|
|
if(usb_monitor_enabled)
|
|
|
|
{
|
2008-02-27 19:08:30 +00:00
|
|
|
#if defined(IPOD_COLOR) || defined(IPOD_4G) \
|
|
|
|
|| defined(IPOD_MINI) || defined(IPOD_MINI2G)
|
|
|
|
int current_firewire_status = firewire_detect();
|
|
|
|
if(current_firewire_status != last_firewire_status)
|
|
|
|
{
|
|
|
|
last_firewire_status = current_firewire_status;
|
|
|
|
firewire_countdown = NUM_POLL_READINGS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Count down until it gets negative */
|
|
|
|
if(firewire_countdown >= 0)
|
|
|
|
firewire_countdown--;
|
|
|
|
|
|
|
|
/* Report to the thread if we have had 3 identical status
|
|
|
|
readings in a row */
|
|
|
|
if(firewire_countdown == 0)
|
|
|
|
{
|
|
|
|
queue_post(&usb_queue, USB_REQUEST_REBOOT, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-03 22:11:14 +00:00
|
|
|
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)
|
|
|
|
{
|
2007-09-04 08:03:07 +00:00
|
|
|
queue_post(&usb_queue, current_status, 0);
|
2002-07-01 19:32:54 +00:00
|
|
|
}
|
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)
|
2006-12-19 16:50:07 +00:00
|
|
|
queue_post(&usb_queue, USB_REENABLE, 0);
|
2004-10-10 19:51:11 +00:00
|
|
|
}
|
|
|
|
#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
|
|
|
{
|
2006-12-19 16:50:07 +00:00
|
|
|
queue_post(&usb_queue, id, 0);
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init(void)
|
|
|
|
{
|
2002-07-01 11:02:54 +00:00
|
|
|
usb_state = USB_EXTRACTED;
|
2008-03-02 00:53:24 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
2008-03-02 00:43:45 +00:00
|
|
|
exclusive_ata_access = false;
|
2008-03-02 00:53:24 +00:00
|
|
|
#endif
|
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
|
|
|
|
2008-02-27 19:08:30 +00:00
|
|
|
#if defined(IPOD_COLOR) || defined(IPOD_4G) \
|
|
|
|
|| defined(IPOD_MINI) || defined(IPOD_MINI2G)
|
|
|
|
firewire_countdown = -1;
|
|
|
|
last_firewire_status = false;
|
|
|
|
#endif
|
|
|
|
|
2006-10-05 10:58:51 +00:00
|
|
|
usb_init_device();
|
2008-04-18 16:42:50 +00:00
|
|
|
#ifdef USB_FULL_INIT
|
2002-06-30 13:13:13 +00:00
|
|
|
usb_enable(false);
|
2008-04-07 13:00:28 +00:00
|
|
|
#endif
|
2002-06-30 13:13:13 +00:00
|
|
|
|
|
|
|
/* We assume that the USB cable is extracted */
|
2008-02-27 19:08:30 +00:00
|
|
|
last_usb_status = USB_EXTRACTED;
|
2004-10-14 21:58:33 +00:00
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
#ifdef USB_FULL_INIT
|
2006-09-16 16:18:11 +00:00
|
|
|
queue_init(&usb_queue, true);
|
2007-03-26 16:55:17 +00:00
|
|
|
|
2008-02-27 19:08:30 +00:00
|
|
|
usb_thread_entry = create_thread(usb_thread, usb_stack,
|
|
|
|
sizeof(usb_stack), 0, usb_thread_name
|
|
|
|
IF_PRIO(, PRIORITY_SYSTEM) IF_COP(, CPU));
|
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)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2002-06-30 13:13:13 +00:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2007-10-16 01:25:17 +00:00
|
|
|
struct queue_event ev;
|
2002-10-11 08:56:23 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2008-05-10 18:00:11 +00:00
|
|
|
#ifdef USB_DRIVER_CLOSE
|
|
|
|
void usb_close(void)
|
2008-04-18 16:42:50 +00:00
|
|
|
{
|
2008-05-10 18:00:11 +00:00
|
|
|
struct thread_entry *thread = usb_thread_entry;
|
|
|
|
usb_thread_entry = NULL;
|
|
|
|
|
|
|
|
if (thread == NULL)
|
|
|
|
return;
|
|
|
|
|
2008-04-18 16:42:50 +00:00
|
|
|
tick_remove_task(usb_tick);
|
|
|
|
usb_monitor_enabled = false;
|
2008-05-10 18:00:11 +00:00
|
|
|
|
|
|
|
queue_post(&usb_queue, USB_QUIT, 0);
|
|
|
|
thread_wait(thread);
|
2008-04-18 16:42:50 +00:00
|
|
|
}
|
2008-05-10 18:00:11 +00:00
|
|
|
#endif /* USB_DRIVER_CLOSE */
|
2008-04-18 16:42:50 +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
|
|
|
}
|
|
|
|
|
2008-03-02 00:15:02 +00:00
|
|
|
#ifdef HAVE_USBSTACK
|
|
|
|
void usb_request_exclusive_ata(void)
|
|
|
|
{
|
2008-03-20 22:05:11 +00:00
|
|
|
/* This is not really a clean place to start boosting the cpu. but it's
|
|
|
|
* currently the best one. We want to get rid of having to boost the cpu
|
|
|
|
* for usb anyway */
|
|
|
|
trigger_cpu_boost();
|
2008-03-02 00:43:45 +00:00
|
|
|
if(!exclusive_ata_access) {
|
2008-03-02 00:15:02 +00:00
|
|
|
queue_post(&usb_queue, USB_REQUEST_DISK, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-10 20:55:24 +00:00
|
|
|
void usb_release_exclusive_ata(void)
|
|
|
|
{
|
2008-03-20 22:05:11 +00:00
|
|
|
cancel_cpu_boost();
|
2008-03-10 20:55:24 +00:00
|
|
|
if(exclusive_ata_access) {
|
|
|
|
queue_post(&usb_queue, USB_RELEASE_DISK, 0);
|
|
|
|
exclusive_ata_access = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-02 00:15:02 +00:00
|
|
|
bool usb_exclusive_ata(void)
|
|
|
|
{
|
2008-03-02 00:43:45 +00:00
|
|
|
return exclusive_ata_access;
|
2008-03-02 00:15:02 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-06-23 02:18:29 +00:00
|
|
|
#ifdef HAVE_USB_POWER
|
|
|
|
bool usb_powered(void)
|
|
|
|
{
|
|
|
|
return usb_state == USB_POWERED;
|
|
|
|
}
|
2007-02-01 10:47:22 +00:00
|
|
|
|
2007-02-18 05:32:06 +00:00
|
|
|
#if CONFIG_CHARGING
|
2007-02-01 10:47:22 +00:00
|
|
|
bool usb_charging_enable(bool on)
|
|
|
|
{
|
|
|
|
bool rc = false;
|
|
|
|
#ifdef IRIVER_H300_SERIES
|
|
|
|
int irqlevel;
|
|
|
|
logf("usb_charging_enable(%s)\n", on ? "on" : "off" );
|
2008-03-26 01:50:41 +00:00
|
|
|
irqlevel = disable_irq_save();
|
2007-02-01 10:47:22 +00:00
|
|
|
pcf50606_set_usb_charging(on);
|
|
|
|
rc = on;
|
2008-03-26 01:50:41 +00:00
|
|
|
restore_irq(irqlevel);
|
2007-02-01 10:47:22 +00:00
|
|
|
#else
|
|
|
|
/* TODO: implement it for other targets... */
|
|
|
|
(void)on;
|
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool usb_charging_enabled(void)
|
|
|
|
{
|
|
|
|
bool rc = false;
|
|
|
|
#ifdef IRIVER_H300_SERIES
|
|
|
|
/* TODO: read the state of the GPOOD2 register...
|
|
|
|
* (this also means to set the irq level here) */
|
|
|
|
rc = pcf50606_usb_charging_enabled();
|
|
|
|
#else
|
|
|
|
/* TODO: implement it for other targets... */
|
|
|
|
#endif
|
|
|
|
|
2007-11-22 20:51:00 +00:00
|
|
|
logf("usb charging %s", rc ? "enabled" : "disabled" );
|
2007-02-01 10:47:22 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
#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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-09-04 08:03:07 +00:00
|
|
|
int usb_detect(void)
|
2003-07-20 00:08:09 +00:00
|
|
|
{
|
2007-09-04 08:03:07 +00:00
|
|
|
return USB_EXTRACTED;
|
2003-07-20 00:08:09 +00:00
|
|
|
}
|
|
|
|
|
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 */
|