2011-09-04 11:48:28 +00:00
|
|
|
/* USBqemu
|
|
|
|
* Copyright (C) 2002-2011 PCSX2 Team
|
2011-09-04 01:36:14 +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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-10-19 20:27:50 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
2011-09-04 01:36:14 +00:00
|
|
|
*/
|
|
|
|
|
2011-09-04 11:48:28 +00:00
|
|
|
#include "qemu-usb/USBinternal.h"
|
2011-09-04 01:36:14 +00:00
|
|
|
|
2011-09-04 11:48:28 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include "svnrev.h"
|
|
|
|
#endif
|
2011-09-04 01:36:14 +00:00
|
|
|
|
|
|
|
const unsigned char version = PS2E_USB_VERSION;
|
|
|
|
const unsigned char revision = 0;
|
|
|
|
const unsigned char build = 1; // increase that with each version
|
|
|
|
|
2011-09-04 11:48:28 +00:00
|
|
|
// PCSX2 expects ASNI, not unicode, so this MUST always be char...
|
|
|
|
static char libraryName[256];
|
2011-09-04 01:36:14 +00:00
|
|
|
|
|
|
|
OHCIState *qemu_ohci;
|
|
|
|
|
|
|
|
Config conf;
|
|
|
|
|
2011-09-04 11:48:28 +00:00
|
|
|
HWND gsWindowHandle=NULL;
|
2011-09-04 01:36:14 +00:00
|
|
|
|
|
|
|
u8 *ram;
|
|
|
|
USBcallback _USBirq;
|
|
|
|
FILE *usbLog;
|
2011-09-04 01:53:30 +00:00
|
|
|
int64_t usb_frame_time=0;
|
|
|
|
int64_t usb_bit_time=0;
|
2011-09-04 01:36:14 +00:00
|
|
|
|
2011-09-04 01:53:30 +00:00
|
|
|
s64 clocks=0;
|
|
|
|
s64 remaining=0;
|
2011-09-04 01:36:14 +00:00
|
|
|
|
2011-09-04 01:53:30 +00:00
|
|
|
int64_t get_ticks_per_sec()
|
|
|
|
{
|
|
|
|
return PSXCLK;
|
2011-09-04 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __Log(char *fmt, ...) {
|
|
|
|
va_list list;
|
|
|
|
|
|
|
|
if (!conf.Log) return;
|
|
|
|
|
|
|
|
va_start(list, fmt);
|
|
|
|
vfprintf(usbLog, fmt, list);
|
|
|
|
va_end(list);
|
|
|
|
}
|
|
|
|
|
2011-09-04 11:48:28 +00:00
|
|
|
static void InitLibraryName()
|
|
|
|
{
|
2011-09-04 16:10:00 +00:00
|
|
|
#ifdef USBQEMU_PUBLIC_RELEASE
|
2011-09-04 11:48:28 +00:00
|
|
|
|
|
|
|
// Public Release!
|
|
|
|
// Output a simplified string that's just our name:
|
|
|
|
|
|
|
|
strcpy( libraryName, "USBqemu" );
|
|
|
|
|
|
|
|
#else
|
|
|
|
#ifdef SVN_REV_UNKNOWN
|
|
|
|
|
|
|
|
// Unknown revision.
|
|
|
|
// Output a name that includes devbuild status but not
|
|
|
|
// subversion revision tags:
|
|
|
|
|
|
|
|
strcpy( libraryName, "USBqemu"
|
|
|
|
#ifdef DEBUG_FAST
|
|
|
|
"-Debug"
|
|
|
|
#elif defined( PCSX2_DEBUG )
|
|
|
|
"-Debug/Strict" // strict debugging is slow!
|
|
|
|
#elif defined( PCSX2_DEVBUILD )
|
|
|
|
"-Dev"
|
|
|
|
#else
|
|
|
|
""
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// Use TortoiseSVN's SubWCRev utility's output
|
|
|
|
// to label the specific revision:
|
|
|
|
|
2014-03-23 14:44:43 +00:00
|
|
|
sprintf_s( libraryName, "USBqemu %lld%s"
|
2011-09-04 11:48:28 +00:00
|
|
|
#ifdef DEBUG_FAST
|
|
|
|
"-Debug"
|
|
|
|
#elif defined( PCSX2_DEBUG )
|
|
|
|
"-Debug/Strict" // strict debugging is slow!
|
|
|
|
#elif defined( PCSX2_DEVBUILD )
|
|
|
|
"-Dev"
|
|
|
|
#else
|
|
|
|
""
|
|
|
|
#endif
|
2014-03-23 14:44:43 +00:00
|
|
|
,SVN_REV,SVN_MODS ? "m" : ""
|
2011-09-04 11:48:28 +00:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-04 01:53:30 +00:00
|
|
|
u32 CALLBACK PS2EgetLibType() {
|
|
|
|
return PS2E_LT_USB;
|
|
|
|
}
|
|
|
|
|
2011-09-04 11:48:28 +00:00
|
|
|
char* CALLBACK PS2EgetLibName()
|
|
|
|
{
|
|
|
|
InitLibraryName();
|
2011-09-04 01:53:30 +00:00
|
|
|
return libraryName;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 CALLBACK PS2EgetLibVersion2(u32 type) {
|
|
|
|
return (version<<16) | (revision<<8) | build;
|
|
|
|
}
|
|
|
|
|
2011-09-04 01:36:14 +00:00
|
|
|
void USBirq(int cycles)
|
|
|
|
{
|
|
|
|
USB_LOG("USBirq.\n");
|
|
|
|
|
|
|
|
_USBirq(cycles);
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 CALLBACK USBinit() {
|
|
|
|
LoadConfig();
|
|
|
|
if (conf.Log)
|
|
|
|
{
|
|
|
|
usbLog = fopen("logs/usbLog.txt", "w");
|
|
|
|
setvbuf(usbLog, NULL, _IONBF, 0);
|
2011-09-04 01:53:30 +00:00
|
|
|
USB_LOG("USBqemu plugin version %d,%d\n",revision,build);
|
2011-09-04 01:36:14 +00:00
|
|
|
USB_LOG("USBinit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_ohci = ohci_create(0x1f801600,2);
|
2011-09-04 15:24:23 +00:00
|
|
|
qemu_ohci->rhport[0].port.dev = usb_keyboard_init();
|
|
|
|
qemu_ohci->rhport[0].port.ops->attach(&(qemu_ohci->rhport[0].port));
|
2011-09-04 01:36:14 +00:00
|
|
|
|
2011-09-04 16:10:00 +00:00
|
|
|
clocks = 0;
|
|
|
|
remaining = 0;
|
|
|
|
|
2011-09-04 01:36:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK USBshutdown() {
|
|
|
|
|
2011-09-04 15:24:23 +00:00
|
|
|
USBDevice* device = qemu_ohci->rhport[0].port.dev;
|
|
|
|
|
|
|
|
qemu_ohci->rhport[0].port.ops->detach(&(qemu_ohci->rhport[0].port));
|
|
|
|
device->info->handle_destroy(qemu_ohci->rhport[0].port.dev);
|
2011-09-04 01:36:14 +00:00
|
|
|
|
|
|
|
free(qemu_ohci);
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2011-09-04 01:53:30 +00:00
|
|
|
if(usbLog)
|
|
|
|
fclose(usbLog);
|
2011-09-04 01:36:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 CALLBACK USBopen(void *pDsp) {
|
|
|
|
USB_LOG("USBopen\n");
|
|
|
|
|
|
|
|
HWND hWnd=(HWND)pDsp;
|
|
|
|
|
|
|
|
if (!IsWindow (hWnd) && !IsBadReadPtr ((u32*)hWnd, 4))
|
|
|
|
hWnd = *(HWND*)hWnd;
|
|
|
|
if (!IsWindow (hWnd))
|
|
|
|
hWnd = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (GetWindowLong (hWnd, GWL_STYLE) & WS_CHILD)
|
|
|
|
hWnd = GetParent (hWnd);
|
|
|
|
}
|
2011-09-04 11:48:28 +00:00
|
|
|
gsWindowHandle = hWnd;
|
2011-09-04 01:36:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK USBclose() {
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 CALLBACK USBread8(u32 addr) {
|
|
|
|
USB_LOG("* Invalid 8bit read at address %lx\n", addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 CALLBACK USBread16(u32 addr) {
|
|
|
|
USB_LOG("* Invalid 16bit read at address %lx\n", addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 CALLBACK USBread32(u32 addr) {
|
|
|
|
u32 hard;
|
|
|
|
|
2011-09-04 01:53:30 +00:00
|
|
|
hard=ohci_mem_read(qemu_ohci,addr - qemu_ohci->mem);
|
2011-09-04 01:36:14 +00:00
|
|
|
|
|
|
|
USB_LOG("* Known 32bit read at address %lx: %lx\n", addr, hard);
|
|
|
|
|
|
|
|
return hard;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK USBwrite8(u32 addr, u8 value) {
|
|
|
|
USB_LOG("* Invalid 8bit write at address %lx value %x\n", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK USBwrite16(u32 addr, u16 value) {
|
|
|
|
USB_LOG("* Invalid 16bit write at address %lx value %x\n", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK USBwrite32(u32 addr, u32 value) {
|
|
|
|
USB_LOG("* Known 32bit write at address %lx value %lx\n", addr, value);
|
2011-09-04 01:53:30 +00:00
|
|
|
ohci_mem_write(qemu_ohci,addr - qemu_ohci->mem, value);
|
2011-09-04 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK USBirqCallback(USBcallback callback) {
|
|
|
|
_USBirq = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern u32 bits;
|
|
|
|
|
|
|
|
int CALLBACK _USBirqHandler(void)
|
|
|
|
{
|
|
|
|
//fprintf(stderr," * USB: IRQ Acknowledged.\n");
|
|
|
|
//qemu_ohci->intr_status&=~bits;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
USBhandler CALLBACK USBirqHandler(void) {
|
|
|
|
return (USBhandler)_USBirqHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CALLBACK USBsetRAM(void *mem) {
|
|
|
|
ram = (u8*)mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extended funcs
|
|
|
|
|
2011-09-04 15:24:23 +00:00
|
|
|
char USBfreezeID[] = "USBqemu01";
|
2011-09-04 01:36:14 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2011-09-04 15:24:23 +00:00
|
|
|
char freezeID[10];
|
2011-09-04 16:10:00 +00:00
|
|
|
int cycles;
|
|
|
|
int remaining;
|
2011-09-04 01:36:14 +00:00
|
|
|
OHCIState t;
|
2011-09-04 15:24:23 +00:00
|
|
|
int extraData; // for future expansion with the device state
|
2011-09-04 01:36:14 +00:00
|
|
|
} USBfreezeData;
|
|
|
|
|
|
|
|
s32 CALLBACK USBfreeze(int mode, freezeData *data) {
|
2011-09-04 15:24:23 +00:00
|
|
|
USBfreezeData usbd;
|
2011-09-04 01:36:14 +00:00
|
|
|
|
2011-09-04 15:24:23 +00:00
|
|
|
if (mode == FREEZE_LOAD)
|
|
|
|
{
|
|
|
|
if(data->size < sizeof(USBfreezeData))
|
|
|
|
{
|
|
|
|
SysMessage("ERROR: Unable to load freeze data! Got %d bytes, expected >= %d.", data->size, sizeof(USBfreezeData));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbd = *(USBfreezeData*)data->data;
|
|
|
|
usbd.freezeID[9] = 0;
|
2011-09-04 16:10:00 +00:00
|
|
|
usbd.cycles = clocks;
|
|
|
|
usbd.remaining = remaining;
|
|
|
|
|
2011-09-04 15:24:23 +00:00
|
|
|
if( strcmp(usbd.freezeID, USBfreezeID) != 0)
|
|
|
|
{
|
|
|
|
SysMessage("ERROR: Unable to load freeze data! Found ID '%s', expected ID '%s'.", usbd.freezeID, USBfreezeID);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->size != sizeof(USBfreezeData))
|
|
|
|
return -1;
|
2011-09-04 01:36:14 +00:00
|
|
|
|
2011-09-04 15:24:23 +00:00
|
|
|
for(int i=0; i< qemu_ohci->num_ports; i++)
|
|
|
|
{
|
|
|
|
usbd.t.rhport[i].port.opaque = qemu_ohci;
|
|
|
|
usbd.t.rhport[i].port.ops = qemu_ohci->rhport[i].port.ops;
|
|
|
|
usbd.t.rhport[i].port.dev = qemu_ohci->rhport[i].port.dev; // pointers
|
|
|
|
}
|
|
|
|
*qemu_ohci = usbd.t;
|
|
|
|
|
|
|
|
// WARNING: TODO: Load the state of the attached devices!
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (mode == FREEZE_SAVE)
|
|
|
|
{
|
2011-09-04 01:36:14 +00:00
|
|
|
data->size = sizeof(USBfreezeData);
|
|
|
|
data->data = (s8*)malloc(data->size);
|
2011-09-04 15:24:23 +00:00
|
|
|
if (data->data == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
strcpy(usbd.freezeID, USBfreezeID);
|
|
|
|
usbd.t = *qemu_ohci;
|
|
|
|
for(int i=0; i< qemu_ohci->num_ports; i++)
|
|
|
|
{
|
|
|
|
usbd.t.rhport[i].port.ops = NULL; // pointers
|
|
|
|
usbd.t.rhport[i].port.opaque = NULL; // pointers
|
|
|
|
usbd.t.rhport[i].port.dev = NULL; // pointers
|
|
|
|
}
|
|
|
|
|
2011-09-04 16:10:00 +00:00
|
|
|
clocks = usbd.cycles;
|
|
|
|
remaining = usbd.remaining;
|
|
|
|
|
2011-09-04 15:24:23 +00:00
|
|
|
// WARNING: TODO: Save the state of the attached devices!
|
2011-09-04 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-04 01:53:30 +00:00
|
|
|
void CALLBACK USBasync(u32 _cycles)
|
2011-09-04 01:36:14 +00:00
|
|
|
{
|
2011-09-04 01:53:30 +00:00
|
|
|
remaining += _cycles;
|
|
|
|
clocks += remaining;
|
2011-09-04 01:36:14 +00:00
|
|
|
if(qemu_ohci->eof_timer>0)
|
|
|
|
{
|
2011-09-04 01:53:30 +00:00
|
|
|
while(remaining>=qemu_ohci->eof_timer)
|
2011-09-04 01:36:14 +00:00
|
|
|
{
|
2011-09-04 01:53:30 +00:00
|
|
|
remaining-=qemu_ohci->eof_timer;
|
2011-09-04 01:36:14 +00:00
|
|
|
qemu_ohci->eof_timer=0;
|
|
|
|
ohci_frame_boundary(qemu_ohci);
|
|
|
|
}
|
2011-09-04 01:53:30 +00:00
|
|
|
if((remaining>0)&&(qemu_ohci->eof_timer>0))
|
|
|
|
{
|
|
|
|
s64 m = qemu_ohci->eof_timer;
|
|
|
|
if(remaining < m)
|
|
|
|
m = remaining;
|
|
|
|
qemu_ohci->eof_timer -= m;
|
|
|
|
remaining -= m;
|
|
|
|
}
|
2011-09-04 01:36:14 +00:00
|
|
|
}
|
2011-09-04 01:53:30 +00:00
|
|
|
//if(qemu_ohci->eof_timer <= 0)
|
|
|
|
//{
|
|
|
|
// ohci_frame_boundary(qemu_ohci);
|
|
|
|
//}
|
2011-09-04 01:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s32 CALLBACK USBtest() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_physical_memory_rw(u32 addr, u8 *buf,
|
|
|
|
int len, int is_write)
|
|
|
|
{
|
|
|
|
if(is_write)
|
|
|
|
memcpy(&(ram[addr]),buf,len);
|
|
|
|
else
|
|
|
|
memcpy(buf,&(ram[addr]),len);
|
|
|
|
}
|
|
|
|
|
|
|
|
s64 get_clock()
|
|
|
|
{
|
|
|
|
return clocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *qemu_mallocz(uint32_t size)
|
|
|
|
{
|
|
|
|
void *m=malloc(size);
|
|
|
|
if(!m) return NULL;
|
|
|
|
memset(m,0,size);
|
|
|
|
return m;
|
|
|
|
}
|