2002-06-29 22:41:39 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "sh7034.h"
|
|
|
|
#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-06-30 13:13:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define USB_REALLY_BRAVE
|
|
|
|
|
2002-06-29 22:41:39 +00:00
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
|
|
|
|
#define USB_INSERTED 1
|
|
|
|
#define USB_EXTRACTED 2
|
|
|
|
|
|
|
|
static char usb_stack[0x100];
|
|
|
|
static struct event_queue usb_queue;
|
2002-06-30 13:13:13 +00:00
|
|
|
static bool last_usb_status;
|
|
|
|
static bool usb_monitor_enabled;
|
|
|
|
|
|
|
|
static void usb_enable(bool on)
|
|
|
|
{
|
|
|
|
#ifdef ARCHOS_RECORDER
|
|
|
|
if(!on) /* The pin is inverted on the Recorder */
|
|
|
|
#else
|
|
|
|
if(on)
|
|
|
|
#endif
|
|
|
|
PADR &= ~0x400; /* enable USB */
|
|
|
|
else
|
|
|
|
PADR |= 0x400;
|
|
|
|
PAIOR |= 0x400;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_slave_mode(bool on)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct partinfo* pinfo;
|
|
|
|
|
|
|
|
if(on)
|
|
|
|
{
|
|
|
|
DEBUGF("Entering USB slave mode\n");
|
|
|
|
ata_enable(false);
|
|
|
|
usb_enable(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("Leaving USB slave mode\n");
|
|
|
|
|
|
|
|
/* Let the ISDx00 settle */
|
|
|
|
sleep(HZ*1);
|
|
|
|
|
|
|
|
usb_enable(false);
|
|
|
|
|
|
|
|
rc = ata_init();
|
|
|
|
if(rc)
|
|
|
|
panicf("ata: %d",rc);
|
|
|
|
|
|
|
|
pinfo = disk_init();
|
|
|
|
if (!pinfo)
|
|
|
|
panicf("disk: NULL");
|
|
|
|
|
|
|
|
rc = fat_mount(pinfo[0].start);
|
|
|
|
if(rc)
|
|
|
|
panicf("mount: %d",rc);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
queue_wait(&usb_queue, &ev);
|
|
|
|
switch(ev.id)
|
|
|
|
{
|
|
|
|
case USB_INSERTED:
|
|
|
|
/* 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;
|
2002-06-30 13:13:13 +00:00
|
|
|
DEBUGF("USB inserted. Waiting for ack from %d threads...\n",
|
|
|
|
num_acks_to_expect);
|
2002-06-29 22:41:39 +00:00
|
|
|
break;
|
2002-06-30 14:11:28 +00:00
|
|
|
|
2002-06-29 22:41:39 +00:00
|
|
|
case SYS_USB_CONNECTED_ACK:
|
|
|
|
if(waiting_for_ack)
|
|
|
|
{
|
|
|
|
num_acks_to_expect--;
|
|
|
|
if(num_acks_to_expect == 0)
|
|
|
|
{
|
|
|
|
/* This is where we are supposed to be cool and keep
|
|
|
|
the Rockbox firmware running while the USB is enabled,
|
|
|
|
maybe even play some games and stuff. However, the
|
|
|
|
current firmware isn't quite ready for this yet.
|
|
|
|
Let's just chicken out and reboot. */
|
2002-06-30 14:11:28 +00:00
|
|
|
DEBUGF("All threads have acknowledged. Continuing...\n");
|
2002-06-30 13:13:13 +00:00
|
|
|
#ifdef USB_REALLY_BRAVE
|
|
|
|
usb_slave_mode(true);
|
|
|
|
#else
|
2002-06-29 22:41:39 +00:00
|
|
|
system_reboot();
|
2002-06-30 13:13:13 +00:00
|
|
|
#endif
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("usb: got ack, %d to go...\n", num_acks_to_expect);
|
|
|
|
}
|
|
|
|
}
|
2002-06-30 13:13:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_EXTRACTED:
|
2002-06-30 14:11:28 +00:00
|
|
|
/* First disable the USB mode */
|
|
|
|
usb_slave_mode(false);
|
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
/* 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);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SYS_USB_DISCONNECTED_ACK:
|
|
|
|
if(waiting_for_ack)
|
|
|
|
{
|
|
|
|
num_acks_to_expect--;
|
|
|
|
if(num_acks_to_expect == 0)
|
|
|
|
{
|
|
|
|
DEBUGF("All threads have acknowledged. We're in business.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("usb: got ack, %d to go...\n", num_acks_to_expect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_tick(void)
|
|
|
|
{
|
2002-06-29 22:59:12 +00:00
|
|
|
bool current_status;
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
if(usb_monitor_enabled)
|
|
|
|
{
|
2002-06-29 22:41:39 +00:00
|
|
|
#ifdef ARCHOS_RECORDER
|
2002-06-30 20:24:57 +00:00
|
|
|
/* If AN2 reads more than about 500, the USB is inserted */
|
|
|
|
current_status = (adc_read(2) > 500);
|
2002-06-29 22:41:39 +00:00
|
|
|
#else
|
2002-06-30 13:13:13 +00:00
|
|
|
current_status = (PADR & 0x8000)?false:true;
|
2002-06-29 22:41:39 +00:00
|
|
|
#endif
|
2002-06-30 13:13:13 +00:00
|
|
|
|
|
|
|
/* Only report when the status has changed */
|
|
|
|
if(current_status != last_usb_status)
|
|
|
|
{
|
|
|
|
last_usb_status = current_status;
|
|
|
|
if(current_status)
|
|
|
|
queue_post(&usb_queue, USB_INSERTED, NULL);
|
|
|
|
else
|
|
|
|
queue_post(&usb_queue, USB_EXTRACTED, NULL);
|
|
|
|
}
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_acknowledge(int id)
|
|
|
|
{
|
|
|
|
queue_post(&usb_queue, id, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init(void)
|
|
|
|
{
|
2002-06-30 13:13:13 +00:00
|
|
|
usb_monitor_enabled = false;
|
2002-06-29 22:41:39 +00:00
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
usb_enable(false);
|
|
|
|
|
|
|
|
/* We assume that the USB cable is extracted */
|
|
|
|
last_usb_status = false;
|
2002-06-29 22:41:39 +00:00
|
|
|
|
|
|
|
queue_init(&usb_queue);
|
|
|
|
create_thread(usb_thread, usb_stack, sizeof(usb_stack));
|
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
tick_add_task(usb_tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_wait_for_disconnect(struct event_queue *q)
|
|
|
|
{
|
|
|
|
struct event ev;
|
|
|
|
|
|
|
|
/* Don't return until we get SYS_USB_DISCONNECTED */
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
queue_wait(q, &ev);
|
|
|
|
if(ev.id == SYS_USB_DISCONNECTED)
|
|
|
|
{
|
|
|
|
usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_start_monitoring(void)
|
|
|
|
{
|
|
|
|
usb_monitor_enabled = true;
|
2002-06-29 22:41:39 +00:00
|
|
|
}
|
|
|
|
|
2002-06-30 14:11:28 +00:00
|
|
|
void usb_display_info(void)
|
|
|
|
{
|
|
|
|
lcd_stop_scroll();
|
|
|
|
lcd_clear_display();
|
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
lcd_puts(4, 3, "[USB Mode]");
|
|
|
|
lcd_update();
|
|
|
|
#else
|
|
|
|
lcd_puts(0, 0, "[USB Mode]");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-06-29 22:41:39 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* Dummy simulator functions */
|
|
|
|
void usb_acknowledge(int id)
|
|
|
|
{
|
|
|
|
id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-06-30 13:13:13 +00:00
|
|
|
void usb_start_monitoring(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-06-30 14:11:28 +00:00
|
|
|
void usb_display_info(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-06-29 22:41:39 +00:00
|
|
|
#endif
|